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_set_base( &entry
, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry
)) );
388 /* FIXME: should use ES:EDI for 32-bit clients */
389 *(LDT_ENTRY
*)MapSL( MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
) )) = entry
;
393 case 0x000c: /* Set descriptor */
394 TRACE("set descriptor (0x%04x)\n",BX_reg(context
));
396 LDT_ENTRY entry
= *(LDT_ENTRY
*)MapSL(MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
)));
397 wine_ldt_set_base( &entry
, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry
)) );
398 wine_ldt_set_entry( LOWORD(context
->Ebx
), &entry
);
402 case 0x000d: /* Allocate specific LDT descriptor */
403 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context
));
404 SET_AX( context
, 0x8011 ); /* descriptor unavailable */
407 case 0x0100: /* Allocate DOS memory block */
408 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context
));
409 dw
= GlobalDOSAlloc16((DWORD
)BX_reg(context
)<<4);
411 SET_AX( context
, HIWORD(dw
) );
412 SET_DX( context
, LOWORD(dw
) );
414 SET_AX( context
, 0x0008 ); /* insufficient memory */
415 SET_BX( context
, DOSMEM_Available()>>4 );
419 case 0x0101: /* Free DOS memory block */
420 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context
));
421 dw
= GlobalDOSFree16(DX_reg(context
));
423 SET_AX( context
, 0x0009 ); /* memory block address invalid */
427 case 0x0200: /* get real mode interrupt vector */
428 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
432 case 0x0201: /* set real mode interrupt vector */
433 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context
),CX_reg(context
),DX_reg(context
));
436 case 0x0204: /* Get protected mode interrupt vector */
437 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context
));
438 dw
= (DWORD
)INT_GetPMHandler( BL_reg(context
) );
439 SET_CX( context
, HIWORD(dw
) );
440 SET_DX( context
, LOWORD(dw
) );
443 case 0x0205: /* Set protected mode interrupt vector */
444 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
445 BL_reg(context
),MapSL(MAKESEGPTR(CX_reg(context
),DX_reg(context
))));
446 INT_SetPMHandler( BL_reg(context
),
447 (FARPROC16
)MAKESEGPTR( CX_reg(context
), DX_reg(context
) ));
450 case 0x0300: /* Simulate real mode interrupt */
451 CallRMInt( context
);
454 case 0x0301: /* Call real mode procedure with far return */
455 CallRMProc( context
, FALSE
);
458 case 0x0302: /* Call real mode procedure with interrupt return */
459 CallRMProc( context
, TRUE
);
462 case 0x0303: /* Allocate Real Mode Callback Address */
463 AllocRMCB( context
);
466 case 0x0304: /* Free Real Mode Callback Address */
470 case 0x0305: /* Get State Save/Restore Addresses */
471 TRACE("get state save/restore addresses\n");
472 /* we probably won't need this kind of state saving */
473 SET_AX( context
, 0 );
474 /* real mode: just point to the lret */
475 SET_BX( context
, DOSMEM_dpmi_segments
.wrap_seg
);
477 /* protected mode: don't have any handler yet... */
478 FIXME("no protected-mode dummy state save/restore handler yet\n");
479 SET_SI( context
, 0 );
483 case 0x0306: /* Get Raw Mode Switch Addresses */
484 TRACE("get raw mode switch addresses\n");
485 /* real mode, point to standard DPMI return wrapper */
486 SET_BX( context
, DOSMEM_dpmi_segments
.wrap_seg
);
488 /* protected mode, point to DPMI call wrapper */
489 SET_SI( context
, DOSMEM_dpmi_segments
.dpmi_sel
);
490 context
->Edi
= 8; /* offset of the INT 0x31 call */
492 case 0x0400: /* Get DPMI version */
493 TRACE("get DPMI version\n");
498 SET_AX( context
, 0x005a ); /* DPMI version 0.90 */
499 SET_BX( context
, 0x0005 ); /* Flags: 32-bit, virtual memory */
500 SET_CL( context
, si
.wProcessorLevel
);
501 SET_DX( context
, 0x0102 ); /* Master/slave interrupt controller base*/
504 case 0x0500: /* Get free memory information */
505 TRACE("get free memory information\n");
509 mmi
.dwSize
= sizeof(mmi
);
511 ptr
= MapSL(MAKESEGPTR(context
->SegEs
,DI_reg(context
)));
512 /* the layout is just the same as MEMMANINFO, but without
515 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
518 case 0x0501: /* Allocate memory block */
519 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
520 if (!(ptr
= (BYTE
*)DPMI_xalloc(MAKELONG(CX_reg(context
), BX_reg(context
)))))
522 SET_AX( context
, 0x8012 ); /* linear memory not available */
525 SET_BX( context
, HIWORD(W32S_WINE2APP(ptr
)) );
526 SET_CX( context
, LOWORD(W32S_WINE2APP(ptr
)) );
527 SET_SI( context
, HIWORD(W32S_WINE2APP(ptr
)) );
528 SET_DI( context
, LOWORD(W32S_WINE2APP(ptr
)) );
532 case 0x0502: /* Free memory block */
533 TRACE("free memory block (0x%08lx)\n",
534 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))));
535 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
), SI_reg(context
))) );
538 case 0x0503: /* Resize memory block */
539 TRACE("resize memory block (0x%08lx,%ld)\n",
540 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
541 MAKELONG(CX_reg(context
),BX_reg(context
)));
542 if (!(ptr
= (BYTE
*)DPMI_xrealloc(
543 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
544 MAKELONG(CX_reg(context
),BX_reg(context
)))))
546 SET_AX( context
, 0x8012 ); /* linear memory not available */
549 SET_BX( context
, HIWORD(W32S_WINE2APP(ptr
)) );
550 SET_CX( context
, LOWORD(W32S_WINE2APP(ptr
)) );
551 SET_SI( context
, HIWORD(W32S_WINE2APP(ptr
)) );
552 SET_DI( context
, LOWORD(W32S_WINE2APP(ptr
)) );
556 case 0x0507: /* Modify page attributes */
557 FIXME("modify page attributes unimplemented\n");
558 break; /* Just ignore it */
560 case 0x0600: /* Lock linear region */
561 FIXME("lock linear region unimplemented\n");
562 break; /* Just ignore it */
564 case 0x0601: /* Unlock linear region */
565 FIXME("unlock linear region unimplemented\n");
566 break; /* Just ignore it */
568 case 0x0602: /* Unlock real-mode region */
569 FIXME("unlock realmode region unimplemented\n");
570 break; /* Just ignore it */
572 case 0x0603: /* Lock real-mode region */
573 FIXME("lock realmode region unimplemented\n");
574 break; /* Just ignore it */
576 case 0x0604: /* Get page size */
577 TRACE("get pagesize\n");
578 SET_BX( context
, 0 );
579 SET_CX( context
, getpagesize() );
582 case 0x0702: /* Mark page as demand-paging candidate */
583 FIXME("mark page as demand-paging candidate\n");
584 break; /* Just ignore it */
586 case 0x0703: /* Discard page contents */
587 FIXME("discard page contents\n");
588 break; /* Just ignore it */
590 case 0x0800: /* Physical address mapping */
591 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
592 if(!(ptr
=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context
),BX_reg(context
)))))
594 SET_AX( context
, 0x8021 );
599 SET_BX( context
, HIWORD(W32S_WINE2APP(ptr
)) );
600 SET_CX( context
, LOWORD(W32S_WINE2APP(ptr
)) );
601 RESET_CFLAG(context
);
606 INT_BARF( context
, 0x31 );
607 SET_AX( context
, 0x8001 ); /* unsupported function */