Removed @PROGEXT@ (it was broken anyway).
[wine.git] / msdos / dpmi.c
blob0bd0898baabb77605d6cf9d51cc72d45d7962b8b
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <unistd.h>
8 #include <string.h>
9 #include "windef.h"
10 #include "wine/winbase16.h"
11 #include "ldt.h"
12 #include "global.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "msdos.h"
16 #include "task.h"
17 #include "thread.h" /* for !MZ_SUPPORTED */
18 #include "stackframe.h" /* for !MZ_SUPPORTED */
19 #include "toolhelp.h"
20 #include "selectors.h"
21 #include "process.h"
22 #include "callback.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(int31);
27 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
29 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
31 static void* lastvalloced = NULL;
33 /* Structure for real-mode callbacks */
34 typedef struct
36 DWORD edi;
37 DWORD esi;
38 DWORD ebp;
39 DWORD reserved;
40 DWORD ebx;
41 DWORD edx;
42 DWORD ecx;
43 DWORD eax;
44 WORD fl;
45 WORD es;
46 WORD ds;
47 WORD fs;
48 WORD gs;
49 WORD ip;
50 WORD cs;
51 WORD sp;
52 WORD ss;
53 } REALMODECALL;
57 typedef struct tagRMCB {
58 DWORD address;
59 DWORD proc_ofs,proc_sel;
60 DWORD regs_ofs,regs_sel;
61 struct tagRMCB *next;
62 } RMCB;
64 static RMCB *FirstRMCB = NULL;
66 UINT16 DPMI_wrap_seg;
68 /**********************************************************************
69 * DPMI_xalloc
70 * special virtualalloc, allocates lineary monoton growing memory.
71 * (the usual VirtualAlloc does not satisfy that restriction)
73 static LPVOID
74 DPMI_xalloc(int len) {
75 LPVOID ret;
76 LPVOID oldlastv = lastvalloced;
78 if (lastvalloced) {
79 int xflag = 0;
80 ret = NULL;
81 while (!ret) {
82 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
83 if (!ret)
84 lastvalloced = (char *) lastvalloced + 0x10000;
85 /* we failed to allocate one in the first round.
86 * try non-linear
88 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
89 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
90 xflag++;
92 /* if we even fail to allocate something in the next
93 * round, return NULL
95 if ((xflag==1) && (lastvalloced >= oldlastv))
96 xflag++;
97 if ((xflag==2) && (lastvalloced < oldlastv)) {
98 FIXME("failed to allocate any memory of %d bytes!\n",len);
99 return NULL;
102 } else
103 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
104 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
105 return ret;
108 static void
109 DPMI_xfree(LPVOID ptr) {
110 VirtualFree(ptr,0,MEM_RELEASE);
113 /* FIXME: perhaps we could grow this mapped area... */
114 static LPVOID
115 DPMI_xrealloc(LPVOID ptr,int newsize) {
116 MEMORY_BASIC_INFORMATION mbi;
117 LPVOID newptr;
119 newptr = DPMI_xalloc(newsize);
120 if (ptr) {
121 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
122 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
123 return NULL;
125 if (mbi.State == MEM_FREE) {
126 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
127 return NULL;
129 /* We do not shrink allocated memory. most reallocs
130 * only do grows anyway
132 if (newsize<=mbi.RegionSize)
133 return ptr;
134 memcpy(newptr,ptr,mbi.RegionSize);
135 DPMI_xfree(ptr);
137 return newptr;
139 /**********************************************************************
140 * INT_GetRealModeContext
142 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
144 EAX_reg(context) = call->eax;
145 EBX_reg(context) = call->ebx;
146 ECX_reg(context) = call->ecx;
147 EDX_reg(context) = call->edx;
148 ESI_reg(context) = call->esi;
149 EDI_reg(context) = call->edi;
150 EBP_reg(context) = call->ebp;
151 EFL_reg(context) = call->fl | V86_FLAG;
152 EIP_reg(context) = call->ip;
153 ESP_reg(context) = call->sp;
154 CS_reg(context) = call->cs;
155 DS_reg(context) = call->ds;
156 ES_reg(context) = call->es;
157 FS_reg(context) = call->fs;
158 GS_reg(context) = call->gs;
159 SS_reg(context) = call->ss;
160 V86BASE(context) = (DWORD) DOSMEM_MemoryBase(0);
164 /**********************************************************************
165 * INT_SetRealModeContext
167 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
169 call->eax = EAX_reg(context);
170 call->ebx = EBX_reg(context);
171 call->ecx = ECX_reg(context);
172 call->edx = EDX_reg(context);
173 call->esi = ESI_reg(context);
174 call->edi = EDI_reg(context);
175 call->ebp = EBP_reg(context);
176 call->fl = LOWORD(EFL_reg(context));
177 call->ip = LOWORD(EIP_reg(context));
178 call->sp = LOWORD(ESP_reg(context));
179 call->cs = CS_reg(context);
180 call->ds = DS_reg(context);
181 call->es = ES_reg(context);
182 call->fs = FS_reg(context);
183 call->gs = GS_reg(context);
184 call->ss = SS_reg(context);
188 /**********************************************************************
189 * DPMI_CallRMCBProc
191 * This routine does the hard work of calling a callback procedure.
193 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
195 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
196 /* Wine-internal RMCB, call directly */
197 ((RMCBPROC)rmcb->proc_ofs)(context);
198 } else {
199 #ifdef __i386__
200 UINT16 ss,es;
201 DWORD edi;
203 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
204 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase(0) + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
206 FIXME("untested!\n");
208 /* The called proc ends with an IRET, and takes these parameters:
209 * DS:ESI = pointer to real-mode SS:SP
210 * ES:EDI = pointer to real-mode call structure
211 * It returns:
212 * ES:EDI = pointer to real-mode call structure (may be a copy)
213 * It is the proc's responsibility to change the return CS:IP in the
214 * real-mode call structure. */
215 if (flag & 1) {
216 /* 32-bit DPMI client */
217 #if HAVE_FIXED_BROKEN_ASSEMBLER_BELOW
218 int _clobber;
219 __asm__ __volatile__(
220 "pushl %%es\n"
221 "pushl %%ds\n"
222 "pushfl\n"
223 "movl %5,%%es\n" /* BAD: we are pushing potential stack
224 * parameters on an already modified
225 * stack
227 "movl %4,%%ds\n"
228 "lcall %3\n"
229 "popl %%ds\n"
230 "movl %%es,%0\n"
231 "popl %%es\n"
232 : "=g" (es), "=D" (edi), "=S" (_clobber)
233 : "m" (rmcb->proc_ofs),
234 "g" (ss), "g" (rmcb->regs_sel),
235 "S" (ESP_reg(context)), "1" (rmcb->regs_ofs)
236 : "ecx", "edx", "ebp" );
237 /* BAD: uses too much registers which is starving the register
238 * alloc stage of gcc, especially in -fPIC.
240 #else
241 FIXME("32 bit DPMI client unsupported.\n");
242 #endif
243 } else {
244 /* 16-bit DPMI client */
245 CONTEXT86 ctx = *context;
246 CS_reg(&ctx) = rmcb->proc_sel;
247 EIP_reg(&ctx) = rmcb->proc_ofs;
248 DS_reg(&ctx) = ss;
249 ESI_reg(&ctx) = ESP_reg(context);
250 ES_reg(&ctx) = rmcb->regs_sel;
251 EDI_reg(&ctx) = rmcb->regs_ofs;
252 Callbacks->CallRegisterShortProc(&ctx, 2);
253 es = ES_reg(&ctx);
254 edi = EDI_reg(&ctx);
256 SELECTOR_FreeBlock(ss, 1);
257 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
258 #else
259 ERR("RMCBs only implemented for i386\n");
260 #endif
265 /**********************************************************************
266 * DPMI_CallRMProc
268 * This routine does the hard work of calling a real mode procedure.
270 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
272 LPWORD stack16;
273 #ifndef MZ_SUPPORTED
274 WORD sel;
275 SEGPTR seg_addr;
276 #endif /* !MZ_SUPPORTED */
277 LPVOID addr = NULL; /* avoid gcc warning */
278 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
279 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
280 RMCB *CurrRMCB;
281 int alloc = 0, already = 0;
282 BYTE *code;
284 GlobalUnlock16( GetCurrentTask() );
286 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
287 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
288 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
289 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
290 CS_reg(context), LOWORD(EIP_reg(context)), args, iret?"IRET":"FAR" );
292 callrmproc_again:
294 /* there might be some code that just jumps to RMCBs or the like,
295 in which case following the jumps here might get us to a shortcut */
296 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
297 switch (*code) {
298 case 0xe9: /* JMP NEAR */
299 EIP_reg(context) += 3 + *(WORD *)(code+1);
300 /* yeah, I know these gotos don't look good... */
301 goto callrmproc_again;
302 case 0xea: /* JMP FAR */
303 EIP_reg(context) = *(WORD *)(code+1);
304 CS_reg(context) = *(WORD *)(code+3);
305 /* ...but since the label is there anyway... */
306 goto callrmproc_again;
307 case 0xeb: /* JMP SHORT */
308 EIP_reg(context) += 2 + *(signed char *)(code+1);
309 /* ...because of other gotos below, so... */
310 goto callrmproc_again;
313 /* shortcut for chaining to internal interrupt handlers */
314 if ((CS_reg(context) == 0xF000) && iret) {
315 return INT_RealModeInterrupt( LOWORD(EIP_reg(context))/4, context);
318 /* shortcut for RMCBs */
319 CurrRMCB = FirstRMCB;
321 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
322 CurrRMCB = CurrRMCB->next;
324 #ifdef MZ_SUPPORTED
325 if (!(CurrRMCB || pModule->lpDosTask)) {
326 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
327 TRACE("creating VM86 task\n");
328 if (!MZ_InitTask( MZ_AllocDPMITask( pModule->self ) )) {
329 ERR("could not setup VM86 task\n");
330 return 1;
333 #endif
334 if (!already) {
335 #ifdef MZ_SUPPORTED
336 if (!SS_reg(context)) {
337 alloc = 1; /* allocate default stack */
338 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
339 ESP_reg(context) = 64-2;
340 stack16 += 32-1;
341 if (!addr) {
342 ERR("could not allocate default stack\n");
343 return 1;
345 } else {
346 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
348 ESP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
349 #else
350 stack16 = (LPWORD) CURRENT_STACK16;
351 #endif
352 stack16 -= args;
353 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
354 /* push flags if iret */
355 if (iret) {
356 stack16--; args++;
357 *stack16 = LOWORD(EFL_reg(context));
359 #ifdef MZ_SUPPORTED
360 /* push return address (return to interrupt wrapper) */
361 *(--stack16) = DPMI_wrap_seg;
362 *(--stack16) = 0;
363 /* adjust stack */
364 ESP_reg(context) -= 2*sizeof(WORD);
365 #endif
366 already = 1;
369 if (CurrRMCB) {
370 /* RMCB call, invoke protected-mode handler directly */
371 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
372 /* check if we returned to where we thought we would */
373 if ((CS_reg(context) != DPMI_wrap_seg) ||
374 (LOWORD(EIP_reg(context)) != 0)) {
375 /* we need to continue at different address in real-mode space,
376 so we need to set it all up for real mode again */
377 goto callrmproc_again;
379 } else {
380 #ifdef MZ_SUPPORTED
381 #if 0 /* this was probably unnecessary */
382 /* push call address */
383 *(--stack16) = CS_reg(context);
384 *(--stack16) = LOWORD(EIP_reg(context));
385 /* adjust stack */
386 ESP_reg(context) -= 2*sizeof(WORD);
387 /* set initial CS:IP to the wrapper's "lret" */
388 CS_reg(context) = DPMI_wrap_seg;
389 EIP_reg(context) = 2;
390 #endif
391 TRACE("entering real mode...\n");
392 DOSVM_Enter( context );
393 TRACE("returned from real-mode call\n");
394 #else
395 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
396 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
397 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
399 CS_reg(context) = HIWORD(seg_addr);
400 EIP_reg(context) = LOWORD(seg_addr);
401 EBP_reg(context) = OFFSETOF( NtCurrentTeb()->cur_stack )
402 + (WORD)&((STACK16FRAME*)0)->bp;
403 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
404 SELECTOR_FreeBlock(sel, 1);
405 #endif
407 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
408 return 0;
412 /**********************************************************************
413 * CallRMInt
415 static void CallRMInt( CONTEXT86 *context )
417 CONTEXT86 realmode_ctx;
418 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
419 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
420 DI_reg(context) );
421 INT_GetRealModeContext( call, &realmode_ctx );
423 /* we need to check if a real-mode program has hooked the interrupt */
424 if (HIWORD(rm_int)!=0xF000) {
425 /* yup, which means we need to switch to real mode... */
426 CS_reg(&realmode_ctx) = HIWORD(rm_int);
427 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
428 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
429 SET_CFLAG(context);
430 } else {
431 RESET_CFLAG(context);
432 /* use the IP we have instead of BL_reg, in case some apps
433 decide to move interrupts around for whatever reason... */
434 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
435 SET_CFLAG(context);
436 if (EFL_reg(context)&1) {
437 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
438 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
439 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
440 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
441 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
442 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
445 INT_SetRealModeContext( call, &realmode_ctx );
449 static void CallRMProc( CONTEXT86 *context, int iret )
451 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
452 CONTEXT86 context16;
454 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
455 p->eax, p->ebx, p->ecx, p->edx);
456 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
457 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
459 if (!(p->cs) && !(p->ip)) { /* remove this check
460 if Int21/6501 case map function
461 has been implemented */
462 SET_CFLAG(context);
463 return;
465 INT_GetRealModeContext(p, &context16);
466 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context))))+3,
467 CX_reg(context), iret );
468 INT_SetRealModeContext(p, &context16);
472 static void WINAPI WINE_UNUSED RMCallbackProc( RMCB *rmcb )
474 /* This routine should call DPMI_CallRMCBProc, but we don't have the
475 register structure available - this is easily fixed by going through
476 a Win16 register relay instead of calling RMCallbackProc "directly",
477 but I won't bother at this time. */
478 FIXME("not properly supported on your architecture!\n");
481 static RMCB *DPMI_AllocRMCB( void )
483 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
484 UINT16 uParagraph;
486 if (NewRMCB)
488 #ifdef MZ_SUPPORTED
489 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
490 LPBYTE p = RMCBmem;
492 *p++ = 0xcd; /* RMCB: */
493 *p++ = 0x31; /* int $0x31 */
494 /* it is the called procedure's task to change the return CS:EIP
495 the DPMI 0.9 spec states that if it doesn't, it will be called again */
496 *p++ = 0xeb;
497 *p++ = 0xfc; /* jmp RMCB */
498 #elif defined(__i386__)
499 LPVOID RMCBmem = DOSMEM_GetBlock(0, 15, &uParagraph);
500 LPBYTE p = RMCBmem;
502 *p++ = 0x68; /* pushl */
503 *(LPVOID *)p = NewRMCB;
504 p+=4;
505 *p++ = 0x9a; /* lcall */
506 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME: register relay */
507 p+=4;
508 *(WORD *)p = __get_cs();
509 p+=2;
510 *p++=0xc3; /* lret (FIXME?) */
511 #else
512 uParagraph = 0;
513 #endif
514 NewRMCB->address = MAKELONG(0, uParagraph);
515 NewRMCB->next = FirstRMCB;
516 FirstRMCB = NewRMCB;
518 return NewRMCB;
522 static void AllocRMCB( CONTEXT86 *context )
524 RMCB *NewRMCB = DPMI_AllocRMCB();
526 TRACE("Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
528 if (NewRMCB)
530 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
531 NewRMCB->proc_ofs = SI_reg(context);
532 NewRMCB->proc_sel = DS_reg(context);
533 NewRMCB->regs_ofs = DI_reg(context);
534 NewRMCB->regs_sel = ES_reg(context);
535 SET_LOWORD( ECX_reg(context), HIWORD(NewRMCB->address) );
536 SET_LOWORD( EDX_reg(context), LOWORD(NewRMCB->address) );
538 else
540 SET_LOWORD( EAX_reg(context), 0x8015 ); /* callback unavailable */
541 SET_CFLAG(context);
546 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
548 RMCB *NewRMCB = DPMI_AllocRMCB();
550 if (NewRMCB) {
551 NewRMCB->proc_ofs = (DWORD)proc;
552 NewRMCB->proc_sel = 0;
553 NewRMCB->regs_ofs = 0;
554 NewRMCB->regs_sel = 0;
555 return (FARPROC16)(NewRMCB->address);
557 return NULL;
561 static int DPMI_FreeRMCB( DWORD address )
563 RMCB *CurrRMCB = FirstRMCB;
564 RMCB *PrevRMCB = NULL;
566 while (CurrRMCB && (CurrRMCB->address != address))
568 PrevRMCB = CurrRMCB;
569 CurrRMCB = CurrRMCB->next;
571 if (CurrRMCB)
573 if (PrevRMCB)
574 PrevRMCB->next = CurrRMCB->next;
575 else
576 FirstRMCB = CurrRMCB->next;
577 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
578 HeapFree(GetProcessHeap(), 0, CurrRMCB);
579 return 0;
581 return 1;
585 static void 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( EAX_reg(context), 0x8024 ); /* invalid callback address */
592 SET_CFLAG(context);
597 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
599 DPMI_FreeRMCB( (DWORD)proc );
603 #ifdef MZ_SUPPORTED
604 /* (see loader/dos/module.c, function MZ_InitDPMI) */
606 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
608 char *base = DOSMEM_MemoryBase(0);
609 UINT16 cs, ss, ds, es;
610 CONTEXT86 pm_ctx;
611 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
612 PDB16 *psp = (PDB16 *)(base + psp_ofs);
613 HANDLE16 env_seg = psp->environment;
614 int is32;
616 RESET_CFLAG(context);
617 lpDosTask->dpmi_flag = AX_reg(context);
618 is32 = lpDosTask->dpmi_flag & 1;
619 /* our mode switch wrapper have placed the desired CS into DX */
620 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
621 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
622 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
623 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
624 32-bit code using this stack. */
625 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
626 /* do the same for the data segments, just in case */
627 if (DS_reg(context) == SS_reg(context)) ds = ss;
628 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
629 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
630 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
631 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
632 0x10000, SEGMENT_DATA, FALSE, FALSE );
634 pm_ctx = *context;
635 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
636 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
637 EAX_reg(&pm_ctx) = ss;
638 EDX_reg(&pm_ctx) = cs;
639 DS_reg(&pm_ctx) = ds;
640 ES_reg(&pm_ctx) = es;
641 FS_reg(&pm_ctx) = 0;
642 GS_reg(&pm_ctx) = 0;
644 TRACE("DOS program is now entering protected mode\n");
645 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
647 /* in the current state of affairs, we won't ever actually return here... */
648 /* we should have int21/ah=4c do it someday, though... */
650 SELECTOR_FreeBlock(psp->environment, 1);
651 psp->environment = env_seg;
652 SELECTOR_FreeBlock(es, 1);
653 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
654 SELECTOR_FreeBlock(ss, 1);
655 SELECTOR_FreeBlock(cs, 1);
658 /* DPMI Raw Mode Switch handler */
660 #if 0
661 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
663 LPDOSTASK lpDosTask = MZ_Current();
664 CONTEXT86 rm_ctx;
665 int ret;
667 if (!lpDosTask) {
668 /* we could probably start a DPMI-only dosmod task here, but I doubt
669 anything other than real DOS apps want to call raw mode switch */
670 ERR("attempting raw mode switch without DOS task!\n");
671 ExitProcess(1);
673 /* initialize real-mode context as per spec */
674 memset(&rm_ctx, 0, sizeof(rm_ctx));
675 DS_reg(&rm_ctx) = AX_sig(context);
676 ES_reg(&rm_ctx) = CX_sig(context);
677 SS_reg(&rm_ctx) = DX_sig(context);
678 ESP_reg(&rm_ctx) = EBX_sig(context);
679 CS_reg(&rm_ctx) = SI_sig(context);
680 EIP_reg(&rm_ctx) = EDI_sig(context);
681 EBP_reg(&rm_ctx) = EBP_sig(context);
682 FS_reg(&rm_ctx) = 0;
683 GS_reg(&rm_ctx) = 0;
684 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
686 /* enter real mode again */
687 TRACE("re-entering real mode at %04lx:%04lx\n",
688 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
689 ret = DOSVM_Enter( &rm_ctx );
690 /* when the real-mode stuff call its mode switch address,
691 DOSVM_Enter will return and we will continue here */
693 if (ret<0) {
694 /* if the sync was lost, there's no way to recover */
695 ExitProcess(1);
698 /* alter protected-mode context as per spec */
699 DS_sig(context) = AX_reg(&rm_ctx);
700 ES_sig(context) = CX_reg(&rm_ctx);
701 SS_sig(context) = DX_reg(&rm_ctx);
702 ESP_sig(context) = EBX_reg(&rm_ctx);
703 CS_sig(context) = SI_reg(&rm_ctx);
704 EIP_sig(context) = EDI_reg(&rm_ctx);
705 EBP_sig(context) = EBP_reg(&rm_ctx);
706 FS_sig(context) = 0;
707 GS_sig(context) = 0;
709 /* Return to new address and hope that we didn't mess up */
710 TRACE("re-entering protected mode at %04x:%08lx\n",
711 CS_sig(context), EIP_sig(context));
713 #endif
715 #else
716 #if 0
717 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
719 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
720 ExitProcess(1);
722 #endif
723 #endif
725 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
726 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
727 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
728 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
729 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
730 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
731 #define DOS_BADLIMIT(addr,base,limit) \
732 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
733 (DOS_WINE_ISDOS(addr,base) && \
734 ((addr) + (limit) > (base) + 0x110000)))
736 /**********************************************************************
737 * INT_Int31Handler
739 * Handler for int 31h (DPMI).
742 void WINAPI INT_Int31Handler( CONTEXT86 *context )
745 * Note: For Win32s processes, the whole linear address space is
746 * shifted by 0x10000 relative to the OS linear address space.
747 * See the comment in msdos/vxd.c.
749 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
751 DWORD dw;
752 BYTE *ptr;
754 LPDOSTASK lpDosTask = MZ_Current();
756 #ifdef MZ_SUPPORTED
757 if (ISV86(context) && lpDosTask) {
758 /* Called from real mode, check if it's our wrapper */
759 TRACE("called from real mode\n");
760 if (CS_reg(context)==lpDosTask->dpmi_seg) {
761 /* This is the protected mode switch */
762 StartPM(context,lpDosTask);
763 return;
764 } else
765 if (CS_reg(context)==lpDosTask->xms_seg) {
766 /* This is the XMS driver entry point */
767 XMS_Handler(context);
768 return;
769 } else
771 /* Check for RMCB */
772 RMCB *CurrRMCB = FirstRMCB;
774 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
775 CurrRMCB = CurrRMCB->next;
777 if (CurrRMCB) {
778 /* RMCB call, propagate to protected-mode handler */
779 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
780 return;
784 #endif
786 RESET_CFLAG(context);
787 switch(AX_reg(context))
789 case 0x0000: /* Allocate LDT descriptors */
790 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
791 if (!(EAX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
793 TRACE("failed\n");
794 EAX_reg(context) = 0x8011; /* descriptor unavailable */
795 SET_CFLAG(context);
797 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
798 break;
800 case 0x0001: /* Free LDT descriptor */
801 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
802 if (FreeSelector16( BX_reg(context) ))
804 EAX_reg(context) = 0x8022; /* invalid selector */
805 SET_CFLAG(context);
807 else
809 /* If a segment register contains the selector being freed, */
810 /* set it to zero. */
811 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
812 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
813 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
814 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
816 break;
818 case 0x0002: /* Real mode segment to descriptor */
819 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
821 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
822 switch(BX_reg(context))
824 case 0x0000: entryPoint = 183; break; /* __0000H */
825 case 0x0040: entryPoint = 193; break; /* __0040H */
826 case 0xa000: entryPoint = 174; break; /* __A000H */
827 case 0xb000: entryPoint = 181; break; /* __B000H */
828 case 0xb800: entryPoint = 182; break; /* __B800H */
829 case 0xc000: entryPoint = 195; break; /* __C000H */
830 case 0xd000: entryPoint = 179; break; /* __D000H */
831 case 0xe000: entryPoint = 190; break; /* __E000H */
832 case 0xf000: entryPoint = 194; break; /* __F000H */
833 default:
834 EAX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
835 break;
837 if (entryPoint)
838 EAX_reg(context) = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
839 entryPoint ));
841 break;
843 case 0x0003: /* Get next selector increment */
844 TRACE("get selector increment (__AHINCR)\n");
845 EAX_reg(context) = __AHINCR;
846 break;
848 case 0x0004: /* Lock selector (not supported) */
849 FIXME("lock selector not supported\n");
850 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
851 break;
853 case 0x0005: /* Unlock selector (not supported) */
854 FIXME("unlock selector not supported\n");
855 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
856 break;
858 case 0x0006: /* Get selector base address */
859 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
860 if (!(dw = GetSelectorBase( BX_reg(context) )))
862 EAX_reg(context) = 0x8022; /* invalid selector */
863 SET_CFLAG(context);
865 else
867 #ifdef MZ_SUPPORTED
868 if (lpDosTask) {
869 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
870 dw = DOS_WINETOAPP(dw, base);
872 #endif
873 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
874 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
876 break;
878 case 0x0007: /* Set selector base address */
879 TRACE("set selector base address (0x%04x,0x%08lx)\n",
880 BX_reg(context),
881 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
882 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
883 #ifdef MZ_SUPPORTED
884 if (lpDosTask) {
885 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
886 dw = DOS_APPTOWINE(dw, base);
888 #endif
889 SetSelectorBase(BX_reg(context), dw);
890 break;
892 case 0x0008: /* Set selector limit */
893 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
894 dw = MAKELONG( DX_reg(context), CX_reg(context) );
895 #ifdef MZ_SUPPORTED
896 if (lpDosTask) {
897 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
898 DWORD sbase = GetSelectorBase( BX_reg(context) );
899 if (!sbase) {
900 /* the app has set the limit without setting the base,
901 * it must be relying on that the default should be DOS space;
902 * so set the base address now */
903 SetSelectorBase( BX_reg(context), sbase = base );
904 if (dw == 0xffffffff) {
905 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
906 * so we have to override the limit here */
907 dw = 0x110000;
910 if (DOS_BADLIMIT(sbase, base, dw)) {
911 AX_reg(context) = 0x8021; /* invalid value */
912 SET_CFLAG(context);
913 break;
916 #endif
917 SetSelectorLimit16( BX_reg(context), dw );
918 break;
920 case 0x0009: /* Set selector access rights */
921 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
922 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
923 break;
925 case 0x000a: /* Allocate selector alias */
926 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
927 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
929 AX_reg(context) = 0x8011; /* descriptor unavailable */
930 SET_CFLAG(context);
932 break;
934 case 0x000b: /* Get descriptor */
935 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
937 ldt_entry entry;
938 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
939 #ifdef MZ_SUPPORTED
940 if (lpDosTask) {
941 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
942 entry.base = DOS_WINETOAPP(entry.base, base);
944 #endif
945 entry.base = W32S_WINE2APP(entry.base, offset);
947 /* FIXME: should use ES:EDI for 32-bit clients */
948 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
949 DI_reg(context) ), &entry );
951 break;
953 case 0x000c: /* Set descriptor */
954 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
956 ldt_entry entry;
957 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
958 DI_reg(context) ), &entry );
959 entry.base = W32S_APP2WINE(entry.base, offset);
960 #ifdef MZ_SUPPORTED
961 if (lpDosTask) {
962 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
963 entry.base = DOS_APPTOWINE(entry.base, base);
964 if (DOS_BADLIMIT(entry.base, base, entry.limit)) {
965 AX_reg(context) = 0x8021; /* invalid value */
966 SET_CFLAG(context);
967 break;
970 #endif
972 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
974 break;
976 case 0x000d: /* Allocate specific LDT descriptor */
977 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
978 AX_reg(context) = 0x8011; /* descriptor unavailable */
979 SET_CFLAG(context);
980 break;
981 case 0x0100: /* Allocate DOS memory block */
982 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
983 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
984 if (dw) {
985 AX_reg(context) = HIWORD(dw);
986 DX_reg(context) = LOWORD(dw);
987 } else {
988 AX_reg(context) = 0x0008; /* insufficient memory */
989 BX_reg(context) = DOSMEM_Available(0)>>4;
990 SET_CFLAG(context);
992 break;
993 case 0x0101: /* Free DOS memory block */
994 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
995 dw = GlobalDOSFree16(DX_reg(context));
996 if (!dw) {
997 AX_reg(context) = 0x0009; /* memory block address invalid */
998 SET_CFLAG(context);
1000 break;
1001 case 0x0200: /* get real mode interrupt vector */
1002 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
1003 BL_reg(context));
1004 SET_CFLAG(context);
1005 break;
1006 case 0x0201: /* set real mode interrupt vector */
1007 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
1008 SET_CFLAG(context);
1009 break;
1010 case 0x0204: /* Get protected mode interrupt vector */
1011 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
1012 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
1013 CX_reg(context) = HIWORD(dw);
1014 DX_reg(context) = LOWORD(dw);
1015 break;
1017 case 0x0205: /* Set protected mode interrupt vector */
1018 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
1019 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
1020 INT_SetPMHandler( BL_reg(context),
1021 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
1022 DX_reg(context) ));
1023 break;
1025 case 0x0300: /* Simulate real mode interrupt */
1026 CallRMInt( context );
1027 break;
1029 case 0x0301: /* Call real mode procedure with far return */
1030 CallRMProc( context, FALSE );
1031 break;
1033 case 0x0302: /* Call real mode procedure with interrupt return */
1034 CallRMProc( context, TRUE );
1035 break;
1037 case 0x0303: /* Allocate Real Mode Callback Address */
1038 AllocRMCB( context );
1039 break;
1041 case 0x0304: /* Free Real Mode Callback Address */
1042 FreeRMCB( context );
1043 break;
1045 case 0x0305: /* Get State Save/Restore Addresses */
1046 TRACE("get state save/restore addresses\n");
1047 /* we probably won't need this kind of state saving */
1048 AX_reg(context) = 0;
1049 /* real mode: just point to the lret */
1050 BX_reg(context) = DPMI_wrap_seg;
1051 ECX_reg(context) = 2;
1052 /* protected mode: don't have any handler yet... */
1053 FIXME("no protected-mode dummy state save/restore handler yet\n");
1054 SI_reg(context) = 0;
1055 EDI_reg(context) = 0;
1056 break;
1058 case 0x0306: /* Get Raw Mode Switch Addresses */
1059 TRACE("get raw mode switch addresses\n");
1060 if (lpDosTask) {
1061 /* real mode, point to standard DPMI return wrapper */
1062 BX_reg(context) = DPMI_wrap_seg;
1063 ECX_reg(context) = 0;
1064 /* protected mode, point to DPMI call wrapper */
1065 SI_reg(context) = lpDosTask->dpmi_sel;
1066 EDI_reg(context) = 8; /* offset of the INT 0x31 call */
1067 } else {
1068 ERR("win app attempting to get raw mode switch!\n");
1069 AX_reg(context) = 0x8001; /* unsupported function */
1070 SET_CFLAG(context);
1072 break;
1073 case 0x0400: /* Get DPMI version */
1074 TRACE("get DPMI version\n");
1076 SYSTEM_INFO si;
1078 GetSystemInfo(&si);
1079 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
1080 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
1081 CL_reg(context) = si.wProcessorLevel;
1082 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
1083 break;
1085 case 0x0500: /* Get free memory information */
1086 TRACE("get free memory information\n");
1088 MEMMANINFO mmi;
1090 mmi.dwSize = sizeof(mmi);
1091 MemManInfo16(&mmi);
1092 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1093 /* the layout is just the same as MEMMANINFO, but without
1094 * the dwSize entry.
1096 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1097 break;
1099 case 0x0501: /* Allocate memory block */
1100 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1101 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1103 AX_reg(context) = 0x8012; /* linear memory not available */
1104 SET_CFLAG(context);
1105 } else {
1106 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1107 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1109 break;
1111 case 0x0502: /* Free memory block */
1112 TRACE("free memory block (0x%08lx)\n",
1113 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1114 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1115 SI_reg(context)), offset) );
1116 break;
1118 case 0x0503: /* Resize memory block */
1119 TRACE("resize memory block (0x%08lx,%ld)\n",
1120 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1121 MAKELONG(CX_reg(context),BX_reg(context)));
1122 if (!(ptr = (BYTE *)DPMI_xrealloc(
1123 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1124 MAKELONG(CX_reg(context),BX_reg(context)))))
1126 AX_reg(context) = 0x8012; /* linear memory not available */
1127 SET_CFLAG(context);
1128 } else {
1129 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1130 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1132 break;
1134 case 0x0507: /* Modify page attributes */
1135 FIXME("modify page attributes unimplemented\n");
1136 break; /* Just ignore it */
1138 case 0x0600: /* Lock linear region */
1139 FIXME("lock linear region unimplemented\n");
1140 break; /* Just ignore it */
1142 case 0x0601: /* Unlock linear region */
1143 FIXME("unlock linear region unimplemented\n");
1144 break; /* Just ignore it */
1146 case 0x0602: /* Unlock real-mode region */
1147 FIXME("unlock realmode region unimplemented\n");
1148 break; /* Just ignore it */
1150 case 0x0603: /* Lock real-mode region */
1151 FIXME("lock realmode region unimplemented\n");
1152 break; /* Just ignore it */
1154 case 0x0604: /* Get page size */
1155 TRACE("get pagesize\n");
1156 BX_reg(context) = 0;
1157 CX_reg(context) = VIRTUAL_GetPageSize();
1158 break;
1160 case 0x0702: /* Mark page as demand-paging candidate */
1161 FIXME("mark page as demand-paging candidate\n");
1162 break; /* Just ignore it */
1164 case 0x0703: /* Discard page contents */
1165 FIXME("discard page contents\n");
1166 break; /* Just ignore it */
1168 case 0x0800: /* Physical address mapping */
1169 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1170 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1172 AX_reg(context) = 0x8021;
1173 SET_CFLAG(context);
1175 else
1177 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1178 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1179 RESET_CFLAG(context);
1181 break;
1183 default:
1184 INT_BARF( context, 0x31 );
1185 AX_reg(context) = 0x8001; /* unsupported function */
1186 SET_CFLAG(context);
1187 break;