2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: subsystems/mvdm/ntvdm/cpu/callback.c
5 * PURPOSE: 16 and 32-bit Callbacks Support
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
10 /******************************************************************************\
11 | WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
13 | Callbacks support supposes implicitly that the callbacks are used
14 | in the SAME thread as the CPU thread, otherwise messing in parallel
15 | with the CPU registers is 100% prone to bugs!!
16 \******************************************************************************/
23 /* PRIVATE VARIABLES **********************************************************/
27 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // 13x nop
29 BOP(BOP_UNSIMULATE
), // UnSimulate16 BOP
31 C_ASSERT(sizeof(Yield
) == 16 * sizeof(BYTE
));
33 /* PUBLIC FUNCTIONS ***********************************************************/
36 InitializeContextEx(IN PCALLBACK16 Context
,
37 IN ULONG TrampolineSize
,
41 Context
->TrampolineFarPtr
= MAKELONG(Offset
, Segment
);
42 Context
->TrampolineSize
= max(TRAMPOLINE_SIZE
, TrampolineSize
);
43 Context
->Segment
= Segment
;
44 Context
->NextOffset
= Offset
+ Context
->TrampolineSize
;
48 InitializeContext(IN PCALLBACK16 Context
,
52 InitializeContextEx(Context
,
59 Call16(IN USHORT Segment
,
63 USHORT OrgCS
= getCS();
64 USHORT OrgIP
= getIP();
66 /* Set the new CS:IP */
70 DPRINT("Call16(%04X:%04X)\n", Segment
, Offset
);
72 /* Start CPU simulation */
81 RunCallback16(IN PCALLBACK16 Context
,
84 PUCHAR TrampolineBase
= (PUCHAR
)FAR_POINTER(Context
->TrampolineFarPtr
);
85 PUCHAR Trampoline
= TrampolineBase
;
86 UCHAR OldTrampoline
[TRAMPOLINE_SIZE
];
88 /* Save the old trampoline */
89 ((PULONGLONG
)&OldTrampoline
)[0] = ((PULONGLONG
)TrampolineBase
)[0];
91 DPRINT("RunCallback16(0x%p)\n", FarPtr
);
93 /* Build the generic entry-point for 16-bit far calls */
94 *Trampoline
++ = 0x9A; // Call far seg:off
95 *(PULONG
)Trampoline
= FarPtr
;
96 Trampoline
+= sizeof(ULONG
);
97 UnSimulate16(Trampoline
);
99 /* Perform the call */
100 Call16(HIWORD(Context
->TrampolineFarPtr
),
101 LOWORD(Context
->TrampolineFarPtr
));
103 /* Restore the old trampoline */
104 ((PULONGLONG
)TrampolineBase
)[0] = ((PULONGLONG
)&OldTrampoline
)[0];
108 RegisterCallback16(IN ULONG FarPtr
,
109 IN LPBYTE CallbackCode
,
110 IN SIZE_T CallbackSize
,
111 OUT PSIZE_T CodeSize OPTIONAL
)
113 LPBYTE CodeStart
= (LPBYTE
)FAR_POINTER(FarPtr
);
114 LPBYTE Code
= CodeStart
;
116 SIZE_T OurCodeSize
= CallbackSize
;
118 if (CallbackCode
== NULL
) CallbackSize
= 0;
122 /* 16-bit interrupt code */
123 RtlCopyMemory(Code
, CallbackCode
, CallbackSize
);
124 Code
+= CallbackSize
;
127 /* Return the real size of the code if needed */
128 if (CodeSize
) *CodeSize
= OurCodeSize
; // == (ULONG_PTR)Code - (ULONG_PTR)CodeStart;
130 // /* Return the entry-point address for 32-bit calls */
131 // return (ULONG_PTR)(CodeStart + CallbackSize);