VirtualBox

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

Last change on this file since 552 was 23, checked in by vboxsync, 18 years ago

string.h & stdio.h + header cleanups.

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