um/kernel/trap.c: port OOM changes to handle_page_fault()
[deliverable/linux.git] / arch / um / kernel / trap.c
CommitLineData
ea2ba7dc 1/*
4c9e1385 2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
4c9e1385
JD
6#include <linux/mm.h>
7#include <linux/sched.h>
8#include <linux/hardirq.h>
73395a00 9#include <linux/module.h>
4c9e1385
JD
10#include <asm/current.h>
11#include <asm/pgtable.h>
12#include <asm/tlbflush.h>
eb830759 13#include "arch.h"
4c9e1385
JD
14#include "as-layout.h"
15#include "kern_util.h"
ea2ba7dc 16#include "os.h"
d83ecf08 17#include "skas.h"
1da177e4 18
4c9e1385
JD
19/*
20 * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by
21 * segv().
22 */
1d3468a6 23int handle_page_fault(unsigned long address, unsigned long ip,
1da177e4
LT
24 int is_write, int is_user, int *code_out)
25{
26 struct mm_struct *mm = current->mm;
27 struct vm_area_struct *vma;
28 pgd_t *pgd;
29 pud_t *pud;
30 pmd_t *pmd;
31 pte_t *pte;
1da177e4 32 int err = -EFAULT;
1cefe28f
KC
33 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
34 (is_write ? FAULT_FLAG_WRITE : 0);
1da177e4
LT
35
36 *code_out = SEGV_MAPERR;
fea03cb4 37
4c9e1385
JD
38 /*
39 * If the fault was during atomic operation, don't take the fault, just
40 * fail.
41 */
fea03cb4
PBG
42 if (in_atomic())
43 goto out_nosemaphore;
44
1cefe28f 45retry:
1da177e4
LT
46 down_read(&mm->mmap_sem);
47 vma = find_vma(mm, address);
4c9e1385 48 if (!vma)
1da177e4 49 goto out;
4c9e1385 50 else if (vma->vm_start <= address)
1da177e4 51 goto good_area;
4c9e1385 52 else if (!(vma->vm_flags & VM_GROWSDOWN))
1da177e4 53 goto out;
4c9e1385 54 else if (is_user && !ARCH_IS_STACKGROW(address))
1da177e4 55 goto out;
4c9e1385 56 else if (expand_stack(vma, address))
1da177e4
LT
57 goto out;
58
3b52166c 59good_area:
1da177e4 60 *code_out = SEGV_ACCERR;
4c9e1385 61 if (is_write && !(vma->vm_flags & VM_WRITE))
1da177e4 62 goto out;
13479d52 63
d129f312 64 /* Don't require VM_READ|VM_EXEC for write faults! */
4c9e1385 65 if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
5d86456d 66 goto out;
13479d52 67
1da177e4 68 do {
83c54070 69 int fault;
1c0fe6e3 70
1cefe28f
KC
71 fault = handle_mm_fault(mm, vma, address, flags);
72
73 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
74 goto out_nosemaphore;
75
83c54070
NP
76 if (unlikely(fault & VM_FAULT_ERROR)) {
77 if (fault & VM_FAULT_OOM) {
83c54070
NP
78 goto out_of_memory;
79 } else if (fault & VM_FAULT_SIGBUS) {
80 err = -EACCES;
81 goto out;
82 }
1da177e4
LT
83 BUG();
84 }
1cefe28f
KC
85 if (flags & FAULT_FLAG_ALLOW_RETRY) {
86 if (fault & VM_FAULT_MAJOR)
87 current->maj_flt++;
88 else
89 current->min_flt++;
90 if (fault & VM_FAULT_RETRY) {
91 flags &= ~FAULT_FLAG_ALLOW_RETRY;
92
93 goto retry;
94 }
95 }
83c54070 96
3b52166c
PBG
97 pgd = pgd_offset(mm, address);
98 pud = pud_offset(pgd, address);
99 pmd = pmd_offset(pud, address);
100 pte = pte_offset_kernel(pmd, address);
4c9e1385 101 } while (!pte_present(*pte));
1da177e4 102 err = 0;
4c9e1385
JD
103 /*
104 * The below warning was added in place of
cbc24afa
PBG
105 * pte_mkyoung(); if (is_write) pte_mkdirty();
106 * If it's triggered, we'd see normally a hang here (a clean pte is
107 * marked read-only to emulate the dirty bit).
108 * However, the generic code can mark a PTE writable but clean on a
109 * concurrent read fault, triggering this harmlessly. So comment it out.
110 */
111#if 0
16b03678 112 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
cbc24afa 113#endif
3b52166c
PBG
114 flush_tlb_page(vma, address);
115out:
1da177e4 116 up_read(&mm->mmap_sem);
fea03cb4 117out_nosemaphore:
4c9e1385 118 return err;
1da177e4 119
1da177e4 120out_of_memory:
1c0fe6e3
NP
121 /*
122 * We ran out of memory, call the OOM killer, and return the userspace
123 * (which will retry the fault, or kill us if we got oom-killed).
124 */
125 up_read(&mm->mmap_sem);
126 pagefault_out_of_memory();
127 return 0;
1da177e4 128}
73395a00 129EXPORT_SYMBOL(handle_page_fault);
1da177e4 130
3ef6130a
RW
131static void show_segv_info(struct uml_pt_regs *regs)
132{
133 struct task_struct *tsk = current;
134 struct faultinfo *fi = UPT_FAULTINFO(regs);
135
136 if (!unhandled_signal(tsk, SIGSEGV))
137 return;
138
139 if (!printk_ratelimit())
140 return;
141
142 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
143 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
144 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
145 (void *)UPT_IP(regs), (void *)UPT_SP(regs),
146 fi->error_code);
147
148 print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
149 printk(KERN_CONT "\n");
150}
151
27aa6ef3
JD
152static void bad_segv(struct faultinfo fi, unsigned long ip)
153{
154 struct siginfo si;
155
156 si.si_signo = SIGSEGV;
157 si.si_code = SEGV_ACCERR;
158 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
159 current->thread.arch.faultinfo = fi;
160 force_sig_info(SIGSEGV, &si, current);
161}
162
3e6f2ac4
JD
163void fatal_sigsegv(void)
164{
165 force_sigsegv(SIGSEGV, current);
166 do_signal();
167 /*
168 * This is to tell gcc that we're not returning - do_signal
169 * can, in general, return, but in this case, it's not, since
170 * we just got a fatal SIGSEGV queued.
171 */
172 os_dump_core();
173}
174
edea1385 175void segv_handler(int sig, struct uml_pt_regs *regs)
c66fdd5e
GS
176{
177 struct faultinfo * fi = UPT_FAULTINFO(regs);
178
4c9e1385 179 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
3ef6130a 180 show_segv_info(regs);
c66fdd5e
GS
181 bad_segv(*fi, UPT_IP(regs));
182 return;
183 }
184 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
185}
186
c578455a
BS
187/*
188 * We give a *copy* of the faultinfo in the regs to segv.
189 * This must be done, since nesting SEGVs could overwrite
190 * the info in the regs. A pointer to the info then would
191 * give us bad data!
192 */
5d86456d 193unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
77bf4400 194 struct uml_pt_regs *regs)
1da177e4
LT
195{
196 struct siginfo si;
fab95c55 197 jmp_buf *catcher;
1da177e4 198 int err;
5d86456d
JD
199 int is_write = FAULT_WRITE(fi);
200 unsigned long address = FAULT_ADDRESS(fi);
1da177e4 201
4c9e1385 202 if (!is_user && (address >= start_vm) && (address < end_vm)) {
5d86456d
JD
203 flush_tlb_kernel_vm();
204 return 0;
205 }
4c9e1385 206 else if (current->mm == NULL) {
377fad3a 207 show_regs(container_of(regs, struct pt_regs, regs));
4c9e1385 208 panic("Segfault with no mm");
377fad3a 209 }
546fe1cb 210
be662a18 211 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
4c9e1385
JD
212 err = handle_page_fault(address, ip, is_write, is_user,
213 &si.si_code);
546fe1cb
PBG
214 else {
215 err = -EFAULT;
4c9e1385
JD
216 /*
217 * A thread accessed NULL, we get a fault, but CR2 is invalid.
218 * This code is used in __do_copy_from_user() of TT mode.
219 * XXX tt mode is gone, so maybe this isn't needed any more
220 */
546fe1cb
PBG
221 address = 0;
222 }
1da177e4
LT
223
224 catcher = current->thread.fault_catcher;
4c9e1385 225 if (!err)
5d86456d 226 return 0;
4c9e1385 227 else if (catcher != NULL) {
1da177e4 228 current->thread.fault_addr = (void *) address;
fab95c55 229 UML_LONGJMP(catcher, 1);
1d3468a6 230 }
4c9e1385 231 else if (current->thread.fault_addr != NULL)
1da177e4 232 panic("fault_addr set but no fault catcher");
4c9e1385 233 else if (!is_user && arch_fixup(ip, regs))
5d86456d 234 return 0;
1da177e4 235
4c9e1385 236 if (!is_user) {
377fad3a 237 show_regs(container_of(regs, struct pt_regs, regs));
1d3468a6 238 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
1da177e4 239 address, ip);
377fad3a 240 }
1da177e4 241
3ef6130a
RW
242 show_segv_info(regs);
243
3b52166c 244 if (err == -EACCES) {
1da177e4
LT
245 si.si_signo = SIGBUS;
246 si.si_errno = 0;
247 si.si_code = BUS_ADRERR;
4d338e1a 248 si.si_addr = (void __user *)address;
5d86456d 249 current->thread.arch.faultinfo = fi;
1da177e4 250 force_sig_info(SIGBUS, &si, current);
3b52166c
PBG
251 } else {
252 BUG_ON(err != -EFAULT);
1da177e4 253 si.si_signo = SIGSEGV;
4d338e1a 254 si.si_addr = (void __user *) address;
5d86456d 255 current->thread.arch.faultinfo = fi;
1da177e4
LT
256 force_sig_info(SIGSEGV, &si, current);
257 }
5d86456d 258 return 0;
1da177e4
LT
259}
260
77bf4400 261void relay_signal(int sig, struct uml_pt_regs *regs)
1da177e4 262{
4c9e1385
JD
263 if (!UPT_IS_USER(regs)) {
264 if (sig == SIGBUS)
265 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
266 "mount likely just ran out of space\n");
1da177e4 267 panic("Kernel mode signal %d", sig);
6edf428e
JD
268 }
269
9226b838
JD
270 arch_examine_signal(sig, regs);
271
5d86456d 272 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
1da177e4
LT
273 force_sig(sig, current);
274}
275
edea1385 276void bus_handler(int sig, struct uml_pt_regs *regs)
1da177e4 277{
4c9e1385 278 if (current->thread.fault_catcher != NULL)
fab95c55 279 UML_LONGJMP(current->thread.fault_catcher, 1);
1da177e4
LT
280 else relay_signal(sig, regs);
281}
282
edea1385 283void winch(int sig, struct uml_pt_regs *regs)
1da177e4
LT
284{
285 do_IRQ(WINCH_IRQ, regs);
286}
287
288void trap_init(void)
289{
290}
This page took 0.563541 seconds and 5 git commands to generate.