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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: This code hasn't been completely cleaned up yet.
24 #include "wine/port.h"
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
40 #include "wine/winbase16.h"
47 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(module
);
54 static BOOL DOSVM_isdosexe
;
56 /**********************************************************************
59 * Return TRUE if we are in Windows process.
61 BOOL
DOSVM_IsWin16(void)
63 return DOSVM_isdosexe
? FALSE
: TRUE
;
68 #ifdef HAVE_SYS_MMAN_H
69 # include <sys/mman.h>
72 /* define this to try mapping through /proc/pid/mem instead of a temp file,
73 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
76 #define BIOS_DATA_SEGMENT 0x40
79 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
80 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
82 /* structures for EXEC */
86 DWORD cmdline WINE_PACKED
;
87 DWORD fcb1 WINE_PACKED
;
88 DWORD fcb2 WINE_PACKED
;
100 /* global variables */
104 static WORD init_cs
,init_ip
,init_ss
,init_sp
;
105 static HANDLE dosvm_thread
, loop_thread
;
106 static DWORD dosvm_tid
, loop_tid
;
108 static void MZ_Launch( LPCSTR cmdtail
, int length
);
109 static BOOL
MZ_InitTask(void);
111 static void MZ_CreatePSP( LPVOID lpPSP
, WORD env
, WORD par
)
115 psp
->int20
=0x20CD; /* int 20 */
116 /* some programs use this to calculate how much memory they need */
117 psp
->nextParagraph
=0x9FFF; /* FIXME: use a real value */
118 /* FIXME: dispatcher */
119 psp
->savedint22
= DOSVM_GetRMHandler(0x22);
120 psp
->savedint23
= DOSVM_GetRMHandler(0x23);
121 psp
->savedint24
= DOSVM_GetRMHandler(0x24);
123 psp
->environment
=env
;
124 /* FIXME: more PSP stuff */
127 static void MZ_FillPSP( LPVOID lpPSP
, LPCSTR cmdtail
, int length
)
133 WARN( "Command tail truncated! (length %d)\n", length
);
137 psp
->cmdLine
[0] = length
;
140 * Length of exactly 127 bytes means that full command line is
141 * stored in environment variable CMDLINE and PSP contains
142 * command tail truncated to 126 bytes.
148 memmove(psp
->cmdLine
+1, cmdtail
, length
);
150 psp
->cmdLine
[length
+1] = '\r';
152 /* FIXME: more PSP stuff */
155 static WORD
MZ_InitEnvironment( LPCSTR env
, LPCSTR name
)
162 /* get size of environment block */
163 while (env
[sz
++]) sz
+=strlen(env
+sz
)+1;
166 envblk
=DOSMEM_GetBlock(sz
+sizeof(WORD
)+strlen(name
)+1,&seg
);
169 memcpy(envblk
,env
,sz
);
171 /* DOS 3.x: the block contains 1 additional string */
172 *(WORD
*)(envblk
+sz
)=1;
173 /* being the program name itself */
174 strcpy(envblk
+sz
+sizeof(WORD
),name
);
178 static BOOL
MZ_InitMemory(void)
180 /* initialize the memory */
181 TRACE("Initializing DOS memory structures\n");
183 DOSDEV_InstallDOSDevices();
188 static BOOL
MZ_DoLoadImage( HANDLE hFile
, LPCSTR filename
, OverlayBlock
*oblk
)
190 IMAGE_DOS_HEADER mz_header
;
191 DWORD image_start
,image_size
,min_size
,max_size
,avail
;
192 BYTE
*psp_start
,*load_start
,*oldenv
;
193 int x
, old_com
=0, alloc
;
195 WORD env_seg
, load_seg
, rel_seg
, oldpsp_seg
;
199 /* DOS process already running, inherit from it */
200 PDB16
* par_psp
= (PDB16
*)((DWORD
)DOSVM_psp
<< 4);
202 oldenv
= (LPBYTE
)((DWORD
)par_psp
->environment
<< 4);
203 oldpsp_seg
= DOSVM_psp
;
205 /* allocate new DOS process, inheriting from Wine environment */
207 oldenv
= GetEnvironmentStringsA();
211 SetFilePointer(hFile
,0,NULL
,FILE_BEGIN
);
212 if ( !ReadFile(hFile
,&mz_header
,sizeof(mz_header
),&len
,NULL
)
213 || len
!= sizeof(mz_header
)
214 || mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
215 char *p
= strrchr( filename
, '.' );
216 if (!p
|| strcasecmp( p
, ".com" )) /* check for .COM extension */
218 SetLastError(ERROR_BAD_FORMAT
);
221 old_com
=1; /* assume .COM file */
223 image_size
=GetFileSize(hFile
,NULL
);
224 min_size
=0x10000; max_size
=0x100000;
226 mz_header
.e_ss
=0; mz_header
.e_sp
=0xFFFE;
227 mz_header
.e_cs
=0; mz_header
.e_ip
=0x100;
229 /* calculate load size */
230 image_start
=mz_header
.e_cparhdr
<<4;
231 image_size
=mz_header
.e_cp
<<9; /* pages are 512 bytes */
232 if ((mz_header
.e_cblp
!=0)&&(mz_header
.e_cblp
!=4)) image_size
-=512-mz_header
.e_cblp
;
233 image_size
-=image_start
;
234 min_size
=image_size
+((DWORD
)mz_header
.e_minalloc
<<4)+(PSP_SIZE
<<4);
235 max_size
=image_size
+((DWORD
)mz_header
.e_maxalloc
<<4)+(PSP_SIZE
<<4);
238 if (alloc
) MZ_InitMemory();
241 /* load overlay into preallocated memory */
242 load_seg
=oblk
->load_seg
;
243 rel_seg
=oblk
->rel_seg
;
244 load_start
=(LPBYTE
)((DWORD
)load_seg
<<4);
246 /* allocate environment block */
247 env_seg
=MZ_InitEnvironment(oldenv
, filename
);
249 /* allocate memory for the executable */
250 TRACE("Allocating DOS memory (min=%ld, max=%ld)\n",min_size
,max_size
);
251 avail
=DOSMEM_Available();
252 if (avail
<min_size
) {
253 ERR("insufficient DOS memory\n");
254 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
257 if (avail
>max_size
) avail
=max_size
;
258 psp_start
=DOSMEM_GetBlock(avail
,&DOSVM_psp
);
260 ERR("error allocating DOS memory\n");
261 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
264 load_seg
=DOSVM_psp
+(old_com
?0:PSP_SIZE
);
266 load_start
=psp_start
+(PSP_SIZE
<<4);
267 MZ_CreatePSP(psp_start
, env_seg
, oldpsp_seg
);
270 /* load executable image */
271 TRACE("loading DOS %s image, %08lx bytes\n",old_com
?"COM":"EXE",image_size
);
272 SetFilePointer(hFile
,image_start
,NULL
,FILE_BEGIN
);
273 if (!ReadFile(hFile
,load_start
,image_size
,&len
,NULL
) || len
!= image_size
) {
274 SetLastError(ERROR_BAD_FORMAT
);
278 if (mz_header
.e_crlc
) {
279 /* load relocation table */
280 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header
.e_crlc
);
281 /* FIXME: is this too slow without read buffering? */
282 SetFilePointer(hFile
,mz_header
.e_lfarlc
,NULL
,FILE_BEGIN
);
283 for (x
=0; x
<mz_header
.e_crlc
; x
++) {
284 if (!ReadFile(hFile
,&reloc
,sizeof(reloc
),&len
,NULL
) || len
!= sizeof(reloc
)) {
285 SetLastError(ERROR_BAD_FORMAT
);
288 *(WORD
*)SEGPTR16(load_start
,reloc
)+=rel_seg
;
293 init_cs
= load_seg
+mz_header
.e_cs
;
294 init_ip
= mz_header
.e_ip
;
295 init_ss
= load_seg
+mz_header
.e_ss
;
296 init_sp
= mz_header
.e_sp
;
298 TRACE("entry point: %04x:%04x\n",init_cs
,init_ip
);
301 if (alloc
&& !MZ_InitTask()) {
302 SetLastError(ERROR_GEN_FAILURE
);
309 DOSVM_psp
= oldpsp_seg
;
314 /***********************************************************************
315 * wine_load_dos_exe (WINEDOS.@)
317 * Called from WineVDM when a new real-mode DOS process is started.
318 * Loads DOS program into memory and executes the program.
320 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
322 char dos_cmdtail
[126];
325 HANDLE hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
326 NULL
, OPEN_EXISTING
, 0, 0 );
327 if (hFile
== INVALID_HANDLE_VALUE
) return;
328 DOSVM_isdosexe
= TRUE
;
330 if(cmdline
&& *cmdline
)
332 dos_length
= strlen(cmdline
);
333 memmove( dos_cmdtail
+ 1, cmdline
,
334 (dos_length
< 125) ? dos_length
: 125 );
336 /* Non-empty command tail always starts with at least one space. */
337 dos_cmdtail
[0] = ' ';
341 * If command tail is longer than 126 characters,
342 * set tail length to 127 and fill CMDLINE environment variable
343 * with full command line (this includes filename).
345 if (dos_length
> 126)
347 char *cmd
= HeapAlloc( GetProcessHeap(), 0,
348 dos_length
+ strlen(filename
) + 4 );
355 * Append filename. If path includes spaces, quote the path.
357 if (strchr(filename
, ' '))
360 strcpy( ptr
, filename
);
361 ptr
+= strlen(filename
);
366 strcpy( ptr
, filename
);
367 ptr
+= strlen(filename
);
371 * Append command tail.
373 if (cmdline
[0] != ' ')
375 strcpy( ptr
, cmdline
);
378 * Set environment variable. This will be passed to
381 if (!SetEnvironmentVariableA( "CMDLINE", cmd
))
383 HeapFree(GetProcessHeap(), 0, cmd
);
387 HeapFree(GetProcessHeap(), 0, cmd
);
392 if (MZ_DoLoadImage( hFile
, filename
, NULL
))
393 MZ_Launch( dos_cmdtail
, dos_length
);
396 /***********************************************************************
399 * this may only be called from existing DOS processes
401 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
405 PROCESS_INFORMATION pe
;
410 if(!GetBinaryTypeA(filename
, &binType
)) /* determine what kind of binary this is */
412 return FALSE
; /* binary is not an executable */
415 /* handle non-dos executables */
416 if(binType
!= SCS_DOS_BINARY
)
418 if(func
== 0) /* load and execute */
422 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
423 PDB16
*psp
= (PDB16
*)psp_start
;
424 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
425 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
426 LPBYTE envblock
= PTR_REAL_TO_LIN(psp
->environment
, 0);
427 int cmdLength
= cmdline
[0];
430 * If cmdLength is 127, command tail is truncated and environment
431 * variable CMDLINE should contain full command line
432 * (this includes filename).
434 if (cmdLength
== 127)
436 FIXME( "CMDLINE argument passing is unimplemented.\n" );
437 cmdLength
= 126; /* FIXME */
440 fullCmdLength
= (strlen(filename
) + 1) + cmdLength
+ 1; /* filename + space + cmdline + terminating null character */
442 fullCmdLine
= HeapAlloc(GetProcessHeap(), 0, fullCmdLength
);
443 if(!fullCmdLine
) return FALSE
; /* return false on memory alloc failure */
445 /* build the full command line from the executable file and the command line being passed in */
446 snprintf(fullCmdLine
, fullCmdLength
, "%s ", filename
); /* start off with the executable filename and a space */
447 memcpy(fullCmdLine
+ strlen(fullCmdLine
), cmdline
+ 1, cmdLength
); /* append cmdline onto the end */
448 fullCmdLine
[fullCmdLength
- 1] = 0; /* null terminate string */
450 ZeroMemory (&st
, sizeof(STARTUPINFOA
));
451 st
.cb
= sizeof(STARTUPINFOA
);
452 ret
= CreateProcessA (NULL
, fullCmdLine
, NULL
, NULL
, TRUE
, 0, envblock
, NULL
, &st
, &pe
);
454 /* wait for the app to finish and clean up PROCESS_INFORMATION handles */
457 WaitForSingleObject(pe
.hProcess
, INFINITE
); /* wait here until the child process is complete */
458 CloseHandle(pe
.hProcess
);
459 CloseHandle(pe
.hThread
);
462 HeapFree(GetProcessHeap(), 0, fullCmdLine
); /* free the memory we allocated */
466 FIXME("EXEC type of %d not implemented for non-dos executables\n", func
);
471 } /* if(binType != SCS_DOS_BINARY) */
474 /* handle dos executables */
476 hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
477 NULL
, OPEN_EXISTING
, 0, 0);
478 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
481 case 0: /* load and execute */
482 case 1: /* load but don't execute */
484 /* save current process's return SS:SP now */
485 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
486 PDB16
*psp
= (PDB16
*)psp_start
;
487 psp
->saveStack
= (DWORD
)MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
));
489 ret
= MZ_DoLoadImage( hFile
, filename
, NULL
);
491 /* MZ_LoadImage created a new PSP and loaded new values into it,
492 * let's work on the new values now */
493 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
494 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
495 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
497 /* First character contains the length of the command line. */
498 MZ_FillPSP(psp_start
, cmdline
+ 1, cmdline
[0]);
500 /* the lame MS-DOS engineers decided that the return address should be in int22 */
501 DOSVM_SetRMHandler(0x22, (FARPROC16
)MAKESEGPTR(context
->SegCs
, LOWORD(context
->Eip
)));
503 /* don't execute, just return startup state */
504 blk
->init_cs
= init_cs
;
505 blk
->init_ip
= init_ip
;
506 blk
->init_ss
= init_ss
;
507 blk
->init_sp
= init_sp
;
509 /* execute by making us return to new process */
510 context
->SegCs
= init_cs
;
511 context
->Eip
= init_ip
;
512 context
->SegSs
= init_ss
;
513 context
->Esp
= init_sp
;
514 context
->SegDs
= DOSVM_psp
;
515 context
->SegEs
= DOSVM_psp
;
520 case 3: /* load overlay */
522 OverlayBlock
*blk
= (OverlayBlock
*)paramblk
;
523 ret
= MZ_DoLoadImage( hFile
, filename
, blk
);
527 FIXME("EXEC load type %d not implemented\n", func
);
528 SetLastError(ERROR_INVALID_FUNCTION
);
535 /***********************************************************************
538 void WINAPI
MZ_AllocDPMITask( void )
544 /***********************************************************************
547 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
555 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
556 PostThreadMessageA(loop_tid
, WM_USER
, (WPARAM
)event
, (LPARAM
)&spc
);
557 WaitForSingleObject(event
, INFINITE
);
563 static DWORD WINAPI
MZ_DOSVM( LPVOID lpExtra
)
568 dosvm_pid
= getpid();
570 memset( &context
, 0, sizeof(context
) );
571 context
.SegCs
= init_cs
;
572 context
.Eip
= init_ip
;
573 context
.SegSs
= init_ss
;
574 context
.Esp
= init_sp
;
575 context
.SegDs
= DOSVM_psp
;
576 context
.SegEs
= DOSVM_psp
;
577 context
.EFlags
= 0x00080000; /* virtual interrupt flag */
578 DOSVM_SetTimer(0x10000);
579 ret
= DOSVM_Enter( &context
);
585 static BOOL
MZ_InitTask(void)
587 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
588 GetCurrentProcess(), &loop_thread
,
589 0, FALSE
, DUPLICATE_SAME_ACCESS
))
591 dosvm_thread
= CreateThread(NULL
, 0, MZ_DOSVM
, NULL
, CREATE_SUSPENDED
, &dosvm_tid
);
593 CloseHandle(loop_thread
);
597 loop_tid
= GetCurrentThreadId();
601 static void MZ_Launch( LPCSTR cmdtail
, int length
)
603 TDB
*pTask
= GlobalLock16( GetCurrentTask() );
604 BYTE
*psp_start
= PTR_REAL_TO_LIN( DOSVM_psp
, 0 );
608 MZ_FillPSP(psp_start
, cmdtail
, length
);
609 pTask
->flags
|= TDBF_WINOLDAP
;
611 /* DTA is set to PSP:0080h when a program is started. */
612 pTask
->dta
= MAKESEGPTR( DOSVM_psp
, 0x80 );
614 GetpWin16Lock( &lock
);
615 _LeaveSysLevel( lock
);
617 ResumeThread(dosvm_thread
);
618 rv
= DOSVM_Loop(dosvm_thread
);
620 CloseHandle(dosvm_thread
);
621 dosvm_thread
= 0; dosvm_tid
= 0;
622 CloseHandle(loop_thread
);
623 loop_thread
= 0; loop_tid
= 0;
629 /***********************************************************************
632 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
635 WORD psp_seg
= cs_psp
? context
->SegCs
: DOSVM_psp
;
636 LPBYTE psp_start
= (LPBYTE
)((DWORD
)psp_seg
<< 4);
637 PDB16
*psp
= (PDB16
*)psp_start
;
638 WORD parpsp
= psp
->parentPSP
; /* check for parent DOS process */
640 /* retrieve parent's return address */
641 FARPROC16 retaddr
= DOSVM_GetRMHandler(0x22);
642 /* restore interrupts */
643 DOSVM_SetRMHandler(0x22, psp
->savedint22
);
644 DOSVM_SetRMHandler(0x23, psp
->savedint23
);
645 DOSVM_SetRMHandler(0x24, psp
->savedint24
);
646 /* FIXME: deallocate file handles etc */
647 /* free process's associated memory
648 * FIXME: walk memory and deallocate all blocks owned by process */
649 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(psp
->environment
,0) );
650 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(DOSVM_psp
,0) );
651 /* switch to parent's PSP */
653 psp_start
= (LPBYTE
)((DWORD
)parpsp
<< 4);
654 psp
= (PDB16
*)psp_start
;
655 /* now return to parent */
656 DOSVM_retval
= retval
;
657 context
->SegCs
= SELECTOROF(retaddr
);
658 context
->Eip
= OFFSETOF(retaddr
);
659 context
->SegSs
= SELECTOROF(psp
->saveStack
);
660 context
->Esp
= OFFSETOF(psp
->saveStack
);
663 TRACE("killing DOS task\n");
665 ExitThread( retval
);
669 /***********************************************************************
672 BOOL WINAPI
MZ_Current( void )
674 return (dosvm_pid
!= 0); /* FIXME: do a better check */
677 #else /* !MZ_SUPPORTED */
679 /***********************************************************************
680 * wine_load_dos_exe (WINEDOS.@)
682 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
684 WARN("DOS executables not supported on this platform\n");
685 SetLastError(ERROR_BAD_FORMAT
);
688 /***********************************************************************
691 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
694 SetLastError(ERROR_BAD_FORMAT
);
698 /***********************************************************************
701 void WINAPI
MZ_AllocDPMITask( void )
703 ERR("Actual real-mode calls not supported on this platform!\n");
706 /***********************************************************************
709 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
714 /***********************************************************************
717 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
719 ExitThread( retval
);
722 /***********************************************************************
725 BOOL WINAPI
MZ_Current( void )
730 #endif /* !MZ_SUPPORTED */