Call DefWindowProc for WM_CTLCOLORSTATIC message when the apps does
[wine.git] / msdos / dpmi.c
blob20906f9ae3027e86f236924ce2773dbe0db4a719
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <unistd.h>
8 #include <string.h>
10 #include "config.h"
11 #include "windef.h"
12 #include "wine/winbase16.h"
13 #include "wine/port.h"
14 #include "builtin16.h"
15 #include "miscemu.h"
16 #include "msdos.h"
17 #include "dosexe.h"
18 #include "task.h"
19 #include "toolhelp.h"
20 #include "selectors.h"
21 #include "callback.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(int31);
26 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
28 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
30 static void* lastvalloced = NULL;
32 /* Structure for real-mode callbacks */
33 typedef struct
35 DWORD edi;
36 DWORD esi;
37 DWORD ebp;
38 DWORD reserved;
39 DWORD ebx;
40 DWORD edx;
41 DWORD ecx;
42 DWORD eax;
43 WORD fl;
44 WORD es;
45 WORD ds;
46 WORD fs;
47 WORD gs;
48 WORD ip;
49 WORD cs;
50 WORD sp;
51 WORD ss;
52 } REALMODECALL;
56 typedef struct tagRMCB {
57 DWORD address;
58 DWORD proc_ofs,proc_sel;
59 DWORD regs_ofs,regs_sel;
60 struct tagRMCB *next;
61 } RMCB;
63 static RMCB *FirstRMCB = NULL;
66 static LPDOSTASK WINAPI DPMI_NoCurrent(void) { return NULL; }
67 DOSVM_TABLE Dosvm = { DPMI_NoCurrent };
69 static HMODULE DosModule;
71 /**********************************************************************
72 * DPMI_LoadDosSystem
74 BOOL DPMI_LoadDosSystem(void)
76 if (DosModule) return TRUE;
77 DosModule = LoadLibraryA( "winedos.dll" );
78 if (!DosModule) {
79 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
80 return FALSE;
82 Dosvm.Current = (void *)GetProcAddress(DosModule, "GetCurrent");
83 Dosvm.LoadDPMI = (void *)GetProcAddress(DosModule, "LoadDPMI");
84 Dosvm.LoadDosExe = (void *)GetProcAddress(DosModule, "LoadDosExe");
85 Dosvm.Exec = (void *)GetProcAddress(DosModule, "Exec");
86 Dosvm.Exit = (void *)GetProcAddress(DosModule, "Exit");
87 Dosvm.Enter = (void *)GetProcAddress(DosModule, "Enter");
88 Dosvm.Wait = (void *)GetProcAddress(DosModule, "Wait");
89 Dosvm.QueueEvent = (void *)GetProcAddress(DosModule, "QueueEvent");
90 Dosvm.OutPIC = (void *)GetProcAddress(DosModule, "OutPIC");
91 Dosvm.SetTimer = (void *)GetProcAddress(DosModule, "SetTimer");
92 Dosvm.GetTimer = (void *)GetProcAddress(DosModule, "GetTimer");
93 return TRUE;
96 /**********************************************************************
97 * DPMI_xalloc
98 * special virtualalloc, allocates lineary monoton growing memory.
99 * (the usual VirtualAlloc does not satisfy that restriction)
101 static LPVOID
102 DPMI_xalloc(int len) {
103 LPVOID ret;
104 LPVOID oldlastv = lastvalloced;
106 if (lastvalloced) {
107 int xflag = 0;
108 ret = NULL;
109 while (!ret) {
110 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
111 if (!ret)
112 lastvalloced = (char *) lastvalloced + 0x10000;
113 /* we failed to allocate one in the first round.
114 * try non-linear
116 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
117 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
118 xflag++;
120 /* if we even fail to allocate something in the next
121 * round, return NULL
123 if ((xflag==1) && (lastvalloced >= oldlastv))
124 xflag++;
125 if ((xflag==2) && (lastvalloced < oldlastv)) {
126 FIXME("failed to allocate any memory of %d bytes!\n",len);
127 return NULL;
130 } else
131 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
132 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
133 return ret;
136 static void
137 DPMI_xfree(LPVOID ptr) {
138 VirtualFree(ptr,0,MEM_RELEASE);
141 /* FIXME: perhaps we could grow this mapped area... */
142 static LPVOID
143 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
144 MEMORY_BASIC_INFORMATION mbi;
145 LPVOID newptr;
147 newptr = DPMI_xalloc(newsize);
148 if (ptr) {
149 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
150 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
151 return NULL;
153 if (mbi.State == MEM_FREE) {
154 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
155 return NULL;
157 /* We do not shrink allocated memory. most reallocs
158 * only do grows anyway
160 if (newsize<=mbi.RegionSize)
161 return ptr;
162 memcpy(newptr,ptr,mbi.RegionSize);
163 DPMI_xfree(ptr);
165 return newptr;
167 /**********************************************************************
168 * INT_GetRealModeContext
170 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
172 context->Eax = call->eax;
173 context->Ebx = call->ebx;
174 context->Ecx = call->ecx;
175 context->Edx = call->edx;
176 context->Esi = call->esi;
177 context->Edi = call->edi;
178 context->Ebp = call->ebp;
179 context->EFlags = call->fl | V86_FLAG;
180 context->Eip = call->ip;
181 context->Esp = call->sp;
182 context->SegCs = call->cs;
183 context->SegDs = call->ds;
184 context->SegEs = call->es;
185 context->SegFs = call->fs;
186 context->SegGs = call->gs;
187 context->SegSs = call->ss;
191 /**********************************************************************
192 * INT_SetRealModeContext
194 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
196 call->eax = context->Eax;
197 call->ebx = context->Ebx;
198 call->ecx = context->Ecx;
199 call->edx = context->Edx;
200 call->esi = context->Esi;
201 call->edi = context->Edi;
202 call->ebp = context->Ebp;
203 call->fl = LOWORD(context->EFlags);
204 call->ip = LOWORD(context->Eip);
205 call->sp = LOWORD(context->Esp);
206 call->cs = context->SegCs;
207 call->ds = context->SegDs;
208 call->es = context->SegEs;
209 call->fs = context->SegFs;
210 call->gs = context->SegGs;
211 call->ss = context->SegSs;
214 #ifdef __i386__
216 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
217 #if 0 /* original code, which early gccs puke on */
219 int _clobber;
220 __asm__ __volatile__(
221 "pushl %%ebp\n"
222 "pushl %%ebx\n"
223 "pushl %%es\n"
224 "pushl %%ds\n"
225 "pushfl\n"
226 "mov %7,%%es\n"
227 "mov %5,%%ds\n"
228 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
229 "popl %%ds\n"
230 "mov %%es,%0\n"
231 "popl %%es\n"
232 "popl %%ebx\n"
233 "popl %%ebp\n"
234 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
235 : "0" (ss), "2" (esp),
236 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
237 "3" (&rmcb->proc_ofs) );
239 #else /* code generated by a gcc new enough */
241 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
242 "pushl %ebp\n\t"
243 "movl %esp,%ebp\n\t"
244 "pushl %edi\n\t"
245 "pushl %esi\n\t"
246 "movl 0x8(%ebp),%eax\n\t"
247 "movl 0x10(%ebp),%esi\n\t"
248 "movl 0xc(%ebp),%edx\n\t"
249 "movl 0x10(%eax),%ecx\n\t"
250 "movl 0xc(%eax),%edi\n\t"
251 "addl $0x4,%eax\n\t"
252 "pushl %ebp\n\t"
253 "pushl %ebx\n\t"
254 "pushl %es\n\t"
255 "pushl %ds\n\t"
256 "pushfl\n\t"
257 "mov %cx,%es\n\t"
258 "mov %dx,%ds\n\t"
259 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
260 "popl %ds\n\t"
261 "mov %es,%dx\n\t"
262 "popl %es\n\t"
263 "popl %ebx\n\t"
264 "popl %ebp\n\t"
265 "movl 0x14(%ebp),%eax\n\t"
266 "movw %dx,(%eax)\n\t"
267 "movl 0x18(%ebp),%edx\n\t"
268 "movl %edi,(%edx)\n\t"
269 "popl %esi\n\t"
270 "popl %edi\n\t"
271 "leave\n\t"
272 "ret")
273 #endif
275 #endif /* __i386__ */
277 /**********************************************************************
278 * DPMI_CallRMCBProc
280 * This routine does the hard work of calling a callback procedure.
282 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
284 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
285 /* Wine-internal RMCB, call directly */
286 ((RMCBPROC)rmcb->proc_ofs)(context);
287 } else {
288 #ifdef __i386__
289 UINT16 ss,es;
290 DWORD esp,edi;
292 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
293 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
294 esp = context->Esp;
296 FIXME("untested!\n");
298 /* The called proc ends with an IRET, and takes these parameters:
299 * DS:ESI = pointer to real-mode SS:SP
300 * ES:EDI = pointer to real-mode call structure
301 * It returns:
302 * ES:EDI = pointer to real-mode call structure (may be a copy)
303 * It is the proc's responsibility to change the return CS:IP in the
304 * real-mode call structure. */
305 if (flag & 1) {
306 /* 32-bit DPMI client */
307 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
308 } else {
309 /* 16-bit DPMI client */
310 CONTEXT86 ctx = *context;
311 ctx.SegCs = rmcb->proc_sel;
312 ctx.Eip = rmcb->proc_ofs;
313 ctx.SegDs = ss;
314 ctx.Esi = esp;
315 ctx.SegEs = rmcb->regs_sel;
316 ctx.Edi = rmcb->regs_ofs;
317 /* FIXME: I'm pretty sure this isn't right - should push flags first */
318 wine_call_to_16_regs_short(&ctx, 0);
319 es = ctx.SegEs;
320 edi = ctx.Edi;
322 FreeSelector16(ss);
323 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
324 #else
325 ERR("RMCBs only implemented for i386\n");
326 #endif
331 /**********************************************************************
332 * DPMI_CallRMProc
334 * This routine does the hard work of calling a real mode procedure.
336 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
338 LPWORD stack16;
339 LPVOID addr = NULL; /* avoid gcc warning */
340 LPDOSTASK lpDosTask = Dosvm.Current();
341 RMCB *CurrRMCB;
342 int alloc = 0, already = 0;
343 BYTE *code;
345 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
346 context->Eax, context->Ebx, context->Ecx, context->Edx );
347 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
348 context->Esi, context->Edi, context->SegEs, context->SegDs,
349 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
351 callrmproc_again:
353 /* there might be some code that just jumps to RMCBs or the like,
354 in which case following the jumps here might get us to a shortcut */
355 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
356 switch (*code) {
357 case 0xe9: /* JMP NEAR */
358 context->Eip += 3 + *(WORD *)(code+1);
359 /* yeah, I know these gotos don't look good... */
360 goto callrmproc_again;
361 case 0xea: /* JMP FAR */
362 context->Eip = *(WORD *)(code+1);
363 context->SegCs = *(WORD *)(code+3);
364 /* ...but since the label is there anyway... */
365 goto callrmproc_again;
366 case 0xeb: /* JMP SHORT */
367 context->Eip += 2 + *(signed char *)(code+1);
368 /* ...because of other gotos below, so... */
369 goto callrmproc_again;
372 /* shortcut for chaining to internal interrupt handlers */
373 if ((context->SegCs == 0xF000) && iret) {
374 return INT_RealModeInterrupt( LOWORD(context->Eip)/4, context);
377 /* shortcut for RMCBs */
378 CurrRMCB = FirstRMCB;
380 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
381 CurrRMCB = CurrRMCB->next;
383 if (!(CurrRMCB || lpDosTask)) {
384 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
385 TRACE("creating VM86 task\n");
386 if ((!DPMI_LoadDosSystem()) || !(lpDosTask = Dosvm.LoadDPMI() )) {
387 ERR("could not setup VM86 task\n");
388 return 1;
391 if (!already) {
392 if (!context->SegSs) {
393 alloc = 1; /* allocate default stack */
394 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
395 context->Esp = 64-2;
396 stack16 += 32-1;
397 if (!addr) {
398 ERR("could not allocate default stack\n");
399 return 1;
401 } else {
402 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
404 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
405 stack16 -= args;
406 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
407 /* push flags if iret */
408 if (iret) {
409 stack16--; args++;
410 *stack16 = LOWORD(context->EFlags);
412 /* push return address (return to interrupt wrapper) */
413 *(--stack16) = DOSMEM_wrap_seg;
414 *(--stack16) = 0;
415 /* adjust stack */
416 context->Esp -= 2*sizeof(WORD);
417 already = 1;
420 if (CurrRMCB) {
421 /* RMCB call, invoke protected-mode handler directly */
422 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
423 /* check if we returned to where we thought we would */
424 if ((context->SegCs != DOSMEM_wrap_seg) ||
425 (LOWORD(context->Eip) != 0)) {
426 /* we need to continue at different address in real-mode space,
427 so we need to set it all up for real mode again */
428 goto callrmproc_again;
430 } else {
431 TRACE("entering real mode...\n");
432 Dosvm.Enter( context );
433 TRACE("returned from real-mode call\n");
435 if (alloc) DOSMEM_FreeBlock( addr );
436 return 0;
440 /**********************************************************************
441 * CallRMInt
443 static void CallRMInt( CONTEXT86 *context )
445 CONTEXT86 realmode_ctx;
446 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
447 REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
448 INT_GetRealModeContext( call, &realmode_ctx );
450 /* we need to check if a real-mode program has hooked the interrupt */
451 if (HIWORD(rm_int)!=0xF000) {
452 /* yup, which means we need to switch to real mode... */
453 realmode_ctx.SegCs = HIWORD(rm_int);
454 realmode_ctx.Eip = LOWORD(rm_int);
455 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
456 SET_CFLAG(context);
457 } else {
458 RESET_CFLAG(context);
459 /* use the IP we have instead of BL_reg, in case some apps
460 decide to move interrupts around for whatever reason... */
461 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
462 SET_CFLAG(context);
463 if (context->EFlags & 1) {
464 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
465 BL_reg(context), realmode_ctx.Eax, realmode_ctx.Ebx,
466 realmode_ctx.Ecx, realmode_ctx.Edx);
467 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
468 realmode_ctx.Esi, realmode_ctx.Edi,
469 realmode_ctx.SegDs, realmode_ctx.SegEs );
472 INT_SetRealModeContext( call, &realmode_ctx );
476 static void CallRMProc( CONTEXT86 *context, int iret )
478 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
479 CONTEXT86 context16;
481 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
482 p->eax, p->ebx, p->ecx, p->edx);
483 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
484 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
486 if (!(p->cs) && !(p->ip)) { /* remove this check
487 if Int21/6501 case map function
488 has been implemented */
489 SET_CFLAG(context);
490 return;
492 INT_GetRealModeContext(p, &context16);
493 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
494 CX_reg(context), iret );
495 INT_SetRealModeContext(p, &context16);
499 static RMCB *DPMI_AllocRMCB( void )
501 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
502 UINT16 uParagraph;
504 if (NewRMCB)
506 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
507 LPBYTE p = RMCBmem;
509 *p++ = 0xcd; /* RMCB: */
510 *p++ = 0x31; /* int $0x31 */
511 /* it is the called procedure's task to change the return CS:EIP
512 the DPMI 0.9 spec states that if it doesn't, it will be called again */
513 *p++ = 0xeb;
514 *p++ = 0xfc; /* jmp RMCB */
515 NewRMCB->address = MAKELONG(0, uParagraph);
516 NewRMCB->next = FirstRMCB;
517 FirstRMCB = NewRMCB;
519 return NewRMCB;
523 static void AllocRMCB( CONTEXT86 *context )
525 RMCB *NewRMCB = DPMI_AllocRMCB();
527 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
529 if (NewRMCB)
531 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
532 NewRMCB->proc_ofs = LOWORD(context->Esi);
533 NewRMCB->proc_sel = context->SegDs;
534 NewRMCB->regs_ofs = LOWORD(context->Edi);
535 NewRMCB->regs_sel = context->SegEs;
536 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
537 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
539 else
541 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
542 SET_CFLAG(context);
547 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
549 RMCB *NewRMCB = DPMI_AllocRMCB();
551 if (NewRMCB) {
552 NewRMCB->proc_ofs = (DWORD)proc;
553 NewRMCB->proc_sel = 0;
554 NewRMCB->regs_ofs = 0;
555 NewRMCB->regs_sel = 0;
556 return (FARPROC16)(NewRMCB->address);
558 return NULL;
562 static int DPMI_FreeRMCB( DWORD address )
564 RMCB *CurrRMCB = FirstRMCB;
565 RMCB *PrevRMCB = NULL;
567 while (CurrRMCB && (CurrRMCB->address != address))
569 PrevRMCB = CurrRMCB;
570 CurrRMCB = CurrRMCB->next;
572 if (CurrRMCB)
574 if (PrevRMCB)
575 PrevRMCB->next = CurrRMCB->next;
576 else
577 FirstRMCB = CurrRMCB->next;
578 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
579 HeapFree(GetProcessHeap(), 0, CurrRMCB);
580 return 0;
582 return 1;
586 static void FreeRMCB( CONTEXT86 *context )
588 FIXME("callback address: %04x:%04x\n",
589 CX_reg(context), DX_reg(context));
591 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
592 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
593 SET_CFLAG(context);
598 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
600 DPMI_FreeRMCB( (DWORD)proc );
604 /* (see dosmem.c, function DOSMEM_InitDPMI) */
606 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
608 UINT16 cs, ss, ds, es;
609 CONTEXT86 pm_ctx;
610 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
611 PDB16 *psp = (PDB16 *)psp_ofs;
612 HANDLE16 env_seg = psp->environment;
613 unsigned char selflags = WINE_LDT_FLAGS_DATA;
615 RESET_CFLAG(context);
616 lpDosTask->dpmi_flag = AX_reg(context);
617 /* our mode switch wrapper have placed the desired CS into DX */
618 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
619 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
620 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
621 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
622 32-bit code using this stack. */
623 if (lpDosTask->dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
624 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
625 /* do the same for the data segments, just in case */
626 if (context->SegDs == context->SegSs) ds = ss;
627 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
628 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
629 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
630 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
632 pm_ctx = *context;
633 pm_ctx.SegCs = DOSMEM_dpmi_sel;
634 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
635 pm_ctx.Eax = ss;
636 pm_ctx.Edx = cs;
637 pm_ctx.SegDs = ds;
638 pm_ctx.SegEs = es;
639 pm_ctx.SegFs = 0;
640 pm_ctx.SegGs = 0;
642 TRACE("DOS program is now entering protected mode\n");
643 wine_call_to_16_regs_short(&pm_ctx, 0);
645 /* in the current state of affairs, we won't ever actually return here... */
646 /* we should have int21/ah=4c do it someday, though... */
648 FreeSelector16(psp->environment);
649 psp->environment = env_seg;
650 FreeSelector16(es);
651 if (ds != ss) FreeSelector16(ds);
652 FreeSelector16(ss);
653 FreeSelector16(cs);
656 /* DPMI Raw Mode Switch handler */
658 #if 0
659 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
661 LPDOSTASK lpDosTask = Dosvm.Current();
662 CONTEXT86 rm_ctx;
663 int ret;
665 if (!lpDosTask) {
666 /* we could probably start a DPMI-only dosmod task here, but I doubt
667 anything other than real DOS apps want to call raw mode switch */
668 ERR("attempting raw mode switch without DOS task!\n");
669 ExitProcess(1);
671 /* initialize real-mode context as per spec */
672 memset(&rm_ctx, 0, sizeof(rm_ctx));
673 rm_ctx.SegDs = AX_sig(context);
674 rm_ctx.SegEs = CX_sig(context);
675 rm_ctx.SegSs = DX_sig(context);
676 rm_ctx.Esp = EBX_sig(context);
677 rm_ctx.SegCs = SI_sig(context);
678 rm_ctx.Eip = EDI_sig(context);
679 rm_ctx.Ebp = EBP_sig(context);
680 rm_ctx.SegFs = 0;
681 rm_ctx.SegGs = 0;
682 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
684 /* enter real mode again */
685 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
686 ret = Dosvm.Enter( &rm_ctx );
687 /* when the real-mode stuff call its mode switch address,
688 Dosvm.Enter will return and we will continue here */
690 if (ret<0) {
691 /* if the sync was lost, there's no way to recover */
692 ExitProcess(1);
695 /* alter protected-mode context as per spec */
696 DS_sig(context) = LOWORD(rm_ctx.Eax);
697 ES_sig(context) = LOWORD(rm_ctx.Ecx);
698 SS_sig(context) = LOWORD(rm_ctx.Edx);
699 ESP_sig(context) = rm_ctx.Ebx;
700 CS_sig(context) = LOWORD(rm_ctx.Esi);
701 EIP_sig(context) = rm_ctx.Edi;
702 EBP_sig(context) = rm_ctx.Ebp;
703 FS_sig(context) = 0;
704 GS_sig(context) = 0;
706 /* Return to new address and hope that we didn't mess up */
707 TRACE("re-entering protected mode at %04x:%08lx\n",
708 CS_sig(context), EIP_sig(context));
710 #endif
713 /**********************************************************************
714 * INT_Int31Handler
716 * Handler for int 31h (DPMI).
719 void WINAPI INT_Int31Handler( CONTEXT86 *context )
722 * Note: For Win32s processes, the whole linear address space is
723 * shifted by 0x10000 relative to the OS linear address space.
724 * See the comment in msdos/vxd.c.
726 DWORD dw;
727 BYTE *ptr;
729 LPDOSTASK lpDosTask = Dosvm.Current();
731 if (ISV86(context) && lpDosTask) {
732 /* Called from real mode, check if it's our wrapper */
733 TRACE("called from real mode\n");
734 if (context->SegCs==DOSMEM_dpmi_seg) {
735 /* This is the protected mode switch */
736 StartPM(context,lpDosTask);
737 return;
738 } else
739 if (context->SegCs==DOSMEM_xms_seg) {
740 /* This is the XMS driver entry point */
741 XMS_Handler(context);
742 return;
743 } else
745 /* Check for RMCB */
746 RMCB *CurrRMCB = FirstRMCB;
748 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
749 CurrRMCB = CurrRMCB->next;
751 if (CurrRMCB) {
752 /* RMCB call, propagate to protected-mode handler */
753 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
754 return;
759 RESET_CFLAG(context);
760 switch(AX_reg(context))
762 case 0x0000: /* Allocate LDT descriptors */
763 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
764 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
766 TRACE("failed\n");
767 context->Eax = 0x8011; /* descriptor unavailable */
768 SET_CFLAG(context);
770 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
771 break;
773 case 0x0001: /* Free LDT descriptor */
774 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
775 if (FreeSelector16( BX_reg(context) ))
777 context->Eax = 0x8022; /* invalid selector */
778 SET_CFLAG(context);
780 else
782 /* If a segment register contains the selector being freed, */
783 /* set it to zero. */
784 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
785 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
786 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
787 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
789 break;
791 case 0x0002: /* Real mode segment to descriptor */
792 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
794 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
795 switch(BX_reg(context))
797 case 0x0000: entryPoint = 183; break; /* __0000H */
798 case 0x0040: entryPoint = 193; break; /* __0040H */
799 case 0xa000: entryPoint = 174; break; /* __A000H */
800 case 0xb000: entryPoint = 181; break; /* __B000H */
801 case 0xb800: entryPoint = 182; break; /* __B800H */
802 case 0xc000: entryPoint = 195; break; /* __C000H */
803 case 0xd000: entryPoint = 179; break; /* __D000H */
804 case 0xe000: entryPoint = 190; break; /* __E000H */
805 case 0xf000: entryPoint = 194; break; /* __F000H */
806 default:
807 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
808 break;
810 if (entryPoint)
811 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
812 (LPCSTR)(ULONG_PTR)entryPoint ));
814 break;
816 case 0x0003: /* Get next selector increment */
817 TRACE("get selector increment (__AHINCR)\n");
818 context->Eax = __AHINCR;
819 break;
821 case 0x0004: /* Lock selector (not supported) */
822 FIXME("lock selector not supported\n");
823 context->Eax = 0; /* FIXME: is this a correct return value? */
824 break;
826 case 0x0005: /* Unlock selector (not supported) */
827 FIXME("unlock selector not supported\n");
828 context->Eax = 0; /* FIXME: is this a correct return value? */
829 break;
831 case 0x0006: /* Get selector base address */
832 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
833 if (!(dw = GetSelectorBase( BX_reg(context) )))
835 context->Eax = 0x8022; /* invalid selector */
836 SET_CFLAG(context);
838 else
840 CX_reg(context) = HIWORD(W32S_WINE2APP(dw));
841 DX_reg(context) = LOWORD(W32S_WINE2APP(dw));
843 break;
845 case 0x0007: /* Set selector base address */
846 TRACE("set selector base address (0x%04x,0x%08lx)\n",
847 BX_reg(context),
848 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
849 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
850 if (dw < 0x10000)
851 /* app wants to access lower 64K of DOS memory, map it in now */
852 DOSMEM_Init(TRUE);
853 SetSelectorBase(BX_reg(context), dw);
854 break;
856 case 0x0008: /* Set selector limit */
857 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
858 dw = MAKELONG( DX_reg(context), CX_reg(context) );
859 SetSelectorLimit16( BX_reg(context), dw );
860 break;
862 case 0x0009: /* Set selector access rights */
863 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
864 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
865 break;
867 case 0x000a: /* Allocate selector alias */
868 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
869 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
871 AX_reg(context) = 0x8011; /* descriptor unavailable */
872 SET_CFLAG(context);
874 break;
876 case 0x000b: /* Get descriptor */
877 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
879 LDT_ENTRY entry;
880 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
881 /* FIXME: should use ES:EDI for 32-bit clients */
882 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
884 break;
886 case 0x000c: /* Set descriptor */
887 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
889 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
890 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
891 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
893 break;
895 case 0x000d: /* Allocate specific LDT descriptor */
896 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
897 AX_reg(context) = 0x8011; /* descriptor unavailable */
898 SET_CFLAG(context);
899 break;
900 case 0x0100: /* Allocate DOS memory block */
901 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
902 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
903 if (dw) {
904 AX_reg(context) = HIWORD(dw);
905 DX_reg(context) = LOWORD(dw);
906 } else {
907 AX_reg(context) = 0x0008; /* insufficient memory */
908 BX_reg(context) = DOSMEM_Available()>>4;
909 SET_CFLAG(context);
911 break;
912 case 0x0101: /* Free DOS memory block */
913 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
914 dw = GlobalDOSFree16(DX_reg(context));
915 if (!dw) {
916 AX_reg(context) = 0x0009; /* memory block address invalid */
917 SET_CFLAG(context);
919 break;
920 case 0x0200: /* get real mode interrupt vector */
921 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
922 BL_reg(context));
923 SET_CFLAG(context);
924 break;
925 case 0x0201: /* set real mode interrupt vector */
926 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
927 SET_CFLAG(context);
928 break;
929 case 0x0204: /* Get protected mode interrupt vector */
930 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
931 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
932 CX_reg(context) = HIWORD(dw);
933 DX_reg(context) = LOWORD(dw);
934 break;
936 case 0x0205: /* Set protected mode interrupt vector */
937 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
938 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
939 INT_SetPMHandler( BL_reg(context),
940 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
941 break;
943 case 0x0300: /* Simulate real mode interrupt */
944 CallRMInt( context );
945 break;
947 case 0x0301: /* Call real mode procedure with far return */
948 CallRMProc( context, FALSE );
949 break;
951 case 0x0302: /* Call real mode procedure with interrupt return */
952 CallRMProc( context, TRUE );
953 break;
955 case 0x0303: /* Allocate Real Mode Callback Address */
956 AllocRMCB( context );
957 break;
959 case 0x0304: /* Free Real Mode Callback Address */
960 FreeRMCB( context );
961 break;
963 case 0x0305: /* Get State Save/Restore Addresses */
964 TRACE("get state save/restore addresses\n");
965 /* we probably won't need this kind of state saving */
966 AX_reg(context) = 0;
967 /* real mode: just point to the lret */
968 BX_reg(context) = DOSMEM_wrap_seg;
969 context->Ecx = 2;
970 /* protected mode: don't have any handler yet... */
971 FIXME("no protected-mode dummy state save/restore handler yet\n");
972 SI_reg(context) = 0;
973 context->Edi = 0;
974 break;
976 case 0x0306: /* Get Raw Mode Switch Addresses */
977 TRACE("get raw mode switch addresses\n");
978 /* real mode, point to standard DPMI return wrapper */
979 BX_reg(context) = DOSMEM_wrap_seg;
980 context->Ecx = 0;
981 /* protected mode, point to DPMI call wrapper */
982 SI_reg(context) = DOSMEM_dpmi_sel;
983 context->Edi = 8; /* offset of the INT 0x31 call */
984 break;
985 case 0x0400: /* Get DPMI version */
986 TRACE("get DPMI version\n");
988 SYSTEM_INFO si;
990 GetSystemInfo(&si);
991 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
992 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
993 CL_reg(context) = si.wProcessorLevel;
994 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
995 break;
997 case 0x0500: /* Get free memory information */
998 TRACE("get free memory information\n");
1000 MEMMANINFO mmi;
1002 mmi.dwSize = sizeof(mmi);
1003 MemManInfo16(&mmi);
1004 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
1005 /* the layout is just the same as MEMMANINFO, but without
1006 * the dwSize entry.
1008 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1009 break;
1011 case 0x0501: /* Allocate memory block */
1012 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1013 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1015 AX_reg(context) = 0x8012; /* linear memory not available */
1016 SET_CFLAG(context);
1017 } else {
1018 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1019 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1021 break;
1023 case 0x0502: /* Free memory block */
1024 TRACE("free memory block (0x%08lx)\n",
1025 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
1026 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
1027 break;
1029 case 0x0503: /* Resize memory block */
1030 TRACE("resize memory block (0x%08lx,%ld)\n",
1031 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1032 MAKELONG(CX_reg(context),BX_reg(context)));
1033 if (!(ptr = (BYTE *)DPMI_xrealloc(
1034 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1035 MAKELONG(CX_reg(context),BX_reg(context)))))
1037 AX_reg(context) = 0x8012; /* linear memory not available */
1038 SET_CFLAG(context);
1039 } else {
1040 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1041 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1043 break;
1045 case 0x0507: /* Modify page attributes */
1046 FIXME("modify page attributes unimplemented\n");
1047 break; /* Just ignore it */
1049 case 0x0600: /* Lock linear region */
1050 FIXME("lock linear region unimplemented\n");
1051 break; /* Just ignore it */
1053 case 0x0601: /* Unlock linear region */
1054 FIXME("unlock linear region unimplemented\n");
1055 break; /* Just ignore it */
1057 case 0x0602: /* Unlock real-mode region */
1058 FIXME("unlock realmode region unimplemented\n");
1059 break; /* Just ignore it */
1061 case 0x0603: /* Lock real-mode region */
1062 FIXME("lock realmode region unimplemented\n");
1063 break; /* Just ignore it */
1065 case 0x0604: /* Get page size */
1066 TRACE("get pagesize\n");
1067 BX_reg(context) = 0;
1068 CX_reg(context) = getpagesize();
1069 break;
1071 case 0x0702: /* Mark page as demand-paging candidate */
1072 FIXME("mark page as demand-paging candidate\n");
1073 break; /* Just ignore it */
1075 case 0x0703: /* Discard page contents */
1076 FIXME("discard page contents\n");
1077 break; /* Just ignore it */
1079 case 0x0800: /* Physical address mapping */
1080 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1081 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1083 AX_reg(context) = 0x8021;
1084 SET_CFLAG(context);
1086 else
1088 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1089 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1090 RESET_CFLAG(context);
1092 break;
1094 default:
1095 INT_BARF( context, 0x31 );
1096 AX_reg(context) = 0x8001; /* unsupported function */
1097 SET_CFLAG(context);
1098 break;