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"
39 static void DOSVM_Dump( LPDOSTASK lpDosTask, int fn, int sig,
40 struct vm86plus_struct*VM86 )
46 switch (VM86_TYPE(fn)) {
48 printf("Trapped signal %d\n",sig); break;
50 printf("Trapped unhandled GPF\n"); break;
52 printf("Trapped INT %02x\n",VM86_ARG(fn)); break;
54 printf("Trapped STI\n"); break;
56 printf("Trapped due to pending PIC request\n"); break;
58 printf("Trapped debug request\n"); break;
60 printf("Trapped unknown VM86 type %d arg %d\n",VM86_TYPE(fn),VM86_ARG(fn)); break;
62 #define REGS VM86->regs
63 fprintf(stderr,"AX=%04lX CX=%04lX DX=%04lX BX=%04lX\n",REGS.eax,REGS.ecx,REGS.edx,REGS.ebx);
64 fprintf(stderr,"SI=%04lX DI=%04lX SP=%04lX BP=%04lX\n",REGS.esi,REGS.edi,REGS.esp,REGS.ebp);
65 fprintf(stderr,"CS=%04X DS=%04X ES=%04X SS=%04X\n",REGS.cs,REGS.ds,REGS.es,REGS.ss);
66 fprintf(stderr,"IP=%04lX EFLAGS=%08lX\n",REGS.eip,REGS.eflags);
68 iofs=((DWORD)REGS.cs<<4)+REGS.eip;
70 inst=(BYTE*)lpDosTask->img+iofs;
72 for (x=0; x<8; x++) printf(" %02x",inst[x]);
76 static int DOSVM_Int( int vect, PCONTEXT context, LPDOSTASK lpDosTask )
78 extern UINT16 DPMI_wrap_seg;
81 if (CS_reg(context)==DPMI_wrap_seg) {
82 /* exit from real-mode wrapper */
85 /* we could probably move some other dodgy stuff here too from dpmi.c */
87 INT_RealModeInterrupt(vect,context);
91 static void DOSVM_SimulateInt( int vect, PCONTEXT context, LPDOSTASK lpDosTask )
93 FARPROC16 handler=INT_GetRMHandler(vect);
94 WORD*stack=(WORD*)(V86BASE(context)+(((DWORD)SS_reg(context))<<4)+SP_reg(context));
96 *(--stack)=FL_reg(context);
97 *(--stack)=CS_reg(context);
98 *(--stack)=IP_reg(context);
100 CS_reg(context)=SELECTOROF(handler);
101 IP_reg(context)=OFFSETOF(handler);
104 #define CV CP(eax,EAX); CP(ecx,ECX); CP(edx,EDX); CP(ebx,EBX); \
105 CP(esi,ESI); CP(edi,EDI); CP(esp,ESP); CP(ebp,EBP); \
106 CP(cs,CS); CP(ds,DS); CP(es,ES); \
107 CP(ss,SS); CP(fs,FS); CP(gs,GS); \
108 CP(eip,EIP); CP(eflags,EFL)
110 static int DOSVM_Process( LPDOSTASK lpDosTask, int fn, int sig,
111 struct vm86plus_struct*VM86 )
113 SIGCONTEXT sigcontext;
117 if (VM86_TYPE(fn)==VM86_UNKNOWN) {
118 /* INSTR_EmulateInstruction needs a SIGCONTEXT, not a CONTEXT... */
119 #define CP(x,y) y##_sig(&sigcontext) = VM86->regs.x
122 if (fnINSTR_EmulateInstruction) ret=fnINSTR_EmulateInstruction(&sigcontext);
123 #define CP(x,y) VM86->regs.x = y##_sig(&sigcontext)
129 #define CP(x,y) y##_reg(&context) = VM86->regs.x
132 (void*)V86BASE(&context)=lpDosTask->img;
134 switch (VM86_TYPE(fn)) {
136 TRACE(int,"DOS module caught signal %d\n",sig);
138 DOSVM_SimulateInt(8,&context,lpDosTask);
141 if (ctx_debug_call) ctx_debug_call(SIGTRAP,&context);
143 if ((sig==SIGILL)||(sig==SIGSEGV)) {
144 if (ctx_debug_call) ctx_debug_call(SIGILL,&context);
146 DOSVM_Dump(lpDosTask,fn,sig,VM86);
150 case VM86_UNKNOWN: /* unhandled GPF */
151 DOSVM_Dump(lpDosTask,fn,sig,VM86);
152 if (ctx_debug_call) ctx_debug_call(SIGSEGV,&context); else ret=-1;
156 DPRINTF("Call DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn),context.Eax,context.SegCs,context.Eip);
157 ret=DOSVM_Int(VM86_ARG(fn),&context,lpDosTask);
159 DPRINTF("Ret DOS int 0x%02x (EAX=%08lx) ret=%04lx:%04lx\n",VM86_ARG(fn),context.Eax,context.SegCs,context.Eip);
164 printf("Trapped due to pending PIC request\n"); break;
166 if (ctx_debug_call) ctx_debug_call(SIGTRAP,&context);
169 DOSVM_Dump(lpDosTask,fn,sig,VM86);
173 #define CP(x,y) VM86->regs.x = y##_reg(&context)
179 int DOSVM_Enter( PCONTEXT context )
181 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
182 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
184 struct vm86plus_struct VM86;
186 fd_set readfds,exceptfds;
188 GlobalUnlock16( GetCurrentTask() );
190 ERR(module,"No task is currently active!\n");
193 if (!(lpDosTask=pModule->lpDosTask)) {
194 /* MZ_CreateProcess or MZ_AllocDPMITask should have been called first */
195 ERR(module,"dosmod has not been initialized!");
200 #define CP(x,y) VM86.regs.x = y##_reg(context)
205 /* allocate standard DOS handles */
206 FILE_InitProcessDosHandles();
208 memset(&VM86,0,sizeof(VM86));
209 VM86.regs.cs=lpDosTask->init_cs;
210 VM86.regs.eip=lpDosTask->init_ip;
211 VM86.regs.ss=lpDosTask->init_ss;
212 VM86.regs.esp=lpDosTask->init_sp;
213 VM86.regs.ds=lpDosTask->psp_seg;
214 VM86.regs.es=lpDosTask->psp_seg;
215 /* hmm, what else do we need? */
218 /* main exchange loop */
222 /* transmit VM86 structure to dosmod task */
223 if (write(lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
224 ERR(module,"dosmod sync lost, errno=%d\n",errno);
227 if (write(lpDosTask->write_pipe,&VM86,sizeof(VM86))!=sizeof(VM86)) {
228 ERR(module,"dosmod sync lost, errno=%d\n",errno);
231 /* wait for response, with async events enabled */
234 SIGNAL_MaskAsyncEvents(FALSE);
236 FD_SET(lpDosTask->read_pipe,&readfds);
237 FD_SET(lpDosTask->read_pipe,&exceptfds);
238 select(lpDosTask->read_pipe+1,&readfds,NULL,&exceptfds,NULL);
239 } while (!(FD_ISSET(lpDosTask->read_pipe,&readfds)||
240 FD_ISSET(lpDosTask->read_pipe,&exceptfds)));
241 SIGNAL_MaskAsyncEvents(TRUE);
242 /* read response (with async events disabled to avoid some strange problems) */
244 if ((len=read(lpDosTask->read_pipe,&stat,sizeof(stat)))!=sizeof(stat)) {
245 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
246 WARN(module,"rereading dosmod return code due to errno=%d, result=%d\n",errno,len);
249 ERR(module,"dosmod sync lost reading return code, errno=%d, result=%d\n",errno,len);
253 TRACE(module,"dosmod return code=%d\n",stat);
255 if ((len=read(lpDosTask->read_pipe,&VM86,sizeof(VM86)))!=sizeof(VM86)) {
256 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
257 WARN(module,"rereading dosmod VM86 structure due to errno=%d, result=%d\n",errno,len);
260 ERR(module,"dosmod sync lost reading VM86 structure, errno=%d, result=%d\n",errno,len);
264 if ((stat&0xff)==DOSMOD_SIGNAL) {
266 if ((len=read(lpDosTask->read_pipe,&sig,sizeof(sig)))!=sizeof(sig)) {
267 if (((errno==EINTR)||(errno==EAGAIN))&&(len<=0)) {
268 WARN(module,"rereading dosmod signal due to errno=%d, result=%d\n",errno,len);
271 ERR(module,"dosmod sync lost reading signal, errno=%d, result=%d\n",errno,len);
277 } while (DOSVM_Process(lpDosTask,stat,sig,&VM86)>=0);
280 #define CP(x,y) y##_reg(context) = VM86.regs.x
287 void DOSVM_SetTimer( unsigned ticks )
289 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
290 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
291 int stat=DOSMOD_SET_TIMER;
294 GlobalUnlock16( GetCurrentTask() );
295 if (pModule&&pModule->lpDosTask) {
296 /* the PC clocks ticks at 1193180 Hz */
298 tim.tv_usec=((unsigned long long)ticks*1000000)/1193180;
300 if (!tim.tv_usec) tim.tv_usec=1;
302 if (write(pModule->lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
303 ERR(module,"dosmod sync lost, errno=%d\n",errno);
306 if (write(pModule->lpDosTask->write_pipe,&tim,sizeof(tim))!=sizeof(tim)) {
307 ERR(module,"dosmod sync lost, errno=%d\n",errno);
310 /* there's no return */
314 unsigned DOSVM_GetTimer( void )
316 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
317 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
318 int stat=DOSMOD_GET_TIMER;
321 GlobalUnlock16( GetCurrentTask() );
322 if (pModule&&pModule->lpDosTask) {
323 if (write(pModule->lpDosTask->write_pipe,&stat,sizeof(stat))!=sizeof(stat)) {
324 ERR(module,"dosmod sync lost, errno=%d\n",errno);
328 if (read(pModule->lpDosTask->read_pipe,&tim,sizeof(tim))!=sizeof(tim)) {
329 ERR(module,"dosmod sync lost, errno=%d\n",errno);
332 return ((unsigned long long)tim.tv_usec*1193180)/1000000;
337 void MZ_Tick( WORD handle )
339 /* find the DOS task that has the right system_timer handle... */
340 /* should usually be the current, so let's just be lazy... */
341 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
342 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
343 LPDOSTASK lpDosTask = pModule ? pModule->lpDosTask : NULL;
345 GlobalUnlock16( GetCurrentTask() );
346 if (lpDosTask&&(lpDosTask->system_timer==handle)) {
347 /* BIOS timer tick */
348 (*((DWORD*)(((BYTE*)(lpDosTask->img))+0x46c)))++;
352 #else /* !MZ_SUPPORTED */
354 int DOSVM_Enter( PCONTEXT context )
356 ERR(module,"DOS realmode not supported on this architecture!\n");
360 void MZ_Tick( WORD handle ) {}
361 void DOSVM_SetTimer( unsigned ticks ) {}
362 unsigned DOSVM_GetTimer( void ) { return 0; }