VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMGC/PGMGC.cpp@ 7821

Last change on this file since 7821 was 7748, checked in by vboxsync, 17 years ago

Removed invalid paging combinations

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.3 KB
Line 
1/* $Id: PGMGC.cpp 7748 2008-04-04 15:42:30Z vboxsync $ */
2/** @file
3 * PGM - Page Monitor, Guest Context.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_PGM
23#include <VBox/pgm.h>
24#include <VBox/cpum.h>
25#include <VBox/mm.h>
26#include <VBox/em.h>
27#include <VBox/selm.h>
28#include <VBox/iom.h>
29#include <VBox/trpm.h>
30#include <VBox/rem.h>
31#include "PGMInternal.h"
32#include <VBox/vm.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <VBox/log.h>
37#include <VBox/param.h>
38#include <VBox/err.h>
39#include <VBox/dis.h>
40#include <VBox/disopcode.h>
41
42
43
44/*******************************************************************************
45* Internal Functions *
46*******************************************************************************/
47
48
49
50#ifndef RT_ARCH_AMD64
51/*
52 * Shadow - 32-bit mode
53 */
54#define PGM_SHW_TYPE PGM_TYPE_32BIT
55#define PGM_SHW_NAME(name) PGM_SHW_NAME_32BIT(name)
56#include "PGMGCShw.h"
57
58/* Guest - real mode */
59#define PGM_GST_TYPE PGM_TYPE_REAL
60#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
61#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_REAL(name)
62#include "PGMGCGst.h"
63#include "PGMGCBth.h"
64#undef PGM_BTH_NAME
65#undef PGM_GST_TYPE
66#undef PGM_GST_NAME
67
68/* Guest - protected mode */
69#define PGM_GST_TYPE PGM_TYPE_PROT
70#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
71#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
72#include "PGMGCGst.h"
73#include "PGMGCBth.h"
74#undef PGM_BTH_NAME
75#undef PGM_GST_TYPE
76#undef PGM_GST_NAME
77
78/* Guest - 32-bit mode */
79#define PGM_GST_TYPE PGM_TYPE_32BIT
80#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
81#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_32BIT(name)
82#include "PGMGCGst.h"
83#include "PGMGCBth.h"
84#undef PGM_BTH_NAME
85#undef PGM_GST_TYPE
86#undef PGM_GST_NAME
87
88#undef PGM_SHW_TYPE
89#undef PGM_SHW_NAME
90#endif /* !RT_ARCH_AMD64 */
91
92
93/*
94 * Shadow - PAE mode
95 */
96#define PGM_SHW_TYPE PGM_TYPE_PAE
97#define PGM_SHW_NAME(name) PGM_SHW_NAME_PAE(name)
98#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
99#include "PGMGCShw.h"
100
101/* Guest - real mode */
102#define PGM_GST_TYPE PGM_TYPE_REAL
103#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
104#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
105#include "PGMGCBth.h"
106#undef PGM_BTH_NAME
107#undef PGM_GST_TYPE
108#undef PGM_GST_NAME
109
110/* Guest - protected mode */
111#define PGM_GST_TYPE PGM_TYPE_PROT
112#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
113#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
114#include "PGMGCBth.h"
115#undef PGM_BTH_NAME
116#undef PGM_GST_TYPE
117#undef PGM_GST_NAME
118
119/* Guest - 32-bit mode */
120#define PGM_GST_TYPE PGM_TYPE_32BIT
121#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
122#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_32BIT(name)
123#include "PGMGCBth.h"
124#undef PGM_BTH_NAME
125#undef PGM_GST_TYPE
126#undef PGM_GST_NAME
127
128/* Guest - PAE mode */
129#define PGM_GST_TYPE PGM_TYPE_PAE
130#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
131#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PAE(name)
132#include "PGMGCGst.h"
133#include "PGMGCBth.h"
134#undef PGM_BTH_NAME
135#undef PGM_GST_TYPE
136#undef PGM_GST_NAME
137
138#undef PGM_SHW_TYPE
139#undef PGM_SHW_NAME
140
141
142/*
143 * Shadow - AMD64 mode
144 */
145#define PGM_SHW_TYPE PGM_TYPE_AMD64
146#define PGM_SHW_NAME(name) PGM_SHW_NAME_AMD64(name)
147#include "PGMGCShw.h"
148
149/* Guest - AMD64 mode */
150#define PGM_GST_TYPE PGM_TYPE_AMD64
151#define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
152#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_AMD64(name)
153#include "PGMGCGst.h"
154#include "PGMGCBth.h"
155#undef PGM_BTH_NAME
156#undef PGM_GST_TYPE
157#undef PGM_GST_NAME
158
159#undef PGM_SHW_TYPE
160#undef PGM_SHW_NAME
161
162
163/**
164 * Temporarily maps one guest page specified by GC physical address.
165 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
166 *
167 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
168 * reused after 8 mappings (or perhaps a few more if you score with the cache).
169 *
170 * @returns VBox status.
171 * @param pVM VM handle.
172 * @param GCPhys GC Physical address of the page.
173 * @param ppv Where to store the address of the mapping.
174 */
175PGMGCDECL(int) PGMGCDynMapGCPage(PVM pVM, RTGCPHYS GCPhys, void **ppv)
176{
177 AssertMsg(!(GCPhys & PAGE_OFFSET_MASK), ("GCPhys=%VGp\n", GCPhys));
178
179 /*
180 * Get the ram range.
181 */
182 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesGC;
183 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
184 pRam = pRam->pNextGC;
185 if (!pRam)
186 {
187 AssertMsgFailed(("Invalid physical address %VGp!\n", GCPhys));
188 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
189 }
190
191 /*
192 * Pass it on to PGMGCDynMapHCPage.
193 */
194 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
195 //Log(("PGMGCDynMapGCPage: GCPhys=%VGp HCPhys=%VHp\n", GCPhys, HCPhys));
196 return PGMGCDynMapHCPage(pVM, HCPhys, ppv);
197}
198
199
200/**
201 * Temporarily maps one guest page specified by unaligned GC physical address.
202 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
203 *
204 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
205 * reused after 8 mappings (or perhaps a few more if you score with the cache).
206 *
207 * The caller is aware that only the speicifed page is mapped and that really bad things
208 * will happen if writing beyond the page!
209 *
210 * @returns VBox status.
211 * @param pVM VM handle.
212 * @param GCPhys GC Physical address within the page to be mapped.
213 * @param ppv Where to store the address of the mapping address corresponding to GCPhys.
214 */
215PGMGCDECL(int) PGMGCDynMapGCPageEx(PVM pVM, RTGCPHYS GCPhys, void **ppv)
216{
217 /*
218 * Get the ram range.
219 */
220 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesGC;
221 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
222 pRam = pRam->pNextGC;
223 if (!pRam)
224 {
225 AssertMsgFailed(("Invalid physical address %VGp!\n", GCPhys));
226 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
227 }
228
229 /*
230 * Pass it on to PGMGCDynMapHCPage.
231 */
232 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
233 int rc = PGMGCDynMapHCPage(pVM, HCPhys, ppv);
234 if (VBOX_SUCCESS(rc))
235 *ppv = (void *)((uintptr_t)*ppv | (GCPhys & PAGE_OFFSET_MASK));
236 return rc;
237}
238
239
240/**
241 * Temporarily maps one host page specified by HC physical address.
242 *
243 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
244 * reused after 8 mappings (or perhaps a few more if you score with the cache).
245 *
246 * @returns VBox status.
247 * @param pVM VM handle.
248 * @param HCPhys HC Physical address of the page.
249 * @param ppv Where to store the address of the mapping.
250 */
251PGMGCDECL(int) PGMGCDynMapHCPage(PVM pVM, RTHCPHYS HCPhys, void **ppv)
252{
253 AssertMsg(!(HCPhys & PAGE_OFFSET_MASK), ("HCPhys=%VHp\n", HCPhys));
254
255 /*
256 * Check the cache.
257 */
258 register unsigned iCache;
259 if ( pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 0] == HCPhys
260 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 1] == HCPhys
261 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 2] == HCPhys
262 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 3] == HCPhys)
263 {
264 static const uint8_t au8Trans[MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT][ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache)] =
265 {
266 { 0, 5, 6, 7 },
267 { 0, 1, 6, 7 },
268 { 0, 1, 2, 7 },
269 { 0, 1, 2, 3 },
270 { 4, 1, 2, 3 },
271 { 4, 5, 2, 3 },
272 { 4, 5, 6, 3 },
273 { 4, 5, 6, 7 },
274 };
275 Assert(ELEMENTS(au8Trans) == 8);
276 Assert(ELEMENTS(au8Trans[0]) == 4);
277 int iPage = au8Trans[pVM->pgm.s.iDynPageMapLast][iCache];
278 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
279 *ppv = pv;
280 STAM_COUNTER_INC(&pVM->pgm.s.StatDynMapCacheHits);
281 //Log(("PGMGCDynMapHCPage: HCPhys=%VHp pv=%VGv iPage=%d iCache=%d\n", HCPhys, pv, iPage, iCache));
282 return VINF_SUCCESS;
283 }
284 Assert(ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) == 4);
285 STAM_COUNTER_INC(&pVM->pgm.s.StatDynMapCacheMisses);
286
287 /*
288 * Update the page tables.
289 */
290 register unsigned iPage = pVM->pgm.s.iDynPageMapLast;
291 pVM->pgm.s.iDynPageMapLast = iPage = (iPage + 1) & ((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) - 1);
292 Assert((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) == 8);
293
294 pVM->pgm.s.aHCPhysDynPageMapCache[iPage & (ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) - 1)] = HCPhys;
295 pVM->pgm.s.paDynPageMap32BitPTEsGC[iPage].u = (uint32_t)HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
296 pVM->pgm.s.paDynPageMapPaePTEsGC[iPage].u = HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
297
298 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
299 *ppv = pv;
300 ASMInvalidatePage(pv);
301 Log4(("PGMGCDynMapHCPage: HCPhys=%VHp pv=%VGv iPage=%d\n", HCPhys, pv, iPage));
302 return VINF_SUCCESS;
303}
304
305
306/**
307 * Emulation of the invlpg instruction.
308 *
309 * @returns VBox status code suitable for scheduling.
310 * @param pVM VM handle.
311 * @param GCPtrPage Page to invalidate.
312 */
313PGMGCDECL(int) PGMGCInvalidatePage(PVM pVM, RTGCPTR GCPtrPage)
314{
315 LogFlow(("PGMGCInvalidatePage: GCPtrPage=%VGv\n", GCPtrPage));
316
317 STAM_PROFILE_START(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
318
319 /*
320 * Check for conflicts and pending CR3 monitoring updates.
321 */
322 if (!pVM->pgm.s.fMappingsFixed)
323 {
324 if ( pgmGetMapping(pVM, GCPtrPage)
325 /** @todo && (PGMGstGetPDE(pVM, GCPtrPage) & X86_PDE_P) - FIX THIS NOW!!! */ )
326 {
327 LogFlow(("PGMGCInvalidatePage: Conflict!\n"));
328 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
329 STAM_PROFILE_STOP(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
330 return VINF_PGM_SYNC_CR3;
331 }
332
333 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
334 {
335 LogFlow(("PGMGCInvalidatePage: PGM_SYNC_MONITOR_CR3 -> reinterpret instruction in HC\n"));
336 STAM_PROFILE_STOP(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
337 /** @todo counter for these... */
338 return VINF_EM_RAW_EMULATE_INSTR;
339 }
340 }
341
342 /*
343 * Notify the recompiler so it can record this instruction.
344 * Failure happens when it's out of space. We'll return to HC in that case.
345 */
346 int rc = REMNotifyInvalidatePage(pVM, GCPtrPage);
347 if (rc == VINF_SUCCESS)
348 rc = PGM_BTH_PFN(InvalidatePage, pVM)(pVM, GCPtrPage);
349
350 STAM_PROFILE_STOP(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
351 return rc;
352}
Note: See TracBrowser for help on using the repository browser.

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