diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index 5c870ad..25b7456 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -3,6 +3,13 @@ #include #include +#include +#include +#include +#include +#include +#include +#include #define read_csr(csr) \ ({ \ @@ -32,13 +39,13 @@ typedef unsigned long sbi_word; void fmt(const char *f, ...) { - using drivers::opensbi::legacy::console_putchar; + namespace legacy = drivers::opensbi::legacy; va_list va; va_start(va, f); while(*f) { if(*f != '{') { - console_putchar(*(f++)); + legacy::console_putchar(*(f++)); continue; } f++; @@ -54,7 +61,7 @@ void fmt(const char *f, ...) { for(int i = 0; i < 16; ++i) { const char *digits = "0123456789abcdef"; int d = (v >> (60 - i * 4)) & 0xF; - console_putchar(digits[d]); + legacy::console_putchar(digits[d]); } } va_end(va); @@ -192,7 +199,8 @@ void switch_task(void *ctx, continuation_t c) { extern "C" void isr(); extern "C" void handle_isr(isr_frame_t *frame) { - using drivers::opensbi::legacy::console_putchar; + namespace legacy = drivers::opensbi::legacy; + namespace timer = drivers::opensbi::timer; unsigned long cause = read_csr("scause"); unsigned long code = cause & ~(1UL << 63); @@ -203,20 +211,22 @@ extern "C" void handle_isr(isr_frame_t *frame) { }else if(code == 5) { // Timer interrupt. //clear_csr_bits("sie", sie_s_timer); unsigned long time = read_csr("time"); - drivers::opensbi::ecall(0x54494D45, 0, time + 100000); - save_stack(switch_task, current_task); + //drivers::opensbi::ecall(0x54494D45, 0, time + 100000); + timer::set_timer(time + 10000000); + //set_timer(time + 100000); + //save_stack(switch_task, current_task); }else{ fmt("it's an unhandled interrupt, code: {}\n", code); } }else{ if(code == 8) { // Syscall. - console_putchar(frame->a[0]); + legacy::console_putchar(frame->a[0]); }else{ unsigned long sepc = read_csr("sepc"); fmt("it's an exception at {}\n", sepc); const char *s = exception_strings[cause]; while(*s) - console_putchar(*(s++)); + legacy::console_putchar(*(s++)); while(1) ; } @@ -266,10 +276,56 @@ extern "C" void kmain(unsigned long hart, void *dtb) { (void)hart; (void)dtb; - using drivers::opensbi::legacy::console_putchar; + namespace legacy = drivers::opensbi::legacy; + namespace base = drivers::opensbi::base; + namespace timer = drivers::opensbi::timer; + namespace ipi = drivers::opensbi::ipi; fmt("test\n"); + auto ret = base::get_spec_version(); + if(ret.error) { + fmt("get_spec_version failed\n"); + } else { + fmt("spec version: {}\n", ret.value); + } + + ret = base::get_impl_id(); + if(ret.error) { + fmt("get_impl_id failed\n"); + } else { + fmt("impl id: {}\n", ret.value); + } + + ret = base::get_impl_version(); + if(ret.error) { + fmt("get_impl_version failed\n"); + } else { + fmt("impl version: {}\n", ret.value); + } + + ret = base::get_mvendor_id(); + if(ret.error) { + fmt("get_mvendor_id failed\n"); + } else { + fmt("vendor id: {}\n", ret.value); + } + + ret = base::get_march_id(); + if(ret.error) { + fmt("get_march_id failed\n"); + } else { + fmt("arch id: {}\n", ret.value); + } + + ret = base::get_mimp_id(); + if(ret.error) { + fmt("get_mimp_id failed\n"); + } else { + fmt("imp id: {}\n", ret.value); + } + + fmt("uintmax: {} bits\n", sizeof(uintmax_t) * 8); fmt("HART: {}, DTB: {}\n", hart, dtb); @@ -279,7 +335,7 @@ extern "C" void kmain(unsigned long hart, void *dtb) { *cfg = 0x10; *dat = 0xFF; - while(1); + //while(1); cli(); @@ -292,9 +348,28 @@ extern "C" void kmain(unsigned long hart, void *dtb) { set_csr_bits("sie", sie_s_software | sie_s_timer); // Self-IPI. - drivers::opensbi::ecall(0x735049, 0, 1 << hart, 0); + ipi::send_ipi({ 1UL << hart, 0 }); sti(); + unsigned long timea = read_csr("time"); + ret = timer::set_timer(timea + 100000); + + drivers::opensbi::rfence::remote_fence_i({0, 0}); + //ret = drivers::opensbi::hsm::hart_stop(); + using drivers::opensbi::srst::ResetType; + using drivers::opensbi::srst::ResetReason; + // ret = drivers::opensbi::srst::system_reset(ResetType::WARM_REBOOT, ResetReason::SYSTEM_FAILURE); + + namespace pmu = drivers::opensbi::pmu; + ret = pmu::num_counters(); + + fmt("Number of counters: {}\n", ret.value); + for(int i = 0; i < ret.value; i++) { + ret = pmu::counter_get_info(i); + fmt("{} err: {}\n", i, ret.error); + fmt("{} val: {}\n", i, ret.value); + } + while(1); cli(); unsigned long time; @@ -325,17 +400,21 @@ extern "C" void kmain(unsigned long hart, void *dtb) { s = "paging works!\n"; s += 512UL * 1024 * 1024 * 1024; while(*s) - console_putchar(*(s++)); + legacy::console_putchar(*(s++)); create_task(&task1, task1_main); create_task(&task2, task2_main); time = read_csr("time"); - drivers::opensbi::ecall(0x54494D45, 0, time + 100000); + //set_timer(time + 100000); + //drivers::opensbi::ecall(0x54494D45, 0, time + 100000); + //timer::set_timer(time + 100000); - save_stack(switch_task, 0); + //save_stack(switch_task, 0); - drivers::opensbi::ecall(8, 0, 0); + namespace hsm = drivers::opensbi::hsm; + hsm::hart_stop(); + // drivers::opensbi::ecall(8, 0, 0); while(1); }