Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/export.h>
0004 #include <linux/mm.h>
0005 #include <asm/pgtable.h>
0006 #include <asm/mem_encrypt.h>
0007 
0008 static pgprot_t protection_map[16] __ro_after_init = {
0009     [VM_NONE]                   = PAGE_NONE,
0010     [VM_READ]                   = PAGE_READONLY,
0011     [VM_WRITE]                  = PAGE_COPY,
0012     [VM_WRITE | VM_READ]                = PAGE_COPY,
0013     [VM_EXEC]                   = PAGE_READONLY_EXEC,
0014     [VM_EXEC | VM_READ]             = PAGE_READONLY_EXEC,
0015     [VM_EXEC | VM_WRITE]                = PAGE_COPY_EXEC,
0016     [VM_EXEC | VM_WRITE | VM_READ]          = PAGE_COPY_EXEC,
0017     [VM_SHARED]                 = PAGE_NONE,
0018     [VM_SHARED | VM_READ]               = PAGE_READONLY,
0019     [VM_SHARED | VM_WRITE]              = PAGE_SHARED,
0020     [VM_SHARED | VM_WRITE | VM_READ]        = PAGE_SHARED,
0021     [VM_SHARED | VM_EXEC]               = PAGE_READONLY_EXEC,
0022     [VM_SHARED | VM_EXEC | VM_READ]         = PAGE_READONLY_EXEC,
0023     [VM_SHARED | VM_EXEC | VM_WRITE]        = PAGE_SHARED_EXEC,
0024     [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]  = PAGE_SHARED_EXEC
0025 };
0026 
0027 void add_encrypt_protection_map(void)
0028 {
0029     unsigned int i;
0030 
0031     for (i = 0; i < ARRAY_SIZE(protection_map); i++)
0032         protection_map[i] = pgprot_encrypted(protection_map[i]);
0033 }
0034 
0035 pgprot_t vm_get_page_prot(unsigned long vm_flags)
0036 {
0037     unsigned long val = pgprot_val(protection_map[vm_flags &
0038                       (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]);
0039 
0040 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
0041     /*
0042      * Take the 4 protection key bits out of the vma->vm_flags value and
0043      * turn them in to the bits that we can put in to a pte.
0044      *
0045      * Only override these if Protection Keys are available (which is only
0046      * on 64-bit).
0047      */
0048     if (vm_flags & VM_PKEY_BIT0)
0049         val |= _PAGE_PKEY_BIT0;
0050     if (vm_flags & VM_PKEY_BIT1)
0051         val |= _PAGE_PKEY_BIT1;
0052     if (vm_flags & VM_PKEY_BIT2)
0053         val |= _PAGE_PKEY_BIT2;
0054     if (vm_flags & VM_PKEY_BIT3)
0055         val |= _PAGE_PKEY_BIT3;
0056 #endif
0057 
0058     val = __sme_set(val);
0059     if (val & _PAGE_PRESENT)
0060         val &= __supported_pte_mask;
0061     return __pgprot(val);
0062 }
0063 EXPORT_SYMBOL(vm_get_page_prot);