1 | /* $Id: VMMRZ.cpp 20875 2009-06-24 02:29:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - Virtual Machine Monitor, Raw-mode and ring-0 context code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <VBox/vmm.h>
|
---|
27 | #include "VMMInternal.h"
|
---|
28 | #include <VBox/vm.h>
|
---|
29 | #include <VBox/err.h>
|
---|
30 |
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Calls the ring-3 host code.
|
---|
37 | *
|
---|
38 | * @returns VBox status code of the ring-3 call.
|
---|
39 | * @retval VERR_VMM_RING3_CALL_DISABLED if called at the wrong time. This must
|
---|
40 | * be passed up the stack, or if that isn't possible then VMMRZCallRing3
|
---|
41 | * needs to change it into an assertion.
|
---|
42 | *
|
---|
43 | *
|
---|
44 | * @param pVM The VM handle.
|
---|
45 | * @param pVCpu The virtual CPU handle of the calling EMT.
|
---|
46 | * @param enmOperation The operation.
|
---|
47 | * @param uArg The argument to the operation.
|
---|
48 | */
|
---|
49 | VMMRZDECL(int) VMMRZCallRing3(PVM pVM, PVMCPU pVCpu, VMMCALLRING3 enmOperation, uint64_t uArg)
|
---|
50 | {
|
---|
51 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Check if calling ring-3 has been disabled and only let let fatal calls thru.
|
---|
55 | */
|
---|
56 | if (RT_UNLIKELY( pVCpu->vmm.s.cCallRing3Disabled != 0
|
---|
57 | && enmOperation != VMMCALLRING3_VM_R0_ASSERTION))
|
---|
58 | {
|
---|
59 | /*
|
---|
60 | * In most cases, it's sufficient to return a status code which
|
---|
61 | * will then be propagated up the code usually encountering several
|
---|
62 | * AssertRC invocations along the way. Hitting one of those is more
|
---|
63 | * helpful than stopping here.
|
---|
64 | *
|
---|
65 | * However, some doesn't check the status code because they are called
|
---|
66 | * from void functions, and for these we'll turn this into a ring-0
|
---|
67 | * assertion host call.
|
---|
68 | */
|
---|
69 | if (enmOperation != VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS)
|
---|
70 | return VERR_VMM_RING3_CALL_DISABLED;
|
---|
71 | #ifdef IN_RC
|
---|
72 | RTStrPrintf(g_szRTAssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
73 | "VMMRZCallRing3: enmOperation=%d uArg=%#llx idCpu=%#x\n", enmOperation, uArg, pVCpu->idCpu);
|
---|
74 | #endif
|
---|
75 | RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
76 | "VMMRZCallRing3: enmOperation=%d uArg=%#llx idCpu=%#x\n", enmOperation, uArg, pVCpu->idCpu);
|
---|
77 | enmOperation = VMMCALLRING3_VM_R0_ASSERTION;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * The normal path.
|
---|
82 | */
|
---|
83 | /** @todo profile this! */
|
---|
84 | pVCpu->vmm.s.enmCallRing3Operation = enmOperation;
|
---|
85 | pVCpu->vmm.s.u64CallRing3Arg = uArg;
|
---|
86 | pVCpu->vmm.s.rcCallRing3 = VERR_INTERNAL_ERROR;
|
---|
87 | #ifdef IN_RC
|
---|
88 | pVM->vmm.s.pfnGuestToHostRC(VINF_VMM_CALL_HOST);
|
---|
89 | #else
|
---|
90 | int rc = vmmR0CallRing3LongJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, VINF_VMM_CALL_HOST);
|
---|
91 | if (RT_FAILURE(rc))
|
---|
92 | return rc;
|
---|
93 | #endif
|
---|
94 | return pVCpu->vmm.s.rcCallRing3;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Simple wrapper that adds the pVCpu argument.
|
---|
100 | *
|
---|
101 | * @returns VBox status code of the ring-3 call.
|
---|
102 | * @retval VERR_VMM_RING3_CALL_DISABLED if called at the wrong time. This must
|
---|
103 | * be passed up the stack, or if that isn't possible then VMMRZCallRing3
|
---|
104 | * needs to change it into an assertion.
|
---|
105 | *
|
---|
106 | * @param pVM The VM handle.
|
---|
107 | * @param pVCpu The virtual CPU handle of the calling EMT.
|
---|
108 | * @param enmOperation The operation.
|
---|
109 | * @param uArg The argument to the operation.
|
---|
110 | */
|
---|
111 | VMMRZDECL(int) VMMRZCallRing3NoCpu(PVM pVM, VMMCALLRING3 enmOperation, uint64_t uArg)
|
---|
112 | {
|
---|
113 | return VMMRZCallRing3(pVM, VMMGetCpu(pVM), enmOperation, uArg);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Disables all host calls, except certain fatal ones.
|
---|
119 | *
|
---|
120 | * @param pVCpu The CPU struct for the calling EMT.
|
---|
121 | * @thread EMT.
|
---|
122 | */
|
---|
123 | VMMRZDECL(void) VMMRZCallRing3Disable(PVMCPU pVCpu)
|
---|
124 | {
|
---|
125 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
126 | Assert(pVCpu->vmm.s.cCallRing3Disabled < 16);
|
---|
127 | if (++pVCpu->vmm.s.cCallRing3Disabled == 1)
|
---|
128 | {
|
---|
129 | /** @todo it might make more sense to just disable logging here, then we
|
---|
130 | * won't flush away important bits... but that goes both ways really. */
|
---|
131 | #ifdef IN_RC
|
---|
132 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = true;
|
---|
133 | #else
|
---|
134 | # ifdef LOG_ENABLED
|
---|
135 | if (pVCpu->vmm.s.pR0LoggerR0)
|
---|
136 | pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
|
---|
137 | # endif
|
---|
138 | #endif
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Counters VMMRZCallRing3Disable and re-enables host calls.
|
---|
145 | *
|
---|
146 | * @param pVCpu The CPU struct for the calling EMT.
|
---|
147 | * @thread EMT.
|
---|
148 | */
|
---|
149 | VMMRZDECL(void) VMMRZCallRing3Enable(PVMCPU pVCpu)
|
---|
150 | {
|
---|
151 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
152 | Assert(pVCpu->vmm.s.cCallRing3Disabled > 0);
|
---|
153 | if (--pVCpu->vmm.s.cCallRing3Disabled == 0)
|
---|
154 | {
|
---|
155 | #ifdef IN_RC
|
---|
156 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = false;
|
---|
157 | #else
|
---|
158 | # ifdef LOG_ENABLED
|
---|
159 | if (pVCpu->vmm.s.pR0LoggerR0)
|
---|
160 | pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
|
---|
161 | # endif
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Checks whether its possible to call host context or not.
|
---|
169 | *
|
---|
170 | * @returns true if it's safe, false if it isn't.
|
---|
171 | * @param pVCpu The CPU struct for the calling EMT.
|
---|
172 | */
|
---|
173 | VMMRZDECL(bool) VMMRZCallRing3IsEnabled(PVMCPU pVCpu)
|
---|
174 | {
|
---|
175 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
176 | Assert(pVCpu->vmm.s.cCallRing3Disabled <= 16);
|
---|
177 | return pVCpu->vmm.s.cCallRing3Disabled == 0;
|
---|
178 | }
|
---|
179 |
|
---|