1 | /* $Id: REMAll.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * REM - Recompiled Execution Monitor, all Contexts part.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_REM
|
---|
23 | #include <VBox/rem.h>
|
---|
24 | #include <VBox/em.h>
|
---|
25 | #include <VBox/vmm.h>
|
---|
26 | #include "REMInternal.h"
|
---|
27 | #include <VBox/vm.h>
|
---|
28 | #include <VBox/err.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 | #include <iprt/assert.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | #ifndef IN_RING3
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Records a invlpg instruction for replaying upon REM entry.
|
---|
38 | *
|
---|
39 | * @param pVM The VM handle.
|
---|
40 | * @param GCPtrPage The
|
---|
41 | */
|
---|
42 | VMMDECL(void) REMNotifyInvalidatePage(PVM pVM, RTGCPTR GCPtrPage)
|
---|
43 | {
|
---|
44 | /*
|
---|
45 | * Try take the REM lock and push the address onto the array.
|
---|
46 | */
|
---|
47 | if ( pVM->rem.s.cInvalidatedPages < RT_ELEMENTS(pVM->rem.s.aGCPtrInvalidatedPages)
|
---|
48 | && EMTryEnterRemLock(pVM) == VINF_SUCCESS)
|
---|
49 | {
|
---|
50 | uint32_t iPage = pVM->rem.s.cInvalidatedPages;
|
---|
51 | if (iPage < RT_ELEMENTS(pVM->rem.s.aGCPtrInvalidatedPages))
|
---|
52 | {
|
---|
53 | ASMAtomicWriteU32(&pVM->rem.s.cInvalidatedPages, iPage + 1);
|
---|
54 | pVM->rem.s.aGCPtrInvalidatedPages[iPage] = GCPtrPage;
|
---|
55 |
|
---|
56 | EMRemUnlock(pVM);
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | CPUMSetChangedFlags(VMMGetCpu(pVM), CPUM_CHANGED_GLOBAL_TLB_FLUSH); /** @todo this array should be per-cpu technically speaking. */
|
---|
61 | ASMAtomicWriteU32(&pVM->rem.s.cInvalidatedPages, 0); /** @todo leave this alone? Optimize this code? */
|
---|
62 |
|
---|
63 | EMRemUnlock(pVM);
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | /* Fallback: Simply tell the recompiler to flush its TLB. */
|
---|
68 | CPUMSetChangedFlags(VMMGetCpu(pVM), CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
69 | ASMAtomicWriteU32(&pVM->rem.s.cInvalidatedPages, 0); /** @todo leave this alone?! Optimize this code? */
|
---|
70 | }
|
---|
71 |
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Insert pending notification
|
---|
78 | *
|
---|
79 | * @param pVM VM Handle.
|
---|
80 | * @param pRec Notification record to insert
|
---|
81 | */
|
---|
82 | static void remNotifyHandlerInsert(PVM pVM, PREMHANDLERNOTIFICATION pRec)
|
---|
83 | {
|
---|
84 | /*
|
---|
85 | * Fetch a free record.
|
---|
86 | */
|
---|
87 | uint32_t cFlushes = 0;
|
---|
88 | uint32_t idxFree;
|
---|
89 | PREMHANDLERNOTIFICATION pFree;
|
---|
90 | do
|
---|
91 | {
|
---|
92 | idxFree = ASMAtomicUoReadU32(&pVM->rem.s.idxFreeList);
|
---|
93 | if (idxFree == UINT32_MAX)
|
---|
94 | {
|
---|
95 | do
|
---|
96 | {
|
---|
97 | cFlushes++;
|
---|
98 | Assert(cFlushes != 128);
|
---|
99 | AssertFatal(cFlushes < _1M);
|
---|
100 | VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS, 0);
|
---|
101 | idxFree = ASMAtomicUoReadU32(&pVM->rem.s.idxFreeList);
|
---|
102 | } while (idxFree == UINT32_MAX);
|
---|
103 | }
|
---|
104 | pFree = &pVM->rem.s.aHandlerNotifications[idxFree];
|
---|
105 | } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxFreeList, pFree->idxNext, idxFree));
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Copy the record.
|
---|
109 | */
|
---|
110 | pFree->enmKind = pRec->enmKind;
|
---|
111 | pFree->u = pRec->u;
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Insert it into the pending list.
|
---|
115 | */
|
---|
116 | uint32_t idxNext;
|
---|
117 | do
|
---|
118 | {
|
---|
119 | idxNext = ASMAtomicUoReadU32(&pVM->rem.s.idxPendingList);
|
---|
120 | ASMAtomicWriteU32(&pFree->idxNext, idxNext);
|
---|
121 | ASMCompilerBarrier();
|
---|
122 | } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxPendingList, idxFree, idxNext));
|
---|
123 |
|
---|
124 | VM_FF_SET(pVM, VM_FF_REM_HANDLER_NOTIFY);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Notification about a successful PGMR3HandlerPhysicalRegister() call.
|
---|
130 | *
|
---|
131 | * @param pVM VM Handle.
|
---|
132 | * @param enmType Handler type.
|
---|
133 | * @param GCPhys Handler range address.
|
---|
134 | * @param cb Size of the handler range.
|
---|
135 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
136 | */
|
---|
137 | VMMDECL(void) REMNotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler)
|
---|
138 | {
|
---|
139 | REMHANDLERNOTIFICATION Rec;
|
---|
140 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_REGISTER;
|
---|
141 | Rec.u.PhysicalRegister.enmType = enmType;
|
---|
142 | Rec.u.PhysicalRegister.GCPhys = GCPhys;
|
---|
143 | Rec.u.PhysicalRegister.cb = cb;
|
---|
144 | Rec.u.PhysicalRegister.fHasHCHandler = fHasHCHandler;
|
---|
145 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
|
---|
151 | *
|
---|
152 | * @param pVM VM Handle.
|
---|
153 | * @param enmType Handler type.
|
---|
154 | * @param GCPhys Handler range address.
|
---|
155 | * @param cb Size of the handler range.
|
---|
156 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
157 | * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
|
---|
158 | */
|
---|
159 | VMMDECL(void) REMNotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
|
---|
160 | {
|
---|
161 | REMHANDLERNOTIFICATION Rec;
|
---|
162 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_DEREGISTER;
|
---|
163 | Rec.u.PhysicalDeregister.enmType = enmType;
|
---|
164 | Rec.u.PhysicalDeregister.GCPhys = GCPhys;
|
---|
165 | Rec.u.PhysicalDeregister.cb = cb;
|
---|
166 | Rec.u.PhysicalDeregister.fHasHCHandler = fHasHCHandler;
|
---|
167 | Rec.u.PhysicalDeregister.fRestoreAsRAM = fRestoreAsRAM;
|
---|
168 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Notification about a successful PGMR3HandlerPhysicalModify() call.
|
---|
174 | *
|
---|
175 | * @param pVM VM Handle.
|
---|
176 | * @param enmType Handler type.
|
---|
177 | * @param GCPhysOld Old handler range address.
|
---|
178 | * @param GCPhysNew New handler range address.
|
---|
179 | * @param cb Size of the handler range.
|
---|
180 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
181 | * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
|
---|
182 | */
|
---|
183 | VMMDECL(void) REMNotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
|
---|
184 | {
|
---|
185 | REMHANDLERNOTIFICATION Rec;
|
---|
186 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_MODIFY;
|
---|
187 | Rec.u.PhysicalModify.enmType = enmType;
|
---|
188 | Rec.u.PhysicalModify.GCPhysOld = GCPhysOld;
|
---|
189 | Rec.u.PhysicalModify.GCPhysNew = GCPhysNew;
|
---|
190 | Rec.u.PhysicalModify.cb = cb;
|
---|
191 | Rec.u.PhysicalModify.fHasHCHandler = fHasHCHandler;
|
---|
192 | Rec.u.PhysicalModify.fRestoreAsRAM = fRestoreAsRAM;
|
---|
193 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
194 | }
|
---|
195 |
|
---|
196 | #endif /* !IN_RING3 */
|
---|
197 |
|
---|
198 | #ifdef IN_RC
|
---|
199 | /**
|
---|
200 | * Flushes the physical handler notifications if the queue is almost full.
|
---|
201 | *
|
---|
202 | * This is for avoiding trouble in RC when changing CR3.
|
---|
203 | *
|
---|
204 | * @param pVM The VM handle.
|
---|
205 | * @param pVCpu The virtual CPU handle of the calling EMT.
|
---|
206 | */
|
---|
207 | VMMDECL(void) REMNotifyHandlerPhysicalFlushIfAlmostFull(PVM pVM, PVMCPU pVCpu)
|
---|
208 | {
|
---|
209 | Assert(pVM->cCpus == 1);
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * Less than 48 items means we should flush.
|
---|
213 | */
|
---|
214 | uint32_t cFree = 0;
|
---|
215 | for (uint32_t idx = pVM->rem.s.idxFreeList;
|
---|
216 | idx != UINT32_MAX;
|
---|
217 | idx = pVM->rem.s.aHandlerNotifications[idx].idxNext)
|
---|
218 | {
|
---|
219 | Assert(idx < RT_ELEMENTS(pVM->rem.s.aHandlerNotifications));
|
---|
220 | if (++cFree >= 48)
|
---|
221 | return;
|
---|
222 | }
|
---|
223 | AssertRelease(VM_FF_ISSET(pVM, VM_FF_REM_HANDLER_NOTIFY));
|
---|
224 | AssertRelease(pVM->rem.s.idxPendingList != UINT32_MAX);
|
---|
225 |
|
---|
226 | /* Ok, we gotta flush them. */
|
---|
227 | VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS, 0);
|
---|
228 |
|
---|
229 | AssertRelease(pVM->rem.s.idxPendingList == UINT32_MAX);
|
---|
230 | AssertRelease(pVM->rem.s.idxFreeList != UINT32_MAX);
|
---|
231 | }
|
---|
232 | #endif /* IN_RC */
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Make REM flush all translation block upon the next call to REMR3State().
|
---|
237 | *
|
---|
238 | * @param pVM Pointer to the shared VM structure.
|
---|
239 | */
|
---|
240 | VMMDECL(void) REMFlushTBs(PVM pVM)
|
---|
241 | {
|
---|
242 | LogFlow(("REMFlushTBs: fFlushTBs=%RTbool fInREM=%RTbool fInStateSync=%RTbool\n",
|
---|
243 | pVM->rem.s.fFlushTBs, pVM->rem.s.fInREM, pVM->rem.s.fInStateSync));
|
---|
244 | pVM->rem.s.fFlushTBs = true;
|
---|
245 | }
|
---|
246 |
|
---|