diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..145ed93 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +temp_build/build/ +temp_build/s-mode/ +temp_build/u-mode/ diff --git a/temp_build/common/pt_interrupt.S b/temp_build/common/pt_interrupt.S new file mode 100644 index 0000000..d87bfe8 --- /dev/null +++ b/temp_build/common/pt_interrupt.S @@ -0,0 +1,29 @@ + +/* +.align 4 +.global vector_table +.option push +.option norvc +vector_table: + j exception_handler // exceptions jump to cause 0 + j ssip_handler // 1 + j unreachable_handler // 2 + j msip_handler // 3 + + j unreachable_handler // 4 + j stip_handler // 5 + j unreachable_handler // 6 + j mtip_handler // 7 + + j unreachable_handler // 8 + j seip_handler // 9 + j unreachable_handler // 10 + j meip_handler // 11 + + j unreachable_handler // 12 + j lcofip_handler // 13 + j unreachable_handler // 14 + j unreachable_handler // 15 + // end of standard interrupts +.option pop +*/ diff --git a/temp_build/interrupts-exceptions/proc.h b/temp_build/interrupts-exceptions/proc.h new file mode 100644 index 0000000..c7f424a --- /dev/null +++ b/temp_build/interrupts-exceptions/proc.h @@ -0,0 +1,30 @@ +/* proc.h */ + +#ifndef _PROC_H_ +#define _PROC_H_ + +#define NPROC 64 /* max number of processes */ + +/* Process states */ +#define PR_FREE 0 +#define PR_READY 1 +#define PR_CURR 2 +#define PR_SLEEP 3 + +struct procent { + uint16 prstate; + pri16 prprio; //process priority + char *prstkptr; //stack pointer + char *prstkbase; //base of runtime stack + uint32 prstklen; //stack length in bytes + //char prname[PNMLEN] //process name + sid32 prsem; //semaphore for process + pid32 prparent; //ID of creating process + //umsg prmsg; //message sent to this process + //bool8 prhasmsg; //Nonzero iff msg is valid + //int16 prdesc[NDESC]; //device descriptors for process +}; + +extern struct procent proctab[NPROC]; + +#endif diff --git a/temp_build/interrupts-exceptions/process_table.c b/temp_build/interrupts-exceptions/process_table.c new file mode 100644 index 0000000..ab6614c --- /dev/null +++ b/temp_build/interrupts-exceptions/process_table.c @@ -0,0 +1,61 @@ + +#include +#include "csr.h" +#include "utility.h" +#include "format.h" + +#define QUANTUM 2; + +int queue[5] = {1, 2, 3, 4, 5}; +int index = 0; +uint64_t volatile time_remaining = QUANTUM; + +void reschedule_function() { + index++; + time_remaining = QUANTUM; + print("Index increments. Index at %d. ", index); +} + +void set_timer(uint64_t value) { + *MTIMECMP = *MTIME + value; +} + +void meip_handler() { + print("meip handling...."); + *EXT_CLEAR = 0x1; // writing anything simulates clearing interrupt + CSRW("mie", 0x088); + //index++; + print("Handled. "); +} + +void mtip_handler() { + print("mtip handling...."); + time_remaining -= 1; + if (time_remaining <= 0) { + reschedule_function(); + } + set_timer(1000); + print("Handled. "); +} + +void msip_handler() { + print("msip handling...."); + *MSIP = 0x0; // writing 0 clears this + //index++; + print("Handled. ", index); +} + +int main() { + *MTIMECMPH = 0x00; + *MTIMECMP = 0xFF; + + setup_interrupt_m_vectored(vector_table, IE_MTIE | IE_MSIE | IE_MEIE); + enable_interrupts_m(); + + *MSIP = 1; + *EXT_SET = 1; + + while (*MTIME < 0xFFFF); + + return 0; +} diff --git a/temp_build/s-mode/csr.c b/temp_build/s-mode/csr.c deleted file mode 100644 index c337a18..0000000 --- a/temp_build/s-mode/csr.c +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include -#include "csr.h" -#include "format.h" -#include "utility.h" - -void __attribute__((interrupt)) __attribute__((aligned(4))) m_mode_handler (void); -void __attribute__((interrupt)) __attribute__((aligned(4))) s_mode_handler (void); -noreturn void user_main (void); -noreturn void __attribute__((aligned(4))) setup_s_mode (void); - -/* -* csr.c - checks for access control of S-mode, U-mode CSRs. -* Tests interrupt delegation from M-mode. -*/ - -void __attribute__((interrupt)) __attribute__((aligned(4))) m_mode_handler() { - exception_context_t ctx; - read_exception_context(&ctx); - print_exception_context(&ctx); - - if (ctx.cause == EX_ECALL_SMODE) { - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - } else { - advance_mepc(4); - flag -= 1; - } -} - -void __attribute__((interrupt)) __attribute__((aligned(4))) s_mode_handler() { - exception_context_t ctx; - read_exception_context_s(&ctx); - print_exception_context(&ctx); - - if (ctx.cause == EX_ECALL_UMODE) { - CSRS("sstatus", MSTATUS_SPP); - set_sepc(&done); - } else { - advance_sepc(4); - flag -= 1; - } - - asm volatile("sret"); -} - -noreturn void user_main(void) { - uint32_t csr_val; - csr_val = CSRR("cycle"); - csr_val = CSRR("time"); - - (void)csr_val; - - ecall(); - - __builtin_unreachable(); -} - -int main(void) { - require_supervisor_mode(); - - CSRW("mtvec", (uint32_t) m_mode_handler); - CSRS("mstatus", MSTATUS_MIE); - - CSRW("stvec", (uint32_t) s_mode_handler); - CSRS("sstatus", 0x08); - - uint32_t pmp_addr = ((uint32_t) (&flag)) >> 2; - CSRW("pmpaddr0", pmp_addr); - uint32_t actual_pmp_addr = CSRR("pmpaddr0"); - if (actual_pmp_addr != pmp_addr) { - print("Set PMP granularity down to 4 to run this test!\n"); - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - asm volatile("mret"); - } - pmp_addr = 0xFFFFFFFF; - CSRW("pmpaddr1", pmp_addr); - uint32_t pmp_cfg = 0x00001F17; - CSRW("pmpcfg0", pmp_cfg); - - flag = 5; - - uint32_t csr_val_0 = 0x0; - CSRW("mcycle", csr_val_0); - csr_val_0 = CSRR("mcycle"); - asm volatile("nop; nop; nop;"); - uint32_t csr_val_1 = CSRR("cycle"); - - if (csr_val_1 - csr_val_0 < 30) { - flag -= 1; - } - - CSRW("cycle", csr_val_0); - - uint32_t medeleg = 0xFFFFFFFF; - CSRW("medeleg", medeleg); - - set_sepc(user_main); - asm volatile("sret"); - - __builtin_unreachable(); -} diff --git a/temp_build/s-mode/s-mode.c b/temp_build/s-mode/s-mode.c deleted file mode 100644 index 7b5b89e..0000000 --- a/temp_build/s-mode/s-mode.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include "csr.h" -#include "format.h" -#include "utility.h" - -/* -* s-mode.c - tests privilege protection from U-mode to S-mode. -*/ - -void __attribute__((interrupt)) __attribute__((aligned(4))) m_mode_handler() { - exception_context_t ctx; - read_exception_context(&ctx); - print_exception_context(&ctx); - - // setup S-mode to go back to 'done' - CSRS("sstatus", SSTATUS_SPP); - set_sepc(&done); - - flag -= 1; - // return to S-mode handler that called us - advance_mepc(4); - asm volatile("mret"); -} - -void __attribute__((interrupt)) __attribute__((aligned(4))) s_mode_handler() { - exception_context_t ctx; - read_exception_context_s(&ctx); - print_exception_context(&ctx); - - if (ctx.cause == EX_ECALL_UMODE) { - ecall(); - } else { - advance_sepc(4); - flag -= 1; - } - - asm volatile("sret"); -} - -noreturn void user_main(void) { - print("A"); // MMIO region is not allowed in PMP, should fail - - // flag = 0; // Flag is protected, should fail - - asm volatile("sret"); // privileged instruction - - uint32_t temp = CSRR("mstatus"); // Machine mode CSR - (void)temp; - - asm volatile("wfi"); // No timeout wait enabled - ecall(); - __builtin_unreachable(); - -} - -int main(void) { - require_supervisor_mode(); - - CSRW("mtvec", (uint32_t) m_mode_handler); - CSRS("mstatus", MSTATUS_MIE); - - CSRW("stvec", (uint32_t) s_mode_handler); - CSRS("sstatus", 0x08); - - uint32_t pmp_addr = ((uint32_t) (&flag)) >> 2; - CSRW("pmpaddr0", pmp_addr); - uint32_t actual_pmp_addr = CSRR("pmpaddr0"); - if (actual_pmp_addr != pmp_addr) { - print("Set PMP granularity down to 4 to run this test!\n"); - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - asm volatile("mret"); - } - pmp_addr = 0xFFFFFFFF; - CSRW("pmpaddr1", pmp_addr); - uint32_t pmp_cfg = 0x00001F17; - CSRW("pmpcfg0", pmp_cfg); - - flag = 5; - - uint32_t medeleg = ~(1 << 9); - CSRW("medeleg", medeleg); - - set_sepc(user_main); - asm volatile("sret"); - - __builtin_unreachable(); -} diff --git a/temp_build/s-mode/tvm_tw_tsr.c b/temp_build/s-mode/tvm_tw_tsr.c deleted file mode 100644 index 6c372e0..0000000 --- a/temp_build/s-mode/tvm_tw_tsr.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include "csr.h" -#include "format.h" -#include "utility.h" - -/* -* tvm_tw_tsr.c - tests S-mode control bits -*/ - -void __attribute__((interrupt)) __attribute__((aligned(4))) handler() { - exception_context_t ctx; - read_exception_context(&ctx); - print_exception_context(&ctx); - - uint32_t satp = CSRR("satp"); - print("satp: %x\n", satp); - print("-----\n"); - - if (ctx.cause == EX_ECALL_SMODE) { - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - } else { - advance_mepc(4); - flag -= 1; - } - asm volatile("mret"); -} - -noreturn void __attribute__((aligned(4))) supervisor_main(void) { - - CSRW("satp", 0x8000FFFF); - - uint32_t temp = CSRR("satp"); - (void)temp; - - asm volatile("sfence.vma"); - asm volatile("wfi"); - asm volatile("sret"); - - ecall(); - - __builtin_unreachable(); -} - -int main(void) { - require_supervisor_mode(); - - CSRW("mtvec", (uint32_t) handler); - uint32_t mstatus_value = (1 << 3) | (1 << 11) | (1 << 20) | (1 << 21) | (1 << 22); - CSRS("mstatus", mstatus_value); - - uint32_t pmp_addr = ((uint32_t) (&flag)) >> 2; - CSRW("pmpaddr0", pmp_addr); - uint32_t actual_pmp_addr = CSRR("pmpaddr0"); - if (actual_pmp_addr != pmp_addr) { - print("Set PMP granularity down to 4 to run this test!\n"); - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - asm volatile("mret"); - } - pmp_addr = 0x20001FFF; - CSRW("pmpaddr1", pmp_addr); - uint32_t pmp_cfg = 0x00001F11; - CSRW("pmpcfg0", pmp_cfg); - - flag = 6; - set_mepc(supervisor_main); - asm volatile("mret"); - - __builtin_unreachable(); -} diff --git a/temp_build/u-mode/csr.c b/temp_build/u-mode/csr.c deleted file mode 100644 index d66d05c..0000000 --- a/temp_build/u-mode/csr.c +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include "csr.h" -#include "format.h" -#include "utility.h" - -void __attribute__((interrupt)) __attribute__((aligned(4))) handler() { - exception_context_t ctx; - read_exception_context(&ctx); - print_exception_context(&ctx); - - if (ctx.cause == EX_ECALL_UMODE) { - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - } else { - advance_mepc(4); - flag -= 1; - } -} - -noreturn void user_main(void) { - uint32_t csr_val; - - asm volatile("csrr %0, cycle" : "=r"(csr_val)); - - asm volatile("csrr %0, time" : "=r"(csr_val)); - - ecall(); - - __builtin_unreachable(); -} - -int main(void) { - if (check_supervisor_mode_available()) { - print("Supervisor is enabled.\n"); - print("Cycle and time CSR reads will fault in user mode.\n"); - print("Cycle and time CSR reads will not fault in superivsor mode.\n"); - flag = 5; - } else { - print("Supervisor is not enabled.\n"); - print("Cycle and time CSR reads will not fault in user mode.\n"); - flag = 3; - } - print("Setting flag to %x\n", flag); - - setup_interrupts_m(handler, 0); - - uint32_t pmp_addr = ((uint32_t) (&flag)) >> 2; - CSRW("pmpaddr0", pmp_addr); - uint32_t actual_pmp_addr = CSRR("pmpaddr0"); - if (actual_pmp_addr != pmp_addr) { - print("Set PMP granularity down to 4 to run this test!\n"); - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - asm volatile("mret"); - } - - pmp_addr = 0x20001FFF; - CSRW("pmpaddr1", pmp_addr); - uint32_t pmp_cfg = 0x00001F11; - CSRW("pmpcfg0", pmp_cfg); - - uint32_t csr_val_0 = 0x0; - CSRW("mcycle", csr_val_0); - csr_val_0 = CSRR("mcycle"); - asm volatile("nop; nop; nop;"); - uint32_t csr_val_1 = CSRR("cycle"); - - if (csr_val_1 - csr_val_0 < 30) { - flag -= 1; - } - - asm volatile("csrw cycle, %0" : : "r"(csr_val_0)); - - set_mepc(user_main); - asm volatile("mret"); - - __builtin_unreachable(); -} diff --git a/temp_build/u-mode/u-mode.c b/temp_build/u-mode/u-mode.c deleted file mode 100644 index 3f156fe..0000000 --- a/temp_build/u-mode/u-mode.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include "csr.h" -#include "format.h" -#include "utility.h" - -void __attribute__((interrupt)) __attribute__((aligned(4))) handler() { - exception_context_t ctx; - read_exception_context(&ctx); - print_exception_context(&ctx); - - if (ctx.cause == EX_ECALL_UMODE) { - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - } else { - advance_mepc(4); - flag -= 1; - } -} - - -noreturn void user_main(void) { - print("A"); - - flag = 0; - - asm volatile("mret"); - - uint32_t temp; - asm volatile("csrr %0, mstatus" : "=r"(temp)); - - asm volatile("wfi"); - - ecall(); - - __builtin_unreachable(); -} - -int main(void) { - setup_interrupts_m(handler, 0); - - uint32_t pmp_addr = ((uint32_t) (&flag)) >> 2; - CSRW("pmpaddr0", pmp_addr); - uint32_t actual_pmp_addr = CSRR("pmpaddr0"); - if (actual_pmp_addr != pmp_addr) { - print("Set PMP granularity down to 4 to run this test!\n"); - CSRS("mstatus", MSTATUS_MPP); - set_mepc(&done); - asm volatile("mret"); - } - - pmp_addr = 0x20001FFF; - CSRW("pmpaddr1", pmp_addr); - uint32_t pmp_cfg = 0x00001F11; - CSRW("pmpcfg0", pmp_cfg); - - flag = 6; - set_mepc(user_main); - asm volatile("mret"); - - __builtin_unreachable(); -}