Fixed IsEqualGUID definition.
[wine/hacks.git] / msdos / dpmi.c
blob6d695ca49007a3ce8f3ddd0754ecec0fb3e7bdcb
1 /*
2 * DPMI 0.9 emulation
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <unistd.h>
8 #include <string.h>
9 #include "windef.h"
10 #include "wine/winbase16.h"
11 #include "ldt.h"
12 #include "global.h"
13 #include "miscemu.h"
14 #include "msdos.h"
15 #include "dosexe.h"
16 #include "task.h"
17 #include "toolhelp.h"
18 #include "selectors.h"
19 #include "process.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;
31 /* Structure for real-mode callbacks */
32 typedef struct
34 DWORD edi;
35 DWORD esi;
36 DWORD ebp;
37 DWORD reserved;
38 DWORD ebx;
39 DWORD edx;
40 DWORD ecx;
41 DWORD eax;
42 WORD fl;
43 WORD es;
44 WORD ds;
45 WORD fs;
46 WORD gs;
47 WORD ip;
48 WORD cs;
49 WORD sp;
50 WORD ss;
51 } REALMODECALL;
55 typedef struct tagRMCB {
56 DWORD address;
57 DWORD proc_ofs,proc_sel;
58 DWORD regs_ofs,regs_sel;
59 struct tagRMCB *next;
60 } RMCB;
62 static RMCB *FirstRMCB = NULL;
64 UINT16 DPMI_wrap_seg;
66 /**********************************************************************
67 * DPMI_xalloc
68 * special virtualalloc, allocates lineary monoton growing memory.
69 * (the usual VirtualAlloc does not satisfy that restriction)
71 static LPVOID
72 DPMI_xalloc(int len) {
73 LPVOID ret;
74 LPVOID oldlastv = lastvalloced;
76 if (lastvalloced) {
77 int xflag = 0;
78 ret = NULL;
79 while (!ret) {
80 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
81 if (!ret)
82 lastvalloced = (char *) lastvalloced + 0x10000;
83 /* we failed to allocate one in the first round.
84 * try non-linear
86 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
87 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
88 xflag++;
90 /* if we even fail to allocate something in the next
91 * round, return NULL
93 if ((xflag==1) && (lastvalloced >= oldlastv))
94 xflag++;
95 if ((xflag==2) && (lastvalloced < oldlastv)) {
96 FIXME("failed to allocate any memory of %d bytes!\n",len);
97 return NULL;
100 } else
101 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
102 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
103 return ret;
106 static void
107 DPMI_xfree(LPVOID ptr) {
108 VirtualFree(ptr,0,MEM_RELEASE);
111 /* FIXME: perhaps we could grow this mapped area... */
112 static LPVOID
113 DPMI_xrealloc(LPVOID ptr,int newsize) {
114 MEMORY_BASIC_INFORMATION mbi;
115 LPVOID newptr;
117 newptr = DPMI_xalloc(newsize);
118 if (ptr) {
119 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
120 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
121 return NULL;
123 if (mbi.State == MEM_FREE) {
124 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
125 return NULL;
127 /* We do not shrink allocated memory. most reallocs
128 * only do grows anyway
130 if (newsize<=mbi.RegionSize)
131 return ptr;
132 memcpy(newptr,ptr,mbi.RegionSize);
133 DPMI_xfree(ptr);
135 return newptr;
137 /**********************************************************************
138 * INT_GetRealModeContext
140 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
142 EAX_reg(context) = call->eax;
143 EBX_reg(context) = call->ebx;
144 ECX_reg(context) = call->ecx;
145 EDX_reg(context) = call->edx;
146 ESI_reg(context) = call->esi;
147 EDI_reg(context) = call->edi;
148 EBP_reg(context) = call->ebp;
149 EFL_reg(context) = call->fl | V86_FLAG;
150 EIP_reg(context) = call->ip;
151 ESP_reg(context) = call->sp;
152 CS_reg(context) = call->cs;
153 DS_reg(context) = call->ds;
154 ES_reg(context) = call->es;
155 FS_reg(context) = call->fs;
156 GS_reg(context) = call->gs;
157 SS_reg(context) = call->ss;
161 /**********************************************************************
162 * INT_SetRealModeContext
164 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
166 call->eax = EAX_reg(context);
167 call->ebx = EBX_reg(context);
168 call->ecx = ECX_reg(context);
169 call->edx = EDX_reg(context);
170 call->esi = ESI_reg(context);
171 call->edi = EDI_reg(context);
172 call->ebp = EBP_reg(context);
173 call->fl = LOWORD(EFL_reg(context));
174 call->ip = LOWORD(EIP_reg(context));
175 call->sp = LOWORD(ESP_reg(context));
176 call->cs = CS_reg(context);
177 call->ds = DS_reg(context);
178 call->es = ES_reg(context);
179 call->fs = FS_reg(context);
180 call->gs = GS_reg(context);
181 call->ss = SS_reg(context);
184 #ifdef __i386__
186 void DPMI_CallRMCB32(RMCB *rmcb, UINT16 ss, DWORD esp, UINT16*es, DWORD*edi)
187 #if 0 /* original code, which early gccs puke on */
189 int _clobber;
190 __asm__ __volatile__(
191 "pushl %%ebp\n"
192 "pushl %%ebx\n"
193 "pushl %%es\n"
194 "pushl %%ds\n"
195 "pushfl\n"
196 "mov %7,%%es\n"
197 "mov %5,%%ds\n"
198 ".byte 0x36, 0xff, 0x18\n" /* lcall *%ss:(%eax) */
199 "popl %%ds\n"
200 "mov %%es,%0\n"
201 "popl %%es\n"
202 "popl %%ebx\n"
203 "popl %%ebp\n"
204 : "=d" (*es), "=D" (*edi), "=S" (_clobber), "=a" (_clobber), "=c" (_clobber)
205 : "0" (ss), "2" (esp),
206 "4" (rmcb->regs_sel), "1" (rmcb->regs_ofs),
207 "3" (&rmcb->proc_ofs) );
209 #else /* code generated by a gcc new enough */
211 __ASM_GLOBAL_FUNC(DPMI_CallRMCB32,
212 "pushl %ebp\n\t"
213 "movl %esp,%ebp\n\t"
214 "pushl %edi\n\t"
215 "pushl %esi\n\t"
216 "movl 0x8(%ebp),%eax\n\t"
217 "movl 0x10(%ebp),%esi\n\t"
218 "movl 0xc(%ebp),%edx\n\t"
219 "movl 0x10(%eax),%ecx\n\t"
220 "movl 0xc(%eax),%edi\n\t"
221 "addl $0x4,%eax\n\t"
222 "pushl %ebp\n\t"
223 "pushl %ebx\n\t"
224 "pushl %es\n\t"
225 "pushl %ds\n\t"
226 "pushfl\n\t"
227 "mov %cx,%es\n\t"
228 "mov %dx,%ds\n\t"
229 ".byte 0x36, 0xff, 0x18\n\t" /* lcall *%ss:(%eax) */
230 "popl %ds\n\t"
231 "mov %es,%dx\n\t"
232 "popl %es\n\t"
233 "popl %ebx\n\t"
234 "popl %ebp\n\t"
235 "movl 0x14(%ebp),%eax\n\t"
236 "movw %dx,(%eax)\n\t"
237 "movl 0x18(%ebp),%edx\n\t"
238 "movl %edi,(%edx)\n\t"
239 "popl %esi\n\t"
240 "popl %edi\n\t"
241 "leave\n\t"
242 "ret")
243 #endif
245 #endif /* __i386__ */
247 /**********************************************************************
248 * DPMI_CallRMCBProc
250 * This routine does the hard work of calling a callback procedure.
252 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
254 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
255 /* Wine-internal RMCB, call directly */
256 ((RMCBPROC)rmcb->proc_ofs)(context);
257 } else {
258 #ifdef __i386__
259 UINT16 ss,es;
260 DWORD esp,edi;
262 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
263 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase() + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
264 esp = ESP_reg(context);
266 FIXME("untested!\n");
268 /* The called proc ends with an IRET, and takes these parameters:
269 * DS:ESI = pointer to real-mode SS:SP
270 * ES:EDI = pointer to real-mode call structure
271 * It returns:
272 * ES:EDI = pointer to real-mode call structure (may be a copy)
273 * It is the proc's responsibility to change the return CS:IP in the
274 * real-mode call structure. */
275 if (flag & 1) {
276 /* 32-bit DPMI client */
277 DPMI_CallRMCB32(rmcb, ss, esp, &es, &edi);
278 } else {
279 /* 16-bit DPMI client */
280 CONTEXT86 ctx = *context;
281 CS_reg(&ctx) = rmcb->proc_sel;
282 EIP_reg(&ctx) = rmcb->proc_ofs;
283 DS_reg(&ctx) = ss;
284 ESI_reg(&ctx) = esp;
285 ES_reg(&ctx) = rmcb->regs_sel;
286 EDI_reg(&ctx) = rmcb->regs_ofs;
287 /* FIXME: I'm pretty sure this isn't right - should push flags first */
288 Callbacks->CallRegisterShortProc(&ctx, 0);
289 es = ES_reg(&ctx);
290 edi = EDI_reg(&ctx);
292 SELECTOR_FreeBlock(ss, 1);
293 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
294 #else
295 ERR("RMCBs only implemented for i386\n");
296 #endif
301 /**********************************************************************
302 * DPMI_CallRMProc
304 * This routine does the hard work of calling a real mode procedure.
306 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
308 LPWORD stack16;
309 LPVOID addr = NULL; /* avoid gcc warning */
310 LPDOSTASK lpDosTask = MZ_Current();
311 RMCB *CurrRMCB;
312 int alloc = 0, already = 0;
313 BYTE *code;
315 GlobalUnlock16( GetCurrentTask() );
317 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
318 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
319 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
320 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
321 CS_reg(context), LOWORD(EIP_reg(context)), args, iret?"IRET":"FAR" );
323 callrmproc_again:
325 /* there might be some code that just jumps to RMCBs or the like,
326 in which case following the jumps here might get us to a shortcut */
327 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
328 switch (*code) {
329 case 0xe9: /* JMP NEAR */
330 EIP_reg(context) += 3 + *(WORD *)(code+1);
331 /* yeah, I know these gotos don't look good... */
332 goto callrmproc_again;
333 case 0xea: /* JMP FAR */
334 EIP_reg(context) = *(WORD *)(code+1);
335 CS_reg(context) = *(WORD *)(code+3);
336 /* ...but since the label is there anyway... */
337 goto callrmproc_again;
338 case 0xeb: /* JMP SHORT */
339 EIP_reg(context) += 2 + *(signed char *)(code+1);
340 /* ...because of other gotos below, so... */
341 goto callrmproc_again;
344 /* shortcut for chaining to internal interrupt handlers */
345 if ((CS_reg(context) == 0xF000) && iret) {
346 return INT_RealModeInterrupt( LOWORD(EIP_reg(context))/4, context);
349 /* shortcut for RMCBs */
350 CurrRMCB = FirstRMCB;
352 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
353 CurrRMCB = CurrRMCB->next;
355 if (!(CurrRMCB || lpDosTask)) {
356 #ifdef MZ_SUPPORTED
357 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
358 TRACE("creating VM86 task\n");
359 if (!MZ_InitTask( lpDosTask = MZ_AllocDPMITask() )) {
360 ERR("could not setup VM86 task\n");
361 return 1;
363 #else
364 ERR("Actual real-mode calls not supported on this architecture!\n");
365 return 1;
366 #endif
368 if (!already) {
369 if (!SS_reg(context)) {
370 alloc = 1; /* allocate default stack */
371 stack16 = addr = DOSMEM_GetBlock( 64, (UINT16 *)&(SS_reg(context)) );
372 ESP_reg(context) = 64-2;
373 stack16 += 32-1;
374 if (!addr) {
375 ERR("could not allocate default stack\n");
376 return 1;
378 } else {
379 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
381 ESP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
382 stack16 -= args;
383 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
384 /* push flags if iret */
385 if (iret) {
386 stack16--; args++;
387 *stack16 = LOWORD(EFL_reg(context));
389 /* push return address (return to interrupt wrapper) */
390 *(--stack16) = DPMI_wrap_seg;
391 *(--stack16) = 0;
392 /* adjust stack */
393 ESP_reg(context) -= 2*sizeof(WORD);
394 already = 1;
397 if (CurrRMCB) {
398 /* RMCB call, invoke protected-mode handler directly */
399 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask ? lpDosTask->dpmi_flag : 0);
400 /* check if we returned to where we thought we would */
401 if ((CS_reg(context) != DPMI_wrap_seg) ||
402 (LOWORD(EIP_reg(context)) != 0)) {
403 /* we need to continue at different address in real-mode space,
404 so we need to set it all up for real mode again */
405 goto callrmproc_again;
407 } else {
408 #ifdef MZ_SUPPORTED
409 TRACE("entering real mode...\n");
410 DOSVM_Enter( context );
411 TRACE("returned from real-mode call\n");
412 #else
413 /* we should never get here, but... */
414 ERR("cannot perform real-mode call\n");
415 #endif
417 if (alloc) DOSMEM_FreeBlock( addr );
418 return 0;
422 /**********************************************************************
423 * CallRMInt
425 static void CallRMInt( CONTEXT86 *context )
427 CONTEXT86 realmode_ctx;
428 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
429 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
430 DI_reg(context) );
431 INT_GetRealModeContext( call, &realmode_ctx );
433 /* we need to check if a real-mode program has hooked the interrupt */
434 if (HIWORD(rm_int)!=0xF000) {
435 /* yup, which means we need to switch to real mode... */
436 CS_reg(&realmode_ctx) = HIWORD(rm_int);
437 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
438 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
439 SET_CFLAG(context);
440 } else {
441 RESET_CFLAG(context);
442 /* use the IP we have instead of BL_reg, in case some apps
443 decide to move interrupts around for whatever reason... */
444 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
445 SET_CFLAG(context);
446 if (EFL_reg(context)&1) {
447 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
448 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
449 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
450 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
451 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
452 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
455 INT_SetRealModeContext( call, &realmode_ctx );
459 static void CallRMProc( CONTEXT86 *context, int iret )
461 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
462 CONTEXT86 context16;
464 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
465 p->eax, p->ebx, p->ecx, p->edx);
466 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
467 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
469 if (!(p->cs) && !(p->ip)) { /* remove this check
470 if Int21/6501 case map function
471 has been implemented */
472 SET_CFLAG(context);
473 return;
475 INT_GetRealModeContext(p, &context16);
476 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context))))+3,
477 CX_reg(context), iret );
478 INT_SetRealModeContext(p, &context16);
482 static RMCB *DPMI_AllocRMCB( void )
484 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
485 UINT16 uParagraph;
487 if (NewRMCB)
489 LPVOID RMCBmem = DOSMEM_GetBlock(4, &uParagraph);
490 LPBYTE p = RMCBmem;
492 *p++ = 0xcd; /* RMCB: */
493 *p++ = 0x31; /* int $0x31 */
494 /* it is the called procedure's task to change the return CS:EIP
495 the DPMI 0.9 spec states that if it doesn't, it will be called again */
496 *p++ = 0xeb;
497 *p++ = 0xfc; /* jmp RMCB */
498 NewRMCB->address = MAKELONG(0, uParagraph);
499 NewRMCB->next = FirstRMCB;
500 FirstRMCB = NewRMCB;
502 return NewRMCB;
506 static void AllocRMCB( CONTEXT86 *context )
508 RMCB *NewRMCB = DPMI_AllocRMCB();
510 TRACE("Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
512 if (NewRMCB)
514 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
515 NewRMCB->proc_ofs = SI_reg(context);
516 NewRMCB->proc_sel = DS_reg(context);
517 NewRMCB->regs_ofs = DI_reg(context);
518 NewRMCB->regs_sel = ES_reg(context);
519 SET_LOWORD( ECX_reg(context), HIWORD(NewRMCB->address) );
520 SET_LOWORD( EDX_reg(context), LOWORD(NewRMCB->address) );
522 else
524 SET_LOWORD( EAX_reg(context), 0x8015 ); /* callback unavailable */
525 SET_CFLAG(context);
530 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
532 RMCB *NewRMCB = DPMI_AllocRMCB();
534 if (NewRMCB) {
535 NewRMCB->proc_ofs = (DWORD)proc;
536 NewRMCB->proc_sel = 0;
537 NewRMCB->regs_ofs = 0;
538 NewRMCB->regs_sel = 0;
539 return (FARPROC16)(NewRMCB->address);
541 return NULL;
545 static int DPMI_FreeRMCB( DWORD address )
547 RMCB *CurrRMCB = FirstRMCB;
548 RMCB *PrevRMCB = NULL;
550 while (CurrRMCB && (CurrRMCB->address != address))
552 PrevRMCB = CurrRMCB;
553 CurrRMCB = CurrRMCB->next;
555 if (CurrRMCB)
557 if (PrevRMCB)
558 PrevRMCB->next = CurrRMCB->next;
559 else
560 FirstRMCB = CurrRMCB->next;
561 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(CurrRMCB->address));
562 HeapFree(GetProcessHeap(), 0, CurrRMCB);
563 return 0;
565 return 1;
569 static void FreeRMCB( CONTEXT86 *context )
571 FIXME("callback address: %04x:%04x\n",
572 CX_reg(context), DX_reg(context));
574 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
575 SET_LOWORD( EAX_reg(context), 0x8024 ); /* invalid callback address */
576 SET_CFLAG(context);
581 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
583 DPMI_FreeRMCB( (DWORD)proc );
587 #ifdef MZ_SUPPORTED
588 /* (see loader/dos/module.c, function MZ_InitDPMI) */
590 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
592 char *base = DOSMEM_MemoryBase();
593 UINT16 cs, ss, ds, es;
594 CONTEXT86 pm_ctx;
595 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
596 PDB16 *psp = (PDB16 *)(base + psp_ofs);
597 HANDLE16 env_seg = psp->environment;
598 int is32;
600 RESET_CFLAG(context);
601 lpDosTask->dpmi_flag = AX_reg(context);
602 is32 = lpDosTask->dpmi_flag & 1;
603 /* our mode switch wrapper have placed the desired CS into DX */
604 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
605 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
606 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
607 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
608 32-bit code using this stack. */
609 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
610 /* do the same for the data segments, just in case */
611 if (DS_reg(context) == SS_reg(context)) ds = ss;
612 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
613 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
614 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
615 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
616 0x10000, SEGMENT_DATA, FALSE, FALSE );
618 pm_ctx = *context;
619 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
620 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
621 EAX_reg(&pm_ctx) = ss;
622 EDX_reg(&pm_ctx) = cs;
623 DS_reg(&pm_ctx) = ds;
624 ES_reg(&pm_ctx) = es;
625 FS_reg(&pm_ctx) = 0;
626 GS_reg(&pm_ctx) = 0;
628 TRACE("DOS program is now entering protected mode\n");
629 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
631 /* in the current state of affairs, we won't ever actually return here... */
632 /* we should have int21/ah=4c do it someday, though... */
634 SELECTOR_FreeBlock(psp->environment, 1);
635 psp->environment = env_seg;
636 SELECTOR_FreeBlock(es, 1);
637 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
638 SELECTOR_FreeBlock(ss, 1);
639 SELECTOR_FreeBlock(cs, 1);
642 /* DPMI Raw Mode Switch handler */
644 #if 0
645 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
647 LPDOSTASK lpDosTask = MZ_Current();
648 CONTEXT86 rm_ctx;
649 int ret;
651 if (!lpDosTask) {
652 /* we could probably start a DPMI-only dosmod task here, but I doubt
653 anything other than real DOS apps want to call raw mode switch */
654 ERR("attempting raw mode switch without DOS task!\n");
655 ExitProcess(1);
657 /* initialize real-mode context as per spec */
658 memset(&rm_ctx, 0, sizeof(rm_ctx));
659 DS_reg(&rm_ctx) = AX_sig(context);
660 ES_reg(&rm_ctx) = CX_sig(context);
661 SS_reg(&rm_ctx) = DX_sig(context);
662 ESP_reg(&rm_ctx) = EBX_sig(context);
663 CS_reg(&rm_ctx) = SI_sig(context);
664 EIP_reg(&rm_ctx) = EDI_sig(context);
665 EBP_reg(&rm_ctx) = EBP_sig(context);
666 FS_reg(&rm_ctx) = 0;
667 GS_reg(&rm_ctx) = 0;
668 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
670 /* enter real mode again */
671 TRACE("re-entering real mode at %04lx:%04lx\n",
672 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
673 ret = DOSVM_Enter( &rm_ctx );
674 /* when the real-mode stuff call its mode switch address,
675 DOSVM_Enter will return and we will continue here */
677 if (ret<0) {
678 /* if the sync was lost, there's no way to recover */
679 ExitProcess(1);
682 /* alter protected-mode context as per spec */
683 DS_sig(context) = AX_reg(&rm_ctx);
684 ES_sig(context) = CX_reg(&rm_ctx);
685 SS_sig(context) = DX_reg(&rm_ctx);
686 ESP_sig(context) = EBX_reg(&rm_ctx);
687 CS_sig(context) = SI_reg(&rm_ctx);
688 EIP_sig(context) = EDI_reg(&rm_ctx);
689 EBP_sig(context) = EBP_reg(&rm_ctx);
690 FS_sig(context) = 0;
691 GS_sig(context) = 0;
693 /* Return to new address and hope that we didn't mess up */
694 TRACE("re-entering protected mode at %04x:%08lx\n",
695 CS_sig(context), EIP_sig(context));
697 #endif
699 #else
700 #if 0
701 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
703 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
704 ExitProcess(1);
706 #endif
707 #endif
709 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
710 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
711 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
712 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
713 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
714 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
715 #define DOS_BADLIMIT(addr,base,limit) \
716 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
717 (DOS_WINE_ISDOS(addr,base) && \
718 ((addr) + (limit) > (base) + 0x110000)))
720 /**********************************************************************
721 * INT_Int31Handler
723 * Handler for int 31h (DPMI).
726 void WINAPI INT_Int31Handler( CONTEXT86 *context )
729 * Note: For Win32s processes, the whole linear address space is
730 * shifted by 0x10000 relative to the OS linear address space.
731 * See the comment in msdos/vxd.c.
733 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
735 DWORD dw;
736 BYTE *ptr;
738 LPDOSTASK lpDosTask = MZ_Current();
740 #ifdef MZ_SUPPORTED
741 if (ISV86(context) && lpDosTask) {
742 /* Called from real mode, check if it's our wrapper */
743 TRACE("called from real mode\n");
744 if (CS_reg(context)==lpDosTask->dpmi_seg) {
745 /* This is the protected mode switch */
746 StartPM(context,lpDosTask);
747 return;
748 } else
749 if (CS_reg(context)==lpDosTask->xms_seg) {
750 /* This is the XMS driver entry point */
751 XMS_Handler(context);
752 return;
753 } else
755 /* Check for RMCB */
756 RMCB *CurrRMCB = FirstRMCB;
758 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
759 CurrRMCB = CurrRMCB->next;
761 if (CurrRMCB) {
762 /* RMCB call, propagate to protected-mode handler */
763 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
764 return;
768 #endif
770 RESET_CFLAG(context);
771 switch(AX_reg(context))
773 case 0x0000: /* Allocate LDT descriptors */
774 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
775 if (!(EAX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
777 TRACE("failed\n");
778 EAX_reg(context) = 0x8011; /* descriptor unavailable */
779 SET_CFLAG(context);
781 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
782 break;
784 case 0x0001: /* Free LDT descriptor */
785 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
786 if (FreeSelector16( BX_reg(context) ))
788 EAX_reg(context) = 0x8022; /* invalid selector */
789 SET_CFLAG(context);
791 else
793 /* If a segment register contains the selector being freed, */
794 /* set it to zero. */
795 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
796 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
797 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
798 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
800 break;
802 case 0x0002: /* Real mode segment to descriptor */
803 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
805 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
806 switch(BX_reg(context))
808 case 0x0000: entryPoint = 183; break; /* __0000H */
809 case 0x0040: entryPoint = 193; break; /* __0040H */
810 case 0xa000: entryPoint = 174; break; /* __A000H */
811 case 0xb000: entryPoint = 181; break; /* __B000H */
812 case 0xb800: entryPoint = 182; break; /* __B800H */
813 case 0xc000: entryPoint = 195; break; /* __C000H */
814 case 0xd000: entryPoint = 179; break; /* __D000H */
815 case 0xe000: entryPoint = 190; break; /* __E000H */
816 case 0xf000: entryPoint = 194; break; /* __F000H */
817 default:
818 EAX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
819 break;
821 if (entryPoint)
822 EAX_reg(context) = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
823 entryPoint ));
825 break;
827 case 0x0003: /* Get next selector increment */
828 TRACE("get selector increment (__AHINCR)\n");
829 EAX_reg(context) = __AHINCR;
830 break;
832 case 0x0004: /* Lock selector (not supported) */
833 FIXME("lock selector not supported\n");
834 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
835 break;
837 case 0x0005: /* Unlock selector (not supported) */
838 FIXME("unlock selector not supported\n");
839 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
840 break;
842 case 0x0006: /* Get selector base address */
843 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
844 if (!(dw = GetSelectorBase( BX_reg(context) )))
846 EAX_reg(context) = 0x8022; /* invalid selector */
847 SET_CFLAG(context);
849 else
851 #ifdef MZ_SUPPORTED
852 if (lpDosTask) {
853 DWORD base = (DWORD)DOSMEM_MemoryBase();
854 dw = DOS_WINETOAPP(dw, base);
856 #endif
857 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
858 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
860 break;
862 case 0x0007: /* Set selector base address */
863 TRACE("set selector base address (0x%04x,0x%08lx)\n",
864 BX_reg(context),
865 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
866 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
867 #ifdef MZ_SUPPORTED
868 if (lpDosTask) {
869 DWORD base = (DWORD)DOSMEM_MemoryBase();
870 dw = DOS_APPTOWINE(dw, base);
872 #endif
873 SetSelectorBase(BX_reg(context), dw);
874 break;
876 case 0x0008: /* Set selector limit */
877 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
878 dw = MAKELONG( DX_reg(context), CX_reg(context) );
879 #ifdef MZ_SUPPORTED
880 if (lpDosTask) {
881 DWORD base = (DWORD)DOSMEM_MemoryBase();
882 DWORD sbase = GetSelectorBase( BX_reg(context) );
883 if (!sbase) {
884 /* the app has set the limit without setting the base,
885 * it must be relying on that the default should be DOS space;
886 * so set the base address now */
887 SetSelectorBase( BX_reg(context), sbase = base );
888 if (dw == 0xffffffff) {
889 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
890 * so we have to override the limit here */
891 dw = 0x110000;
894 if (DOS_BADLIMIT(sbase, base, dw)) {
895 AX_reg(context) = 0x8021; /* invalid value */
896 SET_CFLAG(context);
897 break;
900 #endif
901 SetSelectorLimit16( BX_reg(context), dw );
902 break;
904 case 0x0009: /* Set selector access rights */
905 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
906 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
907 break;
909 case 0x000a: /* Allocate selector alias */
910 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
911 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
913 AX_reg(context) = 0x8011; /* descriptor unavailable */
914 SET_CFLAG(context);
916 break;
918 case 0x000b: /* Get descriptor */
919 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
921 ldt_entry entry;
922 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
923 #ifdef MZ_SUPPORTED
924 if (lpDosTask) {
925 DWORD base = (DWORD)DOSMEM_MemoryBase();
926 entry.base = DOS_WINETOAPP(entry.base, base);
928 #endif
929 entry.base = W32S_WINE2APP(entry.base, offset);
931 /* FIXME: should use ES:EDI for 32-bit clients */
932 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
933 DI_reg(context) ), &entry );
935 break;
937 case 0x000c: /* Set descriptor */
938 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
940 ldt_entry entry;
941 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
942 DI_reg(context) ), &entry );
943 entry.base = W32S_APP2WINE(entry.base, offset);
944 #ifdef MZ_SUPPORTED
945 if (lpDosTask) {
946 DWORD base = (DWORD)DOSMEM_MemoryBase();
947 entry.base = DOS_APPTOWINE(entry.base, base);
948 if (DOS_BADLIMIT(entry.base, base, entry.limit)) {
949 AX_reg(context) = 0x8021; /* invalid value */
950 SET_CFLAG(context);
951 break;
954 #endif
956 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
958 break;
960 case 0x000d: /* Allocate specific LDT descriptor */
961 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
962 AX_reg(context) = 0x8011; /* descriptor unavailable */
963 SET_CFLAG(context);
964 break;
965 case 0x0100: /* Allocate DOS memory block */
966 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
967 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
968 if (dw) {
969 AX_reg(context) = HIWORD(dw);
970 DX_reg(context) = LOWORD(dw);
971 } else {
972 AX_reg(context) = 0x0008; /* insufficient memory */
973 BX_reg(context) = DOSMEM_Available()>>4;
974 SET_CFLAG(context);
976 break;
977 case 0x0101: /* Free DOS memory block */
978 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
979 dw = GlobalDOSFree16(DX_reg(context));
980 if (!dw) {
981 AX_reg(context) = 0x0009; /* memory block address invalid */
982 SET_CFLAG(context);
984 break;
985 case 0x0200: /* get real mode interrupt vector */
986 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
987 BL_reg(context));
988 SET_CFLAG(context);
989 break;
990 case 0x0201: /* set real mode interrupt vector */
991 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
992 SET_CFLAG(context);
993 break;
994 case 0x0204: /* Get protected mode interrupt vector */
995 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
996 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
997 CX_reg(context) = HIWORD(dw);
998 DX_reg(context) = LOWORD(dw);
999 break;
1001 case 0x0205: /* Set protected mode interrupt vector */
1002 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
1003 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
1004 INT_SetPMHandler( BL_reg(context),
1005 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
1006 DX_reg(context) ));
1007 break;
1009 case 0x0300: /* Simulate real mode interrupt */
1010 CallRMInt( context );
1011 break;
1013 case 0x0301: /* Call real mode procedure with far return */
1014 CallRMProc( context, FALSE );
1015 break;
1017 case 0x0302: /* Call real mode procedure with interrupt return */
1018 CallRMProc( context, TRUE );
1019 break;
1021 case 0x0303: /* Allocate Real Mode Callback Address */
1022 AllocRMCB( context );
1023 break;
1025 case 0x0304: /* Free Real Mode Callback Address */
1026 FreeRMCB( context );
1027 break;
1029 case 0x0305: /* Get State Save/Restore Addresses */
1030 TRACE("get state save/restore addresses\n");
1031 /* we probably won't need this kind of state saving */
1032 AX_reg(context) = 0;
1033 /* real mode: just point to the lret */
1034 BX_reg(context) = DPMI_wrap_seg;
1035 ECX_reg(context) = 2;
1036 /* protected mode: don't have any handler yet... */
1037 FIXME("no protected-mode dummy state save/restore handler yet\n");
1038 SI_reg(context) = 0;
1039 EDI_reg(context) = 0;
1040 break;
1042 case 0x0306: /* Get Raw Mode Switch Addresses */
1043 TRACE("get raw mode switch addresses\n");
1044 if (lpDosTask) {
1045 /* real mode, point to standard DPMI return wrapper */
1046 BX_reg(context) = DPMI_wrap_seg;
1047 ECX_reg(context) = 0;
1048 /* protected mode, point to DPMI call wrapper */
1049 SI_reg(context) = lpDosTask->dpmi_sel;
1050 EDI_reg(context) = 8; /* offset of the INT 0x31 call */
1051 } else {
1052 ERR("win app attempting to get raw mode switch!\n");
1053 AX_reg(context) = 0x8001; /* unsupported function */
1054 SET_CFLAG(context);
1056 break;
1057 case 0x0400: /* Get DPMI version */
1058 TRACE("get DPMI version\n");
1060 SYSTEM_INFO si;
1062 GetSystemInfo(&si);
1063 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
1064 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
1065 CL_reg(context) = si.wProcessorLevel;
1066 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
1067 break;
1069 case 0x0500: /* Get free memory information */
1070 TRACE("get free memory information\n");
1072 MEMMANINFO mmi;
1074 mmi.dwSize = sizeof(mmi);
1075 MemManInfo16(&mmi);
1076 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1077 /* the layout is just the same as MEMMANINFO, but without
1078 * the dwSize entry.
1080 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1081 break;
1083 case 0x0501: /* Allocate memory block */
1084 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1085 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1087 AX_reg(context) = 0x8012; /* linear memory not available */
1088 SET_CFLAG(context);
1089 } else {
1090 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1091 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1093 break;
1095 case 0x0502: /* Free memory block */
1096 TRACE("free memory block (0x%08lx)\n",
1097 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1098 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1099 SI_reg(context)), offset) );
1100 break;
1102 case 0x0503: /* Resize memory block */
1103 TRACE("resize memory block (0x%08lx,%ld)\n",
1104 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1105 MAKELONG(CX_reg(context),BX_reg(context)));
1106 if (!(ptr = (BYTE *)DPMI_xrealloc(
1107 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1108 MAKELONG(CX_reg(context),BX_reg(context)))))
1110 AX_reg(context) = 0x8012; /* linear memory not available */
1111 SET_CFLAG(context);
1112 } else {
1113 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1114 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1116 break;
1118 case 0x0507: /* Modify page attributes */
1119 FIXME("modify page attributes unimplemented\n");
1120 break; /* Just ignore it */
1122 case 0x0600: /* Lock linear region */
1123 FIXME("lock linear region unimplemented\n");
1124 break; /* Just ignore it */
1126 case 0x0601: /* Unlock linear region */
1127 FIXME("unlock linear region unimplemented\n");
1128 break; /* Just ignore it */
1130 case 0x0602: /* Unlock real-mode region */
1131 FIXME("unlock realmode region unimplemented\n");
1132 break; /* Just ignore it */
1134 case 0x0603: /* Lock real-mode region */
1135 FIXME("lock realmode region unimplemented\n");
1136 break; /* Just ignore it */
1138 case 0x0604: /* Get page size */
1139 TRACE("get pagesize\n");
1140 BX_reg(context) = 0;
1141 CX_reg(context) = VIRTUAL_GetPageSize();
1142 break;
1144 case 0x0702: /* Mark page as demand-paging candidate */
1145 FIXME("mark page as demand-paging candidate\n");
1146 break; /* Just ignore it */
1148 case 0x0703: /* Discard page contents */
1149 FIXME("discard page contents\n");
1150 break; /* Just ignore it */
1152 case 0x0800: /* Physical address mapping */
1153 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1154 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1156 AX_reg(context) = 0x8021;
1157 SET_CFLAG(context);
1159 else
1161 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1162 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1163 RESET_CFLAG(context);
1165 break;
1167 default:
1168 INT_BARF( context, 0x31 );
1169 AX_reg(context) = 0x8001; /* unsupported function */
1170 SET_CFLAG(context);
1171 break;