1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 4 */ 5 #include <linux/export.h> 6 #include <linux/io.h> 7 #include <linux/kfence.h> 8 #include <linux/memblock.h> 9 #include <linux/mm.h> 10 #include <linux/mman.h> 11 12 #define SHM_ALIGN_MASK (SHMLBA - 1) 13 14 #define COLOUR_ALIGN(addr, pgoff) \ 15 ((((addr) + SHM_ALIGN_MASK) & ~SHM_ALIGN_MASK) \ 16 + (((pgoff) << PAGE_SHIFT) & SHM_ALIGN_MASK)) 17 18 enum mmap_allocation_direction {UP, DOWN}; 19 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) 23 { 24 struct mm_struct *mm = current->mm; 25 struct vm_area_struct *vma; 26 unsigned long addr = addr0; 27 int do_color_align; 28 struct vm_unmapped_area_info info = {}; 29 30 if (unlikely(len > TASK_SIZE)) 31 return -ENOMEM; 32 33 if (flags & MAP_FIXED) { 34 /* Even MAP_FIXED mappings must reside within TASK_SIZE */ 35 if (TASK_SIZE - len < addr) 36 return -EINVAL; 37 38 /* 39 * We do not accept a shared mapping if it would violate 40 * cache aliasing constraints. 41 */ 42 if ((flags & MAP_SHARED) && 43 ((addr - (pgoff << PAGE_SHIFT)) & SHM_ALIGN_MASK)) 44 return -EINVAL; 45 return addr; 46 } 47 48 do_color_align = 0; 49 if (filp || (flags & MAP_SHARED)) 50 do_color_align = 1; 51 52 /* requesting a specific address */ 53 if (addr) { 54 if (do_color_align) 55 addr = COLOUR_ALIGN(addr, pgoff); 56 else 57 addr = PAGE_ALIGN(addr); 58 59 vma = find_vma(mm, addr); 60 if (TASK_SIZE - len >= addr && 61 (!vma || addr + len <= vm_start_gap(vma))) 62 return addr; 63 } 64 65 info.length = len; 66 info.align_mask = do_color_align ? (PAGE_MASK & SHM_ALIGN_MASK) : 0; 67 info.align_offset = pgoff << PAGE_SHIFT; 68 69 if (dir == DOWN) { 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); 74 75 if (!(addr & ~PAGE_MASK)) 76 return addr; 77 78 /* 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() 82 * allocations. 83 */ 84 } 85 86 info.low_limit = mm->mmap_base; 87 info.high_limit = TASK_SIZE; 88 return vm_unmapped_area(&info); 89 } 90 91 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0, 92 unsigned long len, unsigned long pgoff, unsigned long flags) 93 { 94 return arch_get_unmapped_area_common(filp, 95 addr0, len, pgoff, flags, UP); 96 } 97 98 /* 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. 101 */ 102 unsigned long arch_get_unmapped_area_topdown(struct file *filp, 103 unsigned long addr0, unsigned long len, unsigned long pgoff, 104 unsigned long flags) 105 { 106 return arch_get_unmapped_area_common(filp, 107 addr0, len, pgoff, flags, DOWN); 108 } 109 110 int __virt_addr_valid(volatile void *kaddr) 111 { 112 unsigned long vaddr = (unsigned long)kaddr; 113 114 if (is_kfence_address((void *)kaddr)) 115 return 1; 116 117 if ((vaddr < PAGE_OFFSET) || (vaddr >= vm_map_base)) 118 return 0; 119 120 return pfn_valid(PFN_DOWN(PHYSADDR(kaddr))); 121 } 122 EXPORT_SYMBOL_GPL(__virt_addr_valid); 123 124 /* 125 * You really shouldn't be using read() or write() on /dev/mem. This might go 126 * away in the future. 127 */ 128 int valid_phys_addr_range(phys_addr_t addr, size_t size) 129 { 130 /* 131 * Check whether addr is covered by a memory region without the 132 * MEMBLOCK_NOMAP attribute, and whether that region covers the 133 * entire range. In theory, this could lead to false negatives 134 * if the range is covered by distinct but adjacent memory regions 135 * that only differ in other attributes. However, few of such 136 * attributes have been defined, and it is debatable whether it 137 * follows that /dev/mem read() calls should be able traverse 138 * such boundaries. 139 */ 140 return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr); 141 } 142 143 /* 144 * Do not allow /dev/mem mappings beyond the supported physical range. 145 */ 146 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) 147 { 148 return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0))); 149 } 150