Check, Radio & 3State buttons send WM_CTLCOLORSTATIC instead of
[wine/multimedia.git] / dlls / winedos / int31.c
blob3efc177354eaeb721ccd1bf6c2e514b8765f9b11
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
9 #include "windef.h"
10 #include "wine/winbase16.h"
11 #include "miscemu.h"
12 #include "task.h"
13 #include "msdos.h"
14 #include "dosexe.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(int31);
20 /* Structure for real-mode callbacks */
21 typedef struct
23 DWORD edi;
24 DWORD esi;
25 DWORD ebp;
26 DWORD reserved;
27 DWORD ebx;
28 DWORD edx;
29 DWORD ecx;
30 DWORD eax;
31 WORD fl;
32 WORD es;
33 WORD ds;
34 WORD fs;
35 WORD gs;
36 WORD ip;
37 WORD cs;
38 WORD sp;
39 WORD ss;
40 } REALMODECALL;
42 typedef struct tagRMCB {
43 DWORD address;
44 DWORD proc_ofs,proc_sel;
45 DWORD regs_ofs,regs_sel;
46 struct tagRMCB *next;
47 } RMCB;
49 static RMCB *FirstRMCB = NULL;
50 static WORD dpmi_flag;
52 /**********************************************************************
53 * INT_GetRealModeContext
55 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
57 context->Eax = call->eax;
58 context->Ebx = call->ebx;
59 context->Ecx = call->ecx;
60 context->Edx = call->edx;
61 context->Esi = call->esi;
62 context->Edi = call->edi;
63 context->Ebp = call->ebp;
64 context->EFlags = call->fl | V86_FLAG;
65 context->Eip = call->ip;
66 context->Esp = call->sp;
67 context->SegCs = call->cs;
68 context->SegDs = call->ds;
69 context->SegEs = call->es;
70 context->SegFs = call->fs;
71 context->SegGs = call->gs;
72 context->SegSs = call->ss;
76 /**********************************************************************
77 * INT_SetRealModeContext
79 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
81 call->eax = context->Eax;
82 call->ebx = context->Ebx;
83 call->ecx = context->Ecx;
84 call->edx = context->Edx;
85 call->esi = context->Esi;
86 call->edi = context->Edi;
87 call->ebp = context->Ebp;
88 call->fl = LOWORD(context->EFlags);
89 call->ip = LOWORD(context->Eip);
90 call->sp = LOWORD(context->Esp);
91 call->cs = context->SegCs;
92 call->ds = context->SegDs;
93 call->es = context->SegEs;
94 call->fs = context->SegFs;
95 call->gs = context->SegGs;
96 call->ss = context->SegSs;
100 #ifdef __i386__
102 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
103 #if 0 /* original code, which early gccs puke on */
105 int _clobber;
106 __asm__ __volatile__(
107 "pushl %%ebp\n"
108 "pushl %%ebx\n"
109 "pushl %%es\n"
110 "pushl %%ds\n"
111 "pushfl\n"
112 "mov %7,%%es\n"
113 "mov %5,%%ds\n"
114 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
115 "popl %%ds\n"
116 "mov %%es,%0\n"
117 "popl %%es\n"
118 "popl %%ebx\n"
119 "popl %%ebp\n"
120 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
121 : "0" (ss), "2" (esp),
122 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
123 "3" (&rmcb->proc_ofs) );
125 #else /* code generated by a gcc new enough */
127 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
128 "pushl %ebp\n\t"
129 "movl %esp,%ebp\n\t"
130 "pushl %edi\n\t"
131 "pushl %esi\n\t"
132 "movl 0x8(%ebp),%eax\n\t"
133 "movl 0x10(%ebp),%esi\n\t"
134 "movl 0xc(%ebp),%edx\n\t"
135 "movl 0x10(%eax),%ecx\n\t"
136 "movl 0xc(%eax),%edi\n\t"
137 "addl $0x4,%eax\n\t"
138 "pushl %ebp\n\t"
139 "pushl %ebx\n\t"
140 "pushl %es\n\t"
141 "pushl %ds\n\t"
142 "pushfl\n\t"
143 "mov %cx,%es\n\t"
144 "mov %dx,%ds\n\t"
145 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
146 "popl %ds\n\t"
147 "mov %es,%dx\n\t"
148 "popl %es\n\t"
149 "popl %ebx\n\t"
150 "popl %ebp\n\t"
151 "movl 0x14(%ebp),%eax\n\t"
152 "movw %dx,(%eax)\n\t"
153 "movl 0x18(%ebp),%edx\n\t"
154 "movl %edi,(%edx)\n\t"
155 "popl %esi\n\t"
156 "popl %edi\n\t"
157 "leave\n\t"
158 "ret")
159 #endif
161 #endif /* __i386__ */
163 /**********************************************************************
164 * DPMI_CallRMCBProc
166 * This routine does the hard work of calling a callback procedure.
168 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
170 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
171 /* Wine-internal RMCB, call directly */
172 ((RMCBPROC)rmcb->proc_ofs)(context);
173 } else {
174 #ifdef __i386__
175 UINT16 ss,es;
176 DWORD esp,edi;
178 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
179 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
180 esp = context->Esp;
182 FIXME("untested!\n");
184 /* The called proc ends with an IRET, and takes these parameters:
185 * DS:ESI = pointer to real-mode SS:SP
186 * ES:EDI = pointer to real-mode call structure
187 * It returns:
188 * ES:EDI = pointer to real-mode call structure (may be a copy)
189 * It is the proc's responsibility to change the return CS:IP in the
190 * real-mode call structure. */
191 if (flag & 1) {
192 /* 32-bit DPMI client */
193 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
194 } else {
195 /* 16-bit DPMI client */
196 CONTEXT86 ctx = *context;
197 ctx.SegCs = rmcb->proc_sel;
198 ctx.Eip = rmcb->proc_ofs;
199 ctx.SegDs = ss;
200 ctx.Esi = esp;
201 ctx.SegEs = rmcb->regs_sel;
202 ctx.Edi = rmcb->regs_ofs;
203 /* FIXME: I'm pretty sure this isn't right - should push flags first */
204 wine_call_to_16_regs_short(&ctx, 0);
205 es = ctx.SegEs;
206 edi = ctx.Edi;
208 FreeSelector16(ss);
209 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
210 #else
211 ERR("RMCBs only implemented for i386\n");
212 #endif
217 /**********************************************************************
218 * DPMI_CallRMProc
220 * This routine does the hard work of calling a real mode procedure.
222 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
224 LPWORD stack16;
225 LPVOID addr = NULL; /* avoid gcc warning */
226 RMCB *CurrRMCB;
227 int alloc = 0, already = 0;
228 BYTE *code;
230 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
231 context->Eax, context->Ebx, context->Ecx, context->Edx );
232 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
233 context->Esi, context->Edi, context->SegEs, context->SegDs,
234 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
236 callrmproc_again:
238 /* there might be some code that just jumps to RMCBs or the like,
239 in which case following the jumps here might get us to a shortcut */
240 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
241 switch (*code) {
242 case 0xe9: /* JMP NEAR */
243 context->Eip += 3 + *(WORD *)(code+1);
244 /* yeah, I know these gotos don't look good... */
245 goto callrmproc_again;
246 case 0xea: /* JMP FAR */
247 context->Eip = *(WORD *)(code+1);
248 context->SegCs = *(WORD *)(code+3);
249 /* ...but since the label is there anyway... */
250 goto callrmproc_again;
251 case 0xeb: /* JMP SHORT */
252 context->Eip += 2 + *(signed char *)(code+1);
253 /* ...because of other gotos below, so... */
254 goto callrmproc_again;
257 /* shortcut for chaining to internal interrupt handlers */
258 if ((context->SegCs == 0xF000) && iret)
260 DOSVM_RealModeInterrupt( LOWORD(context->Eip)/4, context);
261 return 0;
264 /* shortcut for RMCBs */
265 CurrRMCB = FirstRMCB;
267 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
268 CurrRMCB = CurrRMCB->next;
270 if (!CurrRMCB && !MZ_Current())
272 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
273 TRACE("creating VM86 task\n");
274 MZ_AllocDPMITask();
276 if (!already) {
277 if (!context->SegSs) {
278 alloc = 1; /* allocate default stack */
279 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
280 context->Esp = 64-2;
281 stack16 += 32-1;
282 if (!addr) {
283 ERR("could not allocate default stack\n");
284 return 1;
286 } else {
287 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
289 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
290 stack16 -= args;
291 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
292 /* push flags if iret */
293 if (iret) {
294 stack16--; args++;
295 *stack16 = LOWORD(context->EFlags);
297 /* push return address (return to interrupt wrapper) */
298 *(--stack16) = DOSMEM_wrap_seg;
299 *(--stack16) = 0;
300 /* adjust stack */
301 context->Esp -= 2*sizeof(WORD);
302 already = 1;
305 if (CurrRMCB) {
306 /* RMCB call, invoke protected-mode handler directly */
307 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
308 /* check if we returned to where we thought we would */
309 if ((context->SegCs != DOSMEM_wrap_seg) ||
310 (LOWORD(context->Eip) != 0)) {
311 /* we need to continue at different address in real-mode space,
312 so we need to set it all up for real mode again */
313 goto callrmproc_again;
315 } else {
316 TRACE("entering real mode...\n");
317 DOSVM_Enter( context );
318 TRACE("returned from real-mode call\n");
320 if (alloc) DOSMEM_FreeBlock( addr );
321 return 0;
325 /**********************************************************************
326 * DOSVM_CallRMInt (WINEDOS.@)
328 void WINAPI DOSVM_CallRMInt( CONTEXT86 *context )
330 CONTEXT86 realmode_ctx;
331 FARPROC16 rm_int = DOSVM_GetRMHandler( BL_reg(context) );
332 REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
333 INT_GetRealModeContext( call, &realmode_ctx );
335 /* we need to check if a real-mode program has hooked the interrupt */
336 if (HIWORD(rm_int)!=0xF000) {
337 /* yup, which means we need to switch to real mode... */
338 realmode_ctx.SegCs = HIWORD(rm_int);
339 realmode_ctx.Eip = LOWORD(rm_int);
340 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
341 SET_CFLAG(context);
342 } else {
343 RESET_CFLAG(context);
344 /* use the IP we have instead of BL_reg, in case some apps
345 decide to move interrupts around for whatever reason... */
346 DOSVM_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx );
348 INT_SetRealModeContext( call, &realmode_ctx );
352 /**********************************************************************
353 * DOSVM_CallRMProc (WINEDOS.@)
355 void WINAPI DOSVM_CallRMProc( CONTEXT86 *context, int iret )
357 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
358 CONTEXT86 context16;
360 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
361 p->eax, p->ebx, p->ecx, p->edx);
362 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
363 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
365 if (!(p->cs) && !(p->ip)) { /* remove this check
366 if Int21/6501 case map function
367 has been implemented */
368 SET_CFLAG(context);
369 return;
371 INT_GetRealModeContext(p, &context16);
372 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
373 CX_reg(context), iret );
374 INT_SetRealModeContext(p, &context16);
378 /* (see dosmem.c, function DOSMEM_InitDPMI) */
379 static void StartPM( CONTEXT86 *context )
381 UINT16 cs, ss, ds, es;
382 CONTEXT86 pm_ctx;
383 DWORD psp_ofs = (DWORD)(DOSVM_psp<<4);
384 PDB16 *psp = (PDB16 *)psp_ofs;
385 HANDLE16 env_seg = psp->environment;
386 unsigned char selflags = WINE_LDT_FLAGS_DATA;
388 RESET_CFLAG(context);
389 dpmi_flag = AX_reg(context);
390 /* our mode switch wrapper have placed the desired CS into DX */
391 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
392 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
393 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
394 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
395 32-bit code using this stack. */
396 if (dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
397 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
398 /* do the same for the data segments, just in case */
399 if (context->SegDs == context->SegSs) ds = ss;
400 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
401 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
402 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
403 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
405 pm_ctx = *context;
406 pm_ctx.SegCs = DOSMEM_dpmi_sel;
407 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
408 pm_ctx.Eax = ss;
409 pm_ctx.Edx = cs;
410 pm_ctx.SegDs = ds;
411 pm_ctx.SegEs = es;
412 pm_ctx.SegFs = 0;
413 pm_ctx.SegGs = 0;
415 TRACE("DOS program is now entering protected mode\n");
416 wine_call_to_16_regs_short(&pm_ctx, 0);
418 /* in the current state of affairs, we won't ever actually return here... */
419 /* we should have int21/ah=4c do it someday, though... */
421 FreeSelector16(psp->environment);
422 psp->environment = env_seg;
423 FreeSelector16(es);
424 if (ds != ss) FreeSelector16(ds);
425 FreeSelector16(ss);
426 FreeSelector16(cs);
429 static RMCB *DPMI_AllocRMCB( void )
431 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
432 UINT16 uParagraph;
434 if (NewRMCB)
436 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
437 LPBYTE p = RMCBmem;
439 *p++ = 0xcd; /* RMCB: */
440 *p++ = 0x31; /* int $0x31 */
441 /* it is the called procedure's task to change the return CS:EIP
442 the DPMI 0.9 spec states that if it doesn't, it will be called again */
443 *p++ = 0xeb;
444 *p++ = 0xfc; /* jmp RMCB */
445 NewRMCB->address = MAKELONG(0, uParagraph);
446 NewRMCB->next = FirstRMCB;
447 FirstRMCB = NewRMCB;
449 return NewRMCB;
453 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
455 RMCB *NewRMCB = DPMI_AllocRMCB();
457 if (NewRMCB) {
458 NewRMCB->proc_ofs = (DWORD)proc;
459 NewRMCB->proc_sel = 0;
460 NewRMCB->regs_ofs = 0;
461 NewRMCB->regs_sel = 0;
462 return (FARPROC16)(NewRMCB->address);
464 return NULL;
468 static int DPMI_FreeRMCB( DWORD address )
470 RMCB *CurrRMCB = FirstRMCB;
471 RMCB *PrevRMCB = NULL;
473 while (CurrRMCB && (CurrRMCB->address != address))
475 PrevRMCB = CurrRMCB;
476 CurrRMCB = CurrRMCB->next;
478 if (CurrRMCB)
480 if (PrevRMCB)
481 PrevRMCB->next = CurrRMCB->next;
482 else
483 FirstRMCB = CurrRMCB->next;
484 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
485 HeapFree(GetProcessHeap(), 0, CurrRMCB);
486 return 0;
488 return 1;
492 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
494 DPMI_FreeRMCB( (DWORD)proc );
498 /* DPMI Raw Mode Switch handler */
500 #if 0
501 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
503 LPDOSTASK lpDosTask = MZ_Current();
504 CONTEXT86 rm_ctx;
505 int ret;
507 if (!lpDosTask) {
508 /* we could probably start a DPMI-only dosmod task here, but I doubt
509 anything other than real DOS apps want to call raw mode switch */
510 ERR("attempting raw mode switch without DOS task!\n");
511 ExitProcess(1);
513 /* initialize real-mode context as per spec */
514 memset(&rm_ctx, 0, sizeof(rm_ctx));
515 rm_ctx.SegDs = AX_sig(context);
516 rm_ctx.SegEs = CX_sig(context);
517 rm_ctx.SegSs = DX_sig(context);
518 rm_ctx.Esp = EBX_sig(context);
519 rm_ctx.SegCs = SI_sig(context);
520 rm_ctx.Eip = EDI_sig(context);
521 rm_ctx.Ebp = EBP_sig(context);
522 rm_ctx.SegFs = 0;
523 rm_ctx.SegGs = 0;
524 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
526 /* enter real mode again */
527 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
528 ret = DOSVM_Enter( &rm_ctx );
529 /* when the real-mode stuff call its mode switch address,
530 DOSVM_Enter will return and we will continue here */
532 if (ret<0) {
533 /* if the sync was lost, there's no way to recover */
534 ExitProcess(1);
537 /* alter protected-mode context as per spec */
538 DS_sig(context) = LOWORD(rm_ctx.Eax);
539 ES_sig(context) = LOWORD(rm_ctx.Ecx);
540 SS_sig(context) = LOWORD(rm_ctx.Edx);
541 ESP_sig(context) = rm_ctx.Ebx;
542 CS_sig(context) = LOWORD(rm_ctx.Esi);
543 EIP_sig(context) = rm_ctx.Edi;
544 EBP_sig(context) = rm_ctx.Ebp;
545 FS_sig(context) = 0;
546 GS_sig(context) = 0;
548 /* Return to new address and hope that we didn't mess up */
549 TRACE("re-entering protected mode at %04x:%08lx\n",
550 CS_sig(context), EIP_sig(context));
552 #endif
555 /**********************************************************************
556 * DOSVM_AllocRMCB (WINEDOS.@)
558 void WINAPI DOSVM_AllocRMCB( CONTEXT86 *context )
560 RMCB *NewRMCB = DPMI_AllocRMCB();
562 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
564 if (NewRMCB)
566 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
567 NewRMCB->proc_ofs = LOWORD(context->Esi);
568 NewRMCB->proc_sel = context->SegDs;
569 NewRMCB->regs_ofs = LOWORD(context->Edi);
570 NewRMCB->regs_sel = context->SegEs;
571 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
572 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
574 else
576 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
577 SET_CFLAG(context);
582 /**********************************************************************
583 * DOSVM_FreeRMCB (WINEDOS.@)
585 void WINAPI DOSVM_FreeRMCB( CONTEXT86 *context )
587 FIXME("callback address: %04x:%04x\n",
588 CX_reg(context), DX_reg(context));
590 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
591 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
592 SET_CFLAG(context);
597 /**********************************************************************
598 * DOSVM_Int31Handler
600 * Handler for real-mode int 31h (DPMI).
602 void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
604 /* check if it's our wrapper */
605 TRACE("called from real mode\n");
606 if (context->SegCs==DOSMEM_dpmi_seg) {
607 /* This is the protected mode switch */
608 StartPM(context);
609 return;
611 else if (context->SegCs==DOSMEM_xms_seg)
613 /* This is the XMS driver entry point */
614 XMS_Handler(context);
615 return;
617 else
619 /* Check for RMCB */
620 RMCB *CurrRMCB = FirstRMCB;
622 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
623 CurrRMCB = CurrRMCB->next;
625 if (CurrRMCB) {
626 /* RMCB call, propagate to protected-mode handler */
627 DPMI_CallRMCBProc(context, CurrRMCB, dpmi_flag);
628 return;
632 /* chain to protected mode handler */
633 INT_Int31Handler( context );