GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / parisc / mm / fault.c
1 /*
2  * 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/extable.h>
18 #include <linux/uaccess.h>
19
20 #include <asm/traps.h>
21
22 /* 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
31 DEFINE_PER_CPU(struct exception_data, exception_data);
32
33 int show_unhandled_signals = 1;
34
35 /*
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  */
50 static unsigned long
51 parisc_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
139 int 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;
146                 d = this_cpu_ptr(&exception_data);
147                 d->fault_ip = regs->iaoq[0];
148                 d->fault_gp = regs->gr[27];
149                 d->fault_space = regs->isr;
150                 d->fault_addr = regs->ior;
151
152                 /*
153                  * Fix up get_user() and put_user().
154                  * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() sets the least-significant
155                  * bit in the relative address of the fixup routine to indicate
156                  * that %r8 should be loaded with -EFAULT to report a userspace
157                  * access error.
158                  */
159                 if (fix->fixup & 1) {
160                         regs->gr[8] = -EFAULT;
161
162                         /* zero target register for get_user() */
163                         if (parisc_acctyp(0, regs->iir) == VM_READ) {
164                                 int treg = regs->iir & 0x1f;
165                                 regs->gr[treg] = 0;
166                         }
167                 }
168
169                 regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
170                 regs->iaoq[0] &= ~3;
171                 /*
172                  * NOTE: In some cases the faulting instruction
173                  * may be in the delay slot of a branch. We
174                  * don't want to take the branch, so we don't
175                  * increment iaoq[1], instead we set it to be
176                  * iaoq[0]+4, and clear the B bit in the PSW
177                  */
178                 regs->iaoq[1] = regs->iaoq[0] + 4;
179                 regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
180
181                 return 1;
182         }
183
184         return 0;
185 }
186
187 /*
188  * parisc hardware trap list
189  *
190  * Documented in section 3 "Addressing and Access Control" of the
191  * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
192  * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
193  *
194  * For implementation see handle_interruption() in traps.c
195  */
196 static const char * const trap_description[] = {
197         [1] "High-priority machine check (HPMC)",
198         [2] "Power failure interrupt",
199         [3] "Recovery counter trap",
200         [5] "Low-priority machine check",
201         [6] "Instruction TLB miss fault",
202         [7] "Instruction access rights / protection trap",
203         [8] "Illegal instruction trap",
204         [9] "Break instruction trap",
205         [10] "Privileged operation trap",
206         [11] "Privileged register trap",
207         [12] "Overflow trap",
208         [13] "Conditional trap",
209         [14] "FP Assist Exception trap",
210         [15] "Data TLB miss fault",
211         [16] "Non-access ITLB miss fault",
212         [17] "Non-access DTLB miss fault",
213         [18] "Data memory protection/unaligned access trap",
214         [19] "Data memory break trap",
215         [20] "TLB dirty bit trap",
216         [21] "Page reference trap",
217         [22] "Assist emulation trap",
218         [25] "Taken branch trap",
219         [26] "Data memory access rights trap",
220         [27] "Data memory protection ID trap",
221         [28] "Unaligned data reference trap",
222 };
223
224 const char *trap_name(unsigned long code)
225 {
226         const char *t = NULL;
227
228         if (code < ARRAY_SIZE(trap_description))
229                 t = trap_description[code];
230
231         return t ? t : "Unknown trap";
232 }
233
234 /*
235  * Print out info about fatal segfaults, if the show_unhandled_signals
236  * sysctl is set:
237  */
238 static inline void
239 show_signal_msg(struct pt_regs *regs, unsigned long code,
240                 unsigned long address, struct task_struct *tsk,
241                 struct vm_area_struct *vma)
242 {
243         if (!unhandled_signal(tsk, SIGSEGV))
244                 return;
245
246         if (!printk_ratelimit())
247                 return;
248
249         pr_warn("\n");
250         pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
251             tsk->comm, code, address);
252         print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
253
254         pr_cont("\ntrap #%lu: %s%c", code, trap_name(code),
255                 vma ? ',':'\n');
256
257         if (vma)
258                 pr_warn(KERN_CONT " vm_start = 0x%08lx, vm_end = 0x%08lx\n",
259                                 vma->vm_start, vma->vm_end);
260
261         show_regs(regs);
262 }
263
264 void do_page_fault(struct pt_regs *regs, unsigned long code,
265                               unsigned long address)
266 {
267         struct vm_area_struct *vma, *prev_vma;
268         struct task_struct *tsk;
269         struct mm_struct *mm;
270         unsigned long acc_type;
271         int fault;
272         unsigned int flags;
273
274         if (faulthandler_disabled())
275                 goto no_context;
276
277         tsk = current;
278         mm = tsk->mm;
279         if (!mm)
280                 goto no_context;
281
282         flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
283         if (user_mode(regs))
284                 flags |= FAULT_FLAG_USER;
285
286         acc_type = parisc_acctyp(code, regs->iir);
287         if (acc_type & VM_WRITE)
288                 flags |= FAULT_FLAG_WRITE;
289 retry:
290         down_read(&mm->mmap_sem);
291         vma = find_vma_prev(mm, address, &prev_vma);
292         if (!vma || address < vma->vm_start)
293                 goto check_expansion;
294 /*
295  * Ok, we have a good vm_area for this memory access. We still need to
296  * check the access permissions.
297  */
298
299 good_area:
300
301         if ((vma->vm_flags & acc_type) != acc_type)
302                 goto bad_area;
303
304         /*
305          * If for any reason at all we couldn't handle the fault, make
306          * sure we exit gracefully rather than endlessly redo the
307          * fault.
308          */
309
310         fault = handle_mm_fault(vma, address, flags);
311
312         if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
313                 return;
314
315         if (unlikely(fault & VM_FAULT_ERROR)) {
316                 /*
317                  * We hit a shared mapping outside of the file, or some
318                  * other thing happened to us that made us unable to
319                  * handle the page fault gracefully.
320                  */
321                 if (fault & VM_FAULT_OOM)
322                         goto out_of_memory;
323                 else if (fault & VM_FAULT_SIGSEGV)
324                         goto bad_area;
325                 else if (fault & VM_FAULT_SIGBUS)
326                         goto bad_area;
327                 BUG();
328         }
329         if (flags & FAULT_FLAG_ALLOW_RETRY) {
330                 if (fault & VM_FAULT_MAJOR)
331                         current->maj_flt++;
332                 else
333                         current->min_flt++;
334                 if (fault & VM_FAULT_RETRY) {
335                         flags &= ~FAULT_FLAG_ALLOW_RETRY;
336
337                         /*
338                          * No need to up_read(&mm->mmap_sem) as we would
339                          * have already released it in __lock_page_or_retry
340                          * in mm/filemap.c.
341                          */
342
343                         goto retry;
344                 }
345         }
346         up_read(&mm->mmap_sem);
347         return;
348
349 check_expansion:
350         vma = prev_vma;
351         if (vma && (expand_stack(vma, address) == 0))
352                 goto good_area;
353
354 /*
355  * Something tried to access memory that isn't in our memory map..
356  */
357 bad_area:
358         up_read(&mm->mmap_sem);
359
360         if (user_mode(regs)) {
361                 struct siginfo si;
362
363                 show_signal_msg(regs, code, address, tsk, vma);
364
365                 switch (code) {
366                 case 15:        /* Data TLB miss fault/Data page fault */
367                         /* send SIGSEGV when outside of vma */
368                         if (!vma ||
369                             address < vma->vm_start || address >= vma->vm_end) {
370                                 si.si_signo = SIGSEGV;
371                                 si.si_code = SEGV_MAPERR;
372                                 break;
373                         }
374
375                         /* send SIGSEGV for wrong permissions */
376                         if ((vma->vm_flags & acc_type) != acc_type) {
377                                 si.si_signo = SIGSEGV;
378                                 si.si_code = SEGV_ACCERR;
379                                 break;
380                         }
381
382                         /* probably address is outside of mapped file */
383                         /* fall through */
384                 case 17:        /* NA data TLB miss / page fault */
385                 case 18:        /* Unaligned access - PCXS only */
386                         si.si_signo = SIGBUS;
387                         si.si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
388                         break;
389                 case 16:        /* Non-access instruction TLB miss fault */
390                 case 26:        /* PCXL: Data memory access rights trap */
391                 default:
392                         si.si_signo = SIGSEGV;
393                         si.si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
394                         break;
395                 }
396                 si.si_errno = 0;
397                 si.si_addr = (void __user *) address;
398                 force_sig_info(si.si_signo, &si, current);
399                 return;
400         }
401
402 no_context:
403
404         if (!user_mode(regs) && fixup_exception(regs)) {
405                 return;
406         }
407
408         parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
409
410   out_of_memory:
411         up_read(&mm->mmap_sem);
412         if (!user_mode(regs))
413                 goto no_context;
414         pagefault_out_of_memory();
415 }