VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/SELM.cpp@ 58611

Last change on this file since 58611 was 58123, checked in by vboxsync, 9 years ago

VMM: Made @param pVCpu more uniform and to the point.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 106.7 KB
Line 
1/* $Id: SELM.cpp 58123 2015-10-08 18:09:45Z vboxsync $ */
2/** @file
3 * SELM - The Selector Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @page pg_selm SELM - The Selector Manager
19 *
20 * SELM takes care of GDT, LDT and TSS shadowing in raw-mode, and the injection
21 * of a few hyper selector for the raw-mode context. In the hardware assisted
22 * virtualization mode its only task is to decode entries in the guest GDT or
23 * LDT once in a while.
24 *
25 * @see grp_selm
26 *
27 *
28 * @section seg_selm_shadowing Shadowing
29 *
30 * SELMR3UpdateFromCPUM() and SELMR3SyncTSS() does the bulk synchronization
31 * work. The three structures (GDT, LDT, TSS) are all shadowed wholesale atm.
32 * The idea is to do it in a more on-demand fashion when we get time. There
33 * also a whole bunch of issues with the current synchronization of all three
34 * tables, see notes and todos in the code.
35 *
36 * When the guest makes changes to the GDT we will try update the shadow copy
37 * without involving SELMR3UpdateFromCPUM(), see selmGCSyncGDTEntry().
38 *
39 * When the guest make LDT changes we'll trigger a full resync of the LDT
40 * (SELMR3UpdateFromCPUM()), which, needless to say, isn't optimal.
41 *
42 * The TSS shadowing is limited to the fields we need to care about, namely SS0
43 * and ESP0. The Patch Manager makes use of these. We monitor updates to the
44 * guest TSS and will try keep our SS0 and ESP0 copies up to date this way
45 * rather than go the SELMR3SyncTSS() route.
46 *
47 * When in raw-mode SELM also injects a few extra GDT selectors which are used
48 * by the raw-mode (hyper) context. These start their life at the high end of
49 * the table and will be relocated when the guest tries to make use of them...
50 * Well, that was that idea at least, only the code isn't quite there yet which
51 * is why we have trouble with guests which actually have a full sized GDT.
52 *
53 * So, the summary of the current GDT, LDT and TSS shadowing is that there is a
54 * lot of relatively simple and enjoyable work to be done, see @bugref{3267}.
55 *
56 */
57
58
59/*********************************************************************************************************************************
60* Header Files *
61*********************************************************************************************************************************/
62#define LOG_GROUP LOG_GROUP_SELM
63#include <VBox/vmm/selm.h>
64#include <VBox/vmm/cpum.h>
65#include <VBox/vmm/stam.h>
66#include <VBox/vmm/em.h>
67#include <VBox/vmm/hm.h>
68#include <VBox/vmm/mm.h>
69#include <VBox/vmm/ssm.h>
70#include <VBox/vmm/pgm.h>
71#include <VBox/vmm/trpm.h>
72#include <VBox/vmm/dbgf.h>
73#include "SELMInternal.h"
74#include <VBox/vmm/vm.h>
75#include <VBox/err.h>
76#include <VBox/param.h>
77
78#include <iprt/assert.h>
79#include <VBox/log.h>
80#include <iprt/asm.h>
81#include <iprt/string.h>
82#include <iprt/thread.h>
83#include <iprt/string.h>
84
85#include "SELMInline.h"
86
87
88/** SELM saved state version. */
89#define SELM_SAVED_STATE_VERSION 5
90
91
92/*********************************************************************************************************************************
93* Internal Functions *
94*********************************************************************************************************************************/
95static DECLCALLBACK(int) selmR3Save(PVM pVM, PSSMHANDLE pSSM);
96static DECLCALLBACK(int) selmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
97static DECLCALLBACK(int) selmR3LoadDone(PVM pVM, PSSMHANDLE pSSM);
98static DECLCALLBACK(void) selmR3InfoGdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
99static DECLCALLBACK(void) selmR3InfoGdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
100static DECLCALLBACK(void) selmR3InfoLdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
101static DECLCALLBACK(void) selmR3InfoLdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
102//static DECLCALLBACK(void) selmR3InfoTss(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
103//static DECLCALLBACK(void) selmR3InfoTssGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
104
105
106/*********************************************************************************************************************************
107* Global Variables *
108*********************************************************************************************************************************/
109#ifdef LOG_ENABLED
110/** Segment register names. */
111static char const g_aszSRegNms[X86_SREG_COUNT][4] = { "ES", "CS", "SS", "DS", "FS", "GS" };
112#endif
113
114
115/**
116 * Initializes the SELM.
117 *
118 * @returns VBox status code.
119 * @param pVM The cross context VM structure.
120 */
121VMMR3DECL(int) SELMR3Init(PVM pVM)
122{
123 int rc;
124 LogFlow(("SELMR3Init\n"));
125
126 /*
127 * Assert alignment and sizes.
128 * (The TSS block requires contiguous back.)
129 */
130 AssertCompile(sizeof(pVM->selm.s) <= sizeof(pVM->selm.padding)); AssertRelease(sizeof(pVM->selm.s) <= sizeof(pVM->selm.padding));
131 AssertCompileMemberAlignment(VM, selm.s, 32); AssertRelease(!(RT_OFFSETOF(VM, selm.s) & 31));
132#if 0 /* doesn't work */
133 AssertCompile((RT_OFFSETOF(VM, selm.s.Tss) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.Tss));
134 AssertCompile((RT_OFFSETOF(VM, selm.s.TssTrap08) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.TssTrap08));
135#endif
136 AssertRelease((RT_OFFSETOF(VM, selm.s.Tss) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.Tss));
137 AssertRelease((RT_OFFSETOF(VM, selm.s.TssTrap08) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.TssTrap08));
138 AssertRelease(sizeof(pVM->selm.s.Tss.IntRedirBitmap) == 0x20);
139
140 /*
141 * Init the structure.
142 */
143 pVM->selm.s.offVM = RT_OFFSETOF(VM, selm);
144 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] = (SELM_GDT_ELEMENTS - 0x1) << 3;
145 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] = (SELM_GDT_ELEMENTS - 0x2) << 3;
146 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] = (SELM_GDT_ELEMENTS - 0x3) << 3;
147 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] = (SELM_GDT_ELEMENTS - 0x4) << 3;
148 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = (SELM_GDT_ELEMENTS - 0x5) << 3;
149
150 if (HMIsRawModeCtxNeeded(pVM))
151 {
152 /*
153 * Allocate GDT table.
154 */
155 rc = MMR3HyperAllocOnceNoRel(pVM, sizeof(pVM->selm.s.paGdtR3[0]) * SELM_GDT_ELEMENTS,
156 PAGE_SIZE, MM_TAG_SELM, (void **)&pVM->selm.s.paGdtR3);
157 AssertRCReturn(rc, rc);
158
159 /*
160 * Allocate LDT area.
161 */
162 rc = MMR3HyperAllocOnceNoRel(pVM, _64K + PAGE_SIZE, PAGE_SIZE, MM_TAG_SELM, &pVM->selm.s.pvLdtR3);
163 AssertRCReturn(rc, rc);
164 }
165
166 /*
167 * Init Guest's and Shadow GDT, LDT, TSS changes control variables.
168 */
169 pVM->selm.s.cbEffGuestGdtLimit = 0;
170 pVM->selm.s.GuestGdtr.pGdt = RTRCPTR_MAX;
171 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
172 pVM->selm.s.GCPtrGuestTss = RTRCPTR_MAX;
173
174 pVM->selm.s.paGdtRC = NIL_RTRCPTR; /* Must be set in SELMR3Relocate because of monitoring. */
175 pVM->selm.s.pvLdtRC = RTRCPTR_MAX;
176 pVM->selm.s.pvMonShwTssRC = RTRCPTR_MAX;
177 pVM->selm.s.GCSelTss = RTSEL_MAX;
178
179 pVM->selm.s.fSyncTSSRing0Stack = false;
180
181 /* The I/O bitmap starts right after the virtual interrupt redirection
182 bitmap. Outside the TSS on purpose; the CPU will not check it for
183 I/O operations. */
184 pVM->selm.s.Tss.offIoBitmap = sizeof(VBOXTSS);
185 /* bit set to 1 means no redirection */
186 memset(pVM->selm.s.Tss.IntRedirBitmap, 0xff, sizeof(pVM->selm.s.Tss.IntRedirBitmap));
187
188 /*
189 * Register the virtual access handlers.
190 */
191 pVM->selm.s.hShadowGdtWriteHandlerType = NIL_PGMVIRTHANDLERTYPE;
192 pVM->selm.s.hShadowLdtWriteHandlerType = NIL_PGMVIRTHANDLERTYPE;
193 pVM->selm.s.hShadowTssWriteHandlerType = NIL_PGMVIRTHANDLERTYPE;
194 pVM->selm.s.hGuestGdtWriteHandlerType = NIL_PGMVIRTHANDLERTYPE;
195 pVM->selm.s.hGuestLdtWriteHandlerType = NIL_PGMVIRTHANDLERTYPE;
196 pVM->selm.s.hGuestTssWriteHandlerType = NIL_PGMVIRTHANDLERTYPE;
197#ifdef VBOX_WITH_RAW_MODE
198 if (!HMIsEnabled(pVM))
199 {
200# ifdef SELM_TRACK_SHADOW_GDT_CHANGES
201 rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_HYPERVISOR, false /*fRelocUserRC*/,
202 NULL /*pfnInvalidateR3*/, NULL /*pfnHandlerR3*/,
203 NULL /*pszHandlerRC*/, "selmRCShadowGDTWritePfHandler",
204 "Shadow GDT write access handler", &pVM->selm.s.hShadowGdtWriteHandlerType);
205 AssertRCReturn(rc, rc);
206# endif
207# ifdef SELM_TRACK_SHADOW_TSS_CHANGES
208 rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_HYPERVISOR, false /*fRelocUserRC*/,
209 NULL /*pfnInvalidateR3*/, NULL /*pfnHandlerR3*/,
210 NULL /*pszHandlerRC*/, "selmRCShadowTSSWritePfHandler",
211 "Shadow TSS write access handler", &pVM->selm.s.hShadowTssWriteHandlerType);
212 AssertRCReturn(rc, rc);
213# endif
214# ifdef SELM_TRACK_SHADOW_LDT_CHANGES
215 rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_HYPERVISOR, false /*fRelocUserRC*/,
216 NULL /*pfnInvalidateR3*/, NULL /*pfnHandlerR3*/,
217 NULL /*pszHandlerRC*/, "selmRCShadowLDTWritePfHandler",
218 "Shadow LDT write access handler", &pVM->selm.s.hShadowLdtWriteHandlerType);
219 AssertRCReturn(rc, rc);
220# endif
221 rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_WRITE, false /*fRelocUserRC*/,
222 NULL /*pfnInvalidateR3*/, selmGuestGDTWriteHandler,
223 "selmGuestGDTWriteHandler", "selmRCGuestGDTWritePfHandler",
224 "Guest GDT write access handler", &pVM->selm.s.hGuestGdtWriteHandlerType);
225 AssertRCReturn(rc, rc);
226 rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_WRITE, false /*fRelocUserRC*/,
227 NULL /*pfnInvalidateR3*/, selmGuestLDTWriteHandler,
228 "selmGuestLDTWriteHandler", "selmRCGuestLDTWritePfHandler",
229 "Guest LDT write access handler", &pVM->selm.s.hGuestLdtWriteHandlerType);
230 AssertRCReturn(rc, rc);
231 rc = PGMR3HandlerVirtualTypeRegister(pVM, PGMVIRTHANDLERKIND_WRITE, false /*fRelocUserRC*/,
232 NULL /*pfnInvalidateR3*/, selmGuestTSSWriteHandler,
233 "selmGuestTSSWriteHandler", "selmRCGuestTSSWritePfHandler",
234 "Guest TSS write access handler", &pVM->selm.s.hGuestTssWriteHandlerType);
235 AssertRCReturn(rc, rc);
236 }
237#endif /* VBOX_WITH_RAW_MODE */
238
239 /*
240 * Register the saved state data unit.
241 */
242 rc = SSMR3RegisterInternal(pVM, "selm", 1, SELM_SAVED_STATE_VERSION, sizeof(SELM),
243 NULL, NULL, NULL,
244 NULL, selmR3Save, NULL,
245 NULL, selmR3Load, selmR3LoadDone);
246 if (RT_FAILURE(rc))
247 return rc;
248
249 /*
250 * Statistics.
251 */
252 if (!HMIsEnabled(pVM))
253 {
254 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestGDTHandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/GDTInt", STAMUNIT_OCCURENCES, "The number of handled writes to the Guest GDT.");
255 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestGDTUnhandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/GDTEmu", STAMUNIT_OCCURENCES, "The number of unhandled writes to the Guest GDT.");
256 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestLDT, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/LDT", STAMUNIT_OCCURENCES, "The number of writes to the Guest LDT was detected.");
257 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestTSSHandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSInt", STAMUNIT_OCCURENCES, "The number of handled writes to the Guest TSS.");
258 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestTSSRedir, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSRedir",STAMUNIT_OCCURENCES, "The number of handled redir bitmap writes to the Guest TSS.");
259 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestTSSHandledChanged,STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSIntChg", STAMUNIT_OCCURENCES, "The number of handled writes to the Guest TSS where the R0 stack changed.");
260 STAM_REG(pVM, &pVM->selm.s.StatRCWriteGuestTSSUnhandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSEmu", STAMUNIT_OCCURENCES, "The number of unhandled writes to the Guest TSS.");
261 STAM_REG(pVM, &pVM->selm.s.StatTSSSync, STAMTYPE_PROFILE, "/PROF/SELM/TSSSync", STAMUNIT_TICKS_PER_CALL, "Profiling of the SELMR3SyncTSS() body.");
262 STAM_REG(pVM, &pVM->selm.s.StatUpdateFromCPUM, STAMTYPE_PROFILE, "/PROF/SELM/UpdateFromCPUM", STAMUNIT_TICKS_PER_CALL, "Profiling of the SELMR3UpdateFromCPUM() body.");
263
264 STAM_REL_REG(pVM, &pVM->selm.s.StatHyperSelsChanged, STAMTYPE_COUNTER, "/SELM/HyperSels/Changed", STAMUNIT_OCCURENCES, "The number of times we had to relocate our hypervisor selectors.");
265 STAM_REL_REG(pVM, &pVM->selm.s.StatScanForHyperSels, STAMTYPE_COUNTER, "/SELM/HyperSels/Scan", STAMUNIT_OCCURENCES, "The number of times we had find free hypervisor selectors.");
266
267 STAM_REL_REG(pVM, &pVM->selm.s.aStatDetectedStaleSReg[X86_SREG_ES], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/DetectedStaleES", STAMUNIT_OCCURENCES, "Stale ES was detected in UpdateFromCPUM.");
268 STAM_REL_REG(pVM, &pVM->selm.s.aStatDetectedStaleSReg[X86_SREG_CS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/DetectedStaleCS", STAMUNIT_OCCURENCES, "Stale CS was detected in UpdateFromCPUM.");
269 STAM_REL_REG(pVM, &pVM->selm.s.aStatDetectedStaleSReg[X86_SREG_SS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/DetectedStaleSS", STAMUNIT_OCCURENCES, "Stale SS was detected in UpdateFromCPUM.");
270 STAM_REL_REG(pVM, &pVM->selm.s.aStatDetectedStaleSReg[X86_SREG_DS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/DetectedStaleDS", STAMUNIT_OCCURENCES, "Stale DS was detected in UpdateFromCPUM.");
271 STAM_REL_REG(pVM, &pVM->selm.s.aStatDetectedStaleSReg[X86_SREG_FS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/DetectedStaleFS", STAMUNIT_OCCURENCES, "Stale FS was detected in UpdateFromCPUM.");
272 STAM_REL_REG(pVM, &pVM->selm.s.aStatDetectedStaleSReg[X86_SREG_GS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/DetectedStaleGS", STAMUNIT_OCCURENCES, "Stale GS was detected in UpdateFromCPUM.");
273
274 STAM_REL_REG(pVM, &pVM->selm.s.aStatAlreadyStaleSReg[X86_SREG_ES], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/AlreadyStaleES", STAMUNIT_OCCURENCES, "Already stale ES in UpdateFromCPUM.");
275 STAM_REL_REG(pVM, &pVM->selm.s.aStatAlreadyStaleSReg[X86_SREG_CS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/AlreadyStaleCS", STAMUNIT_OCCURENCES, "Already stale CS in UpdateFromCPUM.");
276 STAM_REL_REG(pVM, &pVM->selm.s.aStatAlreadyStaleSReg[X86_SREG_SS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/AlreadyStaleSS", STAMUNIT_OCCURENCES, "Already stale SS in UpdateFromCPUM.");
277 STAM_REL_REG(pVM, &pVM->selm.s.aStatAlreadyStaleSReg[X86_SREG_DS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/AlreadyStaleDS", STAMUNIT_OCCURENCES, "Already stale DS in UpdateFromCPUM.");
278 STAM_REL_REG(pVM, &pVM->selm.s.aStatAlreadyStaleSReg[X86_SREG_FS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/AlreadyStaleFS", STAMUNIT_OCCURENCES, "Already stale FS in UpdateFromCPUM.");
279 STAM_REL_REG(pVM, &pVM->selm.s.aStatAlreadyStaleSReg[X86_SREG_GS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/AlreadyStaleGS", STAMUNIT_OCCURENCES, "Already stale GS in UpdateFromCPUM.");
280
281 STAM_REL_REG(pVM, &pVM->selm.s.StatStaleToUnstaleSReg, STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/StaleToUnstale", STAMUNIT_OCCURENCES, "Transitions from stale to unstale UpdateFromCPUM.");
282
283 STAM_REG( pVM, &pVM->selm.s.aStatUpdatedSReg[X86_SREG_ES], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/UpdatedES", STAMUNIT_OCCURENCES, "Updated hidden ES values in UpdateFromCPUM.");
284 STAM_REG( pVM, &pVM->selm.s.aStatUpdatedSReg[X86_SREG_CS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/UpdatedCS", STAMUNIT_OCCURENCES, "Updated hidden CS values in UpdateFromCPUM.");
285 STAM_REG( pVM, &pVM->selm.s.aStatUpdatedSReg[X86_SREG_SS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/UpdatedSS", STAMUNIT_OCCURENCES, "Updated hidden SS values in UpdateFromCPUM.");
286 STAM_REG( pVM, &pVM->selm.s.aStatUpdatedSReg[X86_SREG_DS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/UpdatedDS", STAMUNIT_OCCURENCES, "Updated hidden DS values in UpdateFromCPUM.");
287 STAM_REG( pVM, &pVM->selm.s.aStatUpdatedSReg[X86_SREG_FS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/UpdatedFS", STAMUNIT_OCCURENCES, "Updated hidden FS values in UpdateFromCPUM.");
288 STAM_REG( pVM, &pVM->selm.s.aStatUpdatedSReg[X86_SREG_GS], STAMTYPE_COUNTER, "/SELM/UpdateFromCPUM/UpdatedGS", STAMUNIT_OCCURENCES, "Updated hidden GS values in UpdateFromCPUM.");
289 }
290
291 STAM_REG( pVM, &pVM->selm.s.StatLoadHidSelGst, STAMTYPE_COUNTER, "/SELM/LoadHidSel/LoadedGuest", STAMUNIT_OCCURENCES, "SELMLoadHiddenSelectorReg: Loaded from guest tables.");
292 STAM_REG( pVM, &pVM->selm.s.StatLoadHidSelShw, STAMTYPE_COUNTER, "/SELM/LoadHidSel/LoadedShadow", STAMUNIT_OCCURENCES, "SELMLoadHiddenSelectorReg: Loaded from shadow tables.");
293 STAM_REL_REG(pVM, &pVM->selm.s.StatLoadHidSelReadErrors, STAMTYPE_COUNTER, "/SELM/LoadHidSel/GstReadErrors", STAMUNIT_OCCURENCES, "SELMLoadHiddenSelectorReg: Guest table read errors.");
294 STAM_REL_REG(pVM, &pVM->selm.s.StatLoadHidSelGstNoGood, STAMTYPE_COUNTER, "/SELM/LoadHidSel/NoGoodGuest", STAMUNIT_OCCURENCES, "SELMLoadHiddenSelectorReg: No good guest table entry.");
295
296#ifdef VBOX_WITH_RAW_MODE
297 /*
298 * Default action when entering raw mode for the first time
299 */
300 if (!HMIsEnabled(pVM))
301 {
302 PVMCPU pVCpu = &pVM->aCpus[0]; /* raw mode implies on VCPU */
303 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
304 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
305 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
306 }
307#endif
308
309 /*
310 * Register info handlers.
311 */
312 if (HMIsRawModeCtxNeeded(pVM))
313 {
314 DBGFR3InfoRegisterInternal(pVM, "gdt", "Displays the shadow GDT. No arguments.", &selmR3InfoGdt);
315 DBGFR3InfoRegisterInternal(pVM, "ldt", "Displays the shadow LDT. No arguments.", &selmR3InfoLdt);
316 //DBGFR3InfoRegisterInternal(pVM, "tss", "Displays the shadow TSS. No arguments.", &selmR3InfoTss);
317 }
318 DBGFR3InfoRegisterInternal(pVM, "gdtguest", "Displays the guest GDT. No arguments.", &selmR3InfoGdtGuest);
319 DBGFR3InfoRegisterInternal(pVM, "ldtguest", "Displays the guest LDT. No arguments.", &selmR3InfoLdtGuest);
320 //DBGFR3InfoRegisterInternal(pVM, "tssguest", "Displays the guest TSS. No arguments.", &selmR3InfoTssGuest);
321
322 return rc;
323}
324
325
326/**
327 * Finalizes HMA page attributes.
328 *
329 * @returns VBox status code.
330 * @param pVM The cross context VM structure.
331 */
332VMMR3DECL(int) SELMR3InitFinalize(PVM pVM)
333{
334#ifdef VBOX_WITH_RAW_MODE
335 /** @cfgm{/DoubleFault,bool,false}
336 * Enables catching of double faults in the raw-mode context VMM code. This can
337 * be used when the triple faults or hangs occur and one suspect an unhandled
338 * double fault. This is not enabled by default because it means making the
339 * hyper selectors writeable for all supervisor code, including the guest's.
340 * The double fault is a task switch and thus requires write access to the GDT
341 * of the TSS (to set it busy), to the old TSS (to store state), and to the Trap
342 * 8 TSS for the back link.
343 */
344 bool f;
345# if defined(DEBUG_bird)
346 int rc = CFGMR3QueryBoolDef(CFGMR3GetRoot(pVM), "DoubleFault", &f, true);
347# else
348 int rc = CFGMR3QueryBoolDef(CFGMR3GetRoot(pVM), "DoubleFault", &f, false);
349# endif
350 AssertLogRelRCReturn(rc, rc);
351 if (f && HMIsRawModeCtxNeeded(pVM))
352 {
353 PX86DESC paGdt = pVM->selm.s.paGdtR3;
354 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> 3]), sizeof(paGdt[0]),
355 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
356 AssertRC(rc);
357 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> 3]), sizeof(paGdt[0]),
358 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
359 AssertRC(rc);
360 rc = PGMMapSetPage(pVM, VM_RC_ADDR(pVM, &pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]), sizeof(pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]),
361 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
362 AssertRC(rc);
363 rc = PGMMapSetPage(pVM, VM_RC_ADDR(pVM, &pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08]), sizeof(pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08]),
364 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
365 AssertRC(rc);
366 }
367#endif /* VBOX_WITH_RAW_MODE */
368 return VINF_SUCCESS;
369}
370
371
372/**
373 * Setup the hypervisor GDT selectors in our shadow table
374 *
375 * @param pVM The cross context VM structure.
376 */
377static void selmR3SetupHyperGDTSelectors(PVM pVM)
378{
379 PX86DESC paGdt = pVM->selm.s.paGdtR3;
380
381 /*
382 * Set up global code and data descriptors for use in the guest context.
383 * Both are wide open (base 0, limit 4GB)
384 */
385 PX86DESC pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] >> 3];
386 pDesc->Gen.u16LimitLow = 0xffff;
387 pDesc->Gen.u4LimitHigh = 0xf;
388 pDesc->Gen.u16BaseLow = 0;
389 pDesc->Gen.u8BaseHigh1 = 0;
390 pDesc->Gen.u8BaseHigh2 = 0;
391 pDesc->Gen.u4Type = X86_SEL_TYPE_ER_ACC;
392 pDesc->Gen.u1DescType = 1; /* not system, but code/data */
393 pDesc->Gen.u2Dpl = 0; /* supervisor */
394 pDesc->Gen.u1Present = 1;
395 pDesc->Gen.u1Available = 0;
396 pDesc->Gen.u1Long = 0;
397 pDesc->Gen.u1DefBig = 1; /* def 32 bit */
398 pDesc->Gen.u1Granularity = 1; /* 4KB limit */
399
400 /* data */
401 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] >> 3];
402 pDesc->Gen.u16LimitLow = 0xffff;
403 pDesc->Gen.u4LimitHigh = 0xf;
404 pDesc->Gen.u16BaseLow = 0;
405 pDesc->Gen.u8BaseHigh1 = 0;
406 pDesc->Gen.u8BaseHigh2 = 0;
407 pDesc->Gen.u4Type = X86_SEL_TYPE_RW_ACC;
408 pDesc->Gen.u1DescType = 1; /* not system, but code/data */
409 pDesc->Gen.u2Dpl = 0; /* supervisor */
410 pDesc->Gen.u1Present = 1;
411 pDesc->Gen.u1Available = 0;
412 pDesc->Gen.u1Long = 0;
413 pDesc->Gen.u1DefBig = 1; /* big */
414 pDesc->Gen.u1Granularity = 1; /* 4KB limit */
415
416 /* 64-bit mode code (& data?) */
417 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] >> 3];
418 pDesc->Gen.u16LimitLow = 0xffff;
419 pDesc->Gen.u4LimitHigh = 0xf;
420 pDesc->Gen.u16BaseLow = 0;
421 pDesc->Gen.u8BaseHigh1 = 0;
422 pDesc->Gen.u8BaseHigh2 = 0;
423 pDesc->Gen.u4Type = X86_SEL_TYPE_ER_ACC;
424 pDesc->Gen.u1DescType = 1; /* not system, but code/data */
425 pDesc->Gen.u2Dpl = 0; /* supervisor */
426 pDesc->Gen.u1Present = 1;
427 pDesc->Gen.u1Available = 0;
428 pDesc->Gen.u1Long = 1; /* The Long (L) attribute bit. */
429 pDesc->Gen.u1DefBig = 0; /* With L=1 this must be 0. */
430 pDesc->Gen.u1Granularity = 1; /* 4KB limit */
431
432 /*
433 * TSS descriptor
434 */
435 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> 3];
436 RTRCPTR RCPtrTSS = VM_RC_ADDR(pVM, &pVM->selm.s.Tss);
437 pDesc->Gen.u16BaseLow = RT_LOWORD(RCPtrTSS);
438 pDesc->Gen.u8BaseHigh1 = RT_BYTE3(RCPtrTSS);
439 pDesc->Gen.u8BaseHigh2 = RT_BYTE4(RCPtrTSS);
440 pDesc->Gen.u16LimitLow = sizeof(VBOXTSS) - 1;
441 pDesc->Gen.u4LimitHigh = 0;
442 pDesc->Gen.u4Type = X86_SEL_TYPE_SYS_386_TSS_AVAIL;
443 pDesc->Gen.u1DescType = 0; /* system */
444 pDesc->Gen.u2Dpl = 0; /* supervisor */
445 pDesc->Gen.u1Present = 1;
446 pDesc->Gen.u1Available = 0;
447 pDesc->Gen.u1Long = 0;
448 pDesc->Gen.u1DefBig = 0;
449 pDesc->Gen.u1Granularity = 0; /* byte limit */
450
451 /*
452 * TSS descriptor for trap 08
453 */
454 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> 3];
455 pDesc->Gen.u16LimitLow = sizeof(VBOXTSS) - 1;
456 pDesc->Gen.u4LimitHigh = 0;
457 RCPtrTSS = VM_RC_ADDR(pVM, &pVM->selm.s.TssTrap08);
458 pDesc->Gen.u16BaseLow = RT_LOWORD(RCPtrTSS);
459 pDesc->Gen.u8BaseHigh1 = RT_BYTE3(RCPtrTSS);
460 pDesc->Gen.u8BaseHigh2 = RT_BYTE4(RCPtrTSS);
461 pDesc->Gen.u4Type = X86_SEL_TYPE_SYS_386_TSS_AVAIL;
462 pDesc->Gen.u1DescType = 0; /* system */
463 pDesc->Gen.u2Dpl = 0; /* supervisor */
464 pDesc->Gen.u1Present = 1;
465 pDesc->Gen.u1Available = 0;
466 pDesc->Gen.u1Long = 0;
467 pDesc->Gen.u1DefBig = 0;
468 pDesc->Gen.u1Granularity = 0; /* byte limit */
469}
470
471/**
472 * Applies relocations to data and code managed by this
473 * component. This function will be called at init and
474 * whenever the VMM need to relocate it self inside the GC.
475 *
476 * @param pVM The cross context VM structure.
477 */
478VMMR3DECL(void) SELMR3Relocate(PVM pVM)
479{
480 PX86DESC paGdt = pVM->selm.s.paGdtR3;
481 LogFlow(("SELMR3Relocate\n"));
482
483 if (HMIsRawModeCtxNeeded(pVM))
484 {
485 for (VMCPUID i = 0; i < pVM->cCpus; i++)
486 {
487 PVMCPU pVCpu = &pVM->aCpus[i];
488
489 /*
490 * Update GDTR and selector.
491 */
492 CPUMSetHyperGDTR(pVCpu, MMHyperR3ToRC(pVM, paGdt), SELM_GDT_ELEMENTS * sizeof(paGdt[0]) - 1);
493
494 /** @todo selector relocations should be a separate operation? */
495 CPUMSetHyperCS(pVCpu, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS]);
496 CPUMSetHyperDS(pVCpu, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]);
497 CPUMSetHyperES(pVCpu, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]);
498 CPUMSetHyperSS(pVCpu, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]);
499 CPUMSetHyperTR(pVCpu, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]);
500 }
501
502 selmR3SetupHyperGDTSelectors(pVM);
503
504/** @todo SELM must be called when any of the CR3s changes during a cpu mode change. */
505/** @todo PGM knows the proper CR3 values these days, not CPUM. */
506 /*
507 * Update the TSSes.
508 */
509 /* Only applies to raw mode which supports only 1 VCPU */
510 PVMCPU pVCpu = &pVM->aCpus[0];
511
512 /* Current TSS */
513 pVM->selm.s.Tss.cr3 = PGMGetHyperCR3(pVCpu);
514 pVM->selm.s.Tss.ss0 = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
515 pVM->selm.s.Tss.esp0 = VMMGetStackRC(pVCpu);
516 pVM->selm.s.Tss.cs = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS];
517 pVM->selm.s.Tss.ds = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
518 pVM->selm.s.Tss.es = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
519 pVM->selm.s.Tss.offIoBitmap = sizeof(VBOXTSS);
520
521 /* trap 08 */
522 pVM->selm.s.TssTrap08.cr3 = PGMGetInterRCCR3(pVM, pVCpu); /* this should give use better survival chances. */
523 pVM->selm.s.TssTrap08.ss0 = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
524 pVM->selm.s.TssTrap08.ss = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
525 pVM->selm.s.TssTrap08.esp0 = VMMGetStackRC(pVCpu) - PAGE_SIZE / 2; /* upper half can be analysed this way. */
526 pVM->selm.s.TssTrap08.esp = pVM->selm.s.TssTrap08.esp0;
527 pVM->selm.s.TssTrap08.ebp = pVM->selm.s.TssTrap08.esp0;
528 pVM->selm.s.TssTrap08.cs = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS];
529 pVM->selm.s.TssTrap08.ds = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
530 pVM->selm.s.TssTrap08.es = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
531 pVM->selm.s.TssTrap08.fs = 0;
532 pVM->selm.s.TssTrap08.gs = 0;
533 pVM->selm.s.TssTrap08.selLdt = 0;
534 pVM->selm.s.TssTrap08.eflags = 0x2; /* all cleared */
535 pVM->selm.s.TssTrap08.ecx = VM_RC_ADDR(pVM, &pVM->selm.s.Tss); /* setup ecx to normal Hypervisor TSS address. */
536 pVM->selm.s.TssTrap08.edi = pVM->selm.s.TssTrap08.ecx;
537 pVM->selm.s.TssTrap08.eax = pVM->selm.s.TssTrap08.ecx;
538 pVM->selm.s.TssTrap08.edx = VM_RC_ADDR(pVM, pVM); /* setup edx VM address. */
539 pVM->selm.s.TssTrap08.edi = pVM->selm.s.TssTrap08.edx;
540 pVM->selm.s.TssTrap08.ebx = pVM->selm.s.TssTrap08.edx;
541 pVM->selm.s.TssTrap08.offIoBitmap = sizeof(VBOXTSS);
542 /* TRPM will be updating the eip */
543 }
544
545 if (!HMIsEnabled(pVM))
546 {
547 /*
548 * Update shadow GDT/LDT/TSS write access handlers.
549 */
550 PVMCPU pVCpu = VMMGetCpu(pVM); NOREF(pVCpu);
551 int rc; NOREF(rc);
552#ifdef SELM_TRACK_SHADOW_GDT_CHANGES
553 if (pVM->selm.s.paGdtRC != NIL_RTRCPTR)
554 {
555 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.paGdtRC, true /*fHypervisor*/);
556 AssertRC(rc);
557 }
558 pVM->selm.s.paGdtRC = MMHyperR3ToRC(pVM, paGdt);
559 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hShadowGdtWriteHandlerType,
560 pVM->selm.s.paGdtRC,
561 pVM->selm.s.paGdtRC + SELM_GDT_ELEMENTS * sizeof(paGdt[0]) - 1,
562 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
563 AssertRC(rc);
564#endif
565#ifdef SELM_TRACK_SHADOW_TSS_CHANGES
566 if (pVM->selm.s.pvMonShwTssRC != RTRCPTR_MAX)
567 {
568 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.pvMonShwTssRC, true /*fHypervisor*/);
569 AssertRC(rc);
570 }
571 pVM->selm.s.pvMonShwTssRC = VM_RC_ADDR(pVM, &pVM->selm.s.Tss);
572 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hShadowTssWriteHandlerType,
573 pVM->selm.s.pvMonShwTssRC,
574 pVM->selm.s.pvMonShwTssRC + sizeof(pVM->selm.s.Tss) - 1,
575 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
576 AssertRC(rc);
577#endif
578
579 /*
580 * Update the GC LDT region handler and address.
581 */
582#ifdef SELM_TRACK_SHADOW_LDT_CHANGES
583 if (pVM->selm.s.pvLdtRC != RTRCPTR_MAX)
584 {
585 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.pvLdtRC, true /*fHypervisor*/);
586 AssertRC(rc);
587 }
588#endif
589 pVM->selm.s.pvLdtRC = MMHyperR3ToRC(pVM, pVM->selm.s.pvLdtR3);
590#ifdef SELM_TRACK_SHADOW_LDT_CHANGES
591 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hShadowLdtWriteHandlerType,
592 pVM->selm.s.pvLdtRC,
593 pVM->selm.s.pvLdtRC + _64K + PAGE_SIZE - 1,
594 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
595 AssertRC(rc);
596#endif
597 }
598}
599
600
601/**
602 * Terminates the SELM.
603 *
604 * Termination means cleaning up and freeing all resources,
605 * the VM it self is at this point powered off or suspended.
606 *
607 * @returns VBox status code.
608 * @param pVM The cross context VM structure.
609 */
610VMMR3DECL(int) SELMR3Term(PVM pVM)
611{
612 NOREF(pVM);
613 return VINF_SUCCESS;
614}
615
616
617/**
618 * The VM is being reset.
619 *
620 * For the SELM component this means that any GDT/LDT/TSS monitors
621 * needs to be removed.
622 *
623 * @param pVM The cross context VM structure.
624 */
625VMMR3DECL(void) SELMR3Reset(PVM pVM)
626{
627 LogFlow(("SELMR3Reset:\n"));
628 VM_ASSERT_EMT(pVM);
629
630 /*
631 * Uninstall guest GDT/LDT/TSS write access handlers.
632 */
633 PVMCPU pVCpu = VMMGetCpu(pVM); NOREF(pVCpu);
634 int rc = VINF_SUCCESS;
635 if (pVM->selm.s.GuestGdtr.pGdt != RTRCPTR_MAX && pVM->selm.s.fGDTRangeRegistered)
636 {
637#ifdef SELM_TRACK_GUEST_GDT_CHANGES
638 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GuestGdtr.pGdt, false /*fHypervisor*/);
639 AssertRC(rc);
640#endif
641 pVM->selm.s.GuestGdtr.pGdt = RTRCPTR_MAX;
642 pVM->selm.s.GuestGdtr.cbGdt = 0;
643 }
644 pVM->selm.s.fGDTRangeRegistered = false;
645 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
646 {
647#ifdef SELM_TRACK_GUEST_LDT_CHANGES
648 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GCPtrGuestLdt, false /*fHypervisor*/);
649 AssertRC(rc);
650#endif
651 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
652 }
653 if (pVM->selm.s.GCPtrGuestTss != RTRCPTR_MAX)
654 {
655#ifdef SELM_TRACK_GUEST_TSS_CHANGES
656 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GCPtrGuestTss, false /*fHypervisor*/);
657 AssertRC(rc);
658#endif
659 pVM->selm.s.GCPtrGuestTss = RTRCPTR_MAX;
660 pVM->selm.s.GCSelTss = RTSEL_MAX;
661 }
662
663 /*
664 * Re-initialize other members.
665 */
666 pVM->selm.s.cbLdtLimit = 0;
667 pVM->selm.s.offLdtHyper = 0;
668 pVM->selm.s.cbMonitoredGuestTss = 0;
669
670 pVM->selm.s.fSyncTSSRing0Stack = false;
671
672#ifdef VBOX_WITH_RAW_MODE
673 if (!HMIsEnabled(pVM))
674 {
675 /*
676 * Default action when entering raw mode for the first time
677 */
678 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
679 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
680 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
681 }
682#endif
683}
684
685
686/**
687 * Execute state save operation.
688 *
689 * @returns VBox status code.
690 * @param pVM The cross context VM structure.
691 * @param pSSM SSM operation handle.
692 */
693static DECLCALLBACK(int) selmR3Save(PVM pVM, PSSMHANDLE pSSM)
694{
695 LogFlow(("selmR3Save:\n"));
696
697 /*
698 * Save the basic bits - fortunately all the other things can be resynced on load.
699 */
700 PSELM pSelm = &pVM->selm.s;
701
702 SSMR3PutBool(pSSM, HMIsEnabled(pVM));
703 SSMR3PutBool(pSSM, pSelm->fSyncTSSRing0Stack);
704 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_CS]);
705 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_DS]);
706 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_CS64]);
707 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_CS64]); /* reserved for DS64. */
708 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_TSS]);
709 return SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_TSS_TRAP08]);
710}
711
712
713/**
714 * Execute state load operation.
715 *
716 * @returns VBox status code.
717 * @param pVM The cross context VM structure.
718 * @param pSSM SSM operation handle.
719 * @param uVersion Data layout version.
720 * @param uPass The data pass.
721 */
722static DECLCALLBACK(int) selmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
723{
724 LogFlow(("selmR3Load:\n"));
725 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
726
727 /*
728 * Validate version.
729 */
730 if (uVersion != SELM_SAVED_STATE_VERSION)
731 {
732 AssertMsgFailed(("selmR3Load: Invalid version uVersion=%d!\n", uVersion));
733 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
734 }
735
736 /*
737 * Do a reset.
738 */
739 SELMR3Reset(pVM);
740
741 /* Get the monitoring flag. */
742 bool fIgnored;
743 SSMR3GetBool(pSSM, &fIgnored);
744
745 /* Get the TSS state flag. */
746 SSMR3GetBool(pSSM, &pVM->selm.s.fSyncTSSRing0Stack);
747
748 /*
749 * Get the selectors.
750 */
751 RTSEL SelCS;
752 SSMR3GetSel(pSSM, &SelCS);
753 RTSEL SelDS;
754 SSMR3GetSel(pSSM, &SelDS);
755 RTSEL SelCS64;
756 SSMR3GetSel(pSSM, &SelCS64);
757 RTSEL SelDS64;
758 SSMR3GetSel(pSSM, &SelDS64);
759 RTSEL SelTSS;
760 SSMR3GetSel(pSSM, &SelTSS);
761 RTSEL SelTSSTrap08;
762 SSMR3GetSel(pSSM, &SelTSSTrap08);
763
764 /* Copy the selectors; they will be checked during relocation. */
765 PSELM pSelm = &pVM->selm.s;
766 pSelm->aHyperSel[SELM_HYPER_SEL_CS] = SelCS;
767 pSelm->aHyperSel[SELM_HYPER_SEL_DS] = SelDS;
768 pSelm->aHyperSel[SELM_HYPER_SEL_CS64] = SelCS64;
769 pSelm->aHyperSel[SELM_HYPER_SEL_TSS] = SelTSS;
770 pSelm->aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = SelTSSTrap08;
771
772 return VINF_SUCCESS;
773}
774
775
776/**
777 * Sync the GDT, LDT and TSS after loading the state.
778 *
779 * Just to play save, we set the FFs to force syncing before
780 * executing GC code.
781 *
782 * @returns VBox status code.
783 * @param pVM The cross context VM structure.
784 * @param pSSM SSM operation handle.
785 */
786static DECLCALLBACK(int) selmR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
787{
788#ifdef VBOX_WITH_RAW_MODE
789 if (!HMIsEnabled(pVM))
790 {
791 PVMCPU pVCpu = VMMGetCpu(pVM);
792
793 LogFlow(("selmR3LoadDone:\n"));
794
795 /*
796 * Don't do anything if it's a load failure.
797 */
798 int rc = SSMR3HandleGetStatus(pSSM);
799 if (RT_FAILURE(rc))
800 return VINF_SUCCESS;
801
802 /*
803 * Do the syncing if we're in protected mode and using raw-mode.
804 */
805 if (PGMGetGuestMode(pVCpu) != PGMMODE_REAL)
806 {
807 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
808 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
809 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
810 SELMR3UpdateFromCPUM(pVM, pVCpu);
811 }
812
813 /*
814 * Flag everything for resync on next raw mode entry.
815 */
816 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
817 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
818 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
819 }
820#endif /*VBOX_WITH_RAW_MODE*/
821 return VINF_SUCCESS;
822}
823
824#ifdef VBOX_WITH_RAW_MODE
825
826/**
827 * Updates (syncs) the shadow GDT.
828 *
829 * @returns VBox status code.
830 * @param pVM The cross context VM structure.
831 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
832 */
833static int selmR3UpdateShadowGdt(PVM pVM, PVMCPU pVCpu)
834{
835 Assert(!HMIsEnabled(pVM));
836
837 /*
838 * Always assume the best...
839 */
840 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
841
842 /* If the GDT was changed, then make sure the LDT is checked too */
843 /** @todo only do this if the actual ldtr selector was changed; this is a bit excessive */
844 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
845 /* Same goes for the TSS selector */
846 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
847
848 /*
849 * Get the GDTR and check if there is anything to do (there usually is).
850 */
851 VBOXGDTR GDTR;
852 CPUMGetGuestGDTR(pVCpu, &GDTR);
853 if (GDTR.cbGdt < sizeof(X86DESC))
854 {
855 Log(("No GDT entries...\n"));
856 return VINF_SUCCESS;
857 }
858
859 /*
860 * Read the Guest GDT.
861 * ASSUMES that the entire GDT is in memory.
862 */
863 RTUINT cbEffLimit = GDTR.cbGdt;
864 PX86DESC pGDTE = &pVM->selm.s.paGdtR3[1];
865 int rc = PGMPhysSimpleReadGCPtr(pVCpu, pGDTE, GDTR.pGdt + sizeof(X86DESC), cbEffLimit + 1 - sizeof(X86DESC));
866 if (RT_FAILURE(rc))
867 {
868 /*
869 * Read it page by page.
870 *
871 * Keep track of the last valid page and delay memsets and
872 * adjust cbEffLimit to reflect the effective size. The latter
873 * is something we do in the belief that the guest will probably
874 * never actually commit the last page, thus allowing us to keep
875 * our selectors in the high end of the GDT.
876 */
877 RTUINT cbLeft = cbEffLimit + 1 - sizeof(X86DESC);
878 RTGCPTR GCPtrSrc = (RTGCPTR)GDTR.pGdt + sizeof(X86DESC);
879 uint8_t *pu8Dst = (uint8_t *)&pVM->selm.s.paGdtR3[1];
880 uint8_t *pu8DstInvalid = pu8Dst;
881
882 while (cbLeft)
883 {
884 RTUINT cb = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
885 cb = RT_MIN(cb, cbLeft);
886 rc = PGMPhysSimpleReadGCPtr(pVCpu, pu8Dst, GCPtrSrc, cb);
887 if (RT_SUCCESS(rc))
888 {
889 if (pu8DstInvalid != pu8Dst)
890 RT_BZERO(pu8DstInvalid, pu8Dst - pu8DstInvalid);
891 GCPtrSrc += cb;
892 pu8Dst += cb;
893 pu8DstInvalid = pu8Dst;
894 }
895 else if ( rc == VERR_PAGE_NOT_PRESENT
896 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
897 {
898 GCPtrSrc += cb;
899 pu8Dst += cb;
900 }
901 else
902 {
903 AssertLogRelMsgFailed(("Couldn't read GDT at %016RX64, rc=%Rrc!\n", GDTR.pGdt, rc));
904 return VERR_SELM_GDT_READ_ERROR;
905 }
906 cbLeft -= cb;
907 }
908
909 /* any invalid pages at the end? */
910 if (pu8DstInvalid != pu8Dst)
911 {
912 cbEffLimit = pu8DstInvalid - (uint8_t *)pVM->selm.s.paGdtR3 - 1;
913 /* If any GDTEs was invalidated, zero them. */
914 if (cbEffLimit < pVM->selm.s.cbEffGuestGdtLimit)
915 RT_BZERO(pu8DstInvalid + cbEffLimit + 1, pVM->selm.s.cbEffGuestGdtLimit - cbEffLimit);
916 }
917
918 /* keep track of the effective limit. */
919 if (cbEffLimit != pVM->selm.s.cbEffGuestGdtLimit)
920 {
921 Log(("SELMR3UpdateFromCPUM: cbEffGuestGdtLimit=%#x -> %#x (actual %#x)\n",
922 pVM->selm.s.cbEffGuestGdtLimit, cbEffLimit, GDTR.cbGdt));
923 pVM->selm.s.cbEffGuestGdtLimit = cbEffLimit;
924 }
925 }
926
927 /*
928 * Check if the Guest GDT intrudes on our GDT entries.
929 */
930 /** @todo we should try to minimize relocations by making sure our current selectors can be reused. */
931 RTSEL aHyperSel[SELM_HYPER_SEL_MAX];
932 if (cbEffLimit >= SELM_HYPER_DEFAULT_BASE)
933 {
934 PX86DESC pGDTEStart = pVM->selm.s.paGdtR3;
935 PX86DESC pGDTECur = (PX86DESC)((char *)pGDTEStart + GDTR.cbGdt + 1 - sizeof(X86DESC));
936 int iGDT = 0;
937
938 Log(("Internal SELM GDT conflict: use non-present entries\n"));
939 STAM_REL_COUNTER_INC(&pVM->selm.s.StatScanForHyperSels);
940 while ((uintptr_t)pGDTECur > (uintptr_t)pGDTEStart)
941 {
942 /* We can reuse non-present entries */
943 if (!pGDTECur->Gen.u1Present)
944 {
945 aHyperSel[iGDT] = ((uintptr_t)pGDTECur - (uintptr_t)pVM->selm.s.paGdtR3) / sizeof(X86DESC);
946 aHyperSel[iGDT] = aHyperSel[iGDT] << X86_SEL_SHIFT;
947 Log(("SELM: Found unused GDT %04X\n", aHyperSel[iGDT]));
948 iGDT++;
949 if (iGDT >= SELM_HYPER_SEL_MAX)
950 break;
951 }
952
953 pGDTECur--;
954 }
955 if (iGDT != SELM_HYPER_SEL_MAX)
956 {
957 AssertLogRelMsgFailed(("Internal SELM GDT conflict.\n"));
958 return VERR_SELM_GDT_TOO_FULL;
959 }
960 }
961 else
962 {
963 aHyperSel[SELM_HYPER_SEL_CS] = SELM_HYPER_DEFAULT_SEL_CS;
964 aHyperSel[SELM_HYPER_SEL_DS] = SELM_HYPER_DEFAULT_SEL_DS;
965 aHyperSel[SELM_HYPER_SEL_CS64] = SELM_HYPER_DEFAULT_SEL_CS64;
966 aHyperSel[SELM_HYPER_SEL_TSS] = SELM_HYPER_DEFAULT_SEL_TSS;
967 aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = SELM_HYPER_DEFAULT_SEL_TSS_TRAP08;
968 }
969
970# ifdef VBOX_WITH_SAFE_STR
971 /* Use the guest's TR selector to plug the str virtualization hole. */
972 if (CPUMGetGuestTR(pVCpu, NULL) != 0)
973 {
974 Log(("SELM: Use guest TSS selector %x\n", CPUMGetGuestTR(pVCpu, NULL)));
975 aHyperSel[SELM_HYPER_SEL_TSS] = CPUMGetGuestTR(pVCpu, NULL);
976 }
977# endif
978
979 /*
980 * Work thru the copied GDT entries adjusting them for correct virtualization.
981 */
982 PX86DESC pGDTEEnd = (PX86DESC)((char *)pGDTE + cbEffLimit + 1 - sizeof(X86DESC));
983 while (pGDTE < pGDTEEnd)
984 {
985 if (pGDTE->Gen.u1Present)
986 selmGuestToShadowDesc(pVM, pGDTE);
987
988 /* Next GDT entry. */
989 pGDTE++;
990 }
991
992 /*
993 * Check if our hypervisor selectors were changed.
994 */
995 if ( aHyperSel[SELM_HYPER_SEL_CS] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS]
996 || aHyperSel[SELM_HYPER_SEL_DS] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]
997 || aHyperSel[SELM_HYPER_SEL_CS64] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64]
998 || aHyperSel[SELM_HYPER_SEL_TSS] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]
999 || aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08])
1000 {
1001 /* Reinitialize our hypervisor GDTs */
1002 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] = aHyperSel[SELM_HYPER_SEL_CS];
1003 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] = aHyperSel[SELM_HYPER_SEL_DS];
1004 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] = aHyperSel[SELM_HYPER_SEL_CS64];
1005 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] = aHyperSel[SELM_HYPER_SEL_TSS];
1006 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = aHyperSel[SELM_HYPER_SEL_TSS_TRAP08];
1007
1008 STAM_REL_COUNTER_INC(&pVM->selm.s.StatHyperSelsChanged);
1009
1010 /*
1011 * Do the relocation callbacks to let everyone update their hyper selector dependencies.
1012 * (SELMR3Relocate will call selmR3SetupHyperGDTSelectors() for us.)
1013 */
1014 VMR3Relocate(pVM, 0);
1015 }
1016# ifdef VBOX_WITH_SAFE_STR
1017 else if ( cbEffLimit >= SELM_HYPER_DEFAULT_BASE
1018 || CPUMGetGuestTR(pVCpu, NULL) != 0) /* Our shadow TR entry was overwritten when we synced the guest's GDT. */
1019# else
1020 else if (cbEffLimit >= SELM_HYPER_DEFAULT_BASE)
1021# endif
1022 /* We overwrote all entries above, so we have to save them again. */
1023 selmR3SetupHyperGDTSelectors(pVM);
1024
1025 /*
1026 * Adjust the cached GDT limit.
1027 * Any GDT entries which have been removed must be cleared.
1028 */
1029 if (pVM->selm.s.GuestGdtr.cbGdt != GDTR.cbGdt)
1030 {
1031 if (pVM->selm.s.GuestGdtr.cbGdt > GDTR.cbGdt)
1032 RT_BZERO(pGDTE, pVM->selm.s.GuestGdtr.cbGdt - GDTR.cbGdt);
1033 }
1034
1035 /*
1036 * Check if Guest's GDTR is changed.
1037 */
1038 if ( GDTR.pGdt != pVM->selm.s.GuestGdtr.pGdt
1039 || GDTR.cbGdt != pVM->selm.s.GuestGdtr.cbGdt)
1040 {
1041 Log(("SELMR3UpdateFromCPUM: Guest's GDT is changed to pGdt=%016RX64 cbGdt=%08X\n", GDTR.pGdt, GDTR.cbGdt));
1042
1043# ifdef SELM_TRACK_GUEST_GDT_CHANGES
1044 /*
1045 * [Re]Register write virtual handler for guest's GDT.
1046 */
1047 if (pVM->selm.s.GuestGdtr.pGdt != RTRCPTR_MAX && pVM->selm.s.fGDTRangeRegistered)
1048 {
1049 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GuestGdtr.pGdt, false /*fHypervisor*/);
1050 AssertRC(rc);
1051 }
1052 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hGuestGdtWriteHandlerType,
1053 GDTR.pGdt, GDTR.pGdt + GDTR.cbGdt /* already inclusive */,
1054 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
1055# ifdef VBOX_WITH_RAW_RING1
1056 /** @todo !HACK ALERT!
1057 * Some guest OSes (QNX) share code and the GDT on the same page;
1058 * PGMR3HandlerVirtualRegister doesn't support more than one handler,
1059 * so we kick out the PATM handler as this one is more important. Fix this
1060 * properly in PGMR3HandlerVirtualRegister?
1061 */
1062 if (rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT)
1063 {
1064 LogRel(("selmR3UpdateShadowGdt: Virtual handler conflict %RGv -> kick out PATM handler for the higher priority GDT page monitor\n", GDTR.pGdt));
1065 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, GDTR.pGdt & PAGE_BASE_GC_MASK, false /*fHypervisor*/);
1066 AssertRC(rc);
1067 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hGuestGdtWriteHandlerType,
1068 GDTR.pGdt, GDTR.pGdt + GDTR.cbGdt /* already inclusive */,
1069 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
1070 }
1071# endif
1072 if (RT_FAILURE(rc))
1073 return rc;
1074# endif /* SELM_TRACK_GUEST_GDT_CHANGES */
1075
1076 /* Update saved Guest GDTR. */
1077 pVM->selm.s.GuestGdtr = GDTR;
1078 pVM->selm.s.fGDTRangeRegistered = true;
1079 }
1080
1081 return VINF_SUCCESS;
1082}
1083
1084
1085/**
1086 * Updates (syncs) the shadow LDT.
1087 *
1088 * @returns VBox status code.
1089 * @param pVM The cross context VM structure.
1090 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1091 */
1092static int selmR3UpdateShadowLdt(PVM pVM, PVMCPU pVCpu)
1093{
1094 int rc = VINF_SUCCESS;
1095 Assert(!HMIsEnabled(pVM));
1096
1097 /*
1098 * Always assume the best...
1099 */
1100 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
1101
1102 /*
1103 * LDT handling is done similarly to the GDT handling with a shadow
1104 * array. However, since the LDT is expected to be swappable (at least
1105 * some ancient OSes makes it swappable) it must be floating and
1106 * synced on a per-page basis.
1107 *
1108 * Eventually we will change this to be fully on demand. Meaning that
1109 * we will only sync pages containing LDT selectors actually used and
1110 * let the #PF handler lazily sync pages as they are used.
1111 * (This applies to GDT too, when we start making OS/2 fast.)
1112 */
1113
1114 /*
1115 * First, determine the current LDT selector.
1116 */
1117 RTSEL SelLdt = CPUMGetGuestLDTR(pVCpu);
1118 if (!(SelLdt & X86_SEL_MASK_OFF_RPL))
1119 {
1120 /* ldtr = 0 - update hyper LDTR and deregister any active handler. */
1121 CPUMSetHyperLDTR(pVCpu, 0);
1122 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
1123 {
1124 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GCPtrGuestLdt, false /*fHypervisor*/);
1125 AssertRC(rc);
1126 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
1127 }
1128 pVM->selm.s.cbLdtLimit = 0;
1129 return VINF_SUCCESS;
1130 }
1131
1132 /*
1133 * Get the LDT selector.
1134 */
1135/** @todo this is wrong, use CPUMGetGuestLdtrEx */
1136 PX86DESC pDesc = &pVM->selm.s.paGdtR3[SelLdt >> X86_SEL_SHIFT];
1137 RTGCPTR GCPtrLdt = X86DESC_BASE(pDesc);
1138 uint32_t cbLdt = X86DESC_LIMIT_G(pDesc);
1139
1140 /*
1141 * Validate it.
1142 */
1143 if ( !cbLdt
1144 || SelLdt >= pVM->selm.s.GuestGdtr.cbGdt
1145 || pDesc->Gen.u1DescType
1146 || pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1147 {
1148 AssertMsg(!cbLdt, ("Invalid LDT %04x!\n", SelLdt));
1149
1150 /* cbLdt > 0:
1151 * This is quite impossible, so we do as most people do when faced with
1152 * the impossible, we simply ignore it.
1153 */
1154 CPUMSetHyperLDTR(pVCpu, 0);
1155 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
1156 {
1157 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GCPtrGuestLdt, false /*fHypervisor*/);
1158 AssertRC(rc);
1159 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
1160 }
1161 return VINF_SUCCESS;
1162 }
1163 /** @todo check what intel does about odd limits. */
1164 AssertMsg(RT_ALIGN(cbLdt + 1, sizeof(X86DESC)) == cbLdt + 1 && cbLdt <= 0xffff, ("cbLdt=%d\n", cbLdt));
1165
1166 /*
1167 * Use the cached guest ldt address if the descriptor has already been modified (see below)
1168 * (this is necessary due to redundant LDT updates; see todo above at GDT sync)
1169 */
1170 if (MMHyperIsInsideArea(pVM, GCPtrLdt))
1171 GCPtrLdt = pVM->selm.s.GCPtrGuestLdt; /* use the old one */
1172
1173
1174 /** @todo Handle only present LDT segments. */
1175// if (pDesc->Gen.u1Present)
1176 {
1177 /*
1178 * Check if Guest's LDT address/limit is changed.
1179 */
1180 if ( GCPtrLdt != pVM->selm.s.GCPtrGuestLdt
1181 || cbLdt != pVM->selm.s.cbLdtLimit)
1182 {
1183 Log(("SELMR3UpdateFromCPUM: Guest LDT changed to from %RGv:%04x to %RGv:%04x. (GDTR=%016RX64:%04x)\n",
1184 pVM->selm.s.GCPtrGuestLdt, pVM->selm.s.cbLdtLimit, GCPtrLdt, cbLdt, pVM->selm.s.GuestGdtr.pGdt, pVM->selm.s.GuestGdtr.cbGdt));
1185
1186# ifdef SELM_TRACK_GUEST_LDT_CHANGES
1187 /*
1188 * [Re]Register write virtual handler for guest's GDT.
1189 * In the event of LDT overlapping something, don't install it just assume it's being updated.
1190 */
1191 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
1192 {
1193 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GCPtrGuestLdt, false /*fHypervisor*/);
1194 AssertRC(rc);
1195 }
1196# ifdef LOG_ENABLED
1197 if (pDesc->Gen.u1Present)
1198 Log(("LDT selector marked not present!!\n"));
1199# endif
1200 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hGuestLdtWriteHandlerType,
1201 GCPtrLdt, GCPtrLdt + cbLdt /* already inclusive */,
1202 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
1203 if (rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT)
1204 {
1205 /** @todo investigate the various cases where conflicts happen and try avoid them by enh. the instruction emulation. */
1206 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
1207 Log(("WARNING: Guest LDT (%RGv:%04x) conflicted with existing access range!! Assumes LDT is begin updated. (GDTR=%016RX64:%04x)\n",
1208 GCPtrLdt, cbLdt, pVM->selm.s.GuestGdtr.pGdt, pVM->selm.s.GuestGdtr.cbGdt));
1209 }
1210 else if (RT_SUCCESS(rc))
1211 pVM->selm.s.GCPtrGuestLdt = GCPtrLdt;
1212 else
1213 {
1214 CPUMSetHyperLDTR(pVCpu, 0);
1215 return rc;
1216 }
1217# else
1218 pVM->selm.s.GCPtrGuestLdt = GCPtrLdt;
1219# endif
1220 pVM->selm.s.cbLdtLimit = cbLdt;
1221 }
1222 }
1223
1224 /*
1225 * Calc Shadow LDT base.
1226 */
1227 unsigned off;
1228 pVM->selm.s.offLdtHyper = off = (GCPtrLdt & PAGE_OFFSET_MASK);
1229 RTGCPTR GCPtrShadowLDT = (RTGCPTR)((RTGCUINTPTR)pVM->selm.s.pvLdtRC + off);
1230 PX86DESC pShadowLDT = (PX86DESC)((uintptr_t)pVM->selm.s.pvLdtR3 + off);
1231
1232 /*
1233 * Enable the LDT selector in the shadow GDT.
1234 */
1235 pDesc->Gen.u1Present = 1;
1236 pDesc->Gen.u16BaseLow = RT_LOWORD(GCPtrShadowLDT);
1237 pDesc->Gen.u8BaseHigh1 = RT_BYTE3(GCPtrShadowLDT);
1238 pDesc->Gen.u8BaseHigh2 = RT_BYTE4(GCPtrShadowLDT);
1239 pDesc->Gen.u1Available = 0;
1240 pDesc->Gen.u1Long = 0;
1241 if (cbLdt > 0xffff)
1242 {
1243 cbLdt = 0xffff;
1244 pDesc->Gen.u4LimitHigh = 0;
1245 pDesc->Gen.u16LimitLow = pDesc->Gen.u1Granularity ? 0xf : 0xffff;
1246 }
1247
1248 /*
1249 * Set Hyper LDTR and notify TRPM.
1250 */
1251 CPUMSetHyperLDTR(pVCpu, SelLdt);
1252
1253 /*
1254 * Loop synchronising the LDT page by page.
1255 */
1256 /** @todo investigate how intel handle various operations on half present cross page entries. */
1257 off = GCPtrLdt & (sizeof(X86DESC) - 1);
1258 AssertMsg(!off, ("LDT is not aligned on entry size! GCPtrLdt=%08x\n", GCPtrLdt));
1259
1260 /* Note: Do not skip the first selector; unlike the GDT, a zero LDT selector is perfectly valid. */
1261 unsigned cbLeft = cbLdt + 1;
1262 PX86DESC pLDTE = pShadowLDT;
1263 while (cbLeft)
1264 {
1265 /*
1266 * Read a chunk.
1267 */
1268 unsigned cbChunk = PAGE_SIZE - ((RTGCUINTPTR)GCPtrLdt & PAGE_OFFSET_MASK);
1269 if (cbChunk > cbLeft)
1270 cbChunk = cbLeft;
1271 rc = PGMPhysSimpleReadGCPtr(pVCpu, pShadowLDT, GCPtrLdt, cbChunk);
1272 if (RT_SUCCESS(rc))
1273 {
1274 /*
1275 * Mark page
1276 */
1277 rc = PGMMapSetPage(pVM, GCPtrShadowLDT & PAGE_BASE_GC_MASK, PAGE_SIZE, X86_PTE_P | X86_PTE_A | X86_PTE_D);
1278 AssertRC(rc);
1279
1280 /*
1281 * Loop thru the available LDT entries.
1282 * Figure out where to start and end and the potential cross pageness of
1283 * things adds a little complexity. pLDTE is updated there and not in the
1284 * 'next' part of the loop. The pLDTEEnd is inclusive.
1285 */
1286 PX86DESC pLDTEEnd = (PX86DESC)((uintptr_t)pShadowLDT + cbChunk) - 1;
1287 if (pLDTE + 1 < pShadowLDT)
1288 pLDTE = (PX86DESC)((uintptr_t)pShadowLDT + off);
1289 while (pLDTE <= pLDTEEnd)
1290 {
1291 if (pLDTE->Gen.u1Present)
1292 selmGuestToShadowDesc(pVM, pLDTE);
1293
1294 /* Next LDT entry. */
1295 pLDTE++;
1296 }
1297 }
1298 else
1299 {
1300 RT_BZERO(pShadowLDT, cbChunk);
1301 AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc=%Rrc\n", rc));
1302 rc = PGMMapSetPage(pVM, GCPtrShadowLDT & PAGE_BASE_GC_MASK, PAGE_SIZE, 0);
1303 AssertRC(rc);
1304 }
1305
1306 /*
1307 * Advance to the next page.
1308 */
1309 cbLeft -= cbChunk;
1310 GCPtrShadowLDT += cbChunk;
1311 pShadowLDT = (PX86DESC)((char *)pShadowLDT + cbChunk);
1312 GCPtrLdt += cbChunk;
1313 }
1314
1315 return VINF_SUCCESS;
1316}
1317
1318
1319/**
1320 * Checks and updates segment selector registers.
1321 *
1322 * @returns VBox strict status code.
1323 * @retval VINF_EM_RESCHEDULE_REM if a stale register was found.
1324 *
1325 * @param pVM The cross context VM structure.
1326 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1327 */
1328static VBOXSTRICTRC selmR3UpdateSegmentRegisters(PVM pVM, PVMCPU pVCpu)
1329{
1330 Assert(CPUMIsGuestInProtectedMode(pVCpu));
1331 Assert(!HMIsEnabled(pVM));
1332
1333 /*
1334 * No stale selectors in V8086 mode.
1335 */
1336 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1337 if (pCtx->eflags.Bits.u1VM)
1338 return VINF_SUCCESS;
1339
1340 /*
1341 * Check for stale selectors and load hidden register bits where they
1342 * are missing.
1343 */
1344 uint32_t uCpl = CPUMGetGuestCPL(pVCpu);
1345 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1346 PCPUMSELREG paSReg = CPUMCTX_FIRST_SREG(pCtx);
1347 for (uint32_t iSReg = 0; iSReg < X86_SREG_COUNT; iSReg++)
1348 {
1349 RTSEL const Sel = paSReg[iSReg].Sel;
1350 if (Sel & X86_SEL_MASK_OFF_RPL)
1351 {
1352 /* Get the shadow descriptor entry corresponding to this. */
1353 static X86DESC const s_NotPresentDesc = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
1354 PCX86DESC pDesc;
1355 if (!(Sel & X86_SEL_LDT))
1356 {
1357 if ((Sel | (sizeof(*pDesc) - 1)) <= pCtx->gdtr.cbGdt)
1358 pDesc = &pVM->selm.s.paGdtR3[Sel >> X86_SEL_SHIFT];
1359 else
1360 pDesc = &s_NotPresentDesc;
1361 }
1362 else
1363 {
1364 if ((Sel | (sizeof(*pDesc) - 1)) <= pVM->selm.s.cbLdtLimit)
1365 pDesc = &((PCX86DESC)((uintptr_t)pVM->selm.s.pvLdtR3 + pVM->selm.s.offLdtHyper))[Sel >> X86_SEL_SHIFT];
1366 else
1367 pDesc = &s_NotPresentDesc;
1368 }
1369
1370 /* Check the segment register. */
1371 if (CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &paSReg[iSReg]))
1372 {
1373 if (!(paSReg[iSReg].fFlags & CPUMSELREG_FLAGS_STALE))
1374 {
1375 /* Did it go stale? */
1376 if (selmIsSRegStale32(&paSReg[iSReg], pDesc, iSReg))
1377 {
1378 Log2(("SELM: Detected stale %s=%#x (was valid)\n", g_aszSRegNms[iSReg], Sel));
1379 STAM_REL_COUNTER_INC(&pVM->selm.s.aStatDetectedStaleSReg[iSReg]);
1380 paSReg[iSReg].fFlags |= CPUMSELREG_FLAGS_STALE;
1381 rcStrict = VINF_EM_RESCHEDULE_REM;
1382 }
1383 }
1384 else
1385 {
1386 /* Did it stop being stale? I.e. did the guest change it things
1387 back to the way they were? */
1388 if (!selmIsSRegStale32(&paSReg[iSReg], pDesc, iSReg))
1389 {
1390 STAM_REL_COUNTER_INC(&pVM->selm.s.StatStaleToUnstaleSReg);
1391 paSReg[iSReg].fFlags &= CPUMSELREG_FLAGS_STALE;
1392 }
1393 else
1394 {
1395 Log2(("SELM: Already stale %s=%#x\n", g_aszSRegNms[iSReg], Sel));
1396 STAM_REL_COUNTER_INC(&pVM->selm.s.aStatAlreadyStaleSReg[iSReg]);
1397 rcStrict = VINF_EM_RESCHEDULE_REM;
1398 }
1399 }
1400 }
1401 /* Load the hidden registers if it's a valid descriptor for the
1402 current segment register. */
1403 else if (selmIsShwDescGoodForSReg(&paSReg[iSReg], pDesc, iSReg, uCpl))
1404 {
1405 selmLoadHiddenSRegFromShadowDesc(&paSReg[iSReg], pDesc);
1406 STAM_COUNTER_INC(&pVM->selm.s.aStatUpdatedSReg[iSReg]);
1407 }
1408 /* It's stale. */
1409 else
1410 {
1411 Log2(("SELM: Detected stale %s=%#x (wasn't valid)\n", g_aszSRegNms[iSReg], Sel));
1412 STAM_REL_COUNTER_INC(&pVM->selm.s.aStatDetectedStaleSReg[iSReg]);
1413 paSReg[iSReg].fFlags = CPUMSELREG_FLAGS_STALE;
1414 rcStrict = VINF_EM_RESCHEDULE_REM;
1415 }
1416 }
1417 /* else: 0 selector, ignore. */
1418 }
1419
1420 return rcStrict;
1421}
1422
1423
1424/**
1425 * Updates the Guest GDT & LDT virtualization based on current CPU state.
1426 *
1427 * @returns VBox status code.
1428 * @param pVM The cross context VM structure.
1429 * @param pVCpu The cross context virtual CPU structure.
1430 */
1431VMMR3DECL(VBOXSTRICTRC) SELMR3UpdateFromCPUM(PVM pVM, PVMCPU pVCpu)
1432{
1433 STAM_PROFILE_START(&pVM->selm.s.StatUpdateFromCPUM, a);
1434 AssertReturn(!HMIsEnabled(pVM), VERR_SELM_HM_IPE);
1435
1436 /*
1437 * GDT sync
1438 */
1439 int rc;
1440 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT))
1441 {
1442 rc = selmR3UpdateShadowGdt(pVM, pVCpu);
1443 if (RT_FAILURE(rc))
1444 return rc; /* We're toast, so forget the profiling. */
1445 AssertRCSuccess(rc);
1446 }
1447
1448 /*
1449 * TSS sync
1450 */
1451 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS))
1452 {
1453 rc = SELMR3SyncTSS(pVM, pVCpu);
1454 if (RT_FAILURE(rc))
1455 return rc;
1456 AssertRCSuccess(rc);
1457 }
1458
1459 /*
1460 * LDT sync
1461 */
1462 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT))
1463 {
1464 rc = selmR3UpdateShadowLdt(pVM, pVCpu);
1465 if (RT_FAILURE(rc))
1466 return rc;
1467 AssertRCSuccess(rc);
1468 }
1469
1470 /*
1471 * Check selector registers.
1472 */
1473 VBOXSTRICTRC rcStrict = selmR3UpdateSegmentRegisters(pVM, pVCpu);
1474
1475 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1476 return rcStrict;
1477}
1478
1479
1480/**
1481 * Synchronize the shadowed fields in the TSS.
1482 *
1483 * At present we're shadowing the ring-0 stack selector & pointer, and the
1484 * interrupt redirection bitmap (if present). We take the lazy approach wrt to
1485 * REM and this function is called both if REM made any changes to the TSS or
1486 * loaded TR.
1487 *
1488 * @returns VBox status code.
1489 * @param pVM The cross context VM structure.
1490 * @param pVCpu The cross context virtual CPU structure.
1491 */
1492VMMR3DECL(int) SELMR3SyncTSS(PVM pVM, PVMCPU pVCpu)
1493{
1494 int rc;
1495 AssertReturnStmt(!HMIsEnabled(pVM), VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_SELM_SYNC_TSS), VINF_SUCCESS);
1496
1497 STAM_PROFILE_START(&pVM->selm.s.StatTSSSync, a);
1498 Assert(VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS));
1499
1500 /*
1501 * Get TR and extract and store the basic info.
1502 *
1503 * Note! The TSS limit is not checked by the LTR code, so we
1504 * have to be a bit careful with it. We make sure cbTss
1505 * won't be zero if TR is valid and if it's NULL we'll
1506 * make sure cbTss is 0.
1507 */
1508/** @todo use the hidden bits, not shadow GDT. */
1509 CPUMSELREGHID trHid;
1510 RTSEL SelTss = CPUMGetGuestTR(pVCpu, &trHid);
1511 RTGCPTR GCPtrTss = trHid.u64Base;
1512 uint32_t cbTss = trHid.u32Limit;
1513 Assert( (SelTss & X86_SEL_MASK_OFF_RPL)
1514 || (cbTss == 0 && GCPtrTss == 0 && trHid.Attr.u == 0 /* TR=0 */)
1515 || (cbTss == 0xffff && GCPtrTss == 0 && trHid.Attr.n.u1Present && trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY /* RESET */));
1516 if (SelTss & X86_SEL_MASK_OFF_RPL)
1517 {
1518 Assert(!(SelTss & X86_SEL_LDT));
1519 Assert(trHid.Attr.n.u1DescType == 0);
1520 Assert( trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY
1521 || trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY);
1522 if (!++cbTss)
1523 cbTss = UINT32_MAX;
1524 }
1525 else
1526 {
1527 Assert( (cbTss == 0 && GCPtrTss == 0 && trHid.Attr.u == 0 /* TR=0 */)
1528 || (cbTss == 0xffff && GCPtrTss == 0 && trHid.Attr.n.u1Present && trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY /* RESET */));
1529 cbTss = 0; /* the reset case. */
1530 }
1531 pVM->selm.s.cbGuestTss = cbTss;
1532 pVM->selm.s.fGuestTss32Bit = trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL
1533 || trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY;
1534
1535 /*
1536 * Figure out the size of what need to monitor.
1537 */
1538 /* We're not interested in any 16-bit TSSes. */
1539 uint32_t cbMonitoredTss = cbTss;
1540 if ( trHid.Attr.n.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL
1541 && trHid.Attr.n.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
1542 cbMonitoredTss = 0;
1543
1544 pVM->selm.s.offGuestIoBitmap = 0;
1545 bool fNoRing1Stack = true;
1546 if (cbMonitoredTss)
1547 {
1548 /*
1549 * 32-bit TSS. What we're really keen on is the SS0 and ESP0 fields.
1550 * If VME is enabled we also want to keep an eye on the interrupt
1551 * redirection bitmap.
1552 */
1553 VBOXTSS Tss;
1554 uint32_t cr4 = CPUMGetGuestCR4(pVCpu);
1555 rc = PGMPhysSimpleReadGCPtr(pVCpu, &Tss, GCPtrTss, RT_OFFSETOF(VBOXTSS, IntRedirBitmap));
1556 if ( !(cr4 & X86_CR4_VME)
1557 || ( RT_SUCCESS(rc)
1558 && Tss.offIoBitmap < sizeof(VBOXTSS) /* too small */
1559 && Tss.offIoBitmap > cbTss) /* beyond the end */ /** @todo not sure how the partial case is handled; probably not allowed. */
1560 )
1561 /* No interrupt redirection bitmap, just ESP0 and SS0. */
1562 cbMonitoredTss = RT_UOFFSETOF(VBOXTSS, padding_ss0);
1563 else if (RT_SUCCESS(rc))
1564 {
1565 /*
1566 * Everything up to and including the interrupt redirection bitmap. Unfortunately
1567 * this can be quite a large chunk. We use to skip it earlier and just hope it
1568 * was kind of static...
1569 *
1570 * Update the virtual interrupt redirection bitmap while we're here.
1571 * (It is located in the 32 bytes before TR:offIoBitmap.)
1572 */
1573 cbMonitoredTss = Tss.offIoBitmap;
1574 pVM->selm.s.offGuestIoBitmap = Tss.offIoBitmap;
1575
1576 uint32_t offRedirBitmap = Tss.offIoBitmap - sizeof(Tss.IntRedirBitmap);
1577 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pVM->selm.s.Tss.IntRedirBitmap,
1578 GCPtrTss + offRedirBitmap, sizeof(Tss.IntRedirBitmap));
1579 AssertRC(rc);
1580 /** @todo memset the bitmap on failure? */
1581 Log2(("Redirection bitmap:\n"));
1582 Log2(("%.*Rhxd\n", sizeof(Tss.IntRedirBitmap), &pVM->selm.s.Tss.IntRedirBitmap));
1583 }
1584 else
1585 {
1586 cbMonitoredTss = RT_OFFSETOF(VBOXTSS, IntRedirBitmap);
1587 pVM->selm.s.offGuestIoBitmap = 0;
1588 /** @todo memset the bitmap? */
1589 }
1590
1591 /*
1592 * Update the ring 0 stack selector and base address.
1593 */
1594 if (RT_SUCCESS(rc))
1595 {
1596# ifdef LOG_ENABLED
1597 if (LogIsEnabled())
1598 {
1599 uint32_t ssr0, espr0;
1600 SELMGetRing1Stack(pVM, &ssr0, &espr0);
1601 if ((ssr0 & ~1) != Tss.ss0 || espr0 != Tss.esp0)
1602 {
1603 RTGCPHYS GCPhys = NIL_RTGCPHYS;
1604 rc = PGMGstGetPage(pVCpu, GCPtrTss, NULL, &GCPhys); AssertRC(rc);
1605 Log(("SELMR3SyncTSS: Updating TSS ring 0 stack to %04X:%08X from %04X:%08X; TSS Phys=%RGp)\n",
1606 Tss.ss0, Tss.esp0, (ssr0 & ~1), espr0, GCPhys));
1607 AssertMsg(ssr0 != Tss.ss0,
1608 ("ring-1 leak into TSS.SS0! %04X:%08X from %04X:%08X; TSS Phys=%RGp)\n",
1609 Tss.ss0, Tss.esp0, (ssr0 & ~1), espr0, GCPhys));
1610 }
1611 Log(("offIoBitmap=%#x\n", Tss.offIoBitmap));
1612 }
1613# endif /* LOG_ENABLED */
1614 AssertMsg(!(Tss.ss0 & 3), ("ring-1 leak into TSS.SS0? %04X:%08X\n", Tss.ss0, Tss.esp0));
1615
1616 /* Update our TSS structure for the guest's ring 1 stack */
1617 selmSetRing1Stack(pVM, Tss.ss0 | 1, Tss.esp0);
1618 pVM->selm.s.fSyncTSSRing0Stack = fNoRing1Stack = false;
1619
1620# ifdef VBOX_WITH_RAW_RING1
1621 /* Update our TSS structure for the guest's ring 2 stack */
1622 if (EMIsRawRing1Enabled(pVM))
1623 {
1624 if ( (pVM->selm.s.Tss.ss2 != ((Tss.ss1 & ~2) | 1))
1625 || pVM->selm.s.Tss.esp2 != Tss.esp1)
1626 Log(("SELMR3SyncTSS: Updating TSS ring 1 stack to %04X:%08X from %04X:%08X\n", Tss.ss1, Tss.esp1, (pVM->selm.s.Tss.ss2 & ~2) | 1, pVM->selm.s.Tss.esp2));
1627 selmSetRing2Stack(pVM, (Tss.ss1 & ~1) | 2, Tss.esp1);
1628 }
1629# endif
1630 }
1631 }
1632
1633 /*
1634 * Flush the ring-1 stack and the direct syscall dispatching if we
1635 * cannot obtain SS0:ESP0.
1636 */
1637 if (fNoRing1Stack)
1638 {
1639 selmSetRing1Stack(pVM, 0 /* invalid SS */, 0);
1640 pVM->selm.s.fSyncTSSRing0Stack = cbMonitoredTss != 0;
1641
1642 /** @todo handle these dependencies better! */
1643 TRPMR3SetGuestTrapHandler(pVM, 0x2E, TRPM_INVALID_HANDLER);
1644 TRPMR3SetGuestTrapHandler(pVM, 0x80, TRPM_INVALID_HANDLER);
1645 }
1646
1647 /*
1648 * Check for monitor changes and apply them.
1649 */
1650 if ( GCPtrTss != pVM->selm.s.GCPtrGuestTss
1651 || cbMonitoredTss != pVM->selm.s.cbMonitoredGuestTss)
1652 {
1653 Log(("SELMR3SyncTSS: Guest's TSS is changed to pTss=%RGv cbMonitoredTss=%08X cbGuestTss=%#08x\n",
1654 GCPtrTss, cbMonitoredTss, pVM->selm.s.cbGuestTss));
1655
1656 /* Release the old range first. */
1657 if (pVM->selm.s.GCPtrGuestTss != RTRCPTR_MAX)
1658 {
1659 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, pVM->selm.s.GCPtrGuestTss, false /*fHypervisor*/);
1660 AssertRC(rc);
1661 }
1662
1663 /* Register the write handler if TS != 0. */
1664 if (cbMonitoredTss != 0)
1665 {
1666# ifdef SELM_TRACK_GUEST_TSS_CHANGES
1667 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hGuestTssWriteHandlerType,
1668 GCPtrTss, GCPtrTss + cbMonitoredTss - 1,
1669 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
1670 if (RT_FAILURE(rc))
1671 {
1672# ifdef VBOX_WITH_RAW_RING1
1673 /** @todo !HACK ALERT!
1674 * Some guest OSes (QNX) share code and the TSS on the same page;
1675 * PGMR3HandlerVirtualRegister doesn't support more than one
1676 * handler, so we kick out the PATM handler as this one is more
1677 * important. Fix this properly in PGMR3HandlerVirtualRegister?
1678 */
1679 if (rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT)
1680 {
1681 LogRel(("SELMR3SyncTSS: Virtual handler conflict %RGv -> kick out PATM handler for the higher priority TSS page monitor\n", GCPtrTss));
1682 rc = PGMHandlerVirtualDeregister(pVM, pVCpu, GCPtrTss & PAGE_BASE_GC_MASK, false /*fHypervisor*/);
1683 AssertRC(rc);
1684
1685 rc = PGMR3HandlerVirtualRegister(pVM, pVCpu, pVM->selm.s.hGuestTssWriteHandlerType,
1686 GCPtrTss, GCPtrTss + cbMonitoredTss - 1,
1687 NULL /*pvUserR3*/, NIL_RTR0PTR /*pvUserRC*/, NULL /*pszDesc*/);
1688 if (RT_FAILURE(rc))
1689 {
1690 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1691 return rc;
1692 }
1693 }
1694# else
1695 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1696 return rc;
1697# endif
1698 }
1699# endif /* SELM_TRACK_GUEST_TSS_CHANGES */
1700
1701 /* Update saved Guest TSS info. */
1702 pVM->selm.s.GCPtrGuestTss = GCPtrTss;
1703 pVM->selm.s.cbMonitoredGuestTss = cbMonitoredTss;
1704 pVM->selm.s.GCSelTss = SelTss;
1705 }
1706 else
1707 {
1708 pVM->selm.s.GCPtrGuestTss = RTRCPTR_MAX;
1709 pVM->selm.s.cbMonitoredGuestTss = 0;
1710 pVM->selm.s.GCSelTss = 0;
1711 }
1712 }
1713
1714 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
1715
1716 STAM_PROFILE_STOP(&pVM->selm.s.StatTSSSync, a);
1717 return VINF_SUCCESS;
1718}
1719
1720
1721/**
1722 * Compares the Guest GDT and LDT with the shadow tables.
1723 * This is a VBOX_STRICT only function.
1724 *
1725 * @returns VBox status code.
1726 * @param pVM The cross context VM structure.
1727 */
1728VMMR3DECL(int) SELMR3DebugCheck(PVM pVM)
1729{
1730# ifdef VBOX_STRICT
1731 PVMCPU pVCpu = VMMGetCpu(pVM);
1732 AssertReturn(!HMIsEnabled(pVM), VERR_SELM_HM_IPE);
1733
1734 /*
1735 * Get GDTR and check for conflict.
1736 */
1737 VBOXGDTR GDTR;
1738 CPUMGetGuestGDTR(pVCpu, &GDTR);
1739 if (GDTR.cbGdt == 0)
1740 return VINF_SUCCESS;
1741
1742 if (GDTR.cbGdt >= (unsigned)(pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> X86_SEL_SHIFT))
1743 Log(("SELMR3DebugCheck: guest GDT size forced us to look for unused selectors.\n"));
1744
1745 if (GDTR.cbGdt != pVM->selm.s.GuestGdtr.cbGdt)
1746 Log(("SELMR3DebugCheck: limits have changed! new=%d old=%d\n", GDTR.cbGdt, pVM->selm.s.GuestGdtr.cbGdt));
1747
1748 /*
1749 * Loop thru the GDT checking each entry.
1750 */
1751 RTGCPTR GCPtrGDTEGuest = GDTR.pGdt;
1752 PX86DESC pGDTE = pVM->selm.s.paGdtR3;
1753 PX86DESC pGDTEEnd = (PX86DESC)((uintptr_t)pGDTE + GDTR.cbGdt);
1754 while (pGDTE < pGDTEEnd)
1755 {
1756 X86DESC GDTEGuest;
1757 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &GDTEGuest, GCPtrGDTEGuest, sizeof(GDTEGuest));
1758 if (RT_SUCCESS(rc))
1759 {
1760 if (pGDTE->Gen.u1DescType || pGDTE->Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1761 {
1762 if ( pGDTE->Gen.u16LimitLow != GDTEGuest.Gen.u16LimitLow
1763 || pGDTE->Gen.u4LimitHigh != GDTEGuest.Gen.u4LimitHigh
1764 || pGDTE->Gen.u16BaseLow != GDTEGuest.Gen.u16BaseLow
1765 || pGDTE->Gen.u8BaseHigh1 != GDTEGuest.Gen.u8BaseHigh1
1766 || pGDTE->Gen.u8BaseHigh2 != GDTEGuest.Gen.u8BaseHigh2
1767 || pGDTE->Gen.u1DefBig != GDTEGuest.Gen.u1DefBig
1768 || pGDTE->Gen.u1DescType != GDTEGuest.Gen.u1DescType)
1769 {
1770 unsigned iGDT = pGDTE - pVM->selm.s.paGdtR3;
1771 SELMR3DumpDescriptor(*pGDTE, iGDT << 3, "SELMR3DebugCheck: GDT mismatch, shadow");
1772 SELMR3DumpDescriptor(GDTEGuest, iGDT << 3, "SELMR3DebugCheck: GDT mismatch, guest");
1773 }
1774 }
1775 }
1776
1777 /* Advance to the next descriptor. */
1778 GCPtrGDTEGuest += sizeof(X86DESC);
1779 pGDTE++;
1780 }
1781
1782
1783 /*
1784 * LDT?
1785 */
1786 RTSEL SelLdt = CPUMGetGuestLDTR(pVCpu);
1787 if ((SelLdt & X86_SEL_MASK_OFF_RPL) == 0)
1788 return VINF_SUCCESS;
1789 Assert(!(SelLdt & X86_SEL_LDT));
1790 if (SelLdt > GDTR.cbGdt)
1791 {
1792 Log(("SELMR3DebugCheck: ldt is out of bound SelLdt=%#x\n", SelLdt));
1793 return VERR_SELM_LDT_OUT_OF_BOUNDS;
1794 }
1795 X86DESC LDTDesc;
1796 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &LDTDesc, GDTR.pGdt + (SelLdt & X86_SEL_MASK), sizeof(LDTDesc));
1797 if (RT_FAILURE(rc))
1798 {
1799 Log(("SELMR3DebugCheck: Failed to read LDT descriptor. rc=%d\n", rc));
1800 return rc;
1801 }
1802 RTGCPTR GCPtrLDTEGuest = X86DESC_BASE(&LDTDesc);
1803 uint32_t cbLdt = X86DESC_LIMIT_G(&LDTDesc);
1804
1805 /*
1806 * Validate it.
1807 */
1808 if (!cbLdt)
1809 return VINF_SUCCESS;
1810 /** @todo check what intel does about odd limits. */
1811 AssertMsg(RT_ALIGN(cbLdt + 1, sizeof(X86DESC)) == cbLdt + 1 && cbLdt <= 0xffff, ("cbLdt=%d\n", cbLdt));
1812 if ( LDTDesc.Gen.u1DescType
1813 || LDTDesc.Gen.u4Type != X86_SEL_TYPE_SYS_LDT
1814 || SelLdt >= pVM->selm.s.GuestGdtr.cbGdt)
1815 {
1816 Log(("SELmR3DebugCheck: Invalid LDT %04x!\n", SelLdt));
1817 return VERR_SELM_INVALID_LDT;
1818 }
1819
1820 /*
1821 * Loop thru the LDT checking each entry.
1822 */
1823 unsigned off = (GCPtrLDTEGuest & PAGE_OFFSET_MASK);
1824 PX86DESC pLDTE = (PX86DESC)((uintptr_t)pVM->selm.s.pvLdtR3 + off);
1825 PX86DESC pLDTEEnd = (PX86DESC)((uintptr_t)pGDTE + cbLdt);
1826 while (pLDTE < pLDTEEnd)
1827 {
1828 X86DESC LDTEGuest;
1829 rc = PGMPhysSimpleReadGCPtr(pVCpu, &LDTEGuest, GCPtrLDTEGuest, sizeof(LDTEGuest));
1830 if (RT_SUCCESS(rc))
1831 {
1832 if ( pLDTE->Gen.u16LimitLow != LDTEGuest.Gen.u16LimitLow
1833 || pLDTE->Gen.u4LimitHigh != LDTEGuest.Gen.u4LimitHigh
1834 || pLDTE->Gen.u16BaseLow != LDTEGuest.Gen.u16BaseLow
1835 || pLDTE->Gen.u8BaseHigh1 != LDTEGuest.Gen.u8BaseHigh1
1836 || pLDTE->Gen.u8BaseHigh2 != LDTEGuest.Gen.u8BaseHigh2
1837 || pLDTE->Gen.u1DefBig != LDTEGuest.Gen.u1DefBig
1838 || pLDTE->Gen.u1DescType != LDTEGuest.Gen.u1DescType)
1839 {
1840 unsigned iLDT = pLDTE - (PX86DESC)((uintptr_t)pVM->selm.s.pvLdtR3 + off);
1841 SELMR3DumpDescriptor(*pLDTE, iLDT << 3, "SELMR3DebugCheck: LDT mismatch, shadow");
1842 SELMR3DumpDescriptor(LDTEGuest, iLDT << 3, "SELMR3DebugCheck: LDT mismatch, guest");
1843 }
1844 }
1845
1846 /* Advance to the next descriptor. */
1847 GCPtrLDTEGuest += sizeof(X86DESC);
1848 pLDTE++;
1849 }
1850
1851# else /* !VBOX_STRICT */
1852 NOREF(pVM);
1853# endif /* !VBOX_STRICT */
1854
1855 return VINF_SUCCESS;
1856}
1857
1858
1859/**
1860 * Validates the RawR0 TSS values against the one in the Guest TSS.
1861 *
1862 * @returns true if it matches.
1863 * @returns false and assertions on mismatch..
1864 * @param pVM The cross context VM structure.
1865 */
1866VMMR3DECL(bool) SELMR3CheckTSS(PVM pVM)
1867{
1868# if defined(VBOX_STRICT) && defined(SELM_TRACK_GUEST_TSS_CHANGES)
1869 PVMCPU pVCpu = VMMGetCpu(pVM);
1870
1871 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS))
1872 return true;
1873
1874 /*
1875 * Get TR and extract the basic info.
1876 */
1877 CPUMSELREGHID trHid;
1878 RTSEL SelTss = CPUMGetGuestTR(pVCpu, &trHid);
1879 RTGCPTR GCPtrTss = trHid.u64Base;
1880 uint32_t cbTss = trHid.u32Limit;
1881 Assert( (SelTss & X86_SEL_MASK_OFF_RPL)
1882 || (cbTss == 0 && GCPtrTss == 0 && trHid.Attr.u == 0 /* TR=0 */)
1883 || (cbTss == 0xffff && GCPtrTss == 0 && trHid.Attr.n.u1Present && trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY /* RESET */));
1884 if (SelTss & X86_SEL_MASK_OFF_RPL)
1885 {
1886 AssertReturn(!(SelTss & X86_SEL_LDT), false);
1887 AssertReturn(trHid.Attr.n.u1DescType == 0, false);
1888 AssertReturn( trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY
1889 || trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY,
1890 false);
1891 if (!++cbTss)
1892 cbTss = UINT32_MAX;
1893 }
1894 else
1895 {
1896 AssertReturn( (cbTss == 0 && GCPtrTss == 0 && trHid.Attr.u == 0 /* TR=0 */)
1897 || (cbTss == 0xffff && GCPtrTss == 0 && trHid.Attr.n.u1Present && trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY /* RESET */),
1898 false);
1899 cbTss = 0; /* the reset case. */
1900 }
1901 AssertMsgReturn(pVM->selm.s.cbGuestTss == cbTss, ("%#x %#x\n", pVM->selm.s.cbGuestTss, cbTss), false);
1902 AssertMsgReturn(pVM->selm.s.fGuestTss32Bit == ( trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL
1903 || trHid.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY),
1904 ("%RTbool u4Type=%d\n", pVM->selm.s.fGuestTss32Bit, trHid.Attr.n.u4Type),
1905 false);
1906 AssertMsgReturn( pVM->selm.s.GCSelTss == SelTss
1907 || (!pVM->selm.s.GCSelTss && !(SelTss & X86_SEL_LDT)),
1908 ("%#x %#x\n", pVM->selm.s.GCSelTss, SelTss),
1909 false);
1910 AssertMsgReturn( pVM->selm.s.GCPtrGuestTss == GCPtrTss
1911 || (pVM->selm.s.GCPtrGuestTss == RTRCPTR_MAX && !GCPtrTss),
1912 ("%#RGv %#RGv\n", pVM->selm.s.GCPtrGuestTss, GCPtrTss),
1913 false);
1914
1915
1916 /*
1917 * Figure out the size of what need to monitor.
1918 */
1919 /* We're not interested in any 16-bit TSSes. */
1920 uint32_t cbMonitoredTss = cbTss;
1921 if ( trHid.Attr.n.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL
1922 && trHid.Attr.n.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
1923 cbMonitoredTss = 0;
1924 if (cbMonitoredTss)
1925 {
1926 VBOXTSS Tss;
1927 uint32_t cr4 = CPUMGetGuestCR4(pVCpu);
1928 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &Tss, GCPtrTss, RT_OFFSETOF(VBOXTSS, IntRedirBitmap));
1929 AssertReturn( rc == VINF_SUCCESS
1930 /* Happens early in XP boot during page table switching. */
1931 || ( (rc == VERR_PAGE_TABLE_NOT_PRESENT || rc == VERR_PAGE_NOT_PRESENT)
1932 && !(CPUMGetGuestEFlags(pVCpu) & X86_EFL_IF)),
1933 false);
1934 if ( !(cr4 & X86_CR4_VME)
1935 || ( RT_SUCCESS(rc)
1936 && Tss.offIoBitmap < sizeof(VBOXTSS) /* too small */
1937 && Tss.offIoBitmap > cbTss)
1938 )
1939 cbMonitoredTss = RT_UOFFSETOF(VBOXTSS, padding_ss0);
1940 else if (RT_SUCCESS(rc))
1941 {
1942 cbMonitoredTss = Tss.offIoBitmap;
1943 AssertMsgReturn(pVM->selm.s.offGuestIoBitmap == Tss.offIoBitmap,
1944 ("%#x %#x\n", pVM->selm.s.offGuestIoBitmap, Tss.offIoBitmap),
1945 false);
1946
1947 /* check the bitmap */
1948 uint32_t offRedirBitmap = Tss.offIoBitmap - sizeof(Tss.IntRedirBitmap);
1949 rc = PGMPhysSimpleReadGCPtr(pVCpu, &Tss.IntRedirBitmap,
1950 GCPtrTss + offRedirBitmap, sizeof(Tss.IntRedirBitmap));
1951 AssertRCReturn(rc, false);
1952 AssertMsgReturn(!memcmp(&Tss.IntRedirBitmap[0], &pVM->selm.s.Tss.IntRedirBitmap[0], sizeof(Tss.IntRedirBitmap)),
1953 ("offIoBitmap=%#x cbTss=%#x\n"
1954 " Guest: %.32Rhxs\n"
1955 "Shadow: %.32Rhxs\n",
1956 Tss.offIoBitmap, cbTss,
1957 &Tss.IntRedirBitmap[0],
1958 &pVM->selm.s.Tss.IntRedirBitmap[0]),
1959 false);
1960 }
1961 else
1962 cbMonitoredTss = RT_OFFSETOF(VBOXTSS, IntRedirBitmap);
1963
1964 /*
1965 * Check SS0 and ESP0.
1966 */
1967 if ( !pVM->selm.s.fSyncTSSRing0Stack
1968 && RT_SUCCESS(rc))
1969 {
1970 if ( Tss.esp0 != pVM->selm.s.Tss.esp1
1971 || Tss.ss0 != (pVM->selm.s.Tss.ss1 & ~1))
1972 {
1973 RTGCPHYS GCPhys;
1974 rc = PGMGstGetPage(pVCpu, GCPtrTss, NULL, &GCPhys); AssertRC(rc);
1975 AssertMsgFailed(("TSS out of sync!! (%04X:%08X vs %04X:%08X (guest)) Tss=%RGv Phys=%RGp\n",
1976 (pVM->selm.s.Tss.ss1 & ~1), pVM->selm.s.Tss.esp1,
1977 Tss.ss1, Tss.esp1, GCPtrTss, GCPhys));
1978 return false;
1979 }
1980 }
1981 AssertMsgReturn(pVM->selm.s.cbMonitoredGuestTss == cbMonitoredTss, ("%#x %#x\n", pVM->selm.s.cbMonitoredGuestTss, cbMonitoredTss), false);
1982 }
1983 else
1984 {
1985 AssertMsgReturn(pVM->selm.s.Tss.ss1 == 0 && pVM->selm.s.Tss.esp1 == 0, ("%04x:%08x\n", pVM->selm.s.Tss.ss1, pVM->selm.s.Tss.esp1), false);
1986 AssertReturn(!pVM->selm.s.fSyncTSSRing0Stack, false);
1987 AssertMsgReturn(pVM->selm.s.cbMonitoredGuestTss == cbMonitoredTss, ("%#x %#x\n", pVM->selm.s.cbMonitoredGuestTss, cbMonitoredTss), false);
1988 }
1989
1990
1991
1992 return true;
1993
1994# else /* !VBOX_STRICT */
1995 NOREF(pVM);
1996 return true;
1997# endif /* !VBOX_STRICT */
1998}
1999
2000
2001# ifdef VBOX_WITH_SAFE_STR
2002/**
2003 * Validates the RawR0 TR shadow GDT entry.
2004 *
2005 * @returns true if it matches.
2006 * @returns false and assertions on mismatch..
2007 * @param pVM The cross context VM structure.
2008 */
2009VMMR3DECL(bool) SELMR3CheckShadowTR(PVM pVM)
2010{
2011# ifdef VBOX_STRICT
2012 PX86DESC paGdt = pVM->selm.s.paGdtR3;
2013
2014 /*
2015 * TSS descriptor
2016 */
2017 PX86DESC pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> 3];
2018 RTRCPTR RCPtrTSS = VM_RC_ADDR(pVM, &pVM->selm.s.Tss);
2019
2020 if ( pDesc->Gen.u16BaseLow != RT_LOWORD(RCPtrTSS)
2021 || pDesc->Gen.u8BaseHigh1 != RT_BYTE3(RCPtrTSS)
2022 || pDesc->Gen.u8BaseHigh2 != RT_BYTE4(RCPtrTSS)
2023 || pDesc->Gen.u16LimitLow != sizeof(VBOXTSS) - 1
2024 || pDesc->Gen.u4LimitHigh != 0
2025 || (pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL && pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
2026 || pDesc->Gen.u1DescType != 0 /* system */
2027 || pDesc->Gen.u2Dpl != 0 /* supervisor */
2028 || pDesc->Gen.u1Present != 1
2029 || pDesc->Gen.u1Available != 0
2030 || pDesc->Gen.u1Long != 0
2031 || pDesc->Gen.u1DefBig != 0
2032 || pDesc->Gen.u1Granularity != 0 /* byte limit */
2033 )
2034 {
2035 AssertFailed();
2036 return false;
2037 }
2038# endif
2039 return true;
2040}
2041# endif /* VBOX_WITH_SAFE_STR */
2042
2043#endif /* VBOX_WITH_RAW_MODE */
2044
2045/**
2046 * Gets information about a 64-bit selector, SELMR3GetSelectorInfo helper.
2047 *
2048 * See SELMR3GetSelectorInfo for details.
2049 *
2050 * @returns VBox status code, see SELMR3GetSelectorInfo for details.
2051 *
2052 * @param pVCpu The cross context virtual CPU structure.
2053 * @param Sel The selector to get info about.
2054 * @param pSelInfo Where to store the information.
2055 */
2056static int selmR3GetSelectorInfo64(PVMCPU pVCpu, RTSEL Sel, PDBGFSELINFO pSelInfo)
2057{
2058 /*
2059 * Read it from the guest descriptor table.
2060 */
2061/** @todo this is bogus wrt the LDT/GDT limit on long selectors. */
2062 X86DESC64 Desc;
2063 RTGCPTR GCPtrDesc;
2064 if (!(Sel & X86_SEL_LDT))
2065 {
2066 /* GDT */
2067 VBOXGDTR Gdtr;
2068 CPUMGetGuestGDTR(pVCpu, &Gdtr);
2069 if ((Sel | X86_SEL_RPL_LDT) > Gdtr.cbGdt)
2070 return VERR_INVALID_SELECTOR;
2071 GCPtrDesc = Gdtr.pGdt + (Sel & X86_SEL_MASK);
2072 }
2073 else
2074 {
2075 /* LDT */
2076 uint64_t GCPtrBase;
2077 uint32_t cbLimit;
2078 CPUMGetGuestLdtrEx(pVCpu, &GCPtrBase, &cbLimit);
2079 if ((Sel | X86_SEL_RPL_LDT) > cbLimit)
2080 return VERR_INVALID_SELECTOR;
2081
2082 /* calc the descriptor location. */
2083 GCPtrDesc = GCPtrBase + (Sel & X86_SEL_MASK);
2084 }
2085
2086 /* read the descriptor. */
2087 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &Desc, GCPtrDesc, sizeof(Desc));
2088 if (RT_FAILURE(rc))
2089 {
2090 rc = PGMPhysSimpleReadGCPtr(pVCpu, &Desc, GCPtrDesc, sizeof(X86DESC));
2091 if (RT_FAILURE(rc))
2092 return rc;
2093 Desc.au64[1] = 0;
2094 }
2095
2096 /*
2097 * Extract the base and limit
2098 * (We ignore the present bit here, which is probably a bit silly...)
2099 */
2100 pSelInfo->Sel = Sel;
2101 pSelInfo->fFlags = DBGFSELINFO_FLAGS_LONG_MODE;
2102 pSelInfo->u.Raw64 = Desc;
2103 if (Desc.Gen.u1DescType)
2104 {
2105 /*
2106 * 64-bit code selectors are wide open, it's not possible to detect
2107 * 64-bit data or stack selectors without also dragging in assumptions
2108 * about current CS (i.e. that's we're executing in 64-bit mode). So,
2109 * the selinfo user needs to deal with this in the context the info is
2110 * used unfortunately.
2111 */
2112 if ( Desc.Gen.u1Long
2113 && !Desc.Gen.u1DefBig
2114 && (Desc.Gen.u4Type & X86_SEL_TYPE_CODE))
2115 {
2116 /* Note! We ignore the segment limit hacks that was added by AMD. */
2117 pSelInfo->GCPtrBase = 0;
2118 pSelInfo->cbLimit = ~(RTGCUINTPTR)0;
2119 }
2120 else
2121 {
2122 pSelInfo->cbLimit = X86DESC_LIMIT_G(&Desc);
2123 pSelInfo->GCPtrBase = X86DESC_BASE(&Desc);
2124 }
2125 pSelInfo->SelGate = 0;
2126 }
2127 else if ( Desc.Gen.u4Type == AMD64_SEL_TYPE_SYS_LDT
2128 || Desc.Gen.u4Type == AMD64_SEL_TYPE_SYS_TSS_AVAIL
2129 || Desc.Gen.u4Type == AMD64_SEL_TYPE_SYS_TSS_BUSY)
2130 {
2131 /* Note. LDT descriptors are weird in long mode, we ignore the footnote
2132 in the AMD manual here as a simplification. */
2133 pSelInfo->GCPtrBase = X86DESC64_BASE(&Desc);
2134 pSelInfo->cbLimit = X86DESC_LIMIT_G(&Desc);
2135 pSelInfo->SelGate = 0;
2136 }
2137 else if ( Desc.Gen.u4Type == AMD64_SEL_TYPE_SYS_CALL_GATE
2138 || Desc.Gen.u4Type == AMD64_SEL_TYPE_SYS_TRAP_GATE
2139 || Desc.Gen.u4Type == AMD64_SEL_TYPE_SYS_INT_GATE)
2140 {
2141 pSelInfo->cbLimit = X86DESC64_BASE(&Desc);
2142 pSelInfo->GCPtrBase = Desc.Gate.u16OffsetLow
2143 | ((uint32_t)Desc.Gate.u16OffsetHigh << 16)
2144 | ((uint64_t)Desc.Gate.u32OffsetTop << 32);
2145 pSelInfo->SelGate = Desc.Gate.u16Sel;
2146 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_GATE;
2147 }
2148 else
2149 {
2150 pSelInfo->cbLimit = 0;
2151 pSelInfo->GCPtrBase = 0;
2152 pSelInfo->SelGate = 0;
2153 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_INVALID;
2154 }
2155 if (!Desc.Gen.u1Present)
2156 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_NOT_PRESENT;
2157
2158 return VINF_SUCCESS;
2159}
2160
2161
2162/**
2163 * Worker for selmR3GetSelectorInfo32 and SELMR3GetShadowSelectorInfo that
2164 * interprets a legacy descriptor table entry and fills in the selector info
2165 * structure from it.
2166 *
2167 * @param pSelInfo Where to store the selector info. Only the fFlags and
2168 * Sel members have been initialized.
2169 * @param pDesc The legacy descriptor to parse.
2170 */
2171DECLINLINE(void) selmR3SelInfoFromDesc32(PDBGFSELINFO pSelInfo, PCX86DESC pDesc)
2172{
2173 pSelInfo->u.Raw64.au64[1] = 0;
2174 pSelInfo->u.Raw = *pDesc;
2175 if ( pDesc->Gen.u1DescType
2176 || !(pDesc->Gen.u4Type & 4))
2177 {
2178 pSelInfo->cbLimit = X86DESC_LIMIT_G(pDesc);
2179 pSelInfo->GCPtrBase = X86DESC_BASE(pDesc);
2180 pSelInfo->SelGate = 0;
2181 }
2182 else if (pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_UNDEFINED4)
2183 {
2184 pSelInfo->cbLimit = 0;
2185 if (pDesc->Gen.u4Type == X86_SEL_TYPE_SYS_TASK_GATE)
2186 pSelInfo->GCPtrBase = 0;
2187 else
2188 pSelInfo->GCPtrBase = pDesc->Gate.u16OffsetLow
2189 | (uint32_t)pDesc->Gate.u16OffsetHigh << 16;
2190 pSelInfo->SelGate = pDesc->Gate.u16Sel;
2191 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_GATE;
2192 }
2193 else
2194 {
2195 pSelInfo->cbLimit = 0;
2196 pSelInfo->GCPtrBase = 0;
2197 pSelInfo->SelGate = 0;
2198 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_INVALID;
2199 }
2200 if (!pDesc->Gen.u1Present)
2201 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_NOT_PRESENT;
2202}
2203
2204
2205/**
2206 * Gets information about a 64-bit selector, SELMR3GetSelectorInfo helper.
2207 *
2208 * See SELMR3GetSelectorInfo for details.
2209 *
2210 * @returns VBox status code, see SELMR3GetSelectorInfo for details.
2211 *
2212 * @param pVM The cross context VM structure.
2213 * @param pVCpu The cross context virtual CPU structure.
2214 * @param Sel The selector to get info about.
2215 * @param pSelInfo Where to store the information.
2216 */
2217static int selmR3GetSelectorInfo32(PVM pVM, PVMCPU pVCpu, RTSEL Sel, PDBGFSELINFO pSelInfo)
2218{
2219 /*
2220 * Read the descriptor entry
2221 */
2222 pSelInfo->fFlags = 0;
2223 X86DESC Desc;
2224 if ( !(Sel & X86_SEL_LDT)
2225 && ( pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] == (Sel & X86_SEL_RPL_LDT)
2226 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] == (Sel & X86_SEL_RPL_LDT)
2227 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] == (Sel & X86_SEL_RPL_LDT)
2228 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] == (Sel & X86_SEL_RPL_LDT)
2229 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] == (Sel & X86_SEL_RPL_LDT))
2230 )
2231 {
2232 /*
2233 * Hypervisor descriptor.
2234 */
2235 pSelInfo->fFlags = DBGFSELINFO_FLAGS_HYPER;
2236 if (CPUMIsGuestInProtectedMode(pVCpu))
2237 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_PROT_MODE;
2238 else
2239 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_REAL_MODE;
2240
2241 Desc = pVM->selm.s.paGdtR3[Sel >> X86_SEL_SHIFT];
2242 }
2243 else if (CPUMIsGuestInProtectedMode(pVCpu))
2244 {
2245 /*
2246 * Read it from the guest descriptor table.
2247 */
2248 pSelInfo->fFlags = DBGFSELINFO_FLAGS_PROT_MODE;
2249
2250 RTGCPTR GCPtrDesc;
2251 if (!(Sel & X86_SEL_LDT))
2252 {
2253 /* GDT */
2254 VBOXGDTR Gdtr;
2255 CPUMGetGuestGDTR(pVCpu, &Gdtr);
2256 if ((Sel | X86_SEL_RPL_LDT) > Gdtr.cbGdt)
2257 return VERR_INVALID_SELECTOR;
2258 GCPtrDesc = Gdtr.pGdt + (Sel & X86_SEL_MASK);
2259 }
2260 else
2261 {
2262 /* LDT */
2263 uint64_t GCPtrBase;
2264 uint32_t cbLimit;
2265 CPUMGetGuestLdtrEx(pVCpu, &GCPtrBase, &cbLimit);
2266 if ((Sel | X86_SEL_RPL_LDT) > cbLimit)
2267 return VERR_INVALID_SELECTOR;
2268
2269 /* calc the descriptor location. */
2270 GCPtrDesc = GCPtrBase + (Sel & X86_SEL_MASK);
2271 }
2272
2273 /* read the descriptor. */
2274 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &Desc, GCPtrDesc, sizeof(Desc));
2275 if (RT_FAILURE(rc))
2276 return rc;
2277 }
2278 else
2279 {
2280 /*
2281 * We're in real mode.
2282 */
2283 pSelInfo->Sel = Sel;
2284 pSelInfo->GCPtrBase = Sel << 4;
2285 pSelInfo->cbLimit = 0xffff;
2286 pSelInfo->fFlags = DBGFSELINFO_FLAGS_REAL_MODE;
2287 pSelInfo->u.Raw64.au64[0] = 0;
2288 pSelInfo->u.Raw64.au64[1] = 0;
2289 pSelInfo->SelGate = 0;
2290 return VINF_SUCCESS;
2291 }
2292
2293 /*
2294 * Extract the base and limit or sel:offset for gates.
2295 */
2296 pSelInfo->Sel = Sel;
2297 selmR3SelInfoFromDesc32(pSelInfo, &Desc);
2298
2299 return VINF_SUCCESS;
2300}
2301
2302
2303/**
2304 * Gets information about a selector.
2305 *
2306 * Intended for the debugger mostly and will prefer the guest descriptor tables
2307 * over the shadow ones.
2308 *
2309 * @retval VINF_SUCCESS on success.
2310 * @retval VERR_INVALID_SELECTOR if the selector isn't fully inside the
2311 * descriptor table.
2312 * @retval VERR_SELECTOR_NOT_PRESENT if the LDT is invalid or not present. This
2313 * is not returned if the selector itself isn't present, you have to
2314 * check that for yourself (see DBGFSELINFO::fFlags).
2315 * @retval VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the
2316 * pagetable or page backing the selector table wasn't present.
2317 * @returns Other VBox status code on other errors.
2318 *
2319 * @param pVM The cross context VM structure.
2320 * @param pVCpu The cross context virtual CPU structure.
2321 * @param Sel The selector to get info about.
2322 * @param pSelInfo Where to store the information.
2323 */
2324VMMR3DECL(int) SELMR3GetSelectorInfo(PVM pVM, PVMCPU pVCpu, RTSEL Sel, PDBGFSELINFO pSelInfo)
2325{
2326 AssertPtr(pSelInfo);
2327 if (CPUMIsGuestInLongMode(pVCpu))
2328 return selmR3GetSelectorInfo64(pVCpu, Sel, pSelInfo);
2329 return selmR3GetSelectorInfo32(pVM, pVCpu, Sel, pSelInfo);
2330}
2331
2332
2333/**
2334 * Gets information about a selector from the shadow tables.
2335 *
2336 * This is intended to be faster than the SELMR3GetSelectorInfo() method, but
2337 * requires that the caller ensures that the shadow tables are up to date.
2338 *
2339 * @retval VINF_SUCCESS on success.
2340 * @retval VERR_INVALID_SELECTOR if the selector isn't fully inside the
2341 * descriptor table.
2342 * @retval VERR_SELECTOR_NOT_PRESENT if the LDT is invalid or not present. This
2343 * is not returned if the selector itself isn't present, you have to
2344 * check that for yourself (see DBGFSELINFO::fFlags).
2345 * @retval VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the
2346 * pagetable or page backing the selector table wasn't present.
2347 * @returns Other VBox status code on other errors.
2348 *
2349 * @param pVM The cross context VM structure.
2350 * @param Sel The selector to get info about.
2351 * @param pSelInfo Where to store the information.
2352 *
2353 * @remarks Don't use this when in hardware assisted virtualization mode.
2354 */
2355VMMR3DECL(int) SELMR3GetShadowSelectorInfo(PVM pVM, RTSEL Sel, PDBGFSELINFO pSelInfo)
2356{
2357 Assert(pSelInfo);
2358
2359 /*
2360 * Read the descriptor entry
2361 */
2362 X86DESC Desc;
2363 if (!(Sel & X86_SEL_LDT))
2364 {
2365 /*
2366 * Global descriptor.
2367 */
2368 Desc = pVM->selm.s.paGdtR3[Sel >> X86_SEL_SHIFT];
2369 pSelInfo->fFlags = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] == (Sel & X86_SEL_MASK_OFF_RPL)
2370 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] == (Sel & X86_SEL_MASK_OFF_RPL)
2371 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] == (Sel & X86_SEL_MASK_OFF_RPL)
2372 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] == (Sel & X86_SEL_MASK_OFF_RPL)
2373 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] == (Sel & X86_SEL_MASK_OFF_RPL)
2374 ? DBGFSELINFO_FLAGS_HYPER
2375 : 0;
2376 /** @todo check that the GDT offset is valid. */
2377 }
2378 else
2379 {
2380 /*
2381 * Local Descriptor.
2382 */
2383 PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.pvLdtR3 + pVM->selm.s.offLdtHyper);
2384 Desc = paLDT[Sel >> X86_SEL_SHIFT];
2385 /** @todo check if the LDT page is actually available. */
2386 /** @todo check that the LDT offset is valid. */
2387 pSelInfo->fFlags = 0;
2388 }
2389 if (CPUMIsGuestInProtectedMode(VMMGetCpu0(pVM)))
2390 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_PROT_MODE;
2391 else
2392 pSelInfo->fFlags |= DBGFSELINFO_FLAGS_REAL_MODE;
2393
2394 /*
2395 * Extract the base and limit or sel:offset for gates.
2396 */
2397 pSelInfo->Sel = Sel;
2398 selmR3SelInfoFromDesc32(pSelInfo, &Desc);
2399
2400 return VINF_SUCCESS;
2401}
2402
2403
2404/**
2405 * Formats a descriptor.
2406 *
2407 * @param Desc Descriptor to format.
2408 * @param Sel Selector number.
2409 * @param pszOutput Output buffer.
2410 * @param cchOutput Size of output buffer.
2411 */
2412static void selmR3FormatDescriptor(X86DESC Desc, RTSEL Sel, char *pszOutput, size_t cchOutput)
2413{
2414 /*
2415 * Make variable description string.
2416 */
2417 static struct
2418 {
2419 unsigned cch;
2420 const char *psz;
2421 } const aTypes[32] =
2422 {
2423#define STRENTRY(str) { sizeof(str) - 1, str }
2424 /* system */
2425 STRENTRY("Reserved0 "), /* 0x00 */
2426 STRENTRY("TSS16Avail "), /* 0x01 */
2427 STRENTRY("LDT "), /* 0x02 */
2428 STRENTRY("TSS16Busy "), /* 0x03 */
2429 STRENTRY("Call16 "), /* 0x04 */
2430 STRENTRY("Task "), /* 0x05 */
2431 STRENTRY("Int16 "), /* 0x06 */
2432 STRENTRY("Trap16 "), /* 0x07 */
2433 STRENTRY("Reserved8 "), /* 0x08 */
2434 STRENTRY("TSS32Avail "), /* 0x09 */
2435 STRENTRY("ReservedA "), /* 0x0a */
2436 STRENTRY("TSS32Busy "), /* 0x0b */
2437 STRENTRY("Call32 "), /* 0x0c */
2438 STRENTRY("ReservedD "), /* 0x0d */
2439 STRENTRY("Int32 "), /* 0x0e */
2440 STRENTRY("Trap32 "), /* 0x0f */
2441 /* non system */
2442 STRENTRY("DataRO "), /* 0x10 */
2443 STRENTRY("DataRO Accessed "), /* 0x11 */
2444 STRENTRY("DataRW "), /* 0x12 */
2445 STRENTRY("DataRW Accessed "), /* 0x13 */
2446 STRENTRY("DataDownRO "), /* 0x14 */
2447 STRENTRY("DataDownRO Accessed "), /* 0x15 */
2448 STRENTRY("DataDownRW "), /* 0x16 */
2449 STRENTRY("DataDownRW Accessed "), /* 0x17 */
2450 STRENTRY("CodeEO "), /* 0x18 */
2451 STRENTRY("CodeEO Accessed "), /* 0x19 */
2452 STRENTRY("CodeER "), /* 0x1a */
2453 STRENTRY("CodeER Accessed "), /* 0x1b */
2454 STRENTRY("CodeConfEO "), /* 0x1c */
2455 STRENTRY("CodeConfEO Accessed "), /* 0x1d */
2456 STRENTRY("CodeConfER "), /* 0x1e */
2457 STRENTRY("CodeConfER Accessed ") /* 0x1f */
2458#undef SYSENTRY
2459 };
2460#define ADD_STR(psz, pszAdd) do { strcpy(psz, pszAdd); psz += strlen(pszAdd); } while (0)
2461 char szMsg[128];
2462 char *psz = &szMsg[0];
2463 unsigned i = Desc.Gen.u1DescType << 4 | Desc.Gen.u4Type;
2464 memcpy(psz, aTypes[i].psz, aTypes[i].cch);
2465 psz += aTypes[i].cch;
2466
2467 if (Desc.Gen.u1Present)
2468 ADD_STR(psz, "Present ");
2469 else
2470 ADD_STR(psz, "Not-Present ");
2471 if (Desc.Gen.u1Granularity)
2472 ADD_STR(psz, "Page ");
2473 if (Desc.Gen.u1DefBig)
2474 ADD_STR(psz, "32-bit ");
2475 else
2476 ADD_STR(psz, "16-bit ");
2477#undef ADD_STR
2478 *psz = '\0';
2479
2480 /*
2481 * Limit and Base and format the output.
2482 */
2483 uint32_t u32Limit = X86DESC_LIMIT_G(&Desc);
2484 uint32_t u32Base = X86DESC_BASE(&Desc);
2485
2486 RTStrPrintf(pszOutput, cchOutput, "%04x - %08x %08x - base=%08x limit=%08x dpl=%d %s",
2487 Sel, Desc.au32[0], Desc.au32[1], u32Base, u32Limit, Desc.Gen.u2Dpl, szMsg);
2488}
2489
2490
2491/**
2492 * Dumps a descriptor.
2493 *
2494 * @param Desc Descriptor to dump.
2495 * @param Sel Selector number.
2496 * @param pszMsg Message to prepend the log entry with.
2497 */
2498VMMR3DECL(void) SELMR3DumpDescriptor(X86DESC Desc, RTSEL Sel, const char *pszMsg)
2499{
2500 char szOutput[128];
2501 selmR3FormatDescriptor(Desc, Sel, &szOutput[0], sizeof(szOutput));
2502 Log(("%s: %s\n", pszMsg, szOutput));
2503 NOREF(szOutput[0]);
2504}
2505
2506
2507/**
2508 * Display the shadow gdt.
2509 *
2510 * @param pVM The cross context VM structure.
2511 * @param pHlp The info helpers.
2512 * @param pszArgs Arguments, ignored.
2513 */
2514static DECLCALLBACK(void) selmR3InfoGdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2515{
2516 NOREF(pszArgs);
2517 pHlp->pfnPrintf(pHlp, "Shadow GDT (GCAddr=%RRv):\n", MMHyperR3ToRC(pVM, pVM->selm.s.paGdtR3));
2518 for (unsigned iGDT = 0; iGDT < SELM_GDT_ELEMENTS; iGDT++)
2519 {
2520 if (pVM->selm.s.paGdtR3[iGDT].Gen.u1Present)
2521 {
2522 char szOutput[128];
2523 selmR3FormatDescriptor(pVM->selm.s.paGdtR3[iGDT], iGDT << X86_SEL_SHIFT, &szOutput[0], sizeof(szOutput));
2524 const char *psz = "";
2525 if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] >> X86_SEL_SHIFT))
2526 psz = " HyperCS";
2527 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] >> X86_SEL_SHIFT))
2528 psz = " HyperDS";
2529 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] >> X86_SEL_SHIFT))
2530 psz = " HyperCS64";
2531 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> X86_SEL_SHIFT))
2532 psz = " HyperTSS";
2533 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> X86_SEL_SHIFT))
2534 psz = " HyperTSSTrap08";
2535 pHlp->pfnPrintf(pHlp, "%s%s\n", szOutput, psz);
2536 }
2537 }
2538}
2539
2540
2541/**
2542 * Display the guest gdt.
2543 *
2544 * @param pVM The cross context VM structure.
2545 * @param pHlp The info helpers.
2546 * @param pszArgs Arguments, ignored.
2547 */
2548static DECLCALLBACK(void) selmR3InfoGdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2549{
2550 /** @todo SMP support! */
2551 PVMCPU pVCpu = &pVM->aCpus[0];
2552
2553 VBOXGDTR GDTR;
2554 CPUMGetGuestGDTR(pVCpu, &GDTR);
2555 RTGCPTR GCPtrGDT = GDTR.pGdt;
2556 unsigned cGDTs = ((unsigned)GDTR.cbGdt + 1) / sizeof(X86DESC);
2557
2558 pHlp->pfnPrintf(pHlp, "Guest GDT (GCAddr=%RGv limit=%x):\n", GCPtrGDT, GDTR.cbGdt);
2559 for (unsigned iGDT = 0; iGDT < cGDTs; iGDT++, GCPtrGDT += sizeof(X86DESC))
2560 {
2561 X86DESC GDTE;
2562 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &GDTE, GCPtrGDT, sizeof(GDTE));
2563 if (RT_SUCCESS(rc))
2564 {
2565 if (GDTE.Gen.u1Present)
2566 {
2567 char szOutput[128];
2568 selmR3FormatDescriptor(GDTE, iGDT << X86_SEL_SHIFT, &szOutput[0], sizeof(szOutput));
2569 pHlp->pfnPrintf(pHlp, "%s\n", szOutput);
2570 }
2571 }
2572 else if (rc == VERR_PAGE_NOT_PRESENT)
2573 {
2574 if ((GCPtrGDT & PAGE_OFFSET_MASK) + sizeof(X86DESC) - 1 < sizeof(X86DESC))
2575 pHlp->pfnPrintf(pHlp, "%04x - page not present (GCAddr=%RGv)\n", iGDT << X86_SEL_SHIFT, GCPtrGDT);
2576 }
2577 else
2578 pHlp->pfnPrintf(pHlp, "%04x - read error rc=%Rrc GCAddr=%RGv\n", iGDT << X86_SEL_SHIFT, rc, GCPtrGDT);
2579 }
2580 NOREF(pszArgs);
2581}
2582
2583
2584/**
2585 * Display the shadow ldt.
2586 *
2587 * @param pVM The cross context VM structure.
2588 * @param pHlp The info helpers.
2589 * @param pszArgs Arguments, ignored.
2590 */
2591static DECLCALLBACK(void) selmR3InfoLdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2592{
2593 unsigned cLDTs = ((unsigned)pVM->selm.s.cbLdtLimit + 1) >> X86_SEL_SHIFT;
2594 PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.pvLdtR3 + pVM->selm.s.offLdtHyper);
2595 pHlp->pfnPrintf(pHlp, "Shadow LDT (GCAddr=%RRv limit=%#x):\n", pVM->selm.s.pvLdtRC + pVM->selm.s.offLdtHyper, pVM->selm.s.cbLdtLimit);
2596 for (unsigned iLDT = 0; iLDT < cLDTs; iLDT++)
2597 {
2598 if (paLDT[iLDT].Gen.u1Present)
2599 {
2600 char szOutput[128];
2601 selmR3FormatDescriptor(paLDT[iLDT], (iLDT << X86_SEL_SHIFT) | X86_SEL_LDT, &szOutput[0], sizeof(szOutput));
2602 pHlp->pfnPrintf(pHlp, "%s\n", szOutput);
2603 }
2604 }
2605 NOREF(pszArgs);
2606}
2607
2608
2609/**
2610 * Display the guest ldt.
2611 *
2612 * @param pVM The cross context VM structure.
2613 * @param pHlp The info helpers.
2614 * @param pszArgs Arguments, ignored.
2615 */
2616static DECLCALLBACK(void) selmR3InfoLdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2617{
2618 /** @todo SMP support! */
2619 PVMCPU pVCpu = &pVM->aCpus[0];
2620
2621 uint64_t GCPtrLdt;
2622 uint32_t cbLdt;
2623 RTSEL SelLdt = CPUMGetGuestLdtrEx(pVCpu, &GCPtrLdt, &cbLdt);
2624 if (!(SelLdt & X86_SEL_MASK_OFF_RPL))
2625 {
2626 pHlp->pfnPrintf(pHlp, "Guest LDT (Sel=%x): Null-Selector\n", SelLdt);
2627 return;
2628 }
2629
2630 pHlp->pfnPrintf(pHlp, "Guest LDT (Sel=%x GCAddr=%RX64 limit=%x):\n", SelLdt, GCPtrLdt, cbLdt);
2631 unsigned cLdts = (cbLdt + 1) >> X86_SEL_SHIFT;
2632 for (unsigned iLdt = 0; iLdt < cLdts; iLdt++, GCPtrLdt += sizeof(X86DESC))
2633 {
2634 X86DESC LdtE;
2635 int rc = PGMPhysSimpleReadGCPtr(pVCpu, &LdtE, GCPtrLdt, sizeof(LdtE));
2636 if (RT_SUCCESS(rc))
2637 {
2638 if (LdtE.Gen.u1Present)
2639 {
2640 char szOutput[128];
2641 selmR3FormatDescriptor(LdtE, (iLdt << X86_SEL_SHIFT) | X86_SEL_LDT, &szOutput[0], sizeof(szOutput));
2642 pHlp->pfnPrintf(pHlp, "%s\n", szOutput);
2643 }
2644 }
2645 else if (rc == VERR_PAGE_NOT_PRESENT)
2646 {
2647 if ((GCPtrLdt & PAGE_OFFSET_MASK) + sizeof(X86DESC) - 1 < sizeof(X86DESC))
2648 pHlp->pfnPrintf(pHlp, "%04x - page not present (GCAddr=%RGv)\n", (iLdt << X86_SEL_SHIFT) | X86_SEL_LDT, GCPtrLdt);
2649 }
2650 else
2651 pHlp->pfnPrintf(pHlp, "%04x - read error rc=%Rrc GCAddr=%RGv\n", (iLdt << X86_SEL_SHIFT) | X86_SEL_LDT, rc, GCPtrLdt);
2652 }
2653 NOREF(pszArgs);
2654}
2655
2656
2657/**
2658 * Dumps the hypervisor GDT
2659 *
2660 * @param pVM The cross context VM structure.
2661 */
2662VMMR3DECL(void) SELMR3DumpHyperGDT(PVM pVM)
2663{
2664 DBGFR3Info(pVM->pUVM, "gdt", NULL, NULL);
2665}
2666
2667
2668/**
2669 * Dumps the hypervisor LDT
2670 *
2671 * @param pVM The cross context VM structure.
2672 */
2673VMMR3DECL(void) SELMR3DumpHyperLDT(PVM pVM)
2674{
2675 DBGFR3Info(pVM->pUVM, "ldt", NULL, NULL);
2676}
2677
2678
2679/**
2680 * Dumps the guest GDT
2681 *
2682 * @param pVM The cross context VM structure.
2683 */
2684VMMR3DECL(void) SELMR3DumpGuestGDT(PVM pVM)
2685{
2686 DBGFR3Info(pVM->pUVM, "gdtguest", NULL, NULL);
2687}
2688
2689
2690/**
2691 * Dumps the guest LDT
2692 *
2693 * @param pVM The cross context VM structure.
2694 */
2695VMMR3DECL(void) SELMR3DumpGuestLDT(PVM pVM)
2696{
2697 DBGFR3Info(pVM->pUVM, "ldtguest", NULL, NULL);
2698}
2699
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette