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