18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Corporation Limited 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <linux/export.h> 68c2ecf20Sopenharmony_ci#include <linux/io.h> 78c2ecf20Sopenharmony_ci#include <linux/memblock.h> 88c2ecf20Sopenharmony_ci#include <linux/mm.h> 98c2ecf20Sopenharmony_ci#include <linux/mman.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#define SHM_ALIGN_MASK (SHMLBA - 1) 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define COLOUR_ALIGN(addr, pgoff) \ 148c2ecf20Sopenharmony_ci ((((addr) + SHM_ALIGN_MASK) & ~SHM_ALIGN_MASK) \ 158c2ecf20Sopenharmony_ci + (((pgoff) << PAGE_SHIFT) & SHM_ALIGN_MASK)) 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cienum mmap_allocation_direction {UP, DOWN}; 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic unsigned long arch_get_unmapped_area_common(struct file *filp, 208c2ecf20Sopenharmony_ci unsigned long addr0, unsigned long len, unsigned long pgoff, 218c2ecf20Sopenharmony_ci unsigned long flags, enum mmap_allocation_direction dir) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci struct mm_struct *mm = current->mm; 248c2ecf20Sopenharmony_ci struct vm_area_struct *vma; 258c2ecf20Sopenharmony_ci unsigned long addr = addr0; 268c2ecf20Sopenharmony_ci int do_color_align; 278c2ecf20Sopenharmony_ci struct vm_unmapped_area_info info; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci if (unlikely(len > TASK_SIZE)) 308c2ecf20Sopenharmony_ci return -ENOMEM; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci if (flags & MAP_FIXED) { 338c2ecf20Sopenharmony_ci /* Even MAP_FIXED mappings must reside within TASK_SIZE */ 348c2ecf20Sopenharmony_ci if (TASK_SIZE - len < addr) 358c2ecf20Sopenharmony_ci return -EINVAL; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* 388c2ecf20Sopenharmony_ci * We do not accept a shared mapping if it would violate 398c2ecf20Sopenharmony_ci * cache aliasing constraints. 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_ci if ((flags & MAP_SHARED) && 428c2ecf20Sopenharmony_ci ((addr - (pgoff << PAGE_SHIFT)) & SHM_ALIGN_MASK)) 438c2ecf20Sopenharmony_ci return -EINVAL; 448c2ecf20Sopenharmony_ci return addr; 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci do_color_align = 0; 488c2ecf20Sopenharmony_ci if (filp || (flags & MAP_SHARED)) 498c2ecf20Sopenharmony_ci do_color_align = 1; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci /* requesting a specific address */ 528c2ecf20Sopenharmony_ci if (addr) { 538c2ecf20Sopenharmony_ci if (do_color_align) 548c2ecf20Sopenharmony_ci addr = COLOUR_ALIGN(addr, pgoff); 558c2ecf20Sopenharmony_ci else 568c2ecf20Sopenharmony_ci addr = PAGE_ALIGN(addr); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci vma = find_vma(mm, addr); 598c2ecf20Sopenharmony_ci if (TASK_SIZE - len >= addr && 608c2ecf20Sopenharmony_ci (!vma || addr + len <= vm_start_gap(vma))) 618c2ecf20Sopenharmony_ci return addr; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci info.length = len; 658c2ecf20Sopenharmony_ci info.align_mask = do_color_align ? (PAGE_MASK & SHM_ALIGN_MASK) : 0; 668c2ecf20Sopenharmony_ci info.align_offset = pgoff << PAGE_SHIFT; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (dir == DOWN) { 698c2ecf20Sopenharmony_ci info.flags = VM_UNMAPPED_AREA_TOPDOWN; 708c2ecf20Sopenharmony_ci info.low_limit = PAGE_SIZE; 718c2ecf20Sopenharmony_ci info.high_limit = mm->mmap_base; 728c2ecf20Sopenharmony_ci addr = vm_unmapped_area(&info); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (!(addr & ~PAGE_MASK)) 758c2ecf20Sopenharmony_ci return addr; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci /* 788c2ecf20Sopenharmony_ci * A failed mmap() very likely causes application failure, 798c2ecf20Sopenharmony_ci * so fall back to the bottom-up function here. This scenario 808c2ecf20Sopenharmony_ci * can happen with large stack limits and large mmap() 818c2ecf20Sopenharmony_ci * allocations. 828c2ecf20Sopenharmony_ci */ 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci info.flags = 0; 868c2ecf20Sopenharmony_ci info.low_limit = mm->mmap_base; 878c2ecf20Sopenharmony_ci info.high_limit = TASK_SIZE; 888c2ecf20Sopenharmony_ci return vm_unmapped_area(&info); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ciunsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0, 928c2ecf20Sopenharmony_ci unsigned long len, unsigned long pgoff, unsigned long flags) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci return arch_get_unmapped_area_common(filp, 958c2ecf20Sopenharmony_ci addr0, len, pgoff, flags, UP); 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci/* 998c2ecf20Sopenharmony_ci * There is no need to export this but sched.h declares the function as 1008c2ecf20Sopenharmony_ci * extern so making it static here results in an error. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ciunsigned long arch_get_unmapped_area_topdown(struct file *filp, 1038c2ecf20Sopenharmony_ci unsigned long addr0, unsigned long len, unsigned long pgoff, 1048c2ecf20Sopenharmony_ci unsigned long flags) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci return arch_get_unmapped_area_common(filp, 1078c2ecf20Sopenharmony_ci addr0, len, pgoff, flags, DOWN); 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ciint __virt_addr_valid(volatile void *kaddr) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci unsigned long vaddr = (unsigned long)kaddr; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if ((vaddr < PAGE_OFFSET) || (vaddr >= vm_map_base)) 1158c2ecf20Sopenharmony_ci return 0; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci return pfn_valid(PFN_DOWN(PHYSADDR(kaddr))); 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__virt_addr_valid); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* 1228c2ecf20Sopenharmony_ci * You really shouldn't be using read() or write() on /dev/mem. This might go 1238c2ecf20Sopenharmony_ci * away in the future. 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_ciint valid_phys_addr_range(phys_addr_t addr, size_t size) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci /* 1288c2ecf20Sopenharmony_ci * Check whether addr is covered by a memory region without the 1298c2ecf20Sopenharmony_ci * MEMBLOCK_NOMAP attribute, and whether that region covers the 1308c2ecf20Sopenharmony_ci * entire range. In theory, this could lead to false negatives 1318c2ecf20Sopenharmony_ci * if the range is covered by distinct but adjacent memory regions 1328c2ecf20Sopenharmony_ci * that only differ in other attributes. However, few of such 1338c2ecf20Sopenharmony_ci * attributes have been defined, and it is debatable whether it 1348c2ecf20Sopenharmony_ci * follows that /dev/mem read() calls should be able traverse 1358c2ecf20Sopenharmony_ci * such boundaries. 1368c2ecf20Sopenharmony_ci */ 1378c2ecf20Sopenharmony_ci return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr); 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* 1418c2ecf20Sopenharmony_ci * Do not allow /dev/mem mappings beyond the supported physical range. 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_ciint valid_mmap_phys_addr_range(unsigned long pfn, size_t size) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0))); 1468c2ecf20Sopenharmony_ci} 147