forked from 48ca/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.c
167 lines (142 loc) · 3.89 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
#include "types.h"
#include "param.h"
#include "memlayout.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, firstindex;
uint numtextpage;
uint argc, sz, sp, ustack[3+MAXARG+1];
uint vaddr;
struct elfhdr elf;
struct inode *ip;
struct proghdr ph;
struct sechdr sh, strtab;
pde_t *pgdir, *oldpgdir;
struct proc *curproc = myproc();
char names[512];
uint textaddr = 0;
uint textsz;
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;
// skip allocating the initial page
sz = PGSIZE;
// load string table header into strtab
if (readi(ip, (char *)&strtab, elf.shoff + elf.shstrndx * sizeof(sh), sizeof(sh)) != sizeof(sh))
goto bad;
// read in section names
if (readi(ip, names, strtab.sh_offset, strtab.sh_size)<0)
goto bad;
// check section headers for address of the text section
for (i=0, off=elf.shoff; i<elf.shnum; i++, off+= sizeof(sh)){
if (readi(ip, (char*) &sh, off, sizeof(sh)) != sizeof(sh))
goto bad;
firstindex = (i==0) ? sh.sh_name + 1 : sh.sh_name;
if (strncmp(names + firstindex * sizeof(char), ".text", 5) == 0) {
textaddr = sh.sh_addr;
textsz = sh.sh_size;
}
}
// text not found or at wrong address
if (textaddr != 0x1000)
goto bad;
// number of pages that are text only
numtextpage = (textsz - (textsz % PGSIZE)) / PGSIZE;
// this reads the whole program into memory
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
// read the current prog reader into ph
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
goto bad;
if(ph.type != ELF_PROG_LOAD)
continue;
vaddr = ph.vaddr;
if(ph.memsz < ph.filesz)
goto bad;
if(vaddr + ph.memsz < vaddr)
goto bad;
if((sz = allocuvm(pgdir, sz, vaddr + ph.memsz)) == 0)
goto bad;
if(vaddr % PGSIZE != 0)
goto bad;
if(loaduvm(pgdir, (char*)vaddr, ip, ph.off, ph.filesz) < 0)
goto bad;
}
iunlockput(ip);
end_op();
ip = 0;
sz = PGROUNDUP(sz);
// Allocate two pages at the next page boundary.
// Make the first inaccessible. Use the second as the user stack.
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[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->sz = sz;
curproc->tf->eip = elf.entry; // main
curproc->tf->esp = sp;
// start the clock ticks fresh for user program
curproc->ticks = 0;
acquire(&tickslock);
curproc->starttick = ticks;
release(&tickslock);
switchuvm(curproc);
// turn on read-only protection for pure text pages
if (mprotect((void *)PGSIZE, numtextpage) < 0) {
goto bad;
}
// make sure all changes seen by hw
lcr3(V2P(curproc->pgdir));
freevm(oldpgdir);
return 0;
bad:
if(pgdir)
freevm(pgdir);
if(ip){
iunlockput(ip);
end_op();
}
return -1;
}