1 | /* $Id: VMMRZ.cpp 90953 2021-08-27 12:45:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - Virtual Machine Monitor, Raw-mode and ring-0 context code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2020 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 | #ifdef IN_RC
|
---|
142 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = true;
|
---|
143 | #else
|
---|
144 | pVCpu->vmmr0.s.fLogFlushingDisabled = true;
|
---|
145 | #endif
|
---|
146 | }
|
---|
147 |
|
---|
148 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
149 | ASMSetFlags(fFlags);
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Counters VMMRZCallRing3Disable() and re-enables host calls.
|
---|
156 | *
|
---|
157 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
158 | * @thread EMT.
|
---|
159 | */
|
---|
160 | VMMRZDECL(void) VMMRZCallRing3Enable(PVMCPUCC pVCpu)
|
---|
161 | {
|
---|
162 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
163 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
164 | RTCCUINTREG fFlags = ASMIntDisableFlags(); /* preemption consistency. */
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | Assert(pVCpu->vmm.s.cCallRing3Disabled > 0);
|
---|
168 | if (ASMAtomicUoDecU32(&pVCpu->vmm.s.cCallRing3Disabled) == 0)
|
---|
169 | {
|
---|
170 | #ifdef IN_RC
|
---|
171 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = false;
|
---|
172 | #else
|
---|
173 | pVCpu->vmmr0.s.fLogFlushingDisabled = false;
|
---|
174 | #endif
|
---|
175 | }
|
---|
176 |
|
---|
177 | #if defined(LOG_ENABLED) && defined(IN_RING0)
|
---|
178 | ASMSetFlags(fFlags);
|
---|
179 | #endif
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Checks whether its possible to call host context or not.
|
---|
185 | *
|
---|
186 | * @returns true if it's safe, false if it isn't.
|
---|
187 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
188 | */
|
---|
189 | VMMRZDECL(bool) VMMRZCallRing3IsEnabled(PVMCPUCC pVCpu)
|
---|
190 | {
|
---|
191 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
192 | Assert(pVCpu->vmm.s.cCallRing3Disabled <= 16);
|
---|
193 | return pVCpu->vmm.s.cCallRing3Disabled == 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Sets the ring-0 callback before doing the ring-3 call.
|
---|
199 | *
|
---|
200 | * @param pVCpu The cross context virtual CPU structure.
|
---|
201 | * @param pfnCallback Pointer to the callback.
|
---|
202 | * @param pvUser The user argument.
|
---|
203 | *
|
---|
204 | * @return VBox status code.
|
---|
205 | */
|
---|
206 | VMMRZDECL(int) VMMRZCallRing3SetNotification(PVMCPUCC pVCpu, R0PTRTYPE(PFNVMMR0CALLRING3NOTIFICATION) pfnCallback, RTR0PTR pvUser)
|
---|
207 | {
|
---|
208 | AssertPtrReturn(pVCpu, VERR_INVALID_POINTER);
|
---|
209 | AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
|
---|
210 |
|
---|
211 | if (pVCpu->vmm.s.pfnCallRing3CallbackR0)
|
---|
212 | return VERR_ALREADY_EXISTS;
|
---|
213 |
|
---|
214 | pVCpu->vmm.s.pfnCallRing3CallbackR0 = pfnCallback;
|
---|
215 | pVCpu->vmm.s.pvCallRing3CallbackUserR0 = pvUser;
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Removes the ring-0 callback.
|
---|
222 | *
|
---|
223 | * @param pVCpu The cross context virtual CPU structure.
|
---|
224 | */
|
---|
225 | VMMRZDECL(void) VMMRZCallRing3RemoveNotification(PVMCPUCC pVCpu)
|
---|
226 | {
|
---|
227 | pVCpu->vmm.s.pfnCallRing3CallbackR0 = NULL;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Checks whether there is a ring-0 callback notification active.
|
---|
233 | *
|
---|
234 | * @param pVCpu The cross context virtual CPU structure.
|
---|
235 | * @returns true if there the notification is active, false otherwise.
|
---|
236 | */
|
---|
237 | VMMRZDECL(bool) VMMRZCallRing3IsNotificationSet(PVMCPUCC pVCpu)
|
---|
238 | {
|
---|
239 | return pVCpu->vmm.s.pfnCallRing3CallbackR0 != NULL;
|
---|
240 | }
|
---|
241 |
|
---|