| 1 | /* |
| 2 | * rseq.c |
| 3 | * |
| 4 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; only |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | #include <errno.h> |
| 19 | #include <sched.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <syscall.h> |
| 25 | #include <assert.h> |
| 26 | #include <signal.h> |
| 27 | |
| 28 | #include <rseq.h> |
| 29 | |
| 30 | __attribute__((weak)) __thread volatile struct rseq __rseq_abi = { |
| 31 | .u.e.cpu_id = -1, |
| 32 | }; |
| 33 | |
| 34 | static int sys_rseq(volatile struct rseq *rseq_abi, int flags) |
| 35 | { |
| 36 | return syscall(__NR_rseq, rseq_abi, flags); |
| 37 | } |
| 38 | |
| 39 | static void signal_off_save(sigset_t *oldset) |
| 40 | { |
| 41 | sigset_t set; |
| 42 | int ret; |
| 43 | |
| 44 | sigfillset(&set); |
| 45 | ret = pthread_sigmask(SIG_BLOCK, &set, oldset); |
| 46 | if (ret) |
| 47 | abort(); |
| 48 | } |
| 49 | |
| 50 | static void signal_restore(sigset_t oldset) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
| 55 | if (ret) |
| 56 | abort(); |
| 57 | } |
| 58 | |
| 59 | int rseq_register_current_thread(void) |
| 60 | { |
| 61 | int rc; |
| 62 | |
| 63 | rc = sys_rseq(&__rseq_abi, 0); |
| 64 | if (rc) { |
| 65 | fprintf(stderr, "Error: sys_rseq(...) failed(%d): %s\n", |
| 66 | errno, strerror(errno)); |
| 67 | return -1; |
| 68 | } |
| 69 | assert(rseq_current_cpu() >= 0); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int rseq_unregister_current_thread(void) |
| 74 | { |
| 75 | int rc; |
| 76 | |
| 77 | rc = sys_rseq(NULL, 0); |
| 78 | if (rc) { |
| 79 | fprintf(stderr, "Error: sys_rseq(...) failed(%d): %s\n", |
| 80 | errno, strerror(errno)); |
| 81 | return -1; |
| 82 | } |
| 83 | return 0; |
| 84 | } |