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/hugetlb.h>
11 #include <linux/shmem_fs.h>
12 #include <linux/types.h>
13
arch_calc_vm_prot_bits(unsigned long prot,unsigned long pkey)14 static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
15 unsigned long pkey)
16 {
17 unsigned long ret = 0;
18
19 if (system_supports_bti() && (prot & PROT_BTI))
20 ret |= VM_ARM64_BTI;
21
22 if (system_supports_mte() && (prot & PROT_MTE))
23 ret |= VM_MTE;
24
25 #ifdef CONFIG_ARCH_HAS_PKEYS
26 if (system_supports_poe()) {
27 ret |= pkey & BIT(0) ? VM_PKEY_BIT0 : 0;
28 ret |= pkey & BIT(1) ? VM_PKEY_BIT1 : 0;
29 ret |= pkey & BIT(2) ? VM_PKEY_BIT2 : 0;
30 }
31 #endif
32
33 return ret;
34 }
35 #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
36
arch_calc_vm_flag_bits(struct file * file,unsigned long flags)37 static inline unsigned long arch_calc_vm_flag_bits(struct file *file,
38 unsigned long flags)
39 {
40 /*
41 * Only allow MTE on anonymous mappings as these are guaranteed to be
42 * backed by tags-capable memory. The vm_flags may be overridden by a
43 * filesystem supporting MTE (RAM-based).
44 */
45 if (system_supports_mte()) {
46 if (flags & (MAP_ANONYMOUS | MAP_HUGETLB))
47 return VM_MTE_ALLOWED;
48 if (shmem_file(file) || is_file_hugepages(file))
49 return VM_MTE_ALLOWED;
50 }
51
52 return 0;
53 }
54 #define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags)
55
arch_validate_prot(unsigned long prot,unsigned long addr __always_unused)56 static inline bool arch_validate_prot(unsigned long prot,
57 unsigned long addr __always_unused)
58 {
59 unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM;
60
61 if (system_supports_bti())
62 supported |= PROT_BTI;
63
64 if (system_supports_mte())
65 supported |= PROT_MTE;
66
67 return (prot & ~supported) == 0;
68 }
69 #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
70
arch_validate_flags(unsigned long vm_flags)71 static inline bool arch_validate_flags(unsigned long vm_flags)
72 {
73 if (system_supports_mte()) {
74 /*
75 * only allow VM_MTE if VM_MTE_ALLOWED has been set
76 * previously
77 */
78 if ((vm_flags & VM_MTE) && !(vm_flags & VM_MTE_ALLOWED))
79 return false;
80 }
81
82 if (system_supports_gcs() && (vm_flags & VM_SHADOW_STACK)) {
83 /* An executable GCS isn't a good idea. */
84 if (vm_flags & VM_EXEC)
85 return false;
86
87 /* The memory management core should prevent this */
88 VM_WARN_ON(vm_flags & VM_SHARED);
89 }
90
91 return true;
92
93 }
94 #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
95
96 #endif /* !BUILD_VDSO */
97
98 #endif /* ! __ASM_MMAN_H__ */
99