From 4afcea2a7b565bca180b7529a38139350e97f8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20=C3=85gren?= Date: Tue, 17 Dec 2024 14:38:01 +0100 Subject: [PATCH] PoC: Remove IRQ30 from fw/irqpoc_c_example Removing IRQ30 since it us no longer exist --- .../fw/irqpoc_c_example/main.c | 32 +-------- .../fw/irqpoc_c_example/start.S | 66 +++++++------------ 2 files changed, 26 insertions(+), 72 deletions(-) diff --git a/hw/application_fpga/fw/irqpoc_c_example/main.c b/hw/application_fpga/fw/irqpoc_c_example/main.c index 9531da78..dc2a0d60 100644 --- a/hw/application_fpga/fw/irqpoc_c_example/main.c +++ b/hw/application_fpga/fw/irqpoc_c_example/main.c @@ -10,38 +10,12 @@ // Proof-of-concept firmware for handling syscalls. // This is NOT a best-practice example of secure syscall implementation. -#define SYSCALL_HI (1 << 31) -#define SYSCALL_LO 0 +#define SYSCALL_SET_LED 10 -#define SYSCALL_HI_SET_LED (SYSCALL_HI | 10) -#define SYSCALL_LO_SET_LED (SYSCALL_LO | 10) - -static void delay(int32_t count) { - volatile int32_t c = count; - while (c > 0) { - c--; - } -} - -int32_t syscall_lo_handler(uint32_t syscall_nr, uint32_t arg1) { - switch (syscall_nr) { - case SYSCALL_LO_SET_LED: - set_led(arg1); - //delay(1000000); - return 0; - default: - assert(1 == 2); - } - - assert(1 == 2); - return -1; // This should never run -} - -int32_t syscall_hi_handler(uint32_t syscall_nr, uint32_t arg1) { +int32_t syscall_handler(uint32_t syscall_nr, uint32_t arg1) { switch (syscall_nr) { - case SYSCALL_HI_SET_LED: + case SYSCALL_SET_LED: set_led(arg1); - //delay(500000); return 0; default: assert(1 == 2); diff --git a/hw/application_fpga/fw/irqpoc_c_example/start.S b/hw/application_fpga/fw/irqpoc_c_example/start.S index 016a3d8c..103014cf 100644 --- a/hw/application_fpga/fw/irqpoc_c_example/start.S +++ b/hw/application_fpga/fw/irqpoc_c_example/start.S @@ -4,7 +4,7 @@ */ // This firmware copies an app from ROM to app RAM. -// The app continuosly calls the SET_LED syscalls in firmware (main.c). +// The app continuosly calls the SET_LED syscall in firmware (main.c). // #include "../tk1_mem.h" #include "custom_ops.S" // PicoRV32 custom instructions @@ -30,18 +30,12 @@ _start: irq_handler: // PicoRV32 stores the IRQ bitmask in x4. // If bit 31 is 1: IRQ31 was triggered. - // If bit 30 is 1: IRQ30 was triggered. -irq_syscall_lo_check: - li t4, (1 << 30) - bne x4, t4, irq_syscall_hi_check - - // Run low privilege syscall handler - call syscall_lo_handler - - j irq_source_check_done -irq_syscall_hi_check: li t4, (1 << 31) - bne x4, t4, unexpected_irq + beq x4, t4, irq_source_ok +unexpected_irq_source: + illegal_insn() + j unexpected_irq_source +irq_source_ok: // Save app stack pointer. App is responsible for saving the rest of // the registers. @@ -51,18 +45,14 @@ irq_syscall_hi_check: // Setup firmware stack pointer la sp, FW_STACK_TOP - // Run high privilege syscall handler - call syscall_hi_handler + // Run syscall handler + call syscall_handler // Restore app stack pointer la t0, FW_SP_STORAGE lw sp, 0(t0) - j irq_source_check_done -unexpected_irq: - illegal_insn() -irq_source_check_done: - // Verify that interrupt return address is in app RAM + // Verify that interrupt return address (x3) is in app RAM li t0, TK1_RAM_BASE // 0x40000000 blt x3, t0, x3_invalid li t0, TK1_RAM_BASE + TK1_RAM_SIZE // 0x40020000 @@ -140,10 +130,7 @@ copy_app: // // App // -#define APP_SYSCALL_HI (1 << 31) -#define APP_SYSCALL_LO 0 -#define APP_SYSCALL_HI_SET_LED (APP_SYSCALL_HI | 10) -#define APP_SYSCALL_LO_SET_LED (APP_SYSCALL_LO | 10) +#define SYSCALL_SET_LED 10 .=0x1000 app_start: @@ -151,21 +138,15 @@ app_start: li sp, 0x4001fffc app_loop: - li a0, APP_SYSCALL_LO_SET_LED + li a0, SYSCALL_SET_LED li a1, LED_GREEN call app_syscall + call app_delay - li a0, APP_SYSCALL_LO_SET_LED + li a0, SYSCALL_SET_LED li a1, LED_BLUE call app_syscall - - li a0, APP_SYSCALL_HI_SET_LED - li a1, LED_RED | LED_GREEN - call app_syscall - - li a0, APP_SYSCALL_HI_SET_LED - li a1, LED_RED | LED_BLUE - call app_syscall + call app_delay j app_loop @@ -205,20 +186,12 @@ app_syscall: sw x30, 30*4(sp) sw x31, 31*4(sp) - // Raise interrupt depending on bit 31 in a0 - // 0: Low privilege syscall - // 1: High privilege syscall + // Trigger syscall interrupt li t0, (1 << 31) and t0, a0, t0 - beqz t0, app_syscall_low_priv - li t1, 0xe1000000 // High privilege interrupt - sw zero, 0(t1) // Trigger interrupt - j app_syscall_restore_registers -app_syscall_low_priv: - li t1, 0xe0000000 // Low privelege interrupt + li t1, 0xe1000000 // Syscall interrupt trigger address sw zero, 0(t1) // Trigger interrupt -app_syscall_restore_registers: // Restore registers from stack lw x0, 0*4(sp) lw x1, 1*4(sp) @@ -256,6 +229,13 @@ app_syscall_restore_registers: ret +app_delay: + li t5, 0x100000 +app_delay_dec: + addi t5, t5, -1 + bne t5, zero, app_delay_dec + ret + .align 4 app_end: