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 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
12 EXPORT_SYMBOL(shm_align_mask);
14 #define COLOUR_ALIGN(addr, pgoff) \
15 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
16 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
18 enum mmap_allocation_direction {UP, DOWN};
20 static unsigned long arch_get_unmapped_area_common(struct file *filp,
21 unsigned long addr0, unsigned long len, unsigned long pgoff,
22 unsigned long flags, enum mmap_allocation_direction dir)
24 struct mm_struct *mm = current->mm;
25 struct vm_area_struct *vma;
26 unsigned long addr = addr0;
28 struct vm_unmapped_area_info info;
30 if (unlikely(len > TASK_SIZE))
33 if (flags & MAP_FIXED) {
34 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
35 if (TASK_SIZE - len < addr)
39 * We do not accept a shared mapping if it would violate
40 * cache aliasing constraints.
42 if ((flags & MAP_SHARED) &&
43 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
49 if (filp || (flags & MAP_SHARED))
52 /* requesting a specific address */
55 addr = COLOUR_ALIGN(addr, pgoff);
57 addr = PAGE_ALIGN(addr);
59 vma = find_vma(mm, addr);
60 if (TASK_SIZE - len >= addr &&
61 (!vma || addr + len <= vm_start_gap(vma)))
66 info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
67 info.align_offset = pgoff << PAGE_SHIFT;
70 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
71 info.low_limit = PAGE_SIZE;
72 info.high_limit = mm->mmap_base;
73 addr = vm_unmapped_area(&info);
75 if (!(addr & ~PAGE_MASK))
79 * A failed mmap() very likely causes application failure,
80 * so fall back to the bottom-up function here. This scenario
81 * can happen with large stack limits and large mmap()
87 info.low_limit = mm->mmap_base;
88 info.high_limit = TASK_SIZE;
89 return vm_unmapped_area(&info);
92 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
93 unsigned long len, unsigned long pgoff, unsigned long flags)
95 return arch_get_unmapped_area_common(filp,
96 addr0, len, pgoff, flags, UP);
100 * There is no need to export this but sched.h declares the function as
101 * extern so making it static here results in an error.
103 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
104 unsigned long addr0, unsigned long len, unsigned long pgoff,
107 return arch_get_unmapped_area_common(filp,
108 addr0, len, pgoff, flags, DOWN);
111 int __virt_addr_valid(volatile void *kaddr)
113 unsigned long vaddr = (unsigned long)kaddr;
115 if ((vaddr < PAGE_OFFSET) || (vaddr >= vm_map_base))
118 return pfn_valid(PFN_DOWN(PHYSADDR(kaddr)));
120 EXPORT_SYMBOL_GPL(__virt_addr_valid);
123 * You really shouldn't be using read() or write() on /dev/mem. This might go
124 * away in the future.
126 int valid_phys_addr_range(phys_addr_t addr, size_t size)
129 * Check whether addr is covered by a memory region without the
130 * MEMBLOCK_NOMAP attribute, and whether that region covers the
131 * entire range. In theory, this could lead to false negatives
132 * if the range is covered by distinct but adjacent memory regions
133 * that only differ in other attributes. However, few of such
134 * attributes have been defined, and it is debatable whether it
135 * follows that /dev/mem read() calls should be able traverse
138 return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr);
142 * Do not allow /dev/mem mappings beyond the supported physical range.
144 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
146 return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0)));