VirtualBox

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

Last change on this file since 58340 was 58340, checked in by vboxsync, 9 years ago

HostDrivers: Doxygen fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 89.6 KB
Line 
1/* $Id: SUPDrvTracer.cpp 58340 2015-10-20 13:58:41Z vboxsync $ */
2/** @file
3 * VBoxDrv - The VirtualBox Support Driver - Tracer Interface.
4 */
5
6/*
7 * Copyright (C) 2012-2015 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 }
541
542 /*
543 * Validate probes.
544 */
545 i = pVtgHdr->cbProbes / sizeof(VTGDESCPROBE);
546 while (i-- > 0)
547 {
548 PCVTGDESCPROBE pProbe = (PCVTGDESCPROBE)( (uintptr_t)pVtgHdr + pVtgHdr->offProbes) + i;
549 PCVTGDESCPROVIDER pProvider = (PCVTGDESCPROVIDER)((uintptr_t)pVtgHdr + pVtgHdr->offProviders) + pProbe->idxProvider;
550 PCVTGDESCARGLIST pArgList = (PCVTGDESCARGLIST)( (uintptr_t)pVtgHdr + pVtgHdr->offArgLists + pProbe->offArgList );
551 unsigned iArg;
552 bool fHaveLargeArgs;
553
554
555 MY_VALIDATE_STR(pProbe->offName);
556 MY_CHECK_RET(pProbe->offArgList < pVtgHdr->cbArgLists, VERR_SUPDRV_VTG_BAD_PROBE);
557 MY_CHECK_RET((pProbe->offArgList & 3) == 0, VERR_SUPDRV_VTG_BAD_PROBE);
558 MY_CHECK_RET(pProbe->idxEnabled == i, VERR_SUPDRV_VTG_BAD_PROBE); /* The lists are parallel. */
559 MY_CHECK_RET(pProbe->idxProvider < cProviders, VERR_SUPDRV_VTG_BAD_PROBE);
560 MY_CHECK_RET(i - pProvider->iFirstProbe < pProvider->cProbes, VERR_SUPDRV_VTG_BAD_PROBE);
561 if (pProbe->offObjHdr != (intptr_t)pVtgHdr - (intptr_t)pProbe)
562 {
563 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_PROBE - iProbe=%u offObjHdr=%d expected %zd\n",
564 i, pProbe->offObjHdr, (intptr_t)pVtgHdr - (intptr_t)pProbe);
565 return VERR_SUPDRV_VTG_BAD_PROBE;
566 }
567
568 /* The referenced argument list. */
569 if (pArgList->cArgs > 16)
570 {
571 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_ARGLIST - iProbe=%u cArgs=%u\n", i, pArgList->cArgs);
572 return VERR_SUPDRV_VTG_BAD_ARGLIST;
573 }
574 if (pArgList->fHaveLargeArgs >= 2)
575 {
576 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_ARGLIST - iProbe=%u fHaveLargeArgs=%d\n", i, pArgList->fHaveLargeArgs);
577 return VERR_SUPDRV_VTG_BAD_ARGLIST;
578 }
579 if ( pArgList->abReserved[0]
580 || pArgList->abReserved[1])
581 {
582 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_VTG_BAD_ARGLIST - reserved MBZ iProbe=%u\n", i);
583 return VERR_SUPDRV_VTG_BAD_ARGLIST;
584 }
585 fHaveLargeArgs = false;
586 iArg = pArgList->cArgs;
587 while (iArg-- > 0)
588 {
589 uint32_t const fType = pArgList->aArgs[iArg].fType;
590 if (fType & ~VTG_TYPE_VALID_MASK)
591 {
592 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - fType=%#x iArg=%u iProbe=%u (#0)\n", fType, iArg, i);
593 return VERR_SUPDRV_TRACER_BAD_ARG_FLAGS;
594 }
595
596 switch (pArgList->aArgs[iArg].fType & VTG_TYPE_SIZE_MASK)
597 {
598 case 0:
599 if (pArgList->aArgs[iArg].fType & VTG_TYPE_FIXED_SIZED)
600 {
601 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - fType=%#x iArg=%u iProbe=%u (#1)\n", fType, iArg, i);
602 return VERR_SUPDRV_TRACER_BAD_ARG_FLAGS;
603 }
604 break;
605 case 1: case 2: case 4: case 8:
606 break;
607 default:
608 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - fType=%#x iArg=%u iProbe=%u (#2)\n", fType, iArg, i);
609 return VERR_SUPDRV_TRACER_BAD_ARG_FLAGS;
610 }
611 if (VTG_TYPE_IS_LARGE(pArgList->aArgs[iArg].fType))
612 fHaveLargeArgs = true;
613
614 MY_VALIDATE_STR(pArgList->aArgs[iArg].offType);
615 }
616 if ((uint8_t)fHaveLargeArgs != pArgList->fHaveLargeArgs)
617 {
618 SUPR0Printf("supdrvVtgValidate: VERR_SUPDRV_TRACER_BAD_ARG_FLAGS - iProbe=%u fHaveLargeArgs=%d expected %d\n",
619 i, pArgList->fHaveLargeArgs, fHaveLargeArgs);
620 return VERR_SUPDRV_VTG_BAD_PROBE;
621 }
622 }
623
624 /*
625 * Check that pacProbeEnabled is all zeros.
626 */
627 {
628 uint32_t const *pcProbeEnabled = (uint32_t const *)((uintptr_t)pVtgHdr + pVtgHdr->offProbeEnabled);
629 i = pVtgHdr->cbProbeEnabled / sizeof(uint32_t);
630 while (i-- > 0)
631 MY_CHECK_RET(pcProbeEnabled[0] == 0, VERR_SUPDRV_VTG_BAD_PROBE_ENABLED);
632 }
633
634 /*
635 * Probe locations.
636 */
637 {
638 PVTGPROBELOC paProbeLocs = (PVTGPROBELOC)((intptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
639 i = pVtgHdr->cbProbeLocs / sizeof(VTGPROBELOC);
640 while (i-- > 0)
641 {
642 MY_CHECK_RET(paProbeLocs[i].uLine < _1G, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
643 MY_CHECK_RET(paProbeLocs[i].fEnabled == false, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
644 MY_CHECK_RET(paProbeLocs[i].idProbe == 0, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
645 offTmp = (uintptr_t)paProbeLocs[i].pProbe - (uintptr_t)pVtgHdr->offProbes - (uintptr_t)pVtgHdr;
646#ifdef RT_OS_DARWIN /* See header validation code. */
647 if ( offTmp >= pVtgHdr->cbProbes
648 && pVtgHdr->u64VtgObjSectionStart != uVtgHdrAddr
649 && pVtgHdr->u64VtgObjSectionStart < _4M
650 && (uintptr_t)paProbeLocs[i].pProbe < _4M
651 && !fUmod )
652 {
653 uint64_t offDelta = uVtgHdrAddr - pVtgHdr->u64VtgObjSectionStart;
654
655 paProbeLocs[i].pProbe = (PVTGDESCPROBE)((uintptr_t)paProbeLocs[i].pProbe + offDelta);
656 if ((uintptr_t)paProbeLocs[i].pszFunction < _4M)
657 paProbeLocs[i].pszFunction = (const char *)((uintptr_t)paProbeLocs[i].pszFunction + offDelta);
658
659 offTmp += offDelta;
660 }
661#endif
662 MY_CHECK_RET(offTmp < pVtgHdr->cbProbes, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
663 MY_CHECK_RET(offTmp / sizeof(VTGDESCPROBE) * sizeof(VTGDESCPROBE) == offTmp, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
664 MY_WITHIN_IMAGE(paProbeLocs[i].pszFunction, VERR_SUPDRV_VTG_BAD_PROBE_LOC);
665 }
666 }
667
668 return VINF_SUCCESS;
669}
670
671#undef MY_VALIDATE_STR
672#undef MY_VALIDATE_ATTR
673#undef MY_WITHIN_IMAGE
674
675
676/**
677 * Gets a string from the string table.
678 *
679 * @returns Pointer to the string.
680 * @param pVtgHdr The VTG object header.
681 * @param offStrTab The string table offset.
682 */
683static const char *supdrvVtgGetString(PVTGOBJHDR pVtgHdr, uint32_t offStrTab)
684{
685 Assert(offStrTab < pVtgHdr->cbStrTab);
686 return (char *)pVtgHdr + pVtgHdr->offStrTab + offStrTab;
687}
688
689
690/**
691 * Frees the provider structure and associated resources.
692 *
693 * @param pProv The provider to free.
694 */
695static void supdrvTracerFreeProvider(PSUPDRVTPPROVIDER pProv)
696{
697 LOG_TRACER(("Freeing tracepoint provider '%s' / %p\n", pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
698 pProv->fRegistered = false;
699 pProv->fZombie = true;
700 pProv->Core.pDesc = NULL;
701 pProv->Core.pHdr = NULL;
702 pProv->Core.paProbeLocsRO = NULL;
703 pProv->Core.pvProbeLocsEn = NULL;
704 pProv->Core.pacProbeEnabled = NULL;
705 pProv->Core.paR0ProbeLocs = NULL;
706 pProv->Core.paR0Probes = NULL;
707 RT_ZERO(pProv->Core.TracerData);
708 RTMemFree(pProv);
709}
710
711
712/**
713 * Unlinks and deregisters a provider.
714 *
715 * If the provider is still busy, it will be put in the zombie list.
716 *
717 * @param pDevExt The device extension.
718 * @param pProv The provider.
719 *
720 * @remarks The caller owns mtxTracer.
721 */
722static void supdrvTracerDeregisterVtgObj(PSUPDRVDEVEXT pDevExt, PSUPDRVTPPROVIDER pProv)
723{
724 int rc;
725
726 RTListNodeRemove(&pProv->ListEntry);
727 if (pProv->pSession)
728 {
729 RTListNodeRemove(&pProv->SessionListEntry);
730 RTListInit(&pProv->SessionListEntry);
731 pProv->pSession->cTpProviders--;
732 }
733
734 if (!pProv->fRegistered || !pDevExt->pTracerOps)
735 rc = VINF_SUCCESS;
736 else
737 rc = pDevExt->pTracerOps->pfnProviderDeregister(pDevExt->pTracerOps, &pProv->Core);
738 if (RT_SUCCESS(rc))
739 {
740 supdrvTracerFreeProvider(pProv);
741 return;
742 }
743
744 pProv->fZombie = true;
745 pProv->pImage = NULL;
746 pProv->pSession = NULL;
747 pProv->pUmod = NULL;
748 pProv->Core.pDesc = NULL;
749 pProv->Core.pHdr = NULL;
750 pProv->Core.paProbeLocsRO = NULL;
751 pProv->Core.pvProbeLocsEn = NULL;
752 pProv->Core.pacProbeEnabled = NULL;
753 pProv->Core.paR0ProbeLocs = NULL;
754
755 RTListAppend(&pDevExt->TracerProviderZombieList, &pProv->ListEntry);
756 LOG_TRACER(("Invalidated provider '%s' / %p and put it on the zombie list (rc=%Rrc)\n",
757 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
758}
759
760
761/**
762 * Processes the zombie list.
763 *
764 * @param pDevExt The device extension.
765 */
766static void supdrvTracerProcessZombies(PSUPDRVDEVEXT pDevExt)
767{
768 PSUPDRVTPPROVIDER pProv, pProvNext;
769
770 RTSemFastMutexRequest(pDevExt->mtxTracer);
771 RTListForEachSafe(&pDevExt->TracerProviderZombieList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
772 {
773 int rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
774 if (RT_SUCCESS(rc))
775 {
776 RTListNodeRemove(&pProv->ListEntry);
777 supdrvTracerFreeProvider(pProv);
778 }
779 }
780 RTSemFastMutexRelease(pDevExt->mtxTracer);
781}
782
783
784/**
785 * Unregisters all providers, including zombies, waiting for busy providers to
786 * go idle and unregister smoothly.
787 *
788 * This may block.
789 *
790 * @param pDevExt The device extension.
791 */
792static void supdrvTracerRemoveAllProviders(PSUPDRVDEVEXT pDevExt)
793{
794 uint32_t i;
795 PSUPDRVTPPROVIDER pProv;
796 PSUPDRVTPPROVIDER pProvNext;
797
798 /*
799 * Unregister all probes (there should only be one).
800 */
801 RTSemFastMutexRequest(pDevExt->mtxTracer);
802 RTListForEachSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
803 {
804 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
805 }
806 RTSemFastMutexRelease(pDevExt->mtxTracer);
807
808 /*
809 * Try unregister zombies now, sleep on busy ones and tracer opens.
810 */
811 for (i = 0; ; i++)
812 {
813 bool fEmpty;
814
815 RTSemFastMutexRequest(pDevExt->mtxTracer);
816
817 /* Zombies */
818 RTListForEachSafe(&pDevExt->TracerProviderZombieList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
819 {
820 int rc;
821 LOG_TRACER(("supdrvTracerRemoveAllProviders: Attemting to unregister '%s' / %p...\n",
822 pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
823
824 if (pDevExt->pTracerOps)
825 rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
826 else
827 rc = VINF_SUCCESS;
828 if (!rc)
829 {
830 RTListNodeRemove(&pProv->ListEntry);
831 supdrvTracerFreeProvider(pProv);
832 }
833 else if (!(i & 0xf))
834 SUPR0Printf("supdrvTracerRemoveAllProviders: Waiting on busy provider '%s' / %p (rc=%d)\n",
835 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc);
836 else
837 LOG_TRACER(("supdrvTracerRemoveAllProviders: Failed to unregister provider '%s' / %p - rc=%d\n",
838 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
839 }
840
841 fEmpty = RTListIsEmpty(&pDevExt->TracerProviderZombieList);
842
843 /* Tracer opens. */
844 if ( pDevExt->cTracerOpens
845 && pDevExt->pTracerOps)
846 {
847 fEmpty = false;
848 if (!(i & 0xf))
849 SUPR0Printf("supdrvTracerRemoveAllProviders: Waiting on %u opens\n", pDevExt->cTracerOpens);
850 else
851 LOG_TRACER(("supdrvTracerRemoveAllProviders: Waiting on %u opens\n", pDevExt->cTracerOpens));
852 }
853
854 RTSemFastMutexRelease(pDevExt->mtxTracer);
855
856 if (fEmpty)
857 break;
858
859 /* Delay...*/
860 RTThreadSleep(1000);
861 }
862}
863
864
865/**
866 * Registers the VTG tracepoint providers of a driver.
867 *
868 * @returns VBox status code.
869 * @param pDevExt The device instance data.
870 * @param pVtgHdr The VTG object header.
871 * @param pImage The image if applicable.
872 * @param pSession The session if applicable.
873 * @param pUmod The associated user tracepoint module if
874 * applicable.
875 * @param pszModName The module name.
876 */
877static int supdrvTracerRegisterVtgObj(PSUPDRVDEVEXT pDevExt, PVTGOBJHDR pVtgHdr, PSUPDRVLDRIMAGE pImage,
878 PSUPDRVSESSION pSession, PSUPDRVTRACERUMOD pUmod, const char *pszModName)
879{
880 int rc;
881 uintptr_t i;
882 PSUPDRVTPPROVIDER pProv;
883 size_t cchModName;
884
885 /*
886 * Validate input.
887 */
888 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
889 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
890 AssertPtrNullReturn(pImage, VERR_INVALID_POINTER);
891 AssertPtrNullReturn(pSession, VERR_INVALID_POINTER);
892 AssertPtrReturn(pszModName, VERR_INVALID_POINTER);
893 cchModName = strlen(pszModName);
894
895 if (pImage)
896 rc = supdrvVtgValidate(pVtgHdr, (uintptr_t)pVtgHdr,
897 (const uint8_t *)pImage->pvImage, pImage->cbImageBits,
898 false /*fUmod*/);
899 else
900 rc = supdrvVtgValidate(pVtgHdr, (uintptr_t)pVtgHdr, NULL, 0, pUmod != NULL);
901 if (RT_FAILURE(rc))
902 return rc;
903
904 /*
905 * Check that there aren't any obvious duplicates.
906 * (Yes, this isn't race free, but it's good enough for now.)
907 */
908 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
909 if (RT_FAILURE(rc))
910 return rc;
911 if (pImage || !pSession || pSession->R0Process == NIL_RTPROCESS)
912 {
913 RTListForEach(&pDevExt->TracerProviderList, pProv, SUPDRVTPPROVIDER, ListEntry)
914 {
915 if (pProv->Core.pHdr == pVtgHdr)
916 {
917 rc = VERR_SUPDRV_VTG_ALREADY_REGISTERED;
918 break;
919 }
920
921 if ( pProv->pSession == pSession
922 && pProv->pImage == pImage)
923 {
924 rc = VERR_SUPDRV_VTG_ONLY_ONCE_PER_SESSION;
925 break;
926 }
927 }
928 }
929 else
930 {
931 RTListForEach(&pSession->TpProviders, pProv, SUPDRVTPPROVIDER, SessionListEntry)
932 {
933 if (pProv->Core.pHdr == pVtgHdr)
934 {
935 rc = VERR_SUPDRV_VTG_ALREADY_REGISTERED;
936 break;
937 }
938 }
939 }
940 RTSemFastMutexRelease(pDevExt->mtxTracer);
941 if (RT_FAILURE(rc))
942 return rc;
943
944 /*
945 * Register the providers.
946 */
947 i = pVtgHdr->cbProviders / sizeof(VTGDESCPROVIDER);
948 while (i-- > 0)
949 {
950 PVTGDESCPROVIDER pDesc = (PVTGDESCPROVIDER)((uintptr_t)pVtgHdr + pVtgHdr->offProviders) + i;
951 const char *pszName = supdrvVtgGetString(pVtgHdr, pDesc->offName);
952 size_t const cchName = strlen(pszName) + (pUmod ? 16 : 0);
953
954 pProv = (PSUPDRVTPPROVIDER)RTMemAllocZ(RT_OFFSETOF(SUPDRVTPPROVIDER, szName[cchName + 1 + cchModName + 1]));
955 if (pProv)
956 {
957 pProv->Core.pszName = &pProv->szName[0];
958 pProv->Core.pszModName = &pProv->szName[cchName + 1];
959 pProv->Core.pDesc = pDesc;
960 pProv->Core.pHdr = pVtgHdr;
961 pProv->Core.paProbeLocsRO = (PCVTGPROBELOC )((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
962 if (!pUmod)
963 {
964 pProv->Core.pvProbeLocsEn = (void *)((uintptr_t)pVtgHdr + pVtgHdr->offProbeLocs);
965 pProv->Core.pacProbeEnabled = (uint32_t *)((uintptr_t)pVtgHdr + pVtgHdr->offProbeEnabled);
966 pProv->Core.paR0ProbeLocs = NULL;
967 pProv->Core.paR0Probes = NULL;
968 pProv->Core.cbProbeLocsEn = sizeof(VTGPROBELOC);
969 pProv->Core.cBits = ARCH_BITS;
970 pProv->Core.fUmod = false;
971 }
972 else
973 {
974 pProv->Core.pvProbeLocsEn = pUmod->pvProbeLocs;
975 pProv->Core.pacProbeEnabled = pUmod->pacProbeEnabled;
976 pProv->Core.paR0ProbeLocs = &pUmod->aProbeLocs[0];
977 pProv->Core.paR0Probes = (PSUPDRVPROBEINFO)&pUmod->aProbeLocs[pUmod->cProbeLocs];
978 pProv->Core.cbProbeLocsEn = pUmod->cbProbeLoc;
979 pProv->Core.cBits = pUmod->cBits;
980 pProv->Core.fUmod = true;
981 }
982 pProv->pImage = pImage;
983 pProv->pSession = pSession;
984 pProv->pUmod = pUmod;
985 pProv->fZombie = false;
986 pProv->fRegistered = true;
987
988 if (!pUmod)
989 memcpy(pProv->szName, pszName, cchName + 1);
990 else
991 RTStrPrintf(pProv->szName, cchName + 1, "%s%u", pszName, (uint32_t)pSession->Process);
992 memcpy((void *)pProv->Core.pszModName, pszModName, cchModName + 1);
993
994 /*
995 * Do the actual registration and list manipulations while holding
996 * down the lock.
997 */
998 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
999 if (RT_SUCCESS(rc))
1000 {
1001 if ( pDevExt->pTracerOps
1002 && !pDevExt->fTracerUnloading)
1003 rc = pDevExt->pTracerOps->pfnProviderRegister(pDevExt->pTracerOps, &pProv->Core);
1004 else
1005 {
1006 pProv->fRegistered = false;
1007 rc = VINF_SUCCESS;
1008 }
1009 if (RT_SUCCESS(rc))
1010 {
1011 RTListAppend(&pDevExt->TracerProviderList, &pProv->ListEntry);
1012 if (pSession)
1013 {
1014 RTListAppend(&pSession->TpProviders, &pProv->SessionListEntry);
1015 pSession->cTpProviders++;
1016 }
1017 else
1018 RTListInit(&pProv->SessionListEntry);
1019 RTSemFastMutexRelease(pDevExt->mtxTracer);
1020 LOG_TRACER(("Registered tracepoint provider '%s' in '%s' -> %p\n",
1021 pProv->szName, pszModName, pProv->Core.TracerData.DTrace.idProvider));
1022 }
1023 else
1024 {
1025 RTSemFastMutexRelease(pDevExt->mtxTracer);
1026 LOG_TRACER(("Failed to register tracepoint provider '%s' in '%s' -> %Rrc\n",
1027 pProv->szName, pszModName, rc));
1028 }
1029 }
1030 }
1031 else
1032 rc = VERR_NO_MEMORY;
1033
1034 /*
1035 * In case of failure, we have to undo any providers we already
1036 * managed to register.
1037 */
1038 if (RT_FAILURE(rc))
1039 {
1040 PSUPDRVTPPROVIDER pProvNext;
1041
1042 if (pProv)
1043 supdrvTracerFreeProvider(pProv);
1044
1045 RTSemFastMutexRequest(pDevExt->mtxTracer);
1046 if (pImage)
1047 {
1048 RTListForEachReverseSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1049 {
1050 if (pProv->Core.pHdr == pVtgHdr)
1051 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1052 }
1053 }
1054 else
1055 {
1056 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
1057 {
1058 if (pProv->Core.pHdr == pVtgHdr)
1059 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1060 }
1061 }
1062 RTSemFastMutexRelease(pDevExt->mtxTracer);
1063 return rc;
1064 }
1065 }
1066
1067 return VINF_SUCCESS;
1068}
1069
1070
1071/**
1072 * Registers the VTG tracepoint providers of a driver.
1073 *
1074 * @returns VBox status code.
1075 * @param pSession The support driver session handle.
1076 * @param pVtgHdr The VTG header.
1077 * @param pszName The driver name.
1078 */
1079SUPR0DECL(int) SUPR0TracerRegisterDrv(PSUPDRVSESSION pSession, PVTGOBJHDR pVtgHdr, const char *pszName)
1080{
1081 int rc;
1082
1083 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
1084 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1085 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1086 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_INVALID_PARAMETER);
1087 LOG_TRACER(("SUPR0TracerRegisterDrv: pSession=%p pVtgHdr=%p pszName=%s\n", pSession, pVtgHdr, pszName));
1088
1089 rc = supdrvTracerRegisterVtgObj(pSession->pDevExt, pVtgHdr, NULL /*pImage*/, pSession, NULL /*pUmod*/, pszName);
1090
1091 /*
1092 * Try unregister zombies while we have a chance.
1093 */
1094 supdrvTracerProcessZombies(pSession->pDevExt);
1095
1096 return rc;
1097}
1098
1099
1100/**
1101 * Deregister the VTG tracepoint providers of a driver.
1102 *
1103 * @param pSession The support driver session handle.
1104 */
1105SUPR0DECL(void) SUPR0TracerDeregisterDrv(PSUPDRVSESSION pSession)
1106{
1107 PSUPDRVTPPROVIDER pProv, pProvNext;
1108 PSUPDRVDEVEXT pDevExt;
1109 AssertReturnVoid(SUP_IS_SESSION_VALID(pSession));
1110 AssertReturnVoid(pSession->R0Process == NIL_RTR0PROCESS);
1111 LOG_TRACER(("SUPR0TracerDeregisterDrv: pSession=%p\n", pSession));
1112
1113 pDevExt = pSession->pDevExt;
1114
1115 /*
1116 * Search for providers belonging to this driver session.
1117 */
1118 RTSemFastMutexRequest(pDevExt->mtxTracer);
1119 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
1120 {
1121 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1122 }
1123 RTSemFastMutexRelease(pDevExt->mtxTracer);
1124
1125 /*
1126 * Try unregister zombies while we have a chance.
1127 */
1128 supdrvTracerProcessZombies(pDevExt);
1129}
1130
1131
1132/**
1133 * Registers the VTG tracepoint providers of a module loaded by
1134 * the support driver.
1135 *
1136 * This should be called from the ModuleInit code.
1137 *
1138 * @returns VBox status code.
1139 * @param hMod The module handle.
1140 * @param pVtgHdr The VTG header.
1141 */
1142SUPR0DECL(int) SUPR0TracerRegisterModule(void *hMod, PVTGOBJHDR pVtgHdr)
1143{
1144 PSUPDRVLDRIMAGE pImage = (PSUPDRVLDRIMAGE)hMod;
1145 PSUPDRVDEVEXT pDevExt;
1146 int rc;
1147
1148 LOG_TRACER(("SUPR0TracerRegisterModule: %p\n", pVtgHdr));
1149
1150 /*
1151 * Validate input and context.
1152 */
1153 AssertPtrReturn(pImage, VERR_INVALID_HANDLE);
1154 AssertPtrReturn(pVtgHdr, VERR_INVALID_POINTER);
1155
1156 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
1157 pDevExt = pImage->pDevExt;
1158 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1159 AssertReturn(pDevExt->pLdrInitImage == pImage, VERR_WRONG_ORDER);
1160 AssertReturn(pDevExt->hLdrInitThread == RTThreadNativeSelf(), VERR_WRONG_ORDER);
1161 AssertReturn((uintptr_t)pVtgHdr - (uintptr_t)pImage->pvImage < pImage->cbImageBits, VERR_INVALID_PARAMETER);
1162
1163 /*
1164 * Do the job.
1165 */
1166 rc = supdrvTracerRegisterVtgObj(pDevExt, pVtgHdr, pImage, NULL /*pSession*/, NULL /*pUmod*/, pImage->szName);
1167 LOG_TRACER(("SUPR0TracerRegisterModule: rc=%d\n", rc));
1168
1169 /*
1170 * Try unregister zombies while we have a chance.
1171 */
1172 supdrvTracerProcessZombies(pDevExt);
1173
1174 return rc;
1175}
1176
1177
1178/**
1179 * Registers the tracer implementation.
1180 *
1181 * This should be called from the ModuleInit code or from a ring-0 session.
1182 *
1183 * @returns VBox status code.
1184 * @param hMod The module handle.
1185 * @param pSession Ring-0 session handle.
1186 * @param pReg Pointer to the tracer registration structure.
1187 * @param ppHlp Where to return the tracer helper method table.
1188 */
1189SUPR0DECL(int) SUPR0TracerRegisterImpl(void *hMod, PSUPDRVSESSION pSession, PCSUPDRVTRACERREG pReg, PCSUPDRVTRACERHLP *ppHlp)
1190{
1191 PSUPDRVLDRIMAGE pImage = (PSUPDRVLDRIMAGE)hMod;
1192 PSUPDRVDEVEXT pDevExt;
1193 PSUPDRVTPPROVIDER pProv;
1194 int rc;
1195 int rc2;
1196
1197 /*
1198 * Validate input and context.
1199 */
1200 AssertPtrReturn(ppHlp, VERR_INVALID_POINTER);
1201 *ppHlp = NULL;
1202 AssertPtrReturn(pReg, VERR_INVALID_HANDLE);
1203
1204 if (pImage)
1205 {
1206 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
1207 AssertReturn(pSession == NULL, VERR_INVALID_PARAMETER);
1208 pDevExt = pImage->pDevExt;
1209 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1210 AssertReturn(pDevExt->pLdrInitImage == pImage, VERR_WRONG_ORDER);
1211 AssertReturn(pDevExt->hLdrInitThread == RTThreadNativeSelf(), VERR_WRONG_ORDER);
1212 }
1213 else
1214 {
1215 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
1216 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_INVALID_PARAMETER);
1217 pDevExt = pSession->pDevExt;
1218 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1219 }
1220
1221 AssertReturn(pReg->u32Magic == SUPDRVTRACERREG_MAGIC, VERR_INVALID_MAGIC);
1222 AssertReturn(pReg->u32Version == SUPDRVTRACERREG_VERSION, VERR_VERSION_MISMATCH);
1223 AssertReturn(pReg->uEndMagic == SUPDRVTRACERREG_MAGIC, VERR_VERSION_MISMATCH);
1224 AssertPtrReturn(pReg->pfnProbeFireKernel, VERR_INVALID_POINTER);
1225 AssertPtrReturn(pReg->pfnProbeFireUser, VERR_INVALID_POINTER);
1226 AssertPtrReturn(pReg->pfnTracerOpen, VERR_INVALID_POINTER);
1227 AssertPtrReturn(pReg->pfnTracerIoCtl, VERR_INVALID_POINTER);
1228 AssertPtrReturn(pReg->pfnTracerClose, VERR_INVALID_POINTER);
1229 AssertPtrReturn(pReg->pfnProviderRegister, VERR_INVALID_POINTER);
1230 AssertPtrReturn(pReg->pfnProviderDeregister, VERR_INVALID_POINTER);
1231 AssertPtrReturn(pReg->pfnProviderDeregisterZombie, VERR_INVALID_POINTER);
1232
1233 /*
1234 * Do the job.
1235 */
1236 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1237 if (RT_SUCCESS(rc))
1238 {
1239 if (!pDevExt->pTracerOps)
1240 {
1241 LOG_TRACER(("SUPR0TracerRegisterImpl: pReg=%p\n", pReg));
1242 pDevExt->pTracerOps = pReg;
1243 pDevExt->pTracerSession = pSession;
1244 pDevExt->pTracerImage = pImage;
1245
1246 g_pfnSupdrvProbeFireKernel = (PFNRT)pDevExt->pTracerOps->pfnProbeFireKernel;
1247
1248 *ppHlp = &pDevExt->TracerHlp;
1249 rc = VINF_SUCCESS;
1250
1251 /*
1252 * Iterate the already loaded modules and register their providers.
1253 */
1254 RTListForEach(&pDevExt->TracerProviderList, pProv, SUPDRVTPPROVIDER, ListEntry)
1255 {
1256 Assert(!pProv->fRegistered);
1257 pProv->fRegistered = true;
1258 rc2 = pDevExt->pTracerOps->pfnProviderRegister(pDevExt->pTracerOps, &pProv->Core);
1259 if (RT_FAILURE(rc2))
1260 {
1261 pProv->fRegistered = false;
1262 SUPR0Printf("SUPR0TracerRegisterImpl: Failed to register provider %s::%s - rc=%d\n",
1263 pProv->Core.pszModName, pProv->szName, rc2);
1264 }
1265 }
1266 }
1267 else
1268 rc = VERR_SUPDRV_TRACER_ALREADY_REGISTERED;
1269 RTSemFastMutexRelease(pDevExt->mtxTracer);
1270 }
1271
1272 return rc;
1273
1274}
1275
1276
1277/**
1278 * Common tracer implementation deregistration code.
1279 *
1280 * The caller sets fTracerUnloading prior to calling this function.
1281 *
1282 * @param pDevExt The device extension structure.
1283 */
1284static void supdrvTracerCommonDeregisterImpl(PSUPDRVDEVEXT pDevExt)
1285{
1286 uint32_t i;
1287 PSUPDRVTPPROVIDER pProv;
1288 PSUPDRVTPPROVIDER pProvNext;
1289
1290 RTSemFastMutexRequest(pDevExt->mtxTracer);
1291
1292 /*
1293 * Reinstall the stub probe-fire function.
1294 */
1295 g_pfnSupdrvProbeFireKernel = supdrvTracerProbeFireStub;
1296
1297 /*
1298 * Disassociate the tracer implementation from all providers.
1299 * We will have to wait on busy providers.
1300 */
1301 for (i = 0; ; i++)
1302 {
1303 uint32_t cZombies = 0;
1304
1305 /* Live providers. */
1306 RTListForEachSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1307 {
1308 int rc;
1309 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Attemting to unregister '%s' / %p...\n",
1310 pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
1311
1312 if (!pProv->fRegistered)
1313 continue;
1314 if (!pProv->fZombie)
1315 {
1316 rc = pDevExt->pTracerOps->pfnProviderDeregister(pDevExt->pTracerOps, &pProv->Core);
1317 if (RT_FAILURE(rc))
1318 pProv->fZombie = true;
1319 }
1320 else
1321 rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
1322 if (RT_SUCCESS(rc))
1323 pProv->fZombie = pProv->fRegistered = false;
1324 else
1325 {
1326 cZombies++;
1327 if (!(i & 0xf))
1328 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on busy provider '%s' / %p (rc=%d)\n",
1329 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc);
1330 else
1331 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Failed to unregister provider '%s' / %p - rc=%d\n",
1332 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
1333 }
1334 }
1335
1336 /* Zombies providers. */
1337 RTListForEachSafe(&pDevExt->TracerProviderZombieList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1338 {
1339 int rc;
1340 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Attemting to unregister '%s' / %p (zombie)...\n",
1341 pProv->szName, pProv->Core.TracerData.DTrace.idProvider));
1342
1343 rc = pDevExt->pTracerOps->pfnProviderDeregisterZombie(pDevExt->pTracerOps, &pProv->Core);
1344 if (RT_SUCCESS(rc))
1345 {
1346 RTListNodeRemove(&pProv->ListEntry);
1347 supdrvTracerFreeProvider(pProv);
1348 }
1349 else
1350 {
1351 cZombies++;
1352 if (!(i & 0xf))
1353 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on busy provider '%s' / %p (rc=%d)\n",
1354 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc);
1355 else
1356 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Failed to unregister provider '%s' / %p - rc=%d\n",
1357 pProv->szName, pProv->Core.TracerData.DTrace.idProvider, rc));
1358 }
1359 }
1360
1361 /* Tracer opens. */
1362 if (pDevExt->cTracerOpens)
1363 {
1364 cZombies++;
1365 if (!(i & 0xf))
1366 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on %u opens\n", pDevExt->cTracerOpens);
1367 else
1368 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Waiting on %u opens\n", pDevExt->cTracerOpens));
1369 }
1370
1371 /* Tracer calls. */
1372 if (pDevExt->cTracerCallers)
1373 {
1374 cZombies++;
1375 if (!(i & 0xf))
1376 SUPR0Printf("supdrvTracerCommonDeregisterImpl: Waiting on %u callers\n", pDevExt->cTracerCallers);
1377 else
1378 LOG_TRACER(("supdrvTracerCommonDeregisterImpl: Waiting on %u callers\n", pDevExt->cTracerCallers));
1379 }
1380
1381 /* Done? */
1382 if (cZombies == 0)
1383 break;
1384
1385 /* Delay...*/
1386 RTSemFastMutexRelease(pDevExt->mtxTracer);
1387 RTThreadSleep(1000);
1388 RTSemFastMutexRequest(pDevExt->mtxTracer);
1389 }
1390
1391 /*
1392 * Deregister the tracer implementation.
1393 */
1394 pDevExt->pTracerImage = NULL;
1395 pDevExt->pTracerSession = NULL;
1396 pDevExt->pTracerOps = NULL;
1397 pDevExt->fTracerUnloading = false;
1398
1399 RTSemFastMutexRelease(pDevExt->mtxTracer);
1400}
1401
1402
1403/**
1404 * Deregister a tracer implementation.
1405 *
1406 * This should be called from the ModuleTerm code or from a ring-0 session.
1407 *
1408 * @returns VBox status code.
1409 * @param hMod The module handle.
1410 * @param pSession Ring-0 session handle.
1411 */
1412SUPR0DECL(int) SUPR0TracerDeregisterImpl(void *hMod, PSUPDRVSESSION pSession)
1413{
1414 PSUPDRVLDRIMAGE pImage = (PSUPDRVLDRIMAGE)hMod;
1415 PSUPDRVDEVEXT pDevExt;
1416 int rc;
1417
1418 /*
1419 * Validate input and context.
1420 */
1421 if (pImage)
1422 {
1423 AssertPtrReturn(pImage, VERR_INVALID_POINTER);
1424 AssertReturn(pSession == NULL, VERR_INVALID_PARAMETER);
1425 pDevExt = pImage->pDevExt;
1426 }
1427 else
1428 {
1429 AssertReturn(SUP_IS_SESSION_VALID(pSession), VERR_INVALID_PARAMETER);
1430 AssertReturn(pSession->R0Process == NIL_RTR0PROCESS, VERR_INVALID_PARAMETER);
1431 pDevExt = pSession->pDevExt;
1432 }
1433 AssertPtrReturn(pDevExt, VERR_INVALID_POINTER);
1434
1435 /*
1436 * Do the job.
1437 */
1438 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1439 if (RT_SUCCESS(rc))
1440 {
1441 if ( pImage
1442 ? pDevExt->pTracerImage == pImage
1443 : pDevExt->pTracerSession == pSession)
1444 {
1445 LOG_TRACER(("SUPR0TracerDeregisterImpl: Unloading ...\n"));
1446 pDevExt->fTracerUnloading = true;
1447 RTSemFastMutexRelease(pDevExt->mtxTracer);
1448 supdrvTracerCommonDeregisterImpl(pDevExt);
1449 LOG_TRACER(("SUPR0TracerDeregisterImpl: ... done.\n"));
1450 }
1451 else
1452 {
1453 rc = VERR_SUPDRV_TRACER_NOT_REGISTERED;
1454 RTSemFastMutexRelease(pDevExt->mtxTracer);
1455 }
1456 }
1457
1458 return rc;
1459}
1460
1461
1462/*
1463 * The probe function is a bit more fun since we need tail jump optimizating.
1464 *
1465 * Since we cannot ship yasm sources for linux and freebsd, owing to the cursed
1466 * rebuilding of the kernel module from scratch at install time, we have to
1467 * deploy some ugly gcc inline assembly here.
1468 */
1469#if defined(__GNUC__) && (defined(RT_OS_FREEBSD) || defined(RT_OS_LINUX))
1470__asm__("\
1471 .section .text \n\
1472 \n\
1473 .p2align 2,,3 \n\
1474 .global SUPR0TracerFireProbe \n\
1475SUPR0TracerFireProbe: \n\
1476");
1477# if defined(RT_ARCH_AMD64)
1478__asm__(" \
1479 movq g_pfnSupdrvProbeFireKernel(%rip), %rax \n\
1480 jmp *%rax \n\
1481");
1482# elif defined(RT_ARCH_X86)
1483__asm__("\
1484 movl g_pfnSupdrvProbeFireKernel, %eax \n\
1485 jmp *%eax \n\
1486");
1487# else
1488# error "Which arch is this?"
1489# endif
1490__asm__("\
1491 \n\
1492 .type supdrvTracerProbeFireStub,@function \n\
1493 .global supdrvTracerProbeFireStub \n\
1494supdrvTracerProbeFireStub: \n\
1495 ret \n\
1496 \n\
1497 .previous \n\
1498");
1499# if 0 /* Slickedit on windows highlighting fix */
1500 )
1501# endif
1502#endif
1503
1504
1505/**
1506 * Module unloading hook, called after execution in the module have ceased.
1507 *
1508 * @param pDevExt The device extension structure.
1509 * @param pImage The image being unloaded.
1510 */
1511void VBOXCALL supdrvTracerModuleUnloading(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
1512{
1513 PSUPDRVTPPROVIDER pProv, pProvNext;
1514 AssertPtrReturnVoid(pImage); /* paranoia */
1515
1516 RTSemFastMutexRequest(pDevExt->mtxTracer);
1517
1518 /*
1519 * If it is the tracer image, we have to unload all the providers.
1520 */
1521 if (pDevExt->pTracerImage == pImage)
1522 {
1523 LOG_TRACER(("supdrvTracerModuleUnloading: Unloading tracer ...\n"));
1524 pDevExt->fTracerUnloading = true;
1525 RTSemFastMutexRelease(pDevExt->mtxTracer);
1526 supdrvTracerCommonDeregisterImpl(pDevExt);
1527 LOG_TRACER(("supdrvTracerModuleUnloading: ... done.\n"));
1528 }
1529 else
1530 {
1531 /*
1532 * Unregister all providers belonging to this image.
1533 */
1534 RTListForEachSafe(&pDevExt->TracerProviderList, pProv, pProvNext, SUPDRVTPPROVIDER, ListEntry)
1535 {
1536 if (pProv->pImage == pImage)
1537 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1538 }
1539
1540 RTSemFastMutexRelease(pDevExt->mtxTracer);
1541
1542 /*
1543 * Try unregister zombies while we have a chance.
1544 */
1545 supdrvTracerProcessZombies(pDevExt);
1546 }
1547}
1548
1549
1550/**
1551 * Called when a session is being cleaned up.
1552 *
1553 * @param pDevExt The device extension structure.
1554 * @param pSession The session that is being torn down.
1555 */
1556void VBOXCALL supdrvTracerCleanupSession(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
1557{
1558 /*
1559 * Deregister all providers.
1560 */
1561 SUPDRVTPPROVIDER *pProvNext;
1562 SUPDRVTPPROVIDER *pProv;
1563 RTSemFastMutexRequest(pDevExt->mtxTracer);
1564 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
1565 {
1566 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
1567 }
1568 RTSemFastMutexRelease(pDevExt->mtxTracer);
1569
1570 /*
1571 * Clean up instance data the trace may have associated with the session.
1572 */
1573 if (pSession->uTracerData)
1574 supdrvIOCtl_TracerClose(pDevExt, pSession);
1575
1576 /*
1577 * Deregister any tracer implementation.
1578 */
1579 if (pSession->R0Process == NIL_RTR0PROCESS)
1580 (void)SUPR0TracerDeregisterImpl(NULL, pSession);
1581
1582 if (pSession->R0Process != NIL_RTR0PROCESS)
1583 {
1584 /*
1585 * Free any lingering user modules. We don't bother holding the lock
1586 * here as there shouldn't be anyone messing with the session at this
1587 * point.
1588 */
1589 PSUPDRVTRACERUMOD pUmodNext;
1590 PSUPDRVTRACERUMOD pUmod;
1591 RTListForEachSafe(&pSession->TpUmods, pUmod, pUmodNext, SUPDRVTRACERUMOD, ListEntry)
1592 {
1593 RTR0MemObjFree(pUmod->hMemObjMap, false /*fFreeMappings*/);
1594 RTR0MemObjFree(pUmod->hMemObjLock, false /*fFreeMappings*/);
1595 supdrvVtgReleaseObjectCopy(pDevExt, pUmod->pVtgCopy);
1596 RTMemFree(pUmod);
1597 }
1598 }
1599}
1600
1601
1602static void supdrvVtgReleaseObjectCopy(PSUPDRVDEVEXT pDevExt, PSUPDRVVTGCOPY pThis)
1603{
1604 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
1605 if (!cRefs)
1606 {
1607 RTSemFastMutexRequest(pDevExt->mtxTracer);
1608 pThis->u32Magic = ~SUDPRVVTGCOPY_MAGIC;
1609 RTListNodeRemove(&pThis->ListEntry);
1610 RTSemFastMutexRelease(pDevExt->mtxTracer);
1611
1612 RTMemFree(pThis);
1613 }
1614}
1615
1616
1617/**
1618 * Finds a matching VTG object copy, caller owns the lock already.
1619 *
1620 * @returns Copy with reference. NULL if not found.
1621 * @param pHashList The hash list to search.
1622 * @param pHdr The VTG header (valid).
1623 * @param cbStrTab The string table size.
1624 * @param fFlags The user module flags.
1625 */
1626static PSUPDRVVTGCOPY supdrvVtgFindObjectCopyLocked(PRTLISTANCHOR pHashList, PCVTGOBJHDR pHdr, uint32_t cbStrTab, uint32_t fFlags)
1627{
1628 PSUPDRVVTGCOPY pCur;
1629
1630 fFlags &= SUP_TRACER_UMOD_FLAGS_TYPE_MASK;
1631 RTListForEach(pHashList, pCur, SUPDRVVTGCOPY, ListEntry)
1632 {
1633#define HDR_EQUALS(member) pCur->Hdr.member == pHdr->member
1634 if ( HDR_EQUALS(Uuid.au32[0])
1635 && HDR_EQUALS(Uuid.au32[1])
1636 && HDR_EQUALS(Uuid.au32[2])
1637 && HDR_EQUALS(Uuid.au32[3])
1638 && HDR_EQUALS(cbObj)
1639 && HDR_EQUALS(cBits)
1640 && pCur->cbStrTab == cbStrTab
1641 && pCur->fFlags == fFlags
1642 )
1643 {
1644 if (RT_LIKELY( HDR_EQUALS(offStrTab)
1645 && HDR_EQUALS(cbStrTab)
1646 && HDR_EQUALS(offArgLists)
1647 && HDR_EQUALS(cbArgLists)
1648 && HDR_EQUALS(offProbes)
1649 && HDR_EQUALS(cbProbes)
1650 && HDR_EQUALS(offProviders)
1651 && HDR_EQUALS(cbProviders)
1652 && HDR_EQUALS(offProbeEnabled)
1653 && HDR_EQUALS(cbProbeEnabled)
1654 && HDR_EQUALS(offProbeLocs)
1655 && HDR_EQUALS(cbProbeLocs)
1656 )
1657 )
1658 {
1659 Assert(pCur->cRefs > 0);
1660 Assert(pCur->cRefs < _1M);
1661 pCur->cRefs++;
1662 return pCur;
1663 }
1664 }
1665#undef HDR_EQUALS
1666 }
1667
1668 return NULL;
1669}
1670
1671
1672/**
1673 * Finds a matching VTG object copy.
1674 *
1675 * @returns Copy with reference. NULL if not found.
1676 * @param pDevExt The device extension.
1677 * @param pHdr The VTG header (valid).
1678 * @param cbStrTab The string table size.
1679 * @param fFlags The user module flags.
1680 */
1681static PSUPDRVVTGCOPY supdrvVtgFindObjectCopy(PSUPDRVDEVEXT pDevExt, PCVTGOBJHDR pHdr, uint32_t cbStrTab, uint32_t fFlags)
1682{
1683 PRTLISTANCHOR pHashList = &pDevExt->aTrackerUmodHash[pHdr->Uuid.au8[3] % RT_ELEMENTS(pDevExt->aTrackerUmodHash)];
1684 PSUPDRVVTGCOPY pRet;
1685
1686 int rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1687 AssertRCReturn(rc, NULL);
1688
1689 pRet = supdrvVtgFindObjectCopyLocked(pHashList, pHdr, cbStrTab, fFlags);
1690
1691 RTSemFastMutexRelease(pDevExt->mtxTracer);
1692 return pRet;
1693}
1694
1695
1696/**
1697 * Makes a shared copy of the VTG object.
1698 *
1699 * @returns VBox status code.
1700 * @param pDevExt The device extension.
1701 * @param pVtgHdr The VTG header (valid).
1702 * @param R3PtrVtgHdr The ring-3 VTG header address.
1703 * @param uVtgHdrAddr The address of the VTG header in the context
1704 * where it is actually used.
1705 * @param R3PtrStrTab The ring-3 address of the probe location string
1706 * table. The probe location array have offsets
1707 * into this instead of funciton name pointers.
1708 * @param cbStrTab The size of the probe location string table.
1709 * @param fFlags The user module flags.
1710 * @param pUmod The structure we've allocated to track the
1711 * module. This have a valid kernel mapping of the
1712 * probe location array. Upon successful return,
1713 * the pVtgCopy member will hold the address of our
1714 * copy (with a referenced of course).
1715 */
1716static int supdrvVtgCreateObjectCopy(PSUPDRVDEVEXT pDevExt, PCVTGOBJHDR pVtgHdr, RTR3PTR R3PtrVtgHdr, RTUINTPTR uVtgHdrAddr,
1717 RTR3PTR R3PtrStrTab, uint32_t cbStrTab, uint32_t fFlags, PSUPDRVTRACERUMOD pUmod)
1718{
1719 /*
1720 * Calculate the space required, allocate and copy in the data.
1721 */
1722 int rc;
1723 size_t const cProbeLocs = pVtgHdr->cbProbeLocs / (pVtgHdr->cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
1724 size_t const cbProbeLocs = cProbeLocs * sizeof(VTGPROBELOC);
1725 size_t const offProbeLocs = RT_ALIGN(pVtgHdr->cbObj, 8);
1726 size_t const cb = offProbeLocs + cbProbeLocs + cbStrTab + 1;
1727 PSUPDRVVTGCOPY pThis = (PSUPDRVVTGCOPY)RTMemAlloc(RT_OFFSETOF(SUPDRVVTGCOPY, Hdr) + cb);
1728 if (!pThis)
1729 return VERR_NO_MEMORY;
1730
1731 pThis->u32Magic = SUDPRVVTGCOPY_MAGIC;
1732 pThis->cRefs = 1;
1733 pThis->cbStrTab = cbStrTab;
1734 pThis->fFlags = fFlags & SUP_TRACER_UMOD_FLAGS_TYPE_MASK;
1735 RTListInit(&pThis->ListEntry);
1736
1737 rc = RTR0MemUserCopyFrom(&pThis->Hdr, R3PtrVtgHdr, pVtgHdr->cbObj);
1738 if (RT_SUCCESS(rc))
1739 {
1740 char *pchStrTab = (char *)&pThis->Hdr + offProbeLocs + cbProbeLocs;
1741 rc = RTR0MemUserCopyFrom(pchStrTab, R3PtrStrTab, cbStrTab);
1742 if (RT_SUCCESS(rc))
1743 {
1744 PVTGPROBELOC paDst = (PVTGPROBELOC)((char *)&pThis->Hdr + offProbeLocs);
1745 uint32_t i;
1746
1747 /*
1748 * Some paranoia: Overwrite the header with the copy we've already
1749 * validated and zero terminate the string table.
1750 */
1751 pThis->Hdr = *pVtgHdr;
1752 pchStrTab[cbStrTab] = '\0';
1753
1754 /*
1755 * Set the probe location array related header members since we're
1756 * making our own copy in a different location.
1757 */
1758 pThis->Hdr.uProbeLocs.u64 = (uintptr_t)paDst;
1759 pThis->Hdr.uProbeLocsEnd.u64 = (uintptr_t)paDst + cbProbeLocs;
1760 pThis->Hdr.offProbeLocs = offProbeLocs;
1761 pThis->Hdr.cbProbeLocs = cbProbeLocs;
1762 pThis->Hdr.cBits = ARCH_BITS;
1763
1764 /*
1765 * Copy, convert and fix up the probe location table.
1766 */
1767 if (pVtgHdr->cBits == 32)
1768 {
1769 uintptr_t const offDelta = (uintptr_t)&pThis->Hdr - uVtgHdrAddr;
1770 PCVTGPROBELOC32 paSrc = (PCVTGPROBELOC32)pUmod->pvProbeLocs;
1771
1772 for (i = 0; i < cProbeLocs; i++)
1773 {
1774 paDst[i].uLine = paSrc[i].uLine;
1775 paDst[i].fEnabled = paSrc[i].fEnabled;
1776 paDst[i].idProbe = paSrc[i].idProbe;
1777 if (paSrc[i].pszFunction > cbStrTab)
1778 {
1779 rc = VERR_SUPDRV_TRACER_UMOD_STRTAB_OFF_BAD;
1780 break;
1781 }
1782 paDst[i].pszFunction = pchStrTab + paSrc[i].pszFunction;
1783 paDst[i].pProbe = (PVTGDESCPROBE)(uintptr_t)(paSrc[i].pProbe + offDelta);
1784 }
1785 }
1786 else
1787 {
1788 uint64_t const offDelta = (uintptr_t)&pThis->Hdr - uVtgHdrAddr;
1789 PCVTGPROBELOC64 paSrc = (PCVTGPROBELOC64)pUmod->pvProbeLocs;
1790
1791 for (i = 0; i < cProbeLocs; i++)
1792 {
1793 paDst[i].uLine = paSrc[i].uLine;
1794 paDst[i].fEnabled = paSrc[i].fEnabled;
1795 paDst[i].idProbe = paSrc[i].idProbe;
1796 if (paSrc[i].pszFunction > cbStrTab)
1797 {
1798 rc = VERR_SUPDRV_TRACER_UMOD_STRTAB_OFF_BAD;
1799 break;
1800 }
1801 paDst[i].pszFunction = pchStrTab + (uintptr_t)paSrc[i].pszFunction;
1802 paDst[i].pProbe = (PVTGDESCPROBE)(uintptr_t)(paSrc[i].pProbe + offDelta);
1803 }
1804 }
1805
1806 /*
1807 * Validate it
1808 *
1809 * Note! fUmod is false as this is a kernel copy with all native
1810 * structures.
1811 */
1812 if (RT_SUCCESS(rc))
1813 rc = supdrvVtgValidate(&pThis->Hdr, (uintptr_t)&pThis->Hdr, (uint8_t *)&pThis->Hdr, cb, false /*fUmod*/);
1814 if (RT_SUCCESS(rc))
1815 {
1816 /*
1817 * Add it to the hash list, making sure nobody raced us.
1818 */
1819 PRTLISTANCHOR pHashList = &pDevExt->aTrackerUmodHash[ pVtgHdr->Uuid.au8[3]
1820 % RT_ELEMENTS(pDevExt->aTrackerUmodHash)];
1821
1822 rc = RTSemFastMutexRequest(pDevExt->mtxTracer);
1823 if (RT_SUCCESS(rc))
1824 {
1825 pUmod->pVtgCopy = supdrvVtgFindObjectCopyLocked(pHashList, pVtgHdr, cbStrTab, fFlags);
1826 if (!pUmod->pVtgCopy)
1827 {
1828 pUmod->pVtgCopy = pThis;
1829 RTListAppend(pHashList, &pThis->ListEntry);
1830 RTSemFastMutexRelease(pDevExt->mtxTracer);
1831 return rc;
1832 }
1833
1834 /*
1835 * Someone raced us, free our copy and return the existing
1836 * one instead.
1837 */
1838 RTSemFastMutexRelease(pDevExt->mtxTracer);
1839 }
1840 }
1841 }
1842 }
1843 RTMemFree(pThis);
1844 return rc;
1845}
1846
1847
1848/**
1849 * Undoes what supdrvTracerUmodSetProbeIds did.
1850 *
1851 * @param pDevExt The device extension.
1852 * @param pSession The current session.
1853 * @param pUmod The user tracepoint module.
1854 */
1855static void supdrvTracerUmodClearProbeIds(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUMOD pUmod)
1856{
1857 uint32_t i;
1858
1859 AssertReturnVoid(pUmod->iLookupTable < RT_ELEMENTS(pSession->apTpLookupTable));
1860 AssertReturnVoid(pSession->apTpLookupTable[pUmod->iLookupTable] == pUmod);
1861
1862 /*
1863 * Clear the probe IDs and disable the probes.
1864 */
1865 i = pUmod->cProbeLocs;
1866 if (pUmod->cBits == 32)
1867 {
1868 PVTGPROBELOC32 paProbeLocs = (PVTGPROBELOC32)pUmod->pvProbeLocs;
1869 while (i-- > 0)
1870 paProbeLocs[i].idProbe = 0;
1871 }
1872 else
1873 {
1874 PVTGPROBELOC64 paProbeLocs = (PVTGPROBELOC64)pUmod->pvProbeLocs;
1875 while (i-- > 0)
1876 paProbeLocs[i].idProbe = 0;
1877 }
1878
1879 /*
1880 * Free the lookup table entry. We'll have to wait for the table to go
1881 * idle to make sure there are no current users of pUmod.
1882 */
1883 RTSemFastMutexRequest(pDevExt->mtxTracer);
1884 if (pSession->apTpLookupTable[pUmod->iLookupTable] == pUmod)
1885 {
1886 if (pSession->cTpProbesFiring > 0)
1887 {
1888 i = 0;
1889 while (pSession->cTpProbesFiring > 0)
1890 {
1891 RTSemFastMutexRelease(pDevExt->mtxTracer);
1892 i++;
1893 if (!(i & 0xff))
1894 SUPR0Printf("supdrvTracerUmodClearProbeIds: waiting for lookup table to go idle (i=%u)\n", i);
1895 RTThreadSleep(10);
1896 RTSemFastMutexRequest(pDevExt->mtxTracer);
1897 }
1898 }
1899 ASMAtomicWriteNullPtr(&pSession->apTpLookupTable[pUmod->iLookupTable]);
1900 }
1901 RTSemFastMutexRelease(pDevExt->mtxTracer);
1902}
1903
1904
1905/**
1906 * Allocates a lookup table entry for the Umod and sets the
1907 * VTGPROBELOC::idProbe fields in user mode.
1908 *
1909 * @returns VINF_SUCCESS or VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS.
1910 * @param pDevExt The device extension.
1911 * @param pSession The current session.
1912 * @param pUmod The user tracepoint module.
1913 */
1914static int supdrvTracerUmodSetProbeIds(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUMOD pUmod)
1915{
1916 uint32_t iBase;
1917 uint32_t i;
1918
1919 /*
1920 * Allocate a lookup table entry.
1921 */
1922 RTSemFastMutexRequest(pDevExt->mtxTracer);
1923 for (i = 0; i < RT_ELEMENTS(pSession->apTpLookupTable); i++)
1924 {
1925 if (!pSession->apTpLookupTable[i])
1926 {
1927 pSession->apTpLookupTable[i] = pUmod;
1928 pUmod->iLookupTable = i;
1929 break;
1930 }
1931 }
1932 RTSemFastMutexRelease(pDevExt->mtxTracer);
1933 if (i >= RT_ELEMENTS(pSession->apTpLookupTable))
1934 return VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS;
1935
1936 /*
1937 * Set probe IDs of the usermode probe location to indicate our lookup
1938 * table entry as well as the probe location array entry.
1939 */
1940 iBase = (uint32_t)pUmod->iLookupTable << 24;
1941 i = pUmod->cProbeLocs;
1942 if (pUmod->cBits == 32)
1943 {
1944 PVTGPROBELOC32 paProbeLocs = (PVTGPROBELOC32)pUmod->pvProbeLocs;
1945 while (i-- > 0)
1946 paProbeLocs[i].idProbe = iBase | i;
1947 }
1948 else
1949 {
1950 PVTGPROBELOC64 paProbeLocs = (PVTGPROBELOC64)pUmod->pvProbeLocs;
1951 while (i-- > 0)
1952 paProbeLocs[i].idProbe = iBase | i;
1953 }
1954
1955 return VINF_SUCCESS;
1956}
1957
1958
1959int VBOXCALL supdrvIOCtl_TracerUmodRegister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession,
1960 RTR3PTR R3PtrVtgHdr, RTUINTPTR uVtgHdrAddr,
1961 RTR3PTR R3PtrStrTab, uint32_t cbStrTab,
1962 const char *pszModName, uint32_t fFlags)
1963{
1964 VTGOBJHDR Hdr;
1965 PSUPDRVTRACERUMOD pUmod;
1966 RTR3PTR R3PtrLock;
1967 size_t cbLock;
1968 uint32_t cProbeLocs;
1969 int rc;
1970
1971 /*
1972 * Validate input.
1973 */
1974 if (pSession->R0Process == NIL_RTR0PROCESS)
1975 return VERR_INVALID_CONTEXT;
1976 if ( fFlags != SUP_TRACER_UMOD_FLAGS_EXE
1977 && fFlags != SUP_TRACER_UMOD_FLAGS_SHARED)
1978 return VERR_INVALID_PARAMETER;
1979
1980 if (pSession->cTpProviders >= RT_ELEMENTS(pSession->apTpLookupTable))
1981 return VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS;
1982
1983 if ( cbStrTab < 2
1984 || cbStrTab > _1M)
1985 return VERR_SUPDRV_TRACER_UMOD_STRTAB_TOO_BIG;
1986
1987 /*
1988 * Read the VTG header into a temporary buffer and perform some simple
1989 * validations to make sure we aren't wasting our time here.
1990 */
1991 rc = RTR0MemUserCopyFrom(&Hdr, R3PtrVtgHdr, sizeof(Hdr));
1992 if (RT_FAILURE(rc))
1993 return rc;
1994 rc = supdrvVtgValidateHdr(&Hdr, uVtgHdrAddr, NULL, 0, true /*fUmod*/);
1995 if (RT_FAILURE(rc))
1996 return rc;
1997 if (Hdr.cbProviders / sizeof(VTGDESCPROVIDER) > 2)
1998 return VERR_SUPDRV_TRACER_TOO_MANY_PROVIDERS;
1999
2000 /*
2001 * Check how much needs to be locked down and how many probe locations
2002 * there are.
2003 */
2004 if ( Hdr.offProbeLocs <= 0
2005 || Hdr.offProbeEnabled > (uint32_t)Hdr.offProbeLocs
2006 || (uint32_t)Hdr.offProbeLocs - Hdr.offProbeEnabled - Hdr.cbProbeEnabled > 128)
2007 return VERR_SUPDRV_TRACER_UMOD_NOT_ADJACENT;
2008 R3PtrLock = R3PtrVtgHdr + Hdr.offProbeEnabled;
2009 cbLock = Hdr.offProbeLocs + Hdr.cbProbeLocs - Hdr.offProbeEnabled + (R3PtrLock & PAGE_OFFSET_MASK);
2010 R3PtrLock &= ~(RTR3PTR)PAGE_OFFSET_MASK;
2011 if (cbLock > _64K)
2012 return VERR_SUPDRV_TRACER_UMOD_TOO_MANY_PROBES;
2013
2014 cProbeLocs = Hdr.cbProbeLocs / (Hdr.cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64));
2015
2016 /*
2017 * Allocate the tracker data we keep in the session.
2018 */
2019 pUmod = (PSUPDRVTRACERUMOD)RTMemAllocZ( RT_OFFSETOF(SUPDRVTRACERUMOD, aProbeLocs[cProbeLocs])
2020 + (Hdr.cbProbeEnabled / sizeof(uint32_t) * sizeof(SUPDRVPROBEINFO)) );
2021 if (!pUmod)
2022 return VERR_NO_MEMORY;
2023 pUmod->u32Magic = SUPDRVTRACERUMOD_MAGIC;
2024 RTListInit(&pUmod->ListEntry);
2025 pUmod->R3PtrVtgHdr = R3PtrVtgHdr;
2026 pUmod->pVtgCopy = NULL;
2027 pUmod->hMemObjLock = NIL_RTR0MEMOBJ;
2028 pUmod->hMemObjMap = NIL_RTR0MEMOBJ;
2029 pUmod->R3PtrProbeLocs = (RTR3INTPTR)R3PtrVtgHdr + Hdr.offProbeLocs;
2030 pUmod->iLookupTable = UINT8_MAX;
2031 pUmod->cBits = Hdr.cBits;
2032 pUmod->cbProbeLoc = Hdr.cBits == 32 ? sizeof(VTGPROBELOC32) : sizeof(VTGPROBELOC64);
2033 pUmod->cProbeLocs = cProbeLocs;
2034
2035 /*
2036 * Lock down and map the user-mode structures.
2037 */
2038 rc = RTR0MemObjLockUser(&pUmod->hMemObjLock, R3PtrLock, cbLock, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
2039 if (RT_SUCCESS(rc))
2040 {
2041 rc = RTR0MemObjMapKernel(&pUmod->hMemObjMap, pUmod->hMemObjLock, (void *)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
2042 if (RT_SUCCESS(rc))
2043 {
2044 pUmod->pacProbeEnabled = (uint32_t *)( (uintptr_t)RTR0MemObjAddress(pUmod->hMemObjMap)
2045 + ((uintptr_t)(R3PtrVtgHdr + Hdr.offProbeEnabled) & PAGE_OFFSET_MASK));
2046 pUmod->pvProbeLocs = (uint8_t *)pUmod->pacProbeEnabled + Hdr.offProbeLocs - Hdr.offProbeEnabled;
2047
2048 /*
2049 * Does some other process use the same module already? If so,
2050 * share the VTG data with it. Otherwise, make a ring-0 copy it.
2051 */
2052 pUmod->pVtgCopy = supdrvVtgFindObjectCopy(pDevExt, &Hdr, cbStrTab, fFlags);
2053 if (!pUmod->pVtgCopy)
2054 rc = supdrvVtgCreateObjectCopy(pDevExt, &Hdr, R3PtrVtgHdr, uVtgHdrAddr, R3PtrStrTab, cbStrTab, fFlags, pUmod);
2055 if (RT_SUCCESS(rc))
2056 {
2057 AssertPtr(pUmod->pVtgCopy);
2058
2059 /*
2060 * Grabe a place in apTpLookupTable and set the probe IDs
2061 * accordingly.
2062 */
2063 rc = supdrvTracerUmodSetProbeIds(pDevExt, pSession, pUmod);
2064 if (RT_SUCCESS(rc))
2065 {
2066 /*
2067 * Register the providers.
2068 */
2069 rc = supdrvTracerRegisterVtgObj(pDevExt, &pUmod->pVtgCopy->Hdr,
2070 NULL /*pImage*/, pSession, pUmod, pszModName);
2071 if (RT_SUCCESS(rc))
2072 {
2073 RTSemFastMutexRequest(pDevExt->mtxTracer);
2074 RTListAppend(&pSession->TpUmods, &pUmod->ListEntry);
2075 RTSemFastMutexRelease(pDevExt->mtxTracer);
2076
2077 return VINF_SUCCESS;
2078 }
2079
2080 /* bail out. */
2081 supdrvTracerUmodClearProbeIds(pDevExt, pSession, pUmod);
2082 }
2083 supdrvVtgReleaseObjectCopy(pDevExt, pUmod->pVtgCopy);
2084 }
2085 RTR0MemObjFree(pUmod->hMemObjMap, false /*fFreeMappings*/);
2086 }
2087 RTR0MemObjFree(pUmod->hMemObjLock, false /*fFreeMappings*/);
2088 }
2089 pUmod->u32Magic = ~SUPDRVTRACERUMOD_MAGIC;
2090 RTMemFree(pUmod);
2091 return rc;
2092}
2093
2094
2095int VBOXCALL supdrvIOCtl_TracerUmodDeregister(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, RTR3PTR R3PtrVtgHdr)
2096{
2097 PSUPDRVTRACERUMOD pUmod = NULL;
2098 uint32_t i;
2099 int rc;
2100
2101 /*
2102 * Validate the request.
2103 */
2104 RTSemFastMutexRequest(pDevExt->mtxTracer);
2105 for (i = 0; i < RT_ELEMENTS(pSession->apTpLookupTable); i++)
2106 {
2107 pUmod = pSession->apTpLookupTable[i];
2108 if ( pUmod
2109 && pUmod->u32Magic == SUPDRVTRACERUMOD_MAGIC
2110 && pUmod->R3PtrVtgHdr == R3PtrVtgHdr)
2111 break;
2112 }
2113 RTSemFastMutexRelease(pDevExt->mtxTracer);
2114 if (pUmod)
2115 {
2116 SUPDRVTPPROVIDER *pProvNext;
2117 SUPDRVTPPROVIDER *pProv;
2118
2119 /*
2120 * Remove ourselves from the lookup table and clean up the ring-3 bits
2121 * we've dirtied. We do this first to make sure no probes are firing
2122 * when we're destroying the providers in the next step.
2123 */
2124 supdrvTracerUmodClearProbeIds(pDevExt, pSession, pUmod);
2125
2126 /*
2127 * Deregister providers related to the VTG object.
2128 */
2129 RTSemFastMutexRequest(pDevExt->mtxTracer);
2130 RTListForEachSafe(&pSession->TpProviders, pProv, pProvNext, SUPDRVTPPROVIDER, SessionListEntry)
2131 {
2132 if (pProv->pUmod == pUmod)
2133 supdrvTracerDeregisterVtgObj(pDevExt, pProv);
2134 }
2135 RTSemFastMutexRelease(pDevExt->mtxTracer);
2136
2137 /*
2138 * Destroy the Umod object.
2139 */
2140 pUmod->u32Magic = ~SUPDRVTRACERUMOD_MAGIC;
2141 supdrvVtgReleaseObjectCopy(pDevExt, pUmod->pVtgCopy);
2142 RTR0MemObjFree(pUmod->hMemObjMap, false /*fFreeMappings*/);
2143 RTR0MemObjFree(pUmod->hMemObjLock, false /*fFreeMappings*/);
2144 RTMemFree(pUmod);
2145 rc = VINF_SUCCESS;
2146 }
2147 else
2148 rc = VERR_NOT_FOUND;
2149 return rc;
2150}
2151
2152
2153/**
2154 * Implementation of supdrvIOCtl_TracerUmodProbeFire and
2155 * SUPR0TracerUmodProbeFire.
2156 *
2157 * @param pDevExt The device extension.
2158 * @param pSession The calling session.
2159 * @param pCtx The context record.
2160 */
2161static void supdrvTracerUmodProbeFire(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx)
2162{
2163 /*
2164 * We cannot trust user mode to hand us the right bits nor not calling us
2165 * when disabled. So, we have to check for our selves.
2166 */
2167 PSUPDRVTRACERUMOD pUmod;
2168 uint32_t const iLookupTable = pCtx->idProbe >> 24;
2169 uint32_t const iProbeLoc = pCtx->idProbe & UINT32_C(0x00ffffff);
2170
2171 if (RT_UNLIKELY( !pDevExt->pTracerOps
2172 || pDevExt->fTracerUnloading))
2173 return;
2174 if (RT_UNLIKELY(iLookupTable >= RT_ELEMENTS(pSession->apTpLookupTable)))
2175 return;
2176 if (RT_UNLIKELY( pCtx->cBits != 32
2177 && pCtx->cBits != 64))
2178 return;
2179
2180 ASMAtomicIncU32(&pSession->cTpProviders);
2181
2182 pUmod = pSession->apTpLookupTable[iLookupTable];
2183 if (RT_LIKELY(pUmod))
2184 {
2185 if (RT_LIKELY( pUmod->u32Magic == SUPDRVTRACERUMOD_MAGIC
2186 && iProbeLoc < pUmod->cProbeLocs
2187 && pCtx->cBits == pUmod->cBits))
2188 {
2189#if 0 /* This won't work for RC modules. */
2190 RTR3PTR R3PtrProbeLoc = pUmod->R3PtrProbeLocs + iProbeLoc * pUmod->cbProbeLoc;
2191 if (RT_LIKELY( (pCtx->cBits == 32 ? (RTR3PTR)pCtx->u.X86.uVtgProbeLoc : pCtx->u.Amd64.uVtgProbeLoc)
2192 == R3PtrProbeLoc))
2193#endif
2194 {
2195 if (RT_LIKELY(pUmod->aProbeLocs[iProbeLoc].fEnabled))
2196 {
2197 PSUPDRVVTGCOPY pVtgCopy;
2198 ASMAtomicIncU32(&pDevExt->cTracerCallers);
2199 pVtgCopy = pUmod->pVtgCopy;
2200 if (RT_LIKELY( pDevExt->pTracerOps
2201 && !pDevExt->fTracerUnloading
2202 && pVtgCopy))
2203 {
2204 PCVTGPROBELOC pProbeLocRO;
2205 pProbeLocRO = (PCVTGPROBELOC)((uintptr_t)&pVtgCopy->Hdr + pVtgCopy->Hdr.offProbeLocs) + iProbeLoc;
2206
2207 pCtx->idProbe = pUmod->aProbeLocs[iProbeLoc].idProbe;
2208 pDevExt->pTracerOps->pfnProbeFireUser(pDevExt->pTracerOps, pSession, pCtx, &pVtgCopy->Hdr, pProbeLocRO);
2209 }
2210 ASMAtomicDecU32(&pDevExt->cTracerCallers);
2211 }
2212 }
2213 }
2214 }
2215
2216 ASMAtomicDecU32(&pSession->cTpProviders);
2217}
2218
2219
2220SUPR0DECL(void) SUPR0TracerUmodProbeFire(PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx)
2221{
2222 AssertReturnVoid(SUP_IS_SESSION_VALID(pSession));
2223 AssertPtrReturnVoid(pCtx);
2224
2225 supdrvTracerUmodProbeFire(pSession->pDevExt, pSession, pCtx);
2226}
2227
2228
2229void VBOXCALL supdrvIOCtl_TracerUmodProbeFire(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PSUPDRVTRACERUSRCTX pCtx)
2230{
2231 supdrvTracerUmodProbeFire(pDevExt, pSession, pCtx);
2232}
2233
2234
2235/**
2236 * Open the tracer.
2237 *
2238 * @returns VBox status code
2239 * @param pDevExt The device extension structure.
2240 * @param pSession The current session.
2241 * @param uCookie The tracer cookie.
2242 * @param uArg The tracer open argument.
2243 */
2244int VBOXCALL supdrvIOCtl_TracerOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uint32_t uCookie, uintptr_t uArg)
2245{
2246 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
2247 int rc;
2248
2249 RTSemFastMutexRequest(pDevExt->mtxTracer);
2250
2251 if (!pSession->uTracerData)
2252 {
2253 if (pDevExt->pTracerOps)
2254 {
2255 if (pDevExt->pTracerSession != pSession)
2256 {
2257 if (!pDevExt->fTracerUnloading)
2258 {
2259 if (pSession->hTracerCaller == NIL_RTNATIVETHREAD)
2260 {
2261 pDevExt->cTracerOpens++;
2262 pSession->uTracerData = ~(uintptr_t)0;
2263 pSession->hTracerCaller = hNativeSelf;
2264 RTSemFastMutexRelease(pDevExt->mtxTracer);
2265
2266 rc = pDevExt->pTracerOps->pfnTracerOpen(pDevExt->pTracerOps, pSession, uCookie, uArg, &pSession->uTracerData);
2267
2268 RTSemFastMutexRequest(pDevExt->mtxTracer);
2269 if (RT_FAILURE(rc))
2270 {
2271 pDevExt->cTracerOpens--;
2272 pSession->uTracerData = 0;
2273 }
2274 pSession->hTracerCaller = NIL_RTNATIVETHREAD;
2275 }
2276 else
2277 rc = VERR_SUPDRV_TRACER_SESSION_BUSY;
2278 }
2279 else
2280 rc = VERR_SUPDRV_TRACER_UNLOADING;
2281 }
2282 else
2283 rc = VERR_SUPDRV_TRACER_CANNOT_OPEN_SELF;
2284 }
2285 else
2286 rc = VERR_SUPDRV_TRACER_NOT_PRESENT;
2287 }
2288 else
2289 rc = VERR_SUPDRV_TRACER_ALREADY_OPENED;
2290
2291 RTSemFastMutexRelease(pDevExt->mtxTracer);
2292 return rc;
2293}
2294
2295
2296/**
2297 * Closes the tracer.
2298 *
2299 * @returns VBox status code.
2300 * @param pDevExt The device extension structure.
2301 * @param pSession The current session.
2302 */
2303int VBOXCALL supdrvIOCtl_TracerClose(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession)
2304{
2305 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
2306 int rc;
2307
2308 RTSemFastMutexRequest(pDevExt->mtxTracer);
2309
2310 if (pSession->uTracerData)
2311 {
2312 Assert(pDevExt->cTracerOpens > 0);
2313
2314 if (pDevExt->pTracerOps)
2315 {
2316 if (pSession->hTracerCaller == NIL_RTNATIVETHREAD)
2317 {
2318 uintptr_t uTracerData = pSession->uTracerData;
2319 pSession->uTracerData = 0;
2320 pSession->hTracerCaller = hNativeSelf;
2321 RTSemFastMutexRelease(pDevExt->mtxTracer);
2322
2323 pDevExt->pTracerOps->pfnTracerClose(pDevExt->pTracerOps, pSession, uTracerData);
2324 rc = VINF_SUCCESS;
2325
2326 RTSemFastMutexRequest(pDevExt->mtxTracer);
2327 pSession->hTracerCaller = NIL_RTNATIVETHREAD;
2328 Assert(pDevExt->cTracerOpens > 0);
2329 pDevExt->cTracerOpens--;
2330 }
2331 else
2332 rc = VERR_SUPDRV_TRACER_SESSION_BUSY;
2333 }
2334 else
2335 {
2336 rc = VERR_SUPDRV_TRACER_NOT_PRESENT;
2337 pSession->uTracerData = 0;
2338 Assert(pDevExt->cTracerOpens > 0);
2339 pDevExt->cTracerOpens--;
2340 }
2341 }
2342 else
2343 rc = VERR_SUPDRV_TRACER_NOT_OPENED;
2344
2345 RTSemFastMutexRelease(pDevExt->mtxTracer);
2346 return rc;
2347}
2348
2349
2350/**
2351 * Performs a tracer I/O control request.
2352 *
2353 * @returns VBox status code.
2354 * @param pDevExt The device extension structure.
2355 * @param pSession The current session.
2356 * @param uCmd The tracer command.
2357 * @param uArg The tracer argument.
2358 * @param piRetVal Where to store the tracer specific return value.
2359 */
2360int VBOXCALL supdrvIOCtl_TracerIOCtl(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, uintptr_t uCmd, uintptr_t uArg, int32_t *piRetVal)
2361{
2362 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
2363 int rc;
2364
2365 *piRetVal = 0;
2366 RTSemFastMutexRequest(pDevExt->mtxTracer);
2367
2368 if (pSession->uTracerData)
2369 {
2370 Assert(pDevExt->cTracerOpens > 0);
2371 if (pDevExt->pTracerOps)
2372 {
2373 if (!pDevExt->fTracerUnloading)
2374 {
2375 if (pSession->hTracerCaller == NIL_RTNATIVETHREAD)
2376 {
2377 uintptr_t uTracerData = pSession->uTracerData;
2378 pDevExt->cTracerOpens++;
2379 pSession->hTracerCaller = hNativeSelf;
2380 RTSemFastMutexRelease(pDevExt->mtxTracer);
2381
2382 rc = pDevExt->pTracerOps->pfnTracerIoCtl(pDevExt->pTracerOps, pSession, uTracerData, uCmd, uArg, piRetVal);
2383
2384 RTSemFastMutexRequest(pDevExt->mtxTracer);
2385 pSession->hTracerCaller = NIL_RTNATIVETHREAD;
2386 Assert(pDevExt->cTracerOpens > 0);
2387 pDevExt->cTracerOpens--;
2388 }
2389 else
2390 rc = VERR_SUPDRV_TRACER_SESSION_BUSY;
2391 }
2392 else
2393 rc = VERR_SUPDRV_TRACER_UNLOADING;
2394 }
2395 else
2396 rc = VERR_SUPDRV_TRACER_NOT_PRESENT;
2397 }
2398 else
2399 rc = VERR_SUPDRV_TRACER_NOT_OPENED;
2400
2401 RTSemFastMutexRelease(pDevExt->mtxTracer);
2402 return rc;
2403}
2404
2405
2406/**
2407 * Early module initialization hook.
2408 *
2409 * @returns VBox status code.
2410 * @param pDevExt The device extension structure.
2411 */
2412int VBOXCALL supdrvTracerInit(PSUPDRVDEVEXT pDevExt)
2413{
2414 /*
2415 * Initialize the tracer.
2416 */
2417 int rc = RTSemFastMutexCreate(&pDevExt->mtxTracer);
2418 if (RT_SUCCESS(rc))
2419 {
2420 uint32_t i;
2421
2422 pDevExt->TracerHlp.uVersion = SUPDRVTRACERHLP_VERSION;
2423 /** @todo */
2424 pDevExt->TracerHlp.uEndVersion = SUPDRVTRACERHLP_VERSION;
2425 RTListInit(&pDevExt->TracerProviderList);
2426 RTListInit(&pDevExt->TracerProviderZombieList);
2427 for (i = 0; i < RT_ELEMENTS(pDevExt->aTrackerUmodHash); i++)
2428 RTListInit(&pDevExt->aTrackerUmodHash[i]);
2429
2430#ifdef VBOX_WITH_NATIVE_DTRACE
2431 pDevExt->pTracerOps = supdrvDTraceInit();
2432 if (pDevExt->pTracerOps)
2433 g_pfnSupdrvProbeFireKernel = (PFNRT)pDevExt->pTracerOps->pfnProbeFireKernel;
2434#endif
2435
2436 /*
2437 * Register the provider for this module, if compiled in.
2438 */
2439#ifdef VBOX_WITH_DTRACE_R0DRV
2440 rc = supdrvTracerRegisterVtgObj(pDevExt, &g_VTGObjHeader, NULL /*pImage*/, NULL /*pSession*/, NULL /*pUmod*/, "vboxdrv");
2441 if (RT_SUCCESS(rc))
2442 return rc;
2443 SUPR0Printf("supdrvTracerInit: supdrvTracerRegisterVtgObj failed with rc=%d\n", rc);
2444 RTSemFastMutexDestroy(pDevExt->mtxTracer);
2445#else
2446
2447 return VINF_SUCCESS;
2448#endif
2449 }
2450 pDevExt->mtxTracer = NIL_RTSEMFASTMUTEX;
2451 return rc;
2452}
2453
2454
2455/**
2456 * Late module termination hook.
2457 *
2458 * @returns VBox status code.
2459 * @param pDevExt The device extension structure.
2460 */
2461void VBOXCALL supdrvTracerTerm(PSUPDRVDEVEXT pDevExt)
2462{
2463 LOG_TRACER(("supdrvTracerTerm\n"));
2464
2465 supdrvTracerRemoveAllProviders(pDevExt);
2466
2467 RTSemFastMutexDestroy(pDevExt->mtxTracer);
2468 pDevExt->mtxTracer = NIL_RTSEMFASTMUTEX;
2469 LOG_TRACER(("supdrvTracerTerm: Done\n"));
2470}
2471
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