VirtualBox

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

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

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1/* $Id: PGMGC.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
2/** @file
3 * PGM - Page Monitor, Guest Context.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
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 RT_ARCH_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 /* !RT_ARCH_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 - AMD64 mode */
154#define PGM_GST_TYPE PGM_TYPE_AMD64
155#define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
156#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_AMD64(name)
157#include "PGMGCGst.h"
158#include "PGMGCBth.h"
159#undef PGM_BTH_NAME
160#undef PGM_GST_TYPE
161#undef PGM_GST_NAME
162
163#undef PGM_SHW_TYPE
164#undef PGM_SHW_NAME
165
166
167/**
168 * Temporarily maps one guest page specified by GC physical address.
169 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
170 *
171 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
172 * reused after 8 mappings (or perhaps a few more if you score with the cache).
173 *
174 * @returns VBox status.
175 * @param pVM VM handle.
176 * @param GCPhys GC Physical address of the page.
177 * @param ppv Where to store the address of the mapping.
178 */
179PGMGCDECL(int) PGMGCDynMapGCPage(PVM pVM, RTGCPHYS GCPhys, void **ppv)
180{
181 AssertMsg(!(GCPhys & PAGE_OFFSET_MASK), ("GCPhys=%VGp\n", GCPhys));
182
183 /*
184 * Get the ram range.
185 */
186 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesGC;
187 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
188 pRam = pRam->pNextGC;
189 if (!pRam)
190 {
191 AssertMsgFailed(("Invalid physical address %VGp!\n", GCPhys));
192 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
193 }
194
195 /*
196 * Pass it on to PGMGCDynMapHCPage.
197 */
198 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
199 //Log(("PGMGCDynMapGCPage: GCPhys=%VGp HCPhys=%VHp\n", GCPhys, HCPhys));
200 return PGMGCDynMapHCPage(pVM, HCPhys, ppv);
201}
202
203
204/**
205 * Temporarily maps one guest page specified by unaligned GC physical address.
206 * These pages must have a physical mapping in HC, i.e. they cannot be MMIO pages.
207 *
208 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
209 * reused after 8 mappings (or perhaps a few more if you score with the cache).
210 *
211 * The caller is aware that only the speicifed page is mapped and that really bad things
212 * will happen if writing beyond the page!
213 *
214 * @returns VBox status.
215 * @param pVM VM handle.
216 * @param GCPhys GC Physical address within the page to be mapped.
217 * @param ppv Where to store the address of the mapping address corresponding to GCPhys.
218 */
219PGMGCDECL(int) PGMGCDynMapGCPageEx(PVM pVM, RTGCPHYS GCPhys, void **ppv)
220{
221 /*
222 * Get the ram range.
223 */
224 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesGC;
225 while (pRam && GCPhys - pRam->GCPhys >= pRam->cb)
226 pRam = pRam->pNextGC;
227 if (!pRam)
228 {
229 AssertMsgFailed(("Invalid physical address %VGp!\n", GCPhys));
230 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
231 }
232
233 /*
234 * Pass it on to PGMGCDynMapHCPage.
235 */
236 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT]);
237 int rc = PGMGCDynMapHCPage(pVM, HCPhys, ppv);
238 if (VBOX_SUCCESS(rc))
239 *ppv = (void *)((uintptr_t)*ppv | (GCPhys & PAGE_OFFSET_MASK));
240 return rc;
241}
242
243
244/**
245 * Temporarily maps one host page specified by HC physical address.
246 *
247 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
248 * reused after 8 mappings (or perhaps a few more if you score with the cache).
249 *
250 * @returns VBox status.
251 * @param pVM VM handle.
252 * @param HCPhys HC Physical address of the page.
253 * @param ppv Where to store the address of the mapping.
254 */
255PGMGCDECL(int) PGMGCDynMapHCPage(PVM pVM, RTHCPHYS HCPhys, void **ppv)
256{
257 AssertMsg(!(HCPhys & PAGE_OFFSET_MASK), ("HCPhys=%VHp\n", HCPhys));
258
259 /*
260 * Check the cache.
261 */
262 register unsigned iCache;
263 if ( pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 0] == HCPhys
264 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 1] == HCPhys
265 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 2] == HCPhys
266 || pVM->pgm.s.aHCPhysDynPageMapCache[iCache = 3] == HCPhys)
267 {
268 static const uint8_t au8Trans[MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT][ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache)] =
269 {
270 { 0, 5, 6, 7 },
271 { 0, 1, 6, 7 },
272 { 0, 1, 2, 7 },
273 { 0, 1, 2, 3 },
274 { 4, 1, 2, 3 },
275 { 4, 5, 2, 3 },
276 { 4, 5, 6, 3 },
277 { 4, 5, 6, 7 },
278 };
279 Assert(ELEMENTS(au8Trans) == 8);
280 Assert(ELEMENTS(au8Trans[0]) == 4);
281 int iPage = au8Trans[pVM->pgm.s.iDynPageMapLast][iCache];
282 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
283 *ppv = pv;
284 STAM_COUNTER_INC(&pVM->pgm.s.StatDynMapCacheHits);
285 //Log(("PGMGCDynMapHCPage: HCPhys=%VHp pv=%VGv iPage=%d iCache=%d\n", HCPhys, pv, iPage, iCache));
286 return VINF_SUCCESS;
287 }
288 Assert(ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) == 4);
289 STAM_COUNTER_INC(&pVM->pgm.s.StatDynMapCacheMisses);
290
291 /*
292 * Update the page tables.
293 */
294 register unsigned iPage = pVM->pgm.s.iDynPageMapLast;
295 pVM->pgm.s.iDynPageMapLast = iPage = (iPage + 1) & ((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) - 1);
296 Assert((MM_HYPER_DYNAMIC_SIZE >> PAGE_SHIFT) == 8);
297
298 pVM->pgm.s.aHCPhysDynPageMapCache[iPage & (ELEMENTS(pVM->pgm.s.aHCPhysDynPageMapCache) - 1)] = HCPhys;
299 pVM->pgm.s.paDynPageMap32BitPTEsGC[iPage].u = (uint32_t)HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
300 pVM->pgm.s.paDynPageMapPaePTEsGC[iPage].u = HCPhys | X86_PTE_P | X86_PTE_A | X86_PTE_D;
301
302 void *pv = pVM->pgm.s.pbDynPageMapBaseGC + (iPage << PAGE_SHIFT);
303 *ppv = pv;
304 ASMInvalidatePage(pv);
305 Log4(("PGMGCDynMapHCPage: HCPhys=%VHp pv=%VGv iPage=%d\n", HCPhys, pv, iPage));
306 return VINF_SUCCESS;
307}
308
309
310/**
311 * Emulation of the invlpg instruction.
312 *
313 * @returns VBox status code suitable for scheduling.
314 * @param pVM VM handle.
315 * @param GCPtrPage Page to invalidate.
316 */
317PGMGCDECL(int) PGMGCInvalidatePage(PVM pVM, RTGCPTR GCPtrPage)
318{
319 LogFlow(("PGMGCInvalidatePage: GCPtrPage=%VGv\n", GCPtrPage));
320
321 STAM_PROFILE_START(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
322
323 /*
324 * Check for conflicts and pending CR3 monitoring updates.
325 */
326 if (!pVM->pgm.s.fMappingsFixed)
327 {
328 if ( pgmGetMapping(pVM, GCPtrPage)
329 /** @todo && (PGMGstGetPDE(pVM, GCPtrPage) & X86_PDE_P) - FIX THIS NOW!!! */ )
330 {
331 LogFlow(("PGMGCInvalidatePage: Conflict!\n"));
332 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
333 STAM_PROFILE_STOP(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
334 return VINF_PGM_SYNC_CR3;
335 }
336
337 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_MONITOR_CR3)
338 {
339 LogFlow(("PGMGCInvalidatePage: PGM_SYNC_MONITOR_CR3 -> reinterpret instruction in HC\n"));
340 STAM_PROFILE_STOP(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
341 /** @todo counter for these... */
342 return VINF_EM_RAW_EMULATE_INSTR;
343 }
344 }
345
346 /*
347 * Notify the recompiler so it can record this instruction.
348 * Failure happens when it's out of space. We'll return to HC in that case.
349 */
350 int rc = REMNotifyInvalidatePage(pVM, GCPtrPage);
351 if (rc == VINF_SUCCESS)
352 rc = PGM_BTH_PFN(InvalidatePage, pVM)(pVM, GCPtrPage);
353
354 STAM_PROFILE_STOP(&CTXMID(pVM->pgm.s.Stat,InvalidatePage), a);
355 return rc;
356}
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