1 | /** @file
|
---|
2 | * VM - The Virtual Machine, GVM/GVMCPU or VM/VMCPU depending on context.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2019-2023 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef VBOX_INCLUDED_vmm_vmcc_h
|
---|
37 | #define VBOX_INCLUDED_vmm_vmcc_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 |
|
---|
43 | #include <VBox/vmm/vm.h>
|
---|
44 | #ifdef IN_RING0
|
---|
45 | # include <VBox/vmm/gvm.h>
|
---|
46 | #else
|
---|
47 | # include <VBox/vmm/uvm.h>
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /** @typedef VMCC
|
---|
51 | * Context specific VM derived structure.
|
---|
52 | * This is plain VM in ring-3 and GVM (inherits from VM) in ring-0. */
|
---|
53 | /** @typedef VMCPUCC
|
---|
54 | * Context specific VMCPU derived structure.
|
---|
55 | * This is plain VM in ring-3 and GVMCPU (inherits from VMCPU) in ring-0. */
|
---|
56 | #ifdef IN_RING0
|
---|
57 | typedef GVM VMCC;
|
---|
58 | typedef GVMCPU VMCPUCC;
|
---|
59 | #else
|
---|
60 | typedef VM VMCC;
|
---|
61 | typedef VMCPU VMCPUCC;
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | /** @def VMCC_GET_CPU_0
|
---|
65 | * Gets the context specfic pointer to virtual CPU \#0.
|
---|
66 | * @param a_pVM The context specfic VM structure.
|
---|
67 | */
|
---|
68 | #ifdef IN_RING0
|
---|
69 | # define VMCC_GET_CPU_0(a_pVM) (&(a_pVM)->aCpus[0])
|
---|
70 | #else
|
---|
71 | # define VMCC_GET_CPU_0(a_pVM) ((a_pVM)->CTX_SUFF(apCpus)[0])
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | /** @def VMCC_GET_CPU
|
---|
75 | * Gets the context specfic pointer to a virtual CPU by index (ID).
|
---|
76 | * @param a_pVM The context specfic VM structure.
|
---|
77 | * @param a_idCpu The CPU number to get (caller ensures validity).
|
---|
78 | */
|
---|
79 | #ifdef IN_RING0
|
---|
80 | # define VMCC_GET_CPU(a_pVM, a_idCpu) (&(a_pVM)->aCpus[(a_idCpu)])
|
---|
81 | #else
|
---|
82 | # define VMCC_GET_CPU(a_pVM, a_idCpu) ((a_pVM)->CTX_SUFF(apCpus)[(a_idCpu)])
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | /** @def VMCC_FOR_EACH_VMCPU
|
---|
86 | * For enumerating VCpus in ascending order, avoiding unnecessary apCpusR0
|
---|
87 | * access in ring-0, caching the CPU count and not checking for CPU \#0.
|
---|
88 | *
|
---|
89 | * Defines local variables @c idCpu, @c pVCpu and @c cCpus.
|
---|
90 | *
|
---|
91 | * @param a_pVM The VM handle.
|
---|
92 | *
|
---|
93 | * @note Close loop with VMCC_FOR_EACH_VMCPU_END.
|
---|
94 | */
|
---|
95 | #define VMCC_FOR_EACH_VMCPU(a_pVM) \
|
---|
96 | do { \
|
---|
97 | VMCPUID idCpu = 0; \
|
---|
98 | VMCPUID const cCpus = (a_pVM)->cCpus; \
|
---|
99 | PVMCPUCC pVCpu = VMCC_GET_CPU_0(a_pVM); \
|
---|
100 | for (;;) \
|
---|
101 | {
|
---|
102 |
|
---|
103 | /** @def VMCC_FOR_EACH_VMCPU_END
|
---|
104 | * Ends a VMCC_FOR_EACH_VMCPU loop.
|
---|
105 | * @param a_pVM The VM handle.
|
---|
106 | */
|
---|
107 | #define VMCC_FOR_EACH_VMCPU_END(a_pVM) \
|
---|
108 | /* advance */ \
|
---|
109 | if (++idCpu >= cCpus) \
|
---|
110 | break; \
|
---|
111 | pVCpu = VMCC_GET_CPU(pVM, idCpu); \
|
---|
112 | } \
|
---|
113 | } while (0)
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Execute the given statement for each virtual CPU in an environment with
|
---|
117 | * @c pVCpu and @c idCpu variables.
|
---|
118 | *
|
---|
119 | * @param a_pVM The VM handle.
|
---|
120 | * @param a_Stmt The statement to execute.
|
---|
121 | */
|
---|
122 | #define VMCC_FOR_EACH_VMCPU_STMT(a_pVM, a_Stmt) VMCC_FOR_EACH_VMCPU(pVM) { a_Stmt; } VMCC_FOR_EACH_VMCPU_END(pVM)
|
---|
123 |
|
---|
124 | /** @def VMCC_GET_VMR0_FOR_CALL(pVM) */
|
---|
125 | #if defined(IN_RING3)
|
---|
126 | # define VMCC_GET_VMR0_FOR_CALL(a_pVM) ((a_pVM)->pVMR0ForCall)
|
---|
127 | #elif defined(IN_RING3)
|
---|
128 | # define VMCC_GET_VMR0_FOR_CALL(a_pVM) ((a_pVM)->pVMR0)
|
---|
129 | #else
|
---|
130 | # define VMCC_GET_VMR0_FOR_CALL(a_pVM) ((a_pVM)->ring3_only_macro)
|
---|
131 | #endif
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Used to pick ring-0 or ring-3 VM component data.
|
---|
136 | *
|
---|
137 | * @code{.cpp}
|
---|
138 | * pVM->VMCC_CTX(pdm).s.pfnWorker
|
---|
139 | * @endcode
|
---|
140 | */
|
---|
141 | #ifdef IN_RING0
|
---|
142 | # define VMCC_CTX(a_Name) a_Name ## r0
|
---|
143 | #else
|
---|
144 | # define VMCC_CTX(a_Name) a_Name
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | #endif /* !VBOX_INCLUDED_vmm_vmcc_h */
|
---|
148 |
|
---|