Some small corrections to EnumSystemLocales32AW
[wine/multimedia.git] / msdos / dpmi.c
blob0d078a3c83a8b6da07924a4050fc47c1f3f86403
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 struct tagRMCB *next;
61 } RMCB;
63 static RMCB *FirstRMCB = NULL;
65 /**********************************************************************
66 * DPMI_xalloc
67 * special virtualalloc, allocates lineary monoton growing memory.
68 * (the usual VirtualAlloc does not satisfy that restriction)
70 static LPVOID
71 DPMI_xalloc(int len) {
72 LPVOID ret;
73 LPVOID oldlastv = lastvalloced;
75 if (lastvalloced) {
76 int xflag = 0;
77 ret = NULL;
78 while (!ret) {
79 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
80 if (!ret)
81 lastvalloced+=0x10000;
82 /* we failed to allocate one in the first round.
83 * try non-linear
85 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
86 FIXME(int31,"failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
87 xflag++;
89 /* if we even fail to allocate something in the next
90 * round, return NULL
92 if ((xflag==1) && (lastvalloced >= oldlastv))
93 xflag++;
94 if ((xflag==2) && (lastvalloced < oldlastv)) {
95 FIXME(int31,"failed to allocate any memory of %d bytes!\n",len);
96 return NULL;
99 } else
100 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
101 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
102 return ret;
105 static void
106 DPMI_xfree(LPVOID ptr) {
107 VirtualFree(ptr,0,MEM_RELEASE);
110 /* FIXME: perhaps we could grow this mapped area... */
111 static LPVOID
112 DPMI_xrealloc(LPVOID ptr,int newsize) {
113 MEMORY_BASIC_INFORMATION mbi;
114 LPVOID newptr;
116 newptr = DPMI_xalloc(newsize);
117 if (ptr) {
118 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
119 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
120 return NULL;
122 if (mbi.State == MEM_FREE) {
123 FIXME(int31,"realloc of DPMI_xallocd region %p?\n",ptr);
124 return NULL;
126 /* We do not shrink allocated memory. most reallocs
127 * only do grows anyway
129 if (newsize<=mbi.RegionSize)
130 return ptr;
131 memcpy(newptr,ptr,mbi.RegionSize);
132 DPMI_xfree(ptr);
134 return newptr;
136 /**********************************************************************
137 * INT_GetRealModeContext
139 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT *context )
141 EAX_reg(context) = call->eax;
142 EBX_reg(context) = call->ebx;
143 ECX_reg(context) = call->ecx;
144 EDX_reg(context) = call->edx;
145 ESI_reg(context) = call->esi;
146 EDI_reg(context) = call->edi;
147 EBP_reg(context) = call->ebp;
148 EFL_reg(context) = call->fl | V86_FLAG;
149 EIP_reg(context) = call->ip;
150 ESP_reg(context) = call->sp;
151 CS_reg(context) = call->cs;
152 DS_reg(context) = call->ds;
153 ES_reg(context) = call->es;
154 FS_reg(context) = call->fs;
155 GS_reg(context) = call->gs;
156 (char*)V86BASE(context) = DOSMEM_MemoryBase(0);
160 /**********************************************************************
161 * INT_SetRealModeContext
163 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT *context )
165 call->eax = EAX_reg(context);
166 call->ebx = EBX_reg(context);
167 call->ecx = ECX_reg(context);
168 call->edx = EDX_reg(context);
169 call->esi = ESI_reg(context);
170 call->edi = EDI_reg(context);
171 call->ebp = EBP_reg(context);
172 call->fl = FL_reg(context);
173 call->ip = IP_reg(context);
174 call->sp = SP_reg(context);
175 call->cs = CS_reg(context);
176 call->ds = DS_reg(context);
177 call->es = ES_reg(context);
178 call->fs = FS_reg(context);
179 call->gs = GS_reg(context);
183 /**********************************************************************
184 * DPMI_CallRMProc
186 * This routine does the hard work of calling a real mode procedure.
188 int DPMI_CallRMProc( CONTEXT *context, LPWORD stack, int args, int iret )
190 LPWORD stack16;
191 THDB *thdb = THREAD_Current();
192 LPVOID addr;
193 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
194 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
195 int alloc = 0;
196 WORD sel;
197 SEGPTR seg_addr;
199 GlobalUnlock16( GetCurrentTask() );
201 TRACE(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
202 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
203 TRACE(int31, "ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments\n",
204 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
205 CS_reg(context), IP_reg(context), args );
207 #ifdef MZ_SUPPORTED
208 FIXME(int31,"DPMI real-mode call using DOS VM task system, not fully tested\n");
209 if (!pModule->lpDosTask) {
210 TRACE(int31,"creating VM86 task\n");
211 if (MZ_InitTask( MZ_AllocDPMITask( pModule->self ) ) < 32) {
212 ERR(int31,"could not setup VM86 task\n");
213 return 1;
216 if (!SS_reg(context)) {
217 alloc = 1; /* allocate default stack */
218 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, &(SS_reg(context)) );
219 SP_reg(context) = 64-2;
220 stack16 += 32-1;
221 if (!addr) {
222 ERR(int31,"could not allocate default stack\n");
223 return 1;
225 } else {
226 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
227 addr = NULL; /* avoid gcc warning */
229 SP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
230 #else
231 stack16 = THREAD_STACK16(thdb);
232 #endif
233 stack16 -= args;
234 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
235 /* push flags if iret */
236 if (iret) {
237 stack16--; args++;
238 *stack16 = FL_reg(context);
240 #ifdef MZ_SUPPORTED
241 /* push return address (return to interrupt wrapper) */
242 *(--stack16) = pModule->lpDosTask->dpmi_seg;
243 *(--stack16) = pModule->lpDosTask->wrap_ofs;
244 /* push call address */
245 *(--stack16) = CS_reg(context);
246 *(--stack16) = IP_reg(context);
247 /* adjust stack */
248 SP_reg(context) -= 4*sizeof(WORD);
249 /* set initial CS:IP to the wrapper's "lret" */
250 CS_reg(context) = pModule->lpDosTask->dpmi_seg;
251 IP_reg(context) = pModule->lpDosTask->call_ofs;
252 TRACE(int31,"entering real mode...\n");
253 DOSVM_Enter( context );
254 TRACE(int31,"returned from real-mode call\n");
255 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
256 #else
257 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
258 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
259 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
261 CS_reg(context) = HIWORD(seg_addr);
262 IP_reg(context) = LOWORD(seg_addr);
263 EBP_reg(context) = OFFSETOF( thdb->cur_stack )
264 + (WORD)&((STACK16FRAME*)0)->bp;
265 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
266 UnMapLS(seg_addr);
267 #endif
268 return 0;
272 /**********************************************************************
273 * INT_DoRealModeInt
275 static void INT_DoRealModeInt( CONTEXT *context )
277 CONTEXT realmode_ctx;
278 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
279 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
280 DI_reg(context) );
281 INT_GetRealModeContext( call, &realmode_ctx );
283 #ifdef MZ_SUPPORTED
284 /* we need to check if a real-mode program has hooked the interrupt */
285 if (HIWORD(rm_int)!=0xF000) {
286 /* yup, which means we need to switch to real mode... */
287 CS_reg(&realmode_ctx) = HIWORD(rm_int);
288 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
289 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
290 SET_CFLAG(context);
291 } else
292 #endif
294 RESET_CFLAG(context);
295 if (INT_RealModeInterrupt( BL_reg(context), &realmode_ctx ))
296 SET_CFLAG(context);
297 if (EFL_reg(context)&1) {
298 FIXME(int31,"%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
299 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
300 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
301 FIXME(int31," ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
302 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
303 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
306 INT_SetRealModeContext( call, &realmode_ctx );
310 static void CallRMProcFar( CONTEXT *context )
312 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
313 CONTEXT context16;
314 THDB *thdb = THREAD_Current();
315 WORD argsize, sel;
316 LPVOID addr;
317 SEGPTR seg_addr;
319 TRACE(int31, "RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
320 p->eax, p->ebx, p->ecx, p->edx);
321 TRACE(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments\n",
322 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context) );
324 if (!(p->cs) && !(p->ip)) { /* remove this check
325 if Int21/6501 case map function
326 has been implemented */
327 SET_CFLAG(context);
328 return;
330 INT_GetRealModeContext(p, &context16);
332 #if 0
333 addr = DOSMEM_MapRealToLinear(MAKELONG(p->ip, p->cs));
334 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
335 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
337 CS_reg(&context16) = HIWORD(seg_addr);
338 IP_reg(&context16) = LOWORD(seg_addr);
339 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
340 + (WORD)&((STACK16FRAME*)0)->bp;
342 argsize = CX_reg(context)*sizeof(WORD);
343 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
344 (LPBYTE)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context))+6, argsize );
346 Callbacks->CallRegisterShortProc(&context16, argsize);
348 UnMapLS(seg_addr);
349 #else
350 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context)))+3,
351 CX_reg(context), 0 );
352 #endif
353 INT_SetRealModeContext(p, &context16);
357 void WINAPI RMCallbackProc( FARPROC16 pmProc, REALMODECALL *rmc )
359 CONTEXT ctx;
360 INT_GetRealModeContext(rmc, &ctx);
361 Callbacks->CallRegisterShortProc(&ctx, 0);
365 static void AllocRMCB( CONTEXT *context )
367 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
368 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
369 UINT16 uParagraph;
371 FIXME(int31, "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n", p->eax, p->ebx, p->ecx, p->edx);
372 FIXME(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n", p->esi, p->edi, p->es, p->ds, p->cs, p->ip);
373 FIXME(int31, " Function to call: %04x:%04x\n",
374 (WORD)DS_reg(context), SI_reg(context) );
376 if (NewRMCB)
378 LPVOID RMCBmem = DOSMEM_GetBlock(0, 20, &uParagraph);
379 LPBYTE p = RMCBmem;
381 *p++ = 0x68; /* pushl */
382 *(FARPROC16 *)p =
383 PTR_SEG_OFF_TO_LIN(ES_reg(context), SI_reg(context)); /* pmode proc to call */
384 p+=4;
385 *p++ = 0x68; /* pushl */
386 *(LPVOID *)p =
387 PTR_SEG_OFF_TO_LIN(ES_reg(context), DI_reg(context));
388 p+=4;
389 *p++ = 0x9a; /* lcall */
390 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME ? */
391 p+=4;
392 GET_CS(*(WORD *)p);
393 p+=2;
394 *p++=0xc3; /* retf */
395 NewRMCB->address = MAKELONG(0, uParagraph);
396 NewRMCB->next = FirstRMCB;
397 FirstRMCB = NewRMCB;
398 CX_reg(context) = uParagraph;
399 DX_reg(context) = 0;
401 else
403 AX_reg(context) = 0x8015; /* callback unavailable */
404 SET_CFLAG(context);
409 static void FreeRMCB( CONTEXT *context )
411 RMCB *CurrRMCB = FirstRMCB;
412 RMCB *PrevRMCB = NULL;
414 FIXME(int31, "callback address: %04x:%04x\n",
415 CX_reg(context), DX_reg(context));
417 while (CurrRMCB && (CurrRMCB->address != MAKELONG(DX_reg(context), CX_reg(context))))
419 PrevRMCB = CurrRMCB;
420 CurrRMCB = CurrRMCB->next;
422 if (CurrRMCB)
424 if (PrevRMCB)
425 PrevRMCB->next = CurrRMCB->next;
426 else
427 FirstRMCB = CurrRMCB->next;
428 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
429 HeapFree(GetProcessHeap(), 0, CurrRMCB);
431 else
433 AX_reg(context) = 0x8024; /* invalid callback address */
434 SET_CFLAG(context);
438 #ifdef MZ_SUPPORTED
439 /* (see loader/dos/module.c, function MZ_InitDPMI) */
441 static void StartPM( CONTEXT *context, LPDOSTASK lpDosTask )
443 char *base = DOSMEM_MemoryBase(0);
444 UINT16 cs, ss, ds, es;
445 CONTEXT pm_ctx;
447 RESET_CFLAG(context);
448 lpDosTask->dpmi_flag = AX_reg(context);
449 /* our mode switch wrapper have placed the desired CS into DX */
450 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
451 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
452 ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
453 es = SELECTOR_AllocBlock( base + (DWORD)(lpDosTask->psp_seg<<4), 0x100, SEGMENT_DATA, FALSE, FALSE );
455 pm_ctx = *context;
456 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
457 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
458 AX_reg(&pm_ctx) = ss;
459 DX_reg(&pm_ctx) = cs;
460 DS_reg(&pm_ctx) = ds;
461 ES_reg(&pm_ctx) = es;
462 FS_reg(&pm_ctx) = 0;
463 GS_reg(&pm_ctx) = 0;
465 TRACE(int31,"DOS program is now entering protected mode\n");
466 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
468 /* in the current state of affairs, we won't ever actually return here... */
470 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(es,0));
471 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ds,0));
472 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(ss,0));
473 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(cs,0));
475 #endif
477 /**********************************************************************
478 * INT_Int31Handler
480 * Handler for int 31h (DPMI).
483 void WINAPI INT_Int31Handler( CONTEXT *context )
486 * Note: For Win32s processes, the whole linear address space is
487 * shifted by 0x10000 relative to the OS linear address space.
488 * See the comment in msdos/vxd.c.
490 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
492 DWORD dw;
493 BYTE *ptr;
495 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
496 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
498 GlobalUnlock16( GetCurrentTask() );
500 #ifdef MZ_SUPPORTED
501 if (ISV86(context) && pModule && pModule->lpDosTask) {
502 /* Called from real mode, check if it's our wrapper */
503 TRACE(int31,"called from real mode\n");
504 if (CS_reg(context)==pModule->lpDosTask->dpmi_seg) {
505 /* This is the protected mode switch */
506 StartPM(context,pModule->lpDosTask);
507 return;
508 } else
509 if (CS_reg(context)==pModule->lpDosTask->xms_seg) {
510 /* This is the XMS driver entry point */
511 XMS_Handler(context);
512 return;
515 #endif
517 RESET_CFLAG(context);
518 switch(AX_reg(context))
520 case 0x0000: /* Allocate LDT descriptors */
521 TRACE(int31,"allocate LDT descriptors (%d)\n",CX_reg(context));
522 if (!(AX_reg(context) = AllocSelectorArray( CX_reg(context) )))
524 TRACE(int31,"failed\n");
525 AX_reg(context) = 0x8011; /* descriptor unavailable */
526 SET_CFLAG(context);
528 TRACE(int31,"success, array starts at 0x%04x\n",AX_reg(context));
529 break;
531 case 0x0001: /* Free LDT descriptor */
532 TRACE(int31,"free LDT descriptor (0x%04x)\n",BX_reg(context));
533 if (FreeSelector( BX_reg(context) ))
535 AX_reg(context) = 0x8022; /* invalid selector */
536 SET_CFLAG(context);
538 else
540 /* If a segment register contains the selector being freed, */
541 /* set it to zero. */
542 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
543 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
544 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
545 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
547 break;
549 case 0x0002: /* Real mode segment to descriptor */
550 TRACE(int31,"real mode segment to descriptor (0x%04x)\n",BX_reg(context));
552 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
553 switch(BX_reg(context))
555 case 0x0000: entryPoint = 183; break; /* __0000H */
556 case 0x0040: entryPoint = 193; break; /* __0040H */
557 case 0xa000: entryPoint = 174; break; /* __A000H */
558 case 0xb000: entryPoint = 181; break; /* __B000H */
559 case 0xb800: entryPoint = 182; break; /* __B800H */
560 case 0xc000: entryPoint = 195; break; /* __C000H */
561 case 0xd000: entryPoint = 179; break; /* __D000H */
562 case 0xe000: entryPoint = 190; break; /* __E000H */
563 case 0xf000: entryPoint = 194; break; /* __F000H */
564 default:
565 AX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
566 break;
568 if (entryPoint)
569 AX_reg(context) = LOWORD(NE_GetEntryPoint(
570 GetModuleHandle16( "KERNEL" ),
571 entryPoint ));
573 break;
575 case 0x0003: /* Get next selector increment */
576 TRACE(int31,"get selector increment (__AHINCR)\n");
577 AX_reg(context) = __AHINCR;
578 break;
580 case 0x0004: /* Lock selector (not supported) */
581 FIXME(int31,"lock selector not supported\n");
582 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
583 break;
585 case 0x0005: /* Unlock selector (not supported) */
586 FIXME(int31,"unlock selector not supported\n");
587 AX_reg(context) = 0; /* FIXME: is this a correct return value? */
588 break;
590 case 0x0006: /* Get selector base address */
591 TRACE(int31,"get selector base address (0x%04x)\n",BX_reg(context));
592 if (!(dw = GetSelectorBase( BX_reg(context) )))
594 AX_reg(context) = 0x8022; /* invalid selector */
595 SET_CFLAG(context);
597 else
599 #ifdef MZ_SUPPORTED
600 if (pModule && pModule->lpDosTask) {
601 DWORD base = (DWORD)DOSMEM_MemoryBase(pModule->self);
602 if ((dw >= base) && (dw < base + 0x110000)) dw -= base;
604 #endif
605 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
606 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
608 break;
610 case 0x0007: /* Set selector base address */
611 TRACE(int31, "set selector base address (0x%04x,0x%08lx)\n",
612 BX_reg(context),
613 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
614 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
615 #ifdef MZ_SUPPORTED
616 /* well, what else could we possibly do? */
617 if (pModule && pModule->lpDosTask) {
618 if (dw < 0x110000) dw += (DWORD)DOSMEM_MemoryBase(pModule->self);
620 #endif
621 SetSelectorBase(BX_reg(context), dw);
622 break;
624 case 0x0008: /* Set selector limit */
625 TRACE(int31,"set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
626 dw = MAKELONG( DX_reg(context), CX_reg(context) );
627 #ifdef MZ_SUPPORTED
628 if (pModule && pModule->lpDosTask) {
629 DWORD base = GetSelectorBase( BX_reg(context) );
630 if ((dw == 0xffffffff) || ((base < 0x110000) && (base + dw > 0x110000))) {
631 AX_reg(context) = 0x8021; /* invalid value */
632 SET_CFLAG(context);
633 break;
636 #endif
637 SetSelectorLimit( BX_reg(context), dw );
638 break;
640 case 0x0009: /* Set selector access rights */
641 TRACE(int31,"set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
642 SelectorAccessRights( BX_reg(context), 1, CX_reg(context) );
643 break;
645 case 0x000a: /* Allocate selector alias */
646 TRACE(int31,"allocate selector alias (0x%04x)\n",BX_reg(context));
647 if (!(AX_reg(context) = AllocCStoDSAlias( BX_reg(context) )))
649 AX_reg(context) = 0x8011; /* descriptor unavailable */
650 SET_CFLAG(context);
652 break;
654 case 0x000b: /* Get descriptor */
655 TRACE(int31,"get descriptor (0x%04x)\n",BX_reg(context));
657 ldt_entry entry;
658 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
659 entry.base = W32S_WINE2APP(entry.base, offset);
661 /* FIXME: should use ES:EDI for 32-bit clients */
662 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
663 DI_reg(context) ), &entry );
665 break;
667 case 0x000c: /* Set descriptor */
668 TRACE(int31,"set descriptor (0x%04x)\n",BX_reg(context));
670 ldt_entry entry;
671 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
672 DI_reg(context) ), &entry );
673 entry.base = W32S_APP2WINE(entry.base, offset);
675 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
677 break;
679 case 0x000d: /* Allocate specific LDT descriptor */
680 FIXME(int31,"allocate descriptor (0x%04x), stub!\n",BX_reg(context));
681 AX_reg(context) = 0x8011; /* descriptor unavailable */
682 SET_CFLAG(context);
683 break;
684 case 0x0100: /* Allocate DOS memory block */
685 TRACE(int31,"allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
686 dw = GlobalDOSAlloc((DWORD)BX_reg(context)<<4);
687 if (dw) {
688 AX_reg(context) = HIWORD(dw);
689 DX_reg(context) = LOWORD(dw);
690 } else {
691 AX_reg(context) = 0x0008; /* insufficient memory */
692 BX_reg(context) = DOSMEM_Available(0)>>4;
693 SET_CFLAG(context);
695 break;
696 case 0x0101: /* Free DOS memory block */
697 TRACE(int31,"free DOS memory block (0x%04x)\n",DX_reg(context));
698 dw = GlobalDOSFree(DX_reg(context));
699 if (!dw) {
700 AX_reg(context) = 0x0009; /* memory block address invalid */
701 SET_CFLAG(context);
703 break;
704 case 0x0200: /* get real mode interrupt vector */
705 FIXME(int31,"get realmode interupt vector(0x%02x) unimplemented.\n",
706 BL_reg(context));
707 SET_CFLAG(context);
708 break;
709 case 0x0201: /* set real mode interrupt vector */
710 FIXME(int31, "set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
711 SET_CFLAG(context);
712 break;
713 case 0x0204: /* Get protected mode interrupt vector */
714 TRACE(int31,"get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
715 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
716 CX_reg(context) = HIWORD(dw);
717 DX_reg(context) = LOWORD(dw);
718 break;
720 case 0x0205: /* Set protected mode interrupt vector */
721 TRACE(int31,"set protected mode interrupt handler (0x%02x,%p), stub!\n",
722 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
723 INT_SetPMHandler( BL_reg(context),
724 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
725 DX_reg(context) ));
726 break;
728 case 0x0300: /* Simulate real mode interrupt */
729 INT_DoRealModeInt( context );
730 break;
732 case 0x0301: /* Call real mode procedure with far return */
733 CallRMProcFar( context );
734 break;
736 case 0x0302: /* Call real mode procedure with interrupt return */
738 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
739 FIXME(int31, "RealModeCallIret: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n", p->eax, p->ebx, p->ecx, p->edx);
740 FIXME(int31, " ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x\n", p->esi, p->edi, p->es, p->ds, p->cs, p->ip );
741 SET_CFLAG(context);
743 break;
745 case 0x0303: /* Allocate Real Mode Callback Address */
746 AllocRMCB( context );
747 break;
749 case 0x0304: /* Free Real Mode Callback Address */
750 FreeRMCB( context );
751 break;
753 case 0x0400: /* Get DPMI version */
754 TRACE(int31,"get DPMI version\n");
756 SYSTEM_INFO si;
758 GetSystemInfo(&si);
759 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
760 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
761 CL_reg(context) = si.wProcessorLevel;
762 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
763 break;
765 case 0x0500: /* Get free memory information */
766 TRACE(int31,"get free memory information\n");
768 MEMMANINFO mmi;
770 mmi.dwSize = sizeof(mmi);
771 MemManInfo(&mmi);
772 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
773 /* the layout is just the same as MEMMANINFO, but without
774 * the dwSize entry.
776 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
777 break;
779 case 0x0501: /* Allocate memory block */
780 TRACE(int31,"allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
781 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
783 AX_reg(context) = 0x8012; /* linear memory not available */
784 SET_CFLAG(context);
785 } else {
786 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
787 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
789 break;
791 case 0x0502: /* Free memory block */
792 TRACE(int31, "free memory block (0x%08lx)\n",
793 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
794 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
795 SI_reg(context)), offset) );
796 break;
798 case 0x0503: /* Resize memory block */
799 TRACE(int31, "resize memory block (0x%08lx,%ld)\n",
800 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
801 MAKELONG(CX_reg(context),BX_reg(context)));
802 if (!(ptr = (BYTE *)DPMI_xrealloc(
803 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
804 MAKELONG(CX_reg(context),BX_reg(context)))))
806 AX_reg(context) = 0x8012; /* linear memory not available */
807 SET_CFLAG(context);
808 } else {
809 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
810 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
812 break;
814 case 0x0507: /* Modify page attributes */
815 FIXME(int31,"modify page attributes unimplemented\n");
816 break; /* Just ignore it */
818 case 0x0600: /* Lock linear region */
819 FIXME(int31,"lock linear region unimplemented\n");
820 break; /* Just ignore it */
822 case 0x0601: /* Unlock linear region */
823 FIXME(int31,"unlock linear region unimplemented\n");
824 break; /* Just ignore it */
826 case 0x0602: /* Unlock real-mode region */
827 FIXME(int31,"unlock realmode region unimplemented\n");
828 break; /* Just ignore it */
830 case 0x0603: /* Lock real-mode region */
831 FIXME(int31,"lock realmode region unimplemented\n");
832 break; /* Just ignore it */
834 case 0x0604: /* Get page size */
835 TRACE(int31,"get pagesize\n");
836 BX_reg(context) = 0;
837 CX_reg(context) = VIRTUAL_GetPageSize();
838 break;
840 case 0x0702: /* Mark page as demand-paging candidate */
841 FIXME(int31,"mark page as demand-paging candidate\n");
842 break; /* Just ignore it */
844 case 0x0703: /* Discard page contents */
845 FIXME(int31,"discard page contents\n");
846 break; /* Just ignore it */
848 case 0x0800: /* Physical address mapping */
849 FIXME(int31,"map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
850 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
852 AX_reg(context) = 0x8021;
853 SET_CFLAG(context);
855 else
857 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
858 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
859 RESET_CFLAG(context);
861 break;
863 default:
864 INT_BARF( context, 0x31 );
865 AX_reg(context) = 0x8001; /* unsupported function */
866 SET_CFLAG(context);
867 break;