1 | /* $Id: VMMRZ.cpp 35346 2010-12-27 16:13:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - Virtual Machine Monitor, Raw-mode and ring-0 context code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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 The VM handle.
|
---|
41 | * @param pVCpu The virtual CPU handle 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 | /*
|
---|
56 | * In most cases, it's sufficient to return a status code which
|
---|
57 | * will then be propagated up the code usually encountering several
|
---|
58 | * AssertRC invocations along the way. Hitting one of those is more
|
---|
59 | * helpful than stopping here.
|
---|
60 | *
|
---|
61 | * However, some doesn't check the status code because they are called
|
---|
62 | * from void functions, and for these we'll turn this into a ring-0
|
---|
63 | * assertion host call.
|
---|
64 | */
|
---|
65 | if (enmOperation != VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS)
|
---|
66 | return VERR_VMM_RING3_CALL_DISABLED;
|
---|
67 | #ifdef IN_RC
|
---|
68 | RTStrPrintf(g_szRTAssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
69 | "VMMRZCallRing3: enmOperation=%d uArg=%#llx idCpu=%#x\n", enmOperation, uArg, pVCpu->idCpu);
|
---|
70 | #endif
|
---|
71 | RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
72 | "VMMRZCallRing3: enmOperation=%d uArg=%#llx idCpu=%#x\n", enmOperation, uArg, pVCpu->idCpu);
|
---|
73 | enmOperation = VMMCALLRING3_VM_R0_ASSERTION;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * The normal path.
|
---|
78 | */
|
---|
79 | /** @todo profile this! */
|
---|
80 | pVCpu->vmm.s.enmCallRing3Operation = enmOperation;
|
---|
81 | pVCpu->vmm.s.u64CallRing3Arg = uArg;
|
---|
82 | pVCpu->vmm.s.rcCallRing3 = VERR_INTERNAL_ERROR;
|
---|
83 | #ifdef IN_RC
|
---|
84 | pVM->vmm.s.pfnGuestToHostRC(VINF_VMM_CALL_HOST);
|
---|
85 | #else
|
---|
86 | int rc = vmmR0CallRing3LongJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, VINF_VMM_CALL_HOST);
|
---|
87 | if (RT_FAILURE(rc))
|
---|
88 | return rc;
|
---|
89 | #endif
|
---|
90 | return pVCpu->vmm.s.rcCallRing3;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Simple wrapper that adds the pVCpu argument.
|
---|
96 | *
|
---|
97 | * @returns VBox status code of the ring-3 call.
|
---|
98 | * @retval VERR_VMM_RING3_CALL_DISABLED if called at the wrong time. This must
|
---|
99 | * be passed up the stack, or if that isn't possible then VMMRZCallRing3
|
---|
100 | * needs to change it into an assertion.
|
---|
101 | *
|
---|
102 | * @param pVM The VM handle.
|
---|
103 | * @param enmOperation The operation.
|
---|
104 | * @param uArg The argument to the operation.
|
---|
105 | */
|
---|
106 | VMMRZDECL(int) VMMRZCallRing3NoCpu(PVM pVM, VMMCALLRING3 enmOperation, uint64_t uArg)
|
---|
107 | {
|
---|
108 | return VMMRZCallRing3(pVM, VMMGetCpu(pVM), enmOperation, uArg);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Disables all host calls, except certain fatal ones.
|
---|
114 | *
|
---|
115 | * @param pVCpu The CPU struct for the calling EMT.
|
---|
116 | * @thread EMT.
|
---|
117 | */
|
---|
118 | VMMRZDECL(void) VMMRZCallRing3Disable(PVMCPU pVCpu)
|
---|
119 | {
|
---|
120 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
121 | Assert(pVCpu->vmm.s.cCallRing3Disabled < 16);
|
---|
122 | if (++pVCpu->vmm.s.cCallRing3Disabled == 1)
|
---|
123 | {
|
---|
124 | /** @todo it might make more sense to just disable logging here, then we
|
---|
125 | * won't flush away important bits... but that goes both ways really. */
|
---|
126 | #ifdef IN_RC
|
---|
127 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = true;
|
---|
128 | #else
|
---|
129 | # ifdef LOG_ENABLED
|
---|
130 | if (pVCpu->vmm.s.pR0LoggerR0)
|
---|
131 | pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
|
---|
132 | # endif
|
---|
133 | #endif
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Counters VMMRZCallRing3Disable and re-enables host calls.
|
---|
140 | *
|
---|
141 | * @param pVCpu The CPU struct for the calling EMT.
|
---|
142 | * @thread EMT.
|
---|
143 | */
|
---|
144 | VMMRZDECL(void) VMMRZCallRing3Enable(PVMCPU pVCpu)
|
---|
145 | {
|
---|
146 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
147 | Assert(pVCpu->vmm.s.cCallRing3Disabled > 0);
|
---|
148 | if (--pVCpu->vmm.s.cCallRing3Disabled == 0)
|
---|
149 | {
|
---|
150 | #ifdef IN_RC
|
---|
151 | pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = false;
|
---|
152 | #else
|
---|
153 | # ifdef LOG_ENABLED
|
---|
154 | if (pVCpu->vmm.s.pR0LoggerR0)
|
---|
155 | pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
|
---|
156 | # endif
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Checks whether its possible to call host context or not.
|
---|
164 | *
|
---|
165 | * @returns true if it's safe, false if it isn't.
|
---|
166 | * @param pVCpu The CPU struct for the calling EMT.
|
---|
167 | */
|
---|
168 | VMMRZDECL(bool) VMMRZCallRing3IsEnabled(PVMCPU pVCpu)
|
---|
169 | {
|
---|
170 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
171 | Assert(pVCpu->vmm.s.cCallRing3Disabled <= 16);
|
---|
172 | return pVCpu->vmm.s.cCallRing3Disabled == 0;
|
---|
173 | }
|
---|
174 |
|
---|