4 * Explicit pagetable population and nonlinear (random) mappings support.
6 * started by Ingo Molnar, Copyright (C) 2002
10 #include <linux/file.h>
11 #include <linux/mman.h>
12 #include <linux/pagemap.h>
13 #include <linux/swapops.h>
14 #include <linux/rmap-locking.h>
15 #include <asm/mmu_context.h>
16 #include <asm/cacheflush.h>
17 #include <asm/tlbflush.h>
19 static inline void zap_pte(struct mm_struct
*mm
, pte_t
*ptep
)
25 if (pte_present(pte
)) {
26 unsigned long pfn
= pte_pfn(pte
);
28 pte
= ptep_get_and_clear(ptep
);
30 struct page
*page
= pfn_to_page(pfn
);
31 if (!PageReserved(page
)) {
34 page_remove_rmap(page
, ptep
);
35 page_cache_release(page
);
40 free_swap_and_cache(pte_to_swp_entry(pte
));
46 * Install a page to a given virtual memory address, release any
47 * previously existing mapping.
49 int install_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
50 unsigned long addr
, struct page
*page
, unsigned long prot
)
56 struct pte_chain
*pte_chain
= NULL
;
58 pgd
= pgd_offset(mm
, addr
);
59 spin_lock(&mm
->page_table_lock
);
61 pmd
= pmd_alloc(mm
, pgd
, addr
);
65 pte_chain
= pte_chain_alloc(GFP_KERNEL
);
66 pte
= pte_alloc_map(mm
, pmd
, addr
);
73 flush_page_to_ram(page
);
74 flush_icache_page(vma
, page
);
75 entry
= mk_pte(page
, protection_map
[prot
]);
76 if (prot
& PROT_WRITE
)
77 entry
= pte_mkwrite(pte_mkdirty(entry
));
79 pte_chain
= page_add_rmap(page
, pte
, pte_chain
);
81 flush_tlb_page(vma
, addr
);
83 spin_unlock(&mm
->page_table_lock
);
84 pte_chain_free(pte_chain
);
88 spin_unlock(&mm
->page_table_lock
);
89 pte_chain_free(pte_chain
);
94 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
95 * file within an existing vma.
96 * @start: start of the remapped virtual memory range
97 * @size: size of the remapped virtual memory range
98 * @prot: new protection bits of the range
99 * @pgoff: to be mapped page of the backing store file
100 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
102 * this syscall works purely via pagetables, so it's the most efficient
103 * way to map the same (large) file into a given virtual window. Unlike
104 * mremap()/mmap() it does not create any new vmas.
106 * The new mappings do not live across swapout, so either use MAP_LOCKED
107 * or use PROT_NONE in the original linear mapping and add a special
108 * SIGBUS pagefault handler to reinstall zapped mappings.
110 int sys_remap_file_pages(unsigned long start
, unsigned long size
,
111 unsigned long prot
, unsigned long pgoff
, unsigned long flags
)
113 struct mm_struct
*mm
= current
->mm
;
114 unsigned long end
= start
+ size
;
115 struct vm_area_struct
*vma
;
119 * Sanitize the syscall parameters:
121 start
= PAGE_ALIGN(start
);
122 size
= PAGE_ALIGN(size
);
125 down_read(&mm
->mmap_sem
);
127 vma
= find_vma(mm
, start
);
129 * Make sure the vma is shared, that it supports prefaulting,
130 * and that the remapped range is valid and fully within
131 * the single existing vma:
133 if (vma
&& (vma
->vm_flags
& VM_SHARED
) &&
134 vma
->vm_ops
&& vma
->vm_ops
->populate
&&
135 end
> start
&& start
>= vma
->vm_start
&&
136 end
<= vma
->vm_end
) {
138 * Change the default protection to PROT_NONE:
140 if (pgprot_val(vma
->vm_page_prot
) != pgprot_val(__S000
))
141 vma
->vm_page_prot
= __S000
;
142 err
= vma
->vm_ops
->populate(vma
, start
, size
, prot
,
143 pgoff
, flags
& MAP_NONBLOCK
);
146 up_read(&mm
->mmap_sem
);