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