1 | /* $Id: GVMMR3.cpp 92703 2021-12-02 12:45:58Z 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 | */
|
---|
40 | VMMR3_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++, pVCpu++)
|
---|
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 | */
|
---|
121 | VMMR3_INT_DECL(int) GVMMR3DestroyVM(PUVM pUVM)
|
---|
122 | {
|
---|
123 | PVM pVM = pUVM->pVM;
|
---|
124 | AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
|
---|
125 | Assert(pUVM->cCpus == pVM->cCpus);
|
---|
126 |
|
---|
127 | int rc;
|
---|
128 | if (!SUPR3IsDriverless())
|
---|
129 | rc = SUPR3CallVMMR0Ex(pVM->pVMR0ForCall, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
|
---|
130 | else
|
---|
131 | {
|
---|
132 | RTMemPageFree((uint8_t *)pVM - PAGE_SIZE, sizeof(VM) + sizeof(VMCPU) * pVM->cCpus + PAGE_SIZE * (1 + 2 * pVM->cCpus));
|
---|
133 | rc = VINF_SUCCESS;
|
---|
134 | }
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Register the calling EMT with GVM.
|
---|
141 | *
|
---|
142 | * @returns VBox status code.
|
---|
143 | * @param pVM The cross context VM structure.
|
---|
144 | * @param idCpu The Virtual CPU ID.
|
---|
145 | * @thread EMT(idCpu)
|
---|
146 | * @see GVMMR0RegisterVCpu
|
---|
147 | */
|
---|
148 | VMMR3_INT_DECL(int) GVMMR3RegisterVCpu(PVM pVM, VMCPUID idCpu)
|
---|
149 | {
|
---|
150 | Assert(VMMGetCpuId(pVM) == idCpu);
|
---|
151 | int rc;
|
---|
152 | if (!SUPR3IsDriverless())
|
---|
153 | {
|
---|
154 | rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
|
---|
155 | if (RT_FAILURE(rc))
|
---|
156 | LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
|
---|
157 | }
|
---|
158 | else
|
---|
159 | rc = VINF_SUCCESS;
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * @see GVMMR0RegisterWorkerThread
|
---|
166 | */
|
---|
167 | VMMR3_INT_DECL(int) GVMMR3RegisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
|
---|
168 | {
|
---|
169 | if (SUPR3IsDriverless())
|
---|
170 | return VINF_SUCCESS;
|
---|
171 | GVMMREGISTERWORKERTHREADREQ Req;
|
---|
172 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
173 | Req.Hdr.cbReq = sizeof(Req);
|
---|
174 | Req.hNativeThreadR3 = RTThreadNativeSelf();
|
---|
175 | return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
|
---|
176 | VMMR0_DO_GVMM_REGISTER_WORKER_THREAD, (unsigned)enmWorker, &Req.Hdr);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * @see GVMMR0DeregisterWorkerThread
|
---|
182 | */
|
---|
183 | VMMR3_INT_DECL(int) GVMMR3DeregisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
|
---|
184 | {
|
---|
185 | if (SUPR3IsDriverless())
|
---|
186 | return VINF_SUCCESS;
|
---|
187 | return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
|
---|
188 | VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD, (unsigned)enmWorker, NULL);
|
---|
189 | }
|
---|
190 |
|
---|