exception_handler_posix: Support NetBSD and OpenBSD

This commit is contained in:
scribam 2023-10-07 12:14:49 +02:00 committed by merry
parent 7a7557f70f
commit ac643f1bee

View file

@ -10,7 +10,9 @@
# include <sys/ucontext.h> # include <sys/ucontext.h>
#else #else
# include <signal.h> # include <signal.h>
# include <ucontext.h> # ifndef __OpenBSD__
# include <ucontext.h>
# endif
#endif #endif
#include <cstring> #include <cstring>
@ -130,7 +132,10 @@ void SigHandler::RemoveCodeBlock(u64 host_pc) {
void SigHandler::SigAction(int sig, siginfo_t* info, void* raw_context) { void SigHandler::SigAction(int sig, siginfo_t* info, void* raw_context) {
ASSERT(sig == SIGSEGV || sig == SIGBUS); ASSERT(sig == SIGSEGV || sig == SIGBUS);
auto& mctx = ((ucontext_t*)raw_context)->uc_mcontext; ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(raw_context);
#ifndef __OpenBSD__
auto& mctx = ucontext->uc_mcontext;
#endif
#if defined(MCL_ARCHITECTURE_X86_64) #if defined(MCL_ARCHITECTURE_X86_64)
@ -143,6 +148,12 @@ void SigHandler::SigAction(int sig, siginfo_t* info, void* raw_context) {
# elif defined(__FreeBSD__) # elif defined(__FreeBSD__)
# define CTX_RIP (mctx.mc_rip) # define CTX_RIP (mctx.mc_rip)
# define CTX_RSP (mctx.mc_rsp) # define CTX_RSP (mctx.mc_rsp)
# elif defined(__NetBSD__)
# define CTX_RIP (mctx.__gregs[_REG_RIP])
# define CTX_RSP (mctx.__gregs[_REG_RSP])
# elif defined(__OpenBSD__)
# define CTX_RIP (ucontext->sc_rip)
# define CTX_RSP (ucontext->sc_rsp)
# else # else
# error "Unknown platform" # error "Unknown platform"
# endif # endif
@ -192,6 +203,18 @@ void SigHandler::SigAction(int sig, siginfo_t* info, void* raw_context) {
# define CTX_LR (mctx.mc_gpregs.gp_lr) # define CTX_LR (mctx.mc_gpregs.gp_lr)
# define CTX_X(i) (mctx.mc_gpregs.gp_x[i]) # define CTX_X(i) (mctx.mc_gpregs.gp_x[i])
# define CTX_Q(i) (mctx.mc_fpregs.fp_q[i]) # define CTX_Q(i) (mctx.mc_fpregs.fp_q[i])
# elif defined(__NetBSD__)
# define CTX_PC (mctx.mc_gpregs.gp_elr)
# define CTX_SP (mctx.mc_gpregs.gp_sp)
# define CTX_LR (mctx.mc_gpregs.gp_lr)
# define CTX_X(i) (mctx.mc_gpregs.gp_x[i])
# define CTX_Q(i) (mctx.mc_fpregs.fp_q[i])
# elif defined(__OpenBSD__)
# define CTX_PC (ucontext->sc_elr)
# define CTX_SP (ucontext->sc_sp)
# define CTX_LR (ucontext->sc_lr)
# define CTX_X(i) (ucontext->sc_x[i])
# define CTX_Q(i) (ucontext->sc_q[i])
# else # else
# error "Unknown platform" # error "Unknown platform"
# endif # endif