4 * Explicit pagetable population and nonlinear (random) mappings support.
6 * started by Ingo Molnar, Copyright (C) 2002, 2003
10 #include <linux/swap.h>
11 #include <linux/file.h>
12 #include <linux/mman.h>
13 #include <linux/pagemap.h>
14 #include <linux/swapops.h>
15 #include <linux/rmap.h>
16 #include <linux/module.h>
18 #include <asm/mmu_context.h>
19 #include <asm/cacheflush.h>
20 #include <asm/tlbflush.h>
22 static inline void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
23 unsigned long addr, pte_t *ptep)
29 if (pte_present(pte)) {
30 unsigned long pfn = pte_pfn(pte);
32 flush_cache_page(vma, addr);
33 pte = ptep_clear_flush(vma, addr, ptep);
35 struct page *page = pfn_to_page(pfn);
36 if (!PageReserved(page)) {
39 page_remove_rmap(page, ptep);
40 page_cache_release(page);
47 free_swap_and_cache(pte_to_swp_entry(pte));
53 * Install a page to a given virtual memory address, release any
54 * previously existing mapping.
56 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
57 unsigned long addr, struct page *page, pgprot_t prot)
64 struct pte_chain *pte_chain;
66 pte_chain = pte_chain_alloc(GFP_KERNEL);
69 pgd = pgd_offset(mm, addr);
70 spin_lock(&mm->page_table_lock);
72 if (!vx_rsspages_avail(mm, 1))
75 pmd = pmd_alloc(mm, pgd, addr);
79 pte = pte_alloc_map(mm, pmd, addr);
83 zap_pte(mm, vma, addr, pte);
87 flush_icache_page(vma, page);
88 set_pte(pte, mk_pte(page, prot));
89 pte_chain = page_add_rmap(page, pte, pte_chain);
92 update_mmu_cache(vma, addr, pte_val);
93 spin_unlock(&mm->page_table_lock);
94 pte_chain_free(pte_chain);
98 spin_unlock(&mm->page_table_lock);
99 pte_chain_free(pte_chain);
103 EXPORT_SYMBOL(install_page);
107 * Install a file pte to a given virtual memory address, release any
108 * previously existing mapping.
110 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
111 unsigned long addr, unsigned long pgoff, pgprot_t prot)
119 pgd = pgd_offset(mm, addr);
120 spin_lock(&mm->page_table_lock);
122 pmd = pmd_alloc(mm, pgd, addr);
126 pte = pte_alloc_map(mm, pmd, addr);
130 zap_pte(mm, vma, addr, pte);
132 set_pte(pte, pgoff_to_pte(pgoff));
135 update_mmu_cache(vma, addr, pte_val);
136 spin_unlock(&mm->page_table_lock);
140 spin_unlock(&mm->page_table_lock);
146 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
147 * file within an existing vma.
148 * @start: start of the remapped virtual memory range
149 * @size: size of the remapped virtual memory range
150 * @prot: new protection bits of the range
151 * @pgoff: to be mapped page of the backing store file
152 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
154 * this syscall works purely via pagetables, so it's the most efficient
155 * way to map the same (large) file into a given virtual window. Unlike
156 * mmap()/mremap() it does not create any new vmas. The new mappings are
157 * also safe across swapout.
159 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
160 * protection is used. Arbitrary protections might be implemented in the
163 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
164 unsigned long __prot, unsigned long pgoff, unsigned long flags)
166 struct mm_struct *mm = current->mm;
167 unsigned long end = start + size;
168 struct vm_area_struct *vma;
174 * Sanitize the syscall parameters:
176 start = start & PAGE_MASK;
177 size = size & PAGE_MASK;
179 /* Does the address range wrap, or is the span zero-sized? */
180 if (start + size <= start)
183 /* Can we represent this offset inside this architecture's pte's? */
184 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
185 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
189 /* We need down_write() to change vma->vm_flags. */
190 down_write(&mm->mmap_sem);
191 vma = find_vma(mm, start);
194 * Make sure the vma is shared, that it supports prefaulting,
195 * and that the remapped range is valid and fully within
196 * the single existing vma:
198 if (vma && (vma->vm_flags & VM_SHARED) &&
199 vma->vm_ops && vma->vm_ops->populate &&
200 end > start && start >= vma->vm_start &&
201 end <= vma->vm_end) {
203 /* Must set VM_NONLINEAR before any pages are populated. */
204 if (pgoff != ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff)
205 vma->vm_flags |= VM_NONLINEAR;
207 /* ->populate can take a long time, so downgrade the lock. */
208 downgrade_write(&mm->mmap_sem);
209 err = vma->vm_ops->populate(vma, start, size,
211 pgoff, flags & MAP_NONBLOCK);
214 * We can't clear VM_NONLINEAR because we'd have to do
215 * it after ->populate completes, and that would prevent
216 * downgrading the lock. (Locks can't be upgraded).
218 up_read(&mm->mmap_sem);
220 up_write(&mm->mmap_sem);