-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathkernel.cc
409 lines (321 loc) · 12.4 KB
/
kernel.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "kernel.hh"
#include "k-apic.hh"
#include "k-vmiter.hh"
#include <atomic>
// kernel.cc
//
// This is the kernel.
// INITIAL PHYSICAL MEMORY LAYOUT
//
// +-------------- Base Memory --------------+
// v v
// +-----+--------------------+----------------+--------------------+---------/
// | | Kernel Kernel | : I/O | App 1 App 1 | App 2
// | | Code + Data Stack | ... : Memory | Code + Data Stack | Code ...
// +-----+--------------------+----------------+--------------------+---------/
// 0 0x40000 0x80000 0xA0000 0x100000 0x140000
// ^
// | \___ PROC_SIZE ___/
// PROC_START_ADDR
#define PROC_SIZE 0x40000 // initial state only
proc ptable[NPROC]; // array of process descriptors
// Note that `ptable[0]` is never used.
proc* current; // pointer to currently executing proc
#define HZ 100 // timer interrupt frequency (interrupts/sec)
static std::atomic<unsigned long> ticks; // # timer interrupts so far
// Memory state - see `kernel.hh`
physpageinfo physpages[NPAGES];
[[noreturn]] void schedule();
[[noreturn]] void run(proc* p);
void exception(regstate* regs);
uintptr_t syscall(regstate* regs);
void memshow();
// kernel_start(command)
// Initialize the hardware and processes and start running. The `command`
// string is an optional string passed from the boot loader.
static void process_setup(pid_t pid, const char* program_name);
void kernel_start(const char* command) {
// initialize hardware
init_hardware();
log_printf("Starting WeensyOS\n");
ticks = 1;
init_timer(HZ);
// clear screen
console_clear();
// (re-)initialize kernel page table
for (vmiter it(kernel_pagetable);
it.va() < MEMSIZE_PHYSICAL;
it += PAGESIZE) {
if (it.va() != 0) {
it.map(it.va(), PTE_P | PTE_W | PTE_U);
} else {
// nullptr is inaccessible even to the kernel
it.map(it.va(), 0);
}
}
// set up process descriptors
for (pid_t i = 0; i < NPROC; i++) {
ptable[i].pid = i;
ptable[i].state = P_FREE;
}
if (command && !program_image(command).empty()) {
process_setup(1, command);
} else {
process_setup(1, "allocator");
process_setup(2, "allocator2");
process_setup(3, "allocator3");
process_setup(4, "allocator4");
}
// Switch to the first process using run()
run(&ptable[1]);
}
// kalloc(sz)
// Kernel physical memory allocator. Allocates at least `sz` contiguous bytes
// and returns a pointer to the allocated memory, or `nullptr` on failure.
// The returned pointer’s address is a valid physical address, but since the
// WeensyOS kernel uses an identity mapping for virtual memory, it is also
// a valid virtual address that the kernel can access or modify.
//
// The allocator selects from physical pages that can be allocated for
// process use (so not reserved pages or kernel data), and from physical
// pages that are currently unused (so `physpages[I].refcount == 0`).
//
// On WeensyOS, `kalloc` is a page-based allocator: if `sz > PAGESIZE`
// the allocation fails; if `sz < PAGESIZE` it allocates a whole page
// anyway.
//
// The handout code returns the next allocatable free page it can find.
// It checks all pages. (You could maybe make this faster!)
//
// The returned memory is initially filled with 0xCC, which corresponds to
// the x86 instruction `int3`. This may help you debug.
void* kalloc(size_t sz) {
if (sz > PAGESIZE) {
return nullptr;
}
for (uintptr_t pa = 0; pa != MEMSIZE_PHYSICAL; pa += PAGESIZE) {
if (allocatable_physical_address(pa)
&& physpages[pa / PAGESIZE].refcount == 0) {
++physpages[pa / PAGESIZE].refcount;
memset((void*) pa, 0xCC, PAGESIZE);
return (void*) pa;
}
}
return nullptr;
}
// kfree(kptr)
// Free `kptr`, which must have been previously returned by `kalloc`.
// If `kptr == nullptr` does nothing.
void kfree(void* kptr) {
(void) kptr;
assert(false /* your code here */);
}
// process_setup(pid, program_name)
// Load application program `program_name` as process number `pid`.
// This loads the application's code and data into memory, sets its
// %rip and %rsp, gives it a stack page, and marks it as runnable.
void process_setup(pid_t pid, const char* program_name) {
init_process(&ptable[pid], 0);
// initialize process page table
ptable[pid].pagetable = kernel_pagetable;
// obtain reference to the program image
program_image pgm(program_name);
// allocate and map global memory required by loadable segments
for (auto seg = pgm.begin(); seg != pgm.end(); ++seg) {
for (uintptr_t a = round_down(seg.va(), PAGESIZE);
a < seg.va() + seg.size();
a += PAGESIZE) {
// `a` is the process virtual address for the next code or data page
// (The handout code requires that the corresponding physical
// address is currently free.)
assert(physpages[a / PAGESIZE].refcount == 0);
++physpages[a / PAGESIZE].refcount;
}
}
// initialize data in loadable segments
for (auto seg = pgm.begin(); seg != pgm.end(); ++seg) {
memset((void*) seg.va(), 0, seg.size());
memcpy((void*) seg.va(), seg.data(), seg.data_size());
}
// mark entry point
ptable[pid].regs.reg_rip = pgm.entry();
// allocate and map stack segment
// Compute process virtual address for stack page
uintptr_t stack_addr = PROC_START_ADDR + PROC_SIZE * pid - PAGESIZE;
// The handout code requires that the corresponding physical address
// is currently free.
assert(physpages[stack_addr / PAGESIZE].refcount == 0);
++physpages[stack_addr / PAGESIZE].refcount;
ptable[pid].regs.reg_rsp = stack_addr + PAGESIZE;
// mark process as runnable
ptable[pid].state = P_RUNNABLE;
}
// exception(regs)
// Exception handler (for interrupts, traps, and faults).
//
// The register values from exception time are stored in `regs`.
// The processor responds to an exception by saving application state on
// the kernel's stack, then jumping to kernel assembly code (in
// k-exception.S). That code saves more registers on the kernel's stack,
// then calls exception().
//
// Note that hardware interrupts are disabled when the kernel is running.
void exception(regstate* regs) {
// Copy the saved registers into the `current` process descriptor.
current->regs = *regs;
regs = ¤t->regs;
// It can be useful to log events using `log_printf`.
// Events logged this way are stored in the host's `log.txt` file.
/* log_printf("proc %d: exception %d at rip %p\n",
current->pid, regs->reg_intno, regs->reg_rip); */
// Show the current cursor location and memory state
// (unless this is a kernel fault).
console_show_cursor(cursorpos);
if (regs->reg_intno != INT_PF || (regs->reg_errcode & PTE_U)) {
memshow();
}
// If Control-C was typed, exit the virtual machine.
check_keyboard();
// Actually handle the exception.
switch (regs->reg_intno) {
case INT_IRQ + IRQ_TIMER:
++ticks;
lapicstate::get().ack();
schedule();
break; /* will not be reached */
case INT_PF: {
// Analyze faulting address and access type.
uintptr_t addr = rdcr2();
const char* operation = regs->reg_errcode & PTE_W
? "write" : "read";
const char* problem = regs->reg_errcode & PTE_P
? "protection problem" : "missing page";
if (!(regs->reg_errcode & PTE_U)) {
panic("Kernel page fault on %p (%s %s)!\n",
addr, operation, problem);
}
console_printf(CPOS(24, 0), 0x0C00,
"Process %d page fault on %p (%s %s, rip=%p)!\n",
current->pid, addr, operation, problem, regs->reg_rip);
current->state = P_FAULTED;
break;
}
default:
panic("Unexpected exception %d!\n", regs->reg_intno);
}
// Return to the current process (or run something else).
if (current->state == P_RUNNABLE) {
run(current);
} else {
schedule();
}
}
// syscall(regs)
// System call handler.
//
// The register values from system call time are stored in `regs`.
// The return value, if any, is returned to the user process in `%rax`.
//
// Note that hardware interrupts are disabled when the kernel is running.
int syscall_page_alloc(uintptr_t addr);
uintptr_t syscall(regstate* regs) {
// Copy the saved registers into the `current` process descriptor.
current->regs = *regs;
regs = ¤t->regs;
// It can be useful to log events using `log_printf`.
// Events logged this way are stored in the host's `log.txt` file.
/* log_printf("proc %d: syscall %d at rip %p\n",
current->pid, regs->reg_rax, regs->reg_rip); */
// Show the current cursor location and memory state.
console_show_cursor(cursorpos);
memshow();
// If Control-C was typed, exit the virtual machine.
check_keyboard();
// Actually handle the exception.
switch (regs->reg_rax) {
case SYSCALL_PANIC:
user_panic(current); // does not return
case SYSCALL_GETPID:
return current->pid;
case SYSCALL_YIELD:
current->regs.reg_rax = 0;
schedule(); // does not return
case SYSCALL_PAGE_ALLOC:
return syscall_page_alloc(current->regs.reg_rdi);
default:
panic("Unexpected system call %ld!\n", regs->reg_rax);
}
panic("Should not get here!\n");
}
// syscall_page_alloc(addr)
// Handles the SYSCALL_PAGE_ALLOC system call. This function
// should implement the specification for `sys_page_alloc`
// in `u-lib.hh` (but in the handout code, it does not).
int syscall_page_alloc(uintptr_t addr) {
assert(physpages[addr / PAGESIZE].refcount == 0);
++physpages[addr / PAGESIZE].refcount;
memset((void*) addr, 0, PAGESIZE);
return 0;
}
// schedule
// Pick the next process to run and then run it.
// If there are no runnable processes, spins forever.
void schedule() {
pid_t pid = current->pid;
for (unsigned spins = 1; true; ++spins) {
pid = (pid + 1) % NPROC;
if (ptable[pid].state == P_RUNNABLE) {
run(&ptable[pid]);
}
// If Control-C was typed, exit the virtual machine.
check_keyboard();
// If spinning forever, show the memviewer.
if (spins % (1 << 12) == 0) {
memshow();
log_printf("%u\n", spins);
}
}
}
// run(p)
// Run process `p`. This involves setting `current = p` and calling
// `exception_return` to restore its page table and registers.
void run(proc* p) {
assert(p->state == P_RUNNABLE);
current = p;
// Check the process's current pagetable.
check_pagetable(p->pagetable);
// This function is defined in k-exception.S. It restores the process's
// registers then jumps back to user mode.
exception_return(p);
// should never get here
while (true) {
}
}
// memshow()
// Draw a picture of memory (physical and virtual) on the CGA console.
// Switches to a new process's virtual memory map every 0.25 sec.
// Uses `console_memviewer()`, a function defined in `k-memviewer.cc`.
void memshow() {
static unsigned last_ticks = 0;
static int showing = 0;
// switch to a new process every 0.25 sec
if (last_ticks == 0 || ticks - last_ticks >= HZ / 2) {
last_ticks = ticks;
showing = (showing + 1) % NPROC;
}
proc* p = nullptr;
for (int search = 0; !p && search < NPROC; ++search) {
if (ptable[showing].state != P_FREE
&& ptable[showing].pagetable) {
p = &ptable[showing];
} else {
showing = (showing + 1) % NPROC;
}
}
console_memviewer(p);
if (!p) {
console_printf(CPOS(10, 29), 0x0F00, "VIRTUAL ADDRESS SPACE\n"
" [All processes have exited]\n"
"\n\n\n\n\n\n\n\n\n\n\n");
}
}