Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * rseq.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #ifndef RSEQ_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define RSEQ_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/rseq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * Empty code injection macros, override when testing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * It is important to consider that the ASM injection macros need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * fully reentrant (e.g. do not modify the stack).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #ifndef RSEQ_INJECT_ASM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define RSEQ_INJECT_ASM(n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #ifndef RSEQ_INJECT_C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define RSEQ_INJECT_C(n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #ifndef RSEQ_INJECT_INPUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define RSEQ_INJECT_INPUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #ifndef RSEQ_INJECT_CLOBBER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define RSEQ_INJECT_CLOBBER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #ifndef RSEQ_INJECT_FAILED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define RSEQ_INJECT_FAILED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) extern __thread volatile struct rseq __rseq_abi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) extern int __rseq_handled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define rseq_likely(x)		__builtin_expect(!!(x), 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define rseq_unlikely(x)	__builtin_expect(!!(x), 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define rseq_barrier()		__asm__ __volatile__("" : : : "memory")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define RSEQ_ACCESS_ONCE(x)	(*(__volatile__  __typeof__(x) *)&(x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define RSEQ_WRITE_ONCE(x, v)	__extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define RSEQ_READ_ONCE(x)	RSEQ_ACCESS_ONCE(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define __rseq_str_1(x)	#x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define __rseq_str(x)		__rseq_str_1(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define rseq_log(fmt, args...)						       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		## args, __func__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define rseq_bug(fmt, args...)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	do {				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		rseq_log(fmt, ##args);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		abort();		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #if defined(__x86_64__) || defined(__i386__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #include <rseq-x86.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #elif defined(__ARMEL__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #include <rseq-arm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #elif defined (__AARCH64EL__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #include <rseq-arm64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #elif defined(__PPC__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #include <rseq-ppc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #elif defined(__mips__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #include <rseq-mips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #elif defined(__s390__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #include <rseq-s390.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #error unsupported target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * Register rseq for the current thread. This needs to be called once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * by any thread which uses restartable sequences, before they start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * using restartable sequences, to ensure restartable sequences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * succeed. A restartable sequence executed from a non-registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * thread will always fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) int rseq_register_current_thread(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * Unregister rseq for current thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) int rseq_unregister_current_thread(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * Restartable sequence fallback for reading the current CPU number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int32_t rseq_fallback_current_cpu(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * Values returned can be either the current CPU number, -1 (rseq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * uninitialized), or -2 (rseq initialization has failed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static inline int32_t rseq_current_cpu_raw(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return RSEQ_ACCESS_ONCE(__rseq_abi.cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * Returns a possible CPU number, which is typically the current CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * The returned CPU number can be used to prepare for an rseq critical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * section, which will confirm whether the cpu number is indeed the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * current one, and whether rseq is initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * The CPU number returned by rseq_cpu_start should always be validated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * by passing it to a rseq asm sequence, or by comparing it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * return value of rseq_current_cpu_raw() if the rseq asm sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * does not need to be invoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline uint32_t rseq_cpu_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return RSEQ_ACCESS_ONCE(__rseq_abi.cpu_id_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static inline uint32_t rseq_current_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int32_t cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	cpu = rseq_current_cpu_raw();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (rseq_unlikely(cpu < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		cpu = rseq_fallback_current_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static inline void rseq_clear_rseq_cs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #ifdef __LP64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	__rseq_abi.rseq_cs.ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	__rseq_abi.rseq_cs.ptr.ptr32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * rseq_prepare_unload() should be invoked by each thread executing a rseq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * critical section at least once between their last critical section and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * library unload of the library defining the rseq critical section (struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * post_commit_offset fields. This also applies to use of rseq in code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * generated by JIT: rseq_prepare_unload() should be invoked at least once by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * each thread executing a rseq critical section before reclaim of the memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * holding the struct rseq_cs or reclaim of the code pointed to by struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * rseq_cs start_ip and post_commit_offset fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline void rseq_prepare_unload(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	rseq_clear_rseq_cs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #endif  /* RSEQ_H_ */