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"
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
42 #include "wine/winbase16.h"
46 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(module
);
53 static BOOL DOSVM_isdosexe
;
55 /**********************************************************************
58 * Return TRUE if we are in Windows process.
60 BOOL
DOSVM_IsWin16(void)
62 return DOSVM_isdosexe
? FALSE
: TRUE
;
67 #ifdef HAVE_SYS_MMAN_H
68 # include <sys/mman.h>
71 /* define this to try mapping through /proc/pid/mem instead of a temp file,
72 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
75 #define BIOS_DATA_SEGMENT 0x40
78 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
79 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
81 /* structures for EXEC */
85 DWORD cmdline WINE_PACKED
;
86 DWORD fcb1 WINE_PACKED
;
87 DWORD fcb2 WINE_PACKED
;
99 /* global variables */
103 static WORD init_cs
,init_ip
,init_ss
,init_sp
;
104 static HANDLE dosvm_thread
, loop_thread
;
105 static DWORD dosvm_tid
, loop_tid
;
107 static void MZ_Launch( LPCSTR cmdtail
, int length
);
108 static BOOL
MZ_InitTask(void);
110 static void MZ_CreatePSP( LPVOID lpPSP
, WORD env
, WORD par
)
114 psp
->int20
=0x20CD; /* int 20 */
115 /* some programs use this to calculate how much memory they need */
116 psp
->nextParagraph
=0x9FFF; /* FIXME: use a real value */
117 /* FIXME: dispatcher */
118 psp
->savedint22
= DOSVM_GetRMHandler(0x22);
119 psp
->savedint23
= DOSVM_GetRMHandler(0x23);
120 psp
->savedint24
= DOSVM_GetRMHandler(0x24);
122 psp
->environment
=env
;
123 /* FIXME: more PSP stuff */
126 static void MZ_FillPSP( LPVOID lpPSP
, LPCSTR cmdtail
, int length
)
132 WARN( "Command tail truncated! (length %d)\n", length
);
136 psp
->cmdLine
[0] = length
;
139 * Length of exactly 127 bytes means that full command line is
140 * stored in environment variable CMDLINE and PSP contains
141 * command tail truncated to 126 bytes.
147 memmove(psp
->cmdLine
+1, cmdtail
, length
);
149 psp
->cmdLine
[length
+1] = '\r';
151 /* FIXME: more PSP stuff */
154 static WORD
MZ_InitEnvironment( LPCSTR env
, LPCSTR name
)
161 /* get size of environment block */
162 while (env
[sz
++]) sz
+=strlen(env
+sz
)+1;
165 envblk
=DOSMEM_GetBlock(sz
+sizeof(WORD
)+strlen(name
)+1,&seg
);
168 memcpy(envblk
,env
,sz
);
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
);
177 static BOOL
MZ_InitMemory(void)
179 /* initialize the memory */
180 TRACE("Initializing DOS memory structures\n");
182 DOSDEV_InstallDOSDevices();
187 static BOOL
MZ_DoLoadImage( HANDLE hFile
, LPCSTR filename
, OverlayBlock
*oblk
)
189 IMAGE_DOS_HEADER mz_header
;
190 DWORD image_start
,image_size
,min_size
,max_size
,avail
;
191 BYTE
*psp_start
,*load_start
,*oldenv
;
192 int x
, old_com
=0, alloc
;
194 WORD env_seg
, load_seg
, rel_seg
, oldpsp_seg
;
198 /* DOS process already running, inherit from it */
199 PDB16
* par_psp
= (PDB16
*)((DWORD
)DOSVM_psp
<< 4);
201 oldenv
= (LPBYTE
)((DWORD
)par_psp
->environment
<< 4);
202 oldpsp_seg
= DOSVM_psp
;
204 /* allocate new DOS process, inheriting from Wine environment */
206 oldenv
= GetEnvironmentStringsA();
210 SetFilePointer(hFile
,0,NULL
,FILE_BEGIN
);
211 if ( !ReadFile(hFile
,&mz_header
,sizeof(mz_header
),&len
,NULL
)
212 || len
!= sizeof(mz_header
)
213 || mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
214 char *p
= strrchr( filename
, '.' );
215 if (!p
|| strcasecmp( p
, ".com" )) /* check for .COM extension */
217 SetLastError(ERROR_BAD_FORMAT
);
220 old_com
=1; /* assume .COM file */
222 image_size
=GetFileSize(hFile
,NULL
);
223 min_size
=0x10000; max_size
=0x100000;
225 mz_header
.e_ss
=0; mz_header
.e_sp
=0xFFFE;
226 mz_header
.e_cs
=0; mz_header
.e_ip
=0x100;
228 /* calculate load size */
229 image_start
=mz_header
.e_cparhdr
<<4;
230 image_size
=mz_header
.e_cp
<<9; /* pages are 512 bytes */
231 if ((mz_header
.e_cblp
!=0)&&(mz_header
.e_cblp
!=4)) image_size
-=512-mz_header
.e_cblp
;
232 image_size
-=image_start
;
233 min_size
=image_size
+((DWORD
)mz_header
.e_minalloc
<<4)+(PSP_SIZE
<<4);
234 max_size
=image_size
+((DWORD
)mz_header
.e_maxalloc
<<4)+(PSP_SIZE
<<4);
237 if (alloc
) MZ_InitMemory();
240 /* load overlay into preallocated memory */
241 load_seg
=oblk
->load_seg
;
242 rel_seg
=oblk
->rel_seg
;
243 load_start
=(LPBYTE
)((DWORD
)load_seg
<<4);
245 /* allocate environment block */
246 env_seg
=MZ_InitEnvironment(oldenv
, filename
);
248 /* allocate memory for the executable */
249 TRACE("Allocating DOS memory (min=%ld, max=%ld)\n",min_size
,max_size
);
250 avail
=DOSMEM_Available();
251 if (avail
<min_size
) {
252 ERR("insufficient DOS memory\n");
253 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
256 if (avail
>max_size
) avail
=max_size
;
257 psp_start
=DOSMEM_GetBlock(avail
,&DOSVM_psp
);
259 ERR("error allocating DOS memory\n");
260 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
263 load_seg
=DOSVM_psp
+(old_com
?0:PSP_SIZE
);
265 load_start
=psp_start
+(PSP_SIZE
<<4);
266 MZ_CreatePSP(psp_start
, env_seg
, oldpsp_seg
);
269 /* load executable image */
270 TRACE("loading DOS %s image, %08lx bytes\n",old_com
?"COM":"EXE",image_size
);
271 SetFilePointer(hFile
,image_start
,NULL
,FILE_BEGIN
);
272 if (!ReadFile(hFile
,load_start
,image_size
,&len
,NULL
) || len
!= image_size
) {
273 SetLastError(ERROR_BAD_FORMAT
);
277 if (mz_header
.e_crlc
) {
278 /* load relocation table */
279 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header
.e_crlc
);
280 /* FIXME: is this too slow without read buffering? */
281 SetFilePointer(hFile
,mz_header
.e_lfarlc
,NULL
,FILE_BEGIN
);
282 for (x
=0; x
<mz_header
.e_crlc
; x
++) {
283 if (!ReadFile(hFile
,&reloc
,sizeof(reloc
),&len
,NULL
) || len
!= sizeof(reloc
)) {
284 SetLastError(ERROR_BAD_FORMAT
);
287 *(WORD
*)SEGPTR16(load_start
,reloc
)+=rel_seg
;
292 init_cs
= load_seg
+mz_header
.e_cs
;
293 init_ip
= mz_header
.e_ip
;
294 init_ss
= load_seg
+mz_header
.e_ss
;
295 init_sp
= mz_header
.e_sp
;
297 TRACE("entry point: %04x:%04x\n",init_cs
,init_ip
);
300 if (alloc
&& !MZ_InitTask()) {
301 SetLastError(ERROR_GEN_FAILURE
);
308 DOSVM_psp
= oldpsp_seg
;
313 /***********************************************************************
314 * wine_load_dos_exe (WINEDOS.@)
316 * Called from WineVDM when a new real-mode DOS process is started.
317 * Loads DOS program into memory and executes the program.
319 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
321 char dos_cmdtail
[126];
324 HANDLE hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
325 NULL
, OPEN_EXISTING
, 0, 0 );
326 if (hFile
== INVALID_HANDLE_VALUE
) return;
327 DOSVM_isdosexe
= TRUE
;
329 if(cmdline
&& *cmdline
)
331 dos_length
= strlen(cmdline
);
332 memmove( dos_cmdtail
+ 1, cmdline
,
333 (dos_length
< 125) ? dos_length
: 125 );
335 /* Non-empty command tail always starts with at least one space. */
336 dos_cmdtail
[0] = ' ';
340 * If command tail is longer than 126 characters,
341 * set tail length to 127 and fill CMDLINE environment variable
342 * with full command line (this includes filename).
344 if (dos_length
> 126)
346 char *cmd
= HeapAlloc( GetProcessHeap(), 0,
347 dos_length
+ strlen(filename
) + 4 );
354 * Append filename. If path includes spaces, quote the path.
356 if (strchr(filename
, ' '))
359 strcpy( ptr
, filename
);
360 ptr
+= strlen(filename
);
365 strcpy( ptr
, filename
);
366 ptr
+= strlen(filename
);
370 * Append command tail.
372 if (cmdline
[0] != ' ')
374 strcpy( ptr
, cmdline
);
377 * Set environment variable. This will be passed to
380 if (!SetEnvironmentVariableA( "CMDLINE", cmd
))
382 HeapFree(GetProcessHeap(), 0, cmd
);
386 HeapFree(GetProcessHeap(), 0, cmd
);
391 if (MZ_DoLoadImage( hFile
, filename
, NULL
))
392 MZ_Launch( dos_cmdtail
, dos_length
);
395 /***********************************************************************
398 * this may only be called from existing DOS processes
400 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
404 PROCESS_INFORMATION pe
;
409 if(!GetBinaryTypeA(filename
, &binType
)) /* determine what kind of binary this is */
411 return FALSE
; /* binary is not an executable */
414 /* handle non-dos executables */
415 if(binType
!= SCS_DOS_BINARY
)
417 if(func
== 0) /* load and execute */
421 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
422 PDB16
*psp
= (PDB16
*)psp_start
;
423 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
424 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
425 LPBYTE envblock
= PTR_REAL_TO_LIN(psp
->environment
, 0);
426 int cmdLength
= cmdline
[0];
429 * If cmdLength is 127, command tail is truncated and environment
430 * variable CMDLINE should contain full command line
431 * (this includes filename).
433 if (cmdLength
== 127)
435 FIXME( "CMDLINE argument passing is unimplemented.\n" );
436 cmdLength
= 126; /* FIXME */
439 fullCmdLength
= (strlen(filename
) + 1) + cmdLength
+ 1; /* filename + space + cmdline + terminating null character */
441 fullCmdLine
= HeapAlloc(GetProcessHeap(), 0, fullCmdLength
);
442 if(!fullCmdLine
) return FALSE
; /* return false on memory alloc failure */
444 /* build the full command line from the executable file and the command line being passed in */
445 snprintf(fullCmdLine
, fullCmdLength
, "%s ", filename
); /* start off with the executable filename and a space */
446 memcpy(fullCmdLine
+ strlen(fullCmdLine
), cmdline
+ 1, cmdLength
); /* append cmdline onto the end */
447 fullCmdLine
[fullCmdLength
- 1] = 0; /* null terminate string */
449 ZeroMemory (&st
, sizeof(STARTUPINFOA
));
450 st
.cb
= sizeof(STARTUPINFOA
);
451 ret
= CreateProcessA (NULL
, fullCmdLine
, NULL
, NULL
, TRUE
, 0, envblock
, NULL
, &st
, &pe
);
453 /* wait for the app to finish and clean up PROCESS_INFORMATION handles */
456 WaitForSingleObject(pe
.hProcess
, INFINITE
); /* wait here until the child process is complete */
457 CloseHandle(pe
.hProcess
);
458 CloseHandle(pe
.hThread
);
461 HeapFree(GetProcessHeap(), 0, fullCmdLine
); /* free the memory we allocated */
465 FIXME("EXEC type of %d not implemented for non-dos executables\n", func
);
470 } /* if(binType != SCS_DOS_BINARY) */
473 /* handle dos executables */
475 hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
476 NULL
, OPEN_EXISTING
, 0, 0);
477 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
480 case 0: /* load and execute */
481 case 1: /* load but don't execute */
483 /* save current process's return SS:SP now */
484 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
485 PDB16
*psp
= (PDB16
*)psp_start
;
486 psp
->saveStack
= (DWORD
)MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
));
488 ret
= MZ_DoLoadImage( hFile
, filename
, NULL
);
490 /* MZ_LoadImage created a new PSP and loaded new values into it,
491 * let's work on the new values now */
492 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
493 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
494 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
496 /* First character contains the length of the command line. */
497 MZ_FillPSP(psp_start
, cmdline
+ 1, cmdline
[0]);
499 /* the lame MS-DOS engineers decided that the return address should be in int22 */
500 DOSVM_SetRMHandler(0x22, (FARPROC16
)MAKESEGPTR(context
->SegCs
, LOWORD(context
->Eip
)));
502 /* don't execute, just return startup state */
503 blk
->init_cs
= init_cs
;
504 blk
->init_ip
= init_ip
;
505 blk
->init_ss
= init_ss
;
506 blk
->init_sp
= init_sp
;
508 /* execute by making us return to new process */
509 context
->SegCs
= init_cs
;
510 context
->Eip
= init_ip
;
511 context
->SegSs
= init_ss
;
512 context
->Esp
= init_sp
;
513 context
->SegDs
= DOSVM_psp
;
514 context
->SegEs
= DOSVM_psp
;
519 case 3: /* load overlay */
521 OverlayBlock
*blk
= (OverlayBlock
*)paramblk
;
522 ret
= MZ_DoLoadImage( hFile
, filename
, blk
);
526 FIXME("EXEC load type %d not implemented\n", func
);
527 SetLastError(ERROR_INVALID_FUNCTION
);
534 /***********************************************************************
537 void WINAPI
MZ_AllocDPMITask( void )
543 /***********************************************************************
546 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
554 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
555 PostThreadMessageA(loop_tid
, WM_USER
, (WPARAM
)event
, (LPARAM
)&spc
);
556 WaitForSingleObject(event
, INFINITE
);
562 static DWORD WINAPI
MZ_DOSVM( LPVOID lpExtra
)
567 dosvm_pid
= getpid();
569 memset( &context
, 0, sizeof(context
) );
570 context
.SegCs
= init_cs
;
571 context
.Eip
= init_ip
;
572 context
.SegSs
= init_ss
;
573 context
.Esp
= init_sp
;
574 context
.SegDs
= DOSVM_psp
;
575 context
.SegEs
= DOSVM_psp
;
576 context
.EFlags
= V86_FLAG
| VIF_MASK
;
577 DOSVM_SetTimer(0x10000);
578 ret
= DOSVM_Enter( &context
);
584 static BOOL
MZ_InitTask(void)
586 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
587 GetCurrentProcess(), &loop_thread
,
588 0, FALSE
, DUPLICATE_SAME_ACCESS
))
590 dosvm_thread
= CreateThread(NULL
, 0, MZ_DOSVM
, NULL
, CREATE_SUSPENDED
, &dosvm_tid
);
592 CloseHandle(loop_thread
);
596 loop_tid
= GetCurrentThreadId();
600 static void MZ_Launch( LPCSTR cmdtail
, int length
)
602 TDB
*pTask
= GlobalLock16( GetCurrentTask() );
603 BYTE
*psp_start
= PTR_REAL_TO_LIN( DOSVM_psp
, 0 );
607 MZ_FillPSP(psp_start
, cmdtail
, length
);
608 pTask
->flags
|= TDBF_WINOLDAP
;
610 /* DTA is set to PSP:0080h when a program is started. */
611 pTask
->dta
= MAKESEGPTR( DOSVM_psp
, 0x80 );
613 GetpWin16Lock( &lock
);
614 _LeaveSysLevel( lock
);
616 ResumeThread(dosvm_thread
);
617 rv
= DOSVM_Loop(dosvm_thread
);
619 CloseHandle(dosvm_thread
);
620 dosvm_thread
= 0; dosvm_tid
= 0;
621 CloseHandle(loop_thread
);
622 loop_thread
= 0; loop_tid
= 0;
628 /***********************************************************************
631 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
634 WORD psp_seg
= cs_psp
? context
->SegCs
: DOSVM_psp
;
635 LPBYTE psp_start
= (LPBYTE
)((DWORD
)psp_seg
<< 4);
636 PDB16
*psp
= (PDB16
*)psp_start
;
637 WORD parpsp
= psp
->parentPSP
; /* check for parent DOS process */
639 /* retrieve parent's return address */
640 FARPROC16 retaddr
= DOSVM_GetRMHandler(0x22);
641 /* restore interrupts */
642 DOSVM_SetRMHandler(0x22, psp
->savedint22
);
643 DOSVM_SetRMHandler(0x23, psp
->savedint23
);
644 DOSVM_SetRMHandler(0x24, psp
->savedint24
);
645 /* FIXME: deallocate file handles etc */
646 /* free process's associated memory
647 * FIXME: walk memory and deallocate all blocks owned by process */
648 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(psp
->environment
,0) );
649 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(DOSVM_psp
,0) );
650 /* switch to parent's PSP */
652 psp_start
= (LPBYTE
)((DWORD
)parpsp
<< 4);
653 psp
= (PDB16
*)psp_start
;
654 /* now return to parent */
655 DOSVM_retval
= retval
;
656 context
->SegCs
= SELECTOROF(retaddr
);
657 context
->Eip
= OFFSETOF(retaddr
);
658 context
->SegSs
= SELECTOROF(psp
->saveStack
);
659 context
->Esp
= OFFSETOF(psp
->saveStack
);
662 TRACE("killing DOS task\n");
664 ExitThread( retval
);
668 /***********************************************************************
671 BOOL WINAPI
MZ_Current( void )
673 return (dosvm_pid
!= 0); /* FIXME: do a better check */
676 #else /* !MZ_SUPPORTED */
678 /***********************************************************************
679 * wine_load_dos_exe (WINEDOS.@)
681 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
683 WARN("DOS executables not supported on this platform\n");
684 SetLastError(ERROR_BAD_FORMAT
);
687 /***********************************************************************
690 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
693 SetLastError(ERROR_BAD_FORMAT
);
697 /***********************************************************************
700 void WINAPI
MZ_AllocDPMITask( void )
702 ERR("Actual real-mode calls not supported on this platform!\n");
705 /***********************************************************************
708 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
713 /***********************************************************************
716 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
718 ExitThread( retval
);
721 /***********************************************************************
724 BOOL WINAPI
MZ_Current( void )
729 #endif /* !MZ_SUPPORTED */