Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-for-linus-2.6
[linux-2.6] / arch / um / kernel / tt / tlb.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Copyright 2003 PathScale, Inc.
4  * Licensed under the GPL
5  */
6
7 #include "linux/stddef.h"
8 #include "linux/kernel.h"
9 #include "linux/sched.h"
10 #include "linux/mm.h"
11 #include "asm/page.h"
12 #include "asm/pgtable.h"
13 #include "asm/uaccess.h"
14 #include "asm/tlbflush.h"
15 #include "user_util.h"
16 #include "mem_user.h"
17 #include "os.h"
18 #include "tlb.h"
19
20 static int do_ops(union mm_context *mmu, struct host_vm_op *ops, int last,
21                     int finished, void **flush)
22 {
23         struct host_vm_op *op;
24         int i, ret=0;
25
26         for(i = 0; i <= last && !ret; i++){
27                 op = &ops[i];
28                 switch(op->type){
29                 case MMAP:
30                         ret = os_map_memory((void *) op->u.mmap.addr,
31                                             op->u.mmap.fd, op->u.mmap.offset,
32                                             op->u.mmap.len, op->u.mmap.r,
33                                             op->u.mmap.w, op->u.mmap.x);
34                         break;
35                 case MUNMAP:
36                         ret = os_unmap_memory((void *) op->u.munmap.addr,
37                                               op->u.munmap.len);
38                         break;
39                 case MPROTECT:
40                         ret = protect_memory(op->u.mprotect.addr,
41                                              op->u.munmap.len,
42                                              op->u.mprotect.r,
43                                              op->u.mprotect.w,
44                                              op->u.mprotect.x, 1);
45                         protect_memory(op->u.mprotect.addr, op->u.munmap.len,
46                                        op->u.mprotect.r, op->u.mprotect.w,
47                                        op->u.mprotect.x, 1);
48                         break;
49                 default:
50                         printk("Unknown op type %d in do_ops\n", op->type);
51                         break;
52                 }
53         }
54
55         return ret;
56 }
57
58 static void fix_range(struct mm_struct *mm, unsigned long start_addr, 
59                       unsigned long end_addr, int force)
60 {
61         if((current->thread.mode.tt.extern_pid != -1) &&
62            (current->thread.mode.tt.extern_pid != os_getpid()))
63                 panic("fix_range fixing wrong address space, current = 0x%p",
64                       current);
65
66         fix_range_common(mm, start_addr, end_addr, force, do_ops);
67 }
68
69 atomic_t vmchange_seq = ATOMIC_INIT(1);
70
71 void flush_tlb_kernel_range_tt(unsigned long start, unsigned long end)
72 {
73         if(flush_tlb_kernel_range_common(start, end))
74                 atomic_inc(&vmchange_seq);
75 }
76
77 static void protect_vm_page(unsigned long addr, int w, int must_succeed)
78 {
79         int err;
80
81         err = protect_memory(addr, PAGE_SIZE, 1, w, 1, must_succeed);
82         if(err == 0) return;
83         else if((err == -EFAULT) || (err == -ENOMEM)){
84                 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
85                 protect_vm_page(addr, w, 1);
86         }
87         else panic("protect_vm_page : protect failed, errno = %d\n", err);
88 }
89
90 void mprotect_kernel_vm(int w)
91 {
92         struct mm_struct *mm;
93         pgd_t *pgd;
94         pud_t *pud;
95         pmd_t *pmd;
96         pte_t *pte;
97         unsigned long addr;
98         
99         mm = &init_mm;
100         for(addr = start_vm; addr < end_vm;){
101                 pgd = pgd_offset(mm, addr);
102                 pud = pud_offset(pgd, addr);
103                 pmd = pmd_offset(pud, addr);
104                 if(pmd_present(*pmd)){
105                         pte = pte_offset_kernel(pmd, addr);
106                         if(pte_present(*pte)) protect_vm_page(addr, w, 0);
107                         addr += PAGE_SIZE;
108                 }
109                 else addr += PMD_SIZE;
110         }
111 }
112
113 void flush_tlb_kernel_vm_tt(void)
114 {
115         flush_tlb_kernel_range(start_vm, end_vm);
116 }
117
118 void __flush_tlb_one_tt(unsigned long addr)
119 {
120         flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
121 }
122   
123 void flush_tlb_range_tt(struct vm_area_struct *vma, unsigned long start, 
124                      unsigned long end)
125 {
126         if(vma->vm_mm != current->mm) return;
127
128         /* Assumes that the range start ... end is entirely within
129          * either process memory or kernel vm
130          */
131         if((start >= start_vm) && (start < end_vm)){
132                 if(flush_tlb_kernel_range_common(start, end))
133                         atomic_inc(&vmchange_seq);
134         }
135         else fix_range(vma->vm_mm, start, end, 0);
136 }
137
138 void flush_tlb_mm_tt(struct mm_struct *mm)
139 {
140         unsigned long seq;
141
142         if(mm != current->mm) return;
143
144         fix_range(mm, 0, STACK_TOP, 0);
145
146         seq = atomic_read(&vmchange_seq);
147         if(current->thread.mode.tt.vm_seq == seq)
148                 return;
149         current->thread.mode.tt.vm_seq = seq;
150         flush_tlb_kernel_range_common(start_vm, end_vm);
151 }
152
153 void force_flush_all_tt(void)
154 {
155         fix_range(current->mm, 0, STACK_TOP, 1);
156         flush_tlb_kernel_range_common(start_vm, end_vm);
157 }