DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/dos/dos_execute.cpp
00001 /*
00002  *  Copyright (C) 2002-2015  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #include <string.h>
00021 #include <ctype.h>
00022 #include "dosbox.h"
00023 #include "mem.h"
00024 #include "dos_inc.h"
00025 #include "regs.h"
00026 #include "callback.h"
00027 #include "debug.h"
00028 #include "cpu.h"
00029 
00030 const char * RunningProgram="DOSBOX";
00031 
00032 #ifdef _MSC_VER
00033 #pragma pack(1)
00034 #endif
00035 struct EXE_Header {
00036         Bit16u signature;                                       /* EXE Signature MZ or ZM */
00037         Bit16u extrabytes;                                      /* Bytes on the last page */
00038         Bit16u pages;                                           /* Pages in file */
00039         Bit16u relocations;                                     /* Relocations in file */
00040         Bit16u headersize;                                      /* Paragraphs in header */
00041         Bit16u minmemory;                                       /* Minimum amount of memory */
00042         Bit16u maxmemory;                                       /* Maximum amount of memory */
00043         Bit16u initSS;
00044         Bit16u initSP;
00045         Bit16u checksum;
00046         Bit16u initIP;
00047         Bit16u initCS;
00048         Bit16u reloctable;
00049         Bit16u overlay;
00050 } GCC_ATTRIBUTE(packed);
00051 #ifdef _MSC_VER
00052 #pragma pack()
00053 #endif
00054 
00055 #define MAGIC1 0x5a4d
00056 #define MAGIC2 0x4d5a
00057 
00058 unsigned int MAXENV = 32768u;
00059 unsigned int ENV_KEEPFREE = 83;
00060 
00061 //#define MAXENV 65535u                                         /* mainline DOSBOx: original value was 32768u */
00062 //#define ENV_KEEPFREE 1024                                     /* keep unallocated by environment variables (original value mainline DOSBox: 83) */
00063 
00064 #define LOADNGO 0
00065 #define LOAD    1
00066 #define OVERLAY 3
00067 
00068 
00069 
00070 static void SaveRegisters(void) {
00071         reg_sp-=18;
00072         mem_writew(SegPhys(ss)+reg_sp+ 0,reg_ax);
00073         mem_writew(SegPhys(ss)+reg_sp+ 2,reg_cx);
00074         mem_writew(SegPhys(ss)+reg_sp+ 4,reg_dx);
00075         mem_writew(SegPhys(ss)+reg_sp+ 6,reg_bx);
00076         mem_writew(SegPhys(ss)+reg_sp+ 8,reg_si);
00077         mem_writew(SegPhys(ss)+reg_sp+10,reg_di);
00078         mem_writew(SegPhys(ss)+reg_sp+12,reg_bp);
00079         mem_writew(SegPhys(ss)+reg_sp+14,SegValue(ds));
00080         mem_writew(SegPhys(ss)+reg_sp+16,SegValue(es));
00081 }
00082 
00083 static void RestoreRegisters(void) {
00084         reg_ax=mem_readw(SegPhys(ss)+reg_sp+ 0);
00085         reg_cx=mem_readw(SegPhys(ss)+reg_sp+ 2);
00086         reg_dx=mem_readw(SegPhys(ss)+reg_sp+ 4);
00087         reg_bx=mem_readw(SegPhys(ss)+reg_sp+ 6);
00088         reg_si=mem_readw(SegPhys(ss)+reg_sp+ 8);
00089         reg_di=mem_readw(SegPhys(ss)+reg_sp+10);
00090         reg_bp=mem_readw(SegPhys(ss)+reg_sp+12);
00091         SegSet16(ds,mem_readw(SegPhys(ss)+reg_sp+14));
00092         SegSet16(es,mem_readw(SegPhys(ss)+reg_sp+16));
00093         reg_sp+=18;
00094 }
00095 
00096 extern void GFX_SetTitle(Bit32s cycles, Bits frameskip, Bits timing, bool paused);
00097 void DOS_UpdatePSPName(void) {
00098         DOS_MCB mcb(dos.psp()-1);
00099         static char name[9];
00100         mcb.GetFileName(name);
00101         name[8] = 0;
00102         if (!strlen(name)) strcpy(name,"DOSBOX");
00103         for(Bitu i = 0;i < 8;i++) { //Don't put garbage in the title bar. Mac OS X doesn't like it
00104                 if (name[i] == 0) break;
00105                 if ( !isprint(*reinterpret_cast<unsigned char*>(&name[i])) ) name[i] = '?';
00106         }
00107         RunningProgram = name;
00108         GFX_SetTitle(-1,-1,-1,false);
00109 }
00110 
00111 void DOS_Terminate(Bit16u pspseg,bool tsr,Bit8u exitcode) {
00112 
00113         dos.return_code=exitcode;
00114         dos.return_mode=(tsr)?(Bit8u)RETURN_TSR:(Bit8u)RETURN_EXIT;
00115         
00116         DOS_PSP curpsp(pspseg);
00117         if (pspseg==curpsp.GetParent()) return;
00118         /* Free Files owned by process */
00119         if (!tsr) curpsp.CloseFiles();
00120         
00121         /* Get the termination address */
00122         RealPt old22 = curpsp.GetInt22();
00123         /* Restore vector 22,23,24 */
00124         curpsp.RestoreVectors();
00125         /* Set the parent PSP */
00126         dos.psp(curpsp.GetParent());
00127         DOS_PSP parentpsp(curpsp.GetParent());
00128 
00129         /* Restore the SS:SP to the previous one */
00130         SegSet16(ss,RealSeg(parentpsp.GetStack()));
00131         reg_sp = RealOff(parentpsp.GetStack());         
00132         /* Restore the old CS:IP from int 22h */
00133         RestoreRegisters();
00134         /* Set the CS:IP stored in int 0x22 back on the stack */
00135         mem_writew(SegPhys(ss)+reg_sp+0,RealOff(old22));
00136         mem_writew(SegPhys(ss)+reg_sp+2,RealSeg(old22));
00137         /* set IOPL=3 (Strike Commander), nested task set,
00138            interrupts enabled, test flags cleared */
00139         mem_writew(SegPhys(ss)+reg_sp+4,0x7202);
00140         // Free memory owned by process
00141         if (!tsr) DOS_FreeProcessMemory(pspseg);
00142         DOS_UpdatePSPName();
00143 
00144         if ((!(CPU_AutoDetermineMode>>CPU_AUTODETERMINE_SHIFT)) || (cpu.pmode)) return;
00145 
00146         CPU_AutoDetermineMode>>=CPU_AUTODETERMINE_SHIFT;
00147         if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CYCLES) {
00148                 CPU_CycleAutoAdjust=false;
00149                 CPU_CycleLeft=0;
00150                 CPU_Cycles=0;
00151                 CPU_CycleMax=CPU_OldCycleMax;
00152                 GFX_SetTitle(CPU_OldCycleMax,-1,-1,false);
00153         } else {
00154                 GFX_SetTitle(-1,-1,-1,false);
00155         }
00156 #if (C_DYNAMIC_X86)
00157         if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CORE) {
00158                 cpudecoder=&CPU_Core_Normal_Run;
00159                 CPU_CycleLeft=0;
00160                 CPU_Cycles=0;
00161         }
00162 #endif
00163 
00164         return;
00165 }
00166 
00167 static bool MakeEnv(char * name,Bit16u * segment) {
00168         /* If segment to copy environment is 0 copy the caller's environment */
00169         DOS_PSP psp(dos.psp());
00170         PhysPt envread,envwrite;
00171         unsigned int keepfree;
00172         Bit16u envsize=1;
00173         bool parentenv=true;
00174 
00175         /* below 83 bytes, we must not append the mystery 0x01 + program name string */
00176         keepfree = ENV_KEEPFREE;
00177 
00178         if (*segment==0) {
00179                 if (!psp.GetEnvironment()) parentenv=false;                             //environment seg=0
00180                 envread=PhysMake(psp.GetEnvironment(),0);
00181         } else {
00182                 if (!*segment) parentenv=false;                                         //environment seg=0
00183                 envread=PhysMake(*segment,0);
00184         }
00185 
00186         if (parentenv) {
00187                 for (envsize=0; ;envsize++) {
00188                         if (envsize >= (MAXENV - keepfree)) {
00189                                 DOS_SetError(DOSERR_ENVIRONMENT_INVALID);
00190                                 return false;
00191                         }
00192                         if (mem_readw(envread+envsize)==0) break;
00193                 }
00194                 envsize += 2;                                                                   /* account for trailing \0\0 */
00195         }
00196         Bit16u size = long2para(envsize+keepfree);
00197         if (size == 0) size = 1;
00198         if (!DOS_AllocateMemory(segment,&size)) return false;
00199         envwrite=PhysMake(*segment,0);
00200         if (parentenv) {
00201                 MEM_BlockCopy(envwrite,envread,envsize);
00202 //              mem_memcpy(envwrite,envread,envsize);
00203                 envwrite+=envsize;
00204         } else {
00205                 mem_writeb(envwrite++,0);
00206         }
00207 
00208         /* If you look at environment blocks in real DOS, you see \x01 and then the name of your program follow
00209          * the environment block for some reason. If the user set the "keep free" below 83 bytes, then don't
00210          * do it. This might break some games that look for that string, but that's the user's fault for
00211          * setting the option, not ours */
00212         if (keepfree >= 83) {
00213                 mem_writew(envwrite,1);
00214                 envwrite+=2;
00215                 char namebuf[DOS_PATHLENGTH];
00216                 if (DOS_Canonicalize(name,namebuf)) {
00217                         MEM_BlockWrite(envwrite,namebuf,(Bitu)(strlen(namebuf)+1));
00218                         return true;
00219                 } else return false;
00220         }
00221 
00222         return true;
00223 }
00224 
00225 bool DOS_NewPSP(Bit16u segment, Bit16u size) {
00226         DOS_PSP psp(segment);
00227         psp.MakeNew(size);
00228         Bit16u parent_psp_seg=psp.GetParent();
00229         DOS_PSP psp_parent(parent_psp_seg);
00230         psp.CopyFileTable(&psp_parent,false);
00231         // copy command line as well (Kings Quest AGI -cga switch)
00232         psp.SetCommandTail(RealMake(parent_psp_seg,0x80));
00233         return true;
00234 }
00235 
00236 bool DOS_ChildPSP(Bit16u segment, Bit16u size) {
00237         DOS_PSP psp(segment);
00238         psp.MakeNew(size);
00239         Bit16u parent_psp_seg = psp.GetParent();
00240         DOS_PSP psp_parent(parent_psp_seg);
00241         psp.CopyFileTable(&psp_parent,true);
00242         psp.SetCommandTail(RealMake(parent_psp_seg,0x80));
00243         psp.SetFCB1(RealMake(parent_psp_seg,0x5c));
00244         psp.SetFCB2(RealMake(parent_psp_seg,0x6c));
00245         psp.SetEnvironment(psp_parent.GetEnvironment());
00246         psp.SetSize(size);
00247         // push registers in case child PSP is terminated
00248         SaveRegisters();
00249         psp.SetStack(RealMakeSeg(ss,reg_sp));
00250         reg_sp+=18;
00251         return true;
00252 }
00253 
00254 static void SetupPSP(Bit16u pspseg,Bit16u memsize,Bit16u envseg) {
00255         /* Fix the PSP for psp and environment MCB's */
00256         DOS_MCB mcb((Bit16u)(pspseg-1));
00257         mcb.SetPSPSeg(pspseg);
00258         mcb.SetPt((Bit16u)(envseg-1));
00259         mcb.SetPSPSeg(pspseg);
00260 
00261         DOS_PSP psp(pspseg);
00262         psp.MakeNew(memsize);
00263         psp.SetEnvironment(envseg);
00264 
00265         /* Copy file handles */
00266         DOS_PSP oldpsp(dos.psp());
00267         psp.CopyFileTable(&oldpsp,true);
00268 
00269 }
00270 
00271 static void SetupCMDLine(Bit16u pspseg,DOS_ParamBlock & block) {
00272         DOS_PSP psp(pspseg);
00273         // if cmdtail==0 it will inited as empty in SetCommandTail
00274         psp.SetCommandTail(block.exec.cmdtail);
00275 }
00276 
00277 /* FIXME: This code (or the shell perhaps) isn't very good at returning or
00278  *        printing an error message when it is unable to load and run an
00279  *        executable for whatever reason. Worst offense is that if it can't
00280  *        run an EXE due to lack of memory, it silently returns to the
00281  *        shell without any error message. The least we could do is return
00282  *        an error code so that the INT 21h EXEC call can print an informative
00283  *        error message! --J.C. */
00284 bool DOS_Execute(char * name,PhysPt block_pt,Bit8u flags) {
00285         EXE_Header head;Bitu i;
00286         Bit16u fhandle;Bit16u len;Bit32u pos;
00287         Bit16u pspseg,envseg,loadseg,memsize=0xffff,readsize;
00288         Bit16u minsize,maxsize,maxfree=0xffff;
00289         PhysPt loadaddress;RealPt relocpt;
00290         Bitu headersize=0,imagesize=0;
00291         DOS_ParamBlock block(block_pt);
00292 
00293         block.LoadData();
00294         //Remove the loadhigh flag for the moment!
00295         if(flags&0x80) LOG(LOG_EXEC,LOG_ERROR)("using loadhigh flag!!!!!. dropping it");
00296         flags &= 0x7f;
00297         if (flags!=LOADNGO && flags!=OVERLAY && flags!=LOAD) {
00298                 DOS_SetError(DOSERR_FORMAT_INVALID);
00299                 return false;
00300 //              E_Exit("DOS:Not supported execute mode %d for file %s",flags,name);
00301         }
00302         /* Check for EXE or COM File */
00303         bool iscom=false;
00304         if (!DOS_OpenFile(name,OPEN_READ,&fhandle)) {
00305                 DOS_SetError(DOSERR_FILE_NOT_FOUND);
00306                 return false;
00307         }
00308         len=sizeof(EXE_Header);
00309         if (!DOS_ReadFile(fhandle,(Bit8u *)&head,&len)) {
00310                 DOS_CloseFile(fhandle);
00311                 return false;
00312         }
00313         if (len<sizeof(EXE_Header)) {
00314                 if (len==0) {
00315                         /* Prevent executing zero byte files */
00316                         DOS_SetError(DOSERR_ACCESS_DENIED);
00317                         DOS_CloseFile(fhandle);
00318                         return false;
00319                 }
00320                 /* Otherwise must be a .com file */
00321                 iscom=true;
00322         } else {
00323                 /* Convert the header to correct endian, i hope this works */
00324                 HostPt endian=(HostPt)&head;
00325                 for (i=0;i<sizeof(EXE_Header)/2;i++) {
00326                         *((Bit16u *)endian)=host_readw(endian);
00327                         endian+=2;
00328                 }
00329                 if ((head.signature!=MAGIC1) && (head.signature!=MAGIC2)) iscom=true;
00330                 else {
00331                         if(head.pages & ~0x07ffu) /* 1 MB dos maximum address limit. Fixes TC3 IDE (kippesoep) */
00332                                 LOG(LOG_EXEC,LOG_NORMAL)("Weird header: head.pages > 1 MB");
00333                         head.pages&=0x07ffu;
00334                         headersize = head.headersize*16u;
00335                         imagesize = head.pages*512u-headersize; 
00336                         if (imagesize+headersize<512u) imagesize = 512u-headersize;
00337                 }
00338         }
00339         Bit8u * loadbuf=(Bit8u *)new Bit8u[0x10000u];
00340         if (flags!=OVERLAY) {
00341                 /* Create an environment block */
00342                 envseg=block.exec.envseg;
00343                 if (!MakeEnv(name,&envseg)) {
00344                         DOS_CloseFile(fhandle);
00345                         return false;
00346                 }
00347                 /* Get Memory */                
00348                 DOS_AllocateMemory(&pspseg,&maxfree);
00349                 if (iscom) {
00350                         minsize=0x1000;maxsize=0xffff;
00351                         if (machine==MCH_PCJR) {
00352                                 /* try to load file into memory below 96k */ 
00353                                 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);  
00354                                 Bit16u dataread=0x1800;
00355                                 DOS_ReadFile(fhandle,loadbuf,&dataread);
00356                                 if (dataread<0x1800) maxsize=dataread;
00357                                 if (minsize>maxsize) minsize=maxsize;
00358                         }
00359                 } else {        /* Exe size calculated from header */
00360                         minsize=long2para(imagesize+((unsigned int)head.minmemory<<4u)+256u);
00361                         if (head.maxmemory!=0) maxsize=long2para(imagesize+((unsigned int)head.maxmemory<<4u)+256u);
00362                         else maxsize=0xffffu;
00363 
00364             /* Bugfix: scene.org mirrors/hornet/demos/1991/putrefac.zip Putrefaction !PF.{3}
00365              *         has an EXE header that specifies a maxsize less than minsize, and a
00366              *         initial stack pointer that is only valid if we use the maxsize.
00367              *
00368              *         This allows it to run without the SS:IP out of range error below. */
00369             if (maxsize < minsize) maxsize = minsize;
00370                 }
00371                 if (maxfree<minsize) {
00372                         if (iscom) {
00373                                 /* Reduce minimum of needed memory size to filesize */
00374                                 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);  
00375                                 Bit16u dataread=0xf800;
00376                                 DOS_ReadFile(fhandle,loadbuf,&dataread);
00377                                 if (dataread<0xf800) minsize=((dataread+0x10)>>4)+0x20;
00378                         }
00379                         if (maxfree<minsize) {
00380                                 DOS_CloseFile(fhandle);
00381                                 DOS_SetError(DOSERR_INSUFFICIENT_MEMORY);
00382                                 DOS_FreeMemory(envseg);
00383                                 return false;
00384                         }
00385                 }
00386                 if (maxfree<maxsize) memsize=maxfree;
00387                 else memsize=maxsize;
00388                 if (!DOS_AllocateMemory(&pspseg,&memsize)) E_Exit("DOS:Exec error in memory");
00389                 if (iscom && (machine==MCH_PCJR) && (pspseg<0x2000)) {
00390                         maxsize=0xffff;
00391                         /* resize to full extent of memory block */
00392                         DOS_ResizeMemory(pspseg,&maxsize);
00393                         /* now try to lock out memory above segment 0x2000 */
00394                         if ((real_readb(0x2000,0)==0x5a) && (real_readw(0x2000,1)==0) && (real_readw(0x2000,3)==0x7ffe)) {
00395                                 /* MCB after PCJr graphics memory region is still free */
00396                                 if (pspseg+maxsize==0x17ff) {
00397                                         DOS_MCB cmcb((Bit16u)(pspseg-1));
00398                                         cmcb.SetType(0x5a);             // last block
00399                                 }
00400                         }
00401                 }
00402                 loadseg=pspseg+16;
00403                 if (!iscom) {
00404                         /* Check if requested to load program into upper part of allocated memory */
00405                         if ((head.minmemory == 0) && (head.maxmemory == 0)) {
00406                                 loadseg = (pspseg+memsize);
00407                                 loadseg -= (imagesize+0xF)/0x10;
00408                                 if (loadseg < (pspseg+16)) loadseg = pspseg+16;
00409                                 if ((unsigned int)(loadseg+((imagesize+0xF)/0x10)) > (unsigned int)(pspseg+memsize))
00410                                         E_Exit("EXE loading error, unable to load to top of block, nor able to fit into block");
00411                         }
00412                 }
00413         } else loadseg=block.overlay.loadseg;
00414         /* Load the executable */
00415         loadaddress=PhysMake(loadseg,0);
00416 
00417         if (iscom) {    /* COM Load 64k - 256 bytes max */
00418                 /* how big is the COM image? make sure it fits */
00419                 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_END);
00420                 readsize = pos;
00421                 if (readsize > (0xffff-256)) readsize = 0xffff-256;
00422                 pos += 256; /* plus stack */
00423                 if (pos > (unsigned int)(memsize*0x10)) E_Exit("DOS:Not enough memory for COM executable");
00424 
00425                 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);  
00426                 DOS_ReadFile(fhandle,loadbuf,&readsize);
00427                 MEM_BlockWrite(loadaddress,loadbuf,readsize);
00428         } else {        /* EXE Load in 32kb blocks and then relocate */
00429                 if (imagesize > (unsigned int)(memsize*0x10)) E_Exit("DOS:Not enough memory for EXE image");
00430                 pos=headersize;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET); 
00431                 while (imagesize>0x7FFF) {
00432                         readsize=0x8000;DOS_ReadFile(fhandle,loadbuf,&readsize);
00433                         MEM_BlockWrite(loadaddress,loadbuf,readsize);
00434 //                      if (readsize!=0x8000) LOG(LOG_EXEC,LOG_NORMAL)("Illegal header");
00435                         loadaddress+=0x8000;imagesize-=0x8000;
00436                 }
00437                 if (imagesize>0) {
00438                         readsize=(Bit16u)imagesize;DOS_ReadFile(fhandle,loadbuf,&readsize);
00439                         MEM_BlockWrite(loadaddress,loadbuf,readsize);
00440 //                      if (readsize!=imagesize) LOG(LOG_EXEC,LOG_NORMAL)("Illegal header");
00441                 }
00442                 /* Relocate the exe image */
00443                 Bit16u relocate;
00444                 if (flags==OVERLAY) relocate=block.overlay.relocation;
00445                 else relocate=loadseg;
00446                 pos=head.reloctable;DOS_SeekFile(fhandle,&pos,0);
00447                 for (i=0;i<head.relocations;i++) {
00448                         readsize=4;DOS_ReadFile(fhandle,(Bit8u *)&relocpt,&readsize);
00449                         relocpt=host_readd((HostPt)&relocpt);           //Endianize
00450                         PhysPt address=PhysMake(RealSeg(relocpt)+loadseg,RealOff(relocpt));
00451                         mem_writew(address,mem_readw(address)+relocate);
00452                 }
00453         }
00454         delete[] loadbuf;
00455         DOS_CloseFile(fhandle);
00456 
00457         /* Setup a psp */
00458         if (flags!=OVERLAY) {
00459                 // Create psp after closing exe, to avoid dead file handle of exe in copied psp
00460                 SetupPSP(pspseg,memsize,envseg);
00461                 SetupCMDLine(pspseg,block);
00462         };
00463         CALLBACK_SCF(false);            /* Carry flag cleared for caller if successfull */
00464         if (flags==OVERLAY) return true;                        /* Everything done for overlays */
00465         RealPt csip,sssp;
00466         if (iscom) {
00467                 unsigned int stack_sp = 0xfffe;
00468 
00469                 /* On most DOS systems the stack pointer is set to 0xFFFE.
00470                  * Limiting the stack pointer to stay within the memory block
00471                  * enables COM images to run correctly in less than 72KB of RAM.
00472                  * Doing this resolves the random crashes observed with DOSBox's
00473                  * builtin commands when less than 72KB of RAM is assigned.
00474                  *
00475                  * At some point I need to boot MS-DOS/PC-DOS 1.x and 2.x in small
00476                  * amounts of RAM to verify that's what actually happens. --J.C. */
00477                 if (stack_sp > ((memsize*0x10UL)-2))
00478                         stack_sp = (memsize*0x10UL)-2;
00479 
00480                 csip=RealMake(pspseg,0x100);
00481                 sssp=RealMake(pspseg,stack_sp);
00482                 mem_writew(PhysMake(pspseg,stack_sp),0);
00483         } else {
00484                 /* FIXME: I've heard of EXE files with entry points like FFFF:0100 or something (COM images turned EXE if I recall).
00485                  *        Does this check validate those too? */
00486                 csip=RealMake(loadseg+head.initCS,head.initIP);
00487                 sssp=RealMake(loadseg+head.initSS,head.initSP);
00488                 if (sssp >= RealMake(pspseg+memsize,0)) E_Exit("DOS:Initial SS:IP beyond allocated memory block for EXE image");
00489                 if (csip >= RealMake(pspseg+memsize,0)) E_Exit("DOS:Initial CS:IP beyond allocated memory block for EXE image");
00490                 if (head.initSP<4) LOG(LOG_EXEC,LOG_ERROR)("stack underflow/wrap at EXEC SS:SP=%04x:%04x",head.initSS,head.initSP);
00491         }
00492 
00493         if (flags==LOAD) {
00494                 SaveRegisters();
00495                 DOS_PSP callpsp(dos.psp());
00496                 /* Save the SS:SP on the PSP of calling program */
00497                 callpsp.SetStack(RealMakeSeg(ss,reg_sp));
00498                 reg_sp+=18;
00499                 /* Switch the psp's */
00500                 dos.psp(pspseg);
00501                 DOS_PSP newpsp(dos.psp());
00502                 dos.dta(RealMake(newpsp.GetSegment(),0x80));
00503                 /* First word on the stack is the value ax should contain on startup */
00504                 real_writew(RealSeg(sssp-2),RealOff(sssp-2),0xffff);
00505                 block.exec.initsssp = sssp-2;
00506                 block.exec.initcsip = csip;
00507                 block.SaveData();
00508                 return true;
00509         }
00510 
00511         if (flags==LOADNGO) {
00512                 if ((reg_sp>0xfffe) || (reg_sp<18)) LOG(LOG_EXEC,LOG_ERROR)("stack underflow/wrap at EXEC SS:SP=%04x:%04x",SegValue(ss),reg_sp);
00513                 /* Get Caller's program CS:IP of the stack and set termination address to that */
00514                 RealSetVec(0x22,RealMake(mem_readw(SegPhys(ss)+reg_sp+2),mem_readw(SegPhys(ss)+reg_sp)));
00515                 SaveRegisters();
00516                 DOS_PSP callpsp(dos.psp());
00517                 /* Save the SS:SP on the PSP of calling program */
00518                 callpsp.SetStack(RealMakeSeg(ss,reg_sp));
00519                 /* Switch the psp's and set new DTA */
00520                 dos.psp(pspseg);
00521                 DOS_PSP newpsp(dos.psp());
00522                 dos.dta(RealMake(newpsp.GetSegment(),0x80));
00523                 /* save vectors */
00524                 newpsp.SaveVectors();
00525                 /* copy fcbs */
00526                 newpsp.SetFCB1(block.exec.fcb1);
00527                 newpsp.SetFCB2(block.exec.fcb2);
00528         /* Save the SS:SP on the PSP of new program */
00529         newpsp.SetStack(RealMakeSeg(ss,reg_sp));
00530         /* Set the stack for new program */
00531                 SegSet16(ss,RealSeg(sssp));reg_sp=RealOff(sssp);
00532                 /* Add some flags and CS:IP on the stack for the IRET */
00533                 CPU_Push16(RealSeg(csip));
00534                 CPU_Push16(RealOff(csip));
00535                 /* DOS starts programs with a RETF, so critical flags
00536                  * should not be modified (IOPL in v86 mode);
00537                  * interrupt flag is set explicitly, test flags cleared */
00538                 reg_flags=(reg_flags&(~FMASK_TEST))|FLAG_IF;
00539                 //Jump to retf so that we only need to store cs:ip on the stack
00540                 reg_ip++;
00541                 /* Setup the rest of the registers */
00542                 reg_ax=reg_bx=0;reg_cx=0xff;
00543                 reg_dx=pspseg;
00544                 reg_si=RealOff(csip);
00545                 reg_di=RealOff(sssp);
00546                 reg_bp=0x91c;   /* DOS internal stack begin relict */
00547                 SegSet16(ds,pspseg);SegSet16(es,pspseg);
00548 #if C_DEBUG             
00549                 /* Started from debug.com, then set breakpoint at start */
00550                 DEBUG_CheckExecuteBreakpoint(RealSeg(csip),RealOff(csip));
00551 #endif
00552                 /* Add the filename to PSP and environment MCB's */
00553                 char stripname[8]= { 0 };Bitu index=0;
00554                 while (char chr=*name++) {
00555                         switch (chr) {
00556                         case ':':case '\\':case '/':index=0;break;
00557                         default:if (index<8) stripname[index++]=(char)toupper(chr);
00558                         }
00559                 }
00560                 index=0;
00561                 while (index<8) {
00562                         if (stripname[index]=='.') break;
00563                         if (!stripname[index]) break;   
00564                         index++;
00565                 }
00566                 memset(&stripname[index],0,8-index);
00567                 DOS_MCB pspmcb(dos.psp()-1);
00568                 pspmcb.SetFileName(stripname);
00569                 DOS_UpdatePSPName();
00570                 return true;
00571         }
00572         return false;
00573 }