blob: 9d08c71a967ed2e1e86189272369f4b794b8453c [file] [log] [blame]
Alexander Beregalov071327e2009-04-03 01:49:22 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 *
7 * Copyright (C) 1995, 1996, 1997, 1998 by Ralf Baechle
8 * Copyright 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
9 * Copyright 1999 Hewlett Packard Co.
10 *
11 */
12
13#include <linux/mm.h>
14#include <linux/ptrace.h>
15#include <linux/sched.h>
16#include <linux/interrupt.h>
17#include <linux/module.h>
18
19#include <asm/uaccess.h>
20#include <asm/traps.h>
21
22#define PRINT_USER_FAULTS /* (turn this on if you want user faults to be */
23 /* dumped to the console via printk) */
24
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Various important other fields */
27#define bit22set(x) (x & 0x00000200)
28#define bits23_25set(x) (x & 0x000001c0)
29#define isGraphicsFlushRead(x) ((x & 0xfc003fdf) == 0x04001a80)
30 /* extended opcode is 0x6a */
31
32#define BITSSET 0x1c0 /* for identifying LDCW */
33
34
35DEFINE_PER_CPU(struct exception_data, exception_data);
36
37/*
38 * parisc_acctyp(unsigned int inst) --
39 * Given a PA-RISC memory access instruction, determine if the
40 * the instruction would perform a memory read or memory write
41 * operation.
42 *
43 * This function assumes that the given instruction is a memory access
44 * instruction (i.e. you should really only call it if you know that
45 * the instruction has generated some sort of a memory access fault).
46 *
47 * Returns:
48 * VM_READ if read operation
49 * VM_WRITE if write operation
50 * VM_EXEC if execute operation
51 */
52static unsigned long
53parisc_acctyp(unsigned long code, unsigned int inst)
54{
55 if (code == 6 || code == 16)
56 return VM_EXEC;
57
58 switch (inst & 0xf0000000) {
59 case 0x40000000: /* load */
60 case 0x50000000: /* new load */
61 return VM_READ;
62
63 case 0x60000000: /* store */
64 case 0x70000000: /* new store */
65 return VM_WRITE;
66
67 case 0x20000000: /* coproc */
68 case 0x30000000: /* coproc2 */
69 if (bit22set(inst))
70 return VM_WRITE;
71
72 case 0x0: /* indexed/memory management */
73 if (bit22set(inst)) {
74 /*
75 * Check for the 'Graphics Flush Read' instruction.
76 * It resembles an FDC instruction, except for bits
77 * 20 and 21. Any combination other than zero will
78 * utilize the block mover functionality on some
79 * older PA-RISC platforms. The case where a block
80 * move is performed from VM to graphics IO space
81 * should be treated as a READ.
82 *
83 * The significance of bits 20,21 in the FDC
84 * instruction is:
85 *
86 * 00 Flush data cache (normal instruction behavior)
87 * 01 Graphics flush write (IO space -> VM)
88 * 10 Graphics flush read (VM -> IO space)
89 * 11 Graphics flush read/write (VM <-> IO space)
90 */
91 if (isGraphicsFlushRead(inst))
92 return VM_READ;
93 return VM_WRITE;
94 } else {
95 /*
96 * Check for LDCWX and LDCWS (semaphore instructions).
97 * If bits 23 through 25 are all 1's it is one of
98 * the above two instructions and is a write.
99 *
100 * Note: With the limited bits we are looking at,
101 * this will also catch PROBEW and PROBEWI. However,
102 * these should never get in here because they don't
103 * generate exceptions of the type:
104 * Data TLB miss fault/data page fault
105 * Data memory protection trap
106 */
107 if (bits23_25set(inst) == BITSSET)
108 return VM_WRITE;
109 }
110 return VM_READ; /* Default */
111 }
112 return VM_READ; /* Default */
113}
114
115#undef bit22set
116#undef bits23_25set
117#undef isGraphicsFlushRead
118#undef BITSSET
119
120
121#if 0
122/* This is the treewalk to find a vma which is the highest that has
123 * a start < addr. We're using find_vma_prev instead right now, but
124 * we might want to use this at some point in the future. Probably
125 * not, but I want it committed to CVS so I don't lose it :-)
126 */
127 while (tree != vm_avl_empty) {
128 if (tree->vm_start > addr) {
129 tree = tree->vm_avl_left;
130 } else {
131 prev = tree;
132 if (prev->vm_next == NULL)
133 break;
134 if (prev->vm_next->vm_start > addr)
135 break;
136 tree = tree->vm_avl_right;
137 }
138 }
139#endif
140
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000141int fixup_exception(struct pt_regs *regs)
142{
143 const struct exception_table_entry *fix;
144
Helge Deller61dbbae2013-10-13 21:11:30 +0200145 /* If we only stored 32bit addresses in the exception table we can drop
146 * out if we faulted on a 64bit address. */
147 if ((sizeof(regs->iaoq[0]) > sizeof(fix->insn))
148 && (regs->iaoq[0] >> 32))
149 return 0;
150
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000151 fix = search_exception_tables(regs->iaoq[0]);
152 if (fix) {
153 struct exception_data *d;
154 d = &__get_cpu_var(exception_data);
155 d->fault_ip = regs->iaoq[0];
156 d->fault_space = regs->isr;
157 d->fault_addr = regs->ior;
158
159 regs->iaoq[0] = ((fix->fixup) & ~3);
160 /*
161 * NOTE: In some cases the faulting instruction
162 * may be in the delay slot of a branch. We
163 * don't want to take the branch, so we don't
164 * increment iaoq[1], instead we set it to be
165 * iaoq[0]+4, and clear the B bit in the PSW
166 */
167 regs->iaoq[1] = regs->iaoq[0] + 4;
168 regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
169
170 return 1;
171 }
172
173 return 0;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176void do_page_fault(struct pt_regs *regs, unsigned long code,
177 unsigned long address)
178{
179 struct vm_area_struct *vma, *prev_vma;
John David Anglin2d8b22d2013-10-05 10:55:36 -0400180 struct task_struct *tsk;
181 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long acc_type;
Nick Piggin83c54072007-07-19 01:47:05 -0700183 int fault;
John David Anglin2d8b22d2013-10-05 10:55:36 -0400184 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
John David Anglin2d8b22d2013-10-05 10:55:36 -0400186 if (in_atomic())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto no_context;
188
John David Anglin2d8b22d2013-10-05 10:55:36 -0400189 tsk = current;
190 mm = tsk->mm;
191 if (!mm)
192 goto no_context;
193
194 flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Johannes Weiner759496b2013-09-12 15:13:39 -0700195 if (user_mode(regs))
196 flags |= FAULT_FLAG_USER;
Felipe Pena0772dac2013-09-30 13:45:14 -0700197
198 acc_type = parisc_acctyp(code, regs->iir);
Johannes Weiner759496b2013-09-12 15:13:39 -0700199 if (acc_type & VM_WRITE)
200 flags |= FAULT_FLAG_WRITE;
Kautuk Consul38057472012-03-20 09:26:53 -0400201retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 down_read(&mm->mmap_sem);
203 vma = find_vma_prev(mm, address, &prev_vma);
204 if (!vma || address < vma->vm_start)
205 goto check_expansion;
206/*
207 * Ok, we have a good vm_area for this memory access. We still need to
208 * check the access permissions.
209 */
210
211good_area:
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if ((vma->vm_flags & acc_type) != acc_type)
214 goto bad_area;
215
216 /*
217 * If for any reason at all we couldn't handle the fault, make
218 * sure we exit gracefully rather than endlessly redo the
219 * fault.
220 */
221
Johannes Weiner759496b2013-09-12 15:13:39 -0700222 fault = handle_mm_fault(mm, vma, address, flags);
Kautuk Consul38057472012-03-20 09:26:53 -0400223
224 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
225 return;
226
Nick Piggin83c54072007-07-19 01:47:05 -0700227 if (unlikely(fault & VM_FAULT_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
Helge Deller67a5a592006-03-27 19:52:14 +0000229 * We hit a shared mapping outside of the file, or some
Linus Torvalds6e346222005-08-04 08:33:38 -0700230 * other thing happened to us that made us unable to
231 * handle the page fault gracefully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Nick Piggin83c54072007-07-19 01:47:05 -0700233 if (fault & VM_FAULT_OOM)
234 goto out_of_memory;
235 else if (fault & VM_FAULT_SIGBUS)
236 goto bad_area;
237 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Kautuk Consul38057472012-03-20 09:26:53 -0400239 if (flags & FAULT_FLAG_ALLOW_RETRY) {
240 if (fault & VM_FAULT_MAJOR)
241 current->maj_flt++;
242 else
243 current->min_flt++;
244 if (fault & VM_FAULT_RETRY) {
245 flags &= ~FAULT_FLAG_ALLOW_RETRY;
246
247 /*
248 * No need to up_read(&mm->mmap_sem) as we would
249 * have already released it in __lock_page_or_retry
250 * in mm/filemap.c.
251 */
252
253 goto retry;
254 }
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 up_read(&mm->mmap_sem);
257 return;
258
259check_expansion:
260 vma = prev_vma;
261 if (vma && (expand_stack(vma, address) == 0))
262 goto good_area;
263
264/*
265 * Something tried to access memory that isn't in our memory map..
266 */
267bad_area:
268 up_read(&mm->mmap_sem);
269
270 if (user_mode(regs)) {
271 struct siginfo si;
272
273#ifdef PRINT_USER_FAULTS
274 printk(KERN_DEBUG "\n");
275 printk(KERN_DEBUG "do_page_fault() pid=%d command='%s' type=%lu address=0x%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700276 task_pid_nr(tsk), tsk->comm, code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (vma) {
278 printk(KERN_DEBUG "vm_start = 0x%08lx, vm_end = 0x%08lx\n",
279 vma->vm_start, vma->vm_end);
280 }
281 show_regs(regs);
282#endif
Helge Deller1f2048f2013-11-07 17:08:36 +0100283 switch (code) {
284 case 15: /* Data TLB miss fault/Data page fault */
Helge Deller49d1cb22013-11-18 22:12:11 +0100285 /* send SIGSEGV when outside of vma */
286 if (!vma ||
287 address < vma->vm_start || address > vma->vm_end) {
288 si.si_signo = SIGSEGV;
289 si.si_code = SEGV_MAPERR;
290 break;
291 }
292
293 /* send SIGSEGV for wrong permissions */
294 if ((vma->vm_flags & acc_type) != acc_type) {
295 si.si_signo = SIGSEGV;
296 si.si_code = SEGV_ACCERR;
297 break;
298 }
299
300 /* probably address is outside of mapped file */
301 /* fall through */
Helge Deller1f2048f2013-11-07 17:08:36 +0100302 case 17: /* NA data TLB miss / page fault */
303 case 18: /* Unaligned access - PCXS only */
304 si.si_signo = SIGBUS;
Helge Deller49d1cb22013-11-18 22:12:11 +0100305 si.si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
Helge Deller1f2048f2013-11-07 17:08:36 +0100306 break;
307 case 16: /* Non-access instruction TLB miss fault */
308 case 26: /* PCXL: Data memory access rights trap */
309 default:
310 si.si_signo = SIGSEGV;
Helge Deller49d1cb22013-11-18 22:12:11 +0100311 si.si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
312 break;
Helge Deller1f2048f2013-11-07 17:08:36 +0100313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 si.si_errno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 si.si_addr = (void __user *) address;
Helge Deller1f2048f2013-11-07 17:08:36 +0100316 force_sig_info(si.si_signo, &si, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return;
318 }
319
320no_context:
321
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000322 if (!user_mode(regs) && fixup_exception(regs)) {
323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
327
328 out_of_memory:
329 up_read(&mm->mmap_sem);
Nick Piggin53e30d02010-04-22 16:06:23 +0000330 if (!user_mode(regs))
331 goto no_context;
332 pagefault_out_of_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}