style fixes

This commit is contained in:
Yanyan Jiang 2018-08-09 03:46:33 +00:00
parent 0ab93ce23a
commit 3588d60f7f
8 changed files with 59 additions and 39 deletions

View File

@ -12,6 +12,7 @@ void lapic_bootap(int cpu, uint32_t address);
void ioapic_enable(int irq, int cpu);
void smp_init();
void cpu_initgdt();
void cpu_initpte();
void cpu_setustk(uintptr_t ss0, uintptr_t esp0);
static inline void puts(const char *s) {

View File

@ -116,9 +116,10 @@ void irq_handle(struct TrapFrame *tf) {
_Context *ret_ctx = &ctx;
if (user_handler) {
_Context *next = user_handler(ev, &ctx);
if (next != NULL) {
ret_ctx = next;
if (!next) {
panic("return to a null context");
}
ret_ctx = next;
}
// Return to context @ret_ctx
@ -192,6 +193,10 @@ int _asye_init(_Context*(*handler)(_Event, _Context*)) {
return 0;
}
static void panic_on_return() {
panic("kernel context returns");
}
_Context *_kcontext(_Area stack, void (*entry)(void *), void *arg) {
_Context *ctx = (_Context *)stack.start;
ctx->eax = ctx->ebx = ctx->ecx = ctx->edx = 0;
@ -206,7 +211,7 @@ _Context *_kcontext(_Area stack, void (*entry)(void *), void *arg) {
uint32_t **esp = (uint32_t **)&ctx->esp0;
*(*esp -= 1) = (uint32_t)arg; // argument
*(*esp -= 1) = 0; // return address
*(*esp -= 1) = (uint32_t)panic_on_return; // return address
return ctx;
}

View File

@ -58,6 +58,7 @@ static void mp_entry() {
cpu_initgdt();
lapic_init();
ioapic_enable(IRQ_KBD, _cpu());
cpu_initpte();
}
_atomic_xchg(&ap_boot, 1);
_entry();

View File

@ -16,47 +16,50 @@ static void pgfree(void *ptr) {
pgfree_usr(ptr);
}
static intptr_t first_proc = 1;
static PDE *kpt;
static _Area prot_vm_range = {
static _Area prot_vm_range = { // 1GB protected space
.start = (void*)0x40000000,
.end = (void*)0x80000000,
};
static _Area segments[] = {
{.start = (void*)0, .end = (void*)0x10000000}, // Low memory: kernel data
{.start = (void*)0xf0000000, .end = (void*)(0)}, // High memory: APIC and VGA
{.start = (void*)0, .end = (void*)0x10000000}, // kernel data
{.start = (void*)0xf0000000, .end = (void*)(0)}, // system memory
};
// must be called atomically per-cpu
int _pte_init(void * (*pgalloc_f)(size_t), void (*pgfree_f)(void *)) {
if (_cpu() != 0) {
panic("init PTE in non-bootstrap CPU");
}
pgalloc_usr = pgalloc_f;
pgfree_usr = pgfree_f;
if (first_proc) {
// first processor, create kernel page table
kpt = pgalloc();
for (int i = 0; i < sizeof(segments) / sizeof(segments[0]); i++) {
_Area *seg = &segments[i];
for (uint32_t pa = (uint32_t)seg->start; pa != (uint32_t)seg->end; pa += PGSIZE) {
PTE *ptab;
if (!(kpt[PDX(pa)] & PTE_P)) {
ptab = pgalloc();
kpt[PDX(pa)] = PTE_P | PTE_W | (uint32_t)ptab;
} else {
ptab = (PTE*)PTE_ADDR(kpt[PDX(pa)]);
}
ptab[PTX(pa)] = PTE_P | PTE_W | pa;
kpt = pgalloc();
for (int i = 0; i < sizeof(segments) / sizeof(segments[0]); i++) {
_Area *seg = &segments[i];
for (uint32_t pa = (uint32_t)seg->start;
pa != (uint32_t)seg->end;
pa += PGSIZE) {
PTE *ptab;
if (!(kpt[PDX(pa)] & PTE_P)) {
ptab = pgalloc();
kpt[PDX(pa)] = PTE_P | PTE_W | (uint32_t)ptab;
} else {
ptab = (PTE*)PTE_ADDR(kpt[PDX(pa)]);
}
ptab[PTX(pa)] = PTE_P | PTE_W | pa;
}
}
cpu_initpte(); // set CR3 and CR0 if kpt is not NULL
return 0;
}
void cpu_initpte() {
if (kpt) {
set_cr3(kpt);
set_cr0(get_cr0() | CR0_PG);
_atomic_xchg(&first_proc, 0);
}
return 0;
}
int _protect(_Protect *p) {

View File

@ -7,7 +7,6 @@ _Area _heap; // the heap memory defined in AM spec
int main();
static void memory_init();
// the bootloader jumps here,
// with a (small) bootstrap stack
void _start() {
@ -42,15 +41,10 @@ static void memory_init() {
st = ed = (((uintptr_t)&end) & ~(step - 1)) + step;
while (1) {
volatile uint32_t *ptr = (uint32_t*)ed;
*ptr = 0x5a5a5a5a; // write
if (*ptr == 0x5a5a5a5a) { // then read
// if the value check passed, the memory is okay
ed += step;
} else {
// otherwise we hit the end of the physical memory
break;
}
*ptr = 0x5a5a5a5a; // write then read
if (*ptr == 0x5a5a5a5a) ed += step; // check passed, memory is okay
else break; // hit the end of the physical memory
}
_heap.start = (void*)st;
_heap.end = (void*)ed;
_heap.end = (void*)ed;
}

View File

@ -3,13 +3,26 @@
#include <klib.h>
void f() {
// printf("%d / %d\n", _cpu(), _ncpu());
while (1) {
printf("%d", _cpu());
}
}
static uintptr_t start;
static void *alloc(size_t size) {
while (start % size != 0) start++;
void *ret = (void *)start;
start += size;
return ret;
}
static void free(void *ptr) {
}
int main() {
start = (uintptr_t)_heap.start;
_pte_init(alloc, free);
_mpe_init(f);
assert(0);
return 0;

3
tests/mptest/run Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
make && \
qemu-system-i386 -smp 4 -serial stdio build/mptest-x86-qemu

View File

@ -11,7 +11,7 @@ _Context* handler(_Event ev, _Context *ctx) {
break;
case _EVENT_IRQ_TIMER:
case _EVENT_IRQ_IODEV:
printf(".");
printf("==== interrupt ===\n");
break;
case _EVENT_PAGEFAULT:
printf("PF: %x %s%s%s\n",
@ -85,7 +85,7 @@ int main() {
_Area k = { .start = kstk, .end = kstk + 4096 };
_Area u = { .start = ptr + pgsz, .end = ptr + pgsz * 2 };
uctx = _ucontext(&prot, u, k, ptr, NULL);
uctx = _ucontext(&prot, u, k, ptr, 0);
_switch(&prot);
_intr_write(1);