1 | /* $Id: PGMBth.h 16420 2009-01-30 15:01:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox - Page Manager / Monitor, Shadow+Guest Paging Template.
|
---|
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 | * Internal Functions *
|
---|
25 | *******************************************************************************/
|
---|
26 | __BEGIN_DECLS
|
---|
27 | PGM_BTH_DECL(int, InitData)(PVM pVM, PPGMMODEDATA pModeData, bool fResolveGCAndR0);
|
---|
28 | PGM_BTH_DECL(int, Enter)(PVM pVM, RTGCPHYS GCPhysCR3);
|
---|
29 | PGM_BTH_DECL(int, Relocate)(PVM pVM, RTGCPTR offDelta);
|
---|
30 |
|
---|
31 | PGM_BTH_DECL(int, Trap0eHandler)(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault);
|
---|
32 | PGM_BTH_DECL(int, SyncCR3)(PVM pVM, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal);
|
---|
33 | PGM_BTH_DECL(int, SyncPage)(PVM pVM, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError);
|
---|
34 | PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVM pVM, RTGCPTR Addr, unsigned fPage, unsigned uError);
|
---|
35 | PGM_BTH_DECL(int, InvalidatePage)(PVM pVM, RTGCPTR GCPtrPage);
|
---|
36 | PGM_BTH_DECL(int, PrefetchPage)(PVM pVM, RTGCPTR GCPtrPage);
|
---|
37 | PGM_BTH_DECL(unsigned, AssertCR3)(PVM pVM, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr = 0, RTGCPTR cb = ~(RTGCPTR)0);
|
---|
38 | PGM_BTH_DECL(int, MapCR3)(PVM pVM, RTGCPHYS GCPhysCR3);
|
---|
39 | PGM_BTH_DECL(int, UnmapCR3)(PVM pVM);
|
---|
40 | __END_DECLS
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Initializes the both bit of the paging mode data.
|
---|
45 | *
|
---|
46 | * @returns VBox status code.
|
---|
47 | * @param pVM The VM handle.
|
---|
48 | * @param fResolveGCAndR0 Indicate whether or not GC and Ring-0 symbols can be resolved now.
|
---|
49 | * This is used early in the init process to avoid trouble with PDM
|
---|
50 | * not being initialized yet.
|
---|
51 | */
|
---|
52 | PGM_BTH_DECL(int, InitData)(PVM pVM, PPGMMODEDATA pModeData, bool fResolveGCAndR0)
|
---|
53 | {
|
---|
54 | Assert(pModeData->uShwType == PGM_SHW_TYPE); Assert(pModeData->uGstType == PGM_GST_TYPE);
|
---|
55 |
|
---|
56 | /* Ring 3 */
|
---|
57 | pModeData->pfnR3BthRelocate = PGM_BTH_NAME(Relocate);
|
---|
58 | pModeData->pfnR3BthSyncCR3 = PGM_BTH_NAME(SyncCR3);
|
---|
59 | pModeData->pfnR3BthInvalidatePage = PGM_BTH_NAME(InvalidatePage);
|
---|
60 | pModeData->pfnR3BthSyncPage = PGM_BTH_NAME(SyncPage);
|
---|
61 | pModeData->pfnR3BthPrefetchPage = PGM_BTH_NAME(PrefetchPage);
|
---|
62 | pModeData->pfnR3BthVerifyAccessSyncPage = PGM_BTH_NAME(VerifyAccessSyncPage);
|
---|
63 | #ifdef VBOX_STRICT
|
---|
64 | pModeData->pfnR3BthAssertCR3 = PGM_BTH_NAME(AssertCR3);
|
---|
65 | #endif
|
---|
66 | pModeData->pfnR3BthMapCR3 = PGM_BTH_NAME(MapCR3);
|
---|
67 | pModeData->pfnR3BthUnmapCR3 = PGM_BTH_NAME(UnmapCR3);
|
---|
68 |
|
---|
69 | if (fResolveGCAndR0)
|
---|
70 | {
|
---|
71 | int rc;
|
---|
72 |
|
---|
73 | #if PGM_SHW_TYPE != PGM_TYPE_AMD64 && PGM_SHW_TYPE != PGM_TYPE_NESTED && PGM_SHW_TYPE != PGM_TYPE_EPT /* No AMD64 for traditional virtualization, only VT-x and AMD-V. */
|
---|
74 | /* GC */
|
---|
75 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(Trap0eHandler), &pModeData->pfnRCBthTrap0eHandler);
|
---|
76 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(Trap0eHandler), rc), rc);
|
---|
77 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(InvalidatePage), &pModeData->pfnRCBthInvalidatePage);
|
---|
78 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(InvalidatePage), rc), rc);
|
---|
79 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(SyncCR3), &pModeData->pfnRCBthSyncCR3);
|
---|
80 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(SyncPage), rc), rc);
|
---|
81 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(SyncPage), &pModeData->pfnRCBthSyncPage);
|
---|
82 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(SyncPage), rc), rc);
|
---|
83 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(PrefetchPage), &pModeData->pfnRCBthPrefetchPage);
|
---|
84 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(PrefetchPage), rc), rc);
|
---|
85 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(VerifyAccessSyncPage),&pModeData->pfnRCBthVerifyAccessSyncPage);
|
---|
86 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(VerifyAccessSyncPage), rc), rc);
|
---|
87 | # ifdef VBOX_STRICT
|
---|
88 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(AssertCR3), &pModeData->pfnRCBthAssertCR3);
|
---|
89 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(AssertCR3), rc), rc);
|
---|
90 | # endif
|
---|
91 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(MapCR3), &pModeData->pfnRCBthMapCR3);
|
---|
92 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(MapCR3), rc), rc);
|
---|
93 | rc = PDMR3LdrGetSymbolRC(pVM, NULL, PGM_BTH_NAME_RC_STR(UnmapCR3), &pModeData->pfnRCBthUnmapCR3);
|
---|
94 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_RC_STR(UnmapCR3), rc), rc);
|
---|
95 | #endif /* Not AMD64 shadow paging. */
|
---|
96 |
|
---|
97 | /* Ring 0 */
|
---|
98 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(Trap0eHandler), &pModeData->pfnR0BthTrap0eHandler);
|
---|
99 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(Trap0eHandler), rc), rc);
|
---|
100 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(InvalidatePage), &pModeData->pfnR0BthInvalidatePage);
|
---|
101 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(InvalidatePage), rc), rc);
|
---|
102 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(SyncCR3), &pModeData->pfnR0BthSyncCR3);
|
---|
103 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(SyncCR3), rc), rc);
|
---|
104 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(SyncPage), &pModeData->pfnR0BthSyncPage);
|
---|
105 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(SyncPage), rc), rc);
|
---|
106 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(PrefetchPage), &pModeData->pfnR0BthPrefetchPage);
|
---|
107 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(PrefetchPage), rc), rc);
|
---|
108 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(VerifyAccessSyncPage),&pModeData->pfnR0BthVerifyAccessSyncPage);
|
---|
109 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(VerifyAccessSyncPage), rc), rc);
|
---|
110 | #ifdef VBOX_STRICT
|
---|
111 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(AssertCR3), &pModeData->pfnR0BthAssertCR3);
|
---|
112 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(AssertCR3), rc), rc);
|
---|
113 | #endif
|
---|
114 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(MapCR3), &pModeData->pfnR0BthMapCR3);
|
---|
115 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(MapCR3), rc), rc);
|
---|
116 | rc = PDMR3LdrGetSymbolR0(pVM, NULL, PGM_BTH_NAME_R0_STR(UnmapCR3), &pModeData->pfnR0BthUnmapCR3);
|
---|
117 | AssertMsgRCReturn(rc, ("%s -> rc=%Rrc\n", PGM_BTH_NAME_R0_STR(UnmapCR3), rc), rc);
|
---|
118 | }
|
---|
119 | return VINF_SUCCESS;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Enters the shadow+guest mode.
|
---|
125 | *
|
---|
126 | * @returns VBox status code.
|
---|
127 | * @param pVM VM handle.
|
---|
128 | * @param GCPhysCR3 The physical address from the CR3 register.
|
---|
129 | */
|
---|
130 | PGM_BTH_DECL(int, Enter)(PVM pVM, RTGCPHYS GCPhysCR3)
|
---|
131 | {
|
---|
132 | #ifdef VBOX_WITH_PGMPOOL_PAGING_ONLY
|
---|
133 | /* Here we deal with allocation of the root shadow page table for real and protected mode during mode switches;
|
---|
134 | * Other modes rely on MapCR3/UnmapCR3 to setup the shadow root page tables.
|
---|
135 | */
|
---|
136 | # if ( ( PGM_SHW_TYPE == PGM_TYPE_32BITS \
|
---|
137 | || PGM_SHW_TYPE == PGM_TYPE_PAE \
|
---|
138 | || PGM_SHW_TYPE == PGM_TYPE_AMD64) \
|
---|
139 | && ( PGM_GST_TYPE == PGM_TYPE_REAL \
|
---|
140 | || PGM_GST_TYPE == PGM_TYPE_PROT))
|
---|
141 |
|
---|
142 | Assert(!HWACCMIsNestedPagingActive(pVM));
|
---|
143 | /* We only need shadow paging in real and protected mode for VT-x and AMD-V (excluding nested paging/EPT modes) */
|
---|
144 | if (HWACCMR3IsActive(pVM))
|
---|
145 | {
|
---|
146 | /* Free the previous root mapping if still active. */
|
---|
147 | PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
|
---|
148 | if (pVM->pgm.s.CTX_SUFF(pShwPageCR3))
|
---|
149 | {
|
---|
150 | /* It might have been freed already by a pool flush (see e.g. PGMR3MappingsUnfix). */
|
---|
151 | /** @todo Coordinate this better with the pool. */
|
---|
152 | if (pVM->pgm.s.CTX_SUFF(pShwPageCR3)->enmKind != PGMPOOLKIND_FREE)
|
---|
153 | pgmPoolFreeByPage(pPool, pVM->pgm.s.CTX_SUFF(pShwPageCR3), pVM->pgm.s.iShwUser, pVM->pgm.s.iShwUserTable);
|
---|
154 | pVM->pgm.s.pShwPageCR3R3 = 0;
|
---|
155 | pVM->pgm.s.pShwPageCR3R0 = 0;
|
---|
156 | pVM->pgm.s.pShwRootR3 = 0;
|
---|
157 | # ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
158 | pVM->pgm.s.pShwRootR0 = 0;
|
---|
159 | # endif
|
---|
160 | pVM->pgm.s.HCPhysShwCR3 = 0;
|
---|
161 | pVM->pgm.s.iShwUser = 0;
|
---|
162 | pVM->pgm.s.iShwUserTable = 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* contruct a fake address */
|
---|
166 | # if PGM_GST_TYPE == PGM_TYPE_REAL
|
---|
167 | RTGCPHYS GCPhysCR3 = RT_BIT_64(63);
|
---|
168 | # else
|
---|
169 | RTGCPHYS GCPhysCR3 = RT_BIT_64(63) | RT_BIT_64(62);
|
---|
170 | # endif
|
---|
171 | pVM->pgm.s.iShwUser = SHW_POOL_ROOT_IDX;
|
---|
172 | pVM->pgm.s.iShwUserTable = GCPhysCR3 >> PAGE_SHIFT;
|
---|
173 | int rc = pgmPoolAlloc(pVM, GCPhysCR3, BTH_PGMPOOLKIND_ROOT, pVM->pgm.s.iShwUser, pVM->pgm.s.iShwUserTable, &pVM->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
174 | if (rc == VERR_PGM_POOL_FLUSHED)
|
---|
175 | {
|
---|
176 | Log(("Bth-Enter: PGM pool flushed -> signal sync cr3\n"));
|
---|
177 | Assert(VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
|
---|
178 | return VINF_PGM_SYNC_CR3;
|
---|
179 | }
|
---|
180 | AssertRCReturn(rc, rc);
|
---|
181 | # ifdef IN_RING0
|
---|
182 | pVM->pgm.s.pShwPageCR3R3 = MMHyperCCToR3(pVM, pVM->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
183 | # else
|
---|
184 | pVM->pgm.s.pShwPageCR3R0 = MMHyperCCToR0(pVM, pVM->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
185 | # endif
|
---|
186 | pVM->pgm.s.pShwRootR3 = (R3PTRTYPE(void *))pVM->pgm.s.CTX_SUFF(pShwPageCR3)->pvPageR3;
|
---|
187 | Assert(pVM->pgm.s.pShwRootR3);
|
---|
188 | # ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
189 | pVM->pgm.s.pShwRootR0 = (R0PTRTYPE(void *))PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pVM->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
190 | # endif
|
---|
191 | pVM->pgm.s.HCPhysShwCR3 = pVM->pgm.s.CTX_SUFF(pShwPageCR3)->Core.Key;
|
---|
192 | }
|
---|
193 | # endif
|
---|
194 | #else
|
---|
195 | /* nothing special to do here - InitData does the job. */
|
---|
196 | #endif
|
---|
197 | return VINF_SUCCESS;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Relocate any GC pointers related to shadow mode paging.
|
---|
203 | *
|
---|
204 | * @returns VBox status code.
|
---|
205 | * @param pVM The VM handle.
|
---|
206 | * @param offDelta The reloation offset.
|
---|
207 | */
|
---|
208 | PGM_BTH_DECL(int, Relocate)(PVM pVM, RTGCPTR offDelta)
|
---|
209 | {
|
---|
210 | /* nothing special to do here - InitData does the job. */
|
---|
211 | return VINF_SUCCESS;
|
---|
212 | }
|
---|
213 |
|
---|