1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * S390 version 4a53c8fabSHeiko Carstens * Copyright IBM Corp. 1999 51da177e4SLinus Torvalds * Author(s): Hartmut Penner (hp@de.ibm.com) 61da177e4SLinus Torvalds * Ulrich Weigand (uweigand@de.ibm.com) 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Derived from "arch/i386/mm/fault.c" 91da177e4SLinus Torvalds * Copyright (C) 1995 Linus Torvalds 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 12052ff461SHeiko Carstens #include <linux/kernel_stat.h> 13cdd6c482SIngo Molnar #include <linux/perf_event.h> 141da177e4SLinus Torvalds #include <linux/signal.h> 151da177e4SLinus Torvalds #include <linux/sched.h> 16b17b0153SIngo Molnar #include <linux/sched/debug.h> 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/errno.h> 191da177e4SLinus Torvalds #include <linux/string.h> 201da177e4SLinus Torvalds #include <linux/types.h> 211da177e4SLinus Torvalds #include <linux/ptrace.h> 221da177e4SLinus Torvalds #include <linux/mman.h> 231da177e4SLinus Torvalds #include <linux/mm.h> 247757591aSHeiko Carstens #include <linux/compat.h> 251da177e4SLinus Torvalds #include <linux/smp.h> 261eeb66a1SChristoph Hellwig #include <linux/kdebug.h> 271da177e4SLinus Torvalds #include <linux/init.h> 281da177e4SLinus Torvalds #include <linux/console.h> 29dcc096c5SPaul Gortmaker #include <linux/extable.h> 301da177e4SLinus Torvalds #include <linux/hardirq.h> 314ba069b8SMichael Grundy #include <linux/kprobes.h> 32be5ec363SMartin Schwidefsky #include <linux/uaccess.h> 3353492b1dSGerald Schaefer #include <linux/hugetlb.h> 34cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 351ec2772eSMartin Schwidefsky #include <asm/diag.h> 361da177e4SLinus Torvalds #include <asm/pgtable.h> 371e133ab2SMartin Schwidefsky #include <asm/gmap.h> 38d7b250e2SHeiko Carstens #include <asm/irq.h> 396252d702SMartin Schwidefsky #include <asm/mmu_context.h> 40a0616cdeSDavid Howells #include <asm/facility.h> 41a806170eSHeiko Carstens #include "../kernel/entry.h" 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #define __FAIL_ADDR_MASK -4096L 441da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0600 451da177e4SLinus Torvalds #define __PF_RES_FIELD 0x8000000000000000ULL 461da177e4SLinus Torvalds 4750d7280dSMartin Schwidefsky #define VM_FAULT_BADCONTEXT 0x010000 4850d7280dSMartin Schwidefsky #define VM_FAULT_BADMAP 0x020000 4950d7280dSMartin Schwidefsky #define VM_FAULT_BADACCESS 0x040000 50f2c76e3bSHeiko Carstens #define VM_FAULT_SIGNAL 0x080000 5124eb3a82SDominik Dingel #define VM_FAULT_PFAULT 0x100000 5250d7280dSMartin Schwidefsky 530aaba41bSMartin Schwidefsky enum fault_type { 540aaba41bSMartin Schwidefsky KERNEL_FAULT, 550aaba41bSMartin Schwidefsky USER_FAULT, 560aaba41bSMartin Schwidefsky VDSO_FAULT, 570aaba41bSMartin Schwidefsky GMAP_FAULT, 580aaba41bSMartin Schwidefsky }; 590aaba41bSMartin Schwidefsky 60a4f32bdbSHeiko Carstens static unsigned long store_indication __read_mostly; 6192f842eaSMartin Schwidefsky 62a4f32bdbSHeiko Carstens static int __init fault_init(void) 6392f842eaSMartin Schwidefsky { 64a4f32bdbSHeiko Carstens if (test_facility(75)) 6592f842eaSMartin Schwidefsky store_indication = 0xc00; 66a4f32bdbSHeiko Carstens return 0; 6792f842eaSMartin Schwidefsky } 68a4f32bdbSHeiko Carstens early_initcall(fault_init); 6992f842eaSMartin Schwidefsky 707ecb344aSMartin Schwidefsky static inline int notify_page_fault(struct pt_regs *regs) 7110c1031fSMartin Schwidefsky { 7233464e3bSChristoph Hellwig int ret = 0; 7333464e3bSChristoph Hellwig 7433464e3bSChristoph Hellwig /* kprobe_running() needs smp_processor_id() */ 7522e0a046SHeiko Carstens if (kprobes_built_in() && !user_mode(regs)) { 7633464e3bSChristoph Hellwig preempt_disable(); 7733464e3bSChristoph Hellwig if (kprobe_running() && kprobe_fault_handler(regs, 14)) 7833464e3bSChristoph Hellwig ret = 1; 7933464e3bSChristoph Hellwig preempt_enable(); 8033464e3bSChristoph Hellwig } 8133464e3bSChristoph Hellwig return ret; 824ba069b8SMichael Grundy } 834ba069b8SMichael Grundy 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* 861da177e4SLinus Torvalds * Unlock any spinlocks which will prevent us from getting the 87cefc8be8SKirill Korotaev * message out. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds void bust_spinlocks(int yes) 901da177e4SLinus Torvalds { 911da177e4SLinus Torvalds if (yes) { 921da177e4SLinus Torvalds oops_in_progress = 1; 931da177e4SLinus Torvalds } else { 941da177e4SLinus Torvalds int loglevel_save = console_loglevel; 951da177e4SLinus Torvalds console_unblank(); 961da177e4SLinus Torvalds oops_in_progress = 0; 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * OK, the message is on the console. Now we call printk() 991da177e4SLinus Torvalds * without oops_in_progress set so that printk will give klogd 1001da177e4SLinus Torvalds * a poke. Hold onto your hats... 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds console_loglevel = 15; 1031da177e4SLinus Torvalds printk(" "); 1041da177e4SLinus Torvalds console_loglevel = loglevel_save; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 1090aaba41bSMartin Schwidefsky * Find out which address space caused the exception. 1100aaba41bSMartin Schwidefsky * Access register mode is impossible, ignore space == 3. 1111da177e4SLinus Torvalds */ 1120aaba41bSMartin Schwidefsky static inline enum fault_type get_fault_type(struct pt_regs *regs) 1131da177e4SLinus Torvalds { 114457f2180SHeiko Carstens unsigned long trans_exc_code; 115457f2180SHeiko Carstens 116457f2180SHeiko Carstens trans_exc_code = regs->int_parm_long & 3; 1170aaba41bSMartin Schwidefsky if (likely(trans_exc_code == 0)) { 1180aaba41bSMartin Schwidefsky /* primary space exception */ 1190aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && 1200aaba41bSMartin Schwidefsky test_pt_regs_flag(regs, PIF_GUEST_FAULT)) 1210aaba41bSMartin Schwidefsky return GMAP_FAULT; 1220aaba41bSMartin Schwidefsky if (current->thread.mm_segment == USER_DS) 1230aaba41bSMartin Schwidefsky return USER_FAULT; 1240aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1250aaba41bSMartin Schwidefsky } 1260aaba41bSMartin Schwidefsky if (trans_exc_code == 2) { 1270aaba41bSMartin Schwidefsky /* secondary space exception */ 1280aaba41bSMartin Schwidefsky if (current->thread.mm_segment & 1) { 1290aaba41bSMartin Schwidefsky if (current->thread.mm_segment == USER_DS_SACF) 1300aaba41bSMartin Schwidefsky return USER_FAULT; 1310aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1320aaba41bSMartin Schwidefsky } 1330aaba41bSMartin Schwidefsky return VDSO_FAULT; 1340aaba41bSMartin Schwidefsky } 1350aaba41bSMartin Schwidefsky /* home space exception -> access via kernel ASCE */ 1360aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1393b7df342SHeiko Carstens static int bad_address(void *p) 1403b7df342SHeiko Carstens { 1413b7df342SHeiko Carstens unsigned long dummy; 1423b7df342SHeiko Carstens 1433b7df342SHeiko Carstens return probe_kernel_address((unsigned long *)p, dummy); 1443b7df342SHeiko Carstens } 1453b7df342SHeiko Carstens 1463b7df342SHeiko Carstens static void dump_pagetable(unsigned long asce, unsigned long address) 1473b7df342SHeiko Carstens { 148fe7b2747SHeiko Carstens unsigned long *table = __va(asce & _ASCE_ORIGIN); 1493b7df342SHeiko Carstens 1503b7df342SHeiko Carstens pr_alert("AS:%016lx ", asce); 1513b7df342SHeiko Carstens switch (asce & _ASCE_TYPE_MASK) { 1523b7df342SHeiko Carstens case _ASCE_TYPE_REGION1: 153f1c1174fSHeiko Carstens table += (address & _REGION1_INDEX) >> _REGION1_SHIFT; 1543b7df342SHeiko Carstens if (bad_address(table)) 1553b7df342SHeiko Carstens goto bad; 1563b7df342SHeiko Carstens pr_cont("R1:%016lx ", *table); 1573b7df342SHeiko Carstens if (*table & _REGION_ENTRY_INVALID) 1583b7df342SHeiko Carstens goto out; 1593b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1603b7df342SHeiko Carstens /* fallthrough */ 1613b7df342SHeiko Carstens case _ASCE_TYPE_REGION2: 162f1c1174fSHeiko Carstens table += (address & _REGION2_INDEX) >> _REGION2_SHIFT; 1633b7df342SHeiko Carstens if (bad_address(table)) 1643b7df342SHeiko Carstens goto bad; 1653b7df342SHeiko Carstens pr_cont("R2:%016lx ", *table); 1663b7df342SHeiko Carstens if (*table & _REGION_ENTRY_INVALID) 1673b7df342SHeiko Carstens goto out; 1683b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1693b7df342SHeiko Carstens /* fallthrough */ 1703b7df342SHeiko Carstens case _ASCE_TYPE_REGION3: 171f1c1174fSHeiko Carstens table += (address & _REGION3_INDEX) >> _REGION3_SHIFT; 1723b7df342SHeiko Carstens if (bad_address(table)) 1733b7df342SHeiko Carstens goto bad; 1743b7df342SHeiko Carstens pr_cont("R3:%016lx ", *table); 1753b7df342SHeiko Carstens if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE)) 1763b7df342SHeiko Carstens goto out; 1773b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1783b7df342SHeiko Carstens /* fallthrough */ 1793b7df342SHeiko Carstens case _ASCE_TYPE_SEGMENT: 180f1c1174fSHeiko Carstens table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT; 1813b7df342SHeiko Carstens if (bad_address(table)) 1823b7df342SHeiko Carstens goto bad; 18391c0837eSJoe Perches pr_cont("S:%016lx ", *table); 1843b7df342SHeiko Carstens if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE)) 1853b7df342SHeiko Carstens goto out; 1863b7df342SHeiko Carstens table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN); 1873b7df342SHeiko Carstens } 188f1c1174fSHeiko Carstens table += (address & _PAGE_INDEX) >> _PAGE_SHIFT; 1893b7df342SHeiko Carstens if (bad_address(table)) 1903b7df342SHeiko Carstens goto bad; 1913b7df342SHeiko Carstens pr_cont("P:%016lx ", *table); 1923b7df342SHeiko Carstens out: 1933b7df342SHeiko Carstens pr_cont("\n"); 1943b7df342SHeiko Carstens return; 1953b7df342SHeiko Carstens bad: 1963b7df342SHeiko Carstens pr_cont("BAD\n"); 1973b7df342SHeiko Carstens } 1983b7df342SHeiko Carstens 1993b7df342SHeiko Carstens static void dump_fault_info(struct pt_regs *regs) 2003b7df342SHeiko Carstens { 2013b7df342SHeiko Carstens unsigned long asce; 2023b7df342SHeiko Carstens 2035d7eccecSHeiko Carstens pr_alert("Failing address: %016lx TEID: %016lx\n", 2045d7eccecSHeiko Carstens regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long); 2053b7df342SHeiko Carstens pr_alert("Fault in "); 2063b7df342SHeiko Carstens switch (regs->int_parm_long & 3) { 2073b7df342SHeiko Carstens case 3: 2083b7df342SHeiko Carstens pr_cont("home space "); 2093b7df342SHeiko Carstens break; 2103b7df342SHeiko Carstens case 2: 2113b7df342SHeiko Carstens pr_cont("secondary space "); 2123b7df342SHeiko Carstens break; 2133b7df342SHeiko Carstens case 1: 2143b7df342SHeiko Carstens pr_cont("access register "); 2153b7df342SHeiko Carstens break; 2163b7df342SHeiko Carstens case 0: 2173b7df342SHeiko Carstens pr_cont("primary space "); 2183b7df342SHeiko Carstens break; 2193b7df342SHeiko Carstens } 2203b7df342SHeiko Carstens pr_cont("mode while using "); 2210aaba41bSMartin Schwidefsky switch (get_fault_type(regs)) { 2220aaba41bSMartin Schwidefsky case USER_FAULT: 2233b7df342SHeiko Carstens asce = S390_lowcore.user_asce; 2243b7df342SHeiko Carstens pr_cont("user "); 2250aaba41bSMartin Schwidefsky break; 2260aaba41bSMartin Schwidefsky case VDSO_FAULT: 2270aaba41bSMartin Schwidefsky asce = S390_lowcore.vdso_asce; 2280aaba41bSMartin Schwidefsky pr_cont("vdso "); 2290aaba41bSMartin Schwidefsky break; 2300aaba41bSMartin Schwidefsky case GMAP_FAULT: 2310aaba41bSMartin Schwidefsky asce = ((struct gmap *) S390_lowcore.gmap)->asce; 2320aaba41bSMartin Schwidefsky pr_cont("gmap "); 2330aaba41bSMartin Schwidefsky break; 2340aaba41bSMartin Schwidefsky case KERNEL_FAULT: 2350aaba41bSMartin Schwidefsky asce = S390_lowcore.kernel_asce; 2360aaba41bSMartin Schwidefsky pr_cont("kernel "); 2370aaba41bSMartin Schwidefsky break; 2383b7df342SHeiko Carstens } 2393b7df342SHeiko Carstens pr_cont("ASCE.\n"); 2403b7df342SHeiko Carstens dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK); 2413b7df342SHeiko Carstens } 2423b7df342SHeiko Carstens 2435d7eccecSHeiko Carstens int show_unhandled_signals = 1; 2445d7eccecSHeiko Carstens 2455d7eccecSHeiko Carstens void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault) 246ab3c68eeSHeiko Carstens { 247ab3c68eeSHeiko Carstens if ((task_pid_nr(current) > 1) && !show_unhandled_signals) 248ab3c68eeSHeiko Carstens return; 249ab3c68eeSHeiko Carstens if (!unhandled_signal(current, signr)) 250ab3c68eeSHeiko Carstens return; 251ab3c68eeSHeiko Carstens if (!printk_ratelimit()) 252ab3c68eeSHeiko Carstens return; 253413d4047SHeiko Carstens printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ", 254413d4047SHeiko Carstens regs->int_code & 0xffff, regs->int_code >> 17); 2559cb1ccecSHeiko Carstens print_vma_addr(KERN_CONT "in ", regs->psw.addr); 256aa33c8cbSMartin Schwidefsky printk(KERN_CONT "\n"); 2575d7eccecSHeiko Carstens if (is_mm_fault) 2583b7df342SHeiko Carstens dump_fault_info(regs); 259ab3c68eeSHeiko Carstens show_regs(regs); 260ab3c68eeSHeiko Carstens } 261ab3c68eeSHeiko Carstens 2621da177e4SLinus Torvalds /* 2631da177e4SLinus Torvalds * Send SIGSEGV to task. This is an external routine 2641da177e4SLinus Torvalds * to keep the stack usage of do_page_fault small. 2651da177e4SLinus Torvalds */ 266aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code) 2671da177e4SLinus Torvalds { 2685d7eccecSHeiko Carstens report_user_fault(regs, SIGSEGV, 1); 2699507a5d0SEric W. Biederman force_sig_fault(SIGSEGV, si_code, 2709507a5d0SEric W. Biederman (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK), 2719507a5d0SEric W. Biederman current); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 274aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs) 27510c1031fSMartin Schwidefsky { 27610c1031fSMartin Schwidefsky const struct exception_table_entry *fixup; 27710c1031fSMartin Schwidefsky 27810c1031fSMartin Schwidefsky /* Are we prepared to handle this kernel fault? */ 2799cb1ccecSHeiko Carstens fixup = search_exception_tables(regs->psw.addr); 28010c1031fSMartin Schwidefsky if (fixup) { 281fecc868aSHeiko Carstens regs->psw.addr = extable_fixup(fixup); 28210c1031fSMartin Schwidefsky return; 28310c1031fSMartin Schwidefsky } 28410c1031fSMartin Schwidefsky 28510c1031fSMartin Schwidefsky /* 28610c1031fSMartin Schwidefsky * Oops. The kernel tried to access some bad page. We'll have to 28710c1031fSMartin Schwidefsky * terminate things with extreme prejudice. 28810c1031fSMartin Schwidefsky */ 2890aaba41bSMartin Schwidefsky if (get_fault_type(regs) == KERNEL_FAULT) 29010c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel pointer dereference" 2913b7df342SHeiko Carstens " in virtual kernel address space\n"); 29210c1031fSMartin Schwidefsky else 29310c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel paging request" 2943b7df342SHeiko Carstens " in virtual user address space\n"); 2953b7df342SHeiko Carstens dump_fault_info(regs); 296aa33c8cbSMartin Schwidefsky die(regs, "Oops"); 29710c1031fSMartin Schwidefsky do_exit(SIGKILL); 29810c1031fSMartin Schwidefsky } 29910c1031fSMartin Schwidefsky 300aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs) 30110c1031fSMartin Schwidefsky { 30210c1031fSMartin Schwidefsky /* Low-address protection hit in kernel mode means 30310c1031fSMartin Schwidefsky NULL pointer write access in kernel mode. */ 30410c1031fSMartin Schwidefsky if (regs->psw.mask & PSW_MASK_PSTATE) { 30510c1031fSMartin Schwidefsky /* Low-address protection hit in user mode 'cannot happen'. */ 306aa33c8cbSMartin Schwidefsky die (regs, "Low-address protection"); 30710c1031fSMartin Schwidefsky do_exit(SIGKILL); 30810c1031fSMartin Schwidefsky } 30910c1031fSMartin Schwidefsky 310aa33c8cbSMartin Schwidefsky do_no_context(regs); 31110c1031fSMartin Schwidefsky } 31210c1031fSMartin Schwidefsky 313aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs) 31410c1031fSMartin Schwidefsky { 31510c1031fSMartin Schwidefsky /* 31610c1031fSMartin Schwidefsky * Send a sigbus, regardless of whether we were in kernel 31710c1031fSMartin Schwidefsky * or user mode. 31810c1031fSMartin Schwidefsky */ 3199507a5d0SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRERR, 3209507a5d0SEric W. Biederman (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK), 3219507a5d0SEric W. Biederman current); 32210c1031fSMartin Schwidefsky } 32310c1031fSMartin Schwidefsky 32457d7f939SMartin Schwidefsky static noinline int signal_return(struct pt_regs *regs) 32557d7f939SMartin Schwidefsky { 32657d7f939SMartin Schwidefsky u16 instruction; 32757d7f939SMartin Schwidefsky int rc; 32857d7f939SMartin Schwidefsky 32957d7f939SMartin Schwidefsky rc = __get_user(instruction, (u16 __user *) regs->psw.addr); 33057d7f939SMartin Schwidefsky if (rc) 33157d7f939SMartin Schwidefsky return rc; 33257d7f939SMartin Schwidefsky if (instruction == 0x0a77) { 33357d7f939SMartin Schwidefsky set_pt_regs_flag(regs, PIF_SYSCALL); 33457d7f939SMartin Schwidefsky regs->int_code = 0x00040077; 33557d7f939SMartin Schwidefsky return 0; 33657d7f939SMartin Schwidefsky } else if (instruction == 0x0aad) { 33757d7f939SMartin Schwidefsky set_pt_regs_flag(regs, PIF_SYSCALL); 33857d7f939SMartin Schwidefsky regs->int_code = 0x000400ad; 33957d7f939SMartin Schwidefsky return 0; 34057d7f939SMartin Schwidefsky } 34157d7f939SMartin Schwidefsky return -EACCES; 34257d7f939SMartin Schwidefsky } 34357d7f939SMartin Schwidefsky 34457d7f939SMartin Schwidefsky static noinline void do_fault_error(struct pt_regs *regs, int access, int fault) 34550d7280dSMartin Schwidefsky { 34650d7280dSMartin Schwidefsky int si_code; 34750d7280dSMartin Schwidefsky 34850d7280dSMartin Schwidefsky switch (fault) { 34950d7280dSMartin Schwidefsky case VM_FAULT_BADACCESS: 35057d7f939SMartin Schwidefsky if (access == VM_EXEC && signal_return(regs) == 0) 35157d7f939SMartin Schwidefsky break; 35250d7280dSMartin Schwidefsky case VM_FAULT_BADMAP: 35350d7280dSMartin Schwidefsky /* Bad memory access. Check if it is kernel or user space. */ 3547d256175SHeiko Carstens if (user_mode(regs)) { 35550d7280dSMartin Schwidefsky /* User mode accesses just cause a SIGSEGV */ 35650d7280dSMartin Schwidefsky si_code = (fault == VM_FAULT_BADMAP) ? 35750d7280dSMartin Schwidefsky SEGV_MAPERR : SEGV_ACCERR; 358aa33c8cbSMartin Schwidefsky do_sigsegv(regs, si_code); 35957d7f939SMartin Schwidefsky break; 36050d7280dSMartin Schwidefsky } 36150d7280dSMartin Schwidefsky case VM_FAULT_BADCONTEXT: 36224eb3a82SDominik Dingel case VM_FAULT_PFAULT: 363aa33c8cbSMartin Schwidefsky do_no_context(regs); 36450d7280dSMartin Schwidefsky break; 365f2c76e3bSHeiko Carstens case VM_FAULT_SIGNAL: 366f2c76e3bSHeiko Carstens if (!user_mode(regs)) 367f2c76e3bSHeiko Carstens do_no_context(regs); 368f2c76e3bSHeiko Carstens break; 36950d7280dSMartin Schwidefsky default: /* fault & VM_FAULT_ERROR */ 37099583181SHeiko Carstens if (fault & VM_FAULT_OOM) { 3717d256175SHeiko Carstens if (!user_mode(regs)) 372aa33c8cbSMartin Schwidefsky do_no_context(regs); 37399583181SHeiko Carstens else 37450d7280dSMartin Schwidefsky pagefault_out_of_memory(); 37533692f27SLinus Torvalds } else if (fault & VM_FAULT_SIGSEGV) { 37633692f27SLinus Torvalds /* Kernel mode? Handle exceptions or die */ 37733692f27SLinus Torvalds if (!user_mode(regs)) 37833692f27SLinus Torvalds do_no_context(regs); 37933692f27SLinus Torvalds else 38033692f27SLinus Torvalds do_sigsegv(regs, SEGV_MAPERR); 38199583181SHeiko Carstens } else if (fault & VM_FAULT_SIGBUS) { 38250d7280dSMartin Schwidefsky /* Kernel mode? Handle exceptions or die */ 3837d256175SHeiko Carstens if (!user_mode(regs)) 384aa33c8cbSMartin Schwidefsky do_no_context(regs); 38536bf9680SMartin Schwidefsky else 386aa33c8cbSMartin Schwidefsky do_sigbus(regs); 38750d7280dSMartin Schwidefsky } else 38850d7280dSMartin Schwidefsky BUG(); 38950d7280dSMartin Schwidefsky break; 39050d7280dSMartin Schwidefsky } 39150d7280dSMartin Schwidefsky } 39250d7280dSMartin Schwidefsky 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * This routine handles page faults. It determines the address, 3951da177e4SLinus Torvalds * and the problem, and then passes it off to one of the appropriate 3961da177e4SLinus Torvalds * routines. 3971da177e4SLinus Torvalds * 39850d7280dSMartin Schwidefsky * interruption code (int_code): 3991da177e4SLinus Torvalds * 04 Protection -> Write-Protection (suprression) 4001da177e4SLinus Torvalds * 10 Segment translation -> Not present (nullification) 4011da177e4SLinus Torvalds * 11 Page translation -> Not present (nullification) 4021da177e4SLinus Torvalds * 3b Region third trans. -> Not present (nullification) 4031da177e4SLinus Torvalds */ 404aa33c8cbSMartin Schwidefsky static inline int do_exception(struct pt_regs *regs, int access) 4051da177e4SLinus Torvalds { 40624eb3a82SDominik Dingel struct gmap *gmap; 4071da177e4SLinus Torvalds struct task_struct *tsk; 4081da177e4SLinus Torvalds struct mm_struct *mm; 4091da177e4SLinus Torvalds struct vm_area_struct *vma; 4100aaba41bSMartin Schwidefsky enum fault_type type; 411aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 4121da177e4SLinus Torvalds unsigned long address; 41333ce6140SHeiko Carstens unsigned int flags; 41433ce6140SHeiko Carstens int fault; 41510c1031fSMartin Schwidefsky 41639efd4ecSMartin Schwidefsky tsk = current; 41739efd4ecSMartin Schwidefsky /* 41839efd4ecSMartin Schwidefsky * The instruction that caused the program check has 41939efd4ecSMartin Schwidefsky * been nullified. Don't signal single step via SIGTRAP. 42039efd4ecSMartin Schwidefsky */ 421d3a73acbSMartin Schwidefsky clear_pt_regs_flag(regs, PIF_PER_TRAP); 42239efd4ecSMartin Schwidefsky 4237ecb344aSMartin Schwidefsky if (notify_page_fault(regs)) 42450d7280dSMartin Schwidefsky return 0; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds mm = tsk->mm; 427aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* 4301da177e4SLinus Torvalds * Verify that the fault happened in user space, that 4311da177e4SLinus Torvalds * we are not in an interrupt and that there is a 4321da177e4SLinus Torvalds * user context. 4331da177e4SLinus Torvalds */ 43450d7280dSMartin Schwidefsky fault = VM_FAULT_BADCONTEXT; 4350aaba41bSMartin Schwidefsky type = get_fault_type(regs); 4360aaba41bSMartin Schwidefsky switch (type) { 4370aaba41bSMartin Schwidefsky case KERNEL_FAULT: 43850d7280dSMartin Schwidefsky goto out; 4390aaba41bSMartin Schwidefsky case VDSO_FAULT: 4400aaba41bSMartin Schwidefsky fault = VM_FAULT_BADMAP; 4410aaba41bSMartin Schwidefsky goto out; 4420aaba41bSMartin Schwidefsky case USER_FAULT: 4430aaba41bSMartin Schwidefsky case GMAP_FAULT: 4440aaba41bSMartin Schwidefsky if (faulthandler_disabled() || !mm) 4450aaba41bSMartin Schwidefsky goto out; 4460aaba41bSMartin Schwidefsky break; 4470aaba41bSMartin Schwidefsky } 4481da177e4SLinus Torvalds 44961365e13SMartin Schwidefsky address = trans_exc_code & __FAIL_ADDR_MASK; 450a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 451f2c76e3bSHeiko Carstens flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 452759496baSJohannes Weiner if (user_mode(regs)) 453759496baSJohannes Weiner flags |= FAULT_FLAG_USER; 45433ce6140SHeiko Carstens if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400) 45533ce6140SHeiko Carstens flags |= FAULT_FLAG_WRITE; 4561da177e4SLinus Torvalds down_read(&mm->mmap_sem); 4571da177e4SLinus Torvalds 4580aaba41bSMartin Schwidefsky gmap = NULL; 4590aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) { 4600aaba41bSMartin Schwidefsky gmap = (struct gmap *) S390_lowcore.gmap; 461527e30b4SMartin Schwidefsky current->thread.gmap_addr = address; 4624be130a0SMartin Schwidefsky current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE); 4634a494439SDavid Hildenbrand current->thread.gmap_int_code = regs->int_code & 0xffff; 464527e30b4SMartin Schwidefsky address = __gmap_translate(gmap, address); 465e5992f2eSMartin Schwidefsky if (address == -EFAULT) { 466e5992f2eSMartin Schwidefsky fault = VM_FAULT_BADMAP; 467e5992f2eSMartin Schwidefsky goto out_up; 468e5992f2eSMartin Schwidefsky } 46924eb3a82SDominik Dingel if (gmap->pfault_enabled) 47024eb3a82SDominik Dingel flags |= FAULT_FLAG_RETRY_NOWAIT; 471e5992f2eSMartin Schwidefsky } 472e5992f2eSMartin Schwidefsky 473e5992f2eSMartin Schwidefsky retry: 47450d7280dSMartin Schwidefsky fault = VM_FAULT_BADMAP; 4751da177e4SLinus Torvalds vma = find_vma(mm, address); 4761da177e4SLinus Torvalds if (!vma) 47750d7280dSMartin Schwidefsky goto out_up; 478c1821c2eSGerald Schaefer 47950d7280dSMartin Schwidefsky if (unlikely(vma->vm_start > address)) { 4801da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 48150d7280dSMartin Schwidefsky goto out_up; 4821da177e4SLinus Torvalds if (expand_stack(vma, address)) 48350d7280dSMartin Schwidefsky goto out_up; 48450d7280dSMartin Schwidefsky } 48550d7280dSMartin Schwidefsky 4861da177e4SLinus Torvalds /* 4871da177e4SLinus Torvalds * Ok, we have a good vm_area for this memory access, so 4881da177e4SLinus Torvalds * we can handle it.. 4891da177e4SLinus Torvalds */ 49050d7280dSMartin Schwidefsky fault = VM_FAULT_BADACCESS; 4911ab947deSMartin Schwidefsky if (unlikely(!(vma->vm_flags & access))) 49250d7280dSMartin Schwidefsky goto out_up; 4931da177e4SLinus Torvalds 49453492b1dSGerald Schaefer if (is_vm_hugetlb_page(vma)) 49553492b1dSGerald Schaefer address &= HPAGE_MASK; 4961da177e4SLinus Torvalds /* 4971da177e4SLinus Torvalds * If for any reason at all we couldn't handle the fault, 4981da177e4SLinus Torvalds * make sure we exit gracefully rather than endlessly redo 4991da177e4SLinus Torvalds * the fault. 5001da177e4SLinus Torvalds */ 501dcddffd4SKirill A. Shutemov fault = handle_mm_fault(vma, address, flags); 502f2c76e3bSHeiko Carstens /* No reason to continue if interrupted by SIGKILL. */ 503f2c76e3bSHeiko Carstens if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) { 504f2c76e3bSHeiko Carstens fault = VM_FAULT_SIGNAL; 505f2c76e3bSHeiko Carstens goto out; 506f2c76e3bSHeiko Carstens } 50750d7280dSMartin Schwidefsky if (unlikely(fault & VM_FAULT_ERROR)) 50850d7280dSMartin Schwidefsky goto out_up; 50950d7280dSMartin Schwidefsky 51033ce6140SHeiko Carstens /* 51133ce6140SHeiko Carstens * Major/minor page fault accounting is only done on the 51233ce6140SHeiko Carstens * initial attempt. If we go through a retry, it is extremely 51333ce6140SHeiko Carstens * likely that the page will be found in page cache at that point. 51433ce6140SHeiko Carstens */ 51533ce6140SHeiko Carstens if (flags & FAULT_FLAG_ALLOW_RETRY) { 516bde69af2SHeiko Carstens if (fault & VM_FAULT_MAJOR) { 51783c54070SNick Piggin tsk->maj_flt++; 518a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 519bde69af2SHeiko Carstens regs, address); 520bde69af2SHeiko Carstens } else { 52183c54070SNick Piggin tsk->min_flt++; 522a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 523bde69af2SHeiko Carstens regs, address); 524bde69af2SHeiko Carstens } 52533ce6140SHeiko Carstens if (fault & VM_FAULT_RETRY) { 5260aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && gmap && 5270aaba41bSMartin Schwidefsky (flags & FAULT_FLAG_RETRY_NOWAIT)) { 52824eb3a82SDominik Dingel /* FAULT_FLAG_RETRY_NOWAIT has been set, 52924eb3a82SDominik Dingel * mmap_sem has not been released */ 53024eb3a82SDominik Dingel current->thread.gmap_pfault = 1; 53124eb3a82SDominik Dingel fault = VM_FAULT_PFAULT; 53224eb3a82SDominik Dingel goto out_up; 53324eb3a82SDominik Dingel } 53433ce6140SHeiko Carstens /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk 53533ce6140SHeiko Carstens * of starvation. */ 53624eb3a82SDominik Dingel flags &= ~(FAULT_FLAG_ALLOW_RETRY | 53724eb3a82SDominik Dingel FAULT_FLAG_RETRY_NOWAIT); 53845cac65bSShaohua Li flags |= FAULT_FLAG_TRIED; 539e5992f2eSMartin Schwidefsky down_read(&mm->mmap_sem); 54033ce6140SHeiko Carstens goto retry; 54133ce6140SHeiko Carstens } 54233ce6140SHeiko Carstens } 5430aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && gmap) { 544527e30b4SMartin Schwidefsky address = __gmap_link(gmap, current->thread.gmap_addr, 545527e30b4SMartin Schwidefsky address); 546527e30b4SMartin Schwidefsky if (address == -EFAULT) { 547527e30b4SMartin Schwidefsky fault = VM_FAULT_BADMAP; 548527e30b4SMartin Schwidefsky goto out_up; 549527e30b4SMartin Schwidefsky } 550527e30b4SMartin Schwidefsky if (address == -ENOMEM) { 551527e30b4SMartin Schwidefsky fault = VM_FAULT_OOM; 552527e30b4SMartin Schwidefsky goto out_up; 553527e30b4SMartin Schwidefsky } 554527e30b4SMartin Schwidefsky } 55550d7280dSMartin Schwidefsky fault = 0; 55650d7280dSMartin Schwidefsky out_up: 5571da177e4SLinus Torvalds up_read(&mm->mmap_sem); 55850d7280dSMartin Schwidefsky out: 55950d7280dSMartin Schwidefsky return fault; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5627a5388deSHeiko Carstens void do_protection_exception(struct pt_regs *regs) 5631da177e4SLinus Torvalds { 564aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 56557d7f939SMartin Schwidefsky int access, fault; 56661365e13SMartin Schwidefsky 567aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 568f752ac4dSMartin Schwidefsky /* 569f752ac4dSMartin Schwidefsky * Protection exceptions are suppressing, decrement psw address. 570f752ac4dSMartin Schwidefsky * The exception to this rule are aborted transactions, for these 571f752ac4dSMartin Schwidefsky * the PSW already points to the correct location. 572f752ac4dSMartin Schwidefsky */ 573f752ac4dSMartin Schwidefsky if (!(regs->int_code & 0x200)) 574aa33c8cbSMartin Schwidefsky regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16); 57510c1031fSMartin Schwidefsky /* 57610c1031fSMartin Schwidefsky * Check for low-address protection. This needs to be treated 57710c1031fSMartin Schwidefsky * as a special case because the translation exception code 57810c1031fSMartin Schwidefsky * field is not guaranteed to contain valid data in this case. 57910c1031fSMartin Schwidefsky */ 58061365e13SMartin Schwidefsky if (unlikely(!(trans_exc_code & 4))) { 581aa33c8cbSMartin Schwidefsky do_low_address(regs); 58210c1031fSMartin Schwidefsky return; 58310c1031fSMartin Schwidefsky } 58457d7f939SMartin Schwidefsky if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) { 58557d7f939SMartin Schwidefsky regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) | 58657d7f939SMartin Schwidefsky (regs->psw.addr & PAGE_MASK); 58757d7f939SMartin Schwidefsky access = VM_EXEC; 58857d7f939SMartin Schwidefsky fault = VM_FAULT_BADACCESS; 58957d7f939SMartin Schwidefsky } else { 59057d7f939SMartin Schwidefsky access = VM_WRITE; 59157d7f939SMartin Schwidefsky fault = do_exception(regs, access); 59257d7f939SMartin Schwidefsky } 59350d7280dSMartin Schwidefsky if (unlikely(fault)) 59457d7f939SMartin Schwidefsky do_fault_error(regs, access, fault); 5951da177e4SLinus Torvalds } 5967a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_protection_exception); 5971da177e4SLinus Torvalds 5987a5388deSHeiko Carstens void do_dat_exception(struct pt_regs *regs) 5991da177e4SLinus Torvalds { 6001ab947deSMartin Schwidefsky int access, fault; 60150d7280dSMartin Schwidefsky 6021ab947deSMartin Schwidefsky access = VM_READ | VM_EXEC | VM_WRITE; 603aa33c8cbSMartin Schwidefsky fault = do_exception(regs, access); 60450d7280dSMartin Schwidefsky if (unlikely(fault)) 60557d7f939SMartin Schwidefsky do_fault_error(regs, access, fault); 6061da177e4SLinus Torvalds } 6077a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_dat_exception); 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds #ifdef CONFIG_PFAULT 6101da177e4SLinus Torvalds /* 6111da177e4SLinus Torvalds * 'pfault' pseudo page faults routines. 6121da177e4SLinus Torvalds */ 613fb0a9d7eSHeiko Carstens static int pfault_disable; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds static int __init nopfault(char *str) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds pfault_disable = 1; 6181da177e4SLinus Torvalds return 1; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds __setup("nopfault", nopfault); 6221da177e4SLinus Torvalds 6237dd8fe1fSHeiko Carstens struct pfault_refbk { 6247dd8fe1fSHeiko Carstens u16 refdiagc; 6257dd8fe1fSHeiko Carstens u16 reffcode; 6267dd8fe1fSHeiko Carstens u16 refdwlen; 6277dd8fe1fSHeiko Carstens u16 refversn; 6287dd8fe1fSHeiko Carstens u64 refgaddr; 6297dd8fe1fSHeiko Carstens u64 refselmk; 6307dd8fe1fSHeiko Carstens u64 refcmpmk; 6317dd8fe1fSHeiko Carstens u64 reserved; 6327dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8))); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds int pfault_init(void) 6351da177e4SLinus Torvalds { 6367dd8fe1fSHeiko Carstens struct pfault_refbk refbk = { 6377dd8fe1fSHeiko Carstens .refdiagc = 0x258, 6387dd8fe1fSHeiko Carstens .reffcode = 0, 6397dd8fe1fSHeiko Carstens .refdwlen = 5, 6407dd8fe1fSHeiko Carstens .refversn = 2, 641e22cf8caSChristian Borntraeger .refgaddr = __LC_LPP, 6427dd8fe1fSHeiko Carstens .refselmk = 1ULL << 48, 6437dd8fe1fSHeiko Carstens .refcmpmk = 1ULL << 48, 6447dd8fe1fSHeiko Carstens .reserved = __PF_RES_FIELD }; 6451da177e4SLinus Torvalds int rc; 6461da177e4SLinus Torvalds 647f32269a0SCarsten Otte if (pfault_disable) 6481da177e4SLinus Torvalds return -1; 6491ec2772eSMartin Schwidefsky diag_stat_inc(DIAG_STAT_X258); 65094c12cc7SMartin Schwidefsky asm volatile( 6511da177e4SLinus Torvalds " diag %1,%0,0x258\n" 6521da177e4SLinus Torvalds "0: j 2f\n" 6531da177e4SLinus Torvalds "1: la %0,8\n" 6541da177e4SLinus Torvalds "2:\n" 65594c12cc7SMartin Schwidefsky EX_TABLE(0b,1b) 656d4b68996SMartin Schwidefsky : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc"); 6571da177e4SLinus Torvalds return rc; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds void pfault_fini(void) 6611da177e4SLinus Torvalds { 6627dd8fe1fSHeiko Carstens struct pfault_refbk refbk = { 6637dd8fe1fSHeiko Carstens .refdiagc = 0x258, 6647dd8fe1fSHeiko Carstens .reffcode = 1, 6657dd8fe1fSHeiko Carstens .refdwlen = 5, 6667dd8fe1fSHeiko Carstens .refversn = 2, 6677dd8fe1fSHeiko Carstens }; 6681da177e4SLinus Torvalds 669f32269a0SCarsten Otte if (pfault_disable) 6701da177e4SLinus Torvalds return; 6711ec2772eSMartin Schwidefsky diag_stat_inc(DIAG_STAT_X258); 67294c12cc7SMartin Schwidefsky asm volatile( 6731da177e4SLinus Torvalds " diag %0,0,0x258\n" 6746c22c986SHeiko Carstens "0: nopr %%r7\n" 67594c12cc7SMartin Schwidefsky EX_TABLE(0b,0b) 676d4b68996SMartin Schwidefsky : : "a" (&refbk), "m" (refbk) : "cc"); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 679f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock); 680f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list); 681f2db2e6cSHeiko Carstens 6820227f7c4SPeter Zijlstra #define PF_COMPLETE 0x0080 6830227f7c4SPeter Zijlstra 6840227f7c4SPeter Zijlstra /* 6850227f7c4SPeter Zijlstra * The mechanism of our pfault code: if Linux is running as guest, runs a user 6860227f7c4SPeter Zijlstra * space process and the user space process accesses a page that the host has 6870227f7c4SPeter Zijlstra * paged out we get a pfault interrupt. 6880227f7c4SPeter Zijlstra * 6890227f7c4SPeter Zijlstra * This allows us, within the guest, to schedule a different process. Without 6900227f7c4SPeter Zijlstra * this mechanism the host would have to suspend the whole virtual cpu until 6910227f7c4SPeter Zijlstra * the page has been paged in. 6920227f7c4SPeter Zijlstra * 6930227f7c4SPeter Zijlstra * So when we get such an interrupt then we set the state of the current task 6940227f7c4SPeter Zijlstra * to uninterruptible and also set the need_resched flag. Both happens within 6950227f7c4SPeter Zijlstra * interrupt context(!). If we later on want to return to user space we 6960227f7c4SPeter Zijlstra * recognize the need_resched flag and then call schedule(). It's not very 6970227f7c4SPeter Zijlstra * obvious how this works... 6980227f7c4SPeter Zijlstra * 6990227f7c4SPeter Zijlstra * Of course we have a lot of additional fun with the completion interrupt (-> 7000227f7c4SPeter Zijlstra * host signals that a page of a process has been paged in and the process can 7010227f7c4SPeter Zijlstra * continue to run). This interrupt can arrive on any cpu and, since we have 7020227f7c4SPeter Zijlstra * virtual cpus, actually appear before the interrupt that signals that a page 7030227f7c4SPeter Zijlstra * is missing. 7040227f7c4SPeter Zijlstra */ 705fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code, 706f6649a7eSMartin Schwidefsky unsigned int param32, unsigned long param64) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds struct task_struct *tsk; 7091da177e4SLinus Torvalds __u16 subcode; 710f2db2e6cSHeiko Carstens pid_t pid; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* 7130227f7c4SPeter Zijlstra * Get the external interruption subcode & pfault initial/completion 7140227f7c4SPeter Zijlstra * signal bit. VM stores this in the 'cpu address' field associated 7150227f7c4SPeter Zijlstra * with the external interrupt. 7161da177e4SLinus Torvalds */ 717fde15c3aSHeiko Carstens subcode = ext_code.subcode; 7181da177e4SLinus Torvalds if ((subcode & 0xff00) != __SUBCODE_MASK) 7191da177e4SLinus Torvalds return; 720420f42ecSHeiko Carstens inc_irq_stat(IRQEXT_PFL); 721f2db2e6cSHeiko Carstens /* Get the token (= pid of the affected task). */ 722544e8dd7SHendrik Brueckner pid = param64 & LPP_PID_MASK; 723f2db2e6cSHeiko Carstens rcu_read_lock(); 724f2db2e6cSHeiko Carstens tsk = find_task_by_pid_ns(pid, &init_pid_ns); 725f2db2e6cSHeiko Carstens if (tsk) 726f2db2e6cSHeiko Carstens get_task_struct(tsk); 727f2db2e6cSHeiko Carstens rcu_read_unlock(); 728f2db2e6cSHeiko Carstens if (!tsk) 729f2db2e6cSHeiko Carstens return; 730f2db2e6cSHeiko Carstens spin_lock(&pfault_lock); 7310227f7c4SPeter Zijlstra if (subcode & PF_COMPLETE) { 7321da177e4SLinus Torvalds /* signal bit is set -> a page has been swapped in by VM */ 733f2db2e6cSHeiko Carstens if (tsk->thread.pfault_wait == 1) { 7341da177e4SLinus Torvalds /* Initial interrupt was faster than the completion 7351da177e4SLinus Torvalds * interrupt. pfault_wait is valid. Set pfault_wait 7361da177e4SLinus Torvalds * back to zero and wake up the process. This can 7371da177e4SLinus Torvalds * safely be done because the task is still sleeping 738b6d09449SMartin Schwidefsky * and can't produce new pfaults. */ 7391da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 740f2db2e6cSHeiko Carstens list_del(&tsk->thread.list); 7411da177e4SLinus Torvalds wake_up_process(tsk); 742d5e50a51SHeiko Carstens put_task_struct(tsk); 743f2db2e6cSHeiko Carstens } else { 744f2db2e6cSHeiko Carstens /* Completion interrupt was faster than initial 745f2db2e6cSHeiko Carstens * interrupt. Set pfault_wait to -1 so the initial 746fa2fb2f4SHeiko Carstens * interrupt doesn't put the task to sleep. 747fa2fb2f4SHeiko Carstens * If the task is not running, ignore the completion 748fa2fb2f4SHeiko Carstens * interrupt since it must be a leftover of a PFAULT 749fa2fb2f4SHeiko Carstens * CANCEL operation which didn't remove all pending 750fa2fb2f4SHeiko Carstens * completion interrupts. */ 751fa2fb2f4SHeiko Carstens if (tsk->state == TASK_RUNNING) 752f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = -1; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds } else { 7551da177e4SLinus Torvalds /* signal bit not set -> a real page is missing. */ 756d49f47f8SHeiko Carstens if (WARN_ON_ONCE(tsk != current)) 757d49f47f8SHeiko Carstens goto out; 758d5e50a51SHeiko Carstens if (tsk->thread.pfault_wait == 1) { 759d5e50a51SHeiko Carstens /* Already on the list with a reference: put to sleep */ 7600227f7c4SPeter Zijlstra goto block; 761d5e50a51SHeiko Carstens } else if (tsk->thread.pfault_wait == -1) { 7621da177e4SLinus Torvalds /* Completion interrupt was faster than the initial 763f2db2e6cSHeiko Carstens * interrupt (pfault_wait == -1). Set pfault_wait 764f2db2e6cSHeiko Carstens * back to zero and exit. */ 7651da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 766f2db2e6cSHeiko Carstens } else { 767f2db2e6cSHeiko Carstens /* Initial interrupt arrived before completion 768d5e50a51SHeiko Carstens * interrupt. Let the task sleep. 769d5e50a51SHeiko Carstens * An extra task reference is needed since a different 770d5e50a51SHeiko Carstens * cpu may set the task state to TASK_RUNNING again 771d5e50a51SHeiko Carstens * before the scheduler is reached. */ 772d5e50a51SHeiko Carstens get_task_struct(tsk); 773f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = 1; 774f2db2e6cSHeiko Carstens list_add(&tsk->thread.list, &pfault_list); 7750227f7c4SPeter Zijlstra block: 7760227f7c4SPeter Zijlstra /* Since this must be a userspace fault, there 7770227f7c4SPeter Zijlstra * is no kernel task state to trample. Rely on the 7780227f7c4SPeter Zijlstra * return to userspace schedule() to block. */ 7790227f7c4SPeter Zijlstra __set_current_state(TASK_UNINTERRUPTIBLE); 7801da177e4SLinus Torvalds set_tsk_need_resched(tsk); 781c360192bSMartin Schwidefsky set_preempt_need_resched(); 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds } 784d49f47f8SHeiko Carstens out: 785f2db2e6cSHeiko Carstens spin_unlock(&pfault_lock); 78654c27791SHeiko Carstens put_task_struct(tsk); 787f2db2e6cSHeiko Carstens } 788f2db2e6cSHeiko Carstens 78984c9ceefSSebastian Andrzej Siewior static int pfault_cpu_dead(unsigned int cpu) 790f2db2e6cSHeiko Carstens { 791f2db2e6cSHeiko Carstens struct thread_struct *thread, *next; 792f2db2e6cSHeiko Carstens struct task_struct *tsk; 793f2db2e6cSHeiko Carstens 794f2db2e6cSHeiko Carstens spin_lock_irq(&pfault_lock); 795f2db2e6cSHeiko Carstens list_for_each_entry_safe(thread, next, &pfault_list, list) { 796f2db2e6cSHeiko Carstens thread->pfault_wait = 0; 797f2db2e6cSHeiko Carstens list_del(&thread->list); 798f2db2e6cSHeiko Carstens tsk = container_of(thread, struct task_struct, thread); 799f2db2e6cSHeiko Carstens wake_up_process(tsk); 800d5e50a51SHeiko Carstens put_task_struct(tsk); 801f2db2e6cSHeiko Carstens } 802f2db2e6cSHeiko Carstens spin_unlock_irq(&pfault_lock); 80384c9ceefSSebastian Andrzej Siewior return 0; 804f2db2e6cSHeiko Carstens } 8051da177e4SLinus Torvalds 806fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void) 80729b08d2bSHeiko Carstens { 808fb0a9d7eSHeiko Carstens int rc; 80929b08d2bSHeiko Carstens 8101dad093bSThomas Huth rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 8117dd8fe1fSHeiko Carstens if (rc) 8127dd8fe1fSHeiko Carstens goto out_extint; 8137dd8fe1fSHeiko Carstens rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP; 8147dd8fe1fSHeiko Carstens if (rc) 8157dd8fe1fSHeiko Carstens goto out_pfault; 81682003c3eSHeiko Carstens irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); 81784c9ceefSSebastian Andrzej Siewior cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead", 81884c9ceefSSebastian Andrzej Siewior NULL, pfault_cpu_dead); 8197dd8fe1fSHeiko Carstens return 0; 8207dd8fe1fSHeiko Carstens 8217dd8fe1fSHeiko Carstens out_pfault: 8221dad093bSThomas Huth unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 8237dd8fe1fSHeiko Carstens out_extint: 824fb0a9d7eSHeiko Carstens pfault_disable = 1; 825fb0a9d7eSHeiko Carstens return rc; 826fb0a9d7eSHeiko Carstens } 827fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init); 828fb0a9d7eSHeiko Carstens 8297dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */ 830