www.usr.com/support/gpl/USR9107_release.1.4.tar.gz
[bcm963xx.git] / kernel / linux / mm / mprotect.c
1 /*
2  *  mm/mprotect.c
3  *
4  *  (C) Copyright 1994 Linus Torvalds
5  *  (C) Copyright 2002 Christoph Hellwig
6  *
7  *  Address space accounting code       <alan@redhat.com>
8  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9  */
10
11 #include <linux/mm.h>
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
16 #include <linux/fs.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
20 #include <linux/personality.h>
21
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
26
27 static inline void
28 change_pte_range(pmd_t *pmd, unsigned long address,
29                 unsigned long size, pgprot_t newprot)
30 {
31         pte_t * pte;
32         unsigned long end;
33
34         if (pmd_none(*pmd))
35                 return;
36         if (pmd_bad(*pmd)) {
37                 pmd_ERROR(*pmd);
38                 pmd_clear(pmd);
39                 return;
40         }
41         pte = pte_offset_map(pmd, address);
42         address &= ~PMD_MASK;
43         end = address + size;
44         if (end > PMD_SIZE)
45                 end = PMD_SIZE;
46         do {
47                 if (pte_present(*pte)) {
48                         pte_t entry;
49
50                         /* Avoid an SMP race with hardware updated dirty/clean
51                          * bits by wiping the pte and then setting the new pte
52                          * into place.
53                          */
54                         entry = ptep_get_and_clear(pte);
55                         set_pte(pte, pte_modify(entry, newprot));
56                 }
57                 address += PAGE_SIZE;
58                 pte++;
59         } while (address && (address < end));
60         pte_unmap(pte - 1);
61 }
62
63 static inline void
64 change_pmd_range(pgd_t *pgd, unsigned long address,
65                 unsigned long size, pgprot_t newprot)
66 {
67         pmd_t * pmd;
68         unsigned long end;
69
70         if (pgd_none(*pgd))
71                 return;
72         if (pgd_bad(*pgd)) {
73                 pgd_ERROR(*pgd);
74                 pgd_clear(pgd);
75                 return;
76         }
77         pmd = pmd_offset(pgd, address);
78         address &= ~PGDIR_MASK;
79         end = address + size;
80         if (end > PGDIR_SIZE)
81                 end = PGDIR_SIZE;
82         do {
83                 change_pte_range(pmd, address, end - address, newprot);
84                 address = (address + PMD_SIZE) & PMD_MASK;
85                 pmd++;
86         } while (address && (address < end));
87 }
88
89 static void
90 change_protection(struct vm_area_struct *vma, unsigned long start,
91                 unsigned long end, pgprot_t newprot)
92 {
93         pgd_t *dir;
94         unsigned long beg = start;
95
96         dir = pgd_offset(current->mm, start);
97         flush_cache_range(vma, beg, end);
98         if (start >= end)
99                 BUG();
100         spin_lock(&current->mm->page_table_lock);
101         do {
102                 change_pmd_range(dir, start, end - start, newprot);
103                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
104                 dir++;
105         } while (start && (start < end));
106         flush_tlb_range(vma, beg, end);
107         spin_unlock(&current->mm->page_table_lock);
108         return;
109 }
110
111 static int
112 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
113         unsigned long start, unsigned long end, unsigned int newflags)
114 {
115         struct mm_struct * mm = vma->vm_mm;
116         unsigned long charged = 0;
117         pgprot_t newprot;
118         pgoff_t pgoff;
119         int error;
120
121         if (newflags == vma->vm_flags) {
122                 *pprev = vma;
123                 return 0;
124         }
125
126         /*
127          * If we make a private mapping writable we increase our commit;
128          * but (without finer accounting) cannot reduce our commit if we
129          * make it unwritable again.
130          *
131          * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
132          * a MAP_NORESERVE private mapping to writable will now reserve.
133          */
134         if (newflags & VM_WRITE) {
135                 if (!(vma->vm_flags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
136                         charged = (end - start) >> PAGE_SHIFT;
137                         if (security_vm_enough_memory(charged))
138                                 return -ENOMEM;
139                         newflags |= VM_ACCOUNT;
140                 }
141         }
142
143         newprot = protection_map[newflags & 0xf];
144
145         /*
146          * First try to merge with previous and/or next vma.
147          */
148         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
149         *pprev = vma_merge(mm, *pprev, start, end, newflags,
150                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
151         if (*pprev) {
152                 vma = *pprev;
153                 goto success;
154         }
155
156         if (start != vma->vm_start) {
157                 error = split_vma(mm, vma, start, 1);
158                 if (error)
159                         goto fail;
160         }
161         /*
162          * Unless it returns an error, this function always sets *pprev to
163          * the first vma for which vma->vm_end >= end.
164          */
165         *pprev = vma;
166
167         if (end != vma->vm_end) {
168                 error = split_vma(mm, vma, end, 0);
169                 if (error)
170                         goto fail;
171         }
172
173 success:
174         /*
175          * vm_flags and vm_page_prot are protected by the mmap_sem
176          * held in write mode.
177          */
178         vma->vm_flags = newflags;
179         vma->vm_page_prot = newprot;
180         change_protection(vma, start, end, newprot);
181         return 0;
182
183 fail:
184         vm_unacct_memory(charged);
185         return error;
186 }
187
188 asmlinkage long
189 sys_mprotect(unsigned long start, size_t len, unsigned long prot)
190 {
191         unsigned long vm_flags, nstart, end, tmp;
192         struct vm_area_struct *vma, *prev;
193         int error = -EINVAL;
194         const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
195         prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
196         if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
197                 return -EINVAL;
198
199         if (start & ~PAGE_MASK)
200                 return -EINVAL;
201         len = PAGE_ALIGN(len);
202         end = start + len;
203         if (end < start)
204                 return -ENOMEM;
205         if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
206                 return -EINVAL;
207         if (end == start)
208                 return 0;
209         /*
210          * Does the application expect PROT_READ to imply PROT_EXEC:
211          */
212         if (unlikely((prot & PROT_READ) &&
213                         (current->personality & READ_IMPLIES_EXEC)))
214                 prot |= PROT_EXEC;
215
216         vm_flags = calc_vm_prot_bits(prot);
217
218         down_write(&current->mm->mmap_sem);
219
220         vma = find_vma_prev(current->mm, start, &prev);
221         error = -ENOMEM;
222         if (!vma)
223                 goto out;
224         if (unlikely(grows & PROT_GROWSDOWN)) {
225                 if (vma->vm_start >= end)
226                         goto out;
227                 start = vma->vm_start;
228                 error = -EINVAL;
229                 if (!(vma->vm_flags & VM_GROWSDOWN))
230                         goto out;
231         }
232         else {
233                 if (vma->vm_start > start)
234                         goto out;
235                 if (unlikely(grows & PROT_GROWSUP)) {
236                         end = vma->vm_end;
237                         error = -EINVAL;
238                         if (!(vma->vm_flags & VM_GROWSUP))
239                                 goto out;
240                 }
241         }
242         if (start > vma->vm_start)
243                 prev = vma;
244
245         for (nstart = start ; ; ) {
246                 unsigned int newflags;
247
248                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
249
250                 if (is_vm_hugetlb_page(vma)) {
251                         error = -EACCES;
252                         goto out;
253                 }
254
255                 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
256
257                 if ((newflags & ~(newflags >> 4)) & 0xf) {
258                         error = -EACCES;
259                         goto out;
260                 }
261
262                 error = security_file_mprotect(vma, prot);
263                 if (error)
264                         goto out;
265
266                 tmp = vma->vm_end;
267                 if (tmp > end)
268                         tmp = end;
269                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
270                 if (error)
271                         goto out;
272                 nstart = tmp;
273
274                 if (nstart < prev->vm_end)
275                         nstart = prev->vm_end;
276                 if (nstart >= end)
277                         goto out;
278
279                 vma = prev->vm_next;
280                 if (!vma || vma->vm_start != nstart) {
281                         error = -ENOMEM;
282                         goto out;
283                 }
284         }
285 out:
286         up_write(&current->mm->mmap_sem);
287         return error;
288 }