4 * Copyright 1998 Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Note: This code hasn't been completely cleaned up yet.
24 #include "wine/port.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
44 #include "wine/winbase16.h"
48 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(module);
55 static BOOL DOSVM_isdosexe;
57 /**********************************************************************
60 * Return TRUE if we are in Windows process.
62 BOOL DOSVM_IsWin16(void)
64 return DOSVM_isdosexe ? FALSE : TRUE;
67 /**********************************************************************
70 void DOSVM_Exit( WORD retval )
74 ReleaseThunkLock( &count );
81 #ifdef HAVE_SYS_MMAN_H
82 # include <sys/mman.h>
85 /* define this to try mapping through /proc/pid/mem instead of a temp file,
86 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
89 #define BIOS_DATA_SEGMENT 0x40
92 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
93 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
95 /* structures for EXEC */
117 /* global variables */
121 static WORD init_cs,init_ip,init_ss,init_sp;
122 static HANDLE dosvm_thread, loop_thread;
123 static DWORD dosvm_tid, loop_tid;
125 static void MZ_Launch( LPCSTR cmdtail, int length );
126 static BOOL MZ_InitTask(void);
128 static void MZ_CreatePSP( LPVOID lpPSP, WORD env, WORD par )
132 psp->int20=0x20CD; /* int 20 */
133 /* some programs use this to calculate how much memory they need */
134 psp->nextParagraph=0x9FFF; /* FIXME: use a real value */
135 /* FIXME: dispatcher */
136 psp->savedint22 = DOSVM_GetRMHandler(0x22);
137 psp->savedint23 = DOSVM_GetRMHandler(0x23);
138 psp->savedint24 = DOSVM_GetRMHandler(0x24);
140 psp->environment=env;
141 /* FIXME: more PSP stuff */
144 static void MZ_FillPSP( LPVOID lpPSP, LPCSTR cmdtail, int length )
150 WARN( "Command tail truncated! (length %d)\n", length );
154 psp->cmdLine[0] = length;
157 * Length of exactly 127 bytes means that full command line is
158 * stored in environment variable CMDLINE and PSP contains
159 * command tail truncated to 126 bytes.
165 memmove(psp->cmdLine+1, cmdtail, length);
167 psp->cmdLine[length+1] = '\r';
169 /* FIXME: more PSP stuff */
172 static WORD MZ_InitEnvironment( LPCSTR env, LPCSTR name )
180 /* get size of environment block */
181 while (env[sz++]) sz+=strlen(env+sz)+1;
184 envblk=DOSMEM_AllocBlock(sz+sizeof(WORD)+strlen(name)+1,&seg);
187 memcpy(envblk,env,sz);
189 /* DOS environment variables are uppercase */
191 while (envblk[i] != '='){
192 if (envblk[i]>='a' && envblk[i] <= 'z'){
197 i += strlen(envblk+i) + 1;
199 /* DOS 3.x: the block contains 1 additional string */
200 *(WORD*)(envblk+sz)=1;
201 /* being the program name itself */
202 strcpy(envblk+sz+sizeof(WORD),name);
206 static BOOL MZ_InitMemory(void)
208 /* initialize the memory */
209 TRACE("Initializing DOS memory structures\n");
210 DOSMEM_MapDosLayout();
211 DOSDEV_InstallDOSDevices();
212 MSCDEX_InstallCDROM();
217 static BOOL MZ_DoLoadImage( HANDLE hFile, LPCSTR filename, OverlayBlock *oblk, WORD par_env_seg )
219 IMAGE_DOS_HEADER mz_header;
220 DWORD image_start,image_size,min_size,max_size,avail;
221 BYTE*psp_start,*load_start;
223 int x, old_com=0, alloc;
225 WORD env_seg, load_seg, rel_seg, oldpsp_seg;
229 /* DOS process already running, inherit from it */
232 oldpsp_seg = DOSVM_psp;
234 par_psp = (PDB16*)((DWORD)DOSVM_psp << 4);
235 oldenv = (LPSTR)((DWORD)par_psp->environment << 4);
238 /* allocate new DOS process, inheriting from Wine environment */
242 oldenv = GetEnvironmentStringsA();
245 SetFilePointer(hFile,0,NULL,FILE_BEGIN);
246 if ( !ReadFile(hFile,&mz_header,sizeof(mz_header),&len,NULL)
247 || len != sizeof(mz_header)
248 || mz_header.e_magic != IMAGE_DOS_SIGNATURE) {
249 char *p = strrchr( filename, '.' );
250 if (!p || strcasecmp( p, ".com" )) /* check for .COM extension */
252 SetLastError(ERROR_BAD_FORMAT);
255 old_com=1; /* assume .COM file */
257 image_size=GetFileSize(hFile,NULL);
258 min_size=0x10000; max_size=0x100000;
260 mz_header.e_ss=0; mz_header.e_sp=0xFFFE;
261 mz_header.e_cs=0; mz_header.e_ip=0x100;
263 /* calculate load size */
264 image_start=mz_header.e_cparhdr<<4;
265 image_size=mz_header.e_cp<<9; /* pages are 512 bytes */
266 /* From Ralf Brown Interrupt List: If the word at offset 02h is 4, it should
267 * be treated as 00h, since pre-1.10 versions of the MS linker set it that
269 if ((mz_header.e_cblp!=0)&&(mz_header.e_cblp!=4)) image_size-=512-mz_header.e_cblp;
270 image_size-=image_start;
271 min_size=image_size+((DWORD)mz_header.e_minalloc<<4)+(PSP_SIZE<<4);
272 max_size=image_size+((DWORD)mz_header.e_maxalloc<<4)+(PSP_SIZE<<4);
275 if (alloc) MZ_InitMemory();
278 /* load overlay into preallocated memory */
279 load_seg=oblk->load_seg;
280 rel_seg=oblk->rel_seg;
281 load_start=(LPBYTE)((DWORD)load_seg<<4);
283 /* allocate environment block */
285 env_seg = par_env_seg;
287 env_seg=MZ_InitEnvironment(oldenv, filename);
289 FreeEnvironmentStringsA( oldenv);
291 /* allocate memory for the executable */
292 TRACE("Allocating DOS memory (min=%d, max=%d)\n",min_size,max_size);
293 avail=DOSMEM_Available();
294 if (avail<min_size) {
295 ERR("insufficient DOS memory\n");
296 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
299 if (avail>max_size) avail=max_size;
300 psp_start=DOSMEM_AllocBlock(avail,&DOSVM_psp);
302 ERR("error allocating DOS memory\n");
303 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
306 load_seg=DOSVM_psp+(old_com?0:PSP_SIZE);
308 load_start=psp_start+(PSP_SIZE<<4);
309 MZ_CreatePSP(psp_start, env_seg, oldpsp_seg);
312 /* load executable image */
313 TRACE("loading DOS %s image, %08x bytes\n",old_com?"COM":"EXE",image_size);
314 SetFilePointer(hFile,image_start,NULL,FILE_BEGIN);
315 if (!ReadFile(hFile,load_start,image_size,&len,NULL) || len != image_size) {
316 /* check if this is due to the workaround for the pre-1.10 MS linker and we
317 really had only 4 bytes on the last page */
318 if (mz_header.e_cblp != 4 || image_size - len != 512 - 4) {
319 SetLastError(ERROR_BAD_FORMAT);
324 if (mz_header.e_crlc) {
325 /* load relocation table */
326 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header.e_crlc);
327 /* FIXME: is this too slow without read buffering? */
328 SetFilePointer(hFile,mz_header.e_lfarlc,NULL,FILE_BEGIN);
329 for (x=0; x<mz_header.e_crlc; x++) {
330 if (!ReadFile(hFile,&reloc,sizeof(reloc),&len,NULL) || len != sizeof(reloc)) {
331 SetLastError(ERROR_BAD_FORMAT);
334 *(WORD*)SEGPTR16(load_start,reloc)+=rel_seg;
339 init_cs = load_seg+mz_header.e_cs;
340 init_ip = mz_header.e_ip;
341 init_ss = load_seg+mz_header.e_ss;
342 init_sp = mz_header.e_sp;
344 /* .COM files exit with ret. Make sure they jump to psp start (=int 20) */
345 WORD* stack = PTR_REAL_TO_LIN(init_ss, init_sp);
349 TRACE("entry point: %04x:%04x\n",init_cs,init_ip);
352 if (alloc && !MZ_InitTask()) {
353 SetLastError(ERROR_GEN_FAILURE);
360 DOSVM_psp = oldpsp_seg;
365 /***********************************************************************
366 * wine_load_dos_exe (WINEDOS.@)
368 * Called from WineVDM when a new real-mode DOS process is started.
369 * Loads DOS program into memory and executes the program.
371 void WINAPI wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline )
373 char dos_cmdtail[126];
376 HANDLE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
377 NULL, OPEN_EXISTING, 0, 0 );
378 if (hFile == INVALID_HANDLE_VALUE) return;
379 DOSVM_isdosexe = TRUE;
381 if(cmdline && *cmdline)
383 dos_length = strlen(cmdline);
384 memmove( dos_cmdtail + 1, cmdline,
385 (dos_length < 125) ? dos_length : 125 );
387 /* Non-empty command tail always starts with at least one space. */
388 dos_cmdtail[0] = ' ';
392 * If command tail is longer than 126 characters,
393 * set tail length to 127 and fill CMDLINE environment variable
394 * with full command line (this includes filename).
396 if (dos_length > 126)
398 char *cmd = HeapAlloc( GetProcessHeap(), 0,
399 dos_length + strlen(filename) + 4 );
406 * Append filename. If path includes spaces, quote the path.
408 if (strchr(filename, ' '))
411 strcpy( ptr, filename );
412 ptr += strlen(filename);
417 strcpy( ptr, filename );
418 ptr += strlen(filename);
422 * Append command tail.
424 if (cmdline[0] != ' ')
426 strcpy( ptr, cmdline );
429 * Set environment variable. This will be passed to
432 if (!SetEnvironmentVariableA( "CMDLINE", cmd ))
434 HeapFree(GetProcessHeap(), 0, cmd );
438 HeapFree(GetProcessHeap(), 0, cmd );
443 if (MZ_DoLoadImage( hFile, filename, NULL, 0 ))
444 MZ_Launch( dos_cmdtail, dos_length );
447 /***********************************************************************
450 * this may only be called from existing DOS processes
452 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
456 PROCESS_INFORMATION pe;
461 if(!GetBinaryTypeA(filename, &binType)) /* determine what kind of binary this is */
463 return FALSE; /* binary is not an executable */
466 /* handle non-dos executables */
467 if(binType != SCS_DOS_BINARY)
469 if(func == 0) /* load and execute */
473 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
474 PDB16 *psp = (PDB16 *)psp_start;
475 ExecBlock *blk = paramblk;
476 LPBYTE cmdline = PTR_REAL_TO_LIN(SELECTOROF(blk->cmdline),OFFSETOF(blk->cmdline));
477 LPBYTE envblock = PTR_REAL_TO_LIN(psp->environment, 0);
478 int cmdLength = cmdline[0];
481 * If cmdLength is 127, command tail is truncated and environment
482 * variable CMDLINE should contain full command line
483 * (this includes filename).
485 if (cmdLength == 127)
487 FIXME( "CMDLINE argument passing is unimplemented.\n" );
488 cmdLength = 126; /* FIXME */
491 fullCmdLength = (strlen(filename) + 1) + cmdLength + 1; /* filename + space + cmdline + terminating null character */
493 fullCmdLine = HeapAlloc(GetProcessHeap(), 0, fullCmdLength);
494 if(!fullCmdLine) return FALSE; /* return false on memory alloc failure */
496 /* build the full command line from the executable file and the command line being passed in */
497 snprintf(fullCmdLine, fullCmdLength, "%s ", filename); /* start off with the executable filename and a space */
498 memcpy(fullCmdLine + strlen(fullCmdLine), cmdline + 1, cmdLength); /* append cmdline onto the end */
499 fullCmdLine[fullCmdLength - 1] = 0; /* null terminate string */
501 ZeroMemory (&st, sizeof(STARTUPINFOA));
502 st.cb = sizeof(STARTUPINFOA);
503 ret = CreateProcessA (NULL, fullCmdLine, NULL, NULL, TRUE, 0, envblock, NULL, &st, &pe);
505 /* wait for the app to finish and clean up PROCESS_INFORMATION handles */
508 WaitForSingleObject(pe.hProcess, INFINITE); /* wait here until the child process is complete */
509 CloseHandle(pe.hProcess);
510 CloseHandle(pe.hThread);
513 HeapFree(GetProcessHeap(), 0, fullCmdLine); /* free the memory we allocated */
517 FIXME("EXEC type of %d not implemented for non-dos executables\n", func);
522 } /* if(binType != SCS_DOS_BINARY) */
525 /* handle dos executables */
527 hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
528 NULL, OPEN_EXISTING, 0, 0);
529 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
532 case 0: /* load and execute */
533 case 1: /* load but don't execute */
535 /* save current process's return SS:SP now */
536 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
537 PDB16 *psp = (PDB16 *)psp_start;
538 psp->saveStack = (DWORD)MAKESEGPTR(context->SegSs, LOWORD(context->Esp));
540 ret = MZ_DoLoadImage( hFile, filename, NULL, ((ExecBlock *)paramblk)->env_seg );
542 /* MZ_LoadImage created a new PSP and loaded new values into it,
543 * let's work on the new values now */
544 LPBYTE psp_start = (LPBYTE)((DWORD)DOSVM_psp << 4);
545 ExecBlock *blk = paramblk;
546 LPBYTE cmdline = PTR_REAL_TO_LIN(SELECTOROF(blk->cmdline),OFFSETOF(blk->cmdline));
548 /* First character contains the length of the command line. */
549 MZ_FillPSP(psp_start, (LPSTR)cmdline + 1, cmdline[0]);
551 /* the lame MS-DOS engineers decided that the return address should be in int22 */
552 DOSVM_SetRMHandler(0x22, (FARPROC16)MAKESEGPTR(context->SegCs, LOWORD(context->Eip)));
554 /* don't execute, just return startup state */
557 * For function 01h, the AX value to be passed to the child program
558 * is put on top of the child's stack
562 stack = CTX_SEG_OFF_TO_LIN(context, init_ss, init_sp);
563 /* FIXME: push AX correctly */
564 stack[0] = 0x00; /* push AL */
565 stack[1] = 0x00; /* push AH */
567 blk->init_cs = init_cs;
568 blk->init_ip = init_ip;
569 blk->init_ss = init_ss;
570 blk->init_sp = init_sp;
572 /* execute by making us return to new process */
573 context->SegCs = init_cs;
574 context->Eip = init_ip;
575 context->SegSs = init_ss;
576 context->Esp = init_sp;
577 context->SegDs = DOSVM_psp;
578 context->SegEs = DOSVM_psp;
583 case 3: /* load overlay */
585 OverlayBlock *blk = paramblk;
586 ret = MZ_DoLoadImage( hFile, filename, blk, 0);
590 FIXME("EXEC load type %d not implemented\n", func);
591 SetLastError(ERROR_INVALID_FUNCTION);
598 /***********************************************************************
601 void WINAPI MZ_AllocDPMITask( void )
607 /***********************************************************************
610 void WINAPI MZ_RunInThread( PAPCFUNC proc, ULONG_PTR arg )
618 event = CreateEventW(NULL, TRUE, FALSE, NULL);
619 PostThreadMessageA(loop_tid, WM_USER, (WPARAM)event, (LPARAM)&spc);
620 WaitForSingleObject(event, INFINITE);
626 static DWORD WINAPI MZ_DOSVM( LPVOID lpExtra )
631 dosvm_pid = getpid();
633 memset( &context, 0, sizeof(context) );
634 context.SegCs = init_cs;
635 context.Eip = init_ip;
636 context.SegSs = init_ss;
637 context.Esp = init_sp;
638 context.SegDs = DOSVM_psp;
639 context.SegEs = DOSVM_psp;
640 context.EFlags = V86_FLAG | VIF_MASK;
641 DOSVM_SetTimer(0x10000);
642 ret = DOSVM_Enter( &context );
645 /* fetch the app name from the environment */
646 PDB16 *psp = PTR_REAL_TO_LIN( DOSVM_psp, 0 );
647 char *env = PTR_REAL_TO_LIN( psp->environment, 0 );
648 while (*env) env += strlen(env) + 1;
649 env += 1 + sizeof(WORD);
651 if (GetLastError() == ERROR_NOT_SUPPORTED)
652 MESSAGE( "wine: Cannot start DOS application %s\n"
653 " because vm86 mode is not supported on this platform.\n",
656 FIXME( "vm86 mode failed error %u\n", GetLastError() );
662 static BOOL MZ_InitTask(void)
664 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
665 GetCurrentProcess(), &loop_thread,
666 0, FALSE, DUPLICATE_SAME_ACCESS))
668 dosvm_thread = CreateThread(NULL, 0, MZ_DOSVM, NULL, CREATE_SUSPENDED, &dosvm_tid);
670 CloseHandle(loop_thread);
674 loop_tid = GetCurrentThreadId();
678 static void MZ_Launch( LPCSTR cmdtail, int length )
680 TDB *pTask = GlobalLock16( GetCurrentTask() );
681 BYTE *psp_start = PTR_REAL_TO_LIN( DOSVM_psp, 0 );
686 MZ_FillPSP(psp_start, cmdtail, length);
687 pTask->flags |= TDBF_WINOLDAP;
689 /* DTA is set to PSP:0080h when a program is started. */
690 pTask->dta = MAKESEGPTR( DOSVM_psp, 0x80 );
692 GetpWin16Lock( &lock );
693 _LeaveSysLevel( lock );
695 /* force the message queue to be created */
696 PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
698 ResumeThread(dosvm_thread);
699 rv = DOSVM_Loop(dosvm_thread);
701 CloseHandle(dosvm_thread);
702 dosvm_thread = 0; dosvm_tid = 0;
703 CloseHandle(loop_thread);
704 loop_thread = 0; loop_tid = 0;
710 /***********************************************************************
713 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
716 WORD psp_seg = cs_psp ? context->SegCs : DOSVM_psp;
717 LPBYTE psp_start = (LPBYTE)((DWORD)psp_seg << 4);
718 PDB16 *psp = (PDB16 *)psp_start;
719 WORD parpsp = psp->parentPSP; /* check for parent DOS process */
721 /* retrieve parent's return address */
722 FARPROC16 retaddr = DOSVM_GetRMHandler(0x22);
723 /* restore interrupts */
724 DOSVM_SetRMHandler(0x22, psp->savedint22);
725 DOSVM_SetRMHandler(0x23, psp->savedint23);
726 DOSVM_SetRMHandler(0x24, psp->savedint24);
727 /* FIXME: deallocate file handles etc */
728 /* free process's associated memory
729 * FIXME: walk memory and deallocate all blocks owned by process */
730 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(psp->environment,0) );
731 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(DOSVM_psp,0) );
732 /* switch to parent's PSP */
734 psp_start = (LPBYTE)((DWORD)parpsp << 4);
735 psp = (PDB16 *)psp_start;
736 /* now return to parent */
737 DOSVM_retval = retval;
738 context->SegCs = SELECTOROF(retaddr);
739 context->Eip = OFFSETOF(retaddr);
740 context->SegSs = SELECTOROF(psp->saveStack);
741 context->Esp = OFFSETOF(psp->saveStack);
744 TRACE("killing DOS task\n");
746 DOSVM_Exit( retval );
750 /***********************************************************************
753 BOOL WINAPI MZ_Current( void )
755 return (dosvm_pid != 0); /* FIXME: do a better check */
758 #else /* !MZ_SUPPORTED */
760 /***********************************************************************
761 * wine_load_dos_exe (WINEDOS.@)
763 void WINAPI wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline )
765 FIXME("DOS executables not supported on this platform\n");
766 SetLastError(ERROR_BAD_FORMAT);
769 /***********************************************************************
772 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
775 SetLastError(ERROR_BAD_FORMAT);
779 /***********************************************************************
782 void WINAPI MZ_AllocDPMITask( void )
784 FIXME("Actual real-mode calls not supported on this platform!\n");
787 /***********************************************************************
790 void WINAPI MZ_RunInThread( PAPCFUNC proc, ULONG_PTR arg )
795 /***********************************************************************
798 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
800 DOSVM_Exit( retval );
803 /***********************************************************************
806 BOOL WINAPI MZ_Current( void )
811 #endif /* !MZ_SUPPORTED */