VirtualBox

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

Last change on this file since 102869 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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