blob: 3be60765c0e25d634282ea66e0902a9dc8d41bd4 [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>
Jeff Dikeeb830752007-05-06 14:51:07 -070013#include "arch.h"
Jeff Dike4c9e1382007-10-16 01:26:54 -070014#include "as-layout.h"
15#include "kern_util.h"
16#include "os.h"
Jeff Diked83ecf02008-02-04 22:30:47 -080017#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;
92
93 goto retry;
94 }
95 }
Nick Piggin83c54072007-07-19 01:47:05 -070096
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070097 pgd = pgd_offset(mm, address);
98 pud = pud_offset(pgd, address);
99 pmd = pmd_offset(pud, address);
100 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -0700101 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700103 /*
104 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800105 * 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
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +0200112 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800113#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700114 flush_tlb_page(vma, address);
115out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700117out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800121 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
Al Viro73395a02011-08-18 20:14:10 +0100129EXPORT_SYMBOL(handle_page_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Richard Weinberger3ef61302011-05-24 17:13:03 -0700131static 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
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800152static 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
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800163void 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
Jeff Dikeedea1382008-02-04 22:30:46 -0800175void segv_handler(int sig, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800176{
177 struct faultinfo * fi = UPT_FAULTINFO(regs);
178
Jeff Dike4c9e1382007-10-16 01:26:54 -0700179 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700180 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800181 bad_segv(*fi, UPT_IP(regs));
182 return;
183 }
184 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
185}
186
Bodo Stroesserc5784552005-05-05 16:15:31 -0700187/*
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 */
Jeff Dike5d864562007-05-06 14:51:24 -0700193unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700194 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700197 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700199 int is_write = FAULT_WRITE(fi);
200 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jeff Dike4c9e1382007-10-16 01:26:54 -0700202 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700203 flush_tlb_kernel_vm();
204 return 0;
205 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700206 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700207 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700208 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700209 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700210
Paolo 'Blaisorblade' Giarrussobe662a12005-09-30 11:58:59 -0700211 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700212 err = handle_page_fault(address, ip, is_write, is_user,
213 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700214 else {
215 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700216 /*
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 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700221 address = 0;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700225 if (!err)
Jeff Dike5d864562007-05-06 14:51:24 -0700226 return 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700227 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700229 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700230 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700231 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700233 else if (!is_user && arch_fixup(ip, regs))
Jeff Dike5d864562007-05-06 14:51:24 -0700234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jeff Dike4c9e1382007-10-16 01:26:54 -0700236 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700237 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700238 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Richard Weinberger3ef61302011-05-24 17:13:03 -0700242 show_segv_info(regs);
243
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700244 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 si.si_signo = SIGBUS;
246 si.si_errno = 0;
247 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800248 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700249 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700251 } else {
252 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800254 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700255 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 force_sig_info(SIGSEGV, &si, current);
257 }
Jeff Dike5d864562007-05-06 14:51:24 -0700258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Jeff Dike77bf4402007-10-16 01:26:58 -0700261void relay_signal(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700263 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700268 }
269
Jeff Dike9226b832008-02-04 22:30:40 -0800270 arch_examine_signal(sig, regs);
271
Jeff Dike5d864562007-05-06 14:51:24 -0700272 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 force_sig(sig, current);
274}
275
Jeff Dikeedea1382008-02-04 22:30:46 -0800276void bus_handler(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700278 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700279 UML_LONGJMP(current->thread.fault_catcher, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 else relay_signal(sig, regs);
281}
282
Jeff Dikeedea1382008-02-04 22:30:46 -0800283void winch(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 do_IRQ(WINCH_IRQ, regs);
286}
287
288void trap_init(void)
289{
290}