Added some new dosmod communication possibilities, including
[wine/multimedia.git] / loader / dos / module.c
blobdf644ae4b88fbee9d081332eb7511867d84445a2
1 /*
2 * DOS (MZ) loader
4 * Copyright 1998 Ove Kåven
6 * This code hasn't been completely cleaned up yet.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include "windows.h"
20 #include "winbase.h"
21 #include "module.h"
22 #include "task.h"
23 #include "selectors.h"
24 #include "file.h"
25 #include "ldt.h"
26 #include "process.h"
27 #include "miscemu.h"
28 #include "debug.h"
29 #include "dosexe.h"
30 #include "dosmod.h"
31 #include "options.h"
33 #ifdef MZ_SUPPORTED
35 #include <sys/mman.h>
37 /* define this to try mapping through /proc/pid/mem instead of a temp file,
38 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
39 #undef MZ_MAPSELF
41 #define BIOS_DATA_SEGMENT 0x40
42 #define START_OFFSET 0
43 #define PSP_SIZE 0x10
45 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
46 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
48 extern WORD WINAPI SYSTEM_KillSystemTimer( WORD timer );
50 static void MZ_InitPSP( LPVOID lpPSP, LPCSTR cmdline, WORD env )
52 PDB*psp=lpPSP;
53 const char*cmd=cmdline?strchr(cmdline,' '):NULL;
55 psp->int20=0x20CD; /* int 20 */
56 /* some programs use this to calculate how much memory they need */
57 psp->nextParagraph=0x9FFF;
58 psp->environment=env;
59 /* copy parameters */
60 if (cmd) {
61 #if 0
62 /* command.com doesn't do this */
63 while (*cmd == ' ') cmd++;
64 #endif
65 psp->cmdLine[0]=strlen(cmd);
66 strcpy(psp->cmdLine+1,cmd);
67 psp->cmdLine[psp->cmdLine[0]+1]='\r';
68 } else psp->cmdLine[1]='\r';
69 /* FIXME: integrate the memory stuff from Wine (msdos/dosmem.c) */
70 /* FIXME: integrate the PDB stuff from Wine (loader/task.c) */
73 /* default INT 08 handler: increases timer tick counter but not much more */
74 static char int08[]={
75 0xCD,0x1C, /* int $0x1c */
76 0x50, /* pushw %ax */
77 0x1E, /* pushw %ds */
78 0xB8,0x40,0x00, /* movw $0x40,%ax */
79 0x8E,0xD8, /* movw %ax,%ds */
80 #if 0
81 0x83,0x06,0x6C,0x00,0x01, /* addw $1,(0x6c) */
82 0x83,0x16,0x6E,0x00,0x00, /* adcw $0,(0x6e) */
83 #else
84 0x66,0xFF,0x06,0x6C,0x00, /* incl (0x6c) */
85 #endif
86 0xB0,0x20, /* movb $0x20,%al */
87 0xE6,0x20, /* outb %al,$0x20 */
88 0x1F, /* popw %ax */
89 0x58, /* popw %ax */
90 0xCF /* iret */
93 static void MZ_InitHandlers( LPDOSTASK lpDosTask )
95 WORD seg;
96 LPBYTE start=DOSMEM_GetBlock(lpDosTask->hModule,sizeof(int08),&seg);
97 memcpy(start,int08,sizeof(int08));
98 /* INT 08: point it at our tick-incrementing handler */
99 ((SEGPTR*)(lpDosTask->img))[0x08]=PTR_SEG_OFF_TO_SEGPTR(seg,0);
100 /* INT 1C: just point it to IRET, we don't want to handle it ourselves */
101 ((SEGPTR*)(lpDosTask->img))[0x1C]=PTR_SEG_OFF_TO_SEGPTR(seg,sizeof(int08)-1);
104 static char enter_xms[]={
105 /* XMS hookable entry point */
106 0xEB,0x03, /* jmp entry */
107 0x90,0x90,0x90, /* nop;nop;nop */
108 /* entry: */
109 /* real entry point */
110 /* for simplicity, we'll just use the same hook as DPMI below */
111 0xCD,0x31, /* int $0x31 */
112 0xCB /* lret */
115 static void MZ_InitXMS( LPDOSTASK lpDosTask )
117 LPBYTE start=DOSMEM_GetBlock(lpDosTask->hModule,sizeof(enter_xms),&(lpDosTask->xms_seg));
118 memcpy(start,enter_xms,sizeof(enter_xms));
121 static char enter_pm[]={
122 0x50, /* pushw %ax */
123 0x52, /* pushw %dx */
124 0x55, /* pushw %bp */
125 0x89,0xE5, /* movw %sp,%bp */
126 /* get return CS */
127 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
128 /* just call int 31 here to get into protected mode... */
129 /* it'll check whether it was called from dpmi_seg... */
130 0xCD,0x31, /* int $0x31 */
131 /* we are now in the context of a 16-bit relay call */
132 /* need to fixup our stack;
133 * 16-bit relay return address will be lost, but we won't worry quite yet */
134 0x8E,0xD0, /* movw %ax,%ss */
135 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
136 /* set return CS */
137 0x89,0x56,0x08, /* movw %dx,8(%bp) */
138 0x5D, /* popw %bp */
139 0x5A, /* popw %dx */
140 0x58, /* popw %ax */
141 0xCB /* lret */
144 static void MZ_InitDPMI( LPDOSTASK lpDosTask )
146 unsigned size=sizeof(enter_pm);
147 LPBYTE start=DOSMEM_GetBlock(lpDosTask->hModule,size,&(lpDosTask->dpmi_seg));
149 lpDosTask->dpmi_sel = SELECTOR_AllocBlock( start, size, SEGMENT_CODE, FALSE, FALSE );
151 memcpy(start,enter_pm,sizeof(enter_pm));
154 static WORD MZ_InitEnvironment( LPDOSTASK lpDosTask, LPCSTR env, LPCSTR name )
156 unsigned sz=0;
157 WORD seg;
158 LPSTR envblk;
160 if (env) {
161 /* get size of environment block */
162 while (env[sz++]) sz+=strlen(env+sz)+1;
163 } else sz++;
164 /* allocate it */
165 envblk=DOSMEM_GetBlock(lpDosTask->hModule,sz+sizeof(WORD)+strlen(name)+1,&seg);
166 /* fill it */
167 if (env) {
168 memcpy(envblk,env,sz);
169 } else envblk[0]=0;
170 /* DOS 3.x: the block contains 1 additional string */
171 *(WORD*)(envblk+sz)=1;
172 /* being the program name itself */
173 strcpy(envblk+sz+sizeof(WORD),name);
174 return seg;
177 int MZ_InitMemory( LPDOSTASK lpDosTask, NE_MODULE *pModule )
179 int x;
181 if (lpDosTask->img_ofs) return 32; /* already allocated */
183 /* allocate 1MB+64K shared memory */
184 lpDosTask->img_ofs=START_OFFSET;
185 #ifdef MZ_MAPSELF
186 lpDosTask->img=VirtualAlloc(NULL,0x110000,MEM_COMMIT,PAGE_READWRITE);
187 /* make sure mmap accepts it */
188 ((char*)lpDosTask->img)[0x10FFFF]=0;
189 #else
190 tmpnam(lpDosTask->mm_name);
191 /* strcpy(lpDosTask->mm_name,"/tmp/mydosimage"); */
192 lpDosTask->mm_fd=open(lpDosTask->mm_name,O_RDWR|O_CREAT /* |O_TRUNC */,S_IRUSR|S_IWUSR);
193 if (lpDosTask->mm_fd<0) ERR(module,"file %s could not be opened\n",lpDosTask->mm_name);
194 /* expand file to 1MB+64K */
195 lseek(lpDosTask->mm_fd,0x110000-1,SEEK_SET);
196 x=0; write(lpDosTask->mm_fd,&x,1);
197 /* map it in */
198 lpDosTask->img=mmap(NULL,0x110000-START_OFFSET,PROT_READ|PROT_WRITE,MAP_SHARED,lpDosTask->mm_fd,0);
199 #endif
200 if (lpDosTask->img==(LPVOID)-1) {
201 ERR(module,"could not map shared memory, error=%s\n",strerror(errno));
202 return 0;
204 TRACE(module,"DOS VM86 image mapped at %08lx\n",(DWORD)lpDosTask->img);
205 pModule->dos_image=lpDosTask->img;
207 /* initialize the memory */
208 TRACE(module,"Initializing DOS memory structures\n");
209 DOSMEM_Init(lpDosTask->hModule);
210 MZ_InitHandlers(lpDosTask);
211 MZ_InitXMS(lpDosTask);
212 MZ_InitDPMI(lpDosTask);
213 return lpDosTask->hModule;
216 static int MZ_LoadImage( HFILE16 hFile, LPCSTR name, LPCSTR cmdline,
217 LPCSTR env, LPDOSTASK lpDosTask, NE_MODULE *pModule )
219 IMAGE_DOS_HEADER mz_header;
220 DWORD image_start,image_size,min_size,max_size,avail;
221 BYTE*psp_start,*load_start;
222 int x,old_com=0;
223 SEGPTR reloc;
224 WORD env_seg;
226 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
227 (mz_header.e_magic != IMAGE_DOS_SIGNATURE)) {
228 #if 0
229 return 11; /* invalid exe */
230 #endif
231 old_com=1; /* assume .COM file */
232 image_start=0;
233 image_size=GetFileSize(HFILE16_TO_HFILE32(hFile),NULL);
234 min_size=0x10000; max_size=0x100000;
235 mz_header.e_crlc=0;
236 mz_header.e_ss=0; mz_header.e_sp=0xFFFE;
237 mz_header.e_cs=0; mz_header.e_ip=0x100;
238 } else {
239 /* calculate load size */
240 image_start=mz_header.e_cparhdr<<4;
241 image_size=mz_header.e_cp<<9; /* pages are 512 bytes */
242 if ((mz_header.e_cblp!=0)&&(mz_header.e_cblp!=4)) image_size-=512-mz_header.e_cblp;
243 image_size-=image_start;
244 min_size=image_size+((DWORD)mz_header.e_minalloc<<4)+(PSP_SIZE<<4);
245 max_size=image_size+((DWORD)mz_header.e_maxalloc<<4)+(PSP_SIZE<<4);
248 MZ_InitMemory(lpDosTask,pModule);
250 /* allocate environment block */
251 env_seg=MZ_InitEnvironment(lpDosTask,env,name);
253 /* allocate memory for the executable */
254 TRACE(module,"Allocating DOS memory (min=%ld, max=%ld)\n",min_size,max_size);
255 avail=DOSMEM_Available(lpDosTask->hModule);
256 if (avail<min_size) {
257 ERR(module, "insufficient DOS memory\n");
258 return 0;
260 if (avail>max_size) avail=max_size;
261 psp_start=DOSMEM_GetBlock(lpDosTask->hModule,avail,&lpDosTask->psp_seg);
262 if (!psp_start) {
263 ERR(module, "error allocating DOS memory\n");
264 return 0;
266 lpDosTask->load_seg=lpDosTask->psp_seg+(old_com?0:PSP_SIZE);
267 load_start=psp_start+(PSP_SIZE<<4);
268 MZ_InitPSP(psp_start, cmdline, env_seg);
270 /* load executable image */
271 TRACE(module,"loading DOS %s image, %08lx bytes\n",old_com?"COM":"EXE",image_size);
272 _llseek16(hFile,image_start,FILE_BEGIN);
273 if ((_hread16(hFile,load_start,image_size)) != image_size)
274 return 11; /* invalid exe */
276 if (mz_header.e_crlc) {
277 /* load relocation table */
278 TRACE(module,"loading DOS EXE relocation table, %d entries\n",mz_header.e_crlc);
279 /* FIXME: is this too slow without read buffering? */
280 _llseek16(hFile,mz_header.e_lfarlc,FILE_BEGIN);
281 for (x=0; x<mz_header.e_crlc; x++) {
282 if (_lread16(hFile,&reloc,sizeof(reloc)) != sizeof(reloc))
283 return 11; /* invalid exe */
284 *(WORD*)SEGPTR16(load_start,reloc)+=lpDosTask->load_seg;
288 lpDosTask->init_cs=lpDosTask->load_seg+mz_header.e_cs;
289 lpDosTask->init_ip=mz_header.e_ip;
290 lpDosTask->init_ss=lpDosTask->load_seg+mz_header.e_ss;
291 lpDosTask->init_sp=mz_header.e_sp;
293 TRACE(module,"entry point: %04x:%04x\n",lpDosTask->init_cs,lpDosTask->init_ip);
295 return lpDosTask->hModule;
298 LPDOSTASK MZ_AllocDPMITask( HMODULE16 hModule )
300 LPDOSTASK lpDosTask = calloc(1, sizeof(DOSTASK));
301 NE_MODULE *pModule;
303 if (lpDosTask) {
304 lpDosTask->hModule = hModule;
306 pModule = (NE_MODULE *)GlobalLock16(hModule);
307 pModule->lpDosTask = lpDosTask;
309 lpDosTask->img=NULL; lpDosTask->mm_name[0]=0; lpDosTask->mm_fd=-1;
311 MZ_InitMemory(lpDosTask, pModule);
313 GlobalUnlock16(hModule);
315 return lpDosTask;
318 static void MZ_InitTimer( LPDOSTASK lpDosTask, int ver )
320 if (ver<1) {
321 #if 0
322 /* start simulated system 55Hz timer */
323 lpDosTask->system_timer = CreateSystemTimer( 55, MZ_Tick );
324 TRACE(module,"created 55Hz timer tick, handle=%d\n",lpDosTask->system_timer);
325 #endif
326 } else {
327 int func;
328 struct timeval tim;
330 /* start dosmod timer at 55Hz */
331 func=DOSMOD_SET_TIMER;
332 tim.tv_sec=0; tim.tv_usec=54925;
333 write(lpDosTask->write_pipe,&func,sizeof(func));
334 write(lpDosTask->write_pipe,&tim,sizeof(tim));
338 int MZ_InitTask( LPDOSTASK lpDosTask )
340 int read_fd[2],write_fd[2];
341 pid_t child;
342 char *fname,*farg,arg[16],fproc[64],path[256],*fpath;
344 if (!lpDosTask) return 0;
345 /* create read pipe */
346 if (pipe(read_fd)<0) return 0;
347 if (pipe(write_fd)<0) {
348 close(read_fd[0]); close(read_fd[1]); return 0;
350 lpDosTask->read_pipe=read_fd[0];
351 lpDosTask->write_pipe=write_fd[1];
352 /* if we have a mapping file, use it */
353 fname=lpDosTask->mm_name; farg=NULL;
354 if (!fname[0]) {
355 /* otherwise, map our own memory image */
356 sprintf(fproc,"/proc/%d/mem",getpid());
357 sprintf(arg,"%ld",(unsigned long)lpDosTask->img);
358 fname=fproc; farg=arg;
361 TRACE(module,"Loading DOS VM support module (hmodule=%04x)\n",lpDosTask->hModule);
362 if ((child=fork())<0) {
363 close(write_fd[0]); close(write_fd[1]);
364 close(read_fd[0]); close(read_fd[1]); return 0;
366 if (child!=0) {
367 /* parent process */
368 int ret;
370 close(read_fd[1]); close(write_fd[0]);
371 lpDosTask->task=child;
372 /* wait for child process to signal readiness */
373 do {
374 if (read(lpDosTask->read_pipe,&ret,sizeof(ret))!=sizeof(ret)) {
375 if ((errno==EINTR)||(errno==EAGAIN)) continue;
376 /* failure */
377 ERR(module,"dosmod has failed to initialize\n");
378 if (lpDosTask->mm_name[0]!=0) unlink(lpDosTask->mm_name);
379 return 0;
381 } while (0);
382 /* the child has now mmaped the temp file, it's now safe to unlink.
383 * do it here to avoid leaving a mess in /tmp if/when Wine crashes... */
384 if (lpDosTask->mm_name[0]!=0) unlink(lpDosTask->mm_name);
385 /* start simulated system timer */
386 MZ_InitTimer(lpDosTask,ret);
387 /* all systems are now go */
388 } else {
389 /* child process */
390 close(read_fd[0]); close(write_fd[1]);
391 /* put our pipes somewhere dosmod can find them */
392 dup2(write_fd[0],0); /* stdin */
393 dup2(read_fd[1],1); /* stdout */
394 /* enable signals */
395 SIGNAL_MaskAsyncEvents(FALSE);
396 /* now load dosmod */
397 execlp("dosmod",fname,farg,NULL);
398 execl("dosmod",fname,farg,NULL);
399 /* hmm, they didn't install properly */
400 execl("loader/dos/dosmod",fname,farg,NULL);
401 /* last resort, try to find it through argv[0] */
402 fpath=strrchr(strcpy(path,Options.argv0),'/');
403 if (fpath) {
404 strcpy(fpath,"/loader/dos/dosmod");
405 execl(path,fname,farg,NULL);
407 /* if failure, exit */
408 ERR(module,"Failed to spawn dosmod, error=%s\n",strerror(errno));
409 exit(1);
411 return lpDosTask->hModule;
414 HINSTANCE16 MZ_CreateProcess( LPCSTR name, LPCSTR cmdline, LPCSTR env,
415 LPSTARTUPINFO32A startup, LPPROCESS_INFORMATION info )
417 LPDOSTASK lpDosTask = NULL; /* keep gcc from complaining */
418 HMODULE16 hModule;
419 HINSTANCE16 hInstance;
420 PDB32 *pdb = PROCESS_Current();
421 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
422 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
423 HFILE16 hFile;
424 OFSTRUCT ofs;
425 int err, alloc = !(pModule && pModule->dos_image);
427 GlobalUnlock16( GetCurrentTask() );
429 if (alloc && (lpDosTask = calloc(1, sizeof(DOSTASK))) == NULL)
430 return 0;
432 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
433 return 2; /* File not found */
435 if ((!env)&&pdb) env = pdb->env_db->environ;
436 if (alloc) {
437 if ((hModule = MODULE_CreateDummyModule(&ofs)) < 32)
438 return hModule;
440 lpDosTask->hModule = hModule;
442 pModule = (NE_MODULE *)GlobalLock16(hModule);
443 pModule->lpDosTask = lpDosTask;
445 lpDosTask->img=NULL; lpDosTask->mm_name[0]=0; lpDosTask->mm_fd=-1;
446 } else lpDosTask=pModule->lpDosTask;
447 err = MZ_LoadImage( hFile, name, cmdline, env, lpDosTask, pModule );
448 _lclose16(hFile);
449 if (alloc) {
450 pModule->dos_image = lpDosTask->img;
451 if (err<32) {
452 if (lpDosTask->mm_name[0]!=0) {
453 if (lpDosTask->img!=NULL) munmap(lpDosTask->img,0x110000-START_OFFSET);
454 if (lpDosTask->mm_fd>=0) close(lpDosTask->mm_fd);
455 unlink(lpDosTask->mm_name);
456 } else
457 if (lpDosTask->img!=NULL) VirtualFree(lpDosTask->img,0x110000,MEM_RELEASE);
458 return err;
460 err = MZ_InitTask( lpDosTask );
461 if (err<32) {
462 MZ_KillModule( lpDosTask );
463 /* FIXME: cleanup hModule */
464 return err;
467 hInstance = NE_CreateInstance(pModule, NULL, (cmdline == NULL));
468 PROCESS_Create( pModule, cmdline, env, hInstance, 0, startup, info );
469 return hInstance;
470 } else {
471 return (err<32) ? err : pTask->hInstance;
475 void MZ_KillModule( LPDOSTASK lpDosTask )
477 TRACE(module,"killing DOS task\n");
478 #if 0
479 SYSTEM_KillSystemTimer(lpDosTask->system_timer);
480 #endif
481 if (lpDosTask->mm_name[0]!=0) {
482 munmap(lpDosTask->img,0x110000-START_OFFSET);
483 close(lpDosTask->mm_fd);
484 } else VirtualFree(lpDosTask->img,0x110000,MEM_RELEASE);
485 close(lpDosTask->read_pipe);
486 close(lpDosTask->write_pipe);
487 kill(lpDosTask->task,SIGTERM);
488 #if 0
489 /* FIXME: this seems to crash */
490 if (lpDosTask->dpmi_sel)
491 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(lpDosTask->dpmi_sel,0));
492 #endif
495 #else /* !MZ_SUPPORTED */
497 HINSTANCE16 MZ_CreateProcess( LPCSTR name, LPCSTR cmdline, LPCSTR env,
498 LPSTARTUPINFO32A startup, LPPROCESS_INFORMATION info )
500 WARN(module,"DOS executables not supported on this architecture\n");
501 return (HMODULE16)11; /* invalid exe */
504 #endif