Use the approriate command for combining .o files.
[wine/multimedia.git] / msdos / dpmi.c
blob2a6de5a59ec8e77c809aa1c5bf2cf9bf026acd45
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 "ldt.h"
14 #include "builtin16.h"
15 #include "global.h"
16 #include "miscemu.h"
17 #include "msdos.h"
18 #include "dosexe.h"
19 #include "task.h"
20 #include "toolhelp.h"
21 #include "selectors.h"
22 #include "process.h"
23 #include "callback.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(int31);
28 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
30 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
32 static void* lastvalloced = NULL;
34 /* Structure for real-mode callbacks */
35 typedef struct
37 DWORD edi;
38 DWORD esi;
39 DWORD ebp;
40 DWORD reserved;
41 DWORD ebx;
42 DWORD edx;
43 DWORD ecx;
44 DWORD eax;
45 WORD fl;
46 WORD es;
47 WORD ds;
48 WORD fs;
49 WORD gs;
50 WORD ip;
51 WORD cs;
52 WORD sp;
53 WORD ss;
54 } REALMODECALL;
58 typedef struct tagRMCB {
59 DWORD address;
60 DWORD proc_ofs,proc_sel;
61 DWORD regs_ofs,regs_sel;
62 struct tagRMCB *next;
63 } RMCB;
65 static RMCB *FirstRMCB = NULL;
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,DWORD 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 context->Eax = call->eax;
145 context->Ebx = call->ebx;
146 context->Ecx = call->ecx;
147 context->Edx = call->edx;
148 context->Esi = call->esi;
149 context->Edi = call->edi;
150 context->Ebp = call->ebp;
151 context->EFlags = call->fl | V86_FLAG;
152 context->Eip = call->ip;
153 context->Esp = call->sp;
154 context->SegCs = call->cs;
155 context->SegDs = call->ds;
156 context->SegEs = call->es;
157 context->SegFs = call->fs;
158 context->SegGs = call->gs;
159 context->SegSs = call->ss;
163 /**********************************************************************
164 * INT_SetRealModeContext
166 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
168 call->eax = context->Eax;
169 call->ebx = context->Ebx;
170 call->ecx = context->Ecx;
171 call->edx = context->Edx;
172 call->esi = context->Esi;
173 call->edi = context->Edi;
174 call->ebp = context->Ebp;
175 call->fl = LOWORD(context->EFlags);
176 call->ip = LOWORD(context->Eip);
177 call->sp = LOWORD(context->Esp);
178 call->cs = context->SegCs;
179 call->ds = context->SegDs;
180 call->es = context->SegEs;
181 call->fs = context->SegFs;
182 call->gs = context->SegGs;
183 call->ss = context->SegSs;
186 #ifdef __i386__
188 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
189 #if 0 /* original code, which early gccs puke on */
191 int _clobber;
192 __asm__ __volatile__(
193 "pushl %%ebp\n"
194 "pushl %%ebx\n"
195 "pushl %%es\n"
196 "pushl %%ds\n"
197 "pushfl\n"
198 "mov %7,%%es\n"
199 "mov %5,%%ds\n"
200 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
201 "popl %%ds\n"
202 "mov %%es,%0\n"
203 "popl %%es\n"
204 "popl %%ebx\n"
205 "popl %%ebp\n"
206 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
207 : "0" (ss), "2" (esp),
208 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
209 "3" (&rmcb->proc_ofs) );
211 #else /* code generated by a gcc new enough */
213 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
214 "pushl %ebp\n\t"
215 "movl %esp,%ebp\n\t"
216 "pushl %edi\n\t"
217 "pushl %esi\n\t"
218 "movl 0x8(%ebp),%eax\n\t"
219 "movl 0x10(%ebp),%esi\n\t"
220 "movl 0xc(%ebp),%edx\n\t"
221 "movl 0x10(%eax),%ecx\n\t"
222 "movl 0xc(%eax),%edi\n\t"
223 "addl $0x4,%eax\n\t"
224 "pushl %ebp\n\t"
225 "pushl %ebx\n\t"
226 "pushl %es\n\t"
227 "pushl %ds\n\t"
228 "pushfl\n\t"
229 "mov %cx,%es\n\t"
230 "mov %dx,%ds\n\t"
231 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
232 "popl %ds\n\t"
233 "mov %es,%dx\n\t"
234 "popl %es\n\t"
235 "popl %ebx\n\t"
236 "popl %ebp\n\t"
237 "movl 0x14(%ebp),%eax\n\t"
238 "movw %dx,(%eax)\n\t"
239 "movl 0x18(%ebp),%edx\n\t"
240 "movl %edi,(%edx)\n\t"
241 "popl %esi\n\t"
242 "popl %edi\n\t"
243 "leave\n\t"
244 "ret")
245 #endif
247 #endif /* __i386__ */
249 /**********************************************************************
250 * DPMI_CallRMCBProc
252 * This routine does the hard work of calling a callback procedure.
254 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
256 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
257 /* Wine-internal RMCB, call directly */
258 ((RMCBPROC)rmcb->proc_ofs)(context);
259 } else {
260 #ifdef __i386__
261 UINT16 ss,es;
262 DWORD esp,edi;
264 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
265 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
266 esp = context->Esp;
268 FIXME("untested!\n");
270 /* The called proc ends with an IRET, and takes these parameters:
271 * DS:ESI = pointer to real-mode SS:SP
272 * ES:EDI = pointer to real-mode call structure
273 * It returns:
274 * ES:EDI = pointer to real-mode call structure (may be a copy)
275 * It is the proc's responsibility to change the return CS:IP in the
276 * real-mode call structure. */
277 if (flag & 1) {
278 /* 32-bit DPMI client */
279 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
280 } else {
281 /* 16-bit DPMI client */
282 CONTEXT86 ctx = *context;
283 ctx.SegCs = rmcb->proc_sel;
284 ctx.Eip = rmcb->proc_ofs;
285 ctx.SegDs = ss;
286 ctx.Esi = esp;
287 ctx.SegEs = rmcb->regs_sel;
288 ctx.Edi = rmcb->regs_ofs;
289 /* FIXME: I'm pretty sure this isn't right - should push flags first */
290 wine_call_to_16_regs_short(&ctx, 0);
291 es = ctx.SegEs;
292 edi = ctx.Edi;
294 FreeSelector16(ss);
295 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
296 #else
297 ERR("RMCBs only implemented for i386\n");
298 #endif
303 /**********************************************************************
304 * DPMI_CallRMProc
306 * This routine does the hard work of calling a real mode procedure.
308 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
310 LPWORD stack16;
311 LPVOID addr = NULL; /* avoid gcc warning */
312 LPDOSTASK lpDosTask = MZ_Current();
313 RMCB *CurrRMCB;
314 int alloc = 0, already = 0;
315 BYTE *code;
317 GlobalUnlock16( GetCurrentTask() );
319 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
320 context->Eax, context->Ebx, context->Ecx, context->Edx );
321 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
322 context->Esi, context->Edi, context->SegEs, context->SegDs,
323 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
325 callrmproc_again:
327 /* there might be some code that just jumps to RMCBs or the like,
328 in which case following the jumps here might get us to a shortcut */
329 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
330 switch (*code) {
331 case 0xe9: /* JMP NEAR */
332 context->Eip += 3 + *(WORD *)(code+1);
333 /* yeah, I know these gotos don't look good... */
334 goto callrmproc_again;
335 case 0xea: /* JMP FAR */
336 context->Eip = *(WORD *)(code+1);
337 context->SegCs = *(WORD *)(code+3);
338 /* ...but since the label is there anyway... */
339 goto callrmproc_again;
340 case 0xeb: /* JMP SHORT */
341 context->Eip += 2 + *(signed char *)(code+1);
342 /* ...because of other gotos below, so... */
343 goto callrmproc_again;
346 /* shortcut for chaining to internal interrupt handlers */
347 if ((context->SegCs == 0xF000) && iret) {
348 return INT_RealModeInterrupt( LOWORD(context->Eip)/4, context);
351 /* shortcut for RMCBs */
352 CurrRMCB = FirstRMCB;
354 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
355 CurrRMCB = CurrRMCB->next;
357 if (!(CurrRMCB || lpDosTask)) {
358 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
359 TRACE("creating VM86 task\n");
360 if (!(lpDosTask = MZ_AllocDPMITask() )) {
361 ERR("could not setup VM86 task\n");
362 return 1;
365 if (!already) {
366 if (!context->SegSs) {
367 alloc = 1; /* allocate default stack */
368 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
369 context->Esp = 64-2;
370 stack16 += 32-1;
371 if (!addr) {
372 ERR("could not allocate default stack\n");
373 return 1;
375 } else {
376 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
378 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
379 stack16 -= args;
380 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
381 /* push flags if iret */
382 if (iret) {
383 stack16--; args++;
384 *stack16 = LOWORD(context->EFlags);
386 /* push return address (return to interrupt wrapper) */
387 *(--stack16) = DOSMEM_wrap_seg;
388 *(--stack16) = 0;
389 /* adjust stack */
390 context->Esp -= 2*sizeof(WORD);
391 already = 1;
394 if (CurrRMCB) {
395 /* RMCB call, invoke protected-mode handler directly */
396 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
397 /* check if we returned to where we thought we would */
398 if ((context->SegCs != DOSMEM_wrap_seg) ||
399 (LOWORD(context->Eip) != 0)) {
400 /* we need to continue at different address in real-mode space,
401 so we need to set it all up for real mode again */
402 goto callrmproc_again;
404 } else {
405 TRACE("entering real mode...\n");
406 DOSVM_Enter( context );
407 TRACE("returned from real-mode call\n");
409 if (alloc) DOSMEM_FreeBlock( addr );
410 return 0;
414 /**********************************************************************
415 * CallRMInt
417 static void CallRMInt( CONTEXT86 *context )
419 CONTEXT86 realmode_ctx;
420 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
421 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( context->SegEs,
422 DI_reg(context) );
423 INT_GetRealModeContext( call, &realmode_ctx );
425 /* we need to check if a real-mode program has hooked the interrupt */
426 if (HIWORD(rm_int)!=0xF000) {
427 /* yup, which means we need to switch to real mode... */
428 realmode_ctx.SegCs = HIWORD(rm_int);
429 realmode_ctx.Eip = LOWORD(rm_int);
430 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
431 SET_CFLAG(context);
432 } else {
433 RESET_CFLAG(context);
434 /* use the IP we have instead of BL_reg, in case some apps
435 decide to move interrupts around for whatever reason... */
436 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
437 SET_CFLAG(context);
438 if (context->EFlags & 1) {
439 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
440 BL_reg(context), realmode_ctx.Eax, realmode_ctx.Ebx,
441 realmode_ctx.Ecx, realmode_ctx.Edx);
442 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
443 realmode_ctx.Esi, realmode_ctx.Edi,
444 realmode_ctx.SegDs, realmode_ctx.SegEs );
447 INT_SetRealModeContext( call, &realmode_ctx );
451 static void CallRMProc( CONTEXT86 *context, int iret )
453 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( context->SegEs, DI_reg(context) );
454 CONTEXT86 context16;
456 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
457 p->eax, p->ebx, p->ecx, p->edx);
458 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
459 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
461 if (!(p->cs) && !(p->ip)) { /* remove this check
462 if Int21/6501 case map function
463 has been implemented */
464 SET_CFLAG(context);
465 return;
467 INT_GetRealModeContext(p, &context16);
468 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(context->SegSs, LOWORD(context->Esp)))+3,
469 CX_reg(context), iret );
470 INT_SetRealModeContext(p, &context16);
474 static RMCB *DPMI_AllocRMCB( void )
476 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
477 UINT16 uParagraph;
479 if (NewRMCB)
481 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
482 LPBYTE p = RMCBmem;
484 *p++ = 0xcd; /* RMCB: */
485 *p++ = 0x31; /* int $0x31 */
486 /* it is the called procedure's task to change the return CS:EIP
487 the DPMI 0.9 spec states that if it doesn't, it will be called again */
488 *p++ = 0xeb;
489 *p++ = 0xfc; /* jmp RMCB */
490 NewRMCB->address = MAKELONG(0, uParagraph);
491 NewRMCB->next = FirstRMCB;
492 FirstRMCB = NewRMCB;
494 return NewRMCB;
498 static void AllocRMCB( CONTEXT86 *context )
500 RMCB *NewRMCB = DPMI_AllocRMCB();
502 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
504 if (NewRMCB)
506 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
507 NewRMCB->proc_ofs = LOWORD(context->Esi);
508 NewRMCB->proc_sel = context->SegDs;
509 NewRMCB->regs_ofs = LOWORD(context->Edi);
510 NewRMCB->regs_sel = context->SegEs;
511 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
512 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
514 else
516 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
517 SET_CFLAG(context);
522 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
524 RMCB *NewRMCB = DPMI_AllocRMCB();
526 if (NewRMCB) {
527 NewRMCB->proc_ofs = (DWORD)proc;
528 NewRMCB->proc_sel = 0;
529 NewRMCB->regs_ofs = 0;
530 NewRMCB->regs_sel = 0;
531 return (FARPROC16)(NewRMCB->address);
533 return NULL;
537 static int DPMI_FreeRMCB( DWORD address )
539 RMCB *CurrRMCB = FirstRMCB;
540 RMCB *PrevRMCB = NULL;
542 while (CurrRMCB && (CurrRMCB->address != address))
544 PrevRMCB = CurrRMCB;
545 CurrRMCB = CurrRMCB->next;
547 if (CurrRMCB)
549 if (PrevRMCB)
550 PrevRMCB->next = CurrRMCB->next;
551 else
552 FirstRMCB = CurrRMCB->next;
553 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
554 HeapFree(GetProcessHeap(), 0, CurrRMCB);
555 return 0;
557 return 1;
561 static void FreeRMCB( CONTEXT86 *context )
563 FIXME("callback address: %04x:%04x\n",
564 CX_reg(context), DX_reg(context));
566 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
567 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
568 SET_CFLAG(context);
573 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
575 DPMI_FreeRMCB( (DWORD)proc );
579 /* (see dosmem.c, function DOSMEM_InitDPMI) */
581 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
583 UINT16 cs, ss, ds, es;
584 CONTEXT86 pm_ctx;
585 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
586 PDB16 *psp = (PDB16 *)psp_ofs;
587 HANDLE16 env_seg = psp->environment;
588 unsigned char selflags = WINE_LDT_FLAGS_DATA;
590 RESET_CFLAG(context);
591 lpDosTask->dpmi_flag = AX_reg(context);
592 /* our mode switch wrapper have placed the desired CS into DX */
593 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
594 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
595 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
596 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
597 32-bit code using this stack. */
598 if (lpDosTask->dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
599 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
600 /* do the same for the data segments, just in case */
601 if (context->SegDs == context->SegSs) ds = ss;
602 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
603 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
604 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
605 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
607 pm_ctx = *context;
608 pm_ctx.SegCs = DOSMEM_dpmi_sel;
609 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
610 pm_ctx.Eax = ss;
611 pm_ctx.Edx = cs;
612 pm_ctx.SegDs = ds;
613 pm_ctx.SegEs = es;
614 pm_ctx.SegFs = 0;
615 pm_ctx.SegGs = 0;
617 TRACE("DOS program is now entering protected mode\n");
618 wine_call_to_16_regs_short(&pm_ctx, 0);
620 /* in the current state of affairs, we won't ever actually return here... */
621 /* we should have int21/ah=4c do it someday, though... */
623 FreeSelector16(psp->environment);
624 psp->environment = env_seg;
625 FreeSelector16(es);
626 if (ds != ss) FreeSelector16(ds);
627 FreeSelector16(ss);
628 FreeSelector16(cs);
631 /* DPMI Raw Mode Switch handler */
633 #if 0
634 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
636 LPDOSTASK lpDosTask = MZ_Current();
637 CONTEXT86 rm_ctx;
638 int ret;
640 if (!lpDosTask) {
641 /* we could probably start a DPMI-only dosmod task here, but I doubt
642 anything other than real DOS apps want to call raw mode switch */
643 ERR("attempting raw mode switch without DOS task!\n");
644 ExitProcess(1);
646 /* initialize real-mode context as per spec */
647 memset(&rm_ctx, 0, sizeof(rm_ctx));
648 rm_ctx.SegDs = AX_sig(context);
649 rm_ctx.SegEs = CX_sig(context);
650 rm_ctx.SegSs = DX_sig(context);
651 rm_ctx.Esp = EBX_sig(context);
652 rm_ctx.SegCs = SI_sig(context);
653 rm_ctx.Eip = EDI_sig(context);
654 rm_ctx.Ebp = EBP_sig(context);
655 rm_ctx.SegFs = 0;
656 rm_ctx.SegGs = 0;
657 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
659 /* enter real mode again */
660 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
661 ret = DOSVM_Enter( &rm_ctx );
662 /* when the real-mode stuff call its mode switch address,
663 DOSVM_Enter will return and we will continue here */
665 if (ret<0) {
666 /* if the sync was lost, there's no way to recover */
667 ExitProcess(1);
670 /* alter protected-mode context as per spec */
671 DS_sig(context) = LOWORD(rm_ctx.Eax);
672 ES_sig(context) = LOWORD(rm_ctx.Ecx);
673 SS_sig(context) = LOWORD(rm_ctx.Edx);
674 ESP_sig(context) = rm_ctx.Ebx;
675 CS_sig(context) = LOWORD(rm_ctx.Esi);
676 EIP_sig(context) = rm_ctx.Edi;
677 EBP_sig(context) = rm_ctx.Ebp;
678 FS_sig(context) = 0;
679 GS_sig(context) = 0;
681 /* Return to new address and hope that we didn't mess up */
682 TRACE("re-entering protected mode at %04x:%08lx\n",
683 CS_sig(context), EIP_sig(context));
685 #endif
688 /**********************************************************************
689 * INT_Int31Handler
691 * Handler for int 31h (DPMI).
694 void WINAPI INT_Int31Handler( CONTEXT86 *context )
697 * Note: For Win32s processes, the whole linear address space is
698 * shifted by 0x10000 relative to the OS linear address space.
699 * See the comment in msdos/vxd.c.
701 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
703 DWORD dw;
704 BYTE *ptr;
706 LPDOSTASK lpDosTask = MZ_Current();
708 if (ISV86(context) && lpDosTask) {
709 /* Called from real mode, check if it's our wrapper */
710 TRACE("called from real mode\n");
711 if (context->SegCs==DOSMEM_dpmi_seg) {
712 /* This is the protected mode switch */
713 StartPM(context,lpDosTask);
714 return;
715 } else
716 if (context->SegCs==DOSMEM_xms_seg) {
717 /* This is the XMS driver entry point */
718 XMS_Handler(context);
719 return;
720 } else
722 /* Check for RMCB */
723 RMCB *CurrRMCB = FirstRMCB;
725 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
726 CurrRMCB = CurrRMCB->next;
728 if (CurrRMCB) {
729 /* RMCB call, propagate to protected-mode handler */
730 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
731 return;
736 RESET_CFLAG(context);
737 switch(AX_reg(context))
739 case 0x0000: /* Allocate LDT descriptors */
740 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
741 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
743 TRACE("failed\n");
744 context->Eax = 0x8011; /* descriptor unavailable */
745 SET_CFLAG(context);
747 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
748 break;
750 case 0x0001: /* Free LDT descriptor */
751 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
752 if (FreeSelector16( BX_reg(context) ))
754 context->Eax = 0x8022; /* invalid selector */
755 SET_CFLAG(context);
757 else
759 /* If a segment register contains the selector being freed, */
760 /* set it to zero. */
761 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
762 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
763 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
764 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
766 break;
768 case 0x0002: /* Real mode segment to descriptor */
769 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
771 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
772 switch(BX_reg(context))
774 case 0x0000: entryPoint = 183; break; /* __0000H */
775 case 0x0040: entryPoint = 193; break; /* __0040H */
776 case 0xa000: entryPoint = 174; break; /* __A000H */
777 case 0xb000: entryPoint = 181; break; /* __B000H */
778 case 0xb800: entryPoint = 182; break; /* __B800H */
779 case 0xc000: entryPoint = 195; break; /* __C000H */
780 case 0xd000: entryPoint = 179; break; /* __D000H */
781 case 0xe000: entryPoint = 190; break; /* __E000H */
782 case 0xf000: entryPoint = 194; break; /* __F000H */
783 default:
784 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
785 break;
787 if (entryPoint)
788 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
789 (LPCSTR)(ULONG_PTR)entryPoint ));
791 break;
793 case 0x0003: /* Get next selector increment */
794 TRACE("get selector increment (__AHINCR)\n");
795 context->Eax = __AHINCR;
796 break;
798 case 0x0004: /* Lock selector (not supported) */
799 FIXME("lock selector not supported\n");
800 context->Eax = 0; /* FIXME: is this a correct return value? */
801 break;
803 case 0x0005: /* Unlock selector (not supported) */
804 FIXME("unlock selector not supported\n");
805 context->Eax = 0; /* FIXME: is this a correct return value? */
806 break;
808 case 0x0006: /* Get selector base address */
809 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
810 if (!(dw = GetSelectorBase( BX_reg(context) )))
812 context->Eax = 0x8022; /* invalid selector */
813 SET_CFLAG(context);
815 else
817 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
818 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
820 break;
822 case 0x0007: /* Set selector base address */
823 TRACE("set selector base address (0x%04x,0x%08lx)\n",
824 BX_reg(context),
825 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
826 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
827 if (dw < 0x10000)
828 /* app wants to access lower 64K of DOS memory, map it in now */
829 DOSMEM_Init(TRUE);
830 SetSelectorBase(BX_reg(context), dw);
831 break;
833 case 0x0008: /* Set selector limit */
834 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
835 dw = MAKELONG( DX_reg(context), CX_reg(context) );
836 SetSelectorLimit16( BX_reg(context), dw );
837 break;
839 case 0x0009: /* Set selector access rights */
840 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
841 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
842 break;
844 case 0x000a: /* Allocate selector alias */
845 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
846 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
848 AX_reg(context) = 0x8011; /* descriptor unavailable */
849 SET_CFLAG(context);
851 break;
853 case 0x000b: /* Get descriptor */
854 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
856 LDT_ENTRY entry;
857 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry), offset) );
858 /* FIXME: should use ES:EDI for 32-bit clients */
859 *(LDT_ENTRY *)PTR_SEG_OFF_TO_LIN( context->SegEs, LOWORD(context->Edi) ) = entry;
861 break;
863 case 0x000c: /* Set descriptor */
864 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
866 LDT_ENTRY entry = *(LDT_ENTRY *)PTR_SEG_OFF_TO_LIN( context->SegEs,
867 LOWORD(context->Edi) );
868 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry), offset) );
869 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
871 break;
873 case 0x000d: /* Allocate specific LDT descriptor */
874 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
875 AX_reg(context) = 0x8011; /* descriptor unavailable */
876 SET_CFLAG(context);
877 break;
878 case 0x0100: /* Allocate DOS memory block */
879 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
880 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
881 if (dw) {
882 AX_reg(context) = HIWORD(dw);
883 DX_reg(context) = LOWORD(dw);
884 } else {
885 AX_reg(context) = 0x0008; /* insufficient memory */
886 BX_reg(context) = DOSMEM_Available()>>4;
887 SET_CFLAG(context);
889 break;
890 case 0x0101: /* Free DOS memory block */
891 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
892 dw = GlobalDOSFree16(DX_reg(context));
893 if (!dw) {
894 AX_reg(context) = 0x0009; /* memory block address invalid */
895 SET_CFLAG(context);
897 break;
898 case 0x0200: /* get real mode interrupt vector */
899 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
900 BL_reg(context));
901 SET_CFLAG(context);
902 break;
903 case 0x0201: /* set real mode interrupt vector */
904 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
905 SET_CFLAG(context);
906 break;
907 case 0x0204: /* Get protected mode interrupt vector */
908 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
909 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
910 CX_reg(context) = HIWORD(dw);
911 DX_reg(context) = LOWORD(dw);
912 break;
914 case 0x0205: /* Set protected mode interrupt vector */
915 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
916 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
917 INT_SetPMHandler( BL_reg(context),
918 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
919 DX_reg(context) ));
920 break;
922 case 0x0300: /* Simulate real mode interrupt */
923 CallRMInt( context );
924 break;
926 case 0x0301: /* Call real mode procedure with far return */
927 CallRMProc( context, FALSE );
928 break;
930 case 0x0302: /* Call real mode procedure with interrupt return */
931 CallRMProc( context, TRUE );
932 break;
934 case 0x0303: /* Allocate Real Mode Callback Address */
935 AllocRMCB( context );
936 break;
938 case 0x0304: /* Free Real Mode Callback Address */
939 FreeRMCB( context );
940 break;
942 case 0x0305: /* Get State Save/Restore Addresses */
943 TRACE("get state save/restore addresses\n");
944 /* we probably won't need this kind of state saving */
945 AX_reg(context) = 0;
946 /* real mode: just point to the lret */
947 BX_reg(context) = DOSMEM_wrap_seg;
948 context->Ecx = 2;
949 /* protected mode: don't have any handler yet... */
950 FIXME("no protected-mode dummy state save/restore handler yet\n");
951 SI_reg(context) = 0;
952 context->Edi = 0;
953 break;
955 case 0x0306: /* Get Raw Mode Switch Addresses */
956 TRACE("get raw mode switch addresses\n");
957 /* real mode, point to standard DPMI return wrapper */
958 BX_reg(context) = DOSMEM_wrap_seg;
959 context->Ecx = 0;
960 /* protected mode, point to DPMI call wrapper */
961 SI_reg(context) = DOSMEM_dpmi_sel;
962 context->Edi = 8; /* offset of the INT 0x31 call */
963 break;
964 case 0x0400: /* Get DPMI version */
965 TRACE("get DPMI version\n");
967 SYSTEM_INFO si;
969 GetSystemInfo(&si);
970 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
971 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
972 CL_reg(context) = si.wProcessorLevel;
973 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
974 break;
976 case 0x0500: /* Get free memory information */
977 TRACE("get free memory information\n");
979 MEMMANINFO mmi;
981 mmi.dwSize = sizeof(mmi);
982 MemManInfo16(&mmi);
983 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(context->SegEs,DI_reg(context));
984 /* the layout is just the same as MEMMANINFO, but without
985 * the dwSize entry.
987 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
988 break;
990 case 0x0501: /* Allocate memory block */
991 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
992 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
994 AX_reg(context) = 0x8012; /* linear memory not available */
995 SET_CFLAG(context);
996 } else {
997 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
998 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1000 break;
1002 case 0x0502: /* Free memory block */
1003 TRACE("free memory block (0x%08lx)\n",
1004 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1005 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1006 SI_reg(context)), offset) );
1007 break;
1009 case 0x0503: /* Resize memory block */
1010 TRACE("resize memory block (0x%08lx,%ld)\n",
1011 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1012 MAKELONG(CX_reg(context),BX_reg(context)));
1013 if (!(ptr = (BYTE *)DPMI_xrealloc(
1014 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1015 MAKELONG(CX_reg(context),BX_reg(context)))))
1017 AX_reg(context) = 0x8012; /* linear memory not available */
1018 SET_CFLAG(context);
1019 } else {
1020 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1021 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1023 break;
1025 case 0x0507: /* Modify page attributes */
1026 FIXME("modify page attributes unimplemented\n");
1027 break; /* Just ignore it */
1029 case 0x0600: /* Lock linear region */
1030 FIXME("lock linear region unimplemented\n");
1031 break; /* Just ignore it */
1033 case 0x0601: /* Unlock linear region */
1034 FIXME("unlock linear region unimplemented\n");
1035 break; /* Just ignore it */
1037 case 0x0602: /* Unlock real-mode region */
1038 FIXME("unlock realmode region unimplemented\n");
1039 break; /* Just ignore it */
1041 case 0x0603: /* Lock real-mode region */
1042 FIXME("lock realmode region unimplemented\n");
1043 break; /* Just ignore it */
1045 case 0x0604: /* Get page size */
1046 TRACE("get pagesize\n");
1047 BX_reg(context) = 0;
1048 CX_reg(context) = VIRTUAL_GetPageSize();
1049 break;
1051 case 0x0702: /* Mark page as demand-paging candidate */
1052 FIXME("mark page as demand-paging candidate\n");
1053 break; /* Just ignore it */
1055 case 0x0703: /* Discard page contents */
1056 FIXME("discard page contents\n");
1057 break; /* Just ignore it */
1059 case 0x0800: /* Physical address mapping */
1060 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1061 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1063 AX_reg(context) = 0x8021;
1064 SET_CFLAG(context);
1066 else
1068 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1069 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1070 RESET_CFLAG(context);
1072 break;
1074 default:
1075 INT_BARF( context, 0x31 );
1076 AX_reg(context) = 0x8001; /* unsupported function */
1077 SET_CFLAG(context);
1078 break;