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"
28 #include "wine/winbase16.h"
33 #include "selectors.h"
35 #include "wine/debug.h"
36 #include "stackframe.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(int31
);
40 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
42 void CreateBPB(int drive
, BYTE
*data
, BOOL16 limited
); /* defined in int21.c */
44 static void* lastvalloced
= NULL
;
47 DOSVM_TABLE Dosvm
= { NULL
, };
49 static HMODULE DosModule
;
51 /**********************************************************************
54 BOOL
DPMI_LoadDosSystem(void)
56 if (DosModule
) return TRUE
;
57 DosModule
= LoadLibraryA( "winedos.dll" );
59 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
62 #define GET_ADDR(func) Dosvm.func = (void *)GetProcAddress(DosModule, #func);
69 GET_ADDR(RawModeSwitch
);
74 GET_ADDR(ASPIHandler
);
79 /**********************************************************************
81 * special virtualalloc, allocates lineary monoton growing memory.
82 * (the usual VirtualAlloc does not satisfy that restriction)
85 DPMI_xalloc(int len
) {
87 LPVOID oldlastv
= lastvalloced
;
93 ret
=VirtualAlloc(lastvalloced
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
95 lastvalloced
= (char *) lastvalloced
+ 0x10000;
96 /* we failed to allocate one in the first round.
99 if (!xflag
&& (lastvalloced
<oldlastv
)) { /* wrapped */
100 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len
);
103 /* if we even fail to allocate something in the next
106 if ((xflag
==1) && (lastvalloced
>= oldlastv
))
108 if ((xflag
==2) && (lastvalloced
< oldlastv
)) {
109 FIXME("failed to allocate any memory of %d bytes!\n",len
);
114 ret
=VirtualAlloc(NULL
,len
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
115 lastvalloced
= (LPVOID
)(((DWORD
)ret
+len
+0xffff)&~0xffff);
120 DPMI_xfree(LPVOID ptr
) {
121 VirtualFree(ptr
,0,MEM_RELEASE
);
124 /* FIXME: perhaps we could grow this mapped area... */
126 DPMI_xrealloc(LPVOID ptr
,DWORD newsize
) {
127 MEMORY_BASIC_INFORMATION mbi
;
130 newptr
= DPMI_xalloc(newsize
);
132 if (!VirtualQuery(ptr
,&mbi
,sizeof(mbi
))) {
133 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
136 if (mbi
.State
== MEM_FREE
) {
137 FIXME("realloc of DPMI_xallocd region %p?\n",ptr
);
140 /* We do not shrink allocated memory. most reallocs
141 * only do grows anyway
143 if (newsize
<=mbi
.RegionSize
)
145 memcpy(newptr
,ptr
,mbi
.RegionSize
);
151 /**********************************************************************
154 static void CallRMInt( CONTEXT86
*context
)
156 if (!Dosvm
.CallRMInt
&& !DPMI_LoadDosSystem())
158 ERR("could not setup real-mode calls\n");
161 Dosvm
.CallRMInt( context
);
165 static void CallRMProc( CONTEXT86
*context
, int iret
)
167 if (!Dosvm
.CallRMProc
&& !DPMI_LoadDosSystem())
169 ERR("could not setup real-mode calls\n");
172 Dosvm
.CallRMProc( context
, iret
);
175 static void AllocRMCB( CONTEXT86
*context
)
177 if (!Dosvm
.AllocRMCB
&& !DPMI_LoadDosSystem())
179 ERR("could not setup real-mode calls\n");
182 Dosvm
.AllocRMCB( context
);
185 static void FreeRMCB( CONTEXT86
*context
)
187 if (!Dosvm
.FreeRMCB
) /* cannot have allocated one if dosvm not loaded */
189 SET_LOWORD( context
->Eax
, 0x8024 ); /* invalid callback address */
190 SET_CFLAG( context
);
192 else Dosvm
.FreeRMCB( context
);
195 static void RawModeSwitch( CONTEXT86
*context
)
197 if (!Dosvm
.RawModeSwitch
)
199 ERR("could not setup real-mode calls\n");
205 * FIXME: This routine will not work if it is called
206 * from 32 bit DPMI program and the program returns
207 * to protected mode while ESP or EIP is over 0xffff.
208 * FIXME: This routine will not work if it is not called
209 * using 16-bit-to-Wine callback glue function.
211 STACK16FRAME frame
= *CURRENT_STACK16
;
213 Dosvm
.RawModeSwitch( context
);
216 * After this function returns to relay code, protected mode
217 * 16 bit stack will contain STACK16FRAME and single WORD
218 * (EFlags, see next comment).
220 NtCurrentTeb()->cur_stack
=
221 MAKESEGPTR( context
->SegSs
,
222 context
->Esp
- sizeof(STACK16FRAME
) - sizeof(WORD
) );
225 * After relay code returns to glue function, protected
226 * mode 16 bit stack will contain interrupt return record:
227 * IP, CS and EFlags. Since EFlags is ignored, it won't
228 * need to be initialized.
230 context
->Esp
-= 3 * sizeof(WORD
);
233 * Restore stack frame so that relay code won't be confused.
234 * It should be noted that relay code overwrites IP and CS
235 * in STACK16FRAME with values taken from current CONTEXT86.
236 * These values are what is returned to glue function
237 * (see previous comment).
239 *CURRENT_STACK16
= frame
;
243 /**********************************************************************
244 * INT_Int31Handler (WPROCS.149)
246 * Handler for int 31h (DPMI).
249 void WINAPI
INT_Int31Handler( CONTEXT86
*context
)
252 * Note: For Win32s processes, the whole linear address space is
253 * shifted by 0x10000 relative to the OS linear address space.
254 * See the comment in msdos/vxd.c.
259 if (context
->SegCs
== DOSMEM_dpmi_sel
) {
260 RawModeSwitch( context
);
264 RESET_CFLAG(context
);
265 switch(AX_reg(context
))
267 case 0x0000: /* Allocate LDT descriptors */
268 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context
));
269 if (!(context
->Eax
= AllocSelectorArray16( CX_reg(context
) )))
272 context
->Eax
= 0x8011; /* descriptor unavailable */
275 TRACE("success, array starts at 0x%04x\n",AX_reg(context
));
278 case 0x0001: /* Free LDT descriptor */
279 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context
));
280 if (FreeSelector16( BX_reg(context
) ))
282 context
->Eax
= 0x8022; /* invalid selector */
287 /* If a segment register contains the selector being freed, */
288 /* set it to zero. */
289 if (!((context
->SegDs
^BX_reg(context
)) & ~3)) context
->SegDs
= 0;
290 if (!((context
->SegEs
^BX_reg(context
)) & ~3)) context
->SegEs
= 0;
291 if (!((context
->SegFs
^BX_reg(context
)) & ~3)) context
->SegFs
= 0;
292 if (!((context
->SegGs
^BX_reg(context
)) & ~3)) context
->SegGs
= 0;
296 case 0x0002: /* Real mode segment to descriptor */
297 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context
));
299 WORD entryPoint
= 0; /* KERNEL entry point for descriptor */
300 switch(BX_reg(context
))
302 case 0x0000: entryPoint
= 183; break; /* __0000H */
303 case 0x0040: entryPoint
= 193; break; /* __0040H */
304 case 0xa000: entryPoint
= 174; break; /* __A000H */
305 case 0xb000: entryPoint
= 181; break; /* __B000H */
306 case 0xb800: entryPoint
= 182; break; /* __B800H */
307 case 0xc000: entryPoint
= 195; break; /* __C000H */
308 case 0xd000: entryPoint
= 179; break; /* __D000H */
309 case 0xe000: entryPoint
= 190; break; /* __E000H */
310 case 0xf000: entryPoint
= 194; break; /* __F000H */
312 context
->Eax
= DOSMEM_AllocSelector(BX_reg(context
));
316 context
->Eax
= LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
317 (LPCSTR
)(ULONG_PTR
)entryPoint
));
321 case 0x0003: /* Get next selector increment */
322 TRACE("get selector increment (__AHINCR)\n");
323 context
->Eax
= __AHINCR
;
326 case 0x0004: /* Lock selector (not supported) */
327 FIXME("lock selector not supported\n");
328 context
->Eax
= 0; /* FIXME: is this a correct return value? */
331 case 0x0005: /* Unlock selector (not supported) */
332 FIXME("unlock selector not supported\n");
333 context
->Eax
= 0; /* FIXME: is this a correct return value? */
336 case 0x0006: /* Get selector base address */
337 TRACE("get selector base address (0x%04x)\n",BX_reg(context
));
338 if (!(dw
= GetSelectorBase( BX_reg(context
) )))
340 context
->Eax
= 0x8022; /* invalid selector */
345 CX_reg(context
) = HIWORD(W32S_WINE2APP(dw
));
346 DX_reg(context
) = LOWORD(W32S_WINE2APP(dw
));
350 case 0x0007: /* Set selector base address */
351 TRACE("set selector base address (0x%04x,0x%08lx)\n",
353 W32S_APP2WINE(MAKELONG(DX_reg(context
),CX_reg(context
))));
354 dw
= W32S_APP2WINE(MAKELONG(DX_reg(context
), CX_reg(context
)));
356 /* app wants to access lower 64K of DOS memory, load DOS subsystem */
357 DPMI_LoadDosSystem();
358 SetSelectorBase(BX_reg(context
), dw
);
361 case 0x0008: /* Set selector limit */
362 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context
),MAKELONG(DX_reg(context
),CX_reg(context
)));
363 dw
= MAKELONG( DX_reg(context
), CX_reg(context
) );
364 SetSelectorLimit16( BX_reg(context
), dw
);
367 case 0x0009: /* Set selector access rights */
368 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context
),CX_reg(context
));
369 SelectorAccessRights16( BX_reg(context
), 1, CX_reg(context
) );
372 case 0x000a: /* Allocate selector alias */
373 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context
));
374 if (!(AX_reg(context
) = AllocCStoDSAlias16( BX_reg(context
) )))
376 AX_reg(context
) = 0x8011; /* descriptor unavailable */
381 case 0x000b: /* Get descriptor */
382 TRACE("get descriptor (0x%04x)\n",BX_reg(context
));
385 wine_ldt_set_base( &entry
, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry
)) );
386 /* FIXME: should use ES:EDI for 32-bit clients */
387 *(LDT_ENTRY
*)MapSL( MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
) )) = entry
;
391 case 0x000c: /* Set descriptor */
392 TRACE("set descriptor (0x%04x)\n",BX_reg(context
));
394 LDT_ENTRY entry
= *(LDT_ENTRY
*)MapSL(MAKESEGPTR( context
->SegEs
, LOWORD(context
->Edi
)));
395 wine_ldt_set_base( &entry
, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry
)) );
396 wine_ldt_set_entry( LOWORD(context
->Ebx
), &entry
);
400 case 0x000d: /* Allocate specific LDT descriptor */
401 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context
));
402 AX_reg(context
) = 0x8011; /* descriptor unavailable */
405 case 0x0100: /* Allocate DOS memory block */
406 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context
));
407 dw
= GlobalDOSAlloc16((DWORD
)BX_reg(context
)<<4);
409 AX_reg(context
) = HIWORD(dw
);
410 DX_reg(context
) = LOWORD(dw
);
412 AX_reg(context
) = 0x0008; /* insufficient memory */
413 BX_reg(context
) = DOSMEM_Available()>>4;
417 case 0x0101: /* Free DOS memory block */
418 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context
));
419 dw
= GlobalDOSFree16(DX_reg(context
));
421 AX_reg(context
) = 0x0009; /* memory block address invalid */
425 case 0x0200: /* get real mode interrupt vector */
426 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
430 case 0x0201: /* set real mode interrupt vector */
431 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context
),CX_reg(context
),DX_reg(context
));
434 case 0x0204: /* Get protected mode interrupt vector */
435 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context
));
436 dw
= (DWORD
)INT_GetPMHandler( BL_reg(context
) );
437 CX_reg(context
) = HIWORD(dw
);
438 DX_reg(context
) = LOWORD(dw
);
441 case 0x0205: /* Set protected mode interrupt vector */
442 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
443 BL_reg(context
),MapSL(MAKESEGPTR(CX_reg(context
),DX_reg(context
))));
444 INT_SetPMHandler( BL_reg(context
),
445 (FARPROC16
)MAKESEGPTR( CX_reg(context
), DX_reg(context
) ));
448 case 0x0300: /* Simulate real mode interrupt */
449 CallRMInt( context
);
452 case 0x0301: /* Call real mode procedure with far return */
453 CallRMProc( context
, FALSE
);
456 case 0x0302: /* Call real mode procedure with interrupt return */
457 CallRMProc( context
, TRUE
);
460 case 0x0303: /* Allocate Real Mode Callback Address */
461 AllocRMCB( context
);
464 case 0x0304: /* Free Real Mode Callback Address */
468 case 0x0305: /* Get State Save/Restore Addresses */
469 TRACE("get state save/restore addresses\n");
470 /* we probably won't need this kind of state saving */
472 /* real mode: just point to the lret */
473 BX_reg(context
) = DOSMEM_wrap_seg
;
475 /* protected mode: don't have any handler yet... */
476 FIXME("no protected-mode dummy state save/restore handler yet\n");
481 case 0x0306: /* Get Raw Mode Switch Addresses */
482 TRACE("get raw mode switch addresses\n");
483 /* real mode, point to standard DPMI return wrapper */
484 BX_reg(context
) = DOSMEM_wrap_seg
;
486 /* protected mode, point to DPMI call wrapper */
487 SI_reg(context
) = DOSMEM_dpmi_sel
;
488 context
->Edi
= 8; /* offset of the INT 0x31 call */
490 case 0x0400: /* Get DPMI version */
491 TRACE("get DPMI version\n");
496 AX_reg(context
) = 0x005a; /* DPMI version 0.90 */
497 BX_reg(context
) = 0x0005; /* Flags: 32-bit, virtual memory */
498 CL_reg(context
) = si
.wProcessorLevel
;
499 DX_reg(context
) = 0x0102; /* Master/slave interrupt controller base*/
502 case 0x0500: /* Get free memory information */
503 TRACE("get free memory information\n");
507 mmi
.dwSize
= sizeof(mmi
);
509 ptr
= MapSL(MAKESEGPTR(context
->SegEs
,DI_reg(context
)));
510 /* the layout is just the same as MEMMANINFO, but without
513 memcpy(ptr
,((char*)&mmi
)+4,sizeof(mmi
)-4);
516 case 0x0501: /* Allocate memory block */
517 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
518 if (!(ptr
= (BYTE
*)DPMI_xalloc(MAKELONG(CX_reg(context
), BX_reg(context
)))))
520 AX_reg(context
) = 0x8012; /* linear memory not available */
523 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
524 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
528 case 0x0502: /* Free memory block */
529 TRACE("free memory block (0x%08lx)\n",
530 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))));
531 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
), SI_reg(context
))) );
534 case 0x0503: /* Resize memory block */
535 TRACE("resize memory block (0x%08lx,%ld)\n",
536 W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
537 MAKELONG(CX_reg(context
),BX_reg(context
)));
538 if (!(ptr
= (BYTE
*)DPMI_xrealloc(
539 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context
),SI_reg(context
))),
540 MAKELONG(CX_reg(context
),BX_reg(context
)))))
542 AX_reg(context
) = 0x8012; /* linear memory not available */
545 BX_reg(context
) = SI_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
546 CX_reg(context
) = DI_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
550 case 0x0507: /* Modify page attributes */
551 FIXME("modify page attributes unimplemented\n");
552 break; /* Just ignore it */
554 case 0x0600: /* Lock linear region */
555 FIXME("lock linear region unimplemented\n");
556 break; /* Just ignore it */
558 case 0x0601: /* Unlock linear region */
559 FIXME("unlock linear region unimplemented\n");
560 break; /* Just ignore it */
562 case 0x0602: /* Unlock real-mode region */
563 FIXME("unlock realmode region unimplemented\n");
564 break; /* Just ignore it */
566 case 0x0603: /* Lock real-mode region */
567 FIXME("lock realmode region unimplemented\n");
568 break; /* Just ignore it */
570 case 0x0604: /* Get page size */
571 TRACE("get pagesize\n");
573 CX_reg(context
) = getpagesize();
576 case 0x0702: /* Mark page as demand-paging candidate */
577 FIXME("mark page as demand-paging candidate\n");
578 break; /* Just ignore it */
580 case 0x0703: /* Discard page contents */
581 FIXME("discard page contents\n");
582 break; /* Just ignore it */
584 case 0x0800: /* Physical address mapping */
585 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context
),BX_reg(context
)));
586 if(!(ptr
=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context
),BX_reg(context
)))))
588 AX_reg(context
) = 0x8021;
593 BX_reg(context
) = HIWORD(W32S_WINE2APP(ptr
));
594 CX_reg(context
) = LOWORD(W32S_WINE2APP(ptr
));
595 RESET_CFLAG(context
);
600 INT_BARF( context
, 0x31 );
601 AX_reg(context
) = 0x8001; /* unsupported function */