Replaced PTR_SEG_TO_LIN macro by exported MapSL function.
[wine.git] / msdos / dpmi.c
blobf3255ce19ccf34918c696956f730c59808601d08
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 /**********************************************************************
67 * DPMI_xalloc
68 * special virtualalloc, allocates lineary monoton growing memory.
69 * (the usual VirtualAlloc does not satisfy that restriction)
71 static LPVOID
72 DPMI_xalloc(int len) {
73 LPVOID ret;
74 LPVOID oldlastv = lastvalloced;
76 if (lastvalloced) {
77 int xflag = 0;
78 ret = NULL;
79 while (!ret) {
80 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
81 if (!ret)
82 lastvalloced = (char *) lastvalloced + 0x10000;
83 /* we failed to allocate one in the first round.
84 * try non-linear
86 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
87 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
88 xflag++;
90 /* if we even fail to allocate something in the next
91 * round, return NULL
93 if ((xflag==1) && (lastvalloced >= oldlastv))
94 xflag++;
95 if ((xflag==2) && (lastvalloced < oldlastv)) {
96 FIXME("failed to allocate any memory of %d bytes!\n",len);
97 return NULL;
100 } else
101 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
102 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
103 return ret;
106 static void
107 DPMI_xfree(LPVOID ptr) {
108 VirtualFree(ptr,0,MEM_RELEASE);
111 /* FIXME: perhaps we could grow this mapped area... */
112 static LPVOID
113 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
114 MEMORY_BASIC_INFORMATION mbi;
115 LPVOID newptr;
117 newptr = DPMI_xalloc(newsize);
118 if (ptr) {
119 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
120 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
121 return NULL;
123 if (mbi.State == MEM_FREE) {
124 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
125 return NULL;
127 /* We do not shrink allocated memory. most reallocs
128 * only do grows anyway
130 if (newsize<=mbi.RegionSize)
131 return ptr;
132 memcpy(newptr,ptr,mbi.RegionSize);
133 DPMI_xfree(ptr);
135 return newptr;
137 /**********************************************************************
138 * INT_GetRealModeContext
140 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
142 context->Eax = call->eax;
143 context->Ebx = call->ebx;
144 context->Ecx = call->ecx;
145 context->Edx = call->edx;
146 context->Esi = call->esi;
147 context->Edi = call->edi;
148 context->Ebp = call->ebp;
149 context->EFlags = call->fl | V86_FLAG;
150 context->Eip = call->ip;
151 context->Esp = call->sp;
152 context->SegCs = call->cs;
153 context->SegDs = call->ds;
154 context->SegEs = call->es;
155 context->SegFs = call->fs;
156 context->SegGs = call->gs;
157 context->SegSs = call->ss;
161 /**********************************************************************
162 * INT_SetRealModeContext
164 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
166 call->eax = context->Eax;
167 call->ebx = context->Ebx;
168 call->ecx = context->Ecx;
169 call->edx = context->Edx;
170 call->esi = context->Esi;
171 call->edi = context->Edi;
172 call->ebp = context->Ebp;
173 call->fl = LOWORD(context->EFlags);
174 call->ip = LOWORD(context->Eip);
175 call->sp = LOWORD(context->Esp);
176 call->cs = context->SegCs;
177 call->ds = context->SegDs;
178 call->es = context->SegEs;
179 call->fs = context->SegFs;
180 call->gs = context->SegGs;
181 call->ss = context->SegSs;
184 #ifdef __i386__
186 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
187 #if 0 /* original code, which early gccs puke on */
189 int _clobber;
190 __asm__ __volatile__(
191 "pushl %%ebp\n"
192 "pushl %%ebx\n"
193 "pushl %%es\n"
194 "pushl %%ds\n"
195 "pushfl\n"
196 "mov %7,%%es\n"
197 "mov %5,%%ds\n"
198 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
199 "popl %%ds\n"
200 "mov %%es,%0\n"
201 "popl %%es\n"
202 "popl %%ebx\n"
203 "popl %%ebp\n"
204 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
205 : "0" (ss), "2" (esp),
206 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
207 "3" (&rmcb->proc_ofs) );
209 #else /* code generated by a gcc new enough */
211 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
212 "pushl %ebp\n\t"
213 "movl %esp,%ebp\n\t"
214 "pushl %edi\n\t"
215 "pushl %esi\n\t"
216 "movl 0x8(%ebp),%eax\n\t"
217 "movl 0x10(%ebp),%esi\n\t"
218 "movl 0xc(%ebp),%edx\n\t"
219 "movl 0x10(%eax),%ecx\n\t"
220 "movl 0xc(%eax),%edi\n\t"
221 "addl $0x4,%eax\n\t"
222 "pushl %ebp\n\t"
223 "pushl %ebx\n\t"
224 "pushl %es\n\t"
225 "pushl %ds\n\t"
226 "pushfl\n\t"
227 "mov %cx,%es\n\t"
228 "mov %dx,%ds\n\t"
229 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
230 "popl %ds\n\t"
231 "mov %es,%dx\n\t"
232 "popl %es\n\t"
233 "popl %ebx\n\t"
234 "popl %ebp\n\t"
235 "movl 0x14(%ebp),%eax\n\t"
236 "movw %dx,(%eax)\n\t"
237 "movl 0x18(%ebp),%edx\n\t"
238 "movl %edi,(%edx)\n\t"
239 "popl %esi\n\t"
240 "popl %edi\n\t"
241 "leave\n\t"
242 "ret")
243 #endif
245 #endif /* __i386__ */
247 /**********************************************************************
248 * DPMI_CallRMCBProc
250 * This routine does the hard work of calling a callback procedure.
252 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
254 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
255 /* Wine-internal RMCB, call directly */
256 ((RMCBPROC)rmcb->proc_ofs)(context);
257 } else {
258 #ifdef __i386__
259 UINT16 ss,es;
260 DWORD esp,edi;
262 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
263 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
264 esp = context->Esp;
266 FIXME("untested!\n");
268 /* The called proc ends with an IRET, and takes these parameters:
269 * DS:ESI = pointer to real-mode SS:SP
270 * ES:EDI = pointer to real-mode call structure
271 * It returns:
272 * ES:EDI = pointer to real-mode call structure (may be a copy)
273 * It is the proc's responsibility to change the return CS:IP in the
274 * real-mode call structure. */
275 if (flag & 1) {
276 /* 32-bit DPMI client */
277 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
278 } else {
279 /* 16-bit DPMI client */
280 CONTEXT86 ctx = *context;
281 ctx.SegCs = rmcb->proc_sel;
282 ctx.Eip = rmcb->proc_ofs;
283 ctx.SegDs = ss;
284 ctx.Esi = esp;
285 ctx.SegEs = rmcb->regs_sel;
286 ctx.Edi = rmcb->regs_ofs;
287 /* FIXME: I'm pretty sure this isn't right - should push flags first */
288 wine_call_to_16_regs_short(&ctx, 0);
289 es = ctx.SegEs;
290 edi = ctx.Edi;
292 FreeSelector16(ss);
293 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
294 #else
295 ERR("RMCBs only implemented for i386\n");
296 #endif
301 /**********************************************************************
302 * DPMI_CallRMProc
304 * This routine does the hard work of calling a real mode procedure.
306 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
308 LPWORD stack16;
309 LPVOID addr = NULL; /* avoid gcc warning */
310 LPDOSTASK lpDosTask = MZ_Current();
311 RMCB *CurrRMCB;
312 int alloc = 0, already = 0;
313 BYTE *code;
315 GlobalUnlock16( GetCurrentTask() );
317 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
318 context->Eax, context->Ebx, context->Ecx, context->Edx );
319 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
320 context->Esi, context->Edi, context->SegEs, context->SegDs,
321 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
323 callrmproc_again:
325 /* there might be some code that just jumps to RMCBs or the like,
326 in which case following the jumps here might get us to a shortcut */
327 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
328 switch (*code) {
329 case 0xe9: /* JMP NEAR */
330 context->Eip += 3 + *(WORD *)(code+1);
331 /* yeah, I know these gotos don't look good... */
332 goto callrmproc_again;
333 case 0xea: /* JMP FAR */
334 context->Eip = *(WORD *)(code+1);
335 context->SegCs = *(WORD *)(code+3);
336 /* ...but since the label is there anyway... */
337 goto callrmproc_again;
338 case 0xeb: /* JMP SHORT */
339 context->Eip += 2 + *(signed char *)(code+1);
340 /* ...because of other gotos below, so... */
341 goto callrmproc_again;
344 /* shortcut for chaining to internal interrupt handlers */
345 if ((context->SegCs == 0xF000) && iret) {
346 return INT_RealModeInterrupt( LOWORD(context->Eip)/4, context);
349 /* shortcut for RMCBs */
350 CurrRMCB = FirstRMCB;
352 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
353 CurrRMCB = CurrRMCB->next;
355 if (!(CurrRMCB || lpDosTask)) {
356 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
357 TRACE("creating VM86 task\n");
358 if (!(lpDosTask = MZ_AllocDPMITask() )) {
359 ERR("could not setup VM86 task\n");
360 return 1;
363 if (!already) {
364 if (!context->SegSs) {
365 alloc = 1; /* allocate default stack */
366 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
367 context->Esp = 64-2;
368 stack16 += 32-1;
369 if (!addr) {
370 ERR("could not allocate default stack\n");
371 return 1;
373 } else {
374 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
376 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
377 stack16 -= args;
378 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
379 /* push flags if iret */
380 if (iret) {
381 stack16--; args++;
382 *stack16 = LOWORD(context->EFlags);
384 /* push return address (return to interrupt wrapper) */
385 *(--stack16) = DOSMEM_wrap_seg;
386 *(--stack16) = 0;
387 /* adjust stack */
388 context->Esp -= 2*sizeof(WORD);
389 already = 1;
392 if (CurrRMCB) {
393 /* RMCB call, invoke protected-mode handler directly */
394 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
395 /* check if we returned to where we thought we would */
396 if ((context->SegCs != DOSMEM_wrap_seg) ||
397 (LOWORD(context->Eip) != 0)) {
398 /* we need to continue at different address in real-mode space,
399 so we need to set it all up for real mode again */
400 goto callrmproc_again;
402 } else {
403 TRACE("entering real mode...\n");
404 DOSVM_Enter( context );
405 TRACE("returned from real-mode call\n");
407 if (alloc) DOSMEM_FreeBlock( 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 = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
420 INT_GetRealModeContext( call, &realmode_ctx );
422 /* we need to check if a real-mode program has hooked the interrupt */
423 if (HIWORD(rm_int)!=0xF000) {
424 /* yup, which means we need to switch to real mode... */
425 realmode_ctx.SegCs = HIWORD(rm_int);
426 realmode_ctx.Eip = LOWORD(rm_int);
427 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
428 SET_CFLAG(context);
429 } else {
430 RESET_CFLAG(context);
431 /* use the IP we have instead of BL_reg, in case some apps
432 decide to move interrupts around for whatever reason... */
433 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
434 SET_CFLAG(context);
435 if (context->EFlags & 1) {
436 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
437 BL_reg(context), realmode_ctx.Eax, realmode_ctx.Ebx,
438 realmode_ctx.Ecx, realmode_ctx.Edx);
439 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
440 realmode_ctx.Esi, realmode_ctx.Edi,
441 realmode_ctx.SegDs, realmode_ctx.SegEs );
444 INT_SetRealModeContext( call, &realmode_ctx );
448 static void CallRMProc( CONTEXT86 *context, int iret )
450 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
451 CONTEXT86 context16;
453 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
454 p->eax, p->ebx, p->ecx, p->edx);
455 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
456 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
458 if (!(p->cs) && !(p->ip)) { /* remove this check
459 if Int21/6501 case map function
460 has been implemented */
461 SET_CFLAG(context);
462 return;
464 INT_GetRealModeContext(p, &context16);
465 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
466 CX_reg(context), iret );
467 INT_SetRealModeContext(p, &context16);
471 static RMCB *DPMI_AllocRMCB( void )
473 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
474 UINT16 uParagraph;
476 if (NewRMCB)
478 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
479 LPBYTE p = RMCBmem;
481 *p++ = 0xcd; /* RMCB: */
482 *p++ = 0x31; /* int $0x31 */
483 /* it is the called procedure's task to change the return CS:EIP
484 the DPMI 0.9 spec states that if it doesn't, it will be called again */
485 *p++ = 0xeb;
486 *p++ = 0xfc; /* jmp RMCB */
487 NewRMCB->address = MAKELONG(0, uParagraph);
488 NewRMCB->next = FirstRMCB;
489 FirstRMCB = NewRMCB;
491 return NewRMCB;
495 static void AllocRMCB( CONTEXT86 *context )
497 RMCB *NewRMCB = DPMI_AllocRMCB();
499 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
501 if (NewRMCB)
503 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
504 NewRMCB->proc_ofs = LOWORD(context->Esi);
505 NewRMCB->proc_sel = context->SegDs;
506 NewRMCB->regs_ofs = LOWORD(context->Edi);
507 NewRMCB->regs_sel = context->SegEs;
508 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
509 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
511 else
513 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
514 SET_CFLAG(context);
519 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
521 RMCB *NewRMCB = DPMI_AllocRMCB();
523 if (NewRMCB) {
524 NewRMCB->proc_ofs = (DWORD)proc;
525 NewRMCB->proc_sel = 0;
526 NewRMCB->regs_ofs = 0;
527 NewRMCB->regs_sel = 0;
528 return (FARPROC16)(NewRMCB->address);
530 return NULL;
534 static int DPMI_FreeRMCB( DWORD address )
536 RMCB *CurrRMCB = FirstRMCB;
537 RMCB *PrevRMCB = NULL;
539 while (CurrRMCB && (CurrRMCB->address != address))
541 PrevRMCB = CurrRMCB;
542 CurrRMCB = CurrRMCB->next;
544 if (CurrRMCB)
546 if (PrevRMCB)
547 PrevRMCB->next = CurrRMCB->next;
548 else
549 FirstRMCB = CurrRMCB->next;
550 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
551 HeapFree(GetProcessHeap(), 0, CurrRMCB);
552 return 0;
554 return 1;
558 static void FreeRMCB( CONTEXT86 *context )
560 FIXME("callback address: %04x:%04x\n",
561 CX_reg(context), DX_reg(context));
563 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
564 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
565 SET_CFLAG(context);
570 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
572 DPMI_FreeRMCB( (DWORD)proc );
576 /* (see dosmem.c, function DOSMEM_InitDPMI) */
578 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
580 UINT16 cs, ss, ds, es;
581 CONTEXT86 pm_ctx;
582 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
583 PDB16 *psp = (PDB16 *)psp_ofs;
584 HANDLE16 env_seg = psp->environment;
585 unsigned char selflags = WINE_LDT_FLAGS_DATA;
587 RESET_CFLAG(context);
588 lpDosTask->dpmi_flag = AX_reg(context);
589 /* our mode switch wrapper have placed the desired CS into DX */
590 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
591 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
592 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
593 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
594 32-bit code using this stack. */
595 if (lpDosTask->dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
596 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
597 /* do the same for the data segments, just in case */
598 if (context->SegDs == context->SegSs) ds = ss;
599 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
600 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
601 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
602 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
604 pm_ctx = *context;
605 pm_ctx.SegCs = DOSMEM_dpmi_sel;
606 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
607 pm_ctx.Eax = ss;
608 pm_ctx.Edx = cs;
609 pm_ctx.SegDs = ds;
610 pm_ctx.SegEs = es;
611 pm_ctx.SegFs = 0;
612 pm_ctx.SegGs = 0;
614 TRACE("DOS program is now entering protected mode\n");
615 wine_call_to_16_regs_short(&pm_ctx, 0);
617 /* in the current state of affairs, we won't ever actually return here... */
618 /* we should have int21/ah=4c do it someday, though... */
620 FreeSelector16(psp->environment);
621 psp->environment = env_seg;
622 FreeSelector16(es);
623 if (ds != ss) FreeSelector16(ds);
624 FreeSelector16(ss);
625 FreeSelector16(cs);
628 /* DPMI Raw Mode Switch handler */
630 #if 0
631 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
633 LPDOSTASK lpDosTask = MZ_Current();
634 CONTEXT86 rm_ctx;
635 int ret;
637 if (!lpDosTask) {
638 /* we could probably start a DPMI-only dosmod task here, but I doubt
639 anything other than real DOS apps want to call raw mode switch */
640 ERR("attempting raw mode switch without DOS task!\n");
641 ExitProcess(1);
643 /* initialize real-mode context as per spec */
644 memset(&rm_ctx, 0, sizeof(rm_ctx));
645 rm_ctx.SegDs = AX_sig(context);
646 rm_ctx.SegEs = CX_sig(context);
647 rm_ctx.SegSs = DX_sig(context);
648 rm_ctx.Esp = EBX_sig(context);
649 rm_ctx.SegCs = SI_sig(context);
650 rm_ctx.Eip = EDI_sig(context);
651 rm_ctx.Ebp = EBP_sig(context);
652 rm_ctx.SegFs = 0;
653 rm_ctx.SegGs = 0;
654 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
656 /* enter real mode again */
657 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
658 ret = DOSVM_Enter( &rm_ctx );
659 /* when the real-mode stuff call its mode switch address,
660 DOSVM_Enter will return and we will continue here */
662 if (ret<0) {
663 /* if the sync was lost, there's no way to recover */
664 ExitProcess(1);
667 /* alter protected-mode context as per spec */
668 DS_sig(context) = LOWORD(rm_ctx.Eax);
669 ES_sig(context) = LOWORD(rm_ctx.Ecx);
670 SS_sig(context) = LOWORD(rm_ctx.Edx);
671 ESP_sig(context) = rm_ctx.Ebx;
672 CS_sig(context) = LOWORD(rm_ctx.Esi);
673 EIP_sig(context) = rm_ctx.Edi;
674 EBP_sig(context) = rm_ctx.Ebp;
675 FS_sig(context) = 0;
676 GS_sig(context) = 0;
678 /* Return to new address and hope that we didn't mess up */
679 TRACE("re-entering protected mode at %04x:%08lx\n",
680 CS_sig(context), EIP_sig(context));
682 #endif
685 /**********************************************************************
686 * INT_Int31Handler
688 * Handler for int 31h (DPMI).
691 void WINAPI INT_Int31Handler( CONTEXT86 *context )
694 * Note: For Win32s processes, the whole linear address space is
695 * shifted by 0x10000 relative to the OS linear address space.
696 * See the comment in msdos/vxd.c.
698 DWORD dw;
699 BYTE *ptr;
701 LPDOSTASK lpDosTask = MZ_Current();
703 if (ISV86(context) && lpDosTask) {
704 /* Called from real mode, check if it's our wrapper */
705 TRACE("called from real mode\n");
706 if (context->SegCs==DOSMEM_dpmi_seg) {
707 /* This is the protected mode switch */
708 StartPM(context,lpDosTask);
709 return;
710 } else
711 if (context->SegCs==DOSMEM_xms_seg) {
712 /* This is the XMS driver entry point */
713 XMS_Handler(context);
714 return;
715 } else
717 /* Check for RMCB */
718 RMCB *CurrRMCB = FirstRMCB;
720 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
721 CurrRMCB = CurrRMCB->next;
723 if (CurrRMCB) {
724 /* RMCB call, propagate to protected-mode handler */
725 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
726 return;
731 RESET_CFLAG(context);
732 switch(AX_reg(context))
734 case 0x0000: /* Allocate LDT descriptors */
735 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
736 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
738 TRACE("failed\n");
739 context->Eax = 0x8011; /* descriptor unavailable */
740 SET_CFLAG(context);
742 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
743 break;
745 case 0x0001: /* Free LDT descriptor */
746 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
747 if (FreeSelector16( BX_reg(context) ))
749 context->Eax = 0x8022; /* invalid selector */
750 SET_CFLAG(context);
752 else
754 /* If a segment register contains the selector being freed, */
755 /* set it to zero. */
756 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
757 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
758 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
759 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
761 break;
763 case 0x0002: /* Real mode segment to descriptor */
764 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
766 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
767 switch(BX_reg(context))
769 case 0x0000: entryPoint = 183; break; /* __0000H */
770 case 0x0040: entryPoint = 193; break; /* __0040H */
771 case 0xa000: entryPoint = 174; break; /* __A000H */
772 case 0xb000: entryPoint = 181; break; /* __B000H */
773 case 0xb800: entryPoint = 182; break; /* __B800H */
774 case 0xc000: entryPoint = 195; break; /* __C000H */
775 case 0xd000: entryPoint = 179; break; /* __D000H */
776 case 0xe000: entryPoint = 190; break; /* __E000H */
777 case 0xf000: entryPoint = 194; break; /* __F000H */
778 default:
779 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
780 break;
782 if (entryPoint)
783 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
784 (LPCSTR)(ULONG_PTR)entryPoint ));
786 break;
788 case 0x0003: /* Get next selector increment */
789 TRACE("get selector increment (__AHINCR)\n");
790 context->Eax = __AHINCR;
791 break;
793 case 0x0004: /* Lock selector (not supported) */
794 FIXME("lock selector not supported\n");
795 context->Eax = 0; /* FIXME: is this a correct return value? */
796 break;
798 case 0x0005: /* Unlock selector (not supported) */
799 FIXME("unlock selector not supported\n");
800 context->Eax = 0; /* FIXME: is this a correct return value? */
801 break;
803 case 0x0006: /* Get selector base address */
804 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
805 if (!(dw = GetSelectorBase( BX_reg(context) )))
807 context->Eax = 0x8022; /* invalid selector */
808 SET_CFLAG(context);
810 else
812 CX_reg(context) = HIWORD(W32S_WINE2APP(dw));
813 DX_reg(context) = LOWORD(W32S_WINE2APP(dw));
815 break;
817 case 0x0007: /* Set selector base address */
818 TRACE("set selector base address (0x%04x,0x%08lx)\n",
819 BX_reg(context),
820 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
821 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
822 if (dw < 0x10000)
823 /* app wants to access lower 64K of DOS memory, map it in now */
824 DOSMEM_Init(TRUE);
825 SetSelectorBase(BX_reg(context), dw);
826 break;
828 case 0x0008: /* Set selector limit */
829 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
830 dw = MAKELONG( DX_reg(context), CX_reg(context) );
831 SetSelectorLimit16( BX_reg(context), dw );
832 break;
834 case 0x0009: /* Set selector access rights */
835 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
836 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
837 break;
839 case 0x000a: /* Allocate selector alias */
840 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
841 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
843 AX_reg(context) = 0x8011; /* descriptor unavailable */
844 SET_CFLAG(context);
846 break;
848 case 0x000b: /* Get descriptor */
849 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
851 LDT_ENTRY entry;
852 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
853 /* FIXME: should use ES:EDI for 32-bit clients */
854 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
856 break;
858 case 0x000c: /* Set descriptor */
859 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
861 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
862 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
863 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
865 break;
867 case 0x000d: /* Allocate specific LDT descriptor */
868 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
869 AX_reg(context) = 0x8011; /* descriptor unavailable */
870 SET_CFLAG(context);
871 break;
872 case 0x0100: /* Allocate DOS memory block */
873 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
874 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
875 if (dw) {
876 AX_reg(context) = HIWORD(dw);
877 DX_reg(context) = LOWORD(dw);
878 } else {
879 AX_reg(context) = 0x0008; /* insufficient memory */
880 BX_reg(context) = DOSMEM_Available()>>4;
881 SET_CFLAG(context);
883 break;
884 case 0x0101: /* Free DOS memory block */
885 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
886 dw = GlobalDOSFree16(DX_reg(context));
887 if (!dw) {
888 AX_reg(context) = 0x0009; /* memory block address invalid */
889 SET_CFLAG(context);
891 break;
892 case 0x0200: /* get real mode interrupt vector */
893 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
894 BL_reg(context));
895 SET_CFLAG(context);
896 break;
897 case 0x0201: /* set real mode interrupt vector */
898 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
899 SET_CFLAG(context);
900 break;
901 case 0x0204: /* Get protected mode interrupt vector */
902 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
903 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
904 CX_reg(context) = HIWORD(dw);
905 DX_reg(context) = LOWORD(dw);
906 break;
908 case 0x0205: /* Set protected mode interrupt vector */
909 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
910 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
911 INT_SetPMHandler( BL_reg(context),
912 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
913 break;
915 case 0x0300: /* Simulate real mode interrupt */
916 CallRMInt( context );
917 break;
919 case 0x0301: /* Call real mode procedure with far return */
920 CallRMProc( context, FALSE );
921 break;
923 case 0x0302: /* Call real mode procedure with interrupt return */
924 CallRMProc( context, TRUE );
925 break;
927 case 0x0303: /* Allocate Real Mode Callback Address */
928 AllocRMCB( context );
929 break;
931 case 0x0304: /* Free Real Mode Callback Address */
932 FreeRMCB( context );
933 break;
935 case 0x0305: /* Get State Save/Restore Addresses */
936 TRACE("get state save/restore addresses\n");
937 /* we probably won't need this kind of state saving */
938 AX_reg(context) = 0;
939 /* real mode: just point to the lret */
940 BX_reg(context) = DOSMEM_wrap_seg;
941 context->Ecx = 2;
942 /* protected mode: don't have any handler yet... */
943 FIXME("no protected-mode dummy state save/restore handler yet\n");
944 SI_reg(context) = 0;
945 context->Edi = 0;
946 break;
948 case 0x0306: /* Get Raw Mode Switch Addresses */
949 TRACE("get raw mode switch addresses\n");
950 /* real mode, point to standard DPMI return wrapper */
951 BX_reg(context) = DOSMEM_wrap_seg;
952 context->Ecx = 0;
953 /* protected mode, point to DPMI call wrapper */
954 SI_reg(context) = DOSMEM_dpmi_sel;
955 context->Edi = 8; /* offset of the INT 0x31 call */
956 break;
957 case 0x0400: /* Get DPMI version */
958 TRACE("get DPMI version\n");
960 SYSTEM_INFO si;
962 GetSystemInfo(&si);
963 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
964 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
965 CL_reg(context) = si.wProcessorLevel;
966 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
967 break;
969 case 0x0500: /* Get free memory information */
970 TRACE("get free memory information\n");
972 MEMMANINFO mmi;
974 mmi.dwSize = sizeof(mmi);
975 MemManInfo16(&mmi);
976 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
977 /* the layout is just the same as MEMMANINFO, but without
978 * the dwSize entry.
980 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
981 break;
983 case 0x0501: /* Allocate memory block */
984 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
985 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
987 AX_reg(context) = 0x8012; /* linear memory not available */
988 SET_CFLAG(context);
989 } else {
990 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
991 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
993 break;
995 case 0x0502: /* Free memory block */
996 TRACE("free memory block (0x%08lx)\n",
997 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
998 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
999 break;
1001 case 0x0503: /* Resize memory block */
1002 TRACE("resize memory block (0x%08lx,%ld)\n",
1003 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1004 MAKELONG(CX_reg(context),BX_reg(context)));
1005 if (!(ptr = (BYTE *)DPMI_xrealloc(
1006 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1007 MAKELONG(CX_reg(context),BX_reg(context)))))
1009 AX_reg(context) = 0x8012; /* linear memory not available */
1010 SET_CFLAG(context);
1011 } else {
1012 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1013 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1015 break;
1017 case 0x0507: /* Modify page attributes */
1018 FIXME("modify page attributes unimplemented\n");
1019 break; /* Just ignore it */
1021 case 0x0600: /* Lock linear region */
1022 FIXME("lock linear region unimplemented\n");
1023 break; /* Just ignore it */
1025 case 0x0601: /* Unlock linear region */
1026 FIXME("unlock linear region unimplemented\n");
1027 break; /* Just ignore it */
1029 case 0x0602: /* Unlock real-mode region */
1030 FIXME("unlock realmode region unimplemented\n");
1031 break; /* Just ignore it */
1033 case 0x0603: /* Lock real-mode region */
1034 FIXME("lock realmode region unimplemented\n");
1035 break; /* Just ignore it */
1037 case 0x0604: /* Get page size */
1038 TRACE("get pagesize\n");
1039 BX_reg(context) = 0;
1040 CX_reg(context) = getpagesize();
1041 break;
1043 case 0x0702: /* Mark page as demand-paging candidate */
1044 FIXME("mark page as demand-paging candidate\n");
1045 break; /* Just ignore it */
1047 case 0x0703: /* Discard page contents */
1048 FIXME("discard page contents\n");
1049 break; /* Just ignore it */
1051 case 0x0800: /* Physical address mapping */
1052 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1053 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1055 AX_reg(context) = 0x8021;
1056 SET_CFLAG(context);
1058 else
1060 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1061 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1062 RESET_CFLAG(context);
1064 break;
1066 default:
1067 INT_BARF( context, 0x31 );
1068 AX_reg(context) = 0x8001; /* unsupported function */
1069 SET_CFLAG(context);
1070 break;