4 * Copyright 1998 Ove Kåven
6 * This code hasn't been completely cleaned up yet.
17 #include <sys/types.h>
20 #include "wine/winbase16.h"
23 #include "sig_context.h"
33 #include "debugtools.h"
35 DECLARE_DEBUG_CHANNEL(int)
36 DECLARE_DEBUG_CHANNEL(module)
37 DECLARE_DEBUG_CHANNEL(relay)
44 #define IF_CLR(ctx) EFL_reg(ctx) &= ~VIF_MASK
45 #define IF_ENABLED(ctx) (EFL_reg(ctx) & VIF_MASK)
46 #define SET_PEND(ctx) EFL_reg(ctx) |= VIP_MASK
47 #define CLR_PEND(ctx) EFL_reg(ctx) &= ~VIP_MASK
48 #define IS_PEND(ctx) (EFL_reg(ctx) & VIP_MASK)
52 static void DOSVM_Dump( LPDOSTASK lpDosTask, int fn, int sig,
53 struct vm86plus_struct*VM86 )
59 switch (VM86_TYPE(fn)) {
61 printf("Trapped signal %d\n",sig); break;
63 printf("Trapped unhandled GPF\n"); break;
65 printf("Trapped INT %02x\n",VM86_ARG(fn)); break;
67 printf("Trapped STI\n"); break;
69 printf("Trapped due to pending PIC request\n"); break;
71 printf("Trapped debug request\n"); break;
73 printf("Trapped unknown VM86 type %d arg %d\n",VM86_TYPE(fn),VM86_ARG(fn)); break;
75 #define REGS VM86->regs
76 fprintf(stderr,"AX=%04lX CX=%04lX DX=%04lX BX=%04lX\n",REGS.eax,REGS.ecx,REGS.edx,REGS.ebx);
77 fprintf(stderr,"SI=%04lX DI=%04lX SP=%04lX BP=%04lX\n",REGS.esi,REGS.edi,REGS.esp,REGS.ebp);
78 fprintf(stderr,"CS=%04X DS=%04X ES=%04X SS=%04X\n",REGS.cs,REGS.ds,REGS.es,REGS.ss);
79 fprintf(stderr,"IP=%04lX EFLAGS=%08lX\n",REGS.eip,REGS.eflags);
81 iofs=((DWORD)REGS.cs<<4)+REGS.eip;
83 inst=(BYTE*)lpDosTask->img+iofs;
85 for (x=0; x<8; x++) printf(" %02x",inst[x]);
89 static int DOSVM_Int( int vect, PCONTEXT context, LPDOSTASK lpDosTask )
91 extern UINT16 DPMI_wrap_seg;
94 if (CS_reg(context)==DPMI_wrap_seg) {
95 /* exit from real-mode wrapper */
98 /* we could probably move some other dodgy stuff here too from dpmi.c */
100 INT_RealModeInterrupt(vect,context);
104 static void DOSVM_SimulateInt( int vect, PCONTEXT context, LPDOSTASK lpDosTask )
106 FARPROC16 handler=INT_GetRMHandler(vect);
108 if (SELECTOROF(handler)==0xf000) {
109 /* if internal interrupt, call it directly */
110 INT_RealModeInterrupt(vect,context);
112 WORD*stack=(WORD*)(V86BASE(context)+(((DWORD)SS_reg(context))<<4)+SP_reg(context));
113 WORD flag=FL_reg(context);
115 if (IF_ENABLED(context)) flag|=IF_MASK;
119 *(--stack)=CS_reg(context);
120 *(--stack)=IP_reg(context);
122 CS_reg(context)=SELECTOROF(handler);
123 IP_reg(context)=OFFSETOF(handler);
128 #define SHOULD_PEND(x) \
129 (x && ((!lpDosTask->current) || (x->priority < lpDosTask->current->priority)))
131 static void DOSVM_SendQueuedEvent(PCONTEXT context, LPDOSTASK lpDosTask)
133 LPDOSEVENT event = lpDosTask->pending;
135 if (SHOULD_PEND(event)) {
136 /* remove from "pending" list */
137 lpDosTask->pending = event->next;
140 /* it's an IRQ, move it to "current" list */
141 event->next = lpDosTask->current;
142 lpDosTask->current = event;
143 TRACE_(int)("dispatching IRQ %d\n",event->irq);
144 /* note that if DOSVM_SimulateInt calls an internal interrupt directly,
145 * lpDosTask->current might be cleared (and event freed) in this very call! */
146 DOSVM_SimulateInt((event->irq<8)?(event->irq+8):(event->irq-8+0x70),context,lpDosTask);
149 TRACE_(int)("dispatching callback event\n");
150 (*event->relay)(lpDosTask,context,event->data);
154 if (!SHOULD_PEND(lpDosTask->pending)) {
155 TRACE_(int)("clearing Pending flag\n");
160 static void DOSVM_SendQueuedEvents(PCONTEXT context, LPDOSTASK lpDosTask)
162 /* we will send all queued events as long as interrupts are enabled,
163 * but IRQ events will disable interrupts again */
164 while (IS_PEND(context) && IF_ENABLED(context))
165 DOSVM_SendQueuedEvent(context,lpDosTask);
168 void DOSVM_QueueEvent( int irq, int priority, void (*relay)(LPDOSTASK,PCONTEXT,void*), void *data)
170 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
171 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
172 LPDOSEVENT event, cur, prev;
174 GlobalUnlock16( GetCurrentTask() );
175 if (pModule && pModule->lpDosTask) {
176 event = malloc(sizeof(DOSEVENT));
178 ERR_(int)("out of memory allocating event entry\n");
181 event->irq = irq; event->priority = priority;
182 event->relay = relay; event->data = data;
184 /* insert event into linked list, in order *after*
185 * all earlier events of higher or equal priority */
186 cur = pModule->lpDosTask->pending; prev = NULL;
187 while (cur && cur->priority<=priority) {
192 if (prev) prev->next = event;
193 else pModule->lpDosTask->pending = event;
195 /* get dosmod's attention to the new event, except for irq==0 where we already have it */
196 if (irq && !pModule->lpDosTask->sig_sent) {
197 TRACE_(int)("new event queued, signalling dosmod\n");
198 kill(pModule->lpDosTask->task,SIGUSR2);
199 pModule->lpDosTask->sig_sent++;
201 TRACE_(int)("new event queued\n");
206 #define CV CP(eax,EAX); CP(ecx,ECX); CP(edx,EDX); CP(ebx,EBX); \
207 CP(esi,ESI); CP(edi,EDI); CP(esp,ESP); CP(ebp,EBP); \
208 CP(cs,CS); CP(ds,DS); CP(es,ES); \
209 CP(ss,SS); CP(fs,FS); CP(gs,GS); \
210 CP(eip,EIP); CP(eflags,EFL)
212 static int DOSVM_Process( LPDOSTASK lpDosTask, int fn, int sig,
213 struct vm86plus_struct*VM86 )
215 SIGCONTEXT sigcontext;
219 if (VM86_TYPE(fn)==VM86_UNKNOWN) {
220 /* INSTR_EmulateInstruction needs a SIGCONTEXT, not a CONTEXT... */
221 #define CP(x,y) y##_sig(&sigcontext) = VM86->regs.x
224 if (fnINSTR_EmulateInstruction) ret=fnINSTR_EmulateInstruction(&sigcontext);
225 #define CP(x,y) VM86->regs.x = y##_sig(&sigcontext)
231 #define CP(x,y) y##_reg(&context) = VM86->regs.x
234 (void*)V86BASE(&context)=lpDosTask->img;
236 if (VM86->vm86plus.force_return_for_pic) {
240 /* linux doesn't preserve pending flag on return */
241 if (SHOULD_PEND(lpDosTask->pending)) {
246 switch (VM86_TYPE(fn)) {
248 TRACE_(int)("DOS module caught signal %d\n",sig);
249 if ((sig==SIGALRM) || (sig==SIGUSR2)) {
251 DOSVM_QueueEvent(0,DOS_PRIORITY_REALTIME,NULL,NULL);
253 if (lpDosTask->pending) {
254 TRACE_(int)("setting Pending flag, interrupts are currently %s\n",
255 IF_ENABLED(&context) ? "enabled" : "disabled");
257 DOSVM_SendQueuedEvents(&context,lpDosTask);
259 TRACE_(int)("no events are pending, clearing Pending flag\n");
262 if (sig==SIGUSR2) lpDosTask->sig_sent--;
265 if (ctx_debug_call) ctx_debug_call(SIGTRAP,&context);
267 if ((sig==SIGILL)||(sig==SIGSEGV)) {
268 if (ctx_debug_call) ctx_debug_call(SIGILL,&context);
270 DOSVM_Dump(lpDosTask,fn,sig,VM86);
274 case VM86_UNKNOWN: /* unhandled GPF */
275 DOSVM_Dump(lpDosTask,fn,sig,VM86);
276 if (ctx_debug_call) ctx_debug_call(SIGSEGV,&context); else ret=-1;
280 DPRINTF("Call DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn),context.Eax,context.SegCs,context.Eip);
281 ret=DOSVM_Int(VM86_ARG(fn),&context,lpDosTask);
283 DPRINTF("Ret DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn),context.Eax,context.SegCs,context.Eip);
287 TRACE_(int)("DOS task enabled interrupts with events pending, sending events\n");
288 DOSVM_SendQueuedEvents(&context,lpDosTask);
291 if (ctx_debug_call) ctx_debug_call(SIGTRAP,&context);
294 DOSVM_Dump(lpDosTask,fn,sig,VM86);
298 #define CP(x,y) VM86->regs.x = y##_reg(&context)
302 VM86->vm86plus.force_return_for_pic = IS_PEND(&context) ? 1 : 0;
308 void DOSVM_ProcessMessage(LPDOSTASK lpDosTask,MSG *msg)
312 fprintf(stderr,"got message %04x, wparam=%08x, lparam=%08lx\n",msg->message,msg->wParam,msg->lParam);
313 if ((msg->message>=WM_MOUSEFIRST)&&
314 (msg->message<=WM_MOUSELAST)) {
315 INT_Int33Message(msg->message,msg->wParam,msg->lParam);
317 switch (msg->message) {
321 scan |= (msg->lParam >> 16) & 0x7f;
323 /* check whether extended bit is set,
324 * and if so, queue the extension prefix */
325 if (msg->lParam & 0x1000000) {
326 /* FIXME: some keys (function keys) have
327 * extended bit set even when they shouldn't,
328 * should check for them */
329 INT_Int09SendScan(0xE0);
331 INT_Int09SendScan(scan);
337 int DOSVM_Enter( PCONTEXT context )
339 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
340 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
342 struct vm86plus_struct VM86;
347 struct timeval timeout={0,0};
349 GlobalUnlock16( GetCurrentTask() );
351 ERR_(module)("No task is currently active!\n");
354 if (!(lpDosTask=pModule->lpDosTask)) {
355 /* MZ_CreateProcess or MZ_AllocDPMITask should have been called first */
356 ERR_(module)("dosmod has not been initialized!");
361 #define CP(x,y) VM86.regs.x = y##_reg(context)
366 /* allocate standard DOS handles */
367 FILE_InitProcessDosHandles();
369 memset(&VM86,0,sizeof(VM86));
370 VM86.regs.cs=lpDosTask->init_cs;
371 VM86.regs.eip=lpDosTask->init_ip;
372 VM86.regs.ss=lpDosTask->init_ss;
373 VM86.regs.esp=lpDosTask->init_sp;
374 VM86.regs.ds=lpDosTask->psp_seg;
375 VM86.regs.es=lpDosTask->psp_seg;
376 VM86.regs.eflags=VIF_MASK;
377 /* hmm, what else do we need? */
380 /* main exchange loop */
384 /* transmit VM86 structure to dosmod task */
385 if (write(lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
386 ERR_(module)("dosmod sync lost, errno=%d, fd=%d, pid=%d\n",errno,lpDosTask->write_pipe,getpid());
389 if (write(lpDosTask->write_pipe,&VM86,sizeof(VM86))!=sizeof(VM86)) {
390 ERR_(module)("dosmod sync lost, errno=%d\n",errno);
394 /* check for messages (waste time before the response check below) */
395 while (PeekMessageA(&msg,0,0,0,PM_REMOVE|PM_NOYIELD)) {
397 DOSVM_ProcessMessage(lpDosTask,&msg);
398 /* we don't need a TranslateMessage here */
399 DispatchMessageA(&msg);
401 /* quick check for response from dosmod
402 * (faster than doing the full blocking wait, if data already available) */
403 FD_ZERO(&readfds); FD_SET(lpDosTask->read_pipe,&readfds);
404 if (select(lpDosTask->read_pipe+1,&readfds,NULL,NULL,&timeout)>0)
406 /* nothing yet, block while waiting for something to do */
407 waitret=MsgWaitForMultipleObjects(1,&(lpDosTask->hReadPipe),FALSE,INFINITE,QS_ALLINPUT);
408 if (waitret==(DWORD)-1) {
409 ERR_(module)("dosvm wait error=%ld\n",GetLastError());
411 } while (waitret!=WAIT_OBJECT_0);
414 if ((len=read(lpDosTask->read_pipe,&stat,sizeof(stat)))==sizeof(stat)) break;
415 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
416 WARN_(module)("rereading dosmod return code due to errno=%d, result=%d\n",errno,len);
419 ERR_(module)("dosmod sync lost reading return code, errno=%d, result=%d\n",errno,len);
422 TRACE_(module)("dosmod return code=%d\n",stat);
424 if ((len=read(lpDosTask->read_pipe,&VM86,sizeof(VM86)))==sizeof(VM86)) break;
425 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
426 WARN_(module)("rereading dosmod VM86 structure due to errno=%d, result=%d\n",errno,len);
429 ERR_(module)("dosmod sync lost reading VM86 structure, errno=%d, result=%d\n",errno,len);
432 if ((stat&0xff)==DOSMOD_SIGNAL) {
434 if ((len=read(lpDosTask->read_pipe,&sig,sizeof(sig)))==sizeof(sig)) break;
435 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
436 WARN_(module)("rereading dosmod signal due to errno=%d, result=%d\n",errno,len);
439 ERR_(module)("dosmod sync lost reading signal, errno=%d, result=%d\n",errno,len);
444 } while (DOSVM_Process(lpDosTask,stat,sig,&VM86)>=0);
447 #define CP(x,y) y##_reg(context) = VM86.regs.x
454 void DOSVM_PIC_ioport_out( WORD port, BYTE val)
456 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
457 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
460 GlobalUnlock16( GetCurrentTask() );
461 if (pModule && pModule->lpDosTask) {
462 if ((port==0x20) && (val==0x20)) {
463 if (pModule->lpDosTask->current) {
464 /* EOI (End Of Interrupt) */
465 TRACE_(int)("received EOI for current IRQ, clearing\n");
466 event = pModule->lpDosTask->current;
467 pModule->lpDosTask->current = event->next;
469 (*event->relay)(pModule->lpDosTask,NULL,event->data);
472 if (pModule->lpDosTask->pending &&
473 !pModule->lpDosTask->sig_sent) {
474 /* another event is pending, which we should probably
475 * be able to process now, so tell dosmod about it */
476 TRACE_(int)("another event pending, signalling dosmod\n");
477 kill(pModule->lpDosTask->task,SIGUSR2);
478 pModule->lpDosTask->sig_sent++;
481 WARN_(int)("EOI without active IRQ\n");
484 FIXME_(int)("unrecognized PIC command %02x\n",val);
489 void DOSVM_SetTimer( unsigned ticks )
491 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
492 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
493 int stat=DOSMOD_SET_TIMER;
496 GlobalUnlock16( GetCurrentTask() );
497 if (pModule&&pModule->lpDosTask) {
498 /* the PC clocks ticks at 1193180 Hz */
500 tim.tv_usec=((unsigned long long)ticks*1000000)/1193180;
502 if (!tim.tv_usec) tim.tv_usec=1;
504 if (write(pModule->lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
505 ERR_(module)("dosmod sync lost, errno=%d\n",errno);
508 if (write(pModule->lpDosTask->write_pipe,&tim,sizeof(tim))!=sizeof(tim)) {
509 ERR_(module)("dosmod sync lost, errno=%d\n",errno);
512 /* there's no return */
516 unsigned DOSVM_GetTimer( void )
518 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
519 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
520 int stat=DOSMOD_GET_TIMER;
523 GlobalUnlock16( GetCurrentTask() );
524 if (pModule&&pModule->lpDosTask) {
525 if (write(pModule->lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
526 ERR_(module)("dosmod sync lost, errno=%d\n",errno);
531 if (read(pModule->lpDosTask->read_pipe,&tim,sizeof(tim))==sizeof(tim)) break;
532 if ((errno==EINTR)||(errno==EAGAIN)) continue;
533 ERR_(module)("dosmod sync lost, errno=%d\n",errno);
536 return ((unsigned long long)tim.tv_usec*1193180)/1000000;
541 void DOSVM_SetSystemData( int id, void *data )
543 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
544 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
545 DOSSYSTEM *sys, *prev;
547 GlobalUnlock16( GetCurrentTask() );
548 if (pModule && pModule->lpDosTask) {
549 sys = pModule->lpDosTask->sys;
551 while (sys && (sys->id != id)) {
559 sys = malloc(sizeof(DOSSYSTEM));
563 if (prev) prev->next = sys;
564 else pModule->lpDosTask->sys = sys;
569 void* DOSVM_GetSystemData( int id )
571 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
572 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
575 GlobalUnlock16( GetCurrentTask() );
576 if (pModule && pModule->lpDosTask) {
577 sys = pModule->lpDosTask->sys;
578 while (sys && (sys->id != id))
586 #else /* !MZ_SUPPORTED */
588 int DOSVM_Enter( PCONTEXT context )
590 ERR_(module)("DOS realmode not supported on this architecture!\n");
594 void DOSVM_PIC_ioport_out( WORD port, BYTE val) {}
595 void DOSVM_SetTimer( unsigned ticks ) {}
596 unsigned DOSVM_GetTimer( void ) { return 0; }
597 void DOSVM_SetSystemData( int id, void *data ) { free(data); }
598 void* DOSVM_GetSystemData( int id ) { return NULL; }
599 void DOSVM_QueueEvent( int irq, int priority, void (*relay)(LPDOSTASK,PCONTEXT,void*), void *data) { /* EMPTY */ }