Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Based on arch/arm/mm/mmap.c
0004  *
0005  * Copyright (C) 2012 ARM Ltd.
0006  */
0007 
0008 #include <linux/io.h>
0009 #include <linux/memblock.h>
0010 #include <linux/mm.h>
0011 #include <linux/types.h>
0012 
0013 #include <asm/cpufeature.h>
0014 #include <asm/page.h>
0015 
0016 static pgprot_t protection_map[16] __ro_after_init = {
0017     [VM_NONE]                   = PAGE_NONE,
0018     [VM_READ]                   = PAGE_READONLY,
0019     [VM_WRITE]                  = PAGE_READONLY,
0020     [VM_WRITE | VM_READ]                = PAGE_READONLY,
0021     /* PAGE_EXECONLY if Enhanced PAN */
0022     [VM_EXEC]                   = PAGE_READONLY_EXEC,
0023     [VM_EXEC | VM_READ]             = PAGE_READONLY_EXEC,
0024     [VM_EXEC | VM_WRITE]                = PAGE_READONLY_EXEC,
0025     [VM_EXEC | VM_WRITE | VM_READ]          = PAGE_READONLY_EXEC,
0026     [VM_SHARED]                 = PAGE_NONE,
0027     [VM_SHARED | VM_READ]               = PAGE_READONLY,
0028     [VM_SHARED | VM_WRITE]              = PAGE_SHARED,
0029     [VM_SHARED | VM_WRITE | VM_READ]        = PAGE_SHARED,
0030     /* PAGE_EXECONLY if Enhanced PAN */
0031     [VM_SHARED | VM_EXEC]               = PAGE_READONLY_EXEC,
0032     [VM_SHARED | VM_EXEC | VM_READ]         = PAGE_READONLY_EXEC,
0033     [VM_SHARED | VM_EXEC | VM_WRITE]        = PAGE_SHARED_EXEC,
0034     [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]  = PAGE_SHARED_EXEC
0035 };
0036 
0037 /*
0038  * You really shouldn't be using read() or write() on /dev/mem.  This might go
0039  * away in the future.
0040  */
0041 int valid_phys_addr_range(phys_addr_t addr, size_t size)
0042 {
0043     /*
0044      * Check whether addr is covered by a memory region without the
0045      * MEMBLOCK_NOMAP attribute, and whether that region covers the
0046      * entire range. In theory, this could lead to false negatives
0047      * if the range is covered by distinct but adjacent memory regions
0048      * that only differ in other attributes. However, few of such
0049      * attributes have been defined, and it is debatable whether it
0050      * follows that /dev/mem read() calls should be able traverse
0051      * such boundaries.
0052      */
0053     return memblock_is_region_memory(addr, size) &&
0054            memblock_is_map_memory(addr);
0055 }
0056 
0057 /*
0058  * Do not allow /dev/mem mappings beyond the supported physical range.
0059  */
0060 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
0061 {
0062     return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
0063 }
0064 
0065 static int __init adjust_protection_map(void)
0066 {
0067     /*
0068      * With Enhanced PAN we can honour the execute-only permissions as
0069      * there is no PAN override with such mappings.
0070      */
0071     if (cpus_have_const_cap(ARM64_HAS_EPAN)) {
0072         protection_map[VM_EXEC] = PAGE_EXECONLY;
0073         protection_map[VM_EXEC | VM_SHARED] = PAGE_EXECONLY;
0074     }
0075 
0076     return 0;
0077 }
0078 arch_initcall(adjust_protection_map);
0079 
0080 pgprot_t vm_get_page_prot(unsigned long vm_flags)
0081 {
0082     pteval_t prot = pgprot_val(protection_map[vm_flags &
0083                    (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]);
0084 
0085     if (vm_flags & VM_ARM64_BTI)
0086         prot |= PTE_GP;
0087 
0088     /*
0089      * There are two conditions required for returning a Normal Tagged
0090      * memory type: (1) the user requested it via PROT_MTE passed to
0091      * mmap() or mprotect() and (2) the corresponding vma supports MTE. We
0092      * register (1) as VM_MTE in the vma->vm_flags and (2) as
0093      * VM_MTE_ALLOWED. Note that the latter can only be set during the
0094      * mmap() call since mprotect() does not accept MAP_* flags.
0095      * Checking for VM_MTE only is sufficient since arch_validate_flags()
0096      * does not permit (VM_MTE & !VM_MTE_ALLOWED).
0097      */
0098     if (vm_flags & VM_MTE)
0099         prot |= PTE_ATTRINDX(MT_NORMAL_TAGGED);
0100 
0101     return __pgprot(prot);
0102 }
0103 EXPORT_SYMBOL(vm_get_page_prot);