1/* Restartable Sequences internal API. Linux implementation.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#ifndef RSEQ_INTERNAL_H
19#define RSEQ_INTERNAL_H
20
21#include <sysdep.h>
22#include <errno.h>
23#include <kernel-features.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <sys/rseq.h>
27
28/* 32 is the initially required value for the area size. The
29 actually used rseq size may be less (20 bytes initially). */
30#define RSEQ_AREA_SIZE_INITIAL 32
31#define RSEQ_AREA_SIZE_INITIAL_USED 20
32
33/* The variables are in .data.relro but are not yet write-protected. */
34extern unsigned int _rseq_size attribute_hidden;
35extern ptrdiff_t _rseq_offset attribute_hidden;
36
37#ifdef RSEQ_SIG
38static inline bool
39rseq_register_current_thread (struct pthread *self, bool do_rseq)
40{
41 if (do_rseq)
42 {
43 unsigned int size;
44#if IS_IN (rtld)
45 /* Use the hidden symbol in ld.so. */
46 size = _rseq_size;
47#else
48 size = __rseq_size;
49#endif
50 if (size < RSEQ_AREA_SIZE_INITIAL)
51 /* The initial implementation used only 20 bytes out of 32,
52 but still expected size 32. */
53 size = RSEQ_AREA_SIZE_INITIAL;
54
55 /* Initialize the whole rseq area to zero prior to registration. */
56 memset (&self->rseq_area, 0, size);
57
58 /* Set the cpu_id field to RSEQ_CPU_ID_UNINITIALIZED, this is checked by
59 the kernel at registration when CONFIG_DEBUG_RSEQ is enabled. */
60 THREAD_SETMEM (self, rseq_area.cpu_id, RSEQ_CPU_ID_UNINITIALIZED);
61
62 int ret = INTERNAL_SYSCALL_CALL (rseq, &self->rseq_area,
63 size, 0, RSEQ_SIG);
64 if (!INTERNAL_SYSCALL_ERROR_P (ret))
65 return true;
66 }
67 /* When rseq is disabled by tunables or the registration fails, inform
68 userspace by setting 'cpu_id' to RSEQ_CPU_ID_REGISTRATION_FAILED. */
69 THREAD_SETMEM (self, rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
70 return false;
71}
72#else /* RSEQ_SIG */
73static inline bool
74rseq_register_current_thread (struct pthread *self, bool do_rseq)
75{
76 THREAD_SETMEM (self, rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
77 return false;
78}
79#endif /* RSEQ_SIG */
80
81#endif /* rseq-internal.h */
82

source code of glibc/sysdeps/unix/sysv/linux/rseq-internal.h