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