VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPDrvTracer.cpp@ 86607

Last change on this file since 86607 was 85766, checked in by vboxsync, 4 years ago

SUPDrv: Use symbol_get to resolve dtrace symbols on linux. Needed for 5.7+ and fixes reference issue. (untested) bugref:9733

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 90.0 KB
Line 
1/* $Id: SUPDrvTracer.cpp 85766 2020-08-14 12:27:41Z vboxsync $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - Tracer Interface.
4 */
5
6/*
7 * Copyright (C) 2012-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP LOG_GROUP_SUP_DRV
32#define SUPDRV_AGNOSTIC
33#include "SUPDrvInternal.h"
34
35#include <VBox/err.h>
36#include <VBox/log.h>
37#include <VBox/VBoxTpG.h>
38
39#include <iprt/assert.h>
40#include <iprt/ctype.h>
41#include <iprt/list.h>
42#include <iprt/mem.h>
43#include <iprt/semaphore.h>
44#include <iprt/thread.h>
45#include <iprt/param.h>
46#include <iprt/uuid.h>
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52/** Pointer to a user tracer module registration record. */
53typedef struct SUPDRVTRACERUMOD *PSUPDRVTRACERUMOD;
54
55/**
56 * Data for a tracepoint provider.
57 */
58typedef struct SUPDRVTPPROVIDER
59{
60 /** The entry in the provider list for this image. */
61 RTLISTNODE ListEntry;
62 /** The entry in the per session provider list for this image. */
63 RTLISTNODE SessionListEntry;
64
65 /** The core structure. */
66 SUPDRVVDTPROVIDERCORE Core;
67
68 /** Pointer to the image this provider resides in. NULL if it's a
69 * driver. */
70 PSUPDRVLDRIMAGE pImage;
71 /** The session this provider is associated with if registered via
72 * SUPR0VtgRegisterDrv. NULL if pImage is set. */
73 PSUPDRVSESSION pSession;
74 /** The user tracepoint module associated with this provider. NULL if
75 * pImage is set. */
76 PSUPDRVTRACERUMOD pUmod;
77
78 /** Used to indicate that we've called pfnProviderDeregistered already and it
79 * failed because the provider was busy. Next time we must try
80 * pfnProviderDeregisterZombie.
81 *
82 * @remarks This does not necessiarly mean the provider is in the zombie
83 * list. See supdrvTracerCommonDeregisterImpl. */
84 bool fZombie;
85 /** Set if the provider has been successfully registered with the
86 * tracer. */
87 bool fRegistered;
88 /** The provider name (for logging purposes). */
89 char szName[1];
90} SUPDRVTPPROVIDER;
91/** Pointer to the data for a tracepoint provider. */
92typedef SUPDRVTPPROVIDER *PSUPDRVTPPROVIDER;
93
94
95/**
96 * User tracer module VTG data copy.
97 */
98typedef struct SUPDRVVTGCOPY
99{
100 /** Magic (SUDPRVVTGCOPY_MAGIC). */
101 uint32_t u32Magic;
102 /** Refernece counter (we expect to share a lot of these). */
103 uint32_t cRefs;
104 /** The size of the */
105 uint32_t cbStrTab;
106 /** Image type flags. */
107 uint32_t fFlags;
108 /** Hash list entry (SUPDRVDEVEXT::aTrackerUmodHash). */
109 RTLISTNODE ListEntry;
110 /** The VTG object header.
111 * The rest of the data follows immediately afterwards. First the object,
112 * then the probe locations and finally the probe location string table. All
113 * pointers are fixed up to point within this data. */
114 VTGOBJHDR Hdr;
115} SUPDRVVTGCOPY;
116/** Pointer to a VTG object copy. */
117typedef SUPDRVVTGCOPY *PSUPDRVVTGCOPY;
118/** Magic value for SUPDRVVTGCOPY. */
119#define SUDPRVVTGCOPY_MAGIC UINT32_C(0x00080386)
120
121
122/**
123 * User tracer module registration record.
124 */
125typedef struct SUPDRVTRACERUMOD
126{
127 /** Magic (SUPDRVTRACERUMOD_MAGIC). */
128 uint32_t u32Magic;
129 /** List entry. This is anchored in SUPDRVSESSION::UmodList. */
130 RTLISTNODE ListEntry;
131 /** The address of the ring-3 VTG header. */
132 RTR3PTR R3PtrVtgHdr;
133 /** Pointer to the ring-0 copy of the VTG data. */
134 PSUPDRVVTGCOPY pVtgCopy;
135 /** The memory object that locks down the user memory. */
136 RTR0MEMOBJ hMemObjLock;
137 /** The memory object that maps the locked memory into kernel space. */
138 RTR0MEMOBJ hMemObjMap;
139 /** Pointer to the probe enabled-count array within the mapping. */
140 uint32_t *pacProbeEnabled;
141 /** Pointer to the probe location array within the mapping. */
142 void *pvProbeLocs;
143 /** The address of the ring-3 probe locations. */
144 RTR3PTR R3PtrProbeLocs;
145 /** The lookup table index. */
146 uint8_t iLookupTable;
147 /** The module bit count. */
148 uint8_t cBits;
149 /** The size of a probe location record. */
150 uint8_t cbProbeLoc;
151 /** The number of probe locations. */
152 uint32_t cProbeLocs;
153 /** Ring-0 probe location info. */
154 SUPDRVPROBELOC aProbeLocs[1];
155} SUPDRVTRACERUMOD;
156/** Magic value for SUPDRVVTGCOPY. */
157#define SUPDRVTRACERUMOD_MAGIC UINT32_C(0x00080486)
158
159
160/*********************************************************************************************************************************
161* Defined Constants And Macros *
162*********************************************************************************************************************************/
163/** Simple SUPR0Printf-style logging. */
164#ifdef DEBUG_bird
165# define LOG_TRACER(a_Args) SUPR0Printf a_Args
166#else
167# define LOG_TRACER(a_Args) do { } while (0)
168#endif
169
170
171/*********************************************************************************************************************************
172* Global Variables *
173*********************************************************************************************************************************/
174/** The address of the current probe fire routine for kernel mode. */
175PFNRT g_pfnSupdrvProbeFireKernel = supdrvTracerProbeFireStub;
176
177
178/*********************************************************************************************************************************
179* Internal Functions *
180*********************************************************************************************************************************/
181static void supdrvVtgReleaseObjectCopy(PSUPDRVDEVEXT pDevExt, PSUPDRVVTGCOPY pThis);
182
183
184
185/**
186 * Validates a VTG string against length and characterset limitations.
187 *
188 * @returns VINF_SUCCESS, VERR_SUPDRV_VTG_BAD_STRING or
189 * VERR_SUPDRV_VTG_STRING_TOO_LONG.
190 * @param psz The string.
191 */
192static int supdrvVtgValidateString(const char *psz)
193{
194 size_t off = 0;
195 while (off < _4K)
196 {
197 char const ch = psz[off++];
198 if (!ch)
199 return VINF_SUCCESS;
200 if ( !RTLocCIsAlNum(ch)
201 && ch != ' '
202 && ch != '_'
203 && ch != '-'
204 && ch != '('
205 && ch != ')'
206 && ch != ','
207 && ch != '*'
208 && ch != '&'
209 )
210 {
211 /*RTAssertMsg2("off=%u '%s'\n", off, psz);*/
212 return VERR_SUPDRV_VTG_BAD_STRING;
213 }
214 }
215 return VERR_SUPDRV_VTG_STRING_TOO_LONG;
216}
217
218
219/** Used by the validation code below. */
220#define MY_CHECK_RET(a_Expr, a_rc) \
221 MY_CHECK_MSG_RET(a_Expr, ("%s: Validation failed on line " RT_XSTR(__LINE__) ": " #a_Expr "\n", __FUNCTION__), a_rc)
222
223/** Used by the validation code below. */
224#define MY_CHECK_MSG_RET(a_Expr, a_PrintfArgs, a_rc) \
225 do { if (RT_UNLIKELY(!(a_Expr))) { SUPR0Printf a_PrintfArgs; return (a_rc); } } while (0)
226
227/** Used by the validation code below. */
228#define MY_WITHIN_IMAGE(p, rc) \
229 do { \
230 if (pbImage) \
231 { \
232 if ((uintptr_t)(p) - (uintptr_t)pbImage > cbImage) \
233 { \
234 SUPR0Printf("supdrvVtgValidate: " #rc " - p=%p pbImage=%p cbImage=%#zxline=%u %s\n", \
235 p, pbImage, cbImage, #p); \
236 return (rc); \
237 } \
238 } \
239 else if (!RT_VALID_PTR(p)) \
240 return (rc); \
241 } while (0)
242
243
244/**
245 * Validates the VTG object header.
246 *
247 * @returns VBox status code.
248 * @param pVtgHdr The header.
249 * @param uVtgHdrAddr The address where the header is actually
250 * loaded.
251 * @param pbImage The image base, if available.
252 * @param cbImage The image size, if available.
253 * @param fUmod Whether this is a user module.
254 */
255static int supdrvVtgValidateHdr(PVTGOBJHDR pVtgHdr, RTUINTPTR uVtgHdrAddr, const uint8_t *pbImage, size_t cbImage, bool fUmod)
256{
257 struct VTGAREAS
258 {
259 uint32_t off;
260 uint32_t cb;
261 } const *paAreas;
262 unsigned cAreas;
263 unsigned i;
264 uint32_t cbVtgObj;
265 uint32_t off;
266
267#define MY_VALIDATE_SIZE(cb, cMin, cMax, cbUnit, rcBase) \
268 do { \
269 if ((cb) < (cMin) * (cbUnit)) \
270 { \
271 SUPR0Printf("supdrvVtgValidateHdr: " #rcBase "_TOO_FEW - cb=%#zx cMin=%#zx cbUnit=%#zx line=%u %s\n", \
272 (size_t)(cb), (size_t)(cMin), (size_t)cbUnit, __LINE__, #cb); \
273 return rcBase ## _TOO_FEW; \
274 } \
275 if ((cb) >= (cMax) * (cbUnit)) \
276 { \
277 SUPR0Printf("supdrvVtgValidateHdr: " #rcBase "_TOO_MUCH - cb=%#zx cMax=%#zx cbUnit=%#zx line=%u %s\n", \
278 (size_t)(cb), (size_t)(cMax), (size_t)cbUnit, __LINE__, #cb); \
279 return rcBase ## _TOO_MUCH; \
280 } \
281 if ((cb) / (cbUnit) * (cbUnit) != (cb)) \
282 { \
283 SUPR0Printf("supdrvVtgValidateHdr: " #rcBase "_NOT_MULTIPLE - cb=%#zx cbUnit=%#zx line=%u %s\n", \
284 (size_t)(cb), (size_t)cbUnit, __LINE__, #cb); \
285 return rcBase ## _NOT_MULTIPLE; \
286 } \
287 } while (0)
288
289#define MY_VALIDATE_OFF(off, cb, cMin, cMax, cbUnit, cbAlign, rcBase) \
290 do { \
291 if ( (cb) >= cbVtgObj \
292 || off > cbVtgObj - (cb) ) \
293 { \
294 SUPR0Printf("supdrvVtgValidateHdr: " #rcBase "_OFF - off=%#x cb=%#x pVtgHdr=%p cbVtgHdr=%#zx line=%u %s\n", \
295 (off), (cb), pVtgHdr, cbVtgObj, __LINE__, #off); \
296 return rcBase ## _OFF; \
297 } \
298 if (RT_ALIGN(off, cbAlign) != (off)) \
299 { \
300 SUPR0Printf("supdrvVtgValidateHdr: " #rcBase "_OFF - off=%#x align=%#zx line=%u %s\n", \
301 (off), (size_t)(cbAlign), __LINE__, #off); \
302 return rcBase ## _OFF; \
303 } \
304 MY_VALIDATE_SIZE(cb, cMin, cMax, cbUnit, rcBase); \
305 } while (0)
306
307 /*
308 * Make sure both pbImage and cbImage are NULL/0 if one if of them is.
309 */
310 if (!pbImage || !cbImage)
311 {
312 pbImage = NULL;
313 cbImage = 0;
314 cbVtgObj = pVtgHdr->cbObj;
315 }
316 else
317 {
318 MY_WITHIN_IMAGE(pVtgHdr, VERR_SUPDRV_VTG_BAD_HDR_PTR);
319 cbVtgObj = pVtgHdr->cbObj;
320 MY_WITHIN_IMAGE((uint8_t *)pVtgHdr + cbVtgObj - 1, VERR_SUPDRV_VTG_BAD_HDR_PTR);
321 }
322
323 if (cbVtgObj > _1M)
324 {
325 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_TRACER_TOO_LARGE - cbVtgObj=%#x\n", cbVtgObj);
326 return VERR_SUPDRV_TRACER_TOO_LARGE;
327 }
328
329 /*
330 * Set the probe location array offset and size members.
331 */
332 if (!pVtgHdr->offProbeLocs)
333 {
334 uint64_t u64Tmp = pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64;
335 if (u64Tmp >= UINT32_MAX)
336 {
337 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH - u64Tmp=%#llx ProbeLocs=%#llx ProbeLocsEnd=%#llx\n",
338 u64Tmp, pVtgHdr->uProbeLocs.u64, pVtgHdr->uProbeLocsEnd.u64);
339 return VERR_SUPDRV_VTG_BAD_HDR_TOO_MUCH;
340 }
341 /*SUPR0Printf("supdrvVtgValidateHdr: cbProbeLocs %#x -> %#x\n", pVtgHdr->cbProbeLocs, (uint32_t)u64Tmp);*/
342 pVtgHdr->cbProbeLocs = (uint32_t)u64Tmp;
343
344 u64Tmp = pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr;
345#ifdef RT_OS_DARWIN
346 /* The loader and/or ld64-97.17 seems not to generate fixups for our
347 __VTGObj section. Detect this by comparing them with the
348 u64VtgObjSectionStart member and assume max image size of 4MB.
349 Seems to be worked around by the __VTGPrLc.End and __VTGPrLc.Begin
350 padding fudge, meaning that the linker misplaced the relocations. */
351 if ( (int64_t)u64Tmp != (int32_t)u64Tmp
352 && pVtgHdr->u64VtgObjSectionStart != uVtgHdrAddr
353 && pVtgHdr->u64VtgObjSectionStart < _4M
354 && pVtgHdr->uProbeLocsEnd.u64 < _4M
355 && !fUmod)
356 {
357 uint64_t offDelta = uVtgHdrAddr - pVtgHdr->u64VtgObjSectionStart;
358 /*SUPR0Printf("supdrvVtgValidateHdr: offDelta=%#llx\n", offDelta);*/
359 pVtgHdr->uProbeLocs.u64 += offDelta;
360 pVtgHdr->uProbeLocsEnd.u64 += offDelta;
361 u64Tmp += offDelta;
362 }
363#endif
364 if ((int64_t)u64Tmp != (int32_t)u64Tmp)
365 {
366 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_PTR - u64Tmp=%#llx uProbeLocs=%#llx uVtgHdrAddr=%RTptr\n",
367 u64Tmp, pVtgHdr->uProbeLocs.u64, uVtgHdrAddr);
368 return VERR_SUPDRV_VTG_BAD_HDR_PTR;
369 }
370 /*SUPR0Printf("supdrvVtgValidateHdr: offProbeLocs %#x -> %#x\n", pVtgHdr->offProbeLocs, (int32_t)u64Tmp);*/
371 pVtgHdr->offProbeLocs = (int32_t)u64Tmp;
372 }
373
374 /*
375 * The non-area description fields.
376 */
377 if (memcmp(pVtgHdr->szMagic, VTGOBJHDR_MAGIC, sizeof(pVtgHdr->szMagic)))
378 {
379 SUPR0Printf("supdrvVtgValidateHdr: %p: %.16Rhxs\n", pVtgHdr, pVtgHdr->szMagic);
380 return VERR_SUPDRV_VTG_MAGIC;
381 }
382 if ( pVtgHdr->cBits != ARCH_BITS
383 && ( !fUmod
384 || ( pVtgHdr->cBits != 32
385 && pVtgHdr->cBits != 64)) )
386 return VERR_SUPDRV_VTG_BITS;
387 MY_CHECK_RET(pVtgHdr->au32Reserved1[0] == 0, VERR_SUPDRV_VTG_BAD_HDR_MISC);
388 MY_CHECK_RET(pVtgHdr->au32Reserved1[1] == 0, VERR_SUPDRV_VTG_BAD_HDR_MISC);
389 MY_CHECK_RET(!RTUuidIsNull(&pVtgHdr->Uuid), VERR_SUPDRV_VTG_BAD_HDR_MISC);
390
391 /*
392 * Check the individual area descriptors.
393 */
394 MY_VALIDATE_OFF(pVtgHdr->offStrTab, pVtgHdr->cbStrTab, 4, _1M, sizeof(char), sizeof(uint8_t), VERR_SUPDRV_VTG_BAD_HDR);
395 MY_VALIDATE_OFF(pVtgHdr->offArgLists, pVtgHdr->cbArgLists, 1, _32K, sizeof(uint32_t), sizeof(uint32_t), VERR_SUPDRV_VTG_BAD_HDR);
396 MY_VALIDATE_OFF(pVtgHdr->offProbes, pVtgHdr->cbProbes, 1, _32K, sizeof(VTGDESCPROBE), sizeof(uint32_t), VERR_SUPDRV_VTG_BAD_HDR);
397 MY_VALIDATE_OFF(pVtgHdr->offProviders, pVtgHdr->cbProviders, 1, 16, sizeof(VTGDESCPROVIDER), sizeof(uint32_t), VERR_SUPDRV_VTG_BAD_HDR);
398 MY_VALIDATE_OFF(pVtgHdr->offProbeEnabled, pVtgHdr->cbProbeEnabled, 1, _32K, sizeof(uint32_t), sizeof(uint32_t), VERR_SUPDRV_VTG_BAD_HDR);
399 if (!fUmod)
400 {
401 MY_WITHIN_IMAGE(pVtgHdr->uProbeLocs.p, VERR_SUPDRV_VTG_BAD_HDR_PTR);
402 MY_WITHIN_IMAGE(pVtgHdr->uProbeLocsEnd.p, VERR_SUPDRV_VTG_BAD_HDR_PTR);
403 MY_VALIDATE_SIZE( pVtgHdr->cbProbeLocs, 1, _128K, sizeof(VTGPROBELOC), VERR_SUPDRV_VTG_BAD_HDR);
404 }
405 else
406 {
407 if (pVtgHdr->cBits == 32)
408 MY_VALIDATE_SIZE( pVtgHdr->cbProbeLocs, 1, _8K, sizeof(VTGPROBELOC32), VERR_SUPDRV_VTG_BAD_HDR);
409 else
410 MY_VALIDATE_SIZE( pVtgHdr->cbProbeLocs, 1, _8K, sizeof(VTGPROBELOC64), VERR_SUPDRV_VTG_BAD_HDR);
411 /* Will check later that offProbeLocs are following closely on the
412 enable count array, so no need to validate the offset here. */
413 }
414
415 /*
416 * Some additional consistency checks.
417 */
418 if ( pVtgHdr->uProbeLocsEnd.u64 - pVtgHdr->uProbeLocs.u64 != pVtgHdr->cbProbeLocs
419 || (int64_t)(pVtgHdr->uProbeLocs.u64 - uVtgHdrAddr) != pVtgHdr->offProbeLocs)
420 {
421 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_MISC - uProbeLocs=%#llx uProbeLocsEnd=%#llx offProbeLocs=%#llx cbProbeLocs=%#x uVtgHdrAddr=%RTptr\n",
422 pVtgHdr->uProbeLocs.u64, pVtgHdr->uProbeLocsEnd.u64, pVtgHdr->offProbeLocs, pVtgHdr->cbProbeLocs, uVtgHdrAddr);
423 return VERR_SUPDRV_VTG_BAD_HDR_MISC;
424 }
425
426 if (pVtgHdr->cbProbes / sizeof(VTGDESCPROBE) != pVtgHdr->cbProbeEnabled / sizeof(uint32_t))
427 {
428 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_MISC - cbProbeEnabled=%#zx cbProbes=%#zx\n",
429 pVtgHdr->cbProbeEnabled, pVtgHdr->cbProbes);
430 return VERR_SUPDRV_VTG_BAD_HDR_MISC;
431 }
432
433 /*
434 * Check that there are no overlapping areas. This is a little bit ugly...
435 */
436 paAreas = (struct VTGAREAS const *)&pVtgHdr->offStrTab;
437 cAreas = pVtgHdr->offProbeLocs >= 0 ? 6 : 5;
438 off = sizeof(VTGOBJHDR);
439 for (i = 0; i < cAreas; i++)
440 {
441 if (paAreas[i].off < off)
442 {
443 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_MISC - overlapping areas %d and %d\n", i, i-1);
444 return VERR_SUPDRV_VTG_BAD_HDR_MISC;
445 }
446 off = paAreas[i].off + paAreas[i].cb;
447 }
448 if ( pVtgHdr->offProbeLocs > 0
449 && (uint32_t)-pVtgHdr->offProbeLocs < pVtgHdr->cbProbeLocs)
450 {
451 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_MISC - probe locations overlaps the header\n");
452 return VERR_SUPDRV_VTG_BAD_HDR_MISC;
453 }
454
455 /*
456 * Check that the object size is correct.
457 */
458 if (pVtgHdr->cbObj != pVtgHdr->offProbeEnabled + pVtgHdr->cbProbeEnabled)
459 {
460 SUPR0Printf("supdrvVtgValidateHdr: VERR_SUPDRV_VTG_BAD_HDR_MISC - bad header size %#x, expected %#x\n",
461 pVtgHdr->cbObj, pVtgHdr->offProbeEnabled + pVtgHdr->cbProbeEnabled);
462 return VERR_SUPDRV_VTG_BAD_HDR_MISC;
463 }
464
465
466 return VINF_SUCCESS;
467#undef MY_VALIDATE_OFF
468#undef MY_VALIDATE_SIZE
469}
470
471
472/**
473 * Validates the VTG data.
474 *
475 * @returns VBox status code.
476 * @param pVtgHdr The VTG object header of the data to validate.
477 * @param uVtgHdrAddr The address where the header is actually
478 * loaded.
479 * @param pbImage The image base. For validating the probe
480 * locations.
481 * @param cbImage The image size to go with @a pbImage.
482 * @param fUmod Whether this is a user module.
483 */
484static int supdrvVtgValidate(PVTGOBJHDR pVtgHdr, RTUINTPTR uVtgHdrAddr, const uint8_t *pbImage, size_t cbImage, bool fUmod)
485{
486 uintptr_t offTmp;
487 uintptr_t i;
488 uintptr_t cProviders;
489 int rc;
490
491 if (!pbImage || !cbImage)
492 {
493 pbImage = NULL;
494 cbImage = 0;
495 }
496
497#define MY_VALIDATE_STR(a_offStrTab) \
498 do { \
499 if ((a_offStrTab) >= pVtgHdr->cbStrTab) \
500 return VERR_SUPDRV_VTG_STRTAB_OFF; \
501 rc = supdrvVtgValidateString((char *)pVtgHdr + pVtgHdr->offStrTab + (a_offStrTab)); \
502 if (rc != VINF_SUCCESS) \
503 return rc; \
504 } while (0)
505#define MY_VALIDATE_ATTR(Attr) \
506 do { \
507 if ((Attr).u8Code <= (uint8_t)kVTGStability_Invalid || (Attr).u8Code >= (uint8_t)kVTGStability_End) \
508 return VERR_SUPDRV_VTG_BAD_ATTR; \
509 if ((Attr).u8Data <= (uint8_t)kVTGStability_Invalid || (Attr).u8Data >= (uint8_t)kVTGStability_End) \
510 return VERR_SUPDRV_VTG_BAD_ATTR; \
511 if ((Attr).u8DataDep <= (uint8_t)kVTGClass_Invalid || (Attr).u8DataDep >= (uint8_t)kVTGClass_End) \
512 return VERR_SUPDRV_VTG_BAD_ATTR; \
513 } while (0)
514
515 /*
516 * The header.
517 */
518 rc = supdrvVtgValidateHdr(pVtgHdr, uVtgHdrAddr, pbImage, cbImage, fUmod);
519 if (RT_FAILURE(rc))
520 return rc;
521
522 /*
523 * Validate the providers.
524 */
525 cProviders = i = pVtgHdr->cbProviders / sizeof(VTGDESCPROVIDER);
526 while (i-- > 0)
527 {
528 PCVTGDESCPROVIDER pProvider = (PCVTGDESCPROVIDER)((uintptr_t)pVtgHdr + pVtgHdr->offProviders) + i;
529
530 MY_VALIDATE_STR(pProvider->offName);
531 MY_CHECK_RET(pProvider->iFirstProbe < pVtgHdr->cbProbeEnabled / sizeof(uint32_t), VERR_SUPDRV_VTG_BAD_PROVIDER);
532 MY_CHECK_RET((uint32_t)pProvider->iFirstProbe + pProvider->cProbes <= pVtgHdr->cbProbeEnabled / sizeof(uint32_t),
533 VERR_SUPDRV_VTG_BAD_PROVIDER);
534 MY_VALIDATE_ATTR(pProvider->AttrSelf);
535 MY_VALIDATE_ATTR(pProvider->AttrModules);
536 MY_VALIDATE_ATTR(pProvider->AttrFunctions);
537 MY_VALIDATE_ATTR(pProvider->AttrNames);
538 MY_VALIDATE_ATTR(pProvider->AttrArguments);
539 MY_CHECK_RET(pProvider->bReserved == 0, VERR_SUPDRV_VTG_BAD_PROVIDER);
540 MY_CHECK_RET(pProvider->cProbesEnabled == 0, VERR_SUPDRV_VTG_BAD_PROVIDER);
541 MY_CHECK_RET(pProvider->uSettingsSerialNo == 0, VERR_SUPDRV_VTG_BAD_PROVIDER);
542 }
543
544 /*
545 * Validate probes.
546 */
547 i = pVtgHdr->cbProbes / sizeof(VTGDESCPROBE);
548 while (i-- > 0)
549 {
550 PCVTGDESCPROBE pProbe = (PCVTGDESCPROBE)( (uintptr_t)pVtgHdr + pVtgHdr->offProbes) + i;
551 PCVTGDESCPROVIDER pProvider = (PCVTGDESCPROVIDER)((uintptr_t)pVtgHdr + pVtgHdr->offProviders) + pProbe->idxProvider;
552 PCVTGDESCARGLIST pArgList = (PCVTGDESCARGLIST)( (uintptr_t)pVtgHdr + pVtgHdr->offArgLists + pProbe->offArgList );
553 unsigned iArg;
554 bool fHaveLargeArgs;
555
556
557 MY_VALIDATE_STR(pProbe->offName);
558 MY_CHECK_RET(pProbe->offArgList < pVtgHdr->cbArgLists, VERR_SUPDRV_VTG_BAD_PROBE);
559 MY_CHECK_RET((pProbe->offArgList & 3) == 0, VERR_SUPDRV_VTG_BAD_PROBE);
560 MY_CHECK_RET(pProbe->idxEnabled == i, VERR_SUPDRV_VTG_BAD_PROBE); /* The lists are parallel. */
561 MY_CHECK_RET(pProbe->idxProvider < cProviders, VERR_SUPDRV_VTG_BAD_PROBE);
562 MY_CHECK_RET(i - pProvider->iFirstProbe < pProvider->cProbes, VERR_SUPDRV_VTG_BAD_PROBE);
563 if (pProbe->offObjHdr != (intptr_t)pVtgHdr - (intptr_t)pProbe)
564 {
565 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_PROBE - iProbe=%u offObjHdr=%d expected %zd\n",
566 i, pProbe->offObjHdr, (intptr_t)pVtgHdr - (intptr_t)pProbe);
567 return VERR_SUPDRV_VTG_BAD_PROBE;
568 }
569
570 /* The referenced argument list. */
571 if (pArgList->cArgs > 16)
572 {
573 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_ARGLIST - iProbe=%u cArgs=%u\n", i, pArgList->cArgs);
574 return VERR_SUPDRV_VTG_BAD_ARGLIST;
575 }
576 if (pArgList->fHaveLargeArgs >= 2)
577 {
578 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_ARGLIST - iProbe=%u fHaveLargeArgs=%d\n", i, pArgList->fHaveLargeArgs);
579 return VERR_SUPDRV_VTG_BAD_ARGLIST;
580 }
581 if ( pArgList->abReserved[0]
582 || pArgList->abReserved[1])
583 {
584 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_ARGLIST - reserved MBZ iProbe=%u\n", i);
585 return VERR_SUPDRV_VTG_BAD_ARGLIST;
586 }
587 fHaveLargeArgs = false;
588 iArg = pArgList->cArgs;
589 while (iArg-- > 0)
590 {
591 uint32_t const fType = pArgList->aArgs[iArg].fType;
592 if (fType & ~VTG_TYPE_VALID_MASK)
593 {
594 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - fType=%#x iArg=%u iProbe=%u (#0)\n", fType, iArg, i);
595 return VERR_SUPDRV_TRACER_BAD_ARG_FLAGS;
596 }
597
598 switch (pArgList->aArgs[iArg].fType & VTG_TYPE_SIZE_MASK)
599 {
600 case 0:
601 if (pArgList->aArgs[iArg].fType & VTG_TYPE_FIXED_SIZED)
602 {
603 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - fType=%#x iArg=%u iProbe=%u (#1)\n", fType, iArg, i);
604 return VERR_SUPDRV_TRACER_BAD_ARG_FLAGS;
605 }
606 break;
607 case 1: case 2: case 4: case 8:
608 break;
609 default:
610 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - fType=%#x iArg=%u iProbe=%u (#2)\n", fType, iArg, i);
611 return VERR_SUPDRV_TRACER_BAD_ARG_FLAGS;
612 }
613 if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iArg].fType))
614 fHaveLargeArgs = true;
615
616 MY_VALIDATE_STR(pArgList->aArgs[iArg].offType);
617 }
618 if ((uint8_t)fHaveLargeArgs != pArgList->fHaveLargeArgs)
619 {
620 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - iProbe=%u fHaveLargeArgs=%d expected %d\n",
621 i, pArgList->fHaveLargeArgs, fHaveLargeArgs);
622 return VERR_SUPDRV_VTG_BAD_PROBE;
623 }
624 }
625
626 /*
627 * Check that pacProbeEnabled is all zeros.
628 */
629 {
630 uint32_t const *pcProbeEnabled = (uint32_t const *)((uintptr_t)pVtgHdr + pVtgHdr->offProbeEnabled);
631 i = pVtgHdr->cbProbeEnabled / sizeof(uint32_t);
632 while (i-- > 0)
633 MY_CHECK_RET(pcProbeEnabled[0] == 0, VERR_SUPDRV_VTG_BAD_PROBE_ENABLED);
634 }
635
636 /*
637 * Probe locations.
638 */
639 {
640 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((intptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
641 i = pVtgHdr->cbProbeLocs / sizeof(VTGPROBELOC);
642 while (i-- > 0)
643 {
644 MY_CHECK_RET(paProbeLocs[i].uLine < _1G, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
645 MY_CHECK_RET(paProbeLocs[i].fEnabled == false, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
646 MY_CHECK_RET(paProbeLocs[i].idProbe == 0, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
647 offTmp = (uintptr_t)paProbeLocs[i].pProbe - (uintptr_t)pVtgHdr->offProbes - (uintptr_t)pVtgHdr;
648#ifdef RT_OS_DARWIN /* See header validation code. */
649 if ( offTmp >= pVtgHdr->cbProbes
650 && pVtgHdr->u64VtgObjSectionStart != uVtgHdrAddr
651 && pVtgHdr->u64VtgObjSectionStart < _4M
652 && (uintptr_t)paProbeLocs[i].pProbe < _4M
653 && !fUmod )
654 {
655 uint64_t offDelta = uVtgHdrAddr - pVtgHdr->u64VtgObjSectionStart;
656
657 paProbeLocs[i].pProbe = (PVTGDESCPROBE)((uintptr_t)paProbeLocs[i].pProbe + offDelta);
658 if ((uintptr_t)paProbeLocs[i].pszFunction < _4M)
659 paProbeLocs[i].pszFunction = (const char *)((uintptr_t)paProbeLocs[i].pszFunction + offDelta);
660
661 offTmp += offDelta;
662 }
663#endif
664 MY_CHECK_RET(offTmp < pVtgHdr->cbProbes, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
665 MY_CHECK_RET(offTmp / sizeof(VTGDESCPROBE) * sizeof(VTGDESCPROBE) == offTmp, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
666 MY_WITHIN_IMAGE(paProbeLocs[i].pszFunction, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
667 }
668 }
669
670 return VINF_SUCCESS;
671}
672
673#undef MY_VALIDATE_STR
674#undef MY_VALIDATE_ATTR
675#undef MY_WITHIN_IMAGE
676
677
678/**
679 * Gets a string from the string table.
680 *
681 * @returns Pointer to the string.
682 * @param pVtgHdr The VTG object header.
683 * @param offStrTab The string table offset.
684 */
685static const char *supdrvVtgGetString(PVTGOBJHDR pVtgHdr, uint32_t offStrTab)
686{
687 Assert(offStrTab < pVtgHdr->cbStrTab);
688 return (char *)pVtgHdr + pVtgHdr->offStrTab + offStrTab;
689}
690
691
692/**
693 * Frees the provider structure and associated resources.
694 *
695 * @param pProv The provider to free.
696 */
697static void supdrvTracerFreeProvider(PSUPDRVTPPROVIDER pProv)
698{
699 LOG_TRACER(("Freeing tracepoint provider '%s' / %p\n", pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
700 pProv->fRegistered = false;
701 pProv->fZombie = true;
702 pProv->Core.pDesc = NULL;
703 pProv->Core.pHdr = NULL;
704 pProv->Core.paProbeLocsRO = NULL;
705 pProv->Core.pvProbeLocsEn = NULL;
706 pProv->Core.pacProbeEnabled = NULL;
707 pProv->Core.paR0ProbeLocs = NULL;
708 pProv->Core.paR0Probes = NULL;
709 RT_ZERO(pProv->Core.TracerData);
710 RTMemFree(pProv);
711}
712
713
714/**
715 * Unlinks and deregisters a provider.
716 *
717 * If the provider is still busy, it will be put in the zombie list.
718 *
719 * @param pDevExt The device extension.
720 * @param pProv The provider.
721 *
722 * @remarks The caller owns mtxTracer.
723 */
724static void supdrvTracerDeregisterVtgObj(PSUPDRVDEVEXT pDevExt, PSUPDRVTPPROVIDER pProv)
725{
726 int rc;
727
728 RTListNodeRemove(&pProv->ListEntry);
729 if (pProv->pSession)
730 {
731 RTListNodeRemove(&pProv->SessionListEntry);
732 RTListInit(&pProv->SessionListEntry);
733 pProv->pSession->cTpProviders--;
734 }
735
736 if (!pProv->fRegistered || !pDevExt->pTracerOps)
737 rc = VINF_SUCCESS;
738 else
739 rc = pDevExt->pTracerOps->pfnProviderDeregister(pDevExt->pTracerOps, &pProv->Core);
740 if (RT_SUCCESS(rc))
741 {
742 supdrvTracerFreeProvider(pProv);
743 return;
744 }
745
746 pProv->fZombie = true;
747 pProv->pImage = NULL;
748 pProv->pSession = NULL;
749 pProv->pUmod = NULL;
750 pProv->Core.pDesc = NULL;
751 pProv->Core.pHdr = NULL;
752 pProv->Core.paProbeLocsRO = NULL;
753 pProv->Core.pvProbeLocsEn = NULL;
754 pProv->Core.pacProbeEnabled = NULL;
755 pProv->Core.paR0ProbeLocs = NULL;
756
757 RTListAppend(&pDevExt->TracerProviderZombieList, &pProv->ListEntry);
758 LOG_TRACER(("Invalidated provider '%s' / %p and put it on the zombie list (rc=%Rrc)\n",
759 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
760}
761
762
763/**
764 * Processes the zombie list.
765 *
766 * @param pDevExt The device extension.
767 */
768static void supdrvTracerProcessZombies(PSUPDRVDEVEXT pDevExt)
769{
770 PSUPDRVTPPROVIDER pProv, pProvNext;
771
772 RTSemFastMutexRequest(pDevExt->mtxTracer);
773 RTListForEachSafe(&pDevExt->TracerProviderZombieList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
774 {
775 int rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
776 if (RT_SUCCESS(rc))
777 {
778 RTListNodeRemove(&pProv->ListEntry);
779 supdrvTracerFreeProvider(pProv);
780 }
781 }
782 RTSemFastMutexRelease(pDevExt->mtxTracer);
783}
784
785
786/**
787 * Unregisters all providers, including zombies, waiting for busy providers to
788 * go idle and unregister smoothly.
789 *
790 * This may block.
791 *
792 * @param pDevExt The device extension.
793 */
794static void supdrvTracerRemoveAllProviders(PSUPDRVDEVEXT pDevExt)
795{
796 uint32_t i;
797 PSUPDRVTPPROVIDER pProv;
798 PSUPDRVTPPROVIDER pProvNext;
799
800 /*
801 * Unregister all probes (there should only be one).
802 */
803 RTSemFastMutexRequest(pDevExt->mtxTracer);
804 RTListForEachSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
805 {
806 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
807 }
808 RTSemFastMutexRelease(pDevExt->mtxTracer);
809
810 /*
811 * Try unregister zombies now, sleep on busy ones and tracer opens.
812 */
813 for (i = 0; ; i++)
814 {
815 bool fEmpty;
816
817 RTSemFastMutexRequest(pDevExt->mtxTracer);
818
819 /* Zombies */
820 RTListForEachSafe(&pDevExt->TracerProviderZombieList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
821 {
822 int rc;
823 LOG_TRACER(("supdrvTracerRemoveAllProviders: Attemting to unregister '%s' / %p...\n",
824 pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
825
826 if (pDevExt->pTracerOps)
827 rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
828 else
829 rc = VINF_SUCCESS;
830 if (!rc)
831 {
832 RTListNodeRemove(&pProv->ListEntry);
833 supdrvTracerFreeProvider(pProv);
834 }
835 else if (!(i & 0xf))
836 SUPR0Printf("supdrvTracerRemoveAllProviders: Waiting on busy provider '%s' / %p (rc=%d)\n",
837 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc);
838 else
839 LOG_TRACER(("supdrvTracerRemoveAllProviders: Failed to unregister provider '%s' / %p - rc=%d\n",
840 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
841 }
842
843 fEmpty = RTListIsEmpty(&pDevExt->TracerProviderZombieList);
844
845 /* Tracer opens. */
846 if ( pDevExt->cTracerOpens
847 && pDevExt->pTracerOps)
848 {
849 fEmpty = false;
850 if (!(i & 0xf))
851 SUPR0Printf("supdrvTracerRemoveAllProviders: Waiting on %u opens\n", pDevExt->cTracerOpens);
852 else
853 LOG_TRACER(("supdrvTracerRemoveAllProviders: Waiting on %u opens\n", pDevExt->cTracerOpens));
854 }
855
856 RTSemFastMutexRelease(pDevExt->mtxTracer);
857
858 if (fEmpty)
859 break;
860
861 /* Delay...*/
862 RTThreadSleep(1000);
863 }
864}
865
866
867/**
868 * Registers the VTG tracepoint providers of a driver.
869 *
870 * @returns VBox status code.
871 * @param pDevExt The device instance data.
872 * @param pVtgHdr The VTG object header.
873 * @param pImage The image if applicable.
874 * @param pSession The session if applicable.
875 * @param pUmod The associated user tracepoint module if
876 * applicable.
877 * @param pszModName The module name.
878 */
879static int supdrvTracerRegisterVtgObj(PSUPDRVDEVEXT pDevExt, PVTGOBJHDR pVtgHdr, PSUPDRVLDRIMAGE pImage,
880 PSUPDRVSESSION pSession, PSUPDRVTRACERUMOD pUmod, const char *pszModName)
881{
882 int rc;
883 uintptr_t i;
884 PSUPDRVTPPROVIDER pProv;
885 size_t cchModName;
886
887 /*
888 * Validate input.
889 */
890 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
891 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
892 AssertPtrNullReturn(pImage, VERR_INVALID_POINTER);
893 AssertPtrNullReturn(pSession, VERR_INVALID_POINTER);
894 AssertPtrReturn(pszModName, VERR_INVALID_POINTER);
895 cchModName = strlen(pszModName);
896
897 if (pImage)
898 rc = supdrvVtgValidate(pVtgHdr, (uintptr_t)pVtgHdr,
899 (const uint8_t *)pImage->pvImage, pImage->cbImageBits,
900 false /*fUmod*/);
901 else
902 rc = supdrvVtgValidate(pVtgHdr, (uintptr_t)pVtgHdr, NULL, 0, pUmod != NULL);
903 if (RT_FAILURE(rc))
904 return rc;
905
906 /*
907 * Check that there aren't any obvious duplicates.
908 * (Yes, this isn't race free, but it's good enough for now.)
909 */
910 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
911 if (RT_FAILURE(rc))
912 return rc;
913 if (pImage || !pSession || pSession->R0Process == NIL_RTPROCESS)
914 {
915 RTListForEach(&pDevExt->TracerProviderList, pProv, SUPDRVTPPROVIDER, ListEntry)
916 {
917 if (pProv->Core.pHdr == pVtgHdr)
918 {
919 rc = VERR_SUPDRV_VTG_ALREADY_REGISTERED;
920 break;
921 }
922
923 if ( pProv->pSession == pSession
924 && pProv->pImage == pImage)
925 {
926 rc = VERR_SUPDRV_VTG_ONLY_ONCE_PER_SESSION;
927 break;
928 }
929 }
930 }
931 else
932 {
933 RTListForEach(&pSession->TpProviders, pProv, SUPDRVTPPROVIDER, SessionListEntry)
934 {
935 if (pProv->Core.pHdr == pVtgHdr)
936 {
937 rc = VERR_SUPDRV_VTG_ALREADY_REGISTERED;
938 break;
939 }
940 }
941 }
942 RTSemFastMutexRelease(pDevExt->mtxTracer);
943 if (RT_FAILURE(rc))
944 return rc;
945
946 /*
947 * Register the providers.
948 */
949 i = pVtgHdr->cbProviders / sizeof(VTGDESCPROVIDER);
950 while (i-- > 0)
951 {
952 PVTGDESCPROVIDER pDesc = (PVTGDESCPROVIDER)((uintptr_t)pVtgHdr + pVtgHdr->offProviders) + i;
953 const char *pszName = supdrvVtgGetString(pVtgHdr, pDesc->offName);
954 size_t const cchName = strlen(pszName) + (pUmod ? 16 : 0);
955
956 pProv = (PSUPDRVTPPROVIDER)RTMemAllocZ(RT_UOFFSETOF_DYN(SUPDRVTPPROVIDER, szName[cchName + 1 + cchModName + 1]));
957 if (pProv)
958 {
959 pProv->Core.pszName = &pProv->szName[0];
960 pProv->Core.pszModName = &pProv->szName[cchName + 1];
961 pProv->Core.pDesc = pDesc;
962 pProv->Core.pHdr = pVtgHdr;
963 pProv->Core.paProbeLocsRO = (PCVTGPROBELOC )((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
964 if (!pUmod)
965 {
966 pProv->Core.pvProbeLocsEn = (void *)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
967 pProv->Core.pacProbeEnabled = (uint32_t *)((uintptr_t)pVtgHdr + pVtgHdr->offProbeEnabled);
968 pProv->Core.paR0ProbeLocs = NULL;
969 pProv->Core.paR0Probes = NULL;
970 pProv->Core.cbProbeLocsEn = sizeof(VTGPROBELOC);
971 pProv->Core.cBits = ARCH_BITS;
972 pProv->Core.fUmod = false;
973 }
974 else
975 {
976 pProv->Core.pvProbeLocsEn = pUmod->pvProbeLocs;
977 pProv->Core.pacProbeEnabled = pUmod->pacProbeEnabled;
978 pProv->Core.paR0ProbeLocs = &pUmod->aProbeLocs[0];
979 pProv->Core.paR0Probes = (PSUPDRVPROBEINFO)&pUmod->aProbeLocs[pUmod->cProbeLocs];
980 pProv->Core.cbProbeLocsEn = pUmod->cbProbeLoc;
981 pProv->Core.cBits = pUmod->cBits;
982 pProv->Core.fUmod = true;
983 }
984 pProv->pImage = pImage;
985 pProv->pSession = pSession;
986 pProv->pUmod = pUmod;
987 pProv->fZombie = false;
988 pProv->fRegistered = true;
989
990 if (!pUmod)
991 memcpy(pProv->szName, pszName, cchName + 1);
992 else
993 RTStrPrintf(pProv->szName, cchName + 1, "%s%u", pszName, (uint32_t)pSession->Process);
994 memcpy((void *)pProv->Core.pszModName, pszModName, cchModName + 1);
995
996 /*
997 * Do the actual registration and list manipulations while holding
998 * down the lock.
999 */
1000 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1001 if (RT_SUCCESS(rc))
1002 {
1003 if ( pDevExt->pTracerOps
1004 && !pDevExt->fTracerUnloading)
1005 rc = pDevExt->pTracerOps->pfnProviderRegister(pDevExt->pTracerOps, &pProv->Core);
1006 else
1007 {
1008 pProv->fRegistered = false;
1009 rc = VINF_SUCCESS;
1010 }
1011 if (RT_SUCCESS(rc))
1012 {
1013 RTListAppend(&pDevExt->TracerProviderList, &pProv->ListEntry);
1014 if (pSession)
1015 {
1016 RTListAppend(&pSession->TpProviders, &pProv->SessionListEntry);
1017 pSession->cTpProviders++;
1018 }
1019 else
1020 RTListInit(&pProv->SessionListEntry);
1021 RTSemFastMutexRelease(pDevExt->mtxTracer);
1022 LOG_TRACER(("Registered tracepoint provider '%s' in '%s' -> %p\n",
1023 pProv->szName, pszModName, pProv->Core.TracerData.DTrace.idProvider));
1024 }
1025 else
1026 {
1027 RTSemFastMutexRelease(pDevExt->mtxTracer);
1028 LOG_TRACER(("Failed to register tracepoint provider '%s' in '%s' -> %Rrc\n",
1029 pProv->szName, pszModName, rc));
1030 }
1031 }
1032 }
1033 else
1034 rc = VERR_NO_MEMORY;
1035
1036 /*
1037 * In case of failure, we have to undo any providers we already
1038 * managed to register.
1039 */
1040 if (RT_FAILURE(rc))
1041 {
1042 PSUPDRVTPPROVIDER pProvNext;
1043
1044 if (pProv)
1045 supdrvTracerFreeProvider(pProv);
1046
1047 RTSemFastMutexRequest(pDevExt->mtxTracer);
1048 if (pImage)
1049 {
1050 RTListForEachReverseSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1051 {
1052 if (pProv->Core.pHdr == pVtgHdr)
1053 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1054 }
1055 }
1056 else
1057 {
1058 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
1059 {
1060 if (pProv->Core.pHdr == pVtgHdr)
1061 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1062 }
1063 }
1064 RTSemFastMutexRelease(pDevExt->mtxTracer);
1065 return rc;
1066 }
1067 }
1068
1069 return VINF_SUCCESS;
1070}
1071
1072
1073/**
1074 * Registers the VTG tracepoint providers of a driver.
1075 *
1076 * @returns VBox status code.
1077 * @param pSession The support driver session handle.
1078 * @param pVtgHdr The VTG header.
1079 * @param pszName The driver name.
1080 */
1081SUPR0DECL(int) SUPR0TracerRegisterDrv(PSUPDRVSESSION pSession, PVTGOBJHDR pVtgHdr, const char *pszName)
1082{
1083 int rc;
1084
1085 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
1086 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1087 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1088 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_INVALID_PARAMETER);
1089 LOG_TRACER(("SUPR0TracerRegisterDrv: pSession=%p pVtgHdr=%p pszName=%s\n", pSession, pVtgHdr, pszName));
1090
1091 rc = supdrvTracerRegisterVtgObj(pSession->pDevExt, pVtgHdr, NULL /*pImage*/, pSession, NULL /*pUmod*/, pszName);
1092
1093 /*
1094 * Try unregister zombies while we have a chance.
1095 */
1096 supdrvTracerProcessZombies(pSession->pDevExt);
1097
1098 return rc;
1099}
1100
1101
1102/**
1103 * Deregister the VTG tracepoint providers of a driver.
1104 *
1105 * @param pSession The support driver session handle.
1106 */
1107SUPR0DECL(void) SUPR0TracerDeregisterDrv(PSUPDRVSESSION pSession)
1108{
1109 PSUPDRVTPPROVIDER pProv, pProvNext;
1110 PSUPDRVDEVEXT pDevExt;
1111 AssertReturnVoid(SUP_IS_SESSION_VALID(pSession));
1112 AssertReturnVoid(pSession->R0Process == NIL_RTR0PROCESS);
1113 LOG_TRACER(("SUPR0TracerDeregisterDrv: pSession=%p\n", pSession));
1114
1115 pDevExt = pSession->pDevExt;
1116
1117 /*
1118 * Search for providers belonging to this driver session.
1119 */
1120 RTSemFastMutexRequest(pDevExt->mtxTracer);
1121 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
1122 {
1123 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1124 }
1125 RTSemFastMutexRelease(pDevExt->mtxTracer);
1126
1127 /*
1128 * Try unregister zombies while we have a chance.
1129 */
1130 supdrvTracerProcessZombies(pDevExt);
1131}
1132
1133
1134/**
1135 * Registers the VTG tracepoint providers of a module loaded by
1136 * the support driver.
1137 *
1138 * This should be called from the ModuleInit code.
1139 *
1140 * @returns VBox status code.
1141 * @param hMod The module handle.
1142 * @param pVtgHdr The VTG header.
1143 */
1144SUPR0DECL(int) SUPR0TracerRegisterModule(void *hMod, PVTGOBJHDR pVtgHdr)
1145{
1146 PSUPDRVLDRIMAGE pImage = (PSUPDRVLDRIMAGE)hMod;
1147 PSUPDRVDEVEXT pDevExt;
1148 int rc;
1149
1150 LOG_TRACER(("SUPR0TracerRegisterModule: %p\n", pVtgHdr));
1151
1152 /*
1153 * Validate input and context.
1154 */
1155 AssertPtrReturn(pImage, VERR_INVALID_HANDLE);
1156 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1157
1158 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
1159 pDevExt = pImage->pDevExt;
1160 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1161 AssertReturn(pDevExt->pLdrInitImage == pImage, VERR_WRONG_ORDER);
1162 AssertReturn(pDevExt->hLdrInitThread == RTThreadNativeSelf(), VERR_WRONG_ORDER);
1163 AssertReturn((uintptr_t)pVtgHdr - (uintptr_t)pImage->pvImage < pImage->cbImageBits, VERR_INVALID_PARAMETER);
1164
1165 /*
1166 * Do the job.
1167 */
1168 rc = supdrvTracerRegisterVtgObj(pDevExt, pVtgHdr, pImage, NULL /*pSession*/, NULL /*pUmod*/, pImage->szName);
1169 LOG_TRACER(("SUPR0TracerRegisterModule: rc=%d\n", rc));
1170
1171 /*
1172 * Try unregister zombies while we have a chance.
1173 */
1174 supdrvTracerProcessZombies(pDevExt);
1175
1176 return rc;
1177}
1178
1179
1180/**
1181 * Registers the tracer implementation.
1182 *
1183 * This should be called from the ModuleInit code or from a ring-0 session.
1184 *
1185 * @returns VBox status code.
1186 * @param hMod The module handle.
1187 * @param pSession Ring-0 session handle.
1188 * @param pReg Pointer to the tracer registration structure.
1189 * @param ppHlp Where to return the tracer helper method table.
1190 */
1191SUPR0DECL(int) SUPR0TracerRegisterImpl(void *hMod, PSUPDRVSESSION pSession, PCSUPDRVTRACERREG pReg, PCSUPDRVTRACERHLP *ppHlp)
1192{
1193 PSUPDRVLDRIMAGE pImage = (PSUPDRVLDRIMAGE)hMod;
1194 PSUPDRVDEVEXT pDevExt;
1195 PSUPDRVTPPROVIDER pProv;
1196 int rc;
1197 int rc2;
1198
1199 /*
1200 * Validate input and context.
1201 */
1202 AssertPtrReturn(ppHlp, VERR_INVALID_POINTER);
1203 *ppHlp = NULL;
1204 AssertPtrReturn(pReg, VERR_INVALID_HANDLE);
1205
1206 if (pImage)
1207 {
1208 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
1209 AssertReturn(pSession == NULL, VERR_INVALID_PARAMETER);
1210 pDevExt = pImage->pDevExt;
1211 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1212 AssertReturn(pDevExt->pLdrInitImage == pImage, VERR_WRONG_ORDER);
1213 AssertReturn(pDevExt->hLdrInitThread == RTThreadNativeSelf(), VERR_WRONG_ORDER);
1214 }
1215 else
1216 {
1217 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
1218 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_INVALID_PARAMETER);
1219 pDevExt = pSession->pDevExt;
1220 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1221 }
1222
1223 AssertReturn(pReg->u32Magic == SUPDRVTRACERREG_MAGIC, VERR_INVALID_MAGIC);
1224 AssertReturn(pReg->u32Version == SUPDRVTRACERREG_VERSION, VERR_VERSION_MISMATCH);
1225 AssertReturn(pReg->uEndMagic == SUPDRVTRACERREG_MAGIC, VERR_VERSION_MISMATCH);
1226 AssertPtrReturn(pReg->pfnProbeFireKernel, VERR_INVALID_POINTER);
1227 AssertPtrReturn(pReg->pfnProbeFireUser, VERR_INVALID_POINTER);
1228 AssertPtrReturn(pReg->pfnTracerOpen, VERR_INVALID_POINTER);
1229 AssertPtrReturn(pReg->pfnTracerIoCtl, VERR_INVALID_POINTER);
1230 AssertPtrReturn(pReg->pfnTracerClose, VERR_INVALID_POINTER);
1231 AssertPtrReturn(pReg->pfnProviderRegister, VERR_INVALID_POINTER);
1232 AssertPtrReturn(pReg->pfnProviderDeregister, VERR_INVALID_POINTER);
1233 AssertPtrReturn(pReg->pfnProviderDeregisterZombie, VERR_INVALID_POINTER);
1234
1235 /*
1236 * Do the job.
1237 */
1238 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1239 if (RT_SUCCESS(rc))
1240 {
1241 if (!pDevExt->pTracerOps)
1242 {
1243 LOG_TRACER(("SUPR0TracerRegisterImpl: pReg=%p\n", pReg));
1244 pDevExt->pTracerOps = pReg;
1245 pDevExt->pTracerSession = pSession;
1246 pDevExt->pTracerImage = pImage;
1247
1248 g_pfnSupdrvProbeFireKernel = (PFNRT)pDevExt->pTracerOps->pfnProbeFireKernel;
1249
1250 *ppHlp = &pDevExt->TracerHlp;
1251 rc = VINF_SUCCESS;
1252
1253 /*
1254 * Iterate the already loaded modules and register their providers.
1255 */
1256 RTListForEach(&pDevExt->TracerProviderList, pProv, SUPDRVTPPROVIDER, ListEntry)
1257 {
1258 Assert(!pProv->fRegistered);
1259 pProv->fRegistered = true;
1260 rc2 = pDevExt->pTracerOps->pfnProviderRegister(pDevExt->pTracerOps, &pProv->Core);
1261 if (RT_FAILURE(rc2))
1262 {
1263 pProv->fRegistered = false;
1264 SUPR0Printf("SUPR0TracerRegisterImpl: Failed to register provider %s::%s - rc=%d\n",
1265 pProv->Core.pszModName, pProv->szName, rc2);
1266 }
1267 }
1268 }
1269 else
1270 rc = VERR_SUPDRV_TRACER_ALREADY_REGISTERED;
1271 RTSemFastMutexRelease(pDevExt->mtxTracer);
1272 }
1273
1274 return rc;
1275
1276}
1277
1278
1279/**
1280 * Common tracer implementation deregistration code.
1281 *
1282 * The caller sets fTracerUnloading prior to calling this function.
1283 *
1284 * @param pDevExt The device extension structure.
1285 */
1286static void supdrvTracerCommonDeregisterImpl(PSUPDRVDEVEXT pDevExt)
1287{
1288 uint32_t i;
1289 PSUPDRVTPPROVIDER pProv;
1290 PSUPDRVTPPROVIDER pProvNext;
1291
1292 RTSemFastMutexRequest(pDevExt->mtxTracer);
1293
1294 /*
1295 * Reinstall the stub probe-fire function.
1296 */
1297 g_pfnSupdrvProbeFireKernel = supdrvTracerProbeFireStub;
1298
1299 /*
1300 * Disassociate the tracer implementation from all providers.
1301 * We will have to wait on busy providers.
1302 */
1303 for (i = 0; ; i++)
1304 {
1305 uint32_t cZombies = 0;
1306
1307 /* Live providers. */
1308 RTListForEachSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1309 {
1310 int rc;
1311 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Attemting to unregister '%s' / %p...\n",
1312 pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
1313
1314 if (!pProv->fRegistered)
1315 continue;
1316 if (!pProv->fZombie)
1317 {
1318 rc = pDevExt->pTracerOps->pfnProviderDeregister(pDevExt->pTracerOps, &pProv->Core);
1319 if (RT_FAILURE(rc))
1320 pProv->fZombie = true;
1321 }
1322 else
1323 rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
1324 if (RT_SUCCESS(rc))
1325 pProv->fZombie = pProv->fRegistered = false;
1326 else
1327 {
1328 cZombies++;
1329 if (!(i & 0xf))
1330 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on busy provider '%s' / %p (rc=%d)\n",
1331 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc);
1332 else
1333 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Failed to unregister provider '%s' / %p - rc=%d\n",
1334 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
1335 }
1336 }
1337
1338 /* Zombies providers. */
1339 RTListForEachSafe(&pDevExt->TracerProviderZombieList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1340 {
1341 int rc;
1342 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Attemting to unregister '%s' / %p (zombie)...\n",
1343 pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
1344
1345 rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
1346 if (RT_SUCCESS(rc))
1347 {
1348 RTListNodeRemove(&pProv->ListEntry);
1349 supdrvTracerFreeProvider(pProv);
1350 }
1351 else
1352 {
1353 cZombies++;
1354 if (!(i & 0xf))
1355 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on busy provider '%s' / %p (rc=%d)\n",
1356 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc);
1357 else
1358 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Failed to unregister provider '%s' / %p - rc=%d\n",
1359 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
1360 }
1361 }
1362
1363 /* Tracer opens. */
1364 if (pDevExt->cTracerOpens)
1365 {
1366 cZombies++;
1367 if (!(i & 0xf))
1368 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on %u opens\n", pDevExt->cTracerOpens);
1369 else
1370 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Waiting on %u opens\n", pDevExt->cTracerOpens));
1371 }
1372
1373 /* Tracer calls. */
1374 if (pDevExt->cTracerCallers)
1375 {
1376 cZombies++;
1377 if (!(i & 0xf))
1378 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on %u callers\n", pDevExt->cTracerCallers);
1379 else
1380 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Waiting on %u callers\n", pDevExt->cTracerCallers));
1381 }
1382
1383 /* Done? */
1384 if (cZombies == 0)
1385 break;
1386
1387 /* Delay...*/
1388 RTSemFastMutexRelease(pDevExt->mtxTracer);
1389 RTThreadSleep(1000);
1390 RTSemFastMutexRequest(pDevExt->mtxTracer);
1391 }
1392
1393 /*
1394 * Deregister the tracer implementation.
1395 */
1396 pDevExt->pTracerImage = NULL;
1397 pDevExt->pTracerSession = NULL;
1398 pDevExt->pTracerOps = NULL;
1399 pDevExt->fTracerUnloading = false;
1400
1401 RTSemFastMutexRelease(pDevExt->mtxTracer);
1402}
1403
1404
1405/**
1406 * Deregister a tracer implementation.
1407 *
1408 * This should be called from the ModuleTerm code or from a ring-0 session.
1409 *
1410 * @returns VBox status code.
1411 * @param hMod The module handle.
1412 * @param pSession Ring-0 session handle.
1413 */
1414SUPR0DECL(int) SUPR0TracerDeregisterImpl(void *hMod, PSUPDRVSESSION pSession)
1415{
1416 PSUPDRVLDRIMAGE pImage = (PSUPDRVLDRIMAGE)hMod;
1417 PSUPDRVDEVEXT pDevExt;
1418 int rc;
1419
1420 /*
1421 * Validate input and context.
1422 */
1423 if (pImage)
1424 {
1425 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
1426 AssertReturn(pSession == NULL, VERR_INVALID_PARAMETER);
1427 pDevExt = pImage->pDevExt;
1428 }
1429 else
1430 {
1431 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
1432 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_INVALID_PARAMETER);
1433 pDevExt = pSession->pDevExt;
1434 }
1435 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1436
1437 /*
1438 * Do the job.
1439 */
1440 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1441 if (RT_SUCCESS(rc))
1442 {
1443 if ( pImage
1444 ? pDevExt->pTracerImage == pImage
1445 : pDevExt->pTracerSession == pSession)
1446 {
1447 LOG_TRACER(("SUPR0TracerDeregisterImpl: Unloading ...\n"));
1448 pDevExt->fTracerUnloading = true;
1449 RTSemFastMutexRelease(pDevExt->mtxTracer);
1450 supdrvTracerCommonDeregisterImpl(pDevExt);
1451 LOG_TRACER(("SUPR0TracerDeregisterImpl: ... done.\n"));
1452 }
1453 else
1454 {
1455 rc = VERR_SUPDRV_TRACER_NOT_REGISTERED;
1456 RTSemFastMutexRelease(pDevExt->mtxTracer);
1457 }
1458 }
1459
1460 return rc;
1461}
1462
1463
1464/*
1465 * The probe function is a bit more fun since we need tail jump optimizating.
1466 *
1467 * Since we cannot ship yasm sources for linux and freebsd, owing to the cursed
1468 * rebuilding of the kernel module from scratch at install time, we have to
1469 * deploy some ugly gcc inline assembly here.
1470 */
1471#if defined(__GNUC__) && (defined(RT_OS_FREEBSD) || defined(RT_OS_LINUX))
1472__asm__("\
1473 .section .text \n\
1474 \n\
1475 .p2align 4 \n\
1476 .global SUPR0TracerFireProbe \n\
1477 .type SUPR0TracerFireProbe, @function \n\
1478SUPR0TracerFireProbe: \n\
1479");
1480# if defined(RT_ARCH_AMD64)
1481__asm__("\
1482 movq g_pfnSupdrvProbeFireKernel(%rip), %rax \n\
1483 jmp *%rax \n\
1484");
1485# elif defined(RT_ARCH_X86)
1486__asm__("\
1487 movl g_pfnSupdrvProbeFireKernel, %eax \n\
1488 jmp *%eax \n\
1489");
1490# else
1491# error "Which arch is this?"
1492# endif
1493__asm__("\
1494 .size SUPR0TracerFireProbe, . - SUPR0TracerFireProbe \n\
1495 \n\
1496 .type supdrvTracerProbeFireStub,@function \n\
1497 .global supdrvTracerProbeFireStub \n\
1498supdrvTracerProbeFireStub: \n\
1499 ret \n\
1500 .size supdrvTracerProbeFireStub, . - supdrvTracerProbeFireStub \n\
1501 \n\
1502 .previous \n\
1503");
1504# if 0 /* Slickedit on windows highlighting fix */
1505 )
1506# endif
1507#endif
1508
1509
1510/**
1511 * Module unloading hook, called after execution in the module have ceased.
1512 *
1513 * @param pDevExt The device extension structure.
1514 * @param pImage The image being unloaded.
1515 */
1516void VBOXCALL supdrvTracerModuleUnloading(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
1517{
1518 PSUPDRVTPPROVIDER pProv, pProvNext;
1519 AssertPtrReturnVoid(pImage); /* paranoia */
1520
1521 RTSemFastMutexRequest(pDevExt->mtxTracer);
1522
1523 /*
1524 * If it is the tracer image, we have to unload all the providers.
1525 */
1526 if (pDevExt->pTracerImage == pImage)
1527 {
1528 LOG_TRACER(("supdrvTracerModuleUnloading: Unloading tracer ...\n"));
1529 pDevExt->fTracerUnloading = true;
1530 RTSemFastMutexRelease(pDevExt->mtxTracer);
1531 supdrvTracerCommonDeregisterImpl(pDevExt);
1532 LOG_TRACER(("supdrvTracerModuleUnloading: ... done.\n"));
1533 }
1534 else
1535 {
1536 /*
1537 * Unregister all providers belonging to this image.
1538 */
1539 RTListForEachSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1540 {
1541 if (pProv->pImage == pImage)
1542 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1543 }
1544
1545 RTSemFastMutexRelease(pDevExt->mtxTracer);
1546
1547 /*
1548 * Try unregister zombies while we have a chance.
1549 */
1550 supdrvTracerProcessZombies(pDevExt);
1551 }
1552}
1553
1554
1555/**
1556 * Called when a session is being cleaned up.
1557 *
1558 * @param pDevExt The device extension structure.
1559 * @param pSession The session that is being torn down.
1560 */
1561void VBOXCALL supdrvTracerCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
1562{
1563 /*
1564 * Deregister all providers.
1565 */
1566 SUPDRVTPPROVIDER *pProvNext;
1567 SUPDRVTPPROVIDER *pProv;
1568 RTSemFastMutexRequest(pDevExt->mtxTracer);
1569 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
1570 {
1571 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1572 }
1573 RTSemFastMutexRelease(pDevExt->mtxTracer);
1574
1575 /*
1576 * Clean up instance data the trace may have associated with the session.
1577 */
1578 if (pSession->uTracerData)
1579 supdrvIOCtl_TracerClose(pDevExt, pSession);
1580
1581 /*
1582 * Deregister any tracer implementation.
1583 */
1584 if (pSession->R0Process == NIL_RTR0PROCESS)
1585 (void)SUPR0TracerDeregisterImpl(NULL, pSession);
1586
1587 if (pSession->R0Process != NIL_RTR0PROCESS)
1588 {
1589 /*
1590 * Free any lingering user modules. We don't bother holding the lock
1591 * here as there shouldn't be anyone messing with the session at this
1592 * point.
1593 */
1594 PSUPDRVTRACERUMOD pUmodNext;
1595 PSUPDRVTRACERUMOD pUmod;
1596 RTListForEachSafe(&pSession->TpUmods, pUmod, pUmodNext, SUPDRVTRACERUMOD, ListEntry)
1597 {
1598 RTR0MemObjFree(pUmod->hMemObjMap, false /*fFreeMappings*/);
1599 RTR0MemObjFree(pUmod->hMemObjLock, false /*fFreeMappings*/);
1600 supdrvVtgReleaseObjectCopy(pDevExt, pUmod->pVtgCopy);
1601 RTMemFree(pUmod);
1602 }
1603 }
1604}
1605
1606
1607static void supdrvVtgReleaseObjectCopy(PSUPDRVDEVEXT pDevExt, PSUPDRVVTGCOPY pThis)
1608{
1609 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
1610 if (!cRefs)
1611 {
1612 RTSemFastMutexRequest(pDevExt->mtxTracer);
1613 pThis->u32Magic = ~SUDPRVVTGCOPY_MAGIC;
1614 RTListNodeRemove(&pThis->ListEntry);
1615 RTSemFastMutexRelease(pDevExt->mtxTracer);
1616
1617 RTMemFree(pThis);
1618 }
1619}
1620
1621
1622/**
1623 * Finds a matching VTG object copy, caller owns the lock already.
1624 *
1625 * @returns Copy with reference. NULL if not found.
1626 * @param pHashList The hash list to search.
1627 * @param pHdr The VTG header (valid).
1628 * @param cbStrTab The string table size.
1629 * @param fFlags The user module flags.
1630 */
1631static PSUPDRVVTGCOPY supdrvVtgFindObjectCopyLocked(PRTLISTANCHOR pHashList, PCVTGOBJHDR pHdr, uint32_t cbStrTab, uint32_t fFlags)
1632{
1633 PSUPDRVVTGCOPY pCur;
1634
1635 fFlags &= SUP_TRACER_UMOD_FLAGS_TYPE_MASK;
1636 RTListForEach(pHashList, pCur, SUPDRVVTGCOPY, ListEntry)
1637 {
1638#define HDR_EQUALS(member) pCur->Hdr.member == pHdr->member
1639 if ( HDR_EQUALS(Uuid.au32[0])
1640 && HDR_EQUALS(Uuid.au32[1])
1641 && HDR_EQUALS(Uuid.au32[2])
1642 && HDR_EQUALS(Uuid.au32[3])
1643 && HDR_EQUALS(cbObj)
1644 && HDR_EQUALS(cBits)
1645 && pCur->cbStrTab == cbStrTab
1646 && pCur->fFlags == fFlags
1647 )
1648 {
1649 if (RT_LIKELY( HDR_EQUALS(offStrTab)
1650 && HDR_EQUALS(cbStrTab)
1651 && HDR_EQUALS(offArgLists)
1652 && HDR_EQUALS(cbArgLists)
1653 && HDR_EQUALS(offProbes)
1654 && HDR_EQUALS(cbProbes)
1655 && HDR_EQUALS(offProviders)
1656 && HDR_EQUALS(cbProviders)
1657 && HDR_EQUALS(offProbeEnabled)
1658 && HDR_EQUALS(cbProbeEnabled)
1659 && HDR_EQUALS(offProbeLocs)
1660 && HDR_EQUALS(cbProbeLocs)
1661 )
1662 )
1663 {
1664 Assert(pCur->cRefs > 0);
1665 Assert(pCur->cRefs < _1M);
1666 pCur->cRefs++;
1667 return pCur;
1668 }
1669 }
1670#undef HDR_EQUALS
1671 }
1672
1673 return NULL;
1674}
1675
1676
1677/**
1678 * Finds a matching VTG object copy.
1679 *
1680 * @returns Copy with reference. NULL if not found.
1681 * @param pDevExt The device extension.
1682 * @param pHdr The VTG header (valid).
1683 * @param cbStrTab The string table size.
1684 * @param fFlags The user module flags.
1685 */
1686static PSUPDRVVTGCOPY supdrvVtgFindObjectCopy(PSUPDRVDEVEXT pDevExt, PCVTGOBJHDR pHdr, uint32_t cbStrTab, uint32_t fFlags)
1687{
1688 PRTLISTANCHOR pHashList = &pDevExt->aTrackerUmodHash[pHdr->Uuid.au8[3] % RT_ELEMENTS(pDevExt->aTrackerUmodHash)];
1689 PSUPDRVVTGCOPY pRet;
1690
1691 int rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1692 AssertRCReturn(rc, NULL);
1693
1694 pRet = supdrvVtgFindObjectCopyLocked(pHashList, pHdr, cbStrTab, fFlags);
1695
1696 RTSemFastMutexRelease(pDevExt->mtxTracer);
1697 return pRet;
1698}
1699
1700
1701/**
1702 * Makes a shared copy of the VTG object.
1703 *
1704 * @returns VBox status code.
1705 * @param pDevExt The device extension.
1706 * @param pVtgHdr The VTG header (valid).
1707 * @param R3PtrVtgHdr The ring-3 VTG header address.
1708 * @param uVtgHdrAddr The address of the VTG header in the context
1709 * where it is actually used.
1710 * @param R3PtrStrTab The ring-3 address of the probe location string
1711 * table. The probe location array have offsets
1712 * into this instead of funciton name pointers.
1713 * @param cbStrTab The size of the probe location string table.
1714 * @param fFlags The user module flags.
1715 * @param pUmod The structure we've allocated to track the
1716 * module. This have a valid kernel mapping of the
1717 * probe location array. Upon successful return,
1718 * the pVtgCopy member will hold the address of our
1719 * copy (with a referenced of course).
1720 */
1721static int supdrvVtgCreateObjectCopy(PSUPDRVDEVEXT pDevExt, PCVTGOBJHDR pVtgHdr, RTR3PTR R3PtrVtgHdr, RTUINTPTR uVtgHdrAddr,
1722 RTR3PTR R3PtrStrTab, uint32_t cbStrTab, uint32_t fFlags, PSUPDRVTRACERUMOD pUmod)
1723{
1724 /*
1725 * Calculate the space required, allocate and copy in the data.
1726 */
1727 int rc;
1728 uint32_t const cProbeLocs = pVtgHdr->cbProbeLocs / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
1729 uint32_t const cbProbeLocs = cProbeLocs * sizeof(VTGPROBELOC);
1730 uint32_t const offProbeLocs = RT_ALIGN(pVtgHdr->cbObj, 8);
1731 size_t const cb = offProbeLocs + cbProbeLocs + cbStrTab + 1;
1732 PSUPDRVVTGCOPY pThis = (PSUPDRVVTGCOPY)RTMemAlloc(RT_UOFFSETOF(SUPDRVVTGCOPY, Hdr) + cb);
1733 if (!pThis)
1734 return VERR_NO_MEMORY;
1735
1736 pThis->u32Magic = SUDPRVVTGCOPY_MAGIC;
1737 pThis->cRefs = 1;
1738 pThis->cbStrTab = cbStrTab;
1739 pThis->fFlags = fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK;
1740 RTListInit(&pThis->ListEntry);
1741
1742 rc = RTR0MemUserCopyFrom(&pThis->Hdr, R3PtrVtgHdr, pVtgHdr->cbObj);
1743 if (RT_SUCCESS(rc))
1744 {
1745 char *pchStrTab = (char *)&pThis->Hdr + offProbeLocs + cbProbeLocs;
1746 rc = RTR0MemUserCopyFrom(pchStrTab, R3PtrStrTab, cbStrTab);
1747 if (RT_SUCCESS(rc))
1748 {
1749 PVTGPROBELOC paDst = (PVTGPROBELOC)((char *)&pThis->Hdr + offProbeLocs);
1750 uint32_t i;
1751
1752 /*
1753 * Some paranoia: Overwrite the header with the copy we've already
1754 * validated and zero terminate the string table.
1755 */
1756 pThis->Hdr = *pVtgHdr;
1757 pchStrTab[cbStrTab] = '\0';
1758
1759 /*
1760 * Set the probe location array related header members since we're
1761 * making our own copy in a different location.
1762 */
1763 pThis->Hdr.uProbeLocs.u64 = (uintptr_t)paDst;
1764 pThis->Hdr.uProbeLocsEnd.u64 = (uintptr_t)paDst + cbProbeLocs;
1765 pThis->Hdr.offProbeLocs = offProbeLocs;
1766 pThis->Hdr.cbProbeLocs = cbProbeLocs;
1767 pThis->Hdr.cBits = ARCH_BITS;
1768
1769 /*
1770 * Copy, convert and fix up the probe location table.
1771 */
1772 if (pVtgHdr->cBits == 32)
1773 {
1774 uintptr_t const offDelta = (uintptr_t)&pThis->Hdr - uVtgHdrAddr;
1775 PCVTGPROBELOC32 paSrc = (PCVTGPROBELOC32)pUmod->pvProbeLocs;
1776
1777 for (i = 0; i < cProbeLocs; i++)
1778 {
1779 paDst[i].uLine = paSrc[i].uLine;
1780 paDst[i].fEnabled = paSrc[i].fEnabled;
1781 paDst[i].idProbe = paSrc[i].idProbe;
1782 if (paSrc[i].pszFunction > cbStrTab)
1783 {
1784 rc = VERR_SUPDRV_TRACER_UMOD_STRTAB_OFF_BAD;
1785 break;
1786 }
1787 paDst[i].pszFunction = pchStrTab + paSrc[i].pszFunction;
1788 paDst[i].pProbe = (PVTGDESCPROBE)(uintptr_t)(paSrc[i].pProbe + offDelta);
1789 }
1790 }
1791 else
1792 {
1793 uint64_t const offDelta = (uintptr_t)&pThis->Hdr - uVtgHdrAddr;
1794 PCVTGPROBELOC64 paSrc = (PCVTGPROBELOC64)pUmod->pvProbeLocs;
1795
1796 for (i = 0; i < cProbeLocs; i++)
1797 {
1798 paDst[i].uLine = paSrc[i].uLine;
1799 paDst[i].fEnabled = paSrc[i].fEnabled;
1800 paDst[i].idProbe = paSrc[i].idProbe;
1801 if (paSrc[i].pszFunction > cbStrTab)
1802 {
1803 rc = VERR_SUPDRV_TRACER_UMOD_STRTAB_OFF_BAD;
1804 break;
1805 }
1806 paDst[i].pszFunction = pchStrTab + (uintptr_t)paSrc[i].pszFunction;
1807 paDst[i].pProbe = (PVTGDESCPROBE)(uintptr_t)(paSrc[i].pProbe + offDelta);
1808 }
1809 }
1810
1811 /*
1812 * Validate it
1813 *
1814 * Note! fUmod is false as this is a kernel copy with all native
1815 * structures.
1816 */
1817 if (RT_SUCCESS(rc))
1818 rc = supdrvVtgValidate(&pThis->Hdr, (uintptr_t)&pThis->Hdr, (uint8_t *)&pThis->Hdr, cb, false /*fUmod*/);
1819 if (RT_SUCCESS(rc))
1820 {
1821 /*
1822 * Add it to the hash list, making sure nobody raced us.
1823 */
1824 PRTLISTANCHOR pHashList = &pDevExt->aTrackerUmodHash[ pVtgHdr->Uuid.au8[3]
1825 % RT_ELEMENTS(pDevExt->aTrackerUmodHash)];
1826
1827 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1828 if (RT_SUCCESS(rc))
1829 {
1830 pUmod->pVtgCopy = supdrvVtgFindObjectCopyLocked(pHashList, pVtgHdr, cbStrTab, fFlags);
1831 if (!pUmod->pVtgCopy)
1832 {
1833 pUmod->pVtgCopy = pThis;
1834 RTListAppend(pHashList, &pThis->ListEntry);
1835 RTSemFastMutexRelease(pDevExt->mtxTracer);
1836 return rc;
1837 }
1838
1839 /*
1840 * Someone raced us, free our copy and return the existing
1841 * one instead.
1842 */
1843 RTSemFastMutexRelease(pDevExt->mtxTracer);
1844 }
1845 }
1846 }
1847 }
1848 RTMemFree(pThis);
1849 return rc;
1850}
1851
1852
1853/**
1854 * Undoes what supdrvTracerUmodSetProbeIds did.
1855 *
1856 * @param pDevExt The device extension.
1857 * @param pSession The current session.
1858 * @param pUmod The user tracepoint module.
1859 */
1860static void supdrvTracerUmodClearProbeIds(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUMOD pUmod)
1861{
1862 uint32_t i;
1863
1864 AssertReturnVoid(pUmod->iLookupTable < RT_ELEMENTS(pSession->apTpLookupTable));
1865 AssertReturnVoid(pSession->apTpLookupTable[pUmod->iLookupTable] == pUmod);
1866
1867 /*
1868 * Clear the probe IDs and disable the probes.
1869 */
1870 i = pUmod->cProbeLocs;
1871 if (pUmod->cBits == 32)
1872 {
1873 PVTGPROBELOC32 paProbeLocs = (PVTGPROBELOC32)pUmod->pvProbeLocs;
1874 while (i-- > 0)
1875 paProbeLocs[i].idProbe = 0;
1876 }
1877 else
1878 {
1879 PVTGPROBELOC64 paProbeLocs = (PVTGPROBELOC64)pUmod->pvProbeLocs;
1880 while (i-- > 0)
1881 paProbeLocs[i].idProbe = 0;
1882 }
1883
1884 /*
1885 * Free the lookup table entry. We'll have to wait for the table to go
1886 * idle to make sure there are no current users of pUmod.
1887 */
1888 RTSemFastMutexRequest(pDevExt->mtxTracer);
1889 if (pSession->apTpLookupTable[pUmod->iLookupTable] == pUmod)
1890 {
1891 if (pSession->cTpProbesFiring > 0)
1892 {
1893 i = 0;
1894 while (pSession->cTpProbesFiring > 0)
1895 {
1896 RTSemFastMutexRelease(pDevExt->mtxTracer);
1897 i++;
1898 if (!(i & 0xff))
1899 SUPR0Printf("supdrvTracerUmodClearProbeIds: waiting for lookup table to go idle (i=%u)\n", i);
1900 RTThreadSleep(10);
1901 RTSemFastMutexRequest(pDevExt->mtxTracer);
1902 }
1903 }
1904 ASMAtomicWriteNullPtr(&pSession->apTpLookupTable[pUmod->iLookupTable]);
1905 }
1906 RTSemFastMutexRelease(pDevExt->mtxTracer);
1907}
1908
1909
1910/**
1911 * Allocates a lookup table entry for the Umod and sets the
1912 * VTGPROBELOC::idProbe fields in user mode.
1913 *
1914 * @returns VINF_SUCCESS or VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS.
1915 * @param pDevExt The device extension.
1916 * @param pSession The current session.
1917 * @param pUmod The user tracepoint module.
1918 */
1919static int supdrvTracerUmodSetProbeIds(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUMOD pUmod)
1920{
1921 uint32_t iBase;
1922 uint32_t i;
1923
1924 /*
1925 * Allocate a lookup table entry.
1926 */
1927 RTSemFastMutexRequest(pDevExt->mtxTracer);
1928 for (i = 0; i < RT_ELEMENTS(pSession->apTpLookupTable); i++)
1929 {
1930 if (!pSession->apTpLookupTable[i])
1931 {
1932 pSession->apTpLookupTable[i] = pUmod;
1933 pUmod->iLookupTable = i;
1934 break;
1935 }
1936 }
1937 RTSemFastMutexRelease(pDevExt->mtxTracer);
1938 if (i >= RT_ELEMENTS(pSession->apTpLookupTable))
1939 return VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS;
1940
1941 /*
1942 * Set probe IDs of the usermode probe location to indicate our lookup
1943 * table entry as well as the probe location array entry.
1944 */
1945 iBase = (uint32_t)pUmod->iLookupTable << 24;
1946 i = pUmod->cProbeLocs;
1947 if (pUmod->cBits == 32)
1948 {
1949 PVTGPROBELOC32 paProbeLocs = (PVTGPROBELOC32)pUmod->pvProbeLocs;
1950 while (i-- > 0)
1951 paProbeLocs[i].idProbe = iBase | i;
1952 }
1953 else
1954 {
1955 PVTGPROBELOC64 paProbeLocs = (PVTGPROBELOC64)pUmod->pvProbeLocs;
1956 while (i-- > 0)
1957 paProbeLocs[i].idProbe = iBase | i;
1958 }
1959
1960 return VINF_SUCCESS;
1961}
1962
1963
1964int VBOXCALL supdrvIOCtl_TracerUmodRegister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession,
1965 RTR3PTR R3PtrVtgHdr, RTUINTPTR uVtgHdrAddr,
1966 RTR3PTR R3PtrStrTab, uint32_t cbStrTab,
1967 const char *pszModName, uint32_t fFlags)
1968{
1969 VTGOBJHDR Hdr;
1970 PSUPDRVTRACERUMOD pUmod;
1971 RTR3PTR R3PtrLock;
1972 size_t cbLock;
1973 uint32_t cProbeLocs;
1974 int rc;
1975
1976 /*
1977 * Validate input.
1978 */
1979 if (pSession->R0Process == NIL_RTR0PROCESS)
1980 return VERR_INVALID_CONTEXT;
1981 if ( fFlags != SUP_TRACER_UMOD_FLAGS_EXE
1982 && fFlags != SUP_TRACER_UMOD_FLAGS_SHARED)
1983 return VERR_INVALID_PARAMETER;
1984
1985 if (pSession->cTpProviders >= RT_ELEMENTS(pSession->apTpLookupTable))
1986 return VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS;
1987
1988 if ( cbStrTab < 2
1989 || cbStrTab > _1M)
1990 return VERR_SUPDRV_TRACER_UMOD_STRTAB_TOO_BIG;
1991
1992 /*
1993 * Read the VTG header into a temporary buffer and perform some simple
1994 * validations to make sure we aren't wasting our time here.
1995 */
1996 rc = RTR0MemUserCopyFrom(&Hdr, R3PtrVtgHdr, sizeof(Hdr));
1997 if (RT_FAILURE(rc))
1998 return rc;
1999 rc = supdrvVtgValidateHdr(&Hdr, uVtgHdrAddr, NULL, 0, true /*fUmod*/);
2000 if (RT_FAILURE(rc))
2001 return rc;
2002 if (Hdr.cbProviders / sizeof(VTGDESCPROVIDER) > 2)
2003 return VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS;
2004
2005 /*
2006 * Check how much needs to be locked down and how many probe locations
2007 * there are.
2008 */
2009 if ( Hdr.offProbeLocs <= 0
2010 || Hdr.offProbeEnabled > (uint32_t)Hdr.offProbeLocs
2011 || (uint32_t)Hdr.offProbeLocs - Hdr.offProbeEnabled - Hdr.cbProbeEnabled > 128)
2012 return VERR_SUPDRV_TRACER_UMOD_NOT_ADJACENT;
2013 R3PtrLock = R3PtrVtgHdr + Hdr.offProbeEnabled;
2014 cbLock = Hdr.offProbeLocs + Hdr.cbProbeLocs - Hdr.offProbeEnabled + (R3PtrLock & PAGE_OFFSET_MASK);
2015 R3PtrLock &= ~(RTR3PTR)PAGE_OFFSET_MASK;
2016 if (cbLock > _64K)
2017 return VERR_SUPDRV_TRACER_UMOD_TOO_MANY_PROBES;
2018
2019 cProbeLocs = Hdr.cbProbeLocs / (Hdr.cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
2020
2021 /*
2022 * Allocate the tracker data we keep in the session.
2023 */
2024 pUmod = (PSUPDRVTRACERUMOD)RTMemAllocZ( RT_UOFFSETOF_DYN(SUPDRVTRACERUMOD, aProbeLocs[cProbeLocs])
2025 + (Hdr.cbProbeEnabled / sizeof(uint32_t) * sizeof(SUPDRVPROBEINFO)) );
2026 if (!pUmod)
2027 return VERR_NO_MEMORY;
2028 pUmod->u32Magic = SUPDRVTRACERUMOD_MAGIC;
2029 RTListInit(&pUmod->ListEntry);
2030 pUmod->R3PtrVtgHdr = R3PtrVtgHdr;
2031 pUmod->pVtgCopy = NULL;
2032 pUmod->hMemObjLock = NIL_RTR0MEMOBJ;
2033 pUmod->hMemObjMap = NIL_RTR0MEMOBJ;
2034 pUmod->R3PtrProbeLocs = (RTR3INTPTR)R3PtrVtgHdr + Hdr.offProbeLocs;
2035 pUmod->iLookupTable = UINT8_MAX;
2036 pUmod->cBits = Hdr.cBits;
2037 pUmod->cbProbeLoc = Hdr.cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64);
2038 pUmod->cProbeLocs = cProbeLocs;
2039
2040 /*
2041 * Lock down and map the user-mode structures.
2042 */
2043 rc = RTR0MemObjLockUser(&pUmod->hMemObjLock, R3PtrLock, cbLock, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
2044 if (RT_SUCCESS(rc))
2045 {
2046 rc = RTR0MemObjMapKernel(&pUmod->hMemObjMap, pUmod->hMemObjLock, (void *)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
2047 if (RT_SUCCESS(rc))
2048 {
2049 pUmod->pacProbeEnabled = (uint32_t *)( (uintptr_t)RTR0MemObjAddress(pUmod->hMemObjMap)
2050 + ((uintptr_t)(R3PtrVtgHdr + Hdr.offProbeEnabled) & PAGE_OFFSET_MASK));
2051 pUmod->pvProbeLocs = (uint8_t *)pUmod->pacProbeEnabled + Hdr.offProbeLocs - Hdr.offProbeEnabled;
2052
2053 /*
2054 * Does some other process use the same module already? If so,
2055 * share the VTG data with it. Otherwise, make a ring-0 copy it.
2056 */
2057 pUmod->pVtgCopy = supdrvVtgFindObjectCopy(pDevExt, &Hdr, cbStrTab, fFlags);
2058 if (!pUmod->pVtgCopy)
2059 rc = supdrvVtgCreateObjectCopy(pDevExt, &Hdr, R3PtrVtgHdr, uVtgHdrAddr, R3PtrStrTab, cbStrTab, fFlags, pUmod);
2060 if (RT_SUCCESS(rc))
2061 {
2062 AssertPtr(pUmod->pVtgCopy);
2063
2064 /*
2065 * Grabe a place in apTpLookupTable and set the probe IDs
2066 * accordingly.
2067 */
2068 rc = supdrvTracerUmodSetProbeIds(pDevExt, pSession, pUmod);
2069 if (RT_SUCCESS(rc))
2070 {
2071 /*
2072 * Register the providers.
2073 */
2074 rc = supdrvTracerRegisterVtgObj(pDevExt, &pUmod->pVtgCopy->Hdr,
2075 NULL /*pImage*/, pSession, pUmod, pszModName);
2076 if (RT_SUCCESS(rc))
2077 {
2078 RTSemFastMutexRequest(pDevExt->mtxTracer);
2079 RTListAppend(&pSession->TpUmods, &pUmod->ListEntry);
2080 RTSemFastMutexRelease(pDevExt->mtxTracer);
2081
2082 return VINF_SUCCESS;
2083 }
2084
2085 /* bail out. */
2086 supdrvTracerUmodClearProbeIds(pDevExt, pSession, pUmod);
2087 }
2088 supdrvVtgReleaseObjectCopy(pDevExt, pUmod->pVtgCopy);
2089 }
2090 RTR0MemObjFree(pUmod->hMemObjMap, false /*fFreeMappings*/);
2091 }
2092 RTR0MemObjFree(pUmod->hMemObjLock, false /*fFreeMappings*/);
2093 }
2094 pUmod->u32Magic = ~SUPDRVTRACERUMOD_MAGIC;
2095 RTMemFree(pUmod);
2096 return rc;
2097}
2098
2099
2100int VBOXCALL supdrvIOCtl_TracerUmodDeregister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, RTR3PTR R3PtrVtgHdr)
2101{
2102 PSUPDRVTRACERUMOD pUmod = NULL;
2103 uint32_t i;
2104 int rc;
2105
2106 /*
2107 * Validate the request.
2108 */
2109 RTSemFastMutexRequest(pDevExt->mtxTracer);
2110 for (i = 0; i < RT_ELEMENTS(pSession->apTpLookupTable); i++)
2111 {
2112 pUmod = pSession->apTpLookupTable[i];
2113 if ( pUmod
2114 && pUmod->u32Magic == SUPDRVTRACERUMOD_MAGIC
2115 && pUmod->R3PtrVtgHdr == R3PtrVtgHdr)
2116 break;
2117 }
2118 RTSemFastMutexRelease(pDevExt->mtxTracer);
2119 if (pUmod)
2120 {
2121 SUPDRVTPPROVIDER *pProvNext;
2122 SUPDRVTPPROVIDER *pProv;
2123
2124 /*
2125 * Remove ourselves from the lookup table and clean up the ring-3 bits
2126 * we've dirtied. We do this first to make sure no probes are firing
2127 * when we're destroying the providers in the next step.
2128 */
2129 supdrvTracerUmodClearProbeIds(pDevExt, pSession, pUmod);
2130
2131 /*
2132 * Deregister providers related to the VTG object.
2133 */
2134 RTSemFastMutexRequest(pDevExt->mtxTracer);
2135 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
2136 {
2137 if (pProv->pUmod == pUmod)
2138 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
2139 }
2140 RTSemFastMutexRelease(pDevExt->mtxTracer);
2141
2142 /*
2143 * Destroy the Umod object.
2144 */
2145 pUmod->u32Magic = ~SUPDRVTRACERUMOD_MAGIC;
2146 supdrvVtgReleaseObjectCopy(pDevExt, pUmod->pVtgCopy);
2147 RTR0MemObjFree(pUmod->hMemObjMap, false /*fFreeMappings*/);
2148 RTR0MemObjFree(pUmod->hMemObjLock, false /*fFreeMappings*/);
2149 RTMemFree(pUmod);
2150 rc = VINF_SUCCESS;
2151 }
2152 else
2153 rc = VERR_NOT_FOUND;
2154 return rc;
2155}
2156
2157
2158/**
2159 * Implementation of supdrvIOCtl_TracerUmodProbeFire and
2160 * SUPR0TracerUmodProbeFire.
2161 *
2162 * @param pDevExt The device extension.
2163 * @param pSession The calling session.
2164 * @param pCtx The context record.
2165 */
2166static void supdrvTracerUmodProbeFire(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx)
2167{
2168 /*
2169 * We cannot trust user mode to hand us the right bits nor not calling us
2170 * when disabled. So, we have to check for our selves.
2171 */
2172 PSUPDRVTRACERUMOD pUmod;
2173 uint32_t const iLookupTable = pCtx->idProbe >> 24;
2174 uint32_t const iProbeLoc = pCtx->idProbe & UINT32_C(0x00ffffff);
2175
2176 if (RT_UNLIKELY( !pDevExt->pTracerOps
2177 || pDevExt->fTracerUnloading))
2178 return;
2179 if (RT_UNLIKELY(iLookupTable >= RT_ELEMENTS(pSession->apTpLookupTable)))
2180 return;
2181 if (RT_UNLIKELY( pCtx->cBits != 32
2182 && pCtx->cBits != 64))
2183 return;
2184
2185 ASMAtomicIncU32(&pSession->cTpProviders);
2186
2187 pUmod = pSession->apTpLookupTable[iLookupTable];
2188 if (RT_LIKELY(pUmod))
2189 {
2190 if (RT_LIKELY( pUmod->u32Magic == SUPDRVTRACERUMOD_MAGIC
2191 && iProbeLoc < pUmod->cProbeLocs
2192 && pCtx->cBits == pUmod->cBits))
2193 {
2194#if 0 /* This won't work for RC modules. */
2195 RTR3PTR R3PtrProbeLoc = pUmod->R3PtrProbeLocs + iProbeLoc * pUmod->cbProbeLoc;
2196 if (RT_LIKELY( (pCtx->cBits == 32 ? (RTR3PTR)pCtx->u.X86.uVtgProbeLoc : pCtx->u.Amd64.uVtgProbeLoc)
2197 == R3PtrProbeLoc))
2198#endif
2199 {
2200 if (RT_LIKELY(pUmod->aProbeLocs[iProbeLoc].fEnabled))
2201 {
2202 PSUPDRVVTGCOPY pVtgCopy;
2203 ASMAtomicIncU32(&pDevExt->cTracerCallers);
2204 pVtgCopy = pUmod->pVtgCopy;
2205 if (RT_LIKELY( pDevExt->pTracerOps
2206 && !pDevExt->fTracerUnloading
2207 && pVtgCopy))
2208 {
2209 PCVTGPROBELOC pProbeLocRO;
2210 pProbeLocRO = (PCVTGPROBELOC)((uintptr_t)&pVtgCopy->Hdr + pVtgCopy->Hdr.offProbeLocs) + iProbeLoc;
2211
2212 pCtx->idProbe = pUmod->aProbeLocs[iProbeLoc].idProbe;
2213 pDevExt->pTracerOps->pfnProbeFireUser(pDevExt->pTracerOps, pSession, pCtx, &pVtgCopy->Hdr, pProbeLocRO);
2214 }
2215 ASMAtomicDecU32(&pDevExt->cTracerCallers);
2216 }
2217 }
2218 }
2219 }
2220
2221 ASMAtomicDecU32(&pSession->cTpProviders);
2222}
2223
2224
2225SUPR0DECL(void) SUPR0TracerUmodProbeFire(PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx)
2226{
2227 AssertReturnVoid(SUP_IS_SESSION_VALID(pSession));
2228 AssertPtrReturnVoid(pCtx);
2229
2230 supdrvTracerUmodProbeFire(pSession->pDevExt, pSession, pCtx);
2231}
2232
2233
2234void VBOXCALL supdrvIOCtl_TracerUmodProbeFire(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx)
2235{
2236 supdrvTracerUmodProbeFire(pDevExt, pSession, pCtx);
2237}
2238
2239
2240/**
2241 * Open the tracer.
2242 *
2243 * @returns VBox status code
2244 * @param pDevExt The device extension structure.
2245 * @param pSession The current session.
2246 * @param uCookie The tracer cookie.
2247 * @param uArg The tracer open argument.
2248 */
2249int VBOXCALL supdrvIOCtl_TracerOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg)
2250{
2251 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
2252 int rc;
2253
2254 RTSemFastMutexRequest(pDevExt->mtxTracer);
2255
2256 if (!pSession->uTracerData)
2257 {
2258 if (pDevExt->pTracerOps)
2259 {
2260 if (pDevExt->pTracerSession != pSession)
2261 {
2262 if (!pDevExt->fTracerUnloading)
2263 {
2264 if (pSession->hTracerCaller == NIL_RTNATIVETHREAD)
2265 {
2266 pDevExt->cTracerOpens++;
2267 pSession->uTracerData = ~(uintptr_t)0;
2268 pSession->hTracerCaller = hNativeSelf;
2269 RTSemFastMutexRelease(pDevExt->mtxTracer);
2270
2271 rc = pDevExt->pTracerOps->pfnTracerOpen(pDevExt->pTracerOps, pSession, uCookie, uArg, &pSession->uTracerData);
2272
2273 RTSemFastMutexRequest(pDevExt->mtxTracer);
2274 if (RT_FAILURE(rc))
2275 {
2276 pDevExt->cTracerOpens--;
2277 pSession->uTracerData = 0;
2278 }
2279 pSession->hTracerCaller = NIL_RTNATIVETHREAD;
2280 }
2281 else
2282 rc = VERR_SUPDRV_TRACER_SESSION_BUSY;
2283 }
2284 else
2285 rc = VERR_SUPDRV_TRACER_UNLOADING;
2286 }
2287 else
2288 rc = VERR_SUPDRV_TRACER_CANNOT_OPEN_SELF;
2289 }
2290 else
2291 rc = VERR_SUPDRV_TRACER_NOT_PRESENT;
2292 }
2293 else
2294 rc = VERR_SUPDRV_TRACER_ALREADY_OPENED;
2295
2296 RTSemFastMutexRelease(pDevExt->mtxTracer);
2297 return rc;
2298}
2299
2300
2301/**
2302 * Closes the tracer.
2303 *
2304 * @returns VBox status code.
2305 * @param pDevExt The device extension structure.
2306 * @param pSession The current session.
2307 */
2308int VBOXCALL supdrvIOCtl_TracerClose(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
2309{
2310 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
2311 int rc;
2312
2313 RTSemFastMutexRequest(pDevExt->mtxTracer);
2314
2315 if (pSession->uTracerData)
2316 {
2317 Assert(pDevExt->cTracerOpens > 0);
2318
2319 if (pDevExt->pTracerOps)
2320 {
2321 if (pSession->hTracerCaller == NIL_RTNATIVETHREAD)
2322 {
2323 uintptr_t uTracerData = pSession->uTracerData;
2324 pSession->uTracerData = 0;
2325 pSession->hTracerCaller = hNativeSelf;
2326 RTSemFastMutexRelease(pDevExt->mtxTracer);
2327
2328 pDevExt->pTracerOps->pfnTracerClose(pDevExt->pTracerOps, pSession, uTracerData);
2329 rc = VINF_SUCCESS;
2330
2331 RTSemFastMutexRequest(pDevExt->mtxTracer);
2332 pSession->hTracerCaller = NIL_RTNATIVETHREAD;
2333 Assert(pDevExt->cTracerOpens > 0);
2334 pDevExt->cTracerOpens--;
2335 }
2336 else
2337 rc = VERR_SUPDRV_TRACER_SESSION_BUSY;
2338 }
2339 else
2340 {
2341 rc = VERR_SUPDRV_TRACER_NOT_PRESENT;
2342 pSession->uTracerData = 0;
2343 Assert(pDevExt->cTracerOpens > 0);
2344 pDevExt->cTracerOpens--;
2345 }
2346 }
2347 else
2348 rc = VERR_SUPDRV_TRACER_NOT_OPENED;
2349
2350 RTSemFastMutexRelease(pDevExt->mtxTracer);
2351 return rc;
2352}
2353
2354
2355/**
2356 * Performs a tracer I/O control request.
2357 *
2358 * @returns VBox status code.
2359 * @param pDevExt The device extension structure.
2360 * @param pSession The current session.
2361 * @param uCmd The tracer command.
2362 * @param uArg The tracer argument.
2363 * @param piRetVal Where to store the tracer specific return value.
2364 */
2365int VBOXCALL supdrvIOCtl_TracerIOCtl(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
2366{
2367 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
2368 int rc;
2369
2370 *piRetVal = 0;
2371 RTSemFastMutexRequest(pDevExt->mtxTracer);
2372
2373 if (pSession->uTracerData)
2374 {
2375 Assert(pDevExt->cTracerOpens > 0);
2376 if (pDevExt->pTracerOps)
2377 {
2378 if (!pDevExt->fTracerUnloading)
2379 {
2380 if (pSession->hTracerCaller == NIL_RTNATIVETHREAD)
2381 {
2382 uintptr_t uTracerData = pSession->uTracerData;
2383 pDevExt->cTracerOpens++;
2384 pSession->hTracerCaller = hNativeSelf;
2385 RTSemFastMutexRelease(pDevExt->mtxTracer);
2386
2387 rc = pDevExt->pTracerOps->pfnTracerIoCtl(pDevExt->pTracerOps, pSession, uTracerData, uCmd, uArg, piRetVal);
2388
2389 RTSemFastMutexRequest(pDevExt->mtxTracer);
2390 pSession->hTracerCaller = NIL_RTNATIVETHREAD;
2391 Assert(pDevExt->cTracerOpens > 0);
2392 pDevExt->cTracerOpens--;
2393 }
2394 else
2395 rc = VERR_SUPDRV_TRACER_SESSION_BUSY;
2396 }
2397 else
2398 rc = VERR_SUPDRV_TRACER_UNLOADING;
2399 }
2400 else
2401 rc = VERR_SUPDRV_TRACER_NOT_PRESENT;
2402 }
2403 else
2404 rc = VERR_SUPDRV_TRACER_NOT_OPENED;
2405
2406 RTSemFastMutexRelease(pDevExt->mtxTracer);
2407 return rc;
2408}
2409
2410
2411/**
2412 * Early module initialization hook.
2413 *
2414 * @returns VBox status code.
2415 * @param pDevExt The device extension structure.
2416 */
2417int VBOXCALL supdrvTracerInit(PSUPDRVDEVEXT pDevExt)
2418{
2419 /*
2420 * Initialize the tracer.
2421 */
2422 int rc = RTSemFastMutexCreate(&pDevExt->mtxTracer);
2423 if (RT_SUCCESS(rc))
2424 {
2425 uint32_t i;
2426
2427 pDevExt->TracerHlp.uVersion = SUPDRVTRACERHLP_VERSION;
2428 /** @todo */
2429 pDevExt->TracerHlp.uEndVersion = SUPDRVTRACERHLP_VERSION;
2430 RTListInit(&pDevExt->TracerProviderList);
2431 RTListInit(&pDevExt->TracerProviderZombieList);
2432 for (i = 0; i < RT_ELEMENTS(pDevExt->aTrackerUmodHash); i++)
2433 RTListInit(&pDevExt->aTrackerUmodHash[i]);
2434
2435#ifdef VBOX_WITH_NATIVE_DTRACE
2436 pDevExt->pTracerOps = supdrvDTraceInit();
2437 if (pDevExt->pTracerOps)
2438 g_pfnSupdrvProbeFireKernel = (PFNRT)pDevExt->pTracerOps->pfnProbeFireKernel;
2439#endif
2440
2441 /*
2442 * Register the provider for this module, if compiled in.
2443 */
2444#ifdef VBOX_WITH_DTRACE_R0DRV
2445 rc = supdrvTracerRegisterVtgObj(pDevExt, &g_VTGObjHeader, NULL /*pImage*/, NULL /*pSession*/, NULL /*pUmod*/, "vboxdrv");
2446 if (RT_SUCCESS(rc))
2447 return rc;
2448 SUPR0Printf("supdrvTracerInit: supdrvTracerRegisterVtgObj failed with rc=%d\n", rc);
2449 RTSemFastMutexDestroy(pDevExt->mtxTracer);
2450#else
2451
2452 return VINF_SUCCESS;
2453#endif
2454 }
2455 pDevExt->mtxTracer = NIL_RTSEMFASTMUTEX;
2456 return rc;
2457}
2458
2459
2460/**
2461 * Late module termination hook.
2462 *
2463 * @returns VBox status code.
2464 * @param pDevExt The device extension structure.
2465 */
2466void VBOXCALL supdrvTracerTerm(PSUPDRVDEVEXT pDevExt)
2467{
2468 LOG_TRACER(("supdrvTracerTerm\n"));
2469
2470 supdrvTracerRemoveAllProviders(pDevExt);
2471#ifdef VBOX_WITH_NATIVE_DTRACE
2472 supdrvDTraceFini();
2473#endif
2474 RTSemFastMutexDestroy(pDevExt->mtxTracer);
2475 pDevExt->mtxTracer = NIL_RTSEMFASTMUTEX;
2476 LOG_TRACER(("supdrvTracerTerm: Done\n"));
2477}
2478
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette