[PATCH] uml: fix fault handler on write
[linux-2.6] / arch / um / kernel / trap_kern.c
1 /* 
2  * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/kernel.h"
7 #include "asm/errno.h"
8 #include "linux/sched.h"
9 #include "linux/mm.h"
10 #include "linux/spinlock.h"
11 #include "linux/config.h"
12 #include "linux/init.h"
13 #include "linux/ptrace.h"
14 #include "asm/semaphore.h"
15 #include "asm/pgtable.h"
16 #include "asm/pgalloc.h"
17 #include "asm/tlbflush.h"
18 #include "asm/a.out.h"
19 #include "asm/current.h"
20 #include "asm/irq.h"
21 #include "user_util.h"
22 #include "kern_util.h"
23 #include "kern.h"
24 #include "chan_kern.h"
25 #include "mconsole_kern.h"
26 #include "mem.h"
27 #include "mem_kern.h"
28
29 /* Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by segv(). */
30 int handle_page_fault(unsigned long address, unsigned long ip, 
31                       int is_write, int is_user, int *code_out)
32 {
33         struct mm_struct *mm = current->mm;
34         struct vm_area_struct *vma;
35         pgd_t *pgd;
36         pud_t *pud;
37         pmd_t *pmd;
38         pte_t *pte;
39         int err = -EFAULT;
40
41         *code_out = SEGV_MAPERR;
42         down_read(&mm->mmap_sem);
43         vma = find_vma(mm, address);
44         if(!vma) 
45                 goto out;
46         else if(vma->vm_start <= address) 
47                 goto good_area;
48         else if(!(vma->vm_flags & VM_GROWSDOWN)) 
49                 goto out;
50         else if(is_user && !ARCH_IS_STACKGROW(address))
51                 goto out;
52         else if(expand_stack(vma, address)) 
53                 goto out;
54
55 good_area:
56         *code_out = SEGV_ACCERR;
57         if(is_write && !(vma->vm_flags & VM_WRITE)) 
58                 goto out;
59
60         /* Don't require VM_READ|VM_EXEC for write faults! */
61         if(!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
62                 goto out;
63
64         do {
65 survive:
66                 switch (handle_mm_fault(mm, vma, address, is_write)){
67                 case VM_FAULT_MINOR:
68                         current->min_flt++;
69                         break;
70                 case VM_FAULT_MAJOR:
71                         current->maj_flt++;
72                         break;
73                 case VM_FAULT_SIGBUS:
74                         err = -EACCES;
75                         goto out;
76                 case VM_FAULT_OOM:
77                         err = -ENOMEM;
78                         goto out_of_memory;
79                 default:
80                         BUG();
81                 }
82                 pgd = pgd_offset(mm, address);
83                 pud = pud_offset(pgd, address);
84                 pmd = pmd_offset(pud, address);
85                 pte = pte_offset_kernel(pmd, address);
86         } while(!pte_present(*pte));
87         err = 0;
88         *pte = pte_mkyoung(*pte);
89         if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
90         flush_tlb_page(vma, address);
91 out:
92         up_read(&mm->mmap_sem);
93         return(err);
94
95 /*
96  * We ran out of memory, or some other thing happened to us that made
97  * us unable to handle the page fault gracefully.
98  */
99 out_of_memory:
100         if (current->pid == 1) {
101                 up_read(&mm->mmap_sem);
102                 yield();
103                 down_read(&mm->mmap_sem);
104                 goto survive;
105         }
106         goto out;
107 }
108
109 /*
110  * We give a *copy* of the faultinfo in the regs to segv.
111  * This must be done, since nesting SEGVs could overwrite
112  * the info in the regs. A pointer to the info then would
113  * give us bad data!
114  */
115 unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
116 {
117         struct siginfo si;
118         void *catcher;
119         int err;
120         int is_write = FAULT_WRITE(fi);
121         unsigned long address = FAULT_ADDRESS(fi);
122
123         if(!is_user && (address >= start_vm) && (address < end_vm)){
124                 flush_tlb_kernel_vm();
125                 return(0);
126         }
127         else if(current->mm == NULL)
128                 panic("Segfault with no mm");
129         err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
130
131         catcher = current->thread.fault_catcher;
132         if(!err)
133                 return(0);
134         else if(catcher != NULL){
135                 current->thread.fault_addr = (void *) address;
136                 do_longjmp(catcher, 1);
137         } 
138         else if(current->thread.fault_addr != NULL)
139                 panic("fault_addr set but no fault catcher");
140         else if(!is_user && arch_fixup(ip, sc))
141                 return(0);
142
143         if(!is_user) 
144                 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", 
145                       address, ip);
146
147         if (err == -EACCES) {
148                 si.si_signo = SIGBUS;
149                 si.si_errno = 0;
150                 si.si_code = BUS_ADRERR;
151                 si.si_addr = (void *)address;
152                 current->thread.arch.faultinfo = fi;
153                 force_sig_info(SIGBUS, &si, current);
154         } else if (err == -ENOMEM) {
155                 printk("VM: killing process %s\n", current->comm);
156                 do_exit(SIGKILL);
157         } else {
158                 BUG_ON(err != -EFAULT);
159                 si.si_signo = SIGSEGV;
160                 si.si_addr = (void *) address;
161                 current->thread.arch.faultinfo = fi;
162                 force_sig_info(SIGSEGV, &si, current);
163         }
164         return(0);
165 }
166
167 void bad_segv(struct faultinfo fi, unsigned long ip)
168 {
169         struct siginfo si;
170
171         si.si_signo = SIGSEGV;
172         si.si_code = SEGV_ACCERR;
173         si.si_addr = (void *) FAULT_ADDRESS(fi);
174         current->thread.arch.faultinfo = fi;
175         force_sig_info(SIGSEGV, &si, current);
176 }
177
178 void relay_signal(int sig, union uml_pt_regs *regs)
179 {
180         if(arch_handle_signal(sig, regs)) return;
181         if(!UPT_IS_USER(regs))
182                 panic("Kernel mode signal %d", sig);
183         current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
184         force_sig(sig, current);
185 }
186
187 void bus_handler(int sig, union uml_pt_regs *regs)
188 {
189         if(current->thread.fault_catcher != NULL)
190                 do_longjmp(current->thread.fault_catcher, 1);
191         else relay_signal(sig, regs);
192 }
193
194 void winch(int sig, union uml_pt_regs *regs)
195 {
196         do_IRQ(WINCH_IRQ, regs);
197 }
198
199 void trap_init(void)
200 {
201 }