11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * S390 version 3a53c8fabSHeiko Carstens * Copyright IBM Corp. 1999 41da177e4SLinus Torvalds * Author(s): Hartmut Penner (hp@de.ibm.com) 51da177e4SLinus Torvalds * Ulrich Weigand (uweigand@de.ibm.com) 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from "arch/i386/mm/fault.c" 81da177e4SLinus Torvalds * Copyright (C) 1995 Linus Torvalds 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11052ff461SHeiko Carstens #include <linux/kernel_stat.h> 12cdd6c482SIngo Molnar #include <linux/perf_event.h> 131da177e4SLinus Torvalds #include <linux/signal.h> 141da177e4SLinus Torvalds #include <linux/sched.h> 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/errno.h> 171da177e4SLinus Torvalds #include <linux/string.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/ptrace.h> 201da177e4SLinus Torvalds #include <linux/mman.h> 211da177e4SLinus Torvalds #include <linux/mm.h> 227757591aSHeiko Carstens #include <linux/compat.h> 231da177e4SLinus Torvalds #include <linux/smp.h> 241eeb66a1SChristoph Hellwig #include <linux/kdebug.h> 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/console.h> 271da177e4SLinus Torvalds #include <linux/module.h> 281da177e4SLinus Torvalds #include <linux/hardirq.h> 294ba069b8SMichael Grundy #include <linux/kprobes.h> 30be5ec363SMartin Schwidefsky #include <linux/uaccess.h> 3153492b1dSGerald Schaefer #include <linux/hugetlb.h> 32cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 331da177e4SLinus Torvalds #include <asm/pgtable.h> 34d7b250e2SHeiko Carstens #include <asm/irq.h> 356252d702SMartin Schwidefsky #include <asm/mmu_context.h> 36a0616cdeSDavid Howells #include <asm/facility.h> 37a806170eSHeiko Carstens #include "../kernel/entry.h" 381da177e4SLinus Torvalds 39347a8dc3SMartin Schwidefsky #ifndef CONFIG_64BIT 401da177e4SLinus Torvalds #define __FAIL_ADDR_MASK 0x7ffff000 411da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0200 421da177e4SLinus Torvalds #define __PF_RES_FIELD 0ULL 43347a8dc3SMartin Schwidefsky #else /* CONFIG_64BIT */ 441da177e4SLinus Torvalds #define __FAIL_ADDR_MASK -4096L 451da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0600 461da177e4SLinus Torvalds #define __PF_RES_FIELD 0x8000000000000000ULL 47347a8dc3SMartin Schwidefsky #endif /* CONFIG_64BIT */ 481da177e4SLinus Torvalds 4950d7280dSMartin Schwidefsky #define VM_FAULT_BADCONTEXT 0x010000 5050d7280dSMartin Schwidefsky #define VM_FAULT_BADMAP 0x020000 5150d7280dSMartin Schwidefsky #define VM_FAULT_BADACCESS 0x040000 52f2c76e3bSHeiko Carstens #define VM_FAULT_SIGNAL 0x080000 5350d7280dSMartin Schwidefsky 54a4f32bdbSHeiko Carstens static unsigned long store_indication __read_mostly; 5592f842eaSMartin Schwidefsky 56a4f32bdbSHeiko Carstens #ifdef CONFIG_64BIT 57a4f32bdbSHeiko Carstens static int __init fault_init(void) 5892f842eaSMartin Schwidefsky { 59a4f32bdbSHeiko Carstens if (test_facility(75)) 6092f842eaSMartin Schwidefsky store_indication = 0xc00; 61a4f32bdbSHeiko Carstens return 0; 6292f842eaSMartin Schwidefsky } 63a4f32bdbSHeiko Carstens early_initcall(fault_init); 64a4f32bdbSHeiko Carstens #endif 6592f842eaSMartin Schwidefsky 667ecb344aSMartin Schwidefsky static inline int notify_page_fault(struct pt_regs *regs) 6710c1031fSMartin Schwidefsky { 6833464e3bSChristoph Hellwig int ret = 0; 6933464e3bSChristoph Hellwig 7033464e3bSChristoph Hellwig /* kprobe_running() needs smp_processor_id() */ 7122e0a046SHeiko Carstens if (kprobes_built_in() && !user_mode(regs)) { 7233464e3bSChristoph Hellwig preempt_disable(); 7333464e3bSChristoph Hellwig if (kprobe_running() && kprobe_fault_handler(regs, 14)) 7433464e3bSChristoph Hellwig ret = 1; 7533464e3bSChristoph Hellwig preempt_enable(); 7633464e3bSChristoph Hellwig } 7733464e3bSChristoph Hellwig return ret; 784ba069b8SMichael Grundy } 794ba069b8SMichael Grundy 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* 821da177e4SLinus Torvalds * Unlock any spinlocks which will prevent us from getting the 83cefc8be8SKirill Korotaev * message out. 841da177e4SLinus Torvalds */ 851da177e4SLinus Torvalds void bust_spinlocks(int yes) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds if (yes) { 881da177e4SLinus Torvalds oops_in_progress = 1; 891da177e4SLinus Torvalds } else { 901da177e4SLinus Torvalds int loglevel_save = console_loglevel; 911da177e4SLinus Torvalds console_unblank(); 921da177e4SLinus Torvalds oops_in_progress = 0; 931da177e4SLinus Torvalds /* 941da177e4SLinus Torvalds * OK, the message is on the console. Now we call printk() 951da177e4SLinus Torvalds * without oops_in_progress set so that printk will give klogd 961da177e4SLinus Torvalds * a poke. Hold onto your hats... 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds console_loglevel = 15; 991da177e4SLinus Torvalds printk(" "); 1001da177e4SLinus Torvalds console_loglevel = loglevel_save; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 105482b05ddSGerald Schaefer * Returns the address space associated with the fault. 10661365e13SMartin Schwidefsky * Returns 0 for kernel space and 1 for user space. 1071da177e4SLinus Torvalds */ 10861365e13SMartin Schwidefsky static inline int user_space_fault(unsigned long trans_exc_code) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds /* 11161365e13SMartin Schwidefsky * The lowest two bits of the translation exception 11261365e13SMartin Schwidefsky * identification indicate which paging table was used. 1131da177e4SLinus Torvalds */ 11461365e13SMartin Schwidefsky trans_exc_code &= 3; 11561365e13SMartin Schwidefsky if (trans_exc_code == 2) 11661365e13SMartin Schwidefsky /* Access via secondary space, set_fs setting decides */ 11761365e13SMartin Schwidefsky return current->thread.mm_segment.ar4; 118d1b0d842SHeiko Carstens if (s390_user_mode == HOME_SPACE_MODE) 11961365e13SMartin Schwidefsky /* User space if the access has been done via home space. */ 12061365e13SMartin Schwidefsky return trans_exc_code == 3; 121482b05ddSGerald Schaefer /* 12261365e13SMartin Schwidefsky * If the user space is not the home space the kernel runs in home 12361365e13SMartin Schwidefsky * space. Access via secondary space has already been covered, 12461365e13SMartin Schwidefsky * access via primary space or access register is from user space 12561365e13SMartin Schwidefsky * and access via home space is from the kernel. 126482b05ddSGerald Schaefer */ 12761365e13SMartin Schwidefsky return trans_exc_code != 3; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 130aa33c8cbSMartin Schwidefsky static inline void report_user_fault(struct pt_regs *regs, long signr) 131ab3c68eeSHeiko Carstens { 132ab3c68eeSHeiko Carstens if ((task_pid_nr(current) > 1) && !show_unhandled_signals) 133ab3c68eeSHeiko Carstens return; 134ab3c68eeSHeiko Carstens if (!unhandled_signal(current, signr)) 135ab3c68eeSHeiko Carstens return; 136ab3c68eeSHeiko Carstens if (!printk_ratelimit()) 137ab3c68eeSHeiko Carstens return; 138aa33c8cbSMartin Schwidefsky printk(KERN_ALERT "User process fault: interruption code 0x%X ", 139aa33c8cbSMartin Schwidefsky regs->int_code); 140ab3c68eeSHeiko Carstens print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN); 141aa33c8cbSMartin Schwidefsky printk(KERN_CONT "\n"); 142aa33c8cbSMartin Schwidefsky printk(KERN_ALERT "failing address: %lX\n", 143aa33c8cbSMartin Schwidefsky regs->int_parm_long & __FAIL_ADDR_MASK); 144ab3c68eeSHeiko Carstens show_regs(regs); 145ab3c68eeSHeiko Carstens } 146ab3c68eeSHeiko Carstens 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Send SIGSEGV to task. This is an external routine 1491da177e4SLinus Torvalds * to keep the stack usage of do_page_fault small. 1501da177e4SLinus Torvalds */ 151aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds struct siginfo si; 1541da177e4SLinus Torvalds 155aa33c8cbSMartin Schwidefsky report_user_fault(regs, SIGSEGV); 1561da177e4SLinus Torvalds si.si_signo = SIGSEGV; 1571da177e4SLinus Torvalds si.si_code = si_code; 158aa33c8cbSMartin Schwidefsky si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK); 1591da177e4SLinus Torvalds force_sig_info(SIGSEGV, &si, current); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 162aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs) 16310c1031fSMartin Schwidefsky { 16410c1031fSMartin Schwidefsky const struct exception_table_entry *fixup; 16561365e13SMartin Schwidefsky unsigned long address; 16610c1031fSMartin Schwidefsky 16710c1031fSMartin Schwidefsky /* Are we prepared to handle this kernel fault? */ 16850d7280dSMartin Schwidefsky fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN); 16910c1031fSMartin Schwidefsky if (fixup) { 170eb608fb3SHeiko Carstens regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE; 17110c1031fSMartin Schwidefsky return; 17210c1031fSMartin Schwidefsky } 17310c1031fSMartin Schwidefsky 17410c1031fSMartin Schwidefsky /* 17510c1031fSMartin Schwidefsky * Oops. The kernel tried to access some bad page. We'll have to 17610c1031fSMartin Schwidefsky * terminate things with extreme prejudice. 17710c1031fSMartin Schwidefsky */ 178aa33c8cbSMartin Schwidefsky address = regs->int_parm_long & __FAIL_ADDR_MASK; 179aa33c8cbSMartin Schwidefsky if (!user_space_fault(regs->int_parm_long)) 18010c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel pointer dereference" 18110c1031fSMartin Schwidefsky " at virtual kernel address %p\n", (void *)address); 18210c1031fSMartin Schwidefsky else 18310c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel paging request" 18410c1031fSMartin Schwidefsky " at virtual user address %p\n", (void *)address); 18510c1031fSMartin Schwidefsky 186aa33c8cbSMartin Schwidefsky die(regs, "Oops"); 18710c1031fSMartin Schwidefsky do_exit(SIGKILL); 18810c1031fSMartin Schwidefsky } 18910c1031fSMartin Schwidefsky 190aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs) 19110c1031fSMartin Schwidefsky { 19210c1031fSMartin Schwidefsky /* Low-address protection hit in kernel mode means 19310c1031fSMartin Schwidefsky NULL pointer write access in kernel mode. */ 19410c1031fSMartin Schwidefsky if (regs->psw.mask & PSW_MASK_PSTATE) { 19510c1031fSMartin Schwidefsky /* Low-address protection hit in user mode 'cannot happen'. */ 196aa33c8cbSMartin Schwidefsky die (regs, "Low-address protection"); 19710c1031fSMartin Schwidefsky do_exit(SIGKILL); 19810c1031fSMartin Schwidefsky } 19910c1031fSMartin Schwidefsky 200aa33c8cbSMartin Schwidefsky do_no_context(regs); 20110c1031fSMartin Schwidefsky } 20210c1031fSMartin Schwidefsky 203aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs) 20410c1031fSMartin Schwidefsky { 20510c1031fSMartin Schwidefsky struct task_struct *tsk = current; 20636bf9680SMartin Schwidefsky struct siginfo si; 20710c1031fSMartin Schwidefsky 20810c1031fSMartin Schwidefsky /* 20910c1031fSMartin Schwidefsky * Send a sigbus, regardless of whether we were in kernel 21010c1031fSMartin Schwidefsky * or user mode. 21110c1031fSMartin Schwidefsky */ 21236bf9680SMartin Schwidefsky si.si_signo = SIGBUS; 21336bf9680SMartin Schwidefsky si.si_errno = 0; 21436bf9680SMartin Schwidefsky si.si_code = BUS_ADRERR; 215aa33c8cbSMartin Schwidefsky si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK); 21636bf9680SMartin Schwidefsky force_sig_info(SIGBUS, &si, tsk); 21710c1031fSMartin Schwidefsky } 21810c1031fSMartin Schwidefsky 219aa33c8cbSMartin Schwidefsky static noinline void do_fault_error(struct pt_regs *regs, int fault) 22050d7280dSMartin Schwidefsky { 22150d7280dSMartin Schwidefsky int si_code; 22250d7280dSMartin Schwidefsky 22350d7280dSMartin Schwidefsky switch (fault) { 22450d7280dSMartin Schwidefsky case VM_FAULT_BADACCESS: 22550d7280dSMartin Schwidefsky case VM_FAULT_BADMAP: 22650d7280dSMartin Schwidefsky /* Bad memory access. Check if it is kernel or user space. */ 2277d256175SHeiko Carstens if (user_mode(regs)) { 22850d7280dSMartin Schwidefsky /* User mode accesses just cause a SIGSEGV */ 22950d7280dSMartin Schwidefsky si_code = (fault == VM_FAULT_BADMAP) ? 23050d7280dSMartin Schwidefsky SEGV_MAPERR : SEGV_ACCERR; 231aa33c8cbSMartin Schwidefsky do_sigsegv(regs, si_code); 23250d7280dSMartin Schwidefsky return; 23350d7280dSMartin Schwidefsky } 23450d7280dSMartin Schwidefsky case VM_FAULT_BADCONTEXT: 235aa33c8cbSMartin Schwidefsky do_no_context(regs); 23650d7280dSMartin Schwidefsky break; 237f2c76e3bSHeiko Carstens case VM_FAULT_SIGNAL: 238f2c76e3bSHeiko Carstens if (!user_mode(regs)) 239f2c76e3bSHeiko Carstens do_no_context(regs); 240f2c76e3bSHeiko Carstens break; 24150d7280dSMartin Schwidefsky default: /* fault & VM_FAULT_ERROR */ 24299583181SHeiko Carstens if (fault & VM_FAULT_OOM) { 2437d256175SHeiko Carstens if (!user_mode(regs)) 244aa33c8cbSMartin Schwidefsky do_no_context(regs); 24599583181SHeiko Carstens else 24650d7280dSMartin Schwidefsky pagefault_out_of_memory(); 24799583181SHeiko Carstens } else if (fault & VM_FAULT_SIGBUS) { 24850d7280dSMartin Schwidefsky /* Kernel mode? Handle exceptions or die */ 2497d256175SHeiko Carstens if (!user_mode(regs)) 250aa33c8cbSMartin Schwidefsky do_no_context(regs); 25136bf9680SMartin Schwidefsky else 252aa33c8cbSMartin Schwidefsky do_sigbus(regs); 25350d7280dSMartin Schwidefsky } else 25450d7280dSMartin Schwidefsky BUG(); 25550d7280dSMartin Schwidefsky break; 25650d7280dSMartin Schwidefsky } 25750d7280dSMartin Schwidefsky } 25850d7280dSMartin Schwidefsky 2591da177e4SLinus Torvalds /* 2601da177e4SLinus Torvalds * This routine handles page faults. It determines the address, 2611da177e4SLinus Torvalds * and the problem, and then passes it off to one of the appropriate 2621da177e4SLinus Torvalds * routines. 2631da177e4SLinus Torvalds * 26450d7280dSMartin Schwidefsky * interruption code (int_code): 2651da177e4SLinus Torvalds * 04 Protection -> Write-Protection (suprression) 2661da177e4SLinus Torvalds * 10 Segment translation -> Not present (nullification) 2671da177e4SLinus Torvalds * 11 Page translation -> Not present (nullification) 2681da177e4SLinus Torvalds * 3b Region third trans. -> Not present (nullification) 2691da177e4SLinus Torvalds */ 270aa33c8cbSMartin Schwidefsky static inline int do_exception(struct pt_regs *regs, int access) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds struct task_struct *tsk; 2731da177e4SLinus Torvalds struct mm_struct *mm; 2741da177e4SLinus Torvalds struct vm_area_struct *vma; 275aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 2761da177e4SLinus Torvalds unsigned long address; 27733ce6140SHeiko Carstens unsigned int flags; 27833ce6140SHeiko Carstens int fault; 27910c1031fSMartin Schwidefsky 28039efd4ecSMartin Schwidefsky tsk = current; 28139efd4ecSMartin Schwidefsky /* 28239efd4ecSMartin Schwidefsky * The instruction that caused the program check has 28339efd4ecSMartin Schwidefsky * been nullified. Don't signal single step via SIGTRAP. 28439efd4ecSMartin Schwidefsky */ 28539efd4ecSMartin Schwidefsky clear_tsk_thread_flag(tsk, TIF_PER_TRAP); 28639efd4ecSMartin Schwidefsky 2877ecb344aSMartin Schwidefsky if (notify_page_fault(regs)) 28850d7280dSMartin Schwidefsky return 0; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds mm = tsk->mm; 291aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* 2941da177e4SLinus Torvalds * Verify that the fault happened in user space, that 2951da177e4SLinus Torvalds * we are not in an interrupt and that there is a 2961da177e4SLinus Torvalds * user context. 2971da177e4SLinus Torvalds */ 29850d7280dSMartin Schwidefsky fault = VM_FAULT_BADCONTEXT; 29961365e13SMartin Schwidefsky if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm)) 30050d7280dSMartin Schwidefsky goto out; 3011da177e4SLinus Torvalds 30261365e13SMartin Schwidefsky address = trans_exc_code & __FAIL_ADDR_MASK; 303a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 304f2c76e3bSHeiko Carstens flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 30533ce6140SHeiko Carstens if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400) 30633ce6140SHeiko Carstens flags |= FAULT_FLAG_WRITE; 3071da177e4SLinus Torvalds down_read(&mm->mmap_sem); 3081da177e4SLinus Torvalds 309e5992f2eSMartin Schwidefsky #ifdef CONFIG_PGSTE 3105e8010cbSMartin Schwidefsky if ((current->flags & PF_VCPU) && S390_lowcore.gmap) { 311499069e1SCarsten Otte address = __gmap_fault(address, 312e5992f2eSMartin Schwidefsky (struct gmap *) S390_lowcore.gmap); 313e5992f2eSMartin Schwidefsky if (address == -EFAULT) { 314e5992f2eSMartin Schwidefsky fault = VM_FAULT_BADMAP; 315e5992f2eSMartin Schwidefsky goto out_up; 316e5992f2eSMartin Schwidefsky } 317e5992f2eSMartin Schwidefsky if (address == -ENOMEM) { 318e5992f2eSMartin Schwidefsky fault = VM_FAULT_OOM; 319e5992f2eSMartin Schwidefsky goto out_up; 320e5992f2eSMartin Schwidefsky } 321e5992f2eSMartin Schwidefsky } 322e5992f2eSMartin Schwidefsky #endif 323e5992f2eSMartin Schwidefsky 324e5992f2eSMartin Schwidefsky retry: 32550d7280dSMartin Schwidefsky fault = VM_FAULT_BADMAP; 3261da177e4SLinus Torvalds vma = find_vma(mm, address); 3271da177e4SLinus Torvalds if (!vma) 32850d7280dSMartin Schwidefsky goto out_up; 329c1821c2eSGerald Schaefer 33050d7280dSMartin Schwidefsky if (unlikely(vma->vm_start > address)) { 3311da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 33250d7280dSMartin Schwidefsky goto out_up; 3331da177e4SLinus Torvalds if (expand_stack(vma, address)) 33450d7280dSMartin Schwidefsky goto out_up; 33550d7280dSMartin Schwidefsky } 33650d7280dSMartin Schwidefsky 3371da177e4SLinus Torvalds /* 3381da177e4SLinus Torvalds * Ok, we have a good vm_area for this memory access, so 3391da177e4SLinus Torvalds * we can handle it.. 3401da177e4SLinus Torvalds */ 34150d7280dSMartin Schwidefsky fault = VM_FAULT_BADACCESS; 3421ab947deSMartin Schwidefsky if (unlikely(!(vma->vm_flags & access))) 34350d7280dSMartin Schwidefsky goto out_up; 3441da177e4SLinus Torvalds 34553492b1dSGerald Schaefer if (is_vm_hugetlb_page(vma)) 34653492b1dSGerald Schaefer address &= HPAGE_MASK; 3471da177e4SLinus Torvalds /* 3481da177e4SLinus Torvalds * If for any reason at all we couldn't handle the fault, 3491da177e4SLinus Torvalds * make sure we exit gracefully rather than endlessly redo 3501da177e4SLinus Torvalds * the fault. 3511da177e4SLinus Torvalds */ 35233ce6140SHeiko Carstens fault = handle_mm_fault(mm, vma, address, flags); 353f2c76e3bSHeiko Carstens /* No reason to continue if interrupted by SIGKILL. */ 354f2c76e3bSHeiko Carstens if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) { 355f2c76e3bSHeiko Carstens fault = VM_FAULT_SIGNAL; 356f2c76e3bSHeiko Carstens goto out; 357f2c76e3bSHeiko Carstens } 35850d7280dSMartin Schwidefsky if (unlikely(fault & VM_FAULT_ERROR)) 35950d7280dSMartin Schwidefsky goto out_up; 36050d7280dSMartin Schwidefsky 36133ce6140SHeiko Carstens /* 36233ce6140SHeiko Carstens * Major/minor page fault accounting is only done on the 36333ce6140SHeiko Carstens * initial attempt. If we go through a retry, it is extremely 36433ce6140SHeiko Carstens * likely that the page will be found in page cache at that point. 36533ce6140SHeiko Carstens */ 36633ce6140SHeiko Carstens if (flags & FAULT_FLAG_ALLOW_RETRY) { 367bde69af2SHeiko Carstens if (fault & VM_FAULT_MAJOR) { 36883c54070SNick Piggin tsk->maj_flt++; 369a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 370bde69af2SHeiko Carstens regs, address); 371bde69af2SHeiko Carstens } else { 37283c54070SNick Piggin tsk->min_flt++; 373a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 374bde69af2SHeiko Carstens regs, address); 375bde69af2SHeiko Carstens } 37633ce6140SHeiko Carstens if (fault & VM_FAULT_RETRY) { 37733ce6140SHeiko Carstens /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk 37833ce6140SHeiko Carstens * of starvation. */ 37933ce6140SHeiko Carstens flags &= ~FAULT_FLAG_ALLOW_RETRY; 38045cac65bSShaohua Li flags |= FAULT_FLAG_TRIED; 381e5992f2eSMartin Schwidefsky down_read(&mm->mmap_sem); 38233ce6140SHeiko Carstens goto retry; 38333ce6140SHeiko Carstens } 38433ce6140SHeiko Carstens } 38550d7280dSMartin Schwidefsky fault = 0; 38650d7280dSMartin Schwidefsky out_up: 3871da177e4SLinus Torvalds up_read(&mm->mmap_sem); 38850d7280dSMartin Schwidefsky out: 38950d7280dSMartin Schwidefsky return fault; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 392aa33c8cbSMartin Schwidefsky void __kprobes do_protection_exception(struct pt_regs *regs) 3931da177e4SLinus Torvalds { 394aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 39550d7280dSMartin Schwidefsky int fault; 39661365e13SMartin Schwidefsky 397aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 398*f752ac4dSMartin Schwidefsky /* 399*f752ac4dSMartin Schwidefsky * Protection exceptions are suppressing, decrement psw address. 400*f752ac4dSMartin Schwidefsky * The exception to this rule are aborted transactions, for these 401*f752ac4dSMartin Schwidefsky * the PSW already points to the correct location. 402*f752ac4dSMartin Schwidefsky */ 403*f752ac4dSMartin Schwidefsky if (!(regs->int_code & 0x200)) 404aa33c8cbSMartin Schwidefsky regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16); 40510c1031fSMartin Schwidefsky /* 40610c1031fSMartin Schwidefsky * Check for low-address protection. This needs to be treated 40710c1031fSMartin Schwidefsky * as a special case because the translation exception code 40810c1031fSMartin Schwidefsky * field is not guaranteed to contain valid data in this case. 40910c1031fSMartin Schwidefsky */ 41061365e13SMartin Schwidefsky if (unlikely(!(trans_exc_code & 4))) { 411aa33c8cbSMartin Schwidefsky do_low_address(regs); 41210c1031fSMartin Schwidefsky return; 41310c1031fSMartin Schwidefsky } 414aa33c8cbSMartin Schwidefsky fault = do_exception(regs, VM_WRITE); 41550d7280dSMartin Schwidefsky if (unlikely(fault)) 416aa33c8cbSMartin Schwidefsky do_fault_error(regs, fault); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 419aa33c8cbSMartin Schwidefsky void __kprobes do_dat_exception(struct pt_regs *regs) 4201da177e4SLinus Torvalds { 4211ab947deSMartin Schwidefsky int access, fault; 42250d7280dSMartin Schwidefsky 4231ab947deSMartin Schwidefsky access = VM_READ | VM_EXEC | VM_WRITE; 424aa33c8cbSMartin Schwidefsky fault = do_exception(regs, access); 42550d7280dSMartin Schwidefsky if (unlikely(fault)) 426aa33c8cbSMartin Schwidefsky do_fault_error(regs, fault); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4296252d702SMartin Schwidefsky #ifdef CONFIG_64BIT 430aa33c8cbSMartin Schwidefsky void __kprobes do_asce_exception(struct pt_regs *regs) 4316252d702SMartin Schwidefsky { 43250d7280dSMartin Schwidefsky struct mm_struct *mm = current->mm; 4336252d702SMartin Schwidefsky struct vm_area_struct *vma; 434aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 4356252d702SMartin Schwidefsky 43639efd4ecSMartin Schwidefsky /* 43739efd4ecSMartin Schwidefsky * The instruction that caused the program check has 43839efd4ecSMartin Schwidefsky * been nullified. Don't signal single step via SIGTRAP. 43939efd4ecSMartin Schwidefsky */ 44039efd4ecSMartin Schwidefsky clear_tsk_thread_flag(current, TIF_PER_TRAP); 44139efd4ecSMartin Schwidefsky 442aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 44361365e13SMartin Schwidefsky if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm)) 4446252d702SMartin Schwidefsky goto no_context; 4456252d702SMartin Schwidefsky 4466252d702SMartin Schwidefsky down_read(&mm->mmap_sem); 44750d7280dSMartin Schwidefsky vma = find_vma(mm, trans_exc_code & __FAIL_ADDR_MASK); 4486252d702SMartin Schwidefsky up_read(&mm->mmap_sem); 4496252d702SMartin Schwidefsky 4506252d702SMartin Schwidefsky if (vma) { 4516252d702SMartin Schwidefsky update_mm(mm, current); 4526252d702SMartin Schwidefsky return; 4536252d702SMartin Schwidefsky } 4546252d702SMartin Schwidefsky 4556252d702SMartin Schwidefsky /* User mode accesses just cause a SIGSEGV */ 4567d256175SHeiko Carstens if (user_mode(regs)) { 457aa33c8cbSMartin Schwidefsky do_sigsegv(regs, SEGV_MAPERR); 4586252d702SMartin Schwidefsky return; 4596252d702SMartin Schwidefsky } 4606252d702SMartin Schwidefsky 4616252d702SMartin Schwidefsky no_context: 462aa33c8cbSMartin Schwidefsky do_no_context(regs); 4636252d702SMartin Schwidefsky } 4646252d702SMartin Schwidefsky #endif 4656252d702SMartin Schwidefsky 4661e54622eSMartin Schwidefsky int __handle_fault(unsigned long uaddr, unsigned long pgm_int_code, int write) 4676c1e3e79SGerald Schaefer { 4686c1e3e79SGerald Schaefer struct pt_regs regs; 4696c1e3e79SGerald Schaefer int access, fault; 4706c1e3e79SGerald Schaefer 471008c2e8fSHeiko Carstens /* Emulate a uaccess fault from kernel mode. */ 472b50511e4SMartin Schwidefsky regs.psw.mask = psw_kernel_bits | PSW_MASK_DAT | PSW_MASK_MCHECK; 4736c1e3e79SGerald Schaefer if (!irqs_disabled()) 4746c1e3e79SGerald Schaefer regs.psw.mask |= PSW_MASK_IO | PSW_MASK_EXT; 4756c1e3e79SGerald Schaefer regs.psw.addr = (unsigned long) __builtin_return_address(0); 4766c1e3e79SGerald Schaefer regs.psw.addr |= PSW_ADDR_AMODE; 477aa33c8cbSMartin Schwidefsky regs.int_code = pgm_int_code; 478aa33c8cbSMartin Schwidefsky regs.int_parm_long = (uaddr & PAGE_MASK) | 2; 4791e54622eSMartin Schwidefsky access = write ? VM_WRITE : VM_READ; 480aa33c8cbSMartin Schwidefsky fault = do_exception(®s, access); 481008c2e8fSHeiko Carstens /* 482008c2e8fSHeiko Carstens * Since the fault happened in kernel mode while performing a uaccess 483008c2e8fSHeiko Carstens * all we need to do now is emulating a fixup in case "fault" is not 484008c2e8fSHeiko Carstens * zero. 485008c2e8fSHeiko Carstens * For the calling uaccess functions this results always in -EFAULT. 486008c2e8fSHeiko Carstens */ 4876c1e3e79SGerald Schaefer return fault ? -EFAULT : 0; 4886c1e3e79SGerald Schaefer } 4896c1e3e79SGerald Schaefer 4901da177e4SLinus Torvalds #ifdef CONFIG_PFAULT 4911da177e4SLinus Torvalds /* 4921da177e4SLinus Torvalds * 'pfault' pseudo page faults routines. 4931da177e4SLinus Torvalds */ 494fb0a9d7eSHeiko Carstens static int pfault_disable; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds static int __init nopfault(char *str) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds pfault_disable = 1; 4991da177e4SLinus Torvalds return 1; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds __setup("nopfault", nopfault); 5031da177e4SLinus Torvalds 5047dd8fe1fSHeiko Carstens struct pfault_refbk { 5057dd8fe1fSHeiko Carstens u16 refdiagc; 5067dd8fe1fSHeiko Carstens u16 reffcode; 5077dd8fe1fSHeiko Carstens u16 refdwlen; 5087dd8fe1fSHeiko Carstens u16 refversn; 5097dd8fe1fSHeiko Carstens u64 refgaddr; 5107dd8fe1fSHeiko Carstens u64 refselmk; 5117dd8fe1fSHeiko Carstens u64 refcmpmk; 5127dd8fe1fSHeiko Carstens u64 reserved; 5137dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8))); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds int pfault_init(void) 5161da177e4SLinus Torvalds { 5177dd8fe1fSHeiko Carstens struct pfault_refbk refbk = { 5187dd8fe1fSHeiko Carstens .refdiagc = 0x258, 5197dd8fe1fSHeiko Carstens .reffcode = 0, 5207dd8fe1fSHeiko Carstens .refdwlen = 5, 5217dd8fe1fSHeiko Carstens .refversn = 2, 5227dd8fe1fSHeiko Carstens .refgaddr = __LC_CURRENT_PID, 5237dd8fe1fSHeiko Carstens .refselmk = 1ULL << 48, 5247dd8fe1fSHeiko Carstens .refcmpmk = 1ULL << 48, 5257dd8fe1fSHeiko Carstens .reserved = __PF_RES_FIELD }; 5261da177e4SLinus Torvalds int rc; 5271da177e4SLinus Torvalds 528f32269a0SCarsten Otte if (pfault_disable) 5291da177e4SLinus Torvalds return -1; 53094c12cc7SMartin Schwidefsky asm volatile( 5311da177e4SLinus Torvalds " diag %1,%0,0x258\n" 5321da177e4SLinus Torvalds "0: j 2f\n" 5331da177e4SLinus Torvalds "1: la %0,8\n" 5341da177e4SLinus Torvalds "2:\n" 53594c12cc7SMartin Schwidefsky EX_TABLE(0b,1b) 536d4b68996SMartin Schwidefsky : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc"); 5371da177e4SLinus Torvalds return rc; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds void pfault_fini(void) 5411da177e4SLinus Torvalds { 5427dd8fe1fSHeiko Carstens struct pfault_refbk refbk = { 5437dd8fe1fSHeiko Carstens .refdiagc = 0x258, 5447dd8fe1fSHeiko Carstens .reffcode = 1, 5457dd8fe1fSHeiko Carstens .refdwlen = 5, 5467dd8fe1fSHeiko Carstens .refversn = 2, 5477dd8fe1fSHeiko Carstens }; 5481da177e4SLinus Torvalds 549f32269a0SCarsten Otte if (pfault_disable) 5501da177e4SLinus Torvalds return; 55194c12cc7SMartin Schwidefsky asm volatile( 5521da177e4SLinus Torvalds " diag %0,0,0x258\n" 5531da177e4SLinus Torvalds "0:\n" 55494c12cc7SMartin Schwidefsky EX_TABLE(0b,0b) 555d4b68996SMartin Schwidefsky : : "a" (&refbk), "m" (refbk) : "cc"); 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 558f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock); 559f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list); 560f2db2e6cSHeiko Carstens 561fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code, 562f6649a7eSMartin Schwidefsky unsigned int param32, unsigned long param64) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds struct task_struct *tsk; 5651da177e4SLinus Torvalds __u16 subcode; 566f2db2e6cSHeiko Carstens pid_t pid; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* 5691da177e4SLinus Torvalds * Get the external interruption subcode & pfault 5701da177e4SLinus Torvalds * initial/completion signal bit. VM stores this 5711da177e4SLinus Torvalds * in the 'cpu address' field associated with the 5721da177e4SLinus Torvalds * external interrupt. 5731da177e4SLinus Torvalds */ 574fde15c3aSHeiko Carstens subcode = ext_code.subcode; 5751da177e4SLinus Torvalds if ((subcode & 0xff00) != __SUBCODE_MASK) 5761da177e4SLinus Torvalds return; 577420f42ecSHeiko Carstens inc_irq_stat(IRQEXT_PFL); 578f2db2e6cSHeiko Carstens /* Get the token (= pid of the affected task). */ 579f2db2e6cSHeiko Carstens pid = sizeof(void *) == 4 ? param32 : param64; 580f2db2e6cSHeiko Carstens rcu_read_lock(); 581f2db2e6cSHeiko Carstens tsk = find_task_by_pid_ns(pid, &init_pid_ns); 582f2db2e6cSHeiko Carstens if (tsk) 583f2db2e6cSHeiko Carstens get_task_struct(tsk); 584f2db2e6cSHeiko Carstens rcu_read_unlock(); 585f2db2e6cSHeiko Carstens if (!tsk) 586f2db2e6cSHeiko Carstens return; 587f2db2e6cSHeiko Carstens spin_lock(&pfault_lock); 5881da177e4SLinus Torvalds if (subcode & 0x0080) { 5891da177e4SLinus Torvalds /* signal bit is set -> a page has been swapped in by VM */ 590f2db2e6cSHeiko Carstens if (tsk->thread.pfault_wait == 1) { 5911da177e4SLinus Torvalds /* Initial interrupt was faster than the completion 5921da177e4SLinus Torvalds * interrupt. pfault_wait is valid. Set pfault_wait 5931da177e4SLinus Torvalds * back to zero and wake up the process. This can 5941da177e4SLinus Torvalds * safely be done because the task is still sleeping 595b6d09449SMartin Schwidefsky * and can't produce new pfaults. */ 5961da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 597f2db2e6cSHeiko Carstens list_del(&tsk->thread.list); 5981da177e4SLinus Torvalds wake_up_process(tsk); 599d5e50a51SHeiko Carstens put_task_struct(tsk); 600f2db2e6cSHeiko Carstens } else { 601f2db2e6cSHeiko Carstens /* Completion interrupt was faster than initial 602f2db2e6cSHeiko Carstens * interrupt. Set pfault_wait to -1 so the initial 603fa2fb2f4SHeiko Carstens * interrupt doesn't put the task to sleep. 604fa2fb2f4SHeiko Carstens * If the task is not running, ignore the completion 605fa2fb2f4SHeiko Carstens * interrupt since it must be a leftover of a PFAULT 606fa2fb2f4SHeiko Carstens * CANCEL operation which didn't remove all pending 607fa2fb2f4SHeiko Carstens * completion interrupts. */ 608fa2fb2f4SHeiko Carstens if (tsk->state == TASK_RUNNING) 609f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = -1; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds } else { 6121da177e4SLinus Torvalds /* signal bit not set -> a real page is missing. */ 613d49f47f8SHeiko Carstens if (WARN_ON_ONCE(tsk != current)) 614d49f47f8SHeiko Carstens goto out; 615d5e50a51SHeiko Carstens if (tsk->thread.pfault_wait == 1) { 616d5e50a51SHeiko Carstens /* Already on the list with a reference: put to sleep */ 6170a16ba78SHeiko Carstens __set_task_state(tsk, TASK_UNINTERRUPTIBLE); 618d5e50a51SHeiko Carstens set_tsk_need_resched(tsk); 619d5e50a51SHeiko Carstens } else if (tsk->thread.pfault_wait == -1) { 6201da177e4SLinus Torvalds /* Completion interrupt was faster than the initial 621f2db2e6cSHeiko Carstens * interrupt (pfault_wait == -1). Set pfault_wait 622f2db2e6cSHeiko Carstens * back to zero and exit. */ 6231da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 624f2db2e6cSHeiko Carstens } else { 625f2db2e6cSHeiko Carstens /* Initial interrupt arrived before completion 626d5e50a51SHeiko Carstens * interrupt. Let the task sleep. 627d5e50a51SHeiko Carstens * An extra task reference is needed since a different 628d5e50a51SHeiko Carstens * cpu may set the task state to TASK_RUNNING again 629d5e50a51SHeiko Carstens * before the scheduler is reached. */ 630d5e50a51SHeiko Carstens get_task_struct(tsk); 631f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = 1; 632f2db2e6cSHeiko Carstens list_add(&tsk->thread.list, &pfault_list); 6330a16ba78SHeiko Carstens __set_task_state(tsk, TASK_UNINTERRUPTIBLE); 6341da177e4SLinus Torvalds set_tsk_need_resched(tsk); 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds } 637d49f47f8SHeiko Carstens out: 638f2db2e6cSHeiko Carstens spin_unlock(&pfault_lock); 63954c27791SHeiko Carstens put_task_struct(tsk); 640f2db2e6cSHeiko Carstens } 641f2db2e6cSHeiko Carstens 642f2db2e6cSHeiko Carstens static int __cpuinit pfault_cpu_notify(struct notifier_block *self, 643f2db2e6cSHeiko Carstens unsigned long action, void *hcpu) 644f2db2e6cSHeiko Carstens { 645f2db2e6cSHeiko Carstens struct thread_struct *thread, *next; 646f2db2e6cSHeiko Carstens struct task_struct *tsk; 647f2db2e6cSHeiko Carstens 6481c725922SHeiko Carstens switch (action & ~CPU_TASKS_FROZEN) { 649f2db2e6cSHeiko Carstens case CPU_DEAD: 650f2db2e6cSHeiko Carstens spin_lock_irq(&pfault_lock); 651f2db2e6cSHeiko Carstens list_for_each_entry_safe(thread, next, &pfault_list, list) { 652f2db2e6cSHeiko Carstens thread->pfault_wait = 0; 653f2db2e6cSHeiko Carstens list_del(&thread->list); 654f2db2e6cSHeiko Carstens tsk = container_of(thread, struct task_struct, thread); 655f2db2e6cSHeiko Carstens wake_up_process(tsk); 656d5e50a51SHeiko Carstens put_task_struct(tsk); 657f2db2e6cSHeiko Carstens } 658f2db2e6cSHeiko Carstens spin_unlock_irq(&pfault_lock); 659f2db2e6cSHeiko Carstens break; 660f2db2e6cSHeiko Carstens default: 661f2db2e6cSHeiko Carstens break; 662f2db2e6cSHeiko Carstens } 663f2db2e6cSHeiko Carstens return NOTIFY_OK; 664f2db2e6cSHeiko Carstens } 6651da177e4SLinus Torvalds 666fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void) 66729b08d2bSHeiko Carstens { 668fb0a9d7eSHeiko Carstens int rc; 66929b08d2bSHeiko Carstens 670fb0a9d7eSHeiko Carstens rc = register_external_interrupt(0x2603, pfault_interrupt); 6717dd8fe1fSHeiko Carstens if (rc) 6727dd8fe1fSHeiko Carstens goto out_extint; 6737dd8fe1fSHeiko Carstens rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP; 6747dd8fe1fSHeiko Carstens if (rc) 6757dd8fe1fSHeiko Carstens goto out_pfault; 676df7997abSHeiko Carstens service_subclass_irq_register(); 6777dd8fe1fSHeiko Carstens hotcpu_notifier(pfault_cpu_notify, 0); 6787dd8fe1fSHeiko Carstens return 0; 6797dd8fe1fSHeiko Carstens 6807dd8fe1fSHeiko Carstens out_pfault: 6817dd8fe1fSHeiko Carstens unregister_external_interrupt(0x2603, pfault_interrupt); 6827dd8fe1fSHeiko Carstens out_extint: 683fb0a9d7eSHeiko Carstens pfault_disable = 1; 684fb0a9d7eSHeiko Carstens return rc; 685fb0a9d7eSHeiko Carstens } 686fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init); 687fb0a9d7eSHeiko Carstens 6887dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */ 689