Skip to content

Commit

Permalink
x86_64: dispatch signals to their handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Nov 26, 2024
1 parent 76f0d5e commit b95b8d0
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/platform/x86_64/ipc/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,41 @@ int platformSignalSetup(Thread *t) {

memcpy(trampoline, sigstub, sigstubSize);
t->signalTrampoline = (uintptr_t) trampoline;
return 0;
}

/* platformSendSignal(): dispatches a signal to a thread
* params: sender - thread sending the signal
* params: dest - thread receiving the signal
* params: signum - signal number
* params: handler - function to dispatch
* returns: zero on success
*/

int platformSendSignal(Thread *sender, Thread *dest, int signum, uintptr_t handler) {
memcpy(dest->signalContext, dest->context, PLATFORM_CONTEXT_SIZE);

/* TODO: enter the function as func(int sig, siginfo_t *info, void *ctx)
* instead of just func(int sig)
* https://pubs.opengroup.org/onlinepubs/007904875/functions/sigaction.html
*/

ThreadContext *ctx = (ThreadContext *) dest->context;
platformUseContext(ctx);

ctx->regs.rip = handler;
ctx->regs.rdi = signum;
ctx->regs.rflags = 0x202;
ctx->regs.rsp -= 128; // downwards of the red zone

// ensure stack is 16-byte aligned on entry
while(ctx->regs.rsp & 0x0F)
ctx->regs.rsp--;
ctx->regs.rbp = ctx->regs.rsp;

// inject signal trampoline
uint64_t *stack = (uint64_t *) ctx->regs.rsp;
*stack = dest->signalTrampoline;

return 0;
}

0 comments on commit b95b8d0

Please sign in to comment.