Fix: custom signal handler not working

This commit is contained in:
2025-05-06 12:49:44 +08:00
parent d31f5e02fe
commit 7c054743ce
7 changed files with 48 additions and 32 deletions

View File

@@ -47,7 +47,7 @@ typedef struct process {
int32_t current_signal; int32_t current_signal;
signal_handler_t sighandlers[SIGNAL_NUM]; signal_handler_t sighandlers[SIGNAL_NUM];
trapframe_t *regs; trapframe_t *regs, *retregs;
} process_t; } process_t;
// thread_func_t, cast from // thread_func_t, cast from

View File

@@ -9,3 +9,4 @@
typedef void (*signal_handler_t)(); typedef void (*signal_handler_t)();
void run_signal(int32_t SIGNAL); void run_signal(int32_t SIGNAL);
void end_signal(void);

View File

@@ -108,6 +108,8 @@ process_t *fork_process(const process_t *from)
process_t *ret = _make_process(); process_t *ret = _make_process();
memcpy(ret->stack, from->stack, from->stack_size); memcpy(ret->stack, from->stack, from->stack_size);
memcpy(ret->mem, from->mem, from->mem_size); memcpy(ret->mem, from->mem, from->mem_size);
memcpy(ret->sigstack, from->sigstack, from->sigstack_size);
memcpy(ret->sighandlers, from->sighandlers, sizeof(from->sighandlers));
*ret->regs = (trapframe_t){ *ret->regs = (trapframe_t){
.x0 = 0, // fork return value .x0 = 0, // fork return value
.fp = from->regs->fp - (uint64_t)from->stack + (uint64_t)ret->stack, .fp = from->regs->fp - (uint64_t)from->stack + (uint64_t)ret->stack,

View File

@@ -27,11 +27,9 @@ void _sigkill_default_handler()
void run_signal(int32_t SIGNAL) void run_signal(int32_t SIGNAL)
{ {
LOG("run_signal"); DEBUG_SIGNAL(SIGNAL);
process_t *process = get_current()->process; process_t *process = get_current()->process;
process->current_signal = SIGNAL; process->current_signal = SIGNAL;
trapframe_t *regs = kmalloc(sizeof(trapframe_t)); LOG("run_signal"); LOG(process->sighandlers[SIGNAL]); DEBUG_SIGNAL(SIGNAL);
memcpy(regs, process->regs, sizeof(trapframe_t));
if (!process->sighandlers[SIGNAL]) { if (!process->sighandlers[SIGNAL]) {
switch (SIGNAL) { switch (SIGNAL) {
@@ -40,20 +38,39 @@ void run_signal(int32_t SIGNAL)
break; break;
default: default:
LOG("No default handler for this signal"); DEBUG_SIGNAL(SIGNAL); LOG("No default handler for this signal"); DEBUG_SIGNAL(SIGNAL);
return;
} }
return;
} }
*regs = (trapframe_t){ DEBUG_SIGNAL(process->sighandlers[SIGNAL]);
process->retregs = process->regs;
process->regs = kmalloc(sizeof(trapframe_t));
memcpy(process->regs, process->retregs, sizeof(trapframe_t));
*process->regs = (trapframe_t){
.x0 = (uint64_t)process->sighandlers[SIGNAL], .x0 = (uint64_t)process->sighandlers[SIGNAL],
.elr_el1 = (uint64_t)_sighandler_wrapper, .elr_el1 = (uint64_t)_sighandler_wrapper,
.sp_el0 = ((uint64_t)process->sigstack + process->sigstack_size - 64), .sp_el0 = ((uint64_t)process->sigstack + process->sigstack_size - 64),
}; };
disable_interrupt(); disable_interrupt();
process->current_signal = 0x0; process->current_signal = SIGNAL;
process->sigstate[SIGNAL] = false; process->sigstate[SIGNAL] = false;
user_exec(regs); user_exec(process->regs);
__builtin_unreachable();
}
void end_signal()
{
DEBUG_SIGNAL("end_signal");
process_t *process = get_current()->process;
disable_interrupt();
process->current_signal = 0x0;
kfree(process->regs);
process->regs = process->retregs;
user_exec(process->regs);
__builtin_unreachable(); __builtin_unreachable();
} }

View File

@@ -5,6 +5,7 @@
#include <process.h> #include <process.h>
#include <mbox.h> #include <mbox.h>
#include <exec.h> #include <exec.h>
#include <signal.h>
void syscall_handler(trapframe_t *regs) void syscall_handler(trapframe_t *regs)
{ {
@@ -110,8 +111,4 @@ void sys_kill(int32_t pid, int32_t SIGNAL)
} }
void sys_sigreturn(void) void sys_sigreturn(void)
{ { end_signal(); }
DEBUG_SIGNAL("sigreturn");
thread_t *th = get_current();
user_exec(th->process->regs);
}

View File

@@ -42,8 +42,11 @@ void _init(void *dtb)
void main(void *dtb) void main(void *dtb)
{ {
INFOR("kernel running");
_init(dtb); _init(dtb);
INFOR("Run user process syscall.img");
run_thread(run_process_by_name, (uint64_t)&"syscall.img"); run_thread(run_process_by_name, (uint64_t)&"syscall.img");
schedule(); schedule();

View File

@@ -9,26 +9,22 @@ if len(sys.argv) < 2:
print("no args for serial device") print("no args for serial device")
exit() exit()
while True: kernel = open("kernel8.img", "rb").read()
kernel = open("kernel8.img", "rb").read() r = serialtube(sys.argv[1], convert_newlines=False)
r = serialtube(sys.argv[1], convert_newlines=False)
# send size # send size
sz = len(kernel) sz = len(kernel)
print("size:", sz) print("size:", sz)
r.send(pack("<I", sz)) r.send(pack("<I", sz))
print(r.recvuntil(b"$").decode()) print(r.recvuntil(b"$").decode())
# send kernel # send kernel
splitsize = 1000 splitsize = 1000
for i in tqdm(range(len(kernel) // splitsize), unit="KB"): for i in tqdm(range(len(kernel) // splitsize), unit="KB"):
r.send(kernel[i * splitsize : (i + 1) * splitsize]) r.send(kernel[i * splitsize : (i + 1) * splitsize])
r.send(kernel[len(kernel) // splitsize * splitsize:]) r.send(kernel[len(kernel) // splitsize * splitsize:])
sleep(1) sleep(1)
r.send(b"\n") r.send(b"\n")
r.interactive() r.interactive()
r.send(b"\nreboot\n")
sleep(10)