VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/VMReq.cpp@ 74795

Last change on this file since 74795 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 50.0 KB
Line 
1/* $Id: VMReq.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * VM - Virtual Machine
4 */
5
6/*
7 * Copyright (C) 2006-2017 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_VM
23#include <VBox/vmm/mm.h>
24#include <VBox/vmm/vmm.h>
25#include "VMInternal.h"
26#include <VBox/vmm/vm.h>
27#include <VBox/vmm/uvm.h>
28
29#include <VBox/err.h>
30#include <VBox/param.h>
31#include <VBox/log.h>
32#include <iprt/assert.h>
33#include <iprt/asm.h>
34#include <iprt/string.h>
35#include <iprt/time.h>
36#include <iprt/semaphore.h>
37#include <iprt/thread.h>
38
39
40/*********************************************************************************************************************************
41* Internal Functions *
42*********************************************************************************************************************************/
43static int vmR3ReqProcessOne(PVMREQ pReq);
44
45
46/**
47 * Convenience wrapper for VMR3ReqCallU.
48 *
49 * This assumes (1) you're calling a function that returns an VBox status code,
50 * (2) that you want it's return code on success, and (3) that you wish to wait
51 * for ever for it to return.
52 *
53 * @returns VBox status code. In the unlikely event that VMR3ReqCallVU fails,
54 * its status code is return. Otherwise, the status of pfnFunction is
55 * returned.
56 *
57 * @param pVM The cross context VM structure.
58 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
59 * one of the following special values:
60 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
61 * @param pfnFunction Pointer to the function to call.
62 * @param cArgs Number of arguments following in the ellipsis.
63 * @param ... Function arguments.
64 *
65 * @remarks See remarks on VMR3ReqCallVU.
66 * @internal
67 */
68VMMR3_INT_DECL(int) VMR3ReqCallWait(PVM pVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
69{
70 PVMREQ pReq;
71 va_list va;
72 va_start(va, cArgs);
73 int rc = VMR3ReqCallVU(pVM->pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS,
74 pfnFunction, cArgs, va);
75 va_end(va);
76 if (RT_SUCCESS(rc))
77 rc = pReq->iStatus;
78 VMR3ReqFree(pReq);
79 return rc;
80}
81
82
83/**
84 * Convenience wrapper for VMR3ReqCallU.
85 *
86 * This assumes (1) you're calling a function that returns an VBox status code,
87 * (2) that you want it's return code on success, and (3) that you wish to wait
88 * for ever for it to return.
89 *
90 * @returns VBox status code. In the unlikely event that VMR3ReqCallVU fails,
91 * its status code is return. Otherwise, the status of pfnFunction is
92 * returned.
93 *
94 * @param pUVM The user mode VM structure.
95 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
96 * one of the following special values:
97 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
98 * @param pfnFunction Pointer to the function to call.
99 * @param cArgs Number of arguments following in the ellipsis.
100 * @param ... Function arguments.
101 *
102 * @remarks See remarks on VMR3ReqCallVU.
103 * @internal
104 */
105VMMR3DECL(int) VMR3ReqCallWaitU(PUVM pUVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
106{
107 PVMREQ pReq;
108 va_list va;
109 va_start(va, cArgs);
110 int rc = VMR3ReqCallVU(pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS,
111 pfnFunction, cArgs, va);
112 va_end(va);
113 if (RT_SUCCESS(rc))
114 rc = pReq->iStatus;
115 VMR3ReqFree(pReq);
116 return rc;
117}
118
119
120/**
121 * Convenience wrapper for VMR3ReqCallU.
122 *
123 * This assumes (1) you're calling a function that returns an VBox status code
124 * and that you do not wish to wait for it to complete.
125 *
126 * @returns VBox status code returned by VMR3ReqCallVU.
127 *
128 * @param pVM The cross context VM structure.
129 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
130 * one of the following special values:
131 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
132 * @param pfnFunction Pointer to the function to call.
133 * @param cArgs Number of arguments following in the ellipsis.
134 * @param ... Function arguments.
135 *
136 * @remarks See remarks on VMR3ReqCallVU.
137 * @internal
138 */
139VMMR3DECL(int) VMR3ReqCallNoWait(PVM pVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
140{
141 va_list va;
142 va_start(va, cArgs);
143 int rc = VMR3ReqCallVU(pVM->pUVM, idDstCpu, NULL, 0, VMREQFLAGS_VBOX_STATUS | VMREQFLAGS_NO_WAIT,
144 pfnFunction, cArgs, va);
145 va_end(va);
146 return rc;
147}
148
149
150/**
151 * Convenience wrapper for VMR3ReqCallU.
152 *
153 * This assumes (1) you're calling a function that returns an VBox status code
154 * and that you do not wish to wait for it to complete.
155 *
156 * @returns VBox status code returned by VMR3ReqCallVU.
157 *
158 * @param pUVM Pointer to the VM.
159 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
160 * one of the following special values:
161 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
162 * @param pfnFunction Pointer to the function to call.
163 * @param cArgs Number of arguments following in the ellipsis.
164 * @param ... Function arguments.
165 *
166 * @remarks See remarks on VMR3ReqCallVU.
167 */
168VMMR3DECL(int) VMR3ReqCallNoWaitU(PUVM pUVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
169{
170 va_list va;
171 va_start(va, cArgs);
172 int rc = VMR3ReqCallVU(pUVM, idDstCpu, NULL, 0, VMREQFLAGS_VBOX_STATUS | VMREQFLAGS_NO_WAIT,
173 pfnFunction, cArgs, va);
174 va_end(va);
175 return rc;
176}
177
178
179/**
180 * Convenience wrapper for VMR3ReqCallU.
181 *
182 * This assumes (1) you're calling a function that returns void, and (2) that
183 * you wish to wait for ever for it to return.
184 *
185 * @returns VBox status code of VMR3ReqCallVU.
186 *
187 * @param pVM The cross context VM structure.
188 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
189 * one of the following special values:
190 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
191 * @param pfnFunction Pointer to the function to call.
192 * @param cArgs Number of arguments following in the ellipsis.
193 * @param ... Function arguments.
194 *
195 * @remarks See remarks on VMR3ReqCallVU.
196 * @internal
197 */
198VMMR3_INT_DECL(int) VMR3ReqCallVoidWait(PVM pVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
199{
200 PVMREQ pReq;
201 va_list va;
202 va_start(va, cArgs);
203 int rc = VMR3ReqCallVU(pVM->pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VOID,
204 pfnFunction, cArgs, va);
205 va_end(va);
206 VMR3ReqFree(pReq);
207 return rc;
208}
209
210
211/**
212 * Convenience wrapper for VMR3ReqCallU.
213 *
214 * This assumes (1) you're calling a function that returns void, and (2) that
215 * you wish to wait for ever for it to return.
216 *
217 * @returns VBox status code of VMR3ReqCallVU.
218 *
219 * @param pUVM Pointer to the VM.
220 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
221 * one of the following special values:
222 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
223 * @param pfnFunction Pointer to the function to call.
224 * @param cArgs Number of arguments following in the ellipsis.
225 * @param ... Function arguments.
226 *
227 * @remarks See remarks on VMR3ReqCallVU.
228 */
229VMMR3DECL(int) VMR3ReqCallVoidWaitU(PUVM pUVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
230{
231 PVMREQ pReq;
232 va_list va;
233 va_start(va, cArgs);
234 int rc = VMR3ReqCallVU(pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VOID,
235 pfnFunction, cArgs, va);
236 va_end(va);
237 VMR3ReqFree(pReq);
238 return rc;
239}
240
241
242/**
243 * Convenience wrapper for VMR3ReqCallU.
244 *
245 * This assumes (1) you're calling a function that returns void, and (2) that
246 * you do not wish to wait for it to complete.
247 *
248 * @returns VBox status code of VMR3ReqCallVU.
249 *
250 * @param pVM The cross context VM structure.
251 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
252 * one of the following special values:
253 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
254 * @param pfnFunction Pointer to the function to call.
255 * @param cArgs Number of arguments following in the ellipsis.
256 * @param ... Function arguments.
257 *
258 * @remarks See remarks on VMR3ReqCallVU.
259 * @internal
260 */
261VMMR3DECL(int) VMR3ReqCallVoidNoWait(PVM pVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
262{
263 PVMREQ pReq;
264 va_list va;
265 va_start(va, cArgs);
266 int rc = VMR3ReqCallVU(pVM->pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VOID | VMREQFLAGS_NO_WAIT,
267 pfnFunction, cArgs, va);
268 va_end(va);
269 VMR3ReqFree(pReq);
270 return rc;
271}
272
273
274/**
275 * Convenience wrapper for VMR3ReqCallU.
276 *
277 * This assumes (1) you're calling a function that returns an VBox status code,
278 * (2) that you want it's return code on success, (3) that you wish to wait for
279 * ever for it to return, and (4) that it's priority request that can be safely
280 * be handled during async suspend and power off.
281 *
282 * @returns VBox status code. In the unlikely event that VMR3ReqCallVU fails,
283 * its status code is return. Otherwise, the status of pfnFunction is
284 * returned.
285 *
286 * @param pVM The cross context VM structure.
287 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
288 * one of the following special values:
289 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
290 * @param pfnFunction Pointer to the function to call.
291 * @param cArgs Number of arguments following in the ellipsis.
292 * @param ... Function arguments.
293 *
294 * @remarks See remarks on VMR3ReqCallVU.
295 * @internal
296 */
297VMMR3DECL(int) VMR3ReqPriorityCallWait(PVM pVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
298{
299 PVMREQ pReq;
300 va_list va;
301 va_start(va, cArgs);
302 int rc = VMR3ReqCallVU(pVM->pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS | VMREQFLAGS_PRIORITY,
303 pfnFunction, cArgs, va);
304 va_end(va);
305 if (RT_SUCCESS(rc))
306 rc = pReq->iStatus;
307 VMR3ReqFree(pReq);
308 return rc;
309}
310
311
312/**
313 * Convenience wrapper for VMR3ReqCallU.
314 *
315 * This assumes (1) you're calling a function that returns an VBox status code,
316 * (2) that you want it's return code on success, (3) that you wish to wait for
317 * ever for it to return, and (4) that it's priority request that can be safely
318 * be handled during async suspend and power off.
319 *
320 * @returns VBox status code. In the unlikely event that VMR3ReqCallVU fails,
321 * its status code is return. Otherwise, the status of pfnFunction is
322 * returned.
323 *
324 * @param pUVM The user mode VM handle.
325 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
326 * one of the following special values:
327 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
328 * @param pfnFunction Pointer to the function to call.
329 * @param cArgs Number of arguments following in the ellipsis.
330 * @param ... Function arguments.
331 *
332 * @remarks See remarks on VMR3ReqCallVU.
333 */
334VMMR3DECL(int) VMR3ReqPriorityCallWaitU(PUVM pUVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
335{
336 PVMREQ pReq;
337 va_list va;
338 va_start(va, cArgs);
339 int rc = VMR3ReqCallVU(pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VBOX_STATUS | VMREQFLAGS_PRIORITY,
340 pfnFunction, cArgs, va);
341 va_end(va);
342 if (RT_SUCCESS(rc))
343 rc = pReq->iStatus;
344 VMR3ReqFree(pReq);
345 return rc;
346}
347
348
349/**
350 * Convenience wrapper for VMR3ReqCallU.
351 *
352 * This assumes (1) you're calling a function that returns void, (2) that you
353 * wish to wait for ever for it to return, and (3) that it's priority request
354 * that can be safely be handled during async suspend and power off.
355 *
356 * @returns VBox status code of VMR3ReqCallVU.
357 *
358 * @param pUVM The user mode VM handle.
359 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
360 * one of the following special values:
361 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
362 * @param pfnFunction Pointer to the function to call.
363 * @param cArgs Number of arguments following in the ellipsis.
364 * @param ... Function arguments.
365 *
366 * @remarks See remarks on VMR3ReqCallVU.
367 */
368VMMR3DECL(int) VMR3ReqPriorityCallVoidWaitU(PUVM pUVM, VMCPUID idDstCpu, PFNRT pfnFunction, unsigned cArgs, ...)
369{
370 PVMREQ pReq;
371 va_list va;
372 va_start(va, cArgs);
373 int rc = VMR3ReqCallVU(pUVM, idDstCpu, &pReq, RT_INDEFINITE_WAIT, VMREQFLAGS_VOID | VMREQFLAGS_PRIORITY,
374 pfnFunction, cArgs, va);
375 va_end(va);
376 VMR3ReqFree(pReq);
377 return rc;
378}
379
380
381/**
382 * Allocate and queue a call request to a void function.
383 *
384 * If it's desired to poll on the completion of the request set cMillies
385 * to 0 and use VMR3ReqWait() to check for completion. In the other case
386 * use RT_INDEFINITE_WAIT.
387 * The returned request packet must be freed using VMR3ReqFree().
388 *
389 * @returns VBox status code.
390 * Will not return VERR_INTERRUPTED.
391 * @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
392 *
393 * @param pUVM Pointer to the user mode VM structure.
394 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
395 * one of the following special values:
396 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
397 * @param ppReq Where to store the pointer to the request.
398 * This will be NULL or a valid request pointer not matter what happens, unless fFlags
399 * contains VMREQFLAGS_NO_WAIT when it will be optional and always NULL.
400 * @param cMillies Number of milliseconds to wait for the request to
401 * be completed. Use RT_INDEFINITE_WAIT to only
402 * wait till it's completed.
403 * @param fFlags A combination of the VMREQFLAGS values.
404 * @param pfnFunction Pointer to the function to call.
405 * @param cArgs Number of arguments following in the ellipsis.
406 * @param ... Function arguments.
407 *
408 * @remarks See remarks on VMR3ReqCallVU.
409 */
410VMMR3DECL(int) VMR3ReqCallU(PUVM pUVM, VMCPUID idDstCpu, PVMREQ *ppReq, RTMSINTERVAL cMillies, uint32_t fFlags,
411 PFNRT pfnFunction, unsigned cArgs, ...)
412{
413 va_list va;
414 va_start(va, cArgs);
415 int rc = VMR3ReqCallVU(pUVM, idDstCpu, ppReq, cMillies, fFlags, pfnFunction, cArgs, va);
416 va_end(va);
417 return rc;
418}
419
420
421/**
422 * Allocate and queue a call request.
423 *
424 * If it's desired to poll on the completion of the request set cMillies
425 * to 0 and use VMR3ReqWait() to check for completion. In the other case
426 * use RT_INDEFINITE_WAIT.
427 * The returned request packet must be freed using VMR3ReqFree().
428 *
429 * @returns VBox status code.
430 * Will not return VERR_INTERRUPTED.
431 * @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
432 *
433 * @param pUVM Pointer to the user mode VM structure.
434 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
435 * one of the following special values:
436 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
437 * @param ppReq Where to store the pointer to the request.
438 * This will be NULL or a valid request pointer not matter what happens, unless fFlags
439 * contains VMREQFLAGS_NO_WAIT when it will be optional and always NULL.
440 * @param cMillies Number of milliseconds to wait for the request to
441 * be completed. Use RT_INDEFINITE_WAIT to only
442 * wait till it's completed.
443 * @param pfnFunction Pointer to the function to call.
444 * @param fFlags A combination of the VMREQFLAGS values.
445 * @param cArgs Number of arguments following in the ellipsis.
446 * Stuff which differs in size from uintptr_t is gonna make trouble, so don't try!
447 * @param Args Argument vector.
448 *
449 * @remarks Caveats:
450 * - Do not pass anything which is larger than an uintptr_t.
451 * - 64-bit integers are larger than uintptr_t on 32-bit hosts.
452 * Pass integers > 32-bit by reference (pointers).
453 * - Don't use NULL since it should be the integer 0 in C++ and may
454 * therefore end up with garbage in the bits 63:32 on 64-bit
455 * hosts because 'int' is 32-bit.
456 * Use (void *)NULL or (uintptr_t)0 instead of NULL.
457 */
458VMMR3DECL(int) VMR3ReqCallVU(PUVM pUVM, VMCPUID idDstCpu, PVMREQ *ppReq, RTMSINTERVAL cMillies, uint32_t fFlags,
459 PFNRT pfnFunction, unsigned cArgs, va_list Args)
460{
461 LogFlow(("VMR3ReqCallV: idDstCpu=%u cMillies=%d fFlags=%#x pfnFunction=%p cArgs=%d\n", idDstCpu, cMillies, fFlags, pfnFunction, cArgs));
462
463 /*
464 * Validate input.
465 */
466 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
467 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
468 AssertReturn(!(fFlags & ~(VMREQFLAGS_RETURN_MASK | VMREQFLAGS_NO_WAIT | VMREQFLAGS_POKE | VMREQFLAGS_PRIORITY)), VERR_INVALID_PARAMETER);
469 if (!(fFlags & VMREQFLAGS_NO_WAIT) || ppReq)
470 {
471 AssertPtrReturn(ppReq, VERR_INVALID_POINTER);
472 *ppReq = NULL;
473 }
474 PVMREQ pReq = NULL;
475 AssertMsgReturn(cArgs * sizeof(uintptr_t) <= sizeof(pReq->u.Internal.aArgs),
476 ("cArg=%d\n", cArgs),
477 VERR_TOO_MUCH_DATA);
478
479 /*
480 * Allocate request
481 */
482 int rc = VMR3ReqAlloc(pUVM, &pReq, VMREQTYPE_INTERNAL, idDstCpu);
483 if (RT_FAILURE(rc))
484 return rc;
485
486 /*
487 * Initialize the request data.
488 */
489 pReq->fFlags = fFlags;
490 pReq->u.Internal.pfn = pfnFunction;
491 pReq->u.Internal.cArgs = cArgs;
492 for (unsigned iArg = 0; iArg < cArgs; iArg++)
493 pReq->u.Internal.aArgs[iArg] = va_arg(Args, uintptr_t);
494
495 /*
496 * Queue the request and return.
497 */
498 rc = VMR3ReqQueue(pReq, cMillies);
499 if ( RT_FAILURE(rc)
500 && rc != VERR_TIMEOUT)
501 {
502 VMR3ReqFree(pReq);
503 pReq = NULL;
504 }
505 if (!(fFlags & VMREQFLAGS_NO_WAIT))
506 {
507 *ppReq = pReq;
508 LogFlow(("VMR3ReqCallV: returns %Rrc *ppReq=%p\n", rc, pReq));
509 }
510 else
511 LogFlow(("VMR3ReqCallV: returns %Rrc\n", rc));
512 Assert(rc != VERR_INTERRUPTED);
513 return rc;
514}
515
516
517/**
518 * Joins the list pList with whatever is linked up at *pHead.
519 */
520static void vmr3ReqJoinFreeSub(volatile PVMREQ *ppHead, PVMREQ pList)
521{
522 for (unsigned cIterations = 0;; cIterations++)
523 {
524 PVMREQ pHead = ASMAtomicXchgPtrT(ppHead, pList, PVMREQ);
525 if (!pHead)
526 return;
527 PVMREQ pTail = pHead;
528 while (pTail->pNext)
529 pTail = pTail->pNext;
530 ASMAtomicWritePtr(&pTail->pNext, pList);
531 ASMCompilerBarrier();
532 if (ASMAtomicCmpXchgPtr(ppHead, pHead, pList))
533 return;
534 ASMAtomicWriteNullPtr(&pTail->pNext);
535 ASMCompilerBarrier();
536 if (ASMAtomicCmpXchgPtr(ppHead, pHead, NULL))
537 return;
538 pList = pHead;
539 Assert(cIterations != 32);
540 Assert(cIterations != 64);
541 }
542}
543
544
545/**
546 * Joins the list pList with whatever is linked up at *pHead.
547 */
548static void vmr3ReqJoinFree(PVMINTUSERPERVM pVMInt, PVMREQ pList)
549{
550 /*
551 * Split the list if it's too long.
552 */
553 unsigned cReqs = 1;
554 PVMREQ pTail = pList;
555 while (pTail->pNext)
556 {
557 if (cReqs++ > 25)
558 {
559 const uint32_t i = pVMInt->iReqFree;
560 vmr3ReqJoinFreeSub(&pVMInt->apReqFree[(i + 2) % RT_ELEMENTS(pVMInt->apReqFree)], pTail->pNext);
561
562 pTail->pNext = NULL;
563 vmr3ReqJoinFreeSub(&pVMInt->apReqFree[(i + 2 + (i == pVMInt->iReqFree)) % RT_ELEMENTS(pVMInt->apReqFree)], pTail->pNext);
564 return;
565 }
566 pTail = pTail->pNext;
567 }
568 vmr3ReqJoinFreeSub(&pVMInt->apReqFree[(pVMInt->iReqFree + 2) % RT_ELEMENTS(pVMInt->apReqFree)], pList);
569}
570
571
572/**
573 * Allocates a request packet.
574 *
575 * The caller allocates a request packet, fills in the request data
576 * union and queues the request.
577 *
578 * @returns VBox status code.
579 *
580 * @param pUVM Pointer to the user mode VM structure.
581 * @param ppReq Where to store the pointer to the allocated packet.
582 * @param enmType Package type.
583 * @param idDstCpu The destination CPU(s). Either a specific CPU ID or
584 * one of the following special values:
585 * VMCPUID_ANY, VMCPUID_ANY_QUEUE, VMCPUID_ALL or VMCPUID_ALL_REVERSE.
586 */
587VMMR3DECL(int) VMR3ReqAlloc(PUVM pUVM, PVMREQ *ppReq, VMREQTYPE enmType, VMCPUID idDstCpu)
588{
589 /*
590 * Validate input.
591 */
592 AssertMsgReturn(enmType > VMREQTYPE_INVALID && enmType < VMREQTYPE_MAX,
593 ("Invalid package type %d valid range %d-%d inclusively.\n",
594 enmType, VMREQTYPE_INVALID + 1, VMREQTYPE_MAX - 1),
595 VERR_VM_REQUEST_INVALID_TYPE);
596 AssertPtrReturn(ppReq, VERR_INVALID_POINTER);
597 AssertMsgReturn( idDstCpu == VMCPUID_ANY
598 || idDstCpu == VMCPUID_ANY_QUEUE
599 || idDstCpu < pUVM->cCpus
600 || idDstCpu == VMCPUID_ALL
601 || idDstCpu == VMCPUID_ALL_REVERSE,
602 ("Invalid destination %u (max=%u)\n", idDstCpu, pUVM->cCpus), VERR_INVALID_PARAMETER);
603
604 /*
605 * Try get a recycled packet.
606 * While this could all be solved with a single list with a lock, it's a sport
607 * of mine to avoid locks.
608 */
609 int cTries = RT_ELEMENTS(pUVM->vm.s.apReqFree) * 2;
610 while (--cTries >= 0)
611 {
612 PVMREQ volatile *ppHead = &pUVM->vm.s.apReqFree[ASMAtomicIncU32(&pUVM->vm.s.iReqFree) % RT_ELEMENTS(pUVM->vm.s.apReqFree)];
613#if 0 /* sad, but this won't work safely because the reading of pReq->pNext. */
614 PVMREQ pNext = NULL;
615 PVMREQ pReq = *ppHead;
616 if ( pReq
617 && !ASMAtomicCmpXchgPtr(ppHead, (pNext = pReq->pNext), pReq)
618 && (pReq = *ppHead)
619 && !ASMAtomicCmpXchgPtr(ppHead, (pNext = pReq->pNext), pReq))
620 pReq = NULL;
621 if (pReq)
622 {
623 Assert(pReq->pNext == pNext); NOREF(pReq);
624#else
625 PVMREQ pReq = ASMAtomicXchgPtrT(ppHead, NULL, PVMREQ);
626 if (pReq)
627 {
628 PVMREQ pNext = pReq->pNext;
629 if ( pNext
630 && !ASMAtomicCmpXchgPtr(ppHead, pNext, NULL))
631 {
632 STAM_COUNTER_INC(&pUVM->vm.s.StatReqAllocRaces);
633 vmr3ReqJoinFree(&pUVM->vm.s, pReq->pNext);
634 }
635#endif
636 ASMAtomicDecU32(&pUVM->vm.s.cReqFree);
637
638 /*
639 * Make sure the event sem is not signaled.
640 */
641 if (!pReq->fEventSemClear)
642 {
643 int rc = RTSemEventWait(pReq->EventSem, 0);
644 if (rc != VINF_SUCCESS && rc != VERR_TIMEOUT)
645 {
646 /*
647 * This shall not happen, but if it does we'll just destroy
648 * the semaphore and create a new one.
649 */
650 AssertMsgFailed(("rc=%Rrc from RTSemEventWait(%#x).\n", rc, pReq->EventSem));
651 RTSemEventDestroy(pReq->EventSem);
652 rc = RTSemEventCreate(&pReq->EventSem);
653 AssertRC(rc);
654 if (RT_FAILURE(rc))
655 return rc;
656#if 0 /// @todo @bugref{4725} - def RT_LOCK_STRICT
657 for (VMCPUID idCpu = 0; idCpu < pUVM->cCpus; idCpu++)
658 RTSemEventAddSignaller(pReq->EventSem, pUVM->aCpus[idCpu].vm.s.ThreadEMT);
659#endif
660 }
661 pReq->fEventSemClear = true;
662 }
663 else
664 Assert(RTSemEventWait(pReq->EventSem, 0) == VERR_TIMEOUT);
665
666 /*
667 * Initialize the packet and return it.
668 */
669 Assert(pReq->enmType == VMREQTYPE_INVALID);
670 Assert(pReq->enmState == VMREQSTATE_FREE);
671 Assert(pReq->pUVM == pUVM);
672 ASMAtomicXchgSize(&pReq->pNext, NULL);
673 pReq->enmState = VMREQSTATE_ALLOCATED;
674 pReq->iStatus = VERR_VM_REQUEST_STATUS_STILL_PENDING;
675 pReq->fFlags = VMREQFLAGS_VBOX_STATUS;
676 pReq->enmType = enmType;
677 pReq->idDstCpu = idDstCpu;
678
679 *ppReq = pReq;
680 STAM_COUNTER_INC(&pUVM->vm.s.StatReqAllocRecycled);
681 LogFlow(("VMR3ReqAlloc: returns VINF_SUCCESS *ppReq=%p recycled\n", pReq));
682 return VINF_SUCCESS;
683 }
684 }
685
686 /*
687 * Ok allocate one.
688 */
689 PVMREQ pReq = (PVMREQ)MMR3HeapAllocU(pUVM, MM_TAG_VM_REQ, sizeof(*pReq));
690 if (!pReq)
691 return VERR_NO_MEMORY;
692
693 /*
694 * Create the semaphore.
695 */
696 int rc = RTSemEventCreate(&pReq->EventSem);
697 AssertRC(rc);
698 if (RT_FAILURE(rc))
699 {
700 MMR3HeapFree(pReq);
701 return rc;
702 }
703#if 0 /// @todo @bugref{4725} - def RT_LOCK_STRICT
704 for (VMCPUID idCpu = 0; idCpu < pUVM->cCpus; idCpu++)
705 RTSemEventAddSignaller(pReq->EventSem, pUVM->aCpus[idCpu].vm.s.ThreadEMT);
706#endif
707
708 /*
709 * Initialize the packet and return it.
710 */
711 pReq->pNext = NULL;
712 pReq->pUVM = pUVM;
713 pReq->enmState = VMREQSTATE_ALLOCATED;
714 pReq->iStatus = VERR_VM_REQUEST_STATUS_STILL_PENDING;
715 pReq->fEventSemClear = true;
716 pReq->fFlags = VMREQFLAGS_VBOX_STATUS;
717 pReq->enmType = enmType;
718 pReq->idDstCpu = idDstCpu;
719
720 *ppReq = pReq;
721 STAM_COUNTER_INC(&pUVM->vm.s.StatReqAllocNew);
722 LogFlow(("VMR3ReqAlloc: returns VINF_SUCCESS *ppReq=%p new\n", pReq));
723 return VINF_SUCCESS;
724}
725
726
727/**
728 * Free a request packet.
729 *
730 * @returns VBox status code.
731 *
732 * @param pReq Package to free.
733 * @remark The request packet must be in allocated or completed state!
734 */
735VMMR3DECL(int) VMR3ReqFree(PVMREQ pReq)
736{
737 /*
738 * Ignore NULL (all free functions should do this imho).
739 */
740 if (!pReq)
741 return VINF_SUCCESS;
742
743 /*
744 * Check packet state.
745 */
746 switch (pReq->enmState)
747 {
748 case VMREQSTATE_ALLOCATED:
749 case VMREQSTATE_COMPLETED:
750 break;
751 default:
752 AssertMsgFailed(("Invalid state %d!\n", pReq->enmState));
753 return VERR_VM_REQUEST_STATE;
754 }
755
756 /*
757 * Make it a free packet and put it into one of the free packet lists.
758 */
759 pReq->enmState = VMREQSTATE_FREE;
760 pReq->iStatus = VERR_VM_REQUEST_STATUS_FREED;
761 pReq->enmType = VMREQTYPE_INVALID;
762
763 PUVM pUVM = pReq->pUVM;
764 STAM_COUNTER_INC(&pUVM->vm.s.StatReqFree);
765
766 if (pUVM->vm.s.cReqFree < 128)
767 {
768 ASMAtomicIncU32(&pUVM->vm.s.cReqFree);
769 PVMREQ volatile *ppHead = &pUVM->vm.s.apReqFree[ASMAtomicIncU32(&pUVM->vm.s.iReqFree) % RT_ELEMENTS(pUVM->vm.s.apReqFree)];
770 PVMREQ pNext;
771 do
772 {
773 pNext = ASMAtomicUoReadPtrT(ppHead, PVMREQ);
774 ASMAtomicWritePtr(&pReq->pNext, pNext);
775 ASMCompilerBarrier();
776 } while (!ASMAtomicCmpXchgPtr(ppHead, pReq, pNext));
777 }
778 else
779 {
780 STAM_COUNTER_INC(&pReq->pUVM->vm.s.StatReqFreeOverflow);
781 RTSemEventDestroy(pReq->EventSem);
782 MMR3HeapFree(pReq);
783 }
784 return VINF_SUCCESS;
785}
786
787
788/**
789 * Queue a request.
790 *
791 * The quest must be allocated using VMR3ReqAlloc() and contain
792 * all the required data.
793 * If it's desired to poll on the completion of the request set cMillies
794 * to 0 and use VMR3ReqWait() to check for completion. In the other case
795 * use RT_INDEFINITE_WAIT.
796 *
797 * @returns VBox status code.
798 * Will not return VERR_INTERRUPTED.
799 * @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
800 *
801 * @param pReq The request to queue.
802 * @param cMillies Number of milliseconds to wait for the request to
803 * be completed. Use RT_INDEFINITE_WAIT to only
804 * wait till it's completed.
805 */
806VMMR3DECL(int) VMR3ReqQueue(PVMREQ pReq, RTMSINTERVAL cMillies)
807{
808 LogFlow(("VMR3ReqQueue: pReq=%p cMillies=%d\n", pReq, cMillies));
809 /*
810 * Verify the supplied package.
811 */
812 AssertMsgReturn(pReq->enmState == VMREQSTATE_ALLOCATED, ("%d\n", pReq->enmState), VERR_VM_REQUEST_STATE);
813 AssertMsgReturn( VALID_PTR(pReq->pUVM)
814 && !pReq->pNext
815 && pReq->EventSem != NIL_RTSEMEVENT,
816 ("Invalid request package! Anyone cooking their own packages???\n"),
817 VERR_VM_REQUEST_INVALID_PACKAGE);
818 AssertMsgReturn( pReq->enmType > VMREQTYPE_INVALID
819 && pReq->enmType < VMREQTYPE_MAX,
820 ("Invalid package type %d valid range %d-%d inclusively. This was verified on alloc too...\n",
821 pReq->enmType, VMREQTYPE_INVALID + 1, VMREQTYPE_MAX - 1),
822 VERR_VM_REQUEST_INVALID_TYPE);
823 Assert(!(pReq->fFlags & ~(VMREQFLAGS_RETURN_MASK | VMREQFLAGS_NO_WAIT | VMREQFLAGS_POKE | VMREQFLAGS_PRIORITY)));
824
825 /*
826 * Are we the EMT or not?
827 * Also, store pVM (and fFlags) locally since pReq may be invalid after queuing it.
828 */
829 int rc = VINF_SUCCESS;
830 PUVM pUVM = ((VMREQ volatile *)pReq)->pUVM; /* volatile paranoia */
831 PUVMCPU pUVCpu = (PUVMCPU)RTTlsGet(pUVM->vm.s.idxTLS);
832
833 if (pReq->idDstCpu == VMCPUID_ALL)
834 {
835 /* One-by-one. */
836 Assert(!(pReq->fFlags & VMREQFLAGS_NO_WAIT));
837 for (unsigned i = 0; i < pUVM->cCpus; i++)
838 {
839 /* Reinit some members. */
840 pReq->enmState = VMREQSTATE_ALLOCATED;
841 pReq->idDstCpu = i;
842 rc = VMR3ReqQueue(pReq, cMillies);
843 if (RT_FAILURE(rc))
844 break;
845 }
846 }
847 else if (pReq->idDstCpu == VMCPUID_ALL_REVERSE)
848 {
849 /* One-by-one. */
850 Assert(!(pReq->fFlags & VMREQFLAGS_NO_WAIT));
851 for (int i = pUVM->cCpus-1; i >= 0; i--)
852 {
853 /* Reinit some members. */
854 pReq->enmState = VMREQSTATE_ALLOCATED;
855 pReq->idDstCpu = i;
856 rc = VMR3ReqQueue(pReq, cMillies);
857 if (RT_FAILURE(rc))
858 break;
859 }
860 }
861 else if ( pReq->idDstCpu != VMCPUID_ANY /* for a specific VMCPU? */
862 && pReq->idDstCpu != VMCPUID_ANY_QUEUE
863 && ( !pUVCpu /* and it's not the current thread. */
864 || pUVCpu->idCpu != pReq->idDstCpu))
865 {
866 VMCPUID idTarget = pReq->idDstCpu; Assert(idTarget < pUVM->cCpus);
867 PVMCPU pVCpu = &pUVM->pVM->aCpus[idTarget];
868 unsigned fFlags = ((VMREQ volatile *)pReq)->fFlags; /* volatile paranoia */
869
870 /* Fetch the right UVMCPU */
871 pUVCpu = &pUVM->aCpus[idTarget];
872
873 /*
874 * Insert it.
875 */
876 volatile PVMREQ *ppQueueHead = pReq->fFlags & VMREQFLAGS_PRIORITY ? &pUVCpu->vm.s.pPriorityReqs : &pUVCpu->vm.s.pNormalReqs;
877 pReq->enmState = VMREQSTATE_QUEUED;
878 PVMREQ pNext;
879 do
880 {
881 pNext = ASMAtomicUoReadPtrT(ppQueueHead, PVMREQ);
882 ASMAtomicWritePtr(&pReq->pNext, pNext);
883 ASMCompilerBarrier();
884 } while (!ASMAtomicCmpXchgPtr(ppQueueHead, pReq, pNext));
885
886 /*
887 * Notify EMT.
888 */
889 if (pUVM->pVM)
890 VMCPU_FF_SET(pVCpu, VMCPU_FF_REQUEST);
891 VMR3NotifyCpuFFU(pUVCpu, fFlags & VMREQFLAGS_POKE ? VMNOTIFYFF_FLAGS_POKE : 0);
892
893 /*
894 * Wait and return.
895 */
896 if (!(fFlags & VMREQFLAGS_NO_WAIT))
897 rc = VMR3ReqWait(pReq, cMillies);
898 LogFlow(("VMR3ReqQueue: returns %Rrc\n", rc));
899 }
900 else if ( ( pReq->idDstCpu == VMCPUID_ANY
901 && !pUVCpu /* only EMT threads have a valid pointer stored in the TLS slot. */)
902 || pReq->idDstCpu == VMCPUID_ANY_QUEUE)
903 {
904 unsigned fFlags = ((VMREQ volatile *)pReq)->fFlags; /* volatile paranoia */
905
906 /* Note: pUVCpu may or may not be NULL in the VMCPUID_ANY_QUEUE case; we don't care. */
907
908 /*
909 * Insert it.
910 */
911 volatile PVMREQ *ppQueueHead = pReq->fFlags & VMREQFLAGS_PRIORITY ? &pUVM->vm.s.pPriorityReqs : &pUVM->vm.s.pNormalReqs;
912 pReq->enmState = VMREQSTATE_QUEUED;
913 PVMREQ pNext;
914 do
915 {
916 pNext = ASMAtomicUoReadPtrT(ppQueueHead, PVMREQ);
917 ASMAtomicWritePtr(&pReq->pNext, pNext);
918 ASMCompilerBarrier();
919 } while (!ASMAtomicCmpXchgPtr(ppQueueHead, pReq, pNext));
920
921 /*
922 * Notify EMT.
923 */
924 if (pUVM->pVM)
925 VM_FF_SET(pUVM->pVM, VM_FF_REQUEST);
926 VMR3NotifyGlobalFFU(pUVM, fFlags & VMREQFLAGS_POKE ? VMNOTIFYFF_FLAGS_POKE : 0);
927
928 /*
929 * Wait and return.
930 */
931 if (!(fFlags & VMREQFLAGS_NO_WAIT))
932 rc = VMR3ReqWait(pReq, cMillies);
933 LogFlow(("VMR3ReqQueue: returns %Rrc\n", rc));
934 }
935 else
936 {
937 Assert(pUVCpu);
938
939 /*
940 * The requester was an EMT, just execute it.
941 */
942 pReq->enmState = VMREQSTATE_QUEUED;
943 rc = vmR3ReqProcessOne(pReq);
944 LogFlow(("VMR3ReqQueue: returns %Rrc (processed)\n", rc));
945 }
946 return rc;
947}
948
949
950/**
951 * Wait for a request to be completed.
952 *
953 * @returns VBox status code.
954 * @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
955 *
956 * @param pReq The request to wait for.
957 * @param cMillies Number of milliseconds to wait.
958 * Use RT_INDEFINITE_WAIT to only wait till it's completed.
959 */
960VMMR3DECL(int) VMR3ReqWait(PVMREQ pReq, RTMSINTERVAL cMillies)
961{
962 LogFlow(("VMR3ReqWait: pReq=%p cMillies=%d\n", pReq, cMillies));
963
964 /*
965 * Verify the supplied package.
966 */
967 AssertMsgReturn( pReq->enmState == VMREQSTATE_QUEUED
968 || pReq->enmState == VMREQSTATE_PROCESSING
969 || pReq->enmState == VMREQSTATE_COMPLETED,
970 ("Invalid state %d\n", pReq->enmState),
971 VERR_VM_REQUEST_STATE);
972 AssertMsgReturn( VALID_PTR(pReq->pUVM)
973 && pReq->EventSem != NIL_RTSEMEVENT,
974 ("Invalid request package! Anyone cooking their own packages???\n"),
975 VERR_VM_REQUEST_INVALID_PACKAGE);
976 AssertMsgReturn( pReq->enmType > VMREQTYPE_INVALID
977 && pReq->enmType < VMREQTYPE_MAX,
978 ("Invalid package type %d valid range %d-%d inclusively. This was verified on alloc too...\n",
979 pReq->enmType, VMREQTYPE_INVALID + 1, VMREQTYPE_MAX - 1),
980 VERR_VM_REQUEST_INVALID_TYPE);
981
982 /*
983 * Check for deadlock condition
984 */
985 PUVM pUVM = pReq->pUVM;
986 NOREF(pUVM);
987
988 /*
989 * Wait on the package.
990 */
991 int rc;
992 if (cMillies != RT_INDEFINITE_WAIT)
993 rc = RTSemEventWait(pReq->EventSem, cMillies);
994 else
995 {
996 do
997 {
998 rc = RTSemEventWait(pReq->EventSem, RT_INDEFINITE_WAIT);
999 Assert(rc != VERR_TIMEOUT);
1000 } while ( pReq->enmState != VMREQSTATE_COMPLETED
1001 && pReq->enmState != VMREQSTATE_INVALID);
1002 }
1003 if (RT_SUCCESS(rc))
1004 ASMAtomicXchgSize(&pReq->fEventSemClear, true);
1005 if (pReq->enmState == VMREQSTATE_COMPLETED)
1006 rc = VINF_SUCCESS;
1007 LogFlow(("VMR3ReqWait: returns %Rrc\n", rc));
1008 Assert(rc != VERR_INTERRUPTED);
1009 return rc;
1010}
1011
1012
1013/**
1014 * Sets the relevant FF.
1015 *
1016 * @param pUVM Pointer to the user mode VM structure.
1017 * @param idDstCpu VMCPUID_ANY or the ID of the current CPU.
1018 */
1019DECLINLINE(void) vmR3ReqSetFF(PUVM pUVM, VMCPUID idDstCpu)
1020{
1021 if (RT_LIKELY(pUVM->pVM))
1022 {
1023 if (idDstCpu == VMCPUID_ANY)
1024 VM_FF_SET(pUVM->pVM, VM_FF_REQUEST);
1025 else
1026 VMCPU_FF_SET(&pUVM->pVM->aCpus[idDstCpu], VMCPU_FF_REQUEST);
1027 }
1028}
1029
1030
1031/**
1032 * VMR3ReqProcessU helper that handles cases where there are more than one
1033 * pending request.
1034 *
1035 * @returns The oldest request.
1036 * @param pUVM Pointer to the user mode VM structure
1037 * @param idDstCpu VMCPUID_ANY or virtual CPU ID.
1038 * @param pReqList The list of requests.
1039 * @param ppReqs Pointer to the list head.
1040 */
1041static PVMREQ vmR3ReqProcessUTooManyHelper(PUVM pUVM, VMCPUID idDstCpu, PVMREQ pReqList, PVMREQ volatile *ppReqs)
1042{
1043 STAM_COUNTER_INC(&pUVM->vm.s.StatReqMoreThan1);
1044
1045 /*
1046 * Chop off the last one (pReq).
1047 */
1048 PVMREQ pPrev;
1049 PVMREQ pReqRet = pReqList;
1050 do
1051 {
1052 pPrev = pReqRet;
1053 pReqRet = pReqRet->pNext;
1054 } while (pReqRet->pNext);
1055 ASMAtomicWriteNullPtr(&pPrev->pNext);
1056
1057 /*
1058 * Push the others back onto the list (end of it).
1059 */
1060 Log2(("VMR3ReqProcess: Pushing back %p %p...\n", pReqList, pReqList->pNext));
1061 if (RT_UNLIKELY(!ASMAtomicCmpXchgPtr(ppReqs, pReqList, NULL)))
1062 {
1063 STAM_COUNTER_INC(&pUVM->vm.s.StatReqPushBackRaces);
1064 do
1065 {
1066 ASMNopPause();
1067 PVMREQ pReqList2 = ASMAtomicXchgPtrT(ppReqs, NULL, PVMREQ);
1068 if (pReqList2)
1069 {
1070 PVMREQ pLast = pReqList2;
1071 while (pLast->pNext)
1072 pLast = pLast->pNext;
1073 ASMAtomicWritePtr(&pLast->pNext, pReqList);
1074 pReqList = pReqList2;
1075 }
1076 } while (!ASMAtomicCmpXchgPtr(ppReqs, pReqList, NULL));
1077 }
1078
1079 vmR3ReqSetFF(pUVM, idDstCpu);
1080 return pReqRet;
1081}
1082
1083
1084/**
1085 * Process pending request(s).
1086 *
1087 * This function is called from a forced action handler in the EMT
1088 * or from one of the EMT loops.
1089 *
1090 * @returns VBox status code.
1091 *
1092 * @param pUVM Pointer to the user mode VM structure.
1093 * @param idDstCpu Pass VMCPUID_ANY to process the common request queue
1094 * and the CPU ID for a CPU specific one. In the latter
1095 * case the calling thread must be the EMT of that CPU.
1096 * @param fPriorityOnly When set, only process the priority request queue.
1097 *
1098 * @note SMP safe (multiple EMTs trying to satisfy VM_FF_REQUESTs).
1099 *
1100 * @remarks This was made reentrant for async PDM handling, the debugger and
1101 * others.
1102 * @internal
1103 */
1104VMMR3_INT_DECL(int) VMR3ReqProcessU(PUVM pUVM, VMCPUID idDstCpu, bool fPriorityOnly)
1105{
1106 LogFlow(("VMR3ReqProcessU: (enmVMState=%d) idDstCpu=%d\n", pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_CREATING, idDstCpu));
1107
1108 /*
1109 * Determine which queues to process.
1110 */
1111 PVMREQ volatile *ppNormalReqs;
1112 PVMREQ volatile *ppPriorityReqs;
1113 if (idDstCpu == VMCPUID_ANY)
1114 {
1115 ppPriorityReqs = &pUVM->vm.s.pPriorityReqs;
1116 ppNormalReqs = !fPriorityOnly ? &pUVM->vm.s.pNormalReqs : ppPriorityReqs;
1117 }
1118 else
1119 {
1120 Assert(idDstCpu < pUVM->cCpus);
1121 Assert(pUVM->aCpus[idDstCpu].vm.s.NativeThreadEMT == RTThreadNativeSelf());
1122 ppPriorityReqs = &pUVM->aCpus[idDstCpu].vm.s.pPriorityReqs;
1123 ppNormalReqs = !fPriorityOnly ? &pUVM->aCpus[idDstCpu].vm.s.pNormalReqs : ppPriorityReqs;
1124 }
1125
1126 /*
1127 * Process loop.
1128 *
1129 * We do not repeat the outer loop if we've got an informational status code
1130 * since that code needs processing by our caller (usually EM).
1131 */
1132 int rc = VINF_SUCCESS;
1133 for (;;)
1134 {
1135 /*
1136 * Get the pending requests.
1137 *
1138 * If there are more than one request, unlink the oldest and put the
1139 * rest back so that we're reentrant.
1140 */
1141 if (RT_LIKELY(pUVM->pVM))
1142 {
1143 if (idDstCpu == VMCPUID_ANY)
1144 VM_FF_CLEAR(pUVM->pVM, VM_FF_REQUEST);
1145 else
1146 VMCPU_FF_CLEAR(&pUVM->pVM->aCpus[idDstCpu], VMCPU_FF_REQUEST);
1147 }
1148
1149 PVMREQ pReq = ASMAtomicXchgPtrT(ppPriorityReqs, NULL, PVMREQ);
1150 if (pReq)
1151 {
1152 if (RT_UNLIKELY(pReq->pNext))
1153 pReq = vmR3ReqProcessUTooManyHelper(pUVM, idDstCpu, pReq, ppPriorityReqs);
1154 else if (ASMAtomicReadPtrT(ppNormalReqs, PVMREQ))
1155 vmR3ReqSetFF(pUVM, idDstCpu);
1156 }
1157 else
1158 {
1159 pReq = ASMAtomicXchgPtrT(ppNormalReqs, NULL, PVMREQ);
1160 if (!pReq)
1161 break;
1162 if (RT_UNLIKELY(pReq->pNext))
1163 pReq = vmR3ReqProcessUTooManyHelper(pUVM, idDstCpu, pReq, ppNormalReqs);
1164 }
1165
1166 /*
1167 * Process the request
1168 */
1169 STAM_COUNTER_INC(&pUVM->vm.s.StatReqProcessed);
1170 int rc2 = vmR3ReqProcessOne(pReq);
1171 if ( rc2 >= VINF_EM_FIRST
1172 && rc2 <= VINF_EM_LAST)
1173 {
1174 rc = rc2;
1175 break;
1176 }
1177 }
1178
1179 LogFlow(("VMR3ReqProcess: returns %Rrc (enmVMState=%d)\n", rc, pUVM->pVM ? pUVM->pVM->enmVMState : VMSTATE_CREATING));
1180 return rc;
1181}
1182
1183
1184/**
1185 * Process one request.
1186 *
1187 * @returns VBox status code.
1188 *
1189 * @param pReq Request packet to process.
1190 */
1191static int vmR3ReqProcessOne(PVMREQ pReq)
1192{
1193 LogFlow(("vmR3ReqProcessOne: pReq=%p type=%d fFlags=%#x\n", pReq, pReq->enmType, pReq->fFlags));
1194
1195 /*
1196 * Process the request.
1197 */
1198 Assert(pReq->enmState == VMREQSTATE_QUEUED);
1199 pReq->enmState = VMREQSTATE_PROCESSING;
1200 int rcRet = VINF_SUCCESS; /* the return code of this function. */
1201 int rcReq = VERR_NOT_IMPLEMENTED; /* the request status. */
1202 switch (pReq->enmType)
1203 {
1204 /*
1205 * A packed down call frame.
1206 */
1207 case VMREQTYPE_INTERNAL:
1208 {
1209 uintptr_t *pauArgs = &pReq->u.Internal.aArgs[0];
1210 union
1211 {
1212 PFNRT pfn;
1213 DECLCALLBACKMEMBER(int, pfn00)(void);
1214 DECLCALLBACKMEMBER(int, pfn01)(uintptr_t);
1215 DECLCALLBACKMEMBER(int, pfn02)(uintptr_t, uintptr_t);
1216 DECLCALLBACKMEMBER(int, pfn03)(uintptr_t, uintptr_t, uintptr_t);
1217 DECLCALLBACKMEMBER(int, pfn04)(uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1218 DECLCALLBACKMEMBER(int, pfn05)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1219 DECLCALLBACKMEMBER(int, pfn06)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1220 DECLCALLBACKMEMBER(int, pfn07)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1221 DECLCALLBACKMEMBER(int, pfn08)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1222 DECLCALLBACKMEMBER(int, pfn09)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1223 DECLCALLBACKMEMBER(int, pfn10)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1224 DECLCALLBACKMEMBER(int, pfn11)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1225 DECLCALLBACKMEMBER(int, pfn12)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1226 DECLCALLBACKMEMBER(int, pfn13)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1227 DECLCALLBACKMEMBER(int, pfn14)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1228 DECLCALLBACKMEMBER(int, pfn15)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
1229 } u;
1230 u.pfn = pReq->u.Internal.pfn;
1231#ifdef RT_ARCH_AMD64
1232 switch (pReq->u.Internal.cArgs)
1233 {
1234 case 0: rcRet = u.pfn00(); break;
1235 case 1: rcRet = u.pfn01(pauArgs[0]); break;
1236 case 2: rcRet = u.pfn02(pauArgs[0], pauArgs[1]); break;
1237 case 3: rcRet = u.pfn03(pauArgs[0], pauArgs[1], pauArgs[2]); break;
1238 case 4: rcRet = u.pfn04(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3]); break;
1239 case 5: rcRet = u.pfn05(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4]); break;
1240 case 6: rcRet = u.pfn06(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5]); break;
1241 case 7: rcRet = u.pfn07(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6]); break;
1242 case 8: rcRet = u.pfn08(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7]); break;
1243 case 9: rcRet = u.pfn09(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8]); break;
1244 case 10: rcRet = u.pfn10(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9]); break;
1245 case 11: rcRet = u.pfn11(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10]); break;
1246 case 12: rcRet = u.pfn12(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10], pauArgs[11]); break;
1247 case 13: rcRet = u.pfn13(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10], pauArgs[11], pauArgs[12]); break;
1248 case 14: rcRet = u.pfn14(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10], pauArgs[11], pauArgs[12], pauArgs[13]); break;
1249 case 15: rcRet = u.pfn15(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10], pauArgs[11], pauArgs[12], pauArgs[13], pauArgs[14]); break;
1250 default:
1251 AssertReleaseMsgFailed(("cArgs=%d\n", pReq->u.Internal.cArgs));
1252 rcRet = rcReq = VERR_VM_REQUEST_TOO_MANY_ARGS_IPE;
1253 break;
1254 }
1255#else /* x86: */
1256 size_t cbArgs = pReq->u.Internal.cArgs * sizeof(uintptr_t);
1257# ifdef __GNUC__
1258 __asm__ __volatile__("movl %%esp, %%edx\n\t"
1259 "subl %2, %%esp\n\t"
1260 "andl $0xfffffff0, %%esp\n\t"
1261 "shrl $2, %2\n\t"
1262 "movl %%esp, %%edi\n\t"
1263 "rep movsl\n\t"
1264 "movl %%edx, %%edi\n\t"
1265 "call *%%eax\n\t"
1266 "mov %%edi, %%esp\n\t"
1267 : "=a" (rcRet),
1268 "=S" (pauArgs),
1269 "=c" (cbArgs)
1270 : "0" (u.pfn),
1271 "1" (pauArgs),
1272 "2" (cbArgs)
1273 : "edi", "edx");
1274# else
1275 __asm
1276 {
1277 xor edx, edx /* just mess it up. */
1278 mov eax, u.pfn
1279 mov ecx, cbArgs
1280 shr ecx, 2
1281 mov esi, pauArgs
1282 mov ebx, esp
1283 sub esp, cbArgs
1284 and esp, 0xfffffff0
1285 mov edi, esp
1286 rep movsd
1287 call eax
1288 mov esp, ebx
1289 mov rcRet, eax
1290 }
1291# endif
1292#endif /* x86 */
1293 if ((pReq->fFlags & (VMREQFLAGS_RETURN_MASK)) == VMREQFLAGS_VOID)
1294 rcRet = VINF_SUCCESS;
1295 rcReq = rcRet;
1296 break;
1297 }
1298
1299 default:
1300 AssertMsgFailed(("pReq->enmType=%d\n", pReq->enmType));
1301 rcReq = VERR_NOT_IMPLEMENTED;
1302 break;
1303 }
1304
1305 /*
1306 * Complete the request.
1307 */
1308 pReq->iStatus = rcReq;
1309 pReq->enmState = VMREQSTATE_COMPLETED;
1310 if (pReq->fFlags & VMREQFLAGS_NO_WAIT)
1311 {
1312 /* Free the packet, nobody is waiting. */
1313 LogFlow(("vmR3ReqProcessOne: Completed request %p: rcReq=%Rrc rcRet=%Rrc - freeing it\n",
1314 pReq, rcReq, rcRet));
1315 VMR3ReqFree(pReq);
1316 }
1317 else
1318 {
1319 /* Notify the waiter and him free up the packet. */
1320 LogFlow(("vmR3ReqProcessOne: Completed request %p: rcReq=%Rrc rcRet=%Rrc - notifying waiting thread\n",
1321 pReq, rcReq, rcRet));
1322 ASMAtomicXchgSize(&pReq->fEventSemClear, false);
1323 int rc2 = RTSemEventSignal(pReq->EventSem);
1324 if (RT_FAILURE(rc2))
1325 {
1326 AssertRC(rc2);
1327 rcRet = rc2;
1328 }
1329 }
1330
1331 return rcRet;
1332}
1333
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use