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 struct _DOSEVENT
*pending_event
, *current_event
;
80 static HANDLE event_notifier
;
82 static CRITICAL_SECTION qcrit
;
83 static CRITICAL_SECTION_DEBUG critsect_debug
=
86 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
87 0, 0, { 0, (DWORD
)(__FILE__
": qcrit") }
89 static CRITICAL_SECTION qcrit
= { &critsect_debug
, -1, 0, 0, 0, 0 };
92 /***********************************************************************
93 * DOSVM_HasPendingEvents
95 * Return true if there are pending events that are not
96 * blocked by currently active event.
98 static BOOL
DOSVM_HasPendingEvents( void )
106 if (pending_event
->priority
< current_event
->priority
)
113 /***********************************************************************
116 * Process single pending event.
118 * This function should be called with queue critical section locked.
119 * The function temporarily releases the critical section if it is
120 * possible that internal interrupt handler or user procedure will
121 * be called. This is because we may otherwise get a deadlock if
122 * another thread is waiting for the same critical section.
124 static void DOSVM_SendOneEvent( CONTEXT86
*context
)
126 LPDOSEVENT event
= pending_event
;
128 /* Remove from pending events list. */
129 pending_event
= event
->next
;
131 /* Process active event. */
134 BYTE intnum
= (event
->irq
< 8) ?
135 (event
->irq
+ 8) : (event
->irq
- 8 + 0x70);
137 /* Event is an IRQ, move it to current events list. */
138 event
->next
= current_event
;
139 current_event
= event
;
141 TRACE( "Dispatching IRQ %d.\n", event
->irq
);
146 * Note that if DOSVM_HardwareInterruptRM calls an internal
147 * interrupt directly, current_event might be cleared
148 * (and event freed) in this call.
150 LeaveCriticalSection(&qcrit
);
151 DOSVM_HardwareInterruptRM( context
, intnum
);
152 EnterCriticalSection(&qcrit
);
157 * This routine only modifies current context so it is
158 * not necessary to release critical section.
160 DOSVM_HardwareInterruptPM( context
, intnum
);
165 /* Callback event. */
166 TRACE( "Dispatching callback event.\n" );
171 * Call relay immediately in real mode.
173 LeaveCriticalSection(&qcrit
);
174 (*event
->relay
)( context
, event
->data
);
175 EnterCriticalSection(&qcrit
);
180 * Force return to relay code. We do not want to
181 * call relay directly because we may be inside a signal handler.
183 DOSVM_BuildCallFrame( context
, event
->relay
, event
->data
);
191 /***********************************************************************
192 * DOSVM_SendQueuedEvents
194 * As long as context instruction pointer stays unmodified,
195 * process all pending events that are not blocked by currently
198 * This routine assumes that caller has already cleared TEB.vm86_pending
199 * and checked that interrupts are enabled.
201 void DOSVM_SendQueuedEvents( CONTEXT86
*context
)
203 DWORD old_cs
= context
->SegCs
;
204 DWORD old_ip
= context
->Eip
;
206 EnterCriticalSection(&qcrit
);
208 TRACE( "Called in %s mode %s events pending (time=%ld)\n",
209 ISV86(context
) ? "real" : "protected",
210 DOSVM_HasPendingEvents() ? "with" : "without",
212 TRACE( "cs:ip=%04lx:%08lx, ss:sp=%04lx:%08lx\n",
213 context
->SegCs
, context
->Eip
, context
->SegSs
, context
->Esp
);
215 while (context
->SegCs
== old_cs
&&
216 context
->Eip
== old_ip
&&
217 DOSVM_HasPendingEvents())
219 DOSVM_SendOneEvent(context
);
222 * Event handling may have turned pending events flag on.
223 * We disable it here because this prevents some
224 * unnecessary calls to this function.
226 NtCurrentTeb()->vm86_pending
= 0;
231 if (!ISV86(context
) && context
->SegCs
== old_cs
&& context
->Eip
== old_ip
)
234 * Routine was called from DPMI but there was nothing to do.
235 * We force a dummy relay call here so that we don't get a race
236 * if signals are unblocked when we return to DPMI application.
238 TRACE( "Called but there was nothing to do, calling NULL relay.\n" );
239 DOSVM_BuildCallFrame( context
, NULL
, NULL
);
242 if (DOSVM_HasPendingEvents())
245 * Interrupts disabled, but there are still
246 * pending events, make sure that pending flag is turned on.
248 TRACE( "Another event is pending, setting VIP flag.\n" );
249 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
254 FIXME("No DOS .exe file support on this platform (yet)\n");
256 #endif /* MZ_SUPPORTED */
258 LeaveCriticalSection(&qcrit
);
263 /***********************************************************************
264 * QueueEvent (WINEDOS.@)
266 void WINAPI
DOSVM_QueueEvent( INT irq
, INT priority
, DOSRELAY relay
, LPVOID data
)
268 LPDOSEVENT event
, cur
, prev
;
272 event
= malloc(sizeof(DOSEVENT
));
274 ERR("out of memory allocating event entry\n");
277 event
->irq
= irq
; event
->priority
= priority
;
278 event
->relay
= relay
; event
->data
= data
;
280 EnterCriticalSection(&qcrit
);
281 old_pending
= DOSVM_HasPendingEvents();
283 /* insert event into linked list, in order *after*
284 * all earlier events of higher or equal priority */
285 cur
= pending_event
; prev
= NULL
;
286 while (cur
&& cur
->priority
<=priority
) {
291 if (prev
) prev
->next
= event
;
292 else pending_event
= event
;
294 if (!old_pending
&& DOSVM_HasPendingEvents()) {
295 TRACE("new event queued, signalling (time=%ld)\n", GetTickCount());
297 /* Alert VM86 thread about the new event. */
298 kill(dosvm_pid
,SIGUSR2
);
300 /* Wake up DOSVM_Wait so that it can serve pending events. */
301 SetEvent(event_notifier
);
303 TRACE("new event queued (time=%ld)\n", GetTickCount());
306 LeaveCriticalSection(&qcrit
);
308 /* DOS subsystem not running */
309 /* (this probably means that we're running a win16 app
310 * which uses DPMI to thunk down to DOS services) */
312 /* callback event, perform it with dummy context */
314 memset(&context
,0,sizeof(context
));
315 (*relay
)(&context
,data
);
317 ERR("IRQ without DOS task: should not happen\n");
322 static void DOSVM_ProcessConsole(void)
328 if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE
),&msg
,1,&res
)) {
329 switch (msg
.EventType
) {
331 scan
= msg
.Event
.KeyEvent
.wVirtualScanCode
;
332 ascii
= msg
.Event
.KeyEvent
.uChar
.AsciiChar
;
333 TRACE("scan %02x, ascii %02x\n", scan
, ascii
);
335 /* set the "break" (release) flag if key released */
336 if (!msg
.Event
.KeyEvent
.bKeyDown
) scan
|= 0x80;
338 /* check whether extended bit is set,
339 * and if so, queue the extension prefix */
340 if (msg
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
) {
341 DOSVM_Int09SendScan(0xE0,0);
343 DOSVM_Int09SendScan(scan
, ascii
);
346 DOSVM_Int33Console(&msg
.Event
.MouseEvent
);
348 case WINDOW_BUFFER_SIZE_EVENT
:
349 FIXME("unhandled WINDOW_BUFFER_SIZE_EVENT.\n");
352 FIXME("unhandled MENU_EVENT.\n");
355 FIXME("unhandled FOCUS_EVENT.\n");
358 FIXME("unknown console event: %d\n", msg
.EventType
);
363 static void DOSVM_ProcessMessage(MSG
*msg
)
367 TRACE("got message %04x, wparam=%08x, lparam=%08lx\n",msg
->message
,msg
->wParam
,msg
->lParam
);
368 if ((msg
->message
>=WM_MOUSEFIRST
)&&
369 (msg
->message
<=WM_MOUSELAST
)) {
370 DOSVM_Int33Message(msg
->message
,msg
->wParam
,msg
->lParam
);
372 switch (msg
->message
) {
376 scan
|= (msg
->lParam
>> 16) & 0x7f;
378 /* check whether extended bit is set,
379 * and if so, queue the extension prefix */
380 if (msg
->lParam
& 0x1000000) {
381 /* FIXME: some keys (function keys) have
382 * extended bit set even when they shouldn't,
383 * should check for them */
384 DOSVM_Int09SendScan(0xE0,0);
386 DOSVM_Int09SendScan(scan
,0);
393 /***********************************************************************
396 * Wait for asynchronous events. This routine temporarily enables
397 * interrupts and waits until some asynchronous event has been
400 void WINAPI
DOSVM_Wait( CONTEXT86
*waitctx
)
402 if (DOSVM_HasPendingEvents())
404 CONTEXT86 context
= *waitctx
;
407 * If DOSVM_Wait is called from protected mode we emulate
408 * interrupt reflection and convert context into real mode context.
409 * This is actually the correct thing to do as long as DOSVM_Wait
410 * is only called from those interrupt functions that DPMI reflects
413 * FIXME: Need to think about where to place real mode stack.
414 * FIXME: If DOSVM_Wait calls are nested stack gets corrupted.
415 * Can this really happen?
417 if (!ISV86(&context
))
419 context
.EFlags
|= 0x00020000;
420 context
.SegSs
= 0xffff;
424 context
.EFlags
|= VIF_MASK
;
428 DOSVM_SendQueuedEvents(&context
);
430 if(context
.SegCs
|| context
.Eip
)
431 DPMI_CallRMProc( &context
, NULL
, 0, TRUE
);
436 int objc
= DOSVM_IsWin16() ? 2 : 1;
439 objs
[0] = event_notifier
;
440 objs
[1] = GetStdHandle(STD_INPUT_HANDLE
);
442 waitret
= MsgWaitForMultipleObjects( objc
, objs
, FALSE
,
443 INFINITE
, QS_ALLINPUT
);
445 if (waitret
== WAIT_OBJECT_0
)
448 * New pending event has been queued, we ignore it
449 * here because it will be processed on next call to
453 else if (objc
== 2 && waitret
== WAIT_OBJECT_0
+ 1)
455 DOSVM_ProcessConsole();
457 else if (waitret
== WAIT_OBJECT_0
+ objc
)
460 while (PeekMessageA(&msg
,0,0,0,PM_REMOVE
|PM_NOYIELD
))
463 DOSVM_ProcessMessage(&msg
);
464 /* we don't need a TranslateMessage here */
465 DispatchMessageA(&msg
);
470 ERR_(module
)( "dosvm wait error=%ld\n", GetLastError() );
476 DWORD WINAPI
DOSVM_Loop( HANDLE hThread
)
482 objs
[0] = GetStdHandle(STD_INPUT_HANDLE
);
486 TRACE_(int)("waiting for action\n");
487 waitret
= MsgWaitForMultipleObjects(2, objs
, FALSE
, INFINITE
, QS_ALLINPUT
);
488 if (waitret
== WAIT_OBJECT_0
) {
489 DOSVM_ProcessConsole();
491 else if (waitret
== WAIT_OBJECT_0
+ 1) {
493 if(!GetExitCodeThread(hThread
, &rv
)) {
494 ERR("Failed to get thread exit code!\n");
499 else if (waitret
== WAIT_OBJECT_0
+ 2) {
500 while (PeekMessageA(&msg
,0,0,0,PM_REMOVE
)) {
502 /* it's a window message */
503 DOSVM_ProcessMessage(&msg
);
504 DispatchMessageA(&msg
);
506 /* it's a thread message */
507 switch (msg
.message
) {
509 /* stop this madness!! */
512 /* run passed procedure in this thread */
513 /* (sort of like APC, but we signal the completion) */
515 DOS_SPC
*spc
= (DOS_SPC
*)msg
.lParam
;
516 TRACE_(int)("calling %p with arg %08lx\n", spc
->proc
, spc
->arg
);
517 (spc
->proc
)(spc
->arg
);
518 TRACE_(int)("done, signalling event %x\n", msg
.wParam
);
519 SetEvent( (HANDLE
)msg
.wParam
);
523 DispatchMessageA(&msg
);
530 ERR_(int)("MsgWaitForMultipleObjects returned unexpected value.\n");
536 static WINE_EXCEPTION_FILTER(exception_handler
)
538 EXCEPTION_RECORD
*rec
= GetExceptionInformation()->ExceptionRecord
;
539 CONTEXT
*context
= GetExceptionInformation()->ContextRecord
;
540 int arg
= rec
->ExceptionInformation
[0];
543 switch(rec
->ExceptionCode
) {
544 case EXCEPTION_VM86_INTx
:
545 if (TRACE_ON(relay
)) {
546 DPRINTF("Call DOS int 0x%02x ret=%04lx:%04lx\n",
547 arg
, context
->SegCs
, context
->Eip
);
548 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
549 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
,
550 context
->Esi
, context
->Edi
);
551 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
552 context
->Ebp
, context
->Esp
, context
->SegDs
, context
->SegEs
,
553 context
->SegFs
, context
->SegGs
, context
->EFlags
);
555 ret
= DOSVM_EmulateInterruptRM( context
, arg
);
556 if (TRACE_ON(relay
)) {
557 DPRINTF("Ret DOS int 0x%02x ret=%04lx:%04lx\n",
558 arg
, context
->SegCs
, context
->Eip
);
559 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
560 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
,
561 context
->Esi
, context
->Edi
);
562 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
563 context
->Ebp
, context
->Esp
, context
->SegDs
, context
->SegEs
,
564 context
->SegFs
, context
->SegGs
, context
->EFlags
);
566 return ret
? EXCEPTION_CONTINUE_EXECUTION
: EXCEPTION_EXECUTE_HANDLER
;
568 case EXCEPTION_VM86_STI
:
569 /* case EXCEPTION_VM86_PICRETURN: */
571 ERR( "Protected mode STI caught by real mode handler!\n" );
573 context
->EFlags
|= VIF_MASK
;
574 context
->EFlags
&= ~VIP_MASK
;
575 DOSVM_SendQueuedEvents(context
);
576 return EXCEPTION_CONTINUE_EXECUTION
;
578 return EXCEPTION_CONTINUE_SEARCH
;
581 int WINAPI
DOSVM_Enter( CONTEXT86
*context
)
583 /* Some callers forget to turn V86_FLAG on. */
584 context
->EFlags
|= V86_FLAG
;
588 __wine_enter_vm86( context
);
589 TRACE_(module
)( "vm86 returned: %s\n", strerror(errno
) );
591 __EXCEPT(exception_handler
)
593 TRACE_(module
)( "leaving vm86 mode\n" );
600 /***********************************************************************
603 void WINAPI
DOSVM_PIC_ioport_out( WORD port
, BYTE val
)
607 if ((port
==0x20) && (val
==0x20)) {
608 EnterCriticalSection(&qcrit
);
610 /* EOI (End Of Interrupt) */
611 TRACE("received EOI for current IRQ, clearing\n");
612 event
= current_event
;
613 current_event
= event
->next
;
615 (*event
->relay
)(NULL
,event
->data
);
618 if (DOSVM_HasPendingEvents()) {
619 /* another event is pending, which we should probably
620 * be able to process now */
621 TRACE("another event pending, setting flag\n");
622 NtCurrentTeb()->vm86_pending
|= VIP_MASK
;
625 WARN("EOI without active IRQ\n");
627 LeaveCriticalSection(&qcrit
);
629 FIXME("unrecognized PIC command %02x\n",val
);
633 #else /* !MZ_SUPPORTED */
635 /***********************************************************************
638 INT WINAPI
DOSVM_Enter( CONTEXT86
*context
)
640 ERR_(module
)("DOS realmode not supported on this architecture!\n");
644 /***********************************************************************
647 void WINAPI
DOSVM_Wait( CONTEXT86
*waitctx
) { }
649 /***********************************************************************
652 void WINAPI
DOSVM_PIC_ioport_out( WORD port
, BYTE val
) {}
654 /***********************************************************************
655 * QueueEvent (WINEDOS.@)
657 void WINAPI
DOSVM_QueueEvent( INT irq
, INT priority
, DOSRELAY relay
, LPVOID data
)
660 /* callback event, perform it with dummy context */
662 memset(&context
,0,sizeof(context
));
663 (*relay
)(&context
,data
);
665 ERR("IRQ without DOS task: should not happen\n");
669 #endif /* MZ_SUPPORTED */
672 /**********************************************************************
673 * DOSVM_AcknowledgeIRQ
675 * This routine should be called by all internal IRQ handlers.
677 void WINAPI
DOSVM_AcknowledgeIRQ( CONTEXT86
*context
)
682 DOSVM_PIC_ioport_out( 0x20, 0x20 );
685 * Protected mode IRQ handlers are supposed
686 * to turn VIF flag on before they return.
689 NtCurrentTeb()->dpmi_vif
= 1;
693 /**********************************************************************
694 * DllMain (DOSVM.Init)
696 BOOL WINAPI
DllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
698 TRACE_(module
)("(%p,%ld,%p)\n", hinstDLL
, fdwReason
, lpvReserved
);
700 if (fdwReason
== DLL_PROCESS_ATTACH
)
702 DisableThreadLibraryCalls(hinstDLL
);
703 DOSVM_InitSegments();
705 event_notifier
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
707 ERR("Failed to create event object!\n");