| 1 | /* $Id: thunk.cpp,v 1.1 1999-09-15 23:33:01 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * | 
|---|
| 5 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 6 | * | 
|---|
| 7 | */ | 
|---|
| 8 | /* | 
|---|
| 9 | * Win32 thunking API functions (mostly stubs) | 
|---|
| 10 | * | 
|---|
| 11 | * Copyright 1998 Patrick Haller (stubs + Wine port) (?) | 
|---|
| 12 | * | 
|---|
| 13 | * Original WINE code (win32\kernel32.c) | 
|---|
| 14 | * | 
|---|
| 15 | * KERNEL32 thunks and other undocumented stuff | 
|---|
| 16 | * | 
|---|
| 17 | * Copyright 1997-1998 Marcus Meissner | 
|---|
| 18 | * Copyright 1998      Ulrich Weigand | 
|---|
| 19 | */ | 
|---|
| 20 | #include <os2win.h> | 
|---|
| 21 | #include <string.h> | 
|---|
| 22 | #include <builtin.h> | 
|---|
| 23 | #include "thunk.h" | 
|---|
| 24 |  | 
|---|
| 25 | //****************************************************************************** | 
|---|
| 26 | //****************************************************************************** | 
|---|
| 27 | DWORD WIN32API MapLS(void *address) | 
|---|
| 28 | { | 
|---|
| 29 | //  _interrupt(3); | 
|---|
| 30 | dprintf(("MapLS %X, not supported\n", address)); | 
|---|
| 31 | return((DWORD)address); | 
|---|
| 32 | } | 
|---|
| 33 | //****************************************************************************** | 
|---|
| 34 | //****************************************************************************** | 
|---|
| 35 | DWORD WIN32API ThunkProc(DWORD arg1) | 
|---|
| 36 | { | 
|---|
| 37 | dprintf(("ThunkProc - stub\n")); | 
|---|
| 38 | return(0); | 
|---|
| 39 | } | 
|---|
| 40 | //****************************************************************************** | 
|---|
| 41 | //****************************************************************************** | 
|---|
| 42 | void WIN32API FT_Prolog(CONTEXT *context) | 
|---|
| 43 | { | 
|---|
| 44 | dprintf(("FT_Prolog - stub\n")); | 
|---|
| 45 | } | 
|---|
| 46 | //****************************************************************************** | 
|---|
| 47 | //****************************************************************************** | 
|---|
| 48 | /********************************************************************** | 
|---|
| 49 | *      QT_Thunk            (KERNEL32) | 
|---|
| 50 | * | 
|---|
| 51 | * The target address is in EDX. | 
|---|
| 52 | * The 16 bit arguments start at ESP+4. | 
|---|
| 53 | * The number of 16bit argumentbytes is EBP-ESP-0x44 (68 Byte thunksetup). | 
|---|
| 54 | * [ok] | 
|---|
| 55 | */ | 
|---|
| 56 | VOID WIN32API QT_Thunk(CONTEXT *context) | 
|---|
| 57 | { | 
|---|
| 58 | dprintf(("QT_Thunk\n")); | 
|---|
| 59 | #if 0 | 
|---|
| 60 | CONTEXT context16; | 
|---|
| 61 | DWORD   argsize; | 
|---|
| 62 |  | 
|---|
| 63 | memcpy(&context16,context,sizeof(context16)); | 
|---|
| 64 |  | 
|---|
| 65 | CS_reg(&context16)   = HIWORD(EDX_reg(context)); | 
|---|
| 66 | IP_reg(&context16)   = LOWORD(EDX_reg(context)); | 
|---|
| 67 |  | 
|---|
| 68 | argsize = EBP_reg(context)-ESP_reg(context)-0x44; | 
|---|
| 69 |  | 
|---|
| 70 | /* additional 4 bytes used by the relaycode for storing the stackptr */ | 
|---|
| 71 | memcpy( ((LPBYTE)CURRENT_STACK16)-argsize-4, | 
|---|
| 72 | (LPBYTE)ESP_reg(context)+4, | 
|---|
| 73 | argsize | 
|---|
| 74 | ); | 
|---|
| 75 | EAX_reg(context) = CallTo16_regs_short(&context16,-argsize); | 
|---|
| 76 | #endif | 
|---|
| 77 | } | 
|---|
| 78 | //****************************************************************************** | 
|---|
| 79 | /*********************************************************************** | 
|---|
| 80 | * Generates a FT_Prolog call. | 
|---|
| 81 | * | 
|---|
| 82 | *  0FB6D1                  movzbl edx,cl | 
|---|
| 83 | *  8B1495xxxxxxxx      mov edx,[4*edx + xxxxxxxx] | 
|---|
| 84 | *  68xxxxxxxx          push FT_Prolog | 
|---|
| 85 | *  C3              lret | 
|---|
| 86 | */ | 
|---|
| 87 | static void _write_ftprolog(LPBYTE thunk,DWORD thunkstart) | 
|---|
| 88 | { | 
|---|
| 89 | LPBYTE x; | 
|---|
| 90 |  | 
|---|
| 91 | x = thunk; | 
|---|
| 92 | *x++  = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */ | 
|---|
| 93 | *x++  = 0x8B;*x++=0x14;*x++=0x95;*(DWORD*)x= thunkstart; | 
|---|
| 94 | x+=4; /* mov edx, [4*edx + thunkstart] */ | 
|---|
| 95 | *x++  = 0x68; *(DWORD*)x = (DWORD)FT_Prolog; | 
|---|
| 96 | x+=4;     /* push FT_Prolog */ | 
|---|
| 97 | *x++  = 0xC3;     /* lret */ | 
|---|
| 98 | /* fill rest with 0xCC / int 3 */ | 
|---|
| 99 | } | 
|---|
| 100 | //****************************************************************************** | 
|---|
| 101 | /*********************************************************************** | 
|---|
| 102 | * Generates a QT_Thunk style call. | 
|---|
| 103 | * | 
|---|
| 104 | *  33C9                    xor ecx, ecx | 
|---|
| 105 | *  8A4DFC                  mov cl , [ebp-04] | 
|---|
| 106 | *  8B148Dxxxxxxxx          mov edx, [4*ecx + (EAX+EDX)] | 
|---|
| 107 | *  B8yyyyyyyy              mov eax, QT_Thunk | 
|---|
| 108 | *  FFE0                    jmp eax | 
|---|
| 109 | */ | 
|---|
| 110 | static void _write_qtthunk(LPBYTE start,DWORD thunkstart) | 
|---|
| 111 | { | 
|---|
| 112 | LPBYTE x; | 
|---|
| 113 |  | 
|---|
| 114 | x = start; | 
|---|
| 115 | *x++  = 0x33;*x++=0xC9; /* xor ecx,ecx */ | 
|---|
| 116 | *x++  = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */ | 
|---|
| 117 | *x++  = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD*)x= thunkstart; | 
|---|
| 118 | x+=4; /* mov edx, [4*ecx + (EAX+EDX) */ | 
|---|
| 119 | *x++  = 0xB8; *(DWORD*)x = (DWORD)QT_Thunk; | 
|---|
| 120 | x+=4;     /* mov eax , QT_Thunk */ | 
|---|
| 121 | *x++  = 0xFF; *x++ = 0xE0;    /* jmp eax */ | 
|---|
| 122 | /* should fill the rest of the 32 bytes with 0xCC */ | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | //****************************************************************************** | 
|---|
| 126 | //****************************************************************************** | 
|---|
| 127 | DWORD WIN32API ThunkConnect32(thunkstruct *ths,  LPSTR thunkfun16, | 
|---|
| 128 | LPSTR module16, LPSTR module32, HMODULE hmod32, | 
|---|
| 129 | DWORD dllinitarg1 ) | 
|---|
| 130 | { | 
|---|
| 131 | thunkstruct *ths16; | 
|---|
| 132 |  | 
|---|
| 133 | //  _interrupt(3); | 
|---|
| 134 | dprintf(("ThunkConnect32 %s %s %s not supported\n", thunkfun16, module16, module32)); | 
|---|
| 135 |  | 
|---|
| 136 | if(strncmp(ths->magic,"SL01",4) && strncmp(ths->magic,"LS01",4)) | 
|---|
| 137 | return 0; | 
|---|
| 138 |  | 
|---|
| 139 | ths16 = (thunkstruct *)LocalAlloc(LPTR, ths->length); | 
|---|
| 140 | ths16->length = ths->length; | 
|---|
| 141 | ths16->ptr    = (DWORD)ThunkProc; | 
|---|
| 142 |  | 
|---|
| 143 | if(!strncmp(ths->magic,"SL01",4))  { | 
|---|
| 144 | ths->x0C = (DWORD)ths16; | 
|---|
| 145 | *(DWORD *)ths16->magic  = 0x0000304C; | 
|---|
| 146 | } | 
|---|
| 147 | if(!strncmp(ths->magic,"LS01",4))  { | 
|---|
| 148 | ths->ptr = ths16->ptr; | 
|---|
| 149 | /* code offset for QT_Thunk is at 0x1C...  */ | 
|---|
| 150 | _write_qtthunk (((LPBYTE)ths) + ths->x1C,ths->ptr); | 
|---|
| 151 |  | 
|---|
| 152 | /* code offset for FT_Prolog is at 0x20...  */ | 
|---|
| 153 | _write_ftprolog(((LPBYTE)ths) + ths->x20,ths->ptr); | 
|---|
| 154 | return 1; | 
|---|
| 155 | } | 
|---|
| 156 | return TRUE; | 
|---|
| 157 | } | 
|---|
| 158 | //****************************************************************************** | 
|---|
| 159 | //****************************************************************************** | 
|---|
| 160 | DWORD WIN32API K32Thk1632Prolog(DWORD arg1) | 
|---|
| 161 | { | 
|---|
| 162 | dprintf(("OS2K32Thk1632Prolog %X not supported\n", arg1)); | 
|---|
| 163 | return(0); | 
|---|
| 164 | } | 
|---|
| 165 | //****************************************************************************** | 
|---|
| 166 | //****************************************************************************** | 
|---|
| 167 | DWORD WIN32API K32Thk1632Epilog(DWORD arg1) | 
|---|
| 168 | { | 
|---|
| 169 | dprintf(("K32Thk1632Epilog %X not supported\n", arg1)); | 
|---|
| 170 | return(0); | 
|---|
| 171 | } | 
|---|
| 172 | //****************************************************************************** | 
|---|
| 173 | //****************************************************************************** | 
|---|
| 174 | DWORD WIN32API MapSLFix(DWORD arg1) | 
|---|
| 175 | { | 
|---|
| 176 | dprintf(("MapSLFix %X not supported\n", arg1)); | 
|---|
| 177 | return(0); | 
|---|
| 178 | } | 
|---|
| 179 | //****************************************************************************** | 
|---|
| 180 | //****************************************************************************** | 
|---|