Skip to content

Commit

Permalink
rework signal handling
Browse files Browse the repository at this point in the history
JIRA: RTOS-539
  • Loading branch information
lukileczo committed Aug 10, 2023
1 parent 836585e commit 9b15c86
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 82 deletions.
2 changes: 1 addition & 1 deletion hal/armv7a/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ _syscalls_dispatch:

blx syscalls_dispatch

cpsid if
/* Interrupts disabled by dispatcher */

str r0, [sp, #272]

Expand Down
44 changes: 33 additions & 11 deletions hal/armv7a/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,50 @@ int hal_cpuCreateContext(cpu_context_t **nctx, void *start, void *kstack, size_t
}


int hal_cpuPushSignal(void *kstack, void (*handler)(void), int n)
int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int n)
{
cpu_context_t *ctx = (void *)((char *)kstack - sizeof(cpu_context_t));

/* No signal handling inside IT block */
if (ctx->psr & 0x600fc00)
return -1;
hal_memcpy(signalCtx, ctx, sizeof(cpu_context_t));

PUTONSTACK(ctx->sp, u32, ctx->pc | !!(ctx->psr & THUMB_STATE));
PUTONSTACK(ctx->sp, int, n);
signalCtx->pc = (u32)handler & ~1;
signalCtx->sp -= sizeof(cpu_context_t);

ctx->pc = (u32)handler & ~1;
if (((u32)handler & 2) != 0) {
signalCtx->psr |= THUMB_STATE;
}
else {
signalCtx->psr &= ~THUMB_STATE;
}

if ((u32)handler & 1)
ctx->psr |= THUMB_STATE;
else
ctx->psr &= ~THUMB_STATE;
PUTONSTACK(signalCtx->sp, u32, ctx->sp);
PUTONSTACK(signalCtx->sp, u32, ctx->pc);
PUTONSTACK(signalCtx->sp, cpu_context_t *, signalCtx);
PUTONSTACK(signalCtx->sp, int, n);

return 0;
}


void hal_cpuSigreturn(void *ustack, cpu_context_t *ctx)
{
u32 pc, sp;

GETFROMSTACK(ustack, u32, pc, 2);
GETFROMSTACK(ustack, u32, sp, 3);

ctx->pc = pc;
ctx->sp = sp;

if ((pc & 2) != 0) {
ctx->psr |= THUMB_STATE;
}
else {
ctx->psr &= ~THUMB_STATE;
}
}


char *hal_cpuInfo(char *info)
{
size_t n = 0;
Expand Down
6 changes: 6 additions & 0 deletions hal/armv7a/spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ void hal_spinlockDestroy(spinlock_t *spinlock)
}


void hal_spinlockCtxIrqClr(spinlock_ctx_t *sc)
{
*sc |= NO_INT;
}


__attribute__ ((section (".init"))) void _hal_spinlockInit(void)
{
spinlocks.first = NULL;
Expand Down
9 changes: 7 additions & 2 deletions hal/armv7m/arch/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,14 @@ static inline int hal_cpuSupervisorMode(cpu_context_t *ctx)
}


static inline int hal_cpuPushSignal(void *kstack, void (*handler)(void), int sig)
static inline int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int sig)
{
return -1;
}


static inline void hal_cpuSigreturn(void *ustack, cpu_context_t *ctx)
{
return 0;
}


Expand Down
6 changes: 6 additions & 0 deletions hal/armv7m/spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ void hal_spinlockDestroy(spinlock_t *spinlock)
}


void hal_spinlockCtxIrqClr(spinlock_ctx_t *sc)
{
*sc = 1;
}


__attribute__ ((section (".init"))) void _hal_spinlockInit(void)
{
spinlocks.first = NULL;
Expand Down
5 changes: 4 additions & 1 deletion hal/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ extern void *hal_cpuGetUserSP(cpu_context_t *ctx);
extern int hal_cpuSupervisorMode(cpu_context_t *ctx);


extern int hal_cpuPushSignal(void *kstack, void (*handler)(void), int n);
extern int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int n);


extern void hal_cpuSigreturn(void *ustack, cpu_context_t *ctx);


extern void hal_longjmp(cpu_context_t *ctx);
Expand Down
2 changes: 1 addition & 1 deletion hal/ia32/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ _interrupts_syscall:
pushl %eax
sti
call syscalls_dispatch
cli
/* Interrupts disabled by dispatcher */
addl $8, %esp
movl %eax, (4 * CTXPUSHL - EAX_OFFSET)(%esp) /* Save return value to eax in context */
jmp interrupts_popContext
Expand Down
27 changes: 21 additions & 6 deletions hal/ia32/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,36 @@ void _hal_cpuSetKernelStack(void *kstack)
}


int hal_cpuPushSignal(void *kstack, void (*handler)(void), int n)
int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int n)
{
cpu_context_t *ctx = (void *)((char *)kstack - sizeof(cpu_context_t));
char *ustack = (char *)ctx->esp;

PUTONSTACK(ustack, u32, ctx->eip);
PUTONSTACK(ustack, int, n);
hal_memcpy(signalCtx, ctx, sizeof(cpu_context_t));

ctx->eip = (u32)handler;
ctx->esp = (u32)ustack;
signalCtx->eip = (u32)handler;
signalCtx->esp -= sizeof(cpu_context_t);

PUTONSTACK(signalCtx->esp, u32, ctx->esp);
PUTONSTACK(signalCtx->esp, u32, ctx->eip);
PUTONSTACK(signalCtx->esp, cpu_context_t *, signalCtx);
PUTONSTACK(signalCtx->esp, int, n);

return 0;
}


void hal_cpuSigreturn(void *ustack, cpu_context_t *ctx)
{
u32 eip, esp;

GETFROMSTACK(ustack, u32, eip, 2);
GETFROMSTACK(ustack, u32, esp, 3);

ctx->eip = eip;
ctx->esp = esp;
}


void hal_longjmp(cpu_context_t *ctx)
{
/* clang-format off */
Expand Down
6 changes: 6 additions & 0 deletions hal/ia32/spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ void hal_spinlockDestroy(spinlock_t *spinlock)
}


void hal_spinlockCtxIrqClr(spinlock_ctx_t *sc)
{
*sc &= ~0x200;
}


__attribute__ ((section (".init"))) void _hal_spinlockInit(void)
{
spinlocks.first = NULL;
Expand Down
9 changes: 5 additions & 4 deletions hal/riscv64/_interrupts.S
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,18 @@

ld tp, 280(sp) /* tp */

addi sp, sp, 296

/* Restore task's stack pointer */
ld sp, -8(sp)
ld sp, 288(sp)
.endm


.global interrupts_handleintexc
.type interrupts_handleintexc, @function
interrupts_handleintexc:
.align 8
/* Disable interrupts */
csrc sstatus, SR_SIE

SAVE
mv a0, sp

Expand Down Expand Up @@ -207,8 +208,8 @@ interrupts_handleintexc:

csrs sstatus, SR_SIE
call syscalls_dispatch
/* Interrupts disabled by dispatcher */
sd a0, 56(sp)
csrc sstatus, SR_SIE
j 5f
4:
beq a0, zero, 5f
Expand Down
17 changes: 0 additions & 17 deletions hal/riscv64/arch/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,6 @@ static inline int hal_cpuSupervisorMode(cpu_context_t *ctx)
}


static inline int hal_cpuPushSignal(void *kstack, void (*handler)(void), int n)
{
#if 0
cpu_context_t *ctx = (void *)((char *)kstack - sizeof(cpu_context_t));
char *ustack = (char *)ctx->esp;

PUTONSTACK(ustack, u32, ctx->eip);
PUTONSTACK(ustack, int, n);

ctx->eip = (u32)handler;
ctx->esp = (u32)ustack;
#endif

return 0;
}


/* Code used in disabled code vm/object.c - map_pageFault */
#if 0
static inline void *hal_cpuGetFaultAddr(void)
Expand Down
53 changes: 41 additions & 12 deletions hal/riscv64/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
#include "arch/types.h"


extern int threads_schedule(unsigned int n, cpu_context_t *context, void *arg);


int hal_platformctl(void *ptr)
{
return EOK;
Expand All @@ -38,7 +35,7 @@ int hal_platformctl(void *ptr)
/* TODO: use clz/ctz instructions */
unsigned int hal_cpuGetLastBit(unsigned long v)
{
int lb = 63;
unsigned int lb = 63;

if (!(v & 0xffffffff00000000L)) {
lb -= 32;
Expand Down Expand Up @@ -74,7 +71,7 @@ unsigned int hal_cpuGetLastBit(unsigned long v)

unsigned int hal_cpuGetFirstBit(unsigned long v)
{
int fb = 0;
unsigned int fb = 0;

if (!(v & 0xffffffffL)) {
fb += 32;
Expand Down Expand Up @@ -116,17 +113,19 @@ int hal_cpuCreateContext(cpu_context_t **nctx, void *start, void *kstack, size_t
cpu_context_t *ctx;

*nctx = NULL;
if (kstack == NULL)
if (kstack == NULL) {
return -EINVAL;
}

if (kstacksz < sizeof(cpu_context_t))
if (kstacksz < sizeof(cpu_context_t)) {
return -EINVAL;
}

ctx = (cpu_context_t *)(kstack + kstacksz - 2 * sizeof(cpu_context_t));
ctx = (cpu_context_t *)((char *)kstack + kstacksz - sizeof(cpu_context_t));

__asm__ __volatile__ (
"sd gp, %0"
: "=m" (ctx->gp));
/* clang-format off */
__asm__ volatile("sd gp, %0" : "=m"(ctx->gp));
/* clang-format on */

ctx->pc = (u64)start;
ctx->sp = (u64)ctx;
Expand Down Expand Up @@ -186,6 +185,37 @@ int hal_cpuCreateContext(cpu_context_t **nctx, void *start, void *kstack, size_t
}


int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int n)
{
cpu_context_t *ctx = (void *)((char *)kstack - sizeof(cpu_context_t));

hal_memcpy(signalCtx, ctx, sizeof(cpu_context_t));

signalCtx->sepc = (u64)handler;
signalCtx->sp -= sizeof(cpu_context_t);

PUTONSTACK(signalCtx->sp, u64, ctx->sp);
PUTONSTACK(signalCtx->sp, u64, ctx->sepc);
PUTONSTACK(signalCtx->sp, cpu_context_t *, signalCtx);
/* Signal number */
PUTONSTACK(signalCtx->sp, int, n);

return 0;
}


void hal_cpuSigreturn(void *ustack, cpu_context_t *ctx)
{
u64 sepc, sp;

GETFROMSTACK(ustack, u64, sepc, 2);
GETFROMSTACK(ustack, u64, sp, 3);

ctx->sepc = sepc;
ctx->sp = sp;
}


void _hal_cpuSetKernelStack(void *kstack)
{
csr_write(sscratch, kstack);
Expand Down Expand Up @@ -228,7 +258,6 @@ char *hal_cpuFeatures(char *features, unsigned int len)
char *compatible, *isa, *mmu;
u32 clock;


while (!dtb_getCPU(n++, &compatible, &clock, &isa, &mmu)) {

l = hal_strlen(compatible);
Expand Down
6 changes: 6 additions & 0 deletions hal/riscv64/spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ void hal_spinlockDestroy(spinlock_t *spinlock)
}


void hal_spinlockCtxIrqClr(spinlock_ctx_t *sc)
{
*sc &= ~SR_SIE;
}


__attribute__ ((section (".init"))) void _hal_spinlockInit(void)
{
spinlocks.first = NULL;
Expand Down
5 changes: 5 additions & 0 deletions hal/sparcv8leon3/arch/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ static inline void hal_cpuRestore(cpu_context_t *curr, cpu_context_t *next)
}


static inline void hal_cpuSigreturn(void *ustack, cpu_context_t *ctx)
{
}


/* core management */


Expand Down
4 changes: 2 additions & 2 deletions hal/sparcv8leon3/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ void _hal_cpuSetKernelStack(void *kstack)
}


int hal_cpuPushSignal(void *kstack, void (*handler)(void), int n)
int hal_cpuPushSignal(void *kstack, void (*handler)(void), cpu_context_t *signalCtx, int n)
{
return 0;
return -1;
}


Expand Down
6 changes: 6 additions & 0 deletions hal/sparcv8leon3/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ unsigned int relOffs;
extern void _hal_cpuInit(void);


void hal_sigreturn(void *ustack, cpu_context_t *ctx)
{
return;
}


void *hal_syspageRelocate(void *data)
{
return data;
Expand Down
Loading

0 comments on commit 9b15c86

Please sign in to comment.