VirtualBox

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

Last change on this file since 45555 was 45555, checked in by vboxsync, 12 years ago

nits

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