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 701da177e4SLinus Torvalds /* 710aaba41bSMartin Schwidefsky * Find out which address space caused the exception. 721da177e4SLinus Torvalds */ 73bf2f1eeeSMasahiro Yamada static enum fault_type get_fault_type(struct pt_regs *regs) 741da177e4SLinus Torvalds { 75457f2180SHeiko Carstens unsigned long trans_exc_code; 76457f2180SHeiko Carstens 77457f2180SHeiko Carstens trans_exc_code = regs->int_parm_long & 3; 780aaba41bSMartin Schwidefsky if (likely(trans_exc_code == 0)) { 790aaba41bSMartin Schwidefsky /* primary space exception */ 800aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && 810aaba41bSMartin Schwidefsky test_pt_regs_flag(regs, PIF_GUEST_FAULT)) 820aaba41bSMartin Schwidefsky return GMAP_FAULT; 830aaba41bSMartin Schwidefsky if (current->thread.mm_segment == USER_DS) 840aaba41bSMartin Schwidefsky return USER_FAULT; 850aaba41bSMartin Schwidefsky return KERNEL_FAULT; 860aaba41bSMartin Schwidefsky } 870aaba41bSMartin Schwidefsky if (trans_exc_code == 2) { 880aaba41bSMartin Schwidefsky /* secondary space exception */ 890aaba41bSMartin Schwidefsky if (current->thread.mm_segment & 1) { 900aaba41bSMartin Schwidefsky if (current->thread.mm_segment == USER_DS_SACF) 910aaba41bSMartin Schwidefsky return USER_FAULT; 920aaba41bSMartin Schwidefsky return KERNEL_FAULT; 930aaba41bSMartin Schwidefsky } 940aaba41bSMartin Schwidefsky return VDSO_FAULT; 950aaba41bSMartin Schwidefsky } 96962f0af8SGerald Schaefer if (trans_exc_code == 1) { 97962f0af8SGerald Schaefer /* access register mode, not used in the kernel */ 98962f0af8SGerald Schaefer return USER_FAULT; 99962f0af8SGerald Schaefer } 1000aaba41bSMartin Schwidefsky /* home space exception -> access via kernel ASCE */ 1010aaba41bSMartin Schwidefsky return KERNEL_FAULT; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1043b7df342SHeiko Carstens static int bad_address(void *p) 1053b7df342SHeiko Carstens { 1063b7df342SHeiko Carstens unsigned long dummy; 1073b7df342SHeiko Carstens 1083b7df342SHeiko Carstens return probe_kernel_address((unsigned long *)p, dummy); 1093b7df342SHeiko Carstens } 1103b7df342SHeiko Carstens 1113b7df342SHeiko Carstens static void dump_pagetable(unsigned long asce, unsigned long address) 1123b7df342SHeiko Carstens { 113fe7b2747SHeiko Carstens unsigned long *table = __va(asce & _ASCE_ORIGIN); 1143b7df342SHeiko Carstens 1153b7df342SHeiko Carstens pr_alert("AS:%016lx ", asce); 1163b7df342SHeiko Carstens switch (asce & _ASCE_TYPE_MASK) { 1173b7df342SHeiko Carstens case _ASCE_TYPE_REGION1: 118f1c1174fSHeiko Carstens table += (address & _REGION1_INDEX) >> _REGION1_SHIFT; 1193b7df342SHeiko Carstens if (bad_address(table)) 1203b7df342SHeiko Carstens goto bad; 1213b7df342SHeiko Carstens pr_cont("R1:%016lx ", *table); 1223b7df342SHeiko Carstens if (*table & _REGION_ENTRY_INVALID) 1233b7df342SHeiko Carstens goto out; 1243b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1253b7df342SHeiko Carstens /* fallthrough */ 1263b7df342SHeiko Carstens case _ASCE_TYPE_REGION2: 127f1c1174fSHeiko Carstens table += (address & _REGION2_INDEX) >> _REGION2_SHIFT; 1283b7df342SHeiko Carstens if (bad_address(table)) 1293b7df342SHeiko Carstens goto bad; 1303b7df342SHeiko Carstens pr_cont("R2:%016lx ", *table); 1313b7df342SHeiko Carstens if (*table & _REGION_ENTRY_INVALID) 1323b7df342SHeiko Carstens goto out; 1333b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1343b7df342SHeiko Carstens /* fallthrough */ 1353b7df342SHeiko Carstens case _ASCE_TYPE_REGION3: 136f1c1174fSHeiko Carstens table += (address & _REGION3_INDEX) >> _REGION3_SHIFT; 1373b7df342SHeiko Carstens if (bad_address(table)) 1383b7df342SHeiko Carstens goto bad; 1393b7df342SHeiko Carstens pr_cont("R3:%016lx ", *table); 1403b7df342SHeiko Carstens if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE)) 1413b7df342SHeiko Carstens goto out; 1423b7df342SHeiko Carstens table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); 1433b7df342SHeiko Carstens /* fallthrough */ 1443b7df342SHeiko Carstens case _ASCE_TYPE_SEGMENT: 145f1c1174fSHeiko Carstens table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT; 1463b7df342SHeiko Carstens if (bad_address(table)) 1473b7df342SHeiko Carstens goto bad; 14891c0837eSJoe Perches pr_cont("S:%016lx ", *table); 1493b7df342SHeiko Carstens if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE)) 1503b7df342SHeiko Carstens goto out; 1513b7df342SHeiko Carstens table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN); 1523b7df342SHeiko Carstens } 153f1c1174fSHeiko Carstens table += (address & _PAGE_INDEX) >> _PAGE_SHIFT; 1543b7df342SHeiko Carstens if (bad_address(table)) 1553b7df342SHeiko Carstens goto bad; 1563b7df342SHeiko Carstens pr_cont("P:%016lx ", *table); 1573b7df342SHeiko Carstens out: 1583b7df342SHeiko Carstens pr_cont("\n"); 1593b7df342SHeiko Carstens return; 1603b7df342SHeiko Carstens bad: 1613b7df342SHeiko Carstens pr_cont("BAD\n"); 1623b7df342SHeiko Carstens } 1633b7df342SHeiko Carstens 1643b7df342SHeiko Carstens static void dump_fault_info(struct pt_regs *regs) 1653b7df342SHeiko Carstens { 1663b7df342SHeiko Carstens unsigned long asce; 1673b7df342SHeiko Carstens 1685d7eccecSHeiko Carstens pr_alert("Failing address: %016lx TEID: %016lx\n", 1695d7eccecSHeiko Carstens regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long); 1703b7df342SHeiko Carstens pr_alert("Fault in "); 1713b7df342SHeiko Carstens switch (regs->int_parm_long & 3) { 1723b7df342SHeiko Carstens case 3: 1733b7df342SHeiko Carstens pr_cont("home space "); 1743b7df342SHeiko Carstens break; 1753b7df342SHeiko Carstens case 2: 1763b7df342SHeiko Carstens pr_cont("secondary space "); 1773b7df342SHeiko Carstens break; 1783b7df342SHeiko Carstens case 1: 1793b7df342SHeiko Carstens pr_cont("access register "); 1803b7df342SHeiko Carstens break; 1813b7df342SHeiko Carstens case 0: 1823b7df342SHeiko Carstens pr_cont("primary space "); 1833b7df342SHeiko Carstens break; 1843b7df342SHeiko Carstens } 1853b7df342SHeiko Carstens pr_cont("mode while using "); 1860aaba41bSMartin Schwidefsky switch (get_fault_type(regs)) { 1870aaba41bSMartin Schwidefsky case USER_FAULT: 1883b7df342SHeiko Carstens asce = S390_lowcore.user_asce; 1893b7df342SHeiko Carstens pr_cont("user "); 1900aaba41bSMartin Schwidefsky break; 1910aaba41bSMartin Schwidefsky case VDSO_FAULT: 1920aaba41bSMartin Schwidefsky asce = S390_lowcore.vdso_asce; 1930aaba41bSMartin Schwidefsky pr_cont("vdso "); 1940aaba41bSMartin Schwidefsky break; 1950aaba41bSMartin Schwidefsky case GMAP_FAULT: 1960aaba41bSMartin Schwidefsky asce = ((struct gmap *) S390_lowcore.gmap)->asce; 1970aaba41bSMartin Schwidefsky pr_cont("gmap "); 1980aaba41bSMartin Schwidefsky break; 1990aaba41bSMartin Schwidefsky case KERNEL_FAULT: 2000aaba41bSMartin Schwidefsky asce = S390_lowcore.kernel_asce; 2010aaba41bSMartin Schwidefsky pr_cont("kernel "); 2020aaba41bSMartin Schwidefsky break; 203bf2f1eeeSMasahiro Yamada default: 204bf2f1eeeSMasahiro Yamada unreachable(); 2053b7df342SHeiko Carstens } 2063b7df342SHeiko Carstens pr_cont("ASCE.\n"); 2073b7df342SHeiko Carstens dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK); 2083b7df342SHeiko Carstens } 2093b7df342SHeiko Carstens 2105d7eccecSHeiko Carstens int show_unhandled_signals = 1; 2115d7eccecSHeiko Carstens 2125d7eccecSHeiko Carstens void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault) 213ab3c68eeSHeiko Carstens { 214ab3c68eeSHeiko Carstens if ((task_pid_nr(current) > 1) && !show_unhandled_signals) 215ab3c68eeSHeiko Carstens return; 216ab3c68eeSHeiko Carstens if (!unhandled_signal(current, signr)) 217ab3c68eeSHeiko Carstens return; 218ab3c68eeSHeiko Carstens if (!printk_ratelimit()) 219ab3c68eeSHeiko Carstens return; 220413d4047SHeiko Carstens printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ", 221413d4047SHeiko Carstens regs->int_code & 0xffff, regs->int_code >> 17); 2229cb1ccecSHeiko Carstens print_vma_addr(KERN_CONT "in ", regs->psw.addr); 223aa33c8cbSMartin Schwidefsky printk(KERN_CONT "\n"); 2245d7eccecSHeiko Carstens if (is_mm_fault) 2253b7df342SHeiko Carstens dump_fault_info(regs); 226ab3c68eeSHeiko Carstens show_regs(regs); 227ab3c68eeSHeiko Carstens } 228ab3c68eeSHeiko Carstens 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * Send SIGSEGV to task. This is an external routine 2311da177e4SLinus Torvalds * to keep the stack usage of do_page_fault small. 2321da177e4SLinus Torvalds */ 233aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code) 2341da177e4SLinus Torvalds { 2355d7eccecSHeiko Carstens report_user_fault(regs, SIGSEGV, 1); 2369507a5d0SEric W. Biederman force_sig_fault(SIGSEGV, si_code, 2372e1661d2SEric W. Biederman (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK)); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240a80313ffSGerald Schaefer const struct exception_table_entry *s390_search_extables(unsigned long addr) 241a80313ffSGerald Schaefer { 242a80313ffSGerald Schaefer const struct exception_table_entry *fixup; 243a80313ffSGerald Schaefer 244a80313ffSGerald Schaefer fixup = search_extable(__start_dma_ex_table, 245a80313ffSGerald Schaefer __stop_dma_ex_table - __start_dma_ex_table, 246a80313ffSGerald Schaefer addr); 247a80313ffSGerald Schaefer if (!fixup) 248a80313ffSGerald Schaefer fixup = search_exception_tables(addr); 249a80313ffSGerald Schaefer return fixup; 250a80313ffSGerald Schaefer } 251a80313ffSGerald Schaefer 252aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs) 25310c1031fSMartin Schwidefsky { 25410c1031fSMartin Schwidefsky const struct exception_table_entry *fixup; 25510c1031fSMartin Schwidefsky 25610c1031fSMartin Schwidefsky /* Are we prepared to handle this kernel fault? */ 257a80313ffSGerald Schaefer fixup = s390_search_extables(regs->psw.addr); 25810c1031fSMartin Schwidefsky if (fixup) { 259fecc868aSHeiko Carstens regs->psw.addr = extable_fixup(fixup); 26010c1031fSMartin Schwidefsky return; 26110c1031fSMartin Schwidefsky } 26210c1031fSMartin Schwidefsky 26310c1031fSMartin Schwidefsky /* 26410c1031fSMartin Schwidefsky * Oops. The kernel tried to access some bad page. We'll have to 26510c1031fSMartin Schwidefsky * terminate things with extreme prejudice. 26610c1031fSMartin Schwidefsky */ 2670aaba41bSMartin Schwidefsky if (get_fault_type(regs) == KERNEL_FAULT) 26810c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel pointer dereference" 2693b7df342SHeiko Carstens " in virtual kernel address space\n"); 27010c1031fSMartin Schwidefsky else 27110c1031fSMartin Schwidefsky printk(KERN_ALERT "Unable to handle kernel paging request" 2723b7df342SHeiko Carstens " in virtual user address space\n"); 2733b7df342SHeiko Carstens dump_fault_info(regs); 274aa33c8cbSMartin Schwidefsky die(regs, "Oops"); 27510c1031fSMartin Schwidefsky do_exit(SIGKILL); 27610c1031fSMartin Schwidefsky } 27710c1031fSMartin Schwidefsky 278aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs) 27910c1031fSMartin Schwidefsky { 28010c1031fSMartin Schwidefsky /* Low-address protection hit in kernel mode means 28110c1031fSMartin Schwidefsky NULL pointer write access in kernel mode. */ 28210c1031fSMartin Schwidefsky if (regs->psw.mask & PSW_MASK_PSTATE) { 28310c1031fSMartin Schwidefsky /* Low-address protection hit in user mode 'cannot happen'. */ 284aa33c8cbSMartin Schwidefsky die (regs, "Low-address protection"); 28510c1031fSMartin Schwidefsky do_exit(SIGKILL); 28610c1031fSMartin Schwidefsky } 28710c1031fSMartin Schwidefsky 288aa33c8cbSMartin Schwidefsky do_no_context(regs); 28910c1031fSMartin Schwidefsky } 29010c1031fSMartin Schwidefsky 291aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs) 29210c1031fSMartin Schwidefsky { 29310c1031fSMartin Schwidefsky /* 29410c1031fSMartin Schwidefsky * Send a sigbus, regardless of whether we were in kernel 29510c1031fSMartin Schwidefsky * or user mode. 29610c1031fSMartin Schwidefsky */ 2979507a5d0SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRERR, 2982e1661d2SEric W. Biederman (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK)); 29910c1031fSMartin Schwidefsky } 30010c1031fSMartin Schwidefsky 30157d7f939SMartin Schwidefsky static noinline int signal_return(struct pt_regs *regs) 30257d7f939SMartin Schwidefsky { 30357d7f939SMartin Schwidefsky u16 instruction; 30457d7f939SMartin Schwidefsky int rc; 30557d7f939SMartin Schwidefsky 30657d7f939SMartin Schwidefsky rc = __get_user(instruction, (u16 __user *) regs->psw.addr); 30757d7f939SMartin Schwidefsky if (rc) 30857d7f939SMartin Schwidefsky return rc; 30957d7f939SMartin Schwidefsky if (instruction == 0x0a77) { 31057d7f939SMartin Schwidefsky set_pt_regs_flag(regs, PIF_SYSCALL); 31157d7f939SMartin Schwidefsky regs->int_code = 0x00040077; 31257d7f939SMartin Schwidefsky return 0; 31357d7f939SMartin Schwidefsky } else if (instruction == 0x0aad) { 31457d7f939SMartin Schwidefsky set_pt_regs_flag(regs, PIF_SYSCALL); 31557d7f939SMartin Schwidefsky regs->int_code = 0x000400ad; 31657d7f939SMartin Schwidefsky return 0; 31757d7f939SMartin Schwidefsky } 31857d7f939SMartin Schwidefsky return -EACCES; 31957d7f939SMartin Schwidefsky } 32057d7f939SMartin Schwidefsky 32150a7ca3cSSouptick Joarder static noinline void do_fault_error(struct pt_regs *regs, int access, 32250a7ca3cSSouptick Joarder vm_fault_t fault) 32350d7280dSMartin Schwidefsky { 32450d7280dSMartin Schwidefsky int si_code; 32550d7280dSMartin Schwidefsky 32650d7280dSMartin Schwidefsky switch (fault) { 32750d7280dSMartin Schwidefsky case VM_FAULT_BADACCESS: 32857d7f939SMartin Schwidefsky if (access == VM_EXEC && signal_return(regs) == 0) 32957d7f939SMartin Schwidefsky break; 3307f5aa115SHeiko Carstens /* fallthrough */ 33150d7280dSMartin Schwidefsky case VM_FAULT_BADMAP: 33250d7280dSMartin Schwidefsky /* Bad memory access. Check if it is kernel or user space. */ 3337d256175SHeiko Carstens if (user_mode(regs)) { 33450d7280dSMartin Schwidefsky /* User mode accesses just cause a SIGSEGV */ 33550d7280dSMartin Schwidefsky si_code = (fault == VM_FAULT_BADMAP) ? 33650d7280dSMartin Schwidefsky SEGV_MAPERR : SEGV_ACCERR; 337aa33c8cbSMartin Schwidefsky do_sigsegv(regs, si_code); 33857d7f939SMartin Schwidefsky break; 33950d7280dSMartin Schwidefsky } 3407f5aa115SHeiko Carstens /* fallthrough */ 34150d7280dSMartin Schwidefsky case VM_FAULT_BADCONTEXT: 3427f5aa115SHeiko Carstens /* fallthrough */ 34324eb3a82SDominik Dingel case VM_FAULT_PFAULT: 344aa33c8cbSMartin Schwidefsky do_no_context(regs); 34550d7280dSMartin Schwidefsky break; 346f2c76e3bSHeiko Carstens case VM_FAULT_SIGNAL: 347f2c76e3bSHeiko Carstens if (!user_mode(regs)) 348f2c76e3bSHeiko Carstens do_no_context(regs); 349f2c76e3bSHeiko Carstens break; 35050d7280dSMartin Schwidefsky default: /* fault & VM_FAULT_ERROR */ 35199583181SHeiko Carstens if (fault & VM_FAULT_OOM) { 3527d256175SHeiko Carstens if (!user_mode(regs)) 353aa33c8cbSMartin Schwidefsky do_no_context(regs); 35499583181SHeiko Carstens else 35550d7280dSMartin Schwidefsky pagefault_out_of_memory(); 35633692f27SLinus Torvalds } else if (fault & VM_FAULT_SIGSEGV) { 35733692f27SLinus Torvalds /* Kernel mode? Handle exceptions or die */ 35833692f27SLinus Torvalds if (!user_mode(regs)) 35933692f27SLinus Torvalds do_no_context(regs); 36033692f27SLinus Torvalds else 36133692f27SLinus Torvalds do_sigsegv(regs, SEGV_MAPERR); 36299583181SHeiko Carstens } else if (fault & VM_FAULT_SIGBUS) { 36350d7280dSMartin Schwidefsky /* Kernel mode? Handle exceptions or die */ 3647d256175SHeiko Carstens if (!user_mode(regs)) 365aa33c8cbSMartin Schwidefsky do_no_context(regs); 36636bf9680SMartin Schwidefsky else 367aa33c8cbSMartin Schwidefsky do_sigbus(regs); 36850d7280dSMartin Schwidefsky } else 36950d7280dSMartin Schwidefsky BUG(); 37050d7280dSMartin Schwidefsky break; 37150d7280dSMartin Schwidefsky } 37250d7280dSMartin Schwidefsky } 37350d7280dSMartin Schwidefsky 3741da177e4SLinus Torvalds /* 3751da177e4SLinus Torvalds * This routine handles page faults. It determines the address, 3761da177e4SLinus Torvalds * and the problem, and then passes it off to one of the appropriate 3771da177e4SLinus Torvalds * routines. 3781da177e4SLinus Torvalds * 37950d7280dSMartin Schwidefsky * interruption code (int_code): 3801da177e4SLinus Torvalds * 04 Protection -> Write-Protection (suprression) 3811da177e4SLinus Torvalds * 10 Segment translation -> Not present (nullification) 3821da177e4SLinus Torvalds * 11 Page translation -> Not present (nullification) 3831da177e4SLinus Torvalds * 3b Region third trans. -> Not present (nullification) 3841da177e4SLinus Torvalds */ 38550a7ca3cSSouptick Joarder static inline vm_fault_t do_exception(struct pt_regs *regs, int access) 3861da177e4SLinus Torvalds { 38724eb3a82SDominik Dingel struct gmap *gmap; 3881da177e4SLinus Torvalds struct task_struct *tsk; 3891da177e4SLinus Torvalds struct mm_struct *mm; 3901da177e4SLinus Torvalds struct vm_area_struct *vma; 3910aaba41bSMartin Schwidefsky enum fault_type type; 392aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 3931da177e4SLinus Torvalds unsigned long address; 39433ce6140SHeiko Carstens unsigned int flags; 39550a7ca3cSSouptick Joarder vm_fault_t fault; 39610c1031fSMartin Schwidefsky 39739efd4ecSMartin Schwidefsky tsk = current; 39839efd4ecSMartin Schwidefsky /* 39939efd4ecSMartin Schwidefsky * The instruction that caused the program check has 40039efd4ecSMartin Schwidefsky * been nullified. Don't signal single step via SIGTRAP. 40139efd4ecSMartin Schwidefsky */ 402d3a73acbSMartin Schwidefsky clear_pt_regs_flag(regs, PIF_PER_TRAP); 40339efd4ecSMartin Schwidefsky 404b98cca44SAnshuman Khandual if (kprobe_page_fault(regs, 14)) 40550d7280dSMartin Schwidefsky return 0; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds mm = tsk->mm; 408aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds /* 4111da177e4SLinus Torvalds * Verify that the fault happened in user space, that 4121da177e4SLinus Torvalds * we are not in an interrupt and that there is a 4131da177e4SLinus Torvalds * user context. 4141da177e4SLinus Torvalds */ 41550d7280dSMartin Schwidefsky fault = VM_FAULT_BADCONTEXT; 4160aaba41bSMartin Schwidefsky type = get_fault_type(regs); 4170aaba41bSMartin Schwidefsky switch (type) { 4180aaba41bSMartin Schwidefsky case KERNEL_FAULT: 41950d7280dSMartin Schwidefsky goto out; 4200aaba41bSMartin Schwidefsky case VDSO_FAULT: 4210aaba41bSMartin Schwidefsky fault = VM_FAULT_BADMAP; 4220aaba41bSMartin Schwidefsky goto out; 4230aaba41bSMartin Schwidefsky case USER_FAULT: 4240aaba41bSMartin Schwidefsky case GMAP_FAULT: 4250aaba41bSMartin Schwidefsky if (faulthandler_disabled() || !mm) 4260aaba41bSMartin Schwidefsky goto out; 4270aaba41bSMartin Schwidefsky break; 4280aaba41bSMartin Schwidefsky } 4291da177e4SLinus Torvalds 43061365e13SMartin Schwidefsky address = trans_exc_code & __FAIL_ADDR_MASK; 431a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 432f2c76e3bSHeiko Carstens flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 433759496baSJohannes Weiner if (user_mode(regs)) 434759496baSJohannes Weiner flags |= FAULT_FLAG_USER; 43533ce6140SHeiko Carstens if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400) 43633ce6140SHeiko Carstens flags |= FAULT_FLAG_WRITE; 4371da177e4SLinus Torvalds down_read(&mm->mmap_sem); 4381da177e4SLinus Torvalds 4390aaba41bSMartin Schwidefsky gmap = NULL; 4400aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) { 4410aaba41bSMartin Schwidefsky gmap = (struct gmap *) S390_lowcore.gmap; 442527e30b4SMartin Schwidefsky current->thread.gmap_addr = address; 4434be130a0SMartin Schwidefsky current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE); 4444a494439SDavid Hildenbrand current->thread.gmap_int_code = regs->int_code & 0xffff; 445527e30b4SMartin Schwidefsky address = __gmap_translate(gmap, address); 446e5992f2eSMartin Schwidefsky if (address == -EFAULT) { 447e5992f2eSMartin Schwidefsky fault = VM_FAULT_BADMAP; 448e5992f2eSMartin Schwidefsky goto out_up; 449e5992f2eSMartin Schwidefsky } 45024eb3a82SDominik Dingel if (gmap->pfault_enabled) 45124eb3a82SDominik Dingel flags |= FAULT_FLAG_RETRY_NOWAIT; 452e5992f2eSMartin Schwidefsky } 453e5992f2eSMartin Schwidefsky 454e5992f2eSMartin Schwidefsky retry: 45550d7280dSMartin Schwidefsky fault = VM_FAULT_BADMAP; 4561da177e4SLinus Torvalds vma = find_vma(mm, address); 4571da177e4SLinus Torvalds if (!vma) 45850d7280dSMartin Schwidefsky goto out_up; 459c1821c2eSGerald Schaefer 46050d7280dSMartin Schwidefsky if (unlikely(vma->vm_start > address)) { 4611da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 46250d7280dSMartin Schwidefsky goto out_up; 4631da177e4SLinus Torvalds if (expand_stack(vma, address)) 46450d7280dSMartin Schwidefsky goto out_up; 46550d7280dSMartin Schwidefsky } 46650d7280dSMartin Schwidefsky 4671da177e4SLinus Torvalds /* 4681da177e4SLinus Torvalds * Ok, we have a good vm_area for this memory access, so 4691da177e4SLinus Torvalds * we can handle it.. 4701da177e4SLinus Torvalds */ 47150d7280dSMartin Schwidefsky fault = VM_FAULT_BADACCESS; 4721ab947deSMartin Schwidefsky if (unlikely(!(vma->vm_flags & access))) 47350d7280dSMartin Schwidefsky goto out_up; 4741da177e4SLinus Torvalds 47553492b1dSGerald Schaefer if (is_vm_hugetlb_page(vma)) 47653492b1dSGerald Schaefer address &= HPAGE_MASK; 4771da177e4SLinus Torvalds /* 4781da177e4SLinus Torvalds * If for any reason at all we couldn't handle the fault, 4791da177e4SLinus Torvalds * make sure we exit gracefully rather than endlessly redo 4801da177e4SLinus Torvalds * the fault. 4811da177e4SLinus Torvalds */ 482dcddffd4SKirill A. Shutemov fault = handle_mm_fault(vma, address, flags); 4834ef87322SPeter Xu if (fault_signal_pending(fault, regs)) { 484f2c76e3bSHeiko Carstens fault = VM_FAULT_SIGNAL; 485306d6c49SClaudio Imbrenda if (flags & FAULT_FLAG_RETRY_NOWAIT) 486306d6c49SClaudio Imbrenda goto out_up; 487f2c76e3bSHeiko Carstens goto out; 488f2c76e3bSHeiko Carstens } 48950d7280dSMartin Schwidefsky if (unlikely(fault & VM_FAULT_ERROR)) 49050d7280dSMartin Schwidefsky goto out_up; 49150d7280dSMartin Schwidefsky 49233ce6140SHeiko Carstens /* 49333ce6140SHeiko Carstens * Major/minor page fault accounting is only done on the 49433ce6140SHeiko Carstens * initial attempt. If we go through a retry, it is extremely 49533ce6140SHeiko Carstens * likely that the page will be found in page cache at that point. 49633ce6140SHeiko Carstens */ 49733ce6140SHeiko Carstens if (flags & FAULT_FLAG_ALLOW_RETRY) { 498bde69af2SHeiko Carstens if (fault & VM_FAULT_MAJOR) { 49983c54070SNick Piggin tsk->maj_flt++; 500a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 501bde69af2SHeiko Carstens regs, address); 502bde69af2SHeiko Carstens } else { 50383c54070SNick Piggin tsk->min_flt++; 504a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 505bde69af2SHeiko Carstens regs, address); 506bde69af2SHeiko Carstens } 50733ce6140SHeiko Carstens if (fault & VM_FAULT_RETRY) { 5080aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && gmap && 5090aaba41bSMartin Schwidefsky (flags & FAULT_FLAG_RETRY_NOWAIT)) { 51024eb3a82SDominik Dingel /* FAULT_FLAG_RETRY_NOWAIT has been set, 51124eb3a82SDominik Dingel * mmap_sem has not been released */ 51224eb3a82SDominik Dingel current->thread.gmap_pfault = 1; 51324eb3a82SDominik Dingel fault = VM_FAULT_PFAULT; 51424eb3a82SDominik Dingel goto out_up; 51524eb3a82SDominik Dingel } 51633ce6140SHeiko Carstens /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk 51733ce6140SHeiko Carstens * of starvation. */ 51824eb3a82SDominik Dingel flags &= ~(FAULT_FLAG_ALLOW_RETRY | 51924eb3a82SDominik Dingel FAULT_FLAG_RETRY_NOWAIT); 52045cac65bSShaohua Li flags |= FAULT_FLAG_TRIED; 521e5992f2eSMartin Schwidefsky down_read(&mm->mmap_sem); 52233ce6140SHeiko Carstens goto retry; 52333ce6140SHeiko Carstens } 52433ce6140SHeiko Carstens } 5250aaba41bSMartin Schwidefsky if (IS_ENABLED(CONFIG_PGSTE) && gmap) { 526527e30b4SMartin Schwidefsky address = __gmap_link(gmap, current->thread.gmap_addr, 527527e30b4SMartin Schwidefsky address); 528527e30b4SMartin Schwidefsky if (address == -EFAULT) { 529527e30b4SMartin Schwidefsky fault = VM_FAULT_BADMAP; 530527e30b4SMartin Schwidefsky goto out_up; 531527e30b4SMartin Schwidefsky } 532527e30b4SMartin Schwidefsky if (address == -ENOMEM) { 533527e30b4SMartin Schwidefsky fault = VM_FAULT_OOM; 534527e30b4SMartin Schwidefsky goto out_up; 535527e30b4SMartin Schwidefsky } 536527e30b4SMartin Schwidefsky } 53750d7280dSMartin Schwidefsky fault = 0; 53850d7280dSMartin Schwidefsky out_up: 5391da177e4SLinus Torvalds up_read(&mm->mmap_sem); 54050d7280dSMartin Schwidefsky out: 54150d7280dSMartin Schwidefsky return fault; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5447a5388deSHeiko Carstens void do_protection_exception(struct pt_regs *regs) 5451da177e4SLinus Torvalds { 546aa33c8cbSMartin Schwidefsky unsigned long trans_exc_code; 54750a7ca3cSSouptick Joarder int access; 54850a7ca3cSSouptick Joarder vm_fault_t fault; 54961365e13SMartin Schwidefsky 550aa33c8cbSMartin Schwidefsky trans_exc_code = regs->int_parm_long; 551f752ac4dSMartin Schwidefsky /* 552f752ac4dSMartin Schwidefsky * Protection exceptions are suppressing, decrement psw address. 553f752ac4dSMartin Schwidefsky * The exception to this rule are aborted transactions, for these 554f752ac4dSMartin Schwidefsky * the PSW already points to the correct location. 555f752ac4dSMartin Schwidefsky */ 556f752ac4dSMartin Schwidefsky if (!(regs->int_code & 0x200)) 557aa33c8cbSMartin Schwidefsky regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16); 55810c1031fSMartin Schwidefsky /* 55910c1031fSMartin Schwidefsky * Check for low-address protection. This needs to be treated 56010c1031fSMartin Schwidefsky * as a special case because the translation exception code 56110c1031fSMartin Schwidefsky * field is not guaranteed to contain valid data in this case. 56210c1031fSMartin Schwidefsky */ 56361365e13SMartin Schwidefsky if (unlikely(!(trans_exc_code & 4))) { 564aa33c8cbSMartin Schwidefsky do_low_address(regs); 56510c1031fSMartin Schwidefsky return; 56610c1031fSMartin Schwidefsky } 56757d7f939SMartin Schwidefsky if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) { 56857d7f939SMartin Schwidefsky regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) | 56957d7f939SMartin Schwidefsky (regs->psw.addr & PAGE_MASK); 57057d7f939SMartin Schwidefsky access = VM_EXEC; 57157d7f939SMartin Schwidefsky fault = VM_FAULT_BADACCESS; 57257d7f939SMartin Schwidefsky } else { 57357d7f939SMartin Schwidefsky access = VM_WRITE; 57457d7f939SMartin Schwidefsky fault = do_exception(regs, access); 57557d7f939SMartin Schwidefsky } 57650d7280dSMartin Schwidefsky if (unlikely(fault)) 57757d7f939SMartin Schwidefsky do_fault_error(regs, access, fault); 5781da177e4SLinus Torvalds } 5797a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_protection_exception); 5801da177e4SLinus Torvalds 5817a5388deSHeiko Carstens void do_dat_exception(struct pt_regs *regs) 5821da177e4SLinus Torvalds { 58350a7ca3cSSouptick Joarder int access; 58450a7ca3cSSouptick Joarder vm_fault_t fault; 58550d7280dSMartin Schwidefsky 5861ab947deSMartin Schwidefsky access = VM_READ | VM_EXEC | VM_WRITE; 587aa33c8cbSMartin Schwidefsky fault = do_exception(regs, access); 58850d7280dSMartin Schwidefsky if (unlikely(fault)) 58957d7f939SMartin Schwidefsky do_fault_error(regs, access, fault); 5901da177e4SLinus Torvalds } 5917a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_dat_exception); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds #ifdef CONFIG_PFAULT 5941da177e4SLinus Torvalds /* 5951da177e4SLinus Torvalds * 'pfault' pseudo page faults routines. 5961da177e4SLinus Torvalds */ 597fb0a9d7eSHeiko Carstens static int pfault_disable; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds static int __init nopfault(char *str) 6001da177e4SLinus Torvalds { 6011da177e4SLinus Torvalds pfault_disable = 1; 6021da177e4SLinus Torvalds return 1; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds __setup("nopfault", nopfault); 6061da177e4SLinus Torvalds 6077dd8fe1fSHeiko Carstens struct pfault_refbk { 6087dd8fe1fSHeiko Carstens u16 refdiagc; 6097dd8fe1fSHeiko Carstens u16 reffcode; 6107dd8fe1fSHeiko Carstens u16 refdwlen; 6117dd8fe1fSHeiko Carstens u16 refversn; 6127dd8fe1fSHeiko Carstens u64 refgaddr; 6137dd8fe1fSHeiko Carstens u64 refselmk; 6147dd8fe1fSHeiko Carstens u64 refcmpmk; 6157dd8fe1fSHeiko Carstens u64 reserved; 6167dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8))); 6171da177e4SLinus Torvalds 61800e9e664SMartin Schwidefsky static struct pfault_refbk pfault_init_refbk = { 6197dd8fe1fSHeiko Carstens .refdiagc = 0x258, 6207dd8fe1fSHeiko Carstens .reffcode = 0, 6217dd8fe1fSHeiko Carstens .refdwlen = 5, 6227dd8fe1fSHeiko Carstens .refversn = 2, 623e22cf8caSChristian Borntraeger .refgaddr = __LC_LPP, 6247dd8fe1fSHeiko Carstens .refselmk = 1ULL << 48, 6257dd8fe1fSHeiko Carstens .refcmpmk = 1ULL << 48, 62600e9e664SMartin Schwidefsky .reserved = __PF_RES_FIELD 62700e9e664SMartin Schwidefsky }; 62800e9e664SMartin Schwidefsky 62900e9e664SMartin Schwidefsky int pfault_init(void) 63000e9e664SMartin Schwidefsky { 6311da177e4SLinus Torvalds int rc; 6321da177e4SLinus Torvalds 633f32269a0SCarsten Otte if (pfault_disable) 6341da177e4SLinus Torvalds return -1; 6351ec2772eSMartin Schwidefsky diag_stat_inc(DIAG_STAT_X258); 63694c12cc7SMartin Schwidefsky asm volatile( 6371da177e4SLinus Torvalds " diag %1,%0,0x258\n" 6381da177e4SLinus Torvalds "0: j 2f\n" 6391da177e4SLinus Torvalds "1: la %0,8\n" 6401da177e4SLinus Torvalds "2:\n" 64194c12cc7SMartin Schwidefsky EX_TABLE(0b,1b) 64200e9e664SMartin Schwidefsky : "=d" (rc) 64300e9e664SMartin Schwidefsky : "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc"); 6441da177e4SLinus Torvalds return rc; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 64700e9e664SMartin Schwidefsky static struct pfault_refbk pfault_fini_refbk = { 6487dd8fe1fSHeiko Carstens .refdiagc = 0x258, 6497dd8fe1fSHeiko Carstens .reffcode = 1, 6507dd8fe1fSHeiko Carstens .refdwlen = 5, 6517dd8fe1fSHeiko Carstens .refversn = 2, 6527dd8fe1fSHeiko Carstens }; 6531da177e4SLinus Torvalds 65400e9e664SMartin Schwidefsky void pfault_fini(void) 65500e9e664SMartin Schwidefsky { 65600e9e664SMartin Schwidefsky 657f32269a0SCarsten Otte if (pfault_disable) 6581da177e4SLinus Torvalds return; 6591ec2772eSMartin Schwidefsky diag_stat_inc(DIAG_STAT_X258); 66094c12cc7SMartin Schwidefsky asm volatile( 6611da177e4SLinus Torvalds " diag %0,0,0x258\n" 6626c22c986SHeiko Carstens "0: nopr %%r7\n" 66394c12cc7SMartin Schwidefsky EX_TABLE(0b,0b) 66400e9e664SMartin Schwidefsky : : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc"); 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 667f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock); 668f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list); 669f2db2e6cSHeiko Carstens 6700227f7c4SPeter Zijlstra #define PF_COMPLETE 0x0080 6710227f7c4SPeter Zijlstra 6720227f7c4SPeter Zijlstra /* 6730227f7c4SPeter Zijlstra * The mechanism of our pfault code: if Linux is running as guest, runs a user 6740227f7c4SPeter Zijlstra * space process and the user space process accesses a page that the host has 6750227f7c4SPeter Zijlstra * paged out we get a pfault interrupt. 6760227f7c4SPeter Zijlstra * 6770227f7c4SPeter Zijlstra * This allows us, within the guest, to schedule a different process. Without 6780227f7c4SPeter Zijlstra * this mechanism the host would have to suspend the whole virtual cpu until 6790227f7c4SPeter Zijlstra * the page has been paged in. 6800227f7c4SPeter Zijlstra * 6810227f7c4SPeter Zijlstra * So when we get such an interrupt then we set the state of the current task 6820227f7c4SPeter Zijlstra * to uninterruptible and also set the need_resched flag. Both happens within 6830227f7c4SPeter Zijlstra * interrupt context(!). If we later on want to return to user space we 6840227f7c4SPeter Zijlstra * recognize the need_resched flag and then call schedule(). It's not very 6850227f7c4SPeter Zijlstra * obvious how this works... 6860227f7c4SPeter Zijlstra * 6870227f7c4SPeter Zijlstra * Of course we have a lot of additional fun with the completion interrupt (-> 6880227f7c4SPeter Zijlstra * host signals that a page of a process has been paged in and the process can 6890227f7c4SPeter Zijlstra * continue to run). This interrupt can arrive on any cpu and, since we have 6900227f7c4SPeter Zijlstra * virtual cpus, actually appear before the interrupt that signals that a page 6910227f7c4SPeter Zijlstra * is missing. 6920227f7c4SPeter Zijlstra */ 693fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code, 694f6649a7eSMartin Schwidefsky unsigned int param32, unsigned long param64) 6951da177e4SLinus Torvalds { 6961da177e4SLinus Torvalds struct task_struct *tsk; 6971da177e4SLinus Torvalds __u16 subcode; 698f2db2e6cSHeiko Carstens pid_t pid; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* 7010227f7c4SPeter Zijlstra * Get the external interruption subcode & pfault initial/completion 7020227f7c4SPeter Zijlstra * signal bit. VM stores this in the 'cpu address' field associated 7030227f7c4SPeter Zijlstra * with the external interrupt. 7041da177e4SLinus Torvalds */ 705fde15c3aSHeiko Carstens subcode = ext_code.subcode; 7061da177e4SLinus Torvalds if ((subcode & 0xff00) != __SUBCODE_MASK) 7071da177e4SLinus Torvalds return; 708420f42ecSHeiko Carstens inc_irq_stat(IRQEXT_PFL); 709f2db2e6cSHeiko Carstens /* Get the token (= pid of the affected task). */ 710544e8dd7SHendrik Brueckner pid = param64 & LPP_PID_MASK; 711f2db2e6cSHeiko Carstens rcu_read_lock(); 712f2db2e6cSHeiko Carstens tsk = find_task_by_pid_ns(pid, &init_pid_ns); 713f2db2e6cSHeiko Carstens if (tsk) 714f2db2e6cSHeiko Carstens get_task_struct(tsk); 715f2db2e6cSHeiko Carstens rcu_read_unlock(); 716f2db2e6cSHeiko Carstens if (!tsk) 717f2db2e6cSHeiko Carstens return; 718f2db2e6cSHeiko Carstens spin_lock(&pfault_lock); 7190227f7c4SPeter Zijlstra if (subcode & PF_COMPLETE) { 7201da177e4SLinus Torvalds /* signal bit is set -> a page has been swapped in by VM */ 721f2db2e6cSHeiko Carstens if (tsk->thread.pfault_wait == 1) { 7221da177e4SLinus Torvalds /* Initial interrupt was faster than the completion 7231da177e4SLinus Torvalds * interrupt. pfault_wait is valid. Set pfault_wait 7241da177e4SLinus Torvalds * back to zero and wake up the process. This can 7251da177e4SLinus Torvalds * safely be done because the task is still sleeping 726b6d09449SMartin Schwidefsky * and can't produce new pfaults. */ 7271da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 728f2db2e6cSHeiko Carstens list_del(&tsk->thread.list); 7291da177e4SLinus Torvalds wake_up_process(tsk); 730d5e50a51SHeiko Carstens put_task_struct(tsk); 731f2db2e6cSHeiko Carstens } else { 732f2db2e6cSHeiko Carstens /* Completion interrupt was faster than initial 733f2db2e6cSHeiko Carstens * interrupt. Set pfault_wait to -1 so the initial 734fa2fb2f4SHeiko Carstens * interrupt doesn't put the task to sleep. 735fa2fb2f4SHeiko Carstens * If the task is not running, ignore the completion 736fa2fb2f4SHeiko Carstens * interrupt since it must be a leftover of a PFAULT 737fa2fb2f4SHeiko Carstens * CANCEL operation which didn't remove all pending 738fa2fb2f4SHeiko Carstens * completion interrupts. */ 739fa2fb2f4SHeiko Carstens if (tsk->state == TASK_RUNNING) 740f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = -1; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds } else { 7431da177e4SLinus Torvalds /* signal bit not set -> a real page is missing. */ 744d49f47f8SHeiko Carstens if (WARN_ON_ONCE(tsk != current)) 745d49f47f8SHeiko Carstens goto out; 746d5e50a51SHeiko Carstens if (tsk->thread.pfault_wait == 1) { 747d5e50a51SHeiko Carstens /* Already on the list with a reference: put to sleep */ 7480227f7c4SPeter Zijlstra goto block; 749d5e50a51SHeiko Carstens } else if (tsk->thread.pfault_wait == -1) { 7501da177e4SLinus Torvalds /* Completion interrupt was faster than the initial 751f2db2e6cSHeiko Carstens * interrupt (pfault_wait == -1). Set pfault_wait 752f2db2e6cSHeiko Carstens * back to zero and exit. */ 7531da177e4SLinus Torvalds tsk->thread.pfault_wait = 0; 754f2db2e6cSHeiko Carstens } else { 755f2db2e6cSHeiko Carstens /* Initial interrupt arrived before completion 756d5e50a51SHeiko Carstens * interrupt. Let the task sleep. 757d5e50a51SHeiko Carstens * An extra task reference is needed since a different 758d5e50a51SHeiko Carstens * cpu may set the task state to TASK_RUNNING again 759d5e50a51SHeiko Carstens * before the scheduler is reached. */ 760d5e50a51SHeiko Carstens get_task_struct(tsk); 761f2db2e6cSHeiko Carstens tsk->thread.pfault_wait = 1; 762f2db2e6cSHeiko Carstens list_add(&tsk->thread.list, &pfault_list); 7630227f7c4SPeter Zijlstra block: 7640227f7c4SPeter Zijlstra /* Since this must be a userspace fault, there 7650227f7c4SPeter Zijlstra * is no kernel task state to trample. Rely on the 7660227f7c4SPeter Zijlstra * return to userspace schedule() to block. */ 7670227f7c4SPeter Zijlstra __set_current_state(TASK_UNINTERRUPTIBLE); 7681da177e4SLinus Torvalds set_tsk_need_resched(tsk); 769c360192bSMartin Schwidefsky set_preempt_need_resched(); 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds } 772d49f47f8SHeiko Carstens out: 773f2db2e6cSHeiko Carstens spin_unlock(&pfault_lock); 77454c27791SHeiko Carstens put_task_struct(tsk); 775f2db2e6cSHeiko Carstens } 776f2db2e6cSHeiko Carstens 77784c9ceefSSebastian Andrzej Siewior static int pfault_cpu_dead(unsigned int cpu) 778f2db2e6cSHeiko Carstens { 779f2db2e6cSHeiko Carstens struct thread_struct *thread, *next; 780f2db2e6cSHeiko Carstens struct task_struct *tsk; 781f2db2e6cSHeiko Carstens 782f2db2e6cSHeiko Carstens spin_lock_irq(&pfault_lock); 783f2db2e6cSHeiko Carstens list_for_each_entry_safe(thread, next, &pfault_list, list) { 784f2db2e6cSHeiko Carstens thread->pfault_wait = 0; 785f2db2e6cSHeiko Carstens list_del(&thread->list); 786f2db2e6cSHeiko Carstens tsk = container_of(thread, struct task_struct, thread); 787f2db2e6cSHeiko Carstens wake_up_process(tsk); 788d5e50a51SHeiko Carstens put_task_struct(tsk); 789f2db2e6cSHeiko Carstens } 790f2db2e6cSHeiko Carstens spin_unlock_irq(&pfault_lock); 79184c9ceefSSebastian Andrzej Siewior return 0; 792f2db2e6cSHeiko Carstens } 7931da177e4SLinus Torvalds 794fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void) 79529b08d2bSHeiko Carstens { 796fb0a9d7eSHeiko Carstens int rc; 79729b08d2bSHeiko Carstens 7981dad093bSThomas Huth rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 7997dd8fe1fSHeiko Carstens if (rc) 8007dd8fe1fSHeiko Carstens goto out_extint; 8017dd8fe1fSHeiko Carstens rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP; 8027dd8fe1fSHeiko Carstens if (rc) 8037dd8fe1fSHeiko Carstens goto out_pfault; 80482003c3eSHeiko Carstens irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); 80584c9ceefSSebastian Andrzej Siewior cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead", 80684c9ceefSSebastian Andrzej Siewior NULL, pfault_cpu_dead); 8077dd8fe1fSHeiko Carstens return 0; 8087dd8fe1fSHeiko Carstens 8097dd8fe1fSHeiko Carstens out_pfault: 8101dad093bSThomas Huth unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt); 8117dd8fe1fSHeiko Carstens out_extint: 812fb0a9d7eSHeiko Carstens pfault_disable = 1; 813fb0a9d7eSHeiko Carstens return rc; 814fb0a9d7eSHeiko Carstens } 815fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init); 816fb0a9d7eSHeiko Carstens 8177dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */ 818