summaryrefslogtreecommitdiffstats
path: root/src/VBox/VMM/VMMR3/GVMMR3.cpp
blob: 81d72493c5d2c2c5b40c806acb33ca460a42b1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* $Id: GVMMR3.cpp $ */
/** @file
 * GVMM - Global VM Manager, ring-3 request wrappers.
 */

/*
 * Copyright (C) 2021-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_GVMM
#include <VBox/vmm/gvmm.h>
#include <VBox/vmm/vmm.h>
#include <VBox/vmm/vmcc.h>
#include <VBox/vmm/uvm.h>
#include <VBox/sup.h>
#include <VBox/err.h>

#include <iprt/mem.h>


/**
 * Driverless: VMMR0_DO_GVMM_CREATE_VM
 *
 * @returns VBox status code.
 * @param   pUVM        The user mode VM handle.
 * @param   cCpus       The number of CPUs to create the VM for.
 * @param   pSession    The support driver session handle.
 * @param   ppVM        Where to return the pointer to the VM structure.
 * @param   ppVMR0      Where to return the ring-0 address of the VM structure
 *                      for use in VMMR0 calls.
 */
VMMR3_INT_DECL(int) GVMMR3CreateVM(PUVM pUVM, uint32_t cCpus, PSUPDRVSESSION pSession, PVM *ppVM, PRTR0PTR ppVMR0)
{
    AssertReturn(cCpus >= VMM_MIN_CPU_COUNT && cCpus <= VMM_MAX_CPU_COUNT, VERR_INVALID_PARAMETER);
    AssertCompile((sizeof(VM)    & HOST_PAGE_OFFSET_MASK) == 0);
    AssertCompile((sizeof(VMCPU) & HOST_PAGE_OFFSET_MASK) == 0);

    int rc;
    if (!SUPR3IsDriverless())
    {
        GVMMCREATEVMREQ CreateVMReq;
        CreateVMReq.Hdr.u32Magic    = SUPVMMR0REQHDR_MAGIC;
        CreateVMReq.Hdr.cbReq       = sizeof(CreateVMReq);
        CreateVMReq.pSession        = pSession;
        CreateVMReq.pVMR0           = NIL_RTR0PTR;
        CreateVMReq.pVMR3           = NULL;
        CreateVMReq.cCpus           = cCpus;
        rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_GVMM_CREATE_VM, 0, &CreateVMReq.Hdr);
        if (RT_SUCCESS(rc))
        {
            *ppVM   = CreateVMReq.pVMR3;
            *ppVMR0 = CreateVMReq.pVMR0;
        }
    }
    else
    {
        /*
         * Driverless.
         */
        /* Allocate the VM structure: */
        size_t const cbVM = sizeof(VM) + sizeof(VMCPU) * cCpus;
        PVM          pVM  = (PVM)RTMemPageAlloc(cbVM + HOST_PAGE_SIZE * (1 + 2 * cCpus));
        if (!pVM)
            return VERR_NO_PAGE_MEMORY;

        /* Set up guard pages: */
        RTMemProtect(pVM, HOST_PAGE_SIZE, RTMEM_PROT_NONE);
        pVM = (PVM)((uintptr_t)pVM + HOST_PAGE_SIZE);
        RTMemProtect(pVM + 1, HOST_PAGE_SIZE, RTMEM_PROT_NONE);

        /* VM: */
        pVM->enmVMState           = VMSTATE_CREATING;
        pVM->pVMR3                = pVM;
        pVM->hSelf                = _1M;
        pVM->pSession             = pSession;
        pVM->cCpus                = cCpus;
        pVM->uCpuExecutionCap     = 100;
        pVM->cbSelf               = sizeof(VM);
        pVM->cbVCpu               = sizeof(VMCPU);
        pVM->uStructVersion       = 1;

        /* CPUs: */
        PVMCPU pVCpu = (PVMCPU)((uintptr_t)pVM + sizeof(VM) + HOST_PAGE_SIZE);
        for (VMCPUID idxCpu = 0; idxCpu < cCpus; idxCpu++)
        {
            pVM->apCpusR3[idxCpu] = pVCpu;

            pVCpu->enmState        = VMCPUSTATE_STOPPED;
            pVCpu->pVMR3           = pVM;
            pVCpu->hNativeThread   = NIL_RTNATIVETHREAD;
            pVCpu->hNativeThreadR0 = NIL_RTNATIVETHREAD;
            pVCpu->hThread         = NIL_RTTHREAD;
            pVCpu->idCpu           = idxCpu;

            RTMemProtect(pVCpu + 1, HOST_PAGE_SIZE, RTMEM_PROT_NONE);
            pVCpu = (PVMCPU)((uintptr_t)pVCpu + sizeof(VMCPU) + HOST_PAGE_SIZE);
        }

        *ppVM   = pVM;
        *ppVMR0 = NIL_RTR0PTR;
    }
    RT_NOREF(pUVM);
    return VINF_SUCCESS;
}


/**
 * Driverless: VMMR0_DO_GVMM_DESTROY_VM
 *
 * @returns VBox status code.
 * @param   pUVM    The user mode VM handle.
 * @param   pVM     The cross context VM structure.
 */
VMMR3_INT_DECL(int) GVMMR3DestroyVM(PUVM pUVM, PVM pVM)
{
    AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
    Assert(pUVM->cCpus == pVM->cCpus);
    RT_NOREF(pUVM);

    int rc;
    if (!SUPR3IsDriverless())
        rc = SUPR3CallVMMR0Ex(pVM->pVMR0ForCall, 0 /*idCpu*/, VMMR0_DO_GVMM_DESTROY_VM, 0, NULL);
    else
    {
        RTMemPageFree((uint8_t *)pVM - HOST_PAGE_SIZE,
                      sizeof(VM) + sizeof(VMCPU) * pVM->cCpus + HOST_PAGE_SIZE * (1 + 2 * pVM->cCpus));
        rc = VINF_SUCCESS;
    }
    return rc;
}


/**
 * Register the calling EMT with GVM.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 * @param   idCpu       The Virtual CPU ID.
 * @thread  EMT(idCpu)
 * @see     GVMMR0RegisterVCpu
 */
VMMR3_INT_DECL(int) GVMMR3RegisterVCpu(PVM pVM, VMCPUID idCpu)
{
    Assert(VMMGetCpuId(pVM) == idCpu);
    int rc;
    if (!SUPR3IsDriverless())
    {
        rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_REGISTER_VMCPU, 0, NULL);
        if (RT_FAILURE(rc))
            LogRel(("idCpu=%u rc=%Rrc\n", idCpu, rc));
    }
    else
        rc = VINF_SUCCESS;
    return rc;
}


/**
 * Deregister the calling EMT from GVM.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 * @param   idCpu       The Virtual CPU ID.
 * @thread  EMT(idCpu)
 * @see     GVMMR0DeregisterVCpu
 */
VMMR3_INT_DECL(int) GVMMR3DeregisterVCpu(PVM pVM, VMCPUID idCpu)
{
    Assert(VMMGetCpuId(pVM) == idCpu);
    int rc;
    if (!SUPR3IsDriverless())
        rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), idCpu, VMMR0_DO_GVMM_DEREGISTER_VMCPU, 0, NULL);
    else
        rc = VINF_SUCCESS;
    return rc;
}


/**
 * @see GVMMR0RegisterWorkerThread
 */
VMMR3_INT_DECL(int)  GVMMR3RegisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
{
    if (SUPR3IsDriverless())
        return VINF_SUCCESS;
    GVMMREGISTERWORKERTHREADREQ Req;
    Req.Hdr.u32Magic    = SUPVMMR0REQHDR_MAGIC;
    Req.Hdr.cbReq       = sizeof(Req);
    Req.hNativeThreadR3 = RTThreadNativeSelf();
    return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
                            VMMR0_DO_GVMM_REGISTER_WORKER_THREAD, (unsigned)enmWorker, &Req.Hdr);
}


/**
 * @see GVMMR0DeregisterWorkerThread
 */
VMMR3_INT_DECL(int)  GVMMR3DeregisterWorkerThread(PVM pVM, GVMMWORKERTHREAD enmWorker)
{
    if (SUPR3IsDriverless())
        return VINF_SUCCESS;
    return SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID,
                            VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD, (unsigned)enmWorker, NULL);
}