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
22 #include "wine/port.h"
30 #include "wine/winbase16.h"
35 #include "selectors.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 /**********************************************************************
56 BOOL DPMI_LoadDosSystem(void)
58 if (DosModule) return TRUE;
59 DosModule = LoadLibraryA( "winedos.dll" );
61 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
64 #define GET_ADDR(func) Dosvm.func = (void *)GetProcAddress(DosModule, #func);
71 GET_ADDR(RawModeSwitch);
76 GET_ADDR(ASPIHandler);
81 /**********************************************************************
83 * special virtualalloc, allocates lineary monoton growing memory.
84 * (the usual VirtualAlloc does not satisfy that restriction)
87 DPMI_xalloc(int len) {
89 LPVOID oldlastv = lastvalloced;
95 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
97 lastvalloced = (char *) lastvalloced + 0x10000;
98 /* we failed to allocate one in the first round.
101 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
102 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
105 /* if we even fail to allocate something in the next
108 if ((xflag==1) && (lastvalloced >= oldlastv))
110 if ((xflag==2) && (lastvalloced < oldlastv)) {
111 FIXME("failed to allocate any memory of %d bytes!\n",len);
116 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
117 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
122 DPMI_xfree(LPVOID ptr) {
123 VirtualFree(ptr,0,MEM_RELEASE);
126 /* FIXME: perhaps we could grow this mapped area... */
128 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
129 MEMORY_BASIC_INFORMATION mbi;
132 newptr = DPMI_xalloc(newsize);
134 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
135 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
138 if (mbi.State == MEM_FREE) {
139 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
142 /* We do not shrink allocated memory. most reallocs
143 * only do grows anyway
145 if (newsize<=mbi.RegionSize)
147 memcpy(newptr,ptr,mbi.RegionSize);
153 /**********************************************************************
156 static void CallRMInt( CONTEXT86 *context )
158 if (!Dosvm.CallRMInt && !DPMI_LoadDosSystem())
160 ERR("could not setup real-mode calls\n");
163 Dosvm.CallRMInt( context );
167 static void CallRMProc( CONTEXT86 *context, int iret )
169 if (!Dosvm.CallRMProc && !DPMI_LoadDosSystem())
171 ERR("could not setup real-mode calls\n");
174 Dosvm.CallRMProc( context, iret );
177 static void AllocRMCB( CONTEXT86 *context )
179 if (!Dosvm.AllocRMCB && !DPMI_LoadDosSystem())
181 ERR("could not setup real-mode calls\n");
184 Dosvm.AllocRMCB( context );
187 static void FreeRMCB( CONTEXT86 *context )
189 if (!Dosvm.FreeRMCB) /* cannot have allocated one if dosvm not loaded */
191 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
192 SET_CFLAG( context );
194 else Dosvm.FreeRMCB( context );
197 static void RawModeSwitch( CONTEXT86 *context )
199 if (!Dosvm.RawModeSwitch)
201 ERR("could not setup real-mode calls\n");
207 * FIXME: This routine will not work if it is called
208 * from 32 bit DPMI program and the program returns
209 * to protected mode while ESP or EIP is over 0xffff.
210 * FIXME: This routine will not work if it is not called
211 * using 16-bit-to-Wine callback glue function.
213 STACK16FRAME frame = *CURRENT_STACK16;
215 Dosvm.RawModeSwitch( context );
218 * After this function returns to relay code, protected mode
219 * 16 bit stack will contain STACK16FRAME and single WORD
220 * (EFlags, see next comment).
222 NtCurrentTeb()->cur_stack =
223 MAKESEGPTR( context->SegSs,
224 context->Esp - sizeof(STACK16FRAME) - sizeof(WORD) );
227 * After relay code returns to glue function, protected
228 * mode 16 bit stack will contain interrupt return record:
229 * IP, CS and EFlags. Since EFlags is ignored, it won't
230 * need to be initialized.
232 context->Esp -= 3 * sizeof(WORD);
235 * Restore stack frame so that relay code won't be confused.
236 * It should be noted that relay code overwrites IP and CS
237 * in STACK16FRAME with values taken from current CONTEXT86.
238 * These values are what is returned to glue function
239 * (see previous comment).
241 *CURRENT_STACK16 = frame;
245 /**********************************************************************
246 * INT_Int31Handler (WPROCS.149)
248 * Handler for int 31h (DPMI).
251 void WINAPI INT_Int31Handler( CONTEXT86 *context )
254 * Note: For Win32s processes, the whole linear address space is
255 * shifted by 0x10000 relative to the OS linear address space.
256 * See the comment in msdos/vxd.c.
261 if (context->SegCs == DOSMEM_dpmi_segments.dpmi_sel) {
262 RawModeSwitch( context );
266 RESET_CFLAG(context);
267 switch(AX_reg(context))
269 case 0x0000: /* Allocate LDT descriptors */
270 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
271 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
274 context->Eax = 0x8011; /* descriptor unavailable */
277 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
280 case 0x0001: /* Free LDT descriptor */
281 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
282 if (FreeSelector16( BX_reg(context) ))
284 context->Eax = 0x8022; /* invalid selector */
289 /* If a segment register contains the selector being freed, */
290 /* set it to zero. */
291 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
292 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
293 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
294 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
298 case 0x0002: /* Real mode segment to descriptor */
299 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
301 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
302 switch(BX_reg(context))
304 case 0x0000: entryPoint = 183; break; /* __0000H */
305 case 0x0040: entryPoint = 193; break; /* __0040H */
306 case 0xa000: entryPoint = 174; break; /* __A000H */
307 case 0xb000: entryPoint = 181; break; /* __B000H */
308 case 0xb800: entryPoint = 182; break; /* __B800H */
309 case 0xc000: entryPoint = 195; break; /* __C000H */
310 case 0xd000: entryPoint = 179; break; /* __D000H */
311 case 0xe000: entryPoint = 190; break; /* __E000H */
312 case 0xf000: entryPoint = 194; break; /* __F000H */
314 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
318 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
319 (LPCSTR)(ULONG_PTR)entryPoint ));
323 case 0x0003: /* Get next selector increment */
324 TRACE("get selector increment (__AHINCR)\n");
325 context->Eax = __AHINCR;
328 case 0x0004: /* Lock selector (not supported) */
329 FIXME("lock selector not supported\n");
330 context->Eax = 0; /* FIXME: is this a correct return value? */
333 case 0x0005: /* Unlock selector (not supported) */
334 FIXME("unlock selector not supported\n");
335 context->Eax = 0; /* FIXME: is this a correct return value? */
338 case 0x0006: /* Get selector base address */
339 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
340 if (!(dw = GetSelectorBase( BX_reg(context) )))
342 context->Eax = 0x8022; /* invalid selector */
347 SET_CX( context, HIWORD(W32S_WINE2APP(dw)) );
348 SET_DX( context, LOWORD(W32S_WINE2APP(dw)) );
352 case 0x0007: /* Set selector base address */
353 TRACE("set selector base address (0x%04x,0x%08lx)\n",
355 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
356 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
358 /* app wants to access lower 64K of DOS memory, load DOS subsystem */
359 DPMI_LoadDosSystem();
360 SetSelectorBase(BX_reg(context), dw);
363 case 0x0008: /* Set selector limit */
364 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
365 dw = MAKELONG( DX_reg(context), CX_reg(context) );
366 SetSelectorLimit16( BX_reg(context), dw );
369 case 0x0009: /* Set selector access rights */
370 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
371 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
374 case 0x000a: /* Allocate selector alias */
375 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
376 if (!SET_AX( context, AllocCStoDSAlias16( BX_reg(context) )))
378 SET_AX( context, 0x8011 ); /* descriptor unavailable */
383 case 0x000b: /* Get descriptor */
384 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
387 wine_ldt_get_entry( LOWORD(context->Ebx), &entry);
388 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
389 /* FIXME: should use ES:EDI for 32-bit clients */
390 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
394 case 0x000c: /* Set descriptor */
395 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
397 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
398 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
399 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
403 case 0x000d: /* Allocate specific LDT descriptor */
404 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
405 SET_AX( context, 0x8011 ); /* descriptor unavailable */
408 case 0x0100: /* Allocate DOS memory block */
409 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
410 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
412 SET_AX( context, HIWORD(dw) );
413 SET_DX( context, LOWORD(dw) );
415 SET_AX( context, 0x0008 ); /* insufficient memory */
416 SET_BX( context, DOSMEM_Available()>>4 );
420 case 0x0101: /* Free DOS memory block */
421 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
422 dw = GlobalDOSFree16(DX_reg(context));
424 SET_AX( context, 0x0009 ); /* memory block address invalid */
428 case 0x0200: /* get real mode interrupt vector */
429 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
433 case 0x0201: /* set real mode interrupt vector */
434 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
437 case 0x0204: /* Get protected mode interrupt vector */
438 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
439 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
440 SET_CX( context, HIWORD(dw) );
441 SET_DX( context, LOWORD(dw) );
444 case 0x0205: /* Set protected mode interrupt vector */
445 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
446 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
447 INT_SetPMHandler( BL_reg(context),
448 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
451 case 0x0300: /* Simulate real mode interrupt */
452 CallRMInt( context );
455 case 0x0301: /* Call real mode procedure with far return */
456 CallRMProc( context, FALSE );
459 case 0x0302: /* Call real mode procedure with interrupt return */
460 CallRMProc( context, TRUE );
463 case 0x0303: /* Allocate Real Mode Callback Address */
464 AllocRMCB( context );
467 case 0x0304: /* Free Real Mode Callback Address */
471 case 0x0305: /* Get State Save/Restore Addresses */
472 TRACE("get state save/restore addresses\n");
473 /* we probably won't need this kind of state saving */
474 SET_AX( context, 0 );
475 /* real mode: just point to the lret */
476 SET_BX( context, DOSMEM_dpmi_segments.wrap_seg );
478 /* protected mode: don't have any handler yet... */
479 FIXME("no protected-mode dummy state save/restore handler yet\n");
480 SET_SI( context, 0 );
484 case 0x0306: /* Get Raw Mode Switch Addresses */
485 TRACE("get raw mode switch addresses\n");
486 /* real mode, point to standard DPMI return wrapper */
487 SET_BX( context, DOSMEM_dpmi_segments.wrap_seg );
489 /* protected mode, point to DPMI call wrapper */
490 SET_SI( context, DOSMEM_dpmi_segments.dpmi_sel );
491 context->Edi = 8; /* offset of the INT 0x31 call */
493 case 0x0400: /* Get DPMI version */
494 TRACE("get DPMI version\n");
499 SET_AX( context, 0x005a ); /* DPMI version 0.90 */
500 SET_BX( context, 0x0005 ); /* Flags: 32-bit, virtual memory */
501 SET_CL( context, si.wProcessorLevel );
502 SET_DX( context, 0x0102 ); /* Master/slave interrupt controller base*/
505 case 0x0500: /* Get free memory information */
506 TRACE("get free memory information\n");
510 mmi.dwSize = sizeof(mmi);
512 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
513 /* the layout is just the same as MEMMANINFO, but without
516 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
519 case 0x0501: /* Allocate memory block */
520 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
521 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
523 SET_AX( context, 0x8012 ); /* linear memory not available */
526 SET_BX( context, HIWORD(W32S_WINE2APP(ptr)) );
527 SET_CX( context, LOWORD(W32S_WINE2APP(ptr)) );
528 SET_SI( context, HIWORD(W32S_WINE2APP(ptr)) );
529 SET_DI( context, LOWORD(W32S_WINE2APP(ptr)) );
533 case 0x0502: /* Free memory block */
534 TRACE("free memory block (0x%08lx)\n",
535 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
536 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
539 case 0x0503: /* Resize memory block */
540 TRACE("resize memory block (0x%08lx,%ld)\n",
541 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
542 MAKELONG(CX_reg(context),BX_reg(context)));
543 if (!(ptr = (BYTE *)DPMI_xrealloc(
544 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
545 MAKELONG(CX_reg(context),BX_reg(context)))))
547 SET_AX( context, 0x8012 ); /* linear memory not available */
550 SET_BX( context, HIWORD(W32S_WINE2APP(ptr)) );
551 SET_CX( context, LOWORD(W32S_WINE2APP(ptr)) );
552 SET_SI( context, HIWORD(W32S_WINE2APP(ptr)) );
553 SET_DI( context, LOWORD(W32S_WINE2APP(ptr)) );
557 case 0x0507: /* Modify page attributes */
558 FIXME("modify page attributes unimplemented\n");
559 break; /* Just ignore it */
561 case 0x0600: /* Lock linear region */
562 FIXME("lock linear region unimplemented\n");
563 break; /* Just ignore it */
565 case 0x0601: /* Unlock linear region */
566 FIXME("unlock linear region unimplemented\n");
567 break; /* Just ignore it */
569 case 0x0602: /* Unlock real-mode region */
570 FIXME("unlock realmode region unimplemented\n");
571 break; /* Just ignore it */
573 case 0x0603: /* Lock real-mode region */
574 FIXME("lock realmode region unimplemented\n");
575 break; /* Just ignore it */
577 case 0x0604: /* Get page size */
578 TRACE("get pagesize\n");
579 SET_BX( context, 0 );
580 SET_CX( context, getpagesize() );
583 case 0x0702: /* Mark page as demand-paging candidate */
584 FIXME("mark page as demand-paging candidate\n");
585 break; /* Just ignore it */
587 case 0x0703: /* Discard page contents */
588 FIXME("discard page contents\n");
589 break; /* Just ignore it */
591 case 0x0800: /* Physical address mapping */
592 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
593 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
595 SET_AX( context, 0x8021 );
600 SET_BX( context, HIWORD(W32S_WINE2APP(ptr)) );
601 SET_CX( context, LOWORD(W32S_WINE2APP(ptr)) );
602 RESET_CFLAG(context);
607 INT_BARF( context, 0x31 );
608 SET_AX( context, 0x8001 ); /* unsupported function */