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.
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
37 #include <sys/types.h>
39 #include "wine/winbase16.h"
40 #include "wine/exception.h"
53 #include "stackframe.h"
54 #include "wine/debug.h"
55 #include "msvcrt/excpt.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(int);
58 WINE_DECLARE_DEBUG_CHANNEL(module);
59 WINE_DECLARE_DEBUG_CHANNEL(relay);
62 WORD DOSVM_retval = 0;
63 const struct DPMI_segments *DOSVM_dpmi_segments = NULL;
67 #ifdef HAVE_SYS_VM86_H
68 # include <sys/vm86.h>
70 #ifdef HAVE_SYS_MMAN_H
71 # include <sys/mman.h>
74 #define IF_CLR(ctx) ((ctx)->EFlags &= ~VIF_MASK)
75 #define IF_SET(ctx) ((ctx)->EFlags |= VIF_MASK)
76 #define IF_ENABLED(ctx) ((ctx)->EFlags & VIF_MASK)
77 #define SET_PEND(ctx) ((ctx)->EFlags |= VIP_MASK)
78 #define CLR_PEND(ctx) ((ctx)->EFlags &= ~VIP_MASK)
79 #define IS_PEND(ctx) ((ctx)->EFlags & VIP_MASK)
83 typedef struct _DOSEVENT {
87 struct _DOSEVENT *next;
88 } DOSEVENT, *LPDOSEVENT;
90 static CRITICAL_SECTION qcrit = CRITICAL_SECTION_INIT("DOSVM");
91 static struct _DOSEVENT *pending_event, *current_event;
93 static HANDLE event_notifier;
94 static CONTEXT86 *current_context;
96 static int DOSVM_SimulateInt( int vect, CONTEXT86 *context, BOOL inwine )
98 FARPROC16 handler=DOSVM_GetRMHandler(vect);
100 /* check for our real-mode hooks */
102 if (context->SegCs==DOSVM_dpmi_segments->wrap_seg) {
103 /* exit from real-mode wrapper */
106 /* we could probably move some other dodgy stuff here too from dpmi.c */
108 /* check if the call is from our fake BIOS interrupt stubs */
109 if ((context->SegCs==0xf000) && !inwine) {
110 if (vect != (context->Eip/4)) {
111 TRACE("something fishy going on here (interrupt stub is %02lx)\n", context->Eip/4);
113 TRACE("builtin interrupt %02x has been branched to\n", vect);
114 DOSVM_RealModeInterrupt(vect, context);
116 /* check if the call goes to an unhooked interrupt */
117 else if (SELECTOROF(handler)==0xf000) {
118 /* if so, call it directly */
119 TRACE("builtin interrupt %02x has been invoked (through vector %02x)\n", OFFSETOF(handler)/4, vect);
120 DOSVM_RealModeInterrupt(OFFSETOF(handler)/4, context);
122 /* the interrupt is hooked, simulate interrupt in DOS space */
124 WORD*stack= PTR_REAL_TO_LIN( context->SegSs, context->Esp );
125 WORD flag=LOWORD(context->EFlags);
127 TRACE_(int)("invoking hooked interrupt %02x at %04x:%04x\n", vect,
128 SELECTOROF(handler), OFFSETOF(handler));
129 if (IF_ENABLED(context)) flag|=IF_MASK;
133 *(--stack)=context->SegCs;
134 *(--stack)=LOWORD(context->Eip);
136 context->SegCs=SELECTOROF(handler);
137 context->Eip=OFFSETOF(handler);
143 #define SHOULD_PEND(x) \
144 (x && ((!current_event) || (x->priority < current_event->priority)))
146 static void DOSVM_SendQueuedEvent(CONTEXT86 *context)
148 LPDOSEVENT event = pending_event;
150 if (SHOULD_PEND(event)) {
151 /* remove from "pending" list */
152 pending_event = event->next;
155 /* it's an IRQ, move it to "current" list */
156 event->next = current_event;
157 current_event = event;
158 TRACE("dispatching IRQ %d\n",event->irq);
159 /* note that if DOSVM_SimulateInt calls an internal interrupt directly,
160 * current_event might be cleared (and event freed) in this very call! */
161 DOSVM_SimulateInt((event->irq<8)?(event->irq+8):(event->irq-8+0x70),context,TRUE);
164 TRACE("dispatching callback event\n");
165 (*event->relay)(context,event->data);
169 if (!SHOULD_PEND(pending_event)) {
170 TRACE("clearing Pending flag\n");
175 static void DOSVM_SendQueuedEvents(CONTEXT86 *context)
177 /* we will send all queued events as long as interrupts are enabled,
178 * but IRQ events will disable interrupts again */
179 while (IS_PEND(context) && IF_ENABLED(context))
180 DOSVM_SendQueuedEvent(context);
183 /***********************************************************************
184 * QueueEvent (WINEDOS.@)
186 void WINAPI DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
188 LPDOSEVENT event, cur, prev;
190 if (current_context) {
191 EnterCriticalSection(&qcrit);
192 event = malloc(sizeof(DOSEVENT));
194 ERR("out of memory allocating event entry\n");
197 event->irq = irq; event->priority = priority;
198 event->relay = relay; event->data = data;
200 /* insert event into linked list, in order *after*
201 * all earlier events of higher or equal priority */
202 cur = pending_event; prev = NULL;
203 while (cur && cur->priority<=priority) {
208 if (prev) prev->next = event;
209 else pending_event = event;
211 /* alert the vm86 about the new event */
213 TRACE("new event queued, signalling (time=%ld)\n", GetTickCount());
214 kill(dosvm_pid,SIGUSR2);
217 TRACE("new event queued (time=%ld)\n", GetTickCount());
220 /* Wake up DOSVM_Wait so that it can serve pending events. */
221 SetEvent(event_notifier);
223 LeaveCriticalSection(&qcrit);
225 /* DOS subsystem not running */
226 /* (this probably means that we're running a win16 app
227 * which uses DPMI to thunk down to DOS services) */
229 /* callback event, perform it with dummy context */
231 memset(&context,0,sizeof(context));
232 (*relay)(&context,data);
234 ERR("IRQ without DOS task: should not happen\n");
239 static void DOSVM_ProcessConsole(void)
245 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res)) {
246 switch (msg.EventType) {
248 scan = msg.Event.KeyEvent.wVirtualScanCode;
249 if (!msg.Event.KeyEvent.bKeyDown) scan |= 0x80;
251 /* check whether extended bit is set,
252 * and if so, queue the extension prefix */
253 if (msg.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY) {
254 DOSVM_Int09SendScan(0xE0,0);
256 DOSVM_Int09SendScan(scan,msg.Event.KeyEvent.uChar.AsciiChar);
259 DOSVM_Int33Console(&msg.Event.MouseEvent);
261 case WINDOW_BUFFER_SIZE_EVENT:
262 FIXME("unhandled WINDOW_BUFFER_SIZE_EVENT.\n");
265 FIXME("unhandled MENU_EVENT.\n");
268 FIXME("unhandled FOCUS_EVENT.\n");
271 FIXME("unknown console event: %d\n", msg.EventType);
276 static void DOSVM_ProcessMessage(MSG *msg)
280 TRACE("got message %04x, wparam=%08x, lparam=%08lx\n",msg->message,msg->wParam,msg->lParam);
281 if ((msg->message>=WM_MOUSEFIRST)&&
282 (msg->message<=WM_MOUSELAST)) {
283 DOSVM_Int33Message(msg->message,msg->wParam,msg->lParam);
285 switch (msg->message) {
289 scan |= (msg->lParam >> 16) & 0x7f;
291 /* check whether extended bit is set,
292 * and if so, queue the extension prefix */
293 if (msg->lParam & 0x1000000) {
294 /* FIXME: some keys (function keys) have
295 * extended bit set even when they shouldn't,
296 * should check for them */
297 DOSVM_Int09SendScan(0xE0,0);
299 DOSVM_Int09SendScan(scan,0);
305 /***********************************************************************
308 void WINAPI DOSVM_Wait( INT read_pipe, HANDLE hObject )
314 BOOL got_msg = FALSE;
316 objs[0]=GetStdHandle(STD_INPUT_HANDLE);
317 objs[1]=event_notifier;
321 /* check for messages (waste time before the response check below) */
324 while (PeekMessageA(&msg,0,0,0,PM_REMOVE|PM_NOYIELD)) {
326 DOSVM_ProcessMessage(&msg);
327 /* we don't need a TranslateMessage here */
328 DispatchMessageA(&msg);
334 /* check for console input */
337 if (PeekConsoleInputA(objs[0],&msg,1,&num) && num) {
338 DOSVM_ProcessConsole();
342 if (read_pipe == -1) {
343 /* dispatch pending events */
344 if (SHOULD_PEND(pending_event)) {
345 CONTEXT86 context = *current_context;
348 DOSVM_SendQueuedEvents(&context);
354 struct timeval timeout={0,0};
355 /* quick check for response from dosmod
356 * (faster than doing the full blocking wait, if data already available) */
357 FD_ZERO(&readfds); FD_SET(read_pipe,&readfds);
358 if (select(read_pipe+1,&readfds,NULL,NULL,&timeout)>0)
361 /* nothing yet, block while waiting for something to do */
362 if (MsgWaitForMultipleObjects)
363 waitret = MsgWaitForMultipleObjects(objc,objs,FALSE,INFINITE,QS_ALLINPUT);
365 waitret = WaitForMultipleObjects(objc,objs,FALSE,INFINITE);
367 if (waitret==(DWORD)-1) {
368 ERR_(module)("dosvm wait error=%ld\n",GetLastError());
370 if ((read_pipe != -1) && hObject) {
371 if (waitret==(WAIT_OBJECT_0+2)) break;
373 if (waitret==WAIT_OBJECT_0)
374 goto chk_console_input;
378 DWORD WINAPI DOSVM_Loop( HANDLE hThread )
384 objs[0] = GetStdHandle(STD_INPUT_HANDLE);
388 TRACE_(int)("waiting for action\n");
389 waitret = MsgWaitForMultipleObjects(2, objs, FALSE, INFINITE, QS_ALLINPUT);
390 if (waitret == WAIT_OBJECT_0) {
391 DOSVM_ProcessConsole();
393 else if (waitret == WAIT_OBJECT_0 + 1) {
395 if(!GetExitCodeThread(hThread, &rv)) {
396 ERR("Failed to get thread exit code!\n");
401 else if (waitret == WAIT_OBJECT_0 + 2) {
402 while (PeekMessageA(&msg,0,0,0,PM_REMOVE)) {
404 /* it's a window message */
405 DOSVM_ProcessMessage(&msg);
406 DispatchMessageA(&msg);
408 /* it's a thread message */
409 switch (msg.message) {
411 /* stop this madness!! */
414 /* run passed procedure in this thread */
415 /* (sort of like APC, but we signal the completion) */
417 DOS_SPC *spc = (DOS_SPC *)msg.lParam;
418 TRACE_(int)("calling %p with arg %08lx\n", spc->proc, spc->arg);
419 (spc->proc)(spc->arg);
420 TRACE_(int)("done, signalling event %x\n", msg.wParam);
421 SetEvent( (HANDLE)msg.wParam );
430 ERR_(int)("MsgWaitForMultipleObjects returned unexpected value.\n");
436 static WINE_EXCEPTION_FILTER(exception_handler)
438 EXCEPTION_RECORD *rec = GetExceptionInformation()->ExceptionRecord;
439 CONTEXT *context = GetExceptionInformation()->ContextRecord;
440 int ret, arg = rec->ExceptionInformation[0];
442 switch(rec->ExceptionCode) {
443 case EXCEPTION_VM86_INTx:
444 if (TRACE_ON(relay)) {
445 DPRINTF("Call DOS int 0x%02x ret=%04lx:%04lx\n",
446 arg, context->SegCs, context->Eip );
447 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
448 context->Eax, context->Ebx, context->Ecx, context->Edx,
449 context->Esi, context->Edi );
450 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
451 context->Ebp, context->Esp, context->SegDs, context->SegEs,
452 context->SegFs, context->SegGs, context->EFlags );
454 ret = DOSVM_SimulateInt(arg, context, FALSE);
455 if (TRACE_ON(relay)) {
456 DPRINTF("Ret DOS int 0x%02x ret=%04lx:%04lx\n",
457 arg, context->SegCs, context->Eip );
458 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
459 context->Eax, context->Ebx, context->Ecx, context->Edx,
460 context->Esi, context->Edi );
461 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
462 context->Ebp, context->Esp, context->SegDs, context->SegEs,
463 context->SegFs, context->SegGs, context->EFlags );
465 return ret ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_EXECUTION;
467 case EXCEPTION_VM86_STI:
468 /* case EXCEPTION_VM86_PICRETURN: */
470 EnterCriticalSection(&qcrit);
472 while (NtCurrentTeb()->alarms) {
473 DOSVM_QueueEvent(0,DOS_PRIORITY_REALTIME,NULL,NULL);
474 /* hmm, instead of relying on this signal counter, we should
475 * probably check how many ticks have *really* passed, probably using
476 * QueryPerformanceCounter() or something like that */
477 InterlockedDecrement(&(NtCurrentTeb()->alarms));
479 TRACE_(int)("context=%p, current=%p\n", context, current_context);
480 TRACE_(int)("cs:ip=%04lx:%04lx, ss:sp=%04lx:%04lx\n", context->SegCs, context->Eip, context->SegSs, context->Esp);
481 if (!ISV86(context)) {
482 ERR_(int)("@#&*%%, winedos signal handling is *still* messed up\n");
484 TRACE_(int)("DOS task enabled interrupts %s events pending, sending events (time=%ld)\n", IS_PEND(context)?"with":"without", GetTickCount());
485 DOSVM_SendQueuedEvents(context);
487 LeaveCriticalSection(&qcrit);
488 return EXCEPTION_CONTINUE_EXECUTION;
490 return EXCEPTION_CONTINUE_SEARCH;
493 int WINAPI DOSVM_Enter( CONTEXT86 *context )
495 CONTEXT86 *old_context = current_context;
497 current_context = context;
500 __wine_enter_vm86( context );
501 TRACE_(module)( "vm86 returned: %s\n", strerror(errno) );
503 __EXCEPT(exception_handler)
505 TRACE_(module)( "leaving vm86 mode\n" );
508 current_context = old_context;
512 /***********************************************************************
515 void WINAPI DOSVM_PIC_ioport_out( WORD port, BYTE val)
519 if ((port==0x20) && (val==0x20)) {
520 EnterCriticalSection(&qcrit);
522 /* EOI (End Of Interrupt) */
523 TRACE("received EOI for current IRQ, clearing\n");
524 event = current_event;
525 current_event = event->next;
527 (*event->relay)(NULL,event->data);
531 /* another event is pending, which we should probably
532 * be able to process now */
533 TRACE("another event pending, setting flag\n");
534 current_context->EFlags |= VIP_MASK;
537 WARN("EOI without active IRQ\n");
539 LeaveCriticalSection(&qcrit);
541 FIXME("unrecognized PIC command %02x\n",val);
545 /***********************************************************************
546 * SetTimer (WINEDOS.@)
548 void WINAPI DOSVM_SetTimer( UINT ticks )
550 struct itimerval tim;
553 /* the PC clocks ticks at 1193180 Hz */
554 tim.it_interval.tv_sec=0;
555 tim.it_interval.tv_usec=MulDiv(ticks,1000000,1193180);
557 if (!tim.it_interval.tv_usec) tim.it_interval.tv_usec=1;
558 /* first tick value */
559 tim.it_value = tim.it_interval;
560 TRACE_(int)("setting timer tick delay to %ld us\n", tim.it_interval.tv_usec);
561 setitimer(ITIMER_REAL, &tim, NULL);
565 /***********************************************************************
566 * GetTimer (WINEDOS.@)
568 UINT WINAPI DOSVM_GetTimer( void )
570 struct itimerval tim;
573 getitimer(ITIMER_REAL, &tim);
574 return MulDiv(tim.it_value.tv_usec,1193180,1000000);
579 #else /* !MZ_SUPPORTED */
581 /***********************************************************************
584 INT WINAPI DOSVM_Enter( CONTEXT86 *context )
586 ERR_(module)("DOS realmode not supported on this architecture!\n");
590 /***********************************************************************
593 void WINAPI DOSVM_Wait( INT read_pipe, HANDLE hObject) {}
595 /***********************************************************************
598 void WINAPI DOSVM_PIC_ioport_out( WORD port, BYTE val) {}
600 /***********************************************************************
601 * SetTimer (WINEDOS.@)
603 void WINAPI DOSVM_SetTimer( UINT ticks ) {}
605 /***********************************************************************
606 * GetTimer (WINEDOS.@)
608 UINT WINAPI DOSVM_GetTimer( void ) { return 0; }
610 /***********************************************************************
611 * QueueEvent (WINEDOS.@)
613 void WINAPI DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
616 /* callback event, perform it with dummy context */
618 memset(&context,0,sizeof(context));
619 (*relay)(&context,data);
621 ERR("IRQ without DOS task: should not happen\n");
627 /**********************************************************************
630 * Return the real mode interrupt vector for a given interrupt.
632 FARPROC16 DOSVM_GetRMHandler( BYTE intnum )
634 return ((FARPROC16*)0)[intnum];
638 /**********************************************************************
641 * Set the real mode interrupt handler for a given interrupt.
643 void DOSVM_SetRMHandler( BYTE intnum, FARPROC16 handler )
645 TRACE("Set real mode interrupt vector %02x <- %04x:%04x\n",
646 intnum, HIWORD(handler), LOWORD(handler) );
647 ((FARPROC16*)0)[intnum] = handler;
651 static const INTPROC real_mode_handlers[] =
653 /* 00 */ 0, 0, 0, 0, 0, 0, 0, 0,
654 /* 08 */ 0, DOSVM_Int09Handler, 0, 0, 0, 0, 0, 0,
655 /* 10 */ DOSVM_Int10Handler, INT_Int11Handler, INT_Int12Handler, INT_Int13Handler,
656 0, INT_Int15Handler, DOSVM_Int16Handler, DOSVM_Int17Handler,
657 /* 18 */ 0, 0, INT_Int1aHandler, 0, 0, 0, 0, 0,
658 /* 20 */ DOSVM_Int20Handler, DOSVM_Int21Handler, 0, 0, 0, INT_Int25Handler, 0, 0,
659 /* 28 */ 0, DOSVM_Int29Handler, INT_Int2aHandler, 0, 0, 0, 0, INT_Int2fHandler,
660 /* 30 */ 0, DOSVM_Int31Handler, 0, DOSVM_Int33Handler, INT_Int34Handler, INT_Int35Handler, INT_Int36Handler, INT_Int37Handler,
661 /* 38 */ INT_Int38Handler, INT_Int39Handler, INT_Int3aHandler, INT_Int3bHandler, INT_Int3cHandler, INT_Int3dHandler, INT_Int3eHandler, 0,
662 /* 40 */ 0, 0, 0, 0, 0, 0, 0, 0,
663 /* 48 */ 0, 0, 0, 0, 0, 0, 0, 0,
664 /* 50 */ 0, 0, 0, 0, 0, 0, 0, 0,
665 /* 58 */ 0, 0, 0, 0, 0, 0, 0, 0,
666 /* 60 */ 0, 0, 0, 0, 0, 0, 0, DOSVM_Int67Handler
670 /**********************************************************************
671 * DOSVM_RealModeInterrupt
673 * Handle real mode interrupts
675 void DOSVM_RealModeInterrupt( BYTE intnum, CONTEXT86 *context )
677 if (intnum < sizeof(real_mode_handlers)/sizeof(INTPROC) && real_mode_handlers[intnum])
678 (*real_mode_handlers[intnum])(context);
681 FIXME("Unknown Interrupt in DOS mode: 0x%x\n", intnum);
682 FIXME(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx\n",
683 context->Eax, context->Ebx, context->Ecx, context->Edx);
684 FIXME(" esi=%08lx edi=%08lx ds=%04lx es=%04lx\n",
685 context->Esi, context->Edi, context->SegDs, context->SegEs );
690 /**********************************************************************
693 BOOL WINAPI DOSVM_Init( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
695 TRACE_(module)("(%p,%ld,%p)\n", hinstDLL, fdwReason, lpvReserved);
697 if (fdwReason == DLL_PROCESS_ATTACH)
699 /* initialize the memory */
700 TRACE("Initializing DOS memory structures\n");
702 DOSDEV_InstallDOSDevices();
703 DOSVM_dpmi_segments = DOSMEM_GetDPMISegments();
706 event_notifier = CreateEventA(NULL, FALSE, FALSE, NULL);
708 ERR("Failed to create event object!\n");