VirtualBox

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

Last change on this file since 108708 was 108708, checked in by vboxsync, 6 weeks ago

VMM: Introduce VMM_HOST_PAGE_SIZE_DYNAMIC, HOST_PAGE_SIZE_DYNAMIC, and HOST_PAGE_SHIFT_DYNAMIC, bugref:10391

HOST_PAGE_SIZE_DYNAMIC either resolves to HOST_PAGE_SIZE or calls RTSystemGetPageSize() on hosts where
the system page size is not known during build time (linux.arm64 for now).
HOST_PAGE_SHIFT_DYNAMIC is the same for the page shift.

This allows building VMM libraries which are agnostic to the host page size (at the cost of a slightly
larger overhead).

Currently enabled only on linux.arm64

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: GVMMR3.cpp 108708 2025-03-24 09:16:43Z 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#include <iprt/system.h>
42
43
44/**
45 * Driverless: VMMR0_DO_GVMM_CREATE_VM
46 *
47 * @returns VBox status code.
48 * @param pUVM The user mode VM handle.
49 * @param enmTarget The target platform architecture of the VM.
50 * @param cCpus The number of CPUs to create the VM for.
51 * @param pSession The support driver session handle.
52 * @param ppVM Where to return the pointer to the VM structure.
53 * @param ppVMR0 Where to return the ring-0 address of the VM structure
54 * for use in VMMR0 calls.
55 */
56VMMR3_INT_DECL(int) GVMMR3CreateVM(PUVM pUVM, VMTARGET enmTarget, uint32_t cCpus, PSUPDRVSESSION pSession,
57 PVM *ppVM, PRTR0PTR ppVMR0)
58{
59 AssertReturn(cCpus >= VMM_MIN_CPU_COUNT && cCpus <= VMM_MAX_CPU_COUNT, VERR_INVALID_PARAMETER);
60 AssertReturn(enmTarget == VMTARGET_X86 || enmTarget == VMTARGET_ARMV8, VERR_INVALID_PARAMETER);
61 AssertReturn((sizeof(VM) & RTSystemGetPageOffsetMask()) == 0, VERR_UNSUPPORTED_ALIGNMENT);
62 AssertReturn((sizeof(VMCPU) & RTSystemGetPageOffsetMask()) == 0, VERR_UNSUPPORTED_ALIGNMENT);
63
64 int rc;
65 if (!SUPR3IsDriverless())
66 {
67 GVMMCREATEVMREQ CreateVMReq;
68 CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
69 CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
70 CreateVMReq.pSession = pSession;
71 CreateVMReq.enmTarget = enmTarget;
72 CreateVMReq.cCpus = cCpus;
73 CreateVMReq.cbVM = sizeof(VM);
74 CreateVMReq.cbVCpu = sizeof(VMCPU);
75 CreateVMReq.uStructVersion = VM_STRUCT_VERSION;
76 CreateVMReq.uSvnRevision = VMMGetSvnRev();
77 CreateVMReq.pVMR0 = NIL_RTR0PTR;
78 CreateVMReq.pVMR3 = NULL;
79 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
80 if (RT_SUCCESS(rc))
81 {
82 *ppVM = CreateVMReq.pVMR3;
83 *ppVMR0 = CreateVMReq.pVMR0;
84 Assert(CreateVMReq.pVMR3->enmTarget == enmTarget);
85 }
86 }
87 else
88 {
89 /*
90 * Driverless.
91 */
92 /* Allocate the VM structure: */
93 size_t const cbPageSize = RTSystemGetPageSize();
94 size_t const cbVM = sizeof(VM) + sizeof(VMCPU) * cCpus;
95 PVM pVM = (PVM)RTMemPageAlloc(cbVM + cbPageSize * (1 + 2 * cCpus));
96 if (!pVM)
97 return VERR_NO_PAGE_MEMORY;
98
99 /* Set up guard pages: */
100 RTMemProtect(pVM, cbPageSize, RTMEM_PROT_NONE);
101 pVM = (PVM)((uintptr_t)pVM + cbPageSize);
102 RTMemProtect(pVM + 1, cbPageSize, RTMEM_PROT_NONE);
103
104 /* VM: */
105 pVM->enmVMState = VMSTATE_CREATING;
106 pVM->pVMR3 = pVM;
107 pVM->hSelf = _1M;
108 pVM->pSession = pSession;
109 pVM->cCpus = cCpus;
110 pVM->uCpuExecutionCap = 100;
111 pVM->cbSelf = sizeof(VM);
112 pVM->cbVCpu = sizeof(VMCPU);
113 pVM->uStructVersion = VM_STRUCT_VERSION;
114 pVM->enmTarget = enmTarget;
115
116 /* CPUs: */
117 PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(VM) + cbPageSize);
118 for (VMCPUID idxCpu = 0; idxCpu < cCpus; idxCpu++)
119 {
120 pVM->apCpusR3[idxCpu] = pVCpu;
121
122 pVCpu->enmState = VMCPUSTATE_STOPPED;
123 pVCpu->pVMR3 = pVM;
124 pVCpu->hNativeThread = NIL_RTNATIVETHREAD;
125 pVCpu->hNativeThreadR0 = NIL_RTNATIVETHREAD;
126 pVCpu->hThread = NIL_RTTHREAD;
127 pVCpu->idCpu = idxCpu;
128
129 RTMemProtect(pVCpu + 1, cbPageSize, RTMEM_PROT_NONE);
130 pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(VMCPU) + cbPageSize);
131 }
132
133 *ppVM = pVM;
134 *ppVMR0 = NIL_RTR0PTR;
135 }
136 RT_NOREF(pUVM);
137 return VINF_SUCCESS;
138}
139
140
141/**
142 * Driverless: VMMR0_DO_GVMM_DESTROY_VM
143 *
144 * @returns VBox status code.
145 * @param pUVM The user mode VM handle.
146 * @param pVM The cross context VM structure.
147 */
148VMMR3_INT_DECL(int) GVMMR3DestroyVM(PUVM pUVM, PVM pVM)
149{
150 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
151 Assert(pUVM->cCpus == pVM->cCpus);
152 RT_NOREF(pUVM);
153
154 int rc;
155 if (!SUPR3IsDriverless())
156 rc = SUPR3CallVMMR0Ex(pVM->pVMR0ForCall, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
157 else
158 {
159 RTMemPageFree((uint8_t *)pVM - HOST_PAGE_SIZE_DYNAMIC,
160 sizeof(VM) + sizeof(VMCPU) * pVM->cCpus + HOST_PAGE_SIZE_DYNAMIC * (1 + 2 * pVM->cCpus));
161 rc = VINF_SUCCESS;
162 }
163 return rc;
164}
165
166
167/**
168 * Register the calling EMT with GVM.
169 *
170 * @returns VBox status code.
171 * @param pVM The cross context VM structure.
172 * @param idCpu The Virtual CPU ID.
173 * @thread EMT(idCpu)
174 * @see GVMMR0RegisterVCpu
175 */
176VMMR3_INT_DECL(int) GVMMR3RegisterVCpu(PVM pVM, VMCPUID idCpu)
177{
178 Assert(VMMGetCpuId(pVM) == idCpu);
179 int rc;
180 if (!SUPR3IsDriverless())
181 {
182 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
183 if (RT_FAILURE(rc))
184 LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
185 }
186 else
187 rc = VINF_SUCCESS;
188 return rc;
189}
190
191
192/**
193 * Deregister the calling EMT from GVM.
194 *
195 * @returns VBox status code.
196 * @param pVM The cross context VM structure.
197 * @param idCpu The Virtual CPU ID.
198 * @thread EMT(idCpu)
199 * @see GVMMR0DeregisterVCpu
200 */
201VMMR3_INT_DECL(int) GVMMR3DeregisterVCpu(PVM pVM, VMCPUID idCpu)
202{
203 Assert(VMMGetCpuId(pVM) == idCpu);
204 int rc;
205 if (!SUPR3IsDriverless())
206 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_DEREGISTER_VMCPU, 0, NULL);
207 else
208 rc = VINF_SUCCESS;
209 return rc;
210}
211
212
213/**
214 * @see GVMMR0RegisterWorkerThread
215 */
216VMMR3_INT_DECL(int) GVMMR3RegisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
217{
218 if (SUPR3IsDriverless())
219 return VINF_SUCCESS;
220 GVMMREGISTERWORKERTHREADREQ Req;
221 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
222 Req.Hdr.cbReq = sizeof(Req);
223 Req.hNativeThreadR3 = RTThreadNativeSelf();
224 return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
225 VMMR0_DO_GVMM_REGISTER_WORKER_THREAD, (unsigned)enmWorker, &Req.Hdr);
226}
227
228
229/**
230 * @see GVMMR0DeregisterWorkerThread
231 */
232VMMR3_INT_DECL(int) GVMMR3DeregisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
233{
234 if (SUPR3IsDriverless())
235 return VINF_SUCCESS;
236 return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
237 VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD, (unsigned)enmWorker, NULL);
238}
239
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