^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) #ifndef __ASM_MMAN_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __ASM_MMAN_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <uapi/asm/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) unsigned long pkey __always_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) unsigned long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) if (system_supports_bti() && (prot & PROT_BTI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) ret |= VM_ARM64_BTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (system_supports_mte() && (prot & PROT_MTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) ret |= VM_MTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Only allow MTE on anonymous mappings as these are guaranteed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * backed by tags-capable memory. The vm_flags may be overridden by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * filesystem supporting MTE (RAM-based).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (system_supports_mte() && (flags & MAP_ANONYMOUS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return VM_MTE_ALLOWED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pteval_t prot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (vm_flags & VM_ARM64_BTI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) prot |= PTE_GP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * There are two conditions required for returning a Normal Tagged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * memory type: (1) the user requested it via PROT_MTE passed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * mmap() or mprotect() and (2) the corresponding vma supports MTE. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * register (1) as VM_MTE in the vma->vm_flags and (2) as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * VM_MTE_ALLOWED. Note that the latter can only be set during the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * mmap() call since mprotect() does not accept MAP_* flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Checking for VM_MTE only is sufficient since arch_validate_flags()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * does not permit (VM_MTE & !VM_MTE_ALLOWED).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (vm_flags & VM_MTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) prot |= PTE_ATTRINDX(MT_NORMAL_TAGGED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return __pgprot(prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define arch_vm_get_page_prot(vm_flags) arch_vm_get_page_prot(vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static inline bool arch_validate_prot(unsigned long prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned long addr __always_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (system_supports_bti())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) supported |= PROT_BTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (system_supports_mte())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) supported |= PROT_MTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return (prot & ~supported) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static inline bool arch_validate_flags(unsigned long vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!system_supports_mte())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #endif /* ! __ASM_MMAN_H__ */