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>
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
;
69 #ifdef HAVE_SYS_MMAN_H
70 # include <sys/mman.h>
73 /* define this to try mapping through /proc/pid/mem instead of a temp file,
74 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
77 #define BIOS_DATA_SEGMENT 0x40
80 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
81 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
83 /* structures for EXEC */
105 /* global variables */
109 static WORD init_cs
,init_ip
,init_ss
,init_sp
;
110 static HANDLE dosvm_thread
, loop_thread
;
111 static DWORD dosvm_tid
, loop_tid
;
113 static void MZ_Launch( LPCSTR cmdtail
, int length
);
114 static BOOL
MZ_InitTask(void);
116 static void MZ_CreatePSP( LPVOID lpPSP
, WORD env
, WORD par
)
120 psp
->int20
=0x20CD; /* int 20 */
121 /* some programs use this to calculate how much memory they need */
122 psp
->nextParagraph
=0x9FFF; /* FIXME: use a real value */
123 /* FIXME: dispatcher */
124 psp
->savedint22
= DOSVM_GetRMHandler(0x22);
125 psp
->savedint23
= DOSVM_GetRMHandler(0x23);
126 psp
->savedint24
= DOSVM_GetRMHandler(0x24);
128 psp
->environment
=env
;
129 /* FIXME: more PSP stuff */
132 static void MZ_FillPSP( LPVOID lpPSP
, LPCSTR cmdtail
, int length
)
138 WARN( "Command tail truncated! (length %d)\n", length
);
142 psp
->cmdLine
[0] = length
;
145 * Length of exactly 127 bytes means that full command line is
146 * stored in environment variable CMDLINE and PSP contains
147 * command tail truncated to 126 bytes.
153 memmove(psp
->cmdLine
+1, cmdtail
, length
);
155 psp
->cmdLine
[length
+1] = '\r';
157 /* FIXME: more PSP stuff */
160 static WORD
MZ_InitEnvironment( LPCSTR env
, LPCSTR name
)
168 /* get size of environment block */
169 while (env
[sz
++]) sz
+=strlen(env
+sz
)+1;
172 envblk
=DOSMEM_AllocBlock(sz
+sizeof(WORD
)+strlen(name
)+1,&seg
);
175 memcpy(envblk
,env
,sz
);
177 /* DOS environment variables are uppercase */
179 while (envblk
[i
] != '='){
180 if (envblk
[i
]>='a' && envblk
[i
] <= 'z'){
185 i
+= strlen(envblk
+i
) + 1;
187 /* DOS 3.x: the block contains 1 additional string */
188 *(WORD
*)(envblk
+sz
)=1;
189 /* being the program name itself */
190 strcpy(envblk
+sz
+sizeof(WORD
),name
);
194 static BOOL
MZ_InitMemory(void)
196 /* initialize the memory */
197 TRACE("Initializing DOS memory structures\n");
198 DOSMEM_MapDosLayout();
199 DOSDEV_InstallDOSDevices();
204 static BOOL
MZ_DoLoadImage( HANDLE hFile
, LPCSTR filename
, OverlayBlock
*oblk
, WORD par_env_seg
)
206 IMAGE_DOS_HEADER mz_header
;
207 DWORD image_start
,image_size
,min_size
,max_size
,avail
;
208 BYTE
*psp_start
,*load_start
,*oldenv
= 0;
209 int x
, old_com
=0, alloc
;
211 WORD env_seg
, load_seg
, rel_seg
, oldpsp_seg
;
215 /* DOS process already running, inherit from it */
218 oldpsp_seg
= DOSVM_psp
;
220 par_psp
= (PDB16
*)((DWORD
)DOSVM_psp
<< 4);
221 oldenv
= (LPBYTE
)((DWORD
)par_psp
->environment
<< 4);
224 /* allocate new DOS process, inheriting from Wine environment */
228 oldenv
= GetEnvironmentStringsA();
231 SetFilePointer(hFile
,0,NULL
,FILE_BEGIN
);
232 if ( !ReadFile(hFile
,&mz_header
,sizeof(mz_header
),&len
,NULL
)
233 || len
!= sizeof(mz_header
)
234 || mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
235 char *p
= strrchr( filename
, '.' );
236 if (!p
|| strcasecmp( p
, ".com" )) /* check for .COM extension */
238 SetLastError(ERROR_BAD_FORMAT
);
241 old_com
=1; /* assume .COM file */
243 image_size
=GetFileSize(hFile
,NULL
);
244 min_size
=0x10000; max_size
=0x100000;
246 mz_header
.e_ss
=0; mz_header
.e_sp
=0xFFFE;
247 mz_header
.e_cs
=0; mz_header
.e_ip
=0x100;
249 /* calculate load size */
250 image_start
=mz_header
.e_cparhdr
<<4;
251 image_size
=mz_header
.e_cp
<<9; /* pages are 512 bytes */
252 /* From Ralf Brown Interrupt List: If the word at offset 02h is 4, it should
253 * be treated as 00h, since pre-1.10 versions of the MS linker set it that
255 if ((mz_header
.e_cblp
!=0)&&(mz_header
.e_cblp
!=4)) image_size
-=512-mz_header
.e_cblp
;
256 image_size
-=image_start
;
257 min_size
=image_size
+((DWORD
)mz_header
.e_minalloc
<<4)+(PSP_SIZE
<<4);
258 max_size
=image_size
+((DWORD
)mz_header
.e_maxalloc
<<4)+(PSP_SIZE
<<4);
261 if (alloc
) MZ_InitMemory();
264 /* load overlay into preallocated memory */
265 load_seg
=oblk
->load_seg
;
266 rel_seg
=oblk
->rel_seg
;
267 load_start
=(LPBYTE
)((DWORD
)load_seg
<<4);
269 /* allocate environment block */
271 env_seg
= par_env_seg
;
273 env_seg
=MZ_InitEnvironment(oldenv
, filename
);
275 FreeEnvironmentStringsA( oldenv
);
277 /* allocate memory for the executable */
278 TRACE("Allocating DOS memory (min=%ld, max=%ld)\n",min_size
,max_size
);
279 avail
=DOSMEM_Available();
280 if (avail
<min_size
) {
281 ERR("insufficient DOS memory\n");
282 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
285 if (avail
>max_size
) avail
=max_size
;
286 psp_start
=DOSMEM_AllocBlock(avail
,&DOSVM_psp
);
288 ERR("error allocating DOS memory\n");
289 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
292 load_seg
=DOSVM_psp
+(old_com
?0:PSP_SIZE
);
294 load_start
=psp_start
+(PSP_SIZE
<<4);
295 MZ_CreatePSP(psp_start
, env_seg
, oldpsp_seg
);
298 /* load executable image */
299 TRACE("loading DOS %s image, %08lx bytes\n",old_com
?"COM":"EXE",image_size
);
300 SetFilePointer(hFile
,image_start
,NULL
,FILE_BEGIN
);
301 if (!ReadFile(hFile
,load_start
,image_size
,&len
,NULL
) || len
!= image_size
) {
302 /* check if this is due to the workaround for the pre-1.10 MS linker and we
303 realy had only 4 bytes on the last page */
304 if (mz_header
.e_cblp
!= 4 || image_size
- len
!= 512 - 4) {
305 SetLastError(ERROR_BAD_FORMAT
);
310 if (mz_header
.e_crlc
) {
311 /* load relocation table */
312 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header
.e_crlc
);
313 /* FIXME: is this too slow without read buffering? */
314 SetFilePointer(hFile
,mz_header
.e_lfarlc
,NULL
,FILE_BEGIN
);
315 for (x
=0; x
<mz_header
.e_crlc
; x
++) {
316 if (!ReadFile(hFile
,&reloc
,sizeof(reloc
),&len
,NULL
) || len
!= sizeof(reloc
)) {
317 SetLastError(ERROR_BAD_FORMAT
);
320 *(WORD
*)SEGPTR16(load_start
,reloc
)+=rel_seg
;
325 init_cs
= load_seg
+mz_header
.e_cs
;
326 init_ip
= mz_header
.e_ip
;
327 init_ss
= load_seg
+mz_header
.e_ss
;
328 init_sp
= mz_header
.e_sp
;
330 /* .COM files exit with ret. Make sure they jump to psp start (=int 20) */
331 WORD
* stack
= PTR_REAL_TO_LIN(init_ss
, init_sp
);
335 TRACE("entry point: %04x:%04x\n",init_cs
,init_ip
);
338 if (alloc
&& !MZ_InitTask()) {
339 SetLastError(ERROR_GEN_FAILURE
);
346 DOSVM_psp
= oldpsp_seg
;
351 /***********************************************************************
352 * wine_load_dos_exe (WINEDOS.@)
354 * Called from WineVDM when a new real-mode DOS process is started.
355 * Loads DOS program into memory and executes the program.
357 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
359 char dos_cmdtail
[126];
362 HANDLE hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
363 NULL
, OPEN_EXISTING
, 0, 0 );
364 if (hFile
== INVALID_HANDLE_VALUE
) return;
365 DOSVM_isdosexe
= TRUE
;
367 if(cmdline
&& *cmdline
)
369 dos_length
= strlen(cmdline
);
370 memmove( dos_cmdtail
+ 1, cmdline
,
371 (dos_length
< 125) ? dos_length
: 125 );
373 /* Non-empty command tail always starts with at least one space. */
374 dos_cmdtail
[0] = ' ';
378 * If command tail is longer than 126 characters,
379 * set tail length to 127 and fill CMDLINE environment variable
380 * with full command line (this includes filename).
382 if (dos_length
> 126)
384 char *cmd
= HeapAlloc( GetProcessHeap(), 0,
385 dos_length
+ strlen(filename
) + 4 );
392 * Append filename. If path includes spaces, quote the path.
394 if (strchr(filename
, ' '))
397 strcpy( ptr
, filename
);
398 ptr
+= strlen(filename
);
403 strcpy( ptr
, filename
);
404 ptr
+= strlen(filename
);
408 * Append command tail.
410 if (cmdline
[0] != ' ')
412 strcpy( ptr
, cmdline
);
415 * Set environment variable. This will be passed to
418 if (!SetEnvironmentVariableA( "CMDLINE", cmd
))
420 HeapFree(GetProcessHeap(), 0, cmd
);
424 HeapFree(GetProcessHeap(), 0, cmd
);
429 if (MZ_DoLoadImage( hFile
, filename
, NULL
, 0 ))
430 MZ_Launch( dos_cmdtail
, dos_length
);
433 /***********************************************************************
436 * this may only be called from existing DOS processes
438 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
442 PROCESS_INFORMATION pe
;
447 if(!GetBinaryTypeA(filename
, &binType
)) /* determine what kind of binary this is */
449 return FALSE
; /* binary is not an executable */
452 /* handle non-dos executables */
453 if(binType
!= SCS_DOS_BINARY
)
455 if(func
== 0) /* load and execute */
459 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
460 PDB16
*psp
= (PDB16
*)psp_start
;
461 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
462 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
463 LPBYTE envblock
= PTR_REAL_TO_LIN(psp
->environment
, 0);
464 int cmdLength
= cmdline
[0];
467 * If cmdLength is 127, command tail is truncated and environment
468 * variable CMDLINE should contain full command line
469 * (this includes filename).
471 if (cmdLength
== 127)
473 FIXME( "CMDLINE argument passing is unimplemented.\n" );
474 cmdLength
= 126; /* FIXME */
477 fullCmdLength
= (strlen(filename
) + 1) + cmdLength
+ 1; /* filename + space + cmdline + terminating null character */
479 fullCmdLine
= HeapAlloc(GetProcessHeap(), 0, fullCmdLength
);
480 if(!fullCmdLine
) return FALSE
; /* return false on memory alloc failure */
482 /* build the full command line from the executable file and the command line being passed in */
483 snprintf(fullCmdLine
, fullCmdLength
, "%s ", filename
); /* start off with the executable filename and a space */
484 memcpy(fullCmdLine
+ strlen(fullCmdLine
), cmdline
+ 1, cmdLength
); /* append cmdline onto the end */
485 fullCmdLine
[fullCmdLength
- 1] = 0; /* null terminate string */
487 ZeroMemory (&st
, sizeof(STARTUPINFOA
));
488 st
.cb
= sizeof(STARTUPINFOA
);
489 ret
= CreateProcessA (NULL
, fullCmdLine
, NULL
, NULL
, TRUE
, 0, envblock
, NULL
, &st
, &pe
);
491 /* wait for the app to finish and clean up PROCESS_INFORMATION handles */
494 WaitForSingleObject(pe
.hProcess
, INFINITE
); /* wait here until the child process is complete */
495 CloseHandle(pe
.hProcess
);
496 CloseHandle(pe
.hThread
);
499 HeapFree(GetProcessHeap(), 0, fullCmdLine
); /* free the memory we allocated */
503 FIXME("EXEC type of %d not implemented for non-dos executables\n", func
);
508 } /* if(binType != SCS_DOS_BINARY) */
511 /* handle dos executables */
513 hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
514 NULL
, OPEN_EXISTING
, 0, 0);
515 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
518 case 0: /* load and execute */
519 case 1: /* load but don't execute */
521 /* save current process's return SS:SP now */
522 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
523 PDB16
*psp
= (PDB16
*)psp_start
;
524 psp
->saveStack
= (DWORD
)MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
));
526 ret
= MZ_DoLoadImage( hFile
, filename
, NULL
, ((ExecBlock
*)paramblk
)->env_seg
);
528 /* MZ_LoadImage created a new PSP and loaded new values into it,
529 * let's work on the new values now */
530 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
531 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
532 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
534 /* First character contains the length of the command line. */
535 MZ_FillPSP(psp_start
, cmdline
+ 1, cmdline
[0]);
537 /* the lame MS-DOS engineers decided that the return address should be in int22 */
538 DOSVM_SetRMHandler(0x22, (FARPROC16
)MAKESEGPTR(context
->SegCs
, LOWORD(context
->Eip
)));
540 /* don't execute, just return startup state */
543 * For function 01h, the AX value to be passed to the child program
544 * is put on top of the child's stack
548 stack
= (LPBYTE
) CTX_SEG_OFF_TO_LIN(context
, init_ss
, init_sp
);
549 /* FIXME: push AX correctly */
550 stack
[0] = 0x00; /* push AL */
551 stack
[1] = 0x00; /* push AH */
553 blk
->init_cs
= init_cs
;
554 blk
->init_ip
= init_ip
;
555 blk
->init_ss
= init_ss
;
556 blk
->init_sp
= init_sp
;
558 /* execute by making us return to new process */
559 context
->SegCs
= init_cs
;
560 context
->Eip
= init_ip
;
561 context
->SegSs
= init_ss
;
562 context
->Esp
= init_sp
;
563 context
->SegDs
= DOSVM_psp
;
564 context
->SegEs
= DOSVM_psp
;
569 case 3: /* load overlay */
571 OverlayBlock
*blk
= (OverlayBlock
*)paramblk
;
572 ret
= MZ_DoLoadImage( hFile
, filename
, blk
, 0);
576 FIXME("EXEC load type %d not implemented\n", func
);
577 SetLastError(ERROR_INVALID_FUNCTION
);
584 /***********************************************************************
587 void WINAPI
MZ_AllocDPMITask( void )
593 /***********************************************************************
596 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
604 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
605 PostThreadMessageA(loop_tid
, WM_USER
, (WPARAM
)event
, (LPARAM
)&spc
);
606 WaitForSingleObject(event
, INFINITE
);
612 static DWORD WINAPI
MZ_DOSVM( LPVOID lpExtra
)
617 dosvm_pid
= getpid();
619 memset( &context
, 0, sizeof(context
) );
620 context
.SegCs
= init_cs
;
621 context
.Eip
= init_ip
;
622 context
.SegSs
= init_ss
;
623 context
.Esp
= init_sp
;
624 context
.SegDs
= DOSVM_psp
;
625 context
.SegEs
= DOSVM_psp
;
626 context
.EFlags
= V86_FLAG
| VIF_MASK
;
627 DOSVM_SetTimer(0x10000);
628 ret
= DOSVM_Enter( &context
);
634 static BOOL
MZ_InitTask(void)
636 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
637 GetCurrentProcess(), &loop_thread
,
638 0, FALSE
, DUPLICATE_SAME_ACCESS
))
640 dosvm_thread
= CreateThread(NULL
, 0, MZ_DOSVM
, NULL
, CREATE_SUSPENDED
, &dosvm_tid
);
642 CloseHandle(loop_thread
);
646 loop_tid
= GetCurrentThreadId();
650 static void MZ_Launch( LPCSTR cmdtail
, int length
)
652 TDB
*pTask
= GlobalLock16( GetCurrentTask() );
653 BYTE
*psp_start
= PTR_REAL_TO_LIN( DOSVM_psp
, 0 );
657 MZ_FillPSP(psp_start
, cmdtail
, length
);
658 pTask
->flags
|= TDBF_WINOLDAP
;
660 /* DTA is set to PSP:0080h when a program is started. */
661 pTask
->dta
= MAKESEGPTR( DOSVM_psp
, 0x80 );
663 GetpWin16Lock( &lock
);
664 _LeaveSysLevel( lock
);
666 ResumeThread(dosvm_thread
);
667 rv
= DOSVM_Loop(dosvm_thread
);
669 CloseHandle(dosvm_thread
);
670 dosvm_thread
= 0; dosvm_tid
= 0;
671 CloseHandle(loop_thread
);
672 loop_thread
= 0; loop_tid
= 0;
678 /***********************************************************************
681 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
684 WORD psp_seg
= cs_psp
? context
->SegCs
: DOSVM_psp
;
685 LPBYTE psp_start
= (LPBYTE
)((DWORD
)psp_seg
<< 4);
686 PDB16
*psp
= (PDB16
*)psp_start
;
687 WORD parpsp
= psp
->parentPSP
; /* check for parent DOS process */
689 /* retrieve parent's return address */
690 FARPROC16 retaddr
= DOSVM_GetRMHandler(0x22);
691 /* restore interrupts */
692 DOSVM_SetRMHandler(0x22, psp
->savedint22
);
693 DOSVM_SetRMHandler(0x23, psp
->savedint23
);
694 DOSVM_SetRMHandler(0x24, psp
->savedint24
);
695 /* FIXME: deallocate file handles etc */
696 /* free process's associated memory
697 * FIXME: walk memory and deallocate all blocks owned by process */
698 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(psp
->environment
,0) );
699 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(DOSVM_psp
,0) );
700 /* switch to parent's PSP */
702 psp_start
= (LPBYTE
)((DWORD
)parpsp
<< 4);
703 psp
= (PDB16
*)psp_start
;
704 /* now return to parent */
705 DOSVM_retval
= retval
;
706 context
->SegCs
= SELECTOROF(retaddr
);
707 context
->Eip
= OFFSETOF(retaddr
);
708 context
->SegSs
= SELECTOROF(psp
->saveStack
);
709 context
->Esp
= OFFSETOF(psp
->saveStack
);
712 TRACE("killing DOS task\n");
714 ExitThread( retval
);
718 /***********************************************************************
721 BOOL WINAPI
MZ_Current( void )
723 return (dosvm_pid
!= 0); /* FIXME: do a better check */
726 #else /* !MZ_SUPPORTED */
728 /***********************************************************************
729 * wine_load_dos_exe (WINEDOS.@)
731 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
733 WARN("DOS executables not supported on this platform\n");
734 SetLastError(ERROR_BAD_FORMAT
);
737 /***********************************************************************
740 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
743 SetLastError(ERROR_BAD_FORMAT
);
747 /***********************************************************************
750 void WINAPI
MZ_AllocDPMITask( void )
752 ERR("Actual real-mode calls not supported on this platform!\n");
755 /***********************************************************************
758 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
763 /***********************************************************************
766 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
768 ExitThread( retval
);
771 /***********************************************************************
774 BOOL WINAPI
MZ_Current( void )
779 #endif /* !MZ_SUPPORTED */