Added all clipper&3d stubs.
[wine/multimedia.git] / msdos / dpmi.c
blobd49bc540484d942622b3272f46329c8920958de6
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <unistd.h>
8 #include <string.h>
9 #include "windows.h"
10 #include "heap.h"
11 #include "global.h"
12 #include "ldt.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "drive.h"
16 #include "msdos.h"
17 #include "task.h"
18 #include "dosexe.h"
19 #include "toolhelp.h"
20 #include "debug.h"
21 #include "selectors.h"
22 #include "thread.h"
23 #include "process.h"
24 #include "stackframe.h"
25 #include "callback.h"
27 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
29 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
31 static void* lastvalloced = NULL;
33 /* Structure for real-mode callbacks */
34 typedef struct
36 DWORD edi;
37 DWORD esi;
38 DWORD ebp;
39 DWORD reserved;
40 DWORD ebx;
41 DWORD edx;
42 DWORD ecx;
43 DWORD eax;
44 WORD fl;
45 WORD es;
46 WORD ds;
47 WORD fs;
48 WORD gs;
49 WORD ip;
50 WORD cs;
51 WORD sp;
52 WORD ss;
53 } REALMODECALL;
57 typedef struct tagRMCB {
58 DWORD address;
59 DWORD proc_ofs,proc_sel;
60 DWORD regs_ofs,regs_sel;
61 struct tagRMCB *next;
62 } RMCB;
64 static RMCB *FirstRMCB = NULL;
66 UINT16 DPMI_wrap_seg;
68 /**********************************************************************
69 * DPMI_xalloc
70 * special virtualalloc, allocates lineary monoton growing memory.
71 * (the usual VirtualAlloc does not satisfy that restriction)
73 static LPVOID
74 DPMI_xalloc(int len) {
75 LPVOID ret;
76 LPVOID oldlastv = lastvalloced;
78 if (lastvalloced) {
79 int xflag = 0;
80 ret = NULL;
81 while (!ret) {
82 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
83 if (!ret)
84 lastvalloced+=0x10000;
85 /* we failed to allocate one in the first round.
86 * try non-linear
88 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
89 FIXME(int31,"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(int31,"failed to allocate any memory of %d bytes!\n",len);
99 return NULL;
102 } else
103 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
104 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
105 return ret;
108 static void
109 DPMI_xfree(LPVOID ptr) {
110 VirtualFree(ptr,0,MEM_RELEASE);
113 /* FIXME: perhaps we could grow this mapped area... */
114 static LPVOID
115 DPMI_xrealloc(LPVOID ptr,int newsize) {
116 MEMORY_BASIC_INFORMATION mbi;
117 LPVOID newptr;
119 newptr = DPMI_xalloc(newsize);
120 if (ptr) {
121 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
122 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
123 return NULL;
125 if (mbi.State == MEM_FREE) {
126 FIXME(int31,"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, CONTEXT *context )
144 EAX_reg(context) = call->eax;
145 EBX_reg(context) = call->ebx;
146 ECX_reg(context) = call->ecx;
147 EDX_reg(context) = call->edx;
148 ESI_reg(context) = call->esi;
149 EDI_reg(context) = call->edi;
150 EBP_reg(context) = call->ebp;
151 EFL_reg(context) = call->fl | V86_FLAG;
152 EIP_reg(context) = call->ip;
153 ESP_reg(context) = call->sp;
154 CS_reg(context) = call->cs;
155 DS_reg(context) = call->ds;
156 ES_reg(context) = call->es;
157 FS_reg(context) = call->fs;
158 GS_reg(context) = call->gs;
159 SS_reg(context) = call->ss;
160 (char*)V86BASE(context) = DOSMEM_MemoryBase(0);
164 /**********************************************************************
165 * INT_SetRealModeContext
167 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT *context )
169 call->eax = EAX_reg(context);
170 call->ebx = EBX_reg(context);
171 call->ecx = ECX_reg(context);
172 call->edx = EDX_reg(context);
173 call->esi = ESI_reg(context);
174 call->edi = EDI_reg(context);
175 call->ebp = EBP_reg(context);
176 call->fl = FL_reg(context);
177 call->ip = IP_reg(context);
178 call->sp = SP_reg(context);
179 call->cs = CS_reg(context);
180 call->ds = DS_reg(context);
181 call->es = ES_reg(context);
182 call->fs = FS_reg(context);
183 call->gs = GS_reg(context);
184 call->ss = SS_reg(context);
188 /**********************************************************************
189 * DPMI_CallRMCBProc
191 * This routine does the hard work of calling a callback procedure.
193 static void DPMI_CallRMCBProc( CONTEXT *context, RMCB *rmcb, WORD flag )
195 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
196 /* Wine-internal RMCB, call directly */
197 ((RMCBPROC)rmcb->proc_ofs)(context);
198 } else {
199 #ifdef __i386__
200 UINT16 ss,es;
201 DWORD edi;
203 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
204 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase(0) + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
206 FIXME(int31,"untested!\n");
208 /* The called proc ends with an IRET, and takes these parameters:
209 * DS:ESI = pointer to real-mode SS:SP
210 * ES:EDI = pointer to real-mode call structure
211 * It returns:
212 * ES:EDI = pointer to real-mode call structure (may be a copy)
213 * It is the proc's responsibility to change the return CS:IP in the
214 * real-mode call structure. */
215 if (flag & 1) {
216 /* 32-bit DPMI client */
217 __asm__ __volatile__("
218 pushl %%es
219 pushl %%ds
220 pushfl
221 movl %4,%%es
222 movl %3,%%ds
223 lcall %2
224 popl %%ds
225 movl %%es,%0
226 popl %%es
228 : "=g" (es), "=D" (edi)
229 : "m" (rmcb->proc_ofs),
230 "g" (ss), "g" (rmcb->regs_sel),
231 "S" (ESP_reg(context)), "D" (rmcb->regs_ofs)
232 : "eax", "ecx", "edx", "esi", "ebp" );
233 } else {
234 /* 16-bit DPMI client */
235 CONTEXT ctx = *context;
236 CS_reg(&ctx) = rmcb->proc_sel;
237 EIP_reg(&ctx) = rmcb->proc_ofs;
238 DS_reg(&ctx) = ss;
239 ESI_reg(&ctx) = ESP_reg(context);
240 ES_reg(&ctx) = rmcb->regs_sel;
241 EDI_reg(&ctx) = rmcb->regs_ofs;
242 Callbacks->CallRegisterShortProc(&ctx, 2);
243 es = ES_reg(&ctx);
244 edi = EDI_reg(&ctx);
246 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ss,0));
247 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
248 #else
249 ERR(int31,"RMCBs only implemented for i386\n");
250 #endif
255 /**********************************************************************
256 * DPMI_CallRMProc
258 * This routine does the hard work of calling a real mode procedure.
260 int DPMI_CallRMProc( CONTEXT *context, LPWORD stack, int args, int iret )
262 LPWORD stack16;
263 THDB *thdb = THREAD_Current();
264 LPVOID addr = NULL; /* avoid gcc warning */
265 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
266 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
267 RMCB *CurrRMCB;
268 int alloc = 0, already = 0;
269 WORD sel;
270 SEGPTR seg_addr;
272 GlobalUnlock16( GetCurrentTask() );
274 TRACE(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
275 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
276 TRACE(int31, "ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
277 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
278 CS_reg(context), IP_reg(context), args, iret?"IRET":"FAR" );
280 callrmproc_again:
282 /* shortcut for chaining to internal interrupt handlers */
283 if ((CS_reg(context) == 0xF000) && iret) {
284 return INT_RealModeInterrupt( IP_reg(context)/4, context);
287 /* shortcut for RMCBs */
288 CurrRMCB = FirstRMCB;
290 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
291 CurrRMCB = CurrRMCB->next;
293 #ifdef MZ_SUPPORTED
294 FIXME(int31,"DPMI real-mode call using DOS VM task system, not fully tested!\n");
295 if (!(CurrRMCB || pModule->lpDosTask)) {
296 TRACE(int31,"creating VM86 task\n");
297 if (MZ_InitTask( MZ_AllocDPMITask( pModule->self ) ) < 32) {
298 ERR(int31,"could not setup VM86 task\n");
299 return 1;
302 if (!already) {
303 if (!SS_reg(context)) {
304 alloc = 1; /* allocate default stack */
305 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
306 SP_reg(context) = 64-2;
307 stack16 += 32-1;
308 if (!addr) {
309 ERR(int31,"could not allocate default stack\n");
310 return 1;
312 } else {
313 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
315 SP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
316 #else
317 if (!already) {
318 stack16 = THREAD_STACK16(thdb);
319 #endif
320 stack16 -= args;
321 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
322 /* push flags if iret */
323 if (iret) {
324 stack16--; args++;
325 *stack16 = FL_reg(context);
327 #ifdef MZ_SUPPORTED
328 /* push return address (return to interrupt wrapper) */
329 *(--stack16) = DPMI_wrap_seg;
330 *(--stack16) = 0;
331 /* adjust stack */
332 SP_reg(context) -= 2*sizeof(WORD);
333 #endif
334 already = 1;
337 if (CurrRMCB) {
338 /* RMCB call, invoke protected-mode handler directly */
339 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
340 /* check if we returned to where we thought we would */
341 if ((CS_reg(context) != DPMI_wrap_seg) ||
342 (IP_reg(context) != 0)) {
343 /* we need to continue at different address in real-mode space,
344 so we need to set it all up for real mode again */
345 goto callrmproc_again;
347 } else {
348 #ifdef MZ_SUPPORTED
349 #if 0 /* this was probably unnecessary */
350 /* push call address */
351 *(--stack16) = CS_reg(context);
352 *(--stack16) = IP_reg(context);
353 /* adjust stack */
354 SP_reg(context) -= 2*sizeof(WORD);
355 /* set initial CS:IP to the wrapper's "lret" */
356 CS_reg(context) = DPMI_wrap_seg;
357 IP_reg(context) = 2;
358 #endif
359 TRACE(int31,"entering real mode...\n");
360 DOSVM_Enter( context );
361 TRACE(int31,"returned from real-mode call\n");
362 #else
363 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
364 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
365 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
367 CS_reg(context) = HIWORD(seg_addr);
368 IP_reg(context) = LOWORD(seg_addr);
369 EBP_reg(context) = OFFSETOF( thdb->cur_stack )
370 + (WORD)&((STACK16FRAME*)0)->bp;
371 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
372 UnMapLS(seg_addr);
373 #endif
375 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
376 return 0;
380 /**********************************************************************
381 * CallRMInt
383 static void CallRMInt( CONTEXT *context )
385 CONTEXT realmode_ctx;
386 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
387 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
388 DI_reg(context) );
389 INT_GetRealModeContext( call, &realmode_ctx );
391 /* we need to check if a real-mode program has hooked the interrupt */
392 if (HIWORD(rm_int)!=0xF000) {
393 /* yup, which means we need to switch to real mode... */
394 CS_reg(&realmode_ctx) = HIWORD(rm_int);
395 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
396 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
397 SET_CFLAG(context);
398 } else {
399 RESET_CFLAG(context);
400 /* use the IP we have instead of BL_reg, in case some apps
401 decide to move interrupts around for whatever reason... */
402 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
403 SET_CFLAG(context);
404 if (EFL_reg(context)&1) {
405 FIXME(int31,"%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
406 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
407 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
408 FIXME(int31," ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
409 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
410 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
413 INT_SetRealModeContext( call, &realmode_ctx );
417 static void CallRMProc( CONTEXT *context, int iret )
419 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
420 CONTEXT context16;
422 TRACE(int31, "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
423 p->eax, p->ebx, p->ecx, p->edx);
424 TRACE(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
425 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
427 if (!(p->cs) && !(p->ip)) { /* remove this check
428 if Int21/6501 case map function
429 has been implemented */
430 SET_CFLAG(context);
431 return;
433 INT_GetRealModeContext(p, &context16);
434 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context)))+3,
435 CX_reg(context), iret );
436 INT_SetRealModeContext(p, &context16);
440 static void WINAPI RMCallbackProc( RMCB *rmcb )
442 /* This routine should call DPMI_CallRMCBProc, but we don't have the
443 register structure available - this is easily fixed by going through
444 a Win16 register relay instead of calling RMCallbackProc "directly",
445 but I won't bother at this time. */
446 FIXME(int31,"not properly supported on your architecture!\n");
449 static RMCB *DPMI_AllocRMCB( void )
451 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
452 UINT16 uParagraph;
454 if (NewRMCB)
456 #ifdef MZ_SUPPORTED
457 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
458 LPBYTE p = RMCBmem;
460 *p++ = 0xcd; /* RMCB: */
461 *p++ = 0x31; /* int $0x31 */
462 /* it is the called procedure's task to change the return CS:EIP
463 the DPMI 0.9 spec states that if it doesn't, it will be called again */
464 *p++ = 0xeb;
465 *p++ = 0xfc; /* jmp RMCB */
466 #else
467 LPVOID RMCBmem = DOSMEM_GetBlock(0, 15, &uParagraph);
468 LPBYTE p = RMCBmem;
470 *p++ = 0x68; /* pushl */
471 *(LPVOID *)p = NewRMCB;
472 p+=4;
473 *p++ = 0x9a; /* lcall */
474 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME: register relay */
475 p+=4;
476 GET_CS(*(WORD *)p);
477 p+=2;
478 *p++=0xc3; /* lret (FIXME?) */
479 #endif
480 NewRMCB->address = MAKELONG(0, uParagraph);
481 NewRMCB->next = FirstRMCB;
482 FirstRMCB = NewRMCB;
484 return NewRMCB;
488 static void AllocRMCB( CONTEXT *context )
490 RMCB *NewRMCB = DPMI_AllocRMCB();
492 TRACE(int31, "Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
494 if (NewRMCB)
496 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
497 NewRMCB->proc_ofs = SI_reg(context);
498 NewRMCB->proc_sel = DS_reg(context);
499 NewRMCB->regs_ofs = DI_reg(context);
500 NewRMCB->regs_sel = ES_reg(context);
501 CX_reg(context) = HIWORD(NewRMCB->address);
502 DX_reg(context) = LOWORD(NewRMCB->address);
504 else
506 AX_reg(context) = 0x8015; /* callback unavailable */
507 SET_CFLAG(context);
512 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
514 RMCB *NewRMCB = DPMI_AllocRMCB();
516 if (NewRMCB) {
517 NewRMCB->proc_ofs = (DWORD)proc;
518 NewRMCB->proc_sel = 0;
519 NewRMCB->regs_ofs = 0;
520 NewRMCB->regs_sel = 0;
521 return (FARPROC16)(NewRMCB->address);
523 return NULL;
527 static int DPMI_FreeRMCB( DWORD address )
529 RMCB *CurrRMCB = FirstRMCB;
530 RMCB *PrevRMCB = NULL;
532 while (CurrRMCB && (CurrRMCB->address != address))
534 PrevRMCB = CurrRMCB;
535 CurrRMCB = CurrRMCB->next;
537 if (CurrRMCB)
539 if (PrevRMCB)
540 PrevRMCB->next = CurrRMCB->next;
541 else
542 FirstRMCB = CurrRMCB->next;
543 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
544 HeapFree(GetProcessHeap(), 0, CurrRMCB);
545 return 0;
547 return 1;
551 static void FreeRMCB( CONTEXT *context )
553 FIXME(int31, "callback address: %04x:%04x\n",
554 CX_reg(context), DX_reg(context));
556 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
557 AX_reg(context) = 0x8024; /* invalid callback address */
558 SET_CFLAG(context);
563 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
565 DPMI_FreeRMCB( (DWORD)proc );
569 #ifdef MZ_SUPPORTED
570 /* (see loader/dos/module.c, function MZ_InitDPMI) */
572 static void StartPM( CONTEXT *context, LPDOSTASK lpDosTask )
574 char *base = DOSMEM_MemoryBase(0);
575 UINT16 cs, ss, ds, es;
576 CONTEXT pm_ctx;
578 RESET_CFLAG(context);
579 lpDosTask->dpmi_flag = AX_reg(context);
580 /* our mode switch wrapper have placed the desired CS into DX */
581 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
582 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
583 ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
584 es = SELECTOR_AllocBlock( base + (DWORD)(lpDosTask->psp_seg<<4), 0x100, SEGMENT_DATA, FALSE, FALSE );
586 pm_ctx = *context;
587 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
588 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
589 AX_reg(&pm_ctx) = ss;
590 DX_reg(&pm_ctx) = cs;
591 DS_reg(&pm_ctx) = ds;
592 ES_reg(&pm_ctx) = es;
593 FS_reg(&pm_ctx) = 0;
594 GS_reg(&pm_ctx) = 0;
596 TRACE(int31,"DOS program is now entering protected mode\n");
597 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
599 /* in the current state of affairs, we won't ever actually return here... */
601 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(es,0));
602 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ds,0));
603 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ss,0));
604 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(cs,0));
606 #endif
608 /**********************************************************************
609 * INT_Int31Handler
611 * Handler for int 31h (DPMI).
614 void WINAPI INT_Int31Handler( CONTEXT *context )
617 * Note: For Win32s processes, the whole linear address space is
618 * shifted by 0x10000 relative to the OS linear address space.
619 * See the comment in msdos/vxd.c.
621 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
623 DWORD dw;
624 BYTE *ptr;
626 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
627 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
629 GlobalUnlock16( GetCurrentTask() );
631 #ifdef MZ_SUPPORTED
632 if (ISV86(context) && pModule && pModule->lpDosTask) {
633 /* Called from real mode, check if it's our wrapper */
634 TRACE(int31,"called from real mode\n");
635 if (CS_reg(context)==pModule->lpDosTask->dpmi_seg) {
636 /* This is the protected mode switch */
637 StartPM(context,pModule->lpDosTask);
638 return;
639 } else
640 if (CS_reg(context)==pModule->lpDosTask->xms_seg) {
641 /* This is the XMS driver entry point */
642 XMS_Handler(context);
643 return;
644 } else
646 /* Check for RMCB */
647 RMCB *CurrRMCB = FirstRMCB;
649 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
650 CurrRMCB = CurrRMCB->next;
652 if (CurrRMCB) {
653 /* RMCB call, propagate to protected-mode handler */
654 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask->dpmi_flag);
655 return;
659 #endif
661 RESET_CFLAG(context);
662 switch(AX_reg(context))
664 case 0x0000: /* Allocate LDT descriptors */
665 TRACE(int31,"allocate LDT descriptors (%d)\n",CX_reg(context));
666 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
668 TRACE(int31,"failed\n");
669 AX_reg(context) = 0x8011; /* descriptor unavailable */
670 SET_CFLAG(context);
672 TRACE(int31,"success, array starts at 0x%04x\n",AX_reg(context));
673 break;
675 case 0x0001: /* Free LDT descriptor */
676 TRACE(int31,"free LDT descriptor (0x%04x)\n",BX_reg(context));
677 if (FreeSelector( BX_reg(context) ))
679 AX_reg(context) = 0x8022; /* invalid selector */
680 SET_CFLAG(context);
682 else
684 /* If a segment register contains the selector being freed, */
685 /* set it to zero. */
686 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
687 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
688 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
689 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
691 break;
693 case 0x0002: /* Real mode segment to descriptor */
694 TRACE(int31,"real mode segment to descriptor (0x%04x)\n",BX_reg(context));
696 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
697 switch(BX_reg(context))
699 case 0x0000: entryPoint = 183; break; /* __0000H */
700 case 0x0040: entryPoint = 193; break; /* __0040H */
701 case 0xa000: entryPoint = 174; break; /* __A000H */
702 case 0xb000: entryPoint = 181; break; /* __B000H */
703 case 0xb800: entryPoint = 182; break; /* __B800H */
704 case 0xc000: entryPoint = 195; break; /* __C000H */
705 case 0xd000: entryPoint = 179; break; /* __D000H */
706 case 0xe000: entryPoint = 190; break; /* __E000H */
707 case 0xf000: entryPoint = 194; break; /* __F000H */
708 default:
709 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
710 break;
712 if (entryPoint)
713 AX_reg(context) = LOWORD(NE_GetEntryPoint(
714 GetModuleHandle16( "KERNEL" ),
715 entryPoint ));
717 break;
719 case 0x0003: /* Get next selector increment */
720 TRACE(int31,"get selector increment (__AHINCR)\n");
721 AX_reg(context) = __AHINCR;
722 break;
724 case 0x0004: /* Lock selector (not supported) */
725 FIXME(int31,"lock selector not supported\n");
726 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
727 break;
729 case 0x0005: /* Unlock selector (not supported) */
730 FIXME(int31,"unlock selector not supported\n");
731 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
732 break;
734 case 0x0006: /* Get selector base address */
735 TRACE(int31,"get selector base address (0x%04x)\n",BX_reg(context));
736 if (!(dw = GetSelectorBase( BX_reg(context) )))
738 AX_reg(context) = 0x8022; /* invalid selector */
739 SET_CFLAG(context);
741 else
743 #ifdef MZ_SUPPORTED
744 if (pModule && pModule->lpDosTask) {
745 DWORD base = (DWORD)DOSMEM_MemoryBase(pModule->self);
746 if ((dw >= base) && (dw < base + 0x110000)) dw -= base;
748 #endif
749 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
750 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
752 break;
754 case 0x0007: /* Set selector base address */
755 TRACE(int31, "set selector base address (0x%04x,0x%08lx)\n",
756 BX_reg(context),
757 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
758 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
759 #ifdef MZ_SUPPORTED
760 /* well, what else could we possibly do? */
761 if (pModule && pModule->lpDosTask) {
762 if (dw < 0x110000) dw += (DWORD)DOSMEM_MemoryBase(pModule->self);
764 #endif
765 SetSelectorBase(BX_reg(context), dw);
766 break;
768 case 0x0008: /* Set selector limit */
769 TRACE(int31,"set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
770 dw = MAKELONG( DX_reg(context), CX_reg(context) );
771 #ifdef MZ_SUPPORTED
772 if (pModule && pModule->lpDosTask) {
773 DWORD base = GetSelectorBase( BX_reg(context) );
774 if ((dw == 0xffffffff) || ((base < 0x110000) && (base + dw > 0x110000))) {
775 AX_reg(context) = 0x8021; /* invalid value */
776 SET_CFLAG(context);
777 break;
780 #endif
781 SetSelectorLimit( BX_reg(context), dw );
782 break;
784 case 0x0009: /* Set selector access rights */
785 TRACE(int31,"set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
786 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
787 break;
789 case 0x000a: /* Allocate selector alias */
790 TRACE(int31,"allocate selector alias (0x%04x)\n",BX_reg(context));
791 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
793 AX_reg(context) = 0x8011; /* descriptor unavailable */
794 SET_CFLAG(context);
796 break;
798 case 0x000b: /* Get descriptor */
799 TRACE(int31,"get descriptor (0x%04x)\n",BX_reg(context));
801 ldt_entry entry;
802 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
803 entry.base = W32S_WINE2APP(entry.base, offset);
805 /* FIXME: should use ES:EDI for 32-bit clients */
806 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
807 DI_reg(context) ), &entry );
809 break;
811 case 0x000c: /* Set descriptor */
812 TRACE(int31,"set descriptor (0x%04x)\n",BX_reg(context));
814 ldt_entry entry;
815 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
816 DI_reg(context) ), &entry );
817 entry.base = W32S_APP2WINE(entry.base, offset);
819 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
821 break;
823 case 0x000d: /* Allocate specific LDT descriptor */
824 FIXME(int31,"allocate descriptor (0x%04x), stub!\n",BX_reg(context));
825 AX_reg(context) = 0x8011; /* descriptor unavailable */
826 SET_CFLAG(context);
827 break;
828 case 0x0100: /* Allocate DOS memory block */
829 TRACE(int31,"allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
830 dw = GlobalDOSAlloc((DWORD)BX_reg(context)<<4);
831 if (dw) {
832 AX_reg(context) = HIWORD(dw);
833 DX_reg(context) = LOWORD(dw);
834 } else {
835 AX_reg(context) = 0x0008; /* insufficient memory */
836 BX_reg(context) = DOSMEM_Available(0)>>4;
837 SET_CFLAG(context);
839 break;
840 case 0x0101: /* Free DOS memory block */
841 TRACE(int31,"free DOS memory block (0x%04x)\n",DX_reg(context));
842 dw = GlobalDOSFree(DX_reg(context));
843 if (!dw) {
844 AX_reg(context) = 0x0009; /* memory block address invalid */
845 SET_CFLAG(context);
847 break;
848 case 0x0200: /* get real mode interrupt vector */
849 FIXME(int31,"get realmode interupt vector(0x%02x) unimplemented.\n",
850 BL_reg(context));
851 SET_CFLAG(context);
852 break;
853 case 0x0201: /* set real mode interrupt vector */
854 FIXME(int31, "set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
855 SET_CFLAG(context);
856 break;
857 case 0x0204: /* Get protected mode interrupt vector */
858 TRACE(int31,"get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
859 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
860 CX_reg(context) = HIWORD(dw);
861 DX_reg(context) = LOWORD(dw);
862 break;
864 case 0x0205: /* Set protected mode interrupt vector */
865 TRACE(int31,"set protected mode interrupt handler (0x%02x,%p), stub!\n",
866 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
867 INT_SetPMHandler( BL_reg(context),
868 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
869 DX_reg(context) ));
870 break;
872 case 0x0300: /* Simulate real mode interrupt */
873 CallRMInt( context );
874 break;
876 case 0x0301: /* Call real mode procedure with far return */
877 CallRMProc( context, FALSE );
878 break;
880 case 0x0302: /* Call real mode procedure with interrupt return */
881 CallRMProc( context, TRUE );
882 break;
884 case 0x0303: /* Allocate Real Mode Callback Address */
885 AllocRMCB( context );
886 break;
888 case 0x0304: /* Free Real Mode Callback Address */
889 FreeRMCB( context );
890 break;
892 case 0x0400: /* Get DPMI version */
893 TRACE(int31,"get DPMI version\n");
895 SYSTEM_INFO si;
897 GetSystemInfo(&si);
898 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
899 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
900 CL_reg(context) = si.wProcessorLevel;
901 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
902 break;
904 case 0x0500: /* Get free memory information */
905 TRACE(int31,"get free memory information\n");
907 MEMMANINFO mmi;
909 mmi.dwSize = sizeof(mmi);
910 MemManInfo(&mmi);
911 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
912 /* the layout is just the same as MEMMANINFO, but without
913 * the dwSize entry.
915 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
916 break;
918 case 0x0501: /* Allocate memory block */
919 TRACE(int31,"allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
920 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
922 AX_reg(context) = 0x8012; /* linear memory not available */
923 SET_CFLAG(context);
924 } else {
925 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
926 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
928 break;
930 case 0x0502: /* Free memory block */
931 TRACE(int31, "free memory block (0x%08lx)\n",
932 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
933 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
934 SI_reg(context)), offset) );
935 break;
937 case 0x0503: /* Resize memory block */
938 TRACE(int31, "resize memory block (0x%08lx,%ld)\n",
939 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
940 MAKELONG(CX_reg(context),BX_reg(context)));
941 if (!(ptr = (BYTE *)DPMI_xrealloc(
942 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
943 MAKELONG(CX_reg(context),BX_reg(context)))))
945 AX_reg(context) = 0x8012; /* linear memory not available */
946 SET_CFLAG(context);
947 } else {
948 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
949 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
951 break;
953 case 0x0507: /* Modify page attributes */
954 FIXME(int31,"modify page attributes unimplemented\n");
955 break; /* Just ignore it */
957 case 0x0600: /* Lock linear region */
958 FIXME(int31,"lock linear region unimplemented\n");
959 break; /* Just ignore it */
961 case 0x0601: /* Unlock linear region */
962 FIXME(int31,"unlock linear region unimplemented\n");
963 break; /* Just ignore it */
965 case 0x0602: /* Unlock real-mode region */
966 FIXME(int31,"unlock realmode region unimplemented\n");
967 break; /* Just ignore it */
969 case 0x0603: /* Lock real-mode region */
970 FIXME(int31,"lock realmode region unimplemented\n");
971 break; /* Just ignore it */
973 case 0x0604: /* Get page size */
974 TRACE(int31,"get pagesize\n");
975 BX_reg(context) = 0;
976 CX_reg(context) = VIRTUAL_GetPageSize();
977 break;
979 case 0x0702: /* Mark page as demand-paging candidate */
980 FIXME(int31,"mark page as demand-paging candidate\n");
981 break; /* Just ignore it */
983 case 0x0703: /* Discard page contents */
984 FIXME(int31,"discard page contents\n");
985 break; /* Just ignore it */
987 case 0x0800: /* Physical address mapping */
988 FIXME(int31,"map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
989 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
991 AX_reg(context) = 0x8021;
992 SET_CFLAG(context);
994 else
996 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
997 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
998 RESET_CFLAG(context);
1000 break;
1002 default:
1003 INT_BARF( context, 0x31 );
1004 AX_reg(context) = 0x8001; /* unsupported function */
1005 SET_CFLAG(context);
1006 break;