2 static char RCSId
[] = "$Id: wine.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright
[] = "Copyright Robert J. Amstadt, 1993";
13 extern SEGDESC Segments
[];
14 extern unsigned short IF1632_Saved16_ss
;
15 extern unsigned long IF1632_Saved16_ebp
;
16 extern unsigned long IF1632_Saved16_esp
;
17 extern unsigned short IF1632_Saved32_ss
;
18 extern unsigned long IF1632_Saved32_ebp
;
19 extern unsigned long IF1632_Saved32_esp
;
21 extern struct segment_descriptor_s
*MakeProcThunks
;
26 unsigned char thunk
[10];
29 static __inline__
int Is16bitAddress(void *address
)
31 return ((unsigned int) address
32 >= (((FIRST_SELECTOR
<< 3) | 0x0007) << 16));
35 /**********************************************************************
39 PushOn16(int size
, unsigned int value
)
41 char *p
= (char *) (((unsigned int)IF1632_Saved16_ss
<< 16) +
42 (IF1632_Saved16_esp
& 0xffff));
45 unsigned long *lp
= (unsigned long *) p
- 1;
48 IF1632_Saved16_esp
-= 4;
52 unsigned short *sp
= (unsigned short *) p
- 1;
55 IF1632_Saved16_esp
-= 2;
59 /**********************************************************************
60 * FindDataSegmentForCode
63 FindDataSegmentForCode(unsigned long csip
)
67 seg_idx
= (unsigned short) (csip
>> 19);
68 return Segments
[seg_idx
].owner
;
71 /**********************************************************************
75 CallBack16(void *func
, int n_args
, ...)
79 int arg_type
, arg_value
;
83 for (i
= 0; i
< n_args
; i
++)
85 arg_type
= va_arg(ap
, int);
86 arg_value
= va_arg(ap
, int);
87 PushOn16(arg_type
, arg_value
);
92 return CallTo16((unsigned int) func
,
93 FindDataSegmentForCode((unsigned long) func
));
96 /**********************************************************************
97 * CALLBACK_MakeProcInstance
100 CALLBACK_MakeProcInstance(void *func
, int instance
)
107 tp
= (struct thunk_s
*) MakeProcThunks
->base_addr
;
108 for (i
= 0; i
< 0x10000 / sizeof(*tp
); i
++, tp
++)
116 tp
->thunk
[1] = (unsigned char) instance
;
117 tp
->thunk
[2] = (unsigned char) (instance
>> 8);
121 memcpy(&tp
->thunk
[6], &func
, 4);
127 /**********************************************************************
128 * FreeProcInstance (KERNEL.52)
130 void FreeProcInstance(FARPROC func
)
137 tp
= (struct thunk_s
*) MakeProcThunks
->base_addr
;
138 for (i
= 0; i
< 0x10000 / sizeof(*tp
); i
++, tp
++)
140 if ((void *) tp
->thunk
== (void *) func
)
148 /**********************************************************************
149 * GetCodeHandle (KERNEL.93)
151 HANDLE
GetCodeHandle( FARPROC proc
)
153 struct thunk_s
*tp
= (struct thunk_s
*)proc
;
155 /* Return the code segment containing 'proc'. */
156 /* Not sure if this is really correct (shouldn't matter that much). */
157 printf( "STUB: GetCodeHandle(%p) returning %x\n",
158 tp
->thunk
[8] + (tp
->thunk
[9] << 8) );
159 return tp
->thunk
[8] + (tp
->thunk
[9] << 8);
163 /**********************************************************************
164 * CallWindowProc (USER.122)
166 LONG
CallWindowProc( WNDPROC func
, HWND hwnd
, WORD message
,
167 WORD wParam
, LONG lParam
)
169 SpyMessage(hwnd
, message
, wParam
, lParam
);
171 if (HIWORD((LONG
)func
) == WINE_CODE_SELECTOR
)
173 static struct dll_table_entry_s
*user_tab
= NULL
;
174 void *address
= (void *) ((LONG
) func
& 0xffff);
176 if (user_tab
== NULL
)
177 user_tab
= FindDLLTable("USER");
180 if (user_tab
[107].address
== address
)
181 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
184 else if (user_tab
[308].address
== address
)
185 return DefDlgProc(hwnd
, message
, wParam
, lParam
);
187 /* DefMDIChildProc */
188 else if (user_tab
[447].address
== address
)
189 return DefMDIChildProc(hwnd
, message
, wParam
, lParam
);
194 fprintf(stderr
, "wine: Unknown wine callback %08x\n", func
);
198 else if (Is16bitAddress(func
))
200 #ifdef DEBUG_CALLBACK
201 printf("CallWindowProc // 16bit func=%08X !\n", func
);
203 PushOn16( CALLBACK_SIZE_WORD
, hwnd
);
204 PushOn16( CALLBACK_SIZE_WORD
, message
);
205 PushOn16( CALLBACK_SIZE_WORD
, wParam
);
206 PushOn16( CALLBACK_SIZE_LONG
, lParam
);
207 return CallTo16((unsigned int) func
,
208 FindDataSegmentForCode((unsigned long) func
));
212 #ifdef DEBUG_CALLBACK
213 printf("CallWindowProc // 32bit func=%08X !\n", func
);
215 return (*func
)(hwnd
, message
, wParam
, lParam
);
219 /**********************************************************************
222 void CallLineDDAProc(FARPROC func
, short xPos
, short yPos
, long lParam
)
224 if (Is16bitAddress(func
))
226 PushOn16( CALLBACK_SIZE_WORD
, xPos
);
227 PushOn16( CALLBACK_SIZE_WORD
, yPos
);
228 PushOn16( CALLBACK_SIZE_LONG
, lParam
);
229 CallTo16((unsigned int) func
,
230 FindDataSegmentForCode((unsigned long) func
));
234 (*func
)(xPos
, yPos
, lParam
);
238 /**********************************************************************
241 DWORD
CallHookProc( HOOKPROC func
, short code
, WPARAM wParam
, LPARAM lParam
)
243 if (Is16bitAddress(func
))
245 PushOn16( CALLBACK_SIZE_WORD
, code
);
246 PushOn16( CALLBACK_SIZE_WORD
, wParam
);
247 PushOn16( CALLBACK_SIZE_LONG
, lParam
);
248 return CallTo16((unsigned int) func
,
249 FindDataSegmentForCode((unsigned long) func
));
253 return (*func
)( code
, wParam
, lParam
);
257 /**********************************************************************
260 BOOL
CallGrayStringProc(FARPROC func
, HDC hdc
, LPARAM lParam
, INT cch
)
262 if (Is16bitAddress(func
))
264 PushOn16( CALLBACK_SIZE_WORD
, hdc
);
265 PushOn16( CALLBACK_SIZE_LONG
, lParam
);
266 PushOn16( CALLBACK_SIZE_WORD
, cch
);
267 return CallTo16((unsigned int) func
,
268 FindDataSegmentForCode((unsigned long) func
));
272 return (*func
)( hdc
, lParam
, cch
);
276 /* ------------------------------------------------------------------------ */
278 * The following functions realize the Catch/Throw functionality.
279 * My thought is to use the setjmp, longjmp combination to do the
280 * major part of this one. All I have to remember, in addition to
281 * whatever the jmp_buf contains, is the contents of the 16-bit
282 * sp, bp and ss. I do this by storing them in the structure passed
283 * to me by the 16-bit program (including my own jmp_buf...).
284 * Hopefully there isn't any program that modifies the contents!
285 * Bad thing: I have to save part of the stack, since this will
286 * get reused on the next call after my return, leaving it in an
289 #define STACK_DEPTH_16 28
291 struct special_buffer
{
294 char stack_part
[STACK_DEPTH_16
];
297 int Catch (LPCATCHBUF cbuf
)
301 char *stack16
= (char *) (((unsigned int)IF1632_Saved16_ss
<< 16) +
302 (IF1632_Saved16_esp
& 0xffff));
304 sb
= malloc (sizeof (struct special_buffer
));
306 sb
-> regs
[0] = IF1632_Saved16_esp
;
307 sb
-> regs
[1] = IF1632_Saved16_ebp
;
308 sb
-> regs
[2] = IF1632_Saved16_ss
& 0xffff;
309 sb
-> regs
[3] = IF1632_Saved32_esp
;
310 sb
-> regs
[4] = IF1632_Saved32_ebp
;
311 sb
-> regs
[5] = IF1632_Saved32_ss
& 0xffff;
312 memcpy (sb
-> stack_part
, stack16
, STACK_DEPTH_16
);
313 tmp_jmp
= &sb
-> buffer
;
314 *((struct special_buffer
**)cbuf
) = sb
;
316 if ((retval
= setjmp (*tmp_jmp
)))
318 IF1632_Saved16_esp
= sb
-> regs
[0];
319 IF1632_Saved16_ebp
= sb
-> regs
[1];
320 IF1632_Saved16_ss
= sb
-> regs
[2] & 0xffff;
321 IF1632_Saved32_esp
= sb
-> regs
[3];
322 IF1632_Saved32_ebp
= sb
-> regs
[4];
323 IF1632_Saved32_ss
= sb
-> regs
[5] & 0xffff;
324 stack16
= (char *) (((unsigned int)IF1632_Saved16_ss
<< 16) +
325 (IF1632_Saved16_esp
& 0xffff));
327 memcpy (stack16
, sb
-> stack_part
, STACK_DEPTH_16
);
329 printf ("Been thrown here: %d, retval = %d\n", sb
, retval
);
335 printf ("Will somtime get thrown here: %d\n", sb
);
341 void Throw (LPCATCHBUF cbuf
, int val
)
343 sb
= *((struct special_buffer
**)cbuf
);
345 printf ("Throwing to: %d\n", sb
);
347 longjmp (sb
-> buffer
, val
);
349 #endif /* !WINELIB */