summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/PC/DevPit-i8254.cpp
blob: 37446218c82947764534ab50758f32670995af9e (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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
/* $Id: DevPit-i8254.cpp $ */
/** @file
 * DevPIT-i8254 - Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device.
 */

/*
 * Copyright (C) 2006-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
 * --------------------------------------------------------------------
 *
 * This code is based on:
 *
 * QEMU 8253/8254 interval timer emulation
 *
 * Copyright (c) 2003-2004 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_PIT
#include <VBox/vmm/pdmdev.h>
#include <VBox/log.h>
#include <VBox/vmm/stam.h>
#include <iprt/assert.h>
#include <iprt/asm-math.h>

#ifdef IN_RING3
# ifdef RT_OS_LINUX
#  include <fcntl.h>
#  include <errno.h>
#  include <unistd.h>
#  include <stdio.h>
#  include <linux/kd.h>
#  include <linux/input.h>
#  include <sys/ioctl.h>
# endif
# include <iprt/alloc.h>
# include <iprt/string.h>
# include <iprt/uuid.h>
#endif /* IN_RING3 */

#include "VBoxDD.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** The PIT frequency. */
#define PIT_FREQ 1193182

#define RW_STATE_LSB 1
#define RW_STATE_MSB 2
#define RW_STATE_WORD0 3
#define RW_STATE_WORD1 4

/** The current saved state version. */
#define PIT_SAVED_STATE_VERSION             4
/** The saved state version used by VirtualBox 3.1 and earlier.
 * This did not include disable by HPET flag. */
#define PIT_SAVED_STATE_VERSION_VBOX_31     3
/** The saved state version used by VirtualBox 3.0 and earlier.
 * This did not include the config part. */
#define PIT_SAVED_STATE_VERSION_VBOX_30     2

/** @def FAKE_REFRESH_CLOCK
 * Define this to flip the 15usec refresh bit on every read.
 * If not defined, it will be flipped correctly. */
/* #define FAKE_REFRESH_CLOCK */
#ifdef DOXYGEN_RUNNING
# define FAKE_REFRESH_CLOCK
#endif

/** The effective counter mode - if bit 1 is set, bit 2 is ignored. */
#define EFFECTIVE_MODE(x)   ((x) & ~(((x) & 2) << 1))


/**
 * Acquires the PIT lock or returns.
 */
#define DEVPIT_LOCK_RETURN(a_pDevIns, a_pThis, a_rcBusy)  \
    do { \
        int const rcLock = PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSect, (a_rcBusy)); \
        if (rcLock == VINF_SUCCESS) { /* likely */ } \
        else return rcLock; \
    } while (0)

/**
 * Releases the PIT lock.
 */
#define DEVPIT_UNLOCK(a_pDevIns, a_pThis) \
    do { PDMDevHlpCritSectLeave((a_pDevIns), &(a_pThis)->CritSect); } while (0)


/**
 * Acquires the TM lock and PIT lock, returns on failure.
 */
#define DEVPIT_LOCK_BOTH_RETURN(a_pDevIns, a_pThis, a_rcBusy)  \
    do { \
        VBOXSTRICTRC rcLock = PDMDevHlpTimerLockClock2((a_pDevIns), (a_pThis)->channels[0].hTimer, \
                                                       &(a_pThis)->CritSect, (a_rcBusy)); \
        if (RT_LIKELY(rcLock == VINF_SUCCESS)) \
        { /* likely */ } \
        else \
            return rcLock; \
    } while (0)

#ifdef IN_RING3
/**
 * Acquires the TM lock and PIT lock, ignores failures.
 */
# define DEVPIT_R3_LOCK_BOTH(a_pDevIns, a_pThis) \
    PDMDevHlpTimerLockClock2((a_pDevIns), (a_pThis)->channels[0].hTimer, &(a_pThis)->CritSect, VERR_IGNORED)
#endif /* IN_RING3 */

/**
 * Releases the PIT lock and TM lock.
 */
#define DEVPIT_UNLOCK_BOTH(a_pDevIns, a_pThis) \
    PDMDevHlpTimerUnlockClock2((a_pDevIns), (a_pThis)->channels[0].hTimer, &(a_pThis)->CritSect)



/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * The state of one PIT channel.
 */
typedef struct PITCHANNEL
{
    /** The timer.
     * @note Only channel 0 has a timer.  */
    TMTIMERHANDLE                   hTimer;
    /** The virtual time stamp at the last reload (only used in mode 2 for now). */
    uint64_t                        u64ReloadTS;
    /** The actual time of the next tick.
     * As apposed to the next_transition_time which contains the correct time of the next tick. */
    uint64_t                        u64NextTS;

    /** (count_load_time is only set by PDMDevHlpTimerGet() which returns uint64_t) */
    uint64_t count_load_time;
    /* irq handling */
    int64_t next_transition_time;
    int32_t irq;
    /** Number of release log entries. Used to prevent flooding. */
    uint8_t cRelLogEntries;
    /** The channel number. */
    uint8_t iChan;
    uint8_t abAlignment[2];

    uint32_t count; /* can be 65536 */
    uint16_t latched_count;
    uint8_t count_latched;
    uint8_t status_latched;

    uint8_t status;
    uint8_t read_state;
    uint8_t write_state;
    uint8_t write_latch;

    uint8_t rw_mode;
    uint8_t mode;
    uint8_t bcd; /* not supported */
    uint8_t gate; /* timer start */

} PITCHANNEL;
/** Pointer to the state of one PIT channel. */
typedef PITCHANNEL *PPITCHANNEL;

/** Speaker emulation state. */
typedef enum PITSPEAKEREMU
{
    PIT_SPEAKER_EMU_NONE = 0,
    PIT_SPEAKER_EMU_CONSOLE,
    PIT_SPEAKER_EMU_EVDEV,
    PIT_SPEAKER_EMU_TTY
} PITSPEAKEREMU;

/**
 * The shared PIT state.
 */
typedef struct PITSTATE
{
    /** Channel state. Must come first? */
    PITCHANNEL              channels[3];
    /** Speaker data. */
    int32_t                 speaker_data_on;
#ifdef FAKE_REFRESH_CLOCK
    /** Refresh dummy. */
    int32_t                 dummy_refresh_clock;
#else
    uint32_t                Alignment1;
#endif
    /** Config: I/O port base. */
    RTIOPORT                IOPortBaseCfg;
    /** Config: Speaker enabled. */
    bool                    fSpeakerCfg;
    /** Disconnect PIT from the interrupt controllers if requested by HPET. */
    bool                    fDisabledByHpet;
    /** Config: What to do with speaker activity. */
    PITSPEAKEREMU           enmSpeakerEmu;
#ifdef RT_OS_LINUX
    /** File handle for host speaker functionality. */
    int                     hHostSpeaker;
    int                     afAlignment2;
#endif
    /** Number of IRQs that's been raised. */
    STAMCOUNTER             StatPITIrq;
    /** Profiling the timer callback handler. */
    STAMPROFILEADV          StatPITHandler;
    /** Critical section protecting the state. */
    PDMCRITSECT             CritSect;
    /** The primary I/O port range (0x40-0x43). */
    IOMIOPORTHANDLE         hIoPorts;
    /** The speaker I/O port range (0x40-0x43). */
    IOMIOPORTHANDLE         hIoPortSpeaker;
} PITSTATE;
/** Pointer to the shared PIT device state. */
typedef PITSTATE *PPITSTATE;


/**
 * The ring-3 PIT state.
 */
typedef struct PITSTATER3
{
    /** PIT port interface. */
    PDMIHPETLEGACYNOTIFY    IHpetLegacyNotify;
    /** Pointer to the device instance. */
    PPDMDEVINSR3            pDevIns;
} PITSTATER3;
/** Pointer to the ring-3 PIT device state. */
typedef PITSTATER3 *PPITSTATER3;


#ifndef VBOX_DEVICE_STRUCT_TESTCASE



static int pit_get_count(PPDMDEVINS pDevIns, PPITSTATE pThis, PPITCHANNEL pChan)
{
    uint64_t d;
    TMTIMERHANDLE hTimer = pThis->channels[0].hTimer;
    Assert(PDMDevHlpTimerIsLockOwner(pDevIns, hTimer));

    if (EFFECTIVE_MODE(pChan->mode) == 2)
    {
        if (pChan->u64NextTS == UINT64_MAX)
        {
            d = ASMMultU64ByU32DivByU32(PDMDevHlpTimerGet(pDevIns, hTimer) - pChan->count_load_time,
                                        PIT_FREQ, PDMDevHlpTimerGetFreq(pDevIns, hTimer));
            return pChan->count - (d % pChan->count); /** @todo check this value. */
        }
        uint64_t Interval = pChan->u64NextTS - pChan->u64ReloadTS;
        if (!Interval)
            return pChan->count - 1; /** @todo This is WRONG! But I'm too tired to fix it properly and just want to shut up a DIV/0 trap now. */
        d = PDMDevHlpTimerGet(pDevIns, hTimer);
        d = ASMMultU64ByU32DivByU32(d - pChan->u64ReloadTS, pChan->count, Interval);
        if (d >= pChan->count)
            return 1;
        return pChan->count - d;
    }

    d = ASMMultU64ByU32DivByU32(PDMDevHlpTimerGet(pDevIns, hTimer) - pChan->count_load_time,
                                PIT_FREQ, PDMDevHlpTimerGetFreq(pDevIns, hTimer));
    int counter;
    switch (EFFECTIVE_MODE(pChan->mode))
    {
        case 0:
        case 1:
        case 4:
        case 5:
            counter = (pChan->count - d) & 0xffff;
            break;
        case 3:
            /* XXX: may be incorrect for odd counts */
            counter = pChan->count - ((2 * d) % pChan->count);
            break;
        default:
            counter = pChan->count - (d % pChan->count);
            break;
    }
    /** @todo check that we don't return 0, in most modes (all?) the counter shouldn't be zero. */
    return counter;
}


/* get pit output bit */
static int pit_get_out1(PPDMDEVINS pDevIns, PPITSTATE pThis, PPITCHANNEL pChan, int64_t current_time)
{
    TMTIMERHANDLE hTimer = pThis->channels[0].hTimer;
    uint64_t d;
    int out;

    d = ASMMultU64ByU32DivByU32(current_time - pChan->count_load_time, PIT_FREQ, PDMDevHlpTimerGetFreq(pDevIns, hTimer));
    switch (EFFECTIVE_MODE(pChan->mode))
    {
        default:
        case 0:
            out = (d >= pChan->count);
            break;
        case 1:
            out = (d < pChan->count);
            break;
        case 2:
            Log2(("pit_get_out1: d=%llx c=%x %x \n", d, pChan->count, (unsigned)(d % pChan->count)));
            if ((d % pChan->count) == 0 && d != 0)
                out = 1;
            else
                out = 0;
            break;
        case 3:
            out = (d % pChan->count) < ((pChan->count + 1) >> 1);
            break;
        case 4:
        case 5:
            out = (d != pChan->count);
            break;
    }
    return out;
}


static int pit_get_out(PPDMDEVINS pDevIns, PPITSTATE pThis, int channel, int64_t current_time)
{
    PPITCHANNEL pChan = &pThis->channels[channel];
    return pit_get_out1(pDevIns, pThis, pChan, current_time);
}


static int pit_get_gate(PPITSTATE pThis, int channel)
{
    PPITCHANNEL pChan = &pThis->channels[channel];
    return pChan->gate;
}


/* if already latched, do not latch again */
static void pit_latch_count(PPDMDEVINS pDevIns, PPITSTATE pThis, PPITCHANNEL pChan)
{
    if (!pChan->count_latched)
    {
        pChan->latched_count = pit_get_count(pDevIns, pThis, pChan);
        pChan->count_latched = pChan->rw_mode;
        LogFlow(("pit_latch_count: latched_count=%#06x / %10RU64 ns (c=%#06x m=%d)\n",
                 pChan->latched_count, ASMMultU64ByU32DivByU32(pChan->count - pChan->latched_count, 1000000000, PIT_FREQ),
                 pChan->count, pChan->mode));
    }
}

#ifdef IN_RING3

/* return -1 if no transition will occur.  */
static int64_t pitR3GetNextTransitionTime(PPDMDEVINS pDevIns, PPITSTATE pThis, PPITCHANNEL pChan, uint64_t current_time)
{
    TMTIMERHANDLE hTimer = pThis->channels[0].hTimer;
    uint64_t d, next_time, base;
    uint32_t period2;

    d = ASMMultU64ByU32DivByU32(current_time - pChan->count_load_time, PIT_FREQ, PDMDevHlpTimerGetFreq(pDevIns, hTimer));
    switch (EFFECTIVE_MODE(pChan->mode))
    {
        default:
        case 0:
        case 1:
            if (d < pChan->count)
                next_time = pChan->count;
            else
                return -1;
            break;

        /*
         * Mode 2: The period is 'count' PIT ticks.
         * When the counter reaches 1 we set the output low (for channel 0 that
         * means lowering IRQ0). On the next tick, where we should be decrementing
         * from 1 to 0, the count is loaded and the output goes high (channel 0
         * means raising IRQ0 again and triggering timer interrupt).
         *
         * In VirtualBox we compress the pulse and flip-flop the IRQ line at the
         * end of the period, which signals an interrupt at the exact same time.
         */
        case 2:
            base = (d / pChan->count) * pChan->count;
# ifndef VBOX /* see above */
            if ((d - base) == 0 && d != 0)
                next_time = base + pChan->count - 1;
            else
# endif
                next_time = base + pChan->count;
            break;
        case 3:
            base = (d / pChan->count) * pChan->count;
            period2 = ((pChan->count + 1) >> 1);
            if ((d - base) < period2)
                next_time = base + period2;
            else
                next_time = base + pChan->count;
            break;

        /* Modes 4 and 5 generate a short pulse at the end of the time delay. This
         * is similar to mode 2, except modes 4/5 aren't periodic. We use the same
         * optimization - only use one timer callback and pulse the IRQ.
         * Note: Tickless Linux kernels use PIT mode 4 with 'nolapic'.
         */
        case 4:
        case 5:
# ifdef VBOX
            if (d <= pChan->count)
                next_time = pChan->count;
# else
            if (d < pChan->count)
                next_time = pChan->count;
            else if (d == pChan->count)
                next_time = pChan->count + 1;
# endif
            else
                return -1;
            break;
    }

    /* convert to timer units */
    LogFlow(("PIT: next_time=%'14RU64 %'20RU64 mode=%#x count=%#06x\n", next_time,
             ASMMultU64ByU32DivByU32(next_time, PDMDevHlpTimerGetFreq(pDevIns, hTimer), PIT_FREQ), pChan->mode, pChan->count));
    next_time = pChan->count_load_time + ASMMultU64ByU32DivByU32(next_time, PDMDevHlpTimerGetFreq(pDevIns, hTimer), PIT_FREQ);

    /* fix potential rounding problems */
    if (next_time <= current_time)
        next_time = current_time;

    /* Add one to next_time; if we don't, integer truncation will cause
     * the algorithm to think that at the end of each period, it'pChan still
     * within the first one instead of at the beginning of the next one.
     */
    return next_time + 1;
}


static void pitR3IrqTimerUpdate(PPDMDEVINS pDevIns, PPITSTATE pThis, PPITCHANNEL pChan,
                                uint64_t current_time, uint64_t now, bool in_timer)
{
    int64_t expire_time;
    int irq_level;
    Assert(PDMDevHlpTimerIsLockOwner(pDevIns, pThis->channels[0].hTimer));

    if (pChan->hTimer == NIL_TMTIMERHANDLE)
        return;
    expire_time = pitR3GetNextTransitionTime(pDevIns, pThis, pChan, current_time);
    irq_level = pit_get_out1(pDevIns, pThis, pChan, current_time) ? PDM_IRQ_LEVEL_HIGH : PDM_IRQ_LEVEL_LOW;

    /* If PIT is disabled by HPET - simply disconnect ticks from interrupt controllers,
     * but do not modify other aspects of device operation.
     */
    if (!pThis->fDisabledByHpet)
    {
        switch (EFFECTIVE_MODE(pChan->mode))
        {
            case 2:
            case 4:
            case 5:
                /* We just flip-flop the IRQ line to save an extra timer call,
                 * which isn't generally required. However, the pulse is only
                 * generated when running on the timer callback (and thus on
                 * the trailing edge of the output signal pulse).
                 */
                if (in_timer)
                {
                    PDMDevHlpISASetIrq(pDevIns, pChan->irq, PDM_IRQ_LEVEL_FLIP_FLOP);
                    break;
                }
                RT_FALL_THRU();
            default:
                PDMDevHlpISASetIrq(pDevIns, pChan->irq, irq_level);
                break;
        }
    }

    if (irq_level)
    {
        pChan->u64ReloadTS = now;
        STAM_COUNTER_INC(&pThis->StatPITIrq);
    }

    if (expire_time != -1)
    {
        Log3(("pitR3IrqTimerUpdate: next=%'RU64 now=%'RU64\n", expire_time, now));
        pChan->u64NextTS = expire_time;
        PDMDevHlpTimerSet(pDevIns, pChan->hTimer, pChan->u64NextTS);
    }
    else
    {
        LogFlow(("PIT: m=%d count=%#4x irq_level=%#x stopped\n", pChan->mode, pChan->count, irq_level));
        PDMDevHlpTimerStop(pDevIns, pChan->hTimer);
        pChan->u64NextTS = UINT64_MAX;
    }
    pChan->next_transition_time = expire_time;
}


/* val must be 0 or 1 */
static void pitR3SetGate(PPDMDEVINS pDevIns, PPITSTATE pThis, int channel, int val)
{
    PPITCHANNEL     pChan  = &pThis->channels[channel];
    TMTIMERHANDLE   hTimer = pThis->channels[0].hTimer;

    Assert((val & 1) == val);
    Assert(PDMDevHlpTimerIsLockOwner(pDevIns, hTimer));

    switch (EFFECTIVE_MODE(pChan->mode))
    {
        default:
        case 0:
        case 4:
            /* XXX: just disable/enable counting */
            break;
        case 1:
        case 5:
            if (pChan->gate < val)
            {
                /* restart counting on rising edge */
                Log(("pitR3SetGate: restarting mode %d\n", pChan->mode));
                pChan->count_load_time = PDMDevHlpTimerGet(pDevIns, hTimer);
                pitR3IrqTimerUpdate(pDevIns, pThis, pChan, pChan->count_load_time, pChan->count_load_time, false);
            }
            break;
        case 2:
        case 3:
            if (pChan->gate < val)
            {
                /* restart counting on rising edge */
                Log(("pitR3SetGate: restarting mode %d\n", pChan->mode));
                pChan->count_load_time = pChan->u64ReloadTS = PDMDevHlpTimerGet(pDevIns, hTimer);
                pitR3IrqTimerUpdate(pDevIns, pThis, pChan, pChan->count_load_time, pChan->count_load_time, false);
            }
            /* XXX: disable/enable counting */
            break;
    }
    pChan->gate = val;
}


static void pitR3LoadCount(PPDMDEVINS pDevIns, PPITSTATE pThis, PPITCHANNEL pChan, int val)
{
    TMTIMERHANDLE hTimer = pThis->channels[0].hTimer;
    Assert(PDMDevHlpTimerIsLockOwner(pDevIns, hTimer));

    if (val == 0)
        val = 0x10000;
    pChan->count_load_time = pChan->u64ReloadTS = PDMDevHlpTimerGet(pDevIns, hTimer);
    pChan->count = val;
    pitR3IrqTimerUpdate(pDevIns, pThis, pChan, pChan->count_load_time, pChan->count_load_time, false);

    /* log the new rate (ch 0 only). */
    if (pChan->hTimer != NIL_TMTIMERHANDLE /* ch 0 */)
    {
        if (pChan->cRelLogEntries < 32)
        {
            pChan->cRelLogEntries++;
            LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
                    pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100));
        }
        else
            Log(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=0)\n",
                 pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100));
        PDMDevHlpTimerSetFrequencyHint(pDevIns, hTimer, PIT_FREQ / pChan->count);
    }
    else
        Log(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=%d)\n",
             pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100,
             pChan - &pThis->channels[0]));
}

#endif /* IN_RING3 */

/**
 * @callback_method_impl{FNIOMIOPORTNEWIN}
 */
static DECLCALLBACK(VBOXSTRICTRC) pitIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
{
    Log2(("pitIOPortRead: offPort=%#x cb=%x\n", offPort, cb));
    NOREF(pvUser);
    Assert(offPort < 4);
    if (cb != 1 || offPort == 3)
    {
        Log(("pitIOPortRead: offPort=%#x cb=%x *pu32=unused!\n", offPort, cb));
        return VERR_IOM_IOPORT_UNUSED;
    }
    RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia */

    PPITSTATE   pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    PPITCHANNEL pChan = &pThis->channels[offPort];
    int ret;

    DEVPIT_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_READ);
    if (pChan->status_latched)
    {
        pChan->status_latched = 0;
        ret = pChan->status;
        DEVPIT_UNLOCK(pDevIns, pThis);
    }
    else if (pChan->count_latched)
    {
        switch (pChan->count_latched)
        {
            default:
            case RW_STATE_LSB:
                ret = pChan->latched_count & 0xff;
                pChan->count_latched = 0;
                break;
            case RW_STATE_MSB:
                ret = pChan->latched_count >> 8;
                pChan->count_latched = 0;
                break;
            case RW_STATE_WORD0:
                ret = pChan->latched_count & 0xff;
                pChan->count_latched = RW_STATE_MSB;
                break;
        }
        DEVPIT_UNLOCK(pDevIns, pThis);
    }
    else
    {
        DEVPIT_UNLOCK(pDevIns, pThis);
        DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_READ);
        int count;
        switch (pChan->read_state)
        {
            default:
            case RW_STATE_LSB:
                count = pit_get_count(pDevIns, pThis, pChan);
                ret = count & 0xff;
                break;
            case RW_STATE_MSB:
                count = pit_get_count(pDevIns, pThis, pChan);
                ret = (count >> 8) & 0xff;
                break;
            case RW_STATE_WORD0:
                count = pit_get_count(pDevIns, pThis, pChan);
                ret = count & 0xff;
                pChan->read_state = RW_STATE_WORD1;
                break;
            case RW_STATE_WORD1:
                count = pit_get_count(pDevIns, pThis, pChan);
                ret = (count >> 8) & 0xff;
                pChan->read_state = RW_STATE_WORD0;
                break;
        }
        DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
    }

    *pu32 = ret;
    Log2(("pitIOPortRead: offPort=%#x cb=%x *pu32=%#04x\n", offPort, cb, *pu32));
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNIOMIOPORTNEWOUT}
 */
static DECLCALLBACK(VBOXSTRICTRC) pitIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
{
    Log2(("pitIOPortWrite: offPort=%#x cb=%x u32=%#04x\n", offPort, cb, u32));
    NOREF(pvUser);
    Assert(offPort < 4);

    if (cb != 1)
        return VINF_SUCCESS;

    PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    if (offPort == 3)
    {
        /*
         * Port 43h - Mode/Command Register.
         *  7 6 5 4 3 2 1 0
         *  * * . . . . . .  Select channel: 0 0 = Channel 0
         *                                   0 1 = Channel 1
         *                                   1 0 = Channel 2
         *                                   1 1 = Read-back command (8254 only)
         *                                                  (Illegal on 8253)
         *                                                  (Illegal on PS/2 {JAM})
         *  . . * * . . . .  Command/Access mode: 0 0 = Latch count value command
         *                                        0 1 = Access mode: lobyte only
         *                                        1 0 = Access mode: hibyte only
         *                                        1 1 = Access mode: lobyte/hibyte
         *  . . . . * * * .  Operating mode: 0 0 0 = Mode 0, 0 0 1 = Mode 1,
         *                                   0 1 0 = Mode 2, 0 1 1 = Mode 3,
         *                                   1 0 0 = Mode 4, 1 0 1 = Mode 5,
         *                                   1 1 0 = Mode 2, 1 1 1 = Mode 3
         *  . . . . . . . *  BCD/Binary mode: 0 = 16-bit binary, 1 = four-digit BCD
         */
        unsigned channel = (u32 >> 6) & 0x3;
        RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia */
        if (channel == 3)
        {
            /* read-back command */
            DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
            for (channel = 0; channel < RT_ELEMENTS(pThis->channels); channel++)
            {
                PPITCHANNEL pChan = &pThis->channels[channel];
                if (u32 & (2 << channel))
                {
                    if (!(u32 & 0x20))
                        pit_latch_count(pDevIns, pThis, pChan);
                    if (!(u32 & 0x10) && !pChan->status_latched)
                    {
                        /* status latch */
                        /* XXX: add BCD and null count */
                        pChan->status = (pit_get_out1(pDevIns, pThis, pChan,
                                                      PDMDevHlpTimerGet(pDevIns, pThis->channels[0].hTimer)) << 7)
                                      | (pChan->rw_mode << 4)
                                      | (pChan->mode << 1)
                                      | pChan->bcd;
                        pChan->status_latched = 1;
                    }
                }
            }
            DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
        }
        else
        {
            PPITCHANNEL pChan = &pThis->channels[channel];
            unsigned access = (u32 >> 4) & 3;
            if (access == 0)
            {
                DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
                pit_latch_count(pDevIns, pThis, pChan);
                DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
            }
            else
            {
                DEVPIT_LOCK_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
                pChan->rw_mode = access;
                pChan->read_state = access;
                pChan->write_state = access;

                pChan->mode = (u32 >> 1) & 7;
                pChan->bcd = u32 & 1;
                /* XXX: update irq timer ? */
                DEVPIT_UNLOCK(pDevIns, pThis);
            }
        }
    }
    else
    {
#ifndef IN_RING3
        /** @todo There is no reason not to do this in all contexts these
         *        days... */
        return VINF_IOM_R3_IOPORT_WRITE;
#else /* IN_RING3 */
        /*
         * Port 40-42h - Channel Data Ports.
         */
        RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia */
        PPITCHANNEL pChan = &pThis->channels[offPort];
        DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_WRITE);
        switch (pChan->write_state)
        {
            default:
            case RW_STATE_LSB:
                pitR3LoadCount(pDevIns, pThis, pChan, u32);
                break;
            case RW_STATE_MSB:
                pitR3LoadCount(pDevIns, pThis, pChan, u32 << 8);
                break;
            case RW_STATE_WORD0:
                pChan->write_latch = u32;
                pChan->write_state = RW_STATE_WORD1;
                break;
            case RW_STATE_WORD1:
                pitR3LoadCount(pDevIns, pThis, pChan, pChan->write_latch | (u32 << 8));
                pChan->write_state = RW_STATE_WORD0;
                break;
        }
        DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
#endif /* !IN_RING3 */
    }
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNIOMIOPORTNEWIN, Speaker}
 */
static DECLCALLBACK(VBOXSTRICTRC)
pitIOPortSpeakerRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
{
    RT_NOREF(pvUser, offPort);
    if (cb == 1)
    {
        PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
        DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VINF_IOM_R3_IOPORT_READ);

        const uint64_t u64Now = PDMDevHlpTimerGet(pDevIns, pThis->channels[0].hTimer);
        Assert(PDMDevHlpTimerGetFreq(pDevIns, pThis->channels[0].hTimer) == 1000000000); /* lazy bird. */

        /* bit 6,7 Parity error stuff. */
        /* bit 5 - mirrors timer 2 output condition. */
        const int fOut = pit_get_out(pDevIns, pThis, 2, u64Now);
        /* bit 4 - toggled with each (DRAM?) refresh request, every 15.085 u-op Chan.
                   ASSUMES ns timer freq, see assertion above. */
#ifndef FAKE_REFRESH_CLOCK
        const int fRefresh = (u64Now / 15085) & 1;
#else
        pThis->dummy_refresh_clock ^= 1;
        const int fRefresh = pThis->dummy_refresh_clock;
#endif
        /* bit 2,3 NMI / parity status stuff. */
        /* bit 1 - speaker data status */
        const int fSpeakerStatus = pThis->speaker_data_on;
        /* bit 0 - timer 2 clock gate to speaker status. */
        const int fTimer2GateStatus = pit_get_gate(pThis, 2);

        DEVPIT_UNLOCK_BOTH(pDevIns, pThis);

        *pu32 = fTimer2GateStatus
              | (fSpeakerStatus << 1)
              | (fRefresh << 4)
              | (fOut << 5);
        Log(("pitIOPortSpeakerRead: offPort=%#x cb=%x *pu32=%#x\n", offPort, cb, *pu32));
        return VINF_SUCCESS;
    }
    Log(("pitIOPortSpeakerRead: offPort=%#x cb=%x *pu32=unused!\n", offPort, cb));
    return VERR_IOM_IOPORT_UNUSED;
}

#ifdef IN_RING3

/**
 * @callback_method_impl{FNIOMIOPORTNEWOUT, Speaker}
 */
static DECLCALLBACK(VBOXSTRICTRC)
pitR3IOPortSpeakerWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
{
    RT_NOREF(pvUser, offPort);
    if (cb == 1)
    {
        PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
        DEVPIT_LOCK_BOTH_RETURN(pDevIns, pThis, VERR_IGNORED);

        pThis->speaker_data_on = (u32 >> 1) & 1;
        pitR3SetGate(pDevIns, pThis, 2, u32 & 1);

        /** @todo r=klaus move this to a (system-specific) driver, which can
         * abstract the details, and if necessary create a thread to minimize
         * impact on VM execution. */
# ifdef RT_OS_LINUX
        if (pThis->enmSpeakerEmu != PIT_SPEAKER_EMU_NONE)
        {
            PPITCHANNEL pChan = &pThis->channels[2];
            if (pThis->speaker_data_on)
            {
                Log2Func(("starting beep freq=%d\n", PIT_FREQ / pChan->count));
                switch (pThis->enmSpeakerEmu)
                {
                    case PIT_SPEAKER_EMU_CONSOLE:
                    {
                        int res;
                        res = ioctl(pThis->hHostSpeaker, KIOCSOUND, pChan->count);
                        if (res == -1)
                        {
                            LogRel(("PIT: speaker: ioctl failed errno=%d, disabling emulation\n", errno));
                            pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
                        }
                        break;
                    }
                    case PIT_SPEAKER_EMU_EVDEV:
                    {
                        struct input_event e;
                        e.type = EV_SND;
                        e.code = SND_TONE;
                        e.value = PIT_FREQ / pChan->count;
                        int res = write(pThis->hHostSpeaker, &e, sizeof(struct input_event));
                        NOREF(res);
                        break;
                    }
                    case PIT_SPEAKER_EMU_TTY:
                    {
                        int res = write(pThis->hHostSpeaker, "\a", 1);
                        NOREF(res);
                        break;
                    }
                    case PIT_SPEAKER_EMU_NONE:
                        break;
                    default:
                        Log2Func(("unknown speaker emulation %d, disabling emulation\n", pThis->enmSpeakerEmu));
                        pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
                }
            }
            else
            {
                Log2Func(("stopping beep\n"));
                switch (pThis->enmSpeakerEmu)
                {
                    case PIT_SPEAKER_EMU_CONSOLE:
                        /* No error checking here. The Linux device driver
                         * implementation considers it an error (errno=22,
                         * EINVAL) to stop sound if it hasn't been started.
                         * Of course we could detect this by checking only
                         * for enabled->disabled transitions and ignoring
                         * disabled->disabled ones, but it's not worth the
                         * effort. */
                        ioctl(pThis->hHostSpeaker, KIOCSOUND, 0);
                        break;
                    case PIT_SPEAKER_EMU_EVDEV:
                    {
                        struct input_event e;
                        e.type = EV_SND;
                        e.code = SND_TONE;
                        e.value = 0;
                        int res = write(pThis->hHostSpeaker, &e, sizeof(struct input_event));
                        NOREF(res);
                        break;
                    }
                    case PIT_SPEAKER_EMU_TTY:
                        break;
                    case PIT_SPEAKER_EMU_NONE:
                        break;
                    default:
                        Log2Func(("unknown speaker emulation %d, disabling emulation\n", pThis->enmSpeakerEmu));
                        pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_NONE;
                }
            }
        }
# endif /* RT_OS_LINUX */

        DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
    }
    Log(("pitR3IOPortSpeakerWrite: offPort=%#x cb=%x u32=%#x\n", offPort, cb, u32));
    return VINF_SUCCESS;
}


/* -=-=-=-=-=- Saved state -=-=-=-=-=- */

/**
 * @callback_method_impl{FNSSMDEVLIVEEXEC}
 */
static DECLCALLBACK(int) pitR3LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
{
    PPITSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    PCPDMDEVHLPR3 pHlp  = pDevIns->pHlpR3;
    RT_NOREF(uPass);
    pHlp->pfnSSMPutIOPort(pSSM, pThis->IOPortBaseCfg);
    pHlp->pfnSSMPutU8(    pSSM, pThis->channels[0].irq);
    pHlp->pfnSSMPutBool(  pSSM, pThis->fSpeakerCfg);
    return VINF_SSM_DONT_CALL_AGAIN;
}


/**
 * @callback_method_impl{FNSSMDEVSAVEEXEC}
 */
static DECLCALLBACK(int) pitR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    PPITSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    PCPDMDEVHLPR3 pHlp  = pDevIns->pHlpR3;
    int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
    AssertRCReturn(rc, rc);

    /* The config. */
    pitR3LiveExec(pDevIns, pSSM, SSM_PASS_FINAL);

    /* The state. */
    for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
    {
        PPITCHANNEL pChan = &pThis->channels[i];
        pHlp->pfnSSMPutU32(pSSM, pChan->count);
        pHlp->pfnSSMPutU16(pSSM, pChan->latched_count);
        pHlp->pfnSSMPutU8(pSSM, pChan->count_latched);
        pHlp->pfnSSMPutU8(pSSM, pChan->status_latched);
        pHlp->pfnSSMPutU8(pSSM, pChan->status);
        pHlp->pfnSSMPutU8(pSSM, pChan->read_state);
        pHlp->pfnSSMPutU8(pSSM, pChan->write_state);
        pHlp->pfnSSMPutU8(pSSM, pChan->write_latch);
        pHlp->pfnSSMPutU8(pSSM, pChan->rw_mode);
        pHlp->pfnSSMPutU8(pSSM, pChan->mode);
        pHlp->pfnSSMPutU8(pSSM, pChan->bcd);
        pHlp->pfnSSMPutU8(pSSM, pChan->gate);
        pHlp->pfnSSMPutU64(pSSM, pChan->count_load_time);
        pHlp->pfnSSMPutU64(pSSM, pChan->u64NextTS);
        pHlp->pfnSSMPutU64(pSSM, pChan->u64ReloadTS);
        pHlp->pfnSSMPutS64(pSSM, pChan->next_transition_time);
        if (pChan->hTimer != NIL_TMTIMERHANDLE)
            PDMDevHlpTimerSave(pDevIns, pChan->hTimer, pSSM);
    }

    pHlp->pfnSSMPutS32(pSSM, pThis->speaker_data_on);
# ifdef FAKE_REFRESH_CLOCK
    pHlp->pfnSSMPutS32(pSSM, pThis->dummy_refresh_clock);
# else
    pHlp->pfnSSMPutS32(pSSM, 0);
# endif

    pHlp->pfnSSMPutBool(pSSM, pThis->fDisabledByHpet);

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNSSMDEVLOADEXEC}
 */
static DECLCALLBACK(int) pitR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
    PPITSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    PCPDMDEVHLPR3 pHlp  = pDevIns->pHlpR3;
    int           rc;

    if (    uVersion != PIT_SAVED_STATE_VERSION
        &&  uVersion != PIT_SAVED_STATE_VERSION_VBOX_30
        &&  uVersion != PIT_SAVED_STATE_VERSION_VBOX_31)
        return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;

    /* The config. */
    if (uVersion > PIT_SAVED_STATE_VERSION_VBOX_30)
    {
        RTIOPORT IOPortBaseCfg;
        rc = pHlp->pfnSSMGetIOPort(pSSM, &IOPortBaseCfg); AssertRCReturn(rc, rc);
        if (IOPortBaseCfg != pThis->IOPortBaseCfg)
            return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - IOPortBaseCfg: saved=%RTiop config=%RTiop"),
                                    IOPortBaseCfg, pThis->IOPortBaseCfg);

        uint8_t u8Irq;
        rc = pHlp->pfnSSMGetU8(pSSM, &u8Irq); AssertRCReturn(rc, rc);
        if (u8Irq != pThis->channels[0].irq)
            return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - u8Irq: saved=%#x config=%#x"),
                                    u8Irq, pThis->channels[0].irq);

        bool fSpeakerCfg;
        rc = pHlp->pfnSSMGetBool(pSSM, &fSpeakerCfg); AssertRCReturn(rc, rc);
        if (fSpeakerCfg != pThis->fSpeakerCfg)
            return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fSpeakerCfg: saved=%RTbool config=%RTbool"),
                                    fSpeakerCfg, pThis->fSpeakerCfg);
    }

    if (uPass != SSM_PASS_FINAL)
        return VINF_SUCCESS;

    /* The state. */
    for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
    {
        PPITCHANNEL pChan = &pThis->channels[i];
        pHlp->pfnSSMGetU32(pSSM, &pChan->count);
        pHlp->pfnSSMGetU16(pSSM, &pChan->latched_count);
        pHlp->pfnSSMGetU8(pSSM, &pChan->count_latched);
        pHlp->pfnSSMGetU8(pSSM, &pChan->status_latched);
        pHlp->pfnSSMGetU8(pSSM, &pChan->status);
        pHlp->pfnSSMGetU8(pSSM, &pChan->read_state);
        pHlp->pfnSSMGetU8(pSSM, &pChan->write_state);
        pHlp->pfnSSMGetU8(pSSM, &pChan->write_latch);
        pHlp->pfnSSMGetU8(pSSM, &pChan->rw_mode);
        pHlp->pfnSSMGetU8(pSSM, &pChan->mode);
        pHlp->pfnSSMGetU8(pSSM, &pChan->bcd);
        pHlp->pfnSSMGetU8(pSSM, &pChan->gate);
        pHlp->pfnSSMGetU64(pSSM, &pChan->count_load_time);
        pHlp->pfnSSMGetU64(pSSM, &pChan->u64NextTS);
        pHlp->pfnSSMGetU64(pSSM, &pChan->u64ReloadTS);
        pHlp->pfnSSMGetS64(pSSM, &pChan->next_transition_time);
        if (pChan->hTimer != NIL_TMTIMERHANDLE)
        {
            rc = PDMDevHlpTimerLoad(pDevIns, pChan->hTimer, pSSM);
            AssertRCReturn(rc, rc);
            LogRel(("PIT: mode=%d count=%#x (%u) - %d.%02d Hz (ch=%d) (restore)\n",
                    pChan->mode, pChan->count, pChan->count, PIT_FREQ / pChan->count, (PIT_FREQ * 100 / pChan->count) % 100, i));
            rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
            AssertRCReturn(rc, rc);
            PDMDevHlpTimerSetFrequencyHint(pDevIns, pChan->hTimer, PIT_FREQ / pChan->count);
            PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
        }
        pThis->channels[i].cRelLogEntries = 0;
    }

    pHlp->pfnSSMGetS32(pSSM, &pThis->speaker_data_on);
# ifdef FAKE_REFRESH_CLOCK
    pHlp->pfnSSMGetS32(pSSM, &pThis->dummy_refresh_clock);
# else
    int32_t u32Dummy;
    pHlp->pfnSSMGetS32(pSSM, &u32Dummy);
# endif
    if (uVersion > PIT_SAVED_STATE_VERSION_VBOX_31)
        rc = pHlp->pfnSSMGetBool(pSSM, &pThis->fDisabledByHpet);

    return VINF_SUCCESS;
}


/* -=-=-=-=-=- Timer -=-=-=-=-=- */

/**
 * @callback_method_impl{FNTMTIMERDEV, User argument points to the PIT channel state.}
 */
static DECLCALLBACK(void) pitR3Timer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
{
    PPITSTATE   pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    PPITCHANNEL pChan = (PPITCHANNEL)pvUser;
    STAM_PROFILE_ADV_START(&pThis->StatPITHandler, a);
    Assert(hTimer == pChan->hTimer);

    Log(("pitR3Timer\n"));
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    Assert(PDMDevHlpTimerIsLockOwner(pDevIns, hTimer));

    pitR3IrqTimerUpdate(pDevIns, pThis, pChan, pChan->next_transition_time, PDMDevHlpTimerGet(pDevIns, hTimer), true);

    STAM_PROFILE_ADV_STOP(&pThis->StatPITHandler, a);
}


/* -=-=-=-=-=- Debug Info -=-=-=-=-=- */

/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) pitR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    RT_NOREF(pszArgs);
    PPITSTATE   pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    unsigned    i;
    for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
    {
        const PITCHANNEL *pChan = &pThis->channels[i];

        pHlp->pfnPrintf(pHlp,
                        "PIT (i8254) channel %d status: irq=%#x\n"
                        "      count=%08x"      "  latched_count=%04x  count_latched=%02x\n"
                        "           status=%02x   status_latched=%02x     read_state=%02x\n"
                        "      write_state=%02x      write_latch=%02x        rw_mode=%02x\n"
                        "             mode=%02x              bcd=%02x           gate=%02x\n"
                        "  count_load_time=%016RX64 next_transition_time=%016RX64\n"
                        "      u64ReloadTS=%016RX64            u64NextTS=%016RX64\n"
                        ,
                        i, pChan->irq,
                        pChan->count,         pChan->latched_count,     pChan->count_latched,
                        pChan->status,        pChan->status_latched,    pChan->read_state,
                        pChan->write_state,   pChan->write_latch,       pChan->rw_mode,
                        pChan->mode,          pChan->bcd,               pChan->gate,
                        pChan->count_load_time,   pChan->next_transition_time,
                        pChan->u64ReloadTS,       pChan->u64NextTS);
    }
# ifdef FAKE_REFRESH_CLOCK
    pHlp->pfnPrintf(pHlp, "speaker_data_on=%#x dummy_refresh_clock=%#x\n",
                    pThis->speaker_data_on, pThis->dummy_refresh_clock);
# else
    pHlp->pfnPrintf(pHlp, "speaker_data_on=%#x\n", pThis->speaker_data_on);
# endif
    if (pThis->fDisabledByHpet)
        pHlp->pfnPrintf(pHlp, "Disabled by HPET\n");
}


/* -=-=-=-=-=- IHpetLegacyNotify -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMIHPETLEGACYNOTIFY,pfnModeChanged}
 */
static DECLCALLBACK(void) pitR3NotifyHpetLegacyNotify_ModeChanged(PPDMIHPETLEGACYNOTIFY pInterface, bool fActivated)
{
    PPITSTATER3  pThisCC = RT_FROM_MEMBER(pInterface, PITSTATER3, IHpetLegacyNotify);
    PPDMDEVINS   pDevIns = pThisCC->pDevIns;
    PPITSTATE    pThis   = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    int const    rcLock  = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
    PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);

    pThis->fDisabledByHpet = fActivated;

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
}


/* -=-=-=-=-=- PDMDEVINS::IBase -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
 */
static DECLCALLBACK(void *) pitR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
    PPDMDEVINS  pDevIns = RT_FROM_MEMBER(pInterface, PDMDEVINS, IBase);
    PPITSTATER3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PPITSTATER3);
    PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE,    &pDevIns->IBase);
    PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHPETLEGACYNOTIFY, &pThisCC->IHpetLegacyNotify);
    return NULL;
}


/* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMDEVREG,pfnReset}
 */
static DECLCALLBACK(void) pitR3Reset(PPDMDEVINS pDevIns)
{
    PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    LogFlow(("pitR3Reset: \n"));

    DEVPIT_R3_LOCK_BOTH(pDevIns, pThis);

    pThis->fDisabledByHpet = false;

    for (unsigned i = 0; i < RT_ELEMENTS(pThis->channels); i++)
    {
        PPITCHANNEL pChan = &pThis->channels[i];

# if 1 /* Set everything back to virgin state. (might not be strictly correct) */
        pChan->latched_count = 0;
        pChan->count_latched = 0;
        pChan->status_latched = 0;
        pChan->status = 0;
        pChan->read_state = 0;
        pChan->write_state = 0;
        pChan->write_latch = 0;
        pChan->rw_mode = 0;
        pChan->bcd = 0;
# endif
        pChan->u64NextTS = UINT64_MAX;
        pChan->cRelLogEntries = 0;
        pChan->mode = 3;
        pChan->gate = (i != 2);
        pitR3LoadCount(pDevIns, pThis, pChan, 0);
    }

    DEVPIT_UNLOCK_BOTH(pDevIns, pThis);
}

# ifdef RT_OS_LINUX

static int pitR3TryDeviceOpen(const char *pszPath, int flags)
{
    int fd = open(pszPath, flags);
    if (fd == -1)
        LogRel(("PIT: speaker: cannot open \"%s\", errno=%d\n", pszPath, errno));
    else
        LogRel(("PIT: speaker: opened \"%s\"\n", pszPath));
    return fd;
}


static int pitR3TryDeviceOpenSanitizeIoctl(const char *pszPath, int flags)
{
    int fd = open(pszPath, flags);
    if (fd == -1)
        LogRel(("PIT: speaker: cannot open \"%s\", errno=%d\n", pszPath, errno));
    else
    {
        int errno_eviocgsnd0 = 0;
        int errno_kiocsound = 0;
        if (ioctl(fd, EVIOCGSND(0)) == -1)
        {
            errno_eviocgsnd0 = errno;
            if (ioctl(fd, KIOCSOUND, 1) == -1)
                errno_kiocsound = errno;
            else
                ioctl(fd, KIOCSOUND, 0);
        }
        if (errno_eviocgsnd0 && errno_kiocsound)
        {
            LogRel(("PIT: speaker: cannot use \"%s\", ioctl failed errno=%d/errno=%d\n", pszPath, errno_eviocgsnd0, errno_kiocsound));
            close(fd);
            fd = -1;
        }
        else
            LogRel(("PIT: speaker: opened \"%s\"\n", pszPath));
    }
    return fd;
}

# endif /* RT_OS_LINUX */

/**
 * @interface_method_impl{PDMDEVREG,pfnConstruct}
 */
static DECLCALLBACK(int)  pitR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
    PPITSTATE       pThis   = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);
    PPITSTATER3     pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PPITSTATER3);
    PCPDMDEVHLPR3   pHlp    = pDevIns->pHlpR3;
    int             rc;
    uint8_t         u8Irq;
    uint16_t        u16Base;
    bool            fSpeaker;
    unsigned        i;
    Assert(iInstance == 0);

    /*
     * Validate and read the configuration.
     */
    PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "Irq|Base|SpeakerEnabled|PassthroughSpeaker|PassthroughSpeakerDevice", "");

    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "Irq", &u8Irq, 0);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));

    rc = pHlp->pfnCFGMQueryU16Def(pCfg, "Base", &u16Base, 0x40);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"Base\" as a uint16_t failed"));

    rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "SpeakerEnabled", &fSpeaker, true);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"SpeakerEnabled\" as a bool failed"));

    uint8_t uPassthroughSpeaker;
    char *pszPassthroughSpeakerDevice = NULL;
    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "PassthroughSpeaker", &uPassthroughSpeaker, 0);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: failed to read PassthroughSpeaker as uint8_t"));
    if (uPassthroughSpeaker)
    {
        rc = pHlp->pfnCFGMQueryStringAllocDef(pCfg, "PassthroughSpeakerDevice", &pszPassthroughSpeakerDevice, NULL);
        if (RT_FAILURE(rc))
            return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: failed to read PassthroughSpeakerDevice as string"));
    }

    /*
     * Init the data.
     */
    pThis->IOPortBaseCfg   = u16Base;
    pThis->channels[0].irq = u8Irq;
    for (i = 0; i < RT_ELEMENTS(pThis->channels); i++)
    {
        pThis->channels[i].hTimer = NIL_TMTIMERHANDLE;
        pThis->channels[i].iChan  = i;
    }
    pThis->fSpeakerCfg     = fSpeaker;
    pThis->enmSpeakerEmu   = PIT_SPEAKER_EMU_NONE;
    if (uPassthroughSpeaker)
    {
        /** @todo r=klaus move this to a (system-specific) driver */
#ifdef RT_OS_LINUX
        int fd = -1;
        if ((uPassthroughSpeaker == 1 || uPassthroughSpeaker == 100) && fd == -1)
            fd = pitR3TryDeviceOpenSanitizeIoctl("/dev/input/by-path/platform-pcspkr-event-spkr", O_WRONLY);
        if ((uPassthroughSpeaker == 2 || uPassthroughSpeaker == 100) && fd == -1)
            fd = pitR3TryDeviceOpenSanitizeIoctl("/dev/tty", O_WRONLY);
        if ((uPassthroughSpeaker == 3 || uPassthroughSpeaker == 100) && fd == -1)
        {
            fd = pitR3TryDeviceOpenSanitizeIoctl("/dev/tty0", O_WRONLY);
            if (fd == -1)
                fd = pitR3TryDeviceOpenSanitizeIoctl("/dev/vc/0", O_WRONLY);
        }
        if ((uPassthroughSpeaker == 9 || uPassthroughSpeaker == 100) && pszPassthroughSpeakerDevice && fd == -1)
            fd = pitR3TryDeviceOpenSanitizeIoctl(pszPassthroughSpeakerDevice, O_WRONLY);
        if (pThis->enmSpeakerEmu == PIT_SPEAKER_EMU_NONE && fd != -1)
        {
            pThis->hHostSpeaker = fd;
            if (ioctl(fd, EVIOCGSND(0)) != -1)
            {
                pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_EVDEV;
                LogRel(("PIT: speaker: emulation mode evdev\n"));
            }
            else
            {
                pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_CONSOLE;
                LogRel(("PIT: speaker: emulation mode console\n"));
            }
        }
        if ((uPassthroughSpeaker == 70 || uPassthroughSpeaker == 100) && fd == -1)
            fd = pitR3TryDeviceOpen("/dev/tty", O_WRONLY);
        if ((uPassthroughSpeaker == 79 || uPassthroughSpeaker == 100) && pszPassthroughSpeakerDevice && fd == -1)
            fd = pitR3TryDeviceOpen(pszPassthroughSpeakerDevice, O_WRONLY);
        if (pThis->enmSpeakerEmu == PIT_SPEAKER_EMU_NONE && fd != -1)
        {
            pThis->hHostSpeaker = fd;
            pThis->enmSpeakerEmu = PIT_SPEAKER_EMU_TTY;
            LogRel(("PIT: speaker: emulation mode tty\n"));
        }
        if (pThis->enmSpeakerEmu == PIT_SPEAKER_EMU_NONE)
        {
            Assert(fd == -1);
            LogRel(("PIT: speaker: no emulation possible\n"));
        }
#else
        LogRel(("PIT: speaker: emulation deactivated\n"));
#endif
        if (pszPassthroughSpeakerDevice)
        {
            PDMDevHlpMMHeapFree(pDevIns, pszPassthroughSpeakerDevice);
            pszPassthroughSpeakerDevice = NULL;
        }
    }

    /*
     * Interfaces
     */
    /* IBase */
    pDevIns->IBase.pfnQueryInterface          = pitR3QueryInterface;
    /* IHpetLegacyNotify */
    pThisCC->IHpetLegacyNotify.pfnModeChanged = pitR3NotifyHpetLegacyNotify_ModeChanged;
    pThisCC->pDevIns                          = pDevIns;

    /*
     * We do our own locking.  This must be done before creating timers.
     */
    rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "pit#%u", iInstance);
    AssertRCReturn(rc, rc);

    rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
    AssertRCReturn(rc, rc);

    /*
     * Create the timer, make it take our critsect.
     */
    rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, pitR3Timer, &pThis->channels[0],
                              TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_RING0, "i8254 PIT", &pThis->channels[0].hTimer);
    AssertRCReturn(rc, rc);
    rc = PDMDevHlpTimerSetCritSect(pDevIns, pThis->channels[0].hTimer, &pThis->CritSect);
    AssertRCReturn(rc, rc);

    /*
     * Register I/O ports.
     */
    rc = PDMDevHlpIoPortCreateAndMap(pDevIns, u16Base, 4 /*cPorts*/, pitIOPortWrite, pitIOPortRead,
                                     "i8254 Programmable Interval Timer", NULL /*paExtDescs*/, &pThis->hIoPorts);
    AssertRCReturn(rc, rc);

    if (fSpeaker)
    {
        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, 0x61, 1 /*cPorts*/, pitR3IOPortSpeakerWrite, pitIOPortSpeakerRead,
                                         "PC Speaker", NULL /*paExtDescs*/, &pThis->hIoPortSpeaker);
        AssertRCReturn(rc, rc);
    }

    /*
     * Saved state.
     */
    rc = PDMDevHlpSSMRegister3(pDevIns, PIT_SAVED_STATE_VERSION, sizeof(*pThis), pitR3LiveExec, pitR3SaveExec, pitR3LoadExec);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Initialize the device state.
     */
    pitR3Reset(pDevIns);

    /*
     * Register statistics and debug info.
     */
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITIrq,      STAMTYPE_COUNTER, "/TM/PIT/Irq",      STAMUNIT_OCCURENCES,     "The number of times a timer interrupt was triggered.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPITHandler,  STAMTYPE_PROFILE, "/TM/PIT/Handler",  STAMUNIT_TICKS_PER_CALL, "Profiling timer callback handler.");

    PDMDevHlpDBGFInfoRegister(pDevIns, "pit", "Display PIT (i8254) status. (no arguments)", pitR3Info);

    return VINF_SUCCESS;
}

#else  /* !IN_RING3 */

/**
 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
 */
static DECLCALLBACK(int) picRZConstruct(PPDMDEVINS pDevIns)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
    PPITSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PPITSTATE);

    int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
    AssertRCReturn(rc, rc);

    rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPorts, pitIOPortWrite, pitIOPortRead, NULL /*pvUser*/);
    AssertRCReturn(rc, rc);

    rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortSpeaker, NULL /*pfnWrite*/, pitIOPortSpeakerRead, NULL /*pvUser*/);
    AssertRCReturn(rc, rc);

    return VINF_SUCCESS;
}

#endif /* !IN_RING3 */

/**
 * The device registration structure.
 */
const PDMDEVREG g_DeviceI8254 =
{
    /* .u32Version = */             PDM_DEVREG_VERSION,
    /* .uReserved0 = */             0,
    /* .szName = */                 "i8254",
    /* .fFlags = */                 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
    /* .fClass = */                 PDM_DEVREG_CLASS_PIT,
    /* .cMaxInstances = */          1,
    /* .uSharedVersion = */         42,
    /* .cbInstanceShared = */       sizeof(PITSTATE),
    /* .cbInstanceCC = */           CTX_EXPR(sizeof(PITSTATER3), 0, 0),
    /* .cbInstanceRC = */           0,
    /* .cMaxPciDevices = */         0,
    /* .cMaxMsixVectors = */        0,
    /* .pszDescription = */         "Intel 8254 Programmable Interval Timer (PIT) And Dummy Speaker Device",
#if defined(IN_RING3)
    /* .pszRCMod = */               "VBoxDDRC.rc",
    /* .pszR0Mod = */               "VBoxDDR0.r0",
    /* .pfnConstruct = */           pitR3Construct,
    /* .pfnDestruct = */            NULL,
    /* .pfnRelocate = */            NULL,
    /* .pfnMemSetup = */            NULL,
    /* .pfnPowerOn = */             NULL,
    /* .pfnReset = */               pitR3Reset,
    /* .pfnSuspend = */             NULL,
    /* .pfnResume = */              NULL,
    /* .pfnAttach = */              NULL,
    /* .pfnDetach = */              NULL,
    /* .pfnQueryInterface = */      NULL,
    /* .pfnInitComplete = */        NULL,
    /* .pfnPowerOff = */            NULL,
    /* .pfnSoftReset = */           NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#elif defined(IN_RING0)
    /* .pfnEarlyConstruct = */      NULL,
    /* .pfnConstruct = */           picRZConstruct,
    /* .pfnDestruct = */            NULL,
    /* .pfnFinalDestruct = */       NULL,
    /* .pfnRequest = */             NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#elif defined(IN_RC)
    /* .pfnConstruct = */           picRZConstruct,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#else
# error "Not in IN_RING3, IN_RING0 or IN_RC!"
#endif
    /* .u32VersionEnd = */          PDM_DEVREG_VERSION
};

#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */