VirtualBox

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

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

scm --update-copyright-year

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