blob: 637c6505dc00de1ce482335023bff8227aedaaa1 [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>
9#include <asm/current.h>
10#include <asm/pgtable.h>
11#include <asm/tlbflush.h>
Jeff Dikeeb830752007-05-06 14:51:07 -070012#include "arch.h"
Jeff Dike4c9e1382007-10-16 01:26:54 -070013#include "as-layout.h"
14#include "kern_util.h"
15#include "os.h"
Jeff Diked83ecf02008-02-04 22:30:47 -080016#include "skas.h"
Gennady Sharapovc66fdd52006-01-08 01:01:32 -080017#include "sysdep/sigcontext.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;
33
34 *code_out = SEGV_MAPERR;
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070035
Jeff Dike4c9e1382007-10-16 01:26:54 -070036 /*
37 * If the fault was during atomic operation, don't take the fault, just
38 * fail.
39 */
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070040 if (in_atomic())
41 goto out_nosemaphore;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 down_read(&mm->mmap_sem);
44 vma = find_vma(mm, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070045 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070047 else if (vma->vm_start <= address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 goto good_area;
Jeff Dike4c9e1382007-10-16 01:26:54 -070049 else if (!(vma->vm_flags & VM_GROWSDOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070051 else if (is_user && !ARCH_IS_STACKGROW(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070053 else if (expand_stack(vma, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 goto out;
55
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070056good_area:
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *code_out = SEGV_ACCERR;
Jeff Dike4c9e1382007-10-16 01:26:54 -070058 if (is_write && !(vma->vm_flags & VM_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070060
Paolo 'Blaisorblade' Giarrussod129f312005-09-10 19:44:57 +020061 /* Don't require VM_READ|VM_EXEC for write faults! */
Jeff Dike4c9e1382007-10-16 01:26:54 -070062 if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
Jeff Dike5d864562007-05-06 14:51:24 -070063 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 do {
Nick Piggin83c54072007-07-19 01:47:05 -070066 int fault;
Nick Piggin1c0fe6e2009-01-06 14:38:59 -080067
Linus Torvaldsd06063c2009-04-10 09:01:23 -070068 fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -070069 if (unlikely(fault & VM_FAULT_ERROR)) {
70 if (fault & VM_FAULT_OOM) {
Nick Piggin83c54072007-07-19 01:47:05 -070071 goto out_of_memory;
72 } else if (fault & VM_FAULT_SIGBUS) {
73 err = -EACCES;
74 goto out;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 BUG();
77 }
Nick Piggin83c54072007-07-19 01:47:05 -070078 if (fault & VM_FAULT_MAJOR)
79 current->maj_flt++;
80 else
81 current->min_flt++;
82
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070083 pgd = pgd_offset(mm, address);
84 pud = pud_offset(pgd, address);
85 pmd = pmd_offset(pud, address);
86 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070087 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -070089 /*
90 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -080091 * pte_mkyoung(); if (is_write) pte_mkdirty();
92 * If it's triggered, we'd see normally a hang here (a clean pte is
93 * marked read-only to emulate the dirty bit).
94 * However, the generic code can mark a PTE writable but clean on a
95 * concurrent read fault, triggering this harmlessly. So comment it out.
96 */
97#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +020098 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -080099#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700100 flush_tlb_page(vma, address);
101out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700103out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106out_of_memory:
Nick Piggin1c0fe6e2009-01-06 14:38:59 -0800107 /*
108 * We ran out of memory, call the OOM killer, and return the userspace
109 * (which will retry the fault, or kill us if we got oom-killed).
110 */
111 up_read(&mm->mmap_sem);
112 pagefault_out_of_memory();
113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800116static void bad_segv(struct faultinfo fi, unsigned long ip)
117{
118 struct siginfo si;
119
120 si.si_signo = SIGSEGV;
121 si.si_code = SEGV_ACCERR;
122 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
123 current->thread.arch.faultinfo = fi;
124 force_sig_info(SIGSEGV, &si, current);
125}
126
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800127void fatal_sigsegv(void)
128{
129 force_sigsegv(SIGSEGV, current);
130 do_signal();
131 /*
132 * This is to tell gcc that we're not returning - do_signal
133 * can, in general, return, but in this case, it's not, since
134 * we just got a fatal SIGSEGV queued.
135 */
136 os_dump_core();
137}
138
Jeff Dikeedea1382008-02-04 22:30:46 -0800139void segv_handler(int sig, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800140{
141 struct faultinfo * fi = UPT_FAULTINFO(regs);
142
Jeff Dike4c9e1382007-10-16 01:26:54 -0700143 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800144 bad_segv(*fi, UPT_IP(regs));
145 return;
146 }
147 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
148}
149
Bodo Stroesserc5784552005-05-05 16:15:31 -0700150/*
151 * We give a *copy* of the faultinfo in the regs to segv.
152 * This must be done, since nesting SEGVs could overwrite
153 * the info in the regs. A pointer to the info then would
154 * give us bad data!
155 */
Jeff Dike5d864562007-05-06 14:51:24 -0700156unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700157 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700160 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700162 int is_write = FAULT_WRITE(fi);
163 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jeff Dike4c9e1382007-10-16 01:26:54 -0700165 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700166 flush_tlb_kernel_vm();
167 return 0;
168 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700169 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700170 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700171 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700172 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700173
Paolo 'Blaisorblade' Giarrussobe662a12005-09-30 11:58:59 -0700174 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700175 err = handle_page_fault(address, ip, is_write, is_user,
176 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700177 else {
178 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700179 /*
180 * A thread accessed NULL, we get a fault, but CR2 is invalid.
181 * This code is used in __do_copy_from_user() of TT mode.
182 * XXX tt mode is gone, so maybe this isn't needed any more
183 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700184 address = 0;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700188 if (!err)
Jeff Dike5d864562007-05-06 14:51:24 -0700189 return 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700190 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700192 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700193 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700194 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700196 else if (!is_user && arch_fixup(ip, regs))
Jeff Dike5d864562007-05-06 14:51:24 -0700197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jeff Dike4c9e1382007-10-16 01:26:54 -0700199 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700200 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700201 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700205 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 si.si_signo = SIGBUS;
207 si.si_errno = 0;
208 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800209 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700210 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700212 } else {
213 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800215 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700216 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 force_sig_info(SIGSEGV, &si, current);
218 }
Jeff Dike5d864562007-05-06 14:51:24 -0700219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Jeff Dike77bf4402007-10-16 01:26:58 -0700222void relay_signal(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700224 if (!UPT_IS_USER(regs)) {
225 if (sig == SIGBUS)
226 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
227 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700229 }
230
Jeff Dike9226b832008-02-04 22:30:40 -0800231 arch_examine_signal(sig, regs);
232
Jeff Dike5d864562007-05-06 14:51:24 -0700233 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 force_sig(sig, current);
235}
236
Jeff Dikeedea1382008-02-04 22:30:46 -0800237void bus_handler(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700239 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700240 UML_LONGJMP(current->thread.fault_catcher, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 else relay_signal(sig, regs);
242}
243
Jeff Dikeedea1382008-02-04 22:30:46 -0800244void winch(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 do_IRQ(WINCH_IRQ, regs);
247}
248
249void trap_init(void)
250{
251}