-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.c
147 lines (130 loc) · 3.23 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
#include "types.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "sh4.h"
#include "elf.h"
int
exec(char *path, char **argv)
{
#ifdef DEBUG
cprintf("%s: path=%s\n", __func__, path);
char **tmp;
for(tmp=argv; *tmp != 0; ++tmp) {
cprintf("%s: argv[%d]=%s\n", __func__, tmp-argv, *tmp);
}
#endif
#ifdef DEBUGxxx
cprintf("%s: oldpgdir=0x%x\n", __func__, proc->pgdir);
dump_pde(proc->pgdir, 0, 2);
#endif
char *mem, *s, *last;
int i, argc, arglen, len, off;
uint sz, sp, spbottom, argp;
struct elfhdr elf;
struct inode *ip;
struct proghdr ph;
pde_t *pgdir, *oldpgdir;
pgdir = 0;
sz = 0;
if((ip = namei(path)) == 0)
return -1;
ilock(ip);
// 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()))
goto bad;
#ifdef DEBUG
cprintf("%s: start of loading\n", __func__);
#endif
// Load program into memory.
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)))
goto bad;
if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
goto bad;
}
iunlockput(ip);
#ifdef DEBUG
cprintf("%s: end of loading\n", __func__);
#endif
// Allocate and initialize stack at sz
sz = spbottom = PGROUNDUP(sz);
if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))
goto bad;
mem = uva2ka(pgdir, (char *)spbottom);
#ifdef DEBUGxxx
cprintf("%s: allocated user stack\n", __func__);
dump_pgd(pgdir, 2);
#endif
arglen = 0;
for(argc=0; argv[argc]; argc++)
arglen += strlen(argv[argc]) + 1;
arglen = (arglen+3) & ~3;
sp = sz;
argp = sz - arglen - 4*(argc+1);
// Copy argv strings and pointers to stack.
*(uint*)(mem+argp-spbottom + 4*argc) = 0; // argv[argc]
for(i=argc-1; i>=0; i--){
len = strlen(argv[i]) + 1;
sp -= len;
memmove(mem+sp-spbottom, argv[i], len);
*(uint*)(mem+argp-spbottom + 4*i) = sp; // argv[i]
}
// Stack frame for main(argc, argv), below arguments.
#if 0
sp = argp;
sp -= 4;
*(uint*)(mem+sp-spbottom) = argp;
sp -= 4;
*(uint*)(mem+sp-spbottom) = argc;
sp -= 4;
*(uint*)(mem+sp-spbottom) = 0xffffffff; // fake return pc
#else
sp = argp;
uint fakeret = 0xffffffff;
asm volatile("ldc %0, r4_bank" :: "r"(argc));
asm volatile("ldc %0, r5_bank" :: "r"(argp));
asm volatile("lds %0, pr" :: "r"(fakeret)); // fake return pc
#endif
// 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;
#if 0
proc->tf->eip = elf.entry; // main
proc->tf->esp = sp;
#else
proc->tf->spc = elf.entry;
proc->tf->sgr = sp;
#ifdef DEBUG
cprintf("%s: spc=0x%x, sgr=0x%x\n",
__func__, proc->tf->spc, proc->tf->sgr);
#endif
#endif
switchuvm(proc);
freevm(oldpgdir);
#ifdef DEBUG
cprintf("%s: finish\n", __func__);
#endif
return 0;
bad:
if(pgdir) freevm(pgdir);
iunlockput(ip);
return -1;
}