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
);
77 GET_ADDR(EmulateInterruptPM
);
82 /**********************************************************************
84 * special virtualalloc, allocates lineary monoton growing memory.
85 * (the usual VirtualAlloc does not satisfy that restriction)
88 DPMI_xalloc(int len
) {
90 LPVOID oldlastv
= lastvalloced
;
96 ret
=VirtualAlloc(lastvalloced
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
98 lastvalloced
= (char *) lastvalloced
+ 0x10000;
99 /* we failed to allocate one in the first round.
102 if (!xflag
&& (lastvalloced
<oldlastv
)) { /* wrapped */
103 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len
);
106 /* if we even fail to allocate something in the next
109 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
111 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
112 FIXME("failed to allocate any memory of %d bytes!\n",len
);
117 ret
=VirtualAlloc(NULL
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
118 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
123 DPMI_xfree(LPVOID ptr
) {
124 VirtualFree(ptr
,0,MEM_RELEASE
);
127 /* FIXME: perhaps we could grow this mapped area... */
129 DPMI_xrealloc(LPVOID ptr
,DWORD newsize
) {
130 MEMORY_BASIC_INFORMATION mbi
;
133 newptr
= DPMI_xalloc(newsize
);
135 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
))) {
136 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
139 if (mbi
.State
== MEM_FREE
) {
140 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
143 /* We do not shrink allocated memory. most reallocs
144 * only do grows anyway
146 if (newsize
<=mbi
.RegionSize
)
148 memcpy(newptr
,ptr
,mbi
.RegionSize
);
154 /**********************************************************************
157 static void CallRMInt( CONTEXT86
*context
)
159 if (!Dosvm
.CallRMInt
&& !DPMI_LoadDosSystem())
161 ERR("could not setup real-mode calls\n");
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");
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");
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");
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.
262 if (context
->SegCs
== DOSMEM_dpmi_segments
.dpmi_sel
) {
263 RawModeSwitch( context
);
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
) )))
275 context
->Eax
= 0x8011; /* descriptor unavailable */
278 TRACE("success, array starts at 0x%04x\n",AX_reg(context
));
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 */
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;
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 */
315 context
->Eax
= DOSMEM_AllocSelector(BX_reg(context
));
319 context
->Eax
= LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
320 (LPCSTR
)(ULONG_PTR
)entryPoint
));
324 case 0x0003: /* Get next selector increment */
325 TRACE("get selector increment (__AHINCR)\n");
326 context
->Eax
= __AHINCR
;
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? */
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? */
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 */
348 SET_CX( context
, HIWORD(W32S_WINE2APP(dw
)) );
349 SET_DX( context
, LOWORD(W32S_WINE2APP(dw
)) );
353 case 0x0007: /* Set selector base address */
354 TRACE("set selector base address (0x%04x,0x%08lx)\n",
356 W32S_APP2WINE(MAKELONG(DX_reg(context
),CX_reg(context
))));
357 dw
= W32S_APP2WINE(MAKELONG(DX_reg(context
), CX_reg(context
)));
359 /* app wants to access lower 64K of DOS memory, load DOS subsystem */
360 DPMI_LoadDosSystem();
361 SetSelectorBase(BX_reg(context
), dw
);
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
);
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
) );
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 */
384 case 0x000b: /* Get descriptor */
385 TRACE("get descriptor (0x%04x)\n",BX_reg(context
));
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
;
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
);
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 */
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);
413 SET_AX( context
, HIWORD(dw
) );
414 SET_DX( context
, LOWORD(dw
) );
416 SET_AX( context
, 0x0008 ); /* insufficient memory */
417 SET_BX( context
, DOSMEM_Available()>>4 );
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
));
425 SET_AX( context
, 0x0009 ); /* memory block address invalid */
429 case 0x0200: /* get real mode interrupt vector */
430 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
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
));
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
) );
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
) ));
452 case 0x0300: /* Simulate real mode interrupt */
453 CallRMInt( context
);
456 case 0x0301: /* Call real mode procedure with far return */
457 CallRMProc( context
, FALSE
);
460 case 0x0302: /* Call real mode procedure with interrupt return */
461 CallRMProc( context
, TRUE
);
464 case 0x0303: /* Allocate Real Mode Callback Address */
465 AllocRMCB( context
);
468 case 0x0304: /* Free Real Mode Callback Address */
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
);
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 );
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
);
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 */
494 case 0x0400: /* Get DPMI version */
495 TRACE("get DPMI version\n");
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*/
506 case 0x0500: /* Get free memory information */
507 TRACE("get free memory information\n");
511 mmi
.dwSize
= sizeof(mmi
);
513 ptr
= MapSL(MAKESEGPTR(context
->SegEs
,DI_reg(context
)));
514 /* the layout is just the same as MEMMANINFO, but without
517 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
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 */
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
)) );
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
))) );
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 */
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
)) );
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() );
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 );
601 SET_BX( context
, HIWORD(W32S_WINE2APP(ptr
)) );
602 SET_CX( context
, LOWORD(W32S_WINE2APP(ptr
)) );
603 RESET_CFLAG(context
);
608 INT_BARF( context
, 0x31 );
609 SET_AX( context
, 0x8001 ); /* unsupported function */