162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci#include <linux/export.h> 662306a36Sopenharmony_ci#include <linux/io.h> 762306a36Sopenharmony_ci#include <linux/memblock.h> 862306a36Sopenharmony_ci#include <linux/mm.h> 962306a36Sopenharmony_ci#include <linux/mman.h> 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#define SHM_ALIGN_MASK (SHMLBA - 1) 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#define COLOUR_ALIGN(addr, pgoff) \ 1462306a36Sopenharmony_ci ((((addr) + SHM_ALIGN_MASK) & ~SHM_ALIGN_MASK) \ 1562306a36Sopenharmony_ci + (((pgoff) << PAGE_SHIFT) & SHM_ALIGN_MASK)) 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cienum mmap_allocation_direction {UP, DOWN}; 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_cistatic unsigned long arch_get_unmapped_area_common(struct file *filp, 2062306a36Sopenharmony_ci unsigned long addr0, unsigned long len, unsigned long pgoff, 2162306a36Sopenharmony_ci unsigned long flags, enum mmap_allocation_direction dir) 2262306a36Sopenharmony_ci{ 2362306a36Sopenharmony_ci struct mm_struct *mm = current->mm; 2462306a36Sopenharmony_ci struct vm_area_struct *vma; 2562306a36Sopenharmony_ci unsigned long addr = addr0; 2662306a36Sopenharmony_ci int do_color_align; 2762306a36Sopenharmony_ci struct vm_unmapped_area_info info; 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci if (unlikely(len > TASK_SIZE)) 3062306a36Sopenharmony_ci return -ENOMEM; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci if (flags & MAP_FIXED) { 3362306a36Sopenharmony_ci /* Even MAP_FIXED mappings must reside within TASK_SIZE */ 3462306a36Sopenharmony_ci if (TASK_SIZE - len < addr) 3562306a36Sopenharmony_ci return -EINVAL; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci /* 3862306a36Sopenharmony_ci * We do not accept a shared mapping if it would violate 3962306a36Sopenharmony_ci * cache aliasing constraints. 4062306a36Sopenharmony_ci */ 4162306a36Sopenharmony_ci if ((flags & MAP_SHARED) && 4262306a36Sopenharmony_ci ((addr - (pgoff << PAGE_SHIFT)) & SHM_ALIGN_MASK)) 4362306a36Sopenharmony_ci return -EINVAL; 4462306a36Sopenharmony_ci return addr; 4562306a36Sopenharmony_ci } 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci do_color_align = 0; 4862306a36Sopenharmony_ci if (filp || (flags & MAP_SHARED)) 4962306a36Sopenharmony_ci do_color_align = 1; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci /* requesting a specific address */ 5262306a36Sopenharmony_ci if (addr) { 5362306a36Sopenharmony_ci if (do_color_align) 5462306a36Sopenharmony_ci addr = COLOUR_ALIGN(addr, pgoff); 5562306a36Sopenharmony_ci else 5662306a36Sopenharmony_ci addr = PAGE_ALIGN(addr); 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci vma = find_vma(mm, addr); 5962306a36Sopenharmony_ci if (TASK_SIZE - len >= addr && 6062306a36Sopenharmony_ci (!vma || addr + len <= vm_start_gap(vma))) 6162306a36Sopenharmony_ci return addr; 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci info.length = len; 6562306a36Sopenharmony_ci info.align_mask = do_color_align ? (PAGE_MASK & SHM_ALIGN_MASK) : 0; 6662306a36Sopenharmony_ci info.align_offset = pgoff << PAGE_SHIFT; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci if (dir == DOWN) { 6962306a36Sopenharmony_ci info.flags = VM_UNMAPPED_AREA_TOPDOWN; 7062306a36Sopenharmony_ci info.low_limit = PAGE_SIZE; 7162306a36Sopenharmony_ci info.high_limit = mm->mmap_base; 7262306a36Sopenharmony_ci addr = vm_unmapped_area(&info); 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci if (!(addr & ~PAGE_MASK)) 7562306a36Sopenharmony_ci return addr; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci /* 7862306a36Sopenharmony_ci * A failed mmap() very likely causes application failure, 7962306a36Sopenharmony_ci * so fall back to the bottom-up function here. This scenario 8062306a36Sopenharmony_ci * can happen with large stack limits and large mmap() 8162306a36Sopenharmony_ci * allocations. 8262306a36Sopenharmony_ci */ 8362306a36Sopenharmony_ci } 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci info.flags = 0; 8662306a36Sopenharmony_ci info.low_limit = mm->mmap_base; 8762306a36Sopenharmony_ci info.high_limit = TASK_SIZE; 8862306a36Sopenharmony_ci return vm_unmapped_area(&info); 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ciunsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0, 9262306a36Sopenharmony_ci unsigned long len, unsigned long pgoff, unsigned long flags) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci return arch_get_unmapped_area_common(filp, 9562306a36Sopenharmony_ci addr0, len, pgoff, flags, UP); 9662306a36Sopenharmony_ci} 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci/* 9962306a36Sopenharmony_ci * There is no need to export this but sched.h declares the function as 10062306a36Sopenharmony_ci * extern so making it static here results in an error. 10162306a36Sopenharmony_ci */ 10262306a36Sopenharmony_ciunsigned long arch_get_unmapped_area_topdown(struct file *filp, 10362306a36Sopenharmony_ci unsigned long addr0, unsigned long len, unsigned long pgoff, 10462306a36Sopenharmony_ci unsigned long flags) 10562306a36Sopenharmony_ci{ 10662306a36Sopenharmony_ci return arch_get_unmapped_area_common(filp, 10762306a36Sopenharmony_ci addr0, len, pgoff, flags, DOWN); 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciint __virt_addr_valid(volatile void *kaddr) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci unsigned long vaddr = (unsigned long)kaddr; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci if ((vaddr < PAGE_OFFSET) || (vaddr >= vm_map_base)) 11562306a36Sopenharmony_ci return 0; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci return pfn_valid(PFN_DOWN(PHYSADDR(kaddr))); 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__virt_addr_valid); 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci/* 12262306a36Sopenharmony_ci * You really shouldn't be using read() or write() on /dev/mem. This might go 12362306a36Sopenharmony_ci * away in the future. 12462306a36Sopenharmony_ci */ 12562306a36Sopenharmony_ciint valid_phys_addr_range(phys_addr_t addr, size_t size) 12662306a36Sopenharmony_ci{ 12762306a36Sopenharmony_ci /* 12862306a36Sopenharmony_ci * Check whether addr is covered by a memory region without the 12962306a36Sopenharmony_ci * MEMBLOCK_NOMAP attribute, and whether that region covers the 13062306a36Sopenharmony_ci * entire range. In theory, this could lead to false negatives 13162306a36Sopenharmony_ci * if the range is covered by distinct but adjacent memory regions 13262306a36Sopenharmony_ci * that only differ in other attributes. However, few of such 13362306a36Sopenharmony_ci * attributes have been defined, and it is debatable whether it 13462306a36Sopenharmony_ci * follows that /dev/mem read() calls should be able traverse 13562306a36Sopenharmony_ci * such boundaries. 13662306a36Sopenharmony_ci */ 13762306a36Sopenharmony_ci return memblock_is_region_memory(addr, size) && memblock_is_map_memory(addr); 13862306a36Sopenharmony_ci} 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci/* 14162306a36Sopenharmony_ci * Do not allow /dev/mem mappings beyond the supported physical range. 14262306a36Sopenharmony_ci */ 14362306a36Sopenharmony_ciint valid_mmap_phys_addr_range(unsigned long pfn, size_t size) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci return !(((pfn << PAGE_SHIFT) + size) & ~(GENMASK_ULL(cpu_pabits, 0))); 14662306a36Sopenharmony_ci} 147