Skip to content
Snippets Groups Projects
exec.c 2.83 KiB
Newer Older
rsc's avatar
rsc committed
#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)
{
  char *s, *last;
  int i, off;
  uint sz = 0;
rsc's avatar
rsc committed
  struct elfhdr elf;
rsc's avatar
rsc committed
  struct proghdr ph;
  pde_t *pgdir = 0, *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);
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

Austin Clements's avatar
Austin Clements committed
  if(!(pgdir = setupkvm()))
rsc's avatar
rsc committed
    goto bad;

rsc's avatar
 
rsc committed
  // 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;
    if(ph.memsz < ph.filesz)
rsc's avatar
 
rsc committed
      goto bad;
    if(!(sz = allocuvm(pgdir, sz, ph.va + ph.memsz)))
Austin Clements's avatar
Austin Clements committed
    if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
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);
  if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))

  // 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

  uint sp = sz;

  // count arguments
  int argc;
  for(argc = 0; argv[argc]; argc++)
    ;
  if(argc >= MAXARG)
    goto bad;

  // push strings and remember where they are
  uint strings[MAXARG];
  for(i = argc - 1; i >= 0; --i){
    sp -= strlen(argv[i]) + 1;
    strings[i] = sp;
    copyout(pgdir, sp, argv[i], strlen(argv[i]) + 1);
  }

  // push 0 for argv[argc]
  sp -= 4;
  int zero = 0;
  copyout(pgdir, sp, &zero, 4);

  // push argv[] elements
  for(i = argc - 1; i >= 0; --i){
    sp -= 4;
    copyout(pgdir, sp, &strings[i], 4);
rsc's avatar
rsc committed
  }

  // push argv
  uint argvaddr = sp;
rsc's avatar
 
rsc committed
  sp -= 4;
  copyout(pgdir, sp, &argvaddr, 4);

  // push argc
rsc's avatar
 
rsc committed
  sp -= 4;
  copyout(pgdir, sp, &argc, 4);

  // push 0 in case main returns
rsc's avatar
 
rsc committed
  sp -= 4;
  uint ffffffff = 0xffffffff;
  copyout(pgdir, sp, &ffffffff, 4);
rsc's avatar
 
rsc committed

Robert Morris's avatar
Robert Morris committed
  if(sp < sz - PGSIZE)
    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;
}