1 | /* $Id: VMMR0.cpp 13879 2008-11-05 16:11:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - Host Context Ring 0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
26 | #include <VBox/vmm.h>
|
---|
27 | #include <VBox/sup.h>
|
---|
28 | #include <VBox/trpm.h>
|
---|
29 | #include <VBox/cpum.h>
|
---|
30 | #include <VBox/stam.h>
|
---|
31 | #include <VBox/tm.h>
|
---|
32 | #include "VMMInternal.h"
|
---|
33 | #include <VBox/vm.h>
|
---|
34 | #include <VBox/gvmm.h>
|
---|
35 | #include <VBox/gmm.h>
|
---|
36 | #include <VBox/intnet.h>
|
---|
37 | #include <VBox/hwaccm.h>
|
---|
38 | #include <VBox/param.h>
|
---|
39 |
|
---|
40 | #include <VBox/err.h>
|
---|
41 | #include <VBox/version.h>
|
---|
42 | #include <VBox/log.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/stdarg.h>
|
---|
45 | #include <iprt/mp.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 | #if defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
|
---|
49 | # pragma intrinsic(_AddressOfReturnAddress)
|
---|
50 | #endif
|
---|
51 |
|
---|
52 |
|
---|
53 | /*******************************************************************************
|
---|
54 | * Internal Functions *
|
---|
55 | *******************************************************************************/
|
---|
56 | static int VMMR0Init(PVM pVM, uint32_t uSvnRev);
|
---|
57 | static int VMMR0Term(PVM pVM);
|
---|
58 | __BEGIN_DECLS
|
---|
59 | VMMR0DECL(int) ModuleInit(void);
|
---|
60 | VMMR0DECL(void) ModuleTerm(void);
|
---|
61 | __END_DECLS
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Global Variables *
|
---|
66 | *******************************************************************************/
|
---|
67 | /** Pointer to the internal networking service instance. */
|
---|
68 | PINTNET g_pIntNet = 0;
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Initialize the module.
|
---|
73 | * This is called when we're first loaded.
|
---|
74 | *
|
---|
75 | * @returns 0 on success.
|
---|
76 | * @returns VBox status on failure.
|
---|
77 | */
|
---|
78 | VMMR0DECL(int) ModuleInit(void)
|
---|
79 | {
|
---|
80 | LogFlow(("ModuleInit:\n"));
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Initialize the GVMM, GMM.& HWACCM
|
---|
84 | */
|
---|
85 | int rc = GVMMR0Init();
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | rc = GMMR0Init();
|
---|
89 | if (RT_SUCCESS(rc))
|
---|
90 | {
|
---|
91 | rc = HWACCMR0Init();
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | {
|
---|
94 | LogFlow(("ModuleInit: g_pIntNet=%p\n", g_pIntNet));
|
---|
95 | g_pIntNet = NULL;
|
---|
96 | LogFlow(("ModuleInit: g_pIntNet=%p should be NULL now...\n", g_pIntNet));
|
---|
97 | rc = INTNETR0Create(&g_pIntNet);
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | LogFlow(("ModuleInit: returns success. g_pIntNet=%p\n", g_pIntNet));
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 | g_pIntNet = NULL;
|
---|
104 | LogFlow(("ModuleTerm: returns %Rrc\n", rc));
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | LogFlow(("ModuleInit: failed %Rrc\n", rc));
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Terminate the module.
|
---|
116 | * This is called when we're finally unloaded.
|
---|
117 | */
|
---|
118 | VMMR0DECL(void) ModuleTerm(void)
|
---|
119 | {
|
---|
120 | LogFlow(("ModuleTerm:\n"));
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Destroy the internal networking instance.
|
---|
124 | */
|
---|
125 | if (g_pIntNet)
|
---|
126 | {
|
---|
127 | INTNETR0Destroy(g_pIntNet);
|
---|
128 | g_pIntNet = NULL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* Global HWACCM cleanup */
|
---|
132 | HWACCMR0Term();
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Destroy the GMM and GVMM instances.
|
---|
136 | */
|
---|
137 | GMMR0Term();
|
---|
138 | GVMMR0Term();
|
---|
139 |
|
---|
140 | LogFlow(("ModuleTerm: returns\n"));
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Initaties the R0 driver for a particular VM instance.
|
---|
146 | *
|
---|
147 | * @returns VBox status code.
|
---|
148 | *
|
---|
149 | * @param pVM The VM instance in question.
|
---|
150 | * @param uSvnRev The SVN revision of the ring-3 part.
|
---|
151 | * @thread EMT.
|
---|
152 | */
|
---|
153 | static int VMMR0Init(PVM pVM, uint32_t uSvnRev)
|
---|
154 | {
|
---|
155 | /*
|
---|
156 | * Match the SVN revisions.
|
---|
157 | */
|
---|
158 | if (uSvnRev != VMMGetSvnRev())
|
---|
159 | return VERR_VERSION_MISMATCH;
|
---|
160 | if ( !VALID_PTR(pVM)
|
---|
161 | || pVM->pVMR0 != pVM)
|
---|
162 | return VERR_INVALID_PARAMETER;
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * Register the EMT R0 logger instance.
|
---|
166 | */
|
---|
167 | PVMMR0LOGGER pR0Logger = pVM->vmm.s.pR0LoggerR0;
|
---|
168 | if (pR0Logger)
|
---|
169 | {
|
---|
170 | #if 0 /* testing of the logger. */
|
---|
171 | LogCom(("VMMR0Init: before %p\n", RTLogDefaultInstance()));
|
---|
172 | LogCom(("VMMR0Init: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
|
---|
173 | LogCom(("VMMR0Init: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
|
---|
174 | LogCom(("VMMR0Init: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
|
---|
175 |
|
---|
176 | RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
|
---|
177 | LogCom(("VMMR0Init: after %p reg\n", RTLogDefaultInstance()));
|
---|
178 | RTLogSetDefaultInstanceThread(NULL, 0);
|
---|
179 | LogCom(("VMMR0Init: after %p dereg\n", RTLogDefaultInstance()));
|
---|
180 |
|
---|
181 | pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
|
---|
182 | LogCom(("VMMR0Init: returned succesfully from direct logger call.\n"));
|
---|
183 | pR0Logger->Logger.pfnFlush(&pR0Logger->Logger);
|
---|
184 | LogCom(("VMMR0Init: returned succesfully from direct flush call.\n"));
|
---|
185 |
|
---|
186 | RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
|
---|
187 | LogCom(("VMMR0Init: after %p reg2\n", RTLogDefaultInstance()));
|
---|
188 | pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
|
---|
189 | LogCom(("VMMR0Init: returned succesfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
|
---|
190 | RTLogSetDefaultInstanceThread(NULL, 0);
|
---|
191 | LogCom(("VMMR0Init: after %p dereg2\n", RTLogDefaultInstance()));
|
---|
192 |
|
---|
193 | RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
|
---|
194 | LogCom(("VMMR0Init: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
|
---|
195 |
|
---|
196 | RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
|
---|
197 | RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
|
---|
198 | LogCom(("VMMR0Init: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
|
---|
199 | #endif
|
---|
200 | Log(("Switching to per-thread logging instance %p (key=%p)\n", &pR0Logger->Logger, pVM->pSession));
|
---|
201 | RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Initialize the per VM data for GVMM and GMM.
|
---|
206 | */
|
---|
207 | int rc = GVMMR0InitVM(pVM);
|
---|
208 | // if (RT_SUCCESS(rc))
|
---|
209 | // rc = GMMR0InitPerVMData(pVM);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | /*
|
---|
213 | * Init HWACCM.
|
---|
214 | */
|
---|
215 | rc = HWACCMR0InitVM(pVM);
|
---|
216 | if (RT_SUCCESS(rc))
|
---|
217 | {
|
---|
218 | /*
|
---|
219 | * Init CPUM.
|
---|
220 | */
|
---|
221 | rc = CPUMR0Init(pVM);
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* failed */
|
---|
228 | RTLogSetDefaultInstanceThread(NULL, 0);
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Terminates the R0 driver for a particular VM instance.
|
---|
235 | *
|
---|
236 | * @returns VBox status code.
|
---|
237 | *
|
---|
238 | * @param pVM The VM instance in question.
|
---|
239 | * @thread EMT.
|
---|
240 | */
|
---|
241 | static int VMMR0Term(PVM pVM)
|
---|
242 | {
|
---|
243 | HWACCMR0TermVM(pVM);
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Deregister the logger.
|
---|
247 | */
|
---|
248 | RTLogSetDefaultInstanceThread(NULL, 0);
|
---|
249 | return VINF_SUCCESS;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Calls the ring-3 host code.
|
---|
255 | *
|
---|
256 | * @returns VBox status code of the ring-3 call.
|
---|
257 | * @param pVM The VM handle.
|
---|
258 | * @param enmOperation The operation.
|
---|
259 | * @param uArg The argument to the operation.
|
---|
260 | */
|
---|
261 | VMMR0DECL(int) VMMR0CallHost(PVM pVM, VMMCALLHOST enmOperation, uint64_t uArg)
|
---|
262 | {
|
---|
263 | /** @todo profile this! */
|
---|
264 | pVM->vmm.s.enmCallHostOperation = enmOperation;
|
---|
265 | pVM->vmm.s.u64CallHostArg = uArg;
|
---|
266 | pVM->vmm.s.rcCallHost = VERR_INTERNAL_ERROR;
|
---|
267 | int rc = vmmR0CallHostLongJmp(&pVM->vmm.s.CallHostR0JmpBuf, VINF_VMM_CALL_HOST);
|
---|
268 | if (rc == VINF_SUCCESS)
|
---|
269 | rc = pVM->vmm.s.rcCallHost;
|
---|
270 | return rc;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | #ifdef VBOX_WITH_STATISTICS
|
---|
275 | /**
|
---|
276 | * Record return code statistics
|
---|
277 | * @param pVM The VM handle.
|
---|
278 | * @param rc The status code.
|
---|
279 | */
|
---|
280 | static void vmmR0RecordRC(PVM pVM, int rc)
|
---|
281 | {
|
---|
282 | /*
|
---|
283 | * Collect statistics.
|
---|
284 | */
|
---|
285 | switch (rc)
|
---|
286 | {
|
---|
287 | case VINF_SUCCESS:
|
---|
288 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetNormal);
|
---|
289 | break;
|
---|
290 | case VINF_EM_RAW_INTERRUPT:
|
---|
291 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterrupt);
|
---|
292 | break;
|
---|
293 | case VINF_EM_RAW_INTERRUPT_HYPER:
|
---|
294 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
|
---|
295 | break;
|
---|
296 | case VINF_EM_RAW_GUEST_TRAP:
|
---|
297 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGuestTrap);
|
---|
298 | break;
|
---|
299 | case VINF_EM_RAW_RING_SWITCH:
|
---|
300 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitch);
|
---|
301 | break;
|
---|
302 | case VINF_EM_RAW_RING_SWITCH_INT:
|
---|
303 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
|
---|
304 | break;
|
---|
305 | case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
|
---|
306 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetExceptionPrivilege);
|
---|
307 | break;
|
---|
308 | case VINF_EM_RAW_STALE_SELECTOR:
|
---|
309 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
|
---|
310 | break;
|
---|
311 | case VINF_EM_RAW_IRET_TRAP:
|
---|
312 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
|
---|
313 | break;
|
---|
314 | case VINF_IOM_HC_IOPORT_READ:
|
---|
315 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
|
---|
316 | break;
|
---|
317 | case VINF_IOM_HC_IOPORT_WRITE:
|
---|
318 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
|
---|
319 | break;
|
---|
320 | case VINF_IOM_HC_MMIO_READ:
|
---|
321 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
|
---|
322 | break;
|
---|
323 | case VINF_IOM_HC_MMIO_WRITE:
|
---|
324 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
|
---|
325 | break;
|
---|
326 | case VINF_IOM_HC_MMIO_READ_WRITE:
|
---|
327 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
|
---|
328 | break;
|
---|
329 | case VINF_PATM_HC_MMIO_PATCH_READ:
|
---|
330 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
|
---|
331 | break;
|
---|
332 | case VINF_PATM_HC_MMIO_PATCH_WRITE:
|
---|
333 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
|
---|
334 | break;
|
---|
335 | case VINF_EM_RAW_EMULATE_INSTR:
|
---|
336 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
|
---|
337 | break;
|
---|
338 | case VINF_PATCH_EMULATE_INSTR:
|
---|
339 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
|
---|
340 | break;
|
---|
341 | case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
|
---|
342 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
|
---|
343 | break;
|
---|
344 | case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
|
---|
345 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
|
---|
346 | break;
|
---|
347 | case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
|
---|
348 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
|
---|
349 | break;
|
---|
350 | case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
|
---|
351 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
|
---|
352 | break;
|
---|
353 | case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
|
---|
354 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPDFault);
|
---|
355 | break;
|
---|
356 | case VINF_CSAM_PENDING_ACTION:
|
---|
357 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
|
---|
358 | break;
|
---|
359 | case VINF_PGM_SYNC_CR3:
|
---|
360 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
|
---|
361 | break;
|
---|
362 | case VINF_PATM_PATCH_INT3:
|
---|
363 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
|
---|
364 | break;
|
---|
365 | case VINF_PATM_PATCH_TRAP_PF:
|
---|
366 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
|
---|
367 | break;
|
---|
368 | case VINF_PATM_PATCH_TRAP_GP:
|
---|
369 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
|
---|
370 | break;
|
---|
371 | case VINF_PATM_PENDING_IRQ_AFTER_IRET:
|
---|
372 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
|
---|
373 | break;
|
---|
374 | case VERR_REM_FLUSHED_PAGES_OVERFLOW:
|
---|
375 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPageOverflow);
|
---|
376 | break;
|
---|
377 | case VINF_EM_RESCHEDULE_REM:
|
---|
378 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
|
---|
379 | break;
|
---|
380 | case VINF_EM_RAW_TO_R3:
|
---|
381 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3);
|
---|
382 | break;
|
---|
383 | case VINF_EM_RAW_TIMER_PENDING:
|
---|
384 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
|
---|
385 | break;
|
---|
386 | case VINF_EM_RAW_INTERRUPT_PENDING:
|
---|
387 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
|
---|
388 | break;
|
---|
389 | case VINF_VMM_CALL_HOST:
|
---|
390 | switch (pVM->vmm.s.enmCallHostOperation)
|
---|
391 | {
|
---|
392 | case VMMCALLHOST_PDM_LOCK:
|
---|
393 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMLock);
|
---|
394 | break;
|
---|
395 | case VMMCALLHOST_PDM_QUEUE_FLUSH:
|
---|
396 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMQueueFlush);
|
---|
397 | break;
|
---|
398 | case VMMCALLHOST_PGM_POOL_GROW:
|
---|
399 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
|
---|
400 | break;
|
---|
401 | case VMMCALLHOST_PGM_LOCK:
|
---|
402 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMLock);
|
---|
403 | break;
|
---|
404 | case VMMCALLHOST_PGM_MAP_CHUNK:
|
---|
405 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
|
---|
406 | break;
|
---|
407 | case VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES:
|
---|
408 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
|
---|
409 | break;
|
---|
410 | #ifndef VBOX_WITH_NEW_PHYS_CODE
|
---|
411 | case VMMCALLHOST_PGM_RAM_GROW_RANGE:
|
---|
412 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMGrowRAM);
|
---|
413 | break;
|
---|
414 | #endif
|
---|
415 | case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
|
---|
416 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallRemReplay);
|
---|
417 | break;
|
---|
418 | case VMMCALLHOST_VMM_LOGGER_FLUSH:
|
---|
419 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallLogFlush);
|
---|
420 | break;
|
---|
421 | case VMMCALLHOST_VM_SET_ERROR:
|
---|
422 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
|
---|
423 | break;
|
---|
424 | case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
|
---|
425 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
|
---|
426 | break;
|
---|
427 | case VMMCALLHOST_VM_R0_ASSERTION:
|
---|
428 | default:
|
---|
429 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCallHost);
|
---|
430 | break;
|
---|
431 | }
|
---|
432 | break;
|
---|
433 | case VINF_PATM_DUPLICATE_FUNCTION:
|
---|
434 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
|
---|
435 | break;
|
---|
436 | case VINF_PGM_CHANGE_MODE:
|
---|
437 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
|
---|
438 | break;
|
---|
439 | case VINF_EM_RAW_EMULATE_INSTR_HLT:
|
---|
440 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulHlt);
|
---|
441 | break;
|
---|
442 | case VINF_EM_PENDING_REQUEST:
|
---|
443 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
|
---|
444 | break;
|
---|
445 | default:
|
---|
446 | STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMisc);
|
---|
447 | break;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | #endif /* VBOX_WITH_STATISTICS */
|
---|
451 |
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * The Ring 0 entry point, called by the interrupt gate.
|
---|
455 | *
|
---|
456 | * @returns VBox status code.
|
---|
457 | * @param pVM The VM to operate on.
|
---|
458 | * @param enmOperation Which operation to execute.
|
---|
459 | * @param pvArg Argument to the operation.
|
---|
460 | * @remarks Assume called with interrupts disabled.
|
---|
461 | */
|
---|
462 | VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
|
---|
463 | {
|
---|
464 | switch (enmOperation)
|
---|
465 | {
|
---|
466 | #ifdef VBOX_WITH_IDT_PATCHING
|
---|
467 | /*
|
---|
468 | * Switch to GC.
|
---|
469 | * These calls return whatever the GC returns.
|
---|
470 | */
|
---|
471 | case VMMR0_DO_RAW_RUN:
|
---|
472 | {
|
---|
473 | /* Safety precaution as VMX disables the switcher. */
|
---|
474 | Assert(!pVM->vmm.s.fSwitcherDisabled);
|
---|
475 | if (pVM->vmm.s.fSwitcherDisabled)
|
---|
476 | return VERR_NOT_SUPPORTED;
|
---|
477 |
|
---|
478 | STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
|
---|
479 | register int rc;
|
---|
480 | pVM->vmm.s.iLastGZRc = rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
|
---|
481 |
|
---|
482 | #ifdef VBOX_WITH_STATISTICS
|
---|
483 | vmmR0RecordRC(pVM, rc);
|
---|
484 | #endif
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * We'll let TRPM change the stack frame so our return is different.
|
---|
488 | * Just keep in mind that after the call, things have changed!
|
---|
489 | */
|
---|
490 | if ( rc == VINF_EM_RAW_INTERRUPT
|
---|
491 | || rc == VINF_EM_RAW_INTERRUPT_HYPER)
|
---|
492 | {
|
---|
493 | /*
|
---|
494 | * Don't trust the compiler to get this right.
|
---|
495 | * gcc -fomit-frame-pointer screws up big time here. This works fine in 64-bit
|
---|
496 | * mode too because we push the arguments on the stack in the IDT patch code.
|
---|
497 | */
|
---|
498 | # if defined(__GNUC__)
|
---|
499 | void *pvRet = (uint8_t *)__builtin_frame_address(0) + sizeof(void *);
|
---|
500 | # elif defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
|
---|
501 | void *pvRet = (uint8_t *)_AddressOfReturnAddress();
|
---|
502 | # elif defined(RT_ARCH_X86)
|
---|
503 | void *pvRet = (uint8_t *)&pVM - sizeof(pVM);
|
---|
504 | # else
|
---|
505 | # error "huh?"
|
---|
506 | # endif
|
---|
507 | if ( ((uintptr_t *)pvRet)[1] == (uintptr_t)pVM
|
---|
508 | && ((uintptr_t *)pvRet)[2] == (uintptr_t)enmOperation
|
---|
509 | && ((uintptr_t *)pvRet)[3] == (uintptr_t)pvArg)
|
---|
510 | TRPMR0SetupInterruptDispatcherFrame(pVM, pvRet);
|
---|
511 | else
|
---|
512 | {
|
---|
513 | # if defined(DEBUG) || defined(LOG_ENABLED)
|
---|
514 | static bool s_fHaveWarned = false;
|
---|
515 | if (!s_fHaveWarned)
|
---|
516 | {
|
---|
517 | s_fHaveWarned = true;
|
---|
518 | RTLogPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
|
---|
519 | RTLogComPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
|
---|
520 | }
|
---|
521 | # endif
|
---|
522 | TRPMR0DispatchHostInterrupt(pVM);
|
---|
523 | }
|
---|
524 | }
|
---|
525 | return rc;
|
---|
526 | }
|
---|
527 |
|
---|
528 | /*
|
---|
529 | * Switch to GC to execute Hypervisor function.
|
---|
530 | */
|
---|
531 | case VMMR0_DO_CALL_HYPERVISOR:
|
---|
532 | {
|
---|
533 | /* Safety precaution as VMX disables the switcher. */
|
---|
534 | Assert(!pVM->vmm.s.fSwitcherDisabled);
|
---|
535 | if (pVM->vmm.s.fSwitcherDisabled)
|
---|
536 | return VERR_NOT_SUPPORTED;
|
---|
537 |
|
---|
538 | RTCCUINTREG fFlags = ASMIntDisableFlags();
|
---|
539 | int rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
|
---|
540 | /** @todo dispatch interrupts? */
|
---|
541 | ASMSetFlags(fFlags);
|
---|
542 | return rc;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /*
|
---|
546 | * For profiling.
|
---|
547 | */
|
---|
548 | case VMMR0_DO_NOP:
|
---|
549 | return VINF_SUCCESS;
|
---|
550 | #endif /* VBOX_WITH_IDT_PATCHING */
|
---|
551 |
|
---|
552 | default:
|
---|
553 | /*
|
---|
554 | * We're returning VERR_NOT_SUPPORT here so we've got something else
|
---|
555 | * than -1 which the interrupt gate glue code might return.
|
---|
556 | */
|
---|
557 | Log(("operation %#x is not supported\n", enmOperation));
|
---|
558 | return VERR_NOT_SUPPORTED;
|
---|
559 | }
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * The Ring 0 entry point, called by the fast-ioctl path.
|
---|
565 | *
|
---|
566 | * @param pVM The VM to operate on.
|
---|
567 | * The return code is stored in pVM->vmm.s.iLastGZRc.
|
---|
568 | * @param idCpu VMCPU id.
|
---|
569 | * @param enmOperation Which operation to execute.
|
---|
570 | * @remarks Assume called with interrupts _enabled_.
|
---|
571 | */
|
---|
572 | VMMR0DECL(void) VMMR0EntryFast(PVM pVM, unsigned idCpu, VMMR0OPERATION enmOperation)
|
---|
573 | {
|
---|
574 | if (RT_UNLIKELY(idCpu >= pVM->cCPUs))
|
---|
575 | {
|
---|
576 | pVM->vmm.s.iLastGZRc = VERR_INVALID_PARAMETER;
|
---|
577 | return;
|
---|
578 | }
|
---|
579 |
|
---|
580 | switch (enmOperation)
|
---|
581 | {
|
---|
582 | /*
|
---|
583 | * Switch to GC and run guest raw mode code.
|
---|
584 | * Disable interrupts before doing the world switch.
|
---|
585 | */
|
---|
586 | case VMMR0_DO_RAW_RUN:
|
---|
587 | {
|
---|
588 | /* Safety precaution as hwaccm disables the switcher. */
|
---|
589 | if (RT_LIKELY(!pVM->vmm.s.fSwitcherDisabled))
|
---|
590 | {
|
---|
591 | RTCCUINTREG uFlags = ASMIntDisableFlags();
|
---|
592 |
|
---|
593 | TMNotifyStartOfExecution(pVM);
|
---|
594 | int rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
|
---|
595 | pVM->vmm.s.iLastGZRc = rc;
|
---|
596 | TMNotifyEndOfExecution(pVM);
|
---|
597 |
|
---|
598 | if ( rc == VINF_EM_RAW_INTERRUPT
|
---|
599 | || rc == VINF_EM_RAW_INTERRUPT_HYPER)
|
---|
600 | TRPMR0DispatchHostInterrupt(pVM);
|
---|
601 |
|
---|
602 | ASMSetFlags(uFlags);
|
---|
603 |
|
---|
604 | #ifdef VBOX_WITH_STATISTICS
|
---|
605 | STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
|
---|
606 | vmmR0RecordRC(pVM, rc);
|
---|
607 | #endif
|
---|
608 | }
|
---|
609 | else
|
---|
610 | {
|
---|
611 | Assert(!pVM->vmm.s.fSwitcherDisabled);
|
---|
612 | pVM->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
|
---|
613 | }
|
---|
614 | break;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /*
|
---|
618 | * Run guest code using the available hardware acceleration technology.
|
---|
619 | *
|
---|
620 | * Disable interrupts before we do anything interesting. On Windows we avoid
|
---|
621 | * this by having the support driver raise the IRQL before calling us, this way
|
---|
622 | * we hope to get away with page faults and later calling into the kernel.
|
---|
623 | */
|
---|
624 | case VMMR0_DO_HWACC_RUN:
|
---|
625 | {
|
---|
626 | int rc;
|
---|
627 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
628 |
|
---|
629 | STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
|
---|
630 |
|
---|
631 | #ifndef RT_OS_WINDOWS /** @todo check other hosts */
|
---|
632 | RTCCUINTREG uFlags = ASMIntDisableFlags();
|
---|
633 | #endif
|
---|
634 | if (!HWACCMR0SuspendPending())
|
---|
635 | {
|
---|
636 | rc = HWACCMR0Enter(pVM, pVCpu);
|
---|
637 | if (RT_SUCCESS(rc))
|
---|
638 | {
|
---|
639 | rc = vmmR0CallHostSetJmp(&pVM->vmm.s.CallHostR0JmpBuf, HWACCMR0RunGuestCode, pVM, pVCpu); /* this may resume code. */
|
---|
640 | int rc2 = HWACCMR0Leave(pVM, pVCpu);
|
---|
641 | AssertRC(rc2);
|
---|
642 | }
|
---|
643 | }
|
---|
644 | else
|
---|
645 | {
|
---|
646 | /* System is about to go into suspend mode; go back to ring 3. */
|
---|
647 | rc = VINF_EM_RAW_INTERRUPT;
|
---|
648 | }
|
---|
649 | pVM->vmm.s.iLastGZRc = rc;
|
---|
650 | #ifndef RT_OS_WINDOWS /** @todo check other hosts */
|
---|
651 | ASMSetFlags(uFlags);
|
---|
652 | #endif
|
---|
653 |
|
---|
654 | #ifdef VBOX_WITH_STATISTICS
|
---|
655 | vmmR0RecordRC(pVM, rc);
|
---|
656 | #endif
|
---|
657 | /* No special action required for external interrupts, just return. */
|
---|
658 | break;
|
---|
659 | }
|
---|
660 |
|
---|
661 | /*
|
---|
662 | * For profiling.
|
---|
663 | */
|
---|
664 | case VMMR0_DO_NOP:
|
---|
665 | pVM->vmm.s.iLastGZRc = VINF_SUCCESS;
|
---|
666 | break;
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * Impossible.
|
---|
670 | */
|
---|
671 | default:
|
---|
672 | AssertMsgFailed(("%#x\n", enmOperation));
|
---|
673 | pVM->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
|
---|
674 | break;
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Validates a session or VM session argument.
|
---|
681 | *
|
---|
682 | * @returns true / false accordingly.
|
---|
683 | * @param pVM The VM argument.
|
---|
684 | * @param pSession The session argument.
|
---|
685 | */
|
---|
686 | DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
|
---|
687 | {
|
---|
688 | /* This must be set! */
|
---|
689 | if (!pSession)
|
---|
690 | return false;
|
---|
691 |
|
---|
692 | /* Only one out of the two. */
|
---|
693 | if (pVM && pClaimedSession)
|
---|
694 | return false;
|
---|
695 | if (pVM)
|
---|
696 | pClaimedSession = pVM->pSession;
|
---|
697 | return pClaimedSession == pSession;
|
---|
698 | }
|
---|
699 |
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * VMMR0EntryEx worker function, either called directly or when ever possible
|
---|
703 | * called thru a longjmp so we can exit safely on failure.
|
---|
704 | *
|
---|
705 | * @returns VBox status code.
|
---|
706 | * @param pVM The VM to operate on.
|
---|
707 | * @param enmOperation Which operation to execute.
|
---|
708 | * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
|
---|
709 | * The support driver validates this if it's present.
|
---|
710 | * @param u64Arg Some simple constant argument.
|
---|
711 | * @param pSession The session of the caller.
|
---|
712 | * @remarks Assume called with interrupts _enabled_.
|
---|
713 | */
|
---|
714 | static int vmmR0EntryExWorker(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
|
---|
715 | {
|
---|
716 | /*
|
---|
717 | * Common VM pointer validation.
|
---|
718 | */
|
---|
719 | if (pVM)
|
---|
720 | {
|
---|
721 | if (RT_UNLIKELY( !VALID_PTR(pVM)
|
---|
722 | || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
|
---|
723 | {
|
---|
724 | SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
|
---|
725 | return VERR_INVALID_POINTER;
|
---|
726 | }
|
---|
727 | if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
|
---|
728 | || pVM->enmVMState > VMSTATE_TERMINATED
|
---|
729 | || pVM->pVMR0 != pVM))
|
---|
730 | {
|
---|
731 | SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
|
---|
732 | pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
|
---|
733 | return VERR_INVALID_POINTER;
|
---|
734 | }
|
---|
735 | }
|
---|
736 |
|
---|
737 | switch (enmOperation)
|
---|
738 | {
|
---|
739 | /*
|
---|
740 | * GVM requests
|
---|
741 | */
|
---|
742 | case VMMR0_DO_GVMM_CREATE_VM:
|
---|
743 | if (pVM || u64Arg)
|
---|
744 | return VERR_INVALID_PARAMETER;
|
---|
745 | return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
|
---|
746 |
|
---|
747 | case VMMR0_DO_GVMM_DESTROY_VM:
|
---|
748 | if (pReqHdr || u64Arg)
|
---|
749 | return VERR_INVALID_PARAMETER;
|
---|
750 | return GVMMR0DestroyVM(pVM);
|
---|
751 |
|
---|
752 | case VMMR0_DO_GVMM_SCHED_HALT:
|
---|
753 | if (pReqHdr)
|
---|
754 | return VERR_INVALID_PARAMETER;
|
---|
755 | return GVMMR0SchedHalt(pVM, u64Arg);
|
---|
756 |
|
---|
757 | case VMMR0_DO_GVMM_SCHED_WAKE_UP:
|
---|
758 | if (pReqHdr || u64Arg)
|
---|
759 | return VERR_INVALID_PARAMETER;
|
---|
760 | return GVMMR0SchedWakeUp(pVM);
|
---|
761 |
|
---|
762 | case VMMR0_DO_GVMM_SCHED_POLL:
|
---|
763 | if (pReqHdr || u64Arg > 1)
|
---|
764 | return VERR_INVALID_PARAMETER;
|
---|
765 | return GVMMR0SchedPoll(pVM, !!u64Arg);
|
---|
766 |
|
---|
767 | case VMMR0_DO_GVMM_QUERY_STATISTICS:
|
---|
768 | if (u64Arg)
|
---|
769 | return VERR_INVALID_PARAMETER;
|
---|
770 | return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
|
---|
771 |
|
---|
772 | case VMMR0_DO_GVMM_RESET_STATISTICS:
|
---|
773 | if (u64Arg)
|
---|
774 | return VERR_INVALID_PARAMETER;
|
---|
775 | return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * Initialize the R0 part of a VM instance.
|
---|
779 | */
|
---|
780 | case VMMR0_DO_VMMR0_INIT:
|
---|
781 | return VMMR0Init(pVM, (uint32_t)u64Arg);
|
---|
782 |
|
---|
783 | /*
|
---|
784 | * Terminate the R0 part of a VM instance.
|
---|
785 | */
|
---|
786 | case VMMR0_DO_VMMR0_TERM:
|
---|
787 | return VMMR0Term(pVM);
|
---|
788 |
|
---|
789 | /*
|
---|
790 | * Attempt to enable hwacc mode and check the current setting.
|
---|
791 | *
|
---|
792 | */
|
---|
793 | case VMMR0_DO_HWACC_ENABLE:
|
---|
794 | return HWACCMR0EnableAllCpus(pVM, (HWACCMSTATE)u64Arg);
|
---|
795 |
|
---|
796 | /*
|
---|
797 | * Setup the hardware accelerated raw-mode session.
|
---|
798 | */
|
---|
799 | case VMMR0_DO_HWACC_SETUP_VM:
|
---|
800 | {
|
---|
801 | RTCCUINTREG fFlags = ASMIntDisableFlags();
|
---|
802 | int rc = HWACCMR0SetupVM(pVM);
|
---|
803 | ASMSetFlags(fFlags);
|
---|
804 | return rc;
|
---|
805 | }
|
---|
806 |
|
---|
807 | /*
|
---|
808 | * Switch to GC to execute Hypervisor function.
|
---|
809 | */
|
---|
810 | case VMMR0_DO_CALL_HYPERVISOR:
|
---|
811 | {
|
---|
812 | /* Safety precaution as HWACCM can disable the switcher. */
|
---|
813 | Assert(!pVM->vmm.s.fSwitcherDisabled);
|
---|
814 | if (RT_UNLIKELY(pVM->vmm.s.fSwitcherDisabled))
|
---|
815 | return VERR_NOT_SUPPORTED;
|
---|
816 |
|
---|
817 | RTCCUINTREG fFlags = ASMIntDisableFlags();
|
---|
818 | int rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
|
---|
819 | /** @todo dispatch interrupts? */
|
---|
820 | ASMSetFlags(fFlags);
|
---|
821 | return rc;
|
---|
822 | }
|
---|
823 |
|
---|
824 | /*
|
---|
825 | * PGM wrappers.
|
---|
826 | */
|
---|
827 | case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
|
---|
828 | return PGMR0PhysAllocateHandyPages(pVM);
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * GMM wrappers.
|
---|
832 | */
|
---|
833 | case VMMR0_DO_GMM_INITIAL_RESERVATION:
|
---|
834 | if (u64Arg)
|
---|
835 | return VERR_INVALID_PARAMETER;
|
---|
836 | return GMMR0InitialReservationReq(pVM, (PGMMINITIALRESERVATIONREQ)pReqHdr);
|
---|
837 | case VMMR0_DO_GMM_UPDATE_RESERVATION:
|
---|
838 | if (u64Arg)
|
---|
839 | return VERR_INVALID_PARAMETER;
|
---|
840 | return GMMR0UpdateReservationReq(pVM, (PGMMUPDATERESERVATIONREQ)pReqHdr);
|
---|
841 |
|
---|
842 | case VMMR0_DO_GMM_ALLOCATE_PAGES:
|
---|
843 | if (u64Arg)
|
---|
844 | return VERR_INVALID_PARAMETER;
|
---|
845 | return GMMR0AllocatePagesReq(pVM, (PGMMALLOCATEPAGESREQ)pReqHdr);
|
---|
846 | case VMMR0_DO_GMM_FREE_PAGES:
|
---|
847 | if (u64Arg)
|
---|
848 | return VERR_INVALID_PARAMETER;
|
---|
849 | return GMMR0FreePagesReq(pVM, (PGMMFREEPAGESREQ)pReqHdr);
|
---|
850 | case VMMR0_DO_GMM_BALLOONED_PAGES:
|
---|
851 | if (u64Arg)
|
---|
852 | return VERR_INVALID_PARAMETER;
|
---|
853 | return GMMR0BalloonedPagesReq(pVM, (PGMMBALLOONEDPAGESREQ)pReqHdr);
|
---|
854 | case VMMR0_DO_GMM_DEFLATED_BALLOON:
|
---|
855 | if (pReqHdr)
|
---|
856 | return VERR_INVALID_PARAMETER;
|
---|
857 | return GMMR0DeflatedBalloon(pVM, (uint32_t)u64Arg);
|
---|
858 |
|
---|
859 | case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
|
---|
860 | if (u64Arg)
|
---|
861 | return VERR_INVALID_PARAMETER;
|
---|
862 | return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
|
---|
863 | case VMMR0_DO_GMM_SEED_CHUNK:
|
---|
864 | if (pReqHdr)
|
---|
865 | return VERR_INVALID_PARAMETER;
|
---|
866 | return GMMR0SeedChunk(pVM, (RTR3PTR)u64Arg);
|
---|
867 |
|
---|
868 | /*
|
---|
869 | * A quick GCFGM mock-up.
|
---|
870 | */
|
---|
871 | /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
|
---|
872 | case VMMR0_DO_GCFGM_SET_VALUE:
|
---|
873 | case VMMR0_DO_GCFGM_QUERY_VALUE:
|
---|
874 | {
|
---|
875 | if (pVM || !pReqHdr || u64Arg)
|
---|
876 | return VERR_INVALID_PARAMETER;
|
---|
877 | PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
|
---|
878 | if (pReq->Hdr.cbReq != sizeof(*pReq))
|
---|
879 | return VERR_INVALID_PARAMETER;
|
---|
880 | int rc;
|
---|
881 | if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
|
---|
882 | {
|
---|
883 | rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
|
---|
884 | //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
885 | // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
|
---|
886 | }
|
---|
887 | else
|
---|
888 | {
|
---|
889 | rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
|
---|
890 | //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
891 | // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
|
---|
892 | }
|
---|
893 | return rc;
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | /*
|
---|
898 | * Requests to the internal networking service.
|
---|
899 | */
|
---|
900 | case VMMR0_DO_INTNET_OPEN:
|
---|
901 | {
|
---|
902 | PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
|
---|
903 | if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession))
|
---|
904 | return VERR_INVALID_PARAMETER;
|
---|
905 | if (!g_pIntNet)
|
---|
906 | return VERR_NOT_SUPPORTED;
|
---|
907 | return INTNETR0OpenReq(g_pIntNet, pSession, pReq);
|
---|
908 | }
|
---|
909 |
|
---|
910 | case VMMR0_DO_INTNET_IF_CLOSE:
|
---|
911 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession))
|
---|
912 | return VERR_INVALID_PARAMETER;
|
---|
913 | if (!g_pIntNet)
|
---|
914 | return VERR_NOT_SUPPORTED;
|
---|
915 | return INTNETR0IfCloseReq(g_pIntNet, pSession, (PINTNETIFCLOSEREQ)pReqHdr);
|
---|
916 |
|
---|
917 | case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
|
---|
918 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETRING3BUFFERREQ)pReqHdr)->pSession, pSession))
|
---|
919 | return VERR_INVALID_PARAMETER;
|
---|
920 | if (!g_pIntNet)
|
---|
921 | return VERR_NOT_SUPPORTED;
|
---|
922 | return INTNETR0IfGetRing3BufferReq(g_pIntNet, pSession, (PINTNETIFGETRING3BUFFERREQ)pReqHdr);
|
---|
923 |
|
---|
924 | case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
|
---|
925 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession))
|
---|
926 | return VERR_INVALID_PARAMETER;
|
---|
927 | if (!g_pIntNet)
|
---|
928 | return VERR_NOT_SUPPORTED;
|
---|
929 | return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
|
---|
930 |
|
---|
931 | case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
|
---|
932 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession))
|
---|
933 | return VERR_INVALID_PARAMETER;
|
---|
934 | if (!g_pIntNet)
|
---|
935 | return VERR_NOT_SUPPORTED;
|
---|
936 | return INTNETR0IfSetMacAddressReq(g_pIntNet, pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
|
---|
937 |
|
---|
938 | case VMMR0_DO_INTNET_IF_SET_ACTIVE:
|
---|
939 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession))
|
---|
940 | return VERR_INVALID_PARAMETER;
|
---|
941 | if (!g_pIntNet)
|
---|
942 | return VERR_NOT_SUPPORTED;
|
---|
943 | return INTNETR0IfSetActiveReq(g_pIntNet, pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
|
---|
944 |
|
---|
945 | case VMMR0_DO_INTNET_IF_SEND:
|
---|
946 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession))
|
---|
947 | return VERR_INVALID_PARAMETER;
|
---|
948 | if (!g_pIntNet)
|
---|
949 | return VERR_NOT_SUPPORTED;
|
---|
950 | return INTNETR0IfSendReq(g_pIntNet, pSession, (PINTNETIFSENDREQ)pReqHdr);
|
---|
951 |
|
---|
952 | case VMMR0_DO_INTNET_IF_WAIT:
|
---|
953 | if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession))
|
---|
954 | return VERR_INVALID_PARAMETER;
|
---|
955 | if (!g_pIntNet)
|
---|
956 | return VERR_NOT_SUPPORTED;
|
---|
957 | return INTNETR0IfWaitReq(g_pIntNet, pSession, (PINTNETIFWAITREQ)pReqHdr);
|
---|
958 |
|
---|
959 | /*
|
---|
960 | * For profiling.
|
---|
961 | */
|
---|
962 | case VMMR0_DO_NOP:
|
---|
963 | case VMMR0_DO_SLOW_NOP:
|
---|
964 | return VINF_SUCCESS;
|
---|
965 |
|
---|
966 | /*
|
---|
967 | * For testing Ring-0 APIs invoked in this environment.
|
---|
968 | */
|
---|
969 | case VMMR0_DO_TESTS:
|
---|
970 | /** @todo make new test */
|
---|
971 | return VINF_SUCCESS;
|
---|
972 |
|
---|
973 |
|
---|
974 | default:
|
---|
975 | /*
|
---|
976 | * We're returning VERR_NOT_SUPPORT here so we've got something else
|
---|
977 | * than -1 which the interrupt gate glue code might return.
|
---|
978 | */
|
---|
979 | Log(("operation %#x is not supported\n", enmOperation));
|
---|
980 | return VERR_NOT_SUPPORTED;
|
---|
981 | }
|
---|
982 | }
|
---|
983 |
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * Argument for vmmR0EntryExWrapper containing the argument s ofr VMMR0EntryEx.
|
---|
987 | */
|
---|
988 | typedef struct VMMR0ENTRYEXARGS
|
---|
989 | {
|
---|
990 | PVM pVM;
|
---|
991 | VMMR0OPERATION enmOperation;
|
---|
992 | PSUPVMMR0REQHDR pReq;
|
---|
993 | uint64_t u64Arg;
|
---|
994 | PSUPDRVSESSION pSession;
|
---|
995 | } VMMR0ENTRYEXARGS;
|
---|
996 | /** Pointer to a vmmR0EntryExWrapper argument package. */
|
---|
997 | typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
|
---|
998 |
|
---|
999 | /**
|
---|
1000 | * This is just a longjmp wrapper function for VMMR0EntryEx calls.
|
---|
1001 | *
|
---|
1002 | * @returns VBox status code.
|
---|
1003 | * @param pvArgs The argument package
|
---|
1004 | */
|
---|
1005 | static int vmmR0EntryExWrapper(void *pvArgs)
|
---|
1006 | {
|
---|
1007 | return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
|
---|
1008 | ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
|
---|
1009 | ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
|
---|
1010 | ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
|
---|
1011 | ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * The Ring 0 entry point, called by the support library (SUP).
|
---|
1017 | *
|
---|
1018 | * @returns VBox status code.
|
---|
1019 | * @param pVM The VM to operate on.
|
---|
1020 | * @param enmOperation Which operation to execute.
|
---|
1021 | * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
|
---|
1022 | * @param u64Arg Some simple constant argument.
|
---|
1023 | * @param pSession The session of the caller.
|
---|
1024 | * @remarks Assume called with interrupts _enabled_.
|
---|
1025 | */
|
---|
1026 | VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
|
---|
1027 | {
|
---|
1028 | /*
|
---|
1029 | * Requests that should only happen on the EMT thread will be
|
---|
1030 | * wrapped in a setjmp so we can assert without causing trouble.
|
---|
1031 | */
|
---|
1032 | if ( VALID_PTR(pVM)
|
---|
1033 | && pVM->pVMR0)
|
---|
1034 | {
|
---|
1035 | switch (enmOperation)
|
---|
1036 | {
|
---|
1037 | case VMMR0_DO_VMMR0_INIT:
|
---|
1038 | case VMMR0_DO_VMMR0_TERM:
|
---|
1039 | case VMMR0_DO_GMM_INITIAL_RESERVATION:
|
---|
1040 | case VMMR0_DO_GMM_UPDATE_RESERVATION:
|
---|
1041 | case VMMR0_DO_GMM_ALLOCATE_PAGES:
|
---|
1042 | case VMMR0_DO_GMM_FREE_PAGES:
|
---|
1043 | case VMMR0_DO_GMM_BALLOONED_PAGES:
|
---|
1044 | case VMMR0_DO_GMM_DEFLATED_BALLOON:
|
---|
1045 | case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
|
---|
1046 | case VMMR0_DO_GMM_SEED_CHUNK:
|
---|
1047 | {
|
---|
1048 | /** @todo validate this EMT claim... GVM knows. */
|
---|
1049 | VMMR0ENTRYEXARGS Args;
|
---|
1050 | Args.pVM = pVM;
|
---|
1051 | Args.enmOperation = enmOperation;
|
---|
1052 | Args.pReq = pReq;
|
---|
1053 | Args.u64Arg = u64Arg;
|
---|
1054 | Args.pSession = pSession;
|
---|
1055 | return vmmR0CallHostSetJmpEx(&pVM->vmm.s.CallHostR0JmpBuf, vmmR0EntryExWrapper, &Args);
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | default:
|
---|
1059 | break;
|
---|
1060 | }
|
---|
1061 | }
|
---|
1062 | return vmmR0EntryExWorker(pVM, enmOperation, pReq, u64Arg, pSession);
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | /**
|
---|
1067 | * Internal R0 logger worker: Flush logger.
|
---|
1068 | *
|
---|
1069 | * @param pLogger The logger instance to flush.
|
---|
1070 | * @remark This function must be exported!
|
---|
1071 | */
|
---|
1072 | VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
|
---|
1073 | {
|
---|
1074 | /*
|
---|
1075 | * Convert the pLogger into a VM handle and 'call' back to Ring-3.
|
---|
1076 | * (This is a bit paranoid code.)
|
---|
1077 | */
|
---|
1078 | PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
|
---|
1079 | if ( !VALID_PTR(pR0Logger)
|
---|
1080 | || !VALID_PTR(pR0Logger + 1)
|
---|
1081 | || !VALID_PTR(pLogger)
|
---|
1082 | || pLogger->u32Magic != RTLOGGER_MAGIC)
|
---|
1083 | {
|
---|
1084 | #ifdef DEBUG
|
---|
1085 | SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
|
---|
1086 | #endif
|
---|
1087 | return;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | PVM pVM = pR0Logger->pVM;
|
---|
1091 | if ( !VALID_PTR(pVM)
|
---|
1092 | || pVM->pVMR0 != pVM)
|
---|
1093 | {
|
---|
1094 | #ifdef DEBUG
|
---|
1095 | SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
|
---|
1096 | #endif
|
---|
1097 | return;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | /*
|
---|
1101 | * Check that the jump buffer is armed.
|
---|
1102 | */
|
---|
1103 | #ifdef RT_ARCH_X86
|
---|
1104 | if (!pVM->vmm.s.CallHostR0JmpBuf.eip)
|
---|
1105 | #else
|
---|
1106 | if (!pVM->vmm.s.CallHostR0JmpBuf.rip)
|
---|
1107 | #endif
|
---|
1108 | {
|
---|
1109 | #ifdef DEBUG
|
---|
1110 | SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
|
---|
1111 | #endif
|
---|
1112 | pLogger->offScratch = 0;
|
---|
1113 | return;
|
---|
1114 | }
|
---|
1115 | VMMR0CallHost(pVM, VMMCALLHOST_VMM_LOGGER_FLUSH, 0);
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 |
|
---|
1119 | /**
|
---|
1120 | * Jump back to ring-3 if we're the EMT and the longjmp is armed.
|
---|
1121 | *
|
---|
1122 | * @returns true if the breakpoint should be hit, false if it should be ignored.
|
---|
1123 | */
|
---|
1124 | DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
|
---|
1125 | {
|
---|
1126 | PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
|
---|
1127 | if (pVM)
|
---|
1128 | {
|
---|
1129 | #ifdef RT_ARCH_X86
|
---|
1130 | if (pVM->vmm.s.CallHostR0JmpBuf.eip)
|
---|
1131 | #else
|
---|
1132 | if (pVM->vmm.s.CallHostR0JmpBuf.rip)
|
---|
1133 | #endif
|
---|
1134 | {
|
---|
1135 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_VM_R0_ASSERTION, 0);
|
---|
1136 | return RT_FAILURE_NP(rc);
|
---|
1137 | }
|
---|
1138 | }
|
---|
1139 | #ifdef RT_OS_LINUX
|
---|
1140 | return true;
|
---|
1141 | #else
|
---|
1142 | return false;
|
---|
1143 | #endif
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 |
|
---|
1147 | /**
|
---|
1148 | * Override this so we can push it up to ring-3.
|
---|
1149 | *
|
---|
1150 | * @param pszExpr Expression. Can be NULL.
|
---|
1151 | * @param uLine Location line number.
|
---|
1152 | * @param pszFile Location file name.
|
---|
1153 | * @param pszFunction Location function name.
|
---|
1154 | */
|
---|
1155 | DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
1156 | {
|
---|
1157 | #ifndef DEBUG_sandervl
|
---|
1158 | SUPR0Printf("\n!!R0-Assertion Failed!!\n"
|
---|
1159 | "Expression: %s\n"
|
---|
1160 | "Location : %s(%d) %s\n",
|
---|
1161 | pszExpr, pszFile, uLine, pszFunction);
|
---|
1162 | #endif
|
---|
1163 | LogAlways(("\n!!R0-Assertion Failed!!\n"
|
---|
1164 | "Expression: %s\n"
|
---|
1165 | "Location : %s(%d) %s\n",
|
---|
1166 | pszExpr, pszFile, uLine, pszFunction));
|
---|
1167 |
|
---|
1168 | PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
|
---|
1169 | if (pVM)
|
---|
1170 | RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
|
---|
1171 | "\n!!R0-Assertion Failed!!\n"
|
---|
1172 | "Expression: %s\n"
|
---|
1173 | "Location : %s(%d) %s\n",
|
---|
1174 | pszExpr, pszFile, uLine, pszFunction);
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 |
|
---|
1178 | /**
|
---|
1179 | * Callback for RTLogFormatV which writes to the ring-3 log port.
|
---|
1180 | * See PFNLOGOUTPUT() for details.
|
---|
1181 | */
|
---|
1182 | static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
|
---|
1183 | {
|
---|
1184 | for (size_t i = 0; i < cbChars; i++)
|
---|
1185 | {
|
---|
1186 | #ifndef DEBUG_sandervl
|
---|
1187 | SUPR0Printf("%c", pachChars[i]);
|
---|
1188 | #endif
|
---|
1189 | LogAlways(("%c", pachChars[i]));
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | return cbChars;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 |
|
---|
1196 | DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
|
---|
1197 | {
|
---|
1198 | PRTLOGGER pLog = RTLogDefaultInstance(); /** @todo we want this for release as well! */
|
---|
1199 | if (pLog)
|
---|
1200 | {
|
---|
1201 | va_list va;
|
---|
1202 | va_start(va, pszFormat);
|
---|
1203 | RTLogFormatV(rtLogOutput, pLog, pszFormat, va);
|
---|
1204 | va_end(va);
|
---|
1205 |
|
---|
1206 | PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
|
---|
1207 | if (pVM)
|
---|
1208 | {
|
---|
1209 | va_start(va, pszFormat);
|
---|
1210 | RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, va);
|
---|
1211 | va_end(va);
|
---|
1212 | }
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 |
|
---|