blob: c32efb09db2142cd3a53a481a18c4276f33b196b [file] [log] [blame]
Alex Dewar0d1fb0a2019-08-25 10:49:17 +01001// SPDX-License-Identifier: GPL-2.0
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -08002/*
Jeff Dike4c9e1382007-10-16 01:26:54 -07003 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
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>
Jeff Dike4c9e1382007-10-16 01:26:54 -070013#include <asm/tlbflush.h>
Al Viro37185b32012-10-08 03:27:32 +010014#include <arch.h>
15#include <as-layout.h>
16#include <kern_util.h>
17#include <os.h>
18#include <skas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jeff Dike4c9e1382007-10-16 01:26:54 -070020/*
Colin Ian King59fdf912018-10-26 19:02:47 +010021 * Note this is constrained to return 0, -EFAULT, -EACCES, -ENOMEM by
Jeff Dike4c9e1382007-10-16 01:26:54 -070022 * segv().
23 */
Jeff Dike1d3468a2006-07-10 04:45:13 -070024int handle_page_fault(unsigned long address, unsigned long ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 int is_write, int is_user, int *code_out)
26{
27 struct mm_struct *mm = current->mm;
28 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 pmd_t *pmd;
30 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int err = -EFAULT;
Peter Xudde16072020-04-01 21:08:37 -070032 unsigned int flags = FAULT_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 *code_out = SEGV_MAPERR;
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070035
Jeff Dike4c9e1382007-10-16 01:26:54 -070036 /*
David Hildenbrand70ffdb92015-05-11 17:52:11 +020037 * If the fault was with pagefaults disabled, don't take the fault, just
Jeff Dike4c9e1382007-10-16 01:26:54 -070038 * fail.
39 */
David Hildenbrand70ffdb92015-05-11 17:52:11 +020040 if (faulthandler_disabled())
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070041 goto out_nosemaphore;
42
Johannes Weiner759496b2013-09-12 15:13:39 -070043 if (is_user)
44 flags |= FAULT_FLAG_USER;
Kautuk Consul1cefe282012-05-31 16:26:03 -070045retry:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070046 mmap_read_lock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 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;
Johannes Weiner759496b2013-09-12 15:13:39 -070061 if (is_write) {
62 if (!(vma->vm_flags & VM_WRITE))
63 goto out;
64 flags |= FAULT_FLAG_WRITE;
65 } else {
66 /* Don't require VM_READ|VM_EXEC for write faults! */
67 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
68 goto out;
69 }
Jeff Dike13479d52005-05-20 13:59:08 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 do {
Souptick Joarder50a7ca32018-08-17 15:44:47 -070072 vm_fault_t fault;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -080073
Peter Xubce617e2020-08-11 18:37:44 -070074 fault = handle_mm_fault(vma, address, flags, NULL);
Kautuk Consul1cefe282012-05-31 16:26:03 -070075
76 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
77 goto out_nosemaphore;
78
Nick Piggin83c54072007-07-19 01:47:05 -070079 if (unlikely(fault & VM_FAULT_ERROR)) {
80 if (fault & VM_FAULT_OOM) {
Nick Piggin83c54072007-07-19 01:47:05 -070081 goto out_of_memory;
Linus Torvalds33692f22015-01-29 10:51:32 -080082 } else if (fault & VM_FAULT_SIGSEGV) {
83 goto out;
Nick Piggin83c54072007-07-19 01:47:05 -070084 } else if (fault & VM_FAULT_SIGBUS) {
85 err = -EACCES;
86 goto out;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 BUG();
89 }
Kautuk Consul1cefe282012-05-31 16:26:03 -070090 if (flags & FAULT_FLAG_ALLOW_RETRY) {
Kautuk Consul1cefe282012-05-31 16:26:03 -070091 if (fault & VM_FAULT_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
Mike Rapoporte05c7b12020-06-08 21:33:05 -070098 pmd = pmd_off(mm, address);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070099 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -0700100 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700102 /*
103 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800104 * pte_mkyoung(); if (is_write) pte_mkdirty();
105 * If it's triggered, we'd see normally a hang here (a clean pte is
106 * marked read-only to emulate the dirty bit).
107 * However, the generic code can mark a PTE writable but clean on a
108 * concurrent read fault, triggering this harmlessly. So comment it out.
109 */
110#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +0200111 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800112#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700113 flush_tlb_page(vma, address);
114out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700115 mmap_read_unlock(mm);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700116out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800120 /*
121 * We ran out of memory, call the OOM killer, and return the userspace
122 * (which will retry the fault, or kill us if we got oom-killed).
123 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700124 mmap_read_unlock(mm);
Johannes Weiner87134102013-09-12 15:13:38 -0700125 if (!is_user)
126 goto out_nosemaphore;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800127 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
Kees Cook10a7e9d2017-12-19 13:52:23 -0800143 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x",
Richard Weinberger3ef61302011-05-24 17:13:03 -0700144 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{
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800155 current->thread.arch.faultinfo = fi;
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500156 force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi));
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800157}
158
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800159void fatal_sigsegv(void)
160{
Eric W. Biedermane21294a2021-10-25 10:50:57 -0500161 force_fatal_sig(SIGSEGV);
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700162 do_signal(&current->thread.regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800163 /*
164 * This is to tell gcc that we're not returning - do_signal
165 * can, in general, return, but in this case, it's not, since
166 * we just got a fatal SIGSEGV queued.
167 */
168 os_dump_core();
169}
170
Thomas Meyer88af2332017-07-06 00:34:04 +0200171/**
172 * segv_handler() - the SIGSEGV handler
173 * @sig: the signal number
174 * @unused_si: the signal info struct; unused in this handler
175 * @regs: the ptrace register information
176 *
177 * The handler first extracts the faultinfo from the UML ptrace regs struct.
178 * If the userfault did not happen in an UML userspace process, bad_segv is called.
179 * Otherwise the signal did happen in a cloned userspace process, handle it.
180 */
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200181void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800182{
183 struct faultinfo * fi = UPT_FAULTINFO(regs);
184
Jeff Dike4c9e1382007-10-16 01:26:54 -0700185 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700186 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800187 bad_segv(*fi, UPT_IP(regs));
188 return;
189 }
190 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
191}
192
Bodo Stroesserc5784552005-05-05 16:15:31 -0700193/*
194 * We give a *copy* of the faultinfo in the regs to segv.
195 * This must be done, since nesting SEGVs could overwrite
196 * the info in the regs. A pointer to the info then would
197 * give us bad data!
198 */
Jeff Dike5d864562007-05-06 14:51:24 -0700199unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700200 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Jeff Dikefab95c52007-10-16 01:27:05 -0700202 jmp_buf *catcher;
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500203 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700205 int is_write = FAULT_WRITE(fi);
206 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Richard Weinbergerbb6a1b22014-07-20 13:39:27 +0200208 if (!is_user && regs)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200209 current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
210
Jeff Dike4c9e1382007-10-16 01:26:54 -0700211 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700212 flush_tlb_kernel_vm();
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200213 goto out;
Jeff Dike5d864562007-05-06 14:51:24 -0700214 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700215 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700216 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700217 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700218 }
Richard Weinberger56b88a32015-08-09 22:26:33 +0200219 else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
Richard Weinbergerd2313082015-05-31 19:21:51 +0200220 show_regs(container_of(regs, struct pt_regs, regs));
221 panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
222 address, ip);
223 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700224
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100225 if (SEGV_IS_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700226 err = handle_page_fault(address, ip, is_write, is_user,
Eric W. Biedermanbc08c072018-04-15 19:50:48 -0500227 &si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700228 else {
229 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700230 /*
231 * A thread accessed NULL, we get a fault, but CR2 is invalid.
232 * This code is used in __do_copy_from_user() of TT mode.
233 * XXX tt mode is gone, so maybe this isn't needed any more
234 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700235 address = 0;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700239 if (!err)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200240 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700241 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700243 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700244 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700245 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700247 else if (!is_user && arch_fixup(ip, regs))
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jeff Dike4c9e1382007-10-16 01:26:54 -0700250 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700251 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700252 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Richard Weinberger3ef61302011-05-24 17:13:03 -0700256 show_segv_info(regs);
257
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700258 if (err == -EACCES) {
Jeff Dike5d864562007-05-06 14:51:24 -0700259 current->thread.arch.faultinfo = fi;
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500260 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700261 } else {
262 BUG_ON(err != -EFAULT);
Jeff Dike5d864562007-05-06 14:51:24 -0700263 current->thread.arch.faultinfo = fi;
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500264 force_sig_fault(SIGSEGV, si_code, (void __user *) address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200266
267out:
268 if (regs)
269 current->thread.segv_regs = NULL;
270
Jeff Dike5d864562007-05-06 14:51:24 -0700271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200274void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Eric W. Biederman530621b2018-04-16 16:12:31 -0500276 int code, err;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700277 if (!UPT_IS_USER(regs)) {
278 if (sig == SIGBUS)
279 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
280 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700282 }
283
Jeff Dike9226b832008-02-04 22:30:40 -0800284 arch_examine_signal(sig, regs);
285
Eric W. Biederman530621b2018-04-16 16:12:31 -0500286 /* Is the signal layout for the signal known?
287 * Signal data must be scrubbed to prevent information leaks.
288 */
289 code = si->si_code;
290 err = si->si_errno;
291 if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) {
292 struct faultinfo *fi = UPT_FAULTINFO(regs);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200293 current->thread.arch.faultinfo = *fi;
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500294 force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi));
Eric W. Biederman530621b2018-04-16 16:12:31 -0500295 } else {
296 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n",
297 sig, code, err);
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500298 force_sig(sig);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200302void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700304 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700305 UML_LONGJMP(current->thread.fault_catcher, 1);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200306 else
307 relay_signal(sig, si, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200310void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 do_IRQ(WINCH_IRQ, regs);
313}