xref: /linux/arch/arm64/include/asm/mman.h (revision 9f3419315f3cdc41a7318e4d50ba18a592b30c8c)
18ef8f360SDave Martin /* SPDX-License-Identifier: GPL-2.0 */
28ef8f360SDave Martin #ifndef __ASM_MMAN_H__
38ef8f360SDave Martin #define __ASM_MMAN_H__
48ef8f360SDave Martin 
58ef8f360SDave Martin #include <linux/compiler.h>
68ef8f360SDave Martin #include <linux/types.h>
78ef8f360SDave Martin #include <uapi/asm/mman.h>
88ef8f360SDave Martin 
98ef8f360SDave Martin static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
108ef8f360SDave Martin 	unsigned long pkey __always_unused)
118ef8f360SDave Martin {
12*9f341931SCatalin Marinas 	unsigned long ret = 0;
138ef8f360SDave Martin 
14*9f341931SCatalin Marinas 	if (system_supports_bti() && (prot & PROT_BTI))
15*9f341931SCatalin Marinas 		ret |= VM_ARM64_BTI;
16*9f341931SCatalin Marinas 
17*9f341931SCatalin Marinas 	if (system_supports_mte() && (prot & PROT_MTE))
18*9f341931SCatalin Marinas 		ret |= VM_MTE;
19*9f341931SCatalin Marinas 
20*9f341931SCatalin Marinas 	return ret;
218ef8f360SDave Martin }
228ef8f360SDave Martin #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
238ef8f360SDave Martin 
24*9f341931SCatalin Marinas static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags)
25*9f341931SCatalin Marinas {
26*9f341931SCatalin Marinas 	/*
27*9f341931SCatalin Marinas 	 * Only allow MTE on anonymous mappings as these are guaranteed to be
28*9f341931SCatalin Marinas 	 * backed by tags-capable memory. The vm_flags may be overridden by a
29*9f341931SCatalin Marinas 	 * filesystem supporting MTE (RAM-based).
30*9f341931SCatalin Marinas 	 */
31*9f341931SCatalin Marinas 	if (system_supports_mte() && (flags & MAP_ANONYMOUS))
32*9f341931SCatalin Marinas 		return VM_MTE_ALLOWED;
33*9f341931SCatalin Marinas 
34*9f341931SCatalin Marinas 	return 0;
35*9f341931SCatalin Marinas }
36*9f341931SCatalin Marinas #define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags)
37*9f341931SCatalin Marinas 
388ef8f360SDave Martin static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags)
398ef8f360SDave Martin {
40*9f341931SCatalin Marinas 	pteval_t prot = 0;
41*9f341931SCatalin Marinas 
42*9f341931SCatalin Marinas 	if (vm_flags & VM_ARM64_BTI)
43*9f341931SCatalin Marinas 		prot |= PTE_GP;
44*9f341931SCatalin Marinas 
45*9f341931SCatalin Marinas 	/*
46*9f341931SCatalin Marinas 	 * There are two conditions required for returning a Normal Tagged
47*9f341931SCatalin Marinas 	 * memory type: (1) the user requested it via PROT_MTE passed to
48*9f341931SCatalin Marinas 	 * mmap() or mprotect() and (2) the corresponding vma supports MTE. We
49*9f341931SCatalin Marinas 	 * register (1) as VM_MTE in the vma->vm_flags and (2) as
50*9f341931SCatalin Marinas 	 * VM_MTE_ALLOWED. Note that the latter can only be set during the
51*9f341931SCatalin Marinas 	 * mmap() call since mprotect() does not accept MAP_* flags.
52*9f341931SCatalin Marinas 	 */
53*9f341931SCatalin Marinas 	if ((vm_flags & VM_MTE) && (vm_flags & VM_MTE_ALLOWED))
54*9f341931SCatalin Marinas 		prot |= PTE_ATTRINDX(MT_NORMAL_TAGGED);
55*9f341931SCatalin Marinas 
56*9f341931SCatalin Marinas 	return __pgprot(prot);
578ef8f360SDave Martin }
588ef8f360SDave Martin #define arch_vm_get_page_prot(vm_flags) arch_vm_get_page_prot(vm_flags)
598ef8f360SDave Martin 
608ef8f360SDave Martin static inline bool arch_validate_prot(unsigned long prot,
618ef8f360SDave Martin 	unsigned long addr __always_unused)
628ef8f360SDave Martin {
638ef8f360SDave Martin 	unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM;
648ef8f360SDave Martin 
658ef8f360SDave Martin 	if (system_supports_bti())
668ef8f360SDave Martin 		supported |= PROT_BTI;
678ef8f360SDave Martin 
68*9f341931SCatalin Marinas 	if (system_supports_mte())
69*9f341931SCatalin Marinas 		supported |= PROT_MTE;
70*9f341931SCatalin Marinas 
718ef8f360SDave Martin 	return (prot & ~supported) == 0;
728ef8f360SDave Martin }
738ef8f360SDave Martin #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
748ef8f360SDave Martin 
758ef8f360SDave Martin #endif /* ! __ASM_MMAN_H__ */
76