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 8 #include <asm/compiler.h> 9 #include <asm/mte-def.h> 10 11 #ifndef __ASSEMBLY__ 12 13 #include <linux/bitfield.h> 14 #include <linux/page-flags.h> 15 #include <linux/types.h> 16 17 #include <asm/pgtable-types.h> 18 19 extern u64 gcr_kernel_excl; 20 21 void mte_clear_page_tags(void *addr); 22 unsigned long mte_copy_tags_from_user(void *to, const void __user *from, 23 unsigned long n); 24 unsigned long mte_copy_tags_to_user(void __user *to, void *from, 25 unsigned long n); 26 int mte_save_tags(struct page *page); 27 void mte_save_page_tags(const void *page_addr, void *tag_storage); 28 bool mte_restore_tags(swp_entry_t entry, struct page *page); 29 void mte_restore_page_tags(void *page_addr, const void *tag_storage); 30 void mte_invalidate_tags(int type, pgoff_t offset); 31 void mte_invalidate_tags_area(int type); 32 void *mte_allocate_tag_storage(void); 33 void mte_free_tag_storage(char *storage); 34 35 #ifdef CONFIG_ARM64_MTE 36 37 /* track which pages have valid allocation tags */ 38 #define PG_mte_tagged PG_arch_2 39 40 void mte_zero_clear_page_tags(void *addr); 41 void mte_sync_tags(pte_t old_pte, pte_t pte); 42 void mte_copy_page_tags(void *kto, const void *kfrom); 43 void mte_thread_init_user(void); 44 void mte_thread_switch(struct task_struct *next); 45 void mte_suspend_enter(void); 46 void mte_suspend_exit(void); 47 long set_mte_ctrl(struct task_struct *task, unsigned long arg); 48 long get_mte_ctrl(struct task_struct *task); 49 int mte_ptrace_copy_tags(struct task_struct *child, long request, 50 unsigned long addr, unsigned long data); 51 52 #else /* CONFIG_ARM64_MTE */ 53 54 /* unused if !CONFIG_ARM64_MTE, silence the compiler */ 55 #define PG_mte_tagged 0 56 57 static inline void mte_zero_clear_page_tags(void *addr) 58 { 59 } 60 static inline void mte_sync_tags(pte_t old_pte, pte_t pte) 61 { 62 } 63 static inline void mte_copy_page_tags(void *kto, const void *kfrom) 64 { 65 } 66 static inline void mte_thread_init_user(void) 67 { 68 } 69 static inline void mte_thread_switch(struct task_struct *next) 70 { 71 } 72 static inline void mte_suspend_enter(void) 73 { 74 } 75 static inline void mte_suspend_exit(void) 76 { 77 } 78 static inline long set_mte_ctrl(struct task_struct *task, unsigned long arg) 79 { 80 return 0; 81 } 82 static inline long get_mte_ctrl(struct task_struct *task) 83 { 84 return 0; 85 } 86 static inline int mte_ptrace_copy_tags(struct task_struct *child, 87 long request, unsigned long addr, 88 unsigned long data) 89 { 90 return -EIO; 91 } 92 93 #endif /* CONFIG_ARM64_MTE */ 94 95 #ifdef CONFIG_KASAN_HW_TAGS 96 /* Whether the MTE asynchronous mode is enabled. */ 97 DECLARE_STATIC_KEY_FALSE(mte_async_mode); 98 99 static inline bool system_uses_mte_async_mode(void) 100 { 101 return static_branch_unlikely(&mte_async_mode); 102 } 103 104 void mte_check_tfsr_el1(void); 105 106 static inline void mte_check_tfsr_entry(void) 107 { 108 mte_check_tfsr_el1(); 109 } 110 111 static inline void mte_check_tfsr_exit(void) 112 { 113 /* 114 * The asynchronous faults are sync'ed automatically with 115 * TFSR_EL1 on kernel entry but for exit an explicit dsb() 116 * is required. 117 */ 118 dsb(nsh); 119 isb(); 120 121 mte_check_tfsr_el1(); 122 } 123 #else 124 static inline bool system_uses_mte_async_mode(void) 125 { 126 return false; 127 } 128 static inline void mte_check_tfsr_el1(void) 129 { 130 } 131 static inline void mte_check_tfsr_entry(void) 132 { 133 } 134 static inline void mte_check_tfsr_exit(void) 135 { 136 } 137 #endif /* CONFIG_KASAN_HW_TAGS */ 138 139 #endif /* __ASSEMBLY__ */ 140 #endif /* __ASM_MTE_H */ 141