1 | /* $Id: GVMMR3.cpp 108968 2025-04-14 20:45: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 | #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 | */
|
---|
56 | VMMR3_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 | {
|
---|
98 | /* Set up guard pages: */
|
---|
99 | RTMemProtect(pVM, cbPageSize, RTMEM_PROT_NONE);
|
---|
100 | pVM = (PVM)((uintptr_t)pVM + cbPageSize);
|
---|
101 | RTMemProtect(pVM + 1, cbPageSize, RTMEM_PROT_NONE);
|
---|
102 |
|
---|
103 | /* VM: */
|
---|
104 | pVM->enmVMState = VMSTATE_CREATING;
|
---|
105 | pVM->pVMR3 = pVM;
|
---|
106 | pVM->hSelf = _1M;
|
---|
107 | pVM->pSession = pSession;
|
---|
108 | pVM->cCpus = cCpus;
|
---|
109 | pVM->uCpuExecutionCap = 100;
|
---|
110 | pVM->cbSelf = sizeof(VM);
|
---|
111 | pVM->cbVCpu = sizeof(VMCPU);
|
---|
112 | pVM->uStructVersion = VM_STRUCT_VERSION;
|
---|
113 | pVM->enmTarget = enmTarget;
|
---|
114 |
|
---|
115 | /* CPUs: */
|
---|
116 | PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(VM) + cbPageSize);
|
---|
117 | for (VMCPUID idxCpu = 0; idxCpu < cCpus; idxCpu++)
|
---|
118 | {
|
---|
119 | pVM->apCpusR3[idxCpu] = pVCpu;
|
---|
120 |
|
---|
121 | pVCpu->enmState = VMCPUSTATE_STOPPED;
|
---|
122 | pVCpu->pVMR3 = pVM;
|
---|
123 | pVCpu->hNativeThread = NIL_RTNATIVETHREAD;
|
---|
124 | pVCpu->hNativeThreadR0 = NIL_RTNATIVETHREAD;
|
---|
125 | pVCpu->hThread = NIL_RTTHREAD;
|
---|
126 | pVCpu->idCpu = idxCpu;
|
---|
127 |
|
---|
128 | RTMemProtect(pVCpu + 1, cbPageSize, RTMEM_PROT_NONE);
|
---|
129 | pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(VMCPU) + cbPageSize);
|
---|
130 | }
|
---|
131 |
|
---|
132 | *ppVM = pVM;
|
---|
133 | *ppVMR0 = NIL_RTR0PTR;
|
---|
134 | rc = VINF_SUCCESS;
|
---|
135 | }
|
---|
136 | else
|
---|
137 | rc = VERR_NO_PAGE_MEMORY;
|
---|
138 | }
|
---|
139 | RT_NOREF(pUVM);
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Driverless: VMMR0_DO_GVMM_DESTROY_VM
|
---|
146 | *
|
---|
147 | * @returns VBox status code.
|
---|
148 | * @param pUVM The user mode VM handle.
|
---|
149 | * @param pVM The cross context VM structure.
|
---|
150 | */
|
---|
151 | VMMR3_INT_DECL(int) GVMMR3DestroyVM(PUVM pUVM, PVM pVM)
|
---|
152 | {
|
---|
153 | AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
|
---|
154 | Assert(pUVM->cCpus == pVM->cCpus);
|
---|
155 | RT_NOREF(pUVM);
|
---|
156 |
|
---|
157 | int rc;
|
---|
158 | if (!SUPR3IsDriverless())
|
---|
159 | rc = SUPR3CallVMMR0Ex(pVM->pVMR0ForCall, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
|
---|
160 | else
|
---|
161 | {
|
---|
162 | RTMemPageFree((uint8_t *)pVM - HOST_PAGE_SIZE_DYNAMIC,
|
---|
163 | sizeof(VM) + sizeof(VMCPU) * pVM->cCpus + HOST_PAGE_SIZE_DYNAMIC * (1 + 2 * pVM->cCpus));
|
---|
164 | rc = VINF_SUCCESS;
|
---|
165 | }
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Register the calling EMT with GVM.
|
---|
172 | *
|
---|
173 | * @returns VBox status code.
|
---|
174 | * @param pVM The cross context VM structure.
|
---|
175 | * @param idCpu The Virtual CPU ID.
|
---|
176 | * @thread EMT(idCpu)
|
---|
177 | * @see GVMMR0RegisterVCpu
|
---|
178 | */
|
---|
179 | VMMR3_INT_DECL(int) GVMMR3RegisterVCpu(PVM pVM, VMCPUID idCpu)
|
---|
180 | {
|
---|
181 | Assert(VMMGetCpuId(pVM) == idCpu);
|
---|
182 | int rc;
|
---|
183 | if (!SUPR3IsDriverless())
|
---|
184 | {
|
---|
185 | rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
|
---|
186 | if (RT_FAILURE(rc))
|
---|
187 | LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
|
---|
188 | }
|
---|
189 | else
|
---|
190 | rc = VINF_SUCCESS;
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Deregister the calling EMT from GVM.
|
---|
197 | *
|
---|
198 | * @returns VBox status code.
|
---|
199 | * @param pVM The cross context VM structure.
|
---|
200 | * @param idCpu The Virtual CPU ID.
|
---|
201 | * @thread EMT(idCpu)
|
---|
202 | * @see GVMMR0DeregisterVCpu
|
---|
203 | */
|
---|
204 | VMMR3_INT_DECL(int) GVMMR3DeregisterVCpu(PVM pVM, VMCPUID idCpu)
|
---|
205 | {
|
---|
206 | Assert(VMMGetCpuId(pVM) == idCpu);
|
---|
207 | int rc;
|
---|
208 | if (!SUPR3IsDriverless())
|
---|
209 | rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_DEREGISTER_VMCPU, 0, NULL);
|
---|
210 | else
|
---|
211 | rc = VINF_SUCCESS;
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * @see GVMMR0RegisterWorkerThread
|
---|
218 | */
|
---|
219 | VMMR3_INT_DECL(int) GVMMR3RegisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
|
---|
220 | {
|
---|
221 | if (SUPR3IsDriverless())
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | GVMMREGISTERWORKERTHREADREQ Req;
|
---|
224 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
225 | Req.Hdr.cbReq = sizeof(Req);
|
---|
226 | Req.hNativeThreadR3 = RTThreadNativeSelf();
|
---|
227 | return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
|
---|
228 | VMMR0_DO_GVMM_REGISTER_WORKER_THREAD, (unsigned)enmWorker, &Req.Hdr);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * @see GVMMR0DeregisterWorkerThread
|
---|
234 | */
|
---|
235 | VMMR3_INT_DECL(int) GVMMR3DeregisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
|
---|
236 | {
|
---|
237 | if (SUPR3IsDriverless())
|
---|
238 | return VINF_SUCCESS;
|
---|
239 | return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
|
---|
240 | VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD, (unsigned)enmWorker, NULL);
|
---|
241 | }
|
---|
242 |
|
---|