-
Notifications
You must be signed in to change notification settings - Fork 2
/
kernel.hh
543 lines (424 loc) · 14.1 KB
/
kernel.hh
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#ifndef CHICKADEE_KERNEL_HH
#define CHICKADEE_KERNEL_HH
#include "x86-64.h"
#include "lib.hh"
#include "k-list.hh"
#include "k-lock.hh"
#include "k-memrange.hh"
#if CHICKADEE_PROCESS
#error "kernel.hh should not be used by process code."
#endif
struct elf_program;
struct proc;
struct yieldstate;
// kernel.hh
//
// Functions, constants, and definitions for the kernel.
extern unsigned long resumes;
struct fdtable; // forward declaration
// Process descriptor type
struct __attribute__((aligned(4096))) proc {
// These three members must come first:
pid_t pid_; // thread ID
regstate* regs_; // process's current registers
yieldstate* yields_; // process's current yield state
#if HAVE_SANITIZERS
int sanitizer_status_ = 0;
#endif
list_links runq_link_; // for cpu run queue
list_links child_link_; // for reparenting
list<proc, &proc::child_link_> children_; // procs st. ppid_ = this->pid_
enum state_t {
blank = 0, runnable, blocked, broken
};
state_t state_; // process state
x86_64_pagetable* pagetable_; // process's page table
int cpu_; // index of cpu proc is running on
pid_t true_pid_; // actual process ID
pid_t ppid_; // parent process ID
int exit_status_;
bool interrupted_;
bool exiting_;
fdtable* fdtable_;
size_t malloc_top_; // dirty hack for malloc
int canary_;
proc();
NO_COPY_OR_ASSIGN(proc);
inline bool contains(uintptr_t addr) const;
inline bool contains(void* ptr) const;
void init_user(pid_t pid, x86_64_pagetable* pt);
void init_kernel(pid_t pid, void (*f)(proc*));
struct loader {
x86_64_pagetable* pagetable_ = nullptr;
uintptr_t entry_rip_ = 0;
virtual ssize_t get_page(uint8_t** pg, size_t off) = 0;
virtual void put_page(uint8_t* pg) = 0;
};
static int load(loader& ld);
int load(const char* binary_name);
void exception(regstate* reg);
uintptr_t syscall(regstate* reg);
void yield();
void yield_noreturn() __attribute__((noreturn));
void resume() __attribute__((noreturn));
inline void wake();
inline bool resumable() const;
inline irqstate lock_pagetable_read();
inline void unlock_pagetable_read(irqstate& irqs);
private:
static int load_segment(const elf_program& ph, loader& ld);
};
struct wait_queue;
extern wait_queue waitpid_wq;
#include "k-wait.hh"
#define NPROC 16
extern proc* ptable[NPROC];
extern proc* true_ptable[NPROC];
extern spinlock ptable_lock;
#define KTASKSTACK_SIZE 4096
extern int canary_value;
// allocate a new `proc` and call its constructor
proc* kalloc_proc() __attribute__((malloc));
const char* state_string(const proc* p);
// CPU state type
struct __attribute__((aligned(4096))) cpustate {
// These three members must come first:
cpustate* self_;
proc* current_;
uint64_t syscall_scratch_;
int index_;
int lapic_id_;
list<proc, &proc::runq_link_> runq_;
spinlock runq_lock_;
unsigned long nschedule_;
proc* idle_task_;
unsigned spinlock_depth_;
uint64_t gdt_segments_[7];
x86_64_taskstate task_descriptor_;
int canary_;
inline cpustate()
: self_(this), current_(nullptr) {
}
NO_COPY_OR_ASSIGN(cpustate);
inline bool contains(uintptr_t addr) const;
inline bool contains(void* ptr) const;
void init();
void init_ap();
void exception(regstate* reg);
void enqueue(proc* p);
void schedule(proc* yielding_from) __attribute__((noreturn));
void enable_irq(int irqno);
void disable_irq(int irqno);
private:
void init_cpu_hardware();
void init_idle_task();
};
#define NCPU 16
extern cpustate cpus[NCPU];
extern int ncpu;
#define CPUSTACK_SIZE 4096
inline cpustate* this_cpu();
// yieldstate: callee-saved registers that must be preserved across
// proc::yield()
struct yieldstate {
uintptr_t reg_rbp;
uintptr_t reg_rbx;
uintptr_t reg_r12;
uintptr_t reg_r13;
uintptr_t reg_r14;
uintptr_t reg_r15;
uintptr_t reg_rflags;
};
// timekeeping
#define HZ 100 // number of ticks per second
extern volatile unsigned long ticks; // number of ticks since boot
// Segment selectors
#define SEGSEL_BOOT_CODE 0x8 // boot code segment
#define SEGSEL_KERN_CODE 0x8 // kernel code segment
#define SEGSEL_KERN_DATA 0x10 // kernel data segment
#define SEGSEL_APP_CODE 0x18 // application code segment
#define SEGSEL_APP_DATA 0x20 // application data segment
#define SEGSEL_TASKSTATE 0x28 // task state segment
// Physical memory size (75 MB - about 2 MB used by kernel)
#define MEMSIZE_PHYSICAL (30 * 1024 * 1024)
// Virtual memory size
#define MEMSIZE_VIRTUAL MEMSIZE_PHYSICAL
enum memtype_t {
mem_nonexistent = 0, mem_available = 1, mem_kernel = 2, mem_reserved = 3,
mem_console = 4
};
extern memrangeset<16> physical_ranges;
// Hardware interrupt numbers
#define INT_IRQ 32U
#define IRQ_TIMER 0
#define IRQ_KEYBOARD 1
#define IRQ_IDE 14
#define IRQ_ERROR 19
#define IRQ_SPURIOUS 31
#define KTEXT_BASE 0xFFFFFFFF80000000UL
#define HIGHMEM_BASE 0xFFFF800000000000UL
inline uint64_t pa2ktext(uint64_t pa) {
assert(pa < -KTEXT_BASE);
return pa + KTEXT_BASE;
}
template <typename T>
inline T pa2ktext(uint64_t pa) {
return reinterpret_cast<T>(pa2ktext(pa));
}
inline uint64_t ktext2pa(uint64_t ka) {
assert(ka >= KTEXT_BASE);
return ka - KTEXT_BASE;
}
template <typename T>
inline uint64_t ktext2pa(T* ptr) {
return ktext2pa(reinterpret_cast<uint64_t>(ptr));
}
inline uint64_t pa2ka(uint64_t pa) {
assert(pa < -HIGHMEM_BASE);
return pa + HIGHMEM_BASE;
}
template <typename T>
inline T pa2ka(uint64_t pa) {
return reinterpret_cast<T>(pa2ka(pa));
}
inline uint64_t ka2pa(uint64_t ka) {
assert(ka >= HIGHMEM_BASE && ka < KTEXT_BASE);
return ka - HIGHMEM_BASE;
}
template <typename T>
inline uint64_t ka2pa(T* ptr) {
return ka2pa(reinterpret_cast<uint64_t>(ptr));
}
inline uint64_t kptr2pa(uint64_t kptr) {
assert(kptr >= HIGHMEM_BASE);
return kptr - (kptr >= KTEXT_BASE ? KTEXT_BASE : HIGHMEM_BASE);
}
template <typename T>
inline uint64_t kptr2pa(T* ptr) {
return kptr2pa(reinterpret_cast<uint64_t>(ptr));
}
template <typename T>
inline bool is_kptr(T* ptr) {
uintptr_t va = reinterpret_cast<uint64_t>(ptr);
return va >= HIGHMEM_BASE;
}
template <typename T>
inline bool is_ktext(T* ptr) {
uintptr_t va = reinterpret_cast<uint64_t>(ptr);
return va >= KTEXT_BASE;
}
template <typename T> T read_unaligned(const uint8_t* ptr);
template <>
inline uint16_t read_unaligned<uint16_t>(const uint8_t* ptr) {
return ptr[0] | (ptr[1] << 8);
}
template <>
inline int16_t read_unaligned<int16_t>(const uint8_t* ptr) {
return ptr[0] | (ptr[1] << 8);
}
template <>
inline uint32_t read_unaligned<uint32_t>(const uint8_t* ptr) {
return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
}
template <>
inline int32_t read_unaligned<int32_t>(const uint8_t* ptr) {
return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
}
template <typename T>
inline T read_unaligned_pa(uint64_t pa) {
return read_unaligned<T>(pa2ka<const uint8_t*>(pa));
}
// kallocpage
// Allocate and return a page. Returns `nullptr` on failure.
// Returns a high canonical address.
x86_64_page* kallocpage() __attribute__((malloc));
// kalloc(sz)
// Allocate and return a pointer to at least `sz` contiguous bytes
// of memory. Returns `nullptr` if `sz == 0` or on failure.
void* kalloc(size_t sz) __attribute__((malloc));
// kfree(ptr)
// Free a pointer previously returned by `kalloc`, `kallocpage`, or
// `kalloc_pagetable`. Does nothing if `ptr == nullptr`.
void kfree(void* ptr);
// knew<T>()
// Return a pointer to a newly-allocated object of type `T`. Calls
// the new object's constructor. Returns `nullptr` on failure.
template <typename T>
inline T* knew() {
if (void* mem = kalloc(sizeof(T))) {
return new (mem) T;
} else {
return nullptr;
}
}
template <typename T, typename... Args>
inline T* knew(Args&&... args) {
if (void* mem = kalloc(sizeof(T))) {
return new (mem) T(std::forward<Args>(args)...);
} else {
return nullptr;
}
}
// kdelete(ptr)
// Free an object allocated by `knew`. Calls the object's destructor.
template <typename T>
void kdelete(T* obj) {
if (obj) {
obj->~T();
kfree(obj);
}
}
// operator new, operator delete
// Expressions like `new (std::nothrow) T(...)` and `delete x` work,
// and call kalloc/kfree.
inline void* operator new(size_t sz, const std::nothrow_t&) noexcept {
return kalloc(sz);
}
inline void* operator new[](size_t sz, const std::nothrow_t&) noexcept {
return kalloc(sz);
}
inline void operator delete(void* ptr) noexcept {
kfree(ptr);
}
inline void operator delete(void* ptr, size_t) noexcept {
kfree(ptr);
}
inline void operator delete[](void* ptr) noexcept {
kfree(ptr);
}
inline void operator delete[](void* ptr, size_t) noexcept {
kfree(ptr);
}
// init_kalloc
// Initialize stuff needed by `kalloc`. Called from `init_hardware`,
// after `physical_ranges` is initialized.
void init_kalloc();
// run unit tests on the kalloc system
void test_kalloc();
// initialize hardware and CPUs
void init_hardware();
// query machine configuration
unsigned machine_ncpu();
unsigned machine_pci_irq(int pci_addr, int intr_pin);
struct ahcistate;
extern ahcistate* sata_disk;
// kernel page table (used for virtual memory)
extern x86_64_pagetable early_pagetable[3];
// allocate and initialize a new page table
x86_64_pagetable* kalloc_pagetable();
// change current page table
void set_pagetable(x86_64_pagetable* pagetable);
// turn off the virtual machine
void poweroff() __attribute__((noreturn));
// reboot the virtual machine
void reboot() __attribute__((noreturn));
extern "C" {
void kernel_start(const char* command);
}
// console_show_cursor(cpos)
// Move the console cursor to position `cpos`, which should be between 0
// and 80 * 25.
void console_show_cursor(int cpos);
// log_printf, log_vprintf
// Print debugging messages to the host's `log.txt` file. We run QEMU
// so that messages written to the QEMU "parallel port" end up in `log.txt`.
void log_printf(const char* format, ...) __attribute__((noinline));
void log_vprintf(const char* format, va_list val) __attribute__((noinline));
void debug_printf_(const char* file, const char* func, int line,
const char* format, ...);
#define DEBUG true
#include "k-debug.hh"
// log_backtrace
// Print a backtrace to the host's `log.txt` file.
void log_backtrace(const char* prefix = "");
#if HAVE_SANITIZERS
// sanitizer functions
void init_sanitizers();
void disable_asan();
void enable_asan();
void asan_mark_memory(unsigned long pa, size_t sz, bool poisoned);
#else
inline void disable_asan() {}
inline void enable_asan() {}
inline void asan_mark_memory(unsigned long, size_t, bool) {}
#endif
// `panicking == true` iff some CPU has panicked
extern bool panicking;
// this_cpu
// Return a pointer to the current CPU. Requires disabled interrupts.
inline cpustate* this_cpu() {
assert(is_cli());
cpustate* result;
asm volatile ("movq %%gs:(0), %0" : "=r" (result));
return result;
}
// current
// Return a pointer to the current `struct proc`.
inline proc* current() {
proc* result;
asm volatile ("movq %%gs:(8), %0" : "=r" (result));
return result;
}
// adjust_this_cpu_spinlock_depth(delta)
// Adjust this CPU's spinlock_depth_ by `delta`. Does *not* require
// disabled interrupts.
inline void adjust_this_cpu_spinlock_depth(int delta) {
asm volatile ("addl %1, %%gs:%0"
: "+m" (*reinterpret_cast<int*>
(offsetof(cpustate, spinlock_depth_)))
: "er" (delta) : "cc", "memory");
}
// cpustate::contains(ptr)
// Return true iff `ptr` lies within this cpustate's allocation.
inline bool cpustate::contains(void* ptr) const {
return contains(reinterpret_cast<uintptr_t>(ptr));
}
inline bool cpustate::contains(uintptr_t addr) const {
uintptr_t delta = addr - reinterpret_cast<uintptr_t>(this);
return delta <= CPUSTACK_SIZE;
}
// proc::contains(ptr)
// Return true iff `ptr` lies within this cpustate's allocation.
inline bool proc::contains(void* ptr) const {
return contains(reinterpret_cast<uintptr_t>(ptr));
}
inline bool proc::contains(uintptr_t addr) const {
uintptr_t delta = addr - reinterpret_cast<uintptr_t>(this);
return delta <= KTASKSTACK_SIZE;
}
// proc::wake()
// Unblocks a process and re-enqueues it on its cpu
inline void proc::wake() {
auto irqs = cpus[cpu_].runq_lock_.lock();
if (state_ == blocked) {
state_ = runnable;
cpus[cpu_].enqueue(this);
}
cpus[cpu_].runq_lock_.unlock(irqs);
}
// proc::resumable()
// Return true iff this `proc` can be resumed (`regs_` or `yields_`
// is set). Also checks some assertions about `regs_` and `yields_`.
inline bool proc::resumable() const {
assert(!(regs_ && yields_)); // at most one at a time
assert(!regs_ || contains(regs_)); // `regs_` points within this
assert(!yields_ || contains(yields_)); // same for `yields_`
return regs_ || yields_;
}
// proc::lock_pagetable_read()
// Obtain a “read lock” on this process’s page table. While the “read
// lock” is held, it is illegal to remove or change existing valid
// mappings in that page table, or to free page table pages.
inline irqstate proc::lock_pagetable_read() {
return irqstate();
}
inline void proc::unlock_pagetable_read(irqstate&) {
}
struct disk_loader : public proc::loader {
disk_loader() : name_("") { };
static constexpr unsigned namesize = 64;
char name_[namesize];
ssize_t get_page(uint8_t** pg, size_t off) override;
void put_page(uint8_t* pg) override;
};
#endif