Created
July 19, 2022 19:37
-
-
Save ammarfaizi2/47a5c7590127ed3f871451a98905982e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <asm/signal.h> | |
static volatile int stop = 0; | |
static void handle_sig(int sig) | |
{ | |
printf("sig = %d\n", sig); | |
stop = 1; | |
} | |
static void __restore_rt(void) | |
{ | |
__asm__ volatile ( | |
"syscall" | |
: | |
: "a"(15) | |
: "memory", "rcx", "r11" | |
); | |
__builtin_unreachable(); | |
} | |
static int __rt_sigaction(int sig, const struct sigaction *act, | |
struct sigaction *oact, size_t sigsetsize) | |
{ | |
int ret = 13; | |
register size_t size __asm__("r10") = sigsetsize; | |
__asm__ volatile ( | |
"syscall" | |
: "+d"(oact), /* %rdx */ | |
"+a"(ret) /* %rax */ | |
: "D"(sig), /* %rdi */ | |
"S"(act), /* %rsi */ | |
"r"(size) /* %r10 */ | |
: "memory", "rcx", "r11" | |
); | |
return ret; | |
} | |
static int rt_sigaction(int sig, struct sigaction *act, struct sigaction *oact, | |
size_t size) | |
{ | |
int ret; | |
act->sa_flags |= SA_RESTORER; | |
act->sa_restorer = __restore_rt; | |
ret = __rt_sigaction(sig, act, oact, sizeof(sigset_t)); | |
return ret; | |
} | |
static inline void cpu_relax(void) | |
{ | |
__asm__ volatile ("rep; nop;" ::: "memory"); | |
} | |
int main(void) | |
{ | |
struct sigaction act; | |
memset(&act, 0, sizeof(act)); | |
act.sa_handler = handle_sig; | |
rt_sigaction(SIGINT, &act, NULL, 0); | |
while (!stop) | |
cpu_relax(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment