forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexec.c
233 lines (196 loc) · 5.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "elf.h"
extern char _binary_lua_start[], _binary_lua_end[];
void kmemcpy(char *src, char *dest, uint n) {
while (n != 0) {
*(dest++) = *(src++);
n--;
}
}
int exec_strcmp(char *src, char *dest) {
while (*src == *dest) {
if (*src == 0) {
return 0;
}
src++;
dest++;
}
return (*(unsigned char *) src - *(unsigned char *) dest);
}
int readm(char *src, char *dest, uint off, uint n, const char *end) {
if (src + off > end) {
return -1;
}
if (off + n < off) {
return -1;
}
if (src + off + n > end) {
n = end - src - off;
}
kmemcpy(src + off, dest, n);
return n;
}
int load_from_kernel(char code_start[], char code_end[], pde_t **ppgdir, uint *psz, struct proghdr *pph,
struct elfhdr *pelf) {
if (!code_start) {
cprintf("exec: fail\n");
return -1;
}
*ppgdir = 0;
if (readm(code_start, (char *) pelf, 0, sizeof(*pelf), code_end) != sizeof(*pelf)) {
return -1;
}
if (pelf->magic != ELF_MAGIC) {
return -1;
}
if ((*ppgdir = setupkvm()) == 0) {
return -1;
}
*psz = 0;
for (int i = 0, off = pelf->phoff; i < pelf->phnum; i++, off += sizeof(*pph)) {
if (readm(code_start, (char *) pph, off, sizeof(*pph), code_end) != sizeof(*pph)) {
return -1;
}
if (pph->type != ELF_PROG_LOAD) {
continue;
}
if (pph->memsz < pph->filesz) {
return -1;
}
if (pph->vaddr + pph->memsz < pph->vaddr) {
return -1;
}
if ((*psz = (uint) allocuvm(*ppgdir, *psz, pph->vaddr + pph->memsz)) == 0) {
return -1;
}
if (pph->vaddr % PGSIZE != 0) {
return -1;
}
if (loaduvm_from_kernel(*ppgdir, (char *) pph->vaddr, code_start, code_end,
pph->off, pph->filesz) < 0) {
return -1;
}
}
return 0;
}
int
exec(char *path, char **argv)
{
char *s, *last;
int i, off;
uint argc, sz, sp, ustack[3+MAXARG+1];
struct elfhdr elf;
struct inode *ip;
struct proghdr ph;
pde_t *pgdir, *oldpgdir;
struct proc *curproc = myproc();
if (exec_strcmp(path, "lua") == 0) {
ip = 0;
if (load_from_kernel(_binary_lua_start, _binary_lua_end, &pgdir, &sz, &ph, &elf) < 0) {
goto bad;
}
} else {
begin_op();
if((ip = namei(path)) == 0){
end_op();
cprintf("exec: fail\n");
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;
// Save data for swapping, restore them later.
// Normalily we can just clear them, but if exec failed,
// we need to be able to restore.
memstab_clear(curproc);
swapstab_clear(curproc);
//todo Need a mechanism to save the swap table and restore it if exec fails.
// Load program into memory.
sz = PGSIZE;
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(ph.vaddr + ph.memsz < ph.vaddr)
goto bad;
if((sz = allocuvm(pgdir, sz, ph.vaddr + ph.memsz)) == 0)
goto bad;
if(ph.vaddr % PGSIZE != 0)
goto bad;
if(loaduvm(pgdir, (char*)ph.vaddr, ip, ph.off, ph.filesz) < 0)
goto bad;
}
iunlockput(ip);
end_op();
ip = 0;
}
// Set sz to heap bottom.
sz = PGROUNDUP(sz);
// Setup the top page for stack.
curproc->stack_grow = 1;
if (allocuvm(pgdir, USERTOP - PGSIZE, USERTOP) == 0)
goto bad;
curproc->stack_grow = 0;
sp = USERTOP;
// Push argument strings, prepare rest of stack in ustack.
for(argc = 0; argv[argc]; argc++) {
if(argc >= MAXARG)
goto bad;
sp = (sp - (strlen(argv[argc]) + 1)) & ~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)
goto bad;
// Save program name for debugging.
for(last=s=path; *s; s++)
if(*s == '/')
last = s+1;
safestrcpy(curproc->name, last, sizeof(curproc->name));
// Commit to the user image.
oldpgdir = curproc->pgdir;
curproc->pgdir = pgdir;
curproc->stack_size = PGSIZE;
curproc->sz = sz;
curproc->tf->eip = elf.entry; // main
curproc->tf->esp = sp;
// Refresh swapfile.
swapdealloc(curproc);
swapalloc(curproc);
switchuvm(curproc);
freevm(oldpgdir);
return 0;
bad:
if(pgdir)
freevm(pgdir);
if(ip){
iunlockput(ip);
end_op();
}
// Save data for swapping, restore them later.
// Normalily we can just clear them, but if exec failed,
// we need to be able to restore.
//todo Need a mechanism to save the swap table and restore it if exec fails.
return -1;
}