forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
126 lines (102 loc) · 2.66 KB
/
exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#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, argc;
uint sz, sp, strings[MAXARG];
struct elfhdr elf;
struct inode *ip;
struct proghdr ph;
pde_t *pgdir, *oldpgdir;
if((ip = namei(path)) == 0)
return -1;
ilock(ip);
pgdir = 0;
// Check ELF header
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
goto bad;
if(elf.magic != ELF_MAGIC)
goto bad;
if((pgdir = setupkvm()) == 0)
goto bad;
// Load program into memory.
sz = 0;
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)
goto bad;
if((sz = allocuvm(pgdir, sz, ph.va + ph.memsz)) == 0)
goto bad;
if(loaduvm(pgdir, (char*)ph.va, ip, ph.offset, ph.filesz) < 0)
goto bad;
}
iunlockput(ip);
ip = 0;
// Allocate a one-page stack at the next page boundary
sz = PGROUNDUP(sz);
if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
goto bad;
// 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
sp = sz;
// 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);
}
#define PUSH(x){ int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
PUSH(0); // argv[argc] is zero
// push argv[] elements
for(i = argc - 1; i >= 0; --i)
PUSH(strings[i]);
PUSH(sp); // argv
PUSH(argc);
PUSH(0xffffffff); // in case main tries to return
if(sp < sz - PGSIZE)
goto bad;
// Save program name for debugging.
for(last=s=path; *s; s++)
if(*s == '/')
last = s+1;
safestrcpy(proc->name, last, sizeof(proc->name));
// Commit to the user image.
oldpgdir = proc->pgdir;
proc->pgdir = pgdir;
proc->sz = sz;
proc->tf->eip = elf.entry; // main
proc->tf->esp = sp;
switchuvm(proc);
freevm(oldpgdir);
return 0;
bad:
if(pgdir)
freevm(pgdir);
if(ip)
iunlockput(ip);
return -1;
}