]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/arm/mm/mmap.c
ARM: implement CONFIG_STRICT_DEVMEM by disabling access to RAM via /dev/mem
[net-next-2.6.git] / arch / arm / mm / mmap.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/arm/mm/mmap.c
3 */
1da177e4
LT
4#include <linux/fs.h>
5#include <linux/mm.h>
6#include <linux/mman.h>
7#include <linux/shm.h>
e8edc6e0 8#include <linux/sched.h>
09d9bae0 9#include <linux/io.h>
cc92c28b 10#include <linux/random.h>
0ba8b9b2 11#include <asm/cputype.h>
1da177e4
LT
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 */
27unsigned long
28arch_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 */
0ba8b9b2
RK
43 cache_type = read_cpuid_cachetype();
44 if (cache_type != read_cpuid_id()) {
1da177e4
LT
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 /*
acec0ac0 55 * We enforce the MAP_FIXED case.
1da177e4
LT
56 */
57 if (flags & MAP_FIXED) {
e77414e0
AV
58 if (aliasing && flags & MAP_SHARED &&
59 (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
1da177e4
LT
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 }
1363c3cd
WW
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 }
cc92c28b
NP
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;
1da177e4
LT
87
88full_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;
1363c3cd 103 mm->cached_hole_size = 0;
1da177e4
LT
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 }
1363c3cd
WW
115 if (addr + mm->cached_hole_size < vma->vm_start)
116 mm->cached_hole_size = vma->vm_start - addr;
1da177e4
LT
117 addr = vma->vm_end;
118 if (do_align)
119 addr = COLOUR_ALIGN(addr, pgoff);
120 }
121}
122
51635ad2
LB
123
124/*
125 * You really shouldn't be using read() or write() on /dev/mem. This
126 * might go away in the future.
127 */
128int valid_phys_addr_range(unsigned long addr, size_t size)
129{
9ae3ae0b
AR
130 if (addr < PHYS_OFFSET)
131 return 0;
6806bfe1 132 if (addr + size > __pa(high_memory - 1) + 1)
51635ad2
LB
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 */
143int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
144{
145 return !(pfn + (size >> PAGE_SHIFT) > 0x00100000);
146}
087aaffc
NP
147
148#ifdef CONFIG_STRICT_DEVMEM
149
150#include <linux/ioport.h>
151
152/*
153 * devmem_is_allowed() checks to see if /dev/mem access to a certain
154 * address is valid. The argument is a physical page number.
155 * We mimic x86 here by disallowing access to system RAM as well as
156 * device-exclusive MMIO regions. This effectively disable read()/write()
157 * on /dev/mem.
158 */
159int devmem_is_allowed(unsigned long pfn)
160{
161 if (iomem_is_exclusive(pfn << PAGE_SHIFT))
162 return 0;
163 if (!page_is_ram(pfn))
164 return 1;
165 return 0;
166}
167
168#endif