blob: 5f833f7adba1abdc8620a11020cb2c80dec24d26 [file] [log] [blame]
Tony Lindgrenf159f4e2010-07-05 14:53:10 +01001#ifndef __ASMARM_TLS_H
2#define __ASMARM_TLS_H
3
Nathan Lynchfbfb8722014-09-11 02:49:08 +01004#include <linux/compiler.h>
5#include <asm/thread_info.h>
6
Tony Lindgrenf159f4e2010-07-05 14:53:10 +01007#ifdef __ASSEMBLY__
André Hentschela4780ad2013-06-18 23:23:26 +01008#include <asm/asm-offsets.h>
9 .macro switch_tls_none, base, tp, tpuser, tmp1, tmp2
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010010 .endm
11
André Hentschela4780ad2013-06-18 23:23:26 +010012 .macro switch_tls_v6k, base, tp, tpuser, tmp1, tmp2
13 mrc p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010014 mcr p15, 0, \tp, c13, c0, 3 @ set TLS register
André Hentschela4780ad2013-06-18 23:23:26 +010015 mcr p15, 0, \tpuser, c13, c0, 2 @ and the user r/w register
16 str \tmp2, [\base, #TI_TP_VALUE + 4] @ save it
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010017 .endm
18
André Hentschela4780ad2013-06-18 23:23:26 +010019 .macro switch_tls_v6, base, tp, tpuser, tmp1, tmp2
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010020 ldr \tmp1, =elf_hwcap
21 ldr \tmp1, [\tmp1, #0]
22 mov \tmp2, #0xffff0fff
23 tst \tmp1, #HWCAP_TLS @ hardware TLS available?
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010024 streq \tp, [\tmp2, #-15] @ set TLS value at 0xffff0ff0
André Hentschela4780ad2013-06-18 23:23:26 +010025 mrcne p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register
26 mcrne p15, 0, \tp, c13, c0, 3 @ yes, set TLS register
27 mcrne p15, 0, \tpuser, c13, c0, 2 @ set user r/w register
28 strne \tmp2, [\base, #TI_TP_VALUE + 4] @ save it
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010029 .endm
30
André Hentschela4780ad2013-06-18 23:23:26 +010031 .macro switch_tls_software, base, tp, tpuser, tmp1, tmp2
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010032 mov \tmp1, #0xffff0fff
33 str \tp, [\tmp1, #-15] @ set TLS value at 0xffff0ff0
34 .endm
35#endif
36
37#ifdef CONFIG_TLS_REG_EMUL
38#define tls_emu 1
39#define has_tls_reg 1
André Hentschela4780ad2013-06-18 23:23:26 +010040#define switch_tls switch_tls_none
Russell King37bc6182011-01-17 16:38:56 +000041#elif defined(CONFIG_CPU_V6)
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010042#define tls_emu 0
43#define has_tls_reg (elf_hwcap & HWCAP_TLS)
André Hentschela4780ad2013-06-18 23:23:26 +010044#define switch_tls switch_tls_v6
Russell King37bc6182011-01-17 16:38:56 +000045#elif defined(CONFIG_CPU_32v6K)
46#define tls_emu 0
47#define has_tls_reg 1
André Hentschela4780ad2013-06-18 23:23:26 +010048#define switch_tls switch_tls_v6k
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010049#else
50#define tls_emu 0
51#define has_tls_reg 0
André Hentschela4780ad2013-06-18 23:23:26 +010052#define switch_tls switch_tls_software
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010053#endif
54
André Hentschela4780ad2013-06-18 23:23:26 +010055#ifndef __ASSEMBLY__
Nathan Lynchfbfb8722014-09-11 02:49:08 +010056
57static inline void set_tls(unsigned long val)
58{
59 struct thread_info *thread;
60
61 thread = current_thread_info();
62
63 thread->tp_value[0] = val;
64
65 /*
66 * This code runs with preemption enabled and therefore must
67 * be reentrant with respect to switch_tls.
68 *
69 * We need to ensure ordering between the shadow state and the
70 * hardware state, so that we don't corrupt the hardware state
71 * with a stale shadow state during context switch.
72 *
73 * If we're preempted here, switch_tls will load TPIDRURO from
74 * thread_info upon resuming execution and the following mcr
75 * is merely redundant.
76 */
77 barrier();
78
79 if (!tls_emu) {
80 if (has_tls_reg) {
81 asm("mcr p15, 0, %0, c13, c0, 3"
82 : : "r" (val));
83 } else {
Nathan Lynch9cc6d9e2014-09-29 19:11:36 +010084#ifdef CONFIG_KUSER_HELPERS
Nathan Lynchfbfb8722014-09-11 02:49:08 +010085 /*
86 * User space must never try to access this
87 * directly. Expect your app to break
88 * eventually if you do so. The user helper
89 * at 0xffff0fe0 must be used instead. (see
90 * entry-armv.S for details)
91 */
92 *((unsigned int *)0xffff0ff0) = val;
Nathan Lynch9cc6d9e2014-09-29 19:11:36 +010093#endif
Nathan Lynchfbfb8722014-09-11 02:49:08 +010094 }
95
96 }
97}
98
André Hentschela4780ad2013-06-18 23:23:26 +010099static inline unsigned long get_tpuser(void)
100{
101 unsigned long reg = 0;
102
103 if (has_tls_reg && !tls_emu)
104 __asm__("mrc p15, 0, %0, c13, c0, 2" : "=r" (reg));
105
106 return reg;
107}
Nathan Lynchfbfb8722014-09-11 02:49:08 +0100108
109static inline void set_tpuser(unsigned long val)
110{
111 /* Since TPIDRURW is fully context-switched (unlike TPIDRURO),
112 * we need not update thread_info.
113 */
114 if (has_tls_reg && !tls_emu) {
115 asm("mcr p15, 0, %0, c13, c0, 2"
116 : : "r" (val));
117 }
118}
119
120static inline void flush_tls(void)
121{
122 set_tls(0);
123 set_tpuser(0);
124}
125
André Hentschela4780ad2013-06-18 23:23:26 +0100126#endif
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100127#endif /* __ASMARM_TLS_H */