File Index
Files and directories
Sign in
FILE

mmap.c

File preview with syntax highlighting.

linux-6.19.14/arch/arm64/mm/mmap.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Based on arch/arm/mm/mmap.c
  4 *
  5 * Copyright (C) 2012 ARM Ltd.
  6 */
  7
  8#include <linux/io.h>
  9#include <linux/memblock.h>
 10#include <linux/mm.h>
 11#include <linux/types.h>
 12
 13#include <asm/cpufeature.h>
 14#include <asm/page.h>
 15
 16static pgprot_t protection_map[16] __ro_after_init = {
 17	[VM_NONE]					= PAGE_NONE,
 18	[VM_READ]					= PAGE_READONLY,
 19	[VM_WRITE]					= PAGE_READONLY,
 20	[VM_WRITE | VM_READ]				= PAGE_READONLY,
 21	/* PAGE_EXECONLY if Enhanced PAN */
 22	[VM_EXEC]					= PAGE_READONLY_EXEC,
 23	[VM_EXEC | VM_READ]				= PAGE_READONLY_EXEC,
 24	[VM_EXEC | VM_WRITE]				= PAGE_READONLY_EXEC,
 25	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_READONLY_EXEC,
 26	[VM_SHARED]					= PAGE_NONE,
 27	[VM_SHARED | VM_READ]				= PAGE_READONLY,
 28	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
 29	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED,
 30	/* PAGE_EXECONLY if Enhanced PAN */
 31	[VM_SHARED | VM_EXEC]				= PAGE_READONLY_EXEC,
 32	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY_EXEC,
 33	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED_EXEC,
 34	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED_EXEC
 35};
 36
 37static ptdesc_t gcs_page_prot __ro_after_init = _PAGE_GCS_RO;
 38
 39/*
 40 * You really shouldn't be using read() or write() on /dev/mem.  This might go
 41 * away in the future.
 42 */
 43int valid_phys_addr_range(phys_addr_t addr, size_t size)
 44{
 45	/*
 46	 * Check whether addr is covered by a memory region without the
 47	 * MEMBLOCK_NOMAP attribute, and whether that region covers the
 48	 * entire range. In theory, this could lead to false negatives
 49	 * if the range is covered by distinct but adjacent memory regions
 50	 * that only differ in other attributes. However, few of such
 51	 * attributes have been defined, and it is debatable whether it
 52	 * follows that /dev/mem read() calls should be able traverse
 53	 * such boundaries.
 54	 */
 55	return memblock_is_region_memory(addr, size) &&
 56	       memblock_is_map_memory(addr);
 57}
 58
 59/*
 60 * Do not allow /dev/mem mappings beyond the supported physical range.
 61 */
 62int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
 63{
 64	return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
 65}
 66
 67static int __init adjust_protection_map(void)
 68{
 69	/*
 70	 * With Enhanced PAN we can honour the execute-only permissions as
 71	 * there is no PAN override with such mappings.
 72	 */
 73	if (cpus_have_cap(ARM64_HAS_EPAN)) {
 74		protection_map[VM_EXEC] = PAGE_EXECONLY;
 75		protection_map[VM_EXEC | VM_SHARED] = PAGE_EXECONLY;
 76	}
 77
 78	if (lpa2_is_enabled()) {
 79		for (int i = 0; i < ARRAY_SIZE(protection_map); i++)
 80			pgprot_val(protection_map[i]) &= ~PTE_SHARED;
 81		gcs_page_prot &= ~PTE_SHARED;
 82	}
 83
 84	return 0;
 85}
 86arch_initcall(adjust_protection_map);
 87
 88pgprot_t vm_get_page_prot(vm_flags_t vm_flags)
 89{
 90	ptdesc_t prot;
 91
 92	/* Short circuit GCS to avoid bloating the table. */
 93	if (system_supports_gcs() && (vm_flags & VM_SHADOW_STACK)) {
 94		/* Honour mprotect(PROT_NONE) on shadow stack mappings */
 95		if (vm_flags & VM_ACCESS_FLAGS)
 96			prot = gcs_page_prot;
 97		else
 98			prot = pgprot_val(protection_map[VM_NONE]);
 99	} else {
100		prot = pgprot_val(protection_map[vm_flags &
101				   (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]);
102	}
103
104	if (vm_flags & VM_ARM64_BTI)
105		prot |= PTE_GP;
106
107	/*
108	 * There are two conditions required for returning a Normal Tagged
109	 * memory type: (1) the user requested it via PROT_MTE passed to
110	 * mmap() or mprotect() and (2) the corresponding vma supports MTE. We
111	 * register (1) as VM_MTE in the vma->vm_flags and (2) as
112	 * VM_MTE_ALLOWED. Note that the latter can only be set during the
113	 * mmap() call since mprotect() does not accept MAP_* flags.
114	 * Checking for VM_MTE only is sufficient since arch_validate_flags()
115	 * does not permit (VM_MTE & !VM_MTE_ALLOWED).
116	 */
117	if (vm_flags & VM_MTE)
118		prot |= PTE_ATTRINDX(MT_NORMAL_TAGGED);
119
120#ifdef CONFIG_ARCH_HAS_PKEYS
121	if (system_supports_poe()) {
122		if (vm_flags & VM_PKEY_BIT0)
123			prot |= PTE_PO_IDX_0;
124		if (vm_flags & VM_PKEY_BIT1)
125			prot |= PTE_PO_IDX_1;
126		if (vm_flags & VM_PKEY_BIT2)
127			prot |= PTE_PO_IDX_2;
128	}
129#endif
130
131	return __pgprot(prot);
132}
133EXPORT_SYMBOL(vm_get_page_prot);
~/ Read-only file preview.