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