-
Notifications
You must be signed in to change notification settings - Fork 10
/
exec.c
93 lines (84 loc) · 2.07 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
#include <sys/types.h>
#include <xv6/param.h>
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "gaia.h"
int
exec(char *path, char **argv)
{
char *s, *last;
int i, off;
uint argc, sz, sp, ustack[3+MAXARG+1];
int program_size;
struct inode *ip;
pde_t *pgdir, *oldpgdir;
begin_op();
if((ip = namei(path)) == 0){
end_op();
return -1;
}
ilock(ip);
pgdir = 0;
// read program header.
if(readi(ip, (char*)&program_size, 0, sizeof(int)) < sizeof(int))
goto bad;
if((pgdir = setupkvm()) == 0)
goto bad;
// Load program into memory.
off = HEADER_SIZE;
if((sz = allocuvm(pgdir, 0, program_size)) == 0)
goto bad;
if(loaduvm(pgdir, (char*)0, ip, HEADER_SIZE, program_size) < 0)
goto bad;
iunlockput(ip);
end_op();
ip = 0;
// Allocate two pages at the next page boundary.
// Make the first inaccessible. Use the second as the user stack.
sz = PGROUNDUP(sz);
if((sz = allocuvm(pgdir, sz, sz + 2*PGSIZE)) == 0)
goto bad;
clearpteu(pgdir, (char*)(sz - 2*PGSIZE));
sp = sz;
// 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[2] = sp - (argc+1)*4; // argv pointer
ustack[1] = argc;
ustack[0] = sp -= (3+argc+1) * 4; // rbp = rsp
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(proc->name, last, sizeof(proc->name));
// Commit to the user image.
oldpgdir = proc->pgdir;
proc->pgdir = pgdir;
proc->sz = sz;
proc->tf->retaddr = 0; // entry point of user programs
proc->tf->r30 = sp;
proc->tf->r31 = sp;
switchuvm(proc);
freevm(oldpgdir);
return 0;
bad:
cprintf("exec bad\n");
if(pgdir)
freevm(pgdir);
if(ip){
iunlockput(ip);
end_op();
}
return -1;
}