xref: /linux/arch/arm64/include/asm/mman.h (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 		if (flags & (MAP_ANONYMOUS | 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 
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 
70 static inline bool arch_validate_flags(unsigned long vm_flags)
71 {
72 	if (system_supports_mte()) {
73 		/*
74 		 * only allow VM_MTE if VM_MTE_ALLOWED has been set
75 		 * previously
76 		 */
77 		if ((vm_flags & VM_MTE) && !(vm_flags & VM_MTE_ALLOWED))
78 			return false;
79 	}
80 
81 	if (system_supports_gcs() && (vm_flags & VM_SHADOW_STACK)) {
82 		/* An executable GCS isn't a good idea. */
83 		if (vm_flags & VM_EXEC)
84 			return false;
85 
86 		/* The memory management core should prevent this */
87 		VM_WARN_ON(vm_flags & VM_SHARED);
88 	}
89 
90 	return true;
91 
92 }
93 #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
94 
95 #endif /* !BUILD_VDSO */
96 
97 #endif /* ! __ASM_MMAN_H__ */
98