4 * Copyright 1998 Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: This code hasn't been completely cleaned up yet.
33 #include <sys/types.h>
35 #include "wine/winbase16.h"
36 #include "wine/exception.h"
49 #include "stackframe.h"
50 #include "wine/debug.h"
51 #include "msvcrt/excpt.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(int);
54 WINE_DECLARE_DEBUG_CHANNEL(module);
55 WINE_DECLARE_DEBUG_CHANNEL(relay);
58 WORD DOSVM_retval = 0;
62 #ifdef HAVE_SYS_VM86_H
63 # include <sys/vm86.h>
65 #ifdef HAVE_SYS_MMAN_H
66 # include <sys/mman.h>
69 #define IF_CLR(ctx) ((ctx)->EFlags &= ~VIF_MASK)
70 #define IF_SET(ctx) ((ctx)->EFlags |= VIF_MASK)
71 #define IF_ENABLED(ctx) ((ctx)->EFlags & VIF_MASK)
72 #define SET_PEND(ctx) ((ctx)->EFlags |= VIP_MASK)
73 #define CLR_PEND(ctx) ((ctx)->EFlags &= ~VIP_MASK)
74 #define IS_PEND(ctx) ((ctx)->EFlags & VIP_MASK)
78 typedef struct _DOSEVENT {
82 struct _DOSEVENT *next;
83 } DOSEVENT, *LPDOSEVENT;
85 static CRITICAL_SECTION qcrit = CRITICAL_SECTION_INIT("DOSVM");
86 static struct _DOSEVENT *pending_event, *current_event;
88 static CONTEXT86 *current_context;
90 static int DOSVM_SimulateInt( int vect, CONTEXT86 *context, BOOL inwine )
92 FARPROC16 handler=DOSVM_GetRMHandler(vect);
94 /* check for our real-mode hooks */
96 if (context->SegCs==DOSMEM_wrap_seg) {
97 /* exit from real-mode wrapper */
100 /* we could probably move some other dodgy stuff here too from dpmi.c */
102 /* check if the call is from our fake BIOS interrupt stubs */
103 if ((context->SegCs==0xf000) && !inwine) {
104 if (vect != (context->Eip/4)) {
105 TRACE("something fishy going on here (interrupt stub is %02lx)\n", context->Eip/4);
107 TRACE("builtin interrupt %02x has been branched to\n", vect);
108 DOSVM_RealModeInterrupt(vect, context);
110 /* check if the call goes to an unhooked interrupt */
111 else if (SELECTOROF(handler)==0xf000) {
112 /* if so, call it directly */
113 TRACE("builtin interrupt %02x has been invoked (through vector %02x)\n", OFFSETOF(handler)/4, vect);
114 DOSVM_RealModeInterrupt(OFFSETOF(handler)/4, context);
116 /* the interrupt is hooked, simulate interrupt in DOS space */
118 WORD*stack= PTR_REAL_TO_LIN( context->SegSs, context->Esp );
119 WORD flag=LOWORD(context->EFlags);
121 TRACE_(int)("invoking hooked interrupt %02x at %04x:%04x\n", vect,
122 SELECTOROF(handler), OFFSETOF(handler));
123 if (IF_ENABLED(context)) flag|=IF_MASK;
127 *(--stack)=context->SegCs;
128 *(--stack)=LOWORD(context->Eip);
130 context->SegCs=SELECTOROF(handler);
131 context->Eip=OFFSETOF(handler);
137 #define SHOULD_PEND(x) \
138 (x && ((!current_event) || (x->priority < current_event->priority)))
140 static void DOSVM_SendQueuedEvent(CONTEXT86 *context)
142 LPDOSEVENT event = pending_event;
144 if (SHOULD_PEND(event)) {
145 /* remove from "pending" list */
146 pending_event = event->next;
149 /* it's an IRQ, move it to "current" list */
150 event->next = current_event;
151 current_event = event;
152 TRACE("dispatching IRQ %d\n",event->irq);
153 /* note that if DOSVM_SimulateInt calls an internal interrupt directly,
154 * current_event might be cleared (and event freed) in this very call! */
155 DOSVM_SimulateInt((event->irq<8)?(event->irq+8):(event->irq-8+0x70),context,TRUE);
158 TRACE("dispatching callback event\n");
159 (*event->relay)(context,event->data);
163 if (!SHOULD_PEND(pending_event)) {
164 TRACE("clearing Pending flag\n");
169 static void DOSVM_SendQueuedEvents(CONTEXT86 *context)
171 /* we will send all queued events as long as interrupts are enabled,
172 * but IRQ events will disable interrupts again */
173 while (IS_PEND(context) && IF_ENABLED(context))
174 DOSVM_SendQueuedEvent(context);
177 /***********************************************************************
178 * QueueEvent (WINEDOS.@)
180 void WINAPI DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
182 LPDOSEVENT event, cur, prev;
184 if (current_context) {
185 EnterCriticalSection(&qcrit);
186 event = malloc(sizeof(DOSEVENT));
188 ERR("out of memory allocating event entry\n");
191 event->irq = irq; event->priority = priority;
192 event->relay = relay; event->data = data;
194 /* insert event into linked list, in order *after*
195 * all earlier events of higher or equal priority */
196 cur = pending_event; prev = NULL;
197 while (cur && cur->priority<=priority) {
202 if (prev) prev->next = event;
203 else pending_event = event;
205 /* alert the vm86 about the new event */
207 TRACE("new event queued, signalling (time=%ld)\n", GetTickCount());
208 kill(dosvm_pid,SIGUSR2);
211 TRACE("new event queued (time=%ld)\n", GetTickCount());
213 LeaveCriticalSection(&qcrit);
215 /* DOS subsystem not running */
216 /* (this probably means that we're running a win16 app
217 * which uses DPMI to thunk down to DOS services) */
219 /* callback event, perform it with dummy context */
221 memset(&context,0,sizeof(context));
222 (*relay)(&context,data);
224 ERR("IRQ without DOS task: should not happen");
229 static void DOSVM_ProcessConsole(void)
235 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res)) {
236 switch (msg.EventType) {
238 scan = msg.Event.KeyEvent.wVirtualScanCode;
239 if (!msg.Event.KeyEvent.bKeyDown) scan |= 0x80;
241 /* check whether extended bit is set,
242 * and if so, queue the extension prefix */
243 if (msg.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY) {
244 DOSVM_Int09SendScan(0xE0,0);
246 DOSVM_Int09SendScan(scan,msg.Event.KeyEvent.uChar.AsciiChar);
249 DOSVM_Int33Console(&msg.Event.MouseEvent);
251 case WINDOW_BUFFER_SIZE_EVENT:
252 FIXME("unhandled WINDOW_BUFFER_SIZE_EVENT.\n");
255 FIXME("unhandled MENU_EVENT.\n");
258 FIXME("unhandled FOCUS_EVENT.\n");
261 FIXME("unknown console event: %d\n", msg.EventType);
266 static void DOSVM_ProcessMessage(MSG *msg)
270 TRACE("got message %04x, wparam=%08x, lparam=%08lx\n",msg->message,msg->wParam,msg->lParam);
271 if ((msg->message>=WM_MOUSEFIRST)&&
272 (msg->message<=WM_MOUSELAST)) {
273 DOSVM_Int33Message(msg->message,msg->wParam,msg->lParam);
275 switch (msg->message) {
279 scan |= (msg->lParam >> 16) & 0x7f;
281 /* check whether extended bit is set,
282 * and if so, queue the extension prefix */
283 if (msg->lParam & 0x1000000) {
284 /* FIXME: some keys (function keys) have
285 * extended bit set even when they shouldn't,
286 * should check for them */
287 DOSVM_Int09SendScan(0xE0,0);
289 DOSVM_Int09SendScan(scan,0);
295 /***********************************************************************
298 void WINAPI DOSVM_Wait( INT read_pipe, HANDLE hObject )
304 BOOL got_msg = FALSE;
306 objs[0]=GetStdHandle(STD_INPUT_HANDLE);
310 /* check for messages (waste time before the response check below) */
313 while (PeekMessageA(&msg,0,0,0,PM_REMOVE|PM_NOYIELD)) {
315 DOSVM_ProcessMessage(&msg);
316 /* we don't need a TranslateMessage here */
317 DispatchMessageA(&msg);
323 /* check for console input */
326 if (PeekConsoleInputA(objs[0],&msg,1,&num) && num) {
327 DOSVM_ProcessConsole();
331 if (read_pipe == -1) {
332 /* dispatch pending events */
333 if (SHOULD_PEND(pending_event)) {
334 CONTEXT86 context = *current_context;
337 DOSVM_SendQueuedEvents(&context);
342 struct timeval timeout={0,0};
343 /* quick check for response from dosmod
344 * (faster than doing the full blocking wait, if data already available) */
345 FD_ZERO(&readfds); FD_SET(read_pipe,&readfds);
346 if (select(read_pipe+1,&readfds,NULL,NULL,&timeout)>0)
349 /* nothing yet, block while waiting for something to do */
350 if (MsgWaitForMultipleObjects)
351 waitret = MsgWaitForMultipleObjects(objc,objs,FALSE,INFINITE,QS_ALLINPUT);
353 waitret = WaitForMultipleObjects(objc,objs,FALSE,INFINITE);
355 if (waitret==(DWORD)-1) {
356 ERR_(module)("dosvm wait error=%ld\n",GetLastError());
358 if ((read_pipe != -1) && hObject) {
359 if (waitret==(WAIT_OBJECT_0+1)) break;
361 if (waitret==WAIT_OBJECT_0)
362 goto chk_console_input;
366 DWORD WINAPI DOSVM_Loop( LPVOID lpExtra )
368 HANDLE obj = GetStdHandle(STD_INPUT_HANDLE);
373 TRACE_(int)("waiting for action\n");
374 waitret = MsgWaitForMultipleObjects(1, &obj, FALSE, INFINITE, QS_ALLINPUT);
375 if (waitret == WAIT_OBJECT_0) {
376 DOSVM_ProcessConsole();
378 else if (waitret == WAIT_OBJECT_0 + 1) {
379 while (PeekMessageA(&msg,0,0,0,PM_REMOVE)) {
381 /* it's a window message */
382 DOSVM_ProcessMessage(&msg);
383 DispatchMessageA(&msg);
385 /* it's a thread message */
386 switch (msg.message) {
388 /* stop this madness!! */
391 /* run passed procedure in this thread */
392 /* (sort of like APC, but we signal the completion) */
394 DOS_SPC *spc = (DOS_SPC *)msg.lParam;
395 TRACE_(int)("calling %p with arg %08x\n", spc->proc, spc->arg);
396 (spc->proc)(spc->arg);
397 TRACE_(int)("done, signalling event %d\n", msg.wParam);
398 SetEvent(msg.wParam);
407 ERR_(int)("MsgWaitForMultipleObjects returned unexpected value.\n");
413 static WINE_EXCEPTION_FILTER(exception_handler)
415 EXCEPTION_RECORD *rec = GetExceptionInformation()->ExceptionRecord;
416 CONTEXT *context = GetExceptionInformation()->ContextRecord;
417 int ret, arg = rec->ExceptionInformation[0];
419 switch(rec->ExceptionCode) {
420 case EXCEPTION_VM86_INTx:
421 if (TRACE_ON(relay)) {
422 DPRINTF("Call DOS int 0x%02x ret=%04lx:%04lx\n",
423 arg, context->SegCs, context->Eip );
424 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
425 context->Eax, context->Ebx, context->Ecx, context->Edx,
426 context->Esi, context->Edi );
427 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
428 context->Ebp, context->Esp, context->SegDs, context->SegEs,
429 context->SegFs, context->SegGs, context->EFlags );
431 ret = DOSVM_SimulateInt(arg, context, FALSE);
432 if (TRACE_ON(relay)) {
433 DPRINTF("Ret DOS int 0x%02x ret=%04lx:%04lx\n",
434 arg, context->SegCs, context->Eip );
435 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
436 context->Eax, context->Ebx, context->Ecx, context->Edx,
437 context->Esi, context->Edi );
438 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
439 context->Ebp, context->Esp, context->SegDs, context->SegEs,
440 context->SegFs, context->SegGs, context->EFlags );
442 return ret ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_EXECUTION;
444 case EXCEPTION_VM86_STI:
445 /* case EXCEPTION_VM86_PICRETURN: */
447 EnterCriticalSection(&qcrit);
449 while (NtCurrentTeb()->alarms) {
450 DOSVM_QueueEvent(0,DOS_PRIORITY_REALTIME,NULL,NULL);
451 /* hmm, instead of relying on this signal counter, we should
452 * probably check how many ticks have *really* passed, probably using
453 * QueryPerformanceCounter() or something like that */
454 InterlockedDecrement(&(NtCurrentTeb()->alarms));
456 TRACE_(int)("context=%p, current=%p\n", context, current_context);
457 TRACE_(int)("cs:ip=%04lx:%04lx, ss:sp=%04lx:%04lx\n", context->SegCs, context->Eip, context->SegSs, context->Esp);
458 if (!ISV86(context)) {
459 ERR_(int)("@#&*%%, winedos signal handling is *still* messed up\n");
461 TRACE_(int)("DOS task enabled interrupts %s events pending, sending events (time=%ld)\n", IS_PEND(context)?"with":"without", GetTickCount());
462 DOSVM_SendQueuedEvents(context);
464 LeaveCriticalSection(&qcrit);
465 return EXCEPTION_CONTINUE_EXECUTION;
467 return EXCEPTION_CONTINUE_SEARCH;
470 int WINAPI DOSVM_Enter( CONTEXT86 *context )
472 CONTEXT86 *old_context = current_context;
474 current_context = context;
477 __wine_enter_vm86( context );
478 TRACE_(module)( "vm86 returned: %s\n", strerror(errno) );
480 __EXCEPT(exception_handler)
482 TRACE_(module)( "leaving vm86 mode\n" );
485 current_context = old_context;
489 /***********************************************************************
492 void WINAPI DOSVM_PIC_ioport_out( WORD port, BYTE val)
496 if ((port==0x20) && (val==0x20)) {
497 EnterCriticalSection(&qcrit);
499 /* EOI (End Of Interrupt) */
500 TRACE("received EOI for current IRQ, clearing\n");
501 event = current_event;
502 current_event = event->next;
504 (*event->relay)(NULL,event->data);
508 /* another event is pending, which we should probably
509 * be able to process now */
510 TRACE("another event pending, setting flag\n");
511 current_context->EFlags |= VIP_MASK;
514 WARN("EOI without active IRQ\n");
516 LeaveCriticalSection(&qcrit);
518 FIXME("unrecognized PIC command %02x\n",val);
522 /***********************************************************************
523 * SetTimer (WINEDOS.@)
525 void WINAPI DOSVM_SetTimer( UINT ticks )
527 struct itimerval tim;
530 /* the PC clocks ticks at 1193180 Hz */
531 tim.it_interval.tv_sec=0;
532 tim.it_interval.tv_usec=MulDiv(ticks,1000000,1193180);
534 if (!tim.it_interval.tv_usec) tim.it_interval.tv_usec=1;
535 /* first tick value */
536 tim.it_value = tim.it_interval;
537 TRACE_(int)("setting timer tick delay to %ld us\n", tim.it_interval.tv_usec);
538 setitimer(ITIMER_REAL, &tim, NULL);
542 /***********************************************************************
543 * GetTimer (WINEDOS.@)
545 UINT WINAPI DOSVM_GetTimer( void )
547 struct itimerval tim;
550 getitimer(ITIMER_REAL, &tim);
551 return MulDiv(tim.it_value.tv_usec,1193180,1000000);
556 #else /* !MZ_SUPPORTED */
558 /***********************************************************************
561 INT WINAPI DOSVM_Enter( CONTEXT86 *context )
563 ERR_(module)("DOS realmode not supported on this architecture!\n");
567 /***********************************************************************
570 void WINAPI DOSVM_Wait( INT read_pipe, HANDLE hObject) {}
572 /***********************************************************************
575 void WINAPI DOSVM_PIC_ioport_out( WORD port, BYTE val) {}
577 /***********************************************************************
578 * SetTimer (WINEDOS.@)
580 void WINAPI DOSVM_SetTimer( UINT ticks ) {}
582 /***********************************************************************
583 * GetTimer (WINEDOS.@)
585 UINT WINAPI DOSVM_GetTimer( void ) { return 0; }
587 /***********************************************************************
588 * QueueEvent (WINEDOS.@)
590 void WINAPI DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
593 /* callback event, perform it with dummy context */
595 memset(&context,0,sizeof(context));
596 (*relay)(&context,data);
598 ERR("IRQ without DOS task: should not happen");
604 /**********************************************************************
607 * Return the real mode interrupt vector for a given interrupt.
609 FARPROC16 DOSVM_GetRMHandler( BYTE intnum )
611 return ((FARPROC16*)DOSMEM_SystemBase())[intnum];
615 /**********************************************************************
618 * Set the real mode interrupt handler for a given interrupt.
620 void DOSVM_SetRMHandler( BYTE intnum, FARPROC16 handler )
622 TRACE("Set real mode interrupt vector %02x <- %04x:%04x\n",
623 intnum, HIWORD(handler), LOWORD(handler) );
624 ((FARPROC16*)DOSMEM_SystemBase())[intnum] = handler;
628 static const INTPROC real_mode_handlers[] =
630 /* 00 */ 0, 0, 0, 0, 0, 0, 0, 0,
631 /* 08 */ 0, DOSVM_Int09Handler, 0, 0, 0, 0, 0, 0,
632 /* 10 */ DOSVM_Int10Handler, INT_Int11Handler, INT_Int12Handler, INT_Int13Handler,
633 0, INT_Int15Handler, DOSVM_Int16Handler, DOSVM_Int17Handler,
634 /* 18 */ 0, 0, INT_Int1aHandler, 0, 0, 0, 0, 0,
635 /* 20 */ DOSVM_Int20Handler, DOSVM_Int21Handler, 0, 0, 0, INT_Int25Handler, 0, 0,
636 /* 28 */ 0, DOSVM_Int29Handler, INT_Int2aHandler, 0, 0, 0, 0, INT_Int2fHandler,
637 /* 30 */ 0, DOSVM_Int31Handler, 0, DOSVM_Int33Handler, 0, 0, 0, 0,
638 /* 38 */ 0, 0, 0, 0, 0, 0, 0, 0,
639 /* 40 */ 0, 0, 0, 0, 0, 0, 0, 0,
640 /* 48 */ 0, 0, 0, 0, 0, 0, 0, 0,
641 /* 50 */ 0, 0, 0, 0, 0, 0, 0, 0,
642 /* 58 */ 0, 0, 0, 0, 0, 0, 0, 0,
643 /* 60 */ 0, 0, 0, 0, 0, 0, 0, DOSVM_Int67Handler
647 /**********************************************************************
648 * DOSVM_RealModeInterrupt
650 * Handle real mode interrupts
652 void DOSVM_RealModeInterrupt( BYTE intnum, CONTEXT86 *context )
654 if (intnum < sizeof(real_mode_handlers)/sizeof(INTPROC) && real_mode_handlers[intnum])
655 (*real_mode_handlers[intnum])(context);
658 FIXME("Unknown Interrupt in DOS mode: 0x%x\n", intnum);
659 FIXME(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx\n",
660 context->Eax, context->Ebx, context->Ecx, context->Edx);
661 FIXME(" esi=%08lx edi=%08lx ds=%04lx es=%04lx\n",
662 context->Esi, context->Edi, context->SegDs, context->SegEs );
667 /**********************************************************************
670 BOOL WINAPI DOSVM_Init( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
672 TRACE_(module)("(0x%08x,%ld,%p)\n", hinstDLL, fdwReason, lpvReserved);
674 if (fdwReason == DLL_PROCESS_ATTACH)
676 /* initialize the memory */
677 TRACE("Initializing DOS memory structures\n");
679 DOSDEV_InstallDOSDevices();