0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #define _GNU_SOURCE
0019 #include <errno.h>
0020 #include <sched.h>
0021 #include <stdio.h>
0022 #include <stdlib.h>
0023 #include <string.h>
0024 #include <unistd.h>
0025 #include <syscall.h>
0026 #include <assert.h>
0027 #include <signal.h>
0028 #include <limits.h>
0029 #include <dlfcn.h>
0030 #include <stddef.h>
0031
0032 #include "../kselftest.h"
0033 #include "rseq.h"
0034
0035 static const ptrdiff_t *libc_rseq_offset_p;
0036 static const unsigned int *libc_rseq_size_p;
0037 static const unsigned int *libc_rseq_flags_p;
0038
0039
0040 ptrdiff_t rseq_offset;
0041
0042
0043
0044 unsigned int rseq_size = -1U;
0045
0046
0047 unsigned int rseq_flags;
0048
0049 static int rseq_ownership;
0050
0051 static
0052 __thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"))) = {
0053 .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED,
0054 };
0055
0056 static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len,
0057 int flags, uint32_t sig)
0058 {
0059 return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
0060 }
0061
0062 int rseq_available(void)
0063 {
0064 int rc;
0065
0066 rc = sys_rseq(NULL, 0, 0, 0);
0067 if (rc != -1)
0068 abort();
0069 switch (errno) {
0070 case ENOSYS:
0071 return 0;
0072 case EINVAL:
0073 return 1;
0074 default:
0075 abort();
0076 }
0077 }
0078
0079 int rseq_register_current_thread(void)
0080 {
0081 int rc;
0082
0083 if (!rseq_ownership) {
0084
0085 return 0;
0086 }
0087 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), 0, RSEQ_SIG);
0088 if (rc)
0089 return -1;
0090 assert(rseq_current_cpu_raw() >= 0);
0091 return 0;
0092 }
0093
0094 int rseq_unregister_current_thread(void)
0095 {
0096 int rc;
0097
0098 if (!rseq_ownership) {
0099
0100 return 0;
0101 }
0102 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG);
0103 if (rc)
0104 return -1;
0105 return 0;
0106 }
0107
0108 static __attribute__((constructor))
0109 void rseq_init(void)
0110 {
0111 libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
0112 libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
0113 libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");
0114 if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p &&
0115 *libc_rseq_size_p != 0) {
0116
0117 rseq_offset = *libc_rseq_offset_p;
0118 rseq_size = *libc_rseq_size_p;
0119 rseq_flags = *libc_rseq_flags_p;
0120 return;
0121 }
0122 if (!rseq_available())
0123 return;
0124 rseq_ownership = 1;
0125 rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer();
0126 rseq_size = sizeof(struct rseq_abi);
0127 rseq_flags = 0;
0128 }
0129
0130 static __attribute__((destructor))
0131 void rseq_exit(void)
0132 {
0133 if (!rseq_ownership)
0134 return;
0135 rseq_offset = 0;
0136 rseq_size = -1U;
0137 rseq_ownership = 0;
0138 }
0139
0140 int32_t rseq_fallback_current_cpu(void)
0141 {
0142 int32_t cpu;
0143
0144 cpu = sched_getcpu();
0145 if (cpu < 0) {
0146 perror("sched_getcpu()");
0147 abort();
0148 }
0149 return cpu;
0150 }