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"
30 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(module);
40 #ifdef HAVE_SYS_MMAN_H
41 # include <sys/mman.h>
44 /* define this to try mapping through /proc/pid/mem instead of a temp file,
45 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
48 #define BIOS_DATA_SEGMENT 0x40
51 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
52 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
54 /* structures for EXEC */
58 DWORD cmdline WINE_PACKED;
59 DWORD fcb1 WINE_PACKED;
60 DWORD fcb2 WINE_PACKED;
72 /* global variables */
76 static WORD init_cs,init_ip,init_ss,init_sp;
77 static HANDLE dosvm_thread, loop_thread;
78 static DWORD dosvm_tid, loop_tid;
80 static void MZ_Launch(void);
81 static BOOL MZ_InitTask(void);
82 static void MZ_KillTask(void);
84 static void MZ_CreatePSP( LPVOID lpPSP, WORD env, WORD par )
88 psp->int20=0x20CD; /* int 20 */
89 /* some programs use this to calculate how much memory they need */
90 psp->nextParagraph=0x9FFF; /* FIXME: use a real value */
91 /* FIXME: dispatcher */
92 psp->savedint22 = DOSVM_GetRMHandler(0x22);
93 psp->savedint23 = DOSVM_GetRMHandler(0x23);
94 psp->savedint24 = DOSVM_GetRMHandler(0x24);
97 /* FIXME: more PSP stuff */
100 static void MZ_FillPSP( LPVOID lpPSP, LPCSTR cmdline )
103 const char*cmd=cmdline?strchr(cmdline,' '):NULL;
105 /* copy parameters */
108 /* command.com doesn't do this */
109 while (*cmd == ' ') cmd++;
111 psp->cmdLine[0]=strlen(cmd);
112 strcpy(psp->cmdLine+1,cmd);
113 psp->cmdLine[psp->cmdLine[0]+1]='\r';
114 } else psp->cmdLine[1]='\r';
115 /* FIXME: more PSP stuff */
118 /* default INT 08 handler: increases timer tick counter but not much more */
119 static char int08[]={
120 0xCD,0x1C, /* int $0x1c */
121 0x50, /* pushw %ax */
122 0x1E, /* pushw %ds */
123 0xB8,0x40,0x00, /* movw $0x40,%ax */
124 0x8E,0xD8, /* movw %ax,%ds */
126 0x83,0x06,0x6C,0x00,0x01, /* addw $1,(0x6c) */
127 0x83,0x16,0x6E,0x00,0x00, /* adcw $0,(0x6e) */
129 0x66,0xFF,0x06,0x6C,0x00, /* incl (0x6c) */
131 0xB0,0x20, /* movb $0x20,%al */
132 0xE6,0x20, /* outb %al,$0x20 */
138 static void MZ_InitHandlers(void)
141 LPBYTE start=DOSMEM_GetBlock(sizeof(int08),&seg);
142 memcpy(start,int08,sizeof(int08));
143 /* INT 08: point it at our tick-incrementing handler */
144 ((SEGPTR*)0)[0x08]=MAKESEGPTR(seg,0);
145 /* INT 1C: just point it to IRET, we don't want to handle it ourselves */
146 ((SEGPTR*)0)[0x1C]=MAKESEGPTR(seg,sizeof(int08)-1);
149 static WORD MZ_InitEnvironment( LPCSTR env, LPCSTR name )
156 /* get size of environment block */
157 while (env[sz++]) sz+=strlen(env+sz)+1;
160 envblk=DOSMEM_GetBlock(sz+sizeof(WORD)+strlen(name)+1,&seg);
163 memcpy(envblk,env,sz);
165 /* DOS 3.x: the block contains 1 additional string */
166 *(WORD*)(envblk+sz)=1;
167 /* being the program name itself */
168 strcpy(envblk+sz+sizeof(WORD),name);
172 static BOOL MZ_InitMemory(void)
174 /* initialize the memory */
175 TRACE("Initializing DOS memory structures\n");
182 static BOOL MZ_DoLoadImage( HANDLE hFile, LPCSTR filename, OverlayBlock *oblk )
184 IMAGE_DOS_HEADER mz_header;
185 DWORD image_start,image_size,min_size,max_size,avail;
186 BYTE*psp_start,*load_start,*oldenv;
187 int x, old_com=0, alloc;
189 WORD env_seg, load_seg, rel_seg, oldpsp_seg;
193 /* DOS process already running, inherit from it */
194 PDB16* par_psp = (PDB16*)((DWORD)DOSVM_psp << 4);
196 oldenv = (LPBYTE)((DWORD)par_psp->environment << 4);
197 oldpsp_seg = DOSVM_psp;
199 /* allocate new DOS process, inheriting from Wine environment */
201 oldenv = GetEnvironmentStringsA();
205 SetFilePointer(hFile,0,NULL,FILE_BEGIN);
206 if ( !ReadFile(hFile,&mz_header,sizeof(mz_header),&len,NULL)
207 || len != sizeof(mz_header)
208 || mz_header.e_magic != IMAGE_DOS_SIGNATURE) {
209 char *p = strrchr( filename, '.' );
210 if (!p || strcasecmp( p, ".com" )) /* check for .COM extension */
212 SetLastError(ERROR_BAD_FORMAT);
215 old_com=1; /* assume .COM file */
217 image_size=GetFileSize(hFile,NULL);
218 min_size=0x10000; max_size=0x100000;
220 mz_header.e_ss=0; mz_header.e_sp=0xFFFE;
221 mz_header.e_cs=0; mz_header.e_ip=0x100;
223 /* calculate load size */
224 image_start=mz_header.e_cparhdr<<4;
225 image_size=mz_header.e_cp<<9; /* pages are 512 bytes */
226 if ((mz_header.e_cblp!=0)&&(mz_header.e_cblp!=4)) image_size-=512-mz_header.e_cblp;
227 image_size-=image_start;
228 min_size=image_size+((DWORD)mz_header.e_minalloc<<4)+(PSP_SIZE<<4);
229 max_size=image_size+((DWORD)mz_header.e_maxalloc<<4)+(PSP_SIZE<<4);
232 if (alloc) MZ_InitMemory();
235 /* load overlay into preallocated memory */
236 load_seg=oblk->load_seg;
237 rel_seg=oblk->rel_seg;
238 load_start=(LPBYTE)((DWORD)load_seg<<4);
240 /* allocate environment block */
241 env_seg=MZ_InitEnvironment(oldenv, filename);
243 /* allocate memory for the executable */
244 TRACE("Allocating DOS memory (min=%ld, max=%ld)\n",min_size,max_size);
245 avail=DOSMEM_Available();
246 if (avail<min_size) {
247 ERR("insufficient DOS memory\n");
248 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
251 if (avail>max_size) avail=max_size;
252 psp_start=DOSMEM_GetBlock(avail,&DOSVM_psp);
254 ERR("error allocating DOS memory\n");
255 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
258 load_seg=DOSVM_psp+(old_com?0:PSP_SIZE);
260 load_start=psp_start+(PSP_SIZE<<4);
261 MZ_CreatePSP(psp_start, env_seg, oldpsp_seg);
264 /* load executable image */
265 TRACE("loading DOS %s image, %08lx bytes\n",old_com?"COM":"EXE",image_size);
266 SetFilePointer(hFile,image_start,NULL,FILE_BEGIN);
267 if (!ReadFile(hFile,load_start,image_size,&len,NULL) || len != image_size) {
268 SetLastError(ERROR_BAD_FORMAT);
272 if (mz_header.e_crlc) {
273 /* load relocation table */
274 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header.e_crlc);
275 /* FIXME: is this too slow without read buffering? */
276 SetFilePointer(hFile,mz_header.e_lfarlc,NULL,FILE_BEGIN);
277 for (x=0; x<mz_header.e_crlc; x++) {
278 if (!ReadFile(hFile,&reloc,sizeof(reloc),&len,NULL) || len != sizeof(reloc)) {
279 SetLastError(ERROR_BAD_FORMAT);
282 *(WORD*)SEGPTR16(load_start,reloc)+=rel_seg;
287 init_cs = load_seg+mz_header.e_cs;
288 init_ip = mz_header.e_ip;
289 init_ss = load_seg+mz_header.e_ss;
290 init_sp = mz_header.e_sp;
292 TRACE("entry point: %04x:%04x\n",init_cs,init_ip);
295 if (alloc && !MZ_InitTask()) {
296 SetLastError(ERROR_GEN_FAILURE);
303 DOSVM_psp = oldpsp_seg;
308 /***********************************************************************
309 * LoadDosExe (WINEDOS.@)
311 void WINAPI MZ_LoadImage( LPCSTR filename, HANDLE hFile )
313 if (MZ_DoLoadImage( hFile, filename, NULL )) MZ_Launch();
316 /***********************************************************************
319 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
321 /* this may only be called from existing DOS processes
322 * (i.e. one DOS app spawning another) */
323 /* FIXME: do we want to check binary type first, to check
324 * whether it's a NE/PE executable? */
325 HFILE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
326 NULL, OPEN_EXISTING, 0, 0);
328 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
330 case 0: /* load and execute */
331 case 1: /* load but don't execute */
333 /* save current process's return SS:SP now */
334 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
335 PDB16 *psp = (PDB16 *)psp_start;
336 psp->saveStack = (DWORD)MAKESEGPTR(context->SegSs, LOWORD(context->Esp));
338 ret = MZ_DoLoadImage( hFile, filename, NULL );
340 /* MZ_LoadImage created a new PSP and loaded new values into it,
341 * let's work on the new values now */
342 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
343 ExecBlock *blk = (ExecBlock *)paramblk;
344 MZ_FillPSP(psp_start, DOSMEM_MapRealToLinear(blk->cmdline));
345 /* the lame MS-DOS engineers decided that the return address should be in int22 */
346 DOSVM_SetRMHandler(0x22, (FARPROC16)MAKESEGPTR(context->SegCs, LOWORD(context->Eip)));
348 /* don't execute, just return startup state */
349 blk->init_cs = init_cs;
350 blk->init_ip = init_ip;
351 blk->init_ss = init_ss;
352 blk->init_sp = init_sp;
354 /* execute by making us return to new process */
355 context->SegCs = init_cs;
356 context->Eip = init_ip;
357 context->SegSs = init_ss;
358 context->Esp = init_sp;
359 context->SegDs = DOSVM_psp;
360 context->SegEs = DOSVM_psp;
365 case 3: /* load overlay */
367 OverlayBlock *blk = (OverlayBlock *)paramblk;
368 ret = MZ_DoLoadImage( hFile, filename, blk );
372 FIXME("EXEC load type %d not implemented\n", func);
373 SetLastError(ERROR_INVALID_FUNCTION);
380 /***********************************************************************
383 void WINAPI MZ_AllocDPMITask( void )
389 /***********************************************************************
392 void WINAPI MZ_RunInThread( PAPCFUNC proc, ULONG_PTR arg )
400 event = CreateEventA(NULL, TRUE, FALSE, NULL);
401 PostThreadMessageA(loop_tid, WM_USER, event, (LPARAM)&spc);
402 WaitForSingleObject(event, INFINITE);
408 static DWORD WINAPI MZ_DOSVM( LPVOID lpExtra )
413 dosvm_pid = getpid();
415 memset( &context, 0, sizeof(context) );
416 context.SegCs = init_cs;
417 context.Eip = init_ip;
418 context.SegSs = init_ss;
419 context.Esp = init_sp;
420 context.SegDs = DOSVM_psp;
421 context.SegEs = DOSVM_psp;
422 context.EFlags = 0x00080000; /* virtual interrupt flag */
423 DOSVM_SetTimer(0x10000);
424 ret = DOSVM_Enter( &context );
430 static BOOL MZ_InitTask(void)
432 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
433 GetCurrentProcess(), &loop_thread,
434 0, FALSE, DUPLICATE_SAME_ACCESS))
436 dosvm_thread = CreateThread(NULL, 0, MZ_DOSVM, NULL, CREATE_SUSPENDED, &dosvm_tid);
438 CloseHandle(loop_thread);
442 loop_tid = GetCurrentThreadId();
446 static void MZ_Launch(void)
448 TDB *pTask = TASK_GetCurrent();
449 BYTE *psp_start = PTR_REAL_TO_LIN( DOSVM_psp, 0 );
451 MZ_FillPSP(psp_start, GetCommandLineA());
452 pTask->flags |= TDBF_WINOLDAP;
456 ResumeThread(dosvm_thread);
461 static void MZ_KillTask(void)
463 TRACE("killing DOS task\n");
465 PostThreadMessageA(loop_tid, WM_QUIT, 0, 0);
466 WaitForSingleObject(loop_thread, INFINITE); /* ? */
467 CloseHandle(dosvm_thread);
468 dosvm_thread = 0; dosvm_tid = 0;
469 CloseHandle(loop_thread);
470 loop_thread = 0; loop_tid = 0;
473 /***********************************************************************
476 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
479 WORD psp_seg = cs_psp ? context->SegCs : DOSVM_psp;
480 LPBYTE psp_start = (LPBYTE)((DWORD)psp_seg << 4);
481 PDB16 *psp = (PDB16 *)psp_start;
482 WORD parpsp = psp->parentPSP; /* check for parent DOS process */
484 /* retrieve parent's return address */
485 FARPROC16 retaddr = DOSVM_GetRMHandler(0x22);
486 /* restore interrupts */
487 DOSVM_SetRMHandler(0x22, psp->savedint22);
488 DOSVM_SetRMHandler(0x23, psp->savedint23);
489 DOSVM_SetRMHandler(0x24, psp->savedint24);
490 /* FIXME: deallocate file handles etc */
491 /* free process's associated memory
492 * FIXME: walk memory and deallocate all blocks owned by process */
493 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(MAKELONG(0,psp->environment)));
494 DOSMEM_FreeBlock(DOSMEM_MapRealToLinear(MAKELONG(0,DOSVM_psp)));
495 /* switch to parent's PSP */
497 psp_start = (LPBYTE)((DWORD)parpsp << 4);
498 psp = (PDB16 *)psp_start;
499 /* now return to parent */
500 DOSVM_retval = retval;
501 context->SegCs = SELECTOROF(retaddr);
502 context->Eip = OFFSETOF(retaddr);
503 context->SegSs = SELECTOROF(psp->saveStack);
504 context->Esp = OFFSETOF(psp->saveStack);
509 ExitThread( retval );
513 /***********************************************************************
516 BOOL WINAPI MZ_Current( void )
518 return (dosvm_pid != 0); /* FIXME: do a better check */
521 #else /* !MZ_SUPPORTED */
523 /***********************************************************************
524 * LoadDosExe (WINEDOS.@)
526 void WINAPI MZ_LoadImage( LPCSTR filename, HANDLE hFile )
528 WARN("DOS executables not supported on this platform\n");
529 SetLastError(ERROR_BAD_FORMAT);
532 /***********************************************************************
535 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
538 SetLastError(ERROR_BAD_FORMAT);
542 /***********************************************************************
545 void WINAPI MZ_AllocDPMITask( void )
547 ERR("Actual real-mode calls not supported on this platform!\n");
550 /***********************************************************************
553 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
555 ExitThread( retval );
558 /***********************************************************************
561 BOOL WINAPI MZ_Current( void )
566 #endif /* !MZ_SUPPORTED */