2007-08-21 21:22:08 +02: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)
|
|
|
|
{
|
2010-09-27 22:14:33 +02:00
|
|
|
char *s, *last;
|
2011-01-11 19:01:13 +01:00
|
|
|
int i, off, argc;
|
|
|
|
uint sz, sp, strings[MAXARG];
|
2007-08-21 21:22:08 +02:00
|
|
|
struct elfhdr elf;
|
2011-01-11 19:01:13 +01:00
|
|
|
struct inode *ip;
|
2007-08-21 21:22:08 +02:00
|
|
|
struct proghdr ph;
|
2011-01-11 19:01:13 +01:00
|
|
|
pde_t *pgdir, *oldpgdir;
|
2009-07-13 18:34:45 +02:00
|
|
|
|
2007-08-24 22:54:23 +02:00
|
|
|
if((ip = namei(path)) == 0)
|
2007-08-21 21:22:08 +02:00
|
|
|
return -1;
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(ip);
|
2011-01-11 19:01:13 +01:00
|
|
|
pgdir = 0;
|
2007-08-21 21:22:08 +02:00
|
|
|
|
2009-07-13 18:34:45 +02:00
|
|
|
// Check ELF header
|
2007-08-21 21:22:08 +02:00
|
|
|
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
|
|
|
|
goto bad;
|
|
|
|
if(elf.magic != ELF_MAGIC)
|
|
|
|
goto bad;
|
2009-07-13 18:34:45 +02:00
|
|
|
|
2011-01-11 19:01:13 +01:00
|
|
|
if((pgdir = setupkvm()) == 0)
|
2007-08-21 21:22:08 +02:00
|
|
|
goto bad;
|
|
|
|
|
2007-08-27 17:17:40 +02:00
|
|
|
// Load program into memory.
|
2011-01-11 19:01:13 +01:00
|
|
|
sz = 0;
|
2007-08-27 17:17:40 +02: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 10:07:30 +02:00
|
|
|
if(ph.memsz < ph.filesz)
|
2007-08-27 17:17:40 +02:00
|
|
|
goto bad;
|
2011-01-11 19:01:13 +01:00
|
|
|
if((sz = allocuvm(pgdir, sz, ph.va + ph.memsz)) == 0)
|
2010-07-02 20:51:53 +02:00
|
|
|
goto bad;
|
2011-01-11 19:01:13 +01:00
|
|
|
if(loaduvm(pgdir, (char*)ph.va, ip, ph.offset, ph.filesz) < 0)
|
2007-08-27 17:17:40 +02:00
|
|
|
goto bad;
|
2007-08-21 21:22:08 +02:00
|
|
|
}
|
2007-08-27 17:17:40 +02:00
|
|
|
iunlockput(ip);
|
2010-09-27 22:17:57 +02:00
|
|
|
ip = 0;
|
2010-07-02 20:51:53 +02:00
|
|
|
|
2010-09-27 22:14:33 +02:00
|
|
|
// Allocate a one-page stack at the next page boundary
|
|
|
|
sz = PGROUNDUP(sz);
|
2011-01-11 19:01:13 +01:00
|
|
|
if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
|
2010-07-02 20:51:53 +02:00
|
|
|
goto bad;
|
2010-09-27 22:14:33 +02:00
|
|
|
|
|
|
|
// initialize stack content:
|
|
|
|
|
|
|
|
// "argumentN" -- nul-terminated string
|
|
|
|
// ...
|
|
|
|
// "argument0"
|
|
|
|
// 0 -- argv[argc]
|
|
|
|
// address of argumentN
|
|
|
|
// ...
|
|
|
|
// address of argument0 -- argv[0]
|
|
|
|
// address of address of argument0 -- argv argument to main()
|
|
|
|
// argc -- argc argument to main()
|
|
|
|
// ffffffff -- return PC for main() call
|
|
|
|
|
2011-01-11 19:01:13 +01:00
|
|
|
sp = sz;
|
2010-09-27 22:14:33 +02:00
|
|
|
|
|
|
|
// count arguments
|
|
|
|
for(argc = 0; argv[argc]; argc++)
|
|
|
|
;
|
|
|
|
if(argc >= MAXARG)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
// push strings and remember where they are
|
|
|
|
for(i = argc - 1; i >= 0; --i){
|
|
|
|
sp -= strlen(argv[i]) + 1;
|
|
|
|
strings[i] = sp;
|
|
|
|
copyout(pgdir, sp, argv[i], strlen(argv[i]) + 1);
|
|
|
|
}
|
|
|
|
|
2011-01-11 19:01:13 +01:00
|
|
|
#define PUSH(x){ int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
|
2010-09-29 20:12:26 +02:00
|
|
|
|
|
|
|
PUSH(0); // argv[argc] is zero
|
2010-09-27 22:14:33 +02:00
|
|
|
|
|
|
|
// push argv[] elements
|
2010-09-29 20:12:26 +02:00
|
|
|
for(i = argc - 1; i >= 0; --i)
|
|
|
|
PUSH(strings[i]);
|
2007-08-21 21:22:08 +02:00
|
|
|
|
2010-09-29 20:12:26 +02:00
|
|
|
PUSH(sp); // argv
|
2010-09-27 22:14:33 +02:00
|
|
|
|
2010-09-29 20:12:26 +02:00
|
|
|
PUSH(argc);
|
2010-09-27 22:14:33 +02:00
|
|
|
|
2010-09-29 20:12:26 +02:00
|
|
|
PUSH(0xffffffff); // in case main tries to return
|
2007-08-27 17:17:40 +02:00
|
|
|
|
2010-09-27 22:17:57 +02:00
|
|
|
if(sp < sz - PGSIZE)
|
|
|
|
goto bad;
|
|
|
|
|
2007-08-27 17:17:40 +02:00
|
|
|
// Save program name for debugging.
|
2007-08-21 21:22:08 +02:00
|
|
|
for(last=s=path; *s; s++)
|
|
|
|
if(*s == '/')
|
|
|
|
last = s+1;
|
2009-08-31 08:02:08 +02:00
|
|
|
safestrcpy(proc->name, last, sizeof(proc->name));
|
2007-08-21 21:22:08 +02:00
|
|
|
|
2010-07-02 20:51:53 +02:00
|
|
|
// Commit to the user image.
|
|
|
|
oldpgdir = proc->pgdir;
|
|
|
|
proc->pgdir = pgdir;
|
2009-08-31 08:02:08 +02:00
|
|
|
proc->sz = sz;
|
|
|
|
proc->tf->eip = elf.entry; // main
|
|
|
|
proc->tf->esp = sp;
|
2010-07-02 20:51:53 +02:00
|
|
|
|
2010-08-06 17:12:18 +02:00
|
|
|
switchuvm(proc);
|
2010-07-02 20:51:53 +02:00
|
|
|
|
|
|
|
freevm(oldpgdir);
|
|
|
|
|
2007-08-21 21:22:08 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
2010-09-27 22:17:57 +02:00
|
|
|
if(pgdir)
|
|
|
|
freevm(pgdir);
|
|
|
|
if(ip)
|
|
|
|
iunlockput(ip);
|
2007-08-21 21:22:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|