Add missing </para>.
[wine/hacks.git] / msdos / dpmi.c
blob00d9c1ba86d3869dc594968ee561c03db857d211
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 "builtin16.h"
16 #include "miscemu.h"
17 #include "msdos.h"
18 #include "dosexe.h"
19 #include "task.h"
20 #include "toolhelp.h"
21 #include "selectors.h"
22 #include "callback.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(int31);
27 #define DOS_GET_DRIVE(reg) ((reg) ? (reg) - 1 : DRIVE_GetCurrentDrive())
29 void CreateBPB(int drive, BYTE *data, BOOL16 limited); /* defined in int21.c */
31 static void* lastvalloced = NULL;
33 /* Structure for real-mode callbacks */
34 typedef struct
36 DWORD edi;
37 DWORD esi;
38 DWORD ebp;
39 DWORD reserved;
40 DWORD ebx;
41 DWORD edx;
42 DWORD ecx;
43 DWORD eax;
44 WORD fl;
45 WORD es;
46 WORD ds;
47 WORD fs;
48 WORD gs;
49 WORD ip;
50 WORD cs;
51 WORD sp;
52 WORD ss;
53 } REALMODECALL;
57 typedef struct tagRMCB {
58 DWORD address;
59 DWORD proc_ofs,proc_sel;
60 DWORD regs_ofs,regs_sel;
61 struct tagRMCB *next;
62 } RMCB;
64 static RMCB *FirstRMCB = NULL;
67 static LPDOSTASK WINAPI DPMI_NoCurrent(void) { return NULL; }
68 DOSVM_TABLE Dosvm = { DPMI_NoCurrent };
70 static HMODULE DosModule;
72 /**********************************************************************
73 * DPMI_LoadDosSystem
75 BOOL DPMI_LoadDosSystem(void)
77 if (DosModule) return TRUE;
78 DosModule = LoadLibraryA( "winedos.dll" );
79 if (!DosModule) {
80 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
81 return FALSE;
83 Dosvm.Current = (void *)GetProcAddress(DosModule, "GetCurrent");
84 Dosvm.LoadDPMI = (void *)GetProcAddress(DosModule, "LoadDPMI");
85 Dosvm.LoadDosExe = (void *)GetProcAddress(DosModule, "LoadDosExe");
86 Dosvm.Exec = (void *)GetProcAddress(DosModule, "Exec");
87 Dosvm.Exit = (void *)GetProcAddress(DosModule, "Exit");
88 Dosvm.Enter = (void *)GetProcAddress(DosModule, "Enter");
89 Dosvm.RunInThread = (void *)GetProcAddress(DosModule, "RunInThread");
90 Dosvm.Wait = (void *)GetProcAddress(DosModule, "Wait");
91 Dosvm.QueueEvent = (void *)GetProcAddress(DosModule, "QueueEvent");
92 Dosvm.OutPIC = (void *)GetProcAddress(DosModule, "OutPIC");
93 Dosvm.SetTimer = (void *)GetProcAddress(DosModule, "SetTimer");
94 Dosvm.GetTimer = (void *)GetProcAddress(DosModule, "GetTimer");
95 Dosvm.KbdReadScan = (void *)GetProcAddress(DosModule, "KbdReadScan");
96 return TRUE;
99 /**********************************************************************
100 * DPMI_xalloc
101 * special virtualalloc, allocates lineary monoton growing memory.
102 * (the usual VirtualAlloc does not satisfy that restriction)
104 static LPVOID
105 DPMI_xalloc(int len) {
106 LPVOID ret;
107 LPVOID oldlastv = lastvalloced;
109 if (lastvalloced) {
110 int xflag = 0;
111 ret = NULL;
112 while (!ret) {
113 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
114 if (!ret)
115 lastvalloced = (char *) lastvalloced + 0x10000;
116 /* we failed to allocate one in the first round.
117 * try non-linear
119 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
120 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
121 xflag++;
123 /* if we even fail to allocate something in the next
124 * round, return NULL
126 if ((xflag==1) && (lastvalloced >= oldlastv))
127 xflag++;
128 if ((xflag==2) && (lastvalloced < oldlastv)) {
129 FIXME("failed to allocate any memory of %d bytes!\n",len);
130 return NULL;
133 } else
134 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
135 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
136 return ret;
139 static void
140 DPMI_xfree(LPVOID ptr) {
141 VirtualFree(ptr,0,MEM_RELEASE);
144 /* FIXME: perhaps we could grow this mapped area... */
145 static LPVOID
146 DPMI_xrealloc(LPVOID ptr,DWORD newsize) {
147 MEMORY_BASIC_INFORMATION mbi;
148 LPVOID newptr;
150 newptr = DPMI_xalloc(newsize);
151 if (ptr) {
152 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
153 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
154 return NULL;
156 if (mbi.State == MEM_FREE) {
157 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
158 return NULL;
160 /* We do not shrink allocated memory. most reallocs
161 * only do grows anyway
163 if (newsize<=mbi.RegionSize)
164 return ptr;
165 memcpy(newptr,ptr,mbi.RegionSize);
166 DPMI_xfree(ptr);
168 return newptr;
170 /**********************************************************************
171 * INT_GetRealModeContext
173 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
175 context->Eax = call->eax;
176 context->Ebx = call->ebx;
177 context->Ecx = call->ecx;
178 context->Edx = call->edx;
179 context->Esi = call->esi;
180 context->Edi = call->edi;
181 context->Ebp = call->ebp;
182 context->EFlags = call->fl | V86_FLAG;
183 context->Eip = call->ip;
184 context->Esp = call->sp;
185 context->SegCs = call->cs;
186 context->SegDs = call->ds;
187 context->SegEs = call->es;
188 context->SegFs = call->fs;
189 context->SegGs = call->gs;
190 context->SegSs = call->ss;
194 /**********************************************************************
195 * INT_SetRealModeContext
197 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
199 call->eax = context->Eax;
200 call->ebx = context->Ebx;
201 call->ecx = context->Ecx;
202 call->edx = context->Edx;
203 call->esi = context->Esi;
204 call->edi = context->Edi;
205 call->ebp = context->Ebp;
206 call->fl = LOWORD(context->EFlags);
207 call->ip = LOWORD(context->Eip);
208 call->sp = LOWORD(context->Esp);
209 call->cs = context->SegCs;
210 call->ds = context->SegDs;
211 call->es = context->SegEs;
212 call->fs = context->SegFs;
213 call->gs = context->SegGs;
214 call->ss = context->SegSs;
217 #ifdef __i386__
219 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
220 #if 0 /* original code, which early gccs puke on */
222 int _clobber;
223 __asm__ __volatile__(
224 "pushl %%ebp\n"
225 "pushl %%ebx\n"
226 "pushl %%es\n"
227 "pushl %%ds\n"
228 "pushfl\n"
229 "mov %7,%%es\n"
230 "mov %5,%%ds\n"
231 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
232 "popl %%ds\n"
233 "mov %%es,%0\n"
234 "popl %%es\n"
235 "popl %%ebx\n"
236 "popl %%ebp\n"
237 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
238 : "0" (ss), "2" (esp),
239 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
240 "3" (&rmcb->proc_ofs) );
242 #else /* code generated by a gcc new enough */
244 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
245 "pushl %ebp\n\t"
246 "movl %esp,%ebp\n\t"
247 "pushl %edi\n\t"
248 "pushl %esi\n\t"
249 "movl 0x8(%ebp),%eax\n\t"
250 "movl 0x10(%ebp),%esi\n\t"
251 "movl 0xc(%ebp),%edx\n\t"
252 "movl 0x10(%eax),%ecx\n\t"
253 "movl 0xc(%eax),%edi\n\t"
254 "addl $0x4,%eax\n\t"
255 "pushl %ebp\n\t"
256 "pushl %ebx\n\t"
257 "pushl %es\n\t"
258 "pushl %ds\n\t"
259 "pushfl\n\t"
260 "mov %cx,%es\n\t"
261 "mov %dx,%ds\n\t"
262 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
263 "popl %ds\n\t"
264 "mov %es,%dx\n\t"
265 "popl %es\n\t"
266 "popl %ebx\n\t"
267 "popl %ebp\n\t"
268 "movl 0x14(%ebp),%eax\n\t"
269 "movw %dx,(%eax)\n\t"
270 "movl 0x18(%ebp),%edx\n\t"
271 "movl %edi,(%edx)\n\t"
272 "popl %esi\n\t"
273 "popl %edi\n\t"
274 "leave\n\t"
275 "ret")
276 #endif
278 #endif /* __i386__ */
280 /**********************************************************************
281 * DPMI_CallRMCBProc
283 * This routine does the hard work of calling a callback procedure.
285 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
287 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
288 /* Wine-internal RMCB, call directly */
289 ((RMCBPROC)rmcb->proc_ofs)(context);
290 } else {
291 #ifdef __i386__
292 UINT16 ss,es;
293 DWORD esp,edi;
295 INT_SetRealModeContext(MapSL(MAKESEGPTR( rmcb->regs_sel, rmcb->regs_ofs )), context);
296 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, WINE_LDT_FLAGS_DATA );
297 esp = context->Esp;
299 FIXME("untested!\n");
301 /* The called proc ends with an IRET, and takes these parameters:
302 * DS:ESI = pointer to real-mode SS:SP
303 * ES:EDI = pointer to real-mode call structure
304 * It returns:
305 * ES:EDI = pointer to real-mode call structure (may be a copy)
306 * It is the proc's responsibility to change the return CS:IP in the
307 * real-mode call structure. */
308 if (flag & 1) {
309 /* 32-bit DPMI client */
310 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
311 } else {
312 /* 16-bit DPMI client */
313 CONTEXT86 ctx = *context;
314 ctx.SegCs = rmcb->proc_sel;
315 ctx.Eip = rmcb->proc_ofs;
316 ctx.SegDs = ss;
317 ctx.Esi = esp;
318 ctx.SegEs = rmcb->regs_sel;
319 ctx.Edi = rmcb->regs_ofs;
320 /* FIXME: I'm pretty sure this isn't right - should push flags first */
321 wine_call_to_16_regs_short(&ctx, 0);
322 es = ctx.SegEs;
323 edi = ctx.Edi;
325 FreeSelector16(ss);
326 INT_GetRealModeContext( MapSL( MAKESEGPTR( es, edi )), context);
327 #else
328 ERR("RMCBs only implemented for i386\n");
329 #endif
334 /**********************************************************************
335 * DPMI_CallRMProc
337 * This routine does the hard work of calling a real mode procedure.
339 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
341 LPWORD stack16;
342 LPVOID addr = NULL; /* avoid gcc warning */
343 LPDOSTASK lpDosTask = Dosvm.Current();
344 RMCB *CurrRMCB;
345 int alloc = 0, already = 0;
346 BYTE *code;
348 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
349 context->Eax, context->Ebx, context->Ecx, context->Edx );
350 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
351 context->Esi, context->Edi, context->SegEs, context->SegDs,
352 context->SegCs, LOWORD(context->Eip), args, iret?"IRET":"FAR" );
354 callrmproc_again:
356 /* there might be some code that just jumps to RMCBs or the like,
357 in which case following the jumps here might get us to a shortcut */
358 code = CTX_SEG_OFF_TO_LIN(context, context->SegCs, context->Eip);
359 switch (*code) {
360 case 0xe9: /* JMP NEAR */
361 context->Eip += 3 + *(WORD *)(code+1);
362 /* yeah, I know these gotos don't look good... */
363 goto callrmproc_again;
364 case 0xea: /* JMP FAR */
365 context->Eip = *(WORD *)(code+1);
366 context->SegCs = *(WORD *)(code+3);
367 /* ...but since the label is there anyway... */
368 goto callrmproc_again;
369 case 0xeb: /* JMP SHORT */
370 context->Eip += 2 + *(signed char *)(code+1);
371 /* ...because of other gotos below, so... */
372 goto callrmproc_again;
375 /* shortcut for chaining to internal interrupt handlers */
376 if ((context->SegCs == 0xF000) && iret) {
377 return INT_RealModeInterrupt( LOWORD(context->Eip)/4, context);
380 /* shortcut for RMCBs */
381 CurrRMCB = FirstRMCB;
383 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
384 CurrRMCB = CurrRMCB->next;
386 if (!(CurrRMCB || lpDosTask)) {
387 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
388 TRACE("creating VM86 task\n");
389 if ((!DPMI_LoadDosSystem()) || !(lpDosTask = Dosvm.LoadDPMI() )) {
390 ERR("could not setup VM86 task\n");
391 return 1;
394 if (!already) {
395 if (!context->SegSs) {
396 alloc = 1; /* allocate default stack */
397 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(context->SegSs) );
398 context->Esp = 64-2;
399 stack16 += 32-1;
400 if (!addr) {
401 ERR("could not allocate default stack\n");
402 return 1;
404 } else {
405 stack16 = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
407 context->Esp -= (args + (iret?1:0)) * sizeof(WORD);
408 stack16 -= args;
409 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
410 /* push flags if iret */
411 if (iret) {
412 stack16--; args++;
413 *stack16 = LOWORD(context->EFlags);
415 /* push return address (return to interrupt wrapper) */
416 *(--stack16) = DOSMEM_wrap_seg;
417 *(--stack16) = 0;
418 /* adjust stack */
419 context->Esp -= 2*sizeof(WORD);
420 already = 1;
423 if (CurrRMCB) {
424 /* RMCB call, invoke protected-mode handler directly */
425 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
426 /* check if we returned to where we thought we would */
427 if ((context->SegCs != DOSMEM_wrap_seg) ||
428 (LOWORD(context->Eip) != 0)) {
429 /* we need to continue at different address in real-mode space,
430 so we need to set it all up for real mode again */
431 goto callrmproc_again;
433 } else {
434 TRACE("entering real mode...\n");
435 Dosvm.Enter( context );
436 TRACE("returned from real-mode call\n");
438 if (alloc) DOSMEM_FreeBlock( addr );
439 return 0;
443 /**********************************************************************
444 * CallRMInt
446 static void CallRMInt( CONTEXT86 *context )
448 CONTEXT86 realmode_ctx;
449 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
450 REALMODECALL *call = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
451 INT_GetRealModeContext( call, &realmode_ctx );
453 /* we need to check if a real-mode program has hooked the interrupt */
454 if (HIWORD(rm_int)!=0xF000) {
455 /* yup, which means we need to switch to real mode... */
456 realmode_ctx.SegCs = HIWORD(rm_int);
457 realmode_ctx.Eip = LOWORD(rm_int);
458 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
459 SET_CFLAG(context);
460 } else {
461 RESET_CFLAG(context);
462 /* use the IP we have instead of BL_reg, in case some apps
463 decide to move interrupts around for whatever reason... */
464 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
465 SET_CFLAG(context);
466 if (context->EFlags & 1) {
467 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
468 BL_reg(context), realmode_ctx.Eax, realmode_ctx.Ebx,
469 realmode_ctx.Ecx, realmode_ctx.Edx);
470 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
471 realmode_ctx.Esi, realmode_ctx.Edi,
472 realmode_ctx.SegDs, realmode_ctx.SegEs );
475 INT_SetRealModeContext( call, &realmode_ctx );
479 static void CallRMProc( CONTEXT86 *context, int iret )
481 REALMODECALL *p = MapSL( MAKESEGPTR( context->SegEs, DI_reg(context) ));
482 CONTEXT86 context16;
484 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
485 p->eax, p->ebx, p->ecx, p->edx);
486 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
487 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
489 if (!(p->cs) && !(p->ip)) { /* remove this check
490 if Int21/6501 case map function
491 has been implemented */
492 SET_CFLAG(context);
493 return;
495 INT_GetRealModeContext(p, &context16);
496 DPMI_CallRMProc( &context16, ((LPWORD)MapSL(MAKESEGPTR(context->SegSs, LOWORD(context->Esp))))+3,
497 CX_reg(context), iret );
498 INT_SetRealModeContext(p, &context16);
502 static RMCB *DPMI_AllocRMCB( void )
504 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
505 UINT16 uParagraph;
507 if (NewRMCB)
509 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
510 LPBYTE p = RMCBmem;
512 *p++ = 0xcd; /* RMCB: */
513 *p++ = 0x31; /* int $0x31 */
514 /* it is the called procedure's task to change the return CS:EIP
515 the DPMI 0.9 spec states that if it doesn't, it will be called again */
516 *p++ = 0xeb;
517 *p++ = 0xfc; /* jmp RMCB */
518 NewRMCB->address = MAKELONG(0, uParagraph);
519 NewRMCB->next = FirstRMCB;
520 FirstRMCB = NewRMCB;
522 return NewRMCB;
526 static void AllocRMCB( CONTEXT86 *context )
528 RMCB *NewRMCB = DPMI_AllocRMCB();
530 TRACE("Function to call: %04x:%04x\n", (WORD)context->SegDs, SI_reg(context) );
532 if (NewRMCB)
534 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
535 NewRMCB->proc_ofs = LOWORD(context->Esi);
536 NewRMCB->proc_sel = context->SegDs;
537 NewRMCB->regs_ofs = LOWORD(context->Edi);
538 NewRMCB->regs_sel = context->SegEs;
539 SET_LOWORD( context->Ecx, HIWORD(NewRMCB->address) );
540 SET_LOWORD( context->Edx, LOWORD(NewRMCB->address) );
542 else
544 SET_LOWORD( context->Eax, 0x8015 ); /* callback unavailable */
545 SET_CFLAG(context);
550 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
552 RMCB *NewRMCB = DPMI_AllocRMCB();
554 if (NewRMCB) {
555 NewRMCB->proc_ofs = (DWORD)proc;
556 NewRMCB->proc_sel = 0;
557 NewRMCB->regs_ofs = 0;
558 NewRMCB->regs_sel = 0;
559 return (FARPROC16)(NewRMCB->address);
561 return NULL;
565 static int DPMI_FreeRMCB( DWORD address )
567 RMCB *CurrRMCB = FirstRMCB;
568 RMCB *PrevRMCB = NULL;
570 while (CurrRMCB && (CurrRMCB->address != address))
572 PrevRMCB = CurrRMCB;
573 CurrRMCB = CurrRMCB->next;
575 if (CurrRMCB)
577 if (PrevRMCB)
578 PrevRMCB->next = CurrRMCB->next;
579 else
580 FirstRMCB = CurrRMCB->next;
581 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
582 HeapFree(GetProcessHeap(), 0, CurrRMCB);
583 return 0;
585 return 1;
589 static void FreeRMCB( CONTEXT86 *context )
591 FIXME("callback address: %04x:%04x\n",
592 CX_reg(context), DX_reg(context));
594 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
595 SET_LOWORD( context->Eax, 0x8024 ); /* invalid callback address */
596 SET_CFLAG(context);
601 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
603 DPMI_FreeRMCB( (DWORD)proc );
607 /* (see dosmem.c, function DOSMEM_InitDPMI) */
609 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
611 UINT16 cs, ss, ds, es;
612 CONTEXT86 pm_ctx;
613 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
614 PDB16 *psp = (PDB16 *)psp_ofs;
615 HANDLE16 env_seg = psp->environment;
616 unsigned char selflags = WINE_LDT_FLAGS_DATA;
618 RESET_CFLAG(context);
619 lpDosTask->dpmi_flag = AX_reg(context);
620 /* our mode switch wrapper have placed the desired CS into DX */
621 cs = SELECTOR_AllocBlock( (void *)(DX_reg(context)<<4), 0x10000, WINE_LDT_FLAGS_CODE );
622 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
623 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
624 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
625 32-bit code using this stack. */
626 if (lpDosTask->dpmi_flag & 1) selflags |= WINE_LDT_FLAGS_32BIT;
627 ss = SELECTOR_AllocBlock( (void *)(context->SegSs<<4), 0x10000, selflags );
628 /* do the same for the data segments, just in case */
629 if (context->SegDs == context->SegSs) ds = ss;
630 else ds = SELECTOR_AllocBlock( (void *)(context->SegDs<<4), 0x10000, selflags );
631 es = SELECTOR_AllocBlock( psp, 0x100, selflags );
632 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
633 psp->environment = SELECTOR_AllocBlock( (void *)(env_seg<<4), 0x10000, WINE_LDT_FLAGS_DATA );
635 pm_ctx = *context;
636 pm_ctx.SegCs = DOSMEM_dpmi_sel;
637 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
638 pm_ctx.Eax = ss;
639 pm_ctx.Edx = cs;
640 pm_ctx.SegDs = ds;
641 pm_ctx.SegEs = es;
642 pm_ctx.SegFs = 0;
643 pm_ctx.SegGs = 0;
645 TRACE("DOS program is now entering protected mode\n");
646 wine_call_to_16_regs_short(&pm_ctx, 0);
648 /* in the current state of affairs, we won't ever actually return here... */
649 /* we should have int21/ah=4c do it someday, though... */
651 FreeSelector16(psp->environment);
652 psp->environment = env_seg;
653 FreeSelector16(es);
654 if (ds != ss) FreeSelector16(ds);
655 FreeSelector16(ss);
656 FreeSelector16(cs);
659 /* DPMI Raw Mode Switch handler */
661 #if 0
662 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
664 LPDOSTASK lpDosTask = Dosvm.Current();
665 CONTEXT86 rm_ctx;
666 int ret;
668 if (!lpDosTask) {
669 /* we could probably start a DPMI-only dosmod task here, but I doubt
670 anything other than real DOS apps want to call raw mode switch */
671 ERR("attempting raw mode switch without DOS task!\n");
672 ExitProcess(1);
674 /* initialize real-mode context as per spec */
675 memset(&rm_ctx, 0, sizeof(rm_ctx));
676 rm_ctx.SegDs = AX_sig(context);
677 rm_ctx.SegEs = CX_sig(context);
678 rm_ctx.SegSs = DX_sig(context);
679 rm_ctx.Esp = EBX_sig(context);
680 rm_ctx.SegCs = SI_sig(context);
681 rm_ctx.Eip = EDI_sig(context);
682 rm_ctx.Ebp = EBP_sig(context);
683 rm_ctx.SegFs = 0;
684 rm_ctx.SegGs = 0;
685 rm_ctx.EFlags = EFL_sig(context); /* at least we need the IF flag */
687 /* enter real mode again */
688 TRACE("re-entering real mode at %04lx:%04lx\n",rm_ctx.SegCs,rm_ctx.Eip);
689 ret = Dosvm.Enter( &rm_ctx );
690 /* when the real-mode stuff call its mode switch address,
691 Dosvm.Enter will return and we will continue here */
693 if (ret<0) {
694 /* if the sync was lost, there's no way to recover */
695 ExitProcess(1);
698 /* alter protected-mode context as per spec */
699 DS_sig(context) = LOWORD(rm_ctx.Eax);
700 ES_sig(context) = LOWORD(rm_ctx.Ecx);
701 SS_sig(context) = LOWORD(rm_ctx.Edx);
702 ESP_sig(context) = rm_ctx.Ebx;
703 CS_sig(context) = LOWORD(rm_ctx.Esi);
704 EIP_sig(context) = rm_ctx.Edi;
705 EBP_sig(context) = rm_ctx.Ebp;
706 FS_sig(context) = 0;
707 GS_sig(context) = 0;
709 /* Return to new address and hope that we didn't mess up */
710 TRACE("re-entering protected mode at %04x:%08lx\n",
711 CS_sig(context), EIP_sig(context));
713 #endif
716 /**********************************************************************
717 * INT_Int31Handler (WPROCS.149)
719 * Handler for int 31h (DPMI).
722 void WINAPI INT_Int31Handler( CONTEXT86 *context )
725 * Note: For Win32s processes, the whole linear address space is
726 * shifted by 0x10000 relative to the OS linear address space.
727 * See the comment in msdos/vxd.c.
729 DWORD dw;
730 BYTE *ptr;
732 LPDOSTASK lpDosTask = Dosvm.Current();
734 if (ISV86(context) && lpDosTask) {
735 /* Called from real mode, check if it's our wrapper */
736 TRACE("called from real mode\n");
737 if (context->SegCs==DOSMEM_dpmi_seg) {
738 /* This is the protected mode switch */
739 StartPM(context,lpDosTask);
740 return;
741 } else
742 if (context->SegCs==DOSMEM_xms_seg) {
743 /* This is the XMS driver entry point */
744 XMS_Handler(context);
745 return;
746 } else
748 /* Check for RMCB */
749 RMCB *CurrRMCB = FirstRMCB;
751 while (CurrRMCB && (HIWORD(CurrRMCB->address) != context->SegCs))
752 CurrRMCB = CurrRMCB->next;
754 if (CurrRMCB) {
755 /* RMCB call, propagate to protected-mode handler */
756 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
757 return;
762 RESET_CFLAG(context);
763 switch(AX_reg(context))
765 case 0x0000: /* Allocate LDT descriptors */
766 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
767 if (!(context->Eax = AllocSelectorArray16( CX_reg(context) )))
769 TRACE("failed\n");
770 context->Eax = 0x8011; /* descriptor unavailable */
771 SET_CFLAG(context);
773 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
774 break;
776 case 0x0001: /* Free LDT descriptor */
777 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
778 if (FreeSelector16( BX_reg(context) ))
780 context->Eax = 0x8022; /* invalid selector */
781 SET_CFLAG(context);
783 else
785 /* If a segment register contains the selector being freed, */
786 /* set it to zero. */
787 if (!((context->SegDs^BX_reg(context)) & ~3)) context->SegDs = 0;
788 if (!((context->SegEs^BX_reg(context)) & ~3)) context->SegEs = 0;
789 if (!((context->SegFs^BX_reg(context)) & ~3)) context->SegFs = 0;
790 if (!((context->SegGs^BX_reg(context)) & ~3)) context->SegGs = 0;
792 break;
794 case 0x0002: /* Real mode segment to descriptor */
795 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
797 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
798 switch(BX_reg(context))
800 case 0x0000: entryPoint = 183; break; /* __0000H */
801 case 0x0040: entryPoint = 193; break; /* __0040H */
802 case 0xa000: entryPoint = 174; break; /* __A000H */
803 case 0xb000: entryPoint = 181; break; /* __B000H */
804 case 0xb800: entryPoint = 182; break; /* __B800H */
805 case 0xc000: entryPoint = 195; break; /* __C000H */
806 case 0xd000: entryPoint = 179; break; /* __D000H */
807 case 0xe000: entryPoint = 190; break; /* __E000H */
808 case 0xf000: entryPoint = 194; break; /* __F000H */
809 default:
810 context->Eax = DOSMEM_AllocSelector(BX_reg(context));
811 break;
813 if (entryPoint)
814 context->Eax = LOWORD(GetProcAddress16( GetModuleHandle16( "KERNEL" ),
815 (LPCSTR)(ULONG_PTR)entryPoint ));
817 break;
819 case 0x0003: /* Get next selector increment */
820 TRACE("get selector increment (__AHINCR)\n");
821 context->Eax = __AHINCR;
822 break;
824 case 0x0004: /* Lock selector (not supported) */
825 FIXME("lock selector not supported\n");
826 context->Eax = 0; /* FIXME: is this a correct return value? */
827 break;
829 case 0x0005: /* Unlock selector (not supported) */
830 FIXME("unlock selector not supported\n");
831 context->Eax = 0; /* FIXME: is this a correct return value? */
832 break;
834 case 0x0006: /* Get selector base address */
835 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
836 if (!(dw = GetSelectorBase( BX_reg(context) )))
838 context->Eax = 0x8022; /* invalid selector */
839 SET_CFLAG(context);
841 else
843 CX_reg(context) = HIWORD(W32S_WINE2APP(dw));
844 DX_reg(context) = LOWORD(W32S_WINE2APP(dw));
846 break;
848 case 0x0007: /* Set selector base address */
849 TRACE("set selector base address (0x%04x,0x%08lx)\n",
850 BX_reg(context),
851 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context))));
852 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)));
853 if (dw < 0x10000)
854 /* app wants to access lower 64K of DOS memory, map it in now */
855 DOSMEM_Init(TRUE);
856 SetSelectorBase(BX_reg(context), dw);
857 break;
859 case 0x0008: /* Set selector limit */
860 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
861 dw = MAKELONG( DX_reg(context), CX_reg(context) );
862 SetSelectorLimit16( BX_reg(context), dw );
863 break;
865 case 0x0009: /* Set selector access rights */
866 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
867 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
868 break;
870 case 0x000a: /* Allocate selector alias */
871 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
872 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
874 AX_reg(context) = 0x8011; /* descriptor unavailable */
875 SET_CFLAG(context);
877 break;
879 case 0x000b: /* Get descriptor */
880 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
882 LDT_ENTRY entry;
883 wine_ldt_set_base( &entry, (void*)W32S_WINE2APP(wine_ldt_get_base(&entry)) );
884 /* FIXME: should use ES:EDI for 32-bit clients */
885 *(LDT_ENTRY *)MapSL( MAKESEGPTR( context->SegEs, LOWORD(context->Edi) )) = entry;
887 break;
889 case 0x000c: /* Set descriptor */
890 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
892 LDT_ENTRY entry = *(LDT_ENTRY *)MapSL(MAKESEGPTR( context->SegEs, LOWORD(context->Edi)));
893 wine_ldt_set_base( &entry, (void*)W32S_APP2WINE(wine_ldt_get_base(&entry)) );
894 wine_ldt_set_entry( LOWORD(context->Ebx), &entry );
896 break;
898 case 0x000d: /* Allocate specific LDT descriptor */
899 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
900 AX_reg(context) = 0x8011; /* descriptor unavailable */
901 SET_CFLAG(context);
902 break;
903 case 0x0100: /* Allocate DOS memory block */
904 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
905 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
906 if (dw) {
907 AX_reg(context) = HIWORD(dw);
908 DX_reg(context) = LOWORD(dw);
909 } else {
910 AX_reg(context) = 0x0008; /* insufficient memory */
911 BX_reg(context) = DOSMEM_Available()>>4;
912 SET_CFLAG(context);
914 break;
915 case 0x0101: /* Free DOS memory block */
916 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
917 dw = GlobalDOSFree16(DX_reg(context));
918 if (!dw) {
919 AX_reg(context) = 0x0009; /* memory block address invalid */
920 SET_CFLAG(context);
922 break;
923 case 0x0200: /* get real mode interrupt vector */
924 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
925 BL_reg(context));
926 SET_CFLAG(context);
927 break;
928 case 0x0201: /* set real mode interrupt vector */
929 FIXME("set realmode interrupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
930 SET_CFLAG(context);
931 break;
932 case 0x0204: /* Get protected mode interrupt vector */
933 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
934 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
935 CX_reg(context) = HIWORD(dw);
936 DX_reg(context) = LOWORD(dw);
937 break;
939 case 0x0205: /* Set protected mode interrupt vector */
940 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
941 BL_reg(context),MapSL(MAKESEGPTR(CX_reg(context),DX_reg(context))));
942 INT_SetPMHandler( BL_reg(context),
943 (FARPROC16)MAKESEGPTR( CX_reg(context), DX_reg(context) ));
944 break;
946 case 0x0300: /* Simulate real mode interrupt */
947 CallRMInt( context );
948 break;
950 case 0x0301: /* Call real mode procedure with far return */
951 CallRMProc( context, FALSE );
952 break;
954 case 0x0302: /* Call real mode procedure with interrupt return */
955 CallRMProc( context, TRUE );
956 break;
958 case 0x0303: /* Allocate Real Mode Callback Address */
959 AllocRMCB( context );
960 break;
962 case 0x0304: /* Free Real Mode Callback Address */
963 FreeRMCB( context );
964 break;
966 case 0x0305: /* Get State Save/Restore Addresses */
967 TRACE("get state save/restore addresses\n");
968 /* we probably won't need this kind of state saving */
969 AX_reg(context) = 0;
970 /* real mode: just point to the lret */
971 BX_reg(context) = DOSMEM_wrap_seg;
972 context->Ecx = 2;
973 /* protected mode: don't have any handler yet... */
974 FIXME("no protected-mode dummy state save/restore handler yet\n");
975 SI_reg(context) = 0;
976 context->Edi = 0;
977 break;
979 case 0x0306: /* Get Raw Mode Switch Addresses */
980 TRACE("get raw mode switch addresses\n");
981 /* real mode, point to standard DPMI return wrapper */
982 BX_reg(context) = DOSMEM_wrap_seg;
983 context->Ecx = 0;
984 /* protected mode, point to DPMI call wrapper */
985 SI_reg(context) = DOSMEM_dpmi_sel;
986 context->Edi = 8; /* offset of the INT 0x31 call */
987 break;
988 case 0x0400: /* Get DPMI version */
989 TRACE("get DPMI version\n");
991 SYSTEM_INFO si;
993 GetSystemInfo(&si);
994 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
995 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
996 CL_reg(context) = si.wProcessorLevel;
997 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
998 break;
1000 case 0x0500: /* Get free memory information */
1001 TRACE("get free memory information\n");
1003 MEMMANINFO mmi;
1005 mmi.dwSize = sizeof(mmi);
1006 MemManInfo16(&mmi);
1007 ptr = MapSL(MAKESEGPTR(context->SegEs,DI_reg(context)));
1008 /* the layout is just the same as MEMMANINFO, but without
1009 * the dwSize entry.
1011 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1012 break;
1014 case 0x0501: /* Allocate memory block */
1015 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1016 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1018 AX_reg(context) = 0x8012; /* linear memory not available */
1019 SET_CFLAG(context);
1020 } else {
1021 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1022 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1024 break;
1026 case 0x0502: /* Free memory block */
1027 TRACE("free memory block (0x%08lx)\n",
1028 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))));
1029 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context), SI_reg(context))) );
1030 break;
1032 case 0x0503: /* Resize memory block */
1033 TRACE("resize memory block (0x%08lx,%ld)\n",
1034 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1035 MAKELONG(CX_reg(context),BX_reg(context)));
1036 if (!(ptr = (BYTE *)DPMI_xrealloc(
1037 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context))),
1038 MAKELONG(CX_reg(context),BX_reg(context)))))
1040 AX_reg(context) = 0x8012; /* linear memory not available */
1041 SET_CFLAG(context);
1042 } else {
1043 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1044 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1046 break;
1048 case 0x0507: /* Modify page attributes */
1049 FIXME("modify page attributes unimplemented\n");
1050 break; /* Just ignore it */
1052 case 0x0600: /* Lock linear region */
1053 FIXME("lock linear region unimplemented\n");
1054 break; /* Just ignore it */
1056 case 0x0601: /* Unlock linear region */
1057 FIXME("unlock linear region unimplemented\n");
1058 break; /* Just ignore it */
1060 case 0x0602: /* Unlock real-mode region */
1061 FIXME("unlock realmode region unimplemented\n");
1062 break; /* Just ignore it */
1064 case 0x0603: /* Lock real-mode region */
1065 FIXME("lock realmode region unimplemented\n");
1066 break; /* Just ignore it */
1068 case 0x0604: /* Get page size */
1069 TRACE("get pagesize\n");
1070 BX_reg(context) = 0;
1071 CX_reg(context) = getpagesize();
1072 break;
1074 case 0x0702: /* Mark page as demand-paging candidate */
1075 FIXME("mark page as demand-paging candidate\n");
1076 break; /* Just ignore it */
1078 case 0x0703: /* Discard page contents */
1079 FIXME("discard page contents\n");
1080 break; /* Just ignore it */
1082 case 0x0800: /* Physical address mapping */
1083 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1084 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1086 AX_reg(context) = 0x8021;
1087 SET_CFLAG(context);
1089 else
1091 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr));
1092 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr));
1093 RESET_CFLAG(context);
1095 break;
1097 default:
1098 INT_BARF( context, 0x31 );
1099 AX_reg(context) = 0x8001; /* unsupported function */
1100 SET_CFLAG(context);
1101 break;