blob: b2b02df9896ee8ba629494bd4fa1c7551b26b178 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +01007#include <linux/sched/signal.h>
Jeff Dike4c9e1382007-10-16 01:26:54 -07008#include <linux/hardirq.h>
Al Viro73395a02011-08-18 20:14:10 +01009#include <linux/module.h>
Peter Zijlstrafbc9f162015-05-19 13:53:29 +020010#include <linux/uaccess.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010011#include <linux/sched/debug.h>
Jeff Dike4c9e1382007-10-16 01:26:54 -070012#include <asm/current.h>
13#include <asm/pgtable.h>
14#include <asm/tlbflush.h>
Al Viro37185b32012-10-08 03:27:32 +010015#include <arch.h>
16#include <as-layout.h>
17#include <kern_util.h>
18#include <os.h>
19#include <skas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jeff Dike4c9e1382007-10-16 01:26:54 -070021/*
22 * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by
23 * segv().
24 */
Jeff Dike1d3468a2006-07-10 04:45:13 -070025int handle_page_fault(unsigned long address, unsigned long ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int is_write, int is_user, int *code_out)
27{
28 struct mm_struct *mm = current->mm;
29 struct vm_area_struct *vma;
30 pgd_t *pgd;
31 pud_t *pud;
32 pmd_t *pmd;
33 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int err = -EFAULT;
Johannes Weiner759496b2013-09-12 15:13:39 -070035 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 *code_out = SEGV_MAPERR;
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070038
Jeff Dike4c9e1382007-10-16 01:26:54 -070039 /*
David Hildenbrand70ffdb92015-05-11 17:52:11 +020040 * If the fault was with pagefaults disabled, don't take the fault, just
Jeff Dike4c9e1382007-10-16 01:26:54 -070041 * fail.
42 */
David Hildenbrand70ffdb92015-05-11 17:52:11 +020043 if (faulthandler_disabled())
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070044 goto out_nosemaphore;
45
Johannes Weiner759496b2013-09-12 15:13:39 -070046 if (is_user)
47 flags |= FAULT_FLAG_USER;
Kautuk Consul1cefe282012-05-31 16:26:03 -070048retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 down_read(&mm->mmap_sem);
50 vma = find_vma(mm, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070051 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070053 else if (vma->vm_start <= address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 goto good_area;
Jeff Dike4c9e1382007-10-16 01:26:54 -070055 else if (!(vma->vm_flags & VM_GROWSDOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070057 else if (is_user && !ARCH_IS_STACKGROW(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070059 else if (expand_stack(vma, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 goto out;
61
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070062good_area:
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *code_out = SEGV_ACCERR;
Johannes Weiner759496b2013-09-12 15:13:39 -070064 if (is_write) {
65 if (!(vma->vm_flags & VM_WRITE))
66 goto out;
67 flags |= FAULT_FLAG_WRITE;
68 } else {
69 /* Don't require VM_READ|VM_EXEC for write faults! */
70 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
71 goto out;
72 }
Jeff Dike13479d52005-05-20 13:59:08 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 do {
Nick Piggin83c54072007-07-19 01:47:05 -070075 int fault;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -080076
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -070077 fault = handle_mm_fault(vma, address, flags);
Kautuk Consul1cefe282012-05-31 16:26:03 -070078
79 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
80 goto out_nosemaphore;
81
Nick Piggin83c54072007-07-19 01:47:05 -070082 if (unlikely(fault & VM_FAULT_ERROR)) {
83 if (fault & VM_FAULT_OOM) {
Nick Piggin83c54072007-07-19 01:47:05 -070084 goto out_of_memory;
Linus Torvalds33692f22015-01-29 10:51:32 -080085 } else if (fault & VM_FAULT_SIGSEGV) {
86 goto out;
Nick Piggin83c54072007-07-19 01:47:05 -070087 } else if (fault & VM_FAULT_SIGBUS) {
88 err = -EACCES;
89 goto out;
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 BUG();
92 }
Kautuk Consul1cefe282012-05-31 16:26:03 -070093 if (flags & FAULT_FLAG_ALLOW_RETRY) {
94 if (fault & VM_FAULT_MAJOR)
95 current->maj_flt++;
96 else
97 current->min_flt++;
98 if (fault & VM_FAULT_RETRY) {
99 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -0700100 flags |= FAULT_FLAG_TRIED;
Kautuk Consul1cefe282012-05-31 16:26:03 -0700101
102 goto retry;
103 }
104 }
Nick Piggin83c54072007-07-19 01:47:05 -0700105
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700106 pgd = pgd_offset(mm, address);
107 pud = pud_offset(pgd, address);
108 pmd = pmd_offset(pud, address);
109 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -0700110 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700112 /*
113 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800114 * pte_mkyoung(); if (is_write) pte_mkdirty();
115 * If it's triggered, we'd see normally a hang here (a clean pte is
116 * marked read-only to emulate the dirty bit).
117 * However, the generic code can mark a PTE writable but clean on a
118 * concurrent read fault, triggering this harmlessly. So comment it out.
119 */
120#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +0200121 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800122#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700123 flush_tlb_page(vma, address);
124out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700126out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800130 /*
131 * We ran out of memory, call the OOM killer, and return the userspace
132 * (which will retry the fault, or kill us if we got oom-killed).
133 */
134 up_read(&mm->mmap_sem);
Johannes Weiner87134102013-09-12 15:13:38 -0700135 if (!is_user)
136 goto out_nosemaphore;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800137 pagefault_out_of_memory();
138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Al Viro73395a02011-08-18 20:14:10 +0100140EXPORT_SYMBOL(handle_page_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Richard Weinberger3ef61302011-05-24 17:13:03 -0700142static void show_segv_info(struct uml_pt_regs *regs)
143{
144 struct task_struct *tsk = current;
145 struct faultinfo *fi = UPT_FAULTINFO(regs);
146
147 if (!unhandled_signal(tsk, SIGSEGV))
148 return;
149
150 if (!printk_ratelimit())
151 return;
152
Kees Cook10a7e9d2017-12-19 13:52:23 -0800153 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x",
Richard Weinberger3ef61302011-05-24 17:13:03 -0700154 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
155 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
156 (void *)UPT_IP(regs), (void *)UPT_SP(regs),
157 fi->error_code);
158
159 print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
160 printk(KERN_CONT "\n");
161}
162
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800163static void bad_segv(struct faultinfo fi, unsigned long ip)
164{
165 struct siginfo si;
166
167 si.si_signo = SIGSEGV;
168 si.si_code = SEGV_ACCERR;
169 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
170 current->thread.arch.faultinfo = fi;
171 force_sig_info(SIGSEGV, &si, current);
172}
173
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800174void fatal_sigsegv(void)
175{
176 force_sigsegv(SIGSEGV, current);
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700177 do_signal(&current->thread.regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800178 /*
179 * This is to tell gcc that we're not returning - do_signal
180 * can, in general, return, but in this case, it's not, since
181 * we just got a fatal SIGSEGV queued.
182 */
183 os_dump_core();
184}
185
Thomas Meyer88af2332017-07-06 00:34:04 +0200186/**
187 * segv_handler() - the SIGSEGV handler
188 * @sig: the signal number
189 * @unused_si: the signal info struct; unused in this handler
190 * @regs: the ptrace register information
191 *
192 * The handler first extracts the faultinfo from the UML ptrace regs struct.
193 * If the userfault did not happen in an UML userspace process, bad_segv is called.
194 * Otherwise the signal did happen in a cloned userspace process, handle it.
195 */
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200196void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800197{
198 struct faultinfo * fi = UPT_FAULTINFO(regs);
199
Jeff Dike4c9e1382007-10-16 01:26:54 -0700200 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700201 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800202 bad_segv(*fi, UPT_IP(regs));
203 return;
204 }
205 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
206}
207
Bodo Stroesserc5784552005-05-05 16:15:31 -0700208/*
209 * We give a *copy* of the faultinfo in the regs to segv.
210 * This must be done, since nesting SEGVs could overwrite
211 * the info in the regs. A pointer to the info then would
212 * give us bad data!
213 */
Jeff Dike5d864562007-05-06 14:51:24 -0700214unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700215 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700218 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700220 int is_write = FAULT_WRITE(fi);
221 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Richard Weinbergerbb6a1b22014-07-20 13:39:27 +0200223 if (!is_user && regs)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200224 current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
225
Jeff Dike4c9e1382007-10-16 01:26:54 -0700226 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700227 flush_tlb_kernel_vm();
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200228 goto out;
Jeff Dike5d864562007-05-06 14:51:24 -0700229 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700230 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700231 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700232 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700233 }
Richard Weinberger56b88a32015-08-09 22:26:33 +0200234 else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
Richard Weinbergerd2313082015-05-31 19:21:51 +0200235 show_regs(container_of(regs, struct pt_regs, regs));
236 panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
237 address, ip);
238 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700239
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100240 if (SEGV_IS_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700241 err = handle_page_fault(address, ip, is_write, is_user,
242 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700243 else {
244 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700245 /*
246 * A thread accessed NULL, we get a fault, but CR2 is invalid.
247 * This code is used in __do_copy_from_user() of TT mode.
248 * XXX tt mode is gone, so maybe this isn't needed any more
249 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700250 address = 0;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700254 if (!err)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200255 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700256 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700258 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700259 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700260 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700262 else if (!is_user && arch_fixup(ip, regs))
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jeff Dike4c9e1382007-10-16 01:26:54 -0700265 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700266 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700267 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Richard Weinberger3ef61302011-05-24 17:13:03 -0700271 show_segv_info(regs);
272
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700273 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 si.si_signo = SIGBUS;
275 si.si_errno = 0;
276 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800277 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700278 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700280 } else {
281 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800283 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700284 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 force_sig_info(SIGSEGV, &si, current);
286 }
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200287
288out:
289 if (regs)
290 current->thread.segv_regs = NULL;
291
Jeff Dike5d864562007-05-06 14:51:24 -0700292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200295void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200297 struct faultinfo *fi;
298 struct siginfo clean_si;
299
Jeff Dike4c9e1382007-10-16 01:26:54 -0700300 if (!UPT_IS_USER(regs)) {
301 if (sig == SIGBUS)
302 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
303 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700305 }
306
Jeff Dike9226b832008-02-04 22:30:40 -0800307 arch_examine_signal(sig, regs);
308
Eric W. Biederman3b10db2b2017-08-18 19:56:27 -0500309 clear_siginfo(&clean_si);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200310 clean_si.si_signo = si->si_signo;
311 clean_si.si_errno = si->si_errno;
312 clean_si.si_code = si->si_code;
313 switch (sig) {
314 case SIGILL:
315 case SIGFPE:
316 case SIGSEGV:
317 case SIGBUS:
318 case SIGTRAP:
319 fi = UPT_FAULTINFO(regs);
320 clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
321 current->thread.arch.faultinfo = *fi;
322#ifdef __ARCH_SI_TRAPNO
323 clean_si.si_trapno = si->si_trapno;
324#endif
325 break;
326 default:
327 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
328 sig, si->si_code);
329 }
330
331 force_sig_info(sig, &clean_si, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200334void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700336 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700337 UML_LONGJMP(current->thread.fault_catcher, 1);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200338 else
339 relay_signal(sig, si, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200342void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 do_IRQ(WINCH_IRQ, regs);
345}
346
347void trap_init(void)
348{
349}