From 4ba164732d5f28e7a5fc26ac4714efc8cbc49601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20=C3=85gren?= Date: Wed, 19 Feb 2025 17:00:14 +0100 Subject: [PATCH] testapp: Add syscalls --- hw/application_fpga/fw/testapp/Makefile | 1 + hw/application_fpga/fw/testapp/main.c | 2 + hw/application_fpga/fw/testapp/syscall.S | 85 ++++++++++++++++++++++++ hw/application_fpga/fw/testapp/syscall.h | 11 +++ 4 files changed, 99 insertions(+) create mode 100644 hw/application_fpga/fw/testapp/syscall.S create mode 100644 hw/application_fpga/fw/testapp/syscall.h diff --git a/hw/application_fpga/fw/testapp/Makefile b/hw/application_fpga/fw/testapp/Makefile index 8e1e1c6..40c694c 100644 --- a/hw/application_fpga/fw/testapp/Makefile +++ b/hw/application_fpga/fw/testapp/Makefile @@ -43,6 +43,7 @@ all: testapp.bin TESTAPP_FMTFILES = \ $(P)/main.c \ + $(P)/syscall.h TESTAPP_OBJS = \ $(P)/main.o \ diff --git a/hw/application_fpga/fw/testapp/main.c b/hw/application_fpga/fw/testapp/main.c index c2a84b6..cc0b3f3 100644 --- a/hw/application_fpga/fw/testapp/main.c +++ b/hw/application_fpga/fw/testapp/main.c @@ -6,8 +6,10 @@ #include "../tk1/led.h" #include "../tk1/lib.h" #include "../tk1/proto.h" +#include "../tk1/syscall_num.h" #include "../tk1/types.h" #include "../tk1_mem.h" +#include "syscall.h" #define USBMODE_PACKET_SIZE 64 diff --git a/hw/application_fpga/fw/testapp/syscall.S b/hw/application_fpga/fw/testapp/syscall.S new file mode 100644 index 0000000..5fc9b3f --- /dev/null +++ b/hw/application_fpga/fw/testapp/syscall.S @@ -0,0 +1,85 @@ +// SPDX-FileCopyrightText: 2024 Tillitis AB +// SPDX-License-Identifier: BSD-2-Clause + +#include "../tk1/picorv32/custom_ops.S" + + .section ".text" + .globl syscall + + +syscall: + // Save registers to stack + addi sp, sp, -32*4 + sw x0, 0*4(sp) + sw x1, 1*4(sp) + // x2 (sp) is assumed to be preserved by the interrupt handler. + sw x3, 3*4(sp) + sw x4, 4*4(sp) + sw x5, 5*4(sp) + sw x6, 6*4(sp) + sw x7, 7*4(sp) + sw x8, 8*4(sp) + sw x9, 9*4(sp) + // x10 (a0) will contain syscall return value. And should not be saved. + sw x11, 11*4(sp) + sw x12, 12*4(sp) + sw x13, 13*4(sp) + sw x14, 14*4(sp) + sw x15, 15*4(sp) + sw x16, 16*4(sp) + sw x17, 17*4(sp) + sw x18, 18*4(sp) + sw x19, 19*4(sp) + sw x20, 20*4(sp) + sw x21, 21*4(sp) + sw x22, 22*4(sp) + sw x23, 23*4(sp) + sw x24, 24*4(sp) + sw x25, 25*4(sp) + sw x26, 26*4(sp) + sw x27, 27*4(sp) + sw x28, 28*4(sp) + sw x29, 29*4(sp) + sw x30, 30*4(sp) + sw x31, 31*4(sp) + + // Trigger syscall interrupt + li t1, 0xe1000000 // Syscall interrupt trigger address + sw zero, 0(t1) // Trigger interrupt + + // Restore registers from stack + lw x0, 0*4(sp) + lw x1, 1*4(sp) + // x2 (sp) is assumed to be preserved by the interrupt handler. + lw x3, 3*4(sp) + lw x4, 4*4(sp) + lw x5, 5*4(sp) + lw x6, 6*4(sp) + lw x7, 7*4(sp) + lw x8, 8*4(sp) + lw x9, 9*4(sp) + // x10 (a0) contains syscall return value. And should not be destroyed. + lw x11, 11*4(sp) + lw x12, 12*4(sp) + lw x13, 13*4(sp) + lw x14, 14*4(sp) + lw x15, 15*4(sp) + lw x16, 16*4(sp) + lw x17, 17*4(sp) + lw x18, 18*4(sp) + lw x19, 19*4(sp) + lw x20, 20*4(sp) + lw x21, 21*4(sp) + lw x22, 22*4(sp) + lw x23, 23*4(sp) + lw x24, 24*4(sp) + lw x25, 25*4(sp) + lw x26, 26*4(sp) + lw x27, 27*4(sp) + lw x28, 28*4(sp) + lw x29, 29*4(sp) + lw x30, 30*4(sp) + lw x31, 31*4(sp) + addi sp, sp, 32*4 + + ret diff --git a/hw/application_fpga/fw/testapp/syscall.h b/hw/application_fpga/fw/testapp/syscall.h new file mode 100644 index 0000000..fa27f22 --- /dev/null +++ b/hw/application_fpga/fw/testapp/syscall.h @@ -0,0 +1,11 @@ +// SPDX-FileCopyrightText: 2024 Tillitis AB +// SPDX-License-Identifier: BSD-2-Clause + +#include "../tk1/types.h" + +#ifndef TKEY_APP_SYSCALL_H +#define TKEY_APP_SYSCALL_H + +int syscall(uint32_t number, uint32_t arg1); + +#endif