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 /* 850aaba41bSMartin Schwidefsky * Find out which address space caused the exception. 861da177e4SLinus Torvalds */ 87bf2f1eeeSMasahiro Yamada static enum fault_type get_fault_type(struct pt_regs *regs) 881da177e4SLinus Torvalds { 89457f2180SHeiko Carstens unsigned long trans_exc_code; 90457f2180SHeiko Carstens 91457f2180SHeiko Carstens trans_exc_code = regs->int_parm_long & 3; 920aaba41bSMartin Schwidefsky if (likely(trans_exc_code == 0)) { 930aaba41bSMartin Schwidefsky /* primary space exception */ 940aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && 950aaba41bSMartin Schwidefsky test_pt_regs_flag(regs, PIF_GUEST_FAULT)) 960aaba41bSMartin Schwidefsky return GMAP_FAULT; 970aaba41bSMartin Schwidefsky if (current->thread.mm_segment == USER_DS) 980aaba41bSMartin Schwidefsky return USER_FAULT; 990aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1000aaba41bSMartin Schwidefsky } 1010aaba41bSMartin Schwidefsky if (trans_exc_code == 2) { 1020aaba41bSMartin Schwidefsky /* secondary space exception */ 1030aaba41bSMartin Schwidefsky if (current->thread.mm_segment & 1) { 1040aaba41bSMartin Schwidefsky if (current->thread.mm_segment == USER_DS_SACF) 1050aaba41bSMartin Schwidefsky return USER_FAULT; 1060aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1070aaba41bSMartin Schwidefsky } 1080aaba41bSMartin Schwidefsky return VDSO_FAULT; 1090aaba41bSMartin Schwidefsky } 110962f0af8SGerald Schaefer if (trans_exc_code == 1) { 111962f0af8SGerald Schaefer /* access register mode, not used in the kernel */ 112962f0af8SGerald Schaefer return USER_FAULT; 113962f0af8SGerald Schaefer } 1140aaba41bSMartin Schwidefsky /* home space exception -> access via kernel ASCE */ 1150aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1183b7df342SHeiko Carstens static int bad_address(void *p) 1193b7df342SHeiko Carstens { 1203b7df342SHeiko Carstens unsigned long dummy; 1213b7df342SHeiko Carstens 1223b7df342SHeiko Carstens return probe_kernel_address((unsigned long *)p, dummy); 1233b7df342SHeiko Carstens } 1243b7df342SHeiko Carstens 1253b7df342SHeiko Carstens static void dump_pagetable(unsigned long asce, unsigned long address) 1263b7df342SHeiko Carstens { 127fe7b2747SHeiko Carstens unsigned long *table = __va(asce & _ASCE_ORIGIN); 1283b7df342SHeiko Carstens 1293b7df342SHeiko Carstens pr_alert("AS:%016lx ", asce); 1303b7df342SHeiko Carstens switch (asce & _ASCE_TYPE_MASK) { 1313b7df342SHeiko Carstens case _ASCE_TYPE_REGION1: 132f1c1174fSHeiko Carstens table += (address & _REGION1_INDEX) >> _REGION1_SHIFT; 1333b7df342SHeiko Carstens if (bad_address(table)) 1343b7df342SHeiko Carstens goto bad; 1353b7df342SHeiko Carstens pr_cont("R1:%016lx ", *table); 1363b7df342SHeiko Carstens if (*table & _REGION_ENTRY_INVALID) 1373b7df342SHeiko Carstens goto out; 1383b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1393b7df342SHeiko Carstens /* fallthrough */ 1403b7df342SHeiko Carstens case _ASCE_TYPE_REGION2: 141f1c1174fSHeiko Carstens table += (address & _REGION2_INDEX) >> _REGION2_SHIFT; 1423b7df342SHeiko Carstens if (bad_address(table)) 1433b7df342SHeiko Carstens goto bad; 1443b7df342SHeiko Carstens pr_cont("R2:%016lx ", *table); 1453b7df342SHeiko Carstens if (*table & _REGION_ENTRY_INVALID) 1463b7df342SHeiko Carstens goto out; 1473b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1483b7df342SHeiko Carstens /* fallthrough */ 1493b7df342SHeiko Carstens case _ASCE_TYPE_REGION3: 150f1c1174fSHeiko Carstens table += (address & _REGION3_INDEX) >> _REGION3_SHIFT; 1513b7df342SHeiko Carstens if (bad_address(table)) 1523b7df342SHeiko Carstens goto bad; 1533b7df342SHeiko Carstens pr_cont("R3:%016lx ", *table); 1543b7df342SHeiko Carstens if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE)) 1553b7df342SHeiko Carstens goto out; 1563b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1573b7df342SHeiko Carstens /* fallthrough */ 1583b7df342SHeiko Carstens case _ASCE_TYPE_SEGMENT: 159f1c1174fSHeiko Carstens table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT; 1603b7df342SHeiko Carstens if (bad_address(table)) 1613b7df342SHeiko Carstens goto bad; 16291c0837eSJoe Perches pr_cont("S:%016lx ", *table); 1633b7df342SHeiko Carstens if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE)) 1643b7df342SHeiko Carstens goto out; 1653b7df342SHeiko Carstens table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN); 1663b7df342SHeiko Carstens } 167f1c1174fSHeiko Carstens table += (address & _PAGE_INDEX) >> _PAGE_SHIFT; 1683b7df342SHeiko Carstens if (bad_address(table)) 1693b7df342SHeiko Carstens goto bad; 1703b7df342SHeiko Carstens pr_cont("P:%016lx ", *table); 1713b7df342SHeiko Carstens out: 1723b7df342SHeiko Carstens pr_cont("\n"); 1733b7df342SHeiko Carstens return; 1743b7df342SHeiko Carstens bad: 1753b7df342SHeiko Carstens pr_cont("BAD\n"); 1763b7df342SHeiko Carstens } 1773b7df342SHeiko Carstens 1783b7df342SHeiko Carstens static void dump_fault_info(struct pt_regs *regs) 1793b7df342SHeiko Carstens { 1803b7df342SHeiko Carstens unsigned long asce; 1813b7df342SHeiko Carstens 1825d7eccecSHeiko Carstens pr_alert("Failing address: %016lx TEID: %016lx\n", 1835d7eccecSHeiko Carstens regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long); 1843b7df342SHeiko Carstens pr_alert("Fault in "); 1853b7df342SHeiko Carstens switch (regs->int_parm_long & 3) { 1863b7df342SHeiko Carstens case 3: 1873b7df342SHeiko Carstens pr_cont("home space "); 1883b7df342SHeiko Carstens break; 1893b7df342SHeiko Carstens case 2: 1903b7df342SHeiko Carstens pr_cont("secondary space "); 1913b7df342SHeiko Carstens break; 1923b7df342SHeiko Carstens case 1: 1933b7df342SHeiko Carstens pr_cont("access register "); 1943b7df342SHeiko Carstens break; 1953b7df342SHeiko Carstens case 0: 1963b7df342SHeiko Carstens pr_cont("primary space "); 1973b7df342SHeiko Carstens break; 1983b7df342SHeiko Carstens } 1993b7df342SHeiko Carstens pr_cont("mode while using "); 2000aaba41bSMartin Schwidefsky switch (get_fault_type(regs)) { 2010aaba41bSMartin Schwidefsky case USER_FAULT: 2023b7df342SHeiko Carstens asce = S390_lowcore.user_asce; 2033b7df342SHeiko Carstens pr_cont("user "); 2040aaba41bSMartin Schwidefsky break; 2050aaba41bSMartin Schwidefsky case VDSO_FAULT: 2060aaba41bSMartin Schwidefsky asce = S390_lowcore.vdso_asce; 2070aaba41bSMartin Schwidefsky pr_cont("vdso "); 2080aaba41bSMartin Schwidefsky break; 2090aaba41bSMartin Schwidefsky case GMAP_FAULT: 2100aaba41bSMartin Schwidefsky asce = ((struct gmap *) S390_lowcore.gmap)->asce; 2110aaba41bSMartin Schwidefsky pr_cont("gmap "); 2120aaba41bSMartin Schwidefsky break; 2130aaba41bSMartin Schwidefsky case KERNEL_FAULT: 2140aaba41bSMartin Schwidefsky asce = S390_lowcore.kernel_asce; 2150aaba41bSMartin Schwidefsky pr_cont("kernel "); 2160aaba41bSMartin Schwidefsky break; 217bf2f1eeeSMasahiro Yamada default: 218bf2f1eeeSMasahiro Yamada unreachable(); 2193b7df342SHeiko Carstens } 2203b7df342SHeiko Carstens pr_cont("ASCE.\n"); 2213b7df342SHeiko Carstens dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK); 2223b7df342SHeiko Carstens } 2233b7df342SHeiko Carstens 2245d7eccecSHeiko Carstens int show_unhandled_signals = 1; 2255d7eccecSHeiko Carstens 2265d7eccecSHeiko Carstens void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault) 227ab3c68eeSHeiko Carstens { 228ab3c68eeSHeiko Carstens if ((task_pid_nr(current) > 1) && !show_unhandled_signals) 229ab3c68eeSHeiko Carstens return; 230ab3c68eeSHeiko Carstens if (!unhandled_signal(current, signr)) 231ab3c68eeSHeiko Carstens return; 232ab3c68eeSHeiko Carstens if (!printk_ratelimit()) 233ab3c68eeSHeiko Carstens return; 234413d4047SHeiko Carstens printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ", 235413d4047SHeiko Carstens regs->int_code & 0xffff, regs->int_code >> 17); 2369cb1ccecSHeiko Carstens print_vma_addr(KERN_CONT "in ", regs->psw.addr); 237aa33c8cbSMartin Schwidefsky printk(KERN_CONT "\n"); 2385d7eccecSHeiko Carstens if (is_mm_fault) 2393b7df342SHeiko Carstens dump_fault_info(regs); 240ab3c68eeSHeiko Carstens show_regs(regs); 241ab3c68eeSHeiko Carstens } 242ab3c68eeSHeiko Carstens 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * Send SIGSEGV to task. This is an external routine 2451da177e4SLinus Torvalds * to keep the stack usage of do_page_fault small. 2461da177e4SLinus Torvalds */ 247aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code) 2481da177e4SLinus Torvalds { 2495d7eccecSHeiko Carstens report_user_fault(regs, SIGSEGV, 1); 2509507a5d0SEric W. Biederman force_sig_fault(SIGSEGV, si_code, 2519507a5d0SEric W. Biederman (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK), 2529507a5d0SEric W. Biederman current); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 255a80313ffSGerald Schaefer const struct exception_table_entry *s390_search_extables(unsigned long addr) 256a80313ffSGerald Schaefer { 257a80313ffSGerald Schaefer const struct exception_table_entry *fixup; 258a80313ffSGerald Schaefer 259a80313ffSGerald Schaefer fixup = search_extable(__start_dma_ex_table, 260a80313ffSGerald Schaefer __stop_dma_ex_table - __start_dma_ex_table, 261a80313ffSGerald Schaefer addr); 262a80313ffSGerald Schaefer if (!fixup) 263a80313ffSGerald Schaefer fixup = search_exception_tables(addr); 264a80313ffSGerald Schaefer return fixup; 265a80313ffSGerald Schaefer } 266a80313ffSGerald Schaefer 267aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs) 26810c1031fSMartin Schwidefsky { 26910c1031fSMartin Schwidefsky const struct exception_table_entry *fixup; 27010c1031fSMartin Schwidefsky 27110c1031fSMartin Schwidefsky /* Are we prepared to handle this kernel fault? */ 272a80313ffSGerald Schaefer fixup = s390_search_extables(regs->psw.addr); 27310c1031fSMartin Schwidefsky if (fixup) { 274fecc868aSHeiko Carstens regs->psw.addr = extable_fixup(fixup); 27510c1031fSMartin Schwidefsky return; 27610c1031fSMartin Schwidefsky } 27710c1031fSMartin Schwidefsky 27810c1031fSMartin Schwidefsky /* 27910c1031fSMartin Schwidefsky * Oops. The kernel tried to access some bad page. We'll have to 28010c1031fSMartin Schwidefsky * terminate things with extreme prejudice. 28110c1031fSMartin Schwidefsky */ 2820aaba41bSMartin Schwidefsky if (get_fault_type(regs) == KERNEL_FAULT) 28310c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel pointer dereference" 2843b7df342SHeiko Carstens " in virtual kernel address space\n"); 28510c1031fSMartin Schwidefsky else 28610c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel paging request" 2873b7df342SHeiko Carstens " in virtual user address space\n"); 2883b7df342SHeiko Carstens dump_fault_info(regs); 289aa33c8cbSMartin Schwidefsky die(regs, "Oops"); 29010c1031fSMartin Schwidefsky do_exit(SIGKILL); 29110c1031fSMartin Schwidefsky } 29210c1031fSMartin Schwidefsky 293aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs) 29410c1031fSMartin Schwidefsky { 29510c1031fSMartin Schwidefsky /* Low-address protection hit in kernel mode means 29610c1031fSMartin Schwidefsky NULL pointer write access in kernel mode. */ 29710c1031fSMartin Schwidefsky if (regs->psw.mask & PSW_MASK_PSTATE) { 29810c1031fSMartin Schwidefsky /* Low-address protection hit in user mode 'cannot happen'. */ 299aa33c8cbSMartin Schwidefsky die (regs, "Low-address protection"); 30010c1031fSMartin Schwidefsky do_exit(SIGKILL); 30110c1031fSMartin Schwidefsky } 30210c1031fSMartin Schwidefsky 303aa33c8cbSMartin Schwidefsky do_no_context(regs); 30410c1031fSMartin Schwidefsky } 30510c1031fSMartin Schwidefsky 306aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs) 30710c1031fSMartin Schwidefsky { 30810c1031fSMartin Schwidefsky /* 30910c1031fSMartin Schwidefsky * Send a sigbus, regardless of whether we were in kernel 31010c1031fSMartin Schwidefsky * or user mode. 31110c1031fSMartin Schwidefsky */ 3129507a5d0SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRERR, 3139507a5d0SEric W. Biederman (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK), 3149507a5d0SEric W. Biederman current); 31510c1031fSMartin Schwidefsky } 31610c1031fSMartin Schwidefsky 31757d7f939SMartin Schwidefsky static noinline int signal_return(struct pt_regs *regs) 31857d7f939SMartin Schwidefsky { 31957d7f939SMartin Schwidefsky u16 instruction; 32057d7f939SMartin Schwidefsky int rc; 32157d7f939SMartin Schwidefsky 32257d7f939SMartin Schwidefsky rc = __get_user(instruction, (u16 __user *) regs->psw.addr); 32357d7f939SMartin Schwidefsky if (rc) 32457d7f939SMartin Schwidefsky return rc; 32557d7f939SMartin Schwidefsky if (instruction == 0x0a77) { 32657d7f939SMartin Schwidefsky set_pt_regs_flag(regs, PIF_SYSCALL); 32757d7f939SMartin Schwidefsky regs->int_code = 0x00040077; 32857d7f939SMartin Schwidefsky return 0; 32957d7f939SMartin Schwidefsky } else if (instruction == 0x0aad) { 33057d7f939SMartin Schwidefsky set_pt_regs_flag(regs, PIF_SYSCALL); 33157d7f939SMartin Schwidefsky regs->int_code = 0x000400ad; 33257d7f939SMartin Schwidefsky return 0; 33357d7f939SMartin Schwidefsky } 33457d7f939SMartin Schwidefsky return -EACCES; 33557d7f939SMartin Schwidefsky } 33657d7f939SMartin Schwidefsky 33750a7ca3cSSouptick Joarder static noinline void do_fault_error(struct pt_regs *regs, int access, 33850a7ca3cSSouptick Joarder vm_fault_t fault) 33950d7280dSMartin Schwidefsky { 34050d7280dSMartin Schwidefsky int si_code; 34150d7280dSMartin Schwidefsky 34250d7280dSMartin Schwidefsky switch (fault) { 34350d7280dSMartin Schwidefsky case VM_FAULT_BADACCESS: 34457d7f939SMartin Schwidefsky if (access == VM_EXEC && signal_return(regs) == 0) 34557d7f939SMartin Schwidefsky break; 34650d7280dSMartin Schwidefsky case VM_FAULT_BADMAP: 34750d7280dSMartin Schwidefsky /* Bad memory access. Check if it is kernel or user space. */ 3487d256175SHeiko Carstens if (user_mode(regs)) { 34950d7280dSMartin Schwidefsky /* User mode accesses just cause a SIGSEGV */ 35050d7280dSMartin Schwidefsky si_code = (fault == VM_FAULT_BADMAP) ? 35150d7280dSMartin Schwidefsky SEGV_MAPERR : SEGV_ACCERR; 352aa33c8cbSMartin Schwidefsky do_sigsegv(regs, si_code); 35357d7f939SMartin Schwidefsky break; 35450d7280dSMartin Schwidefsky } 35550d7280dSMartin Schwidefsky case VM_FAULT_BADCONTEXT: 35624eb3a82SDominik Dingel case VM_FAULT_PFAULT: 357aa33c8cbSMartin Schwidefsky do_no_context(regs); 35850d7280dSMartin Schwidefsky break; 359f2c76e3bSHeiko Carstens case VM_FAULT_SIGNAL: 360f2c76e3bSHeiko Carstens if (!user_mode(regs)) 361f2c76e3bSHeiko Carstens do_no_context(regs); 362f2c76e3bSHeiko Carstens break; 36350d7280dSMartin Schwidefsky default: /* fault & VM_FAULT_ERROR */ 36499583181SHeiko Carstens if (fault & VM_FAULT_OOM) { 3657d256175SHeiko Carstens if (!user_mode(regs)) 366aa33c8cbSMartin Schwidefsky do_no_context(regs); 36799583181SHeiko Carstens else 36850d7280dSMartin Schwidefsky pagefault_out_of_memory(); 36933692f27SLinus Torvalds } else if (fault & VM_FAULT_SIGSEGV) { 37033692f27SLinus Torvalds /* Kernel mode? Handle exceptions or die */ 37133692f27SLinus Torvalds if (!user_mode(regs)) 37233692f27SLinus Torvalds do_no_context(regs); 37333692f27SLinus Torvalds else 37433692f27SLinus Torvalds do_sigsegv(regs, SEGV_MAPERR); 37599583181SHeiko Carstens } else if (fault & VM_FAULT_SIGBUS) { 37650d7280dSMartin Schwidefsky /* Kernel mode? Handle exceptions or die */ 3777d256175SHeiko Carstens if (!user_mode(regs)) 378aa33c8cbSMartin Schwidefsky do_no_context(regs); 37936bf9680SMartin Schwidefsky else 380aa33c8cbSMartin Schwidefsky do_sigbus(regs); 38150d7280dSMartin Schwidefsky } else 38250d7280dSMartin Schwidefsky BUG(); 38350d7280dSMartin Schwidefsky break; 38450d7280dSMartin Schwidefsky } 38550d7280dSMartin Schwidefsky } 38650d7280dSMartin Schwidefsky 3871da177e4SLinus Torvalds /* 3881da177e4SLinus Torvalds * This routine handles page faults. It determines the address, 3891da177e4SLinus Torvalds * and the problem, and then passes it off to one of the appropriate 3901da177e4SLinus Torvalds * routines. 3911da177e4SLinus Torvalds * 39250d7280dSMartin Schwidefsky * interruption code (int_code): 3931da177e4SLinus Torvalds * 04 Protection -> Write-Protection (suprression) 3941da177e4SLinus Torvalds * 10 Segment translation -> Not present (nullification) 3951da177e4SLinus Torvalds * 11 Page translation -> Not present (nullification) 3961da177e4SLinus Torvalds * 3b Region third trans. -> Not present (nullification) 3971da177e4SLinus Torvalds */ 39850a7ca3cSSouptick Joarder static inline vm_fault_t do_exception(struct pt_regs *regs, int access) 3991da177e4SLinus Torvalds { 40024eb3a82SDominik Dingel struct gmap *gmap; 4011da177e4SLinus Torvalds struct task_struct *tsk; 4021da177e4SLinus Torvalds struct mm_struct *mm; 4031da177e4SLinus Torvalds struct vm_area_struct *vma; 4040aaba41bSMartin Schwidefsky enum fault_type type; 405aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 4061da177e4SLinus Torvalds unsigned long address; 40733ce6140SHeiko Carstens unsigned int flags; 40850a7ca3cSSouptick Joarder vm_fault_t fault; 40910c1031fSMartin Schwidefsky 41039efd4ecSMartin Schwidefsky tsk = current; 41139efd4ecSMartin Schwidefsky /* 41239efd4ecSMartin Schwidefsky * The instruction that caused the program check has 41339efd4ecSMartin Schwidefsky * been nullified. Don't signal single step via SIGTRAP. 41439efd4ecSMartin Schwidefsky */ 415d3a73acbSMartin Schwidefsky clear_pt_regs_flag(regs, PIF_PER_TRAP); 41639efd4ecSMartin Schwidefsky 4177ecb344aSMartin Schwidefsky if (notify_page_fault(regs)) 41850d7280dSMartin Schwidefsky return 0; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds mm = tsk->mm; 421aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds /* 4241da177e4SLinus Torvalds * Verify that the fault happened in user space, that 4251da177e4SLinus Torvalds * we are not in an interrupt and that there is a 4261da177e4SLinus Torvalds * user context. 4271da177e4SLinus Torvalds */ 42850d7280dSMartin Schwidefsky fault = VM_FAULT_BADCONTEXT; 4290aaba41bSMartin Schwidefsky type = get_fault_type(regs); 4300aaba41bSMartin Schwidefsky switch (type) { 4310aaba41bSMartin Schwidefsky case KERNEL_FAULT: 43250d7280dSMartin Schwidefsky goto out; 4330aaba41bSMartin Schwidefsky case VDSO_FAULT: 4340aaba41bSMartin Schwidefsky fault = VM_FAULT_BADMAP; 4350aaba41bSMartin Schwidefsky goto out; 4360aaba41bSMartin Schwidefsky case USER_FAULT: 4370aaba41bSMartin Schwidefsky case GMAP_FAULT: 4380aaba41bSMartin Schwidefsky if (faulthandler_disabled() || !mm) 4390aaba41bSMartin Schwidefsky goto out; 4400aaba41bSMartin Schwidefsky break; 4410aaba41bSMartin Schwidefsky } 4421da177e4SLinus Torvalds 44361365e13SMartin Schwidefsky address = trans_exc_code & __FAIL_ADDR_MASK; 444a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 445f2c76e3bSHeiko Carstens flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 446759496baSJohannes Weiner if (user_mode(regs)) 447759496baSJohannes Weiner flags |= FAULT_FLAG_USER; 44833ce6140SHeiko Carstens if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400) 44933ce6140SHeiko Carstens flags |= FAULT_FLAG_WRITE; 4501da177e4SLinus Torvalds down_read(&mm->mmap_sem); 4511da177e4SLinus Torvalds 4520aaba41bSMartin Schwidefsky gmap = NULL; 4530aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) { 4540aaba41bSMartin Schwidefsky gmap = (struct gmap *) S390_lowcore.gmap; 455527e30b4SMartin Schwidefsky current->thread.gmap_addr = address; 4564be130a0SMartin Schwidefsky current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE); 4574a494439SDavid Hildenbrand current->thread.gmap_int_code = regs->int_code & 0xffff; 458527e30b4SMartin Schwidefsky address = __gmap_translate(gmap, address); 459e5992f2eSMartin Schwidefsky if (address == -EFAULT) { 460e5992f2eSMartin Schwidefsky fault = VM_FAULT_BADMAP; 461e5992f2eSMartin Schwidefsky goto out_up; 462e5992f2eSMartin Schwidefsky } 46324eb3a82SDominik Dingel if (gmap->pfault_enabled) 46424eb3a82SDominik Dingel flags |= FAULT_FLAG_RETRY_NOWAIT; 465e5992f2eSMartin Schwidefsky } 466e5992f2eSMartin Schwidefsky 467e5992f2eSMartin Schwidefsky retry: 46850d7280dSMartin Schwidefsky fault = VM_FAULT_BADMAP; 4691da177e4SLinus Torvalds vma = find_vma(mm, address); 4701da177e4SLinus Torvalds if (!vma) 47150d7280dSMartin Schwidefsky goto out_up; 472c1821c2eSGerald Schaefer 47350d7280dSMartin Schwidefsky if (unlikely(vma->vm_start > address)) { 4741da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 47550d7280dSMartin Schwidefsky goto out_up; 4761da177e4SLinus Torvalds if (expand_stack(vma, address)) 47750d7280dSMartin Schwidefsky goto out_up; 47850d7280dSMartin Schwidefsky } 47950d7280dSMartin Schwidefsky 4801da177e4SLinus Torvalds /* 4811da177e4SLinus Torvalds * Ok, we have a good vm_area for this memory access, so 4821da177e4SLinus Torvalds * we can handle it.. 4831da177e4SLinus Torvalds */ 48450d7280dSMartin Schwidefsky fault = VM_FAULT_BADACCESS; 4851ab947deSMartin Schwidefsky if (unlikely(!(vma->vm_flags & access))) 48650d7280dSMartin Schwidefsky goto out_up; 4871da177e4SLinus Torvalds 48853492b1dSGerald Schaefer if (is_vm_hugetlb_page(vma)) 48953492b1dSGerald Schaefer address &= HPAGE_MASK; 4901da177e4SLinus Torvalds /* 4911da177e4SLinus Torvalds * If for any reason at all we couldn't handle the fault, 4921da177e4SLinus Torvalds * make sure we exit gracefully rather than endlessly redo 4931da177e4SLinus Torvalds * the fault. 4941da177e4SLinus Torvalds */ 495dcddffd4SKirill A. Shutemov fault = handle_mm_fault(vma, address, flags); 496f2c76e3bSHeiko Carstens /* No reason to continue if interrupted by SIGKILL. */ 497f2c76e3bSHeiko Carstens if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) { 498f2c76e3bSHeiko Carstens fault = VM_FAULT_SIGNAL; 499306d6c49SClaudio Imbrenda if (flags & FAULT_FLAG_RETRY_NOWAIT) 500306d6c49SClaudio Imbrenda goto out_up; 501f2c76e3bSHeiko Carstens goto out; 502f2c76e3bSHeiko Carstens } 50350d7280dSMartin Schwidefsky if (unlikely(fault & VM_FAULT_ERROR)) 50450d7280dSMartin Schwidefsky goto out_up; 50550d7280dSMartin Schwidefsky 50633ce6140SHeiko Carstens /* 50733ce6140SHeiko Carstens * Major/minor page fault accounting is only done on the 50833ce6140SHeiko Carstens * initial attempt. If we go through a retry, it is extremely 50933ce6140SHeiko Carstens * likely that the page will be found in page cache at that point. 51033ce6140SHeiko Carstens */ 51133ce6140SHeiko Carstens if (flags & FAULT_FLAG_ALLOW_RETRY) { 512bde69af2SHeiko Carstens if (fault & VM_FAULT_MAJOR) { 51383c54070SNick Piggin tsk->maj_flt++; 514a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 515bde69af2SHeiko Carstens regs, address); 516bde69af2SHeiko Carstens } else { 51783c54070SNick Piggin tsk->min_flt++; 518a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 519bde69af2SHeiko Carstens regs, address); 520bde69af2SHeiko Carstens } 52133ce6140SHeiko Carstens if (fault & VM_FAULT_RETRY) { 5220aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && gmap && 5230aaba41bSMartin Schwidefsky (flags & FAULT_FLAG_RETRY_NOWAIT)) { 52424eb3a82SDominik Dingel /* FAULT_FLAG_RETRY_NOWAIT has been set, 52524eb3a82SDominik Dingel * mmap_sem has not been released */ 52624eb3a82SDominik Dingel current->thread.gmap_pfault = 1; 52724eb3a82SDominik Dingel fault = VM_FAULT_PFAULT; 52824eb3a82SDominik Dingel goto out_up; 52924eb3a82SDominik Dingel } 53033ce6140SHeiko Carstens /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk 53133ce6140SHeiko Carstens * of starvation. */ 53224eb3a82SDominik Dingel flags &= ~(FAULT_FLAG_ALLOW_RETRY | 53324eb3a82SDominik Dingel FAULT_FLAG_RETRY_NOWAIT); 53445cac65bSShaohua Li flags |= FAULT_FLAG_TRIED; 535e5992f2eSMartin Schwidefsky down_read(&mm->mmap_sem); 53633ce6140SHeiko Carstens goto retry; 53733ce6140SHeiko Carstens } 53833ce6140SHeiko Carstens } 5390aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && gmap) { 540527e30b4SMartin Schwidefsky address = __gmap_link(gmap, current->thread.gmap_addr, 541527e30b4SMartin Schwidefsky address); 542527e30b4SMartin Schwidefsky if (address == -EFAULT) { 543527e30b4SMartin Schwidefsky fault = VM_FAULT_BADMAP; 544527e30b4SMartin Schwidefsky goto out_up; 545527e30b4SMartin Schwidefsky } 546527e30b4SMartin Schwidefsky if (address == -ENOMEM) { 547527e30b4SMartin Schwidefsky fault = VM_FAULT_OOM; 548527e30b4SMartin Schwidefsky goto out_up; 549527e30b4SMartin Schwidefsky } 550527e30b4SMartin Schwidefsky } 55150d7280dSMartin Schwidefsky fault = 0; 55250d7280dSMartin Schwidefsky out_up: 5531da177e4SLinus Torvalds up_read(&mm->mmap_sem); 55450d7280dSMartin Schwidefsky out: 55550d7280dSMartin Schwidefsky return fault; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5587a5388deSHeiko Carstens void do_protection_exception(struct pt_regs *regs) 5591da177e4SLinus Torvalds { 560aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 56150a7ca3cSSouptick Joarder int access; 56250a7ca3cSSouptick Joarder vm_fault_t fault; 56361365e13SMartin Schwidefsky 564aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 565f752ac4dSMartin Schwidefsky /* 566f752ac4dSMartin Schwidefsky * Protection exceptions are suppressing, decrement psw address. 567f752ac4dSMartin Schwidefsky * The exception to this rule are aborted transactions, for these 568f752ac4dSMartin Schwidefsky * the PSW already points to the correct location. 569f752ac4dSMartin Schwidefsky */ 570f752ac4dSMartin Schwidefsky if (!(regs->int_code & 0x200)) 571aa33c8cbSMartin Schwidefsky regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16); 57210c1031fSMartin Schwidefsky /* 57310c1031fSMartin Schwidefsky * Check for low-address protection. This needs to be treated 57410c1031fSMartin Schwidefsky * as a special case because the translation exception code 57510c1031fSMartin Schwidefsky * field is not guaranteed to contain valid data in this case. 57610c1031fSMartin Schwidefsky */ 57761365e13SMartin Schwidefsky if (unlikely(!(trans_exc_code & 4))) { 578aa33c8cbSMartin Schwidefsky do_low_address(regs); 57910c1031fSMartin Schwidefsky return; 58010c1031fSMartin Schwidefsky } 58157d7f939SMartin Schwidefsky if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) { 58257d7f939SMartin Schwidefsky regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) | 58357d7f939SMartin Schwidefsky (regs->psw.addr & PAGE_MASK); 58457d7f939SMartin Schwidefsky access = VM_EXEC; 58557d7f939SMartin Schwidefsky fault = VM_FAULT_BADACCESS; 58657d7f939SMartin Schwidefsky } else { 58757d7f939SMartin Schwidefsky access = VM_WRITE; 58857d7f939SMartin Schwidefsky fault = do_exception(regs, access); 58957d7f939SMartin Schwidefsky } 59050d7280dSMartin Schwidefsky if (unlikely(fault)) 59157d7f939SMartin Schwidefsky do_fault_error(regs, access, fault); 5921da177e4SLinus Torvalds } 5937a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_protection_exception); 5941da177e4SLinus Torvalds 5957a5388deSHeiko Carstens void do_dat_exception(struct pt_regs *regs) 5961da177e4SLinus Torvalds { 59750a7ca3cSSouptick Joarder int access; 59850a7ca3cSSouptick Joarder vm_fault_t fault; 59950d7280dSMartin Schwidefsky 6001ab947deSMartin Schwidefsky access = VM_READ | VM_EXEC | VM_WRITE; 601aa33c8cbSMartin Schwidefsky fault = do_exception(regs, access); 60250d7280dSMartin Schwidefsky if (unlikely(fault)) 60357d7f939SMartin Schwidefsky do_fault_error(regs, access, fault); 6041da177e4SLinus Torvalds } 6057a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_dat_exception); 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds #ifdef CONFIG_PFAULT 6081da177e4SLinus Torvalds /* 6091da177e4SLinus Torvalds * 'pfault' pseudo page faults routines. 6101da177e4SLinus Torvalds */ 611fb0a9d7eSHeiko Carstens static int pfault_disable; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds static int __init nopfault(char *str) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds pfault_disable = 1; 6161da177e4SLinus Torvalds return 1; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds __setup("nopfault", nopfault); 6201da177e4SLinus Torvalds 6217dd8fe1fSHeiko Carstens struct pfault_refbk { 6227dd8fe1fSHeiko Carstens u16 refdiagc; 6237dd8fe1fSHeiko Carstens u16 reffcode; 6247dd8fe1fSHeiko Carstens u16 refdwlen; 6257dd8fe1fSHeiko Carstens u16 refversn; 6267dd8fe1fSHeiko Carstens u64 refgaddr; 6277dd8fe1fSHeiko Carstens u64 refselmk; 6287dd8fe1fSHeiko Carstens u64 refcmpmk; 6297dd8fe1fSHeiko Carstens u64 reserved; 6307dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8))); 6311da177e4SLinus Torvalds 63200e9e664SMartin Schwidefsky static struct pfault_refbk pfault_init_refbk = { 6337dd8fe1fSHeiko Carstens .refdiagc = 0x258, 6347dd8fe1fSHeiko Carstens .reffcode = 0, 6357dd8fe1fSHeiko Carstens .refdwlen = 5, 6367dd8fe1fSHeiko Carstens .refversn = 2, 637e22cf8caSChristian Borntraeger .refgaddr = __LC_LPP, 6387dd8fe1fSHeiko Carstens .refselmk = 1ULL << 48, 6397dd8fe1fSHeiko Carstens .refcmpmk = 1ULL << 48, 64000e9e664SMartin Schwidefsky .reserved = __PF_RES_FIELD 64100e9e664SMartin Schwidefsky }; 64200e9e664SMartin Schwidefsky 64300e9e664SMartin Schwidefsky int pfault_init(void) 64400e9e664SMartin Schwidefsky { 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) 65600e9e664SMartin Schwidefsky : "=d" (rc) 65700e9e664SMartin Schwidefsky : "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc"); 6581da177e4SLinus Torvalds return rc; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 66100e9e664SMartin Schwidefsky static struct pfault_refbk pfault_fini_refbk = { 6627dd8fe1fSHeiko Carstens .refdiagc = 0x258, 6637dd8fe1fSHeiko Carstens .reffcode = 1, 6647dd8fe1fSHeiko Carstens .refdwlen = 5, 6657dd8fe1fSHeiko Carstens .refversn = 2, 6667dd8fe1fSHeiko Carstens }; 6671da177e4SLinus Torvalds 66800e9e664SMartin Schwidefsky void pfault_fini(void) 66900e9e664SMartin Schwidefsky { 67000e9e664SMartin Schwidefsky 671f32269a0SCarsten Otte if (pfault_disable) 6721da177e4SLinus Torvalds return; 6731ec2772eSMartin Schwidefsky diag_stat_inc(DIAG_STAT_X258); 67494c12cc7SMartin Schwidefsky asm volatile( 6751da177e4SLinus Torvalds " diag %0,0,0x258\n" 6766c22c986SHeiko Carstens "0: nopr %%r7\n" 67794c12cc7SMartin Schwidefsky EX_TABLE(0b,0b) 67800e9e664SMartin Schwidefsky : : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc"); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 681f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock); 682f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list); 683f2db2e6cSHeiko Carstens 6840227f7c4SPeter Zijlstra #define PF_COMPLETE 0x0080 6850227f7c4SPeter Zijlstra 6860227f7c4SPeter Zijlstra /* 6870227f7c4SPeter Zijlstra * The mechanism of our pfault code: if Linux is running as guest, runs a user 6880227f7c4SPeter Zijlstra * space process and the user space process accesses a page that the host has 6890227f7c4SPeter Zijlstra * paged out we get a pfault interrupt. 6900227f7c4SPeter Zijlstra * 6910227f7c4SPeter Zijlstra * This allows us, within the guest, to schedule a different process. Without 6920227f7c4SPeter Zijlstra * this mechanism the host would have to suspend the whole virtual cpu until 6930227f7c4SPeter Zijlstra * the page has been paged in. 6940227f7c4SPeter Zijlstra * 6950227f7c4SPeter Zijlstra * So when we get such an interrupt then we set the state of the current task 6960227f7c4SPeter Zijlstra * to uninterruptible and also set the need_resched flag. Both happens within 6970227f7c4SPeter Zijlstra * interrupt context(!). If we later on want to return to user space we 6980227f7c4SPeter Zijlstra * recognize the need_resched flag and then call schedule(). It's not very 6990227f7c4SPeter Zijlstra * obvious how this works... 7000227f7c4SPeter Zijlstra * 7010227f7c4SPeter Zijlstra * Of course we have a lot of additional fun with the completion interrupt (-> 7020227f7c4SPeter Zijlstra * host signals that a page of a process has been paged in and the process can 7030227f7c4SPeter Zijlstra * continue to run). This interrupt can arrive on any cpu and, since we have 7040227f7c4SPeter Zijlstra * virtual cpus, actually appear before the interrupt that signals that a page 7050227f7c4SPeter Zijlstra * is missing. 7060227f7c4SPeter Zijlstra */ 707fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code, 708f6649a7eSMartin Schwidefsky unsigned int param32, unsigned long param64) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds struct task_struct *tsk; 7111da177e4SLinus Torvalds __u16 subcode; 712f2db2e6cSHeiko Carstens pid_t pid; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* 7150227f7c4SPeter Zijlstra * Get the external interruption subcode & pfault initial/completion 7160227f7c4SPeter Zijlstra * signal bit. VM stores this in the 'cpu address' field associated 7170227f7c4SPeter Zijlstra * with the external interrupt. 7181da177e4SLinus Torvalds */ 719fde15c3aSHeiko Carstens subcode = ext_code.subcode; 7201da177e4SLinus Torvalds if ((subcode & 0xff00) != __SUBCODE_MASK) 7211da177e4SLinus Torvalds return; 722420f42ecSHeiko Carstens inc_irq_stat(IRQEXT_PFL); 723f2db2e6cSHeiko Carstens /* Get the token (= pid of the affected task). */ 724544e8dd7SHendrik Brueckner pid = param64 & LPP_PID_MASK; 725f2db2e6cSHeiko Carstens rcu_read_lock(); 726f2db2e6cSHeiko Carstens tsk = find_task_by_pid_ns(pid, &init_pid_ns); 727f2db2e6cSHeiko Carstens if (tsk) 728f2db2e6cSHeiko Carstens get_task_struct(tsk); 729f2db2e6cSHeiko Carstens rcu_read_unlock(); 730f2db2e6cSHeiko Carstens if (!tsk) 731f2db2e6cSHeiko Carstens return; 732f2db2e6cSHeiko Carstens spin_lock(&pfault_lock); 7330227f7c4SPeter Zijlstra if (subcode & PF_COMPLETE) { 7341da177e4SLinus Torvalds /* signal bit is set -> a page has been swapped in by VM */ 735f2db2e6cSHeiko Carstens if (tsk->thread.pfault_wait == 1) { 7361da177e4SLinus Torvalds /* Initial interrupt was faster than the completion 7371da177e4SLinus Torvalds * interrupt. pfault_wait is valid. Set pfault_wait 7381da177e4SLinus Torvalds * back to zero and wake up the process. This can 7391da177e4SLinus Torvalds * safely be done because the task is still sleeping 740b6d09449SMartin Schwidefsky * and can't produce new pfaults. */ 7411da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 742f2db2e6cSHeiko Carstens list_del(&tsk->thread.list); 7431da177e4SLinus Torvalds wake_up_process(tsk); 744d5e50a51SHeiko Carstens put_task_struct(tsk); 745f2db2e6cSHeiko Carstens } else { 746f2db2e6cSHeiko Carstens /* Completion interrupt was faster than initial 747f2db2e6cSHeiko Carstens * interrupt. Set pfault_wait to -1 so the initial 748fa2fb2f4SHeiko Carstens * interrupt doesn't put the task to sleep. 749fa2fb2f4SHeiko Carstens * If the task is not running, ignore the completion 750fa2fb2f4SHeiko Carstens * interrupt since it must be a leftover of a PFAULT 751fa2fb2f4SHeiko Carstens * CANCEL operation which didn't remove all pending 752fa2fb2f4SHeiko Carstens * completion interrupts. */ 753fa2fb2f4SHeiko Carstens if (tsk->state == TASK_RUNNING) 754f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = -1; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds } else { 7571da177e4SLinus Torvalds /* signal bit not set -> a real page is missing. */ 758d49f47f8SHeiko Carstens if (WARN_ON_ONCE(tsk != current)) 759d49f47f8SHeiko Carstens goto out; 760d5e50a51SHeiko Carstens if (tsk->thread.pfault_wait == 1) { 761d5e50a51SHeiko Carstens /* Already on the list with a reference: put to sleep */ 7620227f7c4SPeter Zijlstra goto block; 763d5e50a51SHeiko Carstens } else if (tsk->thread.pfault_wait == -1) { 7641da177e4SLinus Torvalds /* Completion interrupt was faster than the initial 765f2db2e6cSHeiko Carstens * interrupt (pfault_wait == -1). Set pfault_wait 766f2db2e6cSHeiko Carstens * back to zero and exit. */ 7671da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 768f2db2e6cSHeiko Carstens } else { 769f2db2e6cSHeiko Carstens /* Initial interrupt arrived before completion 770d5e50a51SHeiko Carstens * interrupt. Let the task sleep. 771d5e50a51SHeiko Carstens * An extra task reference is needed since a different 772d5e50a51SHeiko Carstens * cpu may set the task state to TASK_RUNNING again 773d5e50a51SHeiko Carstens * before the scheduler is reached. */ 774d5e50a51SHeiko Carstens get_task_struct(tsk); 775f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = 1; 776f2db2e6cSHeiko Carstens list_add(&tsk->thread.list, &pfault_list); 7770227f7c4SPeter Zijlstra block: 7780227f7c4SPeter Zijlstra /* Since this must be a userspace fault, there 7790227f7c4SPeter Zijlstra * is no kernel task state to trample. Rely on the 7800227f7c4SPeter Zijlstra * return to userspace schedule() to block. */ 7810227f7c4SPeter Zijlstra __set_current_state(TASK_UNINTERRUPTIBLE); 7821da177e4SLinus Torvalds set_tsk_need_resched(tsk); 783c360192bSMartin Schwidefsky set_preempt_need_resched(); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds } 786d49f47f8SHeiko Carstens out: 787f2db2e6cSHeiko Carstens spin_unlock(&pfault_lock); 78854c27791SHeiko Carstens put_task_struct(tsk); 789f2db2e6cSHeiko Carstens } 790f2db2e6cSHeiko Carstens 79184c9ceefSSebastian Andrzej Siewior static int pfault_cpu_dead(unsigned int cpu) 792f2db2e6cSHeiko Carstens { 793f2db2e6cSHeiko Carstens struct thread_struct *thread, *next; 794f2db2e6cSHeiko Carstens struct task_struct *tsk; 795f2db2e6cSHeiko Carstens 796f2db2e6cSHeiko Carstens spin_lock_irq(&pfault_lock); 797f2db2e6cSHeiko Carstens list_for_each_entry_safe(thread, next, &pfault_list, list) { 798f2db2e6cSHeiko Carstens thread->pfault_wait = 0; 799f2db2e6cSHeiko Carstens list_del(&thread->list); 800f2db2e6cSHeiko Carstens tsk = container_of(thread, struct task_struct, thread); 801f2db2e6cSHeiko Carstens wake_up_process(tsk); 802d5e50a51SHeiko Carstens put_task_struct(tsk); 803f2db2e6cSHeiko Carstens } 804f2db2e6cSHeiko Carstens spin_unlock_irq(&pfault_lock); 80584c9ceefSSebastian Andrzej Siewior return 0; 806f2db2e6cSHeiko Carstens } 8071da177e4SLinus Torvalds 808fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void) 80929b08d2bSHeiko Carstens { 810fb0a9d7eSHeiko Carstens int rc; 81129b08d2bSHeiko Carstens 8121dad093bSThomas Huth rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 8137dd8fe1fSHeiko Carstens if (rc) 8147dd8fe1fSHeiko Carstens goto out_extint; 8157dd8fe1fSHeiko Carstens rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP; 8167dd8fe1fSHeiko Carstens if (rc) 8177dd8fe1fSHeiko Carstens goto out_pfault; 81882003c3eSHeiko Carstens irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); 81984c9ceefSSebastian Andrzej Siewior cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead", 82084c9ceefSSebastian Andrzej Siewior NULL, pfault_cpu_dead); 8217dd8fe1fSHeiko Carstens return 0; 8227dd8fe1fSHeiko Carstens 8237dd8fe1fSHeiko Carstens out_pfault: 8241dad093bSThomas Huth unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 8257dd8fe1fSHeiko Carstens out_extint: 826fb0a9d7eSHeiko Carstens pfault_disable = 1; 827fb0a9d7eSHeiko Carstens return rc; 828fb0a9d7eSHeiko Carstens } 829fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init); 830fb0a9d7eSHeiko Carstens 8317dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */ 832