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"
22 #include "sig_context.h"
38 static void DOSVM_Dump( LPDOSTASK lpDosTask, int fn, int sig,
39 struct vm86plus_struct*VM86 )
45 switch (VM86_TYPE(fn)) {
47 printf("Trapped signal %d\n",sig); break;
49 printf("Trapped unhandled GPF\n"); break;
51 printf("Trapped INT %02x\n",VM86_ARG(fn)); break;
53 printf("Trapped STI\n"); break;
55 printf("Trapped due to pending PIC request\n"); break;
57 printf("Trapped debug request\n"); break;
59 printf("Trapped unknown VM86 type %d arg %d\n",VM86_TYPE(fn),VM86_ARG(fn)); break;
61 #define REGS VM86->regs
62 fprintf(stderr,"AX=%04lX CX=%04lX DX=%04lX BX=%04lX\n",REGS.eax,REGS.ecx,REGS.edx,REGS.ebx);
63 fprintf(stderr,"SI=%04lX DI=%04lX SP=%04lX BP=%04lX\n",REGS.esi,REGS.edi,REGS.esp,REGS.ebp);
64 fprintf(stderr,"CS=%04X DS=%04X ES=%04X SS=%04X\n",REGS.cs,REGS.ds,REGS.es,REGS.ss);
65 fprintf(stderr,"IP=%04lX EFLAGS=%08lX\n",REGS.eip,REGS.eflags);
67 iofs=((DWORD)REGS.cs<<4)+REGS.eip;
69 inst=(BYTE*)lpDosTask->img+iofs;
71 for (x=0; x<8; x++) printf(" %02x",inst[x]);
75 static int DOSVM_Int( int vect, PCONTEXT context, LPDOSTASK lpDosTask )
77 extern UINT16 DPMI_wrap_seg;
80 if (CS_reg(context)==DPMI_wrap_seg) {
81 /* exit from real-mode wrapper */
84 /* we could probably move some other dodgy stuff here too from dpmi.c */
86 INT_RealModeInterrupt(vect,context);
90 static void DOSVM_SimulateInt( int vect, PCONTEXT context, LPDOSTASK lpDosTask )
92 FARPROC16 handler=INT_GetRMHandler(vect);
93 WORD*stack=(WORD*)(V86BASE(context)+(((DWORD)SS_reg(context))<<4)+SP_reg(context));
95 *(--stack)=FL_reg(context);
96 *(--stack)=CS_reg(context);
97 *(--stack)=IP_reg(context);
99 CS_reg(context)=SELECTOROF(handler);
100 IP_reg(context)=OFFSETOF(handler);
103 #define CV CP(eax,EAX); CP(ecx,ECX); CP(edx,EDX); CP(ebx,EBX); \
104 CP(esi,ESI); CP(edi,EDI); CP(esp,ESP); CP(ebp,EBP); \
105 CP(cs,CS); CP(ds,DS); CP(es,ES); \
106 CP(ss,SS); CP(fs,FS); CP(gs,GS); \
107 CP(eip,EIP); CP(eflags,EFL)
109 static int DOSVM_Process( LPDOSTASK lpDosTask, int fn, int sig,
110 struct vm86plus_struct*VM86 )
112 SIGCONTEXT sigcontext;
116 if (VM86_TYPE(fn)==VM86_UNKNOWN) {
117 /* INSTR_EmulateInstruction needs a SIGCONTEXT, not a CONTEXT... */
118 #define CP(x,y) y##_sig(&sigcontext) = VM86->regs.x
121 if (fnINSTR_EmulateInstruction) ret=fnINSTR_EmulateInstruction(&sigcontext);
122 #define CP(x,y) VM86->regs.x = y##_sig(&sigcontext)
128 #define CP(x,y) y##_reg(&context) = VM86->regs.x
131 (void*)V86BASE(&context)=lpDosTask->img;
133 switch (VM86_TYPE(fn)) {
135 TRACE(int,"DOS module caught signal %d\n",sig);
137 DOSVM_SimulateInt(8,&context,lpDosTask);
140 if (ctx_debug_call) ctx_debug_call(SIGTRAP,&context);
142 if ((sig==SIGILL)||(sig==SIGSEGV)) {
143 if (ctx_debug_call) ctx_debug_call(SIGILL,&context);
145 DOSVM_Dump(lpDosTask,fn,sig,VM86);
149 case VM86_UNKNOWN: /* unhandled GPF */
150 DOSVM_Dump(lpDosTask,fn,sig,VM86);
151 if (ctx_debug_call) ctx_debug_call(SIGSEGV,&context); else ret=-1;
155 DPRINTF("Call DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn),context.Eax,context.SegCs,context.Eip);
156 ret=DOSVM_Int(VM86_ARG(fn),&context,lpDosTask);
158 DPRINTF("Ret DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn),context.Eax,context.SegCs,context.Eip);
163 printf("Trapped due to pending PIC request\n"); break;
165 if (ctx_debug_call) ctx_debug_call(SIGTRAP,&context);
168 DOSVM_Dump(lpDosTask,fn,sig,VM86);
172 #define CP(x,y) VM86->regs.x = y##_reg(&context)
178 int DOSVM_Enter( PCONTEXT context )
180 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
181 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
183 struct vm86plus_struct VM86;
185 fd_set readfds,exceptfds;
187 GlobalUnlock16( GetCurrentTask() );
189 ERR(module,"No task is currently active!\n");
192 if (!(lpDosTask=pModule->lpDosTask)) {
193 /* MZ_CreateProcess or MZ_AllocDPMITask should have been called first */
194 ERR(module,"dosmod has not been initialized!");
199 #define CP(x,y) VM86.regs.x = y##_reg(context)
204 /* allocate standard DOS handles */
205 FILE_InitProcessDosHandles();
207 memset(&VM86,0,sizeof(VM86));
208 VM86.regs.cs=lpDosTask->init_cs;
209 VM86.regs.eip=lpDosTask->init_ip;
210 VM86.regs.ss=lpDosTask->init_ss;
211 VM86.regs.esp=lpDosTask->init_sp;
212 VM86.regs.ds=lpDosTask->psp_seg;
213 VM86.regs.es=lpDosTask->psp_seg;
214 /* hmm, what else do we need? */
217 /* main exchange loop */
221 /* transmit VM86 structure to dosmod task */
222 if (write(lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
223 ERR(module,"dosmod sync lost, errno=%d\n",errno);
226 if (write(lpDosTask->write_pipe,&VM86,sizeof(VM86))!=sizeof(VM86)) {
227 ERR(module,"dosmod sync lost, errno=%d\n",errno);
230 /* wait for response, with async events enabled */
233 SIGNAL_MaskAsyncEvents(FALSE);
235 FD_SET(lpDosTask->read_pipe,&readfds);
236 FD_SET(lpDosTask->read_pipe,&exceptfds);
237 select(lpDosTask->read_pipe+1,&readfds,NULL,&exceptfds,NULL);
238 } while (!(FD_ISSET(lpDosTask->read_pipe,&readfds)||
239 FD_ISSET(lpDosTask->read_pipe,&exceptfds)));
240 SIGNAL_MaskAsyncEvents(TRUE);
241 /* read response (with async events disabled to avoid some strange problems) */
243 if ((len=read(lpDosTask->read_pipe,&stat,sizeof(stat)))!=sizeof(stat)) {
244 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
245 WARN(module,"rereading dosmod return code due to errno=%d, result=%d\n",errno,len);
248 ERR(module,"dosmod sync lost reading return code, errno=%d, result=%d\n",errno,len);
252 TRACE(module,"dosmod return code=%d\n",stat);
254 if ((len=read(lpDosTask->read_pipe,&VM86,sizeof(VM86)))!=sizeof(VM86)) {
255 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
256 WARN(module,"rereading dosmod VM86 structure due to errno=%d, result=%d\n",errno,len);
259 ERR(module,"dosmod sync lost reading VM86 structure, errno=%d, result=%d\n",errno,len);
263 if ((stat&0xff)==DOSMOD_SIGNAL) {
265 if ((len=read(lpDosTask->read_pipe,&sig,sizeof(sig)))!=sizeof(sig)) {
266 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
267 WARN(module,"rereading dosmod signal due to errno=%d, result=%d\n",errno,len);
270 ERR(module,"dosmod sync lost reading signal, errno=%d, result=%d\n",errno,len);
276 } while (DOSVM_Process(lpDosTask,stat,sig,&VM86)>=0);
279 #define CP(x,y) y##_reg(context) = VM86.regs.x
286 void DOSVM_SetTimer( unsigned ticks )
288 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
289 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
290 int stat=DOSMOD_SET_TIMER;
293 GlobalUnlock16( GetCurrentTask() );
294 if (pModule&&pModule->lpDosTask) {
295 /* the PC clocks ticks at 1193180 Hz */
297 tim.tv_usec=((unsigned long long)ticks*1000000)/1193180;
299 if (!tim.tv_usec) tim.tv_usec=1;
301 if (write(pModule->lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
302 ERR(module,"dosmod sync lost, errno=%d\n",errno);
305 if (write(pModule->lpDosTask->write_pipe,&tim,sizeof(tim))!=sizeof(tim)) {
306 ERR(module,"dosmod sync lost, errno=%d\n",errno);
309 /* there's no return */
313 unsigned DOSVM_GetTimer( void )
315 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
316 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
317 int stat=DOSMOD_GET_TIMER;
320 GlobalUnlock16( GetCurrentTask() );
321 if (pModule&&pModule->lpDosTask) {
322 if (write(pModule->lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
323 ERR(module,"dosmod sync lost, errno=%d\n",errno);
327 if (read(pModule->lpDosTask->read_pipe,&tim,sizeof(tim))!=sizeof(tim)) {
328 ERR(module,"dosmod sync lost, errno=%d\n",errno);
331 return ((unsigned long long)tim.tv_usec*1193180)/1000000;
336 void MZ_Tick( WORD handle )
338 /* find the DOS task that has the right system_timer handle... */
339 /* should usually be the current, so let's just be lazy... */
340 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
341 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
342 LPDOSTASK lpDosTask = pModule ? pModule->lpDosTask : NULL;
344 GlobalUnlock16( GetCurrentTask() );
345 if (lpDosTask&&(lpDosTask->system_timer==handle)) {
346 /* BIOS timer tick */
347 (*((DWORD*)(((BYTE*)(lpDosTask->img))+0x46c)))++;
351 #else /* !MZ_SUPPORTED */
353 int DOSVM_Enter( PCONTEXT context )
355 ERR(module,"DOS realmode not supported on this architecture!\n");
359 void MZ_Tick( WORD handle ) {}
360 void DOSVM_SetTimer( unsigned ticks ) {}
361 unsigned DOSVM_GetTimer( void ) { return 0; }