Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Relay calls helper routines |
| 3 | * |
| 4 | * Copyright 1993 Robert J. Amstadt |
| 5 | * Copyright 1995 Martin von Loewis |
| 6 | * Copyright 1995, 1996, 1997 Alexandre Julliard |
| 7 | * Copyright 1997 Eric Youngdale |
| 8 | * Copyright 1999 Ulrich Weigand |
| 9 | */ |
| 10 | |
Francois Gouget | e5ddd26 | 2001-10-14 16:18:52 +0000 | [diff] [blame] | 11 | #include "config.h" |
| 12 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 13 | #include <ctype.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "winnt.h" |
| 17 | #include "thread.h" |
| 18 | #include "stackframe.h" |
| 19 | |
| 20 | #include "build.h" |
| 21 | |
Ulrich Weigand | 0108d83 | 2000-12-29 05:17:33 +0000 | [diff] [blame] | 22 | #ifdef __i386__ |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 99eac46 | 2001-08-28 17:26:49 +0000 | [diff] [blame] | 24 | static void function_header( FILE *outfile, const char *name ) |
| 25 | { |
| 26 | fprintf( outfile, "\n\t.align %d\n", get_alignment(4) ); |
| 27 | #ifdef USE_STABS |
| 28 | fprintf( outfile, "\t.stabs \"%s:F1\",36,0,0," PREFIX "%s\n", name, name); |
| 29 | #endif |
| 30 | #ifdef NEED_TYPE_IN_DEF |
| 31 | fprintf( outfile, "\t.def " PREFIX "%s; .scl 2; .type 32; .endef\n", name ); |
| 32 | #else |
| 33 | fprintf( outfile, "\t.type " PREFIX "%s,@function\n", name ); |
| 34 | #endif |
| 35 | fprintf( outfile, "\t.globl " PREFIX "%s\n", name ); |
| 36 | fprintf( outfile, PREFIX "%s:\n", name ); |
| 37 | } |
| 38 | |
| 39 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 40 | /******************************************************************* |
| 41 | * BuildCallFrom16Core |
| 42 | * |
| 43 | * This routine builds the core routines used in 16->32 thunks: |
| 44 | * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk. |
| 45 | * |
| 46 | * These routines are intended to be called via a far call (with 32-bit |
| 47 | * operand size) from 16-bit code. The 16-bit code stub must push %bp, |
| 48 | * the 32-bit entry point to be called, and the argument conversion |
| 49 | * routine to be used (see stack layout below). |
| 50 | * |
| 51 | * The core routine completes the STACK16FRAME on the 16-bit stack and |
| 52 | * switches to the 32-bit stack. Then, the argument conversion routine |
| 53 | * is called; it gets passed the 32-bit entry point and a pointer to the |
| 54 | * 16-bit arguments (on the 16-bit stack) as parameters. (You can either |
| 55 | * use conversion routines automatically generated by BuildCallFrom16, |
| 56 | * or write your own for special purposes.) |
| 57 | * |
| 58 | * The conversion routine must call the 32-bit entry point, passing it |
| 59 | * the converted arguments, and return its return value to the core. |
| 60 | * After the conversion routine has returned, the core switches back |
| 61 | * to the 16-bit stack, converts the return value to the DX:AX format |
| 62 | * (CallFrom16Long), and returns to the 16-bit call stub. All parameters, |
| 63 | * including %bp, are popped off the stack. |
| 64 | * |
| 65 | * The 16-bit call stub now returns to the caller, popping the 16-bit |
| 66 | * arguments if necessary (pascal calling convention). |
| 67 | * |
| 68 | * In the case of a 'register' function, CallFrom16Register fills a |
| 69 | * CONTEXT86 structure with the values all registers had at the point |
| 70 | * the first instruction of the 16-bit call stub was about to be |
| 71 | * executed. A pointer to this CONTEXT86 is passed as third parameter |
| 72 | * to the argument conversion routine, which typically passes it on |
| 73 | * to the called 32-bit entry point. |
| 74 | * |
| 75 | * CallFrom16Thunk is a special variant used by the implementation of |
| 76 | * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is |
| 77 | * implemented as follows: |
| 78 | * On entry, the EBX register is set up to contain a flat pointer to the |
| 79 | * 16-bit stack such that EBX+22 points to the first argument. |
| 80 | * Then, the entry point is called, while EBP is set up to point |
| 81 | * to the return address (on the 32-bit stack). |
| 82 | * The called function returns with CX set to the number of bytes |
| 83 | * to be popped of the caller's stack. |
| 84 | * |
| 85 | * Stack layout upon entry to the core routine (STACK16FRAME): |
| 86 | * ... ... |
| 87 | * (sp+24) word first 16-bit arg |
| 88 | * (sp+22) word cs |
| 89 | * (sp+20) word ip |
| 90 | * (sp+18) word bp |
| 91 | * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count) |
| 92 | * (sp+12) word ip of actual entry point (necessary for relay debugging) |
| 93 | * (sp+8) long relay (argument conversion) function entry point |
| 94 | * (sp+4) long cs of 16-bit entry point |
| 95 | * (sp) long ip of 16-bit entry point |
| 96 | * |
| 97 | * Added on the stack: |
| 98 | * (sp-2) word saved gs |
| 99 | * (sp-4) word saved fs |
| 100 | * (sp-6) word saved es |
| 101 | * (sp-8) word saved ds |
| 102 | * (sp-12) long saved ebp |
| 103 | * (sp-16) long saved ecx |
| 104 | * (sp-20) long saved edx |
| 105 | * (sp-24) long saved previous stack |
| 106 | */ |
| 107 | static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret ) |
| 108 | { |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 109 | char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long"; |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 110 | |
| 111 | /* Function header */ |
Alexandre Julliard | 99eac46 | 2001-08-28 17:26:49 +0000 | [diff] [blame] | 112 | if (thunk) function_header( outfile, "__wine_call_from_16_thunk" ); |
| 113 | else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" ); |
| 114 | else if (short_ret) function_header( outfile, "__wine_call_from_16_word" ); |
| 115 | else function_header( outfile, "__wine_call_from_16_long" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 116 | |
| 117 | /* Create STACK16FRAME (except STACK32FRAME link) */ |
| 118 | fprintf( outfile, "\tpushw %%gs\n" ); |
| 119 | fprintf( outfile, "\tpushw %%fs\n" ); |
| 120 | fprintf( outfile, "\tpushw %%es\n" ); |
| 121 | fprintf( outfile, "\tpushw %%ds\n" ); |
| 122 | fprintf( outfile, "\tpushl %%ebp\n" ); |
| 123 | fprintf( outfile, "\tpushl %%ecx\n" ); |
| 124 | fprintf( outfile, "\tpushl %%edx\n" ); |
| 125 | |
| 126 | /* Save original EFlags register */ |
| 127 | fprintf( outfile, "\tpushfl\n" ); |
| 128 | |
| 129 | if ( UsePIC ) |
| 130 | { |
| 131 | /* Get Global Offset Table into %ecx */ |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 132 | fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot1\n", name ); |
| 133 | fprintf( outfile, ".L__wine_call_from_16_%s.getgot1:\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 134 | fprintf( outfile, "\tpopl %%ecx\n" ); |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 135 | fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Alexandre Julliard | aa5a116 | 2000-10-29 01:28:30 +0000 | [diff] [blame] | 138 | if (UsePIC) |
| 139 | { |
| 140 | fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector@GOT(%%ecx), %%edx\n" ); |
| 141 | fprintf( outfile, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" ); |
| 142 | } |
| 143 | else |
| 144 | fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector,%%edx\n" ); |
| 145 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 146 | /* Load 32-bit segment registers */ |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 147 | #ifdef __svr4__ |
| 148 | fprintf( outfile, "\tdata16\n"); |
| 149 | #endif |
| 150 | fprintf( outfile, "\tmovw %%dx, %%ds\n" ); |
| 151 | #ifdef __svr4__ |
| 152 | fprintf( outfile, "\tdata16\n"); |
| 153 | #endif |
| 154 | fprintf( outfile, "\tmovw %%dx, %%es\n" ); |
| 155 | |
| 156 | if ( UsePIC ) |
| 157 | { |
| 158 | fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" ); |
| 159 | fprintf( outfile, "\tmovw (%%edx), %%fs\n" ); |
| 160 | } |
| 161 | else |
| 162 | fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" ); |
| 163 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 164 | /* Get address of wine_ldt_copy array into %ecx */ |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 165 | if ( UsePIC ) |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 166 | fprintf( outfile, "\tmovl " PREFIX "wine_ldt_copy@GOT(%%ecx), %%ecx\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 167 | else |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 168 | fprintf( outfile, "\tmovl $" PREFIX "wine_ldt_copy, %%ecx\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 169 | |
| 170 | /* Translate STACK16FRAME base to flat offset in %edx */ |
| 171 | fprintf( outfile, "\tmovw %%ss, %%dx\n" ); |
| 172 | fprintf( outfile, "\tandl $0xfff8, %%edx\n" ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 173 | fprintf( outfile, "\tshrl $1, %%edx\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 174 | fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" ); |
| 175 | fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 176 | fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 177 | |
| 178 | /* Get saved flags into %ecx */ |
| 179 | fprintf( outfile, "\tpopl %%ecx\n" ); |
| 180 | |
| 181 | /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */ |
| 182 | fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET ); |
| 183 | fprintf( outfile, "\tpushl %%ebp\n" ); |
| 184 | |
| 185 | /* Switch stacks */ |
| 186 | #ifdef __svr4__ |
| 187 | fprintf( outfile,"\tdata16\n"); |
| 188 | #endif |
| 189 | fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 ); |
| 190 | fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET ); |
| 191 | fprintf( outfile, "\tpushl %%ds\n" ); |
| 192 | fprintf( outfile, "\tpopl %%ss\n" ); |
| 193 | fprintf( outfile, "\tmovl %%ebp, %%esp\n" ); |
| 194 | fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) ); |
| 195 | |
| 196 | |
| 197 | /* At this point: |
| 198 | STACK16FRAME is completely set up |
| 199 | DS, ES, SS: flat data segment |
| 200 | FS: current TEB |
| 201 | ESP: points to last STACK32FRAME |
| 202 | EBP: points to ebp member of last STACK32FRAME |
| 203 | EDX: points to current STACK16FRAME |
| 204 | ECX: contains saved flags |
| 205 | all other registers: unchanged */ |
| 206 | |
| 207 | /* Special case: C16ThkSL stub */ |
| 208 | if ( thunk ) |
| 209 | { |
| 210 | /* Set up registers as expected and call thunk */ |
| 211 | fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 ); |
| 212 | fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" ); |
| 213 | |
| 214 | fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) ); |
| 215 | |
| 216 | /* Switch stack back */ |
Bob Goodwin | d349241 | 2001-06-08 20:25:34 +0000 | [diff] [blame] | 217 | fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 ); |
| 218 | fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 219 | fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET ); |
| 220 | |
| 221 | /* Restore registers and return directly to caller */ |
| 222 | fprintf( outfile, "\taddl $8, %%esp\n" ); |
| 223 | fprintf( outfile, "\tpopl %%ebp\n" ); |
| 224 | fprintf( outfile, "\tpopw %%ds\n" ); |
| 225 | fprintf( outfile, "\tpopw %%es\n" ); |
| 226 | fprintf( outfile, "\tpopw %%fs\n" ); |
| 227 | fprintf( outfile, "\tpopw %%gs\n" ); |
| 228 | fprintf( outfile, "\taddl $20, %%esp\n" ); |
| 229 | |
| 230 | fprintf( outfile, "\txorb %%ch, %%ch\n" ); |
| 231 | fprintf( outfile, "\tpopl %%ebx\n" ); |
| 232 | fprintf( outfile, "\taddw %%cx, %%sp\n" ); |
| 233 | fprintf( outfile, "\tpush %%ebx\n" ); |
| 234 | |
| 235 | fprintf( outfile, "\t.byte 0x66\n" ); |
| 236 | fprintf( outfile, "\tlret\n" ); |
| 237 | |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* Build register CONTEXT */ |
| 243 | if ( reg_func ) |
| 244 | { |
| 245 | fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) ); |
| 246 | |
| 247 | fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) ); |
| 248 | |
| 249 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) ); |
| 250 | fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) ); |
| 251 | fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) ); |
| 252 | fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) ); |
| 253 | |
| 254 | fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) ); |
| 255 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) ); |
| 256 | fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) ); |
| 257 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) ); |
| 258 | fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) ); |
| 259 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) ); |
| 260 | |
| 261 | fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) ); |
| 262 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) ); |
| 263 | fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) ); |
| 264 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) ); |
| 265 | fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) ); |
| 266 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) ); |
| 267 | fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) ); |
| 268 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) ); |
| 269 | |
| 270 | fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) ); |
| 271 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) ); |
| 272 | fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) ); |
| 273 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) ); |
| 274 | |
| 275 | fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 ); |
| 276 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) ); |
| 277 | fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET ); |
| 278 | fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) ); |
| 279 | fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) ); |
| 280 | #if 0 |
| 281 | fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) ); |
| 282 | #endif |
| 283 | |
| 284 | /* Push address of CONTEXT86 structure -- popped by the relay routine */ |
| 285 | fprintf( outfile, "\tpushl %%esp\n" ); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /* Print debug info before call */ |
| 290 | if ( debugging ) |
| 291 | { |
| 292 | if ( UsePIC ) |
| 293 | { |
| 294 | fprintf( outfile, "\tpushl %%ebx\n" ); |
| 295 | |
| 296 | /* Get Global Offset Table into %ebx (for PLT call) */ |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 297 | fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot2\n", name ); |
| 298 | fprintf( outfile, ".L__wine_call_from_16_%s.getgot2:\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 299 | fprintf( outfile, "\tpopl %%ebx\n" ); |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 300 | fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | fprintf( outfile, "\tpushl %%edx\n" ); |
| 304 | if ( reg_func ) |
| 305 | fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n", |
| 306 | sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) ); |
| 307 | else |
| 308 | fprintf( outfile, "\tpushl $0\n" ); |
| 309 | |
| 310 | if ( UsePIC ) |
| 311 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n "); |
| 312 | else |
| 313 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n "); |
| 314 | |
| 315 | fprintf( outfile, "\tpopl %%edx\n" ); |
| 316 | fprintf( outfile, "\tpopl %%edx\n" ); |
| 317 | |
| 318 | if ( UsePIC ) |
| 319 | fprintf( outfile, "\tpopl %%ebx\n" ); |
| 320 | } |
| 321 | |
| 322 | /* Call relay routine (which will call the API entry point) */ |
| 323 | fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) ); |
| 324 | fprintf( outfile, "\tpushl %%eax\n" ); |
| 325 | fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) ); |
| 326 | fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) ); |
| 327 | |
| 328 | /* Print debug info after call */ |
| 329 | if ( debugging ) |
| 330 | { |
| 331 | if ( UsePIC ) |
| 332 | { |
| 333 | fprintf( outfile, "\tpushl %%ebx\n" ); |
| 334 | |
| 335 | /* Get Global Offset Table into %ebx (for PLT call) */ |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 336 | fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot3\n", name ); |
| 337 | fprintf( outfile, ".L__wine_call_from_16_%s.getgot3:\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 338 | fprintf( outfile, "\tpopl %%ebx\n" ); |
Alexandre Julliard | 745ec84 | 2000-11-13 04:54:45 +0000 | [diff] [blame] | 339 | fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | fprintf( outfile, "\tpushl %%eax\n" ); |
| 343 | if ( reg_func ) |
| 344 | fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n", |
| 345 | sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) ); |
| 346 | else |
| 347 | fprintf( outfile, "\tpushl $0\n" ); |
| 348 | |
| 349 | if ( UsePIC ) |
| 350 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n "); |
| 351 | else |
| 352 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n "); |
| 353 | |
| 354 | fprintf( outfile, "\tpopl %%eax\n" ); |
| 355 | fprintf( outfile, "\tpopl %%eax\n" ); |
| 356 | |
| 357 | if ( UsePIC ) |
| 358 | fprintf( outfile, "\tpopl %%ebx\n" ); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | if ( reg_func ) |
| 363 | { |
| 364 | fprintf( outfile, "\tmovl %%esp, %%ebx\n" ); |
| 365 | |
| 366 | /* Switch stack back */ |
Bob Goodwin | d349241 | 2001-06-08 20:25:34 +0000 | [diff] [blame] | 367 | fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 ); |
| 368 | fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 369 | fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET ); |
| 370 | |
| 371 | /* Get return address to CallFrom16 stub */ |
| 372 | fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 ); |
| 373 | fprintf( outfile, "\tpopl %%eax\n" ); |
| 374 | fprintf( outfile, "\tpopl %%edx\n" ); |
| 375 | |
| 376 | /* Restore all registers from CONTEXT */ |
| 377 | fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) ); |
| 378 | fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) ); |
| 379 | fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */ |
| 380 | |
| 381 | fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) ); |
| 382 | fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) ); |
| 383 | fprintf( outfile, "\tpushl %%edx\n" ); |
| 384 | fprintf( outfile, "\tpushl %%eax\n" ); |
| 385 | fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) ); |
| 386 | fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) ); |
| 387 | |
| 388 | fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) ); |
| 389 | fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) ); |
| 390 | fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) ); |
| 391 | |
| 392 | fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) ); |
| 393 | fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) ); |
| 394 | fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) ); |
| 395 | fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) ); |
| 396 | fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) ); |
| 397 | fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) ); |
| 398 | fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) ); |
| 399 | |
| 400 | fprintf( outfile, "\tpopl %%ds\n" ); |
| 401 | fprintf( outfile, "\tpopfl\n" ); |
| 402 | fprintf( outfile, "\tlret\n" ); |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | /* Switch stack back */ |
Bob Goodwin | d349241 | 2001-06-08 20:25:34 +0000 | [diff] [blame] | 407 | fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 ); |
| 408 | fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 409 | fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET ); |
| 410 | |
| 411 | /* Restore registers */ |
| 412 | fprintf( outfile, "\tpopl %%edx\n" ); |
| 413 | fprintf( outfile, "\tpopl %%ecx\n" ); |
| 414 | fprintf( outfile, "\tpopl %%ebp\n" ); |
| 415 | fprintf( outfile, "\tpopw %%ds\n" ); |
| 416 | fprintf( outfile, "\tpopw %%es\n" ); |
| 417 | fprintf( outfile, "\tpopw %%fs\n" ); |
| 418 | fprintf( outfile, "\tpopw %%gs\n" ); |
| 419 | |
| 420 | /* Prepare return value and set flags accordingly */ |
| 421 | if ( !short_ret ) |
| 422 | fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" ); |
| 423 | fprintf( outfile, "\torl %%eax, %%eax\n" ); |
| 424 | |
| 425 | /* Return to return stub which will return to caller */ |
| 426 | fprintf( outfile, "\tlret $12\n" ); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | |
| 431 | /******************************************************************* |
| 432 | * BuildCallTo16Core |
| 433 | * |
| 434 | * This routine builds the core routines used in 32->16 thunks: |
| 435 | * |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 436 | * extern void WINAPI wine_call_to_16_word( SEGPTR target, int nb_args ); |
| 437 | * extern void WINAPI wine_call_to_16_long( SEGPTR target, int nb_args ); |
| 438 | * extern void WINAPI wine_call_to_16_regs_short( const CONTEXT86 *context, int nb_args ); |
| 439 | * extern void WINAPI wine_call_to_16_regs_long ( const CONTEXT86 *context, int nb_args ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 440 | * |
| 441 | * These routines can be called directly from 32-bit code. |
| 442 | * |
| 443 | * All routines expect that the 16-bit stack contents (arguments) were |
| 444 | * already set up by the caller; nb_args must contain the number of bytes |
| 445 | * to be conserved. The 16-bit SS:SP will be set accordinly. |
| 446 | * |
| 447 | * All other registers are either taken from the CONTEXT86 structure |
| 448 | * or else set to default values. The target routine address is either |
| 449 | * given directly or taken from the CONTEXT86. |
| 450 | * |
| 451 | * If you want to call a 16-bit routine taking only standard argument types |
| 452 | * (WORD and LONG), you can also have an appropriate argument conversion |
| 453 | * stub automatically generated (see BuildCallTo16); you'd then call this |
| 454 | * stub, which in turn would prepare the 16-bit stack and call the appropiate |
| 455 | * core routine. |
| 456 | * |
| 457 | */ |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 458 | static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func ) |
| 459 | { |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 460 | char *name = reg_func == 2 ? "regs_long" : |
| 461 | reg_func == 1 ? "regs_short" : |
| 462 | short_ret? "word" : "long"; |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 463 | |
| 464 | /* Function header */ |
Alexandre Julliard | 99eac46 | 2001-08-28 17:26:49 +0000 | [diff] [blame] | 465 | if (reg_func == 2) function_header( outfile, "wine_call_to_16_regs_long" ); |
| 466 | else if (reg_func == 1) function_header( outfile, "wine_call_to_16_regs_short" ); |
| 467 | else if (short_ret) function_header( outfile, "wine_call_to_16_word" ); |
| 468 | else function_header( outfile, "wine_call_to_16_long" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 469 | |
| 470 | /* Function entry sequence */ |
| 471 | fprintf( outfile, "\tpushl %%ebp\n" ); |
| 472 | fprintf( outfile, "\tmovl %%esp, %%ebp\n" ); |
| 473 | |
| 474 | /* Save the 32-bit registers */ |
| 475 | fprintf( outfile, "\tpushl %%ebx\n" ); |
| 476 | fprintf( outfile, "\tpushl %%ecx\n" ); |
| 477 | fprintf( outfile, "\tpushl %%edx\n" ); |
| 478 | fprintf( outfile, "\tpushl %%esi\n" ); |
| 479 | fprintf( outfile, "\tpushl %%edi\n" ); |
| 480 | |
| 481 | if ( UsePIC ) |
| 482 | { |
| 483 | /* Get Global Offset Table into %ebx */ |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 484 | fprintf( outfile, "\tcall .Lwine_call_to_16_%s.getgot1\n", name ); |
| 485 | fprintf( outfile, ".Lwine_call_to_16_%s.getgot1:\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 486 | fprintf( outfile, "\tpopl %%ebx\n" ); |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 487 | fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot1], %%ebx\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /* Enter Win16 Mutex */ |
| 491 | if ( UsePIC ) |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 492 | fprintf( outfile, "\tcall " PREFIX "_EnterWin16Lock@PLT\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 493 | else |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 494 | fprintf( outfile, "\tcall " PREFIX "_EnterWin16Lock\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 495 | |
| 496 | /* Print debugging info */ |
| 497 | if (debugging) |
| 498 | { |
| 499 | /* Push flags, number of arguments, and target */ |
| 500 | fprintf( outfile, "\tpushl $%d\n", reg_func ); |
| 501 | fprintf( outfile, "\tpushl 12(%%ebp)\n" ); |
| 502 | fprintf( outfile, "\tpushl 8(%%ebp)\n" ); |
| 503 | |
| 504 | if ( UsePIC ) |
| 505 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" ); |
| 506 | else |
| 507 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" ); |
| 508 | |
| 509 | fprintf( outfile, "\taddl $12, %%esp\n" ); |
| 510 | } |
| 511 | |
| 512 | /* Get return address */ |
| 513 | if ( UsePIC ) |
| 514 | { |
| 515 | fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" ); |
| 516 | fprintf( outfile, "\tmovl " PREFIX "(%%ecx), %%ecx\n" ); |
| 517 | } |
| 518 | else |
| 519 | fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" ); |
| 520 | |
| 521 | /* Call the actual CallTo16 routine (simulate a lcall) */ |
| 522 | fprintf( outfile, "\tpushl %%cs\n" ); |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 523 | fprintf( outfile, "\tcall .Lwine_call_to_16_%s\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 524 | |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 525 | if ( !reg_func ) |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 526 | { |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 527 | /* Convert and push return value */ |
| 528 | if ( short_ret ) |
| 529 | { |
| 530 | fprintf( outfile, "\tmovzwl %%ax, %%eax\n" ); |
| 531 | fprintf( outfile, "\tpushl %%eax\n" ); |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | fprintf( outfile, "\tshll $16,%%edx\n" ); |
| 536 | fprintf( outfile, "\tmovw %%ax,%%dx\n" ); |
| 537 | fprintf( outfile, "\tpushl %%edx\n" ); |
| 538 | } |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 539 | } |
| 540 | else |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 541 | { |
| 542 | /* |
| 543 | * Modify CONTEXT86 structure to contain new values |
| 544 | * |
| 545 | * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP. |
| 546 | * The segment registers as well as ESI and EDI should |
| 547 | * not be modified by a well-behaved 16-bit routine in |
| 548 | * any case. [If necessary, we could restore them as well, |
| 549 | * at the cost of a somewhat less efficient return path.] |
| 550 | */ |
| 551 | |
| 552 | fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target)-12 ); |
| 553 | fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) ); |
| 554 | fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) ); |
| 555 | fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) ); |
| 556 | fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) ); |
| 557 | fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) ); |
| 558 | fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) ); |
| 559 | /* The return glue code saved %esp into %esi */ |
| 560 | |
| 561 | fprintf( outfile, "\tpushl %%edi\n" ); |
| 562 | } |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 563 | |
| 564 | if ( UsePIC ) |
| 565 | { |
| 566 | /* Get Global Offset Table into %ebx (might have been overwritten) */ |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 567 | fprintf( outfile, "\tcall .Lwine_call_to_16_%s.getgot2\n", name ); |
| 568 | fprintf( outfile, ".Lwine_call_to_16_%s.getgot2:\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 569 | fprintf( outfile, "\tpopl %%ebx\n" ); |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 570 | fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot2], %%ebx\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* Print debugging info */ |
| 574 | if (debugging) |
| 575 | { |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 576 | fprintf( outfile, "\tpushl $%d\n", reg_func ); |
| 577 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 578 | if ( UsePIC ) |
| 579 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" ); |
| 580 | else |
| 581 | fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" ); |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 582 | |
| 583 | fprintf( outfile, "\taddl $4, %%esp\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Leave Win16 Mutex */ |
| 587 | if ( UsePIC ) |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 588 | fprintf( outfile, "\tcall " PREFIX "_LeaveWin16Lock@PLT\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 589 | else |
Alexandre Julliard | ab68797 | 2000-11-15 23:41:46 +0000 | [diff] [blame] | 590 | fprintf( outfile, "\tcall " PREFIX "_LeaveWin16Lock\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 591 | |
| 592 | /* Get return value */ |
| 593 | fprintf( outfile, "\tpopl %%eax\n" ); |
| 594 | |
| 595 | /* Restore the 32-bit registers */ |
| 596 | fprintf( outfile, "\tpopl %%edi\n" ); |
| 597 | fprintf( outfile, "\tpopl %%esi\n" ); |
| 598 | fprintf( outfile, "\tpopl %%edx\n" ); |
| 599 | fprintf( outfile, "\tpopl %%ecx\n" ); |
| 600 | fprintf( outfile, "\tpopl %%ebx\n" ); |
| 601 | |
| 602 | /* Function exit sequence */ |
| 603 | fprintf( outfile, "\tpopl %%ebp\n" ); |
| 604 | fprintf( outfile, "\tret $8\n" ); |
| 605 | |
| 606 | |
| 607 | /* Start of the actual CallTo16 routine */ |
| 608 | |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 609 | fprintf( outfile, ".Lwine_call_to_16_%s:\n", name ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 610 | |
| 611 | /* Complete STACK32FRAME */ |
| 612 | fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET ); |
| 613 | fprintf( outfile, "\tmovl %%esp,%%edx\n" ); |
| 614 | |
| 615 | /* Switch to the 16-bit stack */ |
| 616 | #ifdef __svr4__ |
| 617 | fprintf( outfile,"\tdata16\n"); |
| 618 | #endif |
| 619 | fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2); |
| 620 | fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET ); |
| 621 | fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET ); |
| 622 | |
| 623 | /* Make %bp point to the previous stackframe (built by CallFrom16) */ |
| 624 | fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" ); |
| 625 | fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) ); |
| 626 | |
| 627 | /* Add the specified offset to the new sp */ |
| 628 | fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) ); |
| 629 | |
| 630 | /* Push the return address |
| 631 | * With sreg suffix, we push 16:16 address (normal lret) |
| 632 | * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45) |
| 633 | */ |
| 634 | if (reg_func != 2) |
| 635 | fprintf( outfile, "\tpushl %%ecx\n" ); |
| 636 | else |
| 637 | { |
| 638 | fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" ); |
| 639 | fprintf( outfile, "\tpushw $0\n" ); |
| 640 | fprintf( outfile, "\tpushw %%ax\n" ); |
| 641 | fprintf( outfile, "\tpushw $0\n" ); |
| 642 | fprintf( outfile, "\tpushw %%cx\n" ); |
| 643 | } |
| 644 | |
| 645 | if (reg_func) |
| 646 | { |
| 647 | /* Push the called routine address */ |
| 648 | fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) ); |
| 649 | fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) ); |
| 650 | fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) ); |
| 651 | |
| 652 | /* Get the registers */ |
| 653 | fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) ); |
| 654 | fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) ); |
| 655 | fprintf( outfile, "\tmovw %%ax,%%es\n" ); |
| 656 | fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) ); |
| 657 | fprintf( outfile, "\tmovw %%ax,%%fs\n" ); |
| 658 | fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) ); |
| 659 | fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) ); |
| 660 | fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) ); |
| 661 | fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) ); |
| 662 | fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) ); |
| 663 | fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) ); |
| 664 | fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) ); |
| 665 | |
| 666 | /* Get the 16-bit ds */ |
| 667 | fprintf( outfile, "\tpopw %%ds\n" ); |
| 668 | } |
| 669 | else /* not a register function */ |
| 670 | { |
| 671 | /* Push the called routine address */ |
| 672 | fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) ); |
| 673 | |
| 674 | /* Set %fs to the value saved by the last CallFrom16 */ |
| 675 | fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) ); |
| 676 | fprintf( outfile, "\tmovw %%ax,%%fs\n" ); |
| 677 | |
| 678 | /* Set %ds and %es (and %ax just in case) equal to %ss */ |
| 679 | fprintf( outfile, "\tmovw %%ss,%%ax\n" ); |
| 680 | fprintf( outfile, "\tmovw %%ax,%%ds\n" ); |
| 681 | fprintf( outfile, "\tmovw %%ax,%%es\n" ); |
| 682 | } |
| 683 | |
| 684 | /* Jump to the called routine */ |
| 685 | fprintf( outfile, "\t.byte 0x66\n" ); |
| 686 | fprintf( outfile, "\tlret\n" ); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | /******************************************************************* |
| 691 | * BuildRet16Func |
| 692 | * |
| 693 | * Build the return code for 16-bit callbacks |
| 694 | */ |
| 695 | static void BuildRet16Func( FILE *outfile ) |
| 696 | { |
| 697 | /* |
| 698 | * Note: This must reside in the .data section to allow |
| 699 | * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol |
| 700 | */ |
| 701 | |
Alexandre Julliard | 99eac46 | 2001-08-28 17:26:49 +0000 | [diff] [blame] | 702 | function_header( outfile, "CallTo16_Ret" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 703 | |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 704 | /* Save %esp into %esi */ |
| 705 | fprintf( outfile, "\tmovl %%esp,%%esi\n" ); |
| 706 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 707 | /* Restore 32-bit segment registers */ |
| 708 | |
Alexandre Julliard | aa5a116 | 2000-10-29 01:28:30 +0000 | [diff] [blame] | 709 | fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector-" PREFIX "Call16_Ret_Start,%%edi\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 710 | #ifdef __svr4__ |
| 711 | fprintf( outfile, "\tdata16\n"); |
| 712 | #endif |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 713 | fprintf( outfile, "\tmovw %%di,%%ds\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 714 | #ifdef __svr4__ |
| 715 | fprintf( outfile, "\tdata16\n"); |
| 716 | #endif |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 717 | fprintf( outfile, "\tmovw %%di,%%es\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 718 | |
| 719 | fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" ); |
| 720 | |
| 721 | /* Restore the 32-bit stack */ |
| 722 | |
| 723 | #ifdef __svr4__ |
| 724 | fprintf( outfile, "\tdata16\n"); |
| 725 | #endif |
Ulrich Weigand | a761e3d | 2000-09-13 20:29:44 +0000 | [diff] [blame] | 726 | fprintf( outfile, "\tmovw %%di,%%ss\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 727 | fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET ); |
| 728 | fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET ); |
| 729 | |
| 730 | /* Return to caller */ |
| 731 | |
| 732 | fprintf( outfile, "\tlret\n" ); |
| 733 | |
Alexandre Julliard | aa5a116 | 2000-10-29 01:28:30 +0000 | [diff] [blame] | 734 | /* Declare the return address and data selector variables */ |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 735 | |
Josh DuBois | 0b64cfb | 2001-02-13 02:06:38 +0000 | [diff] [blame] | 736 | fprintf( outfile, "\n\t.align %d\n", get_alignment(4) ); |
Alexandre Julliard | aa5a116 | 2000-10-29 01:28:30 +0000 | [diff] [blame] | 737 | fprintf( outfile, "\t.globl " PREFIX "CallTo16_DataSelector\n" ); |
| 738 | fprintf( outfile, PREFIX "CallTo16_DataSelector:\t.long 0\n" ); |
| 739 | fprintf( outfile, "\t.globl " PREFIX "CallTo16_RetAddr\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 740 | fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" ); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /******************************************************************* |
| 745 | * BuildCallTo32CBClient |
| 746 | * |
| 747 | * Call a CBClient relay stub from 32-bit code (KERNEL.620). |
| 748 | * |
| 749 | * Since the relay stub is itself 32-bit, this should not be a problem; |
| 750 | * unfortunately, the relay stubs are expected to switch back to a |
| 751 | * 16-bit stack (and 16-bit code) after completion :-( |
| 752 | * |
| 753 | * This would conflict with our 16- vs. 32-bit stack handling, so |
| 754 | * we simply switch *back* to our 32-bit stack before returning to |
| 755 | * the caller ... |
| 756 | * |
| 757 | * The CBClient relay stub expects to be called with the following |
| 758 | * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit |
| 759 | * stack at the designated places: |
| 760 | * |
| 761 | * ... |
| 762 | * (ebp+14) original arguments to the callback routine |
| 763 | * (ebp+10) far return address to original caller |
| 764 | * (ebp+6) Thunklet target address |
| 765 | * (ebp+2) Thunklet relay ID code |
| 766 | * (ebp) BP (saved by CBClientGlueSL) |
| 767 | * (ebp-2) SI (saved by CBClientGlueSL) |
| 768 | * (ebp-4) DI (saved by CBClientGlueSL) |
| 769 | * (ebp-6) DS (saved by CBClientGlueSL) |
| 770 | * |
| 771 | * ... buffer space used by the 16-bit side glue for temp copies |
| 772 | * |
| 773 | * (ebx+4) far return address to 16-bit side glue code |
| 774 | * (ebx) saved 16-bit ss:sp (pointing to ebx+4) |
| 775 | * |
| 776 | * The 32-bit side glue code accesses both the original arguments (via ebp) |
| 777 | * and the temporary copies prepared by the 16-bit side glue (via ebx). |
| 778 | * After completion, the stub will load ss:sp from the buffer at ebx |
| 779 | * and perform a far return to 16-bit code. |
| 780 | * |
| 781 | * To trick the relay stub into returning to us, we replace the 16-bit |
| 782 | * return address to the glue code by a cs:ip pair pointing to our |
| 783 | * return entry point (the original return address is saved first). |
| 784 | * Our return stub thus called will then reload the 32-bit ss:esp and |
| 785 | * return to 32-bit code (by using and ss:esp value that we have also |
| 786 | * pushed onto the 16-bit stack before and a cs:eip values found at |
| 787 | * that position on the 32-bit stack). The ss:esp to be restored is |
| 788 | * found relative to the 16-bit stack pointer at: |
| 789 | * |
| 790 | * (ebx-4) ss (flat) |
| 791 | * (ebx-8) sp (32-bit stack pointer) |
| 792 | * |
| 793 | * The second variant of this routine, CALL32_CBClientEx, which is used |
| 794 | * to implement KERNEL.621, has to cope with yet another problem: Here, |
| 795 | * the 32-bit side directly returns to the caller of the CBClient thunklet, |
| 796 | * restoring registers saved by CBClientGlueSL and cleaning up the stack. |
| 797 | * As we have to return to our 32-bit code first, we have to adapt the |
| 798 | * layout of our temporary area so as to include values for the registers |
| 799 | * that are to be restored, and later (in the implementation of KERNEL.621) |
| 800 | * we *really* restore them. The return stub restores DS, DI, SI, and BP |
| 801 | * from the stack, skips the next 8 bytes (CBClient relay code / target), |
| 802 | * and then performs a lret NN, where NN is the number of arguments to be |
| 803 | * removed. Thus, we prepare our temporary area as follows: |
| 804 | * |
| 805 | * (ebx+22) 16-bit cs (this segment) |
| 806 | * (ebx+20) 16-bit ip ('16-bit' return entry point) |
| 807 | * (ebx+16) 32-bit ss (flat) |
| 808 | * (ebx+12) 32-bit sp (32-bit stack pointer) |
| 809 | * (ebx+10) 16-bit bp (points to ebx+24) |
| 810 | * (ebx+8) 16-bit si (ignored) |
| 811 | * (ebx+6) 16-bit di (ignored) |
| 812 | * (ebx+4) 16-bit ds (we actually use the flat DS here) |
| 813 | * (ebx+2) 16-bit ss (16-bit stack segment) |
| 814 | * (ebx+0) 16-bit sp (points to ebx+4) |
| 815 | * |
| 816 | * Note that we ensure that DS is not changed and remains the flat segment, |
| 817 | * and the 32-bit stack pointer our own return stub needs fits just |
| 818 | * perfectly into the 8 bytes that are skipped by the Windows stub. |
| 819 | * One problem is that we have to determine the number of removed arguments, |
| 820 | * as these have to be really removed in KERNEL.621. Thus, the BP value |
| 821 | * that we place in the temporary area to be restored, contains the value |
| 822 | * that SP would have if no arguments were removed. By comparing the actual |
| 823 | * value of SP with this value in our return stub we can compute the number |
| 824 | * of removed arguments. This is then returned to KERNEL.621. |
| 825 | * |
| 826 | * The stack layout of this function: |
| 827 | * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only) |
| 828 | * (ebp+16) esi pointer to caller's esi value |
| 829 | * (ebp+12) arg ebp value to be set for relay stub |
| 830 | * (ebp+8) func CBClient relay stub address |
| 831 | * (ebp+4) ret addr |
| 832 | * (ebp) ebp |
| 833 | */ |
| 834 | static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx ) |
| 835 | { |
| 836 | char *name = isEx? "CBClientEx" : "CBClient"; |
| 837 | int size = isEx? 24 : 12; |
| 838 | |
| 839 | /* Function header */ |
| 840 | |
Josh DuBois | 0b64cfb | 2001-02-13 02:06:38 +0000 | [diff] [blame] | 841 | fprintf( outfile, "\n\t.align %d\n", get_alignment(4) ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 842 | #ifdef USE_STABS |
| 843 | fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n", |
| 844 | name, name ); |
| 845 | #endif |
| 846 | fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name ); |
| 847 | fprintf( outfile, PREFIX "CALL32_%s:\n", name ); |
| 848 | |
| 849 | /* Entry code */ |
| 850 | |
| 851 | fprintf( outfile, "\tpushl %%ebp\n" ); |
| 852 | fprintf( outfile, "\tmovl %%esp,%%ebp\n" ); |
| 853 | fprintf( outfile, "\tpushl %%edi\n" ); |
| 854 | fprintf( outfile, "\tpushl %%esi\n" ); |
| 855 | fprintf( outfile, "\tpushl %%ebx\n" ); |
| 856 | |
| 857 | /* Get the 16-bit stack */ |
| 858 | |
| 859 | fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET); |
| 860 | |
| 861 | /* Convert it to a flat address */ |
| 862 | |
| 863 | fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" ); |
| 864 | fprintf( outfile, "\tandl $0xfff8,%%eax\n" ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 865 | fprintf( outfile, "\tshrl $1,%%eax\n" ); |
| 866 | fprintf( outfile, "\tmovl " PREFIX "wine_ldt_copy(%%eax),%%esi\n" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 867 | fprintf( outfile, "\tmovw %%bx,%%ax\n" ); |
| 868 | fprintf( outfile, "\taddl %%eax,%%esi\n" ); |
| 869 | |
| 870 | /* Allocate temporary area (simulate STACK16_PUSH) */ |
| 871 | |
| 872 | fprintf( outfile, "\tpushf\n" ); |
| 873 | fprintf( outfile, "\tcld\n" ); |
| 874 | fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size ); |
| 875 | fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) ); |
| 876 | fprintf( outfile, "\trep\n\tmovsb\n" ); |
| 877 | fprintf( outfile, "\tpopf\n" ); |
| 878 | |
| 879 | fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET ); |
| 880 | |
| 881 | fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */ |
| 882 | |
| 883 | /* Set up temporary area */ |
| 884 | |
| 885 | if ( !isEx ) |
| 886 | { |
| 887 | fprintf( outfile, "\tleal 4(%%edi), %%edi\n" ); |
| 888 | |
| 889 | fprintf( outfile, "\tleal -8(%%esp), %%eax\n" ); |
| 890 | fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */ |
| 891 | |
| 892 | fprintf( outfile, "\tmovw %%ss, %%ax\n" ); |
| 893 | fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" ); |
| 894 | fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */ |
| 895 | |
| 896 | fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 ); |
| 897 | fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */ |
| 898 | |
| 899 | fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name ); |
| 900 | fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */ |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 ); |
| 905 | fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); |
| 906 | |
| 907 | fprintf( outfile, "\tmovw %%ds, %%ax\n" ); |
| 908 | fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" ); |
| 909 | |
| 910 | fprintf( outfile, "\taddl $20, %%ebx\n" ); |
| 911 | fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" ); |
| 912 | |
| 913 | fprintf( outfile, "\tleal -8(%%esp), %%eax\n" ); |
| 914 | fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" ); |
| 915 | |
| 916 | fprintf( outfile, "\tmovw %%ss, %%ax\n" ); |
| 917 | fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" ); |
| 918 | fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" ); |
| 919 | |
| 920 | fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name ); |
| 921 | fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" ); |
| 922 | } |
| 923 | |
| 924 | /* Set up registers and call CBClient relay stub (simulating a far call) */ |
| 925 | |
| 926 | fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" ); |
| 927 | fprintf( outfile, "\tmovl (%%esi), %%esi\n" ); |
| 928 | |
| 929 | fprintf( outfile, "\tmovl %%edi, %%ebx\n" ); |
| 930 | fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" ); |
| 931 | fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" ); |
| 932 | |
| 933 | fprintf( outfile, "\tpushl %%cs\n" ); |
| 934 | fprintf( outfile, "\tcall *%%eax\n" ); |
| 935 | |
| 936 | /* Return new esi value to caller */ |
| 937 | |
| 938 | fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" ); |
| 939 | fprintf( outfile, "\tmovl %%esi, (%%edi)\n" ); |
| 940 | |
| 941 | /* Cleanup temporary area (simulate STACK16_POP) */ |
| 942 | |
| 943 | fprintf( outfile, "\tpop %%esi\n" ); |
| 944 | |
| 945 | fprintf( outfile, "\tpushf\n" ); |
| 946 | fprintf( outfile, "\tstd\n" ); |
| 947 | fprintf( outfile, "\tdec %%esi\n" ); |
| 948 | fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size ); |
| 949 | fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) ); |
| 950 | fprintf( outfile, "\trep\n\tmovsb\n" ); |
| 951 | fprintf( outfile, "\tpopf\n" ); |
| 952 | |
| 953 | fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET ); |
| 954 | |
| 955 | /* Return argument size to caller */ |
| 956 | if ( isEx ) |
| 957 | { |
| 958 | fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" ); |
| 959 | fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" ); |
| 960 | } |
| 961 | |
| 962 | /* Restore registers and return */ |
| 963 | |
| 964 | fprintf( outfile, "\tpopl %%ebx\n" ); |
| 965 | fprintf( outfile, "\tpopl %%esi\n" ); |
| 966 | fprintf( outfile, "\tpopl %%edi\n" ); |
| 967 | fprintf( outfile, "\tpopl %%ebp\n" ); |
| 968 | fprintf( outfile, "\tret\n" ); |
| 969 | } |
| 970 | |
| 971 | static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx ) |
| 972 | { |
| 973 | char *name = isEx? "CBClientEx" : "CBClient"; |
| 974 | |
| 975 | /* '16-bit' return stub */ |
| 976 | |
| 977 | fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name ); |
| 978 | fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name ); |
| 979 | |
| 980 | if ( !isEx ) |
| 981 | { |
| 982 | fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" ); |
| 983 | fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" ); |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" ); |
| 988 | fprintf( outfile, "\tsubw %%bp, %%sp\n" ); |
| 989 | fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" ); |
| 990 | fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" ); |
| 991 | } |
| 992 | fprintf( outfile, "\tlret\n" ); |
| 993 | |
| 994 | /* Declare the return address variable */ |
| 995 | |
| 996 | fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name ); |
| 997 | fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name ); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | /******************************************************************* |
| 1002 | * BuildCallFrom32Regs |
| 1003 | * |
| 1004 | * Build a 32-bit-to-Wine call-back function for a 'register' function. |
| 1005 | * 'args' is the number of dword arguments. |
| 1006 | * |
| 1007 | * Stack layout: |
| 1008 | * ... |
| 1009 | * (ebp+12) first arg |
| 1010 | * (ebp+8) ret addr to user code |
| 1011 | * (ebp+4) ret addr to relay code |
| 1012 | * (ebp+0) saved ebp |
| 1013 | * (ebp-128) buffer area to allow stack frame manipulation |
| 1014 | * (ebp-332) CONTEXT86 struct |
| 1015 | * (ebp-336) CONTEXT86 *argument |
| 1016 | * .... other arguments copied from (ebp+12) |
| 1017 | * |
| 1018 | * The entry point routine is called with a CONTEXT* extra argument, |
| 1019 | * following the normal args. In this context structure, EIP_reg |
| 1020 | * contains the return address to user code, and ESP_reg the stack |
| 1021 | * pointer on return (with the return address and arguments already |
| 1022 | * removed). |
| 1023 | */ |
| 1024 | static void BuildCallFrom32Regs( FILE *outfile ) |
| 1025 | { |
| 1026 | static const int STACK_SPACE = 128 + sizeof(CONTEXT86); |
| 1027 | |
| 1028 | /* Function header */ |
| 1029 | |
Alexandre Julliard | eb9a863 | 2001-12-11 00:50:33 +0000 | [diff] [blame] | 1030 | function_header( outfile, "__wine_call_from_32_regs" ); |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1031 | |
| 1032 | /* Allocate some buffer space on the stack */ |
| 1033 | |
| 1034 | fprintf( outfile, "\tpushl %%ebp\n" ); |
| 1035 | fprintf( outfile, "\tmovl %%esp,%%ebp\n "); |
| 1036 | fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE ); |
Alexandre Julliard | 99eac46 | 2001-08-28 17:26:49 +0000 | [diff] [blame] | 1037 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1038 | /* Build the context structure */ |
| 1039 | |
| 1040 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE ); |
| 1041 | fprintf( outfile, "\tpushfl\n" ); |
| 1042 | fprintf( outfile, "\tpopl %%eax\n" ); |
| 1043 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE ); |
| 1044 | fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" ); |
| 1045 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE ); |
| 1046 | fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE ); |
| 1047 | fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE ); |
| 1048 | fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE ); |
| 1049 | fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE ); |
| 1050 | fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE ); |
| 1051 | |
| 1052 | fprintf( outfile, "\txorl %%eax,%%eax\n" ); |
| 1053 | fprintf( outfile, "\tmovw %%cs,%%ax\n" ); |
| 1054 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE ); |
| 1055 | fprintf( outfile, "\tmovw %%es,%%ax\n" ); |
| 1056 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE ); |
| 1057 | fprintf( outfile, "\tmovw %%fs,%%ax\n" ); |
| 1058 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE ); |
| 1059 | fprintf( outfile, "\tmovw %%gs,%%ax\n" ); |
| 1060 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE ); |
| 1061 | fprintf( outfile, "\tmovw %%ss,%%ax\n" ); |
| 1062 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE ); |
| 1063 | fprintf( outfile, "\tmovw %%ds,%%ax\n" ); |
| 1064 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE ); |
| 1065 | fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */ |
| 1066 | |
| 1067 | fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL ); |
| 1068 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE ); |
| 1069 | |
| 1070 | fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */ |
| 1071 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE ); |
| 1072 | |
| 1073 | /* Transfer the arguments */ |
| 1074 | |
| 1075 | fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */ |
| 1076 | fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */ |
| 1077 | fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */ |
| 1078 | fprintf( outfile, "\tjecxz 1f\n" ); |
| 1079 | fprintf( outfile, "\tsubl %%ecx,%%esp\n" ); |
| 1080 | fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */ |
| 1081 | fprintf( outfile, "\tmovl %%esp,%%edi\n" ); |
| 1082 | fprintf( outfile, "\tshrl $2,%%ecx\n" ); |
| 1083 | fprintf( outfile, "\tcld\n" ); |
| 1084 | fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */ |
| 1085 | |
| 1086 | fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */ |
| 1087 | fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" ); |
| 1088 | fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE ); |
| 1089 | |
| 1090 | /* Call the entry point */ |
| 1091 | |
| 1092 | fprintf( outfile, "\tcall *0(%%ebx)\n" ); |
| 1093 | |
| 1094 | /* Store %eip and %ebp onto the new stack */ |
| 1095 | |
| 1096 | fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE ); |
| 1097 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE ); |
| 1098 | fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" ); |
| 1099 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE ); |
| 1100 | fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" ); |
| 1101 | |
| 1102 | /* Restore the context structure */ |
| 1103 | |
| 1104 | /* Note: we don't bother to restore %cs, %ds and %ss |
| 1105 | * changing them in 32-bit code is a recipe for disaster anyway |
| 1106 | */ |
| 1107 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE ); |
| 1108 | fprintf( outfile, "\tmovw %%ax,%%es\n" ); |
| 1109 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE ); |
| 1110 | fprintf( outfile, "\tmovw %%ax,%%fs\n" ); |
| 1111 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE ); |
| 1112 | fprintf( outfile, "\tmovw %%ax,%%gs\n" ); |
| 1113 | |
| 1114 | fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE ); |
| 1115 | fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE ); |
| 1116 | fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE ); |
| 1117 | fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE ); |
| 1118 | fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE ); |
| 1119 | |
| 1120 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE ); |
| 1121 | fprintf( outfile, "\tpushl %%eax\n" ); |
| 1122 | fprintf( outfile, "\tpopfl\n" ); |
| 1123 | fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE ); |
| 1124 | |
| 1125 | fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE ); |
| 1126 | fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" ); |
| 1127 | fprintf( outfile, "\tpopl %%ebp\n" ); |
| 1128 | fprintf( outfile, "\tret\n" ); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | /******************************************************************* |
Alexandre Julliard | eb9a863 | 2001-12-11 00:50:33 +0000 | [diff] [blame] | 1133 | * BuildRelays16 |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1134 | * |
Alexandre Julliard | eb9a863 | 2001-12-11 00:50:33 +0000 | [diff] [blame] | 1135 | * Build all the 16-bit relay callbacks |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1136 | */ |
Alexandre Julliard | eb9a863 | 2001-12-11 00:50:33 +0000 | [diff] [blame] | 1137 | void BuildRelays16( FILE *outfile ) |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1138 | { |
Gavriel State | f98c52b | 2002-02-12 18:46:14 +0000 | [diff] [blame] | 1139 | char buffer[1024]; |
| 1140 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1141 | /* File header */ |
| 1142 | |
| 1143 | fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" ); |
| 1144 | fprintf( outfile, "\t.text\n" ); |
| 1145 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1146 | #ifdef USE_STABS |
| 1147 | if (output_file_name) |
| 1148 | { |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1149 | getcwd(buffer, sizeof(buffer)); |
| 1150 | fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name ); |
| 1151 | |
| 1152 | /* |
| 1153 | * The stabs help the internal debugger as they are an indication that it |
| 1154 | * is sensible to step into a thunk/trampoline. |
| 1155 | */ |
| 1156 | fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer); |
| 1157 | fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name ); |
| 1158 | fprintf( outfile, "Code_Start:\n\n" ); |
| 1159 | } |
| 1160 | #endif |
| 1161 | fprintf( outfile, PREFIX"Call16_Start:\n" ); |
| 1162 | fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" ); |
| 1163 | fprintf( outfile, "\t.byte 0\n\n" ); |
| 1164 | |
| 1165 | /* Standard CallFrom16 routine (WORD return) */ |
| 1166 | BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE ); |
| 1167 | |
| 1168 | /* Standard CallFrom16 routine (DWORD return) */ |
| 1169 | BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE ); |
| 1170 | |
| 1171 | /* Register CallFrom16 routine */ |
| 1172 | BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE ); |
| 1173 | |
| 1174 | /* C16ThkSL CallFrom16 routine */ |
| 1175 | BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE ); |
| 1176 | |
| 1177 | /* Standard CallTo16 routine (WORD return) */ |
| 1178 | BuildCallTo16Core( outfile, TRUE, FALSE ); |
| 1179 | |
| 1180 | /* Standard CallTo16 routine (DWORD return) */ |
| 1181 | BuildCallTo16Core( outfile, FALSE, FALSE ); |
| 1182 | |
| 1183 | /* Register CallTo16 routine (16:16 retf) */ |
| 1184 | BuildCallTo16Core( outfile, FALSE, 1 ); |
| 1185 | |
| 1186 | /* Register CallTo16 routine (16:32 retf) */ |
| 1187 | BuildCallTo16Core( outfile, FALSE, 2 ); |
| 1188 | |
| 1189 | /* CBClientThunkSL routine */ |
| 1190 | BuildCallTo32CBClient( outfile, FALSE ); |
| 1191 | |
| 1192 | /* CBClientThunkSLEx routine */ |
| 1193 | BuildCallTo32CBClient( outfile, TRUE ); |
| 1194 | |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1195 | fprintf( outfile, PREFIX"Call16_End:\n" ); |
| 1196 | fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" ); |
| 1197 | |
| 1198 | #ifdef USE_STABS |
| 1199 | fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n"); |
| 1200 | fprintf( outfile, ".Letext:\n"); |
Gavriel State | f98c52b | 2002-02-12 18:46:14 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* Some versions of gdb need to have the filename data for |
| 1203 | each section, so output it again for the data section. */ |
| 1204 | if (output_file_name) |
| 1205 | { |
| 1206 | fprintf( outfile, ".stabs \"%s/\",100,0,0,Data_Start\n", buffer); |
| 1207 | fprintf( outfile, ".stabs \"%s\",100,0,0,Data_Start\n", output_file_name ); |
| 1208 | fprintf( outfile, "Data_Start:\n\n" ); |
| 1209 | } |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1210 | #endif |
| 1211 | |
| 1212 | /* The whole Call16_Ret segment must lie within the .data section */ |
| 1213 | fprintf( outfile, "\n\t.data\n" ); |
| 1214 | fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" ); |
| 1215 | fprintf( outfile, PREFIX "Call16_Ret_Start:\n" ); |
| 1216 | |
| 1217 | /* Standard CallTo16 return stub */ |
| 1218 | BuildRet16Func( outfile ); |
| 1219 | |
| 1220 | /* CBClientThunkSL return stub */ |
| 1221 | BuildCallTo32CBClientRet( outfile, FALSE ); |
| 1222 | |
| 1223 | /* CBClientThunkSLEx return stub */ |
| 1224 | BuildCallTo32CBClientRet( outfile, TRUE ); |
| 1225 | |
| 1226 | /* End of Call16_Ret segment */ |
| 1227 | fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" ); |
| 1228 | fprintf( outfile, PREFIX "Call16_Ret_End:\n" ); |
Ulrich Weigand | 0108d83 | 2000-12-29 05:17:33 +0000 | [diff] [blame] | 1229 | } |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1230 | |
Alexandre Julliard | eb9a863 | 2001-12-11 00:50:33 +0000 | [diff] [blame] | 1231 | /******************************************************************* |
| 1232 | * BuildRelays32 |
| 1233 | * |
| 1234 | * Build all the 32-bit relay callbacks |
| 1235 | */ |
| 1236 | void BuildRelays32( FILE *outfile ) |
| 1237 | { |
| 1238 | /* File header */ |
| 1239 | |
| 1240 | fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" ); |
| 1241 | fprintf( outfile, "\t.text\n" ); |
| 1242 | |
| 1243 | #ifdef USE_STABS |
| 1244 | if (output_file_name) |
| 1245 | { |
| 1246 | char buffer[1024]; |
| 1247 | getcwd(buffer, sizeof(buffer)); |
| 1248 | fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name ); |
| 1249 | |
| 1250 | /* |
| 1251 | * The stabs help the internal debugger as they are an indication that it |
| 1252 | * is sensible to step into a thunk/trampoline. |
| 1253 | */ |
| 1254 | fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer); |
| 1255 | fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name ); |
| 1256 | fprintf( outfile, "Code_Start:\n\n" ); |
| 1257 | } |
| 1258 | #endif |
| 1259 | /* 32-bit register entry point */ |
| 1260 | BuildCallFrom32Regs( outfile ); |
| 1261 | |
| 1262 | #ifdef USE_STABS |
| 1263 | fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n"); |
| 1264 | fprintf( outfile, ".Letext:\n"); |
| 1265 | #endif |
| 1266 | } |
| 1267 | |
Ulrich Weigand | 0108d83 | 2000-12-29 05:17:33 +0000 | [diff] [blame] | 1268 | #else /* __i386__ */ |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1269 | |
Alexandre Julliard | eb9a863 | 2001-12-11 00:50:33 +0000 | [diff] [blame] | 1270 | void BuildRelays16( FILE *outfile ) |
| 1271 | { |
| 1272 | fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" ); |
| 1273 | } |
| 1274 | |
| 1275 | void BuildRelays32( FILE *outfile ) |
Ulrich Weigand | 0108d83 | 2000-12-29 05:17:33 +0000 | [diff] [blame] | 1276 | { |
| 1277 | fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" ); |
| 1278 | } |
Alexandre Julliard | e482eeb | 2000-06-23 20:15:35 +0000 | [diff] [blame] | 1279 | |
| 1280 | #endif /* __i386__ */ |
Ulrich Weigand | 0108d83 | 2000-12-29 05:17:33 +0000 | [diff] [blame] | 1281 | |