1 | /* $Id: REMAll.cpp 20869 2009-06-24 00:27:17Z 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 | * Global Variables *
|
---|
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 | * Flushes the handler notifications by calling the host.
|
---|
83 | *
|
---|
84 | * @param pVM The VM handle.
|
---|
85 | */
|
---|
86 | static void remFlushHandlerNotifications(PVM pVM)
|
---|
87 | {
|
---|
88 | #ifdef IN_RC
|
---|
89 | VMMGCCallHost(pVM, VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS, 0);
|
---|
90 | #elif defined(IN_RING0)
|
---|
91 | /** @todo necessary? */
|
---|
92 | VMMR0CallHost(pVM, VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS, 0);
|
---|
93 | #else
|
---|
94 | AssertReleaseMsgFailed(("Ring 3 call????.\n"));
|
---|
95 | #endif
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Insert pending notification
|
---|
101 | *
|
---|
102 | * @param pVM VM Handle.
|
---|
103 | * @param pRec Notification record to insert
|
---|
104 | */
|
---|
105 | static void remNotifyHandlerInsert(PVM pVM, PREMHANDLERNOTIFICATION pRec)
|
---|
106 | {
|
---|
107 | /*
|
---|
108 | * Fetch a free record.
|
---|
109 | */
|
---|
110 | uint32_t cFlushes = 0;
|
---|
111 | uint32_t idxFree;
|
---|
112 | PREMHANDLERNOTIFICATION pFree;
|
---|
113 | do
|
---|
114 | {
|
---|
115 | idxFree = ASMAtomicUoReadU32(&pVM->rem.s.idxFreeList);
|
---|
116 | if (idxFree == (uint32_t)-1)
|
---|
117 | {
|
---|
118 | do
|
---|
119 | {
|
---|
120 | Assert(cFlushes++ != 128);
|
---|
121 | AssertFatal(cFlushes < _1M);
|
---|
122 | remFlushHandlerNotifications(pVM);
|
---|
123 | idxFree = ASMAtomicUoReadU32(&pVM->rem.s.idxFreeList);
|
---|
124 | } while (idxFree == (uint32_t)-1);
|
---|
125 | }
|
---|
126 | pFree = &pVM->rem.s.aHandlerNotifications[idxFree];
|
---|
127 | } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxFreeList, pFree->idxNext, idxFree));
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Copy the record.
|
---|
131 | */
|
---|
132 | pFree->enmKind = pRec->enmKind;
|
---|
133 | pFree->u = pRec->u;
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Insert it into the pending list.
|
---|
137 | */
|
---|
138 | uint32_t idxNext;
|
---|
139 | do
|
---|
140 | {
|
---|
141 | idxNext = ASMAtomicUoReadU32(&pVM->rem.s.idxPendingList);
|
---|
142 | ASMAtomicWriteU32(&pFree->idxNext, idxNext);
|
---|
143 | ASMCompilerBarrier();
|
---|
144 | } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxPendingList, idxFree, idxNext));
|
---|
145 |
|
---|
146 | VM_FF_SET(pVM, VM_FF_REM_HANDLER_NOTIFY);
|
---|
147 |
|
---|
148 | #if 0 /* Enable this to trigger odd flush bugs. */
|
---|
149 | remFlushHandlerNotifications(pVM);
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Notification about a successful PGMR3HandlerPhysicalRegister() call.
|
---|
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 | */
|
---|
163 | VMMDECL(void) REMNotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler)
|
---|
164 | {
|
---|
165 | REMHANDLERNOTIFICATION Rec;
|
---|
166 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_REGISTER;
|
---|
167 | Rec.u.PhysicalRegister.enmType = enmType;
|
---|
168 | Rec.u.PhysicalRegister.GCPhys = GCPhys;
|
---|
169 | Rec.u.PhysicalRegister.cb = cb;
|
---|
170 | Rec.u.PhysicalRegister.fHasHCHandler = fHasHCHandler;
|
---|
171 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
|
---|
177 | *
|
---|
178 | * @param pVM VM Handle.
|
---|
179 | * @param enmType Handler type.
|
---|
180 | * @param GCPhys Handler range address.
|
---|
181 | * @param cb Size of the handler range.
|
---|
182 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
183 | * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
|
---|
184 | */
|
---|
185 | VMMDECL(void) REMNotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
|
---|
186 | {
|
---|
187 | REMHANDLERNOTIFICATION Rec;
|
---|
188 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_DEREGISTER;
|
---|
189 | Rec.u.PhysicalDeregister.enmType = enmType;
|
---|
190 | Rec.u.PhysicalDeregister.GCPhys = GCPhys;
|
---|
191 | Rec.u.PhysicalDeregister.cb = cb;
|
---|
192 | Rec.u.PhysicalDeregister.fHasHCHandler = fHasHCHandler;
|
---|
193 | Rec.u.PhysicalDeregister.fRestoreAsRAM = fRestoreAsRAM;
|
---|
194 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Notification about a successful PGMR3HandlerPhysicalModify() call.
|
---|
200 | *
|
---|
201 | * @param pVM VM Handle.
|
---|
202 | * @param enmType Handler type.
|
---|
203 | * @param GCPhysOld Old handler range address.
|
---|
204 | * @param GCPhysNew New handler range address.
|
---|
205 | * @param cb Size of the handler range.
|
---|
206 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
207 | * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
|
---|
208 | */
|
---|
209 | VMMDECL(void) REMNotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
|
---|
210 | {
|
---|
211 | REMHANDLERNOTIFICATION Rec;
|
---|
212 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_MODIFY;
|
---|
213 | Rec.u.PhysicalModify.enmType = enmType;
|
---|
214 | Rec.u.PhysicalModify.GCPhysOld = GCPhysOld;
|
---|
215 | Rec.u.PhysicalModify.GCPhysNew = GCPhysNew;
|
---|
216 | Rec.u.PhysicalModify.cb = cb;
|
---|
217 | Rec.u.PhysicalModify.fHasHCHandler = fHasHCHandler;
|
---|
218 | Rec.u.PhysicalModify.fRestoreAsRAM = fRestoreAsRAM;
|
---|
219 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
220 | }
|
---|
221 |
|
---|
222 | #endif /* !IN_RING3 */
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Make REM flush all translation block upon the next call to REMR3State().
|
---|
226 | *
|
---|
227 | * @param pVM Pointer to the shared VM structure.
|
---|
228 | */
|
---|
229 | VMMDECL(void) REMFlushTBs(PVM pVM)
|
---|
230 | {
|
---|
231 | LogFlow(("REMFlushTBs: fFlushTBs=%RTbool fInREM=%RTbool fInStateSync=%RTbool\n",
|
---|
232 | pVM->rem.s.fFlushTBs, pVM->rem.s.fInREM, pVM->rem.s.fInStateSync));
|
---|
233 | pVM->rem.s.fFlushTBs = true;
|
---|
234 | }
|
---|
235 |
|
---|