root/exec.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. exec

   1 #include "types.h"
   2 #include "param.h"
   3 #include "memlayout.h"
   4 #include "mmu.h"
   5 #include "proc.h"
   6 #include "defs.h"
   7 #include "x86.h"
   8 #include "elf.h"
   9 
  10 int
  11 exec(char *path, char **argv)
     /* [previous][next][first][last][top][bottom][index][help]  */
  12 {
  13   char *s, *last;
  14   int i, off;
  15   uint argc, sz, sp, ustack[3+MAXARG+1];
  16   struct elfhdr elf;
  17   struct inode *ip;
  18   struct proghdr ph;
  19   pde_t *pgdir, *oldpgdir;
  20   struct proc *curproc = myproc();
  21 
  22   begin_op();
  23 
  24   if((ip = namei(path)) == 0){
  25     end_op();
  26     cprintf("exec: fail\n");
  27     return -1;
  28   }
  29   ilock(ip);
  30   pgdir = 0;
  31 
  32   // Check ELF header
  33   if(readi(ip, (char*)&elf, 0, sizeof(elf)) != sizeof(elf))
  34     goto bad;
  35   if(elf.magic != ELF_MAGIC)
  36     goto bad;
  37 
  38   if((pgdir = setupkvm()) == 0)
  39     goto bad;
  40 
  41   // Load program into memory.
  42   sz = 0;
  43   for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
  44     if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
  45       goto bad;
  46     if(ph.type != ELF_PROG_LOAD)
  47       continue;
  48     if(ph.memsz < ph.filesz)
  49       goto bad;
  50     if(ph.vaddr + ph.memsz < ph.vaddr)
  51       goto bad;
  52     if((sz = allocuvm(pgdir, sz, ph.vaddr + ph.memsz)) == 0)
  53       goto bad;
  54     if(ph.vaddr % PGSIZE != 0)
  55       goto bad;
  56     if(loaduvm(pgdir, (char*)ph.vaddr, ip, ph.off, ph.filesz) < 0)
  57       goto bad;
  58   }
  59   iunlockput(ip);
  60   end_op();
  61   ip = 0;
  62 
  63   // Allocate two pages at the next page boundary.
  64   // Make the first inaccessible.  Use the second as the user stack.
  65   sz = PGROUNDUP(sz);
  66   if((sz = allocuvm(pgdir, sz, sz + 2*PGSIZE)) == 0)
  67     goto bad;
  68   clearpteu(pgdir, (char*)(sz - 2*PGSIZE));
  69   sp = sz;
  70 
  71   // Push argument strings, prepare rest of stack in ustack.
  72   for(argc = 0; argv[argc]; argc++) {
  73     if(argc >= MAXARG)
  74       goto bad;
  75     sp = (sp - (strlen(argv[argc]) + 1)) & ~3;
  76     if(copyout(pgdir, sp, argv[argc], strlen(argv[argc]) + 1) < 0)
  77       goto bad;
  78     ustack[3+argc] = sp;
  79   }
  80   ustack[3+argc] = 0;
  81 
  82   ustack[0] = 0xffffffff;  // fake return PC
  83   ustack[1] = argc;
  84   ustack[2] = sp - (argc+1)*4;  // argv pointer
  85 
  86   sp -= (3+argc+1) * 4;
  87   if(copyout(pgdir, sp, ustack, (3+argc+1)*4) < 0)
  88     goto bad;
  89 
  90   // Save program name for debugging.
  91   for(last=s=path; *s; s++)
  92     if(*s == '/')
  93       last = s+1;
  94   safestrcpy(curproc->name, last, sizeof(curproc->name));
  95 
  96   // Commit to the user image.
  97   oldpgdir = curproc->pgdir;
  98   curproc->pgdir = pgdir;
  99   curproc->sz = sz;
 100   curproc->tf->eip = elf.entry;  // main
 101   curproc->tf->esp = sp;
 102   switchuvm(curproc);
 103   freevm(oldpgdir);
 104   return 0;
 105 
 106  bad:
 107   if(pgdir)
 108     freevm(pgdir);
 109   if(ip){
 110     iunlockput(ip);
 111     end_op();
 112   }
 113   return -1;
 114 }

/* [previous][next][first][last][top][bottom][index][help]  */