Debian Codegraph @

src/dos/dos_execute.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: dos_execute.cpp,v 1.68 2009-10-04 14:28:07 c2woody Exp $ */
20
21 #include <string.h>
22 #include <ctype.h>
23 #include "dosbox.h"
24 #include "mem.h"
25 #include "dos_inc.h"
26 #include "regs.h"
27 #include "callback.h"
28 #include "debug.h"
29 #include "cpu.h"
30
31 const char * RunningProgram="DOSBOX";
32
33 #ifdef _MSC_VER
34 #pragma pack(1)
35 #endif
36 struct EXE_Header {
37 Bit16u signature; /* EXE Signature MZ or ZM */
38 Bit16u extrabytes; /* Bytes on the last page */
39 Bit16u pages; /* Pages in file */
40 Bit16u relocations; /* Relocations in file */
41 Bit16u headersize; /* Paragraphs in header */
42 Bit16u minmemory; /* Minimum amount of memory */
43 Bit16u maxmemory; /* Maximum amount of memory */
44 Bit16u initSS;
45 Bit16u initSP;
46 Bit16u checksum;
47 Bit16u initIP;
48 Bit16u initCS;
49 Bit16u reloctable;
50 Bit16u overlay;
51 } GCC_ATTRIBUTE(packed);
52 #ifdef _MSC_VER
53 #pragma pack()
54 #endif
55
56 #define MAGIC1 0x5a4d
57 #define MAGIC2 0x4d5a
58 #define MAXENV 32768u
59 #define ENV_KEEPFREE 83 /* keep unallocated by environment variables */
60 #define LOADNGO 0
61 #define LOAD 1
62 #define OVERLAY 3
63
64
65
66 static void SaveRegisters(void) {
67 reg_sp-=18;
68 mem_writew(SegPhys(ss)+reg_sp+ 0,reg_ax);
69 mem_writew(SegPhys(ss)+reg_sp+ 2,reg_cx);
70 mem_writew(SegPhys(ss)+reg_sp+ 4,reg_dx);
71 mem_writew(SegPhys(ss)+reg_sp+ 6,reg_bx);
72 mem_writew(SegPhys(ss)+reg_sp+ 8,reg_si);
73 mem_writew(SegPhys(ss)+reg_sp+10,reg_di);
74 mem_writew(SegPhys(ss)+reg_sp+12,reg_bp);
75 mem_writew(SegPhys(ss)+reg_sp+14,SegValue(ds));
76 mem_writew(SegPhys(ss)+reg_sp+16,SegValue(es));
77 }
78
79 static void RestoreRegisters(void) {
80 reg_ax=mem_readw(SegPhys(ss)+reg_sp+ 0);
81 reg_cx=mem_readw(SegPhys(ss)+reg_sp+ 2);
82 reg_dx=mem_readw(SegPhys(ss)+reg_sp+ 4);
83 reg_bx=mem_readw(SegPhys(ss)+reg_sp+ 6);
84 reg_si=mem_readw(SegPhys(ss)+reg_sp+ 8);
85 reg_di=mem_readw(SegPhys(ss)+reg_sp+10);
86 reg_bp=mem_readw(SegPhys(ss)+reg_sp+12);
87 SegSet16(ds,mem_readw(SegPhys(ss)+reg_sp+14));
88 SegSet16(es,mem_readw(SegPhys(ss)+reg_sp+16));
89 reg_sp+=18;
90 }
91
92 extern void GFX_SetTitle(Bit32s cycles,Bits frameskip,bool paused);
93 void DOS_UpdatePSPName(void) {
94 DOS_MCB mcb(dos.psp()-1);
95 static char name[9];
96 mcb.GetFileName(name);
97 name[8] = 0;
98 if (!strlen(name)) strcpy(name,"DOSBOX");
99 for(Bitu i = 0;i < 8;i++) { //Don't put garbage in the title bar. Mac OS X doesn't like it
100 if (name[i] == 0) break;
101 if ( !isprint(*reinterpret_cast<unsigned char*>(&name[i])) ) name[i] = '?';
102 }
103 RunningProgram = name;
104 GFX_SetTitle(-1,-1,false);
105 }
106
107 void DOS_Terminate(Bit16u pspseg,bool tsr,Bit8u exitcode) {
108
109 dos.return_code=exitcode;
110 dos.return_mode=(tsr)?(Bit8u)RETURN_TSR:(Bit8u)RETURN_EXIT;
111
112 DOS_PSP curpsp(pspseg);
113 if (pspseg==curpsp.GetParent()) return;
114 /* Free Files owned by process */
115 if (!tsr) curpsp.CloseFiles();
116
117 /* Get the termination address */
118 RealPt old22 = curpsp.GetInt22();
119 /* Restore vector 22,23,24 */
120 curpsp.RestoreVectors();
121 /* Set the parent PSP */
122 dos.psp(curpsp.GetParent());
123 DOS_PSP parentpsp(curpsp.GetParent());
124
125 /* Restore the SS:SP to the previous one */
126 SegSet16(ss,RealSeg(parentpsp.GetStack()));
127 reg_sp = RealOff(parentpsp.GetStack());
128 /* Restore the old CS:IP from int 22h */
129 RestoreRegisters();
130 /* Set the CS:IP stored in int 0x22 back on the stack */
131 mem_writew(SegPhys(ss)+reg_sp+0,RealOff(old22));
132 mem_writew(SegPhys(ss)+reg_sp+2,RealSeg(old22));
133 /* set IOPL=3 (Strike Commander), nested task set,
134 interrupts enabled, test flags cleared */
135 mem_writew(SegPhys(ss)+reg_sp+4,0x7202);
136 // Free memory owned by process
137 if (!tsr) DOS_FreeProcessMemory(pspseg);
138 DOS_UpdatePSPName();
139
140 if ((!(CPU_AutoDetermineMode>>CPU_AUTODETERMINE_SHIFT)) || (cpu.pmode)) return;
141
142 CPU_AutoDetermineMode>>=CPU_AUTODETERMINE_SHIFT;
143 if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CYCLES) {
144 CPU_CycleAutoAdjust=false;
145 CPU_CycleLeft=0;
146 CPU_Cycles=0;
147 CPU_CycleMax=CPU_OldCycleMax;
148 GFX_SetTitle(CPU_OldCycleMax,-1,false);
149 } else {
150 GFX_SetTitle(-1,-1,false);
151 }
152 #if (C_DYNAMIC_X86) || (C_DYNREC)
153 if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CORE) {
154 cpudecoder=&CPU_Core_Normal_Run;
155 CPU_CycleLeft=0;
156 CPU_Cycles=0;
157 }
158 #endif
159
160 return;
161 }
162
163 static bool MakeEnv(char * name,Bit16u * segment) {
164 /* If segment to copy environment is 0 copy the caller's environment */
165 DOS_PSP psp(dos.psp());
166 PhysPt envread,envwrite;
167 Bit16u envsize=1;
168 bool parentenv=true;
169
170 if (*segment==0) {
171 if (!psp.GetEnvironment()) parentenv=false; //environment seg=0
172 envread=PhysMake(psp.GetEnvironment(),0);
173 } else {
174 if (!*segment) parentenv=false; //environment seg=0
175 envread=PhysMake(*segment,0);
176 }
177
178 if (parentenv) {
179 for (envsize=0; ;envsize++) {
180 if (envsize>=MAXENV - ENV_KEEPFREE) {
181 DOS_SetError(DOSERR_ENVIRONMENT_INVALID);
182 return false;
183 }
184 if (mem_readw(envread+envsize)==0) break;
185 }
186 envsize += 2; /* account for trailing \0\0 */
187 }
188 Bit16u size=long2para(envsize+ENV_KEEPFREE);
189 if (!DOS_AllocateMemory(segment,&size)) return false;
190 envwrite=PhysMake(*segment,0);
191 if (parentenv) {
192 MEM_BlockCopy(envwrite,envread,envsize);
193 // mem_memcpy(envwrite,envread,envsize);
194 envwrite+=envsize;
195 } else {
196 mem_writeb(envwrite++,0);
197 }
198 mem_writew(envwrite,1);
199 envwrite+=2;
200 char namebuf[DOS_PATHLENGTH];
201 if (DOS_Canonicalize(name,namebuf)) {
202 MEM_BlockWrite(envwrite,namebuf,(Bitu)(strlen(namebuf)+1));
203 return true;
204 } else return false;
205 }
206
207 bool DOS_NewPSP(Bit16u segment, Bit16u size) {
208 DOS_PSP psp(segment);
209 psp.MakeNew(size);
210 Bit16u parent_psp_seg=psp.GetParent();
211 DOS_PSP psp_parent(parent_psp_seg);
212 psp.CopyFileTable(&psp_parent,false);
213 // copy command line as well (Kings Quest AGI -cga switch)
214 psp.SetCommandTail(RealMake(parent_psp_seg,0x80));
215 return true;
216 }
217
218 bool DOS_ChildPSP(Bit16u segment, Bit16u size) {
219 DOS_PSP psp(segment);
220 psp.MakeNew(size);
221 Bit16u parent_psp_seg = psp.GetParent();
222 DOS_PSP psp_parent(parent_psp_seg);
223 psp.CopyFileTable(&psp_parent,true);
224 psp.SetCommandTail(RealMake(parent_psp_seg,0x80));
225 psp.SetFCB1(RealMake(parent_psp_seg,0x5c));
226 psp.SetFCB2(RealMake(parent_psp_seg,0x6c));
227 psp.SetEnvironment(psp_parent.GetEnvironment());
228 psp.SetSize(size);
229 return true;
230 }
231
232 static void SetupPSP(Bit16u pspseg,Bit16u memsize,Bit16u envseg) {
233 /* Fix the PSP for psp and environment MCB's */
234 DOS_MCB mcb((Bit16u)(pspseg-1));
235 mcb.SetPSPSeg(pspseg);
236 mcb.SetPt((Bit16u)(envseg-1));
237 mcb.SetPSPSeg(pspseg);
238
239 DOS_PSP psp(pspseg);
240 psp.MakeNew(memsize);
241 psp.SetEnvironment(envseg);
242
243 /* Copy file handles */
244 DOS_PSP oldpsp(dos.psp());
245 psp.CopyFileTable(&oldpsp,true);
246
247 }
248
249 static void SetupCMDLine(Bit16u pspseg,DOS_ParamBlock & block) {
250 DOS_PSP psp(pspseg);
251 // if cmdtail==0 it will inited as empty in SetCommandTail
252 psp.SetCommandTail(block.exec.cmdtail);
253 }
254
255 bool DOS_Execute(char * name,PhysPt block_pt,Bit8u flags) {
256 EXE_Header head;Bitu i;
257 Bit16u fhandle;Bit16u len;Bit32u pos;
258 Bit16u pspseg,envseg,loadseg,memsize,readsize;
259 PhysPt loadaddress;RealPt relocpt;
260 Bitu headersize=0,imagesize=0;
261 DOS_ParamBlock block(block_pt);
262
263 block.LoadData();
264 //Remove the loadhigh flag for the moment!
265 if(flags&0x80) LOG(LOG_EXEC,LOG_ERROR)("using loadhigh flag!!!!!. dropping it");
266 flags &= 0x7f;
267 if (flags!=LOADNGO && flags!=OVERLAY && flags!=LOAD) {
268 DOS_SetError(DOSERR_FORMAT_INVALID);
269 return false;
270 // E_Exit("DOS:Not supported execute mode %d for file %s",flags,name);
271 }
272 /* Check for EXE or COM File */
273 bool iscom=false;
274 if (!DOS_OpenFile(name,OPEN_READ,&fhandle)) {
275 DOS_SetError(DOSERR_FILE_NOT_FOUND);
276 return false;
277 }
278 len=sizeof(EXE_Header);
279 if (!DOS_ReadFile(fhandle,(Bit8u *)&head,&len)) {
280 DOS_CloseFile(fhandle);
281 return false;
282 }
283 if (len<sizeof(EXE_Header)) {
284 if (len==0) {
285 /* Prevent executing zero byte files */
286 DOS_SetError(DOSERR_ACCESS_DENIED);
287 DOS_CloseFile(fhandle);
288 return false;
289 }
290 /* Otherwise must be a .com file */
291 iscom=true;
292 } else {
293 /* Convert the header to correct endian, i hope this works */
294 HostPt endian=(HostPt)&head;
295 for (i=0;i<sizeof(EXE_Header)/2;i++) {
296 *((Bit16u *)endian)=host_readw(endian);
297 endian+=2;
298 }
299 if ((head.signature!=MAGIC1) && (head.signature!=MAGIC2)) iscom=true;
300 else {
301 if(head.pages & ~0x07ff) /* 1 MB dos maximum address limit. Fixes TC3 IDE (kippesoep) */
302 LOG(LOG_EXEC,LOG_NORMAL)("Weird header: head.pages > 1 MB");
303 head.pages&=0x07ff;
304 headersize = head.headersize*16;
305 imagesize = head.pages*512-headersize;
306 if (imagesize+headersize<512) imagesize = 512-headersize;
307 }
308 }
309 Bit8u * loadbuf=(Bit8u *)new Bit8u[0x10000];
310 if (flags!=OVERLAY) {
311 /* Create an environment block */
312 envseg=block.exec.envseg;
313 if (!MakeEnv(name,&envseg)) {
314 DOS_CloseFile(fhandle);
315 return false;
316 }
317 /* Get Memory */
318 Bit16u minsize,maxsize;Bit16u maxfree=0xffff;DOS_AllocateMemory(&pspseg,&maxfree);
319 if (iscom) {
320 minsize=0x1000;maxsize=0xffff;
321 if (machine==MCH_PCJR) {
322 /* try to load file into memory below 96k */
323 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);
324 Bit16u dataread=0x1800;
325 DOS_ReadFile(fhandle,loadbuf,&dataread);
326 if (dataread<0x1800) maxsize=dataread;
327 if (minsize>maxsize) minsize=maxsize;
328 }
329 } else { /* Exe size calculated from header */
330 minsize=long2para(imagesize+(head.minmemory<<4)+256);
331 if (head.maxmemory!=0) maxsize=long2para(imagesize+(head.maxmemory<<4)+256);
332 else maxsize=0xffff;
333 }
334 if (maxfree<minsize) {
335 if (iscom) {
336 /* Reduce minimum of needed memory size to filesize */
337 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);
338 Bit16u dataread=0xf800;
339 DOS_ReadFile(fhandle,loadbuf,&dataread);
340 if (dataread<0xf800) minsize=((dataread+0x10)>>4)+0x20;
341 }
342 if (maxfree<minsize) {
343 DOS_CloseFile(fhandle);
344 DOS_SetError(DOSERR_INSUFFICIENT_MEMORY);
345 DOS_FreeMemory(envseg);
346 return false;
347 }
348 }
349 if (maxfree<maxsize) memsize=maxfree;
350 else memsize=maxsize;
351 if (!DOS_AllocateMemory(&pspseg,&memsize)) E_Exit("DOS:Exec error in memory");
352 if (iscom && (machine==MCH_PCJR) && (pspseg<0x2000)) {
353 maxsize=0xffff;
354 /* resize to full extent of memory block */
355 DOS_ResizeMemory(pspseg,&maxsize);
356 /* now try to lock out memory above segment 0x2000 */
357 if ((real_readb(0x2000,0)==0x5a) && (real_readw(0x2000,1)==0) && (real_readw(0x2000,3)==0x7ffe)) {
358 /* MCB after PCJr graphics memory region is still free */
359 if (pspseg+maxsize==0x17ff) {
360 DOS_MCB cmcb((Bit16u)(pspseg-1));
361 cmcb.SetType(0x5a); // last block
362 }
363 }
364 }
365 loadseg=pspseg+16;
366 if (!iscom) {
367 /* Check if requested to load program into upper part of allocated memory */
368 if ((head.minmemory == 0) && (head.maxmemory == 0))
369 loadseg = (Bit16u)(((pspseg+memsize)*0x10-imagesize)/0x10);
370 }
371 } else loadseg=block.overlay.loadseg;
372 /* Load the executable */
373 loadaddress=PhysMake(loadseg,0);
374
375 if (iscom) { /* COM Load 64k - 256 bytes max */
376 pos=0;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);
377 readsize=0xffff-256;
378 DOS_ReadFile(fhandle,loadbuf,&readsize);
379 MEM_BlockWrite(loadaddress,loadbuf,readsize);
380 } else { /* EXE Load in 32kb blocks and then relocate */
381 pos=headersize;DOS_SeekFile(fhandle,&pos,DOS_SEEK_SET);
382 while (imagesize>0x7FFF) {
383 readsize=0x8000;DOS_ReadFile(fhandle,loadbuf,&readsize);
384 MEM_BlockWrite(loadaddress,loadbuf,readsize);
385 // if (readsize!=0x8000) LOG(LOG_EXEC,LOG_NORMAL)("Illegal header");
386 loadaddress+=0x8000;imagesize-=0x8000;
387 }
388 if (imagesize>0) {
389 readsize=(Bit16u)imagesize;DOS_ReadFile(fhandle,loadbuf,&readsize);
390 MEM_BlockWrite(loadaddress,loadbuf,readsize);
391 // if (readsize!=imagesize) LOG(LOG_EXEC,LOG_NORMAL)("Illegal header");
392 }
393 /* Relocate the exe image */
394 Bit16u relocate;
395 if (flags==OVERLAY) relocate=block.overlay.relocation;
396 else relocate=loadseg;
397 pos=head.reloctable;DOS_SeekFile(fhandle,&pos,0);
398 for (i=0;i<head.relocations;i++) {
399 readsize=4;DOS_ReadFile(fhandle,(Bit8u *)&relocpt,&readsize);
400 relocpt=host_readd((HostPt)&relocpt); //Endianize
401 PhysPt address=PhysMake(RealSeg(relocpt)+loadseg,RealOff(relocpt));
402 mem_writew(address,mem_readw(address)+relocate);
403 }
404 }
405 delete[] loadbuf;
406 DOS_CloseFile(fhandle);
407
408 /* Setup a psp */
409 if (flags!=OVERLAY) {
410 // Create psp after closing exe, to avoid dead file handle of exe in copied psp
411 SetupPSP(pspseg,memsize,envseg);
412 SetupCMDLine(pspseg,block);
413 };
414 CALLBACK_SCF(false); /* Carry flag cleared for caller if successfull */
415 if (flags==OVERLAY) return true; /* Everything done for overlays */
416 RealPt csip,sssp;
417 if (iscom) {
418 csip=RealMake(pspseg,0x100);
419 sssp=RealMake(pspseg,0xfffe);
420 mem_writew(PhysMake(pspseg,0xfffe),0);
421 } else {
422 csip=RealMake(loadseg+head.initCS,head.initIP);
423 sssp=RealMake(loadseg+head.initSS,head.initSP);
424 if (head.initSP<4) LOG(LOG_EXEC,LOG_ERROR)("stack underflow/wrap at EXEC");
425 }
426
427 if (flags==LOAD) {
428 SaveRegisters();
429 DOS_PSP callpsp(dos.psp());
430 /* Save the SS:SP on the PSP of calling program */
431 callpsp.SetStack(RealMakeSeg(ss,reg_sp));
432 reg_sp+=18;
433 /* Switch the psp's */
434 dos.psp(pspseg);
435 DOS_PSP newpsp(dos.psp());
436 dos.dta(RealMake(newpsp.GetSegment(),0x80));
437 /* First word on the stack is the value ax should contain on startup */
438 real_writew(RealSeg(sssp-2),RealOff(sssp-2),0xffff);
439 block.exec.initsssp = sssp-2;
440 block.exec.initcsip = csip;
441 block.SaveData();
442 return true;
443 }
444
445 if (flags==LOADNGO) {
446 if ((reg_sp>0xfffe) || (reg_sp<18)) LOG(LOG_EXEC,LOG_ERROR)("stack underflow/wrap at EXEC");
447 /* Get Caller's program CS:IP of the stack and set termination address to that */
448 RealSetVec(0x22,RealMake(mem_readw(SegPhys(ss)+reg_sp+2),mem_readw(SegPhys(ss)+reg_sp)));
449 SaveRegisters();
450 DOS_PSP callpsp(dos.psp());
451 /* Save the SS:SP on the PSP of calling program */
452 callpsp.SetStack(RealMakeSeg(ss,reg_sp));
453 /* Switch the psp's and set new DTA */
454 dos.psp(pspseg);
455 DOS_PSP newpsp(dos.psp());
456 dos.dta(RealMake(newpsp.GetSegment(),0x80));
457 /* save vectors */
458 newpsp.SaveVectors();
459 /* copy fcbs */
460 newpsp.SetFCB1(block.exec.fcb1);
461 newpsp.SetFCB2(block.exec.fcb2);
462 /* Set the stack for new program */
463 SegSet16(ss,RealSeg(sssp));reg_sp=RealOff(sssp);
464 /* Add some flags and CS:IP on the stack for the IRET */
465 CPU_Push16(RealSeg(csip));
466 CPU_Push16(RealOff(csip));
467 /* DOS starts programs with a RETF, so critical flags
468 * should not be modified (IOPL in v86 mode);
469 * interrupt flag is set explicitly, test flags cleared */
470 reg_flags=(reg_flags&(~FMASK_TEST))|FLAG_IF;
471 //Jump to retf so that we only need to store cs:ip on the stack
472 reg_ip++;
473 /* Setup the rest of the registers */
474 reg_ax=reg_bx=0;reg_cx=0xff;
475 reg_dx=pspseg;
476 reg_si=RealOff(csip);
477 reg_di=RealOff(sssp);
478 reg_bp=0x91c; /* DOS internal stack begin relict */
479 SegSet16(ds,pspseg);SegSet16(es,pspseg);
480 #if C_DEBUG
481 /* Started from debug.com, then set breakpoint at start */
482 DEBUG_CheckExecuteBreakpoint(RealSeg(csip),RealOff(csip));
483 #endif
484 /* Add the filename to PSP and environment MCB's */
485 char stripname[8]= { 0 };Bitu index=0;
486 while (char chr=*name++) {
487 switch (chr) {
488 case ':':case '\\':case '/':index=0;break;
489 default:if (index<8) stripname[index++]=(char)toupper(chr);
490 }
491 }
492 index=0;
493 while (index<8) {
494 if (stripname[index]=='.') break;
495 if (!stripname[index]) break;
496 index++;
497 }
498 memset(&stripname[index],0,8-index);
499 DOS_MCB pspmcb(dos.psp()-1);
500 pspmcb.SetFileName(stripname);
501 DOS_UpdatePSPName();
502 return true;
503 }
504 return false;
505 }