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