The implementation of mmioWrite is now called instead of the stub.
[wine/multimedia.git] / loader / dos / module.c
blob9573030a65b561503d0b57cb733dbc12143337d2
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 "windows.h"
19 #include "winbase.h"
20 #include "module.h"
21 #include "task.h"
22 #include "selectors.h"
23 #include "file.h"
24 #include "ldt.h"
25 #include "process.h"
26 #include "miscemu.h"
27 #include "debug.h"
28 #include "dosexe.h"
29 #include "options.h"
31 #ifdef MZ_SUPPORTED
33 #include <sys/mman.h>
35 /* define this to try mapping through /proc/pid/mem instead of a temp file,
36 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
37 #undef MZ_MAPSELF
39 #define BIOS_DATA_SEGMENT 0x40
40 #define START_OFFSET 0
41 #define PSP_SIZE 0x10
43 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
44 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
46 static void MZ_InitPSP( LPVOID lpPSP, LPCSTR cmdline, WORD env )
48 PDB*psp=lpPSP;
49 const char*cmd=cmdline?strchr(cmdline,' '):NULL;
51 psp->int20=0x20CD; /* int 20 */
52 /* some programs use this to calculate how much memory they need */
53 psp->nextParagraph=0x9FFF;
54 psp->environment=env;
55 /* copy parameters */
56 if (cmd) {
57 #if 0
58 /* command.com doesn't do this */
59 while (*cmd == ' ') cmd++;
60 #endif
61 psp->cmdLine[0]=strlen(cmd);
62 strcpy(psp->cmdLine+1,cmd);
63 psp->cmdLine[psp->cmdLine[0]+1]='\r';
64 } else psp->cmdLine[1]='\r';
65 /* FIXME: integrate the memory stuff from Wine (msdos/dosmem.c) */
66 /* FIXME: integrate the PDB stuff from Wine (loader/task.c) */
69 static char enter_xms[]={
70 /* XMS hookable entry point */
71 0xEB,0x03, /* jmp entry */
72 0x90,0x90,0x90, /* nop;nop;nop */
73 /* entry: */
74 /* real entry point */
75 /* for simplicity, we'll just use the same hook as DPMI below */
76 0xCD,0x31, /* int 0x31 */
77 0xCB /* retf */
80 static void MZ_InitXMS( LPDOSTASK lpDosTask )
82 LPBYTE start=DOSMEM_GetBlock(lpDosTask->hModule,sizeof(enter_xms),&(lpDosTask->xms_seg));
83 memcpy(start,enter_xms,sizeof(enter_xms));
86 static char enter_pm[]={
87 0x50, /* pushw %ax */
88 0x52, /* pushw %dx */
89 0x55, /* pushw %bp */
90 0x89,0xE5, /* movw %sp,%bp */
91 /* get return CS */
92 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
93 /* just call int 31 here to get into protected mode... */
94 /* it'll check whether it was called from dpmi_seg... */
95 0xCD,0x31, /* int 0x31 */
96 /* we are now in the context of a 16-bit relay call */
97 /* need to fixup our stack;
98 * 16-bit relay return address will be lost, but we won't worry quite yet */
99 0x8E,0xD0, /* movw %ax,%ss */
100 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
101 /* set return CS */
102 0x89,0x56,0x08, /* movw %dx,8(%bp) */
103 0x5D, /* popw %bp */
104 0x5A, /* popw %dx */
105 0x58, /* popw %ax */
106 0xCB /* retf */
109 static void MZ_InitDPMI( LPDOSTASK lpDosTask )
111 LPBYTE start=DOSMEM_GetBlock(lpDosTask->hModule,sizeof(enter_pm),&(lpDosTask->dpmi_seg));
113 lpDosTask->dpmi_sel = SELECTOR_AllocBlock( start, sizeof(enter_pm), SEGMENT_CODE, FALSE, FALSE );
115 memcpy(start,enter_pm,sizeof(enter_pm));
118 static WORD MZ_InitEnvironment( LPDOSTASK lpDosTask, LPCSTR env, LPCSTR name )
120 unsigned sz=0;
121 WORD seg;
122 LPSTR envblk;
124 if (env) {
125 /* get size of environment block */
126 while (env[sz++]) sz+=strlen(env+sz);
127 } else sz++;
128 /* allocate it */
129 envblk=DOSMEM_GetBlock(lpDosTask->hModule,sz+sizeof(WORD)+strlen(name)+1,&seg);
130 /* fill it */
131 if (env) {
132 memcpy(envblk,env,sz);
133 } else envblk[0]=0;
134 /* DOS 3.x: the block contains 1 additional string */
135 *(WORD*)(envblk+sz)=1;
136 /* being the program name itself */
137 strcpy(envblk+sz+sizeof(WORD),name);
138 return seg;
141 int MZ_InitMemory( LPDOSTASK lpDosTask, NE_MODULE *pModule )
143 int x;
145 if (lpDosTask->img_ofs) return 32; /* already allocated */
147 /* allocate 1MB+64K shared memory */
148 lpDosTask->img_ofs=START_OFFSET;
149 #ifdef MZ_MAPSELF
150 lpDosTask->img=VirtualAlloc(NULL,0x110000,MEM_COMMIT,PAGE_READWRITE);
151 /* make sure mmap accepts it */
152 ((char*)lpDosTask->img)[0x10FFFF]=0;
153 #else
154 tmpnam(lpDosTask->mm_name);
155 /* strcpy(lpDosTask->mm_name,"/tmp/mydosimage"); */
156 lpDosTask->mm_fd=open(lpDosTask->mm_name,O_RDWR|O_CREAT /* |O_TRUNC */,S_IRUSR|S_IWUSR);
157 if (lpDosTask->mm_fd<0) ERR(module,"file %s could not be opened\n",lpDosTask->mm_name);
158 /* expand file to 1MB+64K */
159 lseek(lpDosTask->mm_fd,0x110000-1,SEEK_SET);
160 x=0; write(lpDosTask->mm_fd,&x,1);
161 /* map it in */
162 lpDosTask->img=mmap(NULL,0x110000-START_OFFSET,PROT_READ|PROT_WRITE,MAP_SHARED,lpDosTask->mm_fd,0);
163 #endif
164 if (lpDosTask->img==(LPVOID)-1) {
165 ERR(module,"could not map shared memory, error=%s\n",strerror(errno));
166 return 0;
168 TRACE(module,"DOS VM86 image mapped at %08lx\n",(DWORD)lpDosTask->img);
169 pModule->dos_image=lpDosTask->img;
171 /* initialize the memory */
172 TRACE(module,"Initializing DOS memory structures\n");
173 DOSMEM_Init(lpDosTask->hModule);
174 MZ_InitXMS(lpDosTask);
175 MZ_InitDPMI(lpDosTask);
176 return 32;
179 static int MZ_LoadImage( HFILE16 hFile, LPCSTR name, LPCSTR cmdline,
180 LPCSTR env, LPDOSTASK lpDosTask, NE_MODULE *pModule )
182 IMAGE_DOS_HEADER mz_header;
183 DWORD image_start,image_size,min_size,max_size,avail;
184 BYTE*psp_start,*load_start;
185 int x,old_com=0;
186 SEGPTR reloc;
187 WORD env_seg;
189 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
190 (mz_header.e_magic != IMAGE_DOS_SIGNATURE)) {
191 #if 0
192 return 11; /* invalid exe */
193 #endif
194 old_com=1; /* assume .COM file */
195 image_start=0;
196 image_size=GetFileSize(HFILE16_TO_HFILE32(hFile),NULL);
197 min_size=0x10000; max_size=0x100000;
198 mz_header.e_crlc=0;
199 mz_header.e_ss=0; mz_header.e_sp=0xFFFE;
200 mz_header.e_cs=0; mz_header.e_ip=0x100;
201 } else {
202 /* calculate load size */
203 image_start=mz_header.e_cparhdr<<4;
204 image_size=mz_header.e_cp<<9; /* pages are 512 bytes */
205 if ((mz_header.e_cblp!=0)&&(mz_header.e_cblp!=4)) image_size-=512-mz_header.e_cblp;
206 image_size-=image_start;
207 min_size=image_size+((DWORD)mz_header.e_minalloc<<4)+(PSP_SIZE<<4);
208 max_size=image_size+((DWORD)mz_header.e_maxalloc<<4)+(PSP_SIZE<<4);
211 MZ_InitMemory(lpDosTask,pModule);
213 /* allocate environment block */
214 env_seg=MZ_InitEnvironment(lpDosTask,env,name);
216 /* allocate memory for the executable */
217 TRACE(module,"Allocating DOS memory (min=%ld, max=%ld)\n",min_size,max_size);
218 avail=DOSMEM_Available(lpDosTask->hModule);
219 if (avail<min_size) {
220 ERR(module, "insufficient DOS memory\n");
221 return 0;
223 if (avail>max_size) avail=max_size;
224 psp_start=DOSMEM_GetBlock(lpDosTask->hModule,avail,&lpDosTask->psp_seg);
225 if (!psp_start) {
226 ERR(module, "error allocating DOS memory\n");
227 return 0;
229 lpDosTask->load_seg=lpDosTask->psp_seg+(old_com?0:PSP_SIZE);
230 load_start=psp_start+(PSP_SIZE<<4);
231 MZ_InitPSP(psp_start, cmdline, env_seg);
233 /* load executable image */
234 TRACE(module,"loading DOS %s image, %08lx bytes\n",old_com?"COM":"EXE",image_size);
235 _llseek16(hFile,image_start,FILE_BEGIN);
236 if ((_hread16(hFile,load_start,image_size)) != image_size)
237 return 11; /* invalid exe */
239 if (mz_header.e_crlc) {
240 /* load relocation table */
241 TRACE(module,"loading DOS EXE relocation table, %d entries\n",mz_header.e_lfarlc);
242 /* FIXME: is this too slow without read buffering? */
243 _llseek16(hFile,mz_header.e_lfarlc,FILE_BEGIN);
244 for (x=0; x<mz_header.e_crlc; x++) {
245 if (_lread16(hFile,&reloc,sizeof(reloc)) != sizeof(reloc))
246 return 11; /* invalid exe */
247 *(WORD*)SEGPTR16(load_start,reloc)+=lpDosTask->load_seg;
251 lpDosTask->init_cs=lpDosTask->load_seg+mz_header.e_cs;
252 lpDosTask->init_ip=mz_header.e_ip;
253 lpDosTask->init_ss=lpDosTask->load_seg+mz_header.e_ss;
254 lpDosTask->init_sp=mz_header.e_sp;
256 TRACE(module,"entry point: %04x:%04x\n",lpDosTask->init_cs,lpDosTask->init_ip);
258 return 32;
261 int MZ_InitTask( LPDOSTASK lpDosTask )
263 int read_fd[2],write_fd[2];
264 pid_t child;
265 char *fname,*farg,arg[16],fproc[64],path[256],*fpath;
267 /* create read pipe */
268 if (pipe(read_fd)<0) return 0;
269 if (pipe(write_fd)<0) {
270 close(read_fd[0]); close(read_fd[1]); return 0;
272 lpDosTask->read_pipe=read_fd[0];
273 lpDosTask->write_pipe=write_fd[1];
274 /* if we have a mapping file, use it */
275 fname=lpDosTask->mm_name; farg=NULL;
276 if (!fname[0]) {
277 /* otherwise, map our own memory image */
278 sprintf(fproc,"/proc/%d/mem",getpid());
279 sprintf(arg,"%ld",(unsigned long)lpDosTask->img);
280 fname=fproc; farg=arg;
283 TRACE(module,"Loading DOS VM support module (hmodule=%04x)\n",lpDosTask->hModule);
284 if ((child=fork())<0) {
285 close(write_fd[0]); close(write_fd[1]);
286 close(read_fd[0]); close(read_fd[1]); return 0;
288 if (child!=0) {
289 /* parent process */
290 int ret;
292 close(read_fd[1]); close(write_fd[0]);
293 lpDosTask->task=child;
294 /* wait for child process to signal readiness */
295 do {
296 if (read(lpDosTask->read_pipe,&ret,sizeof(ret))!=sizeof(ret)) {
297 if ((errno==EINTR)||(errno==EAGAIN)) continue;
298 /* failure */
299 ERR(module,"dosmod has failed to initialize\n");
300 if (lpDosTask->mm_name[0]!=0) unlink(lpDosTask->mm_name);
301 return 0;
303 } while (0);
304 /* the child has now mmaped the temp file, it's now safe to unlink.
305 * do it here to avoid leaving a mess in /tmp if/when Wine crashes... */
306 if (lpDosTask->mm_name[0]!=0) unlink(lpDosTask->mm_name);
307 /* all systems are now go */
308 } else {
309 /* child process */
310 close(read_fd[0]); close(write_fd[1]);
311 /* put our pipes somewhere dosmod can find them */
312 dup2(write_fd[0],0); /* stdin */
313 dup2(read_fd[1],1); /* stdout */
314 /* now load dosmod */
315 execlp("dosmod",fname,farg,NULL);
316 execl("dosmod",fname,farg,NULL);
317 /* hmm, they didn't install properly */
318 execl("loader/dos/dosmod",fname,farg,NULL);
319 /* last resort, try to find it through argv[0] */
320 fpath=strrchr(strcpy(path,Options.argv0),'/');
321 if (fpath) {
322 strcpy(fpath,"/loader/dos/dosmod");
323 execl(path,fname,farg,NULL);
325 /* if failure, exit */
326 ERR(module,"Failed to spawn dosmod, error=%s\n",strerror(errno));
327 exit(1);
329 return 32;
332 HINSTANCE16 MZ_CreateProcess( LPCSTR name, LPCSTR cmdline, LPCSTR env,
333 LPSTARTUPINFO32A startup, LPPROCESS_INFORMATION info )
335 LPDOSTASK lpDosTask = NULL; /* keep gcc from complaining */
336 HMODULE16 hModule;
337 HINSTANCE16 hInstance;
338 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
339 NE_MODULE *pModule = pTask ? NE_GetPtr( pTask->hModule ) : NULL;
340 HFILE16 hFile;
341 OFSTRUCT ofs;
342 int err, alloc = !(pModule && pModule->dos_image);
344 GlobalUnlock16( GetCurrentTask() );
346 if (alloc && (lpDosTask = calloc(1, sizeof(DOSTASK))) == NULL)
347 return 0;
349 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
350 return 2; /* File not found */
352 if (alloc) {
353 if ((hModule = MODULE_CreateDummyModule(&ofs)) < 32)
354 return hModule;
356 lpDosTask->hModule = hModule;
358 pModule = (NE_MODULE *)GlobalLock16(hModule);
359 pModule->lpDosTask = lpDosTask;
361 lpDosTask->img=NULL; lpDosTask->mm_name[0]=0; lpDosTask->mm_fd=-1;
362 } else lpDosTask=pModule->lpDosTask;
363 err = MZ_LoadImage( hFile, name, cmdline, env, lpDosTask, pModule );
364 _lclose16(hFile);
365 if (alloc) {
366 pModule->dos_image = lpDosTask->img;
367 if (err<32) {
368 if (lpDosTask->mm_name[0]!=0) {
369 if (lpDosTask->img!=NULL) munmap(lpDosTask->img,0x110000-START_OFFSET);
370 if (lpDosTask->mm_fd>=0) close(lpDosTask->mm_fd);
371 unlink(lpDosTask->mm_name);
372 } else
373 if (lpDosTask->img!=NULL) VirtualFree(lpDosTask->img,0x110000,MEM_RELEASE);
374 return err;
376 err = MZ_InitTask( lpDosTask );
377 if (err<32) {
378 MZ_KillModule( lpDosTask );
379 /* FIXME: cleanup hModule */
380 return err;
383 hInstance = NE_CreateInstance(pModule, NULL, (cmdline == NULL));
384 PROCESS_Create( pModule, cmdline, env, hInstance, 0, startup, info );
385 return hInstance;
386 } else {
387 return (err<32) ? err : pTask->hInstance;
391 void MZ_KillModule( LPDOSTASK lpDosTask )
393 if (lpDosTask->mm_name[0]!=0) {
394 munmap(lpDosTask->img,0x110000-START_OFFSET);
395 close(lpDosTask->mm_fd);
396 } else VirtualFree(lpDosTask->img,0x110000,MEM_RELEASE);
397 close(lpDosTask->read_pipe);
398 close(lpDosTask->write_pipe);
399 kill(lpDosTask->task,SIGTERM);
400 #if 0
401 /* FIXME: this seems to crash */
402 if (lpDosTask->dpmi_sel)
403 UnMapLS(PTR_SEG_OFF_TO_SEGPTR(lpDosTask->dpmi_sel,0));
404 #endif
407 #else /* !MZ_SUPPORTED */
409 HINSTANCE16 MZ_CreateProcess( LPCSTR name, LPCSTR cmdline, LPCSTR env,
410 LPSTARTUPINFO32A startup, LPPROCESS_INFORMATION info )
412 WARN(module,"DOS executables not supported on this architecture\n");
413 return (HMODULE16)11; /* invalid exe */
416 #endif