blob: d64f1c854fdf1ea2d7eadf8ace7b034281953b75 [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>
Paul Gortmakera38671d2016-09-19 18:04:51 -040017#include <linux/extable.h>
David Hildenbrand70ffdb92015-05-11 17:52:11 +020018#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/traps.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/* Various important other fields */
23#define bit22set(x) (x & 0x00000200)
24#define bits23_25set(x) (x & 0x000001c0)
25#define isGraphicsFlushRead(x) ((x & 0xfc003fdf) == 0x04001a80)
26 /* extended opcode is 0x6a */
27
28#define BITSSET 0x1c0 /* for identifying LDCW */
29
30
31DEFINE_PER_CPU(struct exception_data, exception_data);
32
Helge Dellerfef47e22014-05-05 18:07:12 +020033int show_unhandled_signals = 1;
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * parisc_acctyp(unsigned int inst) --
37 * Given a PA-RISC memory access instruction, determine if the
38 * the instruction would perform a memory read or memory write
39 * operation.
40 *
41 * This function assumes that the given instruction is a memory access
42 * instruction (i.e. you should really only call it if you know that
43 * the instruction has generated some sort of a memory access fault).
44 *
45 * Returns:
46 * VM_READ if read operation
47 * VM_WRITE if write operation
48 * VM_EXEC if execute operation
49 */
50static unsigned long
51parisc_acctyp(unsigned long code, unsigned int inst)
52{
53 if (code == 6 || code == 16)
54 return VM_EXEC;
55
56 switch (inst & 0xf0000000) {
57 case 0x40000000: /* load */
58 case 0x50000000: /* new load */
59 return VM_READ;
60
61 case 0x60000000: /* store */
62 case 0x70000000: /* new store */
63 return VM_WRITE;
64
65 case 0x20000000: /* coproc */
66 case 0x30000000: /* coproc2 */
67 if (bit22set(inst))
68 return VM_WRITE;
69
70 case 0x0: /* indexed/memory management */
71 if (bit22set(inst)) {
72 /*
73 * Check for the 'Graphics Flush Read' instruction.
74 * It resembles an FDC instruction, except for bits
75 * 20 and 21. Any combination other than zero will
76 * utilize the block mover functionality on some
77 * older PA-RISC platforms. The case where a block
78 * move is performed from VM to graphics IO space
79 * should be treated as a READ.
80 *
81 * The significance of bits 20,21 in the FDC
82 * instruction is:
83 *
84 * 00 Flush data cache (normal instruction behavior)
85 * 01 Graphics flush write (IO space -> VM)
86 * 10 Graphics flush read (VM -> IO space)
87 * 11 Graphics flush read/write (VM <-> IO space)
88 */
89 if (isGraphicsFlushRead(inst))
90 return VM_READ;
91 return VM_WRITE;
92 } else {
93 /*
94 * Check for LDCWX and LDCWS (semaphore instructions).
95 * If bits 23 through 25 are all 1's it is one of
96 * the above two instructions and is a write.
97 *
98 * Note: With the limited bits we are looking at,
99 * this will also catch PROBEW and PROBEWI. However,
100 * these should never get in here because they don't
101 * generate exceptions of the type:
102 * Data TLB miss fault/data page fault
103 * Data memory protection trap
104 */
105 if (bits23_25set(inst) == BITSSET)
106 return VM_WRITE;
107 }
108 return VM_READ; /* Default */
109 }
110 return VM_READ; /* Default */
111}
112
113#undef bit22set
114#undef bits23_25set
115#undef isGraphicsFlushRead
116#undef BITSSET
117
118
119#if 0
120/* This is the treewalk to find a vma which is the highest that has
121 * a start < addr. We're using find_vma_prev instead right now, but
122 * we might want to use this at some point in the future. Probably
123 * not, but I want it committed to CVS so I don't lose it :-)
124 */
125 while (tree != vm_avl_empty) {
126 if (tree->vm_start > addr) {
127 tree = tree->vm_avl_left;
128 } else {
129 prev = tree;
130 if (prev->vm_next == NULL)
131 break;
132 if (prev->vm_next->vm_start > addr)
133 break;
134 tree = tree->vm_avl_right;
135 }
136 }
137#endif
138
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000139int fixup_exception(struct pt_regs *regs)
140{
141 const struct exception_table_entry *fix;
142
143 fix = search_exception_tables(regs->iaoq[0]);
144 if (fix) {
145 struct exception_data *d;
Christoph Lameter496252f2014-02-14 14:18:56 -0600146 d = this_cpu_ptr(&exception_data);
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000147 d->fault_ip = regs->iaoq[0];
Helge Deller2ef4dfd2016-04-08 18:32:52 +0200148 d->fault_gp = regs->gr[27];
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000149 d->fault_space = regs->isr;
150 d->fault_addr = regs->ior;
151
Helge Deller0de79852016-03-23 16:00:46 +0100152 regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
153 regs->iaoq[0] &= ~3;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000154 /*
155 * NOTE: In some cases the faulting instruction
156 * may be in the delay slot of a branch. We
157 * don't want to take the branch, so we don't
158 * increment iaoq[1], instead we set it to be
159 * iaoq[0]+4, and clear the B bit in the PSW
160 */
161 regs->iaoq[1] = regs->iaoq[0] + 4;
162 regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
163
164 return 1;
165 }
166
167 return 0;
168}
169
Helge Dellerfef47e22014-05-05 18:07:12 +0200170/*
Helge Dellerb3916672016-09-24 21:45:46 +0200171 * parisc hardware trap list
172 *
173 * Documented in section 3 "Addressing and Access Control" of the
174 * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
175 * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
176 *
177 * For implementation see handle_interruption() in traps.c
178 */
179static const char * const trap_description[] = {
180 [1] "High-priority machine check (HPMC)",
181 [2] "Power failure interrupt",
182 [3] "Recovery counter trap",
183 [5] "Low-priority machine check",
184 [6] "Instruction TLB miss fault",
185 [7] "Instruction access rights / protection trap",
186 [8] "Illegal instruction trap",
187 [9] "Break instruction trap",
188 [10] "Privileged operation trap",
189 [11] "Privileged register trap",
190 [12] "Overflow trap",
191 [13] "Conditional trap",
192 [14] "FP Assist Exception trap",
193 [15] "Data TLB miss fault",
194 [16] "Non-access ITLB miss fault",
195 [17] "Non-access DTLB miss fault",
196 [18] "Data memory protection/unaligned access trap",
197 [19] "Data memory break trap",
198 [20] "TLB dirty bit trap",
199 [21] "Page reference trap",
200 [22] "Assist emulation trap",
201 [25] "Taken branch trap",
202 [26] "Data memory access rights trap",
203 [27] "Data memory protection ID trap",
204 [28] "Unaligned data reference trap",
205};
206
207/*
Helge Dellerfef47e22014-05-05 18:07:12 +0200208 * Print out info about fatal segfaults, if the show_unhandled_signals
209 * sysctl is set:
210 */
211static inline void
212show_signal_msg(struct pt_regs *regs, unsigned long code,
213 unsigned long address, struct task_struct *tsk,
214 struct vm_area_struct *vma)
215{
Helge Dellerb3916672016-09-24 21:45:46 +0200216 const char *trap_name = NULL;
217
Helge Dellerfef47e22014-05-05 18:07:12 +0200218 if (!unhandled_signal(tsk, SIGSEGV))
219 return;
220
221 if (!printk_ratelimit())
222 return;
223
224 pr_warn("\n");
225 pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
226 tsk->comm, code, address);
227 print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
Helge Dellerb3916672016-09-24 21:45:46 +0200228
229 if (code < ARRAY_SIZE(trap_description))
230 trap_name = trap_description[code];
231 pr_warn(KERN_CONT " trap #%lu: %s%c", code,
232 trap_name ? trap_name : "unknown",
233 vma ? ',':'\n');
234
Helge Dellerfef47e22014-05-05 18:07:12 +0200235 if (vma)
Helge Dellerb3916672016-09-24 21:45:46 +0200236 pr_warn(KERN_CONT " vm_start = 0x%08lx, vm_end = 0x%08lx\n",
Helge Dellerfef47e22014-05-05 18:07:12 +0200237 vma->vm_start, vma->vm_end);
238
239 show_regs(regs);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242void do_page_fault(struct pt_regs *regs, unsigned long code,
243 unsigned long address)
244{
245 struct vm_area_struct *vma, *prev_vma;
John David Anglin2d8b22d2013-10-05 10:55:36 -0400246 struct task_struct *tsk;
247 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unsigned long acc_type;
Nick Piggin83c54072007-07-19 01:47:05 -0700249 int fault;
John David Anglin2d8b22d2013-10-05 10:55:36 -0400250 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Helge Deller699817c2015-09-02 18:18:48 +0200252 if (faulthandler_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto no_context;
254
John David Anglin2d8b22d2013-10-05 10:55:36 -0400255 tsk = current;
256 mm = tsk->mm;
257 if (!mm)
258 goto no_context;
259
260 flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Johannes Weiner759496b2013-09-12 15:13:39 -0700261 if (user_mode(regs))
262 flags |= FAULT_FLAG_USER;
Felipe Pena0772dac2013-09-30 13:45:14 -0700263
264 acc_type = parisc_acctyp(code, regs->iir);
Johannes Weiner759496b2013-09-12 15:13:39 -0700265 if (acc_type & VM_WRITE)
266 flags |= FAULT_FLAG_WRITE;
Kautuk Consul38057472012-03-20 09:26:53 -0400267retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 down_read(&mm->mmap_sem);
269 vma = find_vma_prev(mm, address, &prev_vma);
270 if (!vma || address < vma->vm_start)
271 goto check_expansion;
272/*
273 * Ok, we have a good vm_area for this memory access. We still need to
274 * check the access permissions.
275 */
276
277good_area:
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if ((vma->vm_flags & acc_type) != acc_type)
280 goto bad_area;
281
282 /*
283 * If for any reason at all we couldn't handle the fault, make
284 * sure we exit gracefully rather than endlessly redo the
285 * fault.
286 */
287
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700288 fault = handle_mm_fault(vma, address, flags);
Kautuk Consul38057472012-03-20 09:26:53 -0400289
290 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
291 return;
292
Nick Piggin83c54072007-07-19 01:47:05 -0700293 if (unlikely(fault & VM_FAULT_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
Helge Deller67a5a592006-03-27 19:52:14 +0000295 * We hit a shared mapping outside of the file, or some
Linus Torvalds6e346222005-08-04 08:33:38 -0700296 * other thing happened to us that made us unable to
297 * handle the page fault gracefully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Nick Piggin83c54072007-07-19 01:47:05 -0700299 if (fault & VM_FAULT_OOM)
300 goto out_of_memory;
Linus Torvalds33692f22015-01-29 10:51:32 -0800301 else if (fault & VM_FAULT_SIGSEGV)
302 goto bad_area;
Nick Piggin83c54072007-07-19 01:47:05 -0700303 else if (fault & VM_FAULT_SIGBUS)
304 goto bad_area;
305 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Kautuk Consul38057472012-03-20 09:26:53 -0400307 if (flags & FAULT_FLAG_ALLOW_RETRY) {
308 if (fault & VM_FAULT_MAJOR)
309 current->maj_flt++;
310 else
311 current->min_flt++;
312 if (fault & VM_FAULT_RETRY) {
313 flags &= ~FAULT_FLAG_ALLOW_RETRY;
314
315 /*
316 * No need to up_read(&mm->mmap_sem) as we would
317 * have already released it in __lock_page_or_retry
318 * in mm/filemap.c.
319 */
320
321 goto retry;
322 }
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 up_read(&mm->mmap_sem);
325 return;
326
327check_expansion:
328 vma = prev_vma;
329 if (vma && (expand_stack(vma, address) == 0))
330 goto good_area;
331
332/*
333 * Something tried to access memory that isn't in our memory map..
334 */
335bad_area:
336 up_read(&mm->mmap_sem);
337
338 if (user_mode(regs)) {
339 struct siginfo si;
340
Helge Dellerfef47e22014-05-05 18:07:12 +0200341 show_signal_msg(regs, code, address, tsk, vma);
342
Helge Deller1f2048f2013-11-07 17:08:36 +0100343 switch (code) {
344 case 15: /* Data TLB miss fault/Data page fault */
Helge Deller49d1cb22013-11-18 22:12:11 +0100345 /* send SIGSEGV when outside of vma */
346 if (!vma ||
347 address < vma->vm_start || address > vma->vm_end) {
348 si.si_signo = SIGSEGV;
349 si.si_code = SEGV_MAPERR;
350 break;
351 }
352
353 /* send SIGSEGV for wrong permissions */
354 if ((vma->vm_flags & acc_type) != acc_type) {
355 si.si_signo = SIGSEGV;
356 si.si_code = SEGV_ACCERR;
357 break;
358 }
359
360 /* probably address is outside of mapped file */
361 /* fall through */
Helge Deller1f2048f2013-11-07 17:08:36 +0100362 case 17: /* NA data TLB miss / page fault */
363 case 18: /* Unaligned access - PCXS only */
364 si.si_signo = SIGBUS;
Helge Deller49d1cb22013-11-18 22:12:11 +0100365 si.si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
Helge Deller1f2048f2013-11-07 17:08:36 +0100366 break;
367 case 16: /* Non-access instruction TLB miss fault */
368 case 26: /* PCXL: Data memory access rights trap */
369 default:
370 si.si_signo = SIGSEGV;
Helge Deller49d1cb22013-11-18 22:12:11 +0100371 si.si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
372 break;
Helge Deller1f2048f2013-11-07 17:08:36 +0100373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 si.si_errno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 si.si_addr = (void __user *) address;
Helge Deller1f2048f2013-11-07 17:08:36 +0100376 force_sig_info(si.si_signo, &si, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
378 }
379
380no_context:
381
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000382 if (!user_mode(regs) && fixup_exception(regs)) {
383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
386 parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
387
388 out_of_memory:
389 up_read(&mm->mmap_sem);
Nick Piggin53e30d02010-04-22 16:06:23 +0000390 if (!user_mode(regs))
391 goto no_context;
392 pagefault_out_of_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}