2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
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 implicitely 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 \******************************************************************************/
18 /* INCLUDES *******************************************************************/
30 /* PRIVATE VARIABLES **********************************************************/
34 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
35 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // 13x nop
36 BOP(BOP_UNSIMULATE
), // UnSimulate16 BOP
38 C_ASSERT(sizeof(Yield
) == 16 * sizeof(BYTE
));
40 /* PUBLIC FUNCTIONS ***********************************************************/
43 InitializeContextEx(IN PCALLBACK16 Context
,
44 IN ULONG TrampolineSize
,
48 Context
->TrampolineFarPtr
= MAKELONG(Offset
, Segment
);
49 Context
->TrampolineSize
= max(TRAMPOLINE_SIZE
, TrampolineSize
);
50 Context
->Segment
= Segment
;
51 Context
->NextOffset
= Offset
+ Context
->TrampolineSize
;
55 InitializeContext(IN PCALLBACK16 Context
,
59 InitializeContextEx(Context
,
66 Call16(IN USHORT Segment
,
70 USHORT OrgCS
= getCS();
71 USHORT OrgIP
= getIP();
73 /* Set the new CS:IP */
77 DPRINT("Call16(%04X:%04X)\n", Segment
, Offset
);
79 /* Start CPU simulation */
88 RunCallback16(IN PCALLBACK16 Context
,
91 PUCHAR TrampolineBase
= (PUCHAR
)FAR_POINTER(Context
->TrampolineFarPtr
);
92 PUCHAR Trampoline
= TrampolineBase
;
93 UCHAR OldTrampoline
[TRAMPOLINE_SIZE
];
95 /* Save the old trampoline */
96 ((PULONGLONG
)&OldTrampoline
)[0] = ((PULONGLONG
)TrampolineBase
)[0];
98 DPRINT("RunCallback16(0x%p)\n", FarPtr
);
100 /* Build the generic entry-point for 16-bit far calls */
101 *Trampoline
++ = 0x9A; // Call far seg:off
102 *(PULONG
)Trampoline
= FarPtr
;
103 Trampoline
+= sizeof(ULONG
);
104 UnSimulate16(Trampoline
);
106 /* Perform the call */
107 Call16(HIWORD(Context
->TrampolineFarPtr
),
108 LOWORD(Context
->TrampolineFarPtr
));
110 /* Restore the old trampoline */
111 ((PULONGLONG
)TrampolineBase
)[0] = ((PULONGLONG
)&OldTrampoline
)[0];
115 RegisterCallback16(IN ULONG FarPtr
,
116 IN LPBYTE CallbackCode
,
117 IN SIZE_T CallbackSize
,
118 OUT PSIZE_T CodeSize OPTIONAL
)
120 LPBYTE CodeStart
= (LPBYTE
)FAR_POINTER(FarPtr
);
121 LPBYTE Code
= CodeStart
;
123 SIZE_T OurCodeSize
= CallbackSize
;
125 if (CallbackCode
== NULL
) CallbackSize
= 0;
129 /* 16-bit interrupt code */
130 RtlCopyMemory(Code
, CallbackCode
, CallbackSize
);
131 Code
+= CallbackSize
;
134 /* Return the real size of the code if needed */
135 if (CodeSize
) *CodeSize
= OurCodeSize
; // == (ULONG_PTR)Code - (ULONG_PTR)CodeStart;
137 // /* Return the entry-point address for 32-bit calls */
138 // return (ULONG_PTR)(CodeStart + CallbackSize);