| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/arch/arm/mm/mmap.c | 
|  | 3 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/fs.h> | 
|  | 5 | #include <linux/mm.h> | 
|  | 6 | #include <linux/mman.h> | 
|  | 7 | #include <linux/shm.h> | 
| Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 8 | #include <linux/sched.h> | 
| Russell King | 09d9bae | 2008-09-05 14:08:44 +0100 | [diff] [blame] | 9 | #include <linux/io.h> | 
| Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 10 | #include <linux/random.h> | 
| Russell King | 0ba8b9b | 2008-08-10 18:08:10 +0100 | [diff] [blame] | 11 | #include <asm/cputype.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/system.h> | 
|  | 13 |  | 
|  | 14 | #define COLOUR_ALIGN(addr,pgoff)		\ | 
|  | 15 | ((((addr)+SHMLBA-1)&~(SHMLBA-1)) +	\ | 
|  | 16 | (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1))) | 
|  | 17 |  | 
|  | 18 | /* | 
|  | 19 | * We need to ensure that shared mappings are correctly aligned to | 
|  | 20 | * avoid aliasing issues with VIPT caches.  We need to ensure that | 
|  | 21 | * a specific page of an object is always mapped at a multiple of | 
|  | 22 | * SHMLBA bytes. | 
|  | 23 | * | 
|  | 24 | * We unconditionally provide this function for all cases, however | 
|  | 25 | * in the VIVT case, we optimise out the alignment rules. | 
|  | 26 | */ | 
|  | 27 | unsigned long | 
|  | 28 | arch_get_unmapped_area(struct file *filp, unsigned long addr, | 
|  | 29 | unsigned long len, unsigned long pgoff, unsigned long flags) | 
|  | 30 | { | 
|  | 31 | struct mm_struct *mm = current->mm; | 
|  | 32 | struct vm_area_struct *vma; | 
|  | 33 | unsigned long start_addr; | 
|  | 34 | #ifdef CONFIG_CPU_V6 | 
|  | 35 | unsigned int cache_type; | 
|  | 36 | int do_align = 0, aliasing = 0; | 
|  | 37 |  | 
|  | 38 | /* | 
|  | 39 | * We only need to do colour alignment if either the I or D | 
|  | 40 | * caches alias.  This is indicated by bits 9 and 21 of the | 
|  | 41 | * cache type register. | 
|  | 42 | */ | 
| Russell King | 0ba8b9b | 2008-08-10 18:08:10 +0100 | [diff] [blame] | 43 | cache_type = read_cpuid_cachetype(); | 
|  | 44 | if (cache_type != read_cpuid_id()) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | aliasing = (cache_type | cache_type >> 12) & (1 << 11); | 
|  | 46 | if (aliasing) | 
|  | 47 | do_align = filp || flags & MAP_SHARED; | 
|  | 48 | } | 
|  | 49 | #else | 
|  | 50 | #define do_align 0 | 
|  | 51 | #define aliasing 0 | 
|  | 52 | #endif | 
|  | 53 |  | 
|  | 54 | /* | 
| Benjamin Herrenschmidt | acec0ac | 2007-05-06 14:50:07 -0700 | [diff] [blame] | 55 | * We enforce the MAP_FIXED case. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ | 
|  | 57 | if (flags & MAP_FIXED) { | 
| Al Viro | e77414e | 2009-12-05 15:10:44 -0500 | [diff] [blame] | 58 | if (aliasing && flags & MAP_SHARED && | 
|  | 59 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return -EINVAL; | 
|  | 61 | return addr; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | if (len > TASK_SIZE) | 
|  | 65 | return -ENOMEM; | 
|  | 66 |  | 
|  | 67 | if (addr) { | 
|  | 68 | if (do_align) | 
|  | 69 | addr = COLOUR_ALIGN(addr, pgoff); | 
|  | 70 | else | 
|  | 71 | addr = PAGE_ALIGN(addr); | 
|  | 72 |  | 
|  | 73 | vma = find_vma(mm, addr); | 
|  | 74 | if (TASK_SIZE - len >= addr && | 
|  | 75 | (!vma || addr + len <= vma->vm_start)) | 
|  | 76 | return addr; | 
|  | 77 | } | 
| Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 78 | if (len > mm->cached_hole_size) { | 
|  | 79 | start_addr = addr = mm->free_area_cache; | 
|  | 80 | } else { | 
|  | 81 | start_addr = addr = TASK_UNMAPPED_BASE; | 
|  | 82 | mm->cached_hole_size = 0; | 
|  | 83 | } | 
| Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 84 | /* 8 bits of randomness in 20 address space bits */ | 
|  | 85 | if (current->flags & PF_RANDOMIZE) | 
|  | 86 | addr += (get_random_int() % (1 << 8)) << PAGE_SHIFT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | full_search: | 
|  | 89 | if (do_align) | 
|  | 90 | addr = COLOUR_ALIGN(addr, pgoff); | 
|  | 91 | else | 
|  | 92 | addr = PAGE_ALIGN(addr); | 
|  | 93 |  | 
|  | 94 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { | 
|  | 95 | /* At this point:  (!vma || addr < vma->vm_end). */ | 
|  | 96 | if (TASK_SIZE - len < addr) { | 
|  | 97 | /* | 
|  | 98 | * Start a new search - just in case we missed | 
|  | 99 | * some holes. | 
|  | 100 | */ | 
|  | 101 | if (start_addr != TASK_UNMAPPED_BASE) { | 
|  | 102 | start_addr = addr = TASK_UNMAPPED_BASE; | 
| Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 103 | mm->cached_hole_size = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | goto full_search; | 
|  | 105 | } | 
|  | 106 | return -ENOMEM; | 
|  | 107 | } | 
|  | 108 | if (!vma || addr + len <= vma->vm_start) { | 
|  | 109 | /* | 
|  | 110 | * Remember the place where we stopped the search: | 
|  | 111 | */ | 
|  | 112 | mm->free_area_cache = addr + len; | 
|  | 113 | return addr; | 
|  | 114 | } | 
| Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 115 | if (addr + mm->cached_hole_size < vma->vm_start) | 
|  | 116 | mm->cached_hole_size = vma->vm_start - addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | addr = vma->vm_end; | 
|  | 118 | if (do_align) | 
|  | 119 | addr = COLOUR_ALIGN(addr, pgoff); | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 |  | 
| Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 123 |  | 
|  | 124 | /* | 
|  | 125 | * You really shouldn't be using read() or write() on /dev/mem.  This | 
|  | 126 | * might go away in the future. | 
|  | 127 | */ | 
|  | 128 | int valid_phys_addr_range(unsigned long addr, size_t size) | 
|  | 129 | { | 
| Alexandre Rusev | 9ae3ae0 | 2008-02-26 18:42:10 +0100 | [diff] [blame] | 130 | if (addr < PHYS_OFFSET) | 
|  | 131 | return 0; | 
| Greg Ungerer | 6806bfe | 2009-10-02 00:45:28 +0100 | [diff] [blame] | 132 | if (addr + size > __pa(high_memory - 1) + 1) | 
| Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 133 | return 0; | 
|  | 134 |  | 
|  | 135 | return 1; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | /* | 
|  | 139 | * We don't use supersection mappings for mmap() on /dev/mem, which | 
|  | 140 | * means that we can't map the memory area above the 4G barrier into | 
|  | 141 | * userspace. | 
|  | 142 | */ | 
|  | 143 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) | 
|  | 144 | { | 
|  | 145 | return !(pfn + (size >> PAGE_SHIFT) > 0x00100000); | 
|  | 146 | } |