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