kernel/arm: implement signals, fork, restart syscall

Change-Id: I24219b83d90710ef719190183ba6f069f82dae61
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6198
Tested-by: Automation <automation@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
This commit is contained in:
David Karoly 2023-03-18 12:17:21 +01:00
parent 5865e68ef2
commit 60b19d7eac
8 changed files with 209 additions and 39 deletions

View File

@ -30,7 +30,9 @@ struct vregs
ulong r14; /* link register */
ulong r15; /* program counter */
ulong cpsr;
// TODO: ARM: fix floats in vregs, add missing stuff.
double d[32];
ulong fpscr;
};
#endif /* defined(__arm__) */

View File

@ -24,30 +24,6 @@
#include <arch/arm/arch_thread_types.h>
#include <kernel.h>
/* raw exception frames */
struct iframe {
uint32 spsr;
uint32 r0;
uint32 r1;
uint32 r2;
uint32 r3;
uint32 r4;
uint32 r5;
uint32 r6;
uint32 r7;
uint32 r8;
uint32 r9;
uint32 r10;
uint32 r11;
uint32 r12;
uint32 usr_sp;
uint32 usr_lr;
uint32 svc_sp;
uint32 svc_lr;
uint32 pc;
} _PACKED;
/**! Values for arch_cpu_info.arch */
enum {
ARCH_ARM_PRE_ARM7,

View File

@ -7,6 +7,30 @@
#include <kernel.h>
/* raw exception frames */
struct iframe {
uint32 spsr;
uint32 r0;
uint32 r1;
uint32 r2;
uint32 r3;
uint32 r4;
uint32 r5;
uint32 r6;
uint32 r7;
uint32 r8;
uint32 r9;
uint32 r10;
uint32 r11;
uint32 r12;
uint32 usr_sp;
uint32 usr_lr;
uint32 svc_sp;
uint32 svc_lr;
uint32 pc;
} _PACKED;
#define IFRAME_TRACE_DEPTH 4
struct iframe_stack {
@ -26,6 +50,9 @@ struct arch_thread {
// used to track interrupts on this thread
struct iframe_stack iframes;
struct iframe* userFrame;
uint32 oldR0;
};
struct arch_team {
@ -36,10 +63,7 @@ struct arch_team {
};
struct arch_fork_arg {
// gcc treats empty structures as zero-length in C, but as if they contain
// a char in C++. So we have to put a dummy in to be able to use the struct
// from both in a consistent way.
char dummy;
struct iframe frame;
};

View File

@ -1,5 +1,5 @@
/*
* Copyright 2022, Haiku Inc. All rights reserved.
* Copyright 2022-2023, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Copyright 2007, Travis Geiselbrecht. All rights reserved.
@ -12,6 +12,7 @@
# error Must not be included directly. Include <commpage_defs.h> instead!
#endif
#define COMMPAGE_ENTRY_ARM_THREAD_EXIT (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 0)
#define COMMPAGE_ENTRY_ARM_SIGNAL_HANDLER (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 0)
#define COMMPAGE_ENTRY_ARM_THREAD_EXIT (COMMPAGE_ENTRY_FIRST_ARCH_SPECIFIC + 1)
#endif /* _SYSTEM_ARCH_ARM_COMMPAGE_DEFS_H */

View File

@ -67,5 +67,5 @@ KernelMergeObject kernel_arch_arm.o :
CreateAsmStructOffsetsHeader asm_offsets.h : asm_offsets.cpp : $(TARGET_KERNEL_ARCH) ;
# syscall headers are generated on the fly, so we need a explicit dependency.
Includes [ FGristFiles arch_int.cpp arch_asm.S ]
Includes [ FGristFiles arch_commpage.cpp arch_int.cpp arch_asm.S ]
: <syscalls!$(TARGET_PACKAGING_ARCH)>syscall_numbers.h ;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2022, Haiku Inc. All rights reserved.
* Copyright 2022-2023, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Copyright 2009, Johannes Wischert, johanneswi@gmail.com.
@ -17,10 +17,34 @@
#include <elf.h>
#include <smp.h>
#include "syscall_numbers.h"
extern "C" void arch_user_thread_exit();
extern "C" void __attribute__((noreturn))
arch_user_signal_handler(signal_frame_data* data)
{
if (data->siginfo_handler) {
auto handler = (void (*)(int, siginfo_t*, void*, void*))data->handler;
handler(data->info.si_signo, &data->info, &data->context, data->user_data);
} else {
auto handler = (void (*)(int, void*, vregs*))data->handler;
handler(data->info.si_signo, data->user_data, &data->context.uc_mcontext);
}
// _kern_restore_signal_frame(data)
asm volatile(
"mov r0, %[data];"
"svc %[syscall_num]"
:: [data] "r"(data), [syscall_num] "i" (SYSCALL_RESTORE_SIGNAL_FRAME)
);
__builtin_unreachable();
}
static void
register_commpage_function(const char* functionName, int32 commpageIndex,
const char* commpageSymbolName, addr_t expectedAddress)
@ -55,10 +79,13 @@ arch_commpage_init(void)
status_t
arch_commpage_init_post_cpus(void)
{
register_commpage_function("arch_user_signal_handler",
COMMPAGE_ENTRY_ARM_SIGNAL_HANDLER, "commpage_signal_handler",
(addr_t)&arch_user_signal_handler);
register_commpage_function("arch_user_thread_exit",
COMMPAGE_ENTRY_ARM_THREAD_EXIT, "commpage_thread_exit",
(addr_t)&arch_user_thread_exit);
return B_OK;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2022, Haiku Inc. All rights reserved.
* Copyright 2003-2023, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -276,6 +276,10 @@ arch_arm_syscall(struct iframe *iframe)
}
}
thread_get_current_thread()->arch_info.userFrame = iframe;
thread_get_current_thread()->arch_info.oldR0 = iframe->r0;
thread_at_kernel_entry(system_time());
enable_interrupts();
uint64 returnValue = 0;
@ -283,6 +287,24 @@ arch_arm_syscall(struct iframe *iframe)
TRACE("returning %" B_PRId64 "\n", returnValue);
iframe->r0 = returnValue;
disable_interrupts();
atomic_and(&thread_get_current_thread()->flags, ~THREAD_FLAGS_SYSCALL_RESTARTED);
if ((thread_get_current_thread()->flags & (THREAD_FLAGS_SIGNALS_PENDING
| THREAD_FLAGS_DEBUG_THREAD | THREAD_FLAGS_TRAP_FOR_CORE_DUMP)) != 0) {
enable_interrupts();
thread_at_kernel_exit();
} else {
thread_at_kernel_exit_no_signals();
}
if ((thread_get_current_thread()->flags & THREAD_FLAGS_RESTART_SYSCALL) != 0) {
atomic_and(&thread_get_current_thread()->flags, ~THREAD_FLAGS_RESTART_SYSCALL);
atomic_or(&thread_get_current_thread()->flags, THREAD_FLAGS_SYSCALL_RESTARTED);
iframe->r0 = thread_get_current_thread()->arch_info.oldR0;
iframe->pc -= 4;
}
thread_get_current_thread()->arch_info.userFrame = NULL;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2022, Haiku Inc. All rights reserved.
* Copyright 2003-2023, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -234,7 +234,31 @@ arch_thread_enter_userspace(Thread *thread, addr_t entry,
bool
arch_on_signal_stack(Thread *thread)
{
return false;
struct iframe* frame = thread->arch_info.userFrame;
if (frame == NULL) {
panic("arch_on_signal_stack(): No user iframe!");
return false;
}
return frame->usr_sp >= thread->signal_stack_base
&& frame->usr_sp < thread->signal_stack_base
+ thread->signal_stack_size;
}
static uint8*
get_signal_stack(Thread* thread, struct iframe* frame,
struct sigaction* action, size_t spaceNeeded)
{
// use the alternate signal stack if we should and can
if (thread->signal_stack_enabled && (action->sa_flags & SA_ONSTACK) != 0
&& (frame->usr_sp < thread->signal_stack_base
|| frame->usr_sp >= thread->signal_stack_base + thread->signal_stack_size)) {
addr_t stackTop = thread->signal_stack_base + thread->signal_stack_size;
return (uint8*)ROUNDDOWN(stackTop - spaceNeeded, 16);
}
return (uint8*)ROUNDDOWN(frame->usr_sp - spaceNeeded, 16);
}
@ -242,14 +266,99 @@ status_t
arch_setup_signal_frame(Thread *thread, struct sigaction *sa,
struct signal_frame_data *signalFrameData)
{
return B_ERROR;
iframe* frame = thread->arch_info.userFrame;
if (frame == NULL) {
panic("arch_setup_signal_frame(): No user iframe!");
return B_ERROR;
}
// store the register state in signalFrameData->context.uc_mcontext
signalFrameData->context.uc_mcontext.r0 = frame->r0;
signalFrameData->context.uc_mcontext.r1 = frame->r1;
signalFrameData->context.uc_mcontext.r2 = frame->r2;
signalFrameData->context.uc_mcontext.r3 = frame->r3;
signalFrameData->context.uc_mcontext.r4 = frame->r4;
signalFrameData->context.uc_mcontext.r5 = frame->r5;
signalFrameData->context.uc_mcontext.r6 = frame->r6;
signalFrameData->context.uc_mcontext.r7 = frame->r7;
signalFrameData->context.uc_mcontext.r8 = frame->r8;
signalFrameData->context.uc_mcontext.r9 = frame->r9;
signalFrameData->context.uc_mcontext.r10 = frame->r10;
signalFrameData->context.uc_mcontext.r11 = frame->r11;
signalFrameData->context.uc_mcontext.r12 = frame->r12;
signalFrameData->context.uc_mcontext.r13 = frame->usr_sp;
signalFrameData->context.uc_mcontext.r14 = frame->usr_lr;
signalFrameData->context.uc_mcontext.r15 = frame->pc;
signalFrameData->context.uc_mcontext.cpsr = frame->spsr;
arm_save_fpu((arch_fpu_context*)&signalFrameData->context.uc_mcontext.d[0]);
// Fill in signalFrameData->context.uc_stack
signal_get_user_stack(frame->usr_sp, &signalFrameData->context.uc_stack);
// store oldR0 in syscall_restart_return_value
signalFrameData->syscall_restart_return_value = thread->arch_info.oldR0;
// get the stack to use -- that's either the current one or a special signal stack
uint8* userStack = get_signal_stack(thread, frame, sa,
sizeof(*signalFrameData));
// copy the signal frame data onto the stack
status_t res = user_memcpy(userStack, signalFrameData,
sizeof(*signalFrameData));
if (res < B_OK)
return res;
// prepare the user stack frame for a function call to the signal handler wrapper function
addr_t commpageAddr = (addr_t)thread->team->commpage_address;
addr_t signalHandlerAddr;
ASSERT(user_memcpy(&signalHandlerAddr,
&((addr_t*)commpageAddr)[COMMPAGE_ENTRY_ARM_SIGNAL_HANDLER],
sizeof(signalHandlerAddr)) >= B_OK);
signalHandlerAddr += commpageAddr;
frame->usr_lr = frame->pc;
frame->usr_sp = (addr_t)userStack;
frame->pc = signalHandlerAddr;
frame->r0 = frame->usr_sp;
return B_OK;
}
int64
arch_restore_signal_frame(struct signal_frame_data* signalFrameData)
{
return 0;
iframe* frame = thread_get_current_thread()->arch_info.userFrame;
if (frame == NULL) {
panic("arch_restore_signal_frame(): No user iframe!");
return 0;
}
thread_get_current_thread()->arch_info.oldR0
= signalFrameData->syscall_restart_return_value;
frame->r0 = signalFrameData->context.uc_mcontext.r0;
frame->r1 = signalFrameData->context.uc_mcontext.r1;
frame->r2 = signalFrameData->context.uc_mcontext.r2;
frame->r3 = signalFrameData->context.uc_mcontext.r3;
frame->r4 = signalFrameData->context.uc_mcontext.r4;
frame->r5 = signalFrameData->context.uc_mcontext.r5;
frame->r6 = signalFrameData->context.uc_mcontext.r6;
frame->r7 = signalFrameData->context.uc_mcontext.r7;
frame->r8 = signalFrameData->context.uc_mcontext.r8;
frame->r9 = signalFrameData->context.uc_mcontext.r9;
frame->r10 = signalFrameData->context.uc_mcontext.r10;
frame->r11 = signalFrameData->context.uc_mcontext.r11;
frame->r12 = signalFrameData->context.uc_mcontext.r12;
frame->usr_sp = signalFrameData->context.uc_mcontext.r13;
frame->usr_lr = signalFrameData->context.uc_mcontext.r14;
frame->pc = signalFrameData->context.uc_mcontext.r15;
frame->spsr = signalFrameData->context.uc_mcontext.cpsr;
arm_restore_fpu((arch_fpu_context*)&signalFrameData->context.uc_mcontext.d[0]);
return frame->r0;
}
@ -266,6 +375,13 @@ arch_check_syscall_restart(Thread *thread)
void
arch_store_fork_frame(struct arch_fork_arg *arg)
{
struct iframe* frame = thread_get_current_thread()->arch_info.userFrame;
if (frame == NULL) {
panic("arch_store_fork_frame(): No user iframe!");
}
arg->frame = *frame;
arg->frame.r0 = 0; // fork return value
}
@ -279,4 +395,6 @@ arch_store_fork_frame(struct arch_fork_arg *arg)
void
arch_restore_fork_frame(struct arch_fork_arg *arg)
{
disable_interrupts();
arch_return_to_userland(&arg->frame);
}