4 * (C) Copyright 1994 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
7 * Address space accounting code <alan@redhat.com>
8 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
20 #include <linux/personality.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/pgalloc.h>
25 #include <asm/cacheflush.h>
26 #include <asm/tlbflush.h>
29 change_pte_range(pmd_t *pmd, unsigned long address,
30 unsigned long size, pgprot_t newprot)
42 pte = pte_offset_map(pmd, address);
48 if (pte_present(*pte)) {
51 /* Avoid an SMP race with hardware updated dirty/clean
52 * bits by wiping the pte and then setting the new pte
55 entry = ptep_get_and_clear(pte);
56 set_pte(pte, pte_modify(entry, newprot));
60 } while (address && (address < end));
65 change_pmd_range(pgd_t *pgd, unsigned long address,
66 unsigned long size, pgprot_t newprot)
78 pmd = pmd_offset(pgd, address);
79 address &= ~PGDIR_MASK;
84 change_pte_range(pmd, address, end - address, newprot);
85 address = (address + PMD_SIZE) & PMD_MASK;
87 } while (address && (address < end));
91 change_protection(struct vm_area_struct *vma, unsigned long start,
92 unsigned long end, pgprot_t newprot)
95 unsigned long beg = start;
97 dir = pgd_offset(current->mm, start);
98 flush_cache_range(vma, beg, end);
101 spin_lock(¤t->mm->page_table_lock);
103 change_pmd_range(dir, start, end - start, newprot);
104 start = (start + PGDIR_SIZE) & PGDIR_MASK;
106 } while (start && (start < end));
107 flush_tlb_range(vma, beg, end);
108 spin_unlock(¤t->mm->page_table_lock);
113 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
114 unsigned long start, unsigned long end, unsigned int newflags)
116 struct mm_struct * mm = vma->vm_mm;
117 unsigned long charged = 0, old_end = vma->vm_end;
119 unsigned int oldflags;
123 if (newflags == vma->vm_flags) {
129 * If we make a private mapping writable we increase our commit;
130 * but (without finer accounting) cannot reduce our commit if we
131 * make it unwritable again.
133 * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
134 * a MAP_NORESERVE private mapping to writable will now reserve.
136 if (newflags & VM_WRITE) {
137 if (!(vma->vm_flags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
138 charged = (end - start) >> PAGE_SHIFT;
139 if (security_vm_enough_memory(charged))
141 newflags |= VM_ACCOUNT;
145 newprot = protection_map[newflags & 0xf];
148 * First try to merge with previous and/or next vma.
150 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
151 *pprev = vma_merge(mm, *pprev, start, end, newflags,
152 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
158 if (start != vma->vm_start) {
159 error = split_vma(mm, vma, start, 1);
164 * Unless it returns an error, this function always sets *pprev to
165 * the first vma for which vma->vm_end >= end.
169 if (end != vma->vm_end) {
170 error = split_vma(mm, vma, end, 0);
177 * vm_flags and vm_page_prot are protected by the mmap_sem
178 * held in write mode.
180 vm_stat_unaccount(vma);
181 oldflags = vma->vm_flags;
182 vma->vm_flags = newflags;
183 vma->vm_page_prot = newprot;
184 if (oldflags & VM_EXEC)
185 arch_remove_exec_range(current->mm, old_end);
186 change_protection(vma, start, end, newprot);
187 vm_stat_account(vma);
191 vm_unacct_memory(charged);
196 do_mprotect(struct mm_struct *mm, unsigned long start, size_t len,
199 unsigned long vm_flags, nstart, end, tmp;
200 struct vm_area_struct *vma, *prev;
202 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
203 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
204 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
207 if (start & ~PAGE_MASK)
209 len = PAGE_ALIGN(len);
213 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
218 * Does the application expect PROT_READ to imply PROT_EXEC:
220 if (unlikely((prot & PROT_READ) &&
221 (current->personality & READ_IMPLIES_EXEC)))
224 vm_flags = calc_vm_prot_bits(prot);
226 down_write(&mm->mmap_sem);
228 vma = find_vma_prev(mm, start, &prev);
232 if (unlikely(grows & PROT_GROWSDOWN)) {
233 if (vma->vm_start >= end)
235 start = vma->vm_start;
237 if (!(vma->vm_flags & VM_GROWSDOWN))
241 if (vma->vm_start > start)
243 if (unlikely(grows & PROT_GROWSUP)) {
246 if (!(vma->vm_flags & VM_GROWSUP))
250 if (start > vma->vm_start)
253 for (nstart = start ; ; ) {
254 unsigned int newflags;
256 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
258 if (is_vm_hugetlb_page(vma)) {
263 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
265 if ((newflags & ~(newflags >> 4)) & 0xf) {
270 error = security_file_mprotect(vma, prot);
277 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
282 if (nstart < prev->vm_end)
283 nstart = prev->vm_end;
288 if (!vma || vma->vm_start != nstart) {
294 up_write(&mm->mmap_sem);
298 asmlinkage long sys_mprotect(unsigned long start, size_t len, unsigned long prot)
300 return(do_mprotect(current->mm, start, len, prot));