blob: 4b472ca5348564d5942800647cb3b310b05c1e4d [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"
Gennady Sharapovc66fdd52006-01-08 01:01:32 -080016#include "sysdep/sigcontext.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Jeff Dike4c9e1382007-10-16 01:26:54 -070018/*
19 * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by
20 * segv().
21 */
Jeff Dike1d3468a2006-07-10 04:45:13 -070022int handle_page_fault(unsigned long address, unsigned long ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 int is_write, int is_user, int *code_out)
24{
25 struct mm_struct *mm = current->mm;
26 struct vm_area_struct *vma;
27 pgd_t *pgd;
28 pud_t *pud;
29 pmd_t *pmd;
30 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 int err = -EFAULT;
32
33 *code_out = SEGV_MAPERR;
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070034
Jeff Dike4c9e1382007-10-16 01:26:54 -070035 /*
36 * If the fault was during atomic operation, don't take the fault, just
37 * fail.
38 */
Paolo 'Blaisorblade' Giarrussofea03cb2005-09-22 21:44:20 -070039 if (in_atomic())
40 goto out_nosemaphore;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 down_read(&mm->mmap_sem);
43 vma = find_vma(mm, address);
Jeff Dike4c9e1382007-10-16 01:26:54 -070044 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070046 else if (vma->vm_start <= address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 goto good_area;
Jeff Dike4c9e1382007-10-16 01:26:54 -070048 else if (!(vma->vm_flags & VM_GROWSDOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070050 else if (is_user && !ARCH_IS_STACKGROW(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 goto out;
Jeff Dike4c9e1382007-10-16 01:26:54 -070052 else if (expand_stack(vma, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 goto out;
54
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070055good_area:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *code_out = SEGV_ACCERR;
Jeff Dike4c9e1382007-10-16 01:26:54 -070057 if (is_write && !(vma->vm_flags & VM_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070059
Paolo 'Blaisorblade' Giarrussod129f312005-09-10 19:44:57 +020060 /* Don't require VM_READ|VM_EXEC for write faults! */
Jeff Dike4c9e1382007-10-16 01:26:54 -070061 if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
Jeff Dike5d864562007-05-06 14:51:24 -070062 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 do {
Nick Piggin83c54072007-07-19 01:47:05 -070065 int fault;
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -070066survive:
Nick Piggin83c54072007-07-19 01:47:05 -070067 fault = handle_mm_fault(mm, vma, address, is_write);
68 if (unlikely(fault & VM_FAULT_ERROR)) {
69 if (fault & VM_FAULT_OOM) {
70 err = -ENOMEM;
71 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
106/*
107 * We ran out of memory, or some other thing happened to us that made
108 * us unable to handle the page fault gracefully.
109 */
110out_of_memory:
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700111 if (is_init(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 up_read(&mm->mmap_sem);
113 yield();
114 down_read(&mm->mmap_sem);
115 goto survive;
116 }
117 goto out;
118}
119
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800120static void bad_segv(struct faultinfo fi, unsigned long ip)
121{
122 struct siginfo si;
123
124 si.si_signo = SIGSEGV;
125 si.si_code = SEGV_ACCERR;
126 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
127 current->thread.arch.faultinfo = fi;
128 force_sig_info(SIGSEGV, &si, current);
129}
130
131static void segv_handler(int sig, union uml_pt_regs *regs)
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800132{
133 struct faultinfo * fi = UPT_FAULTINFO(regs);
134
Jeff Dike4c9e1382007-10-16 01:26:54 -0700135 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
Gennady Sharapovc66fdd52006-01-08 01:01:32 -0800136 bad_segv(*fi, UPT_IP(regs));
137 return;
138 }
139 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
140}
141
Bodo Stroesserc5784552005-05-05 16:15:31 -0700142/*
143 * We give a *copy* of the faultinfo in the regs to segv.
144 * This must be done, since nesting SEGVs could overwrite
145 * the info in the regs. A pointer to the info then would
146 * give us bad data!
147 */
Jeff Dike5d864562007-05-06 14:51:24 -0700148unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
149 union uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct siginfo si;
152 void *catcher;
153 int err;
Jeff Dike5d864562007-05-06 14:51:24 -0700154 int is_write = FAULT_WRITE(fi);
155 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Jeff Dike4c9e1382007-10-16 01:26:54 -0700157 if (!is_user && (address >= start_vm) && (address < end_vm)) {
Jeff Dike5d864562007-05-06 14:51:24 -0700158 flush_tlb_kernel_vm();
159 return 0;
160 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700161 else if (current->mm == NULL) {
Jeff Dike377fad32007-05-06 14:51:25 -0700162 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike4c9e1382007-10-16 01:26:54 -0700163 panic("Segfault with no mm");
Jeff Dike377fad32007-05-06 14:51:25 -0700164 }
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700165
Paolo 'Blaisorblade' Giarrussobe662a12005-09-30 11:58:59 -0700166 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
Jeff Dike4c9e1382007-10-16 01:26:54 -0700167 err = handle_page_fault(address, ip, is_write, is_user,
168 &si.si_code);
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700169 else {
170 err = -EFAULT;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700171 /*
172 * A thread accessed NULL, we get a fault, but CR2 is invalid.
173 * This code is used in __do_copy_from_user() of TT mode.
174 * XXX tt mode is gone, so maybe this isn't needed any more
175 */
Paolo 'Blaisorblade' Giarrusso546fe1c2005-09-22 21:44:16 -0700176 address = 0;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 catcher = current->thread.fault_catcher;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700180 if (!err)
Jeff Dike5d864562007-05-06 14:51:24 -0700181 return 0;
Jeff Dike4c9e1382007-10-16 01:26:54 -0700182 else if (catcher != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 current->thread.fault_addr = (void *) address;
184 do_longjmp(catcher, 1);
Jeff Dike1d3468a2006-07-10 04:45:13 -0700185 }
Jeff Dike4c9e1382007-10-16 01:26:54 -0700186 else if (current->thread.fault_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 panic("fault_addr set but no fault catcher");
Jeff Dike4c9e1382007-10-16 01:26:54 -0700188 else if (!is_user && arch_fixup(ip, regs))
Jeff Dike5d864562007-05-06 14:51:24 -0700189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Jeff Dike4c9e1382007-10-16 01:26:54 -0700191 if (!is_user) {
Jeff Dike377fad32007-05-06 14:51:25 -0700192 show_regs(container_of(regs, struct pt_regs, regs));
Jeff Dike1d3468a2006-07-10 04:45:13 -0700193 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 address, ip);
Jeff Dike377fad32007-05-06 14:51:25 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700197 if (err == -EACCES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 si.si_signo = SIGBUS;
199 si.si_errno = 0;
200 si.si_code = BUS_ADRERR;
Al Viro4d338e12006-03-31 02:30:15 -0800201 si.si_addr = (void __user *)address;
Jeff Dike5d864562007-05-06 14:51:24 -0700202 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 force_sig_info(SIGBUS, &si, current);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700204 } else if (err == -ENOMEM) {
Jeff Dike4c9e1382007-10-16 01:26:54 -0700205 printk(KERN_INFO "VM: killing process %s\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 do_exit(SIGKILL);
Paolo 'Blaisorblade' Giarrusso3b521662005-09-03 15:57:26 -0700207 } else {
208 BUG_ON(err != -EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 si.si_signo = SIGSEGV;
Al Viro4d338e12006-03-31 02:30:15 -0800210 si.si_addr = (void __user *) address;
Jeff Dike5d864562007-05-06 14:51:24 -0700211 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 force_sig_info(SIGSEGV, &si, current);
213 }
Jeff Dike5d864562007-05-06 14:51:24 -0700214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217void relay_signal(int sig, union uml_pt_regs *regs)
218{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700219 if (arch_handle_signal(sig, regs))
Jeff Dike6edf4282006-09-25 23:33:03 -0700220 return;
221
Jeff Dike4c9e1382007-10-16 01:26:54 -0700222 if (!UPT_IS_USER(regs)) {
223 if (sig == SIGBUS)
224 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
225 "mount likely just ran out of space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 panic("Kernel mode signal %d", sig);
Jeff Dike6edf4282006-09-25 23:33:03 -0700227 }
228
Jeff Dike5d864562007-05-06 14:51:24 -0700229 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 force_sig(sig, current);
231}
232
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800233static void bus_handler(int sig, union uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Jeff Dike4c9e1382007-10-16 01:26:54 -0700235 if (current->thread.fault_catcher != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 do_longjmp(current->thread.fault_catcher, 1);
237 else relay_signal(sig, regs);
238}
239
Jeff Dike27aa6ef2007-02-10 01:44:14 -0800240static void winch(int sig, union uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 do_IRQ(WINCH_IRQ, regs);
243}
244
Jeff Dike53dd2b52006-09-27 01:50:37 -0700245const struct kern_handlers handlinfo_kern = {
246 .relay_signal = relay_signal,
247 .winch = winch,
248 .bus_handler = bus_handler,
249 .page_fault = segv_handler,
250 .sigio_handler = sigio_handler,
251 .timer_handler = timer_handler
252};
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254void trap_init(void)
255{
256}