Stub support for int2f/ax=1684/bx=0027 (VXDLDR).
[wine/multimedia.git] / msdos / dpmi.c
blobbe2a5964b351b20f35a957b74eb4d70254063f0f
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 "builtin16.h"
13 #include "global.h"
14 #include "miscemu.h"
15 #include "msdos.h"
16 #include "dosexe.h"
17 #include "task.h"
18 #include "toolhelp.h"
19 #include "selectors.h"
20 #include "process.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;
65 UINT16 DPMI_wrap_seg;
67 /**********************************************************************
68 * DPMI_xalloc
69 * special virtualalloc, allocates lineary monoton growing memory.
70 * (the usual VirtualAlloc does not satisfy that restriction)
72 static LPVOID
73 DPMI_xalloc(int len) {
74 LPVOID ret;
75 LPVOID oldlastv = lastvalloced;
77 if (lastvalloced) {
78 int xflag = 0;
79 ret = NULL;
80 while (!ret) {
81 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
82 if (!ret)
83 lastvalloced = (char *) lastvalloced + 0x10000;
84 /* we failed to allocate one in the first round.
85 * try non-linear
87 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
88 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
89 xflag++;
91 /* if we even fail to allocate something in the next
92 * round, return NULL
94 if ((xflag==1) && (lastvalloced >= oldlastv))
95 xflag++;
96 if ((xflag==2) && (lastvalloced < oldlastv)) {
97 FIXME("failed to allocate any memory of %d bytes!\n",len);
98 return NULL;
101 } else
102 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
103 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
104 return ret;
107 static void
108 DPMI_xfree(LPVOID ptr) {
109 VirtualFree(ptr,0,MEM_RELEASE);
112 /* FIXME: perhaps we could grow this mapped area... */
113 static LPVOID
114 DPMI_xrealloc(LPVOID ptr,int newsize) {
115 MEMORY_BASIC_INFORMATION mbi;
116 LPVOID newptr;
118 newptr = DPMI_xalloc(newsize);
119 if (ptr) {
120 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
121 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
122 return NULL;
124 if (mbi.State == MEM_FREE) {
125 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
126 return NULL;
128 /* We do not shrink allocated memory. most reallocs
129 * only do grows anyway
131 if (newsize<=mbi.RegionSize)
132 return ptr;
133 memcpy(newptr,ptr,mbi.RegionSize);
134 DPMI_xfree(ptr);
136 return newptr;
138 /**********************************************************************
139 * INT_GetRealModeContext
141 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
143 EAX_reg(context) = call->eax;
144 EBX_reg(context) = call->ebx;
145 ECX_reg(context) = call->ecx;
146 EDX_reg(context) = call->edx;
147 ESI_reg(context) = call->esi;
148 EDI_reg(context) = call->edi;
149 EBP_reg(context) = call->ebp;
150 EFL_reg(context) = call->fl | V86_FLAG;
151 EIP_reg(context) = call->ip;
152 ESP_reg(context) = call->sp;
153 CS_reg(context) = call->cs;
154 DS_reg(context) = call->ds;
155 ES_reg(context) = call->es;
156 FS_reg(context) = call->fs;
157 GS_reg(context) = call->gs;
158 SS_reg(context) = call->ss;
162 /**********************************************************************
163 * INT_SetRealModeContext
165 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
167 call->eax = EAX_reg(context);
168 call->ebx = EBX_reg(context);
169 call->ecx = ECX_reg(context);
170 call->edx = EDX_reg(context);
171 call->esi = ESI_reg(context);
172 call->edi = EDI_reg(context);
173 call->ebp = EBP_reg(context);
174 call->fl = LOWORD(EFL_reg(context));
175 call->ip = LOWORD(EIP_reg(context));
176 call->sp = LOWORD(ESP_reg(context));
177 call->cs = CS_reg(context);
178 call->ds = DS_reg(context);
179 call->es = ES_reg(context);
180 call->fs = FS_reg(context);
181 call->gs = GS_reg(context);
182 call->ss = SS_reg(context);
185 #ifdef __i386__
187 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
188 #if 0 /* original code, which early gccs puke on */
190 int _clobber;
191 __asm__ __volatile__(
192 "pushl %%ebp\n"
193 "pushl %%ebx\n"
194 "pushl %%es\n"
195 "pushl %%ds\n"
196 "pushfl\n"
197 "mov %7,%%es\n"
198 "mov %5,%%ds\n"
199 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
200 "popl %%ds\n"
201 "mov %%es,%0\n"
202 "popl %%es\n"
203 "popl %%ebx\n"
204 "popl %%ebp\n"
205 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
206 : "0" (ss), "2" (esp),
207 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
208 "3" (&rmcb->proc_ofs) );
210 #else /* code generated by a gcc new enough */
212 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
213 "pushl %ebp\n\t"
214 "movl %esp,%ebp\n\t"
215 "pushl %edi\n\t"
216 "pushl %esi\n\t"
217 "movl 0x8(%ebp),%eax\n\t"
218 "movl 0x10(%ebp),%esi\n\t"
219 "movl 0xc(%ebp),%edx\n\t"
220 "movl 0x10(%eax),%ecx\n\t"
221 "movl 0xc(%eax),%edi\n\t"
222 "addl $0x4,%eax\n\t"
223 "pushl %ebp\n\t"
224 "pushl %ebx\n\t"
225 "pushl %es\n\t"
226 "pushl %ds\n\t"
227 "pushfl\n\t"
228 "mov %cx,%es\n\t"
229 "mov %dx,%ds\n\t"
230 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
231 "popl %ds\n\t"
232 "mov %es,%dx\n\t"
233 "popl %es\n\t"
234 "popl %ebx\n\t"
235 "popl %ebp\n\t"
236 "movl 0x14(%ebp),%eax\n\t"
237 "movw %dx,(%eax)\n\t"
238 "movl 0x18(%ebp),%edx\n\t"
239 "movl %edi,(%edx)\n\t"
240 "popl %esi\n\t"
241 "popl %edi\n\t"
242 "leave\n\t"
243 "ret")
244 #endif
246 #endif /* __i386__ */
248 /**********************************************************************
249 * DPMI_CallRMCBProc
251 * This routine does the hard work of calling a callback procedure.
253 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
255 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
256 /* Wine-internal RMCB, call directly */
257 ((RMCBPROC)rmcb->proc_ofs)(context);
258 } else {
259 #ifdef __i386__
260 UINT16 ss,es;
261 DWORD esp,edi;
263 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
264 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase() + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
265 esp = ESP_reg(context);
267 FIXME("untested!\n");
269 /* The called proc ends with an IRET, and takes these parameters:
270 * DS:ESI = pointer to real-mode SS:SP
271 * ES:EDI = pointer to real-mode call structure
272 * It returns:
273 * ES:EDI = pointer to real-mode call structure (may be a copy)
274 * It is the proc's responsibility to change the return CS:IP in the
275 * real-mode call structure. */
276 if (flag & 1) {
277 /* 32-bit DPMI client */
278 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
279 } else {
280 /* 16-bit DPMI client */
281 CONTEXT86 ctx = *context;
282 CS_reg(&ctx) = rmcb->proc_sel;
283 EIP_reg(&ctx) = rmcb->proc_ofs;
284 DS_reg(&ctx) = ss;
285 ESI_reg(&ctx) = esp;
286 ES_reg(&ctx) = rmcb->regs_sel;
287 EDI_reg(&ctx) = rmcb->regs_ofs;
288 /* FIXME: I'm pretty sure this isn't right - should push flags first */
289 CallTo16RegisterShort(&ctx, 0);
290 es = ES_reg(&ctx);
291 edi = EDI_reg(&ctx);
293 SELECTOR_FreeBlock(ss, 1);
294 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
295 #else
296 ERR("RMCBs only implemented for i386\n");
297 #endif
302 /**********************************************************************
303 * DPMI_CallRMProc
305 * This routine does the hard work of calling a real mode procedure.
307 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
309 LPWORD stack16;
310 LPVOID addr = NULL; /* avoid gcc warning */
311 LPDOSTASK lpDosTask = MZ_Current();
312 RMCB *CurrRMCB;
313 int alloc = 0, already = 0;
314 BYTE *code;
316 GlobalUnlock16( GetCurrentTask() );
318 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
319 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
320 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
321 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
322 CS_reg(context), LOWORD(EIP_reg(context)), args, iret?"IRET":"FAR" );
324 callrmproc_again:
326 /* there might be some code that just jumps to RMCBs or the like,
327 in which case following the jumps here might get us to a shortcut */
328 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
329 switch (*code) {
330 case 0xe9: /* JMP NEAR */
331 EIP_reg(context) += 3 + *(WORD *)(code+1);
332 /* yeah, I know these gotos don't look good... */
333 goto callrmproc_again;
334 case 0xea: /* JMP FAR */
335 EIP_reg(context) = *(WORD *)(code+1);
336 CS_reg(context) = *(WORD *)(code+3);
337 /* ...but since the label is there anyway... */
338 goto callrmproc_again;
339 case 0xeb: /* JMP SHORT */
340 EIP_reg(context) += 2 + *(signed char *)(code+1);
341 /* ...because of other gotos below, so... */
342 goto callrmproc_again;
345 /* shortcut for chaining to internal interrupt handlers */
346 if ((CS_reg(context) == 0xF000) && iret) {
347 return INT_RealModeInterrupt( LOWORD(EIP_reg(context))/4, context);
350 /* shortcut for RMCBs */
351 CurrRMCB = FirstRMCB;
353 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
354 CurrRMCB = CurrRMCB->next;
356 if (!(CurrRMCB || lpDosTask)) {
357 #ifdef MZ_SUPPORTED
358 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
359 TRACE("creating VM86 task\n");
360 if (!MZ_InitTask( lpDosTask = MZ_AllocDPMITask() )) {
361 ERR("could not setup VM86 task\n");
362 return 1;
364 #else
365 ERR("Actual real-mode calls not supported on this architecture!\n");
366 return 1;
367 #endif
369 if (!already) {
370 if (!SS_reg(context)) {
371 alloc = 1; /* allocate default stack */
372 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(SS_reg(context)) );
373 ESP_reg(context) = 64-2;
374 stack16 += 32-1;
375 if (!addr) {
376 ERR("could not allocate default stack\n");
377 return 1;
379 } else {
380 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
382 ESP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
383 stack16 -= args;
384 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
385 /* push flags if iret */
386 if (iret) {
387 stack16--; args++;
388 *stack16 = LOWORD(EFL_reg(context));
390 /* push return address (return to interrupt wrapper) */
391 *(--stack16) = DPMI_wrap_seg;
392 *(--stack16) = 0;
393 /* adjust stack */
394 ESP_reg(context) -= 2*sizeof(WORD);
395 already = 1;
398 if (CurrRMCB) {
399 /* RMCB call, invoke protected-mode handler directly */
400 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
401 /* check if we returned to where we thought we would */
402 if ((CS_reg(context) != DPMI_wrap_seg) ||
403 (LOWORD(EIP_reg(context)) != 0)) {
404 /* we need to continue at different address in real-mode space,
405 so we need to set it all up for real mode again */
406 goto callrmproc_again;
408 } else {
409 #ifdef MZ_SUPPORTED
410 TRACE("entering real mode...\n");
411 DOSVM_Enter( context );
412 TRACE("returned from real-mode call\n");
413 #else
414 /* we should never get here, but... */
415 ERR("cannot perform real-mode call\n");
416 #endif
418 if (alloc) DOSMEM_FreeBlock( addr );
419 return 0;
423 /**********************************************************************
424 * CallRMInt
426 static void CallRMInt( CONTEXT86 *context )
428 CONTEXT86 realmode_ctx;
429 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
430 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
431 DI_reg(context) );
432 INT_GetRealModeContext( call, &realmode_ctx );
434 /* we need to check if a real-mode program has hooked the interrupt */
435 if (HIWORD(rm_int)!=0xF000) {
436 /* yup, which means we need to switch to real mode... */
437 CS_reg(&realmode_ctx) = HIWORD(rm_int);
438 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
439 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
440 SET_CFLAG(context);
441 } else {
442 RESET_CFLAG(context);
443 /* use the IP we have instead of BL_reg, in case some apps
444 decide to move interrupts around for whatever reason... */
445 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
446 SET_CFLAG(context);
447 if (EFL_reg(context)&1) {
448 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
449 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
450 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
451 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
452 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
453 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
456 INT_SetRealModeContext( call, &realmode_ctx );
460 static void CallRMProc( CONTEXT86 *context, int iret )
462 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
463 CONTEXT86 context16;
465 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
466 p->eax, p->ebx, p->ecx, p->edx);
467 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
468 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
470 if (!(p->cs) && !(p->ip)) { /* remove this check
471 if Int21/6501 case map function
472 has been implemented */
473 SET_CFLAG(context);
474 return;
476 INT_GetRealModeContext(p, &context16);
477 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context))))+3,
478 CX_reg(context), iret );
479 INT_SetRealModeContext(p, &context16);
483 static RMCB *DPMI_AllocRMCB( void )
485 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
486 UINT16 uParagraph;
488 if (NewRMCB)
490 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
491 LPBYTE p = RMCBmem;
493 *p++ = 0xcd; /* RMCB: */
494 *p++ = 0x31; /* int $0x31 */
495 /* it is the called procedure's task to change the return CS:EIP
496 the DPMI 0.9 spec states that if it doesn't, it will be called again */
497 *p++ = 0xeb;
498 *p++ = 0xfc; /* jmp RMCB */
499 NewRMCB->address = MAKELONG(0, uParagraph);
500 NewRMCB->next = FirstRMCB;
501 FirstRMCB = NewRMCB;
503 return NewRMCB;
507 static void AllocRMCB( CONTEXT86 *context )
509 RMCB *NewRMCB = DPMI_AllocRMCB();
511 TRACE("Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
513 if (NewRMCB)
515 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
516 NewRMCB->proc_ofs = SI_reg(context);
517 NewRMCB->proc_sel = DS_reg(context);
518 NewRMCB->regs_ofs = DI_reg(context);
519 NewRMCB->regs_sel = ES_reg(context);
520 SET_LOWORD( ECX_reg(context), HIWORD(NewRMCB->address) );
521 SET_LOWORD( EDX_reg(context), LOWORD(NewRMCB->address) );
523 else
525 SET_LOWORD( EAX_reg(context), 0x8015 ); /* callback unavailable */
526 SET_CFLAG(context);
531 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
533 RMCB *NewRMCB = DPMI_AllocRMCB();
535 if (NewRMCB) {
536 NewRMCB->proc_ofs = (DWORD)proc;
537 NewRMCB->proc_sel = 0;
538 NewRMCB->regs_ofs = 0;
539 NewRMCB->regs_sel = 0;
540 return (FARPROC16)(NewRMCB->address);
542 return NULL;
546 static int DPMI_FreeRMCB( DWORD address )
548 RMCB *CurrRMCB = FirstRMCB;
549 RMCB *PrevRMCB = NULL;
551 while (CurrRMCB && (CurrRMCB->address != address))
553 PrevRMCB = CurrRMCB;
554 CurrRMCB = CurrRMCB->next;
556 if (CurrRMCB)
558 if (PrevRMCB)
559 PrevRMCB->next = CurrRMCB->next;
560 else
561 FirstRMCB = CurrRMCB->next;
562 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
563 HeapFree(GetProcessHeap(), 0, CurrRMCB);
564 return 0;
566 return 1;
570 static void FreeRMCB( CONTEXT86 *context )
572 FIXME("callback address: %04x:%04x\n",
573 CX_reg(context), DX_reg(context));
575 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
576 SET_LOWORD( EAX_reg(context), 0x8024 ); /* invalid callback address */
577 SET_CFLAG(context);
582 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
584 DPMI_FreeRMCB( (DWORD)proc );
588 #ifdef MZ_SUPPORTED
589 /* (see loader/dos/module.c, function MZ_InitDPMI) */
591 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
593 char *base = DOSMEM_MemoryBase();
594 UINT16 cs, ss, ds, es;
595 CONTEXT86 pm_ctx;
596 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
597 PDB16 *psp = (PDB16 *)(base + psp_ofs);
598 HANDLE16 env_seg = psp->environment;
599 int is32;
601 RESET_CFLAG(context);
602 lpDosTask->dpmi_flag = AX_reg(context);
603 is32 = lpDosTask->dpmi_flag & 1;
604 /* our mode switch wrapper have placed the desired CS into DX */
605 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
606 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
607 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
608 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
609 32-bit code using this stack. */
610 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
611 /* do the same for the data segments, just in case */
612 if (DS_reg(context) == SS_reg(context)) ds = ss;
613 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
614 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
615 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
616 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
617 0x10000, SEGMENT_DATA, FALSE, FALSE );
619 pm_ctx = *context;
620 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
621 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
622 EAX_reg(&pm_ctx) = ss;
623 EDX_reg(&pm_ctx) = cs;
624 DS_reg(&pm_ctx) = ds;
625 ES_reg(&pm_ctx) = es;
626 FS_reg(&pm_ctx) = 0;
627 GS_reg(&pm_ctx) = 0;
629 TRACE("DOS program is now entering protected mode\n");
630 CallTo16RegisterShort(&pm_ctx, 0);
632 /* in the current state of affairs, we won't ever actually return here... */
633 /* we should have int21/ah=4c do it someday, though... */
635 SELECTOR_FreeBlock(psp->environment, 1);
636 psp->environment = env_seg;
637 SELECTOR_FreeBlock(es, 1);
638 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
639 SELECTOR_FreeBlock(ss, 1);
640 SELECTOR_FreeBlock(cs, 1);
643 /* DPMI Raw Mode Switch handler */
645 #if 0
646 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
648 LPDOSTASK lpDosTask = MZ_Current();
649 CONTEXT86 rm_ctx;
650 int ret;
652 if (!lpDosTask) {
653 /* we could probably start a DPMI-only dosmod task here, but I doubt
654 anything other than real DOS apps want to call raw mode switch */
655 ERR("attempting raw mode switch without DOS task!\n");
656 ExitProcess(1);
658 /* initialize real-mode context as per spec */
659 memset(&rm_ctx, 0, sizeof(rm_ctx));
660 DS_reg(&rm_ctx) = AX_sig(context);
661 ES_reg(&rm_ctx) = CX_sig(context);
662 SS_reg(&rm_ctx) = DX_sig(context);
663 ESP_reg(&rm_ctx) = EBX_sig(context);
664 CS_reg(&rm_ctx) = SI_sig(context);
665 EIP_reg(&rm_ctx) = EDI_sig(context);
666 EBP_reg(&rm_ctx) = EBP_sig(context);
667 FS_reg(&rm_ctx) = 0;
668 GS_reg(&rm_ctx) = 0;
669 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
671 /* enter real mode again */
672 TRACE("re-entering real mode at %04lx:%04lx\n",
673 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
674 ret = DOSVM_Enter( &rm_ctx );
675 /* when the real-mode stuff call its mode switch address,
676 DOSVM_Enter will return and we will continue here */
678 if (ret<0) {
679 /* if the sync was lost, there's no way to recover */
680 ExitProcess(1);
683 /* alter protected-mode context as per spec */
684 DS_sig(context) = AX_reg(&rm_ctx);
685 ES_sig(context) = CX_reg(&rm_ctx);
686 SS_sig(context) = DX_reg(&rm_ctx);
687 ESP_sig(context) = EBX_reg(&rm_ctx);
688 CS_sig(context) = SI_reg(&rm_ctx);
689 EIP_sig(context) = EDI_reg(&rm_ctx);
690 EBP_sig(context) = EBP_reg(&rm_ctx);
691 FS_sig(context) = 0;
692 GS_sig(context) = 0;
694 /* Return to new address and hope that we didn't mess up */
695 TRACE("re-entering protected mode at %04x:%08lx\n",
696 CS_sig(context), EIP_sig(context));
698 #endif
700 #else
701 #if 0
702 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
704 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
705 ExitProcess(1);
707 #endif
708 #endif
710 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
711 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
712 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
713 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
714 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
715 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
716 #define DOS_BADLIMIT(addr,base,limit) \
717 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
718 (DOS_WINE_ISDOS(addr,base) && \
719 ((addr) + (limit) > (base) + 0x110000)))
721 /**********************************************************************
722 * INT_Int31Handler
724 * Handler for int 31h (DPMI).
727 void WINAPI INT_Int31Handler( CONTEXT86 *context )
730 * Note: For Win32s processes, the whole linear address space is
731 * shifted by 0x10000 relative to the OS linear address space.
732 * See the comment in msdos/vxd.c.
734 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
736 DWORD dw;
737 BYTE *ptr;
739 LPDOSTASK lpDosTask = MZ_Current();
741 #ifdef MZ_SUPPORTED
742 if (ISV86(context) && lpDosTask) {
743 /* Called from real mode, check if it's our wrapper */
744 TRACE("called from real mode\n");
745 if (CS_reg(context)==lpDosTask->dpmi_seg) {
746 /* This is the protected mode switch */
747 StartPM(context,lpDosTask);
748 return;
749 } else
750 if (CS_reg(context)==lpDosTask->xms_seg) {
751 /* This is the XMS driver entry point */
752 XMS_Handler(context);
753 return;
754 } else
756 /* Check for RMCB */
757 RMCB *CurrRMCB = FirstRMCB;
759 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
760 CurrRMCB = CurrRMCB->next;
762 if (CurrRMCB) {
763 /* RMCB call, propagate to protected-mode handler */
764 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
765 return;
769 #endif
771 RESET_CFLAG(context);
772 switch(AX_reg(context))
774 case 0x0000: /* Allocate LDT descriptors */
775 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
776 if (!(EAX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
778 TRACE("failed\n");
779 EAX_reg(context) = 0x8011; /* descriptor unavailable */
780 SET_CFLAG(context);
782 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
783 break;
785 case 0x0001: /* Free LDT descriptor */
786 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
787 if (FreeSelector16( BX_reg(context) ))
789 EAX_reg(context) = 0x8022; /* invalid selector */
790 SET_CFLAG(context);
792 else
794 /* If a segment register contains the selector being freed, */
795 /* set it to zero. */
796 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
797 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
798 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
799 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
801 break;
803 case 0x0002: /* Real mode segment to descriptor */
804 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
806 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
807 switch(BX_reg(context))
809 case 0x0000: entryPoint = 183; break; /* __0000H */
810 case 0x0040: entryPoint = 193; break; /* __0040H */
811 case 0xa000: entryPoint = 174; break; /* __A000H */
812 case 0xb000: entryPoint = 181; break; /* __B000H */
813 case 0xb800: entryPoint = 182; break; /* __B800H */
814 case 0xc000: entryPoint = 195; break; /* __C000H */
815 case 0xd000: entryPoint = 179; break; /* __D000H */
816 case 0xe000: entryPoint = 190; break; /* __E000H */
817 case 0xf000: entryPoint = 194; break; /* __F000H */
818 default:
819 EAX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
820 break;
822 if (entryPoint)
823 EAX_reg(context) = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
824 entryPoint ));
826 break;
828 case 0x0003: /* Get next selector increment */
829 TRACE("get selector increment (__AHINCR)\n");
830 EAX_reg(context) = __AHINCR;
831 break;
833 case 0x0004: /* Lock selector (not supported) */
834 FIXME("lock selector not supported\n");
835 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
836 break;
838 case 0x0005: /* Unlock selector (not supported) */
839 FIXME("unlock selector not supported\n");
840 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
841 break;
843 case 0x0006: /* Get selector base address */
844 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
845 if (!(dw = GetSelectorBase( BX_reg(context) )))
847 EAX_reg(context) = 0x8022; /* invalid selector */
848 SET_CFLAG(context);
850 else
852 #ifdef MZ_SUPPORTED
853 if (lpDosTask) {
854 DWORD base = (DWORD)DOSMEM_MemoryBase();
855 dw = DOS_WINETOAPP(dw, base);
857 #endif
858 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
859 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
861 break;
863 case 0x0007: /* Set selector base address */
864 TRACE("set selector base address (0x%04x,0x%08lx)\n",
865 BX_reg(context),
866 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
867 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
868 #ifdef MZ_SUPPORTED
869 if (lpDosTask) {
870 DWORD base = (DWORD)DOSMEM_MemoryBase();
871 dw = DOS_APPTOWINE(dw, base);
873 #endif
874 SetSelectorBase(BX_reg(context), dw);
875 break;
877 case 0x0008: /* Set selector limit */
878 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
879 dw = MAKELONG( DX_reg(context), CX_reg(context) );
880 #ifdef MZ_SUPPORTED
881 if (lpDosTask) {
882 DWORD base = (DWORD)DOSMEM_MemoryBase();
883 DWORD sbase = GetSelectorBase( BX_reg(context) );
884 if (!sbase) {
885 /* the app has set the limit without setting the base,
886 * it must be relying on that the default should be DOS space;
887 * so set the base address now */
888 SetSelectorBase( BX_reg(context), sbase = base );
889 if (dw == 0xffffffff) {
890 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
891 * so we have to override the limit here */
892 dw = 0x110000;
895 if (DOS_BADLIMIT(sbase, base, dw)) {
896 AX_reg(context) = 0x8021; /* invalid value */
897 SET_CFLAG(context);
898 break;
901 #endif
902 SetSelectorLimit16( BX_reg(context), dw );
903 break;
905 case 0x0009: /* Set selector access rights */
906 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
907 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
908 break;
910 case 0x000a: /* Allocate selector alias */
911 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
912 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
914 AX_reg(context) = 0x8011; /* descriptor unavailable */
915 SET_CFLAG(context);
917 break;
919 case 0x000b: /* Get descriptor */
920 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
922 ldt_entry entry;
923 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
924 #ifdef MZ_SUPPORTED
925 if (lpDosTask) {
926 DWORD base = (DWORD)DOSMEM_MemoryBase();
927 entry.base = DOS_WINETOAPP(entry.base, base);
929 #endif
930 entry.base = W32S_WINE2APP(entry.base, offset);
932 /* FIXME: should use ES:EDI for 32-bit clients */
933 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
934 DI_reg(context) ), &entry );
936 break;
938 case 0x000c: /* Set descriptor */
939 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
941 ldt_entry entry;
942 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
943 DI_reg(context) ), &entry );
944 entry.base = W32S_APP2WINE(entry.base, offset);
945 #ifdef MZ_SUPPORTED
946 if (lpDosTask) {
947 DWORD base = (DWORD)DOSMEM_MemoryBase();
948 entry.base = DOS_APPTOWINE(entry.base, base);
949 if (DOS_BADLIMIT(entry.base, base, entry.limit)) {
950 AX_reg(context) = 0x8021; /* invalid value */
951 SET_CFLAG(context);
952 break;
955 #endif
957 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
959 break;
961 case 0x000d: /* Allocate specific LDT descriptor */
962 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
963 AX_reg(context) = 0x8011; /* descriptor unavailable */
964 SET_CFLAG(context);
965 break;
966 case 0x0100: /* Allocate DOS memory block */
967 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
968 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
969 if (dw) {
970 AX_reg(context) = HIWORD(dw);
971 DX_reg(context) = LOWORD(dw);
972 } else {
973 AX_reg(context) = 0x0008; /* insufficient memory */
974 BX_reg(context) = DOSMEM_Available()>>4;
975 SET_CFLAG(context);
977 break;
978 case 0x0101: /* Free DOS memory block */
979 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
980 dw = GlobalDOSFree16(DX_reg(context));
981 if (!dw) {
982 AX_reg(context) = 0x0009; /* memory block address invalid */
983 SET_CFLAG(context);
985 break;
986 case 0x0200: /* get real mode interrupt vector */
987 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
988 BL_reg(context));
989 SET_CFLAG(context);
990 break;
991 case 0x0201: /* set real mode interrupt vector */
992 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
993 SET_CFLAG(context);
994 break;
995 case 0x0204: /* Get protected mode interrupt vector */
996 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
997 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
998 CX_reg(context) = HIWORD(dw);
999 DX_reg(context) = LOWORD(dw);
1000 break;
1002 case 0x0205: /* Set protected mode interrupt vector */
1003 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
1004 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
1005 INT_SetPMHandler( BL_reg(context),
1006 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
1007 DX_reg(context) ));
1008 break;
1010 case 0x0300: /* Simulate real mode interrupt */
1011 CallRMInt( context );
1012 break;
1014 case 0x0301: /* Call real mode procedure with far return */
1015 CallRMProc( context, FALSE );
1016 break;
1018 case 0x0302: /* Call real mode procedure with interrupt return */
1019 CallRMProc( context, TRUE );
1020 break;
1022 case 0x0303: /* Allocate Real Mode Callback Address */
1023 AllocRMCB( context );
1024 break;
1026 case 0x0304: /* Free Real Mode Callback Address */
1027 FreeRMCB( context );
1028 break;
1030 case 0x0305: /* Get State Save/Restore Addresses */
1031 TRACE("get state save/restore addresses\n");
1032 /* we probably won't need this kind of state saving */
1033 AX_reg(context) = 0;
1034 /* real mode: just point to the lret */
1035 BX_reg(context) = DPMI_wrap_seg;
1036 ECX_reg(context) = 2;
1037 /* protected mode: don't have any handler yet... */
1038 FIXME("no protected-mode dummy state save/restore handler yet\n");
1039 SI_reg(context) = 0;
1040 EDI_reg(context) = 0;
1041 break;
1043 case 0x0306: /* Get Raw Mode Switch Addresses */
1044 TRACE("get raw mode switch addresses\n");
1045 if (lpDosTask) {
1046 /* real mode, point to standard DPMI return wrapper */
1047 BX_reg(context) = DPMI_wrap_seg;
1048 ECX_reg(context) = 0;
1049 /* protected mode, point to DPMI call wrapper */
1050 SI_reg(context) = lpDosTask->dpmi_sel;
1051 EDI_reg(context) = 8; /* offset of the INT 0x31 call */
1052 } else {
1053 ERR("win app attempting to get raw mode switch!\n");
1054 AX_reg(context) = 0x8001; /* unsupported function */
1055 SET_CFLAG(context);
1057 break;
1058 case 0x0400: /* Get DPMI version */
1059 TRACE("get DPMI version\n");
1061 SYSTEM_INFO si;
1063 GetSystemInfo(&si);
1064 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
1065 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
1066 CL_reg(context) = si.wProcessorLevel;
1067 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
1068 break;
1070 case 0x0500: /* Get free memory information */
1071 TRACE("get free memory information\n");
1073 MEMMANINFO mmi;
1075 mmi.dwSize = sizeof(mmi);
1076 MemManInfo16(&mmi);
1077 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1078 /* the layout is just the same as MEMMANINFO, but without
1079 * the dwSize entry.
1081 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1082 break;
1084 case 0x0501: /* Allocate memory block */
1085 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1086 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1088 AX_reg(context) = 0x8012; /* linear memory not available */
1089 SET_CFLAG(context);
1090 } else {
1091 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1092 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1094 break;
1096 case 0x0502: /* Free memory block */
1097 TRACE("free memory block (0x%08lx)\n",
1098 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1099 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1100 SI_reg(context)), offset) );
1101 break;
1103 case 0x0503: /* Resize memory block */
1104 TRACE("resize memory block (0x%08lx,%ld)\n",
1105 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1106 MAKELONG(CX_reg(context),BX_reg(context)));
1107 if (!(ptr = (BYTE *)DPMI_xrealloc(
1108 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1109 MAKELONG(CX_reg(context),BX_reg(context)))))
1111 AX_reg(context) = 0x8012; /* linear memory not available */
1112 SET_CFLAG(context);
1113 } else {
1114 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1115 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1117 break;
1119 case 0x0507: /* Modify page attributes */
1120 FIXME("modify page attributes unimplemented\n");
1121 break; /* Just ignore it */
1123 case 0x0600: /* Lock linear region */
1124 FIXME("lock linear region unimplemented\n");
1125 break; /* Just ignore it */
1127 case 0x0601: /* Unlock linear region */
1128 FIXME("unlock linear region unimplemented\n");
1129 break; /* Just ignore it */
1131 case 0x0602: /* Unlock real-mode region */
1132 FIXME("unlock realmode region unimplemented\n");
1133 break; /* Just ignore it */
1135 case 0x0603: /* Lock real-mode region */
1136 FIXME("lock realmode region unimplemented\n");
1137 break; /* Just ignore it */
1139 case 0x0604: /* Get page size */
1140 TRACE("get pagesize\n");
1141 BX_reg(context) = 0;
1142 CX_reg(context) = VIRTUAL_GetPageSize();
1143 break;
1145 case 0x0702: /* Mark page as demand-paging candidate */
1146 FIXME("mark page as demand-paging candidate\n");
1147 break; /* Just ignore it */
1149 case 0x0703: /* Discard page contents */
1150 FIXME("discard page contents\n");
1151 break; /* Just ignore it */
1153 case 0x0800: /* Physical address mapping */
1154 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1155 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1157 AX_reg(context) = 0x8021;
1158 SET_CFLAG(context);
1160 else
1162 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1163 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1164 RESET_CFLAG(context);
1166 break;
1168 default:
1169 INT_BARF( context, 0x31 );
1170 AX_reg(context) = 0x8001; /* unsupported function */
1171 SET_CFLAG(context);
1172 break;