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