xref: /linux/arch/arm/include/asm/current.h (revision 9c46929e7989efacc1dd0a1dd662a839897ea2b6)
150596b75SArd Biesheuvel /* SPDX-License-Identifier: GPL-2.0-only */
250596b75SArd Biesheuvel /*
350596b75SArd Biesheuvel  * Copyright (c) 2021 Keith Packard <keithp@keithp.com>
450596b75SArd Biesheuvel  * Copyright (c) 2021 Google, LLC <ardb@kernel.org>
550596b75SArd Biesheuvel  */
650596b75SArd Biesheuvel 
750596b75SArd Biesheuvel #ifndef _ASM_ARM_CURRENT_H
850596b75SArd Biesheuvel #define _ASM_ARM_CURRENT_H
950596b75SArd Biesheuvel 
1050596b75SArd Biesheuvel #ifndef __ASSEMBLY__
11*9c46929eSArd Biesheuvel #include <asm/insn.h>
1250596b75SArd Biesheuvel 
1350596b75SArd Biesheuvel struct task_struct;
1450596b75SArd Biesheuvel 
15*9c46929eSArd Biesheuvel extern struct task_struct *__current;
1650596b75SArd Biesheuvel 
17*9c46929eSArd Biesheuvel static inline __attribute_const__ struct task_struct *get_current(void)
1850596b75SArd Biesheuvel {
1950596b75SArd Biesheuvel 	struct task_struct *cur;
2050596b75SArd Biesheuvel 
21c1e42efaSArd Biesheuvel #if __has_builtin(__builtin_thread_pointer) && \
22*9c46929eSArd Biesheuvel     defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) && \
23c1e42efaSArd Biesheuvel     !(defined(CONFIG_THUMB2_KERNEL) && \
24c1e42efaSArd Biesheuvel       defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION < 130001)
2550596b75SArd Biesheuvel 	/*
2650596b75SArd Biesheuvel 	 * Use the __builtin helper when available - this results in better
2750596b75SArd Biesheuvel 	 * code, especially when using GCC in combination with the per-task
2850596b75SArd Biesheuvel 	 * stack protector, as the compiler will recognize that it needs to
2950596b75SArd Biesheuvel 	 * load the TLS register only once in every function.
30c1e42efaSArd Biesheuvel 	 *
31c1e42efaSArd Biesheuvel 	 * Clang < 13.0.1 gets this wrong for Thumb2 builds:
32c1e42efaSArd Biesheuvel 	 * https://github.com/ClangBuiltLinux/linux/issues/1485
3350596b75SArd Biesheuvel 	 */
3450596b75SArd Biesheuvel 	cur = __builtin_thread_pointer();
35*9c46929eSArd Biesheuvel #elif defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
36*9c46929eSArd Biesheuvel 	asm("0:	mrc p15, 0, %0, c13, c0, 3			\n\t"
37*9c46929eSArd Biesheuvel #ifdef CONFIG_CPU_V6
38*9c46929eSArd Biesheuvel 	    "1:							\n\t"
39*9c46929eSArd Biesheuvel 	    "	.subsection 1					\n\t"
40*9c46929eSArd Biesheuvel 	    "2: " LOAD_SYM_ARMV6(%0, __current) "		\n\t"
41*9c46929eSArd Biesheuvel 	    "	b	1b					\n\t"
42*9c46929eSArd Biesheuvel 	    "	.previous					\n\t"
43*9c46929eSArd Biesheuvel 	    "	.pushsection \".alt.smp.init\", \"a\"		\n\t"
44*9c46929eSArd Biesheuvel 	    "	.long	0b - .					\n\t"
45*9c46929eSArd Biesheuvel 	    "	b	. + (2b - 0b)				\n\t"
46*9c46929eSArd Biesheuvel 	    "	.popsection					\n\t"
47*9c46929eSArd Biesheuvel #endif
48*9c46929eSArd Biesheuvel 	    : "=r"(cur));
49*9c46929eSArd Biesheuvel #elif __LINUX_ARM_ARCH__>=7 || \
50*9c46929eSArd Biesheuvel       (defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS))
51*9c46929eSArd Biesheuvel 	cur = __current;
5250596b75SArd Biesheuvel #else
53*9c46929eSArd Biesheuvel 	asm(LOAD_SYM_ARMV6(%0, __current) : "=r"(cur));
5450596b75SArd Biesheuvel #endif
5550596b75SArd Biesheuvel 	return cur;
5650596b75SArd Biesheuvel }
5750596b75SArd Biesheuvel 
5850596b75SArd Biesheuvel #define current get_current()
5950596b75SArd Biesheuvel 
6050596b75SArd Biesheuvel #endif /* __ASSEMBLY__ */
6150596b75SArd Biesheuvel 
6250596b75SArd Biesheuvel #endif /* _ASM_ARM_CURRENT_H */
63