blob: 5a66c3b13c92f2fec67a32b61640aface3cedc1b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Tony Lindgrenf159f4e2010-07-05 14:53:10 +01002#ifndef __ASMARM_TLS_H
3#define __ASMARM_TLS_H
4
Nathan Lynchfbfb8722014-09-11 02:49:08 +01005#include <linux/compiler.h>
6#include <asm/thread_info.h>
7
Tony Lindgrenf159f4e2010-07-05 14:53:10 +01008#ifdef __ASSEMBLY__
André Hentschela4780ad2013-06-18 23:23:26 +01009#include <asm/asm-offsets.h>
10 .macro switch_tls_none, base, tp, tpuser, tmp1, tmp2
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010011 .endm
12
André Hentschela4780ad2013-06-18 23:23:26 +010013 .macro switch_tls_v6k, base, tp, tpuser, tmp1, tmp2
14 mrc p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010015 mcr p15, 0, \tp, c13, c0, 3 @ set TLS register
André Hentschela4780ad2013-06-18 23:23:26 +010016 mcr p15, 0, \tpuser, c13, c0, 2 @ and the user r/w register
17 str \tmp2, [\base, #TI_TP_VALUE + 4] @ save it
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010018 .endm
19
André Hentschela4780ad2013-06-18 23:23:26 +010020 .macro switch_tls_v6, base, tp, tpuser, tmp1, tmp2
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010021 ldr \tmp1, =elf_hwcap
22 ldr \tmp1, [\tmp1, #0]
23 mov \tmp2, #0xffff0fff
24 tst \tmp1, #HWCAP_TLS @ hardware TLS available?
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010025 streq \tp, [\tmp2, #-15] @ set TLS value at 0xffff0ff0
André Hentschela4780ad2013-06-18 23:23:26 +010026 mrcne p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register
27 mcrne p15, 0, \tp, c13, c0, 3 @ yes, set TLS register
28 mcrne p15, 0, \tpuser, c13, c0, 2 @ set user r/w register
29 strne \tmp2, [\base, #TI_TP_VALUE + 4] @ save it
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010030 .endm
31
André Hentschela4780ad2013-06-18 23:23:26 +010032 .macro switch_tls_software, base, tp, tpuser, tmp1, tmp2
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010033 mov \tmp1, #0xffff0fff
34 str \tp, [\tmp1, #-15] @ set TLS value at 0xffff0ff0
35 .endm
36#endif
37
38#ifdef CONFIG_TLS_REG_EMUL
39#define tls_emu 1
40#define has_tls_reg 1
André Hentschela4780ad2013-06-18 23:23:26 +010041#define switch_tls switch_tls_none
Russell King37bc6182011-01-17 16:38:56 +000042#elif defined(CONFIG_CPU_V6)
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010043#define tls_emu 0
44#define has_tls_reg (elf_hwcap & HWCAP_TLS)
André Hentschela4780ad2013-06-18 23:23:26 +010045#define switch_tls switch_tls_v6
Russell King37bc6182011-01-17 16:38:56 +000046#elif defined(CONFIG_CPU_32v6K)
47#define tls_emu 0
48#define has_tls_reg 1
André Hentschela4780ad2013-06-18 23:23:26 +010049#define switch_tls switch_tls_v6k
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010050#else
51#define tls_emu 0
52#define has_tls_reg 0
André Hentschela4780ad2013-06-18 23:23:26 +010053#define switch_tls switch_tls_software
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010054#endif
55
André Hentschela4780ad2013-06-18 23:23:26 +010056#ifndef __ASSEMBLY__
Nathan Lynchfbfb8722014-09-11 02:49:08 +010057
58static inline void set_tls(unsigned long val)
59{
60 struct thread_info *thread;
61
62 thread = current_thread_info();
63
64 thread->tp_value[0] = val;
65
66 /*
67 * This code runs with preemption enabled and therefore must
68 * be reentrant with respect to switch_tls.
69 *
70 * We need to ensure ordering between the shadow state and the
71 * hardware state, so that we don't corrupt the hardware state
72 * with a stale shadow state during context switch.
73 *
74 * If we're preempted here, switch_tls will load TPIDRURO from
75 * thread_info upon resuming execution and the following mcr
76 * is merely redundant.
77 */
78 barrier();
79
80 if (!tls_emu) {
81 if (has_tls_reg) {
82 asm("mcr p15, 0, %0, c13, c0, 3"
83 : : "r" (val));
84 } else {
Nathan Lynch9cc6d9e2014-09-29 19:11:36 +010085#ifdef CONFIG_KUSER_HELPERS
Nathan Lynchfbfb8722014-09-11 02:49:08 +010086 /*
87 * User space must never try to access this
88 * directly. Expect your app to break
89 * eventually if you do so. The user helper
90 * at 0xffff0fe0 must be used instead. (see
91 * entry-armv.S for details)
92 */
93 *((unsigned int *)0xffff0ff0) = val;
Nathan Lynch9cc6d9e2014-09-29 19:11:36 +010094#endif
Nathan Lynchfbfb8722014-09-11 02:49:08 +010095 }
96
97 }
98}
99
André Hentschela4780ad2013-06-18 23:23:26 +0100100static inline unsigned long get_tpuser(void)
101{
102 unsigned long reg = 0;
103
104 if (has_tls_reg && !tls_emu)
105 __asm__("mrc p15, 0, %0, c13, c0, 2" : "=r" (reg));
106
107 return reg;
108}
Nathan Lynchfbfb8722014-09-11 02:49:08 +0100109
110static inline void set_tpuser(unsigned long val)
111{
112 /* Since TPIDRURW is fully context-switched (unlike TPIDRURO),
113 * we need not update thread_info.
114 */
115 if (has_tls_reg && !tls_emu) {
116 asm("mcr p15, 0, %0, c13, c0, 2"
117 : : "r" (val));
118 }
119}
120
121static inline void flush_tls(void)
122{
123 set_tls(0);
124 set_tpuser(0);
125}
126
André Hentschela4780ad2013-06-18 23:23:26 +0100127#endif
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100128#endif /* __ASMARM_TLS_H */