1 | /* $Id: VMMRZ.cpp 80333 2019-08-16 20:28:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - Virtual Machine Monitor, Raw-mode and ring-0 context code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 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_VMM
|
---|
23 | #include <VBox/vmm/vmm.h>
|
---|
24 | #include "VMMInternal.h"
|
---|
25 | #include <VBox/vmm/vmcc.h>
|
---|
26 | #include <VBox/err.h>
|
---|
27 |
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/asm-amd64-x86.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Calls the ring-3 host code.
|
---|
35 | *
|
---|
36 | * @returns VBox status code of the ring-3 call.
|
---|
37 | * @retval VERR_VMM_RING3_CALL_DISABLED if called at the wrong time. This must
|
---|
38 | * be passed up the stack, or if that isn't possible then VMMRZCallRing3
|
---|
39 | * needs to change it into an assertion.
|
---|
40 | *
|
---|
41 | *
|
---|
42 | * @param pVM The cross context VM structure.
|
---|
43 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
44 | * @param enmOperation The operation.
|
---|
45 | * @param uArg The argument to the operation.
|
---|
46 | */
|
---|
47 | VMMRZDECL(int) VMMRZCallRing3(PVMCC pVM, PVMCPUCC pVCpu, VMMCALLRING3 enmOperation, uint64_t uArg)
|
---|
48 | {
|
---|
49 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * Check if calling ring-3 has been disabled and only let let fatal calls thru.
|
---|
53 | */
|
---|
54 | if (RT_UNLIKELY( pVCpu->vmm.s.cCallRing3Disabled != 0
|
---|
55 | && enmOperation != VMMCALLRING3_VM_R0_ASSERTION))
|
---|
56 | {
|
---|
57 | #ifndef IN_RING0
|
---|
58 | /*
|
---|
59 | * In most cases, it's sufficient to return a status code which
|
---|
60 | * will then be propagated up the code usually encountering several
|
---|
61 | * AssertRC invocations along the way. Hitting one of those is more
|
---|
62 | * helpful than stopping here.
|
---|
63 | *
|
---|
64 | * However, some doesn't check the status code because they are called
|
---|
65 | * from void functions, and for these we'll turn this into a ring-0
|
---|
66 | * assertion host call.
|
---|
67 | */
|
---|
68 | if (enmOperation != VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS)
|
---|
69 | return VERR_VMM_RING3_CALL_DISABLED;
|
---|
70 | #endif
|
---|
71 | #ifdef IN_RC
|
---|
72 | RTStrPrintf(g_szRTAssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
73 | "VMMRZCallRing3: enmOperation=%d uArg=%#llx idCpu=%#x cCallRing3Disabled=%#x\n",
|
---|
74 | enmOperation, uArg, pVCpu->idCpu, pVCpu->vmm.s.cCallRing3Disabled);
|
---|
75 | #endif
|
---|
76 | RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
77 | "VMMRZCallRing3: enmOperation=%d uArg=%#llx idCpu=%#x cCallRing3Disabled=%#x\n",
|
---|
78 | enmOperation, uArg, pVCpu->idCpu, pVCpu->vmm.s.cCallRing3Disabled);
|
---|
79 | enmOperation = VMMCALLRING3_VM_R0_ASSERTION;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * The normal path.
|
---|
84 | */
|
---|
85 | /** @todo profile this! */
|
---|
86 | pVCpu->vmm.s.enmCallRing3Operation = enmOperation;
|
---|
87 | pVCpu->vmm.s.u64CallRing3Arg = uArg;
|
---|
88 | pVCpu->vmm.s.rcCallRing3 = VERR_VMM_RING3_CALL_NO_RC;
|
---|
89 | #ifdef IN_RC
|
---|
90 | pVM->vmm.s.pfnRCToHost(VINF_VMM_CALL_HOST);
|
---|
91 | #else
|
---|
92 | int rc;
|
---|
93 | if (pVCpu->vmm.s.pfnCallRing3CallbackR0)
|
---|
94 | {
|
---|
95 | rc = pVCpu->vmm.s.pfnCallRing3CallbackR0(pVCpu, enmOperation, pVCpu->vmm.s.pvCallRing3CallbackUserR0);
|
---|
96 | if (RT_FAILURE(rc))
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 | rc = vmmR0CallRing3LongJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, VINF_VMM_CALL_HOST);
|
---|
100 | if (RT_FAILURE(rc))
|
---|
101 | return rc;
|
---|
102 | #endif
|
---|
103 | return pVCpu->vmm.s.rcCallRing3;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Simple wrapper that adds the pVCpu argument.
|
---|
109 | *
|
---|
110 | * @returns VBox status code of the ring-3 call.
|
---|
111 | * @retval VERR_VMM_RING3_CALL_DISABLED if called at the wrong time. This must
|
---|
112 | * be passed up the stack, or if that isn't possible then VMMRZCallRing3
|
---|
113 | * needs to change it into an assertion.
|
---|
114 | *
|
---|
115 | * @param pVM The cross context VM structure.
|
---|
116 | * @param enmOperation The operation.
|
---|
117 | * @param uArg The argument to the operation.
|
---|
118 | */
|
---|
119 | VMMRZDECL(int) VMMRZCallRing3NoCpu(PVMCC pVM, VMMCALLRING3 enmOperation, uint64_t uArg)
|
---|
120 | {
|
---|
121 | return VMMRZCallRing3(pVM, VMMGetCpu(pVM), enmOperation, uArg);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Disables all host calls, except certain fatal ones.
|
---|
127 | *
|
---|
128 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
129 | * @thread EMT.
|
---|
130 | */
|
---|
131 | VMMRZDECL(void) VMMRZCallRing3Disable(PVMCPUCC pVCpu)
|
---|
132 | {
|
---|
133 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
134 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
135 | RTCCUINTREG fFlags = ASMIntDisableFlags(); /* preemption consistency. */
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | Assert(pVCpu->vmm.s.cCallRing3Disabled < 16);
|
---|
139 | if (ASMAtomicUoIncU32(&pVCpu->vmm.s.cCallRing3Disabled) == 1)
|
---|
140 | {
|
---|
141 | /** @todo it might make more sense to just disable logging here, then we
|
---|
142 | * won't flush away important bits... but that goes both ways really. */
|
---|
143 | #ifdef IN_RC
|
---|
144 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = true;
|
---|
145 | #else
|
---|
146 | # ifdef LOG_ENABLED
|
---|
147 | if (pVCpu->vmm.s.pR0LoggerR0)
|
---|
148 | pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
|
---|
149 | # endif
|
---|
150 | if (pVCpu->vmm.s.pR0RelLoggerR0)
|
---|
151 | pVCpu->vmm.s.pR0RelLoggerR0->fFlushingDisabled = true;
|
---|
152 | #endif
|
---|
153 | }
|
---|
154 |
|
---|
155 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
156 | ASMSetFlags(fFlags);
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Counters VMMRZCallRing3Disable() and re-enables host calls.
|
---|
163 | *
|
---|
164 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
165 | * @thread EMT.
|
---|
166 | */
|
---|
167 | VMMRZDECL(void) VMMRZCallRing3Enable(PVMCPUCC pVCpu)
|
---|
168 | {
|
---|
169 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
170 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
171 | RTCCUINTREG fFlags = ASMIntDisableFlags(); /* preemption consistency. */
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | Assert(pVCpu->vmm.s.cCallRing3Disabled > 0);
|
---|
175 | if (ASMAtomicUoDecU32(&pVCpu->vmm.s.cCallRing3Disabled) == 0)
|
---|
176 | {
|
---|
177 | #ifdef IN_RC
|
---|
178 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = false;
|
---|
179 | #else
|
---|
180 | # ifdef LOG_ENABLED
|
---|
181 | if (pVCpu->vmm.s.pR0LoggerR0)
|
---|
182 | pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
|
---|
183 | # endif
|
---|
184 | if (pVCpu->vmm.s.pR0RelLoggerR0)
|
---|
185 | pVCpu->vmm.s.pR0RelLoggerR0->fFlushingDisabled = false;
|
---|
186 | #endif
|
---|
187 | }
|
---|
188 |
|
---|
189 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
190 | ASMSetFlags(fFlags);
|
---|
191 | #endif
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Checks whether its possible to call host context or not.
|
---|
197 | *
|
---|
198 | * @returns true if it's safe, false if it isn't.
|
---|
199 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
200 | */
|
---|
201 | VMMRZDECL(bool) VMMRZCallRing3IsEnabled(PVMCPUCC pVCpu)
|
---|
202 | {
|
---|
203 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
204 | Assert(pVCpu->vmm.s.cCallRing3Disabled <= 16);
|
---|
205 | return pVCpu->vmm.s.cCallRing3Disabled == 0;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Sets the ring-0 callback before doing the ring-3 call.
|
---|
211 | *
|
---|
212 | * @param pVCpu The cross context virtual CPU structure.
|
---|
213 | * @param pfnCallback Pointer to the callback.
|
---|
214 | * @param pvUser The user argument.
|
---|
215 | *
|
---|
216 | * @return VBox status code.
|
---|
217 | */
|
---|
218 | VMMRZDECL(int) VMMRZCallRing3SetNotification(PVMCPUCC pVCpu, R0PTRTYPE(PFNVMMR0CALLRING3NOTIFICATION) pfnCallback, RTR0PTR pvUser)
|
---|
219 | {
|
---|
220 | AssertPtrReturn(pVCpu, VERR_INVALID_POINTER);
|
---|
221 | AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
|
---|
222 |
|
---|
223 | if (pVCpu->vmm.s.pfnCallRing3CallbackR0)
|
---|
224 | return VERR_ALREADY_EXISTS;
|
---|
225 |
|
---|
226 | pVCpu->vmm.s.pfnCallRing3CallbackR0 = pfnCallback;
|
---|
227 | pVCpu->vmm.s.pvCallRing3CallbackUserR0 = pvUser;
|
---|
228 | return VINF_SUCCESS;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Removes the ring-0 callback.
|
---|
234 | *
|
---|
235 | * @param pVCpu The cross context virtual CPU structure.
|
---|
236 | */
|
---|
237 | VMMRZDECL(void) VMMRZCallRing3RemoveNotification(PVMCPUCC pVCpu)
|
---|
238 | {
|
---|
239 | pVCpu->vmm.s.pfnCallRing3CallbackR0 = NULL;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Checks whether there is a ring-0 callback notification active.
|
---|
245 | *
|
---|
246 | * @param pVCpu The cross context virtual CPU structure.
|
---|
247 | * @returns true if there the notification is active, false otherwise.
|
---|
248 | */
|
---|
249 | VMMRZDECL(bool) VMMRZCallRing3IsNotificationSet(PVMCPUCC pVCpu)
|
---|
250 | {
|
---|
251 | return pVCpu->vmm.s.pfnCallRing3CallbackR0 != NULL;
|
---|
252 | }
|
---|
253 |
|
---|