Modify syscall

This commit is contained in:
songyanguang 2025-01-15 17:06:57 +08:00
parent 1631e60baa
commit 5190dadd12
5 changed files with 73 additions and 45 deletions

View File

@ -211,8 +211,8 @@ extern int syscall(int sys_num, uintptr_t param1, uintptr_t param2, uintptr_t pa
__attribute__((__always_inline__)) static inline int arch_syscall(struct trapframe* tf, int* syscall_num) __attribute__((__always_inline__)) static inline int arch_syscall(struct trapframe* tf, int* syscall_num)
{ {
// call syscall // call syscall
*syscall_num = tf->a0; *syscall_num = tf->a7;
return syscall(*syscall_num, tf->a1, tf->a2, tf->a3, tf->a4); return syscall(*syscall_num, tf->a0, tf->a1, tf->a2, tf->a3);
} }
/// @brief set return reg to trapframe /// @brief set return reg to trapframe

View File

@ -78,42 +78,20 @@ void kernel_intr_handler(struct trapframe* tf)
extern void context_switch(struct context**, struct context*); extern void context_switch(struct context**, struct context*);
void syscall_arch_handler(struct trapframe* tf) void syscall_arch_handler(struct trapframe* tf)
{ {
uint64_t ec = tf->cause;
uint64_t esr = r_esr_el1();
uint64_t ec = (esr >> 0x1A) & 0x3F;
switch (ec) { switch (ec) {
case 0B010101: case EXC_SYSCALL:
software_irq_dispatch(tf); software_irq_dispatch(tf);
break; break;
case 0b100100:
case 0b100101:
dabort_handler(tf);
break;
case 0b100000:
case 0b100001:
iabort_handler(tf);
break;
default: { default: {
ERROR("USYSCALL: unexpected\n"); ERROR("USYSCALL: unexpected\n");
ERROR(" esr: %016lx\n", esr); ERROR("tf->cause: %016lx\n", tf->cause);
ERROR(" elr = %016lx far = %016lx\n", r_elr_el1(), r_far_el1());
w_esr_el1(0);
extern void dump_tf(struct trapframe * tf); extern void dump_tf(struct trapframe * tf);
dump_tf(tf); dump_tf(tf);
uint32_t sctlr = 0;
SCTLR_R(sctlr);
DEBUG("SCTLR: %x\n", sctlr);
uint32_t spsr = 0;
// __asm__ volatile("mrs %0, spsr_el1" : "=r"(spsr)::"memory");
DEBUG("SPSR: %x\n", spsr);
uint64_t tcr = 0;
// __asm__ volatile("mrs %0, tcr_el1" : "=r"(tcr)::"memory");
DEBUG("TCR: %x\n", tcr);
uint64_t mair = 0;
// __asm__ volatile("mrs %0, mair_el1" : "=r"(mair)::"memory");
DEBUG("MAIR: %x\n", mair);
// kill error task // kill error task
xizi_enter_kernel(); xizi_enter_kernel();
assert(cur_cpu()->task != NULL); assert(cur_cpu()->task != NULL);

View File

@ -151,7 +151,41 @@ _save_context:
tail do_exception tail do_exception
handle_syscall: handle_syscall:
j . /* save the initial A0 value (needed in signal handlers) */
REG_S a0, PT_ORIG_A0(sp)
/*
* Advance SEPC to avoid executing the original
* scall instruction on sret
*/
addi s2, s2, 0x4
REG_S s2, PT_EPC(sp)
/* Trace syscalls, but only if requested by the user. */
j handle_syscall_trace_enter
ret
/* Slow paths for ptrace. */
handle_syscall_trace_enter:
move a0, sp
//call do_syscall_trace_enter
call syscall_arch_handler
move t0, a0
REG_L a0, PT_A0(sp)
REG_L a1, PT_A1(sp)
REG_L a2, PT_A2(sp)
REG_L a3, PT_A3(sp)
REG_L a4, PT_A4(sp)
REG_L a5, PT_A5(sp)
REG_L a6, PT_A6(sp)
REG_L a7, PT_A7(sp)
//bnez t0, ret_from_syscall_rejected
//j check_syscall_nr
handle_syscall_trace_exit:
move a0, sp
//call do_syscall_trace_exit
j ret_from_exception
ret_from_exception: ret_from_exception:

View File

@ -11,22 +11,33 @@
*/ */
#include "usyscall.h" #include "usyscall.h"
int syscall(int sys_num, intptr_t a1, intptr_t a2, intptr_t a3, intptr_t a4) int sbi_ecall(int ext, int fid, unsigned long arg0,
unsigned long arg1, unsigned long arg2,
unsigned long arg3, unsigned long arg4,
unsigned long arg5)
{ {
int ret = -1; int ret;
__asm__ volatile( register uintptr_t a0 __asm__ ("a0") = (uintptr_t)(arg0);
"mv a0, %1;\ register uintptr_t a1 __asm__ ("a1") = (uintptr_t)(arg1);
mv a1, %2;\ register uintptr_t a2 __asm__ ("a2") = (uintptr_t)(arg2);
mv a2, %3;\ register uintptr_t a3 __asm__ ("a3") = (uintptr_t)(arg3);
mv a3, %4;\ register uintptr_t a4 __asm__ ("a4") = (uintptr_t)(arg4);
mv a4, %5;\ register uintptr_t a5 __asm__ ("a5") = (uintptr_t)(arg5);
ecall;\ register uintptr_t a6 __asm__ ("a6") = (uintptr_t)(fid);
mv %0, a0" register uintptr_t a7 __asm__ ("a7") = (uintptr_t)(ext);
: "=r"(ret) __asm__ volatile ("ecall"
: "r"(sys_num), "r"(a1), "r"(a2), "r"(a3), "r"(a4) : "+r" (a0), "+r" (a1)
: "memory", "a0", "a1", "a2", "a3", "a4" : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
); : "memory");
ret = a0;
return ret; return ret;
} }
int syscall(int sys_num, intptr_t a1, intptr_t a2, intptr_t a3, intptr_t a4)
{
int ret = -1;
ret = sbi_ecall(sys_num, 0, a1, a2, a3, a4, 0, 0);
return ret;
}

View File

@ -153,8 +153,13 @@ int sys_state(sys_state_option option, sys_state_info* info)
{ {
switch (option) { switch (option) {
case SYS_STATE_MEMBLOCK_INFO: { case SYS_STATE_MEMBLOCK_INFO: {
#ifndef __riscv
info->memblock_info.memblock_start = (uintptr_t)V2P(_binary_fs_img_start); info->memblock_info.memblock_start = (uintptr_t)V2P(_binary_fs_img_start);
info->memblock_info.memblock_end = (uintptr_t)V2P(_binary_fs_img_end); info->memblock_info.memblock_end = (uintptr_t)V2P(_binary_fs_img_end);
#else
info->memblock_info.memblock_start = (uintptr_t)V2P_LINK(_binary_fs_img_start);
info->memblock_info.memblock_end = (uintptr_t)V2P_LINK(_binary_fs_img_end);
#endif
break; break;
} }
case SYS_STATE_GET_HEAP_BASE: case SYS_STATE_GET_HEAP_BASE: