mte.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020 ARM Ltd.
  4. */
  5. #ifndef __ASM_MTE_H
  6. #define __ASM_MTE_H
  7. #include <asm/compiler.h>
  8. #include <asm/mte-def.h>
  9. #ifndef __ASSEMBLY__
  10. #include <linux/bitfield.h>
  11. #include <linux/kasan-enabled.h>
  12. #include <linux/page-flags.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <asm/pgtable-types.h>
  16. void mte_clear_page_tags(void *addr);
  17. unsigned long mte_copy_tags_from_user(void *to, const void __user *from,
  18. unsigned long n);
  19. unsigned long mte_copy_tags_to_user(void __user *to, void *from,
  20. unsigned long n);
  21. int mte_save_tags(struct page *page);
  22. void mte_save_page_tags(const void *page_addr, void *tag_storage);
  23. void mte_restore_tags(swp_entry_t entry, struct page *page);
  24. void mte_restore_page_tags(void *page_addr, const void *tag_storage);
  25. void mte_invalidate_tags(int type, pgoff_t offset);
  26. void mte_invalidate_tags_area(int type);
  27. void *mte_allocate_tag_storage(void);
  28. void mte_free_tag_storage(char *storage);
  29. #ifdef CONFIG_ARM64_MTE
  30. /* track which pages have valid allocation tags */
  31. #define PG_mte_tagged PG_arch_2
  32. static inline void set_page_mte_tagged(struct page *page)
  33. {
  34. /*
  35. * Ensure that the tags written prior to this function are visible
  36. * before the page flags update.
  37. */
  38. smp_wmb();
  39. set_bit(PG_mte_tagged, &page->flags);
  40. }
  41. static inline bool page_mte_tagged(struct page *page)
  42. {
  43. bool ret = test_bit(PG_mte_tagged, &page->flags);
  44. /*
  45. * If the page is tagged, ensure ordering with a likely subsequent
  46. * read of the tags.
  47. */
  48. if (ret)
  49. smp_rmb();
  50. return ret;
  51. }
  52. void mte_zero_clear_page_tags(void *addr);
  53. void mte_sync_tags(pte_t pte);
  54. void mte_copy_page_tags(void *kto, const void *kfrom);
  55. void mte_thread_init_user(void);
  56. void mte_thread_switch(struct task_struct *next);
  57. void mte_cpu_setup(void);
  58. void mte_suspend_enter(void);
  59. void mte_suspend_exit(void);
  60. long set_mte_ctrl(struct task_struct *task, unsigned long arg);
  61. long get_mte_ctrl(struct task_struct *task);
  62. int mte_ptrace_copy_tags(struct task_struct *child, long request,
  63. unsigned long addr, unsigned long data);
  64. size_t mte_probe_user_range(const char __user *uaddr, size_t size);
  65. #else /* CONFIG_ARM64_MTE */
  66. /* unused if !CONFIG_ARM64_MTE, silence the compiler */
  67. #define PG_mte_tagged 0
  68. static inline void set_page_mte_tagged(struct page *page)
  69. {
  70. }
  71. static inline bool page_mte_tagged(struct page *page)
  72. {
  73. return false;
  74. }
  75. static inline void mte_zero_clear_page_tags(void *addr)
  76. {
  77. }
  78. static inline void mte_sync_tags(pte_t pte)
  79. {
  80. }
  81. static inline void mte_copy_page_tags(void *kto, const void *kfrom)
  82. {
  83. }
  84. static inline void mte_thread_init_user(void)
  85. {
  86. }
  87. static inline void mte_thread_switch(struct task_struct *next)
  88. {
  89. }
  90. static inline void mte_suspend_enter(void)
  91. {
  92. }
  93. static inline void mte_suspend_exit(void)
  94. {
  95. }
  96. static inline long set_mte_ctrl(struct task_struct *task, unsigned long arg)
  97. {
  98. return 0;
  99. }
  100. static inline long get_mte_ctrl(struct task_struct *task)
  101. {
  102. return 0;
  103. }
  104. static inline int mte_ptrace_copy_tags(struct task_struct *child,
  105. long request, unsigned long addr,
  106. unsigned long data)
  107. {
  108. return -EIO;
  109. }
  110. #endif /* CONFIG_ARM64_MTE */
  111. static inline void mte_disable_tco_entry(struct task_struct *task)
  112. {
  113. if (!system_supports_mte())
  114. return;
  115. /*
  116. * Re-enable tag checking (TCO set on exception entry). This is only
  117. * necessary if MTE is enabled in either the kernel or the userspace
  118. * task in synchronous or asymmetric mode (SCTLR_EL1.TCF0 bit 0 is set
  119. * for both). With MTE disabled in the kernel and disabled or
  120. * asynchronous in userspace, tag check faults (including in uaccesses)
  121. * are not reported, therefore there is no need to re-enable checking.
  122. * This is beneficial on microarchitectures where re-enabling TCO is
  123. * expensive.
  124. */
  125. if (kasan_hw_tags_enabled() ||
  126. (task->thread.sctlr_user & (1UL << SCTLR_EL1_TCF0_SHIFT)))
  127. asm volatile(SET_PSTATE_TCO(0));
  128. }
  129. #ifdef CONFIG_KASAN_HW_TAGS
  130. void mte_check_tfsr_el1(void);
  131. static inline void mte_check_tfsr_entry(void)
  132. {
  133. if (!system_supports_mte())
  134. return;
  135. mte_check_tfsr_el1();
  136. }
  137. static inline void mte_check_tfsr_exit(void)
  138. {
  139. if (!system_supports_mte())
  140. return;
  141. /*
  142. * The asynchronous faults are sync'ed automatically with
  143. * TFSR_EL1 on kernel entry but for exit an explicit dsb()
  144. * is required.
  145. */
  146. dsb(nsh);
  147. isb();
  148. mte_check_tfsr_el1();
  149. }
  150. #else
  151. static inline void mte_check_tfsr_el1(void)
  152. {
  153. }
  154. static inline void mte_check_tfsr_entry(void)
  155. {
  156. }
  157. static inline void mte_check_tfsr_exit(void)
  158. {
  159. }
  160. #endif /* CONFIG_KASAN_HW_TAGS */
  161. #endif /* __ASSEMBLY__ */
  162. #endif /* __ASM_MTE_H */