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;
|
2010-07-02 18:51:53 +00:00
|
|
|
uint sz, sp, spoffset, 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;
|
2010-07-02 18:51:53 +00:00
|
|
|
pde_t *pgdir, *oldpgdir;
|
2007-08-21 19:22:08 +00:00
|
|
|
|
2010-07-02 18:51:53 +00:00
|
|
|
pgdir = 0;
|
2009-07-13 16:34:45 +00:00
|
|
|
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
|
|
|
|
2010-07-02 18:51:53 +00:00
|
|
|
if (!(pgdir = setupkvm()))
|
2007-08-21 19:22:08 +00:00
|
|
|
goto bad;
|
|
|
|
|
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.memsz < ph.filesz)
|
2007-08-27 15:17:40 +00:00
|
|
|
goto bad;
|
2010-07-02 18:51:53 +00:00
|
|
|
if (!allocuvm(pgdir, (char *)ph.va, ph.memsz))
|
|
|
|
goto bad;
|
|
|
|
sz += PGROUNDUP(ph.memsz);
|
|
|
|
if (!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
|
2007-08-27 15:17:40 +00:00
|
|
|
goto bad;
|
2007-08-21 19:22:08 +00:00
|
|
|
}
|
2007-08-27 15:17:40 +00:00
|
|
|
iunlockput(ip);
|
2010-07-02 18:51:53 +00:00
|
|
|
|
|
|
|
// Allocate and initialize stack at sz
|
|
|
|
if (!allocuvm(pgdir, (char *)sz, PGSIZE))
|
|
|
|
goto bad;
|
|
|
|
mem = uva2ka(pgdir, (char *)sz);
|
|
|
|
spoffset = sz;
|
|
|
|
sz += PGSIZE;
|
|
|
|
|
|
|
|
arglen = 0;
|
|
|
|
for(argc=0; argv[argc]; argc++)
|
|
|
|
arglen += strlen(argv[argc]) + 1;
|
|
|
|
arglen = (arglen+3) & ~3;
|
|
|
|
|
2007-08-27 15:17:40 +00:00
|
|
|
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.
|
2010-07-02 18:51:53 +00:00
|
|
|
*(uint*)(mem+argp-spoffset + 4*argc) = 0; // argv[argc]
|
2007-08-27 15:17:40 +00:00
|
|
|
for(i=argc-1; i>=0; i--){
|
|
|
|
len = strlen(argv[i]) + 1;
|
|
|
|
sp -= len;
|
2010-07-02 18:51:53 +00:00
|
|
|
memmove(mem+sp-spoffset, argv[i], len);
|
|
|
|
*(uint*)(mem+argp-spoffset + 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;
|
2010-07-02 18:51:53 +00:00
|
|
|
*(uint*)(mem+sp-spoffset) = argp;
|
2007-08-27 15:17:40 +00:00
|
|
|
sp -= 4;
|
2010-07-02 18:51:53 +00:00
|
|
|
*(uint*)(mem+sp-spoffset) = argc;
|
2007-08-27 15:17:40 +00:00
|
|
|
sp -= 4;
|
2010-07-02 18:51:53 +00:00
|
|
|
*(uint*)(mem+sp-spoffset) = 0xffffffff; // fake return pc
|
2007-08-27 15:17:40 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2010-07-02 18:51:53 +00:00
|
|
|
// Commit to the user image.
|
|
|
|
oldpgdir = proc->pgdir;
|
|
|
|
proc->pgdir = pgdir;
|
2009-08-31 06:02:08 +00:00
|
|
|
proc->sz = sz;
|
|
|
|
proc->tf->eip = elf.entry; // main
|
|
|
|
proc->tf->esp = sp;
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2010-07-23 11:41:13 +00:00
|
|
|
loadvm(proc);
|
2010-07-02 18:51:53 +00:00
|
|
|
|
|
|
|
freevm(oldpgdir);
|
|
|
|
|
2007-08-21 19:22:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
2010-07-23 16:52:50 +00:00
|
|
|
if (pgdir) freevm(pgdir);
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlockput(ip);
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|