1 | /* $Id: VMMAll.cpp 39078 2011-10-21 14:18:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM All Contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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_VMM
|
---|
23 | #include <VBox/vmm/vmm.h>
|
---|
24 | #include "VMMInternal.h"
|
---|
25 | #include <VBox/vmm/vm.h>
|
---|
26 | #include <VBox/vmm/vmm.h>
|
---|
27 | #include <VBox/param.h>
|
---|
28 | #include <iprt/thread.h>
|
---|
29 | #include <iprt/mp.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Gets the bottom of the hypervisor stack - RC Ptr.
|
---|
34 | *
|
---|
35 | * (The returned address is not actually writable, only after it's decremented
|
---|
36 | * by a push/ret/whatever does it become writable.)
|
---|
37 | *
|
---|
38 | * @returns bottom of the stack.
|
---|
39 | * @param pVCpu The VMCPU handle.
|
---|
40 | */
|
---|
41 | VMMDECL(RTRCPTR) VMMGetStackRC(PVMCPU pVCpu)
|
---|
42 | {
|
---|
43 | return (RTRCPTR)pVCpu->vmm.s.pbEMTStackBottomRC;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Gets the ID virtual of the virtual CPU associated with the calling thread.
|
---|
49 | *
|
---|
50 | * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
|
---|
51 | *
|
---|
52 | * @param pVM Pointer to the shared VM handle.
|
---|
53 | */
|
---|
54 | VMMDECL(VMCPUID) VMMGetCpuId(PVM pVM)
|
---|
55 | {
|
---|
56 | #if defined(IN_RING3)
|
---|
57 | return VMR3GetVMCPUId(pVM);
|
---|
58 |
|
---|
59 | #elif defined(IN_RING0)
|
---|
60 | if (pVM->cCpus == 1)
|
---|
61 | return 0;
|
---|
62 |
|
---|
63 | /* Search first by host cpu id (most common case)
|
---|
64 | * and then by native thread id (page fusion case).
|
---|
65 | */
|
---|
66 | /* RTMpCpuId had better be cheap. */
|
---|
67 | RTCPUID idHostCpu = RTMpCpuId();
|
---|
68 |
|
---|
69 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
70 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
71 | {
|
---|
72 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
73 |
|
---|
74 | if (pVCpu->idHostCpu == idHostCpu)
|
---|
75 | return pVCpu->idCpu;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* RTThreadGetNativeSelf had better be cheap. */
|
---|
79 | RTNATIVETHREAD hThread = RTThreadNativeSelf();
|
---|
80 |
|
---|
81 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
82 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
83 | {
|
---|
84 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
85 |
|
---|
86 | if (pVCpu->hNativeThreadR0 == hThread)
|
---|
87 | return pVCpu->idCpu;
|
---|
88 | }
|
---|
89 | return NIL_VMCPUID;
|
---|
90 |
|
---|
91 | #else /* RC: Always EMT(0) */
|
---|
92 | NOREF(pVM);
|
---|
93 | return 0;
|
---|
94 | #endif
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Returns the VMCPU of the calling EMT.
|
---|
100 | *
|
---|
101 | * @returns The VMCPU pointer. NULL if not an EMT.
|
---|
102 | *
|
---|
103 | * @param pVM The VM to operate on.
|
---|
104 | */
|
---|
105 | VMMDECL(PVMCPU) VMMGetCpu(PVM pVM)
|
---|
106 | {
|
---|
107 | #ifdef IN_RING3
|
---|
108 | VMCPUID idCpu = VMR3GetVMCPUId(pVM);
|
---|
109 | if (idCpu == NIL_VMCPUID)
|
---|
110 | return NULL;
|
---|
111 | Assert(idCpu < pVM->cCpus);
|
---|
112 | return &pVM->aCpus[idCpu];
|
---|
113 |
|
---|
114 | #elif defined(IN_RING0)
|
---|
115 | if (pVM->cCpus == 1)
|
---|
116 | return &pVM->aCpus[0];
|
---|
117 |
|
---|
118 | /* Search first by host cpu id (most common case)
|
---|
119 | * and then by native thread id (page fusion case).
|
---|
120 | */
|
---|
121 |
|
---|
122 | /* RTMpCpuId had better be cheap. */
|
---|
123 | RTCPUID idHostCpu = RTMpCpuId();
|
---|
124 |
|
---|
125 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
126 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
127 | {
|
---|
128 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
129 |
|
---|
130 | if (pVCpu->idHostCpu == idHostCpu)
|
---|
131 | return pVCpu;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* RTThreadGetNativeSelf had better be cheap. */
|
---|
135 | RTNATIVETHREAD hThread = RTThreadNativeSelf();
|
---|
136 |
|
---|
137 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
138 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
139 | {
|
---|
140 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
141 |
|
---|
142 | if (pVCpu->hNativeThreadR0 == hThread)
|
---|
143 | return pVCpu;
|
---|
144 | }
|
---|
145 | return NULL;
|
---|
146 |
|
---|
147 | #else /* RC: Always EMT(0) */
|
---|
148 | return &pVM->aCpus[0];
|
---|
149 | #endif /* IN_RING0 */
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Returns the VMCPU of the first EMT thread.
|
---|
155 | *
|
---|
156 | * @returns The VMCPU pointer.
|
---|
157 | * @param pVM The VM to operate on.
|
---|
158 | */
|
---|
159 | VMMDECL(PVMCPU) VMMGetCpu0(PVM pVM)
|
---|
160 | {
|
---|
161 | Assert(pVM->cCpus == 1);
|
---|
162 | return &pVM->aCpus[0];
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Returns the VMCPU of the specified virtual CPU.
|
---|
168 | *
|
---|
169 | * @returns The VMCPU pointer. NULL if idCpu is invalid.
|
---|
170 | *
|
---|
171 | * @param pVM The VM to operate on.
|
---|
172 | * @param idCpu The ID of the virtual CPU.
|
---|
173 | */
|
---|
174 | VMMDECL(PVMCPU) VMMGetCpuById(PVM pVM, RTCPUID idCpu)
|
---|
175 | {
|
---|
176 | AssertReturn(idCpu < pVM->cCpus, NULL);
|
---|
177 | return &pVM->aCpus[idCpu];
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Gets the VBOX_SVN_REV.
|
---|
183 | *
|
---|
184 | * This is just to avoid having to compile a bunch of big files
|
---|
185 | * and requires less Makefile mess.
|
---|
186 | *
|
---|
187 | * @returns VBOX_SVN_REV.
|
---|
188 | */
|
---|
189 | VMMDECL(uint32_t) VMMGetSvnRev(void)
|
---|
190 | {
|
---|
191 | return VBOX_SVN_REV;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Queries the current switcher
|
---|
197 | *
|
---|
198 | * @returns active switcher
|
---|
199 | * @param pVM VM handle.
|
---|
200 | */
|
---|
201 | VMMDECL(VMMSWITCHER) VMMGetSwitcher(PVM pVM)
|
---|
202 | {
|
---|
203 | return pVM->vmm.s.enmSwitcher;
|
---|
204 | }
|
---|