blob: cced829460427180091b494e7eb2c9315dd66d73 [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 {
Souptick Joarder50a7ca32018-08-17 15:44:47 -070075 vm_fault_t 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{
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800165 current->thread.arch.faultinfo = fi;
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500166 force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi),
167 current);
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800168}
169
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800170void fatal_sigsegv(void)
171{
172 force_sigsegv(SIGSEGV, current);
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700173 do_signal(&current->thread.regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800174 /*
175 * This is to tell gcc that we're not returning - do_signal
176 * can, in general, return, but in this case, it's not, since
177 * we just got a fatal SIGSEGV queued.
178 */
179 os_dump_core();
180}
181
Thomas Meyer88af2332017-07-06 00:34:04 +0200182/**
183 * segv_handler() - the SIGSEGV handler
184 * @sig: the signal number
185 * @unused_si: the signal info struct; unused in this handler
186 * @regs: the ptrace register information
187 *
188 * The handler first extracts the faultinfo from the UML ptrace regs struct.
189 * If the userfault did not happen in an UML userspace process, bad_segv is called.
190 * Otherwise the signal did happen in a cloned userspace process, handle it.
191 */
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200192void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800193{
194 struct faultinfo * fi = UPT_FAULTINFO(regs);
195
Jeff Dike4c9e1382007-10-16 01:26:54 -0700196 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700197 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800198 bad_segv(*fi, UPT_IP(regs));
199 return;
200 }
201 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
202}
203
Bodo Stroesserc5784552005-05-05 16:15:31 -0700204/*
205 * We give a *copy* of the faultinfo in the regs to segv.
206 * This must be done, since nesting SEGVs could overwrite
207 * the info in the regs. A pointer to the info then would
208 * give us bad data!
209 */
Jeff Dike5d864562007-05-06 14:51:24 -0700210unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700211 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Jeff Dikefab95c52007-10-16 01:27:05 -0700213 jmp_buf *catcher;
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500214 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700216 int is_write = FAULT_WRITE(fi);
217 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Richard Weinbergerbb6a1b22014-07-20 13:39:27 +0200219 if (!is_user && regs)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200220 current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
221
Jeff Dike4c9e1382007-10-16 01:26:54 -0700222 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700223 flush_tlb_kernel_vm();
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200224 goto out;
Jeff Dike5d864562007-05-06 14:51:24 -0700225 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700226 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700227 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700228 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700229 }
Richard Weinberger56b88a32015-08-09 22:26:33 +0200230 else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
Richard Weinbergerd2313082015-05-31 19:21:51 +0200231 show_regs(container_of(regs, struct pt_regs, regs));
232 panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
233 address, ip);
234 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700235
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100236 if (SEGV_IS_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700237 err = handle_page_fault(address, ip, is_write, is_user,
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500238 &si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700239 else {
240 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700241 /*
242 * A thread accessed NULL, we get a fault, but CR2 is invalid.
243 * This code is used in __do_copy_from_user() of TT mode.
244 * XXX tt mode is gone, so maybe this isn't needed any more
245 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700246 address = 0;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700250 if (!err)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200251 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700252 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700254 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700255 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700256 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700258 else if (!is_user && arch_fixup(ip, regs))
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jeff Dike4c9e1382007-10-16 01:26:54 -0700261 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700262 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700263 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Richard Weinberger3ef61302011-05-24 17:13:03 -0700267 show_segv_info(regs);
268
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700269 if (err == -EACCES) {
Jeff Dike5d864562007-05-06 14:51:24 -0700270 current->thread.arch.faultinfo = fi;
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500271 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address,
272 current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700273 } else {
274 BUG_ON(err != -EFAULT);
Jeff Dike5d864562007-05-06 14:51:24 -0700275 current->thread.arch.faultinfo = fi;
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500276 force_sig_fault(SIGSEGV, si_code, (void __user *) address,
277 current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200279
280out:
281 if (regs)
282 current->thread.segv_regs = NULL;
283
Jeff Dike5d864562007-05-06 14:51:24 -0700284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200287void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Eric W. Biederman530621b2018-04-16 16:12:31 -0500289 int code, err;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700290 if (!UPT_IS_USER(regs)) {
291 if (sig == SIGBUS)
292 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
293 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700295 }
296
Jeff Dike9226b832008-02-04 22:30:40 -0800297 arch_examine_signal(sig, regs);
298
Eric W. Biederman530621b2018-04-16 16:12:31 -0500299 /* Is the signal layout for the signal known?
300 * Signal data must be scrubbed to prevent information leaks.
301 */
302 code = si->si_code;
303 err = si->si_errno;
304 if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) {
305 struct faultinfo *fi = UPT_FAULTINFO(regs);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200306 current->thread.arch.faultinfo = *fi;
Eric W. Biederman530621b2018-04-16 16:12:31 -0500307 force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi),
308 current);
309 } else {
310 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n",
311 sig, code, err);
312 force_sig(sig, current);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200316void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700318 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700319 UML_LONGJMP(current->thread.fault_catcher, 1);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200320 else
321 relay_signal(sig, si, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200324void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 do_IRQ(WINCH_IRQ, regs);
327}
328
329void trap_init(void)
330{
331}