Skip to content
Snippets Groups Projects
exec.c 2.15 KiB
Newer Older
rsc's avatar
rsc committed
#include "types.h"
#include "param.h"
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "memlayout.h"
rsc's avatar
rsc committed
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "elf.h"

int
exec(char *path, char **argv)
{
  int i, off;
  uint argc, sz, sp, ustack[3+MAXARG+1];
rsc's avatar
rsc committed
  struct elfhdr elf;
Russ Cox's avatar
Russ Cox committed
  struct inode *ip;
rsc's avatar
rsc committed
  struct proghdr ph;
Russ Cox's avatar
Russ Cox committed
  pde_t *pgdir, *oldpgdir;
Russ Cox's avatar
Russ Cox committed

rsc's avatar
 
rsc committed
  if((ip = namei(path)) == 0)
rsc's avatar
rsc committed
    return -1;
rsc's avatar
 
rsc committed
  ilock(ip);
Russ Cox's avatar
Russ Cox committed
  pgdir = 0;
rsc's avatar
rsc committed

Russ Cox's avatar
Russ Cox committed
  // Check ELF header
rsc's avatar
rsc committed
  if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
    goto bad;
  if(elf.magic != ELF_MAGIC)
    goto bad;
Russ Cox's avatar
Russ Cox committed

rsc's avatar
rsc committed
    goto bad;

rsc's avatar
 
rsc committed
  // Load program into memory.
Russ Cox's avatar
Russ Cox committed
  sz = 0;
rsc's avatar
 
rsc committed
  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;
    if(ph.memsz < ph.filesz)
rsc's avatar
 
rsc committed
      goto bad;
    if((sz = allocuvm(pgdir, sz, ph.vaddr + ph.memsz)) == 0)
    if(loaduvm(pgdir, (char*)ph.vaddr, ip, ph.off, ph.filesz) < 0)
rsc's avatar
 
rsc committed
      goto bad;
rsc's avatar
rsc committed
  }
rsc's avatar
 
rsc committed
  iunlockput(ip);
Robert Morris's avatar
Robert Morris committed
  ip = 0;
  // Allocate a one-page stack at the next page boundary
  sz = PGROUNDUP(sz);
Russ Cox's avatar
Russ Cox committed
  if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
  // Push argument strings, prepare rest of stack in ustack.
Russ Cox's avatar
Russ Cox committed
  sp = sz;
  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;
  ustack[3+argc] = 0;
  ustack[0] = 0xffffffff;  // fake return PC
  ustack[1] = argc;
  ustack[2] = sp - (argc+1)*4;  // argv pointer
  sp -= (3+argc+1) * 4;
  if(copyout(pgdir, sp, ustack, (3+argc+1)*4) < 0)
Robert Morris's avatar
Robert Morris committed
    goto bad;

rsc's avatar
 
rsc committed
  // Save program name for debugging.
rsc's avatar
rsc committed
  for(last=s=path; *s; s++)
    if(*s == '/')
      last = s+1;
Russ Cox's avatar
Russ Cox committed
  safestrcpy(proc->name, last, sizeof(proc->name));
rsc's avatar
rsc committed

  // Commit to the user image.
  oldpgdir = proc->pgdir;
  proc->pgdir = pgdir;
Russ Cox's avatar
Russ Cox committed
  proc->sz = sz;
  proc->tf->eip = elf.entry;  // main
  proc->tf->esp = sp;
  switchuvm(proc);
rsc's avatar
rsc committed
  return 0;

 bad:
Robert Morris's avatar
Robert Morris committed
  if(pgdir)
    freevm(pgdir);
  if(ip)
    iunlockput(ip);
rsc's avatar
rsc committed
  return -1;
}