Added ListView_EditLabel macros.
[wine/multimedia.git] / msdos / dpmi.c
blobe5c19d468897e77c291ac7cd773c22b1e395166f
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 "module.h"
14 #include "miscemu.h"
15 #include "msdos.h"
16 #include "task.h"
17 #include "thread.h" /* for !MZ_SUPPORTED */
18 #include "stackframe.h" /* for !MZ_SUPPORTED */
19 #include "toolhelp.h"
20 #include "selectors.h"
21 #include "process.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;
66 UINT16 DPMI_wrap_seg;
68 /**********************************************************************
69 * DPMI_xalloc
70 * special virtualalloc, allocates lineary monoton growing memory.
71 * (the usual VirtualAlloc does not satisfy that restriction)
73 static LPVOID
74 DPMI_xalloc(int len) {
75 LPVOID ret;
76 LPVOID oldlastv = lastvalloced;
78 if (lastvalloced) {
79 int xflag = 0;
80 ret = NULL;
81 while (!ret) {
82 ret=VirtualAlloc(lastvalloced,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
83 if (!ret)
84 lastvalloced = (char *) lastvalloced + 0x10000;
85 /* we failed to allocate one in the first round.
86 * try non-linear
88 if (!xflag && (lastvalloced<oldlastv)) { /* wrapped */
89 FIXME("failed to allocate lineary growing memory (%d bytes), using non-linear growing...\n",len);
90 xflag++;
92 /* if we even fail to allocate something in the next
93 * round, return NULL
95 if ((xflag==1) && (lastvalloced >= oldlastv))
96 xflag++;
97 if ((xflag==2) && (lastvalloced < oldlastv)) {
98 FIXME("failed to allocate any memory of %d bytes!\n",len);
99 return NULL;
102 } else
103 ret=VirtualAlloc(NULL,len,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
104 lastvalloced = (LPVOID)(((DWORD)ret+len+0xffff)&~0xffff);
105 return ret;
108 static void
109 DPMI_xfree(LPVOID ptr) {
110 VirtualFree(ptr,0,MEM_RELEASE);
113 /* FIXME: perhaps we could grow this mapped area... */
114 static LPVOID
115 DPMI_xrealloc(LPVOID ptr,int newsize) {
116 MEMORY_BASIC_INFORMATION mbi;
117 LPVOID newptr;
119 newptr = DPMI_xalloc(newsize);
120 if (ptr) {
121 if (!VirtualQuery(ptr,&mbi,sizeof(mbi))) {
122 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
123 return NULL;
125 if (mbi.State == MEM_FREE) {
126 FIXME("realloc of DPMI_xallocd region %p?\n",ptr);
127 return NULL;
129 /* We do not shrink allocated memory. most reallocs
130 * only do grows anyway
132 if (newsize<=mbi.RegionSize)
133 return ptr;
134 memcpy(newptr,ptr,mbi.RegionSize);
135 DPMI_xfree(ptr);
137 return newptr;
139 /**********************************************************************
140 * INT_GetRealModeContext
142 static void INT_GetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
144 EAX_reg(context) = call->eax;
145 EBX_reg(context) = call->ebx;
146 ECX_reg(context) = call->ecx;
147 EDX_reg(context) = call->edx;
148 ESI_reg(context) = call->esi;
149 EDI_reg(context) = call->edi;
150 EBP_reg(context) = call->ebp;
151 EFL_reg(context) = call->fl | V86_FLAG;
152 EIP_reg(context) = call->ip;
153 ESP_reg(context) = call->sp;
154 CS_reg(context) = call->cs;
155 DS_reg(context) = call->ds;
156 ES_reg(context) = call->es;
157 FS_reg(context) = call->fs;
158 GS_reg(context) = call->gs;
159 SS_reg(context) = call->ss;
160 V86BASE(context) = (DWORD) DOSMEM_MemoryBase(0);
164 /**********************************************************************
165 * INT_SetRealModeContext
167 static void INT_SetRealModeContext( REALMODECALL *call, CONTEXT86 *context )
169 call->eax = EAX_reg(context);
170 call->ebx = EBX_reg(context);
171 call->ecx = ECX_reg(context);
172 call->edx = EDX_reg(context);
173 call->esi = ESI_reg(context);
174 call->edi = EDI_reg(context);
175 call->ebp = EBP_reg(context);
176 call->fl = LOWORD(EFL_reg(context));
177 call->ip = LOWORD(EIP_reg(context));
178 call->sp = LOWORD(ESP_reg(context));
179 call->cs = CS_reg(context);
180 call->ds = DS_reg(context);
181 call->es = ES_reg(context);
182 call->fs = FS_reg(context);
183 call->gs = GS_reg(context);
184 call->ss = SS_reg(context);
188 /**********************************************************************
189 * DPMI_CallRMCBProc
191 * This routine does the hard work of calling a callback procedure.
193 static void DPMI_CallRMCBProc( CONTEXT86 *context, RMCB *rmcb, WORD flag )
195 if (IS_SELECTOR_SYSTEM( rmcb->proc_sel )) {
196 /* Wine-internal RMCB, call directly */
197 ((RMCBPROC)rmcb->proc_ofs)(context);
198 } else {
199 #ifdef __i386__
200 UINT16 ss,es;
201 DWORD edi;
203 INT_SetRealModeContext((REALMODECALL *)PTR_SEG_OFF_TO_LIN( rmcb->regs_sel, rmcb->regs_ofs ), context);
204 ss = SELECTOR_AllocBlock( DOSMEM_MemoryBase(0) + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, FALSE, FALSE );
206 FIXME("untested!\n");
208 /* The called proc ends with an IRET, and takes these parameters:
209 * DS:ESI = pointer to real-mode SS:SP
210 * ES:EDI = pointer to real-mode call structure
211 * It returns:
212 * ES:EDI = pointer to real-mode call structure (may be a copy)
213 * It is the proc's responsibility to change the return CS:IP in the
214 * real-mode call structure. */
215 if (flag & 1) {
216 int _clobber;
217 /* 32-bit DPMI client */
218 __asm__ __volatile__(
219 "pushl %%es\n"
220 "pushl %%ds\n"
221 "pushfl\n"
222 "movl %5,%%es\n"
223 "movl %4,%%ds\n"
224 "lcall %3\n"
225 "popl %%ds\n"
226 "movl %%es,%0\n"
227 "popl %%es\n"
228 : "=g" (es), "=D" (edi), "=S" (_clobber)
229 : "m" (rmcb->proc_ofs),
230 "g" (ss), "g" (rmcb->regs_sel),
231 "S" (ESP_reg(context)), "1" (rmcb->regs_ofs)
232 : "ecx", "edx", "ebp" );
233 } else {
234 /* 16-bit DPMI client */
235 CONTEXT86 ctx = *context;
236 CS_reg(&ctx) = rmcb->proc_sel;
237 EIP_reg(&ctx) = rmcb->proc_ofs;
238 DS_reg(&ctx) = ss;
239 ESI_reg(&ctx) = ESP_reg(context);
240 ES_reg(&ctx) = rmcb->regs_sel;
241 EDI_reg(&ctx) = rmcb->regs_ofs;
242 Callbacks->CallRegisterShortProc(&ctx, 2);
243 es = ES_reg(&ctx);
244 edi = EDI_reg(&ctx);
246 SELECTOR_FreeBlock(ss, 1);
247 INT_GetRealModeContext((REALMODECALL*)PTR_SEG_OFF_TO_LIN( es, edi ), context);
248 #else
249 ERR("RMCBs only implemented for i386\n");
250 #endif
255 /**********************************************************************
256 * DPMI_CallRMProc
258 * This routine does the hard work of calling a real mode procedure.
260 int DPMI_CallRMProc( CONTEXT86 *context, LPWORD stack, int args, int iret )
262 LPWORD stack16;
263 #ifndef MZ_SUPPORTED
264 WORD sel;
265 SEGPTR seg_addr;
266 #endif /* !MZ_SUPPORTED */
267 LPVOID addr = NULL; /* avoid gcc warning */
268 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
269 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
270 RMCB *CurrRMCB;
271 int alloc = 0, already = 0;
272 BYTE *code;
274 GlobalUnlock16( GetCurrentTask() );
276 TRACE("EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
277 EAX_reg(context), EBX_reg(context), ECX_reg(context), EDX_reg(context) );
278 TRACE("ESI=%08lx EDI=%08lx ES=%04lx DS=%04lx CS:IP=%04lx:%04x, %d WORD arguments, %s\n",
279 ESI_reg(context), EDI_reg(context), ES_reg(context), DS_reg(context),
280 CS_reg(context), LOWORD(EIP_reg(context)), args, iret?"IRET":"FAR" );
282 callrmproc_again:
284 /* there might be some code that just jumps to RMCBs or the like,
285 in which case following the jumps here might get us to a shortcut */
286 code = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
287 switch (*code) {
288 case 0xe9: /* JMP NEAR */
289 EIP_reg(context) += 3 + *(WORD *)(code+1);
290 /* yeah, I know these gotos don't look good... */
291 goto callrmproc_again;
292 case 0xea: /* JMP FAR */
293 EIP_reg(context) = *(WORD *)(code+1);
294 CS_reg(context) = *(WORD *)(code+3);
295 /* ...but since the label is there anyway... */
296 goto callrmproc_again;
297 case 0xeb: /* JMP SHORT */
298 EIP_reg(context) += 2 + *(signed char *)(code+1);
299 /* ...because of other gotos below, so... */
300 goto callrmproc_again;
303 /* shortcut for chaining to internal interrupt handlers */
304 if ((CS_reg(context) == 0xF000) && iret) {
305 return INT_RealModeInterrupt( LOWORD(EIP_reg(context))/4, context);
308 /* shortcut for RMCBs */
309 CurrRMCB = FirstRMCB;
311 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
312 CurrRMCB = CurrRMCB->next;
314 #ifdef MZ_SUPPORTED
315 if (!(CurrRMCB || pModule->lpDosTask)) {
316 FIXME("DPMI real-mode call using DOS VM task system, not fully tested!\n");
317 TRACE("creating VM86 task\n");
318 if (!MZ_InitTask( MZ_AllocDPMITask( pModule->self ) )) {
319 ERR("could not setup VM86 task\n");
320 return 1;
323 #endif
324 if (!already) {
325 #ifdef MZ_SUPPORTED
326 if (!SS_reg(context)) {
327 alloc = 1; /* allocate default stack */
328 stack16 = addr = DOSMEM_GetBlock( pModule->self, 64, (UINT16 *)&(SS_reg(context)) );
329 ESP_reg(context) = 64-2;
330 stack16 += 32-1;
331 if (!addr) {
332 ERR("could not allocate default stack\n");
333 return 1;
335 } else {
336 stack16 = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
338 ESP_reg(context) -= (args + (iret?1:0)) * sizeof(WORD);
339 #else
340 stack16 = (LPWORD) CURRENT_STACK16;
341 #endif
342 stack16 -= args;
343 if (args) memcpy(stack16, stack, args*sizeof(WORD) );
344 /* push flags if iret */
345 if (iret) {
346 stack16--; args++;
347 *stack16 = LOWORD(EFL_reg(context));
349 #ifdef MZ_SUPPORTED
350 /* push return address (return to interrupt wrapper) */
351 *(--stack16) = DPMI_wrap_seg;
352 *(--stack16) = 0;
353 /* adjust stack */
354 ESP_reg(context) -= 2*sizeof(WORD);
355 #endif
356 already = 1;
359 if (CurrRMCB) {
360 /* RMCB call, invoke protected-mode handler directly */
361 DPMI_CallRMCBProc(context, CurrRMCB, pModule->lpDosTask?pModule->lpDosTask->dpmi_flag:0);
362 /* check if we returned to where we thought we would */
363 if ((CS_reg(context) != DPMI_wrap_seg) ||
364 (LOWORD(EIP_reg(context)) != 0)) {
365 /* we need to continue at different address in real-mode space,
366 so we need to set it all up for real mode again */
367 goto callrmproc_again;
369 } else {
370 #ifdef MZ_SUPPORTED
371 #if 0 /* this was probably unnecessary */
372 /* push call address */
373 *(--stack16) = CS_reg(context);
374 *(--stack16) = LOWORD(EIP_reg(context));
375 /* adjust stack */
376 ESP_reg(context) -= 2*sizeof(WORD);
377 /* set initial CS:IP to the wrapper's "lret" */
378 CS_reg(context) = DPMI_wrap_seg;
379 EIP_reg(context) = 2;
380 #endif
381 TRACE("entering real mode...\n");
382 DOSVM_Enter( context );
383 TRACE("returned from real-mode call\n");
384 #else
385 addr = CTX_SEG_OFF_TO_LIN(context, CS_reg(context), EIP_reg(context));
386 sel = SELECTOR_AllocBlock( addr, 0x10000, SEGMENT_CODE, FALSE, FALSE );
387 seg_addr = PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
389 CS_reg(context) = HIWORD(seg_addr);
390 EIP_reg(context) = LOWORD(seg_addr);
391 EBP_reg(context) = OFFSETOF( NtCurrentTeb()->cur_stack )
392 + (WORD)&((STACK16FRAME*)0)->bp;
393 Callbacks->CallRegisterShortProc(context, args*sizeof(WORD));
394 SELECTOR_FreeBlock(sel, 1);
395 #endif
397 if (alloc) DOSMEM_FreeBlock( pModule->self, addr );
398 return 0;
402 /**********************************************************************
403 * CallRMInt
405 static void CallRMInt( CONTEXT86 *context )
407 CONTEXT86 realmode_ctx;
408 FARPROC16 rm_int = INT_GetRMHandler( BL_reg(context) );
409 REALMODECALL *call = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context),
410 DI_reg(context) );
411 INT_GetRealModeContext( call, &realmode_ctx );
413 /* we need to check if a real-mode program has hooked the interrupt */
414 if (HIWORD(rm_int)!=0xF000) {
415 /* yup, which means we need to switch to real mode... */
416 CS_reg(&realmode_ctx) = HIWORD(rm_int);
417 EIP_reg(&realmode_ctx) = LOWORD(rm_int);
418 if (DPMI_CallRMProc( &realmode_ctx, NULL, 0, TRUE))
419 SET_CFLAG(context);
420 } else {
421 RESET_CFLAG(context);
422 /* use the IP we have instead of BL_reg, in case some apps
423 decide to move interrupts around for whatever reason... */
424 if (INT_RealModeInterrupt( LOWORD(rm_int)/4, &realmode_ctx ))
425 SET_CFLAG(context);
426 if (EFL_reg(context)&1) {
427 FIXME("%02x: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
428 BL_reg(context), EAX_reg(&realmode_ctx), EBX_reg(&realmode_ctx),
429 ECX_reg(&realmode_ctx), EDX_reg(&realmode_ctx));
430 FIXME(" ESI=%08lx EDI=%08lx DS=%04lx ES=%04lx\n",
431 ESI_reg(&realmode_ctx), EDI_reg(&realmode_ctx),
432 DS_reg(&realmode_ctx), ES_reg(&realmode_ctx) );
435 INT_SetRealModeContext( call, &realmode_ctx );
439 static void CallRMProc( CONTEXT86 *context, int iret )
441 REALMODECALL *p = (REALMODECALL *)PTR_SEG_OFF_TO_LIN( ES_reg(context), DI_reg(context) );
442 CONTEXT86 context16;
444 TRACE("RealModeCall: EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n",
445 p->eax, p->ebx, p->ecx, p->edx);
446 TRACE(" ESI=%08lx EDI=%08lx ES=%04x DS=%04x CS:IP=%04x:%04x, %d WORD arguments, %s\n",
447 p->esi, p->edi, p->es, p->ds, p->cs, p->ip, CX_reg(context), iret?"IRET":"FAR" );
449 if (!(p->cs) && !(p->ip)) { /* remove this check
450 if Int21/6501 case map function
451 has been implemented */
452 SET_CFLAG(context);
453 return;
455 INT_GetRealModeContext(p, &context16);
456 DPMI_CallRMProc( &context16, ((LPWORD)PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context))))+3,
457 CX_reg(context), iret );
458 INT_SetRealModeContext(p, &context16);
462 static void WINAPI WINE_UNUSED RMCallbackProc( RMCB *rmcb )
464 /* This routine should call DPMI_CallRMCBProc, but we don't have the
465 register structure available - this is easily fixed by going through
466 a Win16 register relay instead of calling RMCallbackProc "directly",
467 but I won't bother at this time. */
468 FIXME("not properly supported on your architecture!\n");
471 static RMCB *DPMI_AllocRMCB( void )
473 RMCB *NewRMCB = HeapAlloc(GetProcessHeap(), 0, sizeof(RMCB));
474 UINT16 uParagraph;
476 if (NewRMCB)
478 #ifdef MZ_SUPPORTED
479 LPVOID RMCBmem = DOSMEM_GetBlock(0, 4, &uParagraph);
480 LPBYTE p = RMCBmem;
482 *p++ = 0xcd; /* RMCB: */
483 *p++ = 0x31; /* int $0x31 */
484 /* it is the called procedure's task to change the return CS:EIP
485 the DPMI 0.9 spec states that if it doesn't, it will be called again */
486 *p++ = 0xeb;
487 *p++ = 0xfc; /* jmp RMCB */
488 #else
489 LPVOID RMCBmem = DOSMEM_GetBlock(0, 15, &uParagraph);
490 LPBYTE p = RMCBmem;
492 *p++ = 0x68; /* pushl */
493 *(LPVOID *)p = NewRMCB;
494 p+=4;
495 *p++ = 0x9a; /* lcall */
496 *(FARPROC16 *)p = (FARPROC16)RMCallbackProc; /* FIXME: register relay */
497 p+=4;
498 GET_CS(*(WORD *)p);
499 p+=2;
500 *p++=0xc3; /* lret (FIXME?) */
501 #endif
502 NewRMCB->address = MAKELONG(0, uParagraph);
503 NewRMCB->next = FirstRMCB;
504 FirstRMCB = NewRMCB;
506 return NewRMCB;
510 static void AllocRMCB( CONTEXT86 *context )
512 RMCB *NewRMCB = DPMI_AllocRMCB();
514 TRACE("Function to call: %04x:%04x\n", (WORD)DS_reg(context), SI_reg(context) );
516 if (NewRMCB)
518 /* FIXME: if 32-bit DPMI client, use ESI and EDI */
519 NewRMCB->proc_ofs = SI_reg(context);
520 NewRMCB->proc_sel = DS_reg(context);
521 NewRMCB->regs_ofs = DI_reg(context);
522 NewRMCB->regs_sel = ES_reg(context);
523 SET_LOWORD( ECX_reg(context), HIWORD(NewRMCB->address) );
524 SET_LOWORD( EDX_reg(context), LOWORD(NewRMCB->address) );
526 else
528 SET_LOWORD( EAX_reg(context), 0x8015 ); /* callback unavailable */
529 SET_CFLAG(context);
534 FARPROC16 WINAPI DPMI_AllocInternalRMCB( RMCBPROC proc )
536 RMCB *NewRMCB = DPMI_AllocRMCB();
538 if (NewRMCB) {
539 NewRMCB->proc_ofs = (DWORD)proc;
540 NewRMCB->proc_sel = 0;
541 NewRMCB->regs_ofs = 0;
542 NewRMCB->regs_sel = 0;
543 return (FARPROC16)(NewRMCB->address);
545 return NULL;
549 static int DPMI_FreeRMCB( DWORD address )
551 RMCB *CurrRMCB = FirstRMCB;
552 RMCB *PrevRMCB = NULL;
554 while (CurrRMCB && (CurrRMCB->address != address))
556 PrevRMCB = CurrRMCB;
557 CurrRMCB = CurrRMCB->next;
559 if (CurrRMCB)
561 if (PrevRMCB)
562 PrevRMCB->next = CurrRMCB->next;
563 else
564 FirstRMCB = CurrRMCB->next;
565 DOSMEM_FreeBlock(0, DOSMEM_MapRealToLinear(CurrRMCB->address));
566 HeapFree(GetProcessHeap(), 0, CurrRMCB);
567 return 0;
569 return 1;
573 static void FreeRMCB( CONTEXT86 *context )
575 FIXME("callback address: %04x:%04x\n",
576 CX_reg(context), DX_reg(context));
578 if (DPMI_FreeRMCB(MAKELONG(DX_reg(context), CX_reg(context)))) {
579 SET_LOWORD( EAX_reg(context), 0x8024 ); /* invalid callback address */
580 SET_CFLAG(context);
585 void WINAPI DPMI_FreeInternalRMCB( FARPROC16 proc )
587 DPMI_FreeRMCB( (DWORD)proc );
591 #ifdef MZ_SUPPORTED
592 /* (see loader/dos/module.c, function MZ_InitDPMI) */
594 static void StartPM( CONTEXT86 *context, LPDOSTASK lpDosTask )
596 char *base = DOSMEM_MemoryBase(0);
597 UINT16 cs, ss, ds, es;
598 CONTEXT86 pm_ctx;
599 DWORD psp_ofs = (DWORD)(lpDosTask->psp_seg<<4);
600 PDB16 *psp = (PDB16 *)(base + psp_ofs);
601 HANDLE16 env_seg = psp->environment;
602 int is32;
604 RESET_CFLAG(context);
605 lpDosTask->dpmi_flag = AX_reg(context);
606 is32 = lpDosTask->dpmi_flag & 1;
607 /* our mode switch wrapper have placed the desired CS into DX */
608 cs = SELECTOR_AllocBlock( base + (DWORD)(DX_reg(context)<<4), 0x10000, SEGMENT_CODE, FALSE, FALSE );
609 /* due to a flaw in some CPUs (at least mine), it is best to mark stack segments as 32-bit if they
610 can be used in 32-bit code. Otherwise, these CPUs may not set the high word of esp during a
611 ring transition (from kernel code) to the 16-bit stack, and this causes trouble if executing
612 32-bit code using this stack. */
613 ss = SELECTOR_AllocBlock( base + (DWORD)(SS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
614 /* do the same for the data segments, just in case */
615 if (DS_reg(context) == SS_reg(context)) ds = ss;
616 else ds = SELECTOR_AllocBlock( base + (DWORD)(DS_reg(context)<<4), 0x10000, SEGMENT_DATA, is32, FALSE );
617 es = SELECTOR_AllocBlock( base + psp_ofs, 0x100, SEGMENT_DATA, is32, FALSE );
618 /* convert environment pointer, as the spec says, but we're a bit lazy about the size here... */
619 psp->environment = SELECTOR_AllocBlock( base + (DWORD)(env_seg<<4),
620 0x10000, SEGMENT_DATA, FALSE, FALSE );
622 pm_ctx = *context;
623 CS_reg(&pm_ctx) = lpDosTask->dpmi_sel;
624 /* our mode switch wrapper expects the new CS in DX, and the new SS in AX */
625 EAX_reg(&pm_ctx) = ss;
626 EDX_reg(&pm_ctx) = cs;
627 DS_reg(&pm_ctx) = ds;
628 ES_reg(&pm_ctx) = es;
629 FS_reg(&pm_ctx) = 0;
630 GS_reg(&pm_ctx) = 0;
632 TRACE("DOS program is now entering protected mode\n");
633 Callbacks->CallRegisterShortProc(&pm_ctx, 0);
635 /* in the current state of affairs, we won't ever actually return here... */
636 /* we should have int21/ah=4c do it someday, though... */
638 SELECTOR_FreeBlock(psp->environment, 1);
639 psp->environment = env_seg;
640 SELECTOR_FreeBlock(es, 1);
641 if (ds != ss) SELECTOR_FreeBlock(ds, 1);
642 SELECTOR_FreeBlock(ss, 1);
643 SELECTOR_FreeBlock(cs, 1);
646 /* DPMI Raw Mode Switch handler */
648 #if 0
649 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
651 LPDOSTASK lpDosTask = MZ_Current();
652 CONTEXT86 rm_ctx;
653 int ret;
655 if (!lpDosTask) {
656 /* we could probably start a DPMI-only dosmod task here, but I doubt
657 anything other than real DOS apps want to call raw mode switch */
658 ERR("attempting raw mode switch without DOS task!\n");
659 ExitProcess(1);
661 /* initialize real-mode context as per spec */
662 memset(&rm_ctx, 0, sizeof(rm_ctx));
663 DS_reg(&rm_ctx) = AX_sig(context);
664 ES_reg(&rm_ctx) = CX_sig(context);
665 SS_reg(&rm_ctx) = DX_sig(context);
666 ESP_reg(&rm_ctx) = EBX_sig(context);
667 CS_reg(&rm_ctx) = SI_sig(context);
668 EIP_reg(&rm_ctx) = EDI_sig(context);
669 EBP_reg(&rm_ctx) = EBP_sig(context);
670 FS_reg(&rm_ctx) = 0;
671 GS_reg(&rm_ctx) = 0;
672 EFL_reg(&rm_ctx) = EFL_sig(context); /* at least we need the IF flag */
674 /* enter real mode again */
675 TRACE("re-entering real mode at %04lx:%04lx\n",
676 CS_reg(&rm_ctx),EIP_reg(&rm_ctx));
677 ret = DOSVM_Enter( &rm_ctx );
678 /* when the real-mode stuff call its mode switch address,
679 DOSVM_Enter will return and we will continue here */
681 if (ret<0) {
682 /* if the sync was lost, there's no way to recover */
683 ExitProcess(1);
686 /* alter protected-mode context as per spec */
687 DS_sig(context) = AX_reg(&rm_ctx);
688 ES_sig(context) = CX_reg(&rm_ctx);
689 SS_sig(context) = DX_reg(&rm_ctx);
690 ESP_sig(context) = EBX_reg(&rm_ctx);
691 CS_sig(context) = SI_reg(&rm_ctx);
692 EIP_sig(context) = EDI_reg(&rm_ctx);
693 EBP_sig(context) = EBP_reg(&rm_ctx);
694 FS_sig(context) = 0;
695 GS_sig(context) = 0;
697 /* Return to new address and hope that we didn't mess up */
698 TRACE("re-entering protected mode at %04x:%08lx\n",
699 CS_sig(context), EIP_sig(context));
701 #endif
703 #else
704 #if 0
705 void WINAPI DPMI_RawModeSwitch( SIGCONTEXT *context )
707 ERR("don't even think about DPMI raw mode switch without DOS support!\n");
708 ExitProcess(1);
710 #endif
711 #endif
713 #define DOS_APP_ISDOS(addr,base) ((addr) < 0x110000)
714 #define DOS_WINE_ISDOS(addr,base) (((addr) >= (base)) && ((addr) < (base) + 0x110000))
715 #define DOS_UC_APPTOWINE(addr,base) ((addr) + (base))
716 #define DOS_UC_WINETOAPP(addr,base) ((addr) - (base))
717 #define DOS_APPTOWINE(addr,base) (DOS_APP_ISDOS(addr,base) ? DOS_UC_APPTOWINE(addr,base) : (addr))
718 #define DOS_WINETOAPP(addr,base) (DOS_WINE_ISDOS(addr,base) ? DOS_UC_WINETOAPP(addr,base) : (addr))
719 #define DOS_BADLIMIT(addr,base,limit) \
720 ((limit == 0xffffffff) || /* disallow "fat DS" for now */ \
721 (DOS_WINE_ISDOS(addr,base) && \
722 ((addr) + (limit) > (base) + 0x110000)))
724 /**********************************************************************
725 * INT_Int31Handler
727 * Handler for int 31h (DPMI).
730 void WINAPI INT_Int31Handler( CONTEXT86 *context )
733 * Note: For Win32s processes, the whole linear address space is
734 * shifted by 0x10000 relative to the OS linear address space.
735 * See the comment in msdos/vxd.c.
737 DWORD offset = W32S_APPLICATION() ? W32S_OFFSET : 0;
739 DWORD dw;
740 BYTE *ptr;
742 LPDOSTASK lpDosTask = MZ_Current();
744 #ifdef MZ_SUPPORTED
745 if (ISV86(context) && lpDosTask) {
746 /* Called from real mode, check if it's our wrapper */
747 TRACE("called from real mode\n");
748 if (CS_reg(context)==lpDosTask->dpmi_seg) {
749 /* This is the protected mode switch */
750 StartPM(context,lpDosTask);
751 return;
752 } else
753 if (CS_reg(context)==lpDosTask->xms_seg) {
754 /* This is the XMS driver entry point */
755 XMS_Handler(context);
756 return;
757 } else
759 /* Check for RMCB */
760 RMCB *CurrRMCB = FirstRMCB;
762 while (CurrRMCB && (HIWORD(CurrRMCB->address) != CS_reg(context)))
763 CurrRMCB = CurrRMCB->next;
765 if (CurrRMCB) {
766 /* RMCB call, propagate to protected-mode handler */
767 DPMI_CallRMCBProc(context, CurrRMCB, lpDosTask->dpmi_flag);
768 return;
772 #endif
774 RESET_CFLAG(context);
775 switch(AX_reg(context))
777 case 0x0000: /* Allocate LDT descriptors */
778 TRACE("allocate LDT descriptors (%d)\n",CX_reg(context));
779 if (!(EAX_reg(context) = AllocSelectorArray16( CX_reg(context) )))
781 TRACE("failed\n");
782 EAX_reg(context) = 0x8011; /* descriptor unavailable */
783 SET_CFLAG(context);
785 TRACE("success, array starts at 0x%04x\n",AX_reg(context));
786 break;
788 case 0x0001: /* Free LDT descriptor */
789 TRACE("free LDT descriptor (0x%04x)\n",BX_reg(context));
790 if (FreeSelector16( BX_reg(context) ))
792 EAX_reg(context) = 0x8022; /* invalid selector */
793 SET_CFLAG(context);
795 else
797 /* If a segment register contains the selector being freed, */
798 /* set it to zero. */
799 if (!((DS_reg(context)^BX_reg(context)) & ~3)) DS_reg(context) = 0;
800 if (!((ES_reg(context)^BX_reg(context)) & ~3)) ES_reg(context) = 0;
801 if (!((FS_reg(context)^BX_reg(context)) & ~3)) FS_reg(context) = 0;
802 if (!((GS_reg(context)^BX_reg(context)) & ~3)) GS_reg(context) = 0;
804 break;
806 case 0x0002: /* Real mode segment to descriptor */
807 TRACE("real mode segment to descriptor (0x%04x)\n",BX_reg(context));
809 WORD entryPoint = 0; /* KERNEL entry point for descriptor */
810 switch(BX_reg(context))
812 case 0x0000: entryPoint = 183; break; /* __0000H */
813 case 0x0040: entryPoint = 193; break; /* __0040H */
814 case 0xa000: entryPoint = 174; break; /* __A000H */
815 case 0xb000: entryPoint = 181; break; /* __B000H */
816 case 0xb800: entryPoint = 182; break; /* __B800H */
817 case 0xc000: entryPoint = 195; break; /* __C000H */
818 case 0xd000: entryPoint = 179; break; /* __D000H */
819 case 0xe000: entryPoint = 190; break; /* __E000H */
820 case 0xf000: entryPoint = 194; break; /* __F000H */
821 default:
822 EAX_reg(context) = DOSMEM_AllocSelector(BX_reg(context));
823 break;
825 if (entryPoint)
826 EAX_reg(context) = LOWORD(NE_GetEntryPoint( GetModuleHandle16( "KERNEL" ),
827 entryPoint ));
829 break;
831 case 0x0003: /* Get next selector increment */
832 TRACE("get selector increment (__AHINCR)\n");
833 EAX_reg(context) = __AHINCR;
834 break;
836 case 0x0004: /* Lock selector (not supported) */
837 FIXME("lock selector not supported\n");
838 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
839 break;
841 case 0x0005: /* Unlock selector (not supported) */
842 FIXME("unlock selector not supported\n");
843 EAX_reg(context) = 0; /* FIXME: is this a correct return value? */
844 break;
846 case 0x0006: /* Get selector base address */
847 TRACE("get selector base address (0x%04x)\n",BX_reg(context));
848 if (!(dw = GetSelectorBase( BX_reg(context) )))
850 EAX_reg(context) = 0x8022; /* invalid selector */
851 SET_CFLAG(context);
853 else
855 #ifdef MZ_SUPPORTED
856 if (lpDosTask) {
857 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
858 dw = DOS_WINETOAPP(dw, base);
860 #endif
861 CX_reg(context) = HIWORD(W32S_WINE2APP(dw, offset));
862 DX_reg(context) = LOWORD(W32S_WINE2APP(dw, offset));
864 break;
866 case 0x0007: /* Set selector base address */
867 TRACE("set selector base address (0x%04x,0x%08lx)\n",
868 BX_reg(context),
869 W32S_APP2WINE(MAKELONG(DX_reg(context),CX_reg(context)), offset));
870 dw = W32S_APP2WINE(MAKELONG(DX_reg(context), CX_reg(context)), offset);
871 #ifdef MZ_SUPPORTED
872 if (lpDosTask) {
873 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
874 dw = DOS_APPTOWINE(dw, base);
876 #endif
877 SetSelectorBase(BX_reg(context), dw);
878 break;
880 case 0x0008: /* Set selector limit */
881 TRACE("set selector limit (0x%04x,0x%08lx)\n",BX_reg(context),MAKELONG(DX_reg(context),CX_reg(context)));
882 dw = MAKELONG( DX_reg(context), CX_reg(context) );
883 #ifdef MZ_SUPPORTED
884 if (lpDosTask) {
885 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
886 DWORD sbase = GetSelectorBase( BX_reg(context) );
887 if (!sbase) {
888 /* the app has set the limit without setting the base,
889 * it must be relying on that the default should be DOS space;
890 * so set the base address now */
891 SetSelectorBase( BX_reg(context), sbase = base );
892 if (dw == 0xffffffff) {
893 /* djgpp does this without checking (in _dos_ds setup, crt1.c),
894 * so we have to override the limit here */
895 dw = 0x110000;
898 if (DOS_BADLIMIT(sbase, base, dw)) {
899 AX_reg(context) = 0x8021; /* invalid value */
900 SET_CFLAG(context);
901 break;
904 #endif
905 SetSelectorLimit16( BX_reg(context), dw );
906 break;
908 case 0x0009: /* Set selector access rights */
909 TRACE("set selector access rights(0x%04x,0x%04x)\n",BX_reg(context),CX_reg(context));
910 SelectorAccessRights16( BX_reg(context), 1, CX_reg(context) );
911 break;
913 case 0x000a: /* Allocate selector alias */
914 TRACE("allocate selector alias (0x%04x)\n",BX_reg(context));
915 if (!(AX_reg(context) = AllocCStoDSAlias16( BX_reg(context) )))
917 AX_reg(context) = 0x8011; /* descriptor unavailable */
918 SET_CFLAG(context);
920 break;
922 case 0x000b: /* Get descriptor */
923 TRACE("get descriptor (0x%04x)\n",BX_reg(context));
925 ldt_entry entry;
926 LDT_GetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
927 #ifdef MZ_SUPPORTED
928 if (lpDosTask) {
929 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
930 entry.base = DOS_WINETOAPP(entry.base, base);
932 #endif
933 entry.base = W32S_WINE2APP(entry.base, offset);
935 /* FIXME: should use ES:EDI for 32-bit clients */
936 LDT_EntryToBytes( PTR_SEG_OFF_TO_LIN( ES_reg(context),
937 DI_reg(context) ), &entry );
939 break;
941 case 0x000c: /* Set descriptor */
942 TRACE("set descriptor (0x%04x)\n",BX_reg(context));
944 ldt_entry entry;
945 LDT_BytesToEntry( PTR_SEG_OFF_TO_LIN( ES_reg(context),
946 DI_reg(context) ), &entry );
947 entry.base = W32S_APP2WINE(entry.base, offset);
948 #ifdef MZ_SUPPORTED
949 if (lpDosTask) {
950 DWORD base = (DWORD)DOSMEM_MemoryBase(lpDosTask->hModule);
951 entry.base = DOS_APPTOWINE(entry.base, base);
952 if (DOS_BADLIMIT(entry.base, base, entry.limit)) {
953 AX_reg(context) = 0x8021; /* invalid value */
954 SET_CFLAG(context);
955 break;
958 #endif
960 LDT_SetEntry( SELECTOR_TO_ENTRY( BX_reg(context) ), &entry );
962 break;
964 case 0x000d: /* Allocate specific LDT descriptor */
965 FIXME("allocate descriptor (0x%04x), stub!\n",BX_reg(context));
966 AX_reg(context) = 0x8011; /* descriptor unavailable */
967 SET_CFLAG(context);
968 break;
969 case 0x0100: /* Allocate DOS memory block */
970 TRACE("allocate DOS memory block (0x%x paragraphs)\n",BX_reg(context));
971 dw = GlobalDOSAlloc16((DWORD)BX_reg(context)<<4);
972 if (dw) {
973 AX_reg(context) = HIWORD(dw);
974 DX_reg(context) = LOWORD(dw);
975 } else {
976 AX_reg(context) = 0x0008; /* insufficient memory */
977 BX_reg(context) = DOSMEM_Available(0)>>4;
978 SET_CFLAG(context);
980 break;
981 case 0x0101: /* Free DOS memory block */
982 TRACE("free DOS memory block (0x%04x)\n",DX_reg(context));
983 dw = GlobalDOSFree16(DX_reg(context));
984 if (!dw) {
985 AX_reg(context) = 0x0009; /* memory block address invalid */
986 SET_CFLAG(context);
988 break;
989 case 0x0200: /* get real mode interrupt vector */
990 FIXME("get realmode interupt vector(0x%02x) unimplemented.\n",
991 BL_reg(context));
992 SET_CFLAG(context);
993 break;
994 case 0x0201: /* set real mode interrupt vector */
995 FIXME("set realmode interupt vector(0x%02x,0x%04x:0x%04x) unimplemented\n", BL_reg(context),CX_reg(context),DX_reg(context));
996 SET_CFLAG(context);
997 break;
998 case 0x0204: /* Get protected mode interrupt vector */
999 TRACE("get protected mode interrupt handler (0x%02x), stub!\n",BL_reg(context));
1000 dw = (DWORD)INT_GetPMHandler( BL_reg(context) );
1001 CX_reg(context) = HIWORD(dw);
1002 DX_reg(context) = LOWORD(dw);
1003 break;
1005 case 0x0205: /* Set protected mode interrupt vector */
1006 TRACE("set protected mode interrupt handler (0x%02x,%p), stub!\n",
1007 BL_reg(context),PTR_SEG_OFF_TO_LIN(CX_reg(context),DX_reg(context)));
1008 INT_SetPMHandler( BL_reg(context),
1009 (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CX_reg(context),
1010 DX_reg(context) ));
1011 break;
1013 case 0x0300: /* Simulate real mode interrupt */
1014 CallRMInt( context );
1015 break;
1017 case 0x0301: /* Call real mode procedure with far return */
1018 CallRMProc( context, FALSE );
1019 break;
1021 case 0x0302: /* Call real mode procedure with interrupt return */
1022 CallRMProc( context, TRUE );
1023 break;
1025 case 0x0303: /* Allocate Real Mode Callback Address */
1026 AllocRMCB( context );
1027 break;
1029 case 0x0304: /* Free Real Mode Callback Address */
1030 FreeRMCB( context );
1031 break;
1033 case 0x0305: /* Get State Save/Restore Addresses */
1034 TRACE("get state save/restore addresses\n");
1035 /* we probably won't need this kind of state saving */
1036 AX_reg(context) = 0;
1037 /* real mode: just point to the lret */
1038 BX_reg(context) = DPMI_wrap_seg;
1039 ECX_reg(context) = 2;
1040 /* protected mode: don't have any handler yet... */
1041 FIXME("no protected-mode dummy state save/restore handler yet\n");
1042 SI_reg(context) = 0;
1043 EDI_reg(context) = 0;
1044 break;
1046 case 0x0306: /* Get Raw Mode Switch Addresses */
1047 TRACE("get raw mode switch addresses\n");
1048 if (lpDosTask) {
1049 /* real mode, point to standard DPMI return wrapper */
1050 BX_reg(context) = DPMI_wrap_seg;
1051 ECX_reg(context) = 0;
1052 /* protected mode, point to DPMI call wrapper */
1053 SI_reg(context) = lpDosTask->dpmi_sel;
1054 EDI_reg(context) = 8; /* offset of the INT 0x31 call */
1055 } else {
1056 ERR("win app attempting to get raw mode switch!\n");
1057 AX_reg(context) = 0x8001; /* unsupported function */
1058 SET_CFLAG(context);
1060 break;
1061 case 0x0400: /* Get DPMI version */
1062 TRACE("get DPMI version\n");
1064 SYSTEM_INFO si;
1066 GetSystemInfo(&si);
1067 AX_reg(context) = 0x005a; /* DPMI version 0.90 */
1068 BX_reg(context) = 0x0005; /* Flags: 32-bit, virtual memory */
1069 CL_reg(context) = si.wProcessorLevel;
1070 DX_reg(context) = 0x0102; /* Master/slave interrupt controller base*/
1071 break;
1073 case 0x0500: /* Get free memory information */
1074 TRACE("get free memory information\n");
1076 MEMMANINFO mmi;
1078 mmi.dwSize = sizeof(mmi);
1079 MemManInfo16(&mmi);
1080 ptr = (BYTE *)PTR_SEG_OFF_TO_LIN(ES_reg(context),DI_reg(context));
1081 /* the layout is just the same as MEMMANINFO, but without
1082 * the dwSize entry.
1084 memcpy(ptr,((char*)&mmi)+4,sizeof(mmi)-4);
1085 break;
1087 case 0x0501: /* Allocate memory block */
1088 TRACE("allocate memory block (%ld)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1089 if (!(ptr = (BYTE *)DPMI_xalloc(MAKELONG(CX_reg(context), BX_reg(context)))))
1091 AX_reg(context) = 0x8012; /* linear memory not available */
1092 SET_CFLAG(context);
1093 } else {
1094 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1095 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1097 break;
1099 case 0x0502: /* Free memory block */
1100 TRACE("free memory block (0x%08lx)\n",
1101 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset));
1102 DPMI_xfree( (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),
1103 SI_reg(context)), offset) );
1104 break;
1106 case 0x0503: /* Resize memory block */
1107 TRACE("resize memory block (0x%08lx,%ld)\n",
1108 W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1109 MAKELONG(CX_reg(context),BX_reg(context)));
1110 if (!(ptr = (BYTE *)DPMI_xrealloc(
1111 (void *)W32S_APP2WINE(MAKELONG(DI_reg(context),SI_reg(context)), offset),
1112 MAKELONG(CX_reg(context),BX_reg(context)))))
1114 AX_reg(context) = 0x8012; /* linear memory not available */
1115 SET_CFLAG(context);
1116 } else {
1117 BX_reg(context) = SI_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1118 CX_reg(context) = DI_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1120 break;
1122 case 0x0507: /* Modify page attributes */
1123 FIXME("modify page attributes unimplemented\n");
1124 break; /* Just ignore it */
1126 case 0x0600: /* Lock linear region */
1127 FIXME("lock linear region unimplemented\n");
1128 break; /* Just ignore it */
1130 case 0x0601: /* Unlock linear region */
1131 FIXME("unlock linear region unimplemented\n");
1132 break; /* Just ignore it */
1134 case 0x0602: /* Unlock real-mode region */
1135 FIXME("unlock realmode region unimplemented\n");
1136 break; /* Just ignore it */
1138 case 0x0603: /* Lock real-mode region */
1139 FIXME("lock realmode region unimplemented\n");
1140 break; /* Just ignore it */
1142 case 0x0604: /* Get page size */
1143 TRACE("get pagesize\n");
1144 BX_reg(context) = 0;
1145 CX_reg(context) = VIRTUAL_GetPageSize();
1146 break;
1148 case 0x0702: /* Mark page as demand-paging candidate */
1149 FIXME("mark page as demand-paging candidate\n");
1150 break; /* Just ignore it */
1152 case 0x0703: /* Discard page contents */
1153 FIXME("discard page contents\n");
1154 break; /* Just ignore it */
1156 case 0x0800: /* Physical address mapping */
1157 FIXME("map real to linear (0x%08lx)\n",MAKELONG(CX_reg(context),BX_reg(context)));
1158 if(!(ptr=DOSMEM_MapRealToLinear(MAKELONG(CX_reg(context),BX_reg(context)))))
1160 AX_reg(context) = 0x8021;
1161 SET_CFLAG(context);
1163 else
1165 BX_reg(context) = HIWORD(W32S_WINE2APP(ptr, offset));
1166 CX_reg(context) = LOWORD(W32S_WINE2APP(ptr, offset));
1167 RESET_CFLAG(context);
1169 break;
1171 default:
1172 INT_BARF( context, 0x31 );
1173 AX_reg(context) = 0x8001; /* unsupported function */
1174 SET_CFLAG(context);
1175 break;