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;
64 #ifdef HAVE_SYS_VM86_H
65 # include <sys/vm86.h>
67 #ifdef HAVE_SYS_MMAN_H
68 # include <sys/mman.h>
72 typedef struct _DOSEVENT
{
76 struct _DOSEVENT
*next
;
77 } DOSEVENT
, *LPDOSEVENT
;
79 static CRITICAL_SECTION qcrit
= CRITICAL_SECTION_INIT("DOSVM");
80 static struct _DOSEVENT
*pending_event
, *current_event
;
81 static HANDLE event_notifier
;
84 /***********************************************************************
85 * DOSVM_HasPendingEvents
87 * Return true if there are pending events that are not
88 * blocked by currently active event.
90 static BOOL
DOSVM_HasPendingEvents( void )
98 if (pending_event
->priority
< current_event
->priority
)
105 /***********************************************************************
108 * Process single pending event.
110 * This function should be called with queue critical section locked.
111 * The function temporarily releases the critical section if it is
112 * possible that internal interrupt handler or user procedure will
113 * be called. This is because we may otherwise get a deadlock if
114 * another thread is waiting for the same critical section.
116 static void DOSVM_SendOneEvent( CONTEXT86
*context
)
118 LPDOSEVENT event
= pending_event
;
120 /* Remove from pending events list. */
121 pending_event
= event
->next
;
123 /* Process active event. */
126 BYTE intnum
= (event
->irq
< 8) ?
127 (event
->irq
+ 8) : (event
->irq
- 8 + 0x70);
129 /* Event is an IRQ, move it to current events list. */
130 event
->next
= current_event
;
131 current_event
= event
;
133 TRACE( "Dispatching IRQ %d.\n", event
->irq
);
138 * Note that if DOSVM_HardwareInterruptRM calls an internal
139 * interrupt directly, current_event might be cleared
140 * (and event freed) in this call.
142 LeaveCriticalSection(&qcrit
);
143 DOSVM_HardwareInterruptRM( context
, intnum
);
144 EnterCriticalSection(&qcrit
);
149 * This routine only modifies current context so it is
150 * not necessary to release critical section.
152 DOSVM_HardwareInterruptPM( context
, intnum
);
157 /* Callback event. */
158 TRACE( "Dispatching callback event.\n" );
163 * Call relay immediately in real mode.
165 LeaveCriticalSection(&qcrit
);
166 (*event
->relay
)( context
, event
->data
);
167 EnterCriticalSection(&qcrit
);
172 * Force return to relay code. We do not want to
173 * call relay directly because we may be inside a signal handler.
175 DOSVM_BuildCallFrame( context
, event
->relay
, event
->data
);
183 /***********************************************************************
184 * DOSVM_SendQueuedEvents
186 * As long as context instruction pointer stays unmodified,
187 * process all pending events that are not blocked by currently
190 * This routine assumes that caller has already cleared TEB.vm86_pending
191 * and checked that interrupts are enabled.
193 void DOSVM_SendQueuedEvents( CONTEXT86
*context
)
195 DWORD old_cs
= context
->SegCs
;
196 DWORD old_ip
= context
->Eip
;
198 EnterCriticalSection(&qcrit
);
200 TRACE( "Called in %s mode %s events pending (time=%ld)\n",
201 ISV86(context
) ? "real" : "protected",
202 DOSVM_HasPendingEvents() ? "with" : "without",
204 TRACE( "cs:ip=%04lx:%08lx, ss:sp=%04lx:%08lx\n",
205 context
->SegCs
, context
->Eip
, context
->SegSs
, context
->Esp
);
207 while (context
->SegCs
== old_cs
&&
208 context
->Eip
== old_ip
&&
209 DOSVM_HasPendingEvents())
211 DOSVM_SendOneEvent(context
);
214 * Event handling may have turned pending events flag on.
215 * We disable it here because this prevents some
216 * unnecessary calls to this function.
218 NtCurrentTeb()->vm86_pending
= 0;
223 if (!ISV86(context
) && context
->SegCs
== old_cs
&& context
->Eip
== old_ip
)
226 * Routine was called from DPMI but there was nothing to do.
227 * We force a dummy relay call here so that we don't get a race
228 * if signals are unblocked when we return to DPMI application.
230 TRACE( "Called but there was nothing to do, calling NULL relay.\n" );
231 DOSVM_BuildCallFrame( context
, NULL
, NULL
);
234 if (DOSVM_HasPendingEvents())
237 * Interrupts disabled, but there are still
238 * pending events, make sure that pending flag is turned on.
240 TRACE( "Another event is pending, setting VIP flag.\n" );
241 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
246 FIXME("No DOS .exe file support on this platform (yet)\n");
248 #endif /* MZ_SUPPORTED */
250 LeaveCriticalSection(&qcrit
);
255 /***********************************************************************
256 * QueueEvent (WINEDOS.@)
258 void WINAPI
DOSVM_QueueEvent( INT irq
, INT priority
, DOSRELAY relay
, LPVOID data
)
260 LPDOSEVENT event
, cur
, prev
;
264 event
= malloc(sizeof(DOSEVENT
));
266 ERR("out of memory allocating event entry\n");
269 event
->irq
= irq
; event
->priority
= priority
;
270 event
->relay
= relay
; event
->data
= data
;
272 EnterCriticalSection(&qcrit
);
273 old_pending
= DOSVM_HasPendingEvents();
275 /* insert event into linked list, in order *after*
276 * all earlier events of higher or equal priority */
277 cur
= pending_event
; prev
= NULL
;
278 while (cur
&& cur
->priority
<=priority
) {
283 if (prev
) prev
->next
= event
;
284 else pending_event
= event
;
286 if (!old_pending
&& DOSVM_HasPendingEvents()) {
287 TRACE("new event queued, signalling (time=%ld)\n", GetTickCount());
289 /* Alert VM86 thread about the new event. */
290 kill(dosvm_pid
,SIGUSR2
);
292 /* Wake up DOSVM_Wait so that it can serve pending events. */
293 SetEvent(event_notifier
);
295 TRACE("new event queued (time=%ld)\n", GetTickCount());
298 LeaveCriticalSection(&qcrit
);
300 /* DOS subsystem not running */
301 /* (this probably means that we're running a win16 app
302 * which uses DPMI to thunk down to DOS services) */
304 /* callback event, perform it with dummy context */
306 memset(&context
,0,sizeof(context
));
307 (*relay
)(&context
,data
);
309 ERR("IRQ without DOS task: should not happen\n");
314 static void DOSVM_ProcessConsole(void)
320 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE
),&msg
,1,&res
)) {
321 switch (msg
.EventType
) {
323 scan
= msg
.Event
.KeyEvent
.wVirtualScanCode
;
324 ascii
= msg
.Event
.KeyEvent
.uChar
.AsciiChar
;
325 TRACE("scan %02x, ascii %02x\n", scan
, ascii
);
327 /* set the "break" (release) flag if key released */
328 if (!msg
.Event
.KeyEvent
.bKeyDown
) scan
|= 0x80;
330 /* check whether extended bit is set,
331 * and if so, queue the extension prefix */
332 if (msg
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
) {
333 DOSVM_Int09SendScan(0xE0,0);
335 DOSVM_Int09SendScan(scan
, ascii
);
338 DOSVM_Int33Console(&msg
.Event
.MouseEvent
);
340 case WINDOW_BUFFER_SIZE_EVENT
:
341 FIXME("unhandled WINDOW_BUFFER_SIZE_EVENT.\n");
344 FIXME("unhandled MENU_EVENT.\n");
347 FIXME("unhandled FOCUS_EVENT.\n");
350 FIXME("unknown console event: %d\n", msg
.EventType
);
355 static void DOSVM_ProcessMessage(MSG
*msg
)
359 TRACE("got message %04x, wparam=%08x, lparam=%08lx\n",msg
->message
,msg
->wParam
,msg
->lParam
);
360 if ((msg
->message
>=WM_MOUSEFIRST
)&&
361 (msg
->message
<=WM_MOUSELAST
)) {
362 DOSVM_Int33Message(msg
->message
,msg
->wParam
,msg
->lParam
);
364 switch (msg
->message
) {
368 scan
|= (msg
->lParam
>> 16) & 0x7f;
370 /* check whether extended bit is set,
371 * and if so, queue the extension prefix */
372 if (msg
->lParam
& 0x1000000) {
373 /* FIXME: some keys (function keys) have
374 * extended bit set even when they shouldn't,
375 * should check for them */
376 DOSVM_Int09SendScan(0xE0,0);
378 DOSVM_Int09SendScan(scan
,0);
385 /***********************************************************************
388 * Wait for asynchronous events. This routine temporarily enables
389 * interrupts and waits until some asynchronous event has been
392 void WINAPI
DOSVM_Wait( CONTEXT86
*waitctx
)
394 if (DOSVM_HasPendingEvents())
396 CONTEXT86 context
= *waitctx
;
399 * If DOSVM_Wait is called from protected mode we emulate
400 * interrupt reflection and convert context into real mode context.
401 * This is actually the correct thing to do as long as DOSVM_Wait
402 * is only called from those interrupt functions that DPMI reflects
405 * FIXME: Need to think about where to place real mode stack.
406 * FIXME: If DOSVM_Wait calls are nested stack gets corrupted.
407 * Can this really happen?
409 if (!ISV86(&context
))
411 context
.EFlags
|= 0x00020000;
412 context
.SegSs
= 0xffff;
416 context
.EFlags
|= VIF_MASK
;
420 DOSVM_SendQueuedEvents(&context
);
422 if(context
.SegCs
|| context
.Eip
)
423 DPMI_CallRMProc( &context
, NULL
, 0, TRUE
);
428 int objc
= DOSVM_IsWin16() ? 2 : 1;
431 objs
[0] = event_notifier
;
432 objs
[1] = GetStdHandle(STD_INPUT_HANDLE
);
434 waitret
= MsgWaitForMultipleObjects( objc
, objs
, FALSE
,
435 INFINITE
, QS_ALLINPUT
);
437 if (waitret
== WAIT_OBJECT_0
)
440 * New pending event has been queued, we ignore it
441 * here because it will be processed on next call to
445 else if (objc
== 2 && waitret
== WAIT_OBJECT_0
+ 1)
447 DOSVM_ProcessConsole();
449 else if (waitret
== WAIT_OBJECT_0
+ objc
)
452 while (PeekMessageA(&msg
,0,0,0,PM_REMOVE
|PM_NOYIELD
))
455 DOSVM_ProcessMessage(&msg
);
456 /* we don't need a TranslateMessage here */
457 DispatchMessageA(&msg
);
462 ERR_(module
)( "dosvm wait error=%ld\n", GetLastError() );
468 DWORD WINAPI
DOSVM_Loop( HANDLE hThread
)
474 objs
[0] = GetStdHandle(STD_INPUT_HANDLE
);
478 TRACE_(int)("waiting for action\n");
479 waitret
= MsgWaitForMultipleObjects(2, objs
, FALSE
, INFINITE
, QS_ALLINPUT
);
480 if (waitret
== WAIT_OBJECT_0
) {
481 DOSVM_ProcessConsole();
483 else if (waitret
== WAIT_OBJECT_0
+ 1) {
485 if(!GetExitCodeThread(hThread
, &rv
)) {
486 ERR("Failed to get thread exit code!\n");
491 else if (waitret
== WAIT_OBJECT_0
+ 2) {
492 while (PeekMessageA(&msg
,0,0,0,PM_REMOVE
)) {
494 /* it's a window message */
495 DOSVM_ProcessMessage(&msg
);
496 DispatchMessageA(&msg
);
498 /* it's a thread message */
499 switch (msg
.message
) {
501 /* stop this madness!! */
504 /* run passed procedure in this thread */
505 /* (sort of like APC, but we signal the completion) */
507 DOS_SPC
*spc
= (DOS_SPC
*)msg
.lParam
;
508 TRACE_(int)("calling %p with arg %08lx\n", spc
->proc
, spc
->arg
);
509 (spc
->proc
)(spc
->arg
);
510 TRACE_(int)("done, signalling event %x\n", msg
.wParam
);
511 SetEvent( (HANDLE
)msg
.wParam
);
515 DispatchMessageA(&msg
);
522 ERR_(int)("MsgWaitForMultipleObjects returned unexpected value.\n");
528 static WINE_EXCEPTION_FILTER(exception_handler
)
530 EXCEPTION_RECORD
*rec
= GetExceptionInformation()->ExceptionRecord
;
531 CONTEXT
*context
= GetExceptionInformation()->ContextRecord
;
532 int arg
= rec
->ExceptionInformation
[0];
535 switch(rec
->ExceptionCode
) {
536 case EXCEPTION_VM86_INTx
:
537 if (TRACE_ON(relay
)) {
538 DPRINTF("Call DOS int 0x%02x ret=%04lx:%04lx\n",
539 arg
, context
->SegCs
, context
->Eip
);
540 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
541 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
,
542 context
->Esi
, context
->Edi
);
543 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
544 context
->Ebp
, context
->Esp
, context
->SegDs
, context
->SegEs
,
545 context
->SegFs
, context
->SegGs
, context
->EFlags
);
547 ret
= DOSVM_EmulateInterruptRM( context
, arg
);
548 if (TRACE_ON(relay
)) {
549 DPRINTF("Ret DOS int 0x%02x ret=%04lx:%04lx\n",
550 arg
, context
->SegCs
, context
->Eip
);
551 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
552 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
,
553 context
->Esi
, context
->Edi
);
554 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
555 context
->Ebp
, context
->Esp
, context
->SegDs
, context
->SegEs
,
556 context
->SegFs
, context
->SegGs
, context
->EFlags
);
558 return ret
? EXCEPTION_CONTINUE_EXECUTION
: EXCEPTION_EXECUTE_HANDLER
;
560 case EXCEPTION_VM86_STI
:
561 /* case EXCEPTION_VM86_PICRETURN: */
563 ERR( "Protected mode STI caught by real mode handler!\n" );
565 context
->EFlags
|= VIF_MASK
;
566 context
->EFlags
&= ~VIP_MASK
;
567 DOSVM_SendQueuedEvents(context
);
568 return EXCEPTION_CONTINUE_EXECUTION
;
570 return EXCEPTION_CONTINUE_SEARCH
;
573 int WINAPI
DOSVM_Enter( CONTEXT86
*context
)
575 /* Some callers forget to turn V86_FLAG on. */
576 context
->EFlags
|= V86_FLAG
;
580 __wine_enter_vm86( context
);
581 TRACE_(module
)( "vm86 returned: %s\n", strerror(errno
) );
583 __EXCEPT(exception_handler
)
585 TRACE_(module
)( "leaving vm86 mode\n" );
592 /***********************************************************************
595 void WINAPI
DOSVM_PIC_ioport_out( WORD port
, BYTE val
)
599 if ((port
==0x20) && (val
==0x20)) {
600 EnterCriticalSection(&qcrit
);
602 /* EOI (End Of Interrupt) */
603 TRACE("received EOI for current IRQ, clearing\n");
604 event
= current_event
;
605 current_event
= event
->next
;
607 (*event
->relay
)(NULL
,event
->data
);
610 if (DOSVM_HasPendingEvents()) {
611 /* another event is pending, which we should probably
612 * be able to process now */
613 TRACE("another event pending, setting flag\n");
614 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
617 WARN("EOI without active IRQ\n");
619 LeaveCriticalSection(&qcrit
);
621 FIXME("unrecognized PIC command %02x\n",val
);
625 #else /* !MZ_SUPPORTED */
627 /***********************************************************************
630 INT WINAPI
DOSVM_Enter( CONTEXT86
*context
)
632 ERR_(module
)("DOS realmode not supported on this architecture!\n");
636 /***********************************************************************
639 void WINAPI
DOSVM_Wait( CONTEXT86
*waitctx
) { }
641 /***********************************************************************
644 void WINAPI
DOSVM_PIC_ioport_out( WORD port
, BYTE val
) {}
646 /***********************************************************************
647 * QueueEvent (WINEDOS.@)
649 void WINAPI
DOSVM_QueueEvent( INT irq
, INT priority
, DOSRELAY relay
, LPVOID data
)
652 /* callback event, perform it with dummy context */
654 memset(&context
,0,sizeof(context
));
655 (*relay
)(&context
,data
);
657 ERR("IRQ without DOS task: should not happen\n");
661 #endif /* MZ_SUPPORTED */
664 /**********************************************************************
665 * DOSVM_AcknowledgeIRQ
667 * This routine should be called by all internal IRQ handlers.
669 void WINAPI
DOSVM_AcknowledgeIRQ( CONTEXT86
*context
)
674 DOSVM_PIC_ioport_out( 0x20, 0x20 );
677 * Protected mode IRQ handlers are supposed
678 * to turn VIF flag on before they return.
681 NtCurrentTeb()->dpmi_vif
= 1;
685 /**********************************************************************
686 * DllMain (DOSVM.Init)
688 BOOL WINAPI
DllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
690 TRACE_(module
)("(%p,%ld,%p)\n", hinstDLL
, fdwReason
, lpvReserved
);
692 if (fdwReason
== DLL_PROCESS_ATTACH
)
694 DisableThreadLibraryCalls(hinstDLL
);
695 DOSVM_InitSegments();
697 event_notifier
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
699 ERR("Failed to create event object!\n");