blob: 5678c3571e7cb4d1572d0b16a91b0650f76095c7 [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 /*
38 * If the fault was during atomic operation, don't take the fault, just
39 * fail.
40 */
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070041 if (in_atomic())
42 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;
83 } else if (fault & VM_FAULT_SIGBUS) {
84 err = -EACCES;
85 goto out;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 BUG();
88 }
Kautuk Consul1cefe282012-05-31 16:26:03 -070089 if (flags & FAULT_FLAG_ALLOW_RETRY) {
90 if (fault & VM_FAULT_MAJOR)
91 current->maj_flt++;
92 else
93 current->min_flt++;
94 if (fault & VM_FAULT_RETRY) {
95 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -070096 flags |= FAULT_FLAG_TRIED;
Kautuk Consul1cefe282012-05-31 16:26:03 -070097
98 goto retry;
99 }
100 }
Nick Piggin83c54072007-07-19 01:47:05 -0700101
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700102 pgd = pgd_offset(mm, address);
103 pud = pud_offset(pgd, address);
104 pmd = pmd_offset(pud, address);
105 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -0700106 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700108 /*
109 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800110 * pte_mkyoung(); if (is_write) pte_mkdirty();
111 * If it's triggered, we'd see normally a hang here (a clean pte is
112 * marked read-only to emulate the dirty bit).
113 * However, the generic code can mark a PTE writable but clean on a
114 * concurrent read fault, triggering this harmlessly. So comment it out.
115 */
116#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +0200117 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800118#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700119 flush_tlb_page(vma, address);
120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700122out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800126 /*
127 * We ran out of memory, call the OOM killer, and return the userspace
128 * (which will retry the fault, or kill us if we got oom-killed).
129 */
130 up_read(&mm->mmap_sem);
Johannes Weiner87134102013-09-12 15:13:38 -0700131 if (!is_user)
132 goto out_nosemaphore;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800133 pagefault_out_of_memory();
134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Al Viro73395a02011-08-18 20:14:10 +0100136EXPORT_SYMBOL(handle_page_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Richard Weinberger3ef61302011-05-24 17:13:03 -0700138static void show_segv_info(struct uml_pt_regs *regs)
139{
140 struct task_struct *tsk = current;
141 struct faultinfo *fi = UPT_FAULTINFO(regs);
142
143 if (!unhandled_signal(tsk, SIGSEGV))
144 return;
145
146 if (!printk_ratelimit())
147 return;
148
149 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
150 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
151 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
152 (void *)UPT_IP(regs), (void *)UPT_SP(regs),
153 fi->error_code);
154
155 print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
156 printk(KERN_CONT "\n");
157}
158
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800159static void bad_segv(struct faultinfo fi, unsigned long ip)
160{
161 struct siginfo si;
162
163 si.si_signo = SIGSEGV;
164 si.si_code = SEGV_ACCERR;
165 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
166 current->thread.arch.faultinfo = fi;
167 force_sig_info(SIGSEGV, &si, current);
168}
169
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800170void fatal_sigsegv(void)
171{
172 force_sigsegv(SIGSEGV, current);
173 do_signal();
174 /*
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
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200182void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800183{
184 struct faultinfo * fi = UPT_FAULTINFO(regs);
185
Jeff Dike4c9e1382007-10-16 01:26:54 -0700186 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Richard Weinberger3ef61302011-05-24 17:13:03 -0700187 show_segv_info(regs);
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800188 bad_segv(*fi, UPT_IP(regs));
189 return;
190 }
191 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
192}
193
Bodo Stroesserc5784552005-05-05 16:15:31 -0700194/*
195 * We give a *copy* of the faultinfo in the regs to segv.
196 * This must be done, since nesting SEGVs could overwrite
197 * the info in the regs. A pointer to the info then would
198 * give us bad data!
199 */
Jeff Dike5d864562007-05-06 14:51:24 -0700200unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700201 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700204 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700206 int is_write = FAULT_WRITE(fi);
207 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Richard Weinbergerbb6a1b22014-07-20 13:39:27 +0200209 if (!is_user && regs)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200210 current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
211
Jeff Dike4c9e1382007-10-16 01:26:54 -0700212 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700213 flush_tlb_kernel_vm();
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200214 goto out;
Jeff Dike5d864562007-05-06 14:51:24 -0700215 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700216 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700217 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700218 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700219 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700220
Paolo 'Blaisorblade' Giarrussobe662a12005-09-30 11:58:59 -0700221 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700222 err = handle_page_fault(address, ip, is_write, is_user,
223 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700224 else {
225 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700226 /*
227 * A thread accessed NULL, we get a fault, but CR2 is invalid.
228 * This code is used in __do_copy_from_user() of TT mode.
229 * XXX tt mode is gone, so maybe this isn't needed any more
230 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700231 address = 0;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700235 if (!err)
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200236 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700237 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700239 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700240 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700241 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700243 else if (!is_user && arch_fixup(ip, regs))
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Jeff Dike4c9e1382007-10-16 01:26:54 -0700246 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700247 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700248 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Richard Weinberger3ef61302011-05-24 17:13:03 -0700252 show_segv_info(regs);
253
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700254 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 si.si_signo = SIGBUS;
256 si.si_errno = 0;
257 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800258 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700259 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700261 } else {
262 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800264 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700265 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 force_sig_info(SIGSEGV, &si, current);
267 }
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200268
269out:
270 if (regs)
271 current->thread.segv_regs = NULL;
272
Jeff Dike5d864562007-05-06 14:51:24 -0700273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200276void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200278 struct faultinfo *fi;
279 struct siginfo clean_si;
280
Jeff Dike4c9e1382007-10-16 01:26:54 -0700281 if (!UPT_IS_USER(regs)) {
282 if (sig == SIGBUS)
283 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
284 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700286 }
287
Jeff Dike9226b832008-02-04 22:30:40 -0800288 arch_examine_signal(sig, regs);
289
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200290 memset(&clean_si, 0, sizeof(clean_si));
291 clean_si.si_signo = si->si_signo;
292 clean_si.si_errno = si->si_errno;
293 clean_si.si_code = si->si_code;
294 switch (sig) {
295 case SIGILL:
296 case SIGFPE:
297 case SIGSEGV:
298 case SIGBUS:
299 case SIGTRAP:
300 fi = UPT_FAULTINFO(regs);
301 clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
302 current->thread.arch.faultinfo = *fi;
303#ifdef __ARCH_SI_TRAPNO
304 clean_si.si_trapno = si->si_trapno;
305#endif
306 break;
307 default:
308 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
309 sig, si->si_code);
310 }
311
312 force_sig_info(sig, &clean_si, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200315void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700317 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700318 UML_LONGJMP(current->thread.fault_catcher, 1);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200319 else
320 relay_signal(sig, si, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200323void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 do_IRQ(WINCH_IRQ, regs);
326}
327
328void trap_init(void)
329{
330}