VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMRZ/VMMRZ.cpp@ 90829

Last change on this file since 90829 was 90829, checked in by vboxsync, 3 years ago

IPRT,VMM,SUPDrv,++: Reworked the IPRT logger structure and how the VMM ring-0 uses it. bugref:10086

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/* $Id: VMMRZ.cpp 90829 2021-08-24 10:26:07Z 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 */
47VMMRZDECL(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 */
119VMMRZDECL(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 */
131VMMRZDECL(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 /** @todo it might make more sense to just disable logging here, then we
142 * won't flush away important bits... but that goes both ways really. */
143#ifdef IN_RC
144 pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = true;
145#else
146 pVCpu->vmmr0.s.fLogFlushingDisabled = true;
147 if (pVCpu->vmmr0.s.Logger.pLogger)
148 pVCpu->vmmr0.s.Logger.pLogger->u32UserValue1 |= VMMR0_LOGGER_FLAGS_FLUSHING_DISABLED;
149 if (pVCpu->vmmr0.s.RelLogger.pLogger)
150 pVCpu->vmmr0.s.RelLogger.pLogger->u32UserValue1 |= VMMR0_LOGGER_FLAGS_FLUSHING_DISABLED;
151#endif
152 }
153
154#if defined(LOG_ENABLED) && defined(IN_RING0)
155 ASMSetFlags(fFlags);
156#endif
157}
158
159
160/**
161 * Counters VMMRZCallRing3Disable() and re-enables host calls.
162 *
163 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
164 * @thread EMT.
165 */
166VMMRZDECL(void) VMMRZCallRing3Enable(PVMCPUCC pVCpu)
167{
168 VMCPU_ASSERT_EMT(pVCpu);
169#if defined(LOG_ENABLED) && defined(IN_RING0)
170 RTCCUINTREG fFlags = ASMIntDisableFlags(); /* preemption consistency. */
171#endif
172
173 Assert(pVCpu->vmm.s.cCallRing3Disabled > 0);
174 if (ASMAtomicUoDecU32(&pVCpu->vmm.s.cCallRing3Disabled) == 0)
175 {
176#ifdef IN_RC
177 pVCpu->pVMRC->vmm.s.fRCLoggerFlushingDisabled = false;
178#else
179 pVCpu->vmmr0.s.fLogFlushingDisabled = false;
180 if (pVCpu->vmmr0.s.Logger.pLogger)
181 pVCpu->vmmr0.s.Logger.pLogger->u32UserValue1 &= ~VMMR0_LOGGER_FLAGS_FLUSHING_DISABLED;
182 if (pVCpu->vmmr0.s.RelLogger.pLogger)
183 pVCpu->vmmr0.s.RelLogger.pLogger->u32UserValue1 &= ~VMMR0_LOGGER_FLAGS_FLUSHING_DISABLED;
184#endif
185 }
186
187#if defined(LOG_ENABLED) && defined(IN_RING0)
188 ASMSetFlags(fFlags);
189#endif
190}
191
192
193/**
194 * Checks whether its possible to call host context or not.
195 *
196 * @returns true if it's safe, false if it isn't.
197 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
198 */
199VMMRZDECL(bool) VMMRZCallRing3IsEnabled(PVMCPUCC pVCpu)
200{
201 VMCPU_ASSERT_EMT(pVCpu);
202 Assert(pVCpu->vmm.s.cCallRing3Disabled <= 16);
203 return pVCpu->vmm.s.cCallRing3Disabled == 0;
204}
205
206
207/**
208 * Sets the ring-0 callback before doing the ring-3 call.
209 *
210 * @param pVCpu The cross context virtual CPU structure.
211 * @param pfnCallback Pointer to the callback.
212 * @param pvUser The user argument.
213 *
214 * @return VBox status code.
215 */
216VMMRZDECL(int) VMMRZCallRing3SetNotification(PVMCPUCC pVCpu, R0PTRTYPE(PFNVMMR0CALLRING3NOTIFICATION) pfnCallback, RTR0PTR pvUser)
217{
218 AssertPtrReturn(pVCpu, VERR_INVALID_POINTER);
219 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
220
221 if (pVCpu->vmm.s.pfnCallRing3CallbackR0)
222 return VERR_ALREADY_EXISTS;
223
224 pVCpu->vmm.s.pfnCallRing3CallbackR0 = pfnCallback;
225 pVCpu->vmm.s.pvCallRing3CallbackUserR0 = pvUser;
226 return VINF_SUCCESS;
227}
228
229
230/**
231 * Removes the ring-0 callback.
232 *
233 * @param pVCpu The cross context virtual CPU structure.
234 */
235VMMRZDECL(void) VMMRZCallRing3RemoveNotification(PVMCPUCC pVCpu)
236{
237 pVCpu->vmm.s.pfnCallRing3CallbackR0 = NULL;
238}
239
240
241/**
242 * Checks whether there is a ring-0 callback notification active.
243 *
244 * @param pVCpu The cross context virtual CPU structure.
245 * @returns true if there the notification is active, false otherwise.
246 */
247VMMRZDECL(bool) VMMRZCallRing3IsNotificationSet(PVMCPUCC pVCpu)
248{
249 return pVCpu->vmm.s.pfnCallRing3CallbackR0 != NULL;
250}
251
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette