blob: f9c9e5a6bebaacba7b3eae6ade67dc243e2da5eb [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;
Johannes Weiner759496b2013-09-12 15:13:39 -070033 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 *code_out = SEGV_MAPERR;
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070036
Jeff Dike4c9e1382007-10-16 01:26:54 -070037 /*
David Hildenbrand70ffdb92015-05-11 17:52:11 +020038 * If the fault was with pagefaults disabled, don't take the fault, just
Jeff Dike4c9e1382007-10-16 01:26:54 -070039 * fail.
40 */
David Hildenbrand70ffdb92015-05-11 17:52:11 +020041 if (faulthandler_disabled())
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070042 goto out_nosemaphore;
43
Johannes Weiner759496b2013-09-12 15:13:39 -070044 if (is_user)
45 flags |= FAULT_FLAG_USER;
Kautuk Consul1cefe282012-05-31 16:26:03 -070046retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 down_read(&mm->mmap_sem);
48 vma = find_vma(mm, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070049 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070051 else if (vma->vm_start <= address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 goto good_area;
Jeff Dike4c9e1382007-10-16 01:26:54 -070053 else if (!(vma->vm_flags & VM_GROWSDOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070055 else if (is_user && !ARCH_IS_STACKGROW(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070057 else if (expand_stack(vma, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 goto out;
59
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070060good_area:
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *code_out = SEGV_ACCERR;
Johannes Weiner759496b2013-09-12 15:13:39 -070062 if (is_write) {
63 if (!(vma->vm_flags & VM_WRITE))
64 goto out;
65 flags |= FAULT_FLAG_WRITE;
66 } else {
67 /* Don't require VM_READ|VM_EXEC for write faults! */
68 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
69 goto out;
70 }
Jeff Dike13479d52005-05-20 13:59:08 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 do {
Nick Piggin83c54072007-07-19 01:47:05 -070073 int fault;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -080074
Kautuk Consul1cefe282012-05-31 16:26:03 -070075 fault = handle_mm_fault(mm, vma, address, flags);
76
77 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
78 goto out_nosemaphore;
79
Nick Piggin83c54072007-07-19 01:47:05 -070080 if (unlikely(fault & VM_FAULT_ERROR)) {
81 if (fault & VM_FAULT_OOM) {
Nick Piggin83c54072007-07-19 01:47:05 -070082 goto out_of_memory;
Linus Torvalds33692f22015-01-29 10:51:32 -080083 } else if (fault & VM_FAULT_SIGSEGV) {
84 goto out;
Nick Piggin83c54072007-07-19 01:47:05 -070085 } else if (fault & VM_FAULT_SIGBUS) {
86 err = -EACCES;
87 goto out;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 BUG();
90 }
Kautuk Consul1cefe282012-05-31 16:26:03 -070091 if (flags & FAULT_FLAG_ALLOW_RETRY) {
92 if (fault & VM_FAULT_MAJOR)
93 current->maj_flt++;
94 else
95 current->min_flt++;
96 if (fault & VM_FAULT_RETRY) {
97 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -070098 flags |= FAULT_FLAG_TRIED;
Kautuk Consul1cefe282012-05-31 16:26:03 -070099
100 goto retry;
101 }
102 }
Nick Piggin83c54072007-07-19 01:47:05 -0700103
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700104 pgd = pgd_offset(mm, address);
105 pud = pud_offset(pgd, address);
106 pmd = pmd_offset(pud, address);
107 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -0700108 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700110 /*
111 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800112 * pte_mkyoung(); if (is_write) pte_mkdirty();
113 * If it's triggered, we'd see normally a hang here (a clean pte is
114 * marked read-only to emulate the dirty bit).
115 * However, the generic code can mark a PTE writable but clean on a
116 * concurrent read fault, triggering this harmlessly. So comment it out.
117 */
118#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +0200119 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800120#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700121 flush_tlb_page(vma, address);
122out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700124out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800128 /*
129 * We ran out of memory, call the OOM killer, and return the userspace
130 * (which will retry the fault, or kill us if we got oom-killed).
131 */
132 up_read(&mm->mmap_sem);
Johannes Weiner87134102013-09-12 15:13:38 -0700133 if (!is_user)
134 goto out_nosemaphore;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800135 pagefault_out_of_memory();
136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
Al Viro73395a02011-08-18 20:14:10 +0100138EXPORT_SYMBOL(handle_page_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Richard Weinberger3ef61302011-05-24 17:13:03 -0700140static void show_segv_info(struct uml_pt_regs *regs)
141{
142 struct task_struct *tsk = current;
143 struct faultinfo *fi = UPT_FAULTINFO(regs);
144
145 if (!unhandled_signal(tsk, SIGSEGV))
146 return;
147
148 if (!printk_ratelimit())
149 return;
150
151 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
152 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
153 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
154 (void *)UPT_IP(regs), (void *)UPT_SP(regs),
155 fi->error_code);
156
157 print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
158 printk(KERN_CONT "\n");
159}
160
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800161static void bad_segv(struct faultinfo fi, unsigned long ip)
162{
163 struct siginfo si;
164
165 si.si_signo = SIGSEGV;
166 si.si_code = SEGV_ACCERR;
167 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
168 current->thread.arch.faultinfo = fi;
169 force_sig_info(SIGSEGV, &si, current);
170}
171
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800172void fatal_sigsegv(void)
173{
174 force_sigsegv(SIGSEGV, current);
175 do_signal();
176 /*
177 * This is to tell gcc that we're not returning - do_signal
178 * can, in general, return, but in this case, it's not, since
179 * we just got a fatal SIGSEGV queued.
180 */
181 os_dump_core();
182}
183
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200184void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800185{
186 struct faultinfo * fi = UPT_FAULTINFO(regs);
187
Jeff Dike4c9e1382007-10-16 01:26:54 -0700188 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700189 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800190 bad_segv(*fi, UPT_IP(regs));
191 return;
192 }
193 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
194}
195
Bodo Stroesserc5784552005-05-05 16:15:31 -0700196/*
197 * We give a *copy* of the faultinfo in the regs to segv.
198 * This must be done, since nesting SEGVs could overwrite
199 * the info in the regs. A pointer to the info then would
200 * give us bad data!
201 */
Jeff Dike5d864562007-05-06 14:51:24 -0700202unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700203 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700206 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700208 int is_write = FAULT_WRITE(fi);
209 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Richard Weinbergerbb6a1b22014-07-20 13:39:27 +0200211 if (!is_user && regs)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200212 current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
213
Jeff Dike4c9e1382007-10-16 01:26:54 -0700214 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700215 flush_tlb_kernel_vm();
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200216 goto out;
Jeff Dike5d864562007-05-06 14:51:24 -0700217 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700218 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700219 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700220 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700221 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700222
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100223 if (SEGV_IS_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700224 err = handle_page_fault(address, ip, is_write, is_user,
225 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700226 else {
227 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700228 /*
229 * A thread accessed NULL, we get a fault, but CR2 is invalid.
230 * This code is used in __do_copy_from_user() of TT mode.
231 * XXX tt mode is gone, so maybe this isn't needed any more
232 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700233 address = 0;
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700237 if (!err)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200238 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700239 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700241 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700242 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700243 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700245 else if (!is_user && arch_fixup(ip, regs))
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jeff Dike4c9e1382007-10-16 01:26:54 -0700248 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700249 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700250 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Richard Weinberger3ef61302011-05-24 17:13:03 -0700254 show_segv_info(regs);
255
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700256 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 si.si_signo = SIGBUS;
258 si.si_errno = 0;
259 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800260 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700261 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700263 } else {
264 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800266 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700267 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 force_sig_info(SIGSEGV, &si, current);
269 }
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200270
271out:
272 if (regs)
273 current->thread.segv_regs = NULL;
274
Jeff Dike5d864562007-05-06 14:51:24 -0700275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200278void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200280 struct faultinfo *fi;
281 struct siginfo clean_si;
282
Jeff Dike4c9e1382007-10-16 01:26:54 -0700283 if (!UPT_IS_USER(regs)) {
284 if (sig == SIGBUS)
285 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
286 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700288 }
289
Jeff Dike9226b832008-02-04 22:30:40 -0800290 arch_examine_signal(sig, regs);
291
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200292 memset(&clean_si, 0, sizeof(clean_si));
293 clean_si.si_signo = si->si_signo;
294 clean_si.si_errno = si->si_errno;
295 clean_si.si_code = si->si_code;
296 switch (sig) {
297 case SIGILL:
298 case SIGFPE:
299 case SIGSEGV:
300 case SIGBUS:
301 case SIGTRAP:
302 fi = UPT_FAULTINFO(regs);
303 clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
304 current->thread.arch.faultinfo = *fi;
305#ifdef __ARCH_SI_TRAPNO
306 clean_si.si_trapno = si->si_trapno;
307#endif
308 break;
309 default:
310 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
311 sig, si->si_code);
312 }
313
314 force_sig_info(sig, &clean_si, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200317void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700319 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700320 UML_LONGJMP(current->thread.fault_catcher, 1);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200321 else
322 relay_signal(sig, si, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200325void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 do_IRQ(WINCH_IRQ, regs);
328}
329
330void trap_init(void)
331{
332}