blob: 089f3987e273a2c3f9576ef923dbd8ba8648cca5 [file] [log] [blame]
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -08001/*
Jeff Dike4c9e1382007-10-16 01:26:54 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike4c9e1382007-10-16 01:26:54 -07006#include <linux/mm.h>
7#include <linux/sched.h>
8#include <linux/hardirq.h>
Al Viro73395a02011-08-18 20:14:10 +01009#include <linux/module.h>
Jeff Dike4c9e1382007-10-16 01:26:54 -070010#include <asm/current.h>
11#include <asm/pgtable.h>
12#include <asm/tlbflush.h>
Al Viro37185b32012-10-08 03:27:32 +010013#include <arch.h>
14#include <as-layout.h>
15#include <kern_util.h>
16#include <os.h>
17#include <skas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Dike4c9e1382007-10-16 01:26:54 -070019/*
20 * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by
21 * segv().
22 */
Jeff Dike1d3468a2006-07-10 04:45:13 -070023int handle_page_fault(unsigned long address, unsigned long ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int err = -EFAULT;
Kautuk Consul1cefe282012-05-31 16:26:03 -070033 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
34 (is_write ? FAULT_FLAG_WRITE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 *code_out = SEGV_MAPERR;
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070037
Jeff Dike4c9e1382007-10-16 01:26:54 -070038 /*
39 * If the fault was during atomic operation, don't take the fault, just
40 * fail.
41 */
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070042 if (in_atomic())
43 goto out_nosemaphore;
44
Kautuk Consul1cefe282012-05-31 16:26:03 -070045retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 down_read(&mm->mmap_sem);
47 vma = find_vma(mm, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070048 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070050 else if (vma->vm_start <= address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 goto good_area;
Jeff Dike4c9e1382007-10-16 01:26:54 -070052 else if (!(vma->vm_flags & VM_GROWSDOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070054 else if (is_user && !ARCH_IS_STACKGROW(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070056 else if (expand_stack(vma, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 goto out;
58
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070059good_area:
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *code_out = SEGV_ACCERR;
Jeff Dike4c9e1382007-10-16 01:26:54 -070061 if (is_write && !(vma->vm_flags & VM_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070063
Paolo 'Blaisorblade' Giarrussod129f312005-09-10 19:44:57 +020064 /* Don't require VM_READ|VM_EXEC for write faults! */
Jeff Dike4c9e1382007-10-16 01:26:54 -070065 if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
Jeff Dike5d864562007-05-06 14:51:24 -070066 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 do {
Nick Piggin83c54072007-07-19 01:47:05 -070069 int fault;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -080070
Kautuk Consul1cefe282012-05-31 16:26:03 -070071 fault = handle_mm_fault(mm, vma, address, flags);
72
73 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
74 goto out_nosemaphore;
75
Nick Piggin83c54072007-07-19 01:47:05 -070076 if (unlikely(fault & VM_FAULT_ERROR)) {
77 if (fault & VM_FAULT_OOM) {
Nick Piggin83c54072007-07-19 01:47:05 -070078 goto out_of_memory;
79 } else if (fault & VM_FAULT_SIGBUS) {
80 err = -EACCES;
81 goto out;
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 BUG();
84 }
Kautuk Consul1cefe282012-05-31 16:26:03 -070085 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;
Shaohua Li45cac652012-10-08 16:32:19 -070092 flags |= FAULT_FLAG_TRIED;
Kautuk Consul1cefe282012-05-31 16:26:03 -070093
94 goto retry;
95 }
96 }
Nick Piggin83c54072007-07-19 01:47:05 -070097
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070098 pgd = pgd_offset(mm, address);
99 pud = pud_offset(pgd, address);
100 pmd = pmd_offset(pud, address);
101 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -0700102 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700104 /*
105 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800106 * pte_mkyoung(); if (is_write) pte_mkdirty();
107 * If it's triggered, we'd see normally a hang here (a clean pte is
108 * marked read-only to emulate the dirty bit).
109 * However, the generic code can mark a PTE writable but clean on a
110 * concurrent read fault, triggering this harmlessly. So comment it out.
111 */
112#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +0200113 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800114#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700115 flush_tlb_page(vma, address);
116out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700118out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800122 /*
123 * We ran out of memory, call the OOM killer, and return the userspace
124 * (which will retry the fault, or kill us if we got oom-killed).
125 */
126 up_read(&mm->mmap_sem);
127 pagefault_out_of_memory();
128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
Al Viro73395a02011-08-18 20:14:10 +0100130EXPORT_SYMBOL(handle_page_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Richard Weinberger3ef61302011-05-24 17:13:03 -0700132static void show_segv_info(struct uml_pt_regs *regs)
133{
134 struct task_struct *tsk = current;
135 struct faultinfo *fi = UPT_FAULTINFO(regs);
136
137 if (!unhandled_signal(tsk, SIGSEGV))
138 return;
139
140 if (!printk_ratelimit())
141 return;
142
143 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
144 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
145 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
146 (void *)UPT_IP(regs), (void *)UPT_SP(regs),
147 fi->error_code);
148
149 print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
150 printk(KERN_CONT "\n");
151}
152
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800153static void bad_segv(struct faultinfo fi, unsigned long ip)
154{
155 struct siginfo si;
156
157 si.si_signo = SIGSEGV;
158 si.si_code = SEGV_ACCERR;
159 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
160 current->thread.arch.faultinfo = fi;
161 force_sig_info(SIGSEGV, &si, current);
162}
163
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800164void fatal_sigsegv(void)
165{
166 force_sigsegv(SIGSEGV, current);
167 do_signal();
168 /*
169 * This is to tell gcc that we're not returning - do_signal
170 * can, in general, return, but in this case, it's not, since
171 * we just got a fatal SIGSEGV queued.
172 */
173 os_dump_core();
174}
175
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200176void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800177{
178 struct faultinfo * fi = UPT_FAULTINFO(regs);
179
Jeff Dike4c9e1382007-10-16 01:26:54 -0700180 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700181 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800182 bad_segv(*fi, UPT_IP(regs));
183 return;
184 }
185 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
186}
187
Bodo Stroesserc5784552005-05-05 16:15:31 -0700188/*
189 * We give a *copy* of the faultinfo in the regs to segv.
190 * This must be done, since nesting SEGVs could overwrite
191 * the info in the regs. A pointer to the info then would
192 * give us bad data!
193 */
Jeff Dike5d864562007-05-06 14:51:24 -0700194unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700195 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700198 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700200 int is_write = FAULT_WRITE(fi);
201 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jeff Dike4c9e1382007-10-16 01:26:54 -0700203 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700204 flush_tlb_kernel_vm();
205 return 0;
206 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700207 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700208 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700209 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700210 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700211
Paolo 'Blaisorblade' Giarrussobe662a12005-09-30 11:58:59 -0700212 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700213 err = handle_page_fault(address, ip, is_write, is_user,
214 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700215 else {
216 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700217 /*
218 * A thread accessed NULL, we get a fault, but CR2 is invalid.
219 * This code is used in __do_copy_from_user() of TT mode.
220 * XXX tt mode is gone, so maybe this isn't needed any more
221 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700222 address = 0;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700226 if (!err)
Jeff Dike5d864562007-05-06 14:51:24 -0700227 return 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700228 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700230 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700231 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700232 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700234 else if (!is_user && arch_fixup(ip, regs))
Jeff Dike5d864562007-05-06 14:51:24 -0700235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeff Dike4c9e1382007-10-16 01:26:54 -0700237 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700238 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700239 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Richard Weinberger3ef61302011-05-24 17:13:03 -0700243 show_segv_info(regs);
244
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700245 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 si.si_signo = SIGBUS;
247 si.si_errno = 0;
248 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800249 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700250 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700252 } else {
253 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800255 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700256 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 force_sig_info(SIGSEGV, &si, current);
258 }
Jeff Dike5d864562007-05-06 14:51:24 -0700259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200262void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200264 struct faultinfo *fi;
265 struct siginfo clean_si;
266
Jeff Dike4c9e1382007-10-16 01:26:54 -0700267 if (!UPT_IS_USER(regs)) {
268 if (sig == SIGBUS)
269 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
270 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700272 }
273
Jeff Dike9226b832008-02-04 22:30:40 -0800274 arch_examine_signal(sig, regs);
275
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200276 memset(&clean_si, 0, sizeof(clean_si));
277 clean_si.si_signo = si->si_signo;
278 clean_si.si_errno = si->si_errno;
279 clean_si.si_code = si->si_code;
280 switch (sig) {
281 case SIGILL:
282 case SIGFPE:
283 case SIGSEGV:
284 case SIGBUS:
285 case SIGTRAP:
286 fi = UPT_FAULTINFO(regs);
287 clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
288 current->thread.arch.faultinfo = *fi;
289#ifdef __ARCH_SI_TRAPNO
290 clean_si.si_trapno = si->si_trapno;
291#endif
292 break;
293 default:
294 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
295 sig, si->si_code);
296 }
297
298 force_sig_info(sig, &clean_si, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200301void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700303 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700304 UML_LONGJMP(current->thread.fault_catcher, 1);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200305 else
306 relay_signal(sig, si, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200309void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 do_IRQ(WINCH_IRQ, regs);
312}
313
314void trap_init(void)
315{
316}