1 | /* $Id: PGMBth.h 73250 2018-07-19 17:57:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox - Page Manager / Monitor, Shadow+Guest Paging Template.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Internal Functions *
|
---|
21 | *******************************************************************************/
|
---|
22 | RT_C_DECLS_BEGIN
|
---|
23 | PGM_BTH_DECL(int, Enter)(PVMCPU pVCpu, RTGCPHYS GCPhysCR3);
|
---|
24 | PGM_BTH_DECL(int, Relocate)(PVMCPU pVCpu, RTGCPTR offDelta);
|
---|
25 |
|
---|
26 | PGM_BTH_DECL(int, Trap0eHandler)(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, bool *pfLockTaken);
|
---|
27 | PGM_BTH_DECL(int, SyncCR3)(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal);
|
---|
28 | PGM_BTH_DECL(int, VerifyAccessSyncPage)(PVMCPU pVCpu, RTGCPTR Addr, unsigned fPage, unsigned uError);
|
---|
29 | PGM_BTH_DECL(int, InvalidatePage)(PVMCPU pVCpu, RTGCPTR GCPtrPage);
|
---|
30 | PGM_BTH_DECL(int, PrefetchPage)(PVMCPU pVCpu, RTGCPTR GCPtrPage);
|
---|
31 | PGM_BTH_DECL(unsigned, AssertCR3)(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr = 0, RTGCPTR cb = ~(RTGCPTR)0);
|
---|
32 | PGM_BTH_DECL(int, MapCR3)(PVMCPU pVCpu, RTGCPHYS GCPhysCR3);
|
---|
33 | PGM_BTH_DECL(int, UnmapCR3)(PVMCPU pVCpu);
|
---|
34 | RT_C_DECLS_END
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Enters the shadow+guest mode.
|
---|
40 | *
|
---|
41 | * @returns VBox status code.
|
---|
42 | * @param pVCpu The cross context virtual CPU structure.
|
---|
43 | * @param GCPhysCR3 The physical address from the CR3 register.
|
---|
44 | */
|
---|
45 | PGM_BTH_DECL(int, Enter)(PVMCPU pVCpu, RTGCPHYS GCPhysCR3)
|
---|
46 | {
|
---|
47 | /* Here we deal with allocation of the root shadow page table for real and protected mode during mode switches;
|
---|
48 | * Other modes rely on MapCR3/UnmapCR3 to setup the shadow root page tables.
|
---|
49 | */
|
---|
50 | #if ( ( PGM_SHW_TYPE == PGM_TYPE_32BIT \
|
---|
51 | || PGM_SHW_TYPE == PGM_TYPE_PAE \
|
---|
52 | || PGM_SHW_TYPE == PGM_TYPE_AMD64) \
|
---|
53 | && ( PGM_GST_TYPE == PGM_TYPE_REAL \
|
---|
54 | || PGM_GST_TYPE == PGM_TYPE_PROT))
|
---|
55 |
|
---|
56 | PVM pVM = pVCpu->pVMR3;
|
---|
57 |
|
---|
58 | Assert((HMIsNestedPagingActive(pVM) || VM_IS_NEM_ENABLED(pVM)) == pVM->pgm.s.fNestedPaging);
|
---|
59 | Assert(!pVM->pgm.s.fNestedPaging);
|
---|
60 |
|
---|
61 | pgmLock(pVM);
|
---|
62 | /* Note: we only really need shadow paging in real and protected mode for VT-x and AMD-V (excluding nested paging/EPT modes),
|
---|
63 | * but any calls to GC need a proper shadow page setup as well.
|
---|
64 | */
|
---|
65 | /* Free the previous root mapping if still active. */
|
---|
66 | PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
|
---|
67 | if (pVCpu->pgm.s.CTX_SUFF(pShwPageCR3))
|
---|
68 | {
|
---|
69 | Assert(pVCpu->pgm.s.pShwPageCR3R3->enmKind != PGMPOOLKIND_FREE);
|
---|
70 |
|
---|
71 | /* Mark the page as unlocked; allow flushing again. */
|
---|
72 | pgmPoolUnlockPage(pPool, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
73 |
|
---|
74 | # ifndef PGM_WITHOUT_MAPPINGS
|
---|
75 | /* Remove the hypervisor mappings from the shadow page table. */
|
---|
76 | pgmMapDeactivateCR3(pVM, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
77 | # endif
|
---|
78 |
|
---|
79 | pgmPoolFreeByPage(pPool, pVCpu->pgm.s.pShwPageCR3R3, NIL_PGMPOOL_IDX, UINT32_MAX);
|
---|
80 | pVCpu->pgm.s.pShwPageCR3R3 = 0;
|
---|
81 | pVCpu->pgm.s.pShwPageCR3RC = 0;
|
---|
82 | pVCpu->pgm.s.pShwPageCR3R0 = 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* construct a fake address. */
|
---|
86 | GCPhysCR3 = RT_BIT_64(63);
|
---|
87 | int rc = pgmPoolAlloc(pVM, GCPhysCR3, BTH_PGMPOOLKIND_ROOT, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
|
---|
88 | NIL_PGMPOOL_IDX, UINT32_MAX, false /*fLockPage*/,
|
---|
89 | &pVCpu->pgm.s.pShwPageCR3R3);
|
---|
90 | if (rc == VERR_PGM_POOL_FLUSHED)
|
---|
91 | {
|
---|
92 | Log(("Bth-Enter: PGM pool flushed -> signal sync cr3\n"));
|
---|
93 | Assert(VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
|
---|
94 | pgmUnlock(pVM);
|
---|
95 | return VINF_PGM_SYNC_CR3;
|
---|
96 | }
|
---|
97 | AssertRCReturn(rc, rc);
|
---|
98 |
|
---|
99 | /* Mark the page as locked; disallow flushing. */
|
---|
100 | pgmPoolLockPage(pPool, pVCpu->pgm.s.pShwPageCR3R3);
|
---|
101 |
|
---|
102 | pVCpu->pgm.s.pShwPageCR3R0 = MMHyperCCToR0(pVM, pVCpu->pgm.s.pShwPageCR3R3);
|
---|
103 | pVCpu->pgm.s.pShwPageCR3RC = MMHyperCCToRC(pVM, pVCpu->pgm.s.pShwPageCR3R3);
|
---|
104 |
|
---|
105 | /* Set the current hypervisor CR3. */
|
---|
106 | CPUMSetHyperCR3(pVCpu, PGMGetHyperCR3(pVCpu));
|
---|
107 |
|
---|
108 | # ifndef PGM_WITHOUT_MAPPINGS
|
---|
109 | /* Apply all hypervisor mappings to the new CR3. */
|
---|
110 | rc = pgmMapActivateCR3(pVM, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
|
---|
111 | # endif
|
---|
112 |
|
---|
113 | pgmUnlock(pVM);
|
---|
114 | return rc;
|
---|
115 | #else
|
---|
116 | NOREF(pVCpu); NOREF(GCPhysCR3);
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | #endif
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Relocate any GC pointers related to shadow mode paging.
|
---|
124 | *
|
---|
125 | * @returns VBox status code.
|
---|
126 | * @param pVCpu The cross context virtual CPU structure.
|
---|
127 | * @param offDelta The relocation offset.
|
---|
128 | */
|
---|
129 | PGM_BTH_DECL(int, Relocate)(PVMCPU pVCpu, RTGCPTR offDelta)
|
---|
130 | {
|
---|
131 | /* nothing special to do here - InitData does the job. */
|
---|
132 | NOREF(pVCpu); NOREF(offDelta);
|
---|
133 | return VINF_SUCCESS;
|
---|
134 | }
|
---|
135 |
|
---|