Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Copyright (C) 2011 Wind River Systems,
0007  *   written by Ralf Baechle <ralf@linux-mips.org>
0008  */
0009 #include <linux/compiler.h>
0010 #include <linux/elf-randomize.h>
0011 #include <linux/errno.h>
0012 #include <linux/mm.h>
0013 #include <linux/mman.h>
0014 #include <linux/export.h>
0015 #include <linux/personality.h>
0016 #include <linux/random.h>
0017 #include <linux/sched/signal.h>
0018 #include <linux/sched/mm.h>
0019 
0020 unsigned long shm_align_mask = PAGE_SIZE - 1;   /* Sane caches */
0021 EXPORT_SYMBOL(shm_align_mask);
0022 
0023 #define COLOUR_ALIGN(addr, pgoff)               \
0024     ((((addr) + shm_align_mask) & ~shm_align_mask) +    \
0025      (((pgoff) << PAGE_SHIFT) & shm_align_mask))
0026 
0027 enum mmap_allocation_direction {UP, DOWN};
0028 
0029 static unsigned long arch_get_unmapped_area_common(struct file *filp,
0030     unsigned long addr0, unsigned long len, unsigned long pgoff,
0031     unsigned long flags, enum mmap_allocation_direction dir)
0032 {
0033     struct mm_struct *mm = current->mm;
0034     struct vm_area_struct *vma;
0035     unsigned long addr = addr0;
0036     int do_color_align;
0037     struct vm_unmapped_area_info info;
0038 
0039     if (unlikely(len > TASK_SIZE))
0040         return -ENOMEM;
0041 
0042     if (flags & MAP_FIXED) {
0043         /* Even MAP_FIXED mappings must reside within TASK_SIZE */
0044         if (TASK_SIZE - len < addr)
0045             return -EINVAL;
0046 
0047         /*
0048          * We do not accept a shared mapping if it would violate
0049          * cache aliasing constraints.
0050          */
0051         if ((flags & MAP_SHARED) &&
0052             ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
0053             return -EINVAL;
0054         return addr;
0055     }
0056 
0057     do_color_align = 0;
0058     if (filp || (flags & MAP_SHARED))
0059         do_color_align = 1;
0060 
0061     /* requesting a specific address */
0062     if (addr) {
0063         if (do_color_align)
0064             addr = COLOUR_ALIGN(addr, pgoff);
0065         else
0066             addr = PAGE_ALIGN(addr);
0067 
0068         vma = find_vma(mm, addr);
0069         if (TASK_SIZE - len >= addr &&
0070             (!vma || addr + len <= vm_start_gap(vma)))
0071             return addr;
0072     }
0073 
0074     info.length = len;
0075     info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
0076     info.align_offset = pgoff << PAGE_SHIFT;
0077 
0078     if (dir == DOWN) {
0079         info.flags = VM_UNMAPPED_AREA_TOPDOWN;
0080         info.low_limit = PAGE_SIZE;
0081         info.high_limit = mm->mmap_base;
0082         addr = vm_unmapped_area(&info);
0083 
0084         if (!(addr & ~PAGE_MASK))
0085             return addr;
0086 
0087         /*
0088          * A failed mmap() very likely causes application failure,
0089          * so fall back to the bottom-up function here. This scenario
0090          * can happen with large stack limits and large mmap()
0091          * allocations.
0092          */
0093     }
0094 
0095     info.flags = 0;
0096     info.low_limit = mm->mmap_base;
0097     info.high_limit = TASK_SIZE;
0098     return vm_unmapped_area(&info);
0099 }
0100 
0101 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
0102     unsigned long len, unsigned long pgoff, unsigned long flags)
0103 {
0104     return arch_get_unmapped_area_common(filp,
0105             addr0, len, pgoff, flags, UP);
0106 }
0107 
0108 /*
0109  * There is no need to export this but sched.h declares the function as
0110  * extern so making it static here results in an error.
0111  */
0112 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
0113     unsigned long addr0, unsigned long len, unsigned long pgoff,
0114     unsigned long flags)
0115 {
0116     return arch_get_unmapped_area_common(filp,
0117             addr0, len, pgoff, flags, DOWN);
0118 }
0119 
0120 bool __virt_addr_valid(const volatile void *kaddr)
0121 {
0122     unsigned long vaddr = (unsigned long)kaddr;
0123 
0124     if ((vaddr < PAGE_OFFSET) || (vaddr >= MAP_BASE))
0125         return false;
0126 
0127     return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
0128 }
0129 EXPORT_SYMBOL_GPL(__virt_addr_valid);