Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1
0002 /*
0003  * rseq.c
0004  *
0005  * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; only
0010  * version 2.1 of the License.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015  * Lesser General Public License for more details.
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 /* Offset from the thread pointer to the rseq area.  */
0040 ptrdiff_t rseq_offset;
0041 
0042 /* Size of the registered rseq area.  0 if the registration was
0043    unsuccessful.  */
0044 unsigned int rseq_size = -1U;
0045 
0046 /* Flags used during rseq registration.  */
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         /* Treat libc's ownership as a successful registration. */
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         /* Treat libc's ownership as a successful unregistration. */
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         /* rseq registration owned by glibc */
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 }