1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __ASM_MMAN_H__ 3 #define __ASM_MMAN_H__ 4 5 #include <uapi/asm/mman.h> 6 7 #ifndef BUILD_VDSO 8 #include <linux/compiler.h> 9 #include <linux/fs.h> 10 #include <linux/shmem_fs.h> 11 #include <linux/types.h> 12 13 static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, 14 unsigned long pkey) 15 { 16 unsigned long ret = 0; 17 18 if (system_supports_bti() && (prot & PROT_BTI)) 19 ret |= VM_ARM64_BTI; 20 21 if (system_supports_mte() && (prot & PROT_MTE)) 22 ret |= VM_MTE; 23 24 #ifdef CONFIG_ARCH_HAS_PKEYS 25 if (system_supports_poe()) { 26 ret |= pkey & BIT(0) ? VM_PKEY_BIT0 : 0; 27 ret |= pkey & BIT(1) ? VM_PKEY_BIT1 : 0; 28 ret |= pkey & BIT(2) ? VM_PKEY_BIT2 : 0; 29 } 30 #endif 31 32 return ret; 33 } 34 #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) 35 36 static inline unsigned long arch_calc_vm_flag_bits(struct file *file, 37 unsigned long flags) 38 { 39 /* 40 * Only allow MTE on anonymous mappings as these are guaranteed to be 41 * backed by tags-capable memory. The vm_flags may be overridden by a 42 * filesystem supporting MTE (RAM-based). 43 */ 44 if (system_supports_mte() && 45 ((flags & MAP_ANONYMOUS) || shmem_file(file))) 46 return VM_MTE_ALLOWED; 47 48 return 0; 49 } 50 #define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags) 51 52 static inline bool arch_validate_prot(unsigned long prot, 53 unsigned long addr __always_unused) 54 { 55 unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM; 56 57 if (system_supports_bti()) 58 supported |= PROT_BTI; 59 60 if (system_supports_mte()) 61 supported |= PROT_MTE; 62 63 return (prot & ~supported) == 0; 64 } 65 #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr) 66 67 static inline bool arch_validate_flags(unsigned long vm_flags) 68 { 69 if (!system_supports_mte()) 70 return true; 71 72 /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */ 73 return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED); 74 } 75 #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags) 76 77 #endif /* !BUILD_VDSO */ 78 79 #endif /* ! __ASM_MMAN_H__ */ 80