VirtualBox

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

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

VMM: Split up virtual handlers just like the physical ones, such that the kind+callbacks are stored seprately from the actual handler registration. This should hopefully save a byte or two when adding more callbacks. Implemented the pvUser for ring-3 callbacks.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use