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