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