1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
5 #include <linux/export.h>
7 #include <linux/memblock.h>
9 #include <linux/mman.h>
11 #define SHM_ALIGN_MASK (SHMLBA - 1)
13 #define COLOUR_ALIGN(addr, pgoff) \
14 ((((addr) + SHM_ALIGN_MASK) & ~SHM_ALIGN_MASK) \
15 + (((pgoff) << PAGE_SHIFT) & SHM_ALIGN_MASK))
17 enum mmap_allocation_direction {UP, DOWN};
19 static unsigned long arch_get_unmapped_area_common(struct file *filp,
20 unsigned long addr0, unsigned long len, unsigned long pgoff,
21 unsigned long flags, enum mmap_allocation_direction dir)
23 struct mm_struct *mm = current->mm;
24 struct vm_area_struct *vma;
25 unsigned long addr = addr0;
27 struct vm_unmapped_area_info info;
29 if (unlikely(len > TASK_SIZE))
32 if (flags & MAP_FIXED) {
33 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
34 if (TASK_SIZE - len < addr)
38 * We do not accept a shared mapping if it would violate
39 * cache aliasing constraints.
41 if ((flags & MAP_SHARED) &&
42 ((addr - (pgoff << PAGE_SHIFT)) & SHM_ALIGN_MASK))
48 if (filp || (flags & MAP_SHARED))
51 /* requesting a specific address */
54 addr = COLOUR_ALIGN(addr, pgoff);
56 addr = PAGE_ALIGN(addr);
58 vma = find_vma(mm, addr);
59 if (TASK_SIZE - len >= addr &&
60 (!vma || addr + len <= vm_start_gap(vma)))
65 info.align_mask = do_color_align ? (PAGE_MASK & SHM_ALIGN_MASK) : 0;
66 info.align_offset = pgoff << PAGE_SHIFT;
69 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
70 info.low_limit = PAGE_SIZE;
71 info.high_limit = mm->mmap_base;
72 addr = vm_unmapped_area(&info);
74 if (!(addr & ~PAGE_MASK))
78 * A failed mmap() very likely causes application failure,
79 * so fall back to the bottom-up function here. This scenario
80 * can happen with large stack limits and large mmap()
86 info.low_limit = mm->mmap_base;
87 info.high_limit = TASK_SIZE;
88 return vm_unmapped_area(&info);
91 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
92 unsigned long len, unsigned long pgoff, unsigned long flags)
94 return arch_get_unmapped_area_common(filp,
95 addr0, len, pgoff, flags, UP);
99 * There is no need to export this but sched.h declares the function as
100 * extern so making it static here results in an error.
102 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
103 unsigned long addr0, unsigned long len, unsigned long pgoff,
106 return arch_get_unmapped_area_common(filp,
107 addr0, len, pgoff, flags, DOWN);
110 int __virt_addr_valid(volatile void *kaddr)
112 unsigned long vaddr = (unsigned long)kaddr;
114 if ((vaddr < PAGE_OFFSET) || (vaddr >= vm_map_base))
117 return pfn_valid(PFN_DOWN(PHYSADDR(kaddr)));
119 EXPORT_SYMBOL_GPL(__virt_addr_valid);
122 * You really shouldn't be using read() or write() on /dev/mem. This might go
123 * away in the future.
125 int valid_phys_addr_range(phys_addr_t addr, size_t size)
128 * Check whether addr is covered by a memory region without the
129 * MEMBLOCK_NOMAP attribute, and whether that region covers the
130 * entire range. In theory, this could lead to false negatives
131 * if the range is covered by distinct but adjacent memory regions
132 * that only differ in other attributes. However, few of such
133 * attributes have been defined, and it is debatable whether it
134 * follows that /dev/mem read() calls should be able traverse
137 return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr);
141 * Do not allow /dev/mem mappings beyond the supported physical range.
143 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
145 return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0)));