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
arch_calc_vm_prot_bits(unsigned long prot,unsigned long pkey)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
arch_calc_vm_flag_bits(struct file * file,unsigned long flags)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 if ((flags & MAP_ANONYMOUS) && !(flags & MAP_HUGETLB))
46 return VM_MTE_ALLOWED;
47 if (shmem_file(file))
48 return VM_MTE_ALLOWED;
49 }
50
51 return 0;
52 }
53 #define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags)
54
arch_validate_prot(unsigned long prot,unsigned long addr __always_unused)55 static inline bool arch_validate_prot(unsigned long prot,
56 unsigned long addr __always_unused)
57 {
58 unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM;
59
60 if (system_supports_bti())
61 supported |= PROT_BTI;
62
63 if (system_supports_mte())
64 supported |= PROT_MTE;
65
66 return (prot & ~supported) == 0;
67 }
68 #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
69
arch_validate_flags(unsigned long vm_flags)70 static inline bool arch_validate_flags(unsigned long vm_flags)
71 {
72 if (!system_supports_mte())
73 return true;
74
75 /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */
76 return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED);
77 }
78 #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
79
80 #endif /* !BUILD_VDSO */
81
82 #endif /* ! __ASM_MMAN_H__ */
83