4 * Copyright 1998 Ove Kåven
6 * This code hasn't been completely cleaned up yet.
18 #include <sys/types.h>
22 #include "wine/winbase16.h"
28 #include "debugtools.h"
30 #include "../../loader/dos/dosmod.h"
34 DEFAULT_DEBUG_CHANNEL(module);
38 #ifdef HAVE_SYS_MMAN_H
39 # include <sys/mman.h>
42 /* define this to try mapping through /proc/pid/mem instead of a temp file,
43 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
46 #define BIOS_DATA_SEGMENT 0x40
49 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
50 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
52 /* structures for EXEC */
56 DWORD cmdline WINE_PACKED;
57 DWORD fcb1 WINE_PACKED;
58 DWORD fcb2 WINE_PACKED;
70 /* global variables */
72 static WORD init_cs,init_ip,init_ss,init_sp;
73 static char mm_name[128];
75 int read_pipe = -1, write_pipe = -1;
76 HANDLE hReadPipe, hWritePipe;
79 static void MZ_Launch(void);
80 static BOOL MZ_InitTask(void);
81 static void MZ_KillTask(void);
83 static void MZ_CreatePSP( LPVOID lpPSP, WORD env, WORD par )
87 psp->int20=0x20CD; /* int 20 */
88 /* some programs use this to calculate how much memory they need */
89 psp->nextParagraph=0x9FFF; /* FIXME: use a real value */
90 /* FIXME: dispatcher */
91 psp->savedint22 = DOSVM_GetRMHandler(0x22);
92 psp->savedint23 = DOSVM_GetRMHandler(0x23);
93 psp->savedint24 = DOSVM_GetRMHandler(0x24);
96 /* FIXME: more PSP stuff */
99 static void MZ_FillPSP( LPVOID lpPSP, LPCSTR cmdline )
102 const char*cmd=cmdline?strchr(cmdline,' '):NULL;
104 /* copy parameters */
107 /* command.com doesn't do this */
108 while (*cmd == ' ') cmd++;
110 psp->cmdLine[0]=strlen(cmd);
111 strcpy(psp->cmdLine+1,cmd);
112 psp->cmdLine[psp->cmdLine[0]+1]='\r';
113 } else psp->cmdLine[1]='\r';
114 /* FIXME: more PSP stuff */
117 /* default INT 08 handler: increases timer tick counter but not much more */
118 static char int08[]={
119 0xCD,0x1C, /* int $0x1c */
120 0x50, /* pushw %ax */
121 0x1E, /* pushw %ds */
122 0xB8,0x40,0x00, /* movw $0x40,%ax */
123 0x8E,0xD8, /* movw %ax,%ds */
125 0x83,0x06,0x6C,0x00,0x01, /* addw $1,(0x6c) */
126 0x83,0x16,0x6E,0x00,0x00, /* adcw $0,(0x6e) */
128 0x66,0xFF,0x06,0x6C,0x00, /* incl (0x6c) */
130 0xB0,0x20, /* movb $0x20,%al */
131 0xE6,0x20, /* outb %al,$0x20 */
137 static void MZ_InitHandlers(void)
140 LPBYTE start=DOSMEM_GetBlock(sizeof(int08),&seg);
141 memcpy(start,int08,sizeof(int08));
142 /* INT 08: point it at our tick-incrementing handler */
143 ((SEGPTR*)0)[0x08]=MAKESEGPTR(seg,0);
144 /* INT 1C: just point it to IRET, we don't want to handle it ourselves */
145 ((SEGPTR*)0)[0x1C]=MAKESEGPTR(seg,sizeof(int08)-1);
148 static WORD MZ_InitEnvironment( LPCSTR env, LPCSTR name )
155 /* get size of environment block */
156 while (env[sz++]) sz+=strlen(env+sz)+1;
159 envblk=DOSMEM_GetBlock(sz+sizeof(WORD)+strlen(name)+1,&seg);
162 memcpy(envblk,env,sz);
164 /* DOS 3.x: the block contains 1 additional string */
165 *(WORD*)(envblk+sz)=1;
166 /* being the program name itself */
167 strcpy(envblk+sz+sizeof(WORD),name);
171 static BOOL MZ_InitMemory(void)
176 /* allocate 1MB+64K shared memory */
178 /* strcpy(mm_name,"/tmp/mydosimage"); */
179 mm_fd = open(mm_name,O_RDWR|O_CREAT /* |O_TRUNC */,S_IRUSR|S_IWUSR);
180 if (mm_fd < 0) ERR("file %s could not be opened\n",mm_name);
181 /* fill the file with the DOS memory */
182 if (write( mm_fd, NULL, 0x110000 ) != 0x110000) ERR("cannot write DOS mem\n");
184 img_base = mmap(NULL,0x110000,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_SHARED|MAP_FIXED,mm_fd,0);
189 ERR("could not map shared memory, error=%s\n",strerror(errno));
196 static BOOL MZ_DoLoadImage( HANDLE hFile, LPCSTR filename, OverlayBlock *oblk )
198 IMAGE_DOS_HEADER mz_header;
199 DWORD image_start,image_size,min_size,max_size,avail;
200 BYTE*psp_start,*load_start,*oldenv;
201 int x, old_com=0, alloc;
203 WORD env_seg, load_seg, rel_seg, oldpsp_seg;
207 /* DOS process already running, inherit from it */
208 PDB16* par_psp = (PDB16*)((DWORD)DOSVM_psp << 4);
210 oldenv = (LPBYTE)((DWORD)par_psp->environment << 4);
211 oldpsp_seg = DOSVM_psp;
213 /* allocate new DOS process, inheriting from Wine environment */
215 oldenv = GetEnvironmentStringsA();
219 SetFilePointer(hFile,0,NULL,FILE_BEGIN);
220 if ( !ReadFile(hFile,&mz_header,sizeof(mz_header),&len,NULL)
221 || len != sizeof(mz_header)
222 || mz_header.e_magic != IMAGE_DOS_SIGNATURE) {
223 char *p = strrchr( filename, '.' );
224 if (!p || strcasecmp( p, ".com" )) /* check for .COM extension */
226 SetLastError(ERROR_BAD_FORMAT);
229 old_com=1; /* assume .COM file */
231 image_size=GetFileSize(hFile,NULL);
232 min_size=0x10000; max_size=0x100000;
234 mz_header.e_ss=0; mz_header.e_sp=0xFFFE;
235 mz_header.e_cs=0; mz_header.e_ip=0x100;
237 /* calculate load size */
238 image_start=mz_header.e_cparhdr<<4;
239 image_size=mz_header.e_cp<<9; /* pages are 512 bytes */
240 if ((mz_header.e_cblp!=0)&&(mz_header.e_cblp!=4)) image_size-=512-mz_header.e_cblp;
241 image_size-=image_start;
242 min_size=image_size+((DWORD)mz_header.e_minalloc<<4)+(PSP_SIZE<<4);
243 max_size=image_size+((DWORD)mz_header.e_maxalloc<<4)+(PSP_SIZE<<4);
246 if (alloc) MZ_InitMemory();
249 /* load overlay into preallocated memory */
250 load_seg=oblk->load_seg;
251 rel_seg=oblk->rel_seg;
252 load_start=(LPBYTE)((DWORD)load_seg<<4);
254 /* allocate environment block */
255 env_seg=MZ_InitEnvironment(oldenv, filename);
257 /* allocate memory for the executable */
258 TRACE("Allocating DOS memory (min=%ld, max=%ld)\n",min_size,max_size);
259 avail=DOSMEM_Available();
260 if (avail<min_size) {
261 ERR("insufficient DOS memory\n");
262 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
265 if (avail>max_size) avail=max_size;
266 psp_start=DOSMEM_GetBlock(avail,&DOSVM_psp);
268 ERR("error allocating DOS memory\n");
269 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
272 load_seg=DOSVM_psp+(old_com?0:PSP_SIZE);
274 load_start=psp_start+(PSP_SIZE<<4);
275 MZ_CreatePSP(psp_start, env_seg, oldpsp_seg);
278 /* load executable image */
279 TRACE("loading DOS %s image, %08lx bytes\n",old_com?"COM":"EXE",image_size);
280 SetFilePointer(hFile,image_start,NULL,FILE_BEGIN);
281 if (!ReadFile(hFile,load_start,image_size,&len,NULL) || len != image_size) {
282 SetLastError(ERROR_BAD_FORMAT);
286 if (mz_header.e_crlc) {
287 /* load relocation table */
288 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header.e_crlc);
289 /* FIXME: is this too slow without read buffering? */
290 SetFilePointer(hFile,mz_header.e_lfarlc,NULL,FILE_BEGIN);
291 for (x=0; x<mz_header.e_crlc; x++) {
292 if (!ReadFile(hFile,&reloc,sizeof(reloc),&len,NULL) || len != sizeof(reloc)) {
293 SetLastError(ERROR_BAD_FORMAT);
296 *(WORD*)SEGPTR16(load_start,reloc)+=rel_seg;
301 init_cs = load_seg+mz_header.e_cs;
302 init_ip = mz_header.e_ip;
303 init_ss = load_seg+mz_header.e_ss;
304 init_sp = mz_header.e_sp;
306 TRACE("entry point: %04x:%04x\n",init_cs,init_ip);
309 if (alloc && !MZ_InitTask()) {
311 SetLastError(ERROR_GEN_FAILURE);
318 DOSVM_psp = oldpsp_seg;
320 if (mm_name[0]!=0) unlink(mm_name);
326 /***********************************************************************
327 * LoadDosExe (WINEDOS.@)
329 void WINAPI MZ_LoadImage( LPCSTR filename, HANDLE hFile )
331 if (MZ_DoLoadImage( hFile, filename, NULL )) MZ_Launch();
334 /***********************************************************************
337 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
339 /* this may only be called from existing DOS processes
340 * (i.e. one DOS app spawning another) */
341 /* FIXME: do we want to check binary type first, to check
342 * whether it's a NE/PE executable? */
343 HFILE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
344 NULL, OPEN_EXISTING, 0, 0);
346 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
348 case 0: /* load and execute */
349 case 1: /* load but don't execute */
351 /* save current process's return SS:SP now */
352 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
353 PDB16 *psp = (PDB16 *)psp_start;
354 psp->saveStack = (DWORD)MAKESEGPTR(context->SegSs, LOWORD(context->Esp));
356 ret = MZ_DoLoadImage( hFile, filename, NULL );
358 /* MZ_LoadImage created a new PSP and loaded new values into it,
359 * let's work on the new values now */
360 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
361 ExecBlock *blk = (ExecBlock *)paramblk;
362 MZ_FillPSP(psp_start, DOSMEM_MapRealToLinear(blk->cmdline));
363 /* the lame MS-DOS engineers decided that the return address should be in int22 */
364 DOSVM_SetRMHandler(0x22, (FARPROC16)MAKESEGPTR(context->SegCs, LOWORD(context->Eip)));
366 /* don't execute, just return startup state */
367 blk->init_cs = init_cs;
368 blk->init_ip = init_ip;
369 blk->init_ss = init_ss;
370 blk->init_sp = init_sp;
372 /* execute by making us return to new process */
373 context->SegCs = init_cs;
374 context->Eip = init_ip;
375 context->SegSs = init_ss;
376 context->Esp = init_sp;
377 context->SegDs = DOSVM_psp;
378 context->SegEs = DOSVM_psp;
383 case 3: /* load overlay */
385 OverlayBlock *blk = (OverlayBlock *)paramblk;
386 ret = MZ_DoLoadImage( hFile, filename, blk );
390 FIXME("EXEC load type %d not implemented\n", func);
391 SetLastError(ERROR_INVALID_FUNCTION);
398 /***********************************************************************
401 void WINAPI MZ_AllocDPMITask( void )
407 /***********************************************************************
410 void WINAPI MZ_RunInThread( PAPCFUNC proc, ULONG_PTR arg )
415 static void MZ_InitTimer( int ver )
418 /* can't make timer ticks */
423 /* start dosmod timer at 55ms (18.2Hz) */
424 func=DOSMOD_SET_TIMER;
425 tim.tv_sec=0; tim.tv_usec=54925;
426 write(write_pipe,&func,sizeof(func));
427 write(write_pipe,&tim,sizeof(tim));
431 static BOOL MZ_InitTask(void)
433 int write_fd[2],x_fd;
435 char path[256],*fpath;
438 if (!CreatePipe(&hReadPipe,&hWritePipe,NULL,0)) return FALSE;
439 if (pipe(write_fd)<0) {
440 CloseHandle(hReadPipe);
441 CloseHandle(hWritePipe);
445 read_pipe = FILE_GetUnixHandle( hReadPipe, GENERIC_READ );
446 x_fd = FILE_GetUnixHandle( hWritePipe, GENERIC_WRITE );
448 TRACE("win32 pipe: read=%d, write=%d, unix pipe: read=%d, write=%d\n",
449 hReadPipe,hWritePipe,read_pipe,x_fd);
450 TRACE("outbound unix pipe: read=%d, write=%d, pid=%d\n",write_fd[0],write_fd[1],getpid());
452 write_pipe=write_fd[1];
454 TRACE("Loading DOS VM support module\n");
455 if ((child=fork())<0) {
460 CloseHandle(hReadPipe);
461 CloseHandle(hWritePipe);
471 /* wait for child process to signal readiness */
473 if (read(read_pipe,&ret,sizeof(ret))==sizeof(ret)) break;
474 if ((errno==EINTR)||(errno==EAGAIN)) continue;
476 ERR("dosmod has failed to initialize\n");
477 if (mm_name[0]!=0) unlink(mm_name);
480 /* the child has now mmaped the temp file, it's now safe to unlink.
481 * do it here to avoid leaving a mess in /tmp if/when Wine crashes... */
482 if (mm_name[0]!=0) unlink(mm_name);
483 /* start simulated system timer */
486 ERR("dosmod version too old! Please install newer dosmod properly\n");
487 ERR("If you don't, the new dosmod event handling system will not work\n");
489 /* all systems are now go */
494 /* put our pipes somewhere dosmod can find them */
495 dup2(write_fd[0],0); /* stdin */
496 dup2(x_fd,1); /* stdout */
497 /* now load dosmod */
498 /* check argv[0]-derived paths first, since the newest dosmod is most likely there
499 * (at least it was once for Andreas Mohr, so I decided to make it easier for him) */
500 fpath=strrchr(strcpy(path,full_argv0),'/');
502 strcpy(fpath,"/dosmod");
503 execl(path,mm_name,NULL);
504 strcpy(fpath,"/loader/dos/dosmod");
505 execl(path,mm_name,NULL);
507 /* okay, it wasn't there, try in the path */
508 execlp("dosmod",mm_name,NULL);
509 /* last desperate attempts: current directory */
510 execl("dosmod",mm_name,NULL);
511 /* and, just for completeness... */
512 execl("loader/dos/dosmod",mm_name,NULL);
513 /* if failure, exit */
514 ERR("Failed to spawn dosmod, error=%s\n",strerror(errno));
520 static void MZ_Launch(void)
523 TDB *pTask = GlobalLock16( GetCurrentTask() );
524 BYTE *psp_start = PTR_REAL_TO_LIN( DOSVM_psp, 0 );
526 MZ_FillPSP(psp_start, GetCommandLineA());
527 pTask->flags |= TDBF_WINOLDAP;
529 memset( &context, 0, sizeof(context) );
530 context.SegCs = init_cs;
531 context.Eip = init_ip;
532 context.SegSs = init_ss;
533 context.Esp = init_sp;
534 context.SegDs = DOSVM_psp;
535 context.SegEs = DOSVM_psp;
536 context.EFlags = 0x00080000; /* virtual interrupt flag */
538 DOSVM_Enter( &context );
541 static void MZ_KillTask(void)
543 TRACE("killing DOS task\n");
545 kill(dosmod_pid,SIGTERM);
548 /***********************************************************************
551 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
554 WORD psp_seg = cs_psp ? context->SegCs : DOSVM_psp;
555 LPBYTE psp_start = (LPBYTE)((DWORD)psp_seg << 4);
556 PDB16 *psp = (PDB16 *)psp_start;
557 WORD parpsp = psp->parentPSP; /* check for parent DOS process */
559 /* retrieve parent's return address */
560 FARPROC16 retaddr = DOSVM_GetRMHandler(0x22);
561 /* restore interrupts */
562 DOSVM_SetRMHandler(0x22, psp->savedint22);
563 DOSVM_SetRMHandler(0x23, psp->savedint23);
564 DOSVM_SetRMHandler(0x24, psp->savedint24);
565 /* FIXME: deallocate file handles etc */
566 /* free process's associated memory
567 * FIXME: walk memory and deallocate all blocks owned by process */
568 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(MAKELONG(0,psp->environment)));
569 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(MAKELONG(0,DOSVM_psp)));
570 /* switch to parent's PSP */
572 psp_start = (LPBYTE)((DWORD)parpsp << 4);
573 psp = (PDB16 *)psp_start;
574 /* now return to parent */
575 DOSVM_retval = retval;
576 context->SegCs = SELECTOROF(retaddr);
577 context->Eip = OFFSETOF(retaddr);
578 context->SegSs = SELECTOROF(psp->saveStack);
579 context->Esp = OFFSETOF(psp->saveStack);
584 ExitThread( retval );
588 /***********************************************************************
591 BOOL WINAPI MZ_Current( void )
593 return (write_pipe != -1); /* FIXME: do a better check */
596 #else /* !MZ_SUPPORTED */
598 /***********************************************************************
599 * LoadDosExe (WINEDOS.@)
601 void WINAPI MZ_LoadImage( LPCSTR filename, HANDLE hFile )
603 WARN("DOS executables not supported on this platform\n");
604 SetLastError(ERROR_BAD_FORMAT);
607 /***********************************************************************
610 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
613 SetLastError(ERROR_BAD_FORMAT);
617 /***********************************************************************
620 void WINAPI MZ_AllocDPMITask( void )
622 ERR("Actual real-mode calls not supported on this platform!\n");
625 /***********************************************************************
628 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
630 ExitThread( retval );
633 /***********************************************************************
636 BOOL WINAPI MZ_Current( void )
641 #endif /* !MZ_SUPPORTED */