blob: 44e4904194951f680f187fd4f6137ca289b9cab8 [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;
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070067survive:
Nick Piggin83c54072007-07-19 01:47:05 -070068 fault = handle_mm_fault(mm, vma, address, is_write);
69 if (unlikely(fault & VM_FAULT_ERROR)) {
70 if (fault & VM_FAULT_OOM) {
71 err = -ENOMEM;
72 goto out_of_memory;
73 } else if (fault & VM_FAULT_SIGBUS) {
74 err = -EACCES;
75 goto out;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 BUG();
78 }
Nick Piggin83c54072007-07-19 01:47:05 -070079 if (fault & VM_FAULT_MAJOR)
80 current->maj_flt++;
81 else
82 current->min_flt++;
83
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070084 pgd = pgd_offset(mm, address);
85 pud = pud_offset(pgd, address);
86 pmd = pmd_offset(pud, address);
87 pte = pte_offset_kernel(pmd, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070088 } while (!pte_present(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 err = 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -070090 /*
91 * The below warning was added in place of
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -080092 * pte_mkyoung(); if (is_write) pte_mkdirty();
93 * If it's triggered, we'd see normally a hang here (a clean pte is
94 * marked read-only to emulate the dirty bit).
95 * However, the generic code can mark a PTE writable but clean on a
96 * concurrent read fault, triggering this harmlessly. So comment it out.
97 */
98#if 0
Paolo 'Blaisorblade' Giarrusso16b03672005-09-10 19:44:58 +020099 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
Paolo 'Blaisorblade' Giarrussocbc24af2005-11-13 16:07:04 -0800100#endif
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700101 flush_tlb_page(vma, address);
102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 up_read(&mm->mmap_sem);
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -0700104out_nosemaphore:
Jeff Dike4c9e1382007-10-16 01:26:54 -0700105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * We ran out of memory, or some other thing happened to us that made
109 * us unable to handle the page fault gracefully.
110 */
111out_of_memory:
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700112 if (is_global_init(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 up_read(&mm->mmap_sem);
114 yield();
115 down_read(&mm->mmap_sem);
116 goto survive;
117 }
118 goto out;
119}
120
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800121static void bad_segv(struct faultinfo fi, unsigned long ip)
122{
123 struct siginfo si;
124
125 si.si_signo = SIGSEGV;
126 si.si_code = SEGV_ACCERR;
127 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
128 current->thread.arch.faultinfo = fi;
129 force_sig_info(SIGSEGV, &si, current);
130}
131
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800132void fatal_sigsegv(void)
133{
134 force_sigsegv(SIGSEGV, current);
135 do_signal();
136 /*
137 * This is to tell gcc that we're not returning - do_signal
138 * can, in general, return, but in this case, it's not, since
139 * we just got a fatal SIGSEGV queued.
140 */
141 os_dump_core();
142}
143
Jeff Dikeedea1382008-02-04 22:30:46 -0800144void segv_handler(int sig, struct uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800145{
146 struct faultinfo * fi = UPT_FAULTINFO(regs);
147
Jeff Dike4c9e1382007-10-16 01:26:54 -0700148 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800149 bad_segv(*fi, UPT_IP(regs));
150 return;
151 }
152 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
153}
154
Bodo Stroesserc5784552005-05-05 16:15:31 -0700155/*
156 * We give a *copy* of the faultinfo in the regs to segv.
157 * This must be done, since nesting SEGVs could overwrite
158 * the info in the regs. A pointer to the info then would
159 * give us bad data!
160 */
Jeff Dike5d864562007-05-06 14:51:24 -0700161unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
Jeff Dike77bf4402007-10-16 01:26:58 -0700162 struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct siginfo si;
Jeff Dikefab95c52007-10-16 01:27:05 -0700165 jmp_buf *catcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700167 int is_write = FAULT_WRITE(fi);
168 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jeff Dike4c9e1382007-10-16 01:26:54 -0700170 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700171 flush_tlb_kernel_vm();
172 return 0;
173 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700174 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700175 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700176 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700177 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700178
Paolo 'Blaisorblade' Giarrussobe662a12005-09-30 11:58:59 -0700179 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700180 err = handle_page_fault(address, ip, is_write, is_user,
181 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700182 else {
183 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700184 /*
185 * A thread accessed NULL, we get a fault, but CR2 is invalid.
186 * This code is used in __do_copy_from_user() of TT mode.
187 * XXX tt mode is gone, so maybe this isn't needed any more
188 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700189 address = 0;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700193 if (!err)
Jeff Dike5d864562007-05-06 14:51:24 -0700194 return 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700195 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 current->thread.fault_addr = (void *) address;
Jeff Dikefab95c52007-10-16 01:27:05 -0700197 UML_LONGJMP(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700198 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700199 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700201 else if (!is_user && arch_fixup(ip, regs))
Jeff Dike5d864562007-05-06 14:51:24 -0700202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jeff Dike4c9e1382007-10-16 01:26:54 -0700204 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700205 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700206 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700210 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 si.si_signo = SIGBUS;
212 si.si_errno = 0;
213 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800214 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700215 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700217 } else if (err == -ENOMEM) {
Jeff Dike4c9e1382007-10-16 01:26:54 -0700218 printk(KERN_INFO "VM: killing process %s\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 do_exit(SIGKILL);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700220 } else {
221 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800223 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700224 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 force_sig_info(SIGSEGV, &si, current);
226 }
Jeff Dike5d864562007-05-06 14:51:24 -0700227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Jeff Dike77bf4402007-10-16 01:26:58 -0700230void relay_signal(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700232 if (!UPT_IS_USER(regs)) {
233 if (sig == SIGBUS)
234 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
235 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700237 }
238
Jeff Dike9226b832008-02-04 22:30:40 -0800239 arch_examine_signal(sig, regs);
240
Jeff Dike5d864562007-05-06 14:51:24 -0700241 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 force_sig(sig, current);
243}
244
Jeff Dikeedea1382008-02-04 22:30:46 -0800245void bus_handler(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700247 if (current->thread.fault_catcher != NULL)
Jeff Dikefab95c52007-10-16 01:27:05 -0700248 UML_LONGJMP(current->thread.fault_catcher, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 else relay_signal(sig, regs);
250}
251
Jeff Dikeedea1382008-02-04 22:30:46 -0800252void winch(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 do_IRQ(WINCH_IRQ, regs);
255}
256
257void trap_init(void)
258{
259}