VirtualBox

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

Last change on this file since 56085 was 56052, checked in by vboxsync, 10 years ago

VMM: Added non-PF raw-mode context virtual page access handler callback (not yet called).

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