VirtualBox

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

最後變更 在這個檔案從92721是 92721,由 vboxsync 提交於 3 年 前

VMM: Made driverless adjustments to the halt and cleanup code. bugref:10138

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.7 KB
 
1/* $Id: GVMMR3.cpp 92721 2021-12-02 22:42:04Z vboxsync $ */
2/** @file
3 * GVMM - Global VM Manager, ring-3 request wrappers.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 pReq The create VM request.
39 */
40VMMR3_INT_DECL(int) GVMMR3CreateVM(PUVM pUVM, uint32_t cCpus, PSUPDRVSESSION pSession, PVM *ppVM, PRTR0PTR ppVMR0)
41{
42 AssertReturn(cCpus >= VMM_MIN_CPU_COUNT && cCpus <= VMM_MAX_CPU_COUNT, VERR_INVALID_PARAMETER);
43 AssertCompile((sizeof(VM) & PAGE_OFFSET_MASK) == 0);
44 AssertCompile((sizeof(VMCPU) & PAGE_OFFSET_MASK) == 0);
45
46 int rc;
47 if (!SUPR3IsDriverless())
48 {
49 GVMMCREATEVMREQ CreateVMReq;
50 CreateVMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
51 CreateVMReq.Hdr.cbReq = sizeof(CreateVMReq);
52 CreateVMReq.pSession = pSession;
53 CreateVMReq.pVMR0 = NIL_RTR0PTR;
54 CreateVMReq.pVMR3 = NULL;
55 CreateVMReq.cCpus = cCpus;
56 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
57 if (RT_SUCCESS(rc))
58 {
59 *ppVM = CreateVMReq.pVMR3;
60 *ppVMR0 = CreateVMReq.pVMR0;
61 }
62 }
63 else
64 {
65 /*
66 * Driverless.
67 */
68 /* Allocate the VM structure: */
69 size_t const cbVM = sizeof(VM) + sizeof(VMCPU) * cCpus;
70 PVM pVM = (PVM)RTMemPageAlloc(cbVM + PAGE_SIZE * (1 + 2 * cCpus));
71 if (!pVM)
72 return VERR_NO_PAGE_MEMORY;
73
74 /* Set up guard pages: */
75 RTMemProtect(pVM, PAGE_SIZE, RTMEM_PROT_NONE);
76 pVM = (PVM)((uintptr_t)pVM + PAGE_SIZE);
77 RTMemProtect(pVM + 1, PAGE_SIZE, RTMEM_PROT_NONE);
78
79 /* VM: */
80 pVM->enmVMState = VMSTATE_CREATING;
81 pVM->pVMR3 = pVM;
82 pVM->hSelf = _1M;
83 pVM->pSession = pSession;
84 pVM->cCpus = cCpus;
85 pVM->uCpuExecutionCap = 100;
86 pVM->cbSelf = sizeof(VM);
87 pVM->cbVCpu = sizeof(VMCPU);
88 pVM->uStructVersion = 1;
89
90 /* CPUs: */
91 PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(VM) + PAGE_SIZE);
92 for (VMCPUID idxCpu = 0; idxCpu < cCpus; idxCpu++)
93 {
94 pVM->apCpusR3[idxCpu] = pVCpu;
95
96 pVCpu->enmState = VMCPUSTATE_STOPPED;
97 pVCpu->pVMR3 = pVM;
98 pVCpu->hNativeThread = NIL_RTNATIVETHREAD;
99 pVCpu->hNativeThreadR0 = NIL_RTNATIVETHREAD;
100 pVCpu->hThread = NIL_RTTHREAD;
101 pVCpu->idCpu = idxCpu;
102
103 RTMemProtect(pVCpu + 1, PAGE_SIZE, RTMEM_PROT_NONE);
104 pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(VMCPU) + PAGE_SIZE);
105 }
106
107 *ppVM = pVM;
108 *ppVMR0 = NIL_RTR0PTR;
109 }
110 RT_NOREF(pUVM);
111 return VINF_SUCCESS;
112}
113
114
115/**
116 * Driverless: VMMR0_DO_GVMM_DESTROY_VM
117 *
118 * @returns VBox status code.
119 * @param pUVM The user mode VM handle.
120 * @param pVM The cross context VM structure.
121 */
122VMMR3_INT_DECL(int) GVMMR3DestroyVM(PUVM pUVM, PVM pVM)
123{
124 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
125 Assert(pUVM->cCpus == pVM->cCpus);
126 RT_NOREF(pUVM);
127
128 int rc;
129 if (!SUPR3IsDriverless())
130 rc = SUPR3CallVMMR0Ex(pVM->pVMR0ForCall, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
131 else
132 {
133 RTMemPageFree((uint8_t *)pVM - PAGE_SIZE, sizeof(VM) + sizeof(VMCPU) * pVM->cCpus + PAGE_SIZE * (1 + 2 * pVM->cCpus));
134 rc = VINF_SUCCESS;
135 }
136 return rc;
137}
138
139
140/**
141 * Register the calling EMT with GVM.
142 *
143 * @returns VBox status code.
144 * @param pVM The cross context VM structure.
145 * @param idCpu The Virtual CPU ID.
146 * @thread EMT(idCpu)
147 * @see GVMMR0RegisterVCpu
148 */
149VMMR3_INT_DECL(int) GVMMR3RegisterVCpu(PVM pVM, VMCPUID idCpu)
150{
151 Assert(VMMGetCpuId(pVM) == idCpu);
152 int rc;
153 if (!SUPR3IsDriverless())
154 {
155 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
156 if (RT_FAILURE(rc))
157 LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
158 }
159 else
160 rc = VINF_SUCCESS;
161 return rc;
162}
163
164
165/**
166 * Deregister the calling EMT from 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 GVMMR0DeregisterVCpu
173 */
174VMMR3_INT_DECL(int) GVMMR3DeregisterVCpu(PVM pVM, VMCPUID idCpu)
175{
176 Assert(VMMGetCpuId(pVM) == idCpu);
177 int rc;
178 if (!SUPR3IsDriverless())
179 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_DEREGISTER_VMCPU, 0, NULL);
180 else
181 rc = VINF_SUCCESS;
182 return rc;
183}
184
185
186/**
187 * @see GVMMR0RegisterWorkerThread
188 */
189VMMR3_INT_DECL(int) GVMMR3RegisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
190{
191 if (SUPR3IsDriverless())
192 return VINF_SUCCESS;
193 GVMMREGISTERWORKERTHREADREQ Req;
194 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
195 Req.Hdr.cbReq = sizeof(Req);
196 Req.hNativeThreadR3 = RTThreadNativeSelf();
197 return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
198 VMMR0_DO_GVMM_REGISTER_WORKER_THREAD, (unsigned)enmWorker, &Req.Hdr);
199}
200
201
202/**
203 * @see GVMMR0DeregisterWorkerThread
204 */
205VMMR3_INT_DECL(int) GVMMR3DeregisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
206{
207 if (SUPR3IsDriverless())
208 return VINF_SUCCESS;
209 return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
210 VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD, (unsigned)enmWorker, NULL);
211}
212
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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