blob: 5eecab1a84efd3430deab218698ab3dab91c0e6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/alpha/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/sched.h>
8#include <linux/kernel.h>
9#include <linux/mm.h>
10#include <asm/io.h>
11
12#define __EXTERN_INLINE inline
13#include <asm/mmu_context.h>
14#include <asm/tlbflush.h>
15#undef __EXTERN_INLINE
16
17#include <linux/signal.h>
18#include <linux/errno.h>
19#include <linux/string.h>
20#include <linux/types.h>
21#include <linux/ptrace.h>
22#include <linux/mman.h>
23#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/module.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/uaccess.h>
28
29extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
30
31
32/*
33 * Force a new ASN for a task.
34 */
35
36#ifndef CONFIG_SMP
37unsigned long last_asn = ASN_FIRST_VERSION;
38#endif
39
40void
41__load_new_mm_context(struct mm_struct *next_mm)
42{
43 unsigned long mmc;
44 struct pcb_struct *pcb;
45
46 mmc = __get_new_mm_context(next_mm, smp_processor_id());
47 next_mm->context[smp_processor_id()] = mmc;
48
49 pcb = &current_thread_info()->pcb;
50 pcb->asn = mmc & HARDWARE_ASN_MASK;
51 pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
52
53 __reload_thread(pcb);
54}
55
56
57/*
58 * This routine handles page faults. It determines the address,
59 * and the problem, and then passes it off to handle_mm_fault().
60 *
61 * mmcsr:
62 * 0 = translation not valid
63 * 1 = access violation
64 * 2 = fault-on-read
65 * 3 = fault-on-execute
66 * 4 = fault-on-write
67 *
68 * cause:
69 * -1 = instruction fetch
70 * 0 = load
71 * 1 = store
72 *
73 * Registers $9 through $15 are saved in a block just prior to `regs' and
74 * are saved and restored around the call to allow exception code to
75 * modify them.
76 */
77
78/* Macro for exception fixup code to access integer registers. */
79#define dpf_reg(r) \
80 (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 : \
81 (r) <= 18 ? (r)+8 : (r)-10])
82
83asmlinkage void
84do_page_fault(unsigned long address, unsigned long mmcsr,
85 long cause, struct pt_regs *regs)
86{
87 struct vm_area_struct * vma;
88 struct mm_struct *mm = current->mm;
89 const struct exception_table_entry *fixup;
90 int fault, si_code = SEGV_MAPERR;
91 siginfo_t info;
92
93 /* As of EV6, a load into $31/$f31 is a prefetch, and never faults
94 (or is suppressed by the PALcode). Support that for older CPUs
95 by ignoring such an instruction. */
96 if (cause == 0) {
97 unsigned int insn;
98 __get_user(insn, (unsigned int __user *)regs->pc);
99 if ((insn >> 21 & 0x1f) == 0x1f &&
100 /* ldq ldl ldt lds ldg ldf ldwu ldbu */
101 (1ul << (insn >> 26) & 0x30f00001400ul)) {
102 regs->pc += 4;
103 return;
104 }
105 }
106
107 /* If we're in an interrupt context, or have no user context,
108 we must not take the fault. */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800109 if (!mm || in_atomic())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto no_context;
111
112#ifdef CONFIG_ALPHA_LARGE_VMALLOC
113 if (address >= TASK_SIZE)
114 goto vmalloc_fault;
115#endif
116
117 down_read(&mm->mmap_sem);
118 vma = find_vma(mm, address);
119 if (!vma)
120 goto bad_area;
121 if (vma->vm_start <= address)
122 goto good_area;
123 if (!(vma->vm_flags & VM_GROWSDOWN))
124 goto bad_area;
125 if (expand_stack(vma, address))
126 goto bad_area;
127
128 /* Ok, we have a good vm_area for this memory access, so
129 we can handle it. */
130 good_area:
131 si_code = SEGV_ACCERR;
132 if (cause < 0) {
133 if (!(vma->vm_flags & VM_EXEC))
134 goto bad_area;
135 } else if (!cause) {
136 /* Allow reads even for write-only mappings */
137 if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
138 goto bad_area;
139 } else {
140 if (!(vma->vm_flags & VM_WRITE))
141 goto bad_area;
142 }
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* If for any reason at all we couldn't handle the fault,
145 make sure we exit gracefully rather than endlessly redo
146 the fault. */
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700147 fault = handle_mm_fault(mm, vma, address, cause > 0 ? FAULT_FLAG_WRITE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 up_read(&mm->mmap_sem);
Nick Piggin83c54072007-07-19 01:47:05 -0700149 if (unlikely(fault & VM_FAULT_ERROR)) {
150 if (fault & VM_FAULT_OOM)
151 goto out_of_memory;
152 else if (fault & VM_FAULT_SIGBUS)
153 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 BUG();
155 }
Nick Piggin83c54072007-07-19 01:47:05 -0700156 if (fault & VM_FAULT_MAJOR)
157 current->maj_flt++;
158 else
159 current->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return;
161
162 /* Something tried to access memory that isn't in our memory map.
163 Fix it, but check if it's kernel or user first. */
164 bad_area:
165 up_read(&mm->mmap_sem);
166
167 if (user_mode(regs))
168 goto do_sigsegv;
169
170 no_context:
171 /* Are we prepared to handle this fault as an exception? */
172 if ((fixup = search_exception_tables(regs->pc)) != 0) {
173 unsigned long newpc;
174 newpc = fixup_exception(dpf_reg, fixup, regs->pc);
175 regs->pc = newpc;
176 return;
177 }
178
179 /* Oops. The kernel tried to access some bad page. We'll have to
180 terminate things with extreme prejudice. */
181 printk(KERN_ALERT "Unable to handle kernel paging request at "
182 "virtual address %016lx\n", address);
183 die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
184 do_exit(SIGKILL);
185
186 /* We ran out of memory, or some other thing happened to us that
187 made us unable to handle the page fault gracefully. */
188 out_of_memory:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!user_mode(regs))
190 goto no_context;
Nick Piggin1cb3d8e2010-04-29 17:48:18 -0400191 pagefault_out_of_memory();
192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 do_sigbus:
195 /* Send a sigbus, regardless of whether we were in kernel
196 or user mode. */
197 info.si_signo = SIGBUS;
198 info.si_errno = 0;
199 info.si_code = BUS_ADRERR;
200 info.si_addr = (void __user *) address;
201 force_sig_info(SIGBUS, &info, current);
202 if (!user_mode(regs))
203 goto no_context;
204 return;
205
206 do_sigsegv:
207 info.si_signo = SIGSEGV;
208 info.si_errno = 0;
209 info.si_code = si_code;
210 info.si_addr = (void __user *) address;
211 force_sig_info(SIGSEGV, &info, current);
212 return;
213
214#ifdef CONFIG_ALPHA_LARGE_VMALLOC
215 vmalloc_fault:
216 if (user_mode(regs))
217 goto do_sigsegv;
218 else {
219 /* Synchronize this task's top level page-table
220 with the "reference" page table from init. */
221 long index = pgd_index(address);
222 pgd_t *pgd, *pgd_k;
223
224 pgd = current->active_mm->pgd + index;
225 pgd_k = swapper_pg_dir + index;
226 if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
227 pgd_val(*pgd) = pgd_val(*pgd_k);
228 return;
229 }
230 goto no_context;
231 }
232#endif
233}