VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GVMMR3.cpp@ 108472

Last change on this file since 108472 was 107900, checked in by vboxsync, 3 months ago

VMM: Define VM_STRUCT_VERSION (for use inside the VMM) with a few VMM_COMMON_DEFS defines baked into the value. jiraref:1470

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: GVMMR3.cpp 107900 2025-01-22 20:04:36Z vboxsync $ */
2/** @file
3 * GVMM - Global VM Manager, ring-3 request wrappers.
4 */
5
6/*
7 * Copyright (C) 2021-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_GVMM
33#include <VBox/vmm/gvmm.h>
34#include <VBox/vmm/vmm.h>
35#include <VBox/vmm/vmcc.h>
36#include <VBox/vmm/uvm.h>
37#include <VBox/sup.h>
38#include <VBox/err.h>
39
40#include <iprt/mem.h>
41
42
43/**
44 * Driverless: VMMR0_DO_GVMM_CREATE_VM
45 *
46 * @returns VBox status code.
47 * @param pUVM The user mode VM handle.
48 * @param enmTarget The target platform architecture of the VM.
49 * @param cCpus The number of CPUs to create the VM for.
50 * @param pSession The support driver session handle.
51 * @param ppVM Where to return the pointer to the VM structure.
52 * @param ppVMR0 Where to return the ring-0 address of the VM structure
53 * for use in VMMR0 calls.
54 */
55VMMR3_INT_DECL(int) GVMMR3CreateVM(PUVM pUVM, VMTARGET enmTarget, uint32_t cCpus, PSUPDRVSESSION pSession,
56 PVM *ppVM, PRTR0PTR ppVMR0)
57{
58 AssertReturn(cCpus >= VMM_MIN_CPU_COUNT && cCpus <= VMM_MAX_CPU_COUNT, VERR_INVALID_PARAMETER);
59 AssertReturn(enmTarget == VMTARGET_X86 || enmTarget == VMTARGET_ARMV8, VERR_INVALID_PARAMETER);
60 AssertCompile((sizeof(VM) & HOST_PAGE_OFFSET_MASK) == 0);
61 AssertCompile((sizeof(VMCPU) & HOST_PAGE_OFFSET_MASK) == 0);
62
63 int rc;
64 if (!SUPR3IsDriverless())
65 {
66 GVMMCREATEVMREQ CreateVMReq;
67 CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
68 CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
69 CreateVMReq.pSession = pSession;
70 CreateVMReq.enmTarget = enmTarget;
71 CreateVMReq.cCpus = cCpus;
72 CreateVMReq.cbVM = sizeof(VM);
73 CreateVMReq.cbVCpu = sizeof(VMCPU);
74 CreateVMReq.uStructVersion = VM_STRUCT_VERSION;
75 CreateVMReq.uSvnRevision = VMMGetSvnRev();
76 CreateVMReq.pVMR0 = NIL_RTR0PTR;
77 CreateVMReq.pVMR3 = NULL;
78 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
79 if (RT_SUCCESS(rc))
80 {
81 *ppVM = CreateVMReq.pVMR3;
82 *ppVMR0 = CreateVMReq.pVMR0;
83 Assert(CreateVMReq.pVMR3->enmTarget == enmTarget);
84 }
85 }
86 else
87 {
88 /*
89 * Driverless.
90 */
91 /* Allocate the VM structure: */
92 size_t const cbVM = sizeof(VM) + sizeof(VMCPU) * cCpus;
93 PVM pVM = (PVM)RTMemPageAlloc(cbVM + HOST_PAGE_SIZE * (1 + 2 * cCpus));
94 if (!pVM)
95 return VERR_NO_PAGE_MEMORY;
96
97 /* Set up guard pages: */
98 RTMemProtect(pVM, HOST_PAGE_SIZE, RTMEM_PROT_NONE);
99 pVM = (PVM)((uintptr_t)pVM + HOST_PAGE_SIZE);
100 RTMemProtect(pVM + 1, HOST_PAGE_SIZE, RTMEM_PROT_NONE);
101
102 /* VM: */
103 pVM->enmVMState = VMSTATE_CREATING;
104 pVM->pVMR3 = pVM;
105 pVM->hSelf = _1M;
106 pVM->pSession = pSession;
107 pVM->cCpus = cCpus;
108 pVM->uCpuExecutionCap = 100;
109 pVM->cbSelf = sizeof(VM);
110 pVM->cbVCpu = sizeof(VMCPU);
111 pVM->uStructVersion = VM_STRUCT_VERSION;
112 pVM->enmTarget = enmTarget;
113
114 /* CPUs: */
115 PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(VM) + HOST_PAGE_SIZE);
116 for (VMCPUID idxCpu = 0; idxCpu < cCpus; idxCpu++)
117 {
118 pVM->apCpusR3[idxCpu] = pVCpu;
119
120 pVCpu->enmState = VMCPUSTATE_STOPPED;
121 pVCpu->pVMR3 = pVM;
122 pVCpu->hNativeThread = NIL_RTNATIVETHREAD;
123 pVCpu->hNativeThreadR0 = NIL_RTNATIVETHREAD;
124 pVCpu->hThread = NIL_RTTHREAD;
125 pVCpu->idCpu = idxCpu;
126
127 RTMemProtect(pVCpu + 1, HOST_PAGE_SIZE, RTMEM_PROT_NONE);
128 pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(VMCPU) + HOST_PAGE_SIZE);
129 }
130
131 *ppVM = pVM;
132 *ppVMR0 = NIL_RTR0PTR;
133 }
134 RT_NOREF(pUVM);
135 return VINF_SUCCESS;
136}
137
138
139/**
140 * Driverless: VMMR0_DO_GVMM_DESTROY_VM
141 *
142 * @returns VBox status code.
143 * @param pUVM The user mode VM handle.
144 * @param pVM The cross context VM structure.
145 */
146VMMR3_INT_DECL(int) GVMMR3DestroyVM(PUVM pUVM, PVM pVM)
147{
148 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
149 Assert(pUVM->cCpus == pVM->cCpus);
150 RT_NOREF(pUVM);
151
152 int rc;
153 if (!SUPR3IsDriverless())
154 rc = SUPR3CallVMMR0Ex(pVM->pVMR0ForCall, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
155 else
156 {
157 RTMemPageFree((uint8_t *)pVM - HOST_PAGE_SIZE,
158 sizeof(VM) + sizeof(VMCPU) * pVM->cCpus + HOST_PAGE_SIZE * (1 + 2 * pVM->cCpus));
159 rc = VINF_SUCCESS;
160 }
161 return rc;
162}
163
164
165/**
166 * Register the calling EMT with GVM.
167 *
168 * @returns VBox status code.
169 * @param pVM The cross context VM structure.
170 * @param idCpu The Virtual CPU ID.
171 * @thread EMT(idCpu)
172 * @see GVMMR0RegisterVCpu
173 */
174VMMR3_INT_DECL(int) GVMMR3RegisterVCpu(PVM pVM, VMCPUID idCpu)
175{
176 Assert(VMMGetCpuId(pVM) == idCpu);
177 int rc;
178 if (!SUPR3IsDriverless())
179 {
180 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
181 if (RT_FAILURE(rc))
182 LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
183 }
184 else
185 rc = VINF_SUCCESS;
186 return rc;
187}
188
189
190/**
191 * Deregister the calling EMT from GVM.
192 *
193 * @returns VBox status code.
194 * @param pVM The cross context VM structure.
195 * @param idCpu The Virtual CPU ID.
196 * @thread EMT(idCpu)
197 * @see GVMMR0DeregisterVCpu
198 */
199VMMR3_INT_DECL(int) GVMMR3DeregisterVCpu(PVM pVM, VMCPUID idCpu)
200{
201 Assert(VMMGetCpuId(pVM) == idCpu);
202 int rc;
203 if (!SUPR3IsDriverless())
204 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_DEREGISTER_VMCPU, 0, NULL);
205 else
206 rc = VINF_SUCCESS;
207 return rc;
208}
209
210
211/**
212 * @see GVMMR0RegisterWorkerThread
213 */
214VMMR3_INT_DECL(int) GVMMR3RegisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
215{
216 if (SUPR3IsDriverless())
217 return VINF_SUCCESS;
218 GVMMREGISTERWORKERTHREADREQ Req;
219 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
220 Req.Hdr.cbReq = sizeof(Req);
221 Req.hNativeThreadR3 = RTThreadNativeSelf();
222 return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
223 VMMR0_DO_GVMM_REGISTER_WORKER_THREAD, (unsigned)enmWorker, &Req.Hdr);
224}
225
226
227/**
228 * @see GVMMR0DeregisterWorkerThread
229 */
230VMMR3_INT_DECL(int) GVMMR3DeregisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
231{
232 if (SUPR3IsDriverless())
233 return VINF_SUCCESS;
234 return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
235 VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD, (unsigned)enmWorker, NULL);
236}
237
Note: See TracBrowser for help on using the repository browser.

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