Cast time_t to long for printing.
[wine] / dlls / winedos / module.c
1 /*
2  * DOS (MZ) loader
3  *
4  * Copyright 1998 Ove Kåven
5  *
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.
10  *
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.
15  *
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
19  *
20  * Note: This code hasn't been completely cleaned up yet.
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wine/winbase16.h"
43 #include "wingdi.h"
44 #include "winuser.h"
45 #include "winerror.h"
46 #include "task.h"
47 #include "file.h"
48 #include "miscemu.h"
49 #include "wine/debug.h"
50 #include "dosexe.h"
51 #include "dosvm.h"
52 #include "vga.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(module);
55
56 static BOOL DOSVM_isdosexe;
57
58 /**********************************************************************
59  *          DOSVM_IsWin16
60  * 
61  * Return TRUE if we are in Windows process.
62  */
63 BOOL DOSVM_IsWin16(void)
64 {
65   return DOSVM_isdosexe ? FALSE : TRUE;
66 }
67
68 #ifdef MZ_SUPPORTED
69
70 #ifdef HAVE_SYS_MMAN_H
71 # include <sys/mman.h>
72 #endif
73
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 */
76 #undef MZ_MAPSELF
77
78 #define BIOS_DATA_SEGMENT 0x40
79 #define PSP_SIZE 0x10
80
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)))
83
84 /* structures for EXEC */
85
86 typedef struct {
87   WORD env_seg;
88   DWORD cmdline WINE_PACKED;
89   DWORD fcb1 WINE_PACKED;
90   DWORD fcb2 WINE_PACKED;
91   WORD init_sp;
92   WORD init_ss;
93   WORD init_ip;
94   WORD init_cs;
95 } ExecBlock;
96
97 typedef struct {
98   WORD load_seg;
99   WORD rel_seg;
100 } OverlayBlock;
101
102 /* global variables */
103
104 pid_t dosvm_pid;
105
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;
109
110 static void MZ_Launch( LPCSTR cmdtail, int length );
111 static BOOL MZ_InitTask(void);
112
113 static void MZ_CreatePSP( LPVOID lpPSP, WORD env, WORD par )
114 {
115   PDB16*psp=lpPSP;
116
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);
124   psp->parentPSP=par;
125   psp->environment=env;
126   /* FIXME: more PSP stuff */
127 }
128
129 static void MZ_FillPSP( LPVOID lpPSP, LPCSTR cmdtail, int length )
130 {
131     PDB16 *psp = lpPSP;
132
133     if(length > 127) 
134     {
135         WARN( "Command tail truncated! (length %d)\n", length );
136         length = 126;
137     }
138
139     psp->cmdLine[0] = length;
140
141     /*
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.
145      */
146     if(length == 127)
147         length = 126;
148
149     if(length > 0)
150         memmove(psp->cmdLine+1, cmdtail, length);
151
152     psp->cmdLine[length+1] = '\r';
153
154     /* FIXME: more PSP stuff */
155 }
156
157 static WORD MZ_InitEnvironment( LPCSTR env, LPCSTR name )
158 {
159  unsigned sz=0;
160  WORD seg;
161  LPSTR envblk;
162
163  if (env) {
164   /* get size of environment block */
165   while (env[sz++]) sz+=strlen(env+sz)+1;
166  } else sz++;
167  /* allocate it */
168  envblk=DOSMEM_GetBlock(sz+sizeof(WORD)+strlen(name)+1,&seg);
169  /* fill it */
170  if (env) {
171   memcpy(envblk,env,sz);
172  } else envblk[0]=0;
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);
177  return seg;
178 }
179
180 static BOOL MZ_InitMemory(void)
181 {
182     /* initialize the memory */
183     TRACE("Initializing DOS memory structures\n");
184     DOSMEM_Init(TRUE);
185     DOSDEV_InstallDOSDevices();
186
187     return TRUE;
188 }
189
190 static BOOL MZ_DoLoadImage( HANDLE hFile, LPCSTR filename, OverlayBlock *oblk )
191 {
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;
196   SEGPTR reloc;
197   WORD env_seg, load_seg, rel_seg, oldpsp_seg;
198   DWORD len;
199
200   if (DOSVM_psp) {
201     /* DOS process already running, inherit from it */
202     PDB16* par_psp = (PDB16*)((DWORD)DOSVM_psp << 4);
203     alloc=0;
204     oldenv = (LPBYTE)((DWORD)par_psp->environment << 4);
205     oldpsp_seg = DOSVM_psp;
206   } else {
207     /* allocate new DOS process, inheriting from Wine environment */
208     alloc=1;
209     oldenv = GetEnvironmentStringsA();
210     oldpsp_seg = 0;
211   }
212
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 */
219   {
220       SetLastError(ERROR_BAD_FORMAT);
221       goto load_error;
222   }
223   old_com=1; /* assume .COM file */
224   image_start=0;
225   image_size=GetFileSize(hFile,NULL);
226   min_size=0x10000; max_size=0x100000;
227   mz_header.e_crlc=0;
228   mz_header.e_ss=0; mz_header.e_sp=0xFFFE;
229   mz_header.e_cs=0; mz_header.e_ip=0x100;
230  } else {
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);
238  }
239
240   if (alloc) MZ_InitMemory();
241
242   if (oblk) {
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);
247   } else {
248     /* allocate environment block */
249     env_seg=MZ_InitEnvironment(oldenv, filename);
250
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);
257       goto load_error;
258     }
259     if (avail>max_size) avail=max_size;
260     psp_start=DOSMEM_GetBlock(avail,&DOSVM_psp);
261     if (!psp_start) {
262       ERR("error allocating DOS memory\n");
263       SetLastError(ERROR_NOT_ENOUGH_MEMORY);
264       goto load_error;
265     }
266     load_seg=DOSVM_psp+(old_com?0:PSP_SIZE);
267     rel_seg=load_seg;
268     load_start=psp_start+(PSP_SIZE<<4);
269     MZ_CreatePSP(psp_start, env_seg, oldpsp_seg);
270   }
271
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);
277   goto load_error;
278  }
279
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);
288     goto load_error;
289    }
290    *(WORD*)SEGPTR16(load_start,reloc)+=rel_seg;
291   }
292  }
293
294   if (!oblk) {
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;
299
300     TRACE("entry point: %04x:%04x\n",init_cs,init_ip);
301   }
302
303   if (alloc && !MZ_InitTask()) {
304     SetLastError(ERROR_GEN_FAILURE);
305     return FALSE;
306   }
307
308   return TRUE;
309
310 load_error:
311   DOSVM_psp = oldpsp_seg;
312
313   return FALSE;
314 }
315
316 /***********************************************************************
317  *              wine_load_dos_exe (WINEDOS.@)
318  *
319  * Called from WineVDM when a new real-mode DOS process is started.
320  * Loads DOS program into memory and executes the program.
321  */
322 void WINAPI wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline )
323 {
324     char dos_cmdtail[126];
325     int  dos_length = 0;
326
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;
331
332     if(cmdline && *cmdline)
333     {
334         dos_length = strlen(cmdline);
335         memmove( dos_cmdtail + 1, cmdline, 
336                  (dos_length < 125) ? dos_length : 125 );
337
338         /* Non-empty command tail always starts with at least one space. */
339         dos_cmdtail[0] = ' ';
340         dos_length++;
341
342         /*
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).
346          */
347         if (dos_length > 126)
348         {
349             char *cmd = HeapAlloc( GetProcessHeap(), 0, 
350                                    dos_length + strlen(filename) + 4 );
351             char *ptr = cmd;
352
353             if (!cmd)
354                 return;
355
356             /*
357              * Append filename. If path includes spaces, quote the path.
358              */
359             if (strchr(filename, ' '))
360             {
361                 *ptr++ = '\"';
362                 strcpy( ptr, filename );
363                 ptr += strlen(filename);                   
364                 *ptr++ = '\"';
365             }
366             else
367             {
368                 strcpy( ptr, filename );
369                 ptr += strlen(filename);  
370             }
371
372             /*
373              * Append command tail.
374              */
375             if (cmdline[0] != ' ')
376                 *ptr++ = ' ';
377             strcpy( ptr, cmdline );
378
379             /*
380              * Set environment variable. This will be passed to
381              * new DOS process.
382              */
383             if (!SetEnvironmentVariableA( "CMDLINE", cmd ))
384             {
385                 HeapFree(GetProcessHeap(), 0, cmd );
386                 return;
387             }
388
389             HeapFree(GetProcessHeap(), 0, cmd );
390             dos_length = 127;
391         }
392     }
393
394     if (MZ_DoLoadImage( hFile, filename, NULL )) 
395         MZ_Launch( dos_cmdtail, dos_length );
396 }
397
398 /***********************************************************************
399  *              MZ_Exec
400  *
401  * this may only be called from existing DOS processes
402  */
403 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
404 {
405   DWORD binType;
406   STARTUPINFOA st;
407   PROCESS_INFORMATION pe;
408   HANDLE hFile;
409
410   BOOL ret = FALSE;
411
412   if(!GetBinaryTypeA(filename, &binType))   /* determine what kind of binary this is */
413   {
414     return FALSE; /* binary is not an executable */
415   }
416
417   /* handle non-dos executables */
418   if(binType != SCS_DOS_BINARY)
419   {
420     if(func == 0) /* load and execute */
421     {
422       LPBYTE fullCmdLine;
423       WORD fullCmdLength;
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];
430
431       /*
432        * If cmdLength is 127, command tail is truncated and environment 
433        * variable CMDLINE should contain full command line 
434        * (this includes filename).
435        */
436       if (cmdLength == 127)
437       {
438           FIXME( "CMDLINE argument passing is unimplemented.\n" );
439           cmdLength = 126; /* FIXME */
440       }
441
442       fullCmdLength = (strlen(filename) + 1) + cmdLength + 1; /* filename + space + cmdline + terminating null character */
443
444       fullCmdLine = HeapAlloc(GetProcessHeap(), 0, fullCmdLength);
445       if(!fullCmdLine) return FALSE; /* return false on memory alloc failure */
446
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 */
451
452       ZeroMemory (&st, sizeof(STARTUPINFOA));
453       st.cb = sizeof(STARTUPINFOA);
454       ret = CreateProcessA (NULL, fullCmdLine, NULL, NULL, TRUE, 0, envblock, NULL, &st, &pe);
455
456       /* wait for the app to finish and clean up PROCESS_INFORMATION handles */
457       if(ret)
458       {
459         WaitForSingleObject(pe.hProcess, INFINITE);  /* wait here until the child process is complete */
460         CloseHandle(pe.hProcess);
461         CloseHandle(pe.hThread);
462       }
463
464       HeapFree(GetProcessHeap(), 0, fullCmdLine);  /* free the memory we allocated */
465     }
466     else
467     {
468       FIXME("EXEC type of %d not implemented for non-dos executables\n", func);
469       ret = FALSE;
470     }
471
472     return ret;
473   } /* if(binType != SCS_DOS_BINARY) */
474
475
476   /* handle dos executables */
477
478   hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
479                              NULL, OPEN_EXISTING, 0, 0);
480   if (hFile == INVALID_HANDLE_VALUE) return FALSE;
481
482   switch (func) {
483   case 0: /* load and execute */
484   case 1: /* load but don't execute */
485     {
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));
490     }
491     ret = MZ_DoLoadImage( hFile, filename, NULL );
492     if (ret) {
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));
498
499       /* First character contains the length of the command line. */
500       MZ_FillPSP(psp_start, cmdline + 1, cmdline[0]);
501
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)));
504       if (func) {
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;
510       } else {
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;
518         context->Eax   = 0;
519       }
520     }
521     break;
522   case 3: /* load overlay */
523     {
524       OverlayBlock *blk = (OverlayBlock *)paramblk;
525       ret = MZ_DoLoadImage( hFile, filename, blk );
526     }
527     break;
528   default:
529     FIXME("EXEC load type %d not implemented\n", func);
530     SetLastError(ERROR_INVALID_FUNCTION);
531     break;
532   }
533   CloseHandle(hFile);
534   return ret;
535 }
536
537 /***********************************************************************
538  *              MZ_AllocDPMITask
539  */
540 void WINAPI MZ_AllocDPMITask( void )
541 {
542   MZ_InitMemory();
543   MZ_InitTask();
544 }
545
546 /***********************************************************************
547  *              MZ_RunInThread
548  */
549 void WINAPI MZ_RunInThread( PAPCFUNC proc, ULONG_PTR arg )
550 {
551   if (loop_thread) {
552     DOS_SPC spc;
553     HANDLE event;
554
555     spc.proc = proc;
556     spc.arg = arg;
557     event = CreateEventA(NULL, TRUE, FALSE, NULL);
558     PostThreadMessageA(loop_tid, WM_USER, (WPARAM)event, (LPARAM)&spc);
559     WaitForSingleObject(event, INFINITE);
560     CloseHandle(event);
561   } else
562     proc(arg);
563 }
564
565 static DWORD WINAPI MZ_DOSVM( LPVOID lpExtra )
566 {
567   CONTEXT context;
568   DWORD ret;
569
570   dosvm_pid = getpid();
571
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 );
582
583   dosvm_pid = 0;
584   return ret;
585 }
586
587 static BOOL MZ_InitTask(void)
588 {
589   if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
590                        GetCurrentProcess(), &loop_thread,
591                        0, FALSE, DUPLICATE_SAME_ACCESS))
592     return FALSE;
593   dosvm_thread = CreateThread(NULL, 0, MZ_DOSVM, NULL, CREATE_SUSPENDED, &dosvm_tid);
594   if (!dosvm_thread) {
595     CloseHandle(loop_thread);
596     loop_thread = 0;
597     return FALSE;
598   }
599   loop_tid = GetCurrentThreadId();
600   return TRUE;
601 }
602
603 static void MZ_Launch( LPCSTR cmdtail, int length )
604 {
605   TDB *pTask = GlobalLock16( GetCurrentTask() );
606   BYTE *psp_start = PTR_REAL_TO_LIN( DOSVM_psp, 0 );
607   DWORD rv;
608   SYSLEVEL *lock;
609
610   MZ_FillPSP(psp_start, cmdtail, length);
611   pTask->flags |= TDBF_WINOLDAP;
612
613   /* DTA is set to PSP:0080h when a program is started. */
614   pTask->dta = MAKESEGPTR( DOSVM_psp, 0x80 );
615
616   GetpWin16Lock( &lock );
617   _LeaveSysLevel( lock );
618
619   ResumeThread(dosvm_thread);
620   rv = DOSVM_Loop(dosvm_thread);
621
622   CloseHandle(dosvm_thread);
623   dosvm_thread = 0; dosvm_tid = 0;
624   CloseHandle(loop_thread);
625   loop_thread = 0; loop_tid = 0;
626
627   VGA_Clean();
628   ExitProcess(rv);
629 }
630
631 /***********************************************************************
632  *              MZ_Exit
633  */
634 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
635 {
636   if (DOSVM_psp) {
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 */
641     if (parpsp) {
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 */
654       DOSVM_psp = parpsp;
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);
663       return;
664     } else
665       TRACE("killing DOS task\n");
666   }
667   ExitThread( retval );
668 }
669
670
671 /***********************************************************************
672  *              MZ_Current
673  */
674 BOOL WINAPI MZ_Current( void )
675 {
676   return (dosvm_pid != 0); /* FIXME: do a better check */
677 }
678
679 #else /* !MZ_SUPPORTED */
680
681 /***********************************************************************
682  *              wine_load_dos_exe (WINEDOS.@)
683  */
684 void WINAPI wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline )
685 {
686   WARN("DOS executables not supported on this platform\n");
687   SetLastError(ERROR_BAD_FORMAT);
688 }
689
690 /***********************************************************************
691  *              MZ_Exec
692  */
693 BOOL WINAPI MZ_Exec( CONTEXT86 *context, LPCSTR filename, BYTE func, LPVOID paramblk )
694 {
695   /* can't happen */
696   SetLastError(ERROR_BAD_FORMAT);
697   return FALSE;
698 }
699
700 /***********************************************************************
701  *              MZ_AllocDPMITask
702  */
703 void WINAPI MZ_AllocDPMITask( void )
704 {
705     ERR("Actual real-mode calls not supported on this platform!\n");
706 }
707
708 /***********************************************************************
709  *              MZ_RunInThread
710  */
711 void WINAPI MZ_RunInThread( PAPCFUNC proc, ULONG_PTR arg )
712 {
713     proc(arg);
714 }
715
716 /***********************************************************************
717  *              MZ_Exit
718  */
719 void WINAPI MZ_Exit( CONTEXT86 *context, BOOL cs_psp, WORD retval )
720 {
721   ExitThread( retval );
722 }
723
724 /***********************************************************************
725  *              MZ_Current
726  */
727 BOOL WINAPI MZ_Current( void )
728 {
729     return FALSE;
730 }
731
732 #endif /* !MZ_SUPPORTED */