VirtualBox

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

Last change on this file since 13299 was 13232, checked in by vboxsync, 16 years ago

#1865: changed some validation into pure assertions or assert-return. added some todos, update a comment or five and adjusted lots of whitespace.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 89.9 KB
Line 
1/* $Id: SELM.cpp 13232 2008-10-13 20:03:48Z vboxsync $ */
2/** @file
3 * SELM - The Selector manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/** @page pg_selm SELM - The Selector Manager
23 *
24 * Manages the hypervisor GDT entires, monitors and shadows the guest GDT, LDT
25 * and TSS. Only active in raw-mode.
26 *
27 * @see grp_selm
28 *
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#define LOG_GROUP LOG_GROUP_SELM
35#include <VBox/selm.h>
36#include <VBox/cpum.h>
37#include <VBox/stam.h>
38#include <VBox/mm.h>
39#include <VBox/ssm.h>
40#include <VBox/pgm.h>
41#include <VBox/trpm.h>
42#include <VBox/dbgf.h>
43#include "SELMInternal.h"
44#include <VBox/vm.h>
45#include <VBox/err.h>
46#include <VBox/param.h>
47
48#include <iprt/assert.h>
49#include <VBox/log.h>
50#include <iprt/asm.h>
51#include <iprt/string.h>
52#include <iprt/thread.h>
53#include <iprt/string.h>
54
55
56/**
57 * Enable or disable tracking of Guest's GDT/LDT/TSS.
58 * @{
59 */
60#define SELM_TRACK_GUEST_GDT_CHANGES
61#define SELM_TRACK_GUEST_LDT_CHANGES
62#define SELM_TRACK_GUEST_TSS_CHANGES
63/** @} */
64
65/**
66 * Enable or disable tracking of Shadow GDT/LDT/TSS.
67 * @{
68 */
69#define SELM_TRACK_SHADOW_GDT_CHANGES
70#define SELM_TRACK_SHADOW_LDT_CHANGES
71#define SELM_TRACK_SHADOW_TSS_CHANGES
72/** @} */
73
74
75/** SELM saved state version. */
76#define SELM_SAVED_STATE_VERSION 5
77
78/*******************************************************************************
79* Internal Functions *
80*******************************************************************************/
81static DECLCALLBACK(int) selmR3Save(PVM pVM, PSSMHANDLE pSSM);
82static DECLCALLBACK(int) selmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
83static DECLCALLBACK(int) selmR3LoadDone(PVM pVM, PSSMHANDLE pSSM);
84static DECLCALLBACK(void) selmR3InfoGdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
85static DECLCALLBACK(void) selmR3InfoGdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
86static DECLCALLBACK(void) selmR3InfoLdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
87static DECLCALLBACK(void) selmR3InfoLdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
88//static DECLCALLBACK(void) selmR3InfoTss(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
89//static DECLCALLBACK(void) selmR3InfoTssGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
90static DECLCALLBACK(int) selmGuestGDTWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
91static DECLCALLBACK(int) selmGuestLDTWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
92static DECLCALLBACK(int) selmGuestTSSWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
93
94
95
96/**
97 * Initializes the SELM.
98 *
99 * @returns VBox status code.
100 * @param pVM The VM to operate on.
101 */
102VMMR3DECL(int) SELMR3Init(PVM pVM)
103{
104 LogFlow(("SELMR3Init\n"));
105
106 /*
107 * Assert alignment and sizes.
108 * (The TSS block requires contiguous back.)
109 */
110 AssertCompile(sizeof(pVM->selm.s) <= sizeof(pVM->selm.padding)); AssertRelease(sizeof(pVM->selm.s) <= sizeof(pVM->selm.padding));
111 AssertCompileMemberAlignment(VM, selm.s, 32); AssertRelease(!(RT_OFFSETOF(VM, selm.s) & 31));
112#if 0 /* doesn't work */
113 AssertCompile((RT_OFFSETOF(VM, selm.s.Tss) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.Tss));
114 AssertCompile((RT_OFFSETOF(VM, selm.s.TssTrap08) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.TssTrap08));
115#endif
116 AssertRelease((RT_OFFSETOF(VM, selm.s.Tss) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.Tss));
117 AssertRelease((RT_OFFSETOF(VM, selm.s.TssTrap08) & PAGE_OFFSET_MASK) <= PAGE_SIZE - sizeof(pVM->selm.s.TssTrap08));
118 AssertRelease(sizeof(pVM->selm.s.Tss.IntRedirBitmap) == 0x20);
119
120 /*
121 * Init the structure.
122 */
123 pVM->selm.s.offVM = RT_OFFSETOF(VM, selm);
124 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] = (SELM_GDT_ELEMENTS - 0x1) << 3;
125 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] = (SELM_GDT_ELEMENTS - 0x2) << 3;
126 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] = (SELM_GDT_ELEMENTS - 0x3) << 3;
127 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] = (SELM_GDT_ELEMENTS - 0x4) << 3;
128 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = (SELM_GDT_ELEMENTS - 0x5) << 3;
129
130 /*
131 * Allocate GDT table.
132 */
133 int rc = MMR3HyperAllocOnceNoRel(pVM, sizeof(pVM->selm.s.paGdtHC[0]) * SELM_GDT_ELEMENTS,
134 PAGE_SIZE, MM_TAG_SELM, (void **)&pVM->selm.s.paGdtHC);
135 AssertRCReturn(rc, rc);
136
137 /*
138 * Allocate LDT area.
139 */
140 rc = MMR3HyperAllocOnceNoRel(pVM, _64K + PAGE_SIZE, PAGE_SIZE, MM_TAG_SELM, &pVM->selm.s.HCPtrLdt);
141 AssertRCReturn(rc, rc);
142
143 /*
144 * Init Guest's and Shadow GDT, LDT, TSS changes control variables.
145 */
146 pVM->selm.s.cbEffGuestGdtLimit = 0;
147 pVM->selm.s.GuestGdtr.pGdt = RTRCPTR_MAX;
148 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
149 pVM->selm.s.GCPtrGuestTss = RTRCPTR_MAX;
150
151 pVM->selm.s.paGdtGC = 0;
152 pVM->selm.s.GCPtrLdt = RTRCPTR_MAX;
153 pVM->selm.s.GCPtrTss = RTRCPTR_MAX;
154 pVM->selm.s.GCSelTss = ~0;
155
156 pVM->selm.s.fDisableMonitoring = false;
157 pVM->selm.s.fSyncTSSRing0Stack = false;
158
159 /* The I/O bitmap starts right after the virtual interrupt redirection bitmap. Outside the TSS on purpose; the CPU will not check it
160 * for I/O operations. */
161 pVM->selm.s.Tss.offIoBitmap = sizeof(VBOXTSS);
162 /* bit set to 1 means no redirection */
163 memset(pVM->selm.s.Tss.IntRedirBitmap, 0xff, sizeof(pVM->selm.s.Tss.IntRedirBitmap));
164
165 /*
166 * Register the saved state data unit.
167 */
168 rc = SSMR3RegisterInternal(pVM, "selm", 1, SELM_SAVED_STATE_VERSION, sizeof(SELM),
169 NULL, selmR3Save, NULL,
170 NULL, selmR3Load, selmR3LoadDone);
171 if (VBOX_FAILURE(rc))
172 return rc;
173
174 /*
175 * Statistics.
176 */
177 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestGDTHandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/GDTInt", STAMUNIT_OCCURENCES, "The number of handled writes to the Guest GDT.");
178 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestGDTUnhandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/GDTEmu", STAMUNIT_OCCURENCES, "The number of unhandled writes to the Guest GDT.");
179 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestLDT, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/LDT", STAMUNIT_OCCURENCES, "The number of writes to the Guest LDT was detected.");
180 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestTSSHandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSInt", STAMUNIT_OCCURENCES, "The number of handled writes to the Guest TSS.");
181 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestTSSRedir, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSRedir",STAMUNIT_OCCURENCES, "The number of handled redir bitmap writes to the Guest TSS.");
182 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestTSSHandledChanged,STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSIntChg", STAMUNIT_OCCURENCES, "The number of handled writes to the Guest TSS where the R0 stack changed.");
183 STAM_REG(pVM, &pVM->selm.s.StatGCWriteGuestTSSUnhandled, STAMTYPE_COUNTER, "/SELM/GC/Write/Guest/TSSEmu", STAMUNIT_OCCURENCES, "The number of unhandled writes to the Guest TSS.");
184 STAM_REG(pVM, &pVM->selm.s.StatTSSSync, STAMTYPE_PROFILE, "/PROF/SELM/TSSSync", STAMUNIT_TICKS_PER_CALL, "Profiling of the SELMR3SyncTSS() body.");
185 STAM_REG(pVM, &pVM->selm.s.StatUpdateFromCPUM, STAMTYPE_PROFILE, "/PROF/SELM/UpdateFromCPUM", STAMUNIT_TICKS_PER_CALL, "Profiling of the SELMR3UpdateFromCPUM() body.");
186
187 STAM_REG(pVM, &pVM->selm.s.StatHyperSelsChanged, STAMTYPE_COUNTER, "/SELM/HyperSels/Changed", STAMUNIT_OCCURENCES, "The number of times we had to relocate our hypervisor selectors.");
188 STAM_REG(pVM, &pVM->selm.s.StatScanForHyperSels, STAMTYPE_COUNTER, "/SELM/HyperSels/Scan", STAMUNIT_OCCURENCES, "The number of times we had find free hypervisor selectors.");
189
190 /*
191 * Default action when entering raw mode for the first time
192 */
193 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
194 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
195 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
196
197 /*
198 * Register info handlers.
199 */
200 DBGFR3InfoRegisterInternal(pVM, "gdt", "Displays the shadow GDT. No arguments.", &selmR3InfoGdt);
201 DBGFR3InfoRegisterInternal(pVM, "gdtguest", "Displays the guest GDT. No arguments.", &selmR3InfoGdtGuest);
202 DBGFR3InfoRegisterInternal(pVM, "ldt", "Displays the shadow LDT. No arguments.", &selmR3InfoLdt);
203 DBGFR3InfoRegisterInternal(pVM, "ldtguest", "Displays the guest LDT. No arguments.", &selmR3InfoLdtGuest);
204 //DBGFR3InfoRegisterInternal(pVM, "tss", "Displays the shadow TSS. No arguments.", &selmR3InfoTss);
205 //DBGFR3InfoRegisterInternal(pVM, "tssguest", "Displays the guest TSS. No arguments.", &selmR3InfoTssGuest);
206
207 return rc;
208}
209
210
211/**
212 * Finalizes HMA page attributes.
213 *
214 * @returns VBox status code.
215 * @param pVM The VM handle.
216 */
217VMMR3DECL(int) SELMR3InitFinalize(PVM pVM)
218{
219 /*
220 * Make Double Fault work with WP enabled?
221 *
222 * The double fault is a task switch and thus requires write access to the GDT of the TSS
223 * (to set it busy), to the old TSS (to store state), and to the Trap 8 TSS for the back link.
224 *
225 * Since we in enabling write access to these pages make ourself vulnerable to attacks,
226 * it is not possible to do this by default.
227 */
228 bool f;
229 int rc = CFGMR3QueryBool(CFGMR3GetRoot(pVM), "DoubleFault", &f);
230#if !defined(DEBUG_bird)
231 if (VBOX_SUCCESS(rc) && f)
232#endif
233 {
234 PX86DESC paGdt = pVM->selm.s.paGdtHC;
235 rc = PGMMapSetPage(pVM, MMHyperHC2GC(pVM, &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> 3]), sizeof(paGdt[0]),
236 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
237 AssertRC(rc);
238 rc = PGMMapSetPage(pVM, MMHyperHC2GC(pVM, &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> 3]), sizeof(paGdt[0]),
239 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
240 AssertRC(rc);
241 rc = PGMMapSetPage(pVM, VM_GUEST_ADDR(pVM, &pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]), sizeof(pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]),
242 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
243 AssertRC(rc);
244 rc = PGMMapSetPage(pVM, VM_GUEST_ADDR(pVM, &pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08]), sizeof(pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08]),
245 X86_PTE_RW | X86_PTE_P | X86_PTE_A | X86_PTE_D);
246 AssertRC(rc);
247 }
248 return VINF_SUCCESS;
249}
250
251
252/**
253 * Setup the hypervisor GDT selectors in our shadow table
254 *
255 * @param pVM The VM handle.
256 */
257static void selmR3SetupHyperGDTSelectors(PVM pVM)
258{
259 PX86DESC paGdt = pVM->selm.s.paGdtHC;
260
261 /*
262 * Set up global code and data descriptors for use in the guest context.
263 * Both are wide open (base 0, limit 4GB)
264 */
265 PX86DESC pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] >> 3];
266 pDesc->Gen.u16LimitLow = 0xffff;
267 pDesc->Gen.u4LimitHigh = 0xf;
268 pDesc->Gen.u16BaseLow = 0;
269 pDesc->Gen.u8BaseHigh1 = 0;
270 pDesc->Gen.u8BaseHigh2 = 0;
271 pDesc->Gen.u4Type = X86_SEL_TYPE_ER_ACC;
272 pDesc->Gen.u1DescType = 1; /* not system, but code/data */
273 pDesc->Gen.u2Dpl = 0; /* supervisor */
274 pDesc->Gen.u1Present = 1;
275 pDesc->Gen.u1Available = 0;
276 pDesc->Gen.u1Long = 0;
277 pDesc->Gen.u1DefBig = 1; /* def 32 bit */
278 pDesc->Gen.u1Granularity = 1; /* 4KB limit */
279
280 /* data */
281 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] >> 3];
282 pDesc->Gen.u16LimitLow = 0xffff;
283 pDesc->Gen.u4LimitHigh = 0xf;
284 pDesc->Gen.u16BaseLow = 0;
285 pDesc->Gen.u8BaseHigh1 = 0;
286 pDesc->Gen.u8BaseHigh2 = 0;
287 pDesc->Gen.u4Type = X86_SEL_TYPE_RW_ACC;
288 pDesc->Gen.u1DescType = 1; /* not system, but code/data */
289 pDesc->Gen.u2Dpl = 0; /* supervisor */
290 pDesc->Gen.u1Present = 1;
291 pDesc->Gen.u1Available = 0;
292 pDesc->Gen.u1Long = 0;
293 pDesc->Gen.u1DefBig = 1; /* big */
294 pDesc->Gen.u1Granularity = 1; /* 4KB limit */
295
296 /* 64-bit mode code (& data?) */
297 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] >> 3];
298 pDesc->Gen.u16LimitLow = 0xffff;
299 pDesc->Gen.u4LimitHigh = 0xf;
300 pDesc->Gen.u16BaseLow = 0;
301 pDesc->Gen.u8BaseHigh1 = 0;
302 pDesc->Gen.u8BaseHigh2 = 0;
303 pDesc->Gen.u4Type = X86_SEL_TYPE_ER_ACC;
304 pDesc->Gen.u1DescType = 1; /* not system, but code/data */
305 pDesc->Gen.u2Dpl = 0; /* supervisor */
306 pDesc->Gen.u1Present = 1;
307 pDesc->Gen.u1Available = 0;
308 pDesc->Gen.u1Long = 1; /* The Long (L) attribute bit. */
309 pDesc->Gen.u1DefBig = 0; /* With L=1 this must be 0. */
310 pDesc->Gen.u1Granularity = 1; /* 4KB limit */
311
312 /*
313 * TSS descriptor
314 */
315 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> 3];
316 RTGCPTR pGCTSS = VM_GUEST_ADDR(pVM, &pVM->selm.s.Tss);
317 pDesc->Gen.u16BaseLow = RT_LOWORD(pGCTSS);
318 pDesc->Gen.u8BaseHigh1 = RT_BYTE3(pGCTSS);
319 pDesc->Gen.u8BaseHigh2 = RT_BYTE4(pGCTSS);
320 pDesc->Gen.u16LimitLow = sizeof(VBOXTSS) - 1;
321 pDesc->Gen.u4LimitHigh = 0;
322 pDesc->Gen.u4Type = X86_SEL_TYPE_SYS_386_TSS_AVAIL;
323 pDesc->Gen.u1DescType = 0; /* system */
324 pDesc->Gen.u2Dpl = 0; /* supervisor */
325 pDesc->Gen.u1Present = 1;
326 pDesc->Gen.u1Available = 0;
327 pDesc->Gen.u1Long = 0;
328 pDesc->Gen.u1DefBig = 0;
329 pDesc->Gen.u1Granularity = 0; /* byte limit */
330
331 /*
332 * TSS descriptor for trap 08
333 */
334 pDesc = &paGdt[pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> 3];
335 pDesc->Gen.u16LimitLow = sizeof(VBOXTSS) - 1;
336 pDesc->Gen.u4LimitHigh = 0;
337 pGCTSS = VM_GUEST_ADDR(pVM, &pVM->selm.s.TssTrap08);
338 pDesc->Gen.u16BaseLow = RT_LOWORD(pGCTSS);
339 pDesc->Gen.u8BaseHigh1 = RT_BYTE3(pGCTSS);
340 pDesc->Gen.u8BaseHigh2 = RT_BYTE4(pGCTSS);
341 pDesc->Gen.u4Type = X86_SEL_TYPE_SYS_386_TSS_AVAIL;
342 pDesc->Gen.u1DescType = 0; /* system */
343 pDesc->Gen.u2Dpl = 0; /* supervisor */
344 pDesc->Gen.u1Present = 1;
345 pDesc->Gen.u1Available = 0;
346 pDesc->Gen.u1Long = 0;
347 pDesc->Gen.u1DefBig = 0;
348 pDesc->Gen.u1Granularity = 0; /* byte limit */
349}
350
351/**
352 * Applies relocations to data and code managed by this
353 * component. This function will be called at init and
354 * whenever the VMM need to relocate it self inside the GC.
355 *
356 * @param pVM The VM.
357 */
358VMMR3DECL(void) SELMR3Relocate(PVM pVM)
359{
360 PX86DESC paGdt = pVM->selm.s.paGdtHC;
361 LogFlow(("SELMR3Relocate\n"));
362
363 /*
364 * Update GDTR and selector.
365 */
366 CPUMSetHyperGDTR(pVM, MMHyperHC2GC(pVM, paGdt), SELM_GDT_ELEMENTS * sizeof(paGdt[0]) - 1);
367
368 /** @todo selector relocations should be a seperate operation? */
369 CPUMSetHyperCS(pVM, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS]);
370 CPUMSetHyperDS(pVM, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]);
371 CPUMSetHyperES(pVM, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]);
372 CPUMSetHyperSS(pVM, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]);
373 CPUMSetHyperTR(pVM, pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]);
374
375 selmR3SetupHyperGDTSelectors(pVM);
376
377/** @todo SELM must be called when any of the CR3s changes during a cpu mode change. */
378/** @todo PGM knows the proper CR3 values these days, not CPUM. */
379 /*
380 * Update the TSSes.
381 */
382 /* Current TSS */
383 pVM->selm.s.Tss.cr3 = PGMGetHyperCR3(pVM);
384 pVM->selm.s.Tss.ss0 = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
385 pVM->selm.s.Tss.esp0 = VMMGetStackGC(pVM);
386 pVM->selm.s.Tss.cs = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS];
387 pVM->selm.s.Tss.ds = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
388 pVM->selm.s.Tss.es = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
389 pVM->selm.s.Tss.offIoBitmap = sizeof(VBOXTSS);
390
391 /* trap 08 */
392 pVM->selm.s.TssTrap08.cr3 = PGMGetInterRCCR3(pVM); /* this should give use better survival chances. */
393 pVM->selm.s.TssTrap08.ss0 = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
394 pVM->selm.s.TssTrap08.ss = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
395 pVM->selm.s.TssTrap08.esp0 = VMMGetStackGC(pVM) - PAGE_SIZE / 2; /* upper half can be analysed this way. */
396 pVM->selm.s.TssTrap08.esp = pVM->selm.s.TssTrap08.esp0;
397 pVM->selm.s.TssTrap08.ebp = pVM->selm.s.TssTrap08.esp0;
398 pVM->selm.s.TssTrap08.cs = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS];
399 pVM->selm.s.TssTrap08.ds = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
400 pVM->selm.s.TssTrap08.es = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS];
401 pVM->selm.s.TssTrap08.fs = 0;
402 pVM->selm.s.TssTrap08.gs = 0;
403 pVM->selm.s.TssTrap08.selLdt = 0;
404 pVM->selm.s.TssTrap08.eflags = 0x2; /* all cleared */
405 pVM->selm.s.TssTrap08.ecx = VM_GUEST_ADDR(pVM, &pVM->selm.s.Tss); /* setup ecx to normal Hypervisor TSS address. */
406 pVM->selm.s.TssTrap08.edi = pVM->selm.s.TssTrap08.ecx;
407 pVM->selm.s.TssTrap08.eax = pVM->selm.s.TssTrap08.ecx;
408 pVM->selm.s.TssTrap08.edx = VM_GUEST_ADDR(pVM, pVM); /* setup edx VM address. */
409 pVM->selm.s.TssTrap08.edi = pVM->selm.s.TssTrap08.edx;
410 pVM->selm.s.TssTrap08.ebx = pVM->selm.s.TssTrap08.edx;
411 pVM->selm.s.TssTrap08.offIoBitmap = sizeof(VBOXTSS);
412 /* TRPM will be updating the eip */
413
414 if (!pVM->selm.s.fDisableMonitoring)
415 {
416 /*
417 * Update shadow GDT/LDT/TSS write access handlers.
418 */
419 int rc;
420#ifdef SELM_TRACK_SHADOW_GDT_CHANGES
421 if (pVM->selm.s.paGdtGC != 0)
422 {
423 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.paGdtGC);
424 AssertRC(rc);
425 }
426 pVM->selm.s.paGdtGC = MMHyperHC2GC(pVM, paGdt);
427 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_HYPERVISOR, pVM->selm.s.paGdtGC,
428 pVM->selm.s.paGdtGC + SELM_GDT_ELEMENTS * sizeof(paGdt[0]) - 1,
429 0, 0, "selmgcShadowGDTWriteHandler", 0, "Shadow GDT write access handler");
430 AssertRC(rc);
431#endif
432#ifdef SELM_TRACK_SHADOW_TSS_CHANGES
433 if (pVM->selm.s.GCPtrTss != RTRCPTR_MAX)
434 {
435 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrTss);
436 AssertRC(rc);
437 }
438 pVM->selm.s.GCPtrTss = VM_GUEST_ADDR(pVM, &pVM->selm.s.Tss);
439 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_HYPERVISOR, pVM->selm.s.GCPtrTss,
440 pVM->selm.s.GCPtrTss + sizeof(pVM->selm.s.Tss) - 1,
441 0, 0, "selmgcShadowTSSWriteHandler", 0, "Shadow TSS write access handler");
442 AssertRC(rc);
443#endif
444
445 /*
446 * Update the GC LDT region handler and address.
447 */
448#ifdef SELM_TRACK_SHADOW_LDT_CHANGES
449 if (pVM->selm.s.GCPtrLdt != RTRCPTR_MAX)
450 {
451 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrLdt);
452 AssertRC(rc);
453 }
454#endif
455 pVM->selm.s.GCPtrLdt = MMHyperHC2GC(pVM, pVM->selm.s.HCPtrLdt);
456#ifdef SELM_TRACK_SHADOW_LDT_CHANGES
457 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_HYPERVISOR, pVM->selm.s.GCPtrLdt,
458 pVM->selm.s.GCPtrLdt + _64K + PAGE_SIZE - 1,
459 0, 0, "selmgcShadowLDTWriteHandler", 0, "Shadow LDT write access handler");
460 AssertRC(rc);
461#endif
462 }
463}
464
465
466/**
467 * Notification callback which is called whenever there is a chance that a CR3
468 * value might have changed.
469 * This is called by PGM.
470 *
471 * @param pVM The VM handle
472 */
473VMMR3DECL(void) SELMR3PagingModeChanged(PVM pVM)
474{
475 pVM->selm.s.Tss.cr3 = PGMGetHyperCR3(pVM);
476 pVM->selm.s.TssTrap08.cr3 = PGMGetInterRCCR3(pVM);
477}
478
479
480/**
481 * Terminates the SELM.
482 *
483 * Termination means cleaning up and freeing all resources,
484 * the VM it self is at this point powered off or suspended.
485 *
486 * @returns VBox status code.
487 * @param pVM The VM to operate on.
488 */
489VMMR3DECL(int) SELMR3Term(PVM pVM)
490{
491 return 0;
492}
493
494
495/**
496 * The VM is being reset.
497 *
498 * For the SELM component this means that any GDT/LDT/TSS monitors
499 * needs to be removed.
500 *
501 * @param pVM VM handle.
502 */
503VMMR3DECL(void) SELMR3Reset(PVM pVM)
504{
505 LogFlow(("SELMR3Reset:\n"));
506 VM_ASSERT_EMT(pVM);
507
508 /*
509 * Uninstall guest GDT/LDT/TSS write access handlers.
510 */
511 int rc;
512#ifdef SELM_TRACK_GUEST_GDT_CHANGES
513 if (pVM->selm.s.GuestGdtr.pGdt != RTRCPTR_MAX && pVM->selm.s.fGDTRangeRegistered)
514 {
515 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GuestGdtr.pGdt);
516 AssertRC(rc);
517 pVM->selm.s.GuestGdtr.pGdt = RTRCPTR_MAX;
518 pVM->selm.s.GuestGdtr.cbGdt = 0;
519 }
520 pVM->selm.s.fGDTRangeRegistered = false;
521#endif
522#ifdef SELM_TRACK_GUEST_LDT_CHANGES
523 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
524 {
525 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestLdt);
526 AssertRC(rc);
527 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
528 }
529#endif
530#ifdef SELM_TRACK_GUEST_TSS_CHANGES
531 if (pVM->selm.s.GCPtrGuestTss != RTRCPTR_MAX)
532 {
533 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestTss);
534 AssertRC(rc);
535 pVM->selm.s.GCPtrGuestTss = RTRCPTR_MAX;
536 pVM->selm.s.GCSelTss = ~0;
537 }
538#endif
539
540 /*
541 * Re-initialize other members.
542 */
543 pVM->selm.s.cbLdtLimit = 0;
544 pVM->selm.s.offLdtHyper = 0;
545 pVM->selm.s.cbMonitoredGuestTss = 0;
546
547 pVM->selm.s.fSyncTSSRing0Stack = false;
548
549 /*
550 * Default action when entering raw mode for the first time
551 */
552 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
553 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
554 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
555}
556
557/**
558 * Disable GDT/LDT/TSS monitoring and syncing
559 *
560 * @param pVM The VM to operate on.
561 */
562VMMR3DECL(void) SELMR3DisableMonitoring(PVM pVM)
563{
564 /*
565 * Uninstall guest GDT/LDT/TSS write access handlers.
566 */
567 int rc;
568#ifdef SELM_TRACK_GUEST_GDT_CHANGES
569 if (pVM->selm.s.GuestGdtr.pGdt != RTRCPTR_MAX && pVM->selm.s.fGDTRangeRegistered)
570 {
571 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GuestGdtr.pGdt);
572 AssertRC(rc);
573 pVM->selm.s.GuestGdtr.pGdt = RTRCPTR_MAX;
574 pVM->selm.s.GuestGdtr.cbGdt = 0;
575 }
576 pVM->selm.s.fGDTRangeRegistered = false;
577#endif
578#ifdef SELM_TRACK_GUEST_LDT_CHANGES
579 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
580 {
581 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestLdt);
582 AssertRC(rc);
583 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
584 }
585#endif
586#ifdef SELM_TRACK_GUEST_TSS_CHANGES
587 if (pVM->selm.s.GCPtrGuestTss != RTRCPTR_MAX)
588 {
589 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestTss);
590 AssertRC(rc);
591 pVM->selm.s.GCPtrGuestTss = RTRCPTR_MAX;
592 pVM->selm.s.GCSelTss = ~0;
593 }
594#endif
595
596 /*
597 * Unregister shadow GDT/LDT/TSS write access handlers.
598 */
599#ifdef SELM_TRACK_SHADOW_GDT_CHANGES
600 if (pVM->selm.s.paGdtGC != 0)
601 {
602 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.paGdtGC);
603 AssertRC(rc);
604 pVM->selm.s.paGdtGC = 0;
605 }
606#endif
607#ifdef SELM_TRACK_SHADOW_TSS_CHANGES
608 if (pVM->selm.s.GCPtrTss != RTRCPTR_MAX)
609 {
610 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrTss);
611 AssertRC(rc);
612 pVM->selm.s.GCPtrTss = RTRCPTR_MAX;
613 }
614#endif
615#ifdef SELM_TRACK_SHADOW_LDT_CHANGES
616 if (pVM->selm.s.GCPtrLdt != RTRCPTR_MAX)
617 {
618 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrLdt);
619 AssertRC(rc);
620 pVM->selm.s.GCPtrLdt = RTRCPTR_MAX;
621 }
622#endif
623
624 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_TSS);
625 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_GDT);
626 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_LDT);
627
628 pVM->selm.s.fDisableMonitoring = true;
629}
630
631/**
632 * Execute state save operation.
633 *
634 * @returns VBox status code.
635 * @param pVM VM Handle.
636 * @param pSSM SSM operation handle.
637 */
638static DECLCALLBACK(int) selmR3Save(PVM pVM, PSSMHANDLE pSSM)
639{
640 LogFlow(("selmR3Save:\n"));
641
642 /*
643 * Save the basic bits - fortunately all the other things can be resynced on load.
644 */
645 PSELM pSelm = &pVM->selm.s;
646
647 SSMR3PutBool(pSSM, pSelm->fDisableMonitoring);
648 SSMR3PutBool(pSSM, pSelm->fSyncTSSRing0Stack);
649 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_CS]);
650 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_DS]);
651 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_CS64]);
652 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_CS64]); //reserved for DS64.
653 SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_TSS]);
654 return SSMR3PutSel(pSSM, pSelm->aHyperSel[SELM_HYPER_SEL_TSS_TRAP08]);
655}
656
657
658/**
659 * Execute state load operation.
660 *
661 * @returns VBox status code.
662 * @param pVM VM Handle.
663 * @param pSSM SSM operation handle.
664 * @param u32Version Data layout version.
665 */
666static DECLCALLBACK(int) selmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
667{
668 LogFlow(("selmR3Load:\n"));
669
670 /*
671 * Validate version.
672 */
673 if (u32Version != SELM_SAVED_STATE_VERSION)
674 {
675 AssertMsgFailed(("selmR3Load: Invalid version u32Version=%d!\n", u32Version));
676 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
677 }
678
679 /*
680 * Do a reset.
681 */
682 SELMR3Reset(pVM);
683
684 /* Get the monitoring flag. */
685 SSMR3GetBool(pSSM, &pVM->selm.s.fDisableMonitoring);
686
687 /* Get the TSS state flag. */
688 SSMR3GetBool(pSSM, &pVM->selm.s.fSyncTSSRing0Stack);
689
690 /*
691 * Get the selectors.
692 */
693 RTSEL SelCS;
694 SSMR3GetSel(pSSM, &SelCS);
695 RTSEL SelDS;
696 SSMR3GetSel(pSSM, &SelDS);
697 RTSEL SelCS64;
698 SSMR3GetSel(pSSM, &SelCS64);
699 RTSEL SelDS64;
700 SSMR3GetSel(pSSM, &SelDS64);
701 RTSEL SelTSS;
702 SSMR3GetSel(pSSM, &SelTSS);
703 RTSEL SelTSSTrap08;
704 SSMR3GetSel(pSSM, &SelTSSTrap08);
705
706 /* Copy the selectors; they will be checked during relocation. */
707 PSELM pSelm = &pVM->selm.s;
708 pSelm->aHyperSel[SELM_HYPER_SEL_CS] = SelCS;
709 pSelm->aHyperSel[SELM_HYPER_SEL_DS] = SelDS;
710 pSelm->aHyperSel[SELM_HYPER_SEL_CS64] = SelCS64;
711 pSelm->aHyperSel[SELM_HYPER_SEL_TSS] = SelTSS;
712 pSelm->aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = SelTSSTrap08;
713
714 return VINF_SUCCESS;
715}
716
717
718/**
719 * Sync the GDT, LDT and TSS after loading the state.
720 *
721 * Just to play save, we set the FFs to force syncing before
722 * executing GC code.
723 *
724 * @returns VBox status code.
725 * @param pVM VM Handle.
726 * @param pSSM SSM operation handle.
727 */
728static DECLCALLBACK(int) selmR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
729{
730 LogFlow(("selmR3LoadDone:\n"));
731
732 /*
733 * Don't do anything if it's a load failure.
734 */
735 int rc = SSMR3HandleGetStatus(pSSM);
736 if (VBOX_FAILURE(rc))
737 return VINF_SUCCESS;
738
739 /*
740 * Do the syncing if we're in protected mode.
741 */
742 if (PGMGetGuestMode(pVM) != PGMMODE_REAL)
743 {
744 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
745 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
746 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
747 SELMR3UpdateFromCPUM(pVM);
748 }
749
750 /*
751 * Flag everything for resync on next raw mode entry.
752 */
753 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
754 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
755 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
756
757 return VINF_SUCCESS;
758}
759
760
761/**
762 * Updates the Guest GDT & LDT virtualization based on current CPU state.
763 *
764 * @returns VBox status code.
765 * @param pVM The VM to operate on.
766 */
767VMMR3DECL(int) SELMR3UpdateFromCPUM(PVM pVM)
768{
769 int rc = VINF_SUCCESS;
770
771 if (pVM->selm.s.fDisableMonitoring)
772 {
773 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_GDT);
774 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_LDT);
775 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_TSS);
776
777 return VINF_SUCCESS;
778 }
779
780 STAM_PROFILE_START(&pVM->selm.s.StatUpdateFromCPUM, a);
781
782 /*
783 * GDT sync
784 */
785 if (VM_FF_ISSET(pVM, VM_FF_SELM_SYNC_GDT))
786 {
787 /*
788 * Always assume the best
789 */
790 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_GDT);
791
792 /* If the GDT was changed, then make sure the LDT is checked too */
793 /** @todo only do this if the actual ldtr selector was changed; this is a bit excessive */
794 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
795 /* Same goes for the TSS selector */
796 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
797
798 /*
799 * Get the GDTR and check if there is anything to do (there usually is).
800 */
801 VBOXGDTR GDTR;
802 CPUMGetGuestGDTR(pVM, &GDTR);
803 if (GDTR.cbGdt < sizeof(X86DESC))
804 {
805 Log(("No GDT entries...\n"));
806 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
807 return VINF_SUCCESS;
808 }
809
810 /*
811 * Read the Guest GDT.
812 * ASSUMES that the entire GDT is in memory.
813 */
814 RTUINT cbEffLimit = GDTR.cbGdt;
815 PX86DESC pGDTE = &pVM->selm.s.paGdtHC[1];
816 rc = PGMPhysSimpleReadGCPtr(pVM, pGDTE, GDTR.pGdt + sizeof(X86DESC), cbEffLimit + 1 - sizeof(X86DESC));
817 if (VBOX_FAILURE(rc))
818 {
819 /*
820 * Read it page by page.
821 *
822 * Keep track of the last valid page and delay memsets and
823 * adjust cbEffLimit to reflect the effective size. The latter
824 * is something we do in the belief that the guest will probably
825 * never actually commit the last page, thus allowing us to keep
826 * our selectors in the high end of the GDT.
827 */
828 RTUINT cbLeft = cbEffLimit + 1 - sizeof(X86DESC);
829 RTGCPTR GCPtrSrc = (RTGCPTR)GDTR.pGdt + sizeof(X86DESC);
830 uint8_t *pu8Dst = (uint8_t *)&pVM->selm.s.paGdtHC[1];
831 uint8_t *pu8DstInvalid = pu8Dst;
832
833 while (cbLeft)
834 {
835 RTUINT cb = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
836 cb = RT_MIN(cb, cbLeft);
837 rc = PGMPhysSimpleReadGCPtr(pVM, pu8Dst, GCPtrSrc, cb);
838 if (VBOX_SUCCESS(rc))
839 {
840 if (pu8DstInvalid != pu8Dst)
841 memset(pu8DstInvalid, 0, pu8Dst - pu8DstInvalid);
842 GCPtrSrc += cb;
843 pu8Dst += cb;
844 pu8DstInvalid = pu8Dst;
845 }
846 else if ( rc == VERR_PAGE_NOT_PRESENT
847 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
848 {
849 GCPtrSrc += cb;
850 pu8Dst += cb;
851 }
852 else
853 {
854 AssertReleaseMsgFailed(("Couldn't read GDT at %VGv, rc=%Vrc!\n", GDTR.pGdt, rc));
855 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
856 return VERR_NOT_IMPLEMENTED;
857 }
858 cbLeft -= cb;
859 }
860
861 /* any invalid pages at the end? */
862 if (pu8DstInvalid != pu8Dst)
863 {
864 cbEffLimit = pu8DstInvalid - (uint8_t *)pVM->selm.s.paGdtHC - 1;
865 /* If any GDTEs was invalidated, zero them. */
866 if (cbEffLimit < pVM->selm.s.cbEffGuestGdtLimit)
867 memset(pu8DstInvalid + cbEffLimit + 1, 0, pVM->selm.s.cbEffGuestGdtLimit - cbEffLimit);
868 }
869
870 /* keep track of the effective limit. */
871 if (cbEffLimit != pVM->selm.s.cbEffGuestGdtLimit)
872 {
873 Log(("SELMR3UpdateFromCPUM: cbEffGuestGdtLimit=%#x -> %#x (actual %#x)\n",
874 pVM->selm.s.cbEffGuestGdtLimit, cbEffLimit, GDTR.cbGdt));
875 pVM->selm.s.cbEffGuestGdtLimit = cbEffLimit;
876 }
877 }
878
879 /*
880 * Check if the Guest GDT intrudes on our GDT entries.
881 */
882 /** @todo we should try to minimize relocations by making sure our current selectors can be reused. */
883 RTSEL aHyperSel[SELM_HYPER_SEL_MAX];
884 if (cbEffLimit >= SELM_HYPER_DEFAULT_BASE)
885 {
886 PX86DESC pGDTEStart = pVM->selm.s.paGdtHC;
887 PX86DESC pGDTE = (PX86DESC)((char *)pGDTEStart + GDTR.cbGdt + 1 - sizeof(X86DESC));
888 int iGDT = 0;
889
890 Log(("Internal SELM GDT conflict: use non-present entries\n"));
891 STAM_COUNTER_INC(&pVM->selm.s.StatScanForHyperSels);
892 while (pGDTE > pGDTEStart)
893 {
894 /* We can reuse non-present entries */
895 if (!pGDTE->Gen.u1Present)
896 {
897 aHyperSel[iGDT] = ((uintptr_t)pGDTE - (uintptr_t)pVM->selm.s.paGdtHC) / sizeof(X86DESC);
898 aHyperSel[iGDT] = aHyperSel[iGDT] << X86_SEL_SHIFT;
899 Log(("SELM: Found unused GDT %04X\n", aHyperSel[iGDT]));
900 iGDT++;
901 if (iGDT >= SELM_HYPER_SEL_MAX)
902 break;
903 }
904
905 pGDTE--;
906 }
907 if (iGDT != SELM_HYPER_SEL_MAX)
908 {
909 AssertReleaseMsgFailed(("Internal SELM GDT conflict.\n"));
910 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
911 return VERR_NOT_IMPLEMENTED;
912 }
913 }
914 else
915 {
916 aHyperSel[SELM_HYPER_SEL_CS] = SELM_HYPER_DEFAULT_SEL_CS;
917 aHyperSel[SELM_HYPER_SEL_DS] = SELM_HYPER_DEFAULT_SEL_DS;
918 aHyperSel[SELM_HYPER_SEL_CS64] = SELM_HYPER_DEFAULT_SEL_CS64;
919 aHyperSel[SELM_HYPER_SEL_TSS] = SELM_HYPER_DEFAULT_SEL_TSS;
920 aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = SELM_HYPER_DEFAULT_SEL_TSS_TRAP08;
921 }
922
923 /*
924 * Work thru the copied GDT entries adjusting them for correct virtualization.
925 */
926 PX86DESC pGDTEEnd = (PX86DESC)((char *)pGDTE + cbEffLimit + 1 - sizeof(X86DESC));
927 while (pGDTE < pGDTEEnd)
928 {
929 if (pGDTE->Gen.u1Present)
930 {
931 /*
932 * Code and data selectors are generally 1:1, with the
933 * 'little' adjustment we do for DPL 0 selectors.
934 */
935 if (pGDTE->Gen.u1DescType)
936 {
937 /*
938 * Hack for A-bit against Trap E on read-only GDT.
939 */
940 /** @todo Fix this by loading ds and cs before turning off WP. */
941 pGDTE->Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
942
943 /*
944 * All DPL 0 code and data segments are squeezed into DPL 1.
945 *
946 * We're skipping conforming segments here because those
947 * cannot give us any trouble.
948 */
949 if ( pGDTE->Gen.u2Dpl == 0
950 && (pGDTE->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
951 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF) )
952 pGDTE->Gen.u2Dpl = 1;
953 }
954 else
955 {
956 /*
957 * System type selectors are marked not present.
958 * Recompiler or special handling is required for these.
959 */
960 /** @todo what about interrupt gates and rawr0? */
961 pGDTE->Gen.u1Present = 0;
962 }
963 }
964
965 /* Next GDT entry. */
966 pGDTE++;
967 }
968
969 /*
970 * Check if our hypervisor selectors were changed.
971 */
972 if ( aHyperSel[SELM_HYPER_SEL_CS] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS]
973 || aHyperSel[SELM_HYPER_SEL_DS] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS]
974 || aHyperSel[SELM_HYPER_SEL_CS64] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64]
975 || aHyperSel[SELM_HYPER_SEL_TSS] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS]
976 || aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] != pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08])
977 {
978 /* Reinitialize our hypervisor GDTs */
979 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] = aHyperSel[SELM_HYPER_SEL_CS];
980 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] = aHyperSel[SELM_HYPER_SEL_DS];
981 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] = aHyperSel[SELM_HYPER_SEL_CS64];
982 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] = aHyperSel[SELM_HYPER_SEL_TSS];
983 pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] = aHyperSel[SELM_HYPER_SEL_TSS_TRAP08];
984
985 STAM_COUNTER_INC(&pVM->selm.s.StatHyperSelsChanged);
986
987 /*
988 * Do the relocation callbacks to let everyone update their hyper selector dependencies.
989 * (SELMR3Relocate will call selmR3SetupHyperGDTSelectors() for us.)
990 */
991 VMR3Relocate(pVM, 0);
992 }
993 else if (cbEffLimit >= SELM_HYPER_DEFAULT_BASE)
994 /* We overwrote all entries above, so we have to save them again. */
995 selmR3SetupHyperGDTSelectors(pVM);
996
997 /*
998 * Adjust the cached GDT limit.
999 * Any GDT entries which have been removed must be cleared.
1000 */
1001 if (pVM->selm.s.GuestGdtr.cbGdt != GDTR.cbGdt)
1002 {
1003 if (pVM->selm.s.GuestGdtr.cbGdt > GDTR.cbGdt)
1004 memset(pGDTE, 0, pVM->selm.s.GuestGdtr.cbGdt - GDTR.cbGdt);
1005#ifndef SELM_TRACK_GUEST_GDT_CHANGES
1006 pVM->selm.s.GuestGdtr.cbGdt = GDTR.cbGdt;
1007#endif
1008 }
1009
1010#ifdef SELM_TRACK_GUEST_GDT_CHANGES
1011 /*
1012 * Check if Guest's GDTR is changed.
1013 */
1014 if ( GDTR.pGdt != pVM->selm.s.GuestGdtr.pGdt
1015 || GDTR.cbGdt != pVM->selm.s.GuestGdtr.cbGdt)
1016 {
1017 Log(("SELMR3UpdateFromCPUM: Guest's GDT is changed to pGdt=%VGv cbGdt=%08X\n", GDTR.pGdt, GDTR.cbGdt));
1018
1019 /*
1020 * [Re]Register write virtual handler for guest's GDT.
1021 */
1022 if (pVM->selm.s.GuestGdtr.pGdt != RTRCPTR_MAX && pVM->selm.s.fGDTRangeRegistered)
1023 {
1024 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GuestGdtr.pGdt);
1025 AssertRC(rc);
1026 }
1027
1028 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, GDTR.pGdt, GDTR.pGdt + GDTR.cbGdt /* already inclusive */,
1029 0, selmGuestGDTWriteHandler, "selmgcGuestGDTWriteHandler", 0, "Guest GDT write access handler");
1030 if (VBOX_FAILURE(rc))
1031 return rc;
1032
1033 /* Update saved Guest GDTR. */
1034 pVM->selm.s.GuestGdtr = GDTR;
1035 pVM->selm.s.fGDTRangeRegistered = true;
1036 }
1037#endif
1038 }
1039
1040 /*
1041 * TSS sync
1042 */
1043 if (VM_FF_ISSET(pVM, VM_FF_SELM_SYNC_TSS))
1044 {
1045 SELMR3SyncTSS(pVM);
1046 }
1047
1048 /*
1049 * LDT sync
1050 */
1051 if (VM_FF_ISSET(pVM, VM_FF_SELM_SYNC_LDT))
1052 {
1053 /*
1054 * Always assume the best
1055 */
1056 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_LDT);
1057
1058 /*
1059 * LDT handling is done similarly to the GDT handling with a shadow
1060 * array. However, since the LDT is expected to be swappable (at least
1061 * some ancient OSes makes it swappable) it must be floating and
1062 * synced on a per-page basis.
1063 *
1064 * Eventually we will change this to be fully on demand. Meaning that
1065 * we will only sync pages containing LDT selectors actually used and
1066 * let the #PF handler lazily sync pages as they are used.
1067 * (This applies to GDT too, when we start making OS/2 fast.)
1068 */
1069
1070 /*
1071 * First, determin the current LDT selector.
1072 */
1073 RTSEL SelLdt = CPUMGetGuestLDTR(pVM);
1074 if ((SelLdt & X86_SEL_MASK) == 0)
1075 {
1076 /* ldtr = 0 - update hyper LDTR and deregister any active handler. */
1077 CPUMSetHyperLDTR(pVM, 0);
1078#ifdef SELM_TRACK_GUEST_LDT_CHANGES
1079 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
1080 {
1081 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestLdt);
1082 AssertRC(rc);
1083 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
1084 }
1085#endif
1086 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1087 return VINF_SUCCESS;
1088 }
1089
1090 /*
1091 * Get the LDT selector.
1092 */
1093 PX86DESC pDesc = &pVM->selm.s.paGdtHC[SelLdt >> X86_SEL_SHIFT];
1094 RTGCPTR GCPtrLdt = X86DESC_BASE(*pDesc);
1095 unsigned cbLdt = X86DESC_LIMIT(*pDesc);
1096 if (pDesc->Gen.u1Granularity)
1097 cbLdt = (cbLdt << PAGE_SHIFT) | PAGE_OFFSET_MASK;
1098
1099 /*
1100 * Validate it.
1101 */
1102 if ( !cbLdt
1103 || SelLdt >= pVM->selm.s.GuestGdtr.cbGdt
1104 || pDesc->Gen.u1DescType
1105 || pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1106 {
1107 AssertMsg(!cbLdt, ("Invalid LDT %04x!\n", SelLdt));
1108
1109 /* cbLdt > 0:
1110 * This is quite impossible, so we do as most people do when faced with
1111 * the impossible, we simply ignore it.
1112 */
1113 CPUMSetHyperLDTR(pVM, 0);
1114#ifdef SELM_TRACK_GUEST_LDT_CHANGES
1115 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
1116 {
1117 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestLdt);
1118 AssertRC(rc);
1119 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
1120 }
1121#endif
1122 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1123 return VINF_SUCCESS;
1124 }
1125 /** @todo check what intel does about odd limits. */
1126 AssertMsg(RT_ALIGN(cbLdt + 1, sizeof(X86DESC)) == cbLdt + 1 && cbLdt <= 0xffff, ("cbLdt=%d\n", cbLdt));
1127
1128 /*
1129 * Use the cached guest ldt address if the descriptor has already been modified (see below)
1130 * (this is necessary due to redundant LDT updates; see todo above at GDT sync)
1131 */
1132 if (MMHyperIsInsideArea(pVM, GCPtrLdt) == true)
1133 GCPtrLdt = pVM->selm.s.GCPtrGuestLdt; /* use the old one */
1134
1135
1136#ifdef SELM_TRACK_GUEST_LDT_CHANGES
1137 /** @todo Handle only present LDT segments. */
1138 // if (pDesc->Gen.u1Present)
1139 {
1140 /*
1141 * Check if Guest's LDT address/limit is changed.
1142 */
1143 if ( GCPtrLdt != pVM->selm.s.GCPtrGuestLdt
1144 || cbLdt != pVM->selm.s.cbLdtLimit)
1145 {
1146 Log(("SELMR3UpdateFromCPUM: Guest LDT changed to from %VGv:%04x to %VGv:%04x. (GDTR=%VGv:%04x)\n",
1147 pVM->selm.s.GCPtrGuestLdt, pVM->selm.s.cbLdtLimit, GCPtrLdt, cbLdt, pVM->selm.s.GuestGdtr.pGdt, pVM->selm.s.GuestGdtr.cbGdt));
1148
1149 /*
1150 * [Re]Register write virtual handler for guest's GDT.
1151 * In the event of LDT overlapping something, don't install it just assume it's being updated.
1152 */
1153 if (pVM->selm.s.GCPtrGuestLdt != RTRCPTR_MAX)
1154 {
1155 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestLdt);
1156 AssertRC(rc);
1157 }
1158#ifdef DEBUG
1159 if (pDesc->Gen.u1Present)
1160 Log(("LDT selector marked not present!!\n"));
1161#endif
1162 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, GCPtrLdt, GCPtrLdt + cbLdt /* already inclusive */,
1163 0, selmGuestLDTWriteHandler, "selmgcGuestLDTWriteHandler", 0, "Guest LDT write access handler");
1164 if (rc == VERR_PGM_HANDLER_VIRTUAL_CONFLICT)
1165 {
1166 /** @todo investigate the various cases where conflicts happen and try avoid them by enh. the instruction emulation. */
1167 pVM->selm.s.GCPtrGuestLdt = RTRCPTR_MAX;
1168 Log(("WARNING: Guest LDT (%VGv:%04x) conflicted with existing access range!! Assumes LDT is begin updated. (GDTR=%VGv:%04x)\n",
1169 GCPtrLdt, cbLdt, pVM->selm.s.GuestGdtr.pGdt, pVM->selm.s.GuestGdtr.cbGdt));
1170 }
1171 else if (VBOX_SUCCESS(rc))
1172 pVM->selm.s.GCPtrGuestLdt = GCPtrLdt;
1173 else
1174 {
1175 CPUMSetHyperLDTR(pVM, 0);
1176 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1177 return rc;
1178 }
1179
1180 pVM->selm.s.cbLdtLimit = cbLdt;
1181 }
1182 }
1183#else
1184 pVM->selm.s.cbLdtLimit = cbLdt;
1185#endif
1186
1187 /*
1188 * Calc Shadow LDT base.
1189 */
1190 unsigned off;
1191 pVM->selm.s.offLdtHyper = off = (GCPtrLdt & PAGE_OFFSET_MASK);
1192 RTGCPTR GCPtrShadowLDT = (RTGCPTR)((RTGCUINTPTR)pVM->selm.s.GCPtrLdt + off);
1193 PX86DESC pShadowLDT = (PX86DESC)((uintptr_t)pVM->selm.s.HCPtrLdt + off);
1194
1195 /*
1196 * Enable the LDT selector in the shadow GDT.
1197 */
1198 pDesc->Gen.u1Present = 1;
1199 pDesc->Gen.u16BaseLow = RT_LOWORD(GCPtrShadowLDT);
1200 pDesc->Gen.u8BaseHigh1 = RT_BYTE3(GCPtrShadowLDT);
1201 pDesc->Gen.u8BaseHigh2 = RT_BYTE4(GCPtrShadowLDT);
1202 pDesc->Gen.u1Available = 0;
1203 pDesc->Gen.u1Long = 0;
1204 if (cbLdt > 0xffff)
1205 {
1206 cbLdt = 0xffff;
1207 pDesc->Gen.u4LimitHigh = 0;
1208 pDesc->Gen.u16LimitLow = pDesc->Gen.u1Granularity ? 0xf : 0xffff;
1209 }
1210
1211 /*
1212 * Set Hyper LDTR and notify TRPM.
1213 */
1214 CPUMSetHyperLDTR(pVM, SelLdt);
1215
1216 /*
1217 * Loop synchronising the LDT page by page.
1218 */
1219 /** @todo investigate how intel handle various operations on half present cross page entries. */
1220 off = GCPtrLdt & (sizeof(X86DESC) - 1);
1221 AssertMsg(!off, ("LDT is not aligned on entry size! GCPtrLdt=%08x\n", GCPtrLdt));
1222
1223 /* Note: Do not skip the first selector; unlike the GDT, a zero LDT selector is perfectly valid. */
1224 unsigned cbLeft = cbLdt + 1;
1225 PX86DESC pLDTE = pShadowLDT;
1226 while (cbLeft)
1227 {
1228 /*
1229 * Read a chunk.
1230 */
1231 unsigned cbChunk = PAGE_SIZE - ((RTGCUINTPTR)GCPtrLdt & PAGE_OFFSET_MASK);
1232 if (cbChunk > cbLeft)
1233 cbChunk = cbLeft;
1234 rc = PGMPhysSimpleReadGCPtr(pVM, pShadowLDT, GCPtrLdt, cbChunk);
1235 if (VBOX_SUCCESS(rc))
1236 {
1237 /*
1238 * Mark page
1239 */
1240 rc = PGMMapSetPage(pVM, GCPtrShadowLDT & PAGE_BASE_GC_MASK, PAGE_SIZE, X86_PTE_P | X86_PTE_A | X86_PTE_D);
1241 AssertRC(rc);
1242
1243 /*
1244 * Loop thru the available LDT entries.
1245 * Figure out where to start and end and the potential cross pageness of
1246 * things adds a little complexity. pLDTE is updated there and not in the
1247 * 'next' part of the loop. The pLDTEEnd is inclusive.
1248 */
1249 PX86DESC pLDTEEnd = (PX86DESC)((uintptr_t)pShadowLDT + cbChunk) - 1;
1250 if (pLDTE + 1 < pShadowLDT)
1251 pLDTE = (PX86DESC)((uintptr_t)pShadowLDT + off);
1252 while (pLDTE <= pLDTEEnd)
1253 {
1254 if (pLDTE->Gen.u1Present)
1255 {
1256 /*
1257 * Code and data selectors are generally 1:1, with the
1258 * 'little' adjustment we do for DPL 0 selectors.
1259 */
1260 if (pLDTE->Gen.u1DescType)
1261 {
1262 /*
1263 * Hack for A-bit against Trap E on read-only GDT.
1264 */
1265 /** @todo Fix this by loading ds and cs before turning off WP. */
1266 if (!(pLDTE->Gen.u4Type & X86_SEL_TYPE_ACCESSED))
1267 pLDTE->Gen.u4Type |= X86_SEL_TYPE_ACCESSED;
1268
1269 /*
1270 * All DPL 0 code and data segments are squeezed into DPL 1.
1271 *
1272 * We're skipping conforming segments here because those
1273 * cannot give us any trouble.
1274 */
1275 if ( pLDTE->Gen.u2Dpl == 0
1276 && (pLDTE->Gen.u4Type & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF))
1277 != (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF) )
1278 pLDTE->Gen.u2Dpl = 1;
1279 }
1280 else
1281 {
1282 /*
1283 * System type selectors are marked not present.
1284 * Recompiler or special handling is required for these.
1285 */
1286 /** @todo what about interrupt gates and rawr0? */
1287 pLDTE->Gen.u1Present = 0;
1288 }
1289 }
1290
1291 /* Next LDT entry. */
1292 pLDTE++;
1293 }
1294 }
1295 else
1296 {
1297 AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("rc=%d\n", rc));
1298 rc = PGMMapSetPage(pVM, GCPtrShadowLDT & PAGE_BASE_GC_MASK, PAGE_SIZE, 0);
1299 AssertRC(rc);
1300 }
1301
1302 /*
1303 * Advance to the next page.
1304 */
1305 cbLeft -= cbChunk;
1306 GCPtrShadowLDT += cbChunk;
1307 pShadowLDT = (PX86DESC)((char *)pShadowLDT + cbChunk);
1308 GCPtrLdt += cbChunk;
1309 }
1310 }
1311
1312 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1313 return VINF_SUCCESS;
1314}
1315
1316
1317/**
1318 * \#PF Handler callback for virtual access handler ranges.
1319 *
1320 * Important to realize that a physical page in a range can have aliases, and
1321 * for ALL and WRITE handlers these will also trigger.
1322 *
1323 * @returns VINF_SUCCESS if the handler have carried out the operation.
1324 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
1325 * @param pVM VM Handle.
1326 * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
1327 * @param pvPtr The HC mapping of that address.
1328 * @param pvBuf What the guest is reading/writing.
1329 * @param cbBuf How much it's reading/writing.
1330 * @param enmAccessType The access type.
1331 * @param pvUser User argument.
1332 */
1333static DECLCALLBACK(int) selmGuestGDTWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
1334{
1335 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
1336 Log(("selmGuestGDTWriteHandler: write to %VGv size %d\n", GCPtr, cbBuf));
1337 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
1338
1339 return VINF_PGM_HANDLER_DO_DEFAULT;
1340}
1341
1342/**
1343 * \#PF Handler callback for virtual access handler ranges.
1344 *
1345 * Important to realize that a physical page in a range can have aliases, and
1346 * for ALL and WRITE handlers these will also trigger.
1347 *
1348 * @returns VINF_SUCCESS if the handler have carried out the operation.
1349 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
1350 * @param pVM VM Handle.
1351 * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
1352 * @param pvPtr The HC mapping of that address.
1353 * @param pvBuf What the guest is reading/writing.
1354 * @param cbBuf How much it's reading/writing.
1355 * @param enmAccessType The access type.
1356 * @param pvUser User argument.
1357 */
1358static DECLCALLBACK(int) selmGuestLDTWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
1359{
1360 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
1361 Log(("selmGuestLDTWriteHandler: write to %VGv size %d\n", GCPtr, cbBuf));
1362 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
1363 return VINF_PGM_HANDLER_DO_DEFAULT;
1364}
1365
1366/**
1367 * \#PF Handler callback for virtual access handler ranges.
1368 *
1369 * Important to realize that a physical page in a range can have aliases, and
1370 * for ALL and WRITE handlers these will also trigger.
1371 *
1372 * @returns VINF_SUCCESS if the handler have carried out the operation.
1373 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
1374 * @param pVM VM Handle.
1375 * @param GCPtr The virtual address the guest is writing to. (not correct if it's an alias!)
1376 * @param pvPtr The HC mapping of that address.
1377 * @param pvBuf What the guest is reading/writing.
1378 * @param cbBuf How much it's reading/writing.
1379 * @param enmAccessType The access type.
1380 * @param pvUser User argument.
1381 */
1382static DECLCALLBACK(int) selmGuestTSSWriteHandler(PVM pVM, RTGCPTR GCPtr, void *pvPtr, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
1383{
1384 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
1385 Log(("selmGuestTSSWriteHandler: write to %VGv size %d\n", GCPtr, cbBuf));
1386 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
1387 return VINF_PGM_HANDLER_DO_DEFAULT;
1388}
1389
1390/**
1391 * Check if the TSS ring 0 stack selector and pointer were updated (for now)
1392 *
1393 * @returns VBox status code.
1394 * @param pVM The VM to operate on.
1395 */
1396VMMR3DECL(int) SELMR3SyncTSS(PVM pVM)
1397{
1398 int rc;
1399
1400 if (pVM->selm.s.fDisableMonitoring)
1401 {
1402 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_TSS);
1403 return VINF_SUCCESS;
1404 }
1405
1406/** @todo r=bird: SELMR3SyncTSS should be VMMAll code.
1407 * All the base, size, flags and stuff must be kept up to date in the CPUM tr register.
1408 */
1409 STAM_PROFILE_START(&pVM->selm.s.StatTSSSync, a);
1410
1411 Assert(!VM_FF_ISSET(pVM, VM_FF_SELM_SYNC_GDT));
1412 Assert(VM_FF_ISSET(pVM, VM_FF_SELM_SYNC_TSS));
1413
1414 /*
1415 * TSS sync
1416 */
1417 RTSEL SelTss = CPUMGetGuestTR(pVM);
1418 if (SelTss & X86_SEL_MASK)
1419 {
1420 /** @todo r=bird: strictly speaking, this is wrong as we shouldn't bother with changes to
1421 * the TSS selector once its loaded. There are a bunch of this kind of problems (see Sander's
1422 * comment in the unzip defect)
1423 * The first part here should only be done when we're loading TR. The latter part which is
1424 * updating of the ss0:esp0 pair can be done by the access handler now since we can trap all
1425 * accesses, also REM ones. */
1426
1427 /*
1428 * Guest TR is not NULL.
1429 */
1430 PX86DESC pDesc = &pVM->selm.s.paGdtHC[SelTss >> X86_SEL_SHIFT];
1431 RTGCPTR GCPtrTss = X86DESC_BASE(*pDesc);
1432 unsigned cbTss = X86DESC_LIMIT(*pDesc);
1433 if (pDesc->Gen.u1Granularity)
1434 cbTss = (cbTss << PAGE_SHIFT) | PAGE_OFFSET_MASK;
1435 cbTss++;
1436 pVM->selm.s.cbGuestTss = cbTss;
1437 pVM->selm.s.fGuestTss32Bit = pDesc->Gen.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL
1438 || pDesc->Gen.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY;
1439
1440 /* Note: We should monitor the whole TSS to catch accesses to the virtual interrupt redirection bitmap, but
1441 * that causes some problems and with Windows guests some overhead as the entire TSS is rather big (3 pages).
1442 * We'll assume for now that the bitmap is static.
1443 */
1444#if 1
1445 /* Don't bother with anything but the core structure. (Actually all we care for is the r0 ss.) */
1446 if (cbTss > sizeof(VBOXTSS))
1447 cbTss = sizeof(VBOXTSS);
1448#endif
1449 /* The guest's TSS can span multiple pages now. We will monitor the whole thing. */
1450 AssertMsg((GCPtrTss >> PAGE_SHIFT) == ((GCPtrTss + sizeof(VBOXTSS) - 1) >> PAGE_SHIFT),
1451 ("GCPtrTss=%VGv cbTss=%#x - We assume everything is inside one page!\n", GCPtrTss, cbTss));
1452
1453 // All system GDTs are marked not present above. That explains why this check fails.
1454 //if (pDesc->Gen.u1Present)
1455 /** @todo Handle only present TSS segments. */
1456 {
1457 /*
1458 * Check if Guest's TSS is changed.
1459 */
1460 if ( GCPtrTss != pVM->selm.s.GCPtrGuestTss
1461 || cbTss != pVM->selm.s.cbMonitoredGuestTss)
1462 {
1463 Log(("SELMR3UpdateFromCPUM: Guest's TSS is changed to pTss=%08X cbTss=%08X cbGuestTss\n", GCPtrTss, cbTss, pVM->selm.s.cbGuestTss));
1464
1465 /*
1466 * Validate it.
1467 */
1468 if ( SelTss & X86_SEL_LDT
1469 || !cbTss
1470 || SelTss >= pVM->selm.s.GuestGdtr.cbGdt
1471 || pDesc->Gen.u1DescType
1472 || ( pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_AVAIL
1473 && pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_286_TSS_BUSY
1474 && pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_AVAIL
1475 && pDesc->Gen.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY) )
1476 {
1477 AssertMsgFailed(("Invalid Guest TSS %04x!\n", SelTss));
1478 }
1479 else
1480 {
1481 /*
1482 * [Re]Register write virtual handler for guest's TSS.
1483 */
1484 if (pVM->selm.s.GCPtrGuestTss != RTRCPTR_MAX)
1485 {
1486 rc = PGMHandlerVirtualDeregister(pVM, pVM->selm.s.GCPtrGuestTss);
1487 AssertRC(rc);
1488 }
1489
1490 rc = PGMR3HandlerVirtualRegister(pVM, PGMVIRTHANDLERTYPE_WRITE, GCPtrTss, GCPtrTss + cbTss - 1,
1491 0, selmGuestTSSWriteHandler, "selmgcGuestTSSWriteHandler", 0, "Guest TSS write access handler");
1492 if (VBOX_FAILURE(rc))
1493 {
1494 STAM_PROFILE_STOP(&pVM->selm.s.StatUpdateFromCPUM, a);
1495 return rc;
1496 }
1497
1498 /* Update saved Guest TSS info. */
1499 pVM->selm.s.GCPtrGuestTss = GCPtrTss;
1500 pVM->selm.s.cbMonitoredGuestTss = cbTss;
1501 pVM->selm.s.GCSelTss = SelTss;
1502 }
1503 }
1504
1505 /* Update the ring 0 stack selector and base address */
1506 /* feeling very lazy; reading too much */
1507 VBOXTSS tss;
1508 rc = PGMPhysSimpleReadGCPtr(pVM, &tss, GCPtrTss, RT_OFFSETOF(VBOXTSS, offIoBitmap) + sizeof(tss.offIoBitmap));
1509 if (VBOX_SUCCESS(rc))
1510 {
1511 #ifdef DEBUG
1512 uint32_t ssr0, espr0;
1513
1514 SELMGetRing1Stack(pVM, &ssr0, &espr0);
1515 ssr0 &= ~1;
1516
1517 if (ssr0 != tss.ss0 || espr0 != tss.esp0)
1518 Log(("SELMR3SyncTSS: Updating TSS ring 0 stack to %04X:%08X\n", tss.ss0, tss.esp0));
1519 Log(("offIoBitmap=%#x\n", tss.offIoBitmap));
1520 #endif
1521 /* Update our TSS structure for the guest's ring 1 stack */
1522 SELMSetRing1Stack(pVM, tss.ss0 | 1, tss.esp0);
1523
1524 /* Should we sync the virtual interrupt redirection bitmap as well? */
1525 if (CPUMGetGuestCR4(pVM) & X86_CR4_VME)
1526 {
1527 uint32_t offRedirBitmap = tss.offIoBitmap - sizeof(tss.IntRedirBitmap);
1528
1529 /** @todo not sure how the partial case is handled; probably not allowed */
1530 if (offRedirBitmap + sizeof(tss.IntRedirBitmap) <= pVM->selm.s.cbGuestTss)
1531 {
1532 rc = PGMPhysSimpleReadGCPtr(pVM, &pVM->selm.s.Tss.IntRedirBitmap, GCPtrTss + offRedirBitmap, sizeof(tss.IntRedirBitmap));
1533 AssertRC(rc);
1534 Log2(("Redirection bitmap:\n"));
1535 Log2(("%.*Vhxd\n", sizeof(tss.IntRedirBitmap), &pVM->selm.s.Tss.IntRedirBitmap));
1536 }
1537 }
1538 }
1539 else
1540 {
1541 /* Note: the ring 0 stack selector and base address are updated on demand in this case. */
1542
1543 /** @todo handle these dependencies better! */
1544 TRPMR3SetGuestTrapHandler(pVM, 0x2E, TRPM_INVALID_HANDLER);
1545 TRPMR3SetGuestTrapHandler(pVM, 0x80, TRPM_INVALID_HANDLER);
1546 pVM->selm.s.fSyncTSSRing0Stack = true;
1547 }
1548 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_TSS);
1549 }
1550 }
1551 else /* Null TR means there's no TSS, has to be reloaded first, so clear the forced action. */
1552 VM_FF_CLEAR(pVM, VM_FF_SELM_SYNC_TSS);
1553
1554 STAM_PROFILE_STOP(&pVM->selm.s.StatTSSSync, a);
1555 return VINF_SUCCESS;
1556}
1557
1558
1559/**
1560 * Compares the Guest GDT and LDT with the shadow tables.
1561 * This is a VBOX_STRICT only function.
1562 *
1563 * @returns VBox status code.
1564 * @param pVM The VM Handle.
1565 */
1566VMMR3DECL(int) SELMR3DebugCheck(PVM pVM)
1567{
1568#ifdef VBOX_STRICT
1569 /*
1570 * Get GDTR and check for conflict.
1571 */
1572 VBOXGDTR GDTR;
1573 CPUMGetGuestGDTR(pVM, &GDTR);
1574 if (GDTR.cbGdt == 0)
1575 return VINF_SUCCESS;
1576
1577 if (GDTR.cbGdt >= (unsigned)(pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> X86_SEL_SHIFT))
1578 Log(("SELMR3DebugCheck: guest GDT size forced us to look for unused selectors.\n"));
1579
1580 if (GDTR.cbGdt != pVM->selm.s.GuestGdtr.cbGdt)
1581 Log(("SELMR3DebugCheck: limits have changed! new=%d old=%d\n", GDTR.cbGdt, pVM->selm.s.GuestGdtr.cbGdt));
1582
1583 /*
1584 * Loop thru the GDT checking each entry.
1585 */
1586 RTGCPTR GCPtrGDTEGuest = GDTR.pGdt;
1587 PX86DESC pGDTE = pVM->selm.s.paGdtHC;
1588 PX86DESC pGDTEEnd = (PX86DESC)((uintptr_t)pGDTE + GDTR.cbGdt);
1589 while (pGDTE < pGDTEEnd)
1590 {
1591 X86DESC GDTEGuest;
1592 int rc = PGMPhysSimpleReadGCPtr(pVM, &GDTEGuest, GCPtrGDTEGuest, sizeof(GDTEGuest));
1593 if (VBOX_SUCCESS(rc))
1594 {
1595 if (pGDTE->Gen.u1DescType || pGDTE->Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1596 {
1597 if ( pGDTE->Gen.u16LimitLow != GDTEGuest.Gen.u16LimitLow
1598 || pGDTE->Gen.u4LimitHigh != GDTEGuest.Gen.u4LimitHigh
1599 || pGDTE->Gen.u16BaseLow != GDTEGuest.Gen.u16BaseLow
1600 || pGDTE->Gen.u8BaseHigh1 != GDTEGuest.Gen.u8BaseHigh1
1601 || pGDTE->Gen.u8BaseHigh2 != GDTEGuest.Gen.u8BaseHigh2
1602 || pGDTE->Gen.u1DefBig != GDTEGuest.Gen.u1DefBig
1603 || pGDTE->Gen.u1DescType != GDTEGuest.Gen.u1DescType)
1604 {
1605 unsigned iGDT = pGDTE - pVM->selm.s.paGdtHC;
1606 SELMR3DumpDescriptor(*pGDTE, iGDT << 3, "SELMR3DebugCheck: GDT mismatch, shadow");
1607 SELMR3DumpDescriptor(GDTEGuest, iGDT << 3, "SELMR3DebugCheck: GDT mismatch, guest");
1608 }
1609 }
1610 }
1611
1612 /* Advance to the next descriptor. */
1613 GCPtrGDTEGuest += sizeof(X86DESC);
1614 pGDTE++;
1615 }
1616
1617
1618 /*
1619 * LDT?
1620 */
1621 RTSEL SelLdt = CPUMGetGuestLDTR(pVM);
1622 if ((SelLdt & X86_SEL_MASK) == 0)
1623 return VINF_SUCCESS;
1624 if (SelLdt > GDTR.cbGdt)
1625 {
1626 Log(("SELMR3DebugCheck: ldt is out of bound SelLdt=%#x\n", SelLdt));
1627 return VERR_INTERNAL_ERROR;
1628 }
1629 X86DESC LDTDesc;
1630 int rc = PGMPhysSimpleReadGCPtr(pVM, &LDTDesc, GDTR.pGdt + (SelLdt & X86_SEL_MASK), sizeof(LDTDesc));
1631 if (VBOX_FAILURE(rc))
1632 {
1633 Log(("SELMR3DebugCheck: Failed to read LDT descriptor. rc=%d\n", rc));
1634 return rc;
1635 }
1636 RTGCPTR GCPtrLDTEGuest = X86DESC_BASE(LDTDesc);
1637 unsigned cbLdt = X86DESC_LIMIT(LDTDesc);
1638 if (LDTDesc.Gen.u1Granularity)
1639 cbLdt = (cbLdt << PAGE_SHIFT) | PAGE_OFFSET_MASK;
1640
1641 /*
1642 * Validate it.
1643 */
1644 if (!cbLdt)
1645 return VINF_SUCCESS;
1646 /** @todo check what intel does about odd limits. */
1647 AssertMsg(RT_ALIGN(cbLdt + 1, sizeof(X86DESC)) == cbLdt + 1 && cbLdt <= 0xffff, ("cbLdt=%d\n", cbLdt));
1648 if ( LDTDesc.Gen.u1DescType
1649 || LDTDesc.Gen.u4Type != X86_SEL_TYPE_SYS_LDT
1650 || SelLdt >= pVM->selm.s.GuestGdtr.cbGdt)
1651 {
1652 Log(("SELmR3DebugCheck: Invalid LDT %04x!\n", SelLdt));
1653 return VERR_INTERNAL_ERROR;
1654 }
1655
1656 /*
1657 * Loop thru the LDT checking each entry.
1658 */
1659 unsigned off = (GCPtrLDTEGuest & PAGE_OFFSET_MASK);
1660 PX86DESC pLDTE = (PX86DESC)((uintptr_t)pVM->selm.s.HCPtrLdt + off);
1661 PX86DESC pLDTEEnd = (PX86DESC)((uintptr_t)pGDTE + cbLdt);
1662 while (pLDTE < pLDTEEnd)
1663 {
1664 X86DESC LDTEGuest;
1665 int rc = PGMPhysSimpleReadGCPtr(pVM, &LDTEGuest, GCPtrLDTEGuest, sizeof(LDTEGuest));
1666 if (VBOX_SUCCESS(rc))
1667 {
1668 if ( pLDTE->Gen.u16LimitLow != LDTEGuest.Gen.u16LimitLow
1669 || pLDTE->Gen.u4LimitHigh != LDTEGuest.Gen.u4LimitHigh
1670 || pLDTE->Gen.u16BaseLow != LDTEGuest.Gen.u16BaseLow
1671 || pLDTE->Gen.u8BaseHigh1 != LDTEGuest.Gen.u8BaseHigh1
1672 || pLDTE->Gen.u8BaseHigh2 != LDTEGuest.Gen.u8BaseHigh2
1673 || pLDTE->Gen.u1DefBig != LDTEGuest.Gen.u1DefBig
1674 || pLDTE->Gen.u1DescType != LDTEGuest.Gen.u1DescType)
1675 {
1676 unsigned iLDT = pLDTE - (PX86DESC)((uintptr_t)pVM->selm.s.HCPtrLdt + off);
1677 SELMR3DumpDescriptor(*pLDTE, iLDT << 3, "SELMR3DebugCheck: LDT mismatch, shadow");
1678 SELMR3DumpDescriptor(LDTEGuest, iLDT << 3, "SELMR3DebugCheck: LDT mismatch, guest");
1679 }
1680 }
1681
1682 /* Advance to the next descriptor. */
1683 GCPtrLDTEGuest += sizeof(X86DESC);
1684 pLDTE++;
1685 }
1686
1687#else
1688 NOREF(pVM);
1689#endif
1690
1691 return VINF_SUCCESS;
1692}
1693
1694
1695/**
1696 * Validates the RawR0 TSS values against the one in the Guest TSS.
1697 *
1698 * @returns true if it matches.
1699 * @returns false and assertions on mismatch..
1700 * @param pVM VM Handle.
1701 */
1702VMMR3DECL(bool) SELMR3CheckTSS(PVM pVM)
1703{
1704#ifdef VBOX_STRICT
1705
1706 RTSEL SelTss = CPUMGetGuestTR(pVM);
1707 if (SelTss & X86_SEL_MASK)
1708 {
1709 AssertMsg((SelTss & X86_SEL_MASK) == (pVM->selm.s.GCSelTss & X86_SEL_MASK), ("New TSS selector = %04X, old TSS selector = %04X\n", SelTss, pVM->selm.s.GCSelTss));
1710
1711 /*
1712 * Guest TR is not NULL.
1713 */
1714 PX86DESC pDesc = &pVM->selm.s.paGdtHC[SelTss >> X86_SEL_SHIFT];
1715 RTGCPTR GCPtrTss = X86DESC_BASE(*pDesc);
1716 unsigned cbTss = X86DESC_LIMIT(*pDesc);
1717 if (pDesc->Gen.u1Granularity)
1718 cbTss = (cbTss << PAGE_SHIFT) | PAGE_OFFSET_MASK;
1719 cbTss++;
1720#if 1
1721 /* Don't bother with anything but the core structure. (Actually all we care for is the r0 ss.) */
1722 if (cbTss > sizeof(VBOXTSS))
1723 cbTss = sizeof(VBOXTSS);
1724#endif
1725 AssertMsg((GCPtrTss >> PAGE_SHIFT) == ((GCPtrTss + sizeof(VBOXTSS) - 1) >> PAGE_SHIFT),
1726 ("GCPtrTss=%VGv cbTss=%#x - We assume everything is inside one page!\n", GCPtrTss, cbTss));
1727
1728 // All system GDTs are marked not present above. That explains why this check fails.
1729 //if (pDesc->Gen.u1Present)
1730 /** @todo Handle only present TSS segments. */
1731 {
1732 /*
1733 * Check if Guest's TSS was changed.
1734 */
1735 if ( GCPtrTss != pVM->selm.s.GCPtrGuestTss
1736 || cbTss != pVM->selm.s.cbMonitoredGuestTss)
1737 {
1738 AssertMsgFailed(("Guest's TSS (Sel 0x%X) is changed from %RGv:%04x to %RGv:%04x\n",
1739 SelTss, pVM->selm.s.GCPtrGuestTss, pVM->selm.s.cbMonitoredGuestTss,
1740 GCPtrTss, cbTss));
1741 }
1742 }
1743
1744 if (!pVM->selm.s.fSyncTSSRing0Stack)
1745 {
1746 RTGCPTR pGuestTSS = pVM->selm.s.GCPtrGuestTss;
1747 uint32_t ESPR0;
1748 int rc = PGMPhysSimpleReadGCPtr(pVM, &ESPR0, pGuestTSS + RT_OFFSETOF(VBOXTSS, esp0), sizeof(ESPR0));
1749 if (VBOX_SUCCESS(rc))
1750 {
1751 RTSEL SelSS0;
1752 rc = PGMPhysSimpleReadGCPtr(pVM, &SelSS0, pGuestTSS + RT_OFFSETOF(VBOXTSS, ss0), sizeof(SelSS0));
1753 if (VBOX_SUCCESS(rc))
1754 {
1755 if ( ESPR0 == pVM->selm.s.Tss.esp1
1756 && SelSS0 == (pVM->selm.s.Tss.ss1 & ~1))
1757 return true;
1758
1759 RTGCPHYS GCPhys;
1760 uint64_t fFlags;
1761
1762 rc = PGMGstGetPage(pVM, pGuestTSS, &fFlags, &GCPhys);
1763 AssertRC(rc);
1764 AssertMsgFailed(("TSS out of sync!! (%04X:%08X vs %04X:%08X (guest)) Tss=%VGv Phys=%VGp\n",
1765 (pVM->selm.s.Tss.ss1 & ~1), pVM->selm.s.Tss.esp1, SelSS0, ESPR0, pGuestTSS, GCPhys));
1766 }
1767 else
1768 AssertRC(rc);
1769 }
1770 else
1771 /* Happens during early Windows XP boot when it is switching page tables. */
1772 Assert(rc == VINF_SUCCESS || ((rc == VERR_PAGE_TABLE_NOT_PRESENT || rc == VERR_PAGE_NOT_PRESENT) && !(CPUMGetGuestEFlags(pVM) & X86_EFL_IF)));
1773 }
1774 }
1775 return false;
1776#else
1777 NOREF(pVM);
1778 return true;
1779#endif
1780}
1781
1782
1783/**
1784 * Returns flat address and limit of LDT by LDT selector from guest GDTR.
1785 *
1786 * Fully validate selector.
1787 *
1788 * @returns VBox status.
1789 * @param pVM VM Handle.
1790 * @param SelLdt LDT selector.
1791 * @param ppvLdt Where to store the flat address of LDT.
1792 * @param pcbLimit Where to store LDT limit.
1793 */
1794VMMDECL(int) SELMGetLDTFromSel(PVM pVM, RTSEL SelLdt, PRTGCPTR ppvLdt, unsigned *pcbLimit)
1795{
1796 /* Get guest GDTR. */
1797 VBOXGDTR GDTR;
1798 CPUMGetGuestGDTR(pVM, &GDTR);
1799
1800 /* Check selector TI and GDT limit. */
1801 if ( SelLdt & X86_SEL_LDT
1802 || (SelLdt > GDTR.cbGdt))
1803 return VERR_INVALID_SELECTOR;
1804
1805 /* Read descriptor from GC. */
1806 X86DESC Desc;
1807 int rc = PGMPhysSimpleReadGCPtr(pVM, (void *)&Desc, (RTGCPTR)(GDTR.pGdt + (SelLdt & X86_SEL_MASK)), sizeof(Desc));
1808 if (VBOX_FAILURE(rc))
1809 {
1810 /* fatal */
1811 AssertMsgFailed(("Can't read LDT descriptor for selector=%04X\n", SelLdt));
1812 return VERR_SELECTOR_NOT_PRESENT;
1813 }
1814
1815 /* Check if LDT descriptor is not present. */
1816 if (Desc.Gen.u1Present == 0)
1817 return VERR_SELECTOR_NOT_PRESENT;
1818
1819 /* Check LDT descriptor type. */
1820 if ( Desc.Gen.u1DescType == 1
1821 || Desc.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1822 return VERR_INVALID_SELECTOR;
1823
1824 /* LDT descriptor is ok. */
1825 if (ppvLdt)
1826 {
1827 *ppvLdt = (RTGCPTR)X86DESC_BASE(Desc);
1828 *pcbLimit = X86DESC_LIMIT(Desc);
1829 }
1830 return VINF_SUCCESS;
1831}
1832
1833/**
1834 * Gets information about a selector.
1835 * Intended for the debugger mostly and will prefer the guest
1836 * descriptor tables over the shadow ones.
1837 *
1838 * @returns VINF_SUCCESS on success.
1839 * @returns VERR_INVALID_SELECTOR if the selector isn't fully inside the descriptor table.
1840 * @returns VERR_SELECTOR_NOT_PRESENT if the selector wasn't present.
1841 * @returns VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the pagetable or page
1842 * backing the selector table wasn't present.
1843 * @returns Other VBox status code on other errors.
1844 *
1845 * @param pVM VM handle.
1846 * @param Sel The selector to get info about.
1847 * @param pSelInfo Where to store the information.
1848 */
1849static int selmr3GetSelectorInfo64(PVM pVM, RTSEL Sel, PSELMSELINFO pSelInfo)
1850{
1851 X86DESC64 Desc;
1852
1853 Assert(pSelInfo);
1854
1855 /*
1856 * Read it from the guest descriptor table.
1857 */
1858 pSelInfo->fHyper = false;
1859
1860 VBOXGDTR Gdtr;
1861 RTGCPTR GCPtrDesc;
1862 CPUMGetGuestGDTR(pVM, &Gdtr);
1863 if (!(Sel & X86_SEL_LDT))
1864 {
1865 /* GDT */
1866 if ((unsigned)(Sel & X86_SEL_MASK) + sizeof(X86DESC) - 1 > (unsigned)Gdtr.cbGdt)
1867 return VERR_INVALID_SELECTOR;
1868 GCPtrDesc = Gdtr.pGdt + (Sel & X86_SEL_MASK);
1869 }
1870 else
1871 {
1872 /*
1873 * LDT - must locate the LDT first...
1874 */
1875 RTSEL SelLdt = CPUMGetGuestLDTR(pVM);
1876 if ( (unsigned)(SelLdt & X86_SEL_MASK) < sizeof(X86DESC) /* the first selector is invalid, right? */
1877 || (unsigned)(SelLdt & X86_SEL_MASK) + sizeof(X86DESC) - 1 > (unsigned)Gdtr.cbGdt)
1878 return VERR_INVALID_SELECTOR;
1879 GCPtrDesc = Gdtr.pGdt + (SelLdt & X86_SEL_MASK);
1880 int rc = PGMPhysSimpleReadGCPtr(pVM, &Desc, GCPtrDesc, sizeof(Desc));
1881 if (VBOX_FAILURE(rc))
1882 return rc;
1883
1884 /* validate the LDT descriptor. */
1885 if (Desc.Gen.u1Present == 0)
1886 return VERR_SELECTOR_NOT_PRESENT;
1887 if ( Desc.Gen.u1DescType == 1
1888 || Desc.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1889 return VERR_INVALID_SELECTOR;
1890
1891 unsigned cbLimit = X86DESC_LIMIT(Desc);
1892 if (Desc.Gen.u1Granularity)
1893 cbLimit = (cbLimit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
1894 if ((unsigned)(Sel & X86_SEL_MASK) + sizeof(X86DESC) - 1 > cbLimit)
1895 return VERR_INVALID_SELECTOR;
1896
1897 /* calc the descriptor location. */
1898 GCPtrDesc = X86DESC64_BASE(Desc);
1899 GCPtrDesc += (Sel & X86_SEL_MASK);
1900 }
1901
1902 /* read the descriptor. */
1903 int rc = PGMPhysSimpleReadGCPtr(pVM, &Desc, GCPtrDesc, sizeof(Desc));
1904 if (VBOX_FAILURE(rc))
1905 return rc;
1906
1907 /*
1908 * Extract the base and limit
1909 */
1910 pSelInfo->Sel = Sel;
1911 pSelInfo->Raw64 = Desc;
1912 pSelInfo->cbLimit = X86DESC_LIMIT(Desc);
1913 if (Desc.Gen.u1Granularity)
1914 pSelInfo->cbLimit = (pSelInfo->cbLimit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
1915 pSelInfo->GCPtrBase = X86DESC64_BASE(Desc);
1916 pSelInfo->fRealMode = false;
1917
1918 return VINF_SUCCESS;
1919}
1920
1921
1922/**
1923 * Gets information about a selector.
1924 * Intended for the debugger mostly and will prefer the guest
1925 * descriptor tables over the shadow ones.
1926 *
1927 * @returns VINF_SUCCESS on success.
1928 * @returns VERR_INVALID_SELECTOR if the selector isn't fully inside the descriptor table.
1929 * @returns VERR_SELECTOR_NOT_PRESENT if the selector wasn't present.
1930 * @returns VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the pagetable or page
1931 * backing the selector table wasn't present.
1932 * @returns Other VBox status code on other errors.
1933 *
1934 * @param pVM VM handle.
1935 * @param Sel The selector to get info about.
1936 * @param pSelInfo Where to store the information.
1937 */
1938VMMR3DECL(int) SELMR3GetSelectorInfo(PVM pVM, RTSEL Sel, PSELMSELINFO pSelInfo)
1939{
1940 Assert(pSelInfo);
1941
1942 if (CPUMIsGuestInLongMode(pVM))
1943 return selmr3GetSelectorInfo64(pVM, Sel, pSelInfo);
1944
1945 /*
1946 * Read the descriptor entry
1947 */
1948 X86DESC Desc;
1949 if ( !(Sel & X86_SEL_LDT)
1950 && ( pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] == (Sel & X86_SEL_MASK)
1951 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] == (Sel & X86_SEL_MASK)
1952 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] == (Sel & X86_SEL_MASK)
1953 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] == (Sel & X86_SEL_MASK)
1954 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] == (Sel & X86_SEL_MASK))
1955 )
1956 {
1957 /*
1958 * Hypervisor descriptor.
1959 */
1960 pSelInfo->fHyper = true;
1961 Desc = pVM->selm.s.paGdtHC[Sel >> X86_SEL_SHIFT];
1962 }
1963 else if (CPUMIsGuestInProtectedMode(pVM))
1964 {
1965 /*
1966 * Read it from the guest descriptor table.
1967 */
1968 pSelInfo->fHyper = false;
1969
1970 VBOXGDTR Gdtr;
1971 RTGCPTR GCPtrDesc;
1972 CPUMGetGuestGDTR(pVM, &Gdtr);
1973 if (!(Sel & X86_SEL_LDT))
1974 {
1975 /* GDT */
1976 if ((unsigned)(Sel & X86_SEL_MASK) + sizeof(X86DESC) - 1 > (unsigned)Gdtr.cbGdt)
1977 return VERR_INVALID_SELECTOR;
1978 GCPtrDesc = Gdtr.pGdt + (Sel & X86_SEL_MASK);
1979 }
1980 else
1981 {
1982 /*
1983 * LDT - must locate the LDT first...
1984 */
1985 RTSEL SelLdt = CPUMGetGuestLDTR(pVM);
1986 if ( (unsigned)(SelLdt & X86_SEL_MASK) < sizeof(X86DESC) /* the first selector is invalid, right? */
1987 || (unsigned)(SelLdt & X86_SEL_MASK) + sizeof(X86DESC) - 1 > (unsigned)Gdtr.cbGdt)
1988 return VERR_INVALID_SELECTOR;
1989 GCPtrDesc = Gdtr.pGdt + (SelLdt & X86_SEL_MASK);
1990 int rc = PGMPhysSimpleReadGCPtr(pVM, &Desc, GCPtrDesc, sizeof(Desc));
1991 if (VBOX_FAILURE(rc))
1992 return rc;
1993
1994 /* validate the LDT descriptor. */
1995 if (Desc.Gen.u1Present == 0)
1996 return VERR_SELECTOR_NOT_PRESENT;
1997 if ( Desc.Gen.u1DescType == 1
1998 || Desc.Gen.u4Type != X86_SEL_TYPE_SYS_LDT)
1999 return VERR_INVALID_SELECTOR;
2000
2001 unsigned cbLimit = X86DESC_LIMIT(Desc);
2002 if (Desc.Gen.u1Granularity)
2003 cbLimit = (cbLimit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
2004 if ((unsigned)(Sel & X86_SEL_MASK) + sizeof(X86DESC) - 1 > cbLimit)
2005 return VERR_INVALID_SELECTOR;
2006
2007 /* calc the descriptor location. */
2008 GCPtrDesc = X86DESC_BASE(Desc);
2009 GCPtrDesc += (Sel & X86_SEL_MASK);
2010 }
2011
2012 /* read the descriptor. */
2013 int rc = PGMPhysSimpleReadGCPtr(pVM, &Desc, GCPtrDesc, sizeof(Desc));
2014 if (VBOX_FAILURE(rc))
2015 return rc;
2016 }
2017 else
2018 {
2019 /*
2020 * We're in real mode.
2021 */
2022 pSelInfo->Sel = Sel;
2023 pSelInfo->GCPtrBase = Sel << 4;
2024 pSelInfo->cbLimit = 0xffff;
2025 pSelInfo->fHyper = false;
2026 pSelInfo->fRealMode = true;
2027 memset(&pSelInfo->Raw, 0, sizeof(pSelInfo->Raw));
2028 return VINF_SUCCESS;
2029 }
2030
2031 /*
2032 * Extract the base and limit
2033 */
2034 pSelInfo->Sel = Sel;
2035 pSelInfo->Raw = Desc;
2036 pSelInfo->cbLimit = X86DESC_LIMIT(Desc);
2037 if (Desc.Gen.u1Granularity)
2038 pSelInfo->cbLimit = (pSelInfo->cbLimit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
2039 pSelInfo->GCPtrBase = X86DESC_BASE(Desc);
2040 pSelInfo->fRealMode = false;
2041
2042 return VINF_SUCCESS;
2043}
2044
2045
2046/**
2047 * Gets information about a selector from the shadow tables.
2048 *
2049 * This is intended to be faster than the SELMR3GetSelectorInfo() method, but requires
2050 * that the caller ensures that the shadow tables are up to date.
2051 *
2052 * @returns VINF_SUCCESS on success.
2053 * @returns VERR_INVALID_SELECTOR if the selector isn't fully inside the descriptor table.
2054 * @returns VERR_SELECTOR_NOT_PRESENT if the selector wasn't present.
2055 * @returns VERR_PAGE_TABLE_NOT_PRESENT or VERR_PAGE_NOT_PRESENT if the pagetable or page
2056 * backing the selector table wasn't present.
2057 * @returns Other VBox status code on other errors.
2058 *
2059 * @param pVM VM handle.
2060 * @param Sel The selector to get info about.
2061 * @param pSelInfo Where to store the information.
2062 */
2063VMMR3DECL(int) SELMR3GetShadowSelectorInfo(PVM pVM, RTSEL Sel, PSELMSELINFO pSelInfo)
2064{
2065 Assert(pSelInfo);
2066
2067 /*
2068 * Read the descriptor entry
2069 */
2070 X86DESC Desc;
2071 if (!(Sel & X86_SEL_LDT))
2072 {
2073 /*
2074 * Global descriptor.
2075 */
2076 Desc = pVM->selm.s.paGdtHC[Sel >> X86_SEL_SHIFT];
2077 pSelInfo->fHyper = pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] == (Sel & X86_SEL_MASK)
2078 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] == (Sel & X86_SEL_MASK)
2079 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] == (Sel & X86_SEL_MASK)
2080 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] == (Sel & X86_SEL_MASK)
2081 || pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] == (Sel & X86_SEL_MASK);
2082 /** @todo check that the GDT offset is valid. */
2083 }
2084 else
2085 {
2086 /*
2087 * Local Descriptor.
2088 */
2089 PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.HCPtrLdt + pVM->selm.s.offLdtHyper);
2090 Desc = paLDT[Sel >> X86_SEL_SHIFT];
2091 /** @todo check if the LDT page is actually available. */
2092 /** @todo check that the LDT offset is valid. */
2093 pSelInfo->fHyper = false;
2094 }
2095
2096 /*
2097 * Extract the base and limit
2098 */
2099 pSelInfo->Sel = Sel;
2100 pSelInfo->Raw = Desc;
2101 pSelInfo->cbLimit = X86DESC_LIMIT(Desc);
2102 if (Desc.Gen.u1Granularity)
2103 pSelInfo->cbLimit = (pSelInfo->cbLimit << PAGE_SHIFT) | PAGE_OFFSET_MASK;
2104 pSelInfo->GCPtrBase = X86DESC_BASE(Desc);
2105 pSelInfo->fRealMode = false;
2106
2107 return VINF_SUCCESS;
2108}
2109
2110
2111/**
2112 * Formats a descriptor.
2113 *
2114 * @param Desc Descriptor to format.
2115 * @param Sel Selector number.
2116 * @param pszOutput Output buffer.
2117 * @param cchOutput Size of output buffer.
2118 */
2119static void selmR3FormatDescriptor(X86DESC Desc, RTSEL Sel, char *pszOutput, size_t cchOutput)
2120{
2121 /*
2122 * Make variable description string.
2123 */
2124 static struct
2125 {
2126 unsigned cch;
2127 const char *psz;
2128 } const aTypes[32] =
2129 {
2130 #define STRENTRY(str) { sizeof(str) - 1, str }
2131 /* system */
2132 STRENTRY("Reserved0 "), /* 0x00 */
2133 STRENTRY("TSS16Avail "), /* 0x01 */
2134 STRENTRY("LDT "), /* 0x02 */
2135 STRENTRY("TSS16Busy "), /* 0x03 */
2136 STRENTRY("Call16 "), /* 0x04 */
2137 STRENTRY("Task "), /* 0x05 */
2138 STRENTRY("Int16 "), /* 0x06 */
2139 STRENTRY("Trap16 "), /* 0x07 */
2140 STRENTRY("Reserved8 "), /* 0x08 */
2141 STRENTRY("TSS32Avail "), /* 0x09 */
2142 STRENTRY("ReservedA "), /* 0x0a */
2143 STRENTRY("TSS32Busy "), /* 0x0b */
2144 STRENTRY("Call32 "), /* 0x0c */
2145 STRENTRY("ReservedD "), /* 0x0d */
2146 STRENTRY("Int32 "), /* 0x0e */
2147 STRENTRY("Trap32 "), /* 0x0f */
2148 /* non system */
2149 STRENTRY("DataRO "), /* 0x10 */
2150 STRENTRY("DataRO Accessed "), /* 0x11 */
2151 STRENTRY("DataRW "), /* 0x12 */
2152 STRENTRY("DataRW Accessed "), /* 0x13 */
2153 STRENTRY("DataDownRO "), /* 0x14 */
2154 STRENTRY("DataDownRO Accessed "), /* 0x15 */
2155 STRENTRY("DataDownRW "), /* 0x16 */
2156 STRENTRY("DataDownRW Accessed "), /* 0x17 */
2157 STRENTRY("CodeEO "), /* 0x18 */
2158 STRENTRY("CodeEO Accessed "), /* 0x19 */
2159 STRENTRY("CodeER "), /* 0x1a */
2160 STRENTRY("CodeER Accessed "), /* 0x1b */
2161 STRENTRY("CodeConfEO "), /* 0x1c */
2162 STRENTRY("CodeConfEO Accessed "), /* 0x1d */
2163 STRENTRY("CodeConfER "), /* 0x1e */
2164 STRENTRY("CodeConfER Accessed ") /* 0x1f */
2165 #undef SYSENTRY
2166 };
2167 #define ADD_STR(psz, pszAdd) do { strcpy(psz, pszAdd); psz += strlen(pszAdd); } while (0)
2168 char szMsg[128];
2169 char *psz = &szMsg[0];
2170 unsigned i = Desc.Gen.u1DescType << 4 | Desc.Gen.u4Type;
2171 memcpy(psz, aTypes[i].psz, aTypes[i].cch);
2172 psz += aTypes[i].cch;
2173
2174 if (Desc.Gen.u1Present)
2175 ADD_STR(psz, "Present ");
2176 else
2177 ADD_STR(psz, "Not-Present ");
2178 if (Desc.Gen.u1Granularity)
2179 ADD_STR(psz, "Page ");
2180 if (Desc.Gen.u1DefBig)
2181 ADD_STR(psz, "32-bit ");
2182 else
2183 ADD_STR(psz, "16-bit ");
2184 #undef ADD_STR
2185 *psz = '\0';
2186
2187 /*
2188 * Limit and Base and format the output.
2189 */
2190 uint32_t u32Limit = X86DESC_LIMIT(Desc);
2191 if (Desc.Gen.u1Granularity)
2192 u32Limit = u32Limit << PAGE_SHIFT | PAGE_OFFSET_MASK;
2193 uint32_t u32Base = X86DESC_BASE(Desc);
2194
2195 RTStrPrintf(pszOutput, cchOutput, "%04x - %08x %08x - base=%08x limit=%08x dpl=%d %s",
2196 Sel, Desc.au32[0], Desc.au32[1], u32Base, u32Limit, Desc.Gen.u2Dpl, szMsg);
2197}
2198
2199
2200/**
2201 * Dumps a descriptor.
2202 *
2203 * @param Desc Descriptor to dump.
2204 * @param Sel Selector number.
2205 * @param pszMsg Message to prepend the log entry with.
2206 */
2207VMMR3DECL(void) SELMR3DumpDescriptor(X86DESC Desc, RTSEL Sel, const char *pszMsg)
2208{
2209 char szOutput[128];
2210 selmR3FormatDescriptor(Desc, Sel, &szOutput[0], sizeof(szOutput));
2211 Log(("%s: %s\n", pszMsg, szOutput));
2212 NOREF(szOutput[0]);
2213}
2214
2215
2216/**
2217 * Display the shadow gdt.
2218 *
2219 * @param pVM VM Handle.
2220 * @param pHlp The info helpers.
2221 * @param pszArgs Arguments, ignored.
2222 */
2223static DECLCALLBACK(void) selmR3InfoGdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2224{
2225 pHlp->pfnPrintf(pHlp, "Shadow GDT (GCAddr=%VGv):\n", MMHyperHC2GC(pVM, pVM->selm.s.paGdtHC));
2226 for (unsigned iGDT = 0; iGDT < SELM_GDT_ELEMENTS; iGDT++)
2227 {
2228 if (pVM->selm.s.paGdtHC[iGDT].Gen.u1Present)
2229 {
2230 char szOutput[128];
2231 selmR3FormatDescriptor(pVM->selm.s.paGdtHC[iGDT], iGDT << X86_SEL_SHIFT, &szOutput[0], sizeof(szOutput));
2232 const char *psz = "";
2233 if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS] >> X86_SEL_SHIFT))
2234 psz = " HyperCS";
2235 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_DS] >> X86_SEL_SHIFT))
2236 psz = " HyperDS";
2237 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_CS64] >> X86_SEL_SHIFT))
2238 psz = " HyperCS64";
2239 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS] >> X86_SEL_SHIFT))
2240 psz = " HyperTSS";
2241 else if (iGDT == ((unsigned)pVM->selm.s.aHyperSel[SELM_HYPER_SEL_TSS_TRAP08] >> X86_SEL_SHIFT))
2242 psz = " HyperTSSTrap08";
2243 pHlp->pfnPrintf(pHlp, "%s%s\n", szOutput, psz);
2244 }
2245 }
2246}
2247
2248
2249/**
2250 * Display the guest gdt.
2251 *
2252 * @param pVM VM Handle.
2253 * @param pHlp The info helpers.
2254 * @param pszArgs Arguments, ignored.
2255 */
2256static DECLCALLBACK(void) selmR3InfoGdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2257{
2258 VBOXGDTR GDTR;
2259 CPUMGetGuestGDTR(pVM, &GDTR);
2260 RTGCPTR pGDTGC = GDTR.pGdt;
2261 unsigned cGDTs = ((unsigned)GDTR.cbGdt + 1) / sizeof(X86DESC);
2262
2263 pHlp->pfnPrintf(pHlp, "Guest GDT (GCAddr=%VGv limit=%x):\n", pGDTGC, GDTR.cbGdt);
2264 for (unsigned iGDT = 0; iGDT < cGDTs; iGDT++, pGDTGC += sizeof(X86DESC))
2265 {
2266 X86DESC GDTE;
2267 int rc = PGMPhysSimpleReadGCPtr(pVM, &GDTE, pGDTGC, sizeof(GDTE));
2268 if (VBOX_SUCCESS(rc))
2269 {
2270 if (GDTE.Gen.u1Present)
2271 {
2272 char szOutput[128];
2273 selmR3FormatDescriptor(GDTE, iGDT << X86_SEL_SHIFT, &szOutput[0], sizeof(szOutput));
2274 pHlp->pfnPrintf(pHlp, "%s\n", szOutput);
2275 }
2276 }
2277 else if (rc == VERR_PAGE_NOT_PRESENT)
2278 {
2279 if ((pGDTGC & PAGE_OFFSET_MASK) + sizeof(X86DESC) - 1 < sizeof(X86DESC))
2280 pHlp->pfnPrintf(pHlp, "%04x - page not present (GCAddr=%VGv)\n", iGDT << X86_SEL_SHIFT, pGDTGC);
2281 }
2282 else
2283 pHlp->pfnPrintf(pHlp, "%04x - read error rc=%Vrc GCAddr=%VGv\n", iGDT << X86_SEL_SHIFT, rc, pGDTGC);
2284 }
2285}
2286
2287
2288/**
2289 * Display the shadow ldt.
2290 *
2291 * @param pVM VM Handle.
2292 * @param pHlp The info helpers.
2293 * @param pszArgs Arguments, ignored.
2294 */
2295static DECLCALLBACK(void) selmR3InfoLdt(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2296{
2297 unsigned cLDTs = ((unsigned)pVM->selm.s.cbLdtLimit + 1) >> X86_SEL_SHIFT;
2298 PX86DESC paLDT = (PX86DESC)((char *)pVM->selm.s.HCPtrLdt + pVM->selm.s.offLdtHyper);
2299 pHlp->pfnPrintf(pHlp, "Shadow LDT (GCAddr=%VGv limit=%d):\n", pVM->selm.s.GCPtrLdt + pVM->selm.s.offLdtHyper, pVM->selm.s.cbLdtLimit);
2300 for (unsigned iLDT = 0; iLDT < cLDTs; iLDT++)
2301 {
2302 if (paLDT[iLDT].Gen.u1Present)
2303 {
2304 char szOutput[128];
2305 selmR3FormatDescriptor(paLDT[iLDT], (iLDT << X86_SEL_SHIFT) | X86_SEL_LDT, &szOutput[0], sizeof(szOutput));
2306 pHlp->pfnPrintf(pHlp, "%s\n", szOutput);
2307 }
2308 }
2309}
2310
2311
2312/**
2313 * Display the guest ldt.
2314 *
2315 * @param pVM VM Handle.
2316 * @param pHlp The info helpers.
2317 * @param pszArgs Arguments, ignored.
2318 */
2319static DECLCALLBACK(void) selmR3InfoLdtGuest(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2320{
2321 RTSEL SelLdt = CPUMGetGuestLDTR(pVM);
2322 if (!(SelLdt & X86_SEL_MASK))
2323 {
2324 pHlp->pfnPrintf(pHlp, "Guest LDT (Sel=%x): Null-Selector\n", SelLdt);
2325 return;
2326 }
2327
2328 RTGCPTR pLdtGC;
2329 unsigned cbLdt;
2330 int rc = SELMGetLDTFromSel(pVM, SelLdt, &pLdtGC, &cbLdt);
2331 if (VBOX_FAILURE(rc))
2332 {
2333 pHlp->pfnPrintf(pHlp, "Guest LDT (Sel=%x): rc=%Vrc\n", SelLdt, rc);
2334 return;
2335 }
2336
2337 pHlp->pfnPrintf(pHlp, "Guest LDT (Sel=%x GCAddr=%VGv limit=%x):\n", SelLdt, pLdtGC, cbLdt);
2338 unsigned cLdts = (cbLdt + 1) >> X86_SEL_SHIFT;
2339 for (unsigned iLdt = 0; iLdt < cLdts; iLdt++, pLdtGC += sizeof(X86DESC))
2340 {
2341 X86DESC LdtE;
2342 int rc = PGMPhysSimpleReadGCPtr(pVM, &LdtE, pLdtGC, sizeof(LdtE));
2343 if (VBOX_SUCCESS(rc))
2344 {
2345 if (LdtE.Gen.u1Present)
2346 {
2347 char szOutput[128];
2348 selmR3FormatDescriptor(LdtE, (iLdt << X86_SEL_SHIFT) | X86_SEL_LDT, &szOutput[0], sizeof(szOutput));
2349 pHlp->pfnPrintf(pHlp, "%s\n", szOutput);
2350 }
2351 }
2352 else if (rc == VERR_PAGE_NOT_PRESENT)
2353 {
2354 if ((pLdtGC & PAGE_OFFSET_MASK) + sizeof(X86DESC) - 1 < sizeof(X86DESC))
2355 pHlp->pfnPrintf(pHlp, "%04x - page not present (GCAddr=%VGv)\n", (iLdt << X86_SEL_SHIFT) | X86_SEL_LDT, pLdtGC);
2356 }
2357 else
2358 pHlp->pfnPrintf(pHlp, "%04x - read error rc=%Vrc GCAddr=%VGv\n", (iLdt << X86_SEL_SHIFT) | X86_SEL_LDT, rc, pLdtGC);
2359 }
2360}
2361
2362
2363/**
2364 * Dumps the hypervisor GDT
2365 *
2366 * @param pVM VM handle.
2367 */
2368VMMR3DECL(void) SELMR3DumpHyperGDT(PVM pVM)
2369{
2370 DBGFR3Info(pVM, "gdt", NULL, NULL);
2371}
2372
2373/**
2374 * Dumps the hypervisor LDT
2375 *
2376 * @param pVM VM handle.
2377 */
2378VMMR3DECL(void) SELMR3DumpHyperLDT(PVM pVM)
2379{
2380 DBGFR3Info(pVM, "ldt", NULL, NULL);
2381}
2382
2383/**
2384 * Dumps the guest GDT
2385 *
2386 * @param pVM VM handle.
2387 */
2388VMMR3DECL(void) SELMR3DumpGuestGDT(PVM pVM)
2389{
2390 DBGFR3Info(pVM, "gdtguest", NULL, NULL);
2391}
2392
2393/**
2394 * Dumps the guest LDT
2395 *
2396 * @param pVM VM handle.
2397 */
2398VMMR3DECL(void) SELMR3DumpGuestLDT(PVM pVM)
2399{
2400 DBGFR3Info(pVM, "ldtguest", NULL, NULL);
2401}
2402
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