summaryrefslogtreecommitdiffstats
path: root/private/mvdm/v86/monitor/i386/monitor.c
blob: fded8dac01b3dc694def68bea7a4b1b71b6aa2b4 (plain) (blame)
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
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    Monitor.c
Abstract:

    This module is the user mode portion of the x86 monitor

Author:

    Dave Hastings (daveh) 16 Mar 1991

Environment:

    User mode only

Revision History:
    Sudeep Bharati (sudeepb) 31-Dec-1991

    Converted all register manipulation interfaces to functions
    from macros. This is to make ntvdm an exe as well as a dll,
    and these register oriented routines are exported from ntvdm
    for WOW32 and other installable VDDs.

    Dave Hastings (daveh) 18-Apr-1992

    Split into multiple files. Track current monitor thread by
    Teb pointer.  Register initial thread.

    Sudeep Bharati (sudeepb) 22-Sep-1992

    Added Page Fault Handling For installable VDD support

--*/


#include "monitorp.h"


//
// Internal functions
//

VOID
EventVdmIo(
    VOID
    );

VOID
EventVdmStringIo(
    VOID
    );

VOID
EventVdmMemAccess(
    VOID
    );

VOID
EventVdmIntAck(
    VOID
    );

VOID
EventVdmBop(
    VOID
    );

VOID
EventVdmError(
    VOID
    );

VOID
EventVdmIrq13(
    VOID
    );

VOID
CreateProfile(
    VOID
    );

VOID
StartProfile(
    VOID
    );

VOID
StopProfile(
    VOID
    );

VOID
AnalyzeProfile(
    VOID
    );

// [LATER]  how do you prevent a struct from straddling a page boundary?
VDM_TIB VdmTib;

ULONG   IntelBase;          // base memory address
ULONG   VdmSize;            // Size of memory in VDM
ULONG   IntelMSW;           // Msw value (no msw in context)
ULONG   VdmDebugLevel;      // used to control debugging
PVOID  CurrentMonitorTeb;   // thread that is currently executing instructions.
ULONG InitialBreakpoint = FALSE; // if set, breakpoint at end of cpu_init
ULONG InitialVdmTibFlags = 0; // VdmTib flags picked up from here
CONTEXT InitialContext;     // Initial context for all threads
BOOLEAN DebugContextActive = FALSE;
ULONG VdmFeatureBits = 0;   // bit to indicate special features

extern PVOID NTVDMpLockPrefixTable;

IMAGE_LOAD_CONFIG_DIRECTORY _load_config_used = {
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    0,                          // GlobalFlagsClear
    0,                          // GlobalFlagsSet
    0,                          // CriticalSectionTimeout (milliseconds)
    0,                          // DeCommitFreeBlockThreshold
    0,                          // DeCommitTotalFreeThreshold
    &NTVDMpLockPrefixTable,     // LockPrefixTable, defined in FASTPM.ASM
    0, 0, 0, 0, 0, 0, 0         // Reserved
};

// Bop dispatch table

extern void (*BIOS[])();

BOOLEAN ContinueExecution;

//
// Event Dispatch table
//

VOID (*EventDispatch[VdmMaxEvent])(VOID) = {
        EventVdmIo,
        EventVdmStringIo,
        EventVdmMemAccess,
        EventVdmIntAck,
        EventVdmBop,
        EventVdmError,
        EventVdmIrq13
        };

// Debug control flags
BOOLEAN fShowBop = FALSE;
#if DBG
BOOLEAN fBreakInDebugger = FALSE;
LONG NumTasks = -1;
#endif



EXPORT
VOID
cpu_init(
    )

/*++

Routine Description:

    This routine is used to prepare the IEU for instruction simulation.
    It will set the Intel registers to thier initial value, and perform
    any implementation specific initialization necessary.


Arguments:


Return Value:

    None.

--*/

{
    NTSTATUS Status;

    IntelMSW = 0x0;             // bugbug use correct value for ET and MP
    InitialVdmTibFlags |= RM_BIT_MASK;

    VdmTib.VdmContext.SegGs = 0;
    VdmTib.VdmContext.SegFs = 0;
    VdmTib.VdmContext.SegEs = 0;
    VdmTib.VdmContext.SegDs = 0;
    VdmTib.VdmContext.SegCs = 0;
    VdmTib.VdmContext.Eip = 0xFFF0L;
    VdmTib.VdmContext.EFlags = 0x02L | EFLAGS_INTERRUPT_MASK;

    VdmTib.MonitorContext.SegDs = KGDT_R3_DATA | RPL_MASK;
    VdmTib.MonitorContext.SegEs = KGDT_R3_DATA | RPL_MASK;
    VdmTib.MonitorContext.SegGs = 0;
    VdmTib.MonitorContext.SegFs = KGDT_R3_TEB | RPL_MASK;

    VdmTib.PrinterInfo.prt_State       = NULL;
    VdmTib.PrinterInfo.prt_Control     = NULL;
    VdmTib.PrinterInfo.prt_Status      = NULL;
    VdmTib.PrinterInfo.prt_HostState   = NULL;
    ASSERT(VDM_NUMBER_OF_LPT == 3);

    VdmTib.PrinterInfo.prt_Mode[0] =
    VdmTib.PrinterInfo.prt_Mode[1] =
    VdmTib.PrinterInfo.prt_Mode[2] = PRT_MODE_NO_SIMULATION;

    VdmTib.Size = sizeof(VDM_TIB);

    //
    // Find out if we are running with IOPL.  We call the kernel
    // rather than checking the registry ourselves, so that we can
    // insure that both the kernel and ntvdm.exe agree.  If they didn't,
    // it would result in unnecssary trapping instructions.  Whether or
    // not Vdms run with IOPL only changes on reboot
    //
    Status = NtVdmControl(VdmFeatures, &VdmFeatureBits);

#if DBG
    if (!NT_SUCCESS(Status)) {
        DbgPrint(
            "NTVDM: Could not find out whether to use IOPL, %lx\n",
            Status
            );
    }
#endif

    //
    // If we have fast v86 mode IF emulation set the bit that tells
    // the 16 bit IF macros they know.
    //
    if (VdmFeatureBits & V86_VIRTUAL_INT_EXTENSIONS) {
        InitialVdmTibFlags |= RI_BIT_MASK;
    }

    *pNtVDMState = InitialVdmTibFlags;

    // Switch the npx back to 80 bit mode.  Win32 apps start with
    // 64-bit precision for compatibility across platforms, but
    // DOS and Win16 apps expect 80 bit precision.
    //

    _asm fninit;

    //
    // We setup the InitialContext structure with the correct floating
    // point and debug register configuration, and cpu_createthread
    // uses this context to configure each 16-bit thread's floating
    // point and debug registers.
    //

    InitialContext.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS;

    Status = NtGetContextThread(
        NtCurrentThread(),
        &InitialContext
        );

    if (!NT_SUCCESS(Status)) {
#if DBG
        DbgPrint("NtVdm terminating : Could not get float/debug context for\n"
                 "                    initial thread, status %lx\n", Status);
        DbgBreakPoint();
#endif
        TerminateVDM();
    }


    //
    //
    // Turn OFF em bit so that dos apps will work correctly.
    //
    // On machines without 387's the floating point flag will have been
    // cleared.
    //

    InitialContext.ContextFlags = CONTEXT_FLOATING_POINT;
    InitialContext.FloatSave.Cr0NpxState &= ~0x6; // CR0_EM | CR0_MP

    //
    // Do the rest of thread initialization
    //
    cpu_createthread( NtCurrentThread() );

    InterruptInit();

    if (InitialBreakpoint) {
        DbgBreakPoint();
    }

}

EXPORT
VOID
cpu_terminate(
    )
/*++

Routine Description:


Arguments:


Return Value:


--*/
{
    InterruptTerminate();
}

EXPORT
VOID
cpu_simulate(
    )

/*++

Routine Description:

    This routine causes the simulation of intel instructions to start.

Arguments:

Return Value:

    None.

--*/

{
    NTSTATUS Status;
    VDMEVENTINFO OldEventInfo;
    CONTEXT OldMonitorContext;

    OldEventInfo = VdmTib.EventInfo;
    OldMonitorContext = VdmTib.MonitorContext;

    ContinueExecution = TRUE;

    CurrentMonitorTeb = NtCurrentTeb();

    VdmTib.VdmContext.ContextFlags = CONTEXT_FULL;

    while (ContinueExecution) {

        ASSERT(CurrentMonitorTeb == NtCurrentTeb());
        ASSERT(InterlockedIncrement(&NumTasks) == 0);

        if (*pNtVDMState & VDM_INTERRUPT_PENDING) {
            DispatchInterrupts();
        }

        // translate MSW bits into EFLAGS
        if ( getMSW() & MSW_PE ) {
            VdmTib.VdmContext.EFlags &= ~EFLAGS_V86_MASK;
            Status = FastEnterPm();
        } else {
            VdmTib.VdmContext.EFlags |= EFLAGS_V86_MASK;
            Status = NtVdmControl(VdmStartExecution,NULL);
        }

        if (!NT_SUCCESS(Status)) {
#if DBG
            DbgPrint("NTVDM: Could not start execution\n");
#endif
            return;
        }

        ASSERT(InterlockedDecrement(&NumTasks) < 0);

#if DBG
        if (fBreakInDebugger) {
            fBreakInDebugger = 0;
            DbgBreakPoint();
        }
#endif

        // Translate Eflags value
        ASSERT ((!((VdmTib.VdmContext.EFlags & EFLAGS_V86_MASK) &&
            (getMSW() & MSW_PE))));

        if ( VdmTib.VdmContext.EFlags & EFLAGS_V86_MASK ) {
            VdmTib.VdmContext.EFlags &= ~EFLAGS_V86_MASK;
        }

        // bugbug does cs:eip wrap cause some kind of fault?
        VdmTib.VdmContext.Eip += VdmTib.EventInfo.InstructionSize;

        if (VdmTib.EventInfo.Event >= VdmMaxEvent) {
#if DBG
            DbgPrint("NTVDM: Unknown event type\n");
            DbgBreakPoint();
#endif
            ContinueExecution = FALSE;
            continue;
        }

        (*EventDispatch[VdmTib.EventInfo.Event])();

    }


    // set this back to true incase we are nested
    ContinueExecution = TRUE;

    //
    // Restore the old Vdm tib info.  This is necessary for the for the
    // case where the application thread is suspended, and a host simulate is
    // performed from another thread
    //

    VdmTib.EventInfo = OldEventInfo;
    VdmTib.MonitorContext = OldMonitorContext;
}


VOID
host_unsimulate(
    )

/*++

Routine Description:

    This routine causes execution of instructions in a VDM to stop.

Arguments:


Return Value:

    None.

--*/

{

    ContinueExecution = FALSE;

}


VOID
EventVdmIo(
    VOID
    )
/*++

Routine Description:

    This function calls the appropriate io simulation routine.

Arguments:


Return Value:

    None.

--*/
{
    if (VdmTib.EventInfo.IoInfo.Size == 1) {
        if (VdmTib.EventInfo.IoInfo.Read) {
            inb(VdmTib.EventInfo.IoInfo.PortNumber,(half_word *)&(VdmTib.VdmContext.Eax));
        } else {
            outb(VdmTib.EventInfo.IoInfo.PortNumber,getAL());
        }
    } else if (VdmTib.EventInfo.IoInfo.Size == 2) {
        if (VdmTib.EventInfo.IoInfo.Read) {
            inw(VdmTib.EventInfo.IoInfo.PortNumber,(word *)&(VdmTib.VdmContext.Eax));
        } else {
            outw(VdmTib.EventInfo.IoInfo.PortNumber,getAX());
        }
    }
#if DBG
    else {
    DbgPrint(
        "NtVdm: Unimplemented IO size %d\n",
        VdmTib.EventInfo.IoInfo.Size
        );
    DbgBreakPoint();
    }
#endif
}

VOID
EventVdmStringIo(
    VOID
    )
/*++

Routine Description:

    This function calls the appropriate io simulation routine.

Arguments:


Return Value:

    None.

--*/
{
   PVDMSTRINGIOINFO pvsio;
   PUSHORT pIndexRegister;
   USHORT Index;

   // WARNING no 32 bit address support

    pvsio = &VdmTib.EventInfo.StringIoInfo;

    if (pvsio->Size == 1) {
        if (pvsio->Read) {
            insb((io_addr)pvsio->PortNumber,
                 (half_word *)Sim32GetVDMPointer(pvsio->Address, 1, ISPESET),
                 (word)pvsio->Count
                 );
            pIndexRegister = (PUSHORT)&VdmTib.VdmContext.Edi;
        } else {
            outsb((io_addr)pvsio->PortNumber,
                 (half_word *)Sim32GetVDMPointer(pvsio->Address,1,ISPESET),
                 (word)pvsio->Count
                 );
            pIndexRegister = (PUSHORT)&VdmTib.VdmContext.Esi;
        }
    } else if (pvsio->Size == 2) {
        if (pvsio->Read) {
            insw((io_addr)pvsio->PortNumber,
                 (word *)Sim32GetVDMPointer(pvsio->Address,1,ISPESET),
                 (word)pvsio->Count
                 );
            pIndexRegister = (PUSHORT)&VdmTib.VdmContext.Edi;
        } else {
            outsw((io_addr)pvsio->PortNumber,
                 (word *)Sim32GetVDMPointer(pvsio->Address,1,ISPESET),
                 (word)pvsio->Count
                 );
            pIndexRegister = (PUSHORT)&VdmTib.VdmContext.Esi;
        }
    } else {
#if DBG
         DbgPrint(
             "NtVdm: Unimplemented IO size %d\n",
             VdmTib.EventInfo.IoInfo.Size
             );
         DbgBreakPoint();
#endif
        return;
    }

    if (getDF()) {
        Index = *pIndexRegister - (USHORT)(pvsio->Count * pvsio->Size);
        }
    else {
        Index = *pIndexRegister + (USHORT)(pvsio->Count * pvsio->Size);
        }

    *pIndexRegister = Index;

    if (pvsio->Rep) {
        (USHORT)VdmTib.VdmContext.Ecx = 0;
        }


}

VOID
EventVdmIntAck(
    VOID
    )
/*++

Routine Description:

    This routine is called each time we have returned to monitor context
    to dispatch interrupts. Its function is to check for AutoEoi and call
    the ica to do a nonspecific eoi, when the ica adapter is in AEOI mode.

Arguments:


Return Value:

    None.

--*/
{
    int line;
    int adapter;

    if (VdmTib.EventInfo.IntAckInfo) {
        if (VdmTib.EventInfo.IntAckInfo & VDMINTACK_SLAVE)
            adapter = 1;
        else
            adapter = 0;
        line = -1;

        host_ica_lock();
        ica_eoi(adapter,
                &line,
                (int)(VdmTib.EventInfo.IntAckInfo & VDMINTACK_RAEOIMASK)
                );
        host_ica_unlock();
        }
}


VOID
EventVdmBop(
    VOID
    )
/*++

Routine Description:

    This routine dispatches to the appropriate bop handler

Arguments:


Return Value:

    None.

--*/
{
    if (VdmTib.EventInfo.BopNumber > MAX_BOP) {
#if DBG
        DbgPrint(
            "NtVdm: Invalid BOP %lx\n",
            VdmTib.EventInfo.BopNumber
            );
#endif
         ContinueExecution = FALSE;
    } else {
#if DBG
       if (fShowBop) {
       DbgPrint("Ntvdm cpu_simulate : bop dispatch %x,%x\n",
           VdmTib.EventInfo.BopNumber,
           (ULONG)(*((UCHAR *)Sim32GetVDMPointer(
               (VdmTib.VdmContext.SegCs << 16) | VdmTib.VdmContext.Eip,
               1,
               ISPESET)))
           );
       }
#endif
       (*BIOS[VdmTib.EventInfo.BopNumber])();
       CurrentMonitorTeb = NtCurrentTeb();
   }
}

VOID
EventVdmError(
    VOID
    )
/*++

Routine Description:

    This routine prints a message(debug only), and exits the vdm

Arguments:


Return Value:

    None.

--*/
{
#if DBG
    DbgPrint(
        "NtVdm: Error code %lx\n",
        VdmTib.EventInfo.ErrorStatus
        );
    DbgBreakPoint();
#endif
    TerminateVDM();
    ContinueExecution = FALSE;
}

VOID
EventVdmIrq13(
    VOID
    )
/*++

Routine Description:

    This routine simulates an IRQ 13 to the vdm

Arguments:


Return Value:

    None.

--*/
{
    if (!IRQ13BeingHandled) {
        IRQ13BeingHandled = TRUE;
        ica_hw_interrupt(
            ICA_SLAVE,
            5,
            1
            );
    }
}


VOID
EventVdmMemAccess(
    VOID
    )
/*++

Routine Description:

    This routine will call the page fault handler routine which
    is common to both x86 and mips.

Arguments:


Return Value:

    None.

--*/
{

    // RWMode is 0 if read fault or 1 if write fault.

    DispatchPageFault(
        VdmTib.EventInfo.FaultInfo.FaultAddr,
        VdmTib.EventInfo.FaultInfo.RWMode
        );
    CurrentMonitorTeb = NtCurrentTeb();
}

// Get and Set routines for intel registers.

ULONG  getEAX (VOID) { return  (VdmTib.VdmContext.Eax); }
USHORT getAX  (VOID) { return  ((USHORT)(VdmTib.VdmContext.Eax)); }
UCHAR  getAL  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Eax)); }
UCHAR  getAH  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Eax >> 8)); }
ULONG  getEBX (VOID) { return  (VdmTib.VdmContext.Ebx); }
USHORT getBX  (VOID) { return  ((USHORT)(VdmTib.VdmContext.Ebx)); }
UCHAR  getBL  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Ebx)); }
UCHAR  getBH  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Ebx >> 8)); }
ULONG  getECX (VOID) { return  (VdmTib.VdmContext.Ecx); }
USHORT getCX  (VOID) { return  ((USHORT)(VdmTib.VdmContext.Ecx)); }
UCHAR  getCL  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Ecx)); }
UCHAR  getCH  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Ecx >> 8)); }
ULONG  getEDX (VOID) { return  (VdmTib.VdmContext.Edx); }
USHORT getDX  (VOID) { return  ((USHORT)(VdmTib.VdmContext.Edx)); }
UCHAR  getDL  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Edx)); }
UCHAR  getDH  (VOID) { return  ((BYTE)(VdmTib.VdmContext.Edx >> 8)); }
ULONG  getESP (VOID) { return  (VdmTib.VdmContext.Esp); }
USHORT getSP  (VOID) { return  ((USHORT)VdmTib.VdmContext.Esp); }
ULONG  getEBP (VOID) { return  (VdmTib.VdmContext.Ebp); }
USHORT getBP  (VOID) { return  ((USHORT)VdmTib.VdmContext.Ebp); }
ULONG  getESI (VOID) { return  (VdmTib.VdmContext.Esi); }
USHORT getSI  (VOID) { return  ((USHORT)VdmTib.VdmContext.Esi); }
ULONG  getEDI (VOID) { return  (VdmTib.VdmContext.Edi); }
USHORT getDI  (VOID) { return  ((USHORT)VdmTib.VdmContext.Edi); }
ULONG  getEIP (VOID) { return  (VdmTib.VdmContext.Eip); }
USHORT getIP (VOID)  { return  ((USHORT)VdmTib.VdmContext.Eip); }
USHORT getCS (VOID)  { return  ((USHORT)VdmTib.VdmContext.SegCs); }
USHORT getSS (VOID)  { return  ((USHORT)VdmTib.VdmContext.SegSs); }
USHORT getDS (VOID)  { return  ((USHORT)VdmTib.VdmContext.SegDs); }
USHORT getES (VOID)  { return  ((USHORT)VdmTib.VdmContext.SegEs); }
USHORT getFS (VOID)  { return  ((USHORT)VdmTib.VdmContext.SegFs); }
USHORT getGS (VOID)  { return  ((USHORT)VdmTib.VdmContext.SegGs); }
ULONG  getCF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_CARRY) ? 1 : 0); }
ULONG  getPF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_PARITY) ? 1 : 0); }
ULONG  getAF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_AUXILIARY) ? 1 : 0); }
ULONG  getZF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_ZERO) ? 1 : 0); }
ULONG  getSF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_SIGN) ? 1 : 0); }
ULONG  getTF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_TRAP) ? 1 : 0); }
ULONG  getIF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_INTERRUPT) ? 1 : 0); }
ULONG  getDF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_DIRECTION) ? 1 : 0); }
ULONG  getOF (VOID)  { return  ((VdmTib.VdmContext.EFlags & FLG_OVERFLOW) ? 1 : 0); }
USHORT getMSW (VOID) { return  ((USHORT)IntelMSW); }
USHORT getSTATUS(VOID){ return (USHORT)VdmTib.VdmContext.EFlags; }
ULONG  getEFLAGS(VOID) { return VdmTib.VdmContext.EFlags; }
USHORT getFLAGS(VOID) { return (USHORT)VdmTib.VdmContext.EFlags; }

VOID setEAX (ULONG val) {
    VdmTib.VdmContext.Eax = val;
}

VOID setAX  (USHORT val) {
    VdmTib.VdmContext.Eax = (VdmTib.VdmContext.Eax & 0xFFFF0000) |
                            ((ULONG)val & 0x0000FFFF);
}

VOID setAH  (UCHAR val) {
    VdmTib.VdmContext.Eax = (VdmTib.VdmContext.Eax & 0xFFFF00FF) |
                            ((ULONG)(val << 8) & 0x0000FF00);
}

VOID setAL  (UCHAR val) {
    VdmTib.VdmContext.Eax = (VdmTib.VdmContext.Eax & 0xFFFFFF00) |
                            ((ULONG)val & 0x000000FF);
}

VOID setEBX (ULONG val) {
    VdmTib.VdmContext.Ebx = val ;
}

VOID setBX  (USHORT val) {
    VdmTib.VdmContext.Ebx = (VdmTib.VdmContext.Ebx & 0xFFFF0000) |
                            ((ULONG)val & 0x0000FFFF);
}

VOID setBH  (UCHAR val) {
    VdmTib.VdmContext.Ebx = (VdmTib.VdmContext.Ebx & 0xFFFF00FF) |
                            ((ULONG)(val << 8) & 0x0000FF00);
}

VOID setBL  (UCHAR  val) {
    VdmTib.VdmContext.Ebx = (VdmTib.VdmContext.Ebx & 0xFFFFFF00) |
                            ((ULONG)val & 0x000000FF);
}

VOID setECX (ULONG val) {
    VdmTib.VdmContext.Ecx = val ;
}

VOID setCX  (USHORT val) {
    VdmTib.VdmContext.Ecx = (VdmTib.VdmContext.Ecx & 0xFFFF0000) |
                            ((ULONG)val & 0x0000FFFF);
}

VOID setCH  (UCHAR val) {
    VdmTib.VdmContext.Ecx = (VdmTib.VdmContext.Ecx & 0xFFFF00FF) |
                            ((ULONG)(val << 8) & 0x0000FF00);
}

VOID setCL  (UCHAR val) {
    VdmTib.VdmContext.Ecx = (VdmTib.VdmContext.Ecx & 0xFFFFFF00) |
                            ((ULONG)val & 0x000000FF);
}

VOID setEDX (ULONG val) {
    VdmTib.VdmContext.Edx = val ;
}

VOID setDX  (USHORT val) {
    VdmTib.VdmContext.Edx = (VdmTib.VdmContext.Edx & 0xFFFF0000) |
                            ((ULONG)val & 0x0000FFFF);
}

VOID setDH  (UCHAR val) {
    VdmTib.VdmContext.Edx = (VdmTib.VdmContext.Edx & 0xFFFF00FF) |
                            ((ULONG)(val << 8) & 0x0000FF00);
}

VOID setDL  (UCHAR val) {
    VdmTib.VdmContext.Edx = (VdmTib.VdmContext.Edx & 0xFFFFFF00) |
                                ((ULONG)val & 0x000000FF);
}

VOID setESP (ULONG val) {
    VdmTib.VdmContext.Esp = val ;
}

VOID setSP  (USHORT val) {
    VdmTib.VdmContext.Esp = (VdmTib.VdmContext.Esp & 0xFFFF0000) |
                                ((ULONG)val & 0x0000FFFF);
}

VOID setEBP (ULONG val) {
    VdmTib.VdmContext.Ebp = val;
}

VOID setBP  (USHORT val) {
    VdmTib.VdmContext.Ebp = (VdmTib.VdmContext.Ebp & 0xFFFF0000) |
                                ((ULONG)val & 0x0000FFFF);
}

VOID setESI (ULONG val) {
    VdmTib.VdmContext.Esi = val ;
}

VOID setSI  (USHORT val) {
    VdmTib.VdmContext.Esi = (VdmTib.VdmContext.Esi & 0xFFFF0000) |
                                ((ULONG)val & 0x0000FFFF);
}
VOID setEDI (ULONG val) {
    VdmTib.VdmContext.Edi = val ;
}

VOID setDI  (USHORT val) {
    VdmTib.VdmContext.Edi = (VdmTib.VdmContext.Edi & 0xFFFF0000) |
                                ((ULONG)val & 0x0000FFFF);
}

VOID setEIP (ULONG val) {
    VdmTib.VdmContext.Eip = val ;
}

VOID setIP  (USHORT val) {
    VdmTib.VdmContext.Eip = (VdmTib.VdmContext.Eip & 0xFFFF0000) |
                                ((ULONG)val & 0x0000FFFF);
}

VOID setCS  (USHORT val) {
    VdmTib.VdmContext.SegCs = (ULONG) val & 0x0000FFFF ;
}

VOID setSS  (USHORT val) {
    VdmTib.VdmContext.SegSs = (ULONG) val & 0x0000FFFF ;
}

VOID setDS  (USHORT val) {
    VdmTib.VdmContext.SegDs = (ULONG) val & 0x0000FFFF ;
}

VOID setES  (USHORT val) {
    VdmTib.VdmContext.SegEs = (ULONG) val & 0x0000FFFF ;
}

VOID setFS  (USHORT val) {
    VdmTib.VdmContext.SegFs = (ULONG) val & 0x0000FFFF ;
}

VOID setGS  (USHORT val) {
    VdmTib.VdmContext.SegGs = (ULONG) val & 0x0000FFFF ;
}

VOID setCF  (ULONG val)  {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_CARRY) |
                                (((ULONG)val << FLG_CARRY_BIT) & FLG_CARRY);
}

VOID setPF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_PARITY) |
                                (((ULONG)val << FLG_PARITY_BIT) & FLG_PARITY);
}

VOID setAF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_AUXILIARY) |
                                (((ULONG)val << FLG_AUXILIARY_BIT) & FLG_AUXILIARY);
}

VOID setZF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_ZERO) |
                                (((ULONG)val << FLG_ZERO_BIT) & FLG_ZERO);
}

VOID setSF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_SIGN) |
                                (((ULONG)val << FLG_SIGN_BIT) & FLG_SIGN);
}

VOID setIF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_INTERRUPT) |
                                (((ULONG)val << FLG_INTERRUPT_BIT) & FLG_INTERRUPT);
}

VOID setDF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_DIRECTION) |
                                (((ULONG)val << FLG_DIRECTION_BIT) & FLG_DIRECTION);
}

VOID setOF  (ULONG val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & ~FLG_OVERFLOW) |
                                (((ULONG)val << FLG_OVERFLOW_BIT) & FLG_OVERFLOW);
}

VOID setMSW (USHORT val) {
    IntelMSW = val ;
}

VOID setSTATUS(USHORT val) {
    VdmTib.VdmContext.EFlags = (VdmTib.VdmContext.EFlags & 0xFFFF0000) | val;
}
//
// The following is a private register function
//

ULONG getPE(){
    return((IntelMSW & MSW_PE) ? 1 : 0);
}


PX86CONTEXT
getIntelRegistersPointer(
    VOID
    )
/*++

Routine Description:

    Return Address on Intel Registers for WOW Fast Access

Arguments:

    None

Return Value:

    Pointer to Intel Registers x86 Context Record


--*/
{
    return &(VdmTib.VdmContext);
}



BOOLEAN MonitorInitializePrinterInfo(
     WORD   Ports,
     PWORD  PortTable,
     PUCHAR State,
     PUCHAR Control,
     PUCHAR Status,
     PUCHAR HostState)
{
    int     i;

    ASSERT (Ports == 3);
    ASSERT (Status != NULL);

    // only do this if the structure has not been initialized -- meaning
    // the pointers can be set once.
    if (NULL == VdmTib.PrinterInfo.prt_Status) {

	VdmTib.PrinterInfo.prt_PortAddr[0] = PortTable[0];
	VdmTib.PrinterInfo.prt_PortAddr[1] = PortTable[1];
	VdmTib.PrinterInfo.prt_PortAddr[2] = PortTable[2];

	VdmTib.PrinterInfo.prt_Handle[0] =
	VdmTib.PrinterInfo.prt_Handle[1] =
	VdmTib.PrinterInfo.prt_Handle[2] = NULL;


	// primarily for dongle
	VdmTib.PrinterInfo.prt_BytesInBuffer[0] =
	VdmTib.PrinterInfo.prt_BytesInBuffer[1] =
	VdmTib.PrinterInfo.prt_BytesInBuffer[2] = 0;

	// primarily for simulating printer status read in kernel
	VdmTib.PrinterInfo.prt_State = State;
	VdmTib.PrinterInfo.prt_Control = Control;
	VdmTib.PrinterInfo.prt_Status = Status;
	VdmTib.PrinterInfo.prt_HostState = HostState;


	// deal with mode carefully. VDD may have hooked printer ports
	// before we get here. If the mode is undefined, we can
	// safely initialize it to our default, otherwise, just leave
	// it alone.
	for (i = 0; i < 3; i++) {
	    if (PRT_MODE_NO_SIMULATION == VdmTib.PrinterInfo.prt_Mode[i])
		VdmTib.PrinterInfo.prt_Mode[i] = PRT_MODE_SIMULATE_STATUS_PORT;
	}

	return TRUE;
    }
    else
	return FALSE;
}

BOOLEAN MonitorEnablePrinterDirectAccess(WORD adapter, HANDLE handle, BOOLEAN Enable)
{
    ASSERT(VDM_NUMBER_OF_LPT > adapter);
    if (Enable) {
	// if the adapter has been allocated by a third party VDD,
	// can't do direct io.
	if (PRT_MODE_VDD_CONNECTED != VdmTib.PrinterInfo.prt_Mode[adapter]) {
	    VdmTib.PrinterInfo.prt_Mode[adapter] = PRT_MODE_DIRECT_IO;
	    VdmTib.PrinterInfo.prt_Handle[adapter] = handle;
	    // NtVdmControl(VdmPrinterDirectIoOpen, &adapter);
	    return TRUE;
	}
	else
	    return FALSE;
    }
    else {
	// disabling direct i/o. reset it back to status port simulation
	if (VdmTib.PrinterInfo.prt_Handle[adapter] == handle) {
	    NtVdmControl(VdmPrinterDirectIoClose, &adapter);
	    VdmTib.PrinterInfo.prt_Mode[adapter] = PRT_MODE_SIMULATE_STATUS_PORT;
	    VdmTib.PrinterInfo.prt_Handle[adapter] = NULL;
	    VdmTib.PrinterInfo.prt_BytesInBuffer[adapter] = 0;
	    return TRUE;
	}
	else
	    return FALSE;
    }
}

BOOLEAN MonitorVddConnectPrinter(WORD Adapter, HANDLE hVdd, BOOLEAN Connect)
{
    if (VDM_NUMBER_OF_LPT <= Adapter)
	return FALSE;
    if (Connect) {
	VdmTib.PrinterInfo.prt_Mode[Adapter] = PRT_MODE_VDD_CONNECTED;
	VdmTib.PrinterInfo.prt_Handle[Adapter] = hVdd;
	return TRUE;
    }
    else {
	if (hVdd == VdmTib.PrinterInfo.prt_Handle[Adapter]) {
	    VdmTib.PrinterInfo.prt_Mode[Adapter] = PRT_MODE_SIMULATE_STATUS_PORT;
	    VdmTib.PrinterInfo.prt_Handle[Adapter] = NULL;
	    return TRUE;
	}
	else return FALSE;
    }
}

BOOLEAN MonitorPrinterWriteData(WORD Adapter, BYTE Value)
{
    USHORT BytesInBuffer;

    ASSERT(VDM_NUMBER_OF_LPT > Adapter);
    BytesInBuffer = VdmTib.PrinterInfo.prt_BytesInBuffer[Adapter];
    VdmTib.PrinterInfo.prt_Buffer[Adapter][BytesInBuffer] = Value;
    VdmTib.PrinterInfo.prt_BytesInBuffer[Adapter]++;

    return TRUE;
}