2007-08-21 19:22:08 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "param.h"
|
2011-07-29 11:31:27 +00:00
|
|
|
#include "memlayout.h"
|
2007-08-21 19:22:08 +00:00
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "elf.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
exec(char *path, char **argv)
|
|
|
|
{
|
2010-09-27 20:14:33 +00:00
|
|
|
char *s, *last;
|
2011-02-20 02:17:55 +00:00
|
|
|
int i, off;
|
|
|
|
uint argc, sz, sp, ustack[3+MAXARG+1];
|
2007-08-21 19:22:08 +00:00
|
|
|
struct elfhdr elf;
|
2011-01-11 18:01:13 +00:00
|
|
|
struct inode *ip;
|
2007-08-21 19:22:08 +00:00
|
|
|
struct proghdr ph;
|
2011-01-11 18:01:13 +00:00
|
|
|
pde_t *pgdir, *oldpgdir;
|
2009-07-13 16:34:45 +00:00
|
|
|
|
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);
|
2011-01-11 18:01:13 +00:00
|
|
|
pgdir = 0;
|
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
|
|
|
|
2011-08-10 01:37:35 +00:00
|
|
|
if((pgdir = setupkvm(kalloc)) == 0)
|
2007-08-21 19:22:08 +00:00
|
|
|
goto bad;
|
|
|
|
|
2007-08-27 15:17:40 +00:00
|
|
|
// Load program into memory.
|
2011-01-11 18:01:13 +00:00
|
|
|
sz = 0;
|
2007-08-27 15:17:40 +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))
|
|
|
|
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;
|
2011-08-18 00:23:36 +00:00
|
|
|
if((sz = allocuvm(pgdir, sz, ph.vaddr + ph.memsz)) == 0)
|
2010-07-02 18:51:53 +00:00
|
|
|
goto bad;
|
2011-08-18 00:23:36 +00:00
|
|
|
if(loaduvm(pgdir, (char*)ph.vaddr, ip, ph.off, ph.filesz) < 0)
|
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-09-27 20:17:57 +00:00
|
|
|
ip = 0;
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2011-09-01 17:25:34 +00:00
|
|
|
// Allocate two pages at the next page boundary.
|
|
|
|
// Make the first inaccessible.
|
|
|
|
// Use the second as the user stack.
|
2010-09-27 20:14:33 +00:00
|
|
|
sz = PGROUNDUP(sz);
|
2011-09-01 17:25:34 +00:00
|
|
|
if((sz = allocuvm(pgdir, sz, sz + 2*PGSIZE)) == 0)
|
2010-07-02 18:51:53 +00:00
|
|
|
goto bad;
|
2011-09-02 18:37:04 +00:00
|
|
|
clearpteu(pgdir, (char*)(sz - 2*PGSIZE));
|
2011-09-01 17:25:34 +00:00
|
|
|
sp = sz;
|
2010-09-27 20:14:33 +00:00
|
|
|
|
2011-02-20 02:17:55 +00:00
|
|
|
// Push argument strings, prepare rest of stack in ustack.
|
|
|
|
for(argc = 0; argv[argc]; argc++) {
|
|
|
|
if(argc >= MAXARG)
|
|
|
|
goto bad;
|
|
|
|
sp -= strlen(argv[argc]) + 1;
|
|
|
|
sp &= ~3;
|
|
|
|
if(copyout(pgdir, sp, argv[argc], strlen(argv[argc]) + 1) < 0)
|
|
|
|
goto bad;
|
|
|
|
ustack[3+argc] = sp;
|
2010-09-27 20:14:33 +00:00
|
|
|
}
|
2011-02-20 02:17:55 +00:00
|
|
|
ustack[3+argc] = 0;
|
2010-09-27 20:14:33 +00:00
|
|
|
|
2011-02-20 02:17:55 +00:00
|
|
|
ustack[0] = 0xffffffff; // fake return PC
|
|
|
|
ustack[1] = argc;
|
|
|
|
ustack[2] = sp - (argc+1)*4; // argv pointer
|
2010-09-29 18:12:26 +00:00
|
|
|
|
2011-02-20 02:17:55 +00:00
|
|
|
sp -= (3+argc+1) * 4;
|
|
|
|
if(copyout(pgdir, sp, ustack, (3+argc+1)*4) < 0)
|
2010-09-27 20:17:57 +00:00
|
|
|
goto bad;
|
|
|
|
|
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;
|
2011-02-20 02:17:55 +00:00
|
|
|
switchuvm(proc);
|
2010-07-02 18:51:53 +00:00
|
|
|
freevm(oldpgdir);
|
|
|
|
|
2007-08-21 19:22:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
2010-09-27 20:17:57 +00:00
|
|
|
if(pgdir)
|
|
|
|
freevm(pgdir);
|
|
|
|
if(ip)
|
|
|
|
iunlockput(ip);
|
2007-08-21 19:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|