1 | /* $Id: REMAll.cpp 20431 2009-06-09 11:52:48Z 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 | if ( pVM->rem.s.cInvalidatedPages < RT_ELEMENTS(pVM->rem.s.aGCPtrInvalidatedPages)
|
---|
50 | && EMTryEnterRemLock(pVM) == VINF_SUCCESS) /* if this fails, then we'll just flush the tlb as we don't want to waste time here. */
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * We sync them back in REMR3State.
|
---|
54 | */
|
---|
55 | pVM->rem.s.aGCPtrInvalidatedPages[pVM->rem.s.cInvalidatedPages++] = GCPtrPage;
|
---|
56 | EMRemUnlock(pVM);
|
---|
57 | }
|
---|
58 | else
|
---|
59 | {
|
---|
60 | /* Tell the recompiler to flush its TLB. */
|
---|
61 | CPUMSetChangedFlags(VMMGetCpu(pVM), CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
62 | pVM->rem.s.cInvalidatedPages = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | return VINF_SUCCESS;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Flushes the handler notifications by calling the host.
|
---|
71 | *
|
---|
72 | * @param pVM The VM handle.
|
---|
73 | */
|
---|
74 | static void remFlushHandlerNotifications(PVM pVM)
|
---|
75 | {
|
---|
76 | #ifdef IN_RC
|
---|
77 | VMMGCCallHost(pVM, VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS, 0);
|
---|
78 | #elif defined(IN_RING0)
|
---|
79 | /** @todo necessary? */
|
---|
80 | VMMR0CallHost(pVM, VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS, 0);
|
---|
81 | #else
|
---|
82 | AssertReleaseMsgFailed(("Ring 3 call????.\n"));
|
---|
83 | #endif
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Insert pending notification
|
---|
89 | *
|
---|
90 | * @param pVM VM Handle.
|
---|
91 | * @param pRec Notification record to insert
|
---|
92 | */
|
---|
93 | static void remNotifyHandlerInsert(PVM pVM, PREMHANDLERNOTIFICATION pRec)
|
---|
94 | {
|
---|
95 | uint32_t idxFree;
|
---|
96 | uint32_t idxNext;
|
---|
97 | PREMHANDLERNOTIFICATION pFree;
|
---|
98 |
|
---|
99 | /* Fetch a free record. */
|
---|
100 | do
|
---|
101 | {
|
---|
102 | idxFree = pVM->rem.s.idxFreeList;
|
---|
103 | if (idxFree == (uint32_t)-1)
|
---|
104 | {
|
---|
105 | pFree = NULL;
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | pFree = &pVM->rem.s.aHandlerNotifications[idxFree];
|
---|
109 | } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxFreeList, pFree->idxNext, idxFree));
|
---|
110 |
|
---|
111 | if (!pFree)
|
---|
112 | {
|
---|
113 | remFlushHandlerNotifications(pVM);
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* Copy the record. */
|
---|
118 | *pFree = *pRec;
|
---|
119 | pFree->idxSelf = idxFree; /* was trashed */
|
---|
120 |
|
---|
121 | /* Insert it into the pending list. */
|
---|
122 | do
|
---|
123 | {
|
---|
124 | idxNext = pVM->rem.s.idxPendingList;
|
---|
125 | pFree->idxNext = idxNext;
|
---|
126 | } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxPendingList, idxFree, idxNext));
|
---|
127 |
|
---|
128 | VM_FF_SET(pVM, VM_FF_REM_HANDLER_NOTIFY);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Notification about a successful PGMR3HandlerPhysicalRegister() call.
|
---|
133 | *
|
---|
134 | * @param pVM VM Handle.
|
---|
135 | * @param enmType Handler type.
|
---|
136 | * @param GCPhys Handler range address.
|
---|
137 | * @param cb Size of the handler range.
|
---|
138 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
139 | */
|
---|
140 | VMMDECL(void) REMNotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler)
|
---|
141 | {
|
---|
142 | REMHANDLERNOTIFICATION Rec;
|
---|
143 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_REGISTER;
|
---|
144 | Rec.u.PhysicalRegister.enmType = enmType;
|
---|
145 | Rec.u.PhysicalRegister.GCPhys = GCPhys;
|
---|
146 | Rec.u.PhysicalRegister.cb = cb;
|
---|
147 | Rec.u.PhysicalRegister.fHasHCHandler = fHasHCHandler;
|
---|
148 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
|
---|
154 | *
|
---|
155 | * @param pVM VM Handle.
|
---|
156 | * @param enmType Handler type.
|
---|
157 | * @param GCPhys Handler range address.
|
---|
158 | * @param cb Size of the handler range.
|
---|
159 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
160 | * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
|
---|
161 | */
|
---|
162 | VMMDECL(void) REMNotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
|
---|
163 | {
|
---|
164 | REMHANDLERNOTIFICATION Rec;
|
---|
165 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_DEREGISTER;
|
---|
166 | Rec.u.PhysicalDeregister.enmType = enmType;
|
---|
167 | Rec.u.PhysicalDeregister.GCPhys = GCPhys;
|
---|
168 | Rec.u.PhysicalDeregister.cb = cb;
|
---|
169 | Rec.u.PhysicalDeregister.fHasHCHandler = fHasHCHandler;
|
---|
170 | Rec.u.PhysicalDeregister.fRestoreAsRAM = fRestoreAsRAM;
|
---|
171 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Notification about a successful PGMR3HandlerPhysicalModify() call.
|
---|
177 | *
|
---|
178 | * @param pVM VM Handle.
|
---|
179 | * @param enmType Handler type.
|
---|
180 | * @param GCPhysOld Old handler range address.
|
---|
181 | * @param GCPhysNew New handler range address.
|
---|
182 | * @param cb Size of the handler range.
|
---|
183 | * @param fHasHCHandler Set if the handler have a HC callback function.
|
---|
184 | * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
|
---|
185 | */
|
---|
186 | VMMDECL(void) REMNotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
|
---|
187 | {
|
---|
188 | REMHANDLERNOTIFICATION Rec;
|
---|
189 | Rec.enmKind = REMHANDLERNOTIFICATIONKIND_PHYSICAL_MODIFY;
|
---|
190 | Rec.u.PhysicalModify.enmType = enmType;
|
---|
191 | Rec.u.PhysicalModify.GCPhysOld = GCPhysOld;
|
---|
192 | Rec.u.PhysicalModify.GCPhysNew = GCPhysNew;
|
---|
193 | Rec.u.PhysicalModify.cb = cb;
|
---|
194 | Rec.u.PhysicalModify.fHasHCHandler = fHasHCHandler;
|
---|
195 | Rec.u.PhysicalModify.fRestoreAsRAM = fRestoreAsRAM;
|
---|
196 | remNotifyHandlerInsert(pVM, &Rec);
|
---|
197 | }
|
---|
198 |
|
---|
199 | #endif /* !IN_RING3 */
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Make REM flush all translation block upon the next call to REMR3State().
|
---|
203 | *
|
---|
204 | * @param pVM Pointer to the shared VM structure.
|
---|
205 | */
|
---|
206 | VMMDECL(void) REMFlushTBs(PVM pVM)
|
---|
207 | {
|
---|
208 | LogFlow(("REMFlushTBs: fFlushTBs=%RTbool fInREM=%RTbool fInStateSync=%RTbool\n",
|
---|
209 | pVM->rem.s.fFlushTBs, pVM->rem.s.fInREM, pVM->rem.s.fInStateSync));
|
---|
210 | pVM->rem.s.fFlushTBs = true;
|
---|
211 | }
|
---|
212 |
|
---|