-
Notifications
You must be signed in to change notification settings - Fork 2
/
k-proc.cc
306 lines (253 loc) · 8.58 KB
/
k-proc.cc
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "kernel.hh"
#include "elf.h"
#include "k-vmiter.hh"
#include "k-devices.hh"
#include "k-chkfs.hh"
proc* ptable[NPROC]; // array of thread pointers
proc* true_ptable[NPROC]; // array of process descriptor pointers
spinlock ptable_lock; // protects ptable, true_ptable, pid_, ppid_,
// and children_
// proc::proc()
// The constructor initializes the `proc` to empty.
proc::proc()
: pid_(0), regs_(nullptr), yields_(nullptr),
state_(blank), pagetable_(nullptr), interrupted_(false), exiting_(false),
malloc_top_(0x4000000) {
}
// kalloc_proc()
// Allocate and return a new `proc`. Calls the constructor.
proc* kalloc_proc() {
void* ptr;
if (sizeof(proc) <= PAGESIZE) {
ptr = kallocpage();
} else {
ptr = kalloc(sizeof(proc));
}
if (ptr) {
return new (ptr) proc;
} else {
return nullptr;
}
}
// helper function to print a proc state
static const char* sstring_null = "NULL";
static const char* sstring_blank = "BLANK";
static const char* sstring_broken = "BROKEN";
static const char* sstring_blocked = "BLOCKED";
static const char* sstring_runnable = "RUNNABLE";
static const char* sstring_unknown = "UNKNOWN";
const char* state_string(const proc* p) {
if (!p) return sstring_null;
switch (p->state_) {
case proc::blank: return sstring_blank;
case proc::broken: return sstring_broken;
case proc::blocked: return sstring_blocked;
case proc::runnable: return sstring_runnable;
default: return sstring_unknown;
}
}
// proc::init_user(pid, pt)
// Initialize this `proc` as a new runnable user process with PID `pid`
// and initial page table `pt`.
void proc::init_user(pid_t pid, x86_64_pagetable* pt) {
uintptr_t addr = reinterpret_cast<uintptr_t>(this);
assert(!(addr & PAGEOFFMASK));
// ensure layout `k-exception.S` expects
assert(reinterpret_cast<uintptr_t>(&pid_) == addr);
assert(reinterpret_cast<uintptr_t>(®s_) == addr + 8);
assert(reinterpret_cast<uintptr_t>(&yields_) == addr + 16);
// ensure initialized page table
assert(!(reinterpret_cast<uintptr_t>(pt) & PAGEOFFMASK));
assert(pt->entry[256] == early_pagetable->entry[256]);
assert(pt->entry[510] == early_pagetable->entry[510]);
assert(pt->entry[511] == early_pagetable->entry[511]);
pid_ = pid;
true_pid_ = pid;
canary_ = canary_value;
regs_ = reinterpret_cast<regstate*>(addr + KTASKSTACK_SIZE) - 1;
memset(regs_, 0, sizeof(regstate));
regs_->reg_cs = SEGSEL_APP_CODE | 3;
regs_->reg_fs = SEGSEL_APP_DATA | 3;
regs_->reg_gs = SEGSEL_APP_DATA | 3;
regs_->reg_ss = SEGSEL_APP_DATA | 3;
regs_->reg_rflags = EFLAGS_IF;
yields_ = nullptr;
state_ = proc::runnable;
pagetable_ = pt;
}
// proc::init_kernel(pid)
// Initialize this `proc` as a new kernel process with PID `pid`,
// starting at function `f`.
void proc::init_kernel(pid_t pid, void (*f)(proc*)) {
uintptr_t addr = reinterpret_cast<uintptr_t>(this);
assert(!(addr & PAGEOFFMASK));
pid_ = pid;
true_pid_ = pid;
canary_ = canary_value;
regs_ = reinterpret_cast<regstate*>(addr + KTASKSTACK_SIZE) - 1;
memset(regs_, 0, sizeof(regstate));
regs_->reg_cs = SEGSEL_KERN_CODE;
regs_->reg_fs = SEGSEL_KERN_DATA;
regs_->reg_gs = SEGSEL_KERN_DATA;
regs_->reg_ss = SEGSEL_KERN_DATA;
regs_->reg_rflags = EFLAGS_IF;
regs_->reg_rsp = addr + KTASKSTACK_SIZE;
regs_->reg_rip = reinterpret_cast<uintptr_t>(f);
regs_->reg_rdi = addr;
yields_ = nullptr;
state_ = proc::runnable;
pagetable_ = early_pagetable;
}
// PROCESS LOADING FUNCTIONS
namespace {
struct memfile_loader : public proc::loader {
memfile* mf_;
ssize_t get_page(uint8_t** pg, size_t off) override;
void put_page(uint8_t* pg) override;
};
// loader::get_page(pg, off)
// Load a page at offset `off`, which is page-aligned. Set `*pg`
// to the address of the loaded page and return the number of
// valid bytes in that page, or negative on error.
ssize_t memfile_loader::get_page(uint8_t** pg, size_t off) {
if (!mf_) {
*pg = nullptr;
return E_NOENT;
} else if (off >= mf_->len_) {
*pg = nullptr;
return 0;
} else {
*pg = mf_->data_ + off;
return mf_->len_ - off;
}
}
// loader::put_page(pg)
// Called to indicate that `proc::load` is done with `pg`,
// which is a page returned by a previous call to `get_page`.
void memfile_loader::put_page(uint8_t* pg) {
}
}
ssize_t disk_loader::get_page(uint8_t** pg, size_t off) {
auto buf = reinterpret_cast<uint8_t*>(kalloc(PAGESIZE));
int r = chickadeefs_read_file_data(name_, buf, PAGESIZE, off);
*pg = buf;
return r;
}
void disk_loader::put_page(uint8_t* pg) {
kfree(pg);
}
// proc::load(binary_name)
// Load the code corresponding to program `binary_name` into this process
// and set `regs_->reg_rip` to its entry point. Calls `kallocpage()`.
// Returns 0 on success and negative on failure (e.g. out-of-memory).
int proc::load(const char* binary_name) {
memfile_loader ml;
ml.pagetable_ = pagetable_;
ml.mf_ = memfile::initfs_lookup(binary_name);
int r = load(ml);
if (r >= 0) {
regs_->reg_rip = ml.entry_rip_;
}
return r;
}
// proc::load(loader)
// Generic version of `proc::load(binary_name)`.
int proc::load(loader& ld) {
union {
elf_header eh;
elf_program ph;
} u;
size_t len;
unsigned nph, phoff;
// validate the binary
uint8_t* headerpg;
ssize_t r = ld.get_page(&headerpg, 0);
if (r < 0) {
goto exit;
} else if (size_t(r) < sizeof(elf_header)) {
r = E_NOEXEC;
goto exit;
}
len = r;
memcpy(&u.eh, headerpg, sizeof(elf_header));
if (u.eh.e_magic != ELF_MAGIC
|| u.eh.e_type != ELF_ET_EXEC
|| u.eh.e_phentsize != sizeof(elf_program)
|| u.eh.e_shentsize != sizeof(elf_section)
|| u.eh.e_phoff > PAGESIZE
|| u.eh.e_phoff > len
|| u.eh.e_phnum == 0
|| u.eh.e_phnum > (PAGESIZE - u.eh.e_phoff) / sizeof(elf_program)
|| u.eh.e_phnum > (len - u.eh.e_phoff) / sizeof(elf_program)) {
r = E_NOEXEC;
goto exit;
}
nph = u.eh.e_phnum;
phoff = u.eh.e_phoff;
ld.entry_rip_ = u.eh.e_entry;
// load each loadable program segment into memory
for (unsigned i = 0; i != nph; ++i) {
memcpy(&u.ph, headerpg + phoff + i * sizeof(u.ph), sizeof(u.ph));
if (u.ph.p_type == ELF_PTYPE_LOAD
&& (r = load_segment(u.ph, ld)) < 0) {
goto exit;
}
}
// set the entry point from the ELF header
r = 0;
exit:
ld.put_page(headerpg);
return r;
}
// proc::load_segment(ph, ld)
// Load an ELF segment at virtual address `ph->p_va` into this process.
// Loads pages `[src, src + ph->p_filesz)` to `dst`, then clears
// `[ph->p_va + ph->p_filesz, ph->p_va + ph->p_memsz)` to 0.
// Calls `kallocpage` to allocate pages and uses `vmiter::map`
// to map them in `pagetable_`. Returns 0 on success and an error
// code on failure.
int proc::load_segment(const elf_program& ph, loader& ld) {
uintptr_t va = (uintptr_t) ph.p_va;
uintptr_t end_file = va + ph.p_filesz;
uintptr_t end_mem = va + ph.p_memsz;
if (va > VA_LOWEND
|| VA_LOWEND - va < ph.p_memsz
|| ph.p_memsz < ph.p_filesz) {
return E_NOEXEC;
}
// allocate memory
for (vmiter it(ld.pagetable_, ROUNDDOWN(va, PAGESIZE));
it.va() < end_mem;
it += PAGESIZE) {
x86_64_page* pg = kallocpage();
if (!pg || it.map(ka2pa(pg)) < 0) {
return E_NOMEM;
}
}
// load binary data into allocated memory
size_t off = ph.p_offset;
size_t sz;
for (vmiter it(ld.pagetable_, va);
it.va() < end_file;
it += sz, off += sz) {
uint8_t* datapg;
ssize_t r = ld.get_page(&datapg, ROUNDDOWN(off, PAGESIZE));
size_t last_off = ROUNDDOWN(off, PAGESIZE) + r;
if (r < 0) {
return r;
} else if (last_off <= off) {
ld.put_page(datapg);
return E_NOEXEC;
} else {
sz = min(last_off - off, min(it.last_va(), end_file) - it.va());
memcpy(it.ka<uint8_t*>(), datapg + (off % PAGESIZE), sz);
ld.put_page(datapg);
}
}
// set initialized memory to zero
for (vmiter it(ld.pagetable_, end_file); it.va() < end_mem; it += sz) {
sz = min(it.last_va(), end_mem) - it.va();
memset(it.ka<uint8_t*>(), 0, sz);
}
return 0;
}