572c8012958179c94c6d67e46f3b517b08fcbad5
[linux-2.6-block.git] / arch / powerpc / mm / fault.c
1 /*
2  *  PowerPC version
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *
5  *  Derived from "arch/i386/mm/fault.c"
6  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
7  *
8  *  Modified by Cort Dougan and Paul Mackerras.
9  *
10  *  Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
11  *
12  *  This program is free software; you can redistribute it and/or
13  *  modify it under the terms of the GNU General Public License
14  *  as published by the Free Software Foundation; either version
15  *  2 of the License, or (at your option) any later version.
16  */
17
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/ptrace.h>
26 #include <linux/mman.h>
27 #include <linux/mm.h>
28 #include <linux/interrupt.h>
29 #include <linux/highmem.h>
30 #include <linux/extable.h>
31 #include <linux/kprobes.h>
32 #include <linux/kdebug.h>
33 #include <linux/perf_event.h>
34 #include <linux/ratelimit.h>
35 #include <linux/context_tracking.h>
36 #include <linux/hugetlb.h>
37 #include <linux/uaccess.h>
38
39 #include <asm/firmware.h>
40 #include <asm/page.h>
41 #include <asm/pgtable.h>
42 #include <asm/mmu.h>
43 #include <asm/mmu_context.h>
44 #include <asm/tlbflush.h>
45 #include <asm/siginfo.h>
46 #include <asm/debug.h>
47
48 #include "icswx.h"
49
50 static inline bool notify_page_fault(struct pt_regs *regs)
51 {
52         bool ret = false;
53
54 #ifdef CONFIG_KPROBES
55         /* kprobe_running() needs smp_processor_id() */
56         if (!user_mode(regs)) {
57                 preempt_disable();
58                 if (kprobe_running() && kprobe_fault_handler(regs, 11))
59                         ret = true;
60                 preempt_enable();
61         }
62 #endif /* CONFIG_KPROBES */
63
64         if (unlikely(debugger_fault_handler(regs)))
65                 ret = true;
66
67         return ret;
68 }
69
70 /*
71  * Check whether the instruction at regs->nip is a store using
72  * an update addressing form which will update r1.
73  */
74 static int store_updates_sp(struct pt_regs *regs)
75 {
76         unsigned int inst;
77
78         if (get_user(inst, (unsigned int __user *)regs->nip))
79                 return 0;
80         /* check for 1 in the rA field */
81         if (((inst >> 16) & 0x1f) != 1)
82                 return 0;
83         /* check major opcode */
84         switch (inst >> 26) {
85         case 37:        /* stwu */
86         case 39:        /* stbu */
87         case 45:        /* sthu */
88         case 53:        /* stfsu */
89         case 55:        /* stfdu */
90                 return 1;
91         case 62:        /* std or stdu */
92                 return (inst & 3) == 1;
93         case 31:
94                 /* check minor opcode */
95                 switch ((inst >> 1) & 0x3ff) {
96                 case 181:       /* stdux */
97                 case 183:       /* stwux */
98                 case 247:       /* stbux */
99                 case 439:       /* sthux */
100                 case 695:       /* stfsux */
101                 case 759:       /* stfdux */
102                         return 1;
103                 }
104         }
105         return 0;
106 }
107 /*
108  * do_page_fault error handling helpers
109  */
110
111 static int
112 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
113 {
114         /*
115          * If we are in kernel mode, bail out with a SEGV, this will
116          * be caught by the assembly which will restore the non-volatile
117          * registers before calling bad_page_fault()
118          */
119         if (!user_mode(regs))
120                 return SIGSEGV;
121
122         _exception(SIGSEGV, regs, si_code, address);
123
124         return 0;
125 }
126
127 static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
128 {
129         return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
130 }
131
132 static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
133 {
134         struct mm_struct *mm = current->mm;
135
136         /*
137          * Something tried to access memory that isn't in our memory map..
138          * Fix it, but check if it's kernel or user first..
139          */
140         up_read(&mm->mmap_sem);
141
142         return __bad_area_nosemaphore(regs, address, si_code);
143 }
144
145 static noinline int bad_area(struct pt_regs *regs, unsigned long address)
146 {
147         return __bad_area(regs, address, SEGV_MAPERR);
148 }
149
150 static int do_sigbus(struct pt_regs *regs, unsigned long address,
151                      unsigned int fault)
152 {
153         siginfo_t info;
154         unsigned int lsb = 0;
155
156         if (!user_mode(regs))
157                 return SIGBUS;
158
159         current->thread.trap_nr = BUS_ADRERR;
160         info.si_signo = SIGBUS;
161         info.si_errno = 0;
162         info.si_code = BUS_ADRERR;
163         info.si_addr = (void __user *)address;
164 #ifdef CONFIG_MEMORY_FAILURE
165         if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
166                 pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
167                         current->comm, current->pid, address);
168                 info.si_code = BUS_MCEERR_AR;
169         }
170
171         if (fault & VM_FAULT_HWPOISON_LARGE)
172                 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
173         if (fault & VM_FAULT_HWPOISON)
174                 lsb = PAGE_SHIFT;
175 #endif
176         info.si_addr_lsb = lsb;
177         force_sig_info(SIGBUS, &info, current);
178         return 0;
179 }
180
181 static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
182 {
183         /*
184          * Kernel page fault interrupted by SIGKILL. We have no reason to
185          * continue processing.
186          */
187         if (fatal_signal_pending(current) && !user_mode(regs))
188                 return SIGKILL;
189
190         /* Out of memory */
191         if (fault & VM_FAULT_OOM) {
192                 /*
193                  * We ran out of memory, or some other thing happened to us that
194                  * made us unable to handle the page fault gracefully.
195                  */
196                 if (!user_mode(regs))
197                         return SIGSEGV;
198                 pagefault_out_of_memory();
199         } else {
200                 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
201                              VM_FAULT_HWPOISON_LARGE))
202                         return do_sigbus(regs, addr, fault);
203                 else if (fault & VM_FAULT_SIGSEGV)
204                         return bad_area_nosemaphore(regs, addr);
205                 else
206                         BUG();
207         }
208         return 0;
209 }
210
211 /* Is this a bad kernel fault ? */
212 static bool bad_kernel_fault(bool is_exec, unsigned long error_code,
213                              unsigned long address)
214 {
215         if (is_exec && (error_code & (DSISR_NOEXEC_OR_G | DSISR_KEYFAULT))) {
216                 printk_ratelimited(KERN_CRIT "kernel tried to execute"
217                                    " exec-protected page (%lx) -"
218                                    "exploit attempt? (uid: %d)\n",
219                                    address, from_kuid(&init_user_ns,
220                                                       current_uid()));
221         }
222         return is_exec || (address >= TASK_SIZE);
223 }
224
225 #ifdef CONFIG_PPC_SMLPAR
226 static inline void cmo_account_page_fault(void)
227 {
228         if (firmware_has_feature(FW_FEATURE_CMO)) {
229                 u32 page_ins;
230
231                 preempt_disable();
232                 page_ins = be32_to_cpu(get_lppaca()->page_ins);
233                 page_ins += 1 << PAGE_FACTOR;
234                 get_lppaca()->page_ins = cpu_to_be32(page_ins);
235                 preempt_enable();
236         }
237 }
238 #else
239 static inline void cmo_account_page_fault(void) { }
240 #endif /* CONFIG_PPC_SMLPAR */
241
242 #ifdef CONFIG_PPC_STD_MMU
243 static void sanity_check_fault(bool is_write, unsigned long error_code)
244 {
245         /*
246          * For hash translation mode, we should never get a
247          * PROTFAULT. Any update to pte to reduce access will result in us
248          * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
249          * fault instead of DSISR_PROTFAULT.
250          *
251          * A pte update to relax the access will not result in a hash page table
252          * entry invalidate and hence can result in DSISR_PROTFAULT.
253          * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
254          * the special !is_write in the below conditional.
255          *
256          * For platforms that doesn't supports coherent icache and do support
257          * per page noexec bit, we do setup things such that we do the
258          * sync between D/I cache via fault. But that is handled via low level
259          * hash fault code (hash_page_do_lazy_icache()) and we should not reach
260          * here in such case.
261          *
262          * For wrong access that can result in PROTFAULT, the above vma->vm_flags
263          * check should handle those and hence we should fall to the bad_area
264          * handling correctly.
265          *
266          * For embedded with per page exec support that doesn't support coherent
267          * icache we do get PROTFAULT and we handle that D/I cache sync in
268          * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
269          * is conditional for server MMU.
270          *
271          * For radix, we can get prot fault for autonuma case, because radix
272          * page table will have them marked noaccess for user.
273          */
274         if (!radix_enabled() && !is_write)
275                 WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
276 }
277 #else
278 static void sanity_check_fault(bool is_write, unsigned long error_code) { }
279 #endif /* CONFIG_PPC_STD_MMU */
280
281 /*
282  * Define the correct "is_write" bit in error_code based
283  * on the processor family
284  */
285 #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
286 #define page_fault_is_write(__err)      ((__err) & ESR_DST)
287 #define page_fault_is_bad(__err)        (0)
288 #else
289 #define page_fault_is_write(__err)      ((__err) & DSISR_ISSTORE)
290 #if defined(CONFIG_8xx)
291 #define page_fault_is_bad(__err)        ((__err) & 0x10000000)
292 #elif defined(CONFIG_PPC64)
293 #define page_fault_is_bad(__err)        ((__err) & DSISR_BAD_FAULT_64S)
294 #else
295 #define page_fault_is_bad(__err)        ((__err) & DSISR_BAD_FAULT_32S)
296 #endif
297 #endif
298
299 /*
300  * For 600- and 800-family processors, the error_code parameter is DSISR
301  * for a data fault, SRR1 for an instruction fault. For 400-family processors
302  * the error_code parameter is ESR for a data fault, 0 for an instruction
303  * fault.
304  * For 64-bit processors, the error_code parameter is
305  *  - DSISR for a non-SLB data access fault,
306  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
307  *  - 0 any SLB fault.
308  *
309  * The return value is 0 if the fault was handled, or the signal
310  * number if this is a kernel fault that can't be handled here.
311  */
312 static int __do_page_fault(struct pt_regs *regs, unsigned long address,
313                            unsigned long error_code)
314 {
315         struct vm_area_struct * vma;
316         struct mm_struct *mm = current->mm;
317         unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
318         int is_exec = TRAP(regs) == 0x400;
319         int is_user = user_mode(regs);
320         int is_write = page_fault_is_write(error_code);
321         int fault;
322         int store_update_sp = 0;
323
324 #ifdef CONFIG_PPC_ICSWX
325         /*
326          * we need to do this early because this "data storage
327          * interrupt" does not update the DAR/DEAR so we don't want to
328          * look at it
329          */
330         if (error_code & ICSWX_DSI_UCT) {
331                 int rc = acop_handle_fault(regs, address, error_code);
332                 if (rc)
333                         return rc;
334         }
335 #endif /* CONFIG_PPC_ICSWX */
336
337         if (notify_page_fault(regs))
338                 return 0;
339
340         if (unlikely(page_fault_is_bad(error_code))) {
341                 if (is_user) {
342                         _exception(SIGBUS, regs, BUS_OBJERR, address);
343                         return 0;
344                 }
345                 return SIGBUS;
346         }
347
348         /* Additional sanity check(s) */
349         sanity_check_fault(is_write, error_code);
350
351         /*
352          * The kernel should never take an execute fault nor should it
353          * take a page fault to a kernel address.
354          */
355         if (unlikely(!is_user && bad_kernel_fault(is_exec, error_code, address)))
356                 return SIGSEGV;
357
358         /*
359          * If we're in an interrupt, have no user context or are running
360          * in a region with pagefaults disabled then we must not take the fault
361          */
362         if (unlikely(faulthandler_disabled() || !mm)) {
363                 if (is_user)
364                         printk_ratelimited(KERN_ERR "Page fault in user mode"
365                                            " with faulthandler_disabled()=%d"
366                                            " mm=%p\n",
367                                            faulthandler_disabled(), mm);
368                 return bad_area_nosemaphore(regs, address);
369         }
370
371         /* We restore the interrupt state now */
372         if (!arch_irq_disabled_regs(regs))
373                 local_irq_enable();
374
375         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
376
377         /*
378          * We want to do this outside mmap_sem, because reading code around nip
379          * can result in fault, which will cause a deadlock when called with
380          * mmap_sem held
381          */
382         if (is_write && is_user)
383                 store_update_sp = store_updates_sp(regs);
384
385         if (is_user)
386                 flags |= FAULT_FLAG_USER;
387
388         /* When running in the kernel we expect faults to occur only to
389          * addresses in user space.  All other faults represent errors in the
390          * kernel and should generate an OOPS.  Unfortunately, in the case of an
391          * erroneous fault occurring in a code path which already holds mmap_sem
392          * we will deadlock attempting to validate the fault against the
393          * address space.  Luckily the kernel only validly references user
394          * space from well defined areas of code, which are listed in the
395          * exceptions table.
396          *
397          * As the vast majority of faults will be valid we will only perform
398          * the source reference check when there is a possibility of a deadlock.
399          * Attempt to lock the address space, if we cannot we then validate the
400          * source.  If this is invalid we can skip the address space check,
401          * thus avoiding the deadlock.
402          */
403         if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
404                 if (!is_user && !search_exception_tables(regs->nip))
405                         return bad_area_nosemaphore(regs, address);
406
407 retry:
408                 down_read(&mm->mmap_sem);
409         } else {
410                 /*
411                  * The above down_read_trylock() might have succeeded in
412                  * which case we'll have missed the might_sleep() from
413                  * down_read():
414                  */
415                 might_sleep();
416         }
417
418         vma = find_vma(mm, address);
419         if (unlikely(!vma))
420                 return bad_area(regs, address);
421         if (likely(vma->vm_start <= address))
422                 goto good_area;
423         if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
424                 return bad_area(regs, address);
425
426         /*
427          * N.B. The POWER/Open ABI allows programs to access up to
428          * 288 bytes below the stack pointer.
429          * The kernel signal delivery code writes up to about 1.5kB
430          * below the stack pointer (r1) before decrementing it.
431          * The exec code can write slightly over 640kB to the stack
432          * before setting the user r1.  Thus we allow the stack to
433          * expand to 1MB without further checks.
434          */
435         if (address + 0x100000 < vma->vm_end) {
436                 /* get user regs even if this fault is in kernel mode */
437                 struct pt_regs *uregs = current->thread.regs;
438                 if (uregs == NULL)
439                         return bad_area(regs, address);
440
441                 /*
442                  * A user-mode access to an address a long way below
443                  * the stack pointer is only valid if the instruction
444                  * is one which would update the stack pointer to the
445                  * address accessed if the instruction completed,
446                  * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
447                  * (or the byte, halfword, float or double forms).
448                  *
449                  * If we don't check this then any write to the area
450                  * between the last mapped region and the stack will
451                  * expand the stack rather than segfaulting.
452                  */
453                 if (address + 2048 < uregs->gpr[1] && !store_update_sp)
454                         return bad_area(regs, address);
455         }
456         if (unlikely(expand_stack(vma, address)))
457                 return bad_area(regs, address);
458
459 good_area:
460         if (is_exec) {
461                 /*
462                  * Allow execution from readable areas if the MMU does not
463                  * provide separate controls over reading and executing.
464                  *
465                  * Note: That code used to not be enabled for 4xx/BookE.
466                  * It is now as I/D cache coherency for these is done at
467                  * set_pte_at() time and I see no reason why the test
468                  * below wouldn't be valid on those processors. This -may-
469                  * break programs compiled with a really old ABI though.
470                  */
471                 if (unlikely(!(vma->vm_flags & VM_EXEC) &&
472                              (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
473                               !(vma->vm_flags & (VM_READ | VM_WRITE)))))
474                         return bad_area(regs, address);
475         /* a write */
476         } else if (is_write) {
477                 if (unlikely(!(vma->vm_flags & VM_WRITE)))
478                         return bad_area(regs, address);
479                 flags |= FAULT_FLAG_WRITE;
480         /* a read */
481         } else {
482                 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
483                         return bad_area(regs, address);
484         }
485
486         /*
487          * If for any reason at all we couldn't handle the fault,
488          * make sure we exit gracefully rather than endlessly redo
489          * the fault.
490          */
491         fault = handle_mm_fault(vma, address, flags);
492
493         /*
494          * Handle the retry right now, the mmap_sem has been released in that
495          * case.
496          */
497         if (unlikely(fault & VM_FAULT_RETRY)) {
498                 /* We retry only once */
499                 if (flags & FAULT_FLAG_ALLOW_RETRY) {
500                         /*
501                          * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
502                          * of starvation.
503                          */
504                         flags &= ~FAULT_FLAG_ALLOW_RETRY;
505                         flags |= FAULT_FLAG_TRIED;
506                         if (!fatal_signal_pending(current))
507                                 goto retry;
508                 }
509
510                 /*
511                  * User mode? Just return to handle the fatal exception otherwise
512                  * return to bad_page_fault
513                  */
514                 return is_user ? 0 : SIGBUS;
515         }
516
517         up_read(&current->mm->mmap_sem);
518
519         if (unlikely(fault & VM_FAULT_ERROR))
520                 return mm_fault_error(regs, address, fault);
521
522         /*
523          * Major/minor page fault accounting.
524          */
525         if (fault & VM_FAULT_MAJOR) {
526                 current->maj_flt++;
527                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
528                 cmo_account_page_fault();
529         } else {
530                 current->min_flt++;
531                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
532         }
533         return 0;
534 }
535 NOKPROBE_SYMBOL(__do_page_fault);
536
537 int do_page_fault(struct pt_regs *regs, unsigned long address,
538                   unsigned long error_code)
539 {
540         enum ctx_state prev_state = exception_enter();
541         int rc = __do_page_fault(regs, address, error_code);
542         exception_exit(prev_state);
543         return rc;
544 }
545 NOKPROBE_SYMBOL(do_page_fault);
546
547 /*
548  * bad_page_fault is called when we have a bad access from the kernel.
549  * It is called from the DSI and ISI handlers in head.S and from some
550  * of the procedures in traps.c.
551  */
552 void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
553 {
554         const struct exception_table_entry *entry;
555
556         /* Are we prepared to handle this fault?  */
557         if ((entry = search_exception_tables(regs->nip)) != NULL) {
558                 regs->nip = extable_fixup(entry);
559                 return;
560         }
561
562         /* kernel has accessed a bad area */
563
564         switch (regs->trap) {
565         case 0x300:
566         case 0x380:
567                 printk(KERN_ALERT "Unable to handle kernel paging request for "
568                         "data at address 0x%08lx\n", regs->dar);
569                 break;
570         case 0x400:
571         case 0x480:
572                 printk(KERN_ALERT "Unable to handle kernel paging request for "
573                         "instruction fetch\n");
574                 break;
575         case 0x600:
576                 printk(KERN_ALERT "Unable to handle kernel paging request for "
577                         "unaligned access at address 0x%08lx\n", regs->dar);
578                 break;
579         default:
580                 printk(KERN_ALERT "Unable to handle kernel paging request for "
581                         "unknown fault\n");
582                 break;
583         }
584         printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
585                 regs->nip);
586
587         if (task_stack_end_corrupted(current))
588                 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
589
590         die("Kernel access of bad area", regs, sig);
591 }