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 | df5419a | 2011-04-13 04:57:17 +0100 | [diff] [blame] | 10 | #include <linux/personality.h> |
Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 11 | #include <linux/random.h> |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 12 | #include <asm/cachetype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 34 | int do_align = 0; |
| 35 | int aliasing = cache_is_vipt_aliasing(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * We only need to do colour alignment if either the I or D |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 39 | * caches alias. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
Rob Herring | 41dfaa9 | 2011-11-22 04:01:06 +0100 | [diff] [blame] | 41 | if (aliasing) |
| 42 | do_align = filp || (flags & MAP_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
Benjamin Herrenschmidt | acec0ac | 2007-05-06 14:50:07 -0700 | [diff] [blame] | 45 | * We enforce the MAP_FIXED case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | */ |
| 47 | if (flags & MAP_FIXED) { |
Al Viro | e77414e | 2009-12-05 15:10:44 -0500 | [diff] [blame] | 48 | if (aliasing && flags & MAP_SHARED && |
| 49 | (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return -EINVAL; |
| 51 | return addr; |
| 52 | } |
| 53 | |
| 54 | if (len > TASK_SIZE) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | if (addr) { |
| 58 | if (do_align) |
| 59 | addr = COLOUR_ALIGN(addr, pgoff); |
| 60 | else |
| 61 | addr = PAGE_ALIGN(addr); |
| 62 | |
| 63 | vma = find_vma(mm, addr); |
| 64 | if (TASK_SIZE - len >= addr && |
| 65 | (!vma || addr + len <= vma->vm_start)) |
| 66 | return addr; |
| 67 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 68 | if (len > mm->cached_hole_size) { |
| 69 | start_addr = addr = mm->free_area_cache; |
| 70 | } else { |
| 71 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 72 | mm->cached_hole_size = 0; |
| 73 | } |
Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 74 | /* 8 bits of randomness in 20 address space bits */ |
Nicolas Pitre | df5419a | 2011-04-13 04:57:17 +0100 | [diff] [blame] | 75 | if ((current->flags & PF_RANDOMIZE) && |
| 76 | !(current->personality & ADDR_NO_RANDOMIZE)) |
Nicolas Pitre | cc92c28 | 2010-06-14 21:16:19 -0400 | [diff] [blame] | 77 | addr += (get_random_int() % (1 << 8)) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | full_search: |
| 80 | if (do_align) |
| 81 | addr = COLOUR_ALIGN(addr, pgoff); |
| 82 | else |
| 83 | addr = PAGE_ALIGN(addr); |
| 84 | |
| 85 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { |
| 86 | /* At this point: (!vma || addr < vma->vm_end). */ |
| 87 | if (TASK_SIZE - len < addr) { |
| 88 | /* |
| 89 | * Start a new search - just in case we missed |
| 90 | * some holes. |
| 91 | */ |
| 92 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 93 | start_addr = addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 94 | mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | goto full_search; |
| 96 | } |
| 97 | return -ENOMEM; |
| 98 | } |
| 99 | if (!vma || addr + len <= vma->vm_start) { |
| 100 | /* |
| 101 | * Remember the place where we stopped the search: |
| 102 | */ |
| 103 | mm->free_area_cache = addr + len; |
| 104 | return addr; |
| 105 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 106 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 107 | mm->cached_hole_size = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | addr = vma->vm_end; |
| 109 | if (do_align) |
| 110 | addr = COLOUR_ALIGN(addr, pgoff); |
| 111 | } |
| 112 | } |
| 113 | |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * You really shouldn't be using read() or write() on /dev/mem. This |
| 117 | * might go away in the future. |
| 118 | */ |
| 119 | int valid_phys_addr_range(unsigned long addr, size_t size) |
| 120 | { |
Alexandre Rusev | 9ae3ae0 | 2008-02-26 18:42:10 +0100 | [diff] [blame] | 121 | if (addr < PHYS_OFFSET) |
| 122 | return 0; |
Greg Ungerer | 6806bfe | 2009-10-02 00:45:28 +0100 | [diff] [blame] | 123 | if (addr + size > __pa(high_memory - 1) + 1) |
Lennert Buytenhek | 51635ad | 2006-09-16 10:50:22 +0100 | [diff] [blame] | 124 | return 0; |
| 125 | |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * We don't use supersection mappings for mmap() on /dev/mem, which |
| 131 | * means that we can't map the memory area above the 4G barrier into |
| 132 | * userspace. |
| 133 | */ |
| 134 | int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) |
| 135 | { |
| 136 | return !(pfn + (size >> PAGE_SHIFT) > 0x00100000); |
| 137 | } |
Nicolas Pitre | 087aaff | 2010-09-22 18:34:36 -0400 | [diff] [blame] | 138 | |
| 139 | #ifdef CONFIG_STRICT_DEVMEM |
| 140 | |
| 141 | #include <linux/ioport.h> |
| 142 | |
| 143 | /* |
| 144 | * devmem_is_allowed() checks to see if /dev/mem access to a certain |
| 145 | * address is valid. The argument is a physical page number. |
| 146 | * We mimic x86 here by disallowing access to system RAM as well as |
| 147 | * device-exclusive MMIO regions. This effectively disable read()/write() |
| 148 | * on /dev/mem. |
| 149 | */ |
| 150 | int devmem_is_allowed(unsigned long pfn) |
| 151 | { |
| 152 | if (iomem_is_exclusive(pfn << PAGE_SHIFT)) |
| 153 | return 0; |
| 154 | if (!page_is_ram(pfn)) |
| 155 | return 1; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | #endif |