xref: /openbmc/linux/arch/x86/mm/pgprot.c (revision 2bc7d3e0)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/export.h>
4 #include <linux/mm.h>
5 #include <asm/pgtable.h>
6 
7 pgprot_t vm_get_page_prot(unsigned long vm_flags)
8 {
9 	unsigned long val = pgprot_val(protection_map[vm_flags &
10 				      (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]);
11 
12 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
13 	/*
14 	 * Take the 4 protection key bits out of the vma->vm_flags value and
15 	 * turn them in to the bits that we can put in to a pte.
16 	 *
17 	 * Only override these if Protection Keys are available (which is only
18 	 * on 64-bit).
19 	 */
20 	if (vm_flags & VM_PKEY_BIT0)
21 		val |= _PAGE_PKEY_BIT0;
22 	if (vm_flags & VM_PKEY_BIT1)
23 		val |= _PAGE_PKEY_BIT1;
24 	if (vm_flags & VM_PKEY_BIT2)
25 		val |= _PAGE_PKEY_BIT2;
26 	if (vm_flags & VM_PKEY_BIT3)
27 		val |= _PAGE_PKEY_BIT3;
28 #endif
29 
30 	val = __sme_set(val);
31 	if (val & _PAGE_PRESENT)
32 		val &= __supported_pte_mask;
33 	return __pgprot(val);
34 }
35 EXPORT_SYMBOL(vm_get_page_prot);
36