tls.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASMARM_TLS_H
  3. #define __ASMARM_TLS_H
  4. #include <linux/compiler.h>
  5. #include <asm/thread_info.h>
  6. #ifdef __ASSEMBLY__
  7. #include <asm/asm-offsets.h>
  8. .macro switch_tls_none, base, tp, tpuser, tmp1, tmp2
  9. .endm
  10. .macro switch_tls_v6k, base, tp, tpuser, tmp1, tmp2
  11. mrc p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register
  12. mcr p15, 0, \tp, c13, c0, 3 @ set TLS register
  13. mcr p15, 0, \tpuser, c13, c0, 2 @ and the user r/w register
  14. str \tmp2, [\base, #TI_TP_VALUE + 4] @ save it
  15. .endm
  16. .macro switch_tls_v6, base, tp, tpuser, tmp1, tmp2
  17. ldr \tmp1, =elf_hwcap
  18. ldr \tmp1, [\tmp1, #0]
  19. mov \tmp2, #0xffff0fff
  20. tst \tmp1, #HWCAP_TLS @ hardware TLS available?
  21. streq \tp, [\tmp2, #-15] @ set TLS value at 0xffff0ff0
  22. mrcne p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register
  23. mcrne p15, 0, \tp, c13, c0, 3 @ yes, set TLS register
  24. mcrne p15, 0, \tpuser, c13, c0, 2 @ set user r/w register
  25. strne \tmp2, [\base, #TI_TP_VALUE + 4] @ save it
  26. .endm
  27. .macro switch_tls_software, base, tp, tpuser, tmp1, tmp2
  28. mov \tmp1, #0xffff0fff
  29. str \tp, [\tmp1, #-15] @ set TLS value at 0xffff0ff0
  30. .endm
  31. #endif
  32. #ifdef CONFIG_TLS_REG_EMUL
  33. #define tls_emu 1
  34. #define has_tls_reg 1
  35. #define switch_tls switch_tls_none
  36. #elif defined(CONFIG_CPU_V6)
  37. #define tls_emu 0
  38. #define has_tls_reg (elf_hwcap & HWCAP_TLS)
  39. #define switch_tls switch_tls_v6
  40. #elif defined(CONFIG_CPU_32v6K)
  41. #define tls_emu 0
  42. #define has_tls_reg 1
  43. #define switch_tls switch_tls_v6k
  44. #else
  45. #define tls_emu 0
  46. #define has_tls_reg 0
  47. #define switch_tls switch_tls_software
  48. #endif
  49. #ifndef __ASSEMBLY__
  50. static inline void set_tls(unsigned long val)
  51. {
  52. struct thread_info *thread;
  53. thread = current_thread_info();
  54. thread->tp_value[0] = val;
  55. /*
  56. * This code runs with preemption enabled and therefore must
  57. * be reentrant with respect to switch_tls.
  58. *
  59. * We need to ensure ordering between the shadow state and the
  60. * hardware state, so that we don't corrupt the hardware state
  61. * with a stale shadow state during context switch.
  62. *
  63. * If we're preempted here, switch_tls will load TPIDRURO from
  64. * thread_info upon resuming execution and the following mcr
  65. * is merely redundant.
  66. */
  67. barrier();
  68. if (!tls_emu) {
  69. if (has_tls_reg) {
  70. asm("mcr p15, 0, %0, c13, c0, 3"
  71. : : "r" (val));
  72. } else {
  73. #ifdef CONFIG_KUSER_HELPERS
  74. /*
  75. * User space must never try to access this
  76. * directly. Expect your app to break
  77. * eventually if you do so. The user helper
  78. * at 0xffff0fe0 must be used instead. (see
  79. * entry-armv.S for details)
  80. */
  81. *((unsigned int *)0xffff0ff0) = val;
  82. #endif
  83. }
  84. }
  85. }
  86. static inline unsigned long get_tpuser(void)
  87. {
  88. unsigned long reg = 0;
  89. if (has_tls_reg && !tls_emu)
  90. __asm__("mrc p15, 0, %0, c13, c0, 2" : "=r" (reg));
  91. return reg;
  92. }
  93. static inline void set_tpuser(unsigned long val)
  94. {
  95. /* Since TPIDRURW is fully context-switched (unlike TPIDRURO),
  96. * we need not update thread_info.
  97. */
  98. if (has_tls_reg && !tls_emu) {
  99. asm("mcr p15, 0, %0, c13, c0, 2"
  100. : : "r" (val));
  101. }
  102. }
  103. static inline void flush_tls(void)
  104. {
  105. set_tls(0);
  106. set_tpuser(0);
  107. }
  108. #endif
  109. #endif /* __ASMARM_TLS_H */