mte-kasan.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020 ARM Ltd.
  4. */
  5. #ifndef __ASM_MTE_KASAN_H
  6. #define __ASM_MTE_KASAN_H
  7. #include <asm/compiler.h>
  8. #include <asm/cputype.h>
  9. #include <asm/mte-def.h>
  10. #ifndef __ASSEMBLY__
  11. #include <linux/types.h>
  12. #ifdef CONFIG_KASAN_HW_TAGS
  13. /* Whether the MTE asynchronous mode is enabled. */
  14. DECLARE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
  15. static inline bool system_uses_mte_async_or_asymm_mode(void)
  16. {
  17. return static_branch_unlikely(&mte_async_or_asymm_mode);
  18. }
  19. #else /* CONFIG_KASAN_HW_TAGS */
  20. static inline bool system_uses_mte_async_or_asymm_mode(void)
  21. {
  22. return false;
  23. }
  24. #endif /* CONFIG_KASAN_HW_TAGS */
  25. #ifdef CONFIG_ARM64_MTE
  26. /*
  27. * The Tag Check Flag (TCF) mode for MTE is per EL, hence TCF0
  28. * affects EL0 and TCF affects EL1 irrespective of which TTBR is
  29. * used.
  30. * The kernel accesses TTBR0 usually with LDTR/STTR instructions
  31. * when UAO is available, so these would act as EL0 accesses using
  32. * TCF0.
  33. * However futex.h code uses exclusives which would be executed as
  34. * EL1, this can potentially cause a tag check fault even if the
  35. * user disables TCF0.
  36. *
  37. * To address the problem we set the PSTATE.TCO bit in uaccess_enable()
  38. * and reset it in uaccess_disable().
  39. *
  40. * The Tag check override (TCO) bit disables temporarily the tag checking
  41. * preventing the issue.
  42. */
  43. static inline void mte_disable_tco(void)
  44. {
  45. asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(0),
  46. ARM64_MTE, CONFIG_KASAN_HW_TAGS));
  47. }
  48. static inline void mte_enable_tco(void)
  49. {
  50. asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(1),
  51. ARM64_MTE, CONFIG_KASAN_HW_TAGS));
  52. }
  53. /*
  54. * These functions disable tag checking only if in MTE async mode
  55. * since the sync mode generates exceptions synchronously and the
  56. * nofault or load_unaligned_zeropad can handle them.
  57. */
  58. static inline void __mte_disable_tco_async(void)
  59. {
  60. if (system_uses_mte_async_or_asymm_mode())
  61. mte_disable_tco();
  62. }
  63. static inline void __mte_enable_tco_async(void)
  64. {
  65. if (system_uses_mte_async_or_asymm_mode())
  66. mte_enable_tco();
  67. }
  68. /*
  69. * These functions are meant to be only used from KASAN runtime through
  70. * the arch_*() interface defined in asm/memory.h.
  71. * These functions don't include system_supports_mte() checks,
  72. * as KASAN only calls them when MTE is supported and enabled.
  73. */
  74. static inline u8 mte_get_ptr_tag(void *ptr)
  75. {
  76. /* Note: The format of KASAN tags is 0xF<x> */
  77. u8 tag = 0xF0 | (u8)(((u64)(ptr)) >> MTE_TAG_SHIFT);
  78. return tag;
  79. }
  80. /* Get allocation tag for the address. */
  81. static inline u8 mte_get_mem_tag(void *addr)
  82. {
  83. asm(__MTE_PREAMBLE "ldg %0, [%0]"
  84. : "+r" (addr));
  85. return mte_get_ptr_tag(addr);
  86. }
  87. /* Generate a random tag. */
  88. static inline u8 mte_get_random_tag(void)
  89. {
  90. void *addr;
  91. asm(__MTE_PREAMBLE "irg %0, %0"
  92. : "=r" (addr));
  93. return mte_get_ptr_tag(addr);
  94. }
  95. static inline u64 __stg_post(u64 p)
  96. {
  97. asm volatile(__MTE_PREAMBLE "stg %0, [%0], #16"
  98. : "+r"(p)
  99. :
  100. : "memory");
  101. return p;
  102. }
  103. static inline u64 __stzg_post(u64 p)
  104. {
  105. asm volatile(__MTE_PREAMBLE "stzg %0, [%0], #16"
  106. : "+r"(p)
  107. :
  108. : "memory");
  109. return p;
  110. }
  111. static inline void __dc_gva(u64 p)
  112. {
  113. asm volatile(__MTE_PREAMBLE "dc gva, %0" : : "r"(p) : "memory");
  114. }
  115. static inline void __dc_gzva(u64 p)
  116. {
  117. asm volatile(__MTE_PREAMBLE "dc gzva, %0" : : "r"(p) : "memory");
  118. }
  119. /*
  120. * Assign allocation tags for a region of memory based on the pointer tag.
  121. * Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and
  122. * size must be MTE_GRANULE_SIZE aligned.
  123. */
  124. static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag,
  125. bool init)
  126. {
  127. u64 curr, mask, dczid, dczid_bs, dczid_dzp, end1, end2, end3;
  128. /* Read DC G(Z)VA block size from the system register. */
  129. dczid = read_cpuid(DCZID_EL0);
  130. dczid_bs = 4ul << (dczid & 0xf);
  131. dczid_dzp = (dczid >> 4) & 1;
  132. curr = (u64)__tag_set(addr, tag);
  133. mask = dczid_bs - 1;
  134. /* STG/STZG up to the end of the first block. */
  135. end1 = curr | mask;
  136. end3 = curr + size;
  137. /* DC GVA / GZVA in [end1, end2) */
  138. end2 = end3 & ~mask;
  139. /*
  140. * The following code uses STG on the first DC GVA block even if the
  141. * start address is aligned - it appears to be faster than an alignment
  142. * check + conditional branch. Also, if the range size is at least 2 DC
  143. * GVA blocks, the first two loops can use post-condition to save one
  144. * branch each.
  145. */
  146. #define SET_MEMTAG_RANGE(stg_post, dc_gva) \
  147. do { \
  148. if (!dczid_dzp && size >= 2 * dczid_bs) {\
  149. do { \
  150. curr = stg_post(curr); \
  151. } while (curr < end1); \
  152. \
  153. do { \
  154. dc_gva(curr); \
  155. curr += dczid_bs; \
  156. } while (curr < end2); \
  157. } \
  158. \
  159. while (curr < end3) \
  160. curr = stg_post(curr); \
  161. } while (0)
  162. if (init)
  163. SET_MEMTAG_RANGE(__stzg_post, __dc_gzva);
  164. else
  165. SET_MEMTAG_RANGE(__stg_post, __dc_gva);
  166. #undef SET_MEMTAG_RANGE
  167. }
  168. void mte_enable_kernel_sync(void);
  169. void mte_enable_kernel_async(void);
  170. void mte_enable_kernel_asymm(void);
  171. #else /* CONFIG_ARM64_MTE */
  172. static inline void mte_disable_tco(void)
  173. {
  174. }
  175. static inline void mte_enable_tco(void)
  176. {
  177. }
  178. static inline void __mte_disable_tco_async(void)
  179. {
  180. }
  181. static inline void __mte_enable_tco_async(void)
  182. {
  183. }
  184. static inline u8 mte_get_ptr_tag(void *ptr)
  185. {
  186. return 0xFF;
  187. }
  188. static inline u8 mte_get_mem_tag(void *addr)
  189. {
  190. return 0xFF;
  191. }
  192. static inline u8 mte_get_random_tag(void)
  193. {
  194. return 0xFF;
  195. }
  196. static inline void mte_set_mem_tag_range(void *addr, size_t size,
  197. u8 tag, bool init)
  198. {
  199. }
  200. static inline void mte_enable_kernel_sync(void)
  201. {
  202. }
  203. static inline void mte_enable_kernel_async(void)
  204. {
  205. }
  206. static inline void mte_enable_kernel_asymm(void)
  207. {
  208. }
  209. #endif /* CONFIG_ARM64_MTE */
  210. #endif /* __ASSEMBLY__ */
  211. #endif /* __ASM_MTE_KASAN_H */