2007-08-21 19:22:08 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "elf.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
exec(char *path, char **argv)
|
|
|
|
{
|
2007-08-27 15:17:40 +00:00
|
|
|
char *mem, *s, *last;
|
|
|
|
int i, argc, arglen, len, off;
|
2009-08-31 06:02:08 +00:00
|
|
|
uint sz, sp, argp;
|
2007-08-21 19:22:08 +00:00
|
|
|
struct elfhdr elf;
|
2007-08-27 15:17:40 +00:00
|
|
|
struct inode *ip;
|
2007-08-21 19:22:08 +00:00
|
|
|
struct proghdr ph;
|
|
|
|
|
2009-07-13 16:34:45 +00:00
|
|
|
mem = 0;
|
|
|
|
sz = 0;
|
|
|
|
|
2007-08-24 20:54:23 +00:00
|
|
|
if((ip = namei(path)) == 0)
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(ip);
|
2007-08-21 19:22:08 +00:00
|
|
|
|
2009-07-13 16:34:45 +00:00
|
|
|
// Check ELF header
|
2007-08-21 19:22:08 +00:00
|
|
|
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
|
|
|
|
goto bad;
|
|
|
|
if(elf.magic != ELF_MAGIC)
|
|
|
|
goto bad;
|
2009-07-13 16:34:45 +00:00
|
|
|
|
|
|
|
// Compute memory size of new process.
|
|
|
|
// Program segments.
|
2007-08-22 06:01:32 +00:00
|
|
|
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
|
|
|
|
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
|
2007-08-21 19:22:08 +00:00
|
|
|
goto bad;
|
|
|
|
if(ph.type != ELF_PROG_LOAD)
|
|
|
|
continue;
|
|
|
|
if(ph.memsz < ph.filesz)
|
|
|
|
goto bad;
|
|
|
|
sz += ph.memsz;
|
|
|
|
}
|
2007-08-27 15:17:40 +00:00
|
|
|
|
|
|
|
// Arguments.
|
|
|
|
arglen = 0;
|
|
|
|
for(argc=0; argv[argc]; argc++)
|
2007-08-30 18:19:52 +00:00
|
|
|
arglen += strlen(argv[argc]) + 1;
|
2007-08-27 15:17:40 +00:00
|
|
|
arglen = (arglen+3) & ~3;
|
2009-07-13 16:34:45 +00:00
|
|
|
sz += arglen;
|
|
|
|
sz += 4*(argc+1); // argv data
|
|
|
|
sz += 4; // argv
|
|
|
|
sz += 4; // argc
|
2007-08-27 15:17:40 +00:00
|
|
|
|
|
|
|
// Stack.
|
|
|
|
sz += PAGE;
|
|
|
|
|
|
|
|
// Allocate program memory.
|
|
|
|
sz = (sz+PAGE-1) & ~(PAGE-1);
|
2007-08-21 19:22:08 +00:00
|
|
|
mem = kalloc(sz);
|
|
|
|
if(mem == 0)
|
|
|
|
goto bad;
|
|
|
|
memset(mem, 0, sz);
|
|
|
|
|
2007-08-27 15:17:40 +00:00
|
|
|
// Load program into memory.
|
|
|
|
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
|
|
|
|
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
|
|
|
|
goto bad;
|
|
|
|
if(ph.type != ELF_PROG_LOAD)
|
|
|
|
continue;
|
2009-08-08 08:07:30 +00:00
|
|
|
if(ph.va + ph.memsz < ph.va || ph.va + ph.memsz > sz)
|
|
|
|
goto bad;
|
|
|
|
if(ph.memsz < ph.filesz)
|
2007-08-27 15:17:40 +00:00
|
|
|
goto bad;
|
|
|
|
if(readi(ip, mem + ph.va, ph.offset, ph.filesz) != ph.filesz)
|
|
|
|
goto bad;
|
|
|
|
memset(mem + ph.va + ph.filesz, 0, ph.memsz - ph.filesz);
|
2007-08-21 19:22:08 +00:00
|
|
|
}
|
2007-08-27 15:17:40 +00:00
|
|
|
iunlockput(ip);
|
|
|
|
|
|
|
|
// Initialize stack.
|
|
|
|
sp = sz;
|
2007-08-28 02:39:40 +00:00
|
|
|
argp = sz - arglen - 4*(argc+1);
|
2007-08-27 15:17:40 +00:00
|
|
|
|
|
|
|
// Copy argv strings and pointers to stack.
|
|
|
|
*(uint*)(mem+argp + 4*argc) = 0; // argv[argc]
|
|
|
|
for(i=argc-1; i>=0; i--){
|
|
|
|
len = strlen(argv[i]) + 1;
|
|
|
|
sp -= len;
|
|
|
|
memmove(mem+sp, argv[i], len);
|
|
|
|
*(uint*)(mem+argp + 4*i) = sp; // argv[i]
|
2007-08-21 19:22:08 +00:00
|
|
|
}
|
|
|
|
|
2007-08-27 15:17:40 +00:00
|
|
|
// Stack frame for main(argc, argv), below arguments.
|
|
|
|
sp = argp;
|
|
|
|
sp -= 4;
|
|
|
|
*(uint*)(mem+sp) = argp;
|
|
|
|
sp -= 4;
|
|
|
|
*(uint*)(mem+sp) = argc;
|
|
|
|
sp -= 4;
|
|
|
|
*(uint*)(mem+sp) = 0xffffffff; // fake return pc
|
|
|
|
|
|
|
|
// Save program name for debugging.
|
2007-08-21 19:22:08 +00:00
|
|
|
for(last=s=path; *s; s++)
|
|
|
|
if(*s == '/')
|
|
|
|
last = s+1;
|
2009-08-31 06:02:08 +00:00
|
|
|
safestrcpy(proc->name, last, sizeof(proc->name));
|
2007-08-21 19:22:08 +00:00
|
|
|
|
2007-08-27 15:17:40 +00:00
|
|
|
// Commit to the new image.
|
2009-08-31 06:02:08 +00:00
|
|
|
kfree(proc->mem, proc->sz);
|
|
|
|
proc->mem = mem;
|
|
|
|
proc->sz = sz;
|
|
|
|
proc->tf->eip = elf.entry; // main
|
|
|
|
proc->tf->esp = sp;
|
2009-05-31 00:28:45 +00:00
|
|
|
usegment();
|
2007-08-21 19:22:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if(mem)
|
|
|
|
kfree(mem, sz);
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|