xref: /openbmc/linux/arch/s390/mm/fault.c (revision 2e1661d2)
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.
860aaba41bSMartin Schwidefsky  * Access register mode is impossible, ignore space == 3.
871da177e4SLinus Torvalds  */
880aaba41bSMartin Schwidefsky static inline enum fault_type get_fault_type(struct pt_regs *regs)
891da177e4SLinus Torvalds {
90457f2180SHeiko Carstens 	unsigned long trans_exc_code;
91457f2180SHeiko Carstens 
92457f2180SHeiko Carstens 	trans_exc_code = regs->int_parm_long & 3;
930aaba41bSMartin Schwidefsky 	if (likely(trans_exc_code == 0)) {
940aaba41bSMartin Schwidefsky 		/* primary space exception */
950aaba41bSMartin Schwidefsky 		if (IS_ENABLED(CONFIG_PGSTE) &&
960aaba41bSMartin Schwidefsky 		    test_pt_regs_flag(regs, PIF_GUEST_FAULT))
970aaba41bSMartin Schwidefsky 			return GMAP_FAULT;
980aaba41bSMartin Schwidefsky 		if (current->thread.mm_segment == USER_DS)
990aaba41bSMartin Schwidefsky 			return USER_FAULT;
1000aaba41bSMartin Schwidefsky 		return KERNEL_FAULT;
1010aaba41bSMartin Schwidefsky 	}
1020aaba41bSMartin Schwidefsky 	if (trans_exc_code == 2) {
1030aaba41bSMartin Schwidefsky 		/* secondary space exception */
1040aaba41bSMartin Schwidefsky 		if (current->thread.mm_segment & 1) {
1050aaba41bSMartin Schwidefsky 			if (current->thread.mm_segment == USER_DS_SACF)
1060aaba41bSMartin Schwidefsky 				return USER_FAULT;
1070aaba41bSMartin Schwidefsky 			return KERNEL_FAULT;
1080aaba41bSMartin Schwidefsky 		}
1090aaba41bSMartin Schwidefsky 		return VDSO_FAULT;
1100aaba41bSMartin Schwidefsky 	}
1110aaba41bSMartin Schwidefsky 	/* home space exception -> access via kernel ASCE */
1120aaba41bSMartin Schwidefsky 	return KERNEL_FAULT;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1153b7df342SHeiko Carstens static int bad_address(void *p)
1163b7df342SHeiko Carstens {
1173b7df342SHeiko Carstens 	unsigned long dummy;
1183b7df342SHeiko Carstens 
1193b7df342SHeiko Carstens 	return probe_kernel_address((unsigned long *)p, dummy);
1203b7df342SHeiko Carstens }
1213b7df342SHeiko Carstens 
1223b7df342SHeiko Carstens static void dump_pagetable(unsigned long asce, unsigned long address)
1233b7df342SHeiko Carstens {
124fe7b2747SHeiko Carstens 	unsigned long *table = __va(asce & _ASCE_ORIGIN);
1253b7df342SHeiko Carstens 
1263b7df342SHeiko Carstens 	pr_alert("AS:%016lx ", asce);
1273b7df342SHeiko Carstens 	switch (asce & _ASCE_TYPE_MASK) {
1283b7df342SHeiko Carstens 	case _ASCE_TYPE_REGION1:
129f1c1174fSHeiko Carstens 		table += (address & _REGION1_INDEX) >> _REGION1_SHIFT;
1303b7df342SHeiko Carstens 		if (bad_address(table))
1313b7df342SHeiko Carstens 			goto bad;
1323b7df342SHeiko Carstens 		pr_cont("R1:%016lx ", *table);
1333b7df342SHeiko Carstens 		if (*table & _REGION_ENTRY_INVALID)
1343b7df342SHeiko Carstens 			goto out;
1353b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
1363b7df342SHeiko Carstens 		/* fallthrough */
1373b7df342SHeiko Carstens 	case _ASCE_TYPE_REGION2:
138f1c1174fSHeiko Carstens 		table += (address & _REGION2_INDEX) >> _REGION2_SHIFT;
1393b7df342SHeiko Carstens 		if (bad_address(table))
1403b7df342SHeiko Carstens 			goto bad;
1413b7df342SHeiko Carstens 		pr_cont("R2:%016lx ", *table);
1423b7df342SHeiko Carstens 		if (*table & _REGION_ENTRY_INVALID)
1433b7df342SHeiko Carstens 			goto out;
1443b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
1453b7df342SHeiko Carstens 		/* fallthrough */
1463b7df342SHeiko Carstens 	case _ASCE_TYPE_REGION3:
147f1c1174fSHeiko Carstens 		table += (address & _REGION3_INDEX) >> _REGION3_SHIFT;
1483b7df342SHeiko Carstens 		if (bad_address(table))
1493b7df342SHeiko Carstens 			goto bad;
1503b7df342SHeiko Carstens 		pr_cont("R3:%016lx ", *table);
1513b7df342SHeiko Carstens 		if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
1523b7df342SHeiko Carstens 			goto out;
1533b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
1543b7df342SHeiko Carstens 		/* fallthrough */
1553b7df342SHeiko Carstens 	case _ASCE_TYPE_SEGMENT:
156f1c1174fSHeiko Carstens 		table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
1573b7df342SHeiko Carstens 		if (bad_address(table))
1583b7df342SHeiko Carstens 			goto bad;
15991c0837eSJoe Perches 		pr_cont("S:%016lx ", *table);
1603b7df342SHeiko Carstens 		if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
1613b7df342SHeiko Carstens 			goto out;
1623b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
1633b7df342SHeiko Carstens 	}
164f1c1174fSHeiko Carstens 	table += (address & _PAGE_INDEX) >> _PAGE_SHIFT;
1653b7df342SHeiko Carstens 	if (bad_address(table))
1663b7df342SHeiko Carstens 		goto bad;
1673b7df342SHeiko Carstens 	pr_cont("P:%016lx ", *table);
1683b7df342SHeiko Carstens out:
1693b7df342SHeiko Carstens 	pr_cont("\n");
1703b7df342SHeiko Carstens 	return;
1713b7df342SHeiko Carstens bad:
1723b7df342SHeiko Carstens 	pr_cont("BAD\n");
1733b7df342SHeiko Carstens }
1743b7df342SHeiko Carstens 
1753b7df342SHeiko Carstens static void dump_fault_info(struct pt_regs *regs)
1763b7df342SHeiko Carstens {
1773b7df342SHeiko Carstens 	unsigned long asce;
1783b7df342SHeiko Carstens 
1795d7eccecSHeiko Carstens 	pr_alert("Failing address: %016lx TEID: %016lx\n",
1805d7eccecSHeiko Carstens 		 regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
1813b7df342SHeiko Carstens 	pr_alert("Fault in ");
1823b7df342SHeiko Carstens 	switch (regs->int_parm_long & 3) {
1833b7df342SHeiko Carstens 	case 3:
1843b7df342SHeiko Carstens 		pr_cont("home space ");
1853b7df342SHeiko Carstens 		break;
1863b7df342SHeiko Carstens 	case 2:
1873b7df342SHeiko Carstens 		pr_cont("secondary space ");
1883b7df342SHeiko Carstens 		break;
1893b7df342SHeiko Carstens 	case 1:
1903b7df342SHeiko Carstens 		pr_cont("access register ");
1913b7df342SHeiko Carstens 		break;
1923b7df342SHeiko Carstens 	case 0:
1933b7df342SHeiko Carstens 		pr_cont("primary space ");
1943b7df342SHeiko Carstens 		break;
1953b7df342SHeiko Carstens 	}
1963b7df342SHeiko Carstens 	pr_cont("mode while using ");
1970aaba41bSMartin Schwidefsky 	switch (get_fault_type(regs)) {
1980aaba41bSMartin Schwidefsky 	case USER_FAULT:
1993b7df342SHeiko Carstens 		asce = S390_lowcore.user_asce;
2003b7df342SHeiko Carstens 		pr_cont("user ");
2010aaba41bSMartin Schwidefsky 		break;
2020aaba41bSMartin Schwidefsky 	case VDSO_FAULT:
2030aaba41bSMartin Schwidefsky 		asce = S390_lowcore.vdso_asce;
2040aaba41bSMartin Schwidefsky 		pr_cont("vdso ");
2050aaba41bSMartin Schwidefsky 		break;
2060aaba41bSMartin Schwidefsky 	case GMAP_FAULT:
2070aaba41bSMartin Schwidefsky 		asce = ((struct gmap *) S390_lowcore.gmap)->asce;
2080aaba41bSMartin Schwidefsky 		pr_cont("gmap ");
2090aaba41bSMartin Schwidefsky 		break;
2100aaba41bSMartin Schwidefsky 	case KERNEL_FAULT:
2110aaba41bSMartin Schwidefsky 		asce = S390_lowcore.kernel_asce;
2120aaba41bSMartin Schwidefsky 		pr_cont("kernel ");
2130aaba41bSMartin Schwidefsky 		break;
2143b7df342SHeiko Carstens 	}
2153b7df342SHeiko Carstens 	pr_cont("ASCE.\n");
2163b7df342SHeiko Carstens 	dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
2173b7df342SHeiko Carstens }
2183b7df342SHeiko Carstens 
2195d7eccecSHeiko Carstens int show_unhandled_signals = 1;
2205d7eccecSHeiko Carstens 
2215d7eccecSHeiko Carstens void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
222ab3c68eeSHeiko Carstens {
223ab3c68eeSHeiko Carstens 	if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
224ab3c68eeSHeiko Carstens 		return;
225ab3c68eeSHeiko Carstens 	if (!unhandled_signal(current, signr))
226ab3c68eeSHeiko Carstens 		return;
227ab3c68eeSHeiko Carstens 	if (!printk_ratelimit())
228ab3c68eeSHeiko Carstens 		return;
229413d4047SHeiko Carstens 	printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
230413d4047SHeiko Carstens 	       regs->int_code & 0xffff, regs->int_code >> 17);
2319cb1ccecSHeiko Carstens 	print_vma_addr(KERN_CONT "in ", regs->psw.addr);
232aa33c8cbSMartin Schwidefsky 	printk(KERN_CONT "\n");
2335d7eccecSHeiko Carstens 	if (is_mm_fault)
2343b7df342SHeiko Carstens 		dump_fault_info(regs);
235ab3c68eeSHeiko Carstens 	show_regs(regs);
236ab3c68eeSHeiko Carstens }
237ab3c68eeSHeiko Carstens 
2381da177e4SLinus Torvalds /*
2391da177e4SLinus Torvalds  * Send SIGSEGV to task.  This is an external routine
2401da177e4SLinus Torvalds  * to keep the stack usage of do_page_fault small.
2411da177e4SLinus Torvalds  */
242aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
2431da177e4SLinus Torvalds {
2445d7eccecSHeiko Carstens 	report_user_fault(regs, SIGSEGV, 1);
2459507a5d0SEric W. Biederman 	force_sig_fault(SIGSEGV, si_code,
2462e1661d2SEric W. Biederman 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249a80313ffSGerald Schaefer const struct exception_table_entry *s390_search_extables(unsigned long addr)
250a80313ffSGerald Schaefer {
251a80313ffSGerald Schaefer 	const struct exception_table_entry *fixup;
252a80313ffSGerald Schaefer 
253a80313ffSGerald Schaefer 	fixup = search_extable(__start_dma_ex_table,
254a80313ffSGerald Schaefer 			       __stop_dma_ex_table - __start_dma_ex_table,
255a80313ffSGerald Schaefer 			       addr);
256a80313ffSGerald Schaefer 	if (!fixup)
257a80313ffSGerald Schaefer 		fixup = search_exception_tables(addr);
258a80313ffSGerald Schaefer 	return fixup;
259a80313ffSGerald Schaefer }
260a80313ffSGerald Schaefer 
261aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs)
26210c1031fSMartin Schwidefsky {
26310c1031fSMartin Schwidefsky 	const struct exception_table_entry *fixup;
26410c1031fSMartin Schwidefsky 
26510c1031fSMartin Schwidefsky 	/* Are we prepared to handle this kernel fault?  */
266a80313ffSGerald Schaefer 	fixup = s390_search_extables(regs->psw.addr);
26710c1031fSMartin Schwidefsky 	if (fixup) {
268fecc868aSHeiko Carstens 		regs->psw.addr = extable_fixup(fixup);
26910c1031fSMartin Schwidefsky 		return;
27010c1031fSMartin Schwidefsky 	}
27110c1031fSMartin Schwidefsky 
27210c1031fSMartin Schwidefsky 	/*
27310c1031fSMartin Schwidefsky 	 * Oops. The kernel tried to access some bad page. We'll have to
27410c1031fSMartin Schwidefsky 	 * terminate things with extreme prejudice.
27510c1031fSMartin Schwidefsky 	 */
2760aaba41bSMartin Schwidefsky 	if (get_fault_type(regs) == KERNEL_FAULT)
27710c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
2783b7df342SHeiko Carstens 		       " in virtual kernel address space\n");
27910c1031fSMartin Schwidefsky 	else
28010c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel paging request"
2813b7df342SHeiko Carstens 		       " in virtual user address space\n");
2823b7df342SHeiko Carstens 	dump_fault_info(regs);
283aa33c8cbSMartin Schwidefsky 	die(regs, "Oops");
28410c1031fSMartin Schwidefsky 	do_exit(SIGKILL);
28510c1031fSMartin Schwidefsky }
28610c1031fSMartin Schwidefsky 
287aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs)
28810c1031fSMartin Schwidefsky {
28910c1031fSMartin Schwidefsky 	/* Low-address protection hit in kernel mode means
29010c1031fSMartin Schwidefsky 	   NULL pointer write access in kernel mode.  */
29110c1031fSMartin Schwidefsky 	if (regs->psw.mask & PSW_MASK_PSTATE) {
29210c1031fSMartin Schwidefsky 		/* Low-address protection hit in user mode 'cannot happen'. */
293aa33c8cbSMartin Schwidefsky 		die (regs, "Low-address protection");
29410c1031fSMartin Schwidefsky 		do_exit(SIGKILL);
29510c1031fSMartin Schwidefsky 	}
29610c1031fSMartin Schwidefsky 
297aa33c8cbSMartin Schwidefsky 	do_no_context(regs);
29810c1031fSMartin Schwidefsky }
29910c1031fSMartin Schwidefsky 
300aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs)
30110c1031fSMartin Schwidefsky {
30210c1031fSMartin Schwidefsky 	/*
30310c1031fSMartin Schwidefsky 	 * Send a sigbus, regardless of whether we were in kernel
30410c1031fSMartin Schwidefsky 	 * or user mode.
30510c1031fSMartin Schwidefsky 	 */
3069507a5d0SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR,
3072e1661d2SEric W. Biederman 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
30810c1031fSMartin Schwidefsky }
30910c1031fSMartin Schwidefsky 
31057d7f939SMartin Schwidefsky static noinline int signal_return(struct pt_regs *regs)
31157d7f939SMartin Schwidefsky {
31257d7f939SMartin Schwidefsky 	u16 instruction;
31357d7f939SMartin Schwidefsky 	int rc;
31457d7f939SMartin Schwidefsky 
31557d7f939SMartin Schwidefsky 	rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
31657d7f939SMartin Schwidefsky 	if (rc)
31757d7f939SMartin Schwidefsky 		return rc;
31857d7f939SMartin Schwidefsky 	if (instruction == 0x0a77) {
31957d7f939SMartin Schwidefsky 		set_pt_regs_flag(regs, PIF_SYSCALL);
32057d7f939SMartin Schwidefsky 		regs->int_code = 0x00040077;
32157d7f939SMartin Schwidefsky 		return 0;
32257d7f939SMartin Schwidefsky 	} else if (instruction == 0x0aad) {
32357d7f939SMartin Schwidefsky 		set_pt_regs_flag(regs, PIF_SYSCALL);
32457d7f939SMartin Schwidefsky 		regs->int_code = 0x000400ad;
32557d7f939SMartin Schwidefsky 		return 0;
32657d7f939SMartin Schwidefsky 	}
32757d7f939SMartin Schwidefsky 	return -EACCES;
32857d7f939SMartin Schwidefsky }
32957d7f939SMartin Schwidefsky 
33050a7ca3cSSouptick Joarder static noinline void do_fault_error(struct pt_regs *regs, int access,
33150a7ca3cSSouptick Joarder 					vm_fault_t fault)
33250d7280dSMartin Schwidefsky {
33350d7280dSMartin Schwidefsky 	int si_code;
33450d7280dSMartin Schwidefsky 
33550d7280dSMartin Schwidefsky 	switch (fault) {
33650d7280dSMartin Schwidefsky 	case VM_FAULT_BADACCESS:
33757d7f939SMartin Schwidefsky 		if (access == VM_EXEC && signal_return(regs) == 0)
33857d7f939SMartin Schwidefsky 			break;
33950d7280dSMartin Schwidefsky 	case VM_FAULT_BADMAP:
34050d7280dSMartin Schwidefsky 		/* Bad memory access. Check if it is kernel or user space. */
3417d256175SHeiko Carstens 		if (user_mode(regs)) {
34250d7280dSMartin Schwidefsky 			/* User mode accesses just cause a SIGSEGV */
34350d7280dSMartin Schwidefsky 			si_code = (fault == VM_FAULT_BADMAP) ?
34450d7280dSMartin Schwidefsky 				SEGV_MAPERR : SEGV_ACCERR;
345aa33c8cbSMartin Schwidefsky 			do_sigsegv(regs, si_code);
34657d7f939SMartin Schwidefsky 			break;
34750d7280dSMartin Schwidefsky 		}
34850d7280dSMartin Schwidefsky 	case VM_FAULT_BADCONTEXT:
34924eb3a82SDominik Dingel 	case VM_FAULT_PFAULT:
350aa33c8cbSMartin Schwidefsky 		do_no_context(regs);
35150d7280dSMartin Schwidefsky 		break;
352f2c76e3bSHeiko Carstens 	case VM_FAULT_SIGNAL:
353f2c76e3bSHeiko Carstens 		if (!user_mode(regs))
354f2c76e3bSHeiko Carstens 			do_no_context(regs);
355f2c76e3bSHeiko Carstens 		break;
35650d7280dSMartin Schwidefsky 	default: /* fault & VM_FAULT_ERROR */
35799583181SHeiko Carstens 		if (fault & VM_FAULT_OOM) {
3587d256175SHeiko Carstens 			if (!user_mode(regs))
359aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
36099583181SHeiko Carstens 			else
36150d7280dSMartin Schwidefsky 				pagefault_out_of_memory();
36233692f27SLinus Torvalds 		} else if (fault & VM_FAULT_SIGSEGV) {
36333692f27SLinus Torvalds 			/* Kernel mode? Handle exceptions or die */
36433692f27SLinus Torvalds 			if (!user_mode(regs))
36533692f27SLinus Torvalds 				do_no_context(regs);
36633692f27SLinus Torvalds 			else
36733692f27SLinus Torvalds 				do_sigsegv(regs, SEGV_MAPERR);
36899583181SHeiko Carstens 		} else if (fault & VM_FAULT_SIGBUS) {
36950d7280dSMartin Schwidefsky 			/* Kernel mode? Handle exceptions or die */
3707d256175SHeiko Carstens 			if (!user_mode(regs))
371aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
37236bf9680SMartin Schwidefsky 			else
373aa33c8cbSMartin Schwidefsky 				do_sigbus(regs);
37450d7280dSMartin Schwidefsky 		} else
37550d7280dSMartin Schwidefsky 			BUG();
37650d7280dSMartin Schwidefsky 		break;
37750d7280dSMartin Schwidefsky 	}
37850d7280dSMartin Schwidefsky }
37950d7280dSMartin Schwidefsky 
3801da177e4SLinus Torvalds /*
3811da177e4SLinus Torvalds  * This routine handles page faults.  It determines the address,
3821da177e4SLinus Torvalds  * and the problem, and then passes it off to one of the appropriate
3831da177e4SLinus Torvalds  * routines.
3841da177e4SLinus Torvalds  *
38550d7280dSMartin Schwidefsky  * interruption code (int_code):
3861da177e4SLinus Torvalds  *   04       Protection           ->  Write-Protection  (suprression)
3871da177e4SLinus Torvalds  *   10       Segment translation  ->  Not present       (nullification)
3881da177e4SLinus Torvalds  *   11       Page translation     ->  Not present       (nullification)
3891da177e4SLinus Torvalds  *   3b       Region third trans.  ->  Not present       (nullification)
3901da177e4SLinus Torvalds  */
39150a7ca3cSSouptick Joarder static inline vm_fault_t do_exception(struct pt_regs *regs, int access)
3921da177e4SLinus Torvalds {
39324eb3a82SDominik Dingel 	struct gmap *gmap;
3941da177e4SLinus Torvalds 	struct task_struct *tsk;
3951da177e4SLinus Torvalds 	struct mm_struct *mm;
3961da177e4SLinus Torvalds 	struct vm_area_struct *vma;
3970aaba41bSMartin Schwidefsky 	enum fault_type type;
398aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
3991da177e4SLinus Torvalds 	unsigned long address;
40033ce6140SHeiko Carstens 	unsigned int flags;
40150a7ca3cSSouptick Joarder 	vm_fault_t fault;
40210c1031fSMartin Schwidefsky 
40339efd4ecSMartin Schwidefsky 	tsk = current;
40439efd4ecSMartin Schwidefsky 	/*
40539efd4ecSMartin Schwidefsky 	 * The instruction that caused the program check has
40639efd4ecSMartin Schwidefsky 	 * been nullified. Don't signal single step via SIGTRAP.
40739efd4ecSMartin Schwidefsky 	 */
408d3a73acbSMartin Schwidefsky 	clear_pt_regs_flag(regs, PIF_PER_TRAP);
40939efd4ecSMartin Schwidefsky 
4107ecb344aSMartin Schwidefsky 	if (notify_page_fault(regs))
41150d7280dSMartin Schwidefsky 		return 0;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	mm = tsk->mm;
414aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/*
4171da177e4SLinus Torvalds 	 * Verify that the fault happened in user space, that
4181da177e4SLinus Torvalds 	 * we are not in an interrupt and that there is a
4191da177e4SLinus Torvalds 	 * user context.
4201da177e4SLinus Torvalds 	 */
42150d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADCONTEXT;
4220aaba41bSMartin Schwidefsky 	type = get_fault_type(regs);
4230aaba41bSMartin Schwidefsky 	switch (type) {
4240aaba41bSMartin Schwidefsky 	case KERNEL_FAULT:
42550d7280dSMartin Schwidefsky 		goto out;
4260aaba41bSMartin Schwidefsky 	case VDSO_FAULT:
4270aaba41bSMartin Schwidefsky 		fault = VM_FAULT_BADMAP;
4280aaba41bSMartin Schwidefsky 		goto out;
4290aaba41bSMartin Schwidefsky 	case USER_FAULT:
4300aaba41bSMartin Schwidefsky 	case GMAP_FAULT:
4310aaba41bSMartin Schwidefsky 		if (faulthandler_disabled() || !mm)
4320aaba41bSMartin Schwidefsky 			goto out;
4330aaba41bSMartin Schwidefsky 		break;
4340aaba41bSMartin Schwidefsky 	}
4351da177e4SLinus Torvalds 
43661365e13SMartin Schwidefsky 	address = trans_exc_code & __FAIL_ADDR_MASK;
437a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
438f2c76e3bSHeiko Carstens 	flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
439759496baSJohannes Weiner 	if (user_mode(regs))
440759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
44133ce6140SHeiko Carstens 	if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
44233ce6140SHeiko Carstens 		flags |= FAULT_FLAG_WRITE;
4431da177e4SLinus Torvalds 	down_read(&mm->mmap_sem);
4441da177e4SLinus Torvalds 
4450aaba41bSMartin Schwidefsky 	gmap = NULL;
4460aaba41bSMartin Schwidefsky 	if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) {
4470aaba41bSMartin Schwidefsky 		gmap = (struct gmap *) S390_lowcore.gmap;
448527e30b4SMartin Schwidefsky 		current->thread.gmap_addr = address;
4494be130a0SMartin Schwidefsky 		current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
4504a494439SDavid Hildenbrand 		current->thread.gmap_int_code = regs->int_code & 0xffff;
451527e30b4SMartin Schwidefsky 		address = __gmap_translate(gmap, address);
452e5992f2eSMartin Schwidefsky 		if (address == -EFAULT) {
453e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
454e5992f2eSMartin Schwidefsky 			goto out_up;
455e5992f2eSMartin Schwidefsky 		}
45624eb3a82SDominik Dingel 		if (gmap->pfault_enabled)
45724eb3a82SDominik Dingel 			flags |= FAULT_FLAG_RETRY_NOWAIT;
458e5992f2eSMartin Schwidefsky 	}
459e5992f2eSMartin Schwidefsky 
460e5992f2eSMartin Schwidefsky retry:
46150d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADMAP;
4621da177e4SLinus Torvalds 	vma = find_vma(mm, address);
4631da177e4SLinus Torvalds 	if (!vma)
46450d7280dSMartin Schwidefsky 		goto out_up;
465c1821c2eSGerald Schaefer 
46650d7280dSMartin Schwidefsky 	if (unlikely(vma->vm_start > address)) {
4671da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
46850d7280dSMartin Schwidefsky 			goto out_up;
4691da177e4SLinus Torvalds 		if (expand_stack(vma, address))
47050d7280dSMartin Schwidefsky 			goto out_up;
47150d7280dSMartin Schwidefsky 	}
47250d7280dSMartin Schwidefsky 
4731da177e4SLinus Torvalds 	/*
4741da177e4SLinus Torvalds 	 * Ok, we have a good vm_area for this memory access, so
4751da177e4SLinus Torvalds 	 * we can handle it..
4761da177e4SLinus Torvalds 	 */
47750d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADACCESS;
4781ab947deSMartin Schwidefsky 	if (unlikely(!(vma->vm_flags & access)))
47950d7280dSMartin Schwidefsky 		goto out_up;
4801da177e4SLinus Torvalds 
48153492b1dSGerald Schaefer 	if (is_vm_hugetlb_page(vma))
48253492b1dSGerald Schaefer 		address &= HPAGE_MASK;
4831da177e4SLinus Torvalds 	/*
4841da177e4SLinus Torvalds 	 * If for any reason at all we couldn't handle the fault,
4851da177e4SLinus Torvalds 	 * make sure we exit gracefully rather than endlessly redo
4861da177e4SLinus Torvalds 	 * the fault.
4871da177e4SLinus Torvalds 	 */
488dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
489f2c76e3bSHeiko Carstens 	/* No reason to continue if interrupted by SIGKILL. */
490f2c76e3bSHeiko Carstens 	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
491f2c76e3bSHeiko Carstens 		fault = VM_FAULT_SIGNAL;
492306d6c49SClaudio Imbrenda 		if (flags & FAULT_FLAG_RETRY_NOWAIT)
493306d6c49SClaudio Imbrenda 			goto out_up;
494f2c76e3bSHeiko Carstens 		goto out;
495f2c76e3bSHeiko Carstens 	}
49650d7280dSMartin Schwidefsky 	if (unlikely(fault & VM_FAULT_ERROR))
49750d7280dSMartin Schwidefsky 		goto out_up;
49850d7280dSMartin Schwidefsky 
49933ce6140SHeiko Carstens 	/*
50033ce6140SHeiko Carstens 	 * Major/minor page fault accounting is only done on the
50133ce6140SHeiko Carstens 	 * initial attempt. If we go through a retry, it is extremely
50233ce6140SHeiko Carstens 	 * likely that the page will be found in page cache at that point.
50333ce6140SHeiko Carstens 	 */
50433ce6140SHeiko Carstens 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
505bde69af2SHeiko Carstens 		if (fault & VM_FAULT_MAJOR) {
50683c54070SNick Piggin 			tsk->maj_flt++;
507a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
508bde69af2SHeiko Carstens 				      regs, address);
509bde69af2SHeiko Carstens 		} else {
51083c54070SNick Piggin 			tsk->min_flt++;
511a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
512bde69af2SHeiko Carstens 				      regs, address);
513bde69af2SHeiko Carstens 		}
51433ce6140SHeiko Carstens 		if (fault & VM_FAULT_RETRY) {
5150aaba41bSMartin Schwidefsky 			if (IS_ENABLED(CONFIG_PGSTE) && gmap &&
5160aaba41bSMartin Schwidefsky 			    (flags & FAULT_FLAG_RETRY_NOWAIT)) {
51724eb3a82SDominik Dingel 				/* FAULT_FLAG_RETRY_NOWAIT has been set,
51824eb3a82SDominik Dingel 				 * mmap_sem has not been released */
51924eb3a82SDominik Dingel 				current->thread.gmap_pfault = 1;
52024eb3a82SDominik Dingel 				fault = VM_FAULT_PFAULT;
52124eb3a82SDominik Dingel 				goto out_up;
52224eb3a82SDominik Dingel 			}
52333ce6140SHeiko Carstens 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
52433ce6140SHeiko Carstens 			 * of starvation. */
52524eb3a82SDominik Dingel 			flags &= ~(FAULT_FLAG_ALLOW_RETRY |
52624eb3a82SDominik Dingel 				   FAULT_FLAG_RETRY_NOWAIT);
52745cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
528e5992f2eSMartin Schwidefsky 			down_read(&mm->mmap_sem);
52933ce6140SHeiko Carstens 			goto retry;
53033ce6140SHeiko Carstens 		}
53133ce6140SHeiko Carstens 	}
5320aaba41bSMartin Schwidefsky 	if (IS_ENABLED(CONFIG_PGSTE) && gmap) {
533527e30b4SMartin Schwidefsky 		address =  __gmap_link(gmap, current->thread.gmap_addr,
534527e30b4SMartin Schwidefsky 				       address);
535527e30b4SMartin Schwidefsky 		if (address == -EFAULT) {
536527e30b4SMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
537527e30b4SMartin Schwidefsky 			goto out_up;
538527e30b4SMartin Schwidefsky 		}
539527e30b4SMartin Schwidefsky 		if (address == -ENOMEM) {
540527e30b4SMartin Schwidefsky 			fault = VM_FAULT_OOM;
541527e30b4SMartin Schwidefsky 			goto out_up;
542527e30b4SMartin Schwidefsky 		}
543527e30b4SMartin Schwidefsky 	}
54450d7280dSMartin Schwidefsky 	fault = 0;
54550d7280dSMartin Schwidefsky out_up:
5461da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
54750d7280dSMartin Schwidefsky out:
54850d7280dSMartin Schwidefsky 	return fault;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5517a5388deSHeiko Carstens void do_protection_exception(struct pt_regs *regs)
5521da177e4SLinus Torvalds {
553aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
55450a7ca3cSSouptick Joarder 	int access;
55550a7ca3cSSouptick Joarder 	vm_fault_t fault;
55661365e13SMartin Schwidefsky 
557aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
558f752ac4dSMartin Schwidefsky 	/*
559f752ac4dSMartin Schwidefsky 	 * Protection exceptions are suppressing, decrement psw address.
560f752ac4dSMartin Schwidefsky 	 * The exception to this rule are aborted transactions, for these
561f752ac4dSMartin Schwidefsky 	 * the PSW already points to the correct location.
562f752ac4dSMartin Schwidefsky 	 */
563f752ac4dSMartin Schwidefsky 	if (!(regs->int_code & 0x200))
564aa33c8cbSMartin Schwidefsky 		regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
56510c1031fSMartin Schwidefsky 	/*
56610c1031fSMartin Schwidefsky 	 * Check for low-address protection.  This needs to be treated
56710c1031fSMartin Schwidefsky 	 * as a special case because the translation exception code
56810c1031fSMartin Schwidefsky 	 * field is not guaranteed to contain valid data in this case.
56910c1031fSMartin Schwidefsky 	 */
57061365e13SMartin Schwidefsky 	if (unlikely(!(trans_exc_code & 4))) {
571aa33c8cbSMartin Schwidefsky 		do_low_address(regs);
57210c1031fSMartin Schwidefsky 		return;
57310c1031fSMartin Schwidefsky 	}
57457d7f939SMartin Schwidefsky 	if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) {
57557d7f939SMartin Schwidefsky 		regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) |
57657d7f939SMartin Schwidefsky 					(regs->psw.addr & PAGE_MASK);
57757d7f939SMartin Schwidefsky 		access = VM_EXEC;
57857d7f939SMartin Schwidefsky 		fault = VM_FAULT_BADACCESS;
57957d7f939SMartin Schwidefsky 	} else {
58057d7f939SMartin Schwidefsky 		access = VM_WRITE;
58157d7f939SMartin Schwidefsky 		fault = do_exception(regs, access);
58257d7f939SMartin Schwidefsky 	}
58350d7280dSMartin Schwidefsky 	if (unlikely(fault))
58457d7f939SMartin Schwidefsky 		do_fault_error(regs, access, fault);
5851da177e4SLinus Torvalds }
5867a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_protection_exception);
5871da177e4SLinus Torvalds 
5887a5388deSHeiko Carstens void do_dat_exception(struct pt_regs *regs)
5891da177e4SLinus Torvalds {
59050a7ca3cSSouptick Joarder 	int access;
59150a7ca3cSSouptick Joarder 	vm_fault_t fault;
59250d7280dSMartin Schwidefsky 
5931ab947deSMartin Schwidefsky 	access = VM_READ | VM_EXEC | VM_WRITE;
594aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, access);
59550d7280dSMartin Schwidefsky 	if (unlikely(fault))
59657d7f939SMartin Schwidefsky 		do_fault_error(regs, access, fault);
5971da177e4SLinus Torvalds }
5987a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_dat_exception);
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds #ifdef CONFIG_PFAULT
6011da177e4SLinus Torvalds /*
6021da177e4SLinus Torvalds  * 'pfault' pseudo page faults routines.
6031da177e4SLinus Torvalds  */
604fb0a9d7eSHeiko Carstens static int pfault_disable;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds static int __init nopfault(char *str)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	pfault_disable = 1;
6091da177e4SLinus Torvalds 	return 1;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds __setup("nopfault", nopfault);
6131da177e4SLinus Torvalds 
6147dd8fe1fSHeiko Carstens struct pfault_refbk {
6157dd8fe1fSHeiko Carstens 	u16 refdiagc;
6167dd8fe1fSHeiko Carstens 	u16 reffcode;
6177dd8fe1fSHeiko Carstens 	u16 refdwlen;
6187dd8fe1fSHeiko Carstens 	u16 refversn;
6197dd8fe1fSHeiko Carstens 	u64 refgaddr;
6207dd8fe1fSHeiko Carstens 	u64 refselmk;
6217dd8fe1fSHeiko Carstens 	u64 refcmpmk;
6227dd8fe1fSHeiko Carstens 	u64 reserved;
6237dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8)));
6241da177e4SLinus Torvalds 
62500e9e664SMartin Schwidefsky static struct pfault_refbk pfault_init_refbk = {
6267dd8fe1fSHeiko Carstens 	.refdiagc = 0x258,
6277dd8fe1fSHeiko Carstens 	.reffcode = 0,
6287dd8fe1fSHeiko Carstens 	.refdwlen = 5,
6297dd8fe1fSHeiko Carstens 	.refversn = 2,
630e22cf8caSChristian Borntraeger 	.refgaddr = __LC_LPP,
6317dd8fe1fSHeiko Carstens 	.refselmk = 1ULL << 48,
6327dd8fe1fSHeiko Carstens 	.refcmpmk = 1ULL << 48,
63300e9e664SMartin Schwidefsky 	.reserved = __PF_RES_FIELD
63400e9e664SMartin Schwidefsky };
63500e9e664SMartin Schwidefsky 
63600e9e664SMartin Schwidefsky int pfault_init(void)
63700e9e664SMartin Schwidefsky {
6381da177e4SLinus Torvalds         int rc;
6391da177e4SLinus Torvalds 
640f32269a0SCarsten Otte 	if (pfault_disable)
6411da177e4SLinus Torvalds 		return -1;
6421ec2772eSMartin Schwidefsky 	diag_stat_inc(DIAG_STAT_X258);
64394c12cc7SMartin Schwidefsky 	asm volatile(
6441da177e4SLinus Torvalds 		"	diag	%1,%0,0x258\n"
6451da177e4SLinus Torvalds 		"0:	j	2f\n"
6461da177e4SLinus Torvalds 		"1:	la	%0,8\n"
6471da177e4SLinus Torvalds 		"2:\n"
64894c12cc7SMartin Schwidefsky 		EX_TABLE(0b,1b)
64900e9e664SMartin Schwidefsky 		: "=d" (rc)
65000e9e664SMartin Schwidefsky 		: "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc");
6511da177e4SLinus Torvalds         return rc;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
65400e9e664SMartin Schwidefsky static struct pfault_refbk pfault_fini_refbk = {
6557dd8fe1fSHeiko Carstens 	.refdiagc = 0x258,
6567dd8fe1fSHeiko Carstens 	.reffcode = 1,
6577dd8fe1fSHeiko Carstens 	.refdwlen = 5,
6587dd8fe1fSHeiko Carstens 	.refversn = 2,
6597dd8fe1fSHeiko Carstens };
6601da177e4SLinus Torvalds 
66100e9e664SMartin Schwidefsky void pfault_fini(void)
66200e9e664SMartin Schwidefsky {
66300e9e664SMartin Schwidefsky 
664f32269a0SCarsten Otte 	if (pfault_disable)
6651da177e4SLinus Torvalds 		return;
6661ec2772eSMartin Schwidefsky 	diag_stat_inc(DIAG_STAT_X258);
66794c12cc7SMartin Schwidefsky 	asm volatile(
6681da177e4SLinus Torvalds 		"	diag	%0,0,0x258\n"
6696c22c986SHeiko Carstens 		"0:	nopr	%%r7\n"
67094c12cc7SMartin Schwidefsky 		EX_TABLE(0b,0b)
67100e9e664SMartin Schwidefsky 		: : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc");
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock);
675f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list);
676f2db2e6cSHeiko Carstens 
6770227f7c4SPeter Zijlstra #define PF_COMPLETE	0x0080
6780227f7c4SPeter Zijlstra 
6790227f7c4SPeter Zijlstra /*
6800227f7c4SPeter Zijlstra  * The mechanism of our pfault code: if Linux is running as guest, runs a user
6810227f7c4SPeter Zijlstra  * space process and the user space process accesses a page that the host has
6820227f7c4SPeter Zijlstra  * paged out we get a pfault interrupt.
6830227f7c4SPeter Zijlstra  *
6840227f7c4SPeter Zijlstra  * This allows us, within the guest, to schedule a different process. Without
6850227f7c4SPeter Zijlstra  * this mechanism the host would have to suspend the whole virtual cpu until
6860227f7c4SPeter Zijlstra  * the page has been paged in.
6870227f7c4SPeter Zijlstra  *
6880227f7c4SPeter Zijlstra  * So when we get such an interrupt then we set the state of the current task
6890227f7c4SPeter Zijlstra  * to uninterruptible and also set the need_resched flag. Both happens within
6900227f7c4SPeter Zijlstra  * interrupt context(!). If we later on want to return to user space we
6910227f7c4SPeter Zijlstra  * recognize the need_resched flag and then call schedule().  It's not very
6920227f7c4SPeter Zijlstra  * obvious how this works...
6930227f7c4SPeter Zijlstra  *
6940227f7c4SPeter Zijlstra  * Of course we have a lot of additional fun with the completion interrupt (->
6950227f7c4SPeter Zijlstra  * host signals that a page of a process has been paged in and the process can
6960227f7c4SPeter Zijlstra  * continue to run). This interrupt can arrive on any cpu and, since we have
6970227f7c4SPeter Zijlstra  * virtual cpus, actually appear before the interrupt that signals that a page
6980227f7c4SPeter Zijlstra  * is missing.
6990227f7c4SPeter Zijlstra  */
700fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code,
701f6649a7eSMartin Schwidefsky 			     unsigned int param32, unsigned long param64)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct task_struct *tsk;
7041da177e4SLinus Torvalds 	__u16 subcode;
705f2db2e6cSHeiko Carstens 	pid_t pid;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	/*
7080227f7c4SPeter Zijlstra 	 * Get the external interruption subcode & pfault initial/completion
7090227f7c4SPeter Zijlstra 	 * signal bit. VM stores this in the 'cpu address' field associated
7100227f7c4SPeter Zijlstra 	 * with the external interrupt.
7111da177e4SLinus Torvalds 	 */
712fde15c3aSHeiko Carstens 	subcode = ext_code.subcode;
7131da177e4SLinus Torvalds 	if ((subcode & 0xff00) != __SUBCODE_MASK)
7141da177e4SLinus Torvalds 		return;
715420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_PFL);
716f2db2e6cSHeiko Carstens 	/* Get the token (= pid of the affected task). */
717544e8dd7SHendrik Brueckner 	pid = param64 & LPP_PID_MASK;
718f2db2e6cSHeiko Carstens 	rcu_read_lock();
719f2db2e6cSHeiko Carstens 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
720f2db2e6cSHeiko Carstens 	if (tsk)
721f2db2e6cSHeiko Carstens 		get_task_struct(tsk);
722f2db2e6cSHeiko Carstens 	rcu_read_unlock();
723f2db2e6cSHeiko Carstens 	if (!tsk)
724f2db2e6cSHeiko Carstens 		return;
725f2db2e6cSHeiko Carstens 	spin_lock(&pfault_lock);
7260227f7c4SPeter Zijlstra 	if (subcode & PF_COMPLETE) {
7271da177e4SLinus Torvalds 		/* signal bit is set -> a page has been swapped in by VM */
728f2db2e6cSHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
7291da177e4SLinus Torvalds 			/* Initial interrupt was faster than the completion
7301da177e4SLinus Torvalds 			 * interrupt. pfault_wait is valid. Set pfault_wait
7311da177e4SLinus Torvalds 			 * back to zero and wake up the process. This can
7321da177e4SLinus Torvalds 			 * safely be done because the task is still sleeping
733b6d09449SMartin Schwidefsky 			 * and can't produce new pfaults. */
7341da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
735f2db2e6cSHeiko Carstens 			list_del(&tsk->thread.list);
7361da177e4SLinus Torvalds 			wake_up_process(tsk);
737d5e50a51SHeiko Carstens 			put_task_struct(tsk);
738f2db2e6cSHeiko Carstens 		} else {
739f2db2e6cSHeiko Carstens 			/* Completion interrupt was faster than initial
740f2db2e6cSHeiko Carstens 			 * interrupt. Set pfault_wait to -1 so the initial
741fa2fb2f4SHeiko Carstens 			 * interrupt doesn't put the task to sleep.
742fa2fb2f4SHeiko Carstens 			 * If the task is not running, ignore the completion
743fa2fb2f4SHeiko Carstens 			 * interrupt since it must be a leftover of a PFAULT
744fa2fb2f4SHeiko Carstens 			 * CANCEL operation which didn't remove all pending
745fa2fb2f4SHeiko Carstens 			 * completion interrupts. */
746fa2fb2f4SHeiko Carstens 			if (tsk->state == TASK_RUNNING)
747f2db2e6cSHeiko Carstens 				tsk->thread.pfault_wait = -1;
7481da177e4SLinus Torvalds 		}
7491da177e4SLinus Torvalds 	} else {
7501da177e4SLinus Torvalds 		/* signal bit not set -> a real page is missing. */
751d49f47f8SHeiko Carstens 		if (WARN_ON_ONCE(tsk != current))
752d49f47f8SHeiko Carstens 			goto out;
753d5e50a51SHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
754d5e50a51SHeiko Carstens 			/* Already on the list with a reference: put to sleep */
7550227f7c4SPeter Zijlstra 			goto block;
756d5e50a51SHeiko Carstens 		} else if (tsk->thread.pfault_wait == -1) {
7571da177e4SLinus Torvalds 			/* Completion interrupt was faster than the initial
758f2db2e6cSHeiko Carstens 			 * interrupt (pfault_wait == -1). Set pfault_wait
759f2db2e6cSHeiko Carstens 			 * back to zero and exit. */
7601da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
761f2db2e6cSHeiko Carstens 		} else {
762f2db2e6cSHeiko Carstens 			/* Initial interrupt arrived before completion
763d5e50a51SHeiko Carstens 			 * interrupt. Let the task sleep.
764d5e50a51SHeiko Carstens 			 * An extra task reference is needed since a different
765d5e50a51SHeiko Carstens 			 * cpu may set the task state to TASK_RUNNING again
766d5e50a51SHeiko Carstens 			 * before the scheduler is reached. */
767d5e50a51SHeiko Carstens 			get_task_struct(tsk);
768f2db2e6cSHeiko Carstens 			tsk->thread.pfault_wait = 1;
769f2db2e6cSHeiko Carstens 			list_add(&tsk->thread.list, &pfault_list);
7700227f7c4SPeter Zijlstra block:
7710227f7c4SPeter Zijlstra 			/* Since this must be a userspace fault, there
7720227f7c4SPeter Zijlstra 			 * is no kernel task state to trample. Rely on the
7730227f7c4SPeter Zijlstra 			 * return to userspace schedule() to block. */
7740227f7c4SPeter Zijlstra 			__set_current_state(TASK_UNINTERRUPTIBLE);
7751da177e4SLinus Torvalds 			set_tsk_need_resched(tsk);
776c360192bSMartin Schwidefsky 			set_preempt_need_resched();
7771da177e4SLinus Torvalds 		}
7781da177e4SLinus Torvalds 	}
779d49f47f8SHeiko Carstens out:
780f2db2e6cSHeiko Carstens 	spin_unlock(&pfault_lock);
78154c27791SHeiko Carstens 	put_task_struct(tsk);
782f2db2e6cSHeiko Carstens }
783f2db2e6cSHeiko Carstens 
78484c9ceefSSebastian Andrzej Siewior static int pfault_cpu_dead(unsigned int cpu)
785f2db2e6cSHeiko Carstens {
786f2db2e6cSHeiko Carstens 	struct thread_struct *thread, *next;
787f2db2e6cSHeiko Carstens 	struct task_struct *tsk;
788f2db2e6cSHeiko Carstens 
789f2db2e6cSHeiko Carstens 	spin_lock_irq(&pfault_lock);
790f2db2e6cSHeiko Carstens 	list_for_each_entry_safe(thread, next, &pfault_list, list) {
791f2db2e6cSHeiko Carstens 		thread->pfault_wait = 0;
792f2db2e6cSHeiko Carstens 		list_del(&thread->list);
793f2db2e6cSHeiko Carstens 		tsk = container_of(thread, struct task_struct, thread);
794f2db2e6cSHeiko Carstens 		wake_up_process(tsk);
795d5e50a51SHeiko Carstens 		put_task_struct(tsk);
796f2db2e6cSHeiko Carstens 	}
797f2db2e6cSHeiko Carstens 	spin_unlock_irq(&pfault_lock);
79884c9ceefSSebastian Andrzej Siewior 	return 0;
799f2db2e6cSHeiko Carstens }
8001da177e4SLinus Torvalds 
801fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void)
80229b08d2bSHeiko Carstens {
803fb0a9d7eSHeiko Carstens 	int rc;
80429b08d2bSHeiko Carstens 
8051dad093bSThomas Huth 	rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
8067dd8fe1fSHeiko Carstens 	if (rc)
8077dd8fe1fSHeiko Carstens 		goto out_extint;
8087dd8fe1fSHeiko Carstens 	rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
8097dd8fe1fSHeiko Carstens 	if (rc)
8107dd8fe1fSHeiko Carstens 		goto out_pfault;
81182003c3eSHeiko Carstens 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
81284c9ceefSSebastian Andrzej Siewior 	cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
81384c9ceefSSebastian Andrzej Siewior 				  NULL, pfault_cpu_dead);
8147dd8fe1fSHeiko Carstens 	return 0;
8157dd8fe1fSHeiko Carstens 
8167dd8fe1fSHeiko Carstens out_pfault:
8171dad093bSThomas Huth 	unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
8187dd8fe1fSHeiko Carstens out_extint:
819fb0a9d7eSHeiko Carstens 	pfault_disable = 1;
820fb0a9d7eSHeiko Carstens 	return rc;
821fb0a9d7eSHeiko Carstens }
822fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init);
823fb0a9d7eSHeiko Carstens 
8247dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */
825