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>
21 #include <linux/syscalls.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/pgalloc.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
30 change_pte_range(pmd_t *pmd, unsigned long address,
31 unsigned long size, pgprot_t newprot)
43 pte = pte_offset_map(pmd, address);
49 if (pte_present(*pte)) {
52 /* Avoid an SMP race with hardware updated dirty/clean
53 * bits by wiping the pte and then setting the new pte
56 entry = ptep_get_and_clear(pte);
57 set_pte(pte, pte_modify(entry, newprot));
61 } while (address && (address < end));
66 change_pmd_range(pgd_t *pgd, unsigned long address,
67 unsigned long size, pgprot_t newprot)
79 pmd = pmd_offset(pgd, address);
80 address &= ~PGDIR_MASK;
85 change_pte_range(pmd, address, end - address, newprot);
86 address = (address + PMD_SIZE) & PMD_MASK;
88 } while (address && (address < end));
92 change_protection(struct vm_area_struct *vma, unsigned long start,
93 unsigned long end, pgprot_t newprot)
96 unsigned long beg = start;
98 dir = pgd_offset(current->mm, start);
99 flush_cache_range(vma, beg, end);
102 spin_lock(¤t->mm->page_table_lock);
104 change_pmd_range(dir, start, end - start, newprot);
105 start = (start + PGDIR_SIZE) & PGDIR_MASK;
107 } while (start && (start < end));
108 flush_tlb_range(vma, beg, end);
109 spin_unlock(¤t->mm->page_table_lock);
114 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
115 unsigned long start, unsigned long end, unsigned long newflags)
117 struct mm_struct * mm = vma->vm_mm;
118 unsigned long oldflags = vma->vm_flags;
119 long nrpages = (end - start) >> PAGE_SHIFT;
120 unsigned long charged = 0, old_end = vma->vm_end;
125 if (newflags == oldflags) {
131 * If we make a private mapping writable we increase our commit;
132 * but (without finer accounting) cannot reduce our commit if we
133 * make it unwritable again.
135 * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
136 * a MAP_NORESERVE private mapping to writable will now reserve.
138 if (newflags & VM_WRITE) {
139 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
141 if (security_vm_enough_memory(charged))
143 newflags |= VM_ACCOUNT;
147 newprot = protection_map[newflags & 0xf];
150 * First try to merge with previous and/or next vma.
152 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
153 *pprev = vma_merge(mm, *pprev, start, end, newflags,
154 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
160 if (start != vma->vm_start) {
161 error = split_vma(mm, vma, start, 1);
166 * Unless it returns an error, this function always sets *pprev to
167 * the first vma for which vma->vm_end >= end.
171 if (end != vma->vm_end) {
172 error = split_vma(mm, vma, end, 0);
179 * vm_flags and vm_page_prot are protected by the mmap_sem
180 * held in write mode.
182 oldflags = vma->vm_flags;
183 vma->vm_flags = newflags;
184 vma->vm_page_prot = newprot;
185 if (oldflags & VM_EXEC)
186 arch_remove_exec_range(current->mm, old_end);
187 change_protection(vma, start, end, newprot);
188 __vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
189 __vm_stat_account(mm, newflags, vma->vm_file, nrpages);
193 vm_unacct_memory(charged);
198 do_mprotect(struct mm_struct *mm, unsigned long start, size_t len,
201 unsigned long vm_flags, nstart, end, tmp;
202 struct vm_area_struct *vma, *prev;
204 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
205 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
206 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
209 if (start & ~PAGE_MASK)
211 len = PAGE_ALIGN(len);
215 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
220 * Does the application expect PROT_READ to imply PROT_EXEC:
222 if (unlikely((prot & PROT_READ) &&
223 (current->personality & READ_IMPLIES_EXEC)))
226 vm_flags = calc_vm_prot_bits(prot);
228 down_write(&mm->mmap_sem);
230 vma = find_vma_prev(mm, start, &prev);
234 if (unlikely(grows & PROT_GROWSDOWN)) {
235 if (vma->vm_start >= end)
237 start = vma->vm_start;
239 if (!(vma->vm_flags & VM_GROWSDOWN))
243 if (vma->vm_start > start)
245 if (unlikely(grows & PROT_GROWSUP)) {
248 if (!(vma->vm_flags & VM_GROWSUP))
252 if (start > vma->vm_start)
255 for (nstart = start ; ; ) {
256 unsigned long newflags;
258 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
260 if (is_vm_hugetlb_page(vma)) {
265 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
267 if ((newflags & ~(newflags >> 4)) & 0xf) {
272 error = security_file_mprotect(vma, prot);
279 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
284 if (nstart < prev->vm_end)
285 nstart = prev->vm_end;
290 if (!vma || vma->vm_start != nstart) {
296 up_write(&mm->mmap_sem);
300 asmlinkage long sys_mprotect(unsigned long start, size_t len, unsigned long prot)
302 return(do_mprotect(current->mm, start, len, prot));