tls.h 3.1 KB

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