Fix SM_CYVTHUMB, SM_CXSMSIZE, SM_CXMENUCHECK and SM_CYMENUCHECK.
[wine/multimedia.git] / msdos / dpmi.c
blob27982547074c479518400eee588ad1c69055707a
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <string.h>
29 #include "windef.h"
30 #include "wine/winbase16.h"
31 #include "miscemu.h"
32 #include "msdos.h"
33 #include "task.h"
34 #include "toolhelp.h"
35 #include "selectors.h"
36 #include "callback.h"
37 #include "wine/debug.h"
38 #include "stackframe.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(int31);
42 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
44 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
46 static void* lastvalloced = NULL;
49 DOSVM_TABLE Dosvm = { NULL, };
51 static HMODULE DosModule;
53 /**********************************************************************
54 * DPMI_LoadDosSystem
56 BOOL DPMI_LoadDosSystem(void)
58 if (DosModule) return TRUE;
59 DosModule = LoadLibraryA( "winedos.dll" );
60 if (!DosModule) {
61 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
62 return FALSE;
64 #define GET_ADDR(func) Dosvm.func = (void *)GetProcAddress(DosModule, #func);
66 GET_ADDR(LoadDosExe);
67 GET_ADDR(CallRMInt);
68 GET_ADDR(CallRMProc);
69 GET_ADDR(AllocRMCB);
70 GET_ADDR(FreeRMCB);
71 GET_ADDR(RawModeSwitch);
72 GET_ADDR(SetTimer);
73 GET_ADDR(GetTimer);
74 GET_ADDR(inport);
75 GET_ADDR(outport);
76 GET_ADDR(ASPIHandler);
77 GET_ADDR(EmulateInterruptPM);
78 #undef GET_ADDR
79 return TRUE;
82 /**********************************************************************
83 * DPMI_xalloc
84 * special virtualalloc, allocates lineary monoton growing memory.
85 * (the usual VirtualAlloc does not satisfy that restriction)
87 static LPVOID
88 DPMI_xalloc(int len) {
89 LPVOID ret;
90 LPVOID oldlastv = lastvalloced;
92 if (lastvalloced) {
93 int xflag = 0;
94 ret = NULL;
95 while (!ret) {
96 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
97 if (!ret)
98 lastvalloced = (char *) lastvalloced + 0x10000;
99 /* we failed to allocate one in the first round.
100 * try non-linear
102 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
103 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
104 xflag++;
106 /* if we even fail to allocate something in the next
107 * round, return NULL
109 if ((xflag==1) && (lastvalloced >= oldlastv))
110 xflag++;
111 if ((xflag==2) && (lastvalloced < oldlastv)) {
112 FIXME("failed to allocate any memory of %d bytes!\n",len);
113 return NULL;
116 } else
117 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
118 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
119 return ret;
122 static void
123 DPMI_xfree(LPVOID ptr) {
124 VirtualFree(ptr,0,MEM_RELEASE);
127 /* FIXME: perhaps we could grow this mapped area... */
128 static LPVOID
129 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
130 MEMORY_BASIC_INFORMATION mbi;
131 LPVOID newptr;
133 newptr = DPMI_xalloc(newsize);
134 if (ptr) {
135 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
136 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
137 return NULL;
139 if (mbi.State == MEM_FREE) {
140 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
141 return NULL;
143 /* We do not shrink allocated memory. most reallocs
144 * only do grows anyway
146 if (newsize<=mbi.RegionSize)
147 return ptr;
148 memcpy(newptr,ptr,mbi.RegionSize);
149 DPMI_xfree(ptr);
151 return newptr;
154 /**********************************************************************
155 * CallRMInt
157 static void CallRMInt( CONTEXT86 *context )
159 if (!Dosvm.CallRMInt && !DPMI_LoadDosSystem())
161 ERR("could not setup real-mode calls\n");
162 return;
164 Dosvm.CallRMInt( context );
168 static void CallRMProc( CONTEXT86 *context, int iret )
170 if (!Dosvm.CallRMProc && !DPMI_LoadDosSystem())
172 ERR("could not setup real-mode calls\n");
173 return;
175 Dosvm.CallRMProc( context, iret );
178 static void AllocRMCB( CONTEXT86 *context )
180 if (!Dosvm.AllocRMCB && !DPMI_LoadDosSystem())
182 ERR("could not setup real-mode calls\n");
183 return;
185 Dosvm.AllocRMCB( context );
188 static void FreeRMCB( CONTEXT86 *context )
190 if (!Dosvm.FreeRMCB) /* cannot have allocated one if dosvm not loaded */
192 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
193 SET_CFLAG( context );
195 else Dosvm.FreeRMCB( context );
198 static void RawModeSwitch( CONTEXT86 *context )
200 if (!Dosvm.RawModeSwitch)
202 ERR("could not setup real-mode calls\n");
203 return;
205 else
208 * FIXME: This routine will not work if it is called
209 * from 32 bit DPMI program and the program returns
210 * to protected mode while ESP or EIP is over 0xffff.
211 * FIXME: This routine will not work if it is not called
212 * using 16-bit-to-Wine callback glue function.
214 STACK16FRAME frame = *CURRENT_STACK16;
216 Dosvm.RawModeSwitch( context );
219 * After this function returns to relay code, protected mode
220 * 16 bit stack will contain STACK16FRAME and single WORD
221 * (EFlags, see next comment).
223 NtCurrentTeb()->cur_stack =
224 MAKESEGPTR( context->SegSs,
225 context->Esp - sizeof(STACK16FRAME) - sizeof(WORD) );
228 * After relay code returns to glue function, protected
229 * mode 16 bit stack will contain interrupt return record:
230 * IP, CS and EFlags. Since EFlags is ignored, it won't
231 * need to be initialized.
233 context->Esp -= 3 * sizeof(WORD);
236 * Restore stack frame so that relay code won't be confused.
237 * It should be noted that relay code overwrites IP and CS
238 * in STACK16FRAME with values taken from current CONTEXT86.
239 * These values are what is returned to glue function
240 * (see previous comment).
242 *CURRENT_STACK16 = frame;
246 /**********************************************************************
247 * INT_Int31Handler (WPROCS.149)
249 * Handler for int 31h (DPMI).
252 void WINAPI INT_Int31Handler( CONTEXT86 *context )
255 * Note: For Win32s processes, the whole linear address space is
256 * shifted by 0x10000 relative to the OS linear address space.
257 * See the comment in msdos/vxd.c.
259 DWORD dw;
260 BYTE *ptr;
262 if (context->SegCs == DOSMEM_dpmi_segments.dpmi_sel) {
263 RawModeSwitch( context );
264 return;
267 RESET_CFLAG(context);
268 switch(AX_reg(context))
270 case 0x0000: /* Allocate LDT descriptors */
271 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
272 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
274 TRACE("failed\n");
275 context->Eax = 0x8011; /* descriptor unavailable */
276 SET_CFLAG(context);
278 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
279 break;
281 case 0x0001: /* Free LDT descriptor */
282 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
283 if (FreeSelector16( BX_reg(context) ))
285 context->Eax = 0x8022; /* invalid selector */
286 SET_CFLAG(context);
288 else
290 /* If a segment register contains the selector being freed, */
291 /* set it to zero. */
292 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
293 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
294 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
295 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
297 break;
299 case 0x0002: /* Real mode segment to descriptor */
300 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
302 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
303 switch(BX_reg(context))
305 case 0x0000: entryPoint = 183; break; /* __0000H */
306 case 0x0040: entryPoint = 193; break; /* __0040H */
307 case 0xa000: entryPoint = 174; break; /* __A000H */
308 case 0xb000: entryPoint = 181; break; /* __B000H */
309 case 0xb800: entryPoint = 182; break; /* __B800H */
310 case 0xc000: entryPoint = 195; break; /* __C000H */
311 case 0xd000: entryPoint = 179; break; /* __D000H */
312 case 0xe000: entryPoint = 190; break; /* __E000H */
313 case 0xf000: entryPoint = 194; break; /* __F000H */
314 default:
315 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
316 break;
318 if (entryPoint)
319 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
320 (LPCSTR)(ULONG_PTR)entryPoint ));
322 break;
324 case 0x0003: /* Get next selector increment */
325 TRACE("get selector increment (__AHINCR)\n");
326 context->Eax = __AHINCR;
327 break;
329 case 0x0004: /* Lock selector (not supported) */
330 FIXME("lock selector not supported\n");
331 context->Eax = 0; /* FIXME: is this a correct return value? */
332 break;
334 case 0x0005: /* Unlock selector (not supported) */
335 FIXME("unlock selector not supported\n");
336 context->Eax = 0; /* FIXME: is this a correct return value? */
337 break;
339 case 0x0006: /* Get selector base address */
340 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
341 if (!(dw = GetSelectorBase( BX_reg(context) )))
343 context->Eax = 0x8022; /* invalid selector */
344 SET_CFLAG(context);
346 else
348 SET_CX( context, HIWORD(W32S_WINE2APP(dw)) );
349 SET_DX( context, LOWORD(W32S_WINE2APP(dw)) );
351 break;
353 case 0x0007: /* Set selector base address */
354 TRACE("set selector base address (0x%04x,0x%08lx)\n",
355 BX_reg(context),
356 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
357 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
358 if (dw < 0x10000)
359 /* app wants to access lower 64K of DOS memory, load DOS subsystem */
360 DPMI_LoadDosSystem();
361 SetSelectorBase(BX_reg(context), dw);
362 break;
364 case 0x0008: /* Set selector limit */
365 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
366 dw = MAKELONG( DX_reg(context), CX_reg(context) );
367 SetSelectorLimit16( BX_reg(context), dw );
368 break;
370 case 0x0009: /* Set selector access rights */
371 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
372 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
373 break;
375 case 0x000a: /* Allocate selector alias */
376 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
377 if (!SET_AX( context, AllocCStoDSAlias16( BX_reg(context) )))
379 SET_AX( context, 0x8011 ); /* descriptor unavailable */
380 SET_CFLAG(context);
382 break;
384 case 0x000b: /* Get descriptor */
385 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
387 LDT_ENTRY entry;
388 wine_ldt_get_entry( LOWORD(context->Ebx), &entry);
389 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
390 /* FIXME: should use ES:EDI for 32-bit clients */
391 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
393 break;
395 case 0x000c: /* Set descriptor */
396 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
398 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
399 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
400 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
402 break;
404 case 0x000d: /* Allocate specific LDT descriptor */
405 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
406 SET_AX( context, 0x8011 ); /* descriptor unavailable */
407 SET_CFLAG(context);
408 break;
409 case 0x0100: /* Allocate DOS memory block */
410 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
411 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
412 if (dw) {
413 SET_AX( context, HIWORD(dw) );
414 SET_DX( context, LOWORD(dw) );
415 } else {
416 SET_AX( context, 0x0008 ); /* insufficient memory */
417 SET_BX( context, DOSMEM_Available()>>4 );
418 SET_CFLAG(context);
420 break;
421 case 0x0101: /* Free DOS memory block */
422 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
423 dw = GlobalDOSFree16(DX_reg(context));
424 if (!dw) {
425 SET_AX( context, 0x0009 ); /* memory block address invalid */
426 SET_CFLAG(context);
428 break;
429 case 0x0200: /* get real mode interrupt vector */
430 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
431 BL_reg(context));
432 SET_CFLAG(context);
433 break;
434 case 0x0201: /* set real mode interrupt vector */
435 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
436 SET_CFLAG(context);
437 break;
438 case 0x0204: /* Get protected mode interrupt vector */
439 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
440 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
441 SET_CX( context, HIWORD(dw) );
442 SET_DX( context, LOWORD(dw) );
443 break;
445 case 0x0205: /* Set protected mode interrupt vector */
446 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
447 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
448 INT_SetPMHandler( BL_reg(context),
449 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
450 break;
452 case 0x0300: /* Simulate real mode interrupt */
453 CallRMInt( context );
454 break;
456 case 0x0301: /* Call real mode procedure with far return */
457 CallRMProc( context, FALSE );
458 break;
460 case 0x0302: /* Call real mode procedure with interrupt return */
461 CallRMProc( context, TRUE );
462 break;
464 case 0x0303: /* Allocate Real Mode Callback Address */
465 AllocRMCB( context );
466 break;
468 case 0x0304: /* Free Real Mode Callback Address */
469 FreeRMCB( context );
470 break;
472 case 0x0305: /* Get State Save/Restore Addresses */
473 TRACE("get state save/restore addresses\n");
474 /* we probably won't need this kind of state saving */
475 SET_AX( context, 0 );
476 /* real mode: just point to the lret */
477 SET_BX( context, DOSMEM_dpmi_segments.wrap_seg );
478 context->Ecx = 2;
479 /* protected mode: don't have any handler yet... */
480 FIXME("no protected-mode dummy state save/restore handler yet\n");
481 SET_SI( context, 0 );
482 context->Edi = 0;
483 break;
485 case 0x0306: /* Get Raw Mode Switch Addresses */
486 TRACE("get raw mode switch addresses\n");
487 /* real mode, point to standard DPMI return wrapper */
488 SET_BX( context, DOSMEM_dpmi_segments.wrap_seg );
489 context->Ecx = 0;
490 /* protected mode, point to DPMI call wrapper */
491 SET_SI( context, DOSMEM_dpmi_segments.dpmi_sel );
492 context->Edi = 8; /* offset of the INT 0x31 call */
493 break;
494 case 0x0400: /* Get DPMI version */
495 TRACE("get DPMI version\n");
497 SYSTEM_INFO si;
499 GetSystemInfo(&si);
500 SET_AX( context, 0x005a ); /* DPMI version 0.90 */
501 SET_BX( context, 0x0005 ); /* Flags: 32-bit, virtual memory */
502 SET_CL( context, si.wProcessorLevel );
503 SET_DX( context, 0x0102 ); /* Master/slave interrupt controller base*/
504 break;
506 case 0x0500: /* Get free memory information */
507 TRACE("get free memory information\n");
509 MEMMANINFO mmi;
511 mmi.dwSize = sizeof(mmi);
512 MemManInfo16(&mmi);
513 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
514 /* the layout is just the same as MEMMANINFO, but without
515 * the dwSize entry.
517 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
518 break;
520 case 0x0501: /* Allocate memory block */
521 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
522 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
524 SET_AX( context, 0x8012 ); /* linear memory not available */
525 SET_CFLAG(context);
526 } else {
527 SET_BX( context, HIWORD(W32S_WINE2APP(ptr)) );
528 SET_CX( context, LOWORD(W32S_WINE2APP(ptr)) );
529 SET_SI( context, HIWORD(W32S_WINE2APP(ptr)) );
530 SET_DI( context, LOWORD(W32S_WINE2APP(ptr)) );
532 break;
534 case 0x0502: /* Free memory block */
535 TRACE("free memory block (0x%08lx)\n",
536 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
537 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
538 break;
540 case 0x0503: /* Resize memory block */
541 TRACE("resize memory block (0x%08lx,%ld)\n",
542 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
543 MAKELONG(CX_reg(context),BX_reg(context)));
544 if (!(ptr = (BYTE *)DPMI_xrealloc(
545 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
546 MAKELONG(CX_reg(context),BX_reg(context)))))
548 SET_AX( context, 0x8012 ); /* linear memory not available */
549 SET_CFLAG(context);
550 } else {
551 SET_BX( context, HIWORD(W32S_WINE2APP(ptr)) );
552 SET_CX( context, LOWORD(W32S_WINE2APP(ptr)) );
553 SET_SI( context, HIWORD(W32S_WINE2APP(ptr)) );
554 SET_DI( context, LOWORD(W32S_WINE2APP(ptr)) );
556 break;
558 case 0x0507: /* Modify page attributes */
559 FIXME("modify page attributes unimplemented\n");
560 break; /* Just ignore it */
562 case 0x0600: /* Lock linear region */
563 FIXME("lock linear region unimplemented\n");
564 break; /* Just ignore it */
566 case 0x0601: /* Unlock linear region */
567 FIXME("unlock linear region unimplemented\n");
568 break; /* Just ignore it */
570 case 0x0602: /* Unlock real-mode region */
571 FIXME("unlock realmode region unimplemented\n");
572 break; /* Just ignore it */
574 case 0x0603: /* Lock real-mode region */
575 FIXME("lock realmode region unimplemented\n");
576 break; /* Just ignore it */
578 case 0x0604: /* Get page size */
579 TRACE("get pagesize\n");
580 SET_BX( context, 0 );
581 SET_CX( context, getpagesize() );
582 break;
584 case 0x0702: /* Mark page as demand-paging candidate */
585 FIXME("mark page as demand-paging candidate\n");
586 break; /* Just ignore it */
588 case 0x0703: /* Discard page contents */
589 FIXME("discard page contents\n");
590 break; /* Just ignore it */
592 case 0x0800: /* Physical address mapping */
593 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
594 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
596 SET_AX( context, 0x8021 );
597 SET_CFLAG(context);
599 else
601 SET_BX( context, HIWORD(W32S_WINE2APP(ptr)) );
602 SET_CX( context, LOWORD(W32S_WINE2APP(ptr)) );
603 RESET_CFLAG(context);
605 break;
607 default:
608 INT_BARF( context, 0x31 );
609 SET_AX( context, 0x8001 ); /* unsupported function */
610 SET_CFLAG(context);
611 break;