Don't fill colormap beyond end of screen depth.
[wine.git] / msdos / dpmi.c
blob13d18b9e79f6faf18babaa8959e22b2e1d55b486
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
8 #include "wine/port.h"
10 #include <unistd.h>
11 #include <string.h>
13 #include "windef.h"
14 #include "wine/winbase16.h"
15 #include "miscemu.h"
16 #include "msdos.h"
17 #include "task.h"
18 #include "toolhelp.h"
19 #include "selectors.h"
20 #include "callback.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(int31);
25 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
27 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
29 static void* lastvalloced = NULL;
32 DOSVM_TABLE Dosvm = { NULL, };
34 static HMODULE DosModule;
36 /**********************************************************************
37 * DPMI_LoadDosSystem
39 BOOL DPMI_LoadDosSystem(void)
41 if (DosModule) return TRUE;
42 DosModule = LoadLibraryA( "winedos.dll" );
43 if (!DosModule) {
44 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
45 return FALSE;
47 #define GET_ADDR(func) Dosvm.func = (void *)GetProcAddress(DosModule, #func);
49 GET_ADDR(LoadDosExe);
50 GET_ADDR(CallRMInt);
51 GET_ADDR(CallRMProc);
52 GET_ADDR(AllocRMCB);
53 GET_ADDR(FreeRMCB);
54 GET_ADDR(SetTimer);
55 GET_ADDR(GetTimer);
56 GET_ADDR(inport);
57 GET_ADDR(outport);
58 GET_ADDR(ASPIHandler);
59 #undef GET_ADDR
60 return TRUE;
63 /**********************************************************************
64 * DPMI_xalloc
65 * special virtualalloc, allocates lineary monoton growing memory.
66 * (the usual VirtualAlloc does not satisfy that restriction)
68 static LPVOID
69 DPMI_xalloc(int len) {
70 LPVOID ret;
71 LPVOID oldlastv = lastvalloced;
73 if (lastvalloced) {
74 int xflag = 0;
75 ret = NULL;
76 while (!ret) {
77 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
78 if (!ret)
79 lastvalloced = (char *) lastvalloced + 0x10000;
80 /* we failed to allocate one in the first round.
81 * try non-linear
83 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
84 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
85 xflag++;
87 /* if we even fail to allocate something in the next
88 * round, return NULL
90 if ((xflag==1) && (lastvalloced >= oldlastv))
91 xflag++;
92 if ((xflag==2) && (lastvalloced < oldlastv)) {
93 FIXME("failed to allocate any memory of %d bytes!\n",len);
94 return NULL;
97 } else
98 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
99 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
100 return ret;
103 static void
104 DPMI_xfree(LPVOID ptr) {
105 VirtualFree(ptr,0,MEM_RELEASE);
108 /* FIXME: perhaps we could grow this mapped area... */
109 static LPVOID
110 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
111 MEMORY_BASIC_INFORMATION mbi;
112 LPVOID newptr;
114 newptr = DPMI_xalloc(newsize);
115 if (ptr) {
116 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
117 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
118 return NULL;
120 if (mbi.State == MEM_FREE) {
121 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
122 return NULL;
124 /* We do not shrink allocated memory. most reallocs
125 * only do grows anyway
127 if (newsize<=mbi.RegionSize)
128 return ptr;
129 memcpy(newptr,ptr,mbi.RegionSize);
130 DPMI_xfree(ptr);
132 return newptr;
135 /**********************************************************************
136 * CallRMInt
138 static void CallRMInt( CONTEXT86 *context )
140 if (!Dosvm.CallRMInt && !DPMI_LoadDosSystem())
142 ERR("could not setup real-mode calls\n");
143 return;
145 Dosvm.CallRMInt( context );
149 static void CallRMProc( CONTEXT86 *context, int iret )
151 if (!Dosvm.CallRMProc && !DPMI_LoadDosSystem())
153 ERR("could not setup real-mode calls\n");
154 return;
156 Dosvm.CallRMProc( context, iret );
159 static void AllocRMCB( CONTEXT86 *context )
161 if (!Dosvm.AllocRMCB && !DPMI_LoadDosSystem())
163 ERR("could not setup real-mode calls\n");
164 return;
166 Dosvm.AllocRMCB( context );
169 static void FreeRMCB( CONTEXT86 *context )
171 if (!Dosvm.FreeRMCB) /* cannot have allocated one if dosvm not loaded */
173 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
174 SET_CFLAG( context );
176 else Dosvm.FreeRMCB( context );
180 /**********************************************************************
181 * INT_Int31Handler (WPROCS.149)
183 * Handler for int 31h (DPMI).
186 void WINAPI INT_Int31Handler( CONTEXT86 *context )
189 * Note: For Win32s processes, the whole linear address space is
190 * shifted by 0x10000 relative to the OS linear address space.
191 * See the comment in msdos/vxd.c.
193 DWORD dw;
194 BYTE *ptr;
196 RESET_CFLAG(context);
197 switch(AX_reg(context))
199 case 0x0000: /* Allocate LDT descriptors */
200 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
201 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
203 TRACE("failed\n");
204 context->Eax = 0x8011; /* descriptor unavailable */
205 SET_CFLAG(context);
207 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
208 break;
210 case 0x0001: /* Free LDT descriptor */
211 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
212 if (FreeSelector16( BX_reg(context) ))
214 context->Eax = 0x8022; /* invalid selector */
215 SET_CFLAG(context);
217 else
219 /* If a segment register contains the selector being freed, */
220 /* set it to zero. */
221 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
222 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
223 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
224 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
226 break;
228 case 0x0002: /* Real mode segment to descriptor */
229 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
231 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
232 switch(BX_reg(context))
234 case 0x0000: entryPoint = 183; break; /* __0000H */
235 case 0x0040: entryPoint = 193; break; /* __0040H */
236 case 0xa000: entryPoint = 174; break; /* __A000H */
237 case 0xb000: entryPoint = 181; break; /* __B000H */
238 case 0xb800: entryPoint = 182; break; /* __B800H */
239 case 0xc000: entryPoint = 195; break; /* __C000H */
240 case 0xd000: entryPoint = 179; break; /* __D000H */
241 case 0xe000: entryPoint = 190; break; /* __E000H */
242 case 0xf000: entryPoint = 194; break; /* __F000H */
243 default:
244 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
245 break;
247 if (entryPoint)
248 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
249 (LPCSTR)(ULONG_PTR)entryPoint ));
251 break;
253 case 0x0003: /* Get next selector increment */
254 TRACE("get selector increment (__AHINCR)\n");
255 context->Eax = __AHINCR;
256 break;
258 case 0x0004: /* Lock selector (not supported) */
259 FIXME("lock selector not supported\n");
260 context->Eax = 0; /* FIXME: is this a correct return value? */
261 break;
263 case 0x0005: /* Unlock selector (not supported) */
264 FIXME("unlock selector not supported\n");
265 context->Eax = 0; /* FIXME: is this a correct return value? */
266 break;
268 case 0x0006: /* Get selector base address */
269 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
270 if (!(dw = GetSelectorBase( BX_reg(context) )))
272 context->Eax = 0x8022; /* invalid selector */
273 SET_CFLAG(context);
275 else
277 CX_reg(context) = HIWORD(W32S_WINE2APP(dw));
278 DX_reg(context) = LOWORD(W32S_WINE2APP(dw));
280 break;
282 case 0x0007: /* Set selector base address */
283 TRACE("set selector base address (0x%04x,0x%08lx)\n",
284 BX_reg(context),
285 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
286 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
287 if (dw < 0x10000)
288 /* app wants to access lower 64K of DOS memory, load DOS subsystem */
289 DPMI_LoadDosSystem();
290 SetSelectorBase(BX_reg(context), dw);
291 break;
293 case 0x0008: /* Set selector limit */
294 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
295 dw = MAKELONG( DX_reg(context), CX_reg(context) );
296 SetSelectorLimit16( BX_reg(context), dw );
297 break;
299 case 0x0009: /* Set selector access rights */
300 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
301 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
302 break;
304 case 0x000a: /* Allocate selector alias */
305 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
306 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
308 AX_reg(context) = 0x8011; /* descriptor unavailable */
309 SET_CFLAG(context);
311 break;
313 case 0x000b: /* Get descriptor */
314 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
316 LDT_ENTRY entry;
317 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
318 /* FIXME: should use ES:EDI for 32-bit clients */
319 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
321 break;
323 case 0x000c: /* Set descriptor */
324 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
326 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
327 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
328 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
330 break;
332 case 0x000d: /* Allocate specific LDT descriptor */
333 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
334 AX_reg(context) = 0x8011; /* descriptor unavailable */
335 SET_CFLAG(context);
336 break;
337 case 0x0100: /* Allocate DOS memory block */
338 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
339 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
340 if (dw) {
341 AX_reg(context) = HIWORD(dw);
342 DX_reg(context) = LOWORD(dw);
343 } else {
344 AX_reg(context) = 0x0008; /* insufficient memory */
345 BX_reg(context) = DOSMEM_Available()>>4;
346 SET_CFLAG(context);
348 break;
349 case 0x0101: /* Free DOS memory block */
350 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
351 dw = GlobalDOSFree16(DX_reg(context));
352 if (!dw) {
353 AX_reg(context) = 0x0009; /* memory block address invalid */
354 SET_CFLAG(context);
356 break;
357 case 0x0200: /* get real mode interrupt vector */
358 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
359 BL_reg(context));
360 SET_CFLAG(context);
361 break;
362 case 0x0201: /* set real mode interrupt vector */
363 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
364 SET_CFLAG(context);
365 break;
366 case 0x0204: /* Get protected mode interrupt vector */
367 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
368 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
369 CX_reg(context) = HIWORD(dw);
370 DX_reg(context) = LOWORD(dw);
371 break;
373 case 0x0205: /* Set protected mode interrupt vector */
374 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
375 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
376 INT_SetPMHandler( BL_reg(context),
377 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
378 break;
380 case 0x0300: /* Simulate real mode interrupt */
381 CallRMInt( context );
382 break;
384 case 0x0301: /* Call real mode procedure with far return */
385 CallRMProc( context, FALSE );
386 break;
388 case 0x0302: /* Call real mode procedure with interrupt return */
389 CallRMProc( context, TRUE );
390 break;
392 case 0x0303: /* Allocate Real Mode Callback Address */
393 AllocRMCB( context );
394 break;
396 case 0x0304: /* Free Real Mode Callback Address */
397 FreeRMCB( context );
398 break;
400 case 0x0305: /* Get State Save/Restore Addresses */
401 TRACE("get state save/restore addresses\n");
402 /* we probably won't need this kind of state saving */
403 AX_reg(context) = 0;
404 /* real mode: just point to the lret */
405 BX_reg(context) = DOSMEM_wrap_seg;
406 context->Ecx = 2;
407 /* protected mode: don't have any handler yet... */
408 FIXME("no protected-mode dummy state save/restore handler yet\n");
409 SI_reg(context) = 0;
410 context->Edi = 0;
411 break;
413 case 0x0306: /* Get Raw Mode Switch Addresses */
414 TRACE("get raw mode switch addresses\n");
415 /* real mode, point to standard DPMI return wrapper */
416 BX_reg(context) = DOSMEM_wrap_seg;
417 context->Ecx = 0;
418 /* protected mode, point to DPMI call wrapper */
419 SI_reg(context) = DOSMEM_dpmi_sel;
420 context->Edi = 8; /* offset of the INT 0x31 call */
421 break;
422 case 0x0400: /* Get DPMI version */
423 TRACE("get DPMI version\n");
425 SYSTEM_INFO si;
427 GetSystemInfo(&si);
428 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
429 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
430 CL_reg(context) = si.wProcessorLevel;
431 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
432 break;
434 case 0x0500: /* Get free memory information */
435 TRACE("get free memory information\n");
437 MEMMANINFO mmi;
439 mmi.dwSize = sizeof(mmi);
440 MemManInfo16(&mmi);
441 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
442 /* the layout is just the same as MEMMANINFO, but without
443 * the dwSize entry.
445 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
446 break;
448 case 0x0501: /* Allocate memory block */
449 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
450 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
452 AX_reg(context) = 0x8012; /* linear memory not available */
453 SET_CFLAG(context);
454 } else {
455 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
456 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
458 break;
460 case 0x0502: /* Free memory block */
461 TRACE("free memory block (0x%08lx)\n",
462 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
463 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
464 break;
466 case 0x0503: /* Resize memory block */
467 TRACE("resize memory block (0x%08lx,%ld)\n",
468 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
469 MAKELONG(CX_reg(context),BX_reg(context)));
470 if (!(ptr = (BYTE *)DPMI_xrealloc(
471 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
472 MAKELONG(CX_reg(context),BX_reg(context)))))
474 AX_reg(context) = 0x8012; /* linear memory not available */
475 SET_CFLAG(context);
476 } else {
477 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
478 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
480 break;
482 case 0x0507: /* Modify page attributes */
483 FIXME("modify page attributes unimplemented\n");
484 break; /* Just ignore it */
486 case 0x0600: /* Lock linear region */
487 FIXME("lock linear region unimplemented\n");
488 break; /* Just ignore it */
490 case 0x0601: /* Unlock linear region */
491 FIXME("unlock linear region unimplemented\n");
492 break; /* Just ignore it */
494 case 0x0602: /* Unlock real-mode region */
495 FIXME("unlock realmode region unimplemented\n");
496 break; /* Just ignore it */
498 case 0x0603: /* Lock real-mode region */
499 FIXME("lock realmode region unimplemented\n");
500 break; /* Just ignore it */
502 case 0x0604: /* Get page size */
503 TRACE("get pagesize\n");
504 BX_reg(context) = 0;
505 CX_reg(context) = getpagesize();
506 break;
508 case 0x0702: /* Mark page as demand-paging candidate */
509 FIXME("mark page as demand-paging candidate\n");
510 break; /* Just ignore it */
512 case 0x0703: /* Discard page contents */
513 FIXME("discard page contents\n");
514 break; /* Just ignore it */
516 case 0x0800: /* Physical address mapping */
517 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
518 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
520 AX_reg(context) = 0x8021;
521 SET_CFLAG(context);
523 else
525 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr));
526 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr));
527 RESET_CFLAG(context);
529 break;
531 default:
532 INT_BARF( context, 0x31 );
533 AX_reg(context) = 0x8001; /* unsupported function */
534 SET_CFLAG(context);
535 break;