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__
119c46929eSArd Biesheuvel #include <asm/insn.h>
1250596b75SArd Biesheuvel
1350596b75SArd Biesheuvel struct task_struct;
1450596b75SArd Biesheuvel
159c46929eSArd Biesheuvel extern struct task_struct *__current;
1650596b75SArd Biesheuvel
get_current(void)17*4d5a643eSArd Biesheuvel static __always_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) && \
229c46929eSArd 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();
359c46929eSArd Biesheuvel #elif defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
369c46929eSArd Biesheuvel asm("0: mrc p15, 0, %0, c13, c0, 3 \n\t"
379c46929eSArd Biesheuvel #ifdef CONFIG_CPU_V6
389c46929eSArd Biesheuvel "1: \n\t"
399c46929eSArd Biesheuvel " .subsection 1 \n\t"
40d6905849SArd Biesheuvel #if defined(CONFIG_ARM_HAS_GROUP_RELOCS) && \
41d6905849SArd Biesheuvel !(defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS))
429c46929eSArd Biesheuvel "2: " LOAD_SYM_ARMV6(%0, __current) " \n\t"
439c46929eSArd Biesheuvel " b 1b \n\t"
445fe41793SArd Biesheuvel #else
455fe41793SArd Biesheuvel "2: ldr %0, 3f \n\t"
465fe41793SArd Biesheuvel " ldr %0, [%0] \n\t"
475fe41793SArd Biesheuvel " b 1b \n\t"
485fe41793SArd Biesheuvel "3: .long __current \n\t"
495fe41793SArd Biesheuvel #endif
509c46929eSArd Biesheuvel " .previous \n\t"
519c46929eSArd Biesheuvel " .pushsection \".alt.smp.init\", \"a\" \n\t"
529c46929eSArd Biesheuvel " .long 0b - . \n\t"
539c46929eSArd Biesheuvel " b . + (2b - 0b) \n\t"
549c46929eSArd Biesheuvel " .popsection \n\t"
559c46929eSArd Biesheuvel #endif
569c46929eSArd Biesheuvel : "=r"(cur));
579c46929eSArd Biesheuvel #elif __LINUX_ARM_ARCH__>= 7 || \
58d6905849SArd Biesheuvel !defined(CONFIG_ARM_HAS_GROUP_RELOCS) || \
59d6905849SArd Biesheuvel (defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS))
609c46929eSArd Biesheuvel cur = __current;
6150596b75SArd Biesheuvel #else
629c46929eSArd Biesheuvel asm(LOAD_SYM_ARMV6(%0, __current) : "=r"(cur));
6350596b75SArd Biesheuvel #endif
6450596b75SArd Biesheuvel return cur;
6550596b75SArd Biesheuvel }
6650596b75SArd Biesheuvel
6750596b75SArd Biesheuvel #define current get_current()
6850596b75SArd Biesheuvel
6950596b75SArd Biesheuvel #endif /* __ASSEMBLY__ */
7050596b75SArd Biesheuvel
7150596b75SArd Biesheuvel #endif /* _ASM_ARM_CURRENT_H */
72