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
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
|
/* $Id$ */
/** @file
* VBoxDTraceR0.
*
* Contributed by: bird
*/
/*
* Copyright (C) 2012-2017 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 Common
* Development and Distribution License Version 1.0 (CDDL) only, as it
* comes in the "COPYING.CDDL" 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 *
*********************************************************************************************************************************/
#include <VBox/sup.h>
#include <VBox/log.h>
#include <iprt/asm-amd64-x86.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/mp.h>
#include <iprt/process.h>
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include <sys/dtrace_impl.h>
#include <VBox/VBoxTpG.h>
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
//#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
//# define HAVE_RTMEMALLOCEX_FEATURES
//#endif
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** Caller indicator. */
typedef enum VBOXDTCALLER
{
kVBoxDtCaller_Invalid = 0,
kVBoxDtCaller_Generic,
kVBoxDtCaller_ProbeFireUser,
kVBoxDtCaller_ProbeFireKernel
} VBOXDTCALLER;
/**
* Stack data used for thread structure and such.
*
* This is planted in every external entry point and used to emulate solaris
* curthread, CRED, curproc and similar. It is also used to get at the
* uncached probe arguments.
*/
typedef struct VBoxDtStackData
{
/** Eyecatcher no. 1 (VBDT_STACK_DATA_MAGIC2). */
uint32_t u32Magic1;
/** Eyecatcher no. 2 (VBDT_STACK_DATA_MAGIC2). */
uint32_t u32Magic2;
/** The format of the caller specific data. */
VBOXDTCALLER enmCaller;
/** Caller specific data. */
union
{
/** kVBoxDtCaller_ProbeFireKernel. */
struct
{
/** The caller. */
uintptr_t uCaller;
/** Pointer to the stack arguments of a probe function call. */
uintptr_t *pauStackArgs;
} ProbeFireKernel;
/** kVBoxDtCaller_ProbeFireUser. */
struct
{
/** The user context. */
PCSUPDRVTRACERUSRCTX pCtx;
/** The argument displacement caused by 64-bit arguments passed directly to
* dtrace_probe. */
int offArg;
} ProbeFireUser;
} u;
/** Credentials allocated by VBoxDtGetCurrentCreds. */
struct VBoxDtCred *pCred;
/** Thread structure currently being held by this thread. */
struct VBoxDtThread *pThread;
/** Pointer to this structure.
* This is the final bit of integrity checking. */
struct VBoxDtStackData *pSelf;
} VBDTSTACKDATA;
/** Pointer to the on-stack thread specific data. */
typedef VBDTSTACKDATA *PVBDTSTACKDATA;
/** The first magic value. */
#define VBDT_STACK_DATA_MAGIC1 RT_MAKE_U32_FROM_U8('V', 'B', 'o', 'x')
/** The second magic value. */
#define VBDT_STACK_DATA_MAGIC2 RT_MAKE_U32_FROM_U8('D', 'T', 'r', 'c')
/** The alignment of the stack data.
* The data doesn't require more than sizeof(uintptr_t) alignment, but the
* greater alignment the quicker lookup. */
#define VBDT_STACK_DATA_ALIGN 32
/** Plants the stack data. */
#define VBDT_SETUP_STACK_DATA(a_enmCaller) \
uint8_t abBlob[sizeof(VBDTSTACKDATA) + VBDT_STACK_DATA_ALIGN - 1]; \
PVBDTSTACKDATA pStackData = (PVBDTSTACKDATA)( (uintptr_t)&abBlob[VBDT_STACK_DATA_ALIGN - 1] \
& ~(uintptr_t)(VBDT_STACK_DATA_ALIGN - 1)); \
pStackData->u32Magic1 = VBDT_STACK_DATA_MAGIC1; \
pStackData->u32Magic2 = VBDT_STACK_DATA_MAGIC2; \
pStackData->enmCaller = a_enmCaller; \
pStackData->pCred = NULL; \
pStackData->pThread = NULL; \
pStackData->pSelf = pStackData
/** Passifies the stack data and frees up resource held within it. */
#define VBDT_CLEAR_STACK_DATA() \
do \
{ \
pStackData->u32Magic1 = 0; \
pStackData->u32Magic2 = 0; \
pStackData->pSelf = NULL; \
if (pStackData->pCred) \
crfree(pStackData->pCred); \
if (pStackData->pThread) \
VBoxDtReleaseThread(pStackData->pThread); \
} while (0)
/** Simple SUPR0Printf-style logging. */
#if 0 /*def DEBUG_bird*/
# define LOG_DTRACE(a) SUPR0Printf a
#else
# define LOG_DTRACE(a) do { } while (0)
#endif
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Per CPU information */
cpucore_t g_aVBoxDtCpuCores[RTCPUSET_MAX_CPUS];
/** Dummy mutex. */
struct VBoxDtMutex g_DummyMtx;
/** Pointer to the tracer helpers provided by VBoxDrv. */
static PCSUPDRVTRACERHLP g_pVBoxDTraceHlp;
dtrace_cacheid_t dtrace_predcache_id = DTRACE_CACHEIDNONE + 1;
#if 0
void (*dtrace_cpu_init)(processorid_t);
void (*dtrace_modload)(struct modctl *);
void (*dtrace_modunload)(struct modctl *);
void (*dtrace_helpers_cleanup)(void);
void (*dtrace_helpers_fork)(proc_t *, proc_t *);
void (*dtrace_cpustart_init)(void);
void (*dtrace_cpustart_fini)(void);
void (*dtrace_cpc_fire)(uint64_t);
void (*dtrace_debugger_init)(void);
void (*dtrace_debugger_fini)(void);
#endif
/**
* Gets the stack data.
*
* @returns Pointer to the stack data. Never NULL.
*/
static PVBDTSTACKDATA vboxDtGetStackData(void)
{
int volatile iDummy = 1; /* use this to get the stack address. */
PVBDTSTACKDATA pData = (PVBDTSTACKDATA)( ((uintptr_t)&iDummy + VBDT_STACK_DATA_ALIGN - 1)
& ~(uintptr_t)(VBDT_STACK_DATA_ALIGN - 1));
for (;;)
{
if ( pData->u32Magic1 == VBDT_STACK_DATA_MAGIC1
&& pData->u32Magic2 == VBDT_STACK_DATA_MAGIC2
&& pData->pSelf == pData)
return pData;
pData = (PVBDTSTACKDATA)((uintptr_t)pData + VBDT_STACK_DATA_ALIGN);
}
}
void dtrace_toxic_ranges(void (*pfnAddOne)(uintptr_t uBase, uintptr_t cbRange))
{
/** @todo ? */
RT_NOREF_PV(pfnAddOne);
}
/**
* Dummy callback used by dtrace_sync.
*/
static DECLCALLBACK(void) vboxDtSyncCallback(RTCPUID idCpu, void *pvUser1, void *pvUser2)
{
NOREF(idCpu); NOREF(pvUser1); NOREF(pvUser2);
}
/**
* Synchronzie across all CPUs (expensive).
*/
void dtrace_sync(void)
{
int rc = RTMpOnAll(vboxDtSyncCallback, NULL, NULL);
AssertRC(rc);
}
/**
* Fetch a 8-bit "word" from userland.
*
* @return The byte value.
* @param pvUserAddr The userland address.
*/
uint8_t dtrace_fuword8( void *pvUserAddr)
{
uint8_t u8;
int rc = RTR0MemUserCopyFrom(&u8, (uintptr_t)pvUserAddr, sizeof(u8));
if (RT_FAILURE(rc))
{
RTCPUID iCpu = VBDT_GET_CPUID();
cpu_core[iCpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
cpu_core[iCpu].cpuc_dtrace_illval = (uintptr_t)pvUserAddr;
u8 = 0;
}
return u8;
}
/**
* Fetch a 16-bit word from userland.
*
* @return The word value.
* @param pvUserAddr The userland address.
*/
uint16_t dtrace_fuword16(void *pvUserAddr)
{
uint16_t u16;
int rc = RTR0MemUserCopyFrom(&u16, (uintptr_t)pvUserAddr, sizeof(u16));
if (RT_FAILURE(rc))
{
RTCPUID iCpu = VBDT_GET_CPUID();
cpu_core[iCpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
cpu_core[iCpu].cpuc_dtrace_illval = (uintptr_t)pvUserAddr;
u16 = 0;
}
return u16;
}
/**
* Fetch a 32-bit word from userland.
*
* @return The dword value.
* @param pvUserAddr The userland address.
*/
uint32_t dtrace_fuword32(void *pvUserAddr)
{
uint32_t u32;
int rc = RTR0MemUserCopyFrom(&u32, (uintptr_t)pvUserAddr, sizeof(u32));
if (RT_FAILURE(rc))
{
RTCPUID iCpu = VBDT_GET_CPUID();
cpu_core[iCpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
cpu_core[iCpu].cpuc_dtrace_illval = (uintptr_t)pvUserAddr;
u32 = 0;
}
return u32;
}
/**
* Fetch a 64-bit word from userland.
*
* @return The qword value.
* @param pvUserAddr The userland address.
*/
uint64_t dtrace_fuword64(void *pvUserAddr)
{
uint64_t u64;
int rc = RTR0MemUserCopyFrom(&u64, (uintptr_t)pvUserAddr, sizeof(u64));
if (RT_FAILURE(rc))
{
RTCPUID iCpu = VBDT_GET_CPUID();
cpu_core[iCpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
cpu_core[iCpu].cpuc_dtrace_illval = (uintptr_t)pvUserAddr;
u64 = 0;
}
return u64;
}
/** copyin implementation */
int VBoxDtCopyIn(void const *pvUser, void *pvDst, size_t cb)
{
int rc = RTR0MemUserCopyFrom(pvDst, (uintptr_t)pvUser, cb);
return RT_SUCCESS(rc) ? 0 : -1;
}
/** copyout implementation */
int VBoxDtCopyOut(void const *pvSrc, void *pvUser, size_t cb)
{
int rc = RTR0MemUserCopyTo((uintptr_t)pvUser, pvSrc, cb);
return RT_SUCCESS(rc) ? 0 : -1;
}
/**
* Copy data from userland into the kernel.
*
* @param uUserAddr The userland address.
* @param uKrnlAddr The kernel buffer address.
* @param cb The number of bytes to copy.
* @param pfFlags Pointer to the relevant cpuc_dtrace_flags.
*/
void dtrace_copyin( uintptr_t uUserAddr, uintptr_t uKrnlAddr, size_t cb, volatile uint16_t *pfFlags)
{
int rc = RTR0MemUserCopyFrom((void *)uKrnlAddr, uUserAddr, cb);
if (RT_FAILURE(rc))
{
*pfFlags |= CPU_DTRACE_BADADDR;
cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = uUserAddr;
}
}
/**
* Copy data from the kernel into userland.
*
* @param uKrnlAddr The kernel buffer address.
* @param uUserAddr The userland address.
* @param cb The number of bytes to copy.
* @param pfFlags Pointer to the relevant cpuc_dtrace_flags.
*/
void dtrace_copyout( uintptr_t uKrnlAddr, uintptr_t uUserAddr, size_t cb, volatile uint16_t *pfFlags)
{
int rc = RTR0MemUserCopyTo(uUserAddr, (void const *)uKrnlAddr, cb);
if (RT_FAILURE(rc))
{
*pfFlags |= CPU_DTRACE_BADADDR;
cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = uUserAddr;
}
}
/**
* Copy a string from userland into the kernel.
*
* @param uUserAddr The userland address.
* @param uKrnlAddr The kernel buffer address.
* @param cbMax The maximum number of bytes to copy. May stop
* earlier if zero byte is encountered.
* @param pfFlags Pointer to the relevant cpuc_dtrace_flags.
*/
void dtrace_copyinstr( uintptr_t uUserAddr, uintptr_t uKrnlAddr, size_t cbMax, volatile uint16_t *pfFlags)
{
if (!cbMax)
return;
char *pszDst = (char *)uKrnlAddr;
int rc = RTR0MemUserCopyFrom(pszDst, uUserAddr, cbMax);
if (RT_FAILURE(rc))
{
/* Byte by byte - lazy bird! */
size_t off = 0;
while (off < cbMax)
{
rc = RTR0MemUserCopyFrom(&pszDst[off], uUserAddr + off, 1);
if (RT_FAILURE(rc))
{
*pfFlags |= CPU_DTRACE_BADADDR;
cpu_core[VBDT_GET_CPUID()].cpuc_dtrace_illval = uUserAddr;
pszDst[off] = '\0';
return;
}
if (!pszDst[off])
return;
off++;
}
}
pszDst[cbMax - 1] = '\0';
}
/**
* Copy a string from the kernel and into user land.
*
* @param uKrnlAddr The kernel string address.
* @param uUserAddr The userland address.
* @param cbMax The maximum number of bytes to copy. Will stop
* earlier if zero byte is encountered.
* @param pfFlags Pointer to the relevant cpuc_dtrace_flags.
*/
void dtrace_copyoutstr(uintptr_t uKrnlAddr, uintptr_t uUserAddr, size_t cbMax, volatile uint16_t *pfFlags)
{
const char *pszSrc = (const char *)uKrnlAddr;
size_t cbActual = RTStrNLen(pszSrc, cbMax);
cbActual += cbActual < cbMax;
dtrace_copyout(uKrnlAddr,uUserAddr, cbActual, pfFlags);
}
/**
* Get the caller @a cCallFrames call frames up the stack.
*
* @returns The caller's return address or ~(uintptr_t)0.
* @param cCallFrames The number of frames.
*/
uintptr_t dtrace_caller(int cCallFrames)
{
PVBDTSTACKDATA pData = vboxDtGetStackData();
if (pData->enmCaller == kVBoxDtCaller_ProbeFireKernel)
return pData->u.ProbeFireKernel.uCaller;
RT_NOREF_PV(cCallFrames);
return ~(uintptr_t)0;
}
/**
* Get argument number @a iArg @a cCallFrames call frames up the stack.
*
* @returns The caller's return address or ~(uintptr_t)0.
* @param iArg The argument to get.
* @param cCallFrames The number of frames.
*/
uint64_t dtrace_getarg(int iArg, int cCallFrames)
{
PVBDTSTACKDATA pData = vboxDtGetStackData();
AssertReturn(iArg >= 5, UINT64_MAX);
if (pData->enmCaller == kVBoxDtCaller_ProbeFireKernel)
return pData->u.ProbeFireKernel.pauStackArgs[iArg - 5];
RT_NOREF_PV(cCallFrames);
return UINT64_MAX;
}
/**
* Produce a traceback of the kernel stack.
*
* @param paPcStack Where to return the program counters.
* @param cMaxFrames The maximum number of PCs to return.
* @param cSkipFrames The number of artificial callstack frames to
* skip at the top.
* @param pIntr Not sure what this is...
*/
void dtrace_getpcstack(pc_t *paPcStack, int cMaxFrames, int cSkipFrames, uint32_t *pIntr)
{
int iFrame = 0;
while (iFrame < cMaxFrames)
{
paPcStack[iFrame] = NULL;
iFrame++;
}
RT_NOREF_PV(pIntr);
RT_NOREF_PV(cSkipFrames);
}
/**
* Get the number of call frames on the stack.
*
* @returns The stack depth.
* @param cSkipFrames The number of artificial callstack frames to
* skip at the top.
*/
int dtrace_getstackdepth(int cSkipFrames)
{
RT_NOREF_PV(cSkipFrames);
return 1;
}
/**
* Produce a traceback of the userland stack.
*
* @param paPcStack Where to return the program counters.
* @param paFpStack Where to return the frame pointers.
* @param cMaxFrames The maximum number of frames to return.
*/
void dtrace_getufpstack(uint64_t *paPcStack, uint64_t *paFpStack, int cMaxFrames)
{
int iFrame = 0;
while (iFrame < cMaxFrames)
{
paPcStack[iFrame] = 0;
paFpStack[iFrame] = 0;
iFrame++;
}
}
/**
* Produce a traceback of the userland stack.
*
* @param paPcStack Where to return the program counters.
* @param cMaxFrames The maximum number of frames to return.
*/
void dtrace_getupcstack(uint64_t *paPcStack, int cMaxFrames)
{
int iFrame = 0;
while (iFrame < cMaxFrames)
{
paPcStack[iFrame] = 0;
iFrame++;
}
}
/**
* Computes the depth of the userland stack.
*/
int dtrace_getustackdepth(void)
{
return 0;
}
/**
* Get the current IPL/IRQL.
*
* @returns Current level.
*/
int dtrace_getipl(void)
{
#ifdef RT_ARCH_AMD64
/* CR8 is normally the same as IRQL / IPL on AMD64. */
return ASMGetCR8();
#else
/* Just fake it on x86. */
return !ASMIntAreEnabled();
#endif
}
/**
* Get current monotonic timestamp.
*
* @returns Timestamp, nano seconds.
*/
hrtime_t dtrace_gethrtime(void)
{
return RTTimeNanoTS();
}
/**
* Get current walltime.
*
* @returns Timestamp, nano seconds.
*/
hrtime_t dtrace_gethrestime(void)
{
/** @todo try get better resolution here somehow ... */
RTTIMESPEC Now;
return RTTimeSpecGetNano(RTTimeNow(&Now));
}
/**
* DTrace panic routine.
*
* @param pszFormat Panic message.
* @param va Arguments to the panic message.
*/
void dtrace_vpanic(const char *pszFormat, va_list va)
{
RTAssertMsg1(NULL, __LINE__, __FILE__, __FUNCTION__);
RTAssertMsg2WeakV(pszFormat, va);
RTR0AssertPanicSystem();
for (;;)
{
ASMBreakpoint();
volatile char *pchCrash = (volatile char *)~(uintptr_t)0;
*pchCrash = '\0';
}
}
/**
* DTrace panic routine.
*
* @param pszFormat Panic message.
* @param ... Arguments to the panic message.
*/
void VBoxDtPanic(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
dtrace_vpanic(pszFormat, va);
/*va_end(va); - unreachable */
}
/**
* DTrace kernel message routine.
*
* @param pszFormat Kernel message.
* @param ... Arguments to the panic message.
*/
void VBoxDtCmnErr(int iLevel, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
SUPR0Printf("%N", pszFormat, va);
va_end(va);
RT_NOREF_PV(iLevel);
}
/** uprintf implementation */
void VBoxDtUPrintf(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
VBoxDtUPrintfV(pszFormat, va);
va_end(va);
}
/** vuprintf implementation */
void VBoxDtUPrintfV(const char *pszFormat, va_list va)
{
SUPR0Printf("%N", pszFormat, va);
}
/* CRED implementation. */
cred_t *VBoxDtGetCurrentCreds(void)
{
PVBDTSTACKDATA pData = vboxDtGetStackData();
if (!pData->pCred)
{
struct VBoxDtCred *pCred;
#ifdef HAVE_RTMEMALLOCEX_FEATURES
int rc = RTMemAllocEx(sizeof(*pCred), 0, RTMEMALLOCEX_FLAGS_ANY_CTX, (void **)&pCred);
#else
int rc = RTMemAllocEx(sizeof(*pCred), 0, 0, (void **)&pCred);
#endif
AssertFatalRC(rc);
pCred->cr_refs = 1;
/** @todo get the right creds on unix systems. */
pCred->cr_uid = 0;
pCred->cr_ruid = 0;
pCred->cr_suid = 0;
pCred->cr_gid = 0;
pCred->cr_rgid = 0;
pCred->cr_sgid = 0;
pCred->cr_zone = 0;
pData->pCred = pCred;
}
return pData->pCred;
}
/* crhold implementation */
void VBoxDtCredHold(struct VBoxDtCred *pCred)
{
int32_t cRefs = ASMAtomicIncS32(&pCred->cr_refs);
Assert(cRefs > 1); NOREF(cRefs);
}
/* crfree implementation */
void VBoxDtCredFree(struct VBoxDtCred *pCred)
{
int32_t cRefs = ASMAtomicDecS32(&pCred->cr_refs);
Assert(cRefs >= 0);
if (!cRefs)
RTMemFreeEx(pCred, sizeof(*pCred));
}
/** Spinlock protecting the thread structures. */
static RTSPINLOCK g_hThreadSpinlock = NIL_RTSPINLOCK;
/** List of threads by usage age. */
static RTLISTANCHOR g_ThreadAgeList;
/** Hash table for looking up thread structures. */
static struct VBoxDtThread *g_apThreadsHash[16384];
/** Fake kthread_t structures.
* The size of this array is making horrible ASSUMPTIONS about the number of
* thread in the system that will be subjected to DTracing. */
static struct VBoxDtThread g_aThreads[8192];
static int vboxDtInitThreadDb(void)
{
int rc = RTSpinlockCreate(&g_hThreadSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDtThreadDb");
if (RT_FAILURE(rc))
return rc;
RTListInit(&g_ThreadAgeList);
for (uint32_t i = 0; i < RT_ELEMENTS(g_aThreads); i++)
{
g_aThreads[i].hNative = NIL_RTNATIVETHREAD;
g_aThreads[i].uPid = NIL_RTPROCESS;
RTListPrepend(&g_ThreadAgeList, &g_aThreads[i].AgeEntry);
}
return VINF_SUCCESS;
}
static void vboxDtTermThreadDb(void)
{
RTSpinlockDestroy(g_hThreadSpinlock);
g_hThreadSpinlock = NIL_RTSPINLOCK;
RTListInit(&g_ThreadAgeList);
}
/* curthread implementation, providing a fake kthread_t. */
struct VBoxDtThread *VBoxDtGetCurrentThread(void)
{
/*
* Once we've retrieved a thread, we hold on to it until the thread exits
* the VBoxDTrace module.
*/
PVBDTSTACKDATA pData = vboxDtGetStackData();
if (pData->pThread)
{
AssertPtr(pData->pThread);
Assert(pData->pThread->hNative == RTThreadNativeSelf());
Assert(pData->pThread->uPid == RTProcSelf());
Assert(RTListIsEmpty(&pData->pThread->AgeEntry));
return pData->pThread;
}
/*
* Lookup the thread in the hash table.
*/
RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
RTPROCESS uPid = RTProcSelf();
uintptr_t iHash = (hNativeSelf * 2654435761U) % RT_ELEMENTS(g_apThreadsHash);
RTSpinlockAcquire(g_hThreadSpinlock);
struct VBoxDtThread *pThread = g_apThreadsHash[iHash];
while (pThread)
{
if (pThread->hNative == hNativeSelf)
{
if (pThread->uPid != uPid)
{
/* Re-initialize the reused thread. */
pThread->uPid = uPid;
pThread->t_dtrace_vtime = 0;
pThread->t_dtrace_start = 0;
pThread->t_dtrace_stop = 0;
pThread->t_dtrace_scrpc = 0;
pThread->t_dtrace_astpc = 0;
pThread->t_predcache = 0;
}
/* Hold the thread in the on-stack data, making sure it does not
get reused till the thread leaves VBoxDTrace. */
RTListNodeRemove(&pThread->AgeEntry);
pData->pThread = pThread;
RTSpinlockRelease(g_hThreadSpinlock);
return pThread;
}
pThread = pThread->pNext;
}
/*
* Unknown thread. Allocate a new entry, recycling unused or old ones.
*/
pThread = RTListGetLast(&g_ThreadAgeList, struct VBoxDtThread, AgeEntry);
AssertFatal(pThread);
RTListNodeRemove(&pThread->AgeEntry);
if (pThread->hNative != NIL_RTNATIVETHREAD)
{
uintptr_t iHash2 = (pThread->hNative * 2654435761U) % RT_ELEMENTS(g_apThreadsHash);
if (g_apThreadsHash[iHash2] == pThread)
g_apThreadsHash[iHash2] = pThread->pNext;
else
{
for (struct VBoxDtThread *pPrev = g_apThreadsHash[iHash2]; ; pPrev = pPrev->pNext)
{
AssertPtr(pPrev);
if (pPrev->pNext == pThread)
{
pPrev->pNext = pThread->pNext;
break;
}
}
}
}
/*
* Initialize the data.
*/
pThread->t_dtrace_vtime = 0;
pThread->t_dtrace_start = 0;
pThread->t_dtrace_stop = 0;
pThread->t_dtrace_scrpc = 0;
pThread->t_dtrace_astpc = 0;
pThread->t_predcache = 0;
pThread->hNative = hNativeSelf;
pThread->uPid = uPid;
/*
* Add it to the hash as well as the on-stack data.
*/
pThread->pNext = g_apThreadsHash[iHash];
g_apThreadsHash[iHash] = pThread->pNext;
pData->pThread = pThread;
RTSpinlockRelease(g_hThreadSpinlock);
return pThread;
}
/**
* Called by the stack data destructor.
*
* @param pThread The thread to release.
*
*/
static void VBoxDtReleaseThread(struct VBoxDtThread *pThread)
{
RTSpinlockAcquire(g_hThreadSpinlock);
RTListAppend(&g_ThreadAgeList, &pThread->AgeEntry);
RTSpinlockRelease(g_hThreadSpinlock);
}
/*
*
* Virtual Memory / Resource Allocator.
* Virtual Memory / Resource Allocator.
* Virtual Memory / Resource Allocator.
*
*/
/** The number of bits per chunk.
* @remarks The 32 bytes are for heap headers and such like. */
#define VBOXDTVMEMCHUNK_BITS ( ((_64K - 32 - sizeof(uint32_t) * 2) / sizeof(uint32_t)) * 32)
/**
* Resource allocator chunk.
*/
typedef struct VBoxDtVMemChunk
{
/** The ordinal (unbased) of the first item. */
uint32_t iFirst;
/** The current number of free items in this chunk. */
uint32_t cCurFree;
/** The allocation bitmap. */
uint32_t bm[VBOXDTVMEMCHUNK_BITS / 32];
} VBOXDTVMEMCHUNK;
/** Pointer to a resource allocator chunk. */
typedef VBOXDTVMEMCHUNK *PVBOXDTVMEMCHUNK;
/**
* Resource allocator instance.
*/
typedef struct VBoxDtVMem
{
/** Spinlock protecting the data (interrupt safe). */
RTSPINLOCK hSpinlock;
/** Magic value. */
uint32_t u32Magic;
/** The current number of free items in the chunks. */
uint32_t cCurFree;
/** The current number of chunks that we have allocated. */
uint32_t cCurChunks;
/** The configured resource base. */
uint32_t uBase;
/** The configured max number of items. */
uint32_t cMaxItems;
/** The size of the apChunks array. */
uint32_t cMaxChunks;
/** Array of chunk pointers.
* (The size is determined at creation.) */
PVBOXDTVMEMCHUNK apChunks[1];
} VBOXDTVMEM;
/** Pointer to a resource allocator instance. */
typedef VBOXDTVMEM *PVBOXDTVMEM;
/** Magic value for the VBOXDTVMEM structure. */
#define VBOXDTVMEM_MAGIC RT_MAKE_U32_FROM_U8('V', 'M', 'e', 'm')
/* vmem_create implementation */
struct VBoxDtVMem *VBoxDtVMemCreate(const char *pszName, void *pvBase, size_t cb, size_t cbUnit,
PFNRT pfnAlloc, PFNRT pfnFree, struct VBoxDtVMem *pSrc,
size_t cbQCacheMax, uint32_t fFlags)
{
/*
* Assert preconditions of this implementation.
*/
AssertMsgReturn((uintptr_t)pvBase <= UINT32_MAX, ("%p\n", pvBase), NULL);
AssertMsgReturn(cb <= UINT32_MAX, ("%zu\n", cb), NULL);
AssertMsgReturn((uintptr_t)pvBase + cb - 1 <= UINT32_MAX, ("%p %zu\n", pvBase, cb), NULL);
AssertMsgReturn(cbUnit == 1, ("%zu\n", cbUnit), NULL);
AssertReturn(!pfnAlloc, NULL);
AssertReturn(!pfnFree, NULL);
AssertReturn(!pSrc, NULL);
AssertReturn(!cbQCacheMax, NULL);
AssertReturn(fFlags & VM_SLEEP, NULL);
AssertReturn(fFlags & VMC_IDENTIFIER, NULL);
RT_NOREF_PV(pszName);
/*
* Allocate the instance.
*/
uint32_t cChunks = (uint32_t)cb / VBOXDTVMEMCHUNK_BITS;
if (cb % VBOXDTVMEMCHUNK_BITS)
cChunks++;
PVBOXDTVMEM pThis = (PVBOXDTVMEM)RTMemAllocZ(RT_OFFSETOF(VBOXDTVMEM, apChunks[cChunks]));
if (!pThis)
return NULL;
int rc = RTSpinlockCreate(&pThis->hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDtVMem");
if (RT_FAILURE(rc))
{
RTMemFree(pThis);
return NULL;
}
pThis->u32Magic = VBOXDTVMEM_MAGIC;
pThis->cCurFree = 0;
pThis->cCurChunks = 0;
pThis->uBase = (uint32_t)(uintptr_t)pvBase;
pThis->cMaxItems = (uint32_t)cb;
pThis->cMaxChunks = cChunks;
return pThis;
}
/* vmem_destroy implementation */
void VBoxDtVMemDestroy(struct VBoxDtVMem *pThis)
{
if (!pThis)
return;
AssertPtrReturnVoid(pThis);
AssertReturnVoid(pThis->u32Magic == VBOXDTVMEM_MAGIC);
/*
* Invalidate the instance.
*/
RTSpinlockAcquire(pThis->hSpinlock); /* paranoia */
pThis->u32Magic = 0;
RTSpinlockRelease(pThis->hSpinlock);
RTSpinlockDestroy(pThis->hSpinlock);
/*
* Free the chunks, then the instance.
*/
uint32_t iChunk = pThis->cCurChunks;
while (iChunk-- > 0)
{
RTMemFree(pThis->apChunks[iChunk]);
pThis->apChunks[iChunk] = NULL;
}
RTMemFree(pThis);
}
/* vmem_alloc implementation */
void *VBoxDtVMemAlloc(struct VBoxDtVMem *pThis, size_t cbMem, uint32_t fFlags)
{
/*
* Validate input.
*/
AssertReturn(fFlags & VM_BESTFIT, NULL);
AssertReturn(fFlags & VM_SLEEP, NULL);
AssertReturn(cbMem == 1, NULL);
AssertPtrReturn(pThis, NULL);
AssertReturn(pThis->u32Magic == VBOXDTVMEM_MAGIC, NULL);
/*
* Allocation loop.
*/
RTSpinlockAcquire(pThis->hSpinlock);
for (;;)
{
PVBOXDTVMEMCHUNK pChunk;
uint32_t const cChunks = pThis->cCurChunks;
if (RT_LIKELY(pThis->cCurFree > 0))
{
for (uint32_t iChunk = 0; iChunk < cChunks; iChunk++)
{
pChunk = pThis->apChunks[iChunk];
if (pChunk->cCurFree > 0)
{
int iBit = ASMBitFirstClear(pChunk->bm, VBOXDTVMEMCHUNK_BITS);
AssertMsgReturnStmt(iBit >= 0 && (unsigned)iBit < VBOXDTVMEMCHUNK_BITS, ("%d\n", iBit),
RTSpinlockRelease(pThis->hSpinlock),
NULL);
ASMBitSet(pChunk->bm, iBit);
pChunk->cCurFree--;
pThis->cCurFree--;
uint32_t iRet = (uint32_t)iBit + pChunk->iFirst + pThis->uBase;
RTSpinlockRelease(pThis->hSpinlock);
return (void *)(uintptr_t)iRet;
}
}
AssertFailedBreak();
}
/* Out of resources? */
if (cChunks >= pThis->cMaxChunks)
break;
/*
* Allocate another chunk.
*/
uint32_t const iFirstBit = cChunks > 0 ? pThis->apChunks[cChunks - 1]->iFirst + VBOXDTVMEMCHUNK_BITS : 0;
uint32_t const cFreeBits = cChunks + 1 == pThis->cMaxChunks
? pThis->cMaxItems - (iFirstBit - pThis->uBase)
: VBOXDTVMEMCHUNK_BITS;
Assert(cFreeBits <= VBOXDTVMEMCHUNK_BITS);
RTSpinlockRelease(pThis->hSpinlock);
pChunk = (PVBOXDTVMEMCHUNK)RTMemAllocZ(sizeof(*pChunk));
if (!pChunk)
return NULL;
pChunk->iFirst = iFirstBit;
pChunk->cCurFree = cFreeBits;
if (cFreeBits != VBOXDTVMEMCHUNK_BITS)
{
/* lazy bird. */
uint32_t iBit = cFreeBits;
while (iBit < VBOXDTVMEMCHUNK_BITS)
{
ASMBitSet(pChunk->bm, iBit);
iBit++;
}
}
RTSpinlockAcquire(pThis->hSpinlock);
/*
* Insert the new chunk. If someone raced us here, we'll drop it to
* avoid wasting resources.
*/
if (pThis->cCurChunks == cChunks)
{
pThis->apChunks[cChunks] = pChunk;
pThis->cCurFree += pChunk->cCurFree;
pThis->cCurChunks += 1;
}
else
{
RTSpinlockRelease(pThis->hSpinlock);
RTMemFree(pChunk);
RTSpinlockAcquire(pThis->hSpinlock);
}
}
RTSpinlockRelease(pThis->hSpinlock);
return NULL;
}
/* vmem_free implementation */
void VBoxDtVMemFree(struct VBoxDtVMem *pThis, void *pvMem, size_t cbMem)
{
/*
* Validate input.
*/
AssertReturnVoid(cbMem == 1);
AssertPtrReturnVoid(pThis);
AssertReturnVoid(pThis->u32Magic == VBOXDTVMEM_MAGIC);
AssertReturnVoid((uintptr_t)pvMem < UINT32_MAX);
uint32_t uMem = (uint32_t)(uintptr_t)pvMem;
AssertReturnVoid(uMem >= pThis->uBase);
uMem -= pThis->uBase;
AssertReturnVoid(uMem < pThis->cMaxItems);
/*
* Free it.
*/
RTSpinlockAcquire(pThis->hSpinlock);
uint32_t const iChunk = uMem / VBOXDTVMEMCHUNK_BITS;
if (iChunk < pThis->cCurChunks)
{
PVBOXDTVMEMCHUNK pChunk = pThis->apChunks[iChunk];
uint32_t iBit = uMem - pChunk->iFirst;
AssertReturnVoidStmt(iBit < VBOXDTVMEMCHUNK_BITS, RTSpinlockRelease(pThis->hSpinlock));
AssertReturnVoidStmt(ASMBitTestAndClear(pChunk->bm, iBit), RTSpinlockRelease(pThis->hSpinlock));
pChunk->cCurFree++;
pThis->cCurFree++;
}
RTSpinlockRelease(pThis->hSpinlock);
}
/*
*
* Memory Allocators.
* Memory Allocators.
* Memory Allocators.
*
*/
/* kmem_alloc implementation */
void *VBoxDtKMemAlloc(size_t cbMem, uint32_t fFlags)
{
void *pvMem;
#ifdef HAVE_RTMEMALLOCEX_FEATURES
uint32_t fMemAllocFlags = fFlags & KM_NOSLEEP ? RTMEMALLOCEX_FLAGS_ANY_CTX : 0;
#else
uint32_t fMemAllocFlags = 0;
RT_NOREF_PV(fFlags);
#endif
int rc = RTMemAllocEx(cbMem, 0, fMemAllocFlags, &pvMem);
AssertRCReturn(rc, NULL);
AssertPtr(pvMem);
return pvMem;
}
/* kmem_zalloc implementation */
void *VBoxDtKMemAllocZ(size_t cbMem, uint32_t fFlags)
{
void *pvMem;
#ifdef HAVE_RTMEMALLOCEX_FEATURES
uint32_t fMemAllocFlags = (fFlags & KM_NOSLEEP ? RTMEMALLOCEX_FLAGS_ANY_CTX : 0) | RTMEMALLOCEX_FLAGS_ZEROED;
#else
uint32_t fMemAllocFlags = RTMEMALLOCEX_FLAGS_ZEROED;
RT_NOREF_PV(fFlags);
#endif
int rc = RTMemAllocEx(cbMem, 0, fMemAllocFlags, &pvMem);
AssertRCReturn(rc, NULL);
AssertPtr(pvMem);
return pvMem;
}
/* kmem_free implementation */
void VBoxDtKMemFree(void *pvMem, size_t cbMem)
{
RTMemFreeEx(pvMem, cbMem);
}
/**
* Memory cache mockup structure.
* No slab allocator here!
*/
struct VBoxDtMemCache
{
uint32_t u32Magic;
size_t cbBuf;
size_t cbAlign;
};
/* Limited kmem_cache_create implementation. */
struct VBoxDtMemCache *VBoxDtKMemCacheCreate(const char *pszName, size_t cbBuf, size_t cbAlign,
PFNRT pfnCtor, PFNRT pfnDtor, PFNRT pfnReclaim,
void *pvUser, void *pvVM, uint32_t fFlags)
{
/*
* Check the input.
*/
AssertReturn(cbBuf > 0 && cbBuf < _1G, NULL);
AssertReturn(RT_IS_POWER_OF_TWO(cbAlign), NULL);
AssertReturn(!pfnCtor, NULL);
AssertReturn(!pfnDtor, NULL);
AssertReturn(!pfnReclaim, NULL);
AssertReturn(!pvUser, NULL);
AssertReturn(!pvVM, NULL);
AssertReturn(!fFlags, NULL);
RT_NOREF_PV(pszName);
/*
* Create a parameter container. Don't bother with anything fancy here yet,
* just get something working.
*/
struct VBoxDtMemCache *pThis = (struct VBoxDtMemCache *)RTMemAlloc(sizeof(*pThis));
if (!pThis)
return NULL;
pThis->cbAlign = cbAlign;
pThis->cbBuf = cbBuf;
return pThis;
}
/* Limited kmem_cache_destroy implementation. */
void VBoxDtKMemCacheDestroy(struct VBoxDtMemCache *pThis)
{
RTMemFree(pThis);
}
/* kmem_cache_alloc implementation. */
void *VBoxDtKMemCacheAlloc(struct VBoxDtMemCache *pThis, uint32_t fFlags)
{
void *pvMem;
#ifdef HAVE_RTMEMALLOCEX_FEATURES
uint32_t fMemAllocFlags = (fFlags & KM_NOSLEEP ? RTMEMALLOCEX_FLAGS_ANY_CTX : 0) | RTMEMALLOCEX_FLAGS_ZEROED;
#else
uint32_t fMemAllocFlags = RTMEMALLOCEX_FLAGS_ZEROED;
RT_NOREF_PV(fFlags);
#endif
int rc = RTMemAllocEx(pThis->cbBuf, /*pThis->cbAlign*/0, fMemAllocFlags, &pvMem);
AssertRCReturn(rc, NULL);
AssertPtr(pvMem);
return pvMem;
}
/* kmem_cache_free implementation. */
void VBoxDtKMemCacheFree(struct VBoxDtMemCache *pThis, void *pvMem)
{
RTMemFreeEx(pvMem, pThis->cbBuf);
}
/*
*
* Mutex Semaphore Wrappers.
*
*/
/** Initializes a mutex. */
int VBoxDtMutexInit(struct VBoxDtMutex *pMtx)
{
AssertReturn(pMtx != &g_DummyMtx, -1);
AssertPtr(pMtx);
pMtx->hOwner = NIL_RTNATIVETHREAD;
pMtx->hMtx = NIL_RTSEMMUTEX;
int rc = RTSemMutexCreate(&pMtx->hMtx);
if (RT_SUCCESS(rc))
return 0;
return -1;
}
/** Deletes a mutex. */
void VBoxDtMutexDelete(struct VBoxDtMutex *pMtx)
{
AssertReturnVoid(pMtx != &g_DummyMtx);
AssertPtr(pMtx);
if (pMtx->hMtx == NIL_RTSEMMUTEX)
return;
Assert(pMtx->hOwner == NIL_RTNATIVETHREAD);
int rc = RTSemMutexDestroy(pMtx->hMtx); AssertRC(rc);
pMtx->hMtx = NIL_RTSEMMUTEX;
}
/* mutex_enter implementation */
void VBoxDtMutexEnter(struct VBoxDtMutex *pMtx)
{
AssertPtr(pMtx);
if (pMtx == &g_DummyMtx)
return;
RTNATIVETHREAD hSelf = RTThreadNativeSelf();
int rc = RTSemMutexRequest(pMtx->hMtx, RT_INDEFINITE_WAIT);
AssertFatalRC(rc);
Assert(pMtx->hOwner == NIL_RTNATIVETHREAD);
pMtx->hOwner = hSelf;
}
/* mutex_exit implementation */
void VBoxDtMutexExit(struct VBoxDtMutex *pMtx)
{
AssertPtr(pMtx);
if (pMtx == &g_DummyMtx)
return;
Assert(pMtx->hOwner == RTThreadNativeSelf());
pMtx->hOwner = NIL_RTNATIVETHREAD;
int rc = RTSemMutexRelease(pMtx->hMtx);
AssertFatalRC(rc);
}
/* MUTEX_HELD implementation */
bool VBoxDtMutexIsOwner(struct VBoxDtMutex *pMtx)
{
AssertPtrReturn(pMtx, false);
if (pMtx == &g_DummyMtx)
return true;
return pMtx->hOwner == RTThreadNativeSelf();
}
/*
*
* Helpers for handling VTG structures.
* Helpers for handling VTG structures.
* Helpers for handling VTG structures.
*
*/
/**
* Converts an attribute from VTG description speak to DTrace.
*
* @param pDtAttr The DTrace attribute (dst).
* @param pVtgAttr The VTG attribute descriptor (src).
*/
static void vboxDtVtgConvAttr(dtrace_attribute_t *pDtAttr, PCVTGDESCATTR pVtgAttr)
{
pDtAttr->dtat_name = pVtgAttr->u8Code - 1;
pDtAttr->dtat_data = pVtgAttr->u8Data - 1;
pDtAttr->dtat_class = pVtgAttr->u8DataDep - 1;
}
/**
* Gets a string from the string table.
*
* @returns Pointer to the string.
* @param pVtgHdr The VTG object header.
* @param offStrTab The string table offset.
*/
static const char *vboxDtVtgGetString(PVTGOBJHDR pVtgHdr, uint32_t offStrTab)
{
Assert(offStrTab < pVtgHdr->cbStrTab);
return (const char *)pVtgHdr + pVtgHdr->offStrTab + offStrTab;
}
/*
*
* DTrace Provider Interface.
* DTrace Provider Interface.
* DTrace Provider Interface.
*
*/
/**
* @callback_method_impl{dtrace_pops_t,dtps_provide}
*/
static void vboxDtPOps_Provide(void *pvProv, const dtrace_probedesc_t *pDtProbeDesc)
{
PSUPDRVVDTPROVIDERCORE pProv = (PSUPDRVVDTPROVIDERCORE)pvProv;
AssertPtrReturnVoid(pProv);
LOG_DTRACE(("%s: %p / %p pDtProbeDesc=%p\n", __FUNCTION__, pProv, pProv->TracerData.DTrace.idProvider, pDtProbeDesc));
if (pDtProbeDesc)
return; /* We don't generate probes, so never mind these requests. */
if (pProv->TracerData.DTrace.fZombie)
return;
dtrace_provider_id_t const idProvider = pProv->TracerData.DTrace.idProvider;
AssertPtrReturnVoid(idProvider);
AssertPtrReturnVoid(pProv->pHdr);
AssertReturnVoid(pProv->pHdr->offProbeLocs != 0);
uint32_t const cProbeLocs = pProv->pHdr->cbProbeLocs / sizeof(VTGPROBELOC);
/* Need a buffer for extracting the function names and mangling them in
case of collision. */
size_t const cbFnNmBuf = _4K + _1K;
char *pszFnNmBuf = (char *)RTMemAlloc(cbFnNmBuf);
if (!pszFnNmBuf)
return;
/*
* Itereate the probe location list and register all probes related to
* this provider.
*/
uint16_t const idxProv = (uint16_t)((PVTGDESCPROVIDER)((uintptr_t)pProv->pHdr + pProv->pHdr->offProviders) - pProv->pDesc);
for (uint32_t idxProbeLoc = 0; idxProbeLoc < cProbeLocs; idxProbeLoc++)
{
/* Skip probe location belonging to other providers or once that
we've already reported. */
PCVTGPROBELOC pProbeLocRO = &pProv->paProbeLocsRO[idxProbeLoc];
PVTGDESCPROBE pProbeDesc = pProbeLocRO->pProbe;
if (pProbeDesc->idxProvider != idxProv)
continue;
uint32_t *pidProbe;
if (!pProv->fUmod)
pidProbe = (uint32_t *)&pProbeLocRO->idProbe;
else
pidProbe = &pProv->paR0ProbeLocs[idxProbeLoc].idProbe;
if (*pidProbe != 0)
continue;
/* The function name may need to be stripped since we're using C++
compilers for most of the code. ASSUMES nobody are brave/stupid
enough to use function pointer returns without typedef'ing
properly them (e.g. signal). */
const char *pszPrbName = vboxDtVtgGetString(pProv->pHdr, pProbeDesc->offName);
const char *pszFunc = pProbeLocRO->pszFunction;
const char *psz = strchr(pProbeLocRO->pszFunction, '(');
size_t cch;
if (psz)
{
/* skip blanks preceeding the parameter parenthesis. */
while ( (uintptr_t)psz > (uintptr_t)pProbeLocRO->pszFunction
&& RT_C_IS_BLANK(psz[-1]))
psz--;
/* Find the start of the function name. */
pszFunc = psz - 1;
while ((uintptr_t)pszFunc > (uintptr_t)pProbeLocRO->pszFunction)
{
char ch = pszFunc[-1];
if (!RT_C_IS_ALNUM(ch) && ch != '_' && ch != ':')
break;
pszFunc--;
}
cch = psz - pszFunc;
}
else
cch = strlen(pszFunc);
RTStrCopyEx(pszFnNmBuf, cbFnNmBuf, pszFunc, cch);
/* Look up the probe, if we have one in the same function, mangle
the function name a little to avoid having to deal with having
multiple location entries with the same probe ID. (lazy bird) */
Assert(!*pidProbe);
if (dtrace_probe_lookup(idProvider, pProv->pszModName, pszFnNmBuf, pszPrbName) != DTRACE_IDNONE)
{
RTStrPrintf(pszFnNmBuf+cch, cbFnNmBuf - cch, "-%u", pProbeLocRO->uLine);
if (dtrace_probe_lookup(idProvider, pProv->pszModName, pszFnNmBuf, pszPrbName) != DTRACE_IDNONE)
{
unsigned iOrd = 2;
while (iOrd < 128)
{
RTStrPrintf(pszFnNmBuf+cch, cbFnNmBuf - cch, "-%u-%u", pProbeLocRO->uLine, iOrd);
if (dtrace_probe_lookup(idProvider, pProv->pszModName, pszFnNmBuf, pszPrbName) == DTRACE_IDNONE)
break;
iOrd++;
}
if (iOrd >= 128)
{
LogRel(("VBoxDrv: More than 128 duplicate probe location instances at line %u in function %s [%s], probe %s\n",
pProbeLocRO->uLine, pProbeLocRO->pszFunction, pszFnNmBuf, pszPrbName));
continue;
}
}
}
/* Create the probe. */
AssertCompile(sizeof(*pidProbe) == sizeof(dtrace_id_t));
*pidProbe = dtrace_probe_create(idProvider, pProv->pszModName, pszFnNmBuf, pszPrbName,
1 /*aframes*/, (void *)(uintptr_t)idxProbeLoc);
pProv->TracerData.DTrace.cProvidedProbes++;
}
RTMemFree(pszFnNmBuf);
LOG_DTRACE(("%s: returns\n", __FUNCTION__));
}
/**
* @callback_method_impl{dtrace_pops_t,dtps_enable}
*/
static int vboxDtPOps_Enable(void *pvProv, dtrace_id_t idProbe, void *pvProbe)
{
PSUPDRVVDTPROVIDERCORE pProv = (PSUPDRVVDTPROVIDERCORE)pvProv;
LOG_DTRACE(("%s: %p / %p - %#x / %p\n", __FUNCTION__, pProv, pProv->TracerData.DTrace.idProvider, idProbe, pvProbe));
AssertPtrReturn(pProv->TracerData.DTrace.idProvider, EINVAL);
RT_NOREF_PV(idProbe);
if (!pProv->TracerData.DTrace.fZombie)
{
uint32_t idxProbeLoc = (uint32_t)(uintptr_t)pvProbe;
PVTGPROBELOC32 pProbeLocEn = (PVTGPROBELOC32)( (uintptr_t)pProv->pvProbeLocsEn + idxProbeLoc * pProv->cbProbeLocsEn);
PCVTGPROBELOC pProbeLocRO = (PVTGPROBELOC)&pProv->paProbeLocsRO[idxProbeLoc];
PCVTGDESCPROBE pProbeDesc = pProbeLocRO->pProbe;
uint32_t const idxProbe = pProbeDesc->idxEnabled;
if (!pProv->fUmod)
{
if (!pProbeLocEn->fEnabled)
{
pProbeLocEn->fEnabled = 1;
ASMAtomicIncU32(&pProv->pacProbeEnabled[idxProbe]);
ASMAtomicIncU32(&pProv->pDesc->cProbesEnabled);
ASMAtomicIncU32(&pProv->pDesc->uSettingsSerialNo);
}
}
else
{
/* Update kernel mode structure */
if (!pProv->paR0ProbeLocs[idxProbeLoc].fEnabled)
{
pProv->paR0ProbeLocs[idxProbeLoc].fEnabled = 1;
ASMAtomicIncU32(&pProv->paR0Probes[idxProbe].cEnabled);
ASMAtomicIncU32(&pProv->pDesc->cProbesEnabled);
ASMAtomicIncU32(&pProv->pDesc->uSettingsSerialNo);
}
/* Update user mode structure. */
pProbeLocEn->fEnabled = 1;
pProv->pacProbeEnabled[idxProbe] = pProv->paR0Probes[idxProbe].cEnabled;
}
}
return 0;
}
/**
* @callback_method_impl{dtrace_pops_t,dtps_disable}
*/
static void vboxDtPOps_Disable(void *pvProv, dtrace_id_t idProbe, void *pvProbe)
{
PSUPDRVVDTPROVIDERCORE pProv = (PSUPDRVVDTPROVIDERCORE)pvProv;
AssertPtrReturnVoid(pProv);
LOG_DTRACE(("%s: %p / %p - %#x / %p\n", __FUNCTION__, pProv, pProv->TracerData.DTrace.idProvider, idProbe, pvProbe));
AssertPtrReturnVoid(pProv->TracerData.DTrace.idProvider);
RT_NOREF_PV(idProbe);
if (!pProv->TracerData.DTrace.fZombie)
{
uint32_t idxProbeLoc = (uint32_t)(uintptr_t)pvProbe;
PVTGPROBELOC32 pProbeLocEn = (PVTGPROBELOC32)( (uintptr_t)pProv->pvProbeLocsEn + idxProbeLoc * pProv->cbProbeLocsEn);
PCVTGPROBELOC pProbeLocRO = (PVTGPROBELOC)&pProv->paProbeLocsRO[idxProbeLoc];
PCVTGDESCPROBE pProbeDesc = pProbeLocRO->pProbe;
uint32_t const idxProbe = pProbeDesc->idxEnabled;
if (!pProv->fUmod)
{
if (pProbeLocEn->fEnabled)
{
pProbeLocEn->fEnabled = 0;
ASMAtomicDecU32(&pProv->pacProbeEnabled[idxProbe]);
ASMAtomicDecU32(&pProv->pDesc->cProbesEnabled);
ASMAtomicIncU32(&pProv->pDesc->uSettingsSerialNo);
}
}
else
{
/* Update kernel mode structure */
if (pProv->paR0ProbeLocs[idxProbeLoc].fEnabled)
{
pProv->paR0ProbeLocs[idxProbeLoc].fEnabled = 0;
ASMAtomicDecU32(&pProv->paR0Probes[idxProbe].cEnabled);
ASMAtomicDecU32(&pProv->pDesc->cProbesEnabled);
ASMAtomicIncU32(&pProv->pDesc->uSettingsSerialNo);
}
/* Update user mode structure. */
pProbeLocEn->fEnabled = 0;
pProv->pacProbeEnabled[idxProbe] = pProv->paR0Probes[idxProbe].cEnabled;
}
}
}
/**
* @callback_method_impl{dtrace_pops_t,dtps_getargdesc}
*/
static void vboxDtPOps_GetArgDesc(void *pvProv, dtrace_id_t idProbe, void *pvProbe,
dtrace_argdesc_t *pArgDesc)
{
PSUPDRVVDTPROVIDERCORE pProv = (PSUPDRVVDTPROVIDERCORE)pvProv;
unsigned uArg = pArgDesc->dtargd_ndx;
RT_NOREF_PV(idProbe);
pArgDesc->dtargd_ndx = DTRACE_ARGNONE;
AssertPtrReturnVoid(pProv);
LOG_DTRACE(("%s: %p / %p - %#x / %p uArg=%d\n", __FUNCTION__, pProv, pProv->TracerData.DTrace.idProvider, idProbe, pvProbe, uArg));
AssertPtrReturnVoid(pProv->TracerData.DTrace.idProvider);
if (!pProv->TracerData.DTrace.fZombie)
{
uint32_t idxProbeLoc = (uint32_t)(uintptr_t)pvProbe;
PCVTGPROBELOC pProbeLocRO = (PVTGPROBELOC)&pProv->paProbeLocsRO[idxProbeLoc];
PCVTGDESCPROBE pProbeDesc = pProbeLocRO->pProbe;
PCVTGDESCARGLIST pArgList = (PCVTGDESCARGLIST)( (uintptr_t)pProv->pHdr
+ pProv->pHdr->offArgLists
+ pProbeDesc->offArgList);
AssertReturnVoid(pProbeDesc->offArgList < pProv->pHdr->cbArgLists);
if (uArg < pArgList->cArgs)
{
const char *pszType = vboxDtVtgGetString(pProv->pHdr, pArgList->aArgs[uArg].offType);
size_t cchType = strlen(pszType);
if (cchType < sizeof(pArgDesc->dtargd_native))
{
memcpy(pArgDesc->dtargd_native, pszType, cchType + 1);
/** @todo mapping? */
pArgDesc->dtargd_ndx = uArg;
LOG_DTRACE(("%s: returns dtargd_native = %s\n", __FUNCTION__, pArgDesc->dtargd_native));
return;
}
}
}
}
/**
* @callback_method_impl{dtrace_pops_t,dtps_getargval}
*/
static uint64_t vboxDtPOps_GetArgVal(void *pvProv, dtrace_id_t idProbe, void *pvProbe,
int iArg, int cFrames)
{
PSUPDRVVDTPROVIDERCORE pProv = (PSUPDRVVDTPROVIDERCORE)pvProv;
AssertPtrReturn(pProv, UINT64_MAX);
LOG_DTRACE(("%s: %p / %p - %#x / %p iArg=%d cFrames=%u\n", __FUNCTION__, pProv, pProv->TracerData.DTrace.idProvider, idProbe, pvProbe, iArg, cFrames));
AssertReturn(iArg >= 5, UINT64_MAX);
RT_NOREF_PV(idProbe); RT_NOREF_PV(cFrames);
if (pProv->TracerData.DTrace.fZombie)
return UINT64_MAX;
uint32_t idxProbeLoc = (uint32_t)(uintptr_t)pvProbe;
PCVTGPROBELOC pProbeLocRO = (PVTGPROBELOC)&pProv->paProbeLocsRO[idxProbeLoc];
PCVTGDESCPROBE pProbeDesc = pProbeLocRO->pProbe;
PCVTGDESCARGLIST pArgList = (PCVTGDESCARGLIST)( (uintptr_t)pProv->pHdr
+ pProv->pHdr->offArgLists
+ pProbeDesc->offArgList);
AssertReturn(pProbeDesc->offArgList < pProv->pHdr->cbArgLists, UINT64_MAX);
PVBDTSTACKDATA pData = vboxDtGetStackData();
/*
* Get the stack data. This is a wee bit complicated on 32-bit systems
* since we want to support 64-bit integer arguments.
*/
uint64_t u64Ret;
if (iArg >= 20)
u64Ret = UINT64_MAX;
else if (pData->enmCaller == kVBoxDtCaller_ProbeFireKernel)
{
#if ARCH_BITS == 64
u64Ret = pData->u.ProbeFireKernel.pauStackArgs[iArg - 5];
#else
if ( !pArgList->fHaveLargeArgs
|| iArg >= pArgList->cArgs)
u64Ret = pData->u.ProbeFireKernel.pauStackArgs[iArg - 5];
else
{
/* Similar to what we did for mac in when calling dtrace_probe(). */
uint32_t offArg = 0;
for (int i = 5; i < iArg; i++)
if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iArg].fType))
offArg++;
u64Ret = pData->u.ProbeFireKernel.pauStackArgs[iArg - 5 + offArg];
if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iArg].fType))
u64Ret |= (uint64_t)pData->u.ProbeFireKernel.pauStackArgs[iArg - 5 + offArg + 1] << 32;
}
#endif
}
else if (pData->enmCaller == kVBoxDtCaller_ProbeFireUser)
{
int offArg = pData->u.ProbeFireUser.offArg;
PCSUPDRVTRACERUSRCTX pCtx = pData->u.ProbeFireUser.pCtx;
AssertPtrReturn(pCtx, UINT64_MAX);
if (pCtx->cBits == 32)
{
if ( !pArgList->fHaveLargeArgs
|| iArg >= pArgList->cArgs)
{
if (iArg + offArg < (int)RT_ELEMENTS(pCtx->u.X86.aArgs))
u64Ret = pCtx->u.X86.aArgs[iArg + offArg];
else
u64Ret = UINT64_MAX;
}
else
{
for (int i = 5; i < iArg; i++)
if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iArg].fType))
offArg++;
if (offArg + iArg < (int)RT_ELEMENTS(pCtx->u.X86.aArgs))
{
u64Ret = pCtx->u.X86.aArgs[iArg + offArg];
if ( VTG_TYPE_IS_LARGE(pArgList->aArgs[iArg].fType)
&& offArg + iArg + 1 < (int)RT_ELEMENTS(pCtx->u.X86.aArgs))
u64Ret |= (uint64_t)pCtx->u.X86.aArgs[iArg + offArg + 1] << 32;
}
else
u64Ret = UINT64_MAX;
}
}
else
{
if (iArg + offArg < (int)RT_ELEMENTS(pCtx->u.Amd64.aArgs))
u64Ret = pCtx->u.Amd64.aArgs[iArg + offArg];
else
u64Ret = UINT64_MAX;
}
}
else
AssertFailedReturn(UINT64_MAX);
LOG_DTRACE(("%s: returns %#llx\n", __FUNCTION__, u64Ret));
return u64Ret;
}
/**
* @callback_method_impl{dtrace_pops_t,dtps_destroy}
*/
static void vboxDtPOps_Destroy(void *pvProv, dtrace_id_t idProbe, void *pvProbe)
{
PSUPDRVVDTPROVIDERCORE pProv = (PSUPDRVVDTPROVIDERCORE)pvProv;
AssertPtrReturnVoid(pProv);
LOG_DTRACE(("%s: %p / %p - %#x / %p\n", __FUNCTION__, pProv, pProv->TracerData.DTrace.idProvider, idProbe, pvProbe));
AssertReturnVoid(pProv->TracerData.DTrace.cProvidedProbes > 0);
AssertPtrReturnVoid(pProv->TracerData.DTrace.idProvider);
if (!pProv->TracerData.DTrace.fZombie)
{
uint32_t idxProbeLoc = (uint32_t)(uintptr_t)pvProbe;
PCVTGPROBELOC pProbeLocRO = (PVTGPROBELOC)&pProv->paProbeLocsRO[idxProbeLoc];
uint32_t *pidProbe;
if (!pProv->fUmod)
{
pidProbe = (uint32_t *)&pProbeLocRO->idProbe;
Assert(!pProbeLocRO->fEnabled);
Assert(*pidProbe == idProbe);
}
else
{
pidProbe = &pProv->paR0ProbeLocs[idxProbeLoc].idProbe;
Assert(!pProv->paR0ProbeLocs[idxProbeLoc].fEnabled);
Assert(*pidProbe == idProbe); NOREF(idProbe);
}
*pidProbe = 0;
}
pProv->TracerData.DTrace.cProvidedProbes--;
}
/**
* DTrace provider method table.
*/
static const dtrace_pops_t g_vboxDtVtgProvOps =
{
/* .dtps_provide = */ vboxDtPOps_Provide,
/* .dtps_provide_module = */ NULL,
/* .dtps_enable = */ vboxDtPOps_Enable,
/* .dtps_disable = */ vboxDtPOps_Disable,
/* .dtps_suspend = */ NULL,
/* .dtps_resume = */ NULL,
/* .dtps_getargdesc = */ vboxDtPOps_GetArgDesc,
/* .dtps_getargval = */ vboxDtPOps_GetArgVal,
/* .dtps_usermode = */ NULL,
/* .dtps_destroy = */ vboxDtPOps_Destroy
};
/*
*
* Support Driver Tracer Interface.
* Support Driver Tracer Interface.
* Support Driver Tracer Interface.
*
*/
/**
* interface_method_impl{SUPDRVTRACERREG,pfnProbeFireKernel}
*/
static DECLCALLBACK(void) vboxDtTOps_ProbeFireKernel(struct VTGPROBELOC *pVtgProbeLoc, uintptr_t uArg0, uintptr_t uArg1, uintptr_t uArg2,
uintptr_t uArg3, uintptr_t uArg4)
{
AssertPtrReturnVoid(pVtgProbeLoc);
LOG_DTRACE(("%s: %p / %p\n", __FUNCTION__, pVtgProbeLoc, pVtgProbeLoc->idProbe));
AssertPtrReturnVoid(pVtgProbeLoc->pProbe);
AssertPtrReturnVoid(pVtgProbeLoc->pszFunction);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_ProbeFireKernel);
pStackData->u.ProbeFireKernel.pauStackArgs = &uArg4 + 1;
#if defined(RT_OS_DARWIN) && ARCH_BITS == 32
/*
* Convert arguments from uintptr_t to uint64_t.
*/
PVTGDESCPROBE pProbe = pVtgProbeLoc->pProbe;
AssertPtrReturnVoid(pProbe);
PVTGOBJHDR pVtgHdr = (PVTGOBJHDR)((uintptr_t)pProbe + pProbe->offObjHdr);
AssertPtrReturnVoid(pVtgHdr);
PVTGDESCARGLIST pArgList = (PVTGDESCARGLIST)((uintptr_t)pVtgHdr + pVtgHdr->offArgLists + pProbe->offArgList);
AssertPtrReturnVoid(pArgList);
if (!pArgList->fHaveLargeArgs)
dtrace_probe(pVtgProbeLoc->idProbe, uArg0, uArg1, uArg2, uArg3, uArg4);
else
{
uintptr_t *auSrcArgs = &uArg0;
uint32_t iSrcArg = 0;
uint32_t iDstArg = 0;
uint64_t au64DstArgs[5];
while ( iDstArg < RT_ELEMENTS(au64DstArgs)
&& iSrcArg < pArgList->cArgs)
{
au64DstArgs[iDstArg] = auSrcArgs[iSrcArg];
if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iDstArg].fType))
au64DstArgs[iDstArg] |= (uint64_t)auSrcArgs[++iSrcArg] << 32;
iSrcArg++;
iDstArg++;
}
while (iDstArg < RT_ELEMENTS(au64DstArgs))
au64DstArgs[iDstArg++] = auSrcArgs[iSrcArg++];
pStackData->u.ProbeFireKernel.pauStackArgs = &auSrcArgs[iSrcArg];
dtrace_probe(pVtgProbeLoc->idProbe, au64DstArgs[0], au64DstArgs[1], au64DstArgs[2], au64DstArgs[3], au64DstArgs[4]);
}
#else
dtrace_probe(pVtgProbeLoc->idProbe, uArg0, uArg1, uArg2, uArg3, uArg4);
#endif
VBDT_CLEAR_STACK_DATA();
LOG_DTRACE(("%s: returns\n", __FUNCTION__));
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnProbeFireUser}
*/
static DECLCALLBACK(void) vboxDtTOps_ProbeFireUser(PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, PCSUPDRVTRACERUSRCTX pCtx,
PCVTGOBJHDR pVtgHdr, PCVTGPROBELOC pProbeLocRO)
{
LOG_DTRACE(("%s: %p / %p\n", __FUNCTION__, pCtx, pCtx->idProbe));
AssertPtrReturnVoid(pProbeLocRO);
AssertPtrReturnVoid(pVtgHdr);
RT_NOREF_PV(pThis);
RT_NOREF_PV(pSession);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_ProbeFireUser);
if (pCtx->cBits == 32)
{
pStackData->u.ProbeFireUser.pCtx = pCtx;
pStackData->u.ProbeFireUser.offArg = 0;
#if ARCH_BITS == 64 || defined(RT_OS_DARWIN)
/*
* Combine two 32-bit arguments into one 64-bit argument where needed.
*/
PVTGDESCPROBE pProbeDesc = pProbeLocRO->pProbe;
AssertPtrReturnVoid(pProbeDesc);
PVTGDESCARGLIST pArgList = (PVTGDESCARGLIST)((uintptr_t)pVtgHdr + pVtgHdr->offArgLists + pProbeDesc->offArgList);
AssertPtrReturnVoid(pArgList);
if (!pArgList->fHaveLargeArgs)
dtrace_probe(pCtx->idProbe,
pCtx->u.X86.aArgs[0],
pCtx->u.X86.aArgs[1],
pCtx->u.X86.aArgs[2],
pCtx->u.X86.aArgs[3],
pCtx->u.X86.aArgs[4]);
else
{
uint32_t const *auSrcArgs = &pCtx->u.X86.aArgs[0];
uint32_t iSrcArg = 0;
uint32_t iDstArg = 0;
uint64_t au64DstArgs[5];
while ( iDstArg < RT_ELEMENTS(au64DstArgs)
&& iSrcArg < pArgList->cArgs)
{
au64DstArgs[iDstArg] = auSrcArgs[iSrcArg];
if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iDstArg].fType))
au64DstArgs[iDstArg] |= (uint64_t)auSrcArgs[++iSrcArg] << 32;
iSrcArg++;
iDstArg++;
}
while (iDstArg < RT_ELEMENTS(au64DstArgs))
au64DstArgs[iDstArg++] = auSrcArgs[iSrcArg++];
pStackData->u.ProbeFireUser.offArg = iSrcArg - RT_ELEMENTS(au64DstArgs);
dtrace_probe(pCtx->idProbe, au64DstArgs[0], au64DstArgs[1], au64DstArgs[2], au64DstArgs[3], au64DstArgs[4]);
}
#else
dtrace_probe(pCtx->idProbe,
pCtx->u.X86.aArgs[0],
pCtx->u.X86.aArgs[1],
pCtx->u.X86.aArgs[2],
pCtx->u.X86.aArgs[3],
pCtx->u.X86.aArgs[4]);
#endif
}
else if (pCtx->cBits == 64)
{
pStackData->u.ProbeFireUser.pCtx = pCtx;
pStackData->u.ProbeFireUser.offArg = 0;
dtrace_probe(pCtx->idProbe,
pCtx->u.Amd64.aArgs[0],
pCtx->u.Amd64.aArgs[1],
pCtx->u.Amd64.aArgs[2],
pCtx->u.Amd64.aArgs[3],
pCtx->u.Amd64.aArgs[4]);
}
else
AssertFailed();
VBDT_CLEAR_STACK_DATA();
LOG_DTRACE(("%s: returns\n", __FUNCTION__));
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnTracerOpen}
*/
static DECLCALLBACK(int) vboxDtTOps_TracerOpen(PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uint32_t uCookie,
uintptr_t uArg, uintptr_t *puSessionData)
{
if (uCookie != RT_MAKE_U32_FROM_U8('V', 'B', 'D', 'T'))
return VERR_INVALID_MAGIC;
if (uArg)
return VERR_INVALID_PARAMETER;
RT_NOREF_PV(pThis); RT_NOREF_PV(pSession);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_Generic);
int rc = dtrace_open((dtrace_state_t **)puSessionData, VBoxDtGetCurrentCreds());
VBDT_CLEAR_STACK_DATA();
return RTErrConvertFromErrno(rc);
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnTracerClose}
*/
static DECLCALLBACK(int) vboxDtTOps_TracerIoCtl(PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData,
uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
{
AssertPtrReturn(uSessionData, VERR_INVALID_POINTER);
RT_NOREF_PV(pThis); RT_NOREF_PV(pSession);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_Generic);
int rc = dtrace_ioctl((dtrace_state_t *)uSessionData, (intptr_t)uCmd, (intptr_t)uArg, piRetVal);
VBDT_CLEAR_STACK_DATA();
return RTErrConvertFromErrno(rc);
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnTracerClose}
*/
static DECLCALLBACK(void) vboxDtTOps_TracerClose(PCSUPDRVTRACERREG pThis, PSUPDRVSESSION pSession, uintptr_t uSessionData)
{
AssertPtrReturnVoid(uSessionData);
RT_NOREF_PV(pThis); RT_NOREF_PV(pSession);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_Generic);
dtrace_close((dtrace_state_t *)uSessionData);
VBDT_CLEAR_STACK_DATA();
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnProviderRegister}
*/
static DECLCALLBACK(int) vboxDtTOps_ProviderRegister(PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore)
{
LOG_DTRACE(("%s: %p %s/%s\n", __FUNCTION__, pThis, pCore->pszModName, pCore->pszName));
AssertReturn(pCore->TracerData.DTrace.idProvider == 0, VERR_INTERNAL_ERROR_3);
RT_NOREF_PV(pThis);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_Generic);
PVTGDESCPROVIDER pDesc = pCore->pDesc;
dtrace_pattr_t DtAttrs;
vboxDtVtgConvAttr(&DtAttrs.dtpa_provider, &pDesc->AttrSelf);
vboxDtVtgConvAttr(&DtAttrs.dtpa_mod, &pDesc->AttrModules);
vboxDtVtgConvAttr(&DtAttrs.dtpa_func, &pDesc->AttrFunctions);
vboxDtVtgConvAttr(&DtAttrs.dtpa_name, &pDesc->AttrNames);
vboxDtVtgConvAttr(&DtAttrs.dtpa_args, &pDesc->AttrArguments);
/* Note! DTrace may call us back before dtrace_register returns, so we
have to point it to pCore->TracerData.DTrace.idProvider. */
AssertCompile(sizeof(dtrace_provider_id_t) == sizeof(pCore->TracerData.DTrace.idProvider));
int rc = dtrace_register(pCore->pszName,
&DtAttrs,
DTRACE_PRIV_KERNEL,
NULL /* cred */,
&g_vboxDtVtgProvOps,
pCore,
&pCore->TracerData.DTrace.idProvider);
if (!rc)
{
LOG_DTRACE(("%s: idProvider=%p\n", __FUNCTION__, pCore->TracerData.DTrace.idProvider));
AssertPtr(pCore->TracerData.DTrace.idProvider);
rc = VINF_SUCCESS;
}
else
{
pCore->TracerData.DTrace.idProvider = 0;
rc = RTErrConvertFromErrno(rc);
}
VBDT_CLEAR_STACK_DATA();
LOG_DTRACE(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnProviderDeregister}
*/
static DECLCALLBACK(int) vboxDtTOps_ProviderDeregister(PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore)
{
uintptr_t idProvider = pCore->TracerData.DTrace.idProvider;
LOG_DTRACE(("%s: %p / %p\n", __FUNCTION__, pThis, idProvider));
AssertPtrReturn(idProvider, VERR_INTERNAL_ERROR_3);
RT_NOREF_PV(pThis);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_Generic);
dtrace_invalidate(idProvider);
int rc = dtrace_unregister(idProvider);
if (!rc)
{
pCore->TracerData.DTrace.idProvider = 0;
rc = VINF_SUCCESS;
}
else
{
AssertMsg(rc == EBUSY, ("%d\n", rc));
pCore->TracerData.DTrace.fZombie = true;
rc = VERR_TRY_AGAIN;
}
VBDT_CLEAR_STACK_DATA();
LOG_DTRACE(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/**
* interface_method_impl{SUPDRVTRACERREG,pfnProviderDeregisterZombie}
*/
static DECLCALLBACK(int) vboxDtTOps_ProviderDeregisterZombie(PCSUPDRVTRACERREG pThis, PSUPDRVVDTPROVIDERCORE pCore)
{
uintptr_t idProvider = pCore->TracerData.DTrace.idProvider;
LOG_DTRACE(("%s: %p / %p\n", __FUNCTION__, pThis, idProvider));
AssertPtrReturn(idProvider, VERR_INTERNAL_ERROR_3);
Assert(pCore->TracerData.DTrace.fZombie);
RT_NOREF_PV(pThis);
VBDT_SETUP_STACK_DATA(kVBoxDtCaller_Generic);
int rc = dtrace_unregister(idProvider);
if (!rc)
{
pCore->TracerData.DTrace.idProvider = 0;
rc = VINF_SUCCESS;
}
else
{
AssertMsg(rc == EBUSY, ("%d\n", rc));
rc = VERR_TRY_AGAIN;
}
VBDT_CLEAR_STACK_DATA();
LOG_DTRACE(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/**
* The tracer registration record of the VBox DTrace implementation
*/
static SUPDRVTRACERREG g_VBoxDTraceReg =
{
SUPDRVTRACERREG_MAGIC,
SUPDRVTRACERREG_VERSION,
vboxDtTOps_ProbeFireKernel,
vboxDtTOps_ProbeFireUser,
vboxDtTOps_TracerOpen,
vboxDtTOps_TracerIoCtl,
vboxDtTOps_TracerClose,
vboxDtTOps_ProviderRegister,
vboxDtTOps_ProviderDeregister,
vboxDtTOps_ProviderDeregisterZombie,
SUPDRVTRACERREG_MAGIC
};
/**
* Module termination code.
*
* @param hMod Opque module handle.
*/
DECLEXPORT(void) ModuleTerm(void *hMod)
{
SUPR0TracerDeregisterImpl(hMod, NULL);
dtrace_detach();
vboxDtTermThreadDb();
}
/**
* Module initialization code.
*
* @param hMod Opque module handle.
*/
DECLEXPORT(int) ModuleInit(void *hMod)
{
int rc = vboxDtInitThreadDb();
if (RT_SUCCESS(rc))
{
rc = dtrace_attach();
if (rc == DDI_SUCCESS)
{
rc = SUPR0TracerRegisterImpl(hMod, NULL, &g_VBoxDTraceReg, &g_pVBoxDTraceHlp);
if (RT_SUCCESS(rc))
return rc;
dtrace_detach();
}
else
{
SUPR0Printf("dtrace_attach -> %d\n", rc);
rc = VERR_INTERNAL_ERROR_5;
}
vboxDtTermThreadDb();
}
else
SUPR0Printf("vboxDtInitThreadDb -> %d\n", rc);
return rc;
}
|