summaryrefslogtreecommitdiffstats
path: root/src/VBox/VMM/VMMAll/DBGFAllTracer.cpp
blob: 8e79602befc0e769cda6f3881b9f3020e4d63143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/* $Id: DBGFAllTracer.cpp $ */
/** @file
 * DBGF - Debugger Facility, All Context Code tracing part.
 */

/*
 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGF
#include "DBGFInternal.h"
#include <VBox/types.h>
#include <VBox/err.h>
#include <VBox/vmm/dbgf.h>
#if defined(IN_RING3)
# include <VBox/vmm/uvm.h>
# include <VBox/vmm/vm.h>
#elif defined(IN_RING0)
# include <VBox/vmm/gvm.h>
#else
# error "Invalid environment"
#endif


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/

/**
 * Returns the current context tracer instance of the given VM instance.
 *
 * @returns Current context pointer to the DBGF tracer instance.
 */
DECLINLINE(PDBGFTRACERINSCC) dbgfTracerGetInstance(PVMCC pVM)
{
#if defined(IN_RING0)
     return pVM->dbgfr0.s.pTracerR0;
#elif defined(IN_RING3)
     PUVM pUVM = pVM->pUVM;
     return pUVM->dbgf.s.pTracerR3;
#elif defined(IN_RC)
# error "Not implemented"
#else
# error "No/Invalid context specified"
#endif
}


/**
 * Returns the size of the tracing ring buffer.
 *
 * @returns Size of the ring buffer in bytes.
 * @param   pThisCC                 The event tracer instance current context data.
 */
DECLINLINE(size_t) dbgfTracerGetRingBufSz(PDBGFTRACERINSCC pThisCC)
{
#if defined(IN_RING0) /* For R0 we are extra cautious and use the ring buffer size stored in R0 memory so R3 can't corrupt it. */
    return pThisCC->cbRingBuf;
#else
    return pThisCC->CTX_SUFF(pShared)->cbRingBuf;
#endif
}


/**
 * Posts a single event descriptor to the ring buffer of the given tracer instance - extended version.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   pThisCC                 The event tracer instance current context data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   enmTraceEvt             The trace event type posted.
 * @param   idEvtPrev               The previous event ID the posted event links to.
 * @param   pvEvtDesc               The event descriptor to copy after the header.
 * @param   cbEvtDesc               Size of the event descriptor.
 * @param   pidEvt                  Where to store the assigned event ID, optional.
 */
static int dbgfTracerEvtPostEx(PVMCC pVM, PDBGFTRACERINSCC pThisCC, DBGFTRACEREVTSRC hEvtSrc,
                               DBGFTRACEREVT enmTraceEvt, uint64_t idEvtPrev, const void *pvEvtDesc,
                               size_t cbEvtDesc, uint64_t *pidEvt)
{
    LogFlowFunc(("pVM=%p pThisCC=%p hEvtSrc=%llu enmTraceEvt=%u idEvtPrev=%llu pvEvtDesc=%p cbEvtDesc=%zu pidEvt=%p\n",
                 pVM, pThisCC, hEvtSrc, enmTraceEvt, idEvtPrev, pvEvtDesc, cbEvtDesc, pidEvt));

    PDBGFTRACERSHARED pSharedCC = pThisCC->CTX_SUFF(pShared);
    size_t cRingBufEvts = dbgfTracerGetRingBufSz(pThisCC) / DBGF_TRACER_EVT_SZ;
    AssertReturn(cRingBufEvts, VERR_DBGF_TRACER_IPE_1);
    AssertReturn(cbEvtDesc <= DBGF_TRACER_EVT_PAYLOAD_SZ, VERR_DBGF_TRACER_IPE_1);

    /* Grab a new event ID first. */
    uint64_t idEvt = ASMAtomicIncU64(&pSharedCC->idEvt) - 1;
    uint64_t idxRingBuf = idEvt % cRingBufEvts; /* This gives the index in the ring buffer for the event. */
    PDBGFTRACEREVTHDR pEvtHdr = (PDBGFTRACEREVTHDR)(pThisCC->CTX_SUFF(pbRingBuf) + idxRingBuf * DBGF_TRACER_EVT_SZ);

    if (RT_UNLIKELY(ASMAtomicReadU64(&pEvtHdr->idEvt) != DBGF_TRACER_EVT_HDR_ID_INVALID))
    {
        /** @todo The event ring buffer is full and we need to go back (from R0 to R3) and wait for the flusher thread to
         * get its act together.
         */
        AssertMsgFailed(("Flush thread can't keep up with event amount!\n"));
    }

    /* Write the event and kick the flush thread if necessary. */
    if (cbEvtDesc)
        memcpy(pEvtHdr + 1, pvEvtDesc, cbEvtDesc);
    pEvtHdr->idEvtPrev   = idEvtPrev;
    pEvtHdr->hEvtSrc     = hEvtSrc;
    pEvtHdr->enmEvt      = enmTraceEvt;
    pEvtHdr->fFlags      = DBGF_TRACER_EVT_HDR_F_DEFAULT;
    ASMAtomicWriteU64(&pEvtHdr->idEvt, idEvt);

    int rc = VINF_SUCCESS;
    if (!ASMAtomicXchgBool(&pSharedCC->fEvtsWaiting, true))
    {
        if (!ASMAtomicXchgBool(&pSharedCC->fFlushThrdActive, true))
            rc = SUPSemEventSignal(pVM->pSession, pSharedCC->hSupSemEvtFlush);
    }

    if (pidEvt)
        *pidEvt = idEvt;
    return rc;
}


/**
 * Posts a single event descriptor to the ring buffer of the given tracer instance.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   pThisCC                 The event tracer instance current context data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   enmTraceEvt             The trace event type posted.
 * @param   pvEvtDesc               The event descriptor to copy after the header.
 * @param   pidEvt                  Where to store the assigned event ID, optional.
 */
DECLINLINE(int) dbgfTracerEvtPostSingle(PVMCC pVM, PDBGFTRACERINSCC pThisCC, DBGFTRACEREVTSRC hEvtSrc,
                                        DBGFTRACEREVT enmTraceEvt, const void *pvEvtDesc, uint64_t *pidEvt)
{
    return dbgfTracerEvtPostEx(pVM, pThisCC, hEvtSrc, enmTraceEvt, DBGF_TRACER_EVT_HDR_ID_INVALID,
                               pvEvtDesc, DBGF_TRACER_EVT_PAYLOAD_SZ, pidEvt);
}


#ifdef IN_RING3
/**
 * Posts a single event descriptor to the ring buffer of the given tracer instance - R3 only variant
 * (used for the register/deregister event source events currently).
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   pThisCC                 The event tracer instance current context data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   enmTraceEvt             The trace event type posted.
 * @param   pvEvtDesc               The event descriptor to copy after the header.
 * @param   cbEvtDesc               Event descriptor size in bytes.
 * @param   pidEvt                  Where to store the assigned event ID, optional.
 */
DECLHIDDEN(int) dbgfTracerR3EvtPostSingle(PVMCC pVM, PDBGFTRACERINSCC pThisCC, DBGFTRACEREVTSRC hEvtSrc,
                                          DBGFTRACEREVT enmTraceEvt, const void *pvEvtDesc, size_t cbEvtDesc,
                                          uint64_t *pidEvt)
{
    return dbgfTracerEvtPostEx(pVM, pThisCC, hEvtSrc, enmTraceEvt, DBGF_TRACER_EVT_HDR_ID_INVALID,
                               pvEvtDesc, cbEvtDesc, pidEvt);
}
#endif

/**
 * Copies the given MMIO value into the event descriptor based on the given size.
 *
 * @param   pEvtMmio                Pointer to the MMIO event descriptor to fill.
 * @param   pvVal                   The value to copy.
 * @param   cbVal                   Size of the value in bytes.
 */
static void dbgfTracerEvtMmioCopyVal(PDBGFTRACEREVTMMIO pEvtMmio, const void *pvVal, size_t cbVal)
{
    switch (cbVal)
    {
        case 1:
            pEvtMmio->u64Val = *(uint8_t *)pvVal;
            break;
        case 2:
            pEvtMmio->u64Val = *(uint16_t *)pvVal;
            break;
        case 4:
            pEvtMmio->u64Val = *(uint32_t *)pvVal;
            break;
        case 8:
            pEvtMmio->u64Val = *(uint64_t *)pvVal;
            break;
        default:
            AssertMsgFailed(("The value size %zu is not supported!\n", cbVal));
    }
}


/**
 * Copies the given I/O port value into the event descriptor based on the given size.
 *
 * @param   pEvtIoPort              Pointer to the I/O port read/write event descriptor to fill.
 * @param   pvVal                   The value to copy.
 * @param   cbVal                   Size of the value in bytes.
 */
static void dbgfTracerEvtIoPortCopyVal(PDBGFTRACEREVTIOPORT pEvtIoPort, const void *pvVal, size_t cbVal)
{
    switch (cbVal)
    {
        case 1:
            pEvtIoPort->u32Val = *(uint8_t *)pvVal;
            break;
        case 2:
            pEvtIoPort->u32Val = *(uint16_t *)pvVal;
            break;
        case 4:
            pEvtIoPort->u32Val = *(uint32_t *)pvVal;
            break;
        default:
            AssertMsgFailed(("The value size %zu is not supported!\n", cbVal));
    }
}


/**
 * Handles a guest memory transfer event.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   pThisCC                 The event tracer instance current context data.
 * @param   enmTraceEvt             The trace event type posted.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   GCPhys                  The guest physical address the transfer starts at.
 * @param   pvBuf                   The data being transfered.
 * @param   cbXfer                  Number of bytes being transfered.
 */
static int dbgfTracerEvtGCPhys(PVMCC pVM, PDBGFTRACERINSCC pThisCC, DBGFTRACEREVT enmTraceEvt, DBGFTRACEREVTSRC hEvtSrc,
                               RTGCPHYS GCPhys, const void *pvBuf, size_t cbXfer)
{
    /* Fast path for really small transfers where everything fits into the descriptor. */
    DBGFTRACEREVTGCPHYS EvtGCPhys;
    EvtGCPhys.GCPhys = GCPhys;
    EvtGCPhys.cbXfer = cbXfer;
    if (cbXfer <= sizeof(EvtGCPhys.abData))
    {
        memcpy(&EvtGCPhys.abData[0], pvBuf, cbXfer);
        return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, enmTraceEvt, &EvtGCPhys, NULL /*pidEvt*/);
    }

    /*
     * Slow path where we have to split the data into multiple entries.
     * Each one is linked to the previous one by the previous event ID.
     */
    const uint8_t *pbBuf = (const uint8_t *)pvBuf;
    size_t cbLeft = cbXfer;
    uint64_t idEvtPrev = 0;
    memcpy(&EvtGCPhys.abData[0], pbBuf, sizeof(EvtGCPhys.abData));
    pbBuf  += sizeof(EvtGCPhys.abData);
    cbLeft -= sizeof(EvtGCPhys.abData);

    int rc = dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, enmTraceEvt, &EvtGCPhys, &idEvtPrev);
    while (   RT_SUCCESS(rc)
           && cbLeft)
    {
        size_t cbThisXfer = RT_MIN(cbLeft, DBGF_TRACER_EVT_PAYLOAD_SZ);
        rc = dbgfTracerEvtPostEx(pVM, pThisCC, hEvtSrc, enmTraceEvt, idEvtPrev,
                                 pbBuf, cbThisXfer, &idEvtPrev);

        pbBuf  += cbThisXfer;
        cbLeft -= cbThisXfer;
    }

    return rc;
}


/**
 * Handles a I/O port string transfer event.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   pThisCC                 The event tracer instance current context data.
 * @param   enmTraceEvt             The trace event type posted.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle for the transfer.
 * @param   offPort                 The offset into the region where the transfer happened.
 * @param   pv                      The data being transfered.
 * @param   cb                      Number of bytes of valid data in the buffer.
 * @param   cbItem                  Item size in bytes.
 * @param   cTransfersReq           Number of transfers requested.
 * @param   cTransfersRet           Number of transfers done.
 */
static int dbgfTracerEvtIoPortStr(PVMCC pVM, PDBGFTRACERINSCC pThisCC, DBGFTRACEREVT enmTraceEvt, DBGFTRACEREVTSRC hEvtSrc,
                                  uint64_t hIoPorts, RTIOPORT offPort, const void *pv, size_t cb, size_t cbItem, uint32_t cTransfersReq,
                                  uint32_t cTransfersRet)
{
    /* Fast path for really small transfers where everything fits into the descriptor. */
    DBGFTRACEREVTIOPORTSTR EvtIoPortStr;
    EvtIoPortStr.hIoPorts      = hIoPorts;
    EvtIoPortStr.cbItem        = (uint32_t)cbItem;
    EvtIoPortStr.cTransfersReq = cTransfersReq;
    EvtIoPortStr.cTransfersRet = cTransfersRet;
    EvtIoPortStr.offPort       = offPort;
    if (cb <= sizeof(EvtIoPortStr.abData))
    {
        memcpy(&EvtIoPortStr.abData[0], pv, cb);
        return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, enmTraceEvt, &EvtIoPortStr, NULL /*pidEvt*/);
    }

    /*
     * Slow path where we have to split the data into multiple entries.
     * Each one is linked to the previous one by the previous event ID.
     */
    const uint8_t *pbBuf = (const uint8_t *)pv;
    size_t cbLeft = cb;
    uint64_t idEvtPrev = 0;
    memcpy(&EvtIoPortStr.abData[0], pbBuf, sizeof(EvtIoPortStr.abData));
    pbBuf  += sizeof(EvtIoPortStr.abData);
    cbLeft -= sizeof(EvtIoPortStr.abData);

    int rc = dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, enmTraceEvt, &EvtIoPortStr, &idEvtPrev);
    while (   RT_SUCCESS(rc)
           && cbLeft)
    {
        size_t cbThisXfer = RT_MIN(cbLeft, DBGF_TRACER_EVT_PAYLOAD_SZ);
        rc = dbgfTracerEvtPostEx(pVM, pThisCC, hEvtSrc, enmTraceEvt, idEvtPrev,
                                 pbBuf, cbThisXfer, &idEvtPrev);

        pbBuf  += cbThisXfer;
        cbLeft -= cbThisXfer;
    }

    return rc;
}


/**
 * Registers an MMIO region mapping event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hRegion                 The MMIO region handle being mapped.
 * @param   GCPhysMmio              The guest physical address where the region is mapped.
 */
VMM_INT_DECL(int) DBGFTracerEvtMmioMap(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hRegion, RTGCPHYS GCPhysMmio)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTMMIOMAP EvtMmioMap;
    EvtMmioMap.hMmioRegion     = hRegion;
    EvtMmioMap.GCPhysMmioBase  = GCPhysMmio;
    EvtMmioMap.au64Pad0[0]     = 0;
    EvtMmioMap.au64Pad0[1]     = 0;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_MMIO_MAP, &EvtMmioMap, NULL /*pidEvt*/);
}


/**
 * Registers an MMIO region unmap event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hRegion                 The MMIO region handle being unmapped.
 */
VMM_INT_DECL(int) DBGFTracerEvtMmioUnmap(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hRegion)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTMMIOUNMAP EvtMmioUnmap;
    EvtMmioUnmap.hMmioRegion = hRegion;
    EvtMmioUnmap.au64Pad0[0] = 0;
    EvtMmioUnmap.au64Pad0[1] = 0;
    EvtMmioUnmap.au64Pad0[2] = 0;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_MMIO_UNMAP, &EvtMmioUnmap, NULL /*pidEvt*/);
}


/**
 * Registers an MMIO region read event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hRegion                 The MMIO region handle being read.
 * @param   offMmio                 The MMIO offset into the region where the read happened.
 * @param   pvVal                   The value being read.
 * @param   cbVal                   Value size in bytes.
 */
VMM_INT_DECL(int) DBGFTracerEvtMmioRead(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hRegion, RTGCPHYS offMmio, const void *pvVal, size_t cbVal)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTMMIO EvtMmio;
    EvtMmio.hMmioRegion = hRegion;
    EvtMmio.offMmio     = offMmio;
    EvtMmio.cbXfer      = cbVal;
    dbgfTracerEvtMmioCopyVal(&EvtMmio, pvVal, cbVal);

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_MMIO_READ, &EvtMmio, NULL /*pidEvt*/);
}


/**
 * Registers an MMIO region write event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hRegion                 The MMIO region handle being written to.
 * @param   offMmio                 The MMIO offset into the region where the write happened.
 * @param   pvVal                   The value being written.
 * @param   cbVal                   Value size in bytes.
 */
VMM_INT_DECL(int) DBGFTracerEvtMmioWrite(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hRegion, RTGCPHYS offMmio, const void *pvVal, size_t cbVal)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTMMIO EvtMmio;
    EvtMmio.hMmioRegion = hRegion;
    EvtMmio.offMmio     = offMmio;
    EvtMmio.cbXfer      = cbVal;
    dbgfTracerEvtMmioCopyVal(&EvtMmio, pvVal, cbVal);

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_MMIO_WRITE, &EvtMmio, NULL /*pidEvt*/);
}


/**
 * Registers an MMIO region fill event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hRegion                 The MMIO region handle being filled.
 * @param   offMmio                 The MMIO offset into the region where the fill starts.
 * @param   u32Item                 The value being used for filling.
 * @param   cbItem                  Item size in bytes.
 * @param   cItems                  Number of items being written.
 */
VMM_INT_DECL(int) DBGFTracerEvtMmioFill(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hRegion, RTGCPHYS offMmio,
                                        uint32_t u32Item, uint32_t cbItem, uint32_t cItems)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTMMIOFILL EvtMmioFill;
    EvtMmioFill.hMmioRegion = hRegion;
    EvtMmioFill.offMmio     = offMmio;
    EvtMmioFill.cbItem      = cbItem;
    EvtMmioFill.cItems      = cItems;
    EvtMmioFill.u32Item     = u32Item;
    EvtMmioFill.u32Pad0     = 0;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_MMIO_FILL, &EvtMmioFill, NULL /*pidEvt*/);
}


/**
 * Registers an I/O region mapping event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle being mapped.
 * @param   IoPortBase              The I/O port base where the region is mapped.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoPortMap(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hIoPorts, RTIOPORT IoPortBase)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTIOPORTMAP EvtIoPortMap;
    RT_ZERO(EvtIoPortMap);
    EvtIoPortMap.hIoPorts    = hIoPorts;
    EvtIoPortMap.IoPortBase  = IoPortBase;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_IOPORT_MAP, &EvtIoPortMap, NULL /*pidEvt*/);
}


/**
 * Registers an I/O region unmap event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle being unmapped.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoPortUnmap(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hIoPorts)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTIOPORTUNMAP EvtIoPortUnmap;
    EvtIoPortUnmap.hIoPorts    = hIoPorts;
    EvtIoPortUnmap.au64Pad0[0] = 0;
    EvtIoPortUnmap.au64Pad0[1] = 0;
    EvtIoPortUnmap.au64Pad0[2] = 0;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_IOPORT_UNMAP, &EvtIoPortUnmap, NULL /*pidEvt*/);
}


/**
 * Registers an I/O region read event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle being read from.
 * @param   offPort                 The offset into the region where the read happened.
 * @param   pvVal                   The value being read.
 * @param   cbVal                   Value size in bytes.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoPortRead(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hIoPorts, RTIOPORT offPort, const void *pvVal, size_t cbVal)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTIOPORT EvtIoPort;
    RT_ZERO(EvtIoPort);
    EvtIoPort.hIoPorts = hIoPorts;
    EvtIoPort.offPort  = offPort;
    EvtIoPort.cbXfer   = cbVal;
    dbgfTracerEvtIoPortCopyVal(&EvtIoPort, pvVal, cbVal);

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_IOPORT_READ, &EvtIoPort, NULL /*pidEvt*/);
}


/**
 * Registers an I/O region string read event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle being read from.
 * @param   offPort                 The offset into the region where the read happened.
 * @param   pv                      The data being read.
 * @param   cb                      Item size in bytes.
 * @param   cTransfersReq           Number of transfers requested.
 * @param   cTransfersRet           Number of transfers done.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoPortReadStr(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hIoPorts, RTIOPORT offPort, const void *pv, size_t cb,
                                             uint32_t cTransfersReq, uint32_t cTransfersRet)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    return dbgfTracerEvtIoPortStr(pVM, pThisCC, DBGFTRACEREVT_IOPORT_READ_STR, hEvtSrc, hIoPorts, offPort, pv, cTransfersRet * cb,
                                  cb, cTransfersReq, cTransfersRet);
}


/**
 * Registers an I/O region write event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle being written to.
 * @param   offPort                 The offset into the region where the write happened.
 * @param   pvVal                   The value being written.
 * @param   cbVal                   Value size in bytes.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoPortWrite(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hIoPorts, RTIOPORT offPort, const void *pvVal, size_t cbVal)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTIOPORT EvtIoPort;
    RT_ZERO(EvtIoPort);
    EvtIoPort.hIoPorts = hIoPorts;
    EvtIoPort.offPort  = offPort;
    EvtIoPort.cbXfer   = cbVal;
    dbgfTracerEvtIoPortCopyVal(&EvtIoPort, pvVal, cbVal);

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_IOPORT_WRITE, &EvtIoPort, NULL /*pidEvt*/);
}


/**
 * Registers an I/O region string write event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   hIoPorts                The I/O port region handle being written to.
 * @param   offPort                 The offset into the region where the write happened.
 * @param   pv                      The data being written.
 * @param   cb                      Item size in bytes.
 * @param   cTransfersReq           Number of transfers requested.
 * @param   cTransfersRet           Number of transfers done.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoPortWriteStr(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, uint64_t hIoPorts, RTIOPORT offPort, const void *pv, size_t cb,
                                              uint32_t cTransfersReq, uint32_t cTransfersRet)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    return dbgfTracerEvtIoPortStr(pVM, pThisCC, DBGFTRACEREVT_IOPORT_WRITE_STR, hEvtSrc, hIoPorts, offPort, pv, cTransfersReq * cb,
                                  cb, cTransfersReq, cTransfersRet);
}


/**
 * Registers an IRQ change event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   iIrq                    The IRQ line changed.
 * @param   fIrqLvl                 The new IRQ level mask.
 */
VMM_INT_DECL(int) DBGFTracerEvtIrq(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, int32_t iIrq, int32_t fIrqLvl)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTIRQ EvtIrq;
    RT_ZERO(EvtIrq);
    EvtIrq.iIrq    = iIrq;
    EvtIrq.fIrqLvl = fIrqLvl;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_IRQ, &EvtIrq, NULL /*pidEvt*/);
}


/**
 * Registers an I/O APIC MSI event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   GCPhys                  Guest physical address where the value is written to.
 * @param   u32Val                  The MSI event value being written.
 */
VMM_INT_DECL(int) DBGFTracerEvtIoApicMsi(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, RTGCPHYS GCPhys, uint32_t u32Val)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    DBGFTRACEREVTIOAPICMSI EvtMsi;
    RT_ZERO(EvtMsi);
    EvtMsi.GCPhys = GCPhys;
    EvtMsi.u32Val = u32Val;

    return dbgfTracerEvtPostSingle(pVM, pThisCC, hEvtSrc, DBGFTRACEREVT_IOAPIC_MSI, &EvtMsi, NULL /*pidEvt*/);
}


/**
 * Registers an guest physical memory read event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   GCPhys                  Guest physical address the read started at.
 * @param   pvBuf                   The read data.
 * @param   cbRead                  Number of bytes read.
 */
VMM_INT_DECL(int) DBGFTracerEvtGCPhysRead(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, RTGCPHYS GCPhys, const void *pvBuf, size_t cbRead)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    return dbgfTracerEvtGCPhys(pVM, pThisCC, DBGFTRACEREVT_GCPHYS_READ, hEvtSrc, GCPhys, pvBuf, cbRead);
}


/**
 * Registers an guest physical memory write event for the given event source.
 *
 * @returns VBox status code.
 * @param   pVM                     The current context VM instance data.
 * @param   hEvtSrc                 The event source for the posted event.
 * @param   GCPhys                  Guest physical address the write started at.
 * @param   pvBuf                   The written data.
 * @param   cbWrite                 Number of bytes written.
 */
VMM_INT_DECL(int) DBGFTracerEvtGCPhysWrite(PVMCC pVM, DBGFTRACEREVTSRC hEvtSrc, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
{
    PDBGFTRACERINSCC pThisCC = dbgfTracerGetInstance(pVM);
    AssertReturn(pThisCC, VERR_DBGF_TRACER_IPE_1);

    return dbgfTracerEvtGCPhys(pVM, pThisCC, DBGFTRACEREVT_GCPHYS_WRITE, hEvtSrc, GCPhys, pvBuf, cbWrite);
}