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