2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
8 #include "asm/pgalloc.h"
9 #include "asm/tlbflush.h"
10 #include "choose-mode.h"
11 #include "mode_kern.h"
12 #include "user_util.h"
18 static int add_mmap(unsigned long virt, unsigned long phys, unsigned long len,
19 int r, int w, int x, struct host_vm_op *ops, int index,
20 int last_filled, union mm_context *mmu, void **flush,
21 void *(*do_ops)(union mm_context *, struct host_vm_op *,
25 struct host_vm_op *last;
28 fd = phys_mapping(phys, &offset);
31 if((last->type == MMAP) &&
32 (last->u.mmap.addr + last->u.mmap.len == virt) &&
33 (last->u.mmap.r == r) && (last->u.mmap.w == w) &&
34 (last->u.mmap.x == x) && (last->u.mmap.fd == fd) &&
35 (last->u.mmap.offset + last->u.mmap.len == offset)){
36 last->u.mmap.len += len;
41 if(index == last_filled){
42 *flush = (*do_ops)(mmu, ops, last_filled, 0, *flush);
46 ops[++index] = ((struct host_vm_op) { .type = MMAP,
59 static int add_munmap(unsigned long addr, unsigned long len,
60 struct host_vm_op *ops, int index, int last_filled,
61 union mm_context *mmu, void **flush,
62 void *(*do_ops)(union mm_context *, struct host_vm_op *,
65 struct host_vm_op *last;
69 if((last->type == MUNMAP) &&
70 (last->u.munmap.addr + last->u.mmap.len == addr)){
71 last->u.munmap.len += len;
76 if(index == last_filled){
77 *flush = (*do_ops)(mmu, ops, last_filled, 0, *flush);
81 ops[++index] = ((struct host_vm_op) { .type = MUNMAP,
88 static int add_mprotect(unsigned long addr, unsigned long len, int r, int w,
89 int x, struct host_vm_op *ops, int index,
90 int last_filled, union mm_context *mmu, void **flush,
91 void *(*do_ops)(union mm_context *,
92 struct host_vm_op *, int, int, void *))
94 struct host_vm_op *last;
98 if((last->type == MPROTECT) &&
99 (last->u.mprotect.addr + last->u.mprotect.len == addr) &&
100 (last->u.mprotect.r == r) && (last->u.mprotect.w == w) &&
101 (last->u.mprotect.x == x)){
102 last->u.mprotect.len += len;
107 if(index == last_filled){
108 *flush = (*do_ops)(mmu, ops, last_filled, 0, *flush);
112 ops[++index] = ((struct host_vm_op) { .type = MPROTECT,
122 #define ADD_ROUND(n, inc) (((n) + (inc)) & ~((inc) - 1))
124 void fix_range_common(struct mm_struct *mm, unsigned long start_addr,
125 unsigned long end_addr, int force,
126 void *(*do_ops)(union mm_context *, struct host_vm_op *,
133 union mm_context *mmu = &mm->context;
134 unsigned long addr, end;
136 struct host_vm_op ops[1];
138 int op_index = -1, last_op = sizeof(ops) / sizeof(ops[0]) - 1;
140 if(mm == NULL) return;
143 for(addr = start_addr; addr < end_addr;){
144 npgd = pgd_offset(mm, addr);
145 if(!pgd_present(*npgd)){
146 end = ADD_ROUND(addr, PGDIR_SIZE);
149 if(force || pgd_newpage(*npgd)){
150 op_index = add_munmap(addr, end - addr, ops,
151 op_index, last_op, mmu,
153 pgd_mkuptodate(*npgd);
159 npud = pud_offset(npgd, addr);
160 if(!pud_present(*npud)){
161 end = ADD_ROUND(addr, PUD_SIZE);
164 if(force || pud_newpage(*npud)){
165 op_index = add_munmap(addr, end - addr, ops,
166 op_index, last_op, mmu,
168 pud_mkuptodate(*npud);
174 npmd = pmd_offset(npud, addr);
175 if(!pmd_present(*npmd)){
176 end = ADD_ROUND(addr, PMD_SIZE);
179 if(force || pmd_newpage(*npmd)){
180 op_index = add_munmap(addr, end - addr, ops,
181 op_index, last_op, mmu,
183 pmd_mkuptodate(*npmd);
189 npte = pte_offset_kernel(npmd, addr);
191 w = pte_write(*npte);
193 if(!pte_dirty(*npte))
195 if(!pte_young(*npte)){
199 if(force || pte_newpage(*npte)){
200 if(pte_present(*npte))
201 op_index = add_mmap(addr,
202 pte_val(*npte) & PAGE_MASK,
203 PAGE_SIZE, r, w, x, ops,
204 op_index, last_op, mmu,
206 else op_index = add_munmap(addr, PAGE_SIZE, ops,
207 op_index, last_op, mmu,
210 else if(pte_newprot(*npte))
211 op_index = add_mprotect(addr, PAGE_SIZE, r, w, x, ops,
212 op_index, last_op, mmu,
215 *npte = pte_mkuptodate(*npte);
218 flush = (*do_ops)(mmu, ops, op_index, 1, flush);
221 int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
223 struct mm_struct *mm;
228 unsigned long addr, last;
229 int updated = 0, err;
232 for(addr = start; addr < end;){
233 pgd = pgd_offset(mm, addr);
234 if(!pgd_present(*pgd)){
235 last = ADD_ROUND(addr, PGDIR_SIZE);
238 if(pgd_newpage(*pgd)){
240 err = os_unmap_memory((void *) addr,
243 panic("munmap failed, errno = %d\n",
250 pud = pud_offset(pgd, addr);
251 if(!pud_present(*pud)){
252 last = ADD_ROUND(addr, PUD_SIZE);
255 if(pud_newpage(*pud)){
257 err = os_unmap_memory((void *) addr,
260 panic("munmap failed, errno = %d\n",
267 pmd = pmd_offset(pud, addr);
268 if(!pmd_present(*pmd)){
269 last = ADD_ROUND(addr, PMD_SIZE);
272 if(pmd_newpage(*pmd)){
274 err = os_unmap_memory((void *) addr,
277 panic("munmap failed, errno = %d\n",
284 pte = pte_offset_kernel(pmd, addr);
285 if(!pte_present(*pte) || pte_newpage(*pte)){
287 err = os_unmap_memory((void *) addr,
290 panic("munmap failed, errno = %d\n",
292 if(pte_present(*pte))
294 pte_val(*pte) & PAGE_MASK,
297 else if(pte_newprot(*pte)){
299 protect_memory(addr, PAGE_SIZE, 1, 1, 1, 1);
306 pgd_t *pgd_offset_proc(struct mm_struct *mm, unsigned long address)
308 return(pgd_offset(mm, address));
311 pud_t *pud_offset_proc(pgd_t *pgd, unsigned long address)
313 return(pud_offset(pgd, address));
316 pmd_t *pmd_offset_proc(pud_t *pud, unsigned long address)
318 return(pmd_offset(pud, address));
321 pte_t *pte_offset_proc(pmd_t *pmd, unsigned long address)
323 return(pte_offset_kernel(pmd, address));
326 pte_t *addr_pte(struct task_struct *task, unsigned long addr)
328 pgd_t *pgd = pgd_offset(task->mm, addr);
329 pud_t *pud = pud_offset(pgd, addr);
330 pmd_t *pmd = pmd_offset(pud, addr);
332 return(pte_offset_map(pmd, addr));
335 void flush_tlb_page(struct vm_area_struct *vma, unsigned long address)
337 address &= PAGE_MASK;
338 flush_tlb_range(vma, address, address + PAGE_SIZE);
341 void flush_tlb_all(void)
343 flush_tlb_mm(current->mm);
346 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
348 CHOOSE_MODE_PROC(flush_tlb_kernel_range_tt,
349 flush_tlb_kernel_range_common, start, end);
352 void flush_tlb_kernel_vm(void)
354 CHOOSE_MODE(flush_tlb_kernel_vm_tt(),
355 flush_tlb_kernel_range_common(start_vm, end_vm));
358 void __flush_tlb_one(unsigned long addr)
360 CHOOSE_MODE_PROC(__flush_tlb_one_tt, __flush_tlb_one_skas, addr);
363 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
366 CHOOSE_MODE_PROC(flush_tlb_range_tt, flush_tlb_range_skas, vma, start,
370 void flush_tlb_mm(struct mm_struct *mm)
372 CHOOSE_MODE_PROC(flush_tlb_mm_tt, flush_tlb_mm_skas, mm);
375 void force_flush_all(void)
377 CHOOSE_MODE(force_flush_all_tt(), force_flush_all_skas());