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"
57 WINE_DEFAULT_DEBUG_CHANNEL(int);
58 WINE_DECLARE_DEBUG_CHANNEL(module);
59 WINE_DECLARE_DEBUG_CHANNEL(relay);
62 WORD DOSVM_retval = 0;
66 #ifdef HAVE_SYS_VM86_H
67 # include <sys/vm86.h>
69 #ifdef HAVE_SYS_MMAN_H
70 # include <sys/mman.h>
73 #define IF_CLR(ctx) ((ctx)->EFlags &= ~VIF_MASK)
74 #define IF_SET(ctx) ((ctx)->EFlags |= VIF_MASK)
75 #define IF_ENABLED(ctx) ((ctx)->EFlags & VIF_MASK)
76 #define SET_PEND(ctx) ((ctx)->EFlags |= VIP_MASK)
77 #define CLR_PEND(ctx) ((ctx)->EFlags &= ~VIP_MASK)
78 #define IS_PEND(ctx) ((ctx)->EFlags & VIP_MASK)
82 typedef struct _DOSEVENT {
86 struct _DOSEVENT *next;
87 } DOSEVENT, *LPDOSEVENT;
89 static CRITICAL_SECTION qcrit = CRITICAL_SECTION_INIT("DOSVM");
90 static struct _DOSEVENT *pending_event, *current_event;
92 static HANDLE event_notifier;
93 static CONTEXT86 *current_context;
95 static int DOSVM_SimulateInt( int vect, CONTEXT86 *context, BOOL inwine )
97 FARPROC16 handler=DOSVM_GetRMHandler(vect);
99 /* check for our real-mode hooks */
101 if (context->SegCs==DOSVM_dpmi_segments->wrap_seg) {
102 /* exit from real-mode wrapper */
105 /* we could probably move some other dodgy stuff here too from dpmi.c */
107 /* check if the call is from our fake BIOS interrupt stubs */
108 if ((context->SegCs==0xf000) && !inwine) {
109 if (vect != (context->Eip/4)) {
110 TRACE("something fishy going on here (interrupt stub is %02lx)\n", context->Eip/4);
112 TRACE("builtin interrupt %02x has been branched to\n", vect);
113 DOSVM_RealModeInterrupt(vect, context);
115 /* check if the call goes to an unhooked interrupt */
116 else if (SELECTOROF(handler)==0xf000) {
117 /* if so, call it directly */
118 TRACE("builtin interrupt %02x has been invoked (through vector %02x)\n", OFFSETOF(handler)/4, vect);
119 DOSVM_RealModeInterrupt(OFFSETOF(handler)/4, context);
121 /* the interrupt is hooked, simulate interrupt in DOS space */
123 WORD*stack= PTR_REAL_TO_LIN( context->SegSs, context->Esp );
124 WORD flag=LOWORD(context->EFlags);
126 TRACE_(int)("invoking hooked interrupt %02x at %04x:%04x\n", vect,
127 SELECTOROF(handler), OFFSETOF(handler));
128 if (IF_ENABLED(context)) flag|=IF_MASK;
132 *(--stack)=context->SegCs;
133 *(--stack)=LOWORD(context->Eip);
135 context->SegCs=SELECTOROF(handler);
136 context->Eip=OFFSETOF(handler);
142 #define SHOULD_PEND(x) \
143 (x && ((!current_event) || (x->priority < current_event->priority)))
145 static void DOSVM_SendQueuedEvent(CONTEXT86 *context)
147 LPDOSEVENT event = pending_event;
149 if (SHOULD_PEND(event)) {
150 /* remove from "pending" list */
151 pending_event = event->next;
154 /* it's an IRQ, move it to "current" list */
155 event->next = current_event;
156 current_event = event;
157 TRACE("dispatching IRQ %d\n",event->irq);
158 /* note that if DOSVM_SimulateInt calls an internal interrupt directly,
159 * current_event might be cleared (and event freed) in this very call! */
160 DOSVM_SimulateInt((event->irq<8)?(event->irq+8):(event->irq-8+0x70),context,TRUE);
163 TRACE("dispatching callback event\n");
164 (*event->relay)(context,event->data);
168 if (!SHOULD_PEND(pending_event)) {
169 TRACE("clearing Pending flag\n");
174 static void DOSVM_SendQueuedEvents(CONTEXT86 *context)
176 /* we will send all queued events as long as interrupts are enabled,
177 * but IRQ events will disable interrupts again */
178 while (IS_PEND(context) && IF_ENABLED(context))
179 DOSVM_SendQueuedEvent(context);
182 /***********************************************************************
183 * QueueEvent (WINEDOS.@)
185 void WINAPI DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
187 LPDOSEVENT event, cur, prev;
189 if (current_context) {
190 EnterCriticalSection(&qcrit);
191 event = malloc(sizeof(DOSEVENT));
193 ERR("out of memory allocating event entry\n");
196 event->irq = irq; event->priority = priority;
197 event->relay = relay; event->data = data;
199 /* insert event into linked list, in order *after*
200 * all earlier events of higher or equal priority */
201 cur = pending_event; prev = NULL;
202 while (cur && cur->priority<=priority) {
207 if (prev) prev->next = event;
208 else pending_event = event;
210 /* alert the vm86 about the new event */
212 TRACE("new event queued, signalling (time=%ld)\n", GetTickCount());
213 kill(dosvm_pid,SIGUSR2);
216 TRACE("new event queued (time=%ld)\n", GetTickCount());
219 /* Wake up DOSVM_Wait so that it can serve pending events. */
220 SetEvent(event_notifier);
222 LeaveCriticalSection(&qcrit);
224 /* DOS subsystem not running */
225 /* (this probably means that we're running a win16 app
226 * which uses DPMI to thunk down to DOS services) */
228 /* callback event, perform it with dummy context */
230 memset(&context,0,sizeof(context));
231 (*relay)(&context,data);
233 ERR("IRQ without DOS task: should not happen\n");
238 static void DOSVM_ProcessConsole(void)
244 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res)) {
245 switch (msg.EventType) {
247 scan = msg.Event.KeyEvent.wVirtualScanCode;
248 ascii = msg.Event.KeyEvent.uChar.AsciiChar;
249 TRACE("scan %02x, ascii %02x\n", scan, ascii);
251 /* set the "break" (release) flag if key released */
252 if (!msg.Event.KeyEvent.bKeyDown) scan |= 0x80;
254 /* check whether extended bit is set,
255 * and if so, queue the extension prefix */
256 if (msg.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY) {
257 DOSVM_Int09SendScan(0xE0,0);
259 DOSVM_Int09SendScan(scan, ascii);
262 DOSVM_Int33Console(&msg.Event.MouseEvent);
264 case WINDOW_BUFFER_SIZE_EVENT:
265 FIXME("unhandled WINDOW_BUFFER_SIZE_EVENT.\n");
268 FIXME("unhandled MENU_EVENT.\n");
271 FIXME("unhandled FOCUS_EVENT.\n");
274 FIXME("unknown console event: %d\n", msg.EventType);
279 static void DOSVM_ProcessMessage(MSG *msg)
283 TRACE("got message %04x, wparam=%08x, lparam=%08lx\n",msg->message,msg->wParam,msg->lParam);
284 if ((msg->message>=WM_MOUSEFIRST)&&
285 (msg->message<=WM_MOUSELAST)) {
286 DOSVM_Int33Message(msg->message,msg->wParam,msg->lParam);
288 switch (msg->message) {
292 scan |= (msg->lParam >> 16) & 0x7f;
294 /* check whether extended bit is set,
295 * and if so, queue the extension prefix */
296 if (msg->lParam & 0x1000000) {
297 /* FIXME: some keys (function keys) have
298 * extended bit set even when they shouldn't,
299 * should check for them */
300 DOSVM_Int09SendScan(0xE0,0);
302 DOSVM_Int09SendScan(scan,0);
308 /***********************************************************************
311 void WINAPI DOSVM_Wait( INT read_pipe, HANDLE hObject )
317 BOOL got_msg = FALSE;
319 objs[0]=GetStdHandle(STD_INPUT_HANDLE);
320 objs[1]=event_notifier;
324 /* check for messages (waste time before the response check below) */
327 while (PeekMessageA(&msg,0,0,0,PM_REMOVE|PM_NOYIELD)) {
329 DOSVM_ProcessMessage(&msg);
330 /* we don't need a TranslateMessage here */
331 DispatchMessageA(&msg);
337 /* check for console input */
340 if (PeekConsoleInputA(objs[0],&msg,1,&num) && num) {
341 DOSVM_ProcessConsole();
345 if (read_pipe == -1) {
346 /* dispatch pending events */
347 if (SHOULD_PEND(pending_event)) {
348 CONTEXT86 context = *current_context;
351 DOSVM_SendQueuedEvents(&context);
357 struct timeval timeout={0,0};
358 /* quick check for response from dosmod
359 * (faster than doing the full blocking wait, if data already available) */
360 FD_ZERO(&readfds); FD_SET(read_pipe,&readfds);
361 if (select(read_pipe+1,&readfds,NULL,NULL,&timeout)>0)
364 /* nothing yet, block while waiting for something to do */
365 if (MsgWaitForMultipleObjects)
366 waitret = MsgWaitForMultipleObjects(objc,objs,FALSE,INFINITE,QS_ALLINPUT);
368 waitret = WaitForMultipleObjects(objc,objs,FALSE,INFINITE);
370 if (waitret==(DWORD)-1) {
371 ERR_(module)("dosvm wait error=%ld\n",GetLastError());
373 if ((read_pipe != -1) && hObject) {
374 if (waitret==(WAIT_OBJECT_0+2)) break;
376 if (waitret==WAIT_OBJECT_0)
377 goto chk_console_input;
381 DWORD WINAPI DOSVM_Loop( HANDLE hThread )
387 objs[0] = GetStdHandle(STD_INPUT_HANDLE);
391 TRACE_(int)("waiting for action\n");
392 waitret = MsgWaitForMultipleObjects(2, objs, FALSE, INFINITE, QS_ALLINPUT);
393 if (waitret == WAIT_OBJECT_0) {
394 DOSVM_ProcessConsole();
396 else if (waitret == WAIT_OBJECT_0 + 1) {
398 if(!GetExitCodeThread(hThread, &rv)) {
399 ERR("Failed to get thread exit code!\n");
404 else if (waitret == WAIT_OBJECT_0 + 2) {
405 while (PeekMessageA(&msg,0,0,0,PM_REMOVE)) {
407 /* it's a window message */
408 DOSVM_ProcessMessage(&msg);
409 DispatchMessageA(&msg);
411 /* it's a thread message */
412 switch (msg.message) {
414 /* stop this madness!! */
417 /* run passed procedure in this thread */
418 /* (sort of like APC, but we signal the completion) */
420 DOS_SPC *spc = (DOS_SPC *)msg.lParam;
421 TRACE_(int)("calling %p with arg %08lx\n", spc->proc, spc->arg);
422 (spc->proc)(spc->arg);
423 TRACE_(int)("done, signalling event %x\n", msg.wParam);
424 SetEvent( (HANDLE)msg.wParam );
433 ERR_(int)("MsgWaitForMultipleObjects returned unexpected value.\n");
439 static WINE_EXCEPTION_FILTER(exception_handler)
441 EXCEPTION_RECORD *rec = GetExceptionInformation()->ExceptionRecord;
442 CONTEXT *context = GetExceptionInformation()->ContextRecord;
443 int ret, arg = rec->ExceptionInformation[0];
445 switch(rec->ExceptionCode) {
446 case EXCEPTION_VM86_INTx:
447 if (TRACE_ON(relay)) {
448 DPRINTF("Call DOS int 0x%02x ret=%04lx:%04lx\n",
449 arg, context->SegCs, context->Eip );
450 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
451 context->Eax, context->Ebx, context->Ecx, context->Edx,
452 context->Esi, context->Edi );
453 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
454 context->Ebp, context->Esp, context->SegDs, context->SegEs,
455 context->SegFs, context->SegGs, context->EFlags );
457 ret = DOSVM_SimulateInt(arg, context, FALSE);
458 if (TRACE_ON(relay)) {
459 DPRINTF("Ret DOS int 0x%02x ret=%04lx:%04lx\n",
460 arg, context->SegCs, context->Eip );
461 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
462 context->Eax, context->Ebx, context->Ecx, context->Edx,
463 context->Esi, context->Edi );
464 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
465 context->Ebp, context->Esp, context->SegDs, context->SegEs,
466 context->SegFs, context->SegGs, context->EFlags );
468 return ret ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_EXECUTION;
470 case EXCEPTION_VM86_STI:
471 /* case EXCEPTION_VM86_PICRETURN: */
473 EnterCriticalSection(&qcrit);
475 while (NtCurrentTeb()->alarms) {
476 DOSVM_QueueEvent(0,DOS_PRIORITY_REALTIME,NULL,NULL);
477 /* hmm, instead of relying on this signal counter, we should
478 * probably check how many ticks have *really* passed, probably using
479 * QueryPerformanceCounter() or something like that */
480 InterlockedDecrement(&(NtCurrentTeb()->alarms));
482 TRACE_(int)("context=%p, current=%p\n", context, current_context);
483 TRACE_(int)("cs:ip=%04lx:%04lx, ss:sp=%04lx:%04lx\n", context->SegCs, context->Eip, context->SegSs, context->Esp);
484 if (!ISV86(context)) {
485 ERR_(int)("@#&*%%, winedos signal handling is *still* messed up\n");
487 TRACE_(int)("DOS task enabled interrupts %s events pending, sending events (time=%ld)\n", IS_PEND(context)?"with":"without", GetTickCount());
488 DOSVM_SendQueuedEvents(context);
490 LeaveCriticalSection(&qcrit);
491 return EXCEPTION_CONTINUE_EXECUTION;
493 return EXCEPTION_CONTINUE_SEARCH;
496 int WINAPI DOSVM_Enter( CONTEXT86 *context )
498 CONTEXT86 *old_context = current_context;
500 current_context = context;
503 __wine_enter_vm86( context );
504 TRACE_(module)( "vm86 returned: %s\n", strerror(errno) );
506 __EXCEPT(exception_handler)
508 TRACE_(module)( "leaving vm86 mode\n" );
511 current_context = old_context;
515 /***********************************************************************
518 void WINAPI DOSVM_PIC_ioport_out( WORD port, BYTE val)
522 if ((port==0x20) && (val==0x20)) {
523 EnterCriticalSection(&qcrit);
525 /* EOI (End Of Interrupt) */
526 TRACE("received EOI for current IRQ, clearing\n");
527 event = current_event;
528 current_event = event->next;
530 (*event->relay)(NULL,event->data);
534 /* another event is pending, which we should probably
535 * be able to process now */
536 TRACE("another event pending, setting flag\n");
537 current_context->EFlags |= VIP_MASK;
540 WARN("EOI without active IRQ\n");
542 LeaveCriticalSection(&qcrit);
544 FIXME("unrecognized PIC command %02x\n",val);
548 /***********************************************************************
549 * SetTimer (WINEDOS.@)
551 void WINAPI DOSVM_SetTimer( UINT ticks )
553 struct itimerval tim;
556 /* the PC clocks ticks at 1193180 Hz */
557 tim.it_interval.tv_sec=0;
558 tim.it_interval.tv_usec=MulDiv(ticks,1000000,1193180);
560 if (!tim.it_interval.tv_usec) tim.it_interval.tv_usec=1;
561 /* first tick value */
562 tim.it_value = tim.it_interval;
563 TRACE_(int)("setting timer tick delay to %ld us\n", tim.it_interval.tv_usec);
564 setitimer(ITIMER_REAL, &tim, NULL);
568 /***********************************************************************
569 * GetTimer (WINEDOS.@)
571 UINT WINAPI DOSVM_GetTimer( void )
573 struct itimerval tim;
576 getitimer(ITIMER_REAL, &tim);
577 return MulDiv(tim.it_value.tv_usec,1193180,1000000);
582 #else /* !MZ_SUPPORTED */
584 /***********************************************************************
587 INT WINAPI DOSVM_Enter( CONTEXT86 *context )
589 ERR_(module)("DOS realmode not supported on this architecture!\n");
593 /***********************************************************************
596 void WINAPI DOSVM_Wait( INT read_pipe, HANDLE hObject) {}
598 /***********************************************************************
601 void WINAPI DOSVM_PIC_ioport_out( WORD port, BYTE val) {}
603 /***********************************************************************
604 * SetTimer (WINEDOS.@)
606 void WINAPI DOSVM_SetTimer( UINT ticks ) {}
608 /***********************************************************************
609 * GetTimer (WINEDOS.@)
611 UINT WINAPI DOSVM_GetTimer( void ) { return 0; }
613 /***********************************************************************
614 * QueueEvent (WINEDOS.@)
616 void WINAPI DOSVM_QueueEvent( INT irq, INT priority, DOSRELAY relay, LPVOID data)
619 /* callback event, perform it with dummy context */
621 memset(&context,0,sizeof(context));
622 (*relay)(&context,data);
624 ERR("IRQ without DOS task: should not happen\n");
630 /**********************************************************************
631 * DOSVM_RealModeInterrupt
633 * Handle real mode interrupts
635 void DOSVM_RealModeInterrupt( BYTE intnum, CONTEXT86 *context )
637 INTPROC proc = DOSVM_GetBuiltinHandler( intnum );
642 /**********************************************************************
643 * DllMain (DOSVM.Init)
645 BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
647 TRACE_(module)("(%p,%ld,%p)\n", hinstDLL, fdwReason, lpvReserved);
649 if (fdwReason == DLL_PROCESS_ATTACH)
651 DOSVM_InitSegments();
654 event_notifier = CreateEventA(NULL, FALSE, FALSE, NULL);
656 ERR("Failed to create event object!\n");