Skip to content

Commit

Permalink
PoC: Remove IRQ30 from fw/irqpoc_c_example
Browse files Browse the repository at this point in the history
Removing IRQ30 since it us no longer exist
  • Loading branch information
agren committed Dec 17, 2024
1 parent c9ae734 commit 4afcea2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 72 deletions.
32 changes: 3 additions & 29 deletions hw/application_fpga/fw/irqpoc_c_example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 23 additions & 43 deletions hw/application_fpga/fw/irqpoc_c_example/start.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -140,32 +130,23 @@ 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:
// Set stack pointer to end of app RAM
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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:

0 comments on commit 4afcea2

Please sign in to comment.