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"
49 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(module
);
56 static BOOL DOSVM_isdosexe
;
58 /**********************************************************************
61 * Return TRUE if we are in Windows process.
63 BOOL
DOSVM_IsWin16(void)
65 return DOSVM_isdosexe
? FALSE
: TRUE
;
70 #ifdef HAVE_SYS_MMAN_H
71 # include <sys/mman.h>
74 /* define this to try mapping through /proc/pid/mem instead of a temp file,
75 but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
78 #define BIOS_DATA_SEGMENT 0x40
81 #define SEG16(ptr,seg) ((LPVOID)((BYTE*)ptr+((DWORD)(seg)<<4)))
82 #define SEGPTR16(ptr,segptr) ((LPVOID)((BYTE*)ptr+((DWORD)SELECTOROF(segptr)<<4)+OFFSETOF(segptr)))
84 /* structures for EXEC */
88 DWORD cmdline WINE_PACKED
;
89 DWORD fcb1 WINE_PACKED
;
90 DWORD fcb2 WINE_PACKED
;
102 /* global variables */
106 static WORD init_cs
,init_ip
,init_ss
,init_sp
;
107 static HANDLE dosvm_thread
, loop_thread
;
108 static DWORD dosvm_tid
, loop_tid
;
110 static void MZ_Launch( LPCSTR cmdtail
, int length
);
111 static BOOL
MZ_InitTask(void);
113 static void MZ_CreatePSP( LPVOID lpPSP
, WORD env
, WORD par
)
117 psp
->int20
=0x20CD; /* int 20 */
118 /* some programs use this to calculate how much memory they need */
119 psp
->nextParagraph
=0x9FFF; /* FIXME: use a real value */
120 /* FIXME: dispatcher */
121 psp
->savedint22
= DOSVM_GetRMHandler(0x22);
122 psp
->savedint23
= DOSVM_GetRMHandler(0x23);
123 psp
->savedint24
= DOSVM_GetRMHandler(0x24);
125 psp
->environment
=env
;
126 /* FIXME: more PSP stuff */
129 static void MZ_FillPSP( LPVOID lpPSP
, LPCSTR cmdtail
, int length
)
135 WARN( "Command tail truncated! (length %d)\n", length
);
139 psp
->cmdLine
[0] = length
;
142 * Length of exactly 127 bytes means that full command line is
143 * stored in environment variable CMDLINE and PSP contains
144 * command tail truncated to 126 bytes.
150 memmove(psp
->cmdLine
+1, cmdtail
, length
);
152 psp
->cmdLine
[length
+1] = '\r';
154 /* FIXME: more PSP stuff */
157 static WORD
MZ_InitEnvironment( LPCSTR env
, LPCSTR name
)
164 /* get size of environment block */
165 while (env
[sz
++]) sz
+=strlen(env
+sz
)+1;
168 envblk
=DOSMEM_GetBlock(sz
+sizeof(WORD
)+strlen(name
)+1,&seg
);
171 memcpy(envblk
,env
,sz
);
173 /* DOS 3.x: the block contains 1 additional string */
174 *(WORD
*)(envblk
+sz
)=1;
175 /* being the program name itself */
176 strcpy(envblk
+sz
+sizeof(WORD
),name
);
180 static BOOL
MZ_InitMemory(void)
182 /* initialize the memory */
183 TRACE("Initializing DOS memory structures\n");
185 DOSDEV_InstallDOSDevices();
190 static BOOL
MZ_DoLoadImage( HANDLE hFile
, LPCSTR filename
, OverlayBlock
*oblk
)
192 IMAGE_DOS_HEADER mz_header
;
193 DWORD image_start
,image_size
,min_size
,max_size
,avail
;
194 BYTE
*psp_start
,*load_start
,*oldenv
;
195 int x
, old_com
=0, alloc
;
197 WORD env_seg
, load_seg
, rel_seg
, oldpsp_seg
;
201 /* DOS process already running, inherit from it */
202 PDB16
* par_psp
= (PDB16
*)((DWORD
)DOSVM_psp
<< 4);
204 oldenv
= (LPBYTE
)((DWORD
)par_psp
->environment
<< 4);
205 oldpsp_seg
= DOSVM_psp
;
207 /* allocate new DOS process, inheriting from Wine environment */
209 oldenv
= GetEnvironmentStringsA();
213 SetFilePointer(hFile
,0,NULL
,FILE_BEGIN
);
214 if ( !ReadFile(hFile
,&mz_header
,sizeof(mz_header
),&len
,NULL
)
215 || len
!= sizeof(mz_header
)
216 || mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
217 char *p
= strrchr( filename
, '.' );
218 if (!p
|| strcasecmp( p
, ".com" )) /* check for .COM extension */
220 SetLastError(ERROR_BAD_FORMAT
);
223 old_com
=1; /* assume .COM file */
225 image_size
=GetFileSize(hFile
,NULL
);
226 min_size
=0x10000; max_size
=0x100000;
228 mz_header
.e_ss
=0; mz_header
.e_sp
=0xFFFE;
229 mz_header
.e_cs
=0; mz_header
.e_ip
=0x100;
231 /* calculate load size */
232 image_start
=mz_header
.e_cparhdr
<<4;
233 image_size
=mz_header
.e_cp
<<9; /* pages are 512 bytes */
234 if ((mz_header
.e_cblp
!=0)&&(mz_header
.e_cblp
!=4)) image_size
-=512-mz_header
.e_cblp
;
235 image_size
-=image_start
;
236 min_size
=image_size
+((DWORD
)mz_header
.e_minalloc
<<4)+(PSP_SIZE
<<4);
237 max_size
=image_size
+((DWORD
)mz_header
.e_maxalloc
<<4)+(PSP_SIZE
<<4);
240 if (alloc
) MZ_InitMemory();
243 /* load overlay into preallocated memory */
244 load_seg
=oblk
->load_seg
;
245 rel_seg
=oblk
->rel_seg
;
246 load_start
=(LPBYTE
)((DWORD
)load_seg
<<4);
248 /* allocate environment block */
249 env_seg
=MZ_InitEnvironment(oldenv
, filename
);
251 /* allocate memory for the executable */
252 TRACE("Allocating DOS memory (min=%ld, max=%ld)\n",min_size
,max_size
);
253 avail
=DOSMEM_Available();
254 if (avail
<min_size
) {
255 ERR("insufficient DOS memory\n");
256 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
259 if (avail
>max_size
) avail
=max_size
;
260 psp_start
=DOSMEM_GetBlock(avail
,&DOSVM_psp
);
262 ERR("error allocating DOS memory\n");
263 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
266 load_seg
=DOSVM_psp
+(old_com
?0:PSP_SIZE
);
268 load_start
=psp_start
+(PSP_SIZE
<<4);
269 MZ_CreatePSP(psp_start
, env_seg
, oldpsp_seg
);
272 /* load executable image */
273 TRACE("loading DOS %s image, %08lx bytes\n",old_com
?"COM":"EXE",image_size
);
274 SetFilePointer(hFile
,image_start
,NULL
,FILE_BEGIN
);
275 if (!ReadFile(hFile
,load_start
,image_size
,&len
,NULL
) || len
!= image_size
) {
276 SetLastError(ERROR_BAD_FORMAT
);
280 if (mz_header
.e_crlc
) {
281 /* load relocation table */
282 TRACE("loading DOS EXE relocation table, %d entries\n",mz_header
.e_crlc
);
283 /* FIXME: is this too slow without read buffering? */
284 SetFilePointer(hFile
,mz_header
.e_lfarlc
,NULL
,FILE_BEGIN
);
285 for (x
=0; x
<mz_header
.e_crlc
; x
++) {
286 if (!ReadFile(hFile
,&reloc
,sizeof(reloc
),&len
,NULL
) || len
!= sizeof(reloc
)) {
287 SetLastError(ERROR_BAD_FORMAT
);
290 *(WORD
*)SEGPTR16(load_start
,reloc
)+=rel_seg
;
295 init_cs
= load_seg
+mz_header
.e_cs
;
296 init_ip
= mz_header
.e_ip
;
297 init_ss
= load_seg
+mz_header
.e_ss
;
298 init_sp
= mz_header
.e_sp
;
300 TRACE("entry point: %04x:%04x\n",init_cs
,init_ip
);
303 if (alloc
&& !MZ_InitTask()) {
304 SetLastError(ERROR_GEN_FAILURE
);
311 DOSVM_psp
= oldpsp_seg
;
316 /***********************************************************************
317 * wine_load_dos_exe (WINEDOS.@)
319 * Called from WineVDM when a new real-mode DOS process is started.
320 * Loads DOS program into memory and executes the program.
322 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
324 char dos_cmdtail
[126];
327 HANDLE hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
328 NULL
, OPEN_EXISTING
, 0, 0 );
329 if (hFile
== INVALID_HANDLE_VALUE
) return;
330 DOSVM_isdosexe
= TRUE
;
332 if(cmdline
&& *cmdline
)
334 dos_length
= strlen(cmdline
);
335 memmove( dos_cmdtail
+ 1, cmdline
,
336 (dos_length
< 125) ? dos_length
: 125 );
338 /* Non-empty command tail always starts with at least one space. */
339 dos_cmdtail
[0] = ' ';
343 * If command tail is longer than 126 characters,
344 * set tail length to 127 and fill CMDLINE environment variable
345 * with full command line (this includes filename).
347 if (dos_length
> 126)
349 char *cmd
= HeapAlloc( GetProcessHeap(), 0,
350 dos_length
+ strlen(filename
) + 4 );
357 * Append filename. If path includes spaces, quote the path.
359 if (strchr(filename
, ' '))
362 strcpy( ptr
, filename
);
363 ptr
+= strlen(filename
);
368 strcpy( ptr
, filename
);
369 ptr
+= strlen(filename
);
373 * Append command tail.
375 if (cmdline
[0] != ' ')
377 strcpy( ptr
, cmdline
);
380 * Set environment variable. This will be passed to
383 if (!SetEnvironmentVariableA( "CMDLINE", cmd
))
385 HeapFree(GetProcessHeap(), 0, cmd
);
389 HeapFree(GetProcessHeap(), 0, cmd
);
394 if (MZ_DoLoadImage( hFile
, filename
, NULL
))
395 MZ_Launch( dos_cmdtail
, dos_length
);
398 /***********************************************************************
401 * this may only be called from existing DOS processes
403 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
407 PROCESS_INFORMATION pe
;
412 if(!GetBinaryTypeA(filename
, &binType
)) /* determine what kind of binary this is */
414 return FALSE
; /* binary is not an executable */
417 /* handle non-dos executables */
418 if(binType
!= SCS_DOS_BINARY
)
420 if(func
== 0) /* load and execute */
424 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
425 PDB16
*psp
= (PDB16
*)psp_start
;
426 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
427 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
428 LPBYTE envblock
= PTR_REAL_TO_LIN(psp
->environment
, 0);
429 int cmdLength
= cmdline
[0];
432 * If cmdLength is 127, command tail is truncated and environment
433 * variable CMDLINE should contain full command line
434 * (this includes filename).
436 if (cmdLength
== 127)
438 FIXME( "CMDLINE argument passing is unimplemented.\n" );
439 cmdLength
= 126; /* FIXME */
442 fullCmdLength
= (strlen(filename
) + 1) + cmdLength
+ 1; /* filename + space + cmdline + terminating null character */
444 fullCmdLine
= HeapAlloc(GetProcessHeap(), 0, fullCmdLength
);
445 if(!fullCmdLine
) return FALSE
; /* return false on memory alloc failure */
447 /* build the full command line from the executable file and the command line being passed in */
448 snprintf(fullCmdLine
, fullCmdLength
, "%s ", filename
); /* start off with the executable filename and a space */
449 memcpy(fullCmdLine
+ strlen(fullCmdLine
), cmdline
+ 1, cmdLength
); /* append cmdline onto the end */
450 fullCmdLine
[fullCmdLength
- 1] = 0; /* null terminate string */
452 ZeroMemory (&st
, sizeof(STARTUPINFOA
));
453 st
.cb
= sizeof(STARTUPINFOA
);
454 ret
= CreateProcessA (NULL
, fullCmdLine
, NULL
, NULL
, TRUE
, 0, envblock
, NULL
, &st
, &pe
);
456 /* wait for the app to finish and clean up PROCESS_INFORMATION handles */
459 WaitForSingleObject(pe
.hProcess
, INFINITE
); /* wait here until the child process is complete */
460 CloseHandle(pe
.hProcess
);
461 CloseHandle(pe
.hThread
);
464 HeapFree(GetProcessHeap(), 0, fullCmdLine
); /* free the memory we allocated */
468 FIXME("EXEC type of %d not implemented for non-dos executables\n", func
);
473 } /* if(binType != SCS_DOS_BINARY) */
476 /* handle dos executables */
478 hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
479 NULL
, OPEN_EXISTING
, 0, 0);
480 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
483 case 0: /* load and execute */
484 case 1: /* load but don't execute */
486 /* save current process's return SS:SP now */
487 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
488 PDB16
*psp
= (PDB16
*)psp_start
;
489 psp
->saveStack
= (DWORD
)MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
));
491 ret
= MZ_DoLoadImage( hFile
, filename
, NULL
);
493 /* MZ_LoadImage created a new PSP and loaded new values into it,
494 * let's work on the new values now */
495 LPBYTE psp_start
= (LPBYTE
)((DWORD
)DOSVM_psp
<< 4);
496 ExecBlock
*blk
= (ExecBlock
*)paramblk
;
497 LPBYTE cmdline
= PTR_REAL_TO_LIN(SELECTOROF(blk
->cmdline
),OFFSETOF(blk
->cmdline
));
499 /* First character contains the length of the command line. */
500 MZ_FillPSP(psp_start
, cmdline
+ 1, cmdline
[0]);
502 /* the lame MS-DOS engineers decided that the return address should be in int22 */
503 DOSVM_SetRMHandler(0x22, (FARPROC16
)MAKESEGPTR(context
->SegCs
, LOWORD(context
->Eip
)));
505 /* don't execute, just return startup state */
506 blk
->init_cs
= init_cs
;
507 blk
->init_ip
= init_ip
;
508 blk
->init_ss
= init_ss
;
509 blk
->init_sp
= init_sp
;
511 /* execute by making us return to new process */
512 context
->SegCs
= init_cs
;
513 context
->Eip
= init_ip
;
514 context
->SegSs
= init_ss
;
515 context
->Esp
= init_sp
;
516 context
->SegDs
= DOSVM_psp
;
517 context
->SegEs
= DOSVM_psp
;
522 case 3: /* load overlay */
524 OverlayBlock
*blk
= (OverlayBlock
*)paramblk
;
525 ret
= MZ_DoLoadImage( hFile
, filename
, blk
);
529 FIXME("EXEC load type %d not implemented\n", func
);
530 SetLastError(ERROR_INVALID_FUNCTION
);
537 /***********************************************************************
540 void WINAPI
MZ_AllocDPMITask( void )
546 /***********************************************************************
549 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
557 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
558 PostThreadMessageA(loop_tid
, WM_USER
, (WPARAM
)event
, (LPARAM
)&spc
);
559 WaitForSingleObject(event
, INFINITE
);
565 static DWORD WINAPI
MZ_DOSVM( LPVOID lpExtra
)
570 dosvm_pid
= getpid();
572 memset( &context
, 0, sizeof(context
) );
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
;
579 context
.EFlags
= V86_FLAG
| VIF_MASK
;
580 DOSVM_SetTimer(0x10000);
581 ret
= DOSVM_Enter( &context
);
587 static BOOL
MZ_InitTask(void)
589 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
590 GetCurrentProcess(), &loop_thread
,
591 0, FALSE
, DUPLICATE_SAME_ACCESS
))
593 dosvm_thread
= CreateThread(NULL
, 0, MZ_DOSVM
, NULL
, CREATE_SUSPENDED
, &dosvm_tid
);
595 CloseHandle(loop_thread
);
599 loop_tid
= GetCurrentThreadId();
603 static void MZ_Launch( LPCSTR cmdtail
, int length
)
605 TDB
*pTask
= GlobalLock16( GetCurrentTask() );
606 BYTE
*psp_start
= PTR_REAL_TO_LIN( DOSVM_psp
, 0 );
610 MZ_FillPSP(psp_start
, cmdtail
, length
);
611 pTask
->flags
|= TDBF_WINOLDAP
;
613 /* DTA is set to PSP:0080h when a program is started. */
614 pTask
->dta
= MAKESEGPTR( DOSVM_psp
, 0x80 );
616 GetpWin16Lock( &lock
);
617 _LeaveSysLevel( lock
);
619 ResumeThread(dosvm_thread
);
620 rv
= DOSVM_Loop(dosvm_thread
);
622 CloseHandle(dosvm_thread
);
623 dosvm_thread
= 0; dosvm_tid
= 0;
624 CloseHandle(loop_thread
);
625 loop_thread
= 0; loop_tid
= 0;
631 /***********************************************************************
634 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
637 WORD psp_seg
= cs_psp
? context
->SegCs
: DOSVM_psp
;
638 LPBYTE psp_start
= (LPBYTE
)((DWORD
)psp_seg
<< 4);
639 PDB16
*psp
= (PDB16
*)psp_start
;
640 WORD parpsp
= psp
->parentPSP
; /* check for parent DOS process */
642 /* retrieve parent's return address */
643 FARPROC16 retaddr
= DOSVM_GetRMHandler(0x22);
644 /* restore interrupts */
645 DOSVM_SetRMHandler(0x22, psp
->savedint22
);
646 DOSVM_SetRMHandler(0x23, psp
->savedint23
);
647 DOSVM_SetRMHandler(0x24, psp
->savedint24
);
648 /* FIXME: deallocate file handles etc */
649 /* free process's associated memory
650 * FIXME: walk memory and deallocate all blocks owned by process */
651 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(psp
->environment
,0) );
652 DOSMEM_FreeBlock( PTR_REAL_TO_LIN(DOSVM_psp
,0) );
653 /* switch to parent's PSP */
655 psp_start
= (LPBYTE
)((DWORD
)parpsp
<< 4);
656 psp
= (PDB16
*)psp_start
;
657 /* now return to parent */
658 DOSVM_retval
= retval
;
659 context
->SegCs
= SELECTOROF(retaddr
);
660 context
->Eip
= OFFSETOF(retaddr
);
661 context
->SegSs
= SELECTOROF(psp
->saveStack
);
662 context
->Esp
= OFFSETOF(psp
->saveStack
);
665 TRACE("killing DOS task\n");
667 ExitThread( retval
);
671 /***********************************************************************
674 BOOL WINAPI
MZ_Current( void )
676 return (dosvm_pid
!= 0); /* FIXME: do a better check */
679 #else /* !MZ_SUPPORTED */
681 /***********************************************************************
682 * wine_load_dos_exe (WINEDOS.@)
684 void WINAPI
wine_load_dos_exe( LPCSTR filename
, LPCSTR cmdline
)
686 WARN("DOS executables not supported on this platform\n");
687 SetLastError(ERROR_BAD_FORMAT
);
690 /***********************************************************************
693 BOOL WINAPI
MZ_Exec( CONTEXT86
*context
, LPCSTR filename
, BYTE func
, LPVOID paramblk
)
696 SetLastError(ERROR_BAD_FORMAT
);
700 /***********************************************************************
703 void WINAPI
MZ_AllocDPMITask( void )
705 ERR("Actual real-mode calls not supported on this platform!\n");
708 /***********************************************************************
711 void WINAPI
MZ_RunInThread( PAPCFUNC proc
, ULONG_PTR arg
)
716 /***********************************************************************
719 void WINAPI
MZ_Exit( CONTEXT86
*context
, BOOL cs_psp
, WORD retval
)
721 ExitThread( retval
);
724 /***********************************************************************
727 BOOL WINAPI
MZ_Current( void )
732 #endif /* !MZ_SUPPORTED */