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