4 * Copyright 1998 Ove Kåven
6 * This code hasn't been completely cleaned up yet.
19 #include <sys/types.h>
21 #include "wine/winbase16.h"
22 #include "wine/exception.h"
34 #include "../../loader/dos/dosmod.h"
35 #include "stackframe.h"
36 #include "debugtools.h"
38 DEFAULT_DEBUG_CHANNEL(int);
39 DECLARE_DEBUG_CHANNEL(module
);
40 DECLARE_DEBUG_CHANNEL(relay
);
43 WORD DOSVM_retval
= 0;
47 #ifdef HAVE_SYS_VM86_H
48 # include <sys/vm86.h>
50 #ifdef HAVE_SYS_MMAN_H
51 # include <sys/mman.h>
54 #define IF_CLR(ctx) ((ctx)->EFlags &= ~VIF_MASK)
55 #define IF_SET(ctx) ((ctx)->EFlags |= VIF_MASK)
56 #define IF_ENABLED(ctx) ((ctx)->EFlags & VIF_MASK)
57 #define SET_PEND(ctx) ((ctx)->EFlags |= VIP_MASK)
58 #define CLR_PEND(ctx) ((ctx)->EFlags &= ~VIP_MASK)
59 #define IS_PEND(ctx) ((ctx)->EFlags & VIP_MASK)
63 typedef struct _DOSEVENT
{
67 struct _DOSEVENT
*next
;
68 } DOSEVENT
, *LPDOSEVENT
;
70 static struct _DOSEVENT
*pending_event
, *current_event
;
71 static int sig_sent
, entered
;
72 static CONTEXT86
*current_context
;
75 extern int read_pipe
, write_pipe
;
76 extern HANDLE hReadPipe
;
77 extern pid_t dosmod_pid
;
79 static void do_exception( int signal
, CONTEXT86
*context
)
82 if ((signal
== SIGTRAP
) || (signal
== SIGHUP
))
84 rec
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
85 rec
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
89 rec
.ExceptionCode
= EXCEPTION_ILLEGAL_INSTRUCTION
; /* generic error */
90 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
92 rec
.ExceptionRecord
= NULL
;
93 rec
.ExceptionAddress
= (LPVOID
)context
->Eip
;
94 rec
.NumberParameters
= 0;
95 EXC_RtlRaiseException( &rec
, context
);
98 static void DOSVM_Dump( int fn
, int sig
, struct vm86plus_struct
*VM86
)
103 switch (VM86_TYPE(fn
)) {
105 printf("Trapped signal %d\n",sig
); break;
107 printf("Trapped unhandled GPF\n"); break;
109 printf("Trapped INT %02x\n",VM86_ARG(fn
)); break;
111 printf("Trapped STI\n"); break;
113 printf("Trapped due to pending PIC request\n"); break;
115 printf("Trapped debug request\n"); break;
117 printf("Trapped unknown VM86 type %d arg %d\n",VM86_TYPE(fn
),VM86_ARG(fn
)); break;
119 #define REGS VM86->regs
120 fprintf(stderr
,"AX=%04lX CX=%04lX DX=%04lX BX=%04lX\n",REGS
.eax
,REGS
.ecx
,REGS
.edx
,REGS
.ebx
);
121 fprintf(stderr
,"SI=%04lX DI=%04lX SP=%04lX BP=%04lX\n",REGS
.esi
,REGS
.edi
,REGS
.esp
,REGS
.ebp
);
122 fprintf(stderr
,"CS=%04X DS=%04X ES=%04X SS=%04X\n",REGS
.cs
,REGS
.ds
,REGS
.es
,REGS
.ss
);
123 fprintf(stderr
,"IP=%04lX EFLAGS=%08lX\n",REGS
.eip
,REGS
.eflags
);
125 inst
= PTR_REAL_TO_LIN( REGS
.cs
, REGS
.eip
);
128 for (x
=0; x
<8; x
++) printf(" %02x",inst
[x
]);
132 static int DOSVM_SimulateInt( int vect
, CONTEXT86
*context
, BOOL inwine
)
134 FARPROC16 handler
=DOSVM_GetRMHandler(vect
);
136 /* check for our real-mode hooks */
138 if (context
->SegCs
==DOSMEM_wrap_seg
) {
139 /* exit from real-mode wrapper */
142 /* we could probably move some other dodgy stuff here too from dpmi.c */
144 /* check if the call is from our fake BIOS interrupt stubs */
145 if ((context
->SegCs
==0xf000) && !inwine
) {
146 if (vect
!= (context
->Eip
/4)) {
147 TRACE("something fishy going on here (interrupt stub is %02lx)\n", context
->Eip
/4);
149 TRACE("builtin interrupt %02x has been branched to\n", vect
);
150 DOSVM_RealModeInterrupt(vect
, context
);
152 /* check if the call goes to an unhooked interrupt */
153 else if (SELECTOROF(handler
)==0xf000) {
154 /* if so, call it directly */
155 TRACE("builtin interrupt %02x has been invoked (through vector %02x)\n", OFFSETOF(handler
)/4, vect
);
156 DOSVM_RealModeInterrupt(OFFSETOF(handler
)/4, context
);
158 /* the interrupt is hooked, simulate interrupt in DOS space */
160 WORD
*stack
= PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
161 WORD flag
=LOWORD(context
->EFlags
);
163 if (IF_ENABLED(context
)) flag
|=IF_MASK
;
167 *(--stack
)=context
->SegCs
;
168 *(--stack
)=LOWORD(context
->Eip
);
170 context
->SegCs
=SELECTOROF(handler
);
171 context
->Eip
=OFFSETOF(handler
);
177 #define SHOULD_PEND(x) \
178 (x && ((!current_event) || (x->priority < current_event->priority)))
180 static void DOSVM_SendQueuedEvent(CONTEXT86
*context
)
182 LPDOSEVENT event
= pending_event
;
184 if (SHOULD_PEND(event
)) {
185 /* remove from "pending" list */
186 pending_event
= event
->next
;
189 /* it's an IRQ, move it to "current" list */
190 event
->next
= current_event
;
191 current_event
= event
;
192 TRACE("dispatching IRQ %d\n",event
->irq
);
193 /* note that if DOSVM_SimulateInt calls an internal interrupt directly,
194 * current_event might be cleared (and event freed) in this very call! */
195 DOSVM_SimulateInt((event
->irq
<8)?(event
->irq
+8):(event
->irq
-8+0x70),context
,TRUE
);
198 TRACE("dispatching callback event\n");
199 (*event
->relay
)(context
,event
->data
);
203 if (!SHOULD_PEND(pending_event
)) {
204 TRACE("clearing Pending flag\n");
209 static void DOSVM_SendQueuedEvents(CONTEXT86
*context
)
211 /* we will send all queued events as long as interrupts are enabled,
212 * but IRQ events will disable interrupts again */
213 while (IS_PEND(context
) && IF_ENABLED(context
))
214 DOSVM_SendQueuedEvent(context
);
217 /***********************************************************************
218 * QueueEvent (WINEDOS.@)
220 void WINAPI
DOSVM_QueueEvent( INT irq
, INT priority
, DOSRELAY relay
, LPVOID data
)
222 LPDOSEVENT event
, cur
, prev
;
225 event
= malloc(sizeof(DOSEVENT
));
227 ERR("out of memory allocating event entry\n");
230 event
->irq
= irq
; event
->priority
= priority
;
231 event
->relay
= relay
; event
->data
= data
;
233 /* insert event into linked list, in order *after*
234 * all earlier events of higher or equal priority */
235 cur
= pending_event
; prev
= NULL
;
236 while (cur
&& cur
->priority
<=priority
) {
241 if (prev
) prev
->next
= event
;
242 else pending_event
= event
;
244 /* get dosmod's attention to the new event, if necessary */
246 TRACE("new event queued, signalling dosmod\n");
247 kill(dosmod_pid
,SIGUSR2
);
250 TRACE("new event queued\n");
253 /* DOS subsystem not running */
254 /* (this probably means that we're running a win16 app
255 * which uses DPMI to thunk down to DOS services) */
257 /* callback event, perform it with dummy context */
259 memset(&context
,0,sizeof(context
));
260 (*relay
)(&context
,data
);
262 ERR("IRQ without DOS task: should not happen");
267 #define CV do { CP(eax,Eax); CP(ecx,Ecx); CP(edx,Edx); CP(ebx,Ebx); \
268 CP(esi,Esi); CP(edi,Edi); CP(esp,Esp); CP(ebp,Ebp); \
269 CP(cs,SegCs); CP(ds,SegDs); CP(es,SegEs); \
270 CP(ss,SegSs); CP(fs,SegFs); CP(gs,SegGs); \
271 CP(eip,Eip); CP(eflags,EFlags); } while(0)
273 static int DOSVM_Process( int fn
, int sig
, struct vm86plus_struct
*VM86
)
275 CONTEXT86 context
, *old_context
;
278 #define CP(x,y) context.y = VM86->regs.x
281 if (VM86_TYPE(fn
)==VM86_UNKNOWN
) {
282 ret
=INSTR_EmulateInstruction(&context
);
283 #define CP(x,y) VM86->regs.x = context.y
290 if (VM86
->vm86plus
.force_return_for_pic
) {
294 /* linux doesn't preserve pending flag on return */
295 if (SHOULD_PEND(pending_event
)) {
300 old_context
= current_context
;
301 current_context
= &context
;
303 switch (VM86_TYPE(fn
)) {
305 TRACE("DOS module caught signal %d\n",sig
);
306 if ((sig
==SIGALRM
) || (sig
==SIGUSR2
)) {
309 DOSVM_QueueEvent(0,DOS_PRIORITY_REALTIME
,NULL
,NULL
);
312 TRACE("setting Pending flag, interrupts are currently %s\n",
313 IF_ENABLED(&context
) ? "enabled" : "disabled");
315 DOSVM_SendQueuedEvents(&context
);
317 TRACE("no events are pending, clearing Pending flag\n");
322 else if ((sig
==SIGHUP
) || (sig
==SIGILL
) || (sig
==SIGSEGV
)) {
323 do_exception( sig
, &context
);
325 DOSVM_Dump(fn
,sig
,VM86
);
329 case VM86_UNKNOWN
: /* unhandled GPF */
330 DOSVM_Dump(fn
,sig
,VM86
);
331 do_exception( SIGSEGV
, &context
);
335 DPRINTF("Call DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn
),context
.Eax
,context
.SegCs
,context
.Eip
);
336 ret
=DOSVM_SimulateInt(VM86_ARG(fn
),&context
,FALSE
);
338 DPRINTF("Ret DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn
),context
.Eax
,context
.SegCs
,context
.Eip
);
342 /* case VM86_PICRETURN: */
343 TRACE("DOS task enabled interrupts %s events pending, sending events\n", IS_PEND(&context
)?"with":"without");
344 DOSVM_SendQueuedEvents(&context
);
347 do_exception( SIGTRAP
, &context
);
350 DOSVM_Dump(fn
,sig
,VM86
);
354 current_context
= old_context
;
356 #define CP(x,y) VM86->regs.x = context.y
360 VM86
->vm86plus
.force_return_for_pic
= IS_PEND(&context
) ? 1 : 0;
366 static void DOSVM_ProcessConsole(void)
372 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE
),&msg
,1,&res
)) {
373 switch (msg
.EventType
) {
375 scan
= msg
.Event
.KeyEvent
.wVirtualScanCode
;
376 if (!msg
.Event
.KeyEvent
.bKeyDown
) scan
|= 0x80;
378 /* check whether extended bit is set,
379 * and if so, queue the extension prefix */
380 if (msg
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
) {
381 DOSVM_Int09SendScan(0xE0,0);
383 DOSVM_Int09SendScan(scan
,msg
.Event
.KeyEvent
.uChar
.AsciiChar
);
386 FIXME("unhandled console event: %d\n", msg
.EventType
);
391 static void DOSVM_ProcessMessage(MSG
*msg
)
395 TRACE("got message %04x, wparam=%08x, lparam=%08lx\n",msg
->message
,msg
->wParam
,msg
->lParam
);
396 if ((msg
->message
>=WM_MOUSEFIRST
)&&
397 (msg
->message
<=WM_MOUSELAST
)) {
398 DOSVM_Int33Message(msg
->message
,msg
->wParam
,msg
->lParam
);
400 switch (msg
->message
) {
404 scan
|= (msg
->lParam
>> 16) & 0x7f;
406 /* check whether extended bit is set,
407 * and if so, queue the extension prefix */
408 if (msg
->lParam
& 0x1000000) {
409 /* FIXME: some keys (function keys) have
410 * extended bit set even when they shouldn't,
411 * should check for them */
412 DOSVM_Int09SendScan(0xE0,0);
414 DOSVM_Int09SendScan(scan
,0);
420 /***********************************************************************
423 void WINAPI
DOSVM_Wait( INT read_pipe
, HANDLE hObject
)
429 BOOL got_msg
= FALSE
;
431 objs
[0]=GetStdHandle(STD_INPUT_HANDLE
);
435 /* check for messages (waste time before the response check below) */
438 while (PeekMessageA(&msg
,0,0,0,PM_REMOVE
|PM_NOYIELD
)) {
440 DOSVM_ProcessMessage(&msg
);
441 /* we don't need a TranslateMessage here */
442 DispatchMessageA(&msg
);
448 /* check for console input */
451 if (PeekConsoleInputA(objs
[0],&msg
,1,&num
) && num
) {
452 DOSVM_ProcessConsole();
456 if (read_pipe
== -1) {
457 /* dispatch pending events */
458 if (SHOULD_PEND(pending_event
)) {
459 CONTEXT86 context
= *current_context
;
462 DOSVM_SendQueuedEvents(&context
);
467 struct timeval timeout
={0,0};
468 /* quick check for response from dosmod
469 * (faster than doing the full blocking wait, if data already available) */
470 FD_ZERO(&readfds
); FD_SET(read_pipe
,&readfds
);
471 if (select(read_pipe
+1,&readfds
,NULL
,NULL
,&timeout
)>0)
474 /* nothing yet, block while waiting for something to do */
475 if (MsgWaitForMultipleObjects
)
476 waitret
= MsgWaitForMultipleObjects(objc
,objs
,FALSE
,INFINITE
,QS_ALLINPUT
);
478 waitret
= WaitForMultipleObjects(objc
,objs
,FALSE
,INFINITE
);
480 if (waitret
==(DWORD
)-1) {
481 ERR_(module
)("dosvm wait error=%ld\n",GetLastError());
483 if ((read_pipe
!= -1) && hObject
) {
484 if (waitret
==(WAIT_OBJECT_0
+1)) break;
486 if (waitret
==WAIT_OBJECT_0
)
487 goto chk_console_input
;
491 /***********************************************************************
494 INT WINAPI
DOSVM_Enter( CONTEXT86
*context
)
496 struct vm86plus_struct VM86
;
499 memset(&VM86
, 0, sizeof(VM86
));
500 #define CP(x,y) VM86.regs.x = context->y
503 if (VM86
.regs
.eflags
& IF_MASK
)
504 VM86
.regs
.eflags
|= VIF_MASK
;
506 /* main exchange loop */
509 TRACE_(module
)("thread is: %lx\n",GetCurrentThreadId());
512 /* transmit VM86 structure to dosmod task */
513 if (write(write_pipe
,&stat
,sizeof(stat
))!=sizeof(stat
)) {
514 ERR_(module
)("dosmod sync lost, errno=%d, fd=%d, pid=%d\n",errno
,write_pipe
,getpid());
517 if (write(write_pipe
,&VM86
,sizeof(VM86
))!=sizeof(VM86
)) {
518 ERR_(module
)("dosmod sync lost, errno=%d\n",errno
);
521 /* wait for response, doing other things in the meantime */
522 DOSVM_Wait(read_pipe
, hReadPipe
);
525 if ((len
=read(read_pipe
,&stat
,sizeof(stat
)))==sizeof(stat
)) break;
526 if (((errno
==EINTR
)||(errno
==EAGAIN
))&&(len
<=0)) {
527 WARN_(module
)("rereading dosmod return code due to errno=%d, result=%d\n",errno
,len
);
530 ERR_(module
)("dosmod sync lost reading return code, errno=%d, result=%d\n",errno
,len
);
533 TRACE_(module
)("dosmod return code=%d\n",stat
);
534 if (stat
==DOSMOD_LEFTIDLE
) {
538 if ((len
=read(read_pipe
,&VM86
,sizeof(VM86
)))==sizeof(VM86
)) break;
539 if (((errno
==EINTR
)||(errno
==EAGAIN
))&&(len
<=0)) {
540 WARN_(module
)("rereading dosmod VM86 structure due to errno=%d, result=%d\n",errno
,len
);
543 ERR_(module
)("dosmod sync lost reading VM86 structure, errno=%d, result=%d\n",errno
,len
);
546 if ((stat
&0xff)==DOSMOD_SIGNAL
) {
548 if ((len
=read(read_pipe
,&sig
,sizeof(sig
)))==sizeof(sig
)) break;
549 if (((errno
==EINTR
)||(errno
==EAGAIN
))&&(len
<=0)) {
550 WARN_(module
)("rereading dosmod signal due to errno=%d, result=%d\n",errno
,len
);
553 ERR_(module
)("dosmod sync lost reading signal, errno=%d, result=%d\n",errno
,len
);
558 } while (DOSVM_Process(stat
,sig
,&VM86
)>=0);
561 #define CP(x,y) context->y = VM86.regs.x
567 /***********************************************************************
570 void WINAPI
DOSVM_PIC_ioport_out( WORD port
, BYTE val
)
574 if ((port
==0x20) && (val
==0x20)) {
576 /* EOI (End Of Interrupt) */
577 TRACE("received EOI for current IRQ, clearing\n");
578 event
= current_event
;
579 current_event
= event
->next
;
581 (*event
->relay
)(NULL
,event
->data
);
586 /* another event is pending, which we should probably
587 * be able to process now, so tell dosmod about it */
588 TRACE("another event pending, signalling dosmod\n");
589 kill(dosmod_pid
,SIGUSR2
);
593 WARN("EOI without active IRQ\n");
596 FIXME("unrecognized PIC command %02x\n",val
);
600 /***********************************************************************
601 * SetTimer (WINEDOS.@)
603 void WINAPI
DOSVM_SetTimer( UINT ticks
)
605 int stat
=DOSMOD_SET_TIMER
;
608 if (write_pipe
!= -1) {
609 /* the PC clocks ticks at 1193180 Hz */
611 tim
.tv_usec
=MulDiv(ticks
,1000000,1193180);
613 if (!tim
.tv_usec
) tim
.tv_usec
=1;
615 if (write(write_pipe
,&stat
,sizeof(stat
))!=sizeof(stat
)) {
616 ERR_(module
)("dosmod sync lost, errno=%d\n",errno
);
619 if (write(write_pipe
,&tim
,sizeof(tim
))!=sizeof(tim
)) {
620 ERR_(module
)("dosmod sync lost, errno=%d\n",errno
);
623 /* there's no return */
627 /***********************************************************************
628 * GetTimer (WINEDOS.@)
630 UINT WINAPI
DOSVM_GetTimer( void )
632 int stat
=DOSMOD_GET_TIMER
;
635 if (write_pipe
!= -1) {
636 if (write(write_pipe
,&stat
,sizeof(stat
))!=sizeof(stat
)) {
637 ERR_(module
)("dosmod sync lost, errno=%d\n",errno
);
642 if (read(read_pipe
,&tim
,sizeof(tim
))==sizeof(tim
)) break;
643 if ((errno
==EINTR
)||(errno
==EAGAIN
)) continue;
644 ERR_(module
)("dosmod sync lost, errno=%d\n",errno
);
647 return MulDiv(tim
.tv_usec
,1193180,1000000);
652 #else /* !MZ_SUPPORTED */
654 /***********************************************************************
657 INT WINAPI
DOSVM_Enter( CONTEXT86
*context
)
659 ERR_(module
)("DOS realmode not supported on this architecture!\n");
663 /***********************************************************************
666 void WINAPI
DOSVM_Wait( INT read_pipe
, HANDLE hObject
) {}
668 /***********************************************************************
671 void WINAPI
DOSVM_PIC_ioport_out( WORD port
, BYTE val
) {}
673 /***********************************************************************
674 * SetTimer (WINEDOS.@)
676 void WINAPI
DOSVM_SetTimer( UINT ticks
) {}
678 /***********************************************************************
679 * GetTimer (WINEDOS.@)
681 UINT WINAPI
DOSVM_GetTimer( void ) { return 0; }
683 /***********************************************************************
684 * QueueEvent (WINEDOS.@)
686 void WINAPI
DOSVM_QueueEvent( INT irq
, INT priority
, DOSRELAY relay
, LPVOID data
)
689 /* callback event, perform it with dummy context */
691 memset(&context
,0,sizeof(context
));
692 (*relay
)(&context
,data
);
694 ERR("IRQ without DOS task: should not happen");
700 /**********************************************************************
703 * Return the real mode interrupt vector for a given interrupt.
705 FARPROC16
DOSVM_GetRMHandler( BYTE intnum
)
707 return ((FARPROC16
*)DOSMEM_SystemBase())[intnum
];
711 /**********************************************************************
714 * Set the real mode interrupt handler for a given interrupt.
716 void DOSVM_SetRMHandler( BYTE intnum
, FARPROC16 handler
)
718 TRACE("Set real mode interrupt vector %02x <- %04x:%04x\n",
719 intnum
, HIWORD(handler
), LOWORD(handler
) );
720 ((FARPROC16
*)DOSMEM_SystemBase())[intnum
] = handler
;
724 static const INTPROC real_mode_handlers
[] =
726 /* 00 */ 0, 0, 0, 0, 0, 0, 0, 0,
727 /* 08 */ 0, DOSVM_Int09Handler
, 0, 0, 0, 0, 0, 0,
728 /* 10 */ DOSVM_Int10Handler
, INT_Int11Handler
, INT_Int12Handler
, INT_Int13Handler
,
729 0, INT_Int15Handler
, DOSVM_Int16Handler
, DOSVM_Int17Handler
,
730 /* 18 */ 0, 0, INT_Int1aHandler
, 0, 0, 0, 0, 0,
731 /* 20 */ DOSVM_Int20Handler
, DOSVM_Int21Handler
, 0, 0, 0, INT_Int25Handler
, 0, 0,
732 /* 28 */ 0, DOSVM_Int29Handler
, INT_Int2aHandler
, 0, 0, 0, 0, INT_Int2fHandler
,
733 /* 30 */ 0, DOSVM_Int31Handler
, 0, DOSVM_Int33Handler
737 /**********************************************************************
738 * DOSVM_RealModeInterrupt
740 * Handle real mode interrupts
742 void DOSVM_RealModeInterrupt( BYTE intnum
, CONTEXT86
*context
)
744 if (intnum
< sizeof(real_mode_handlers
)/sizeof(INTPROC
) && real_mode_handlers
[intnum
])
745 (*real_mode_handlers
[intnum
])(context
);
748 FIXME("Unknown Interrupt in DOS mode: 0x%x\n", intnum
);
749 FIXME(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx\n",
750 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
);
751 FIXME(" esi=%08lx edi=%08lx ds=%04lx es=%04lx\n",
752 context
->Esi
, context
->Edi
, context
->SegDs
, context
->SegEs
);
757 /**********************************************************************
760 BOOL WINAPI
DOSVM_Init( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
762 TRACE_(module
)("(0x%08x,%ld,%p)\n", hinstDLL
, fdwReason
, lpvReserved
);
764 if (fdwReason
== DLL_PROCESS_ATTACH
)
766 /* initialize the memory */
767 TRACE("Initializing DOS memory structures\n");
769 DOSDEV_InstallDOSDevices();