xref: /openbmc/linux/arch/s390/mm/fault.c (revision a80313ff)
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,
2469507a5d0SEric W. Biederman 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK),
2479507a5d0SEric W. Biederman 			current);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
250a80313ffSGerald Schaefer const struct exception_table_entry *s390_search_extables(unsigned long addr)
251a80313ffSGerald Schaefer {
252a80313ffSGerald Schaefer 	const struct exception_table_entry *fixup;
253a80313ffSGerald Schaefer 
254a80313ffSGerald Schaefer 	fixup = search_extable(__start_dma_ex_table,
255a80313ffSGerald Schaefer 			       __stop_dma_ex_table - __start_dma_ex_table,
256a80313ffSGerald Schaefer 			       addr);
257a80313ffSGerald Schaefer 	if (!fixup)
258a80313ffSGerald Schaefer 		fixup = search_exception_tables(addr);
259a80313ffSGerald Schaefer 	return fixup;
260a80313ffSGerald Schaefer }
261a80313ffSGerald Schaefer 
262aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs)
26310c1031fSMartin Schwidefsky {
26410c1031fSMartin Schwidefsky 	const struct exception_table_entry *fixup;
26510c1031fSMartin Schwidefsky 
26610c1031fSMartin Schwidefsky 	/* Are we prepared to handle this kernel fault?  */
267a80313ffSGerald Schaefer 	fixup = s390_search_extables(regs->psw.addr);
26810c1031fSMartin Schwidefsky 	if (fixup) {
269fecc868aSHeiko Carstens 		regs->psw.addr = extable_fixup(fixup);
27010c1031fSMartin Schwidefsky 		return;
27110c1031fSMartin Schwidefsky 	}
27210c1031fSMartin Schwidefsky 
27310c1031fSMartin Schwidefsky 	/*
27410c1031fSMartin Schwidefsky 	 * Oops. The kernel tried to access some bad page. We'll have to
27510c1031fSMartin Schwidefsky 	 * terminate things with extreme prejudice.
27610c1031fSMartin Schwidefsky 	 */
2770aaba41bSMartin Schwidefsky 	if (get_fault_type(regs) == KERNEL_FAULT)
27810c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
2793b7df342SHeiko Carstens 		       " in virtual kernel address space\n");
28010c1031fSMartin Schwidefsky 	else
28110c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel paging request"
2823b7df342SHeiko Carstens 		       " in virtual user address space\n");
2833b7df342SHeiko Carstens 	dump_fault_info(regs);
284aa33c8cbSMartin Schwidefsky 	die(regs, "Oops");
28510c1031fSMartin Schwidefsky 	do_exit(SIGKILL);
28610c1031fSMartin Schwidefsky }
28710c1031fSMartin Schwidefsky 
288aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs)
28910c1031fSMartin Schwidefsky {
29010c1031fSMartin Schwidefsky 	/* Low-address protection hit in kernel mode means
29110c1031fSMartin Schwidefsky 	   NULL pointer write access in kernel mode.  */
29210c1031fSMartin Schwidefsky 	if (regs->psw.mask & PSW_MASK_PSTATE) {
29310c1031fSMartin Schwidefsky 		/* Low-address protection hit in user mode 'cannot happen'. */
294aa33c8cbSMartin Schwidefsky 		die (regs, "Low-address protection");
29510c1031fSMartin Schwidefsky 		do_exit(SIGKILL);
29610c1031fSMartin Schwidefsky 	}
29710c1031fSMartin Schwidefsky 
298aa33c8cbSMartin Schwidefsky 	do_no_context(regs);
29910c1031fSMartin Schwidefsky }
30010c1031fSMartin Schwidefsky 
301aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs)
30210c1031fSMartin Schwidefsky {
30310c1031fSMartin Schwidefsky 	/*
30410c1031fSMartin Schwidefsky 	 * Send a sigbus, regardless of whether we were in kernel
30510c1031fSMartin Schwidefsky 	 * or user mode.
30610c1031fSMartin Schwidefsky 	 */
3079507a5d0SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR,
3089507a5d0SEric W. Biederman 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK),
3099507a5d0SEric W. Biederman 			current);
31010c1031fSMartin Schwidefsky }
31110c1031fSMartin Schwidefsky 
31257d7f939SMartin Schwidefsky static noinline int signal_return(struct pt_regs *regs)
31357d7f939SMartin Schwidefsky {
31457d7f939SMartin Schwidefsky 	u16 instruction;
31557d7f939SMartin Schwidefsky 	int rc;
31657d7f939SMartin Schwidefsky 
31757d7f939SMartin Schwidefsky 	rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
31857d7f939SMartin Schwidefsky 	if (rc)
31957d7f939SMartin Schwidefsky 		return rc;
32057d7f939SMartin Schwidefsky 	if (instruction == 0x0a77) {
32157d7f939SMartin Schwidefsky 		set_pt_regs_flag(regs, PIF_SYSCALL);
32257d7f939SMartin Schwidefsky 		regs->int_code = 0x00040077;
32357d7f939SMartin Schwidefsky 		return 0;
32457d7f939SMartin Schwidefsky 	} else if (instruction == 0x0aad) {
32557d7f939SMartin Schwidefsky 		set_pt_regs_flag(regs, PIF_SYSCALL);
32657d7f939SMartin Schwidefsky 		regs->int_code = 0x000400ad;
32757d7f939SMartin Schwidefsky 		return 0;
32857d7f939SMartin Schwidefsky 	}
32957d7f939SMartin Schwidefsky 	return -EACCES;
33057d7f939SMartin Schwidefsky }
33157d7f939SMartin Schwidefsky 
33250a7ca3cSSouptick Joarder static noinline void do_fault_error(struct pt_regs *regs, int access,
33350a7ca3cSSouptick Joarder 					vm_fault_t fault)
33450d7280dSMartin Schwidefsky {
33550d7280dSMartin Schwidefsky 	int si_code;
33650d7280dSMartin Schwidefsky 
33750d7280dSMartin Schwidefsky 	switch (fault) {
33850d7280dSMartin Schwidefsky 	case VM_FAULT_BADACCESS:
33957d7f939SMartin Schwidefsky 		if (access == VM_EXEC && signal_return(regs) == 0)
34057d7f939SMartin Schwidefsky 			break;
34150d7280dSMartin Schwidefsky 	case VM_FAULT_BADMAP:
34250d7280dSMartin Schwidefsky 		/* Bad memory access. Check if it is kernel or user space. */
3437d256175SHeiko Carstens 		if (user_mode(regs)) {
34450d7280dSMartin Schwidefsky 			/* User mode accesses just cause a SIGSEGV */
34550d7280dSMartin Schwidefsky 			si_code = (fault == VM_FAULT_BADMAP) ?
34650d7280dSMartin Schwidefsky 				SEGV_MAPERR : SEGV_ACCERR;
347aa33c8cbSMartin Schwidefsky 			do_sigsegv(regs, si_code);
34857d7f939SMartin Schwidefsky 			break;
34950d7280dSMartin Schwidefsky 		}
35050d7280dSMartin Schwidefsky 	case VM_FAULT_BADCONTEXT:
35124eb3a82SDominik Dingel 	case VM_FAULT_PFAULT:
352aa33c8cbSMartin Schwidefsky 		do_no_context(regs);
35350d7280dSMartin Schwidefsky 		break;
354f2c76e3bSHeiko Carstens 	case VM_FAULT_SIGNAL:
355f2c76e3bSHeiko Carstens 		if (!user_mode(regs))
356f2c76e3bSHeiko Carstens 			do_no_context(regs);
357f2c76e3bSHeiko Carstens 		break;
35850d7280dSMartin Schwidefsky 	default: /* fault & VM_FAULT_ERROR */
35999583181SHeiko Carstens 		if (fault & VM_FAULT_OOM) {
3607d256175SHeiko Carstens 			if (!user_mode(regs))
361aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
36299583181SHeiko Carstens 			else
36350d7280dSMartin Schwidefsky 				pagefault_out_of_memory();
36433692f27SLinus Torvalds 		} else if (fault & VM_FAULT_SIGSEGV) {
36533692f27SLinus Torvalds 			/* Kernel mode? Handle exceptions or die */
36633692f27SLinus Torvalds 			if (!user_mode(regs))
36733692f27SLinus Torvalds 				do_no_context(regs);
36833692f27SLinus Torvalds 			else
36933692f27SLinus Torvalds 				do_sigsegv(regs, SEGV_MAPERR);
37099583181SHeiko Carstens 		} else if (fault & VM_FAULT_SIGBUS) {
37150d7280dSMartin Schwidefsky 			/* Kernel mode? Handle exceptions or die */
3727d256175SHeiko Carstens 			if (!user_mode(regs))
373aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
37436bf9680SMartin Schwidefsky 			else
375aa33c8cbSMartin Schwidefsky 				do_sigbus(regs);
37650d7280dSMartin Schwidefsky 		} else
37750d7280dSMartin Schwidefsky 			BUG();
37850d7280dSMartin Schwidefsky 		break;
37950d7280dSMartin Schwidefsky 	}
38050d7280dSMartin Schwidefsky }
38150d7280dSMartin Schwidefsky 
3821da177e4SLinus Torvalds /*
3831da177e4SLinus Torvalds  * This routine handles page faults.  It determines the address,
3841da177e4SLinus Torvalds  * and the problem, and then passes it off to one of the appropriate
3851da177e4SLinus Torvalds  * routines.
3861da177e4SLinus Torvalds  *
38750d7280dSMartin Schwidefsky  * interruption code (int_code):
3881da177e4SLinus Torvalds  *   04       Protection           ->  Write-Protection  (suprression)
3891da177e4SLinus Torvalds  *   10       Segment translation  ->  Not present       (nullification)
3901da177e4SLinus Torvalds  *   11       Page translation     ->  Not present       (nullification)
3911da177e4SLinus Torvalds  *   3b       Region third trans.  ->  Not present       (nullification)
3921da177e4SLinus Torvalds  */
39350a7ca3cSSouptick Joarder static inline vm_fault_t do_exception(struct pt_regs *regs, int access)
3941da177e4SLinus Torvalds {
39524eb3a82SDominik Dingel 	struct gmap *gmap;
3961da177e4SLinus Torvalds 	struct task_struct *tsk;
3971da177e4SLinus Torvalds 	struct mm_struct *mm;
3981da177e4SLinus Torvalds 	struct vm_area_struct *vma;
3990aaba41bSMartin Schwidefsky 	enum fault_type type;
400aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
4011da177e4SLinus Torvalds 	unsigned long address;
40233ce6140SHeiko Carstens 	unsigned int flags;
40350a7ca3cSSouptick Joarder 	vm_fault_t fault;
40410c1031fSMartin Schwidefsky 
40539efd4ecSMartin Schwidefsky 	tsk = current;
40639efd4ecSMartin Schwidefsky 	/*
40739efd4ecSMartin Schwidefsky 	 * The instruction that caused the program check has
40839efd4ecSMartin Schwidefsky 	 * been nullified. Don't signal single step via SIGTRAP.
40939efd4ecSMartin Schwidefsky 	 */
410d3a73acbSMartin Schwidefsky 	clear_pt_regs_flag(regs, PIF_PER_TRAP);
41139efd4ecSMartin Schwidefsky 
4127ecb344aSMartin Schwidefsky 	if (notify_page_fault(regs))
41350d7280dSMartin Schwidefsky 		return 0;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	mm = tsk->mm;
416aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	/*
4191da177e4SLinus Torvalds 	 * Verify that the fault happened in user space, that
4201da177e4SLinus Torvalds 	 * we are not in an interrupt and that there is a
4211da177e4SLinus Torvalds 	 * user context.
4221da177e4SLinus Torvalds 	 */
42350d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADCONTEXT;
4240aaba41bSMartin Schwidefsky 	type = get_fault_type(regs);
4250aaba41bSMartin Schwidefsky 	switch (type) {
4260aaba41bSMartin Schwidefsky 	case KERNEL_FAULT:
42750d7280dSMartin Schwidefsky 		goto out;
4280aaba41bSMartin Schwidefsky 	case VDSO_FAULT:
4290aaba41bSMartin Schwidefsky 		fault = VM_FAULT_BADMAP;
4300aaba41bSMartin Schwidefsky 		goto out;
4310aaba41bSMartin Schwidefsky 	case USER_FAULT:
4320aaba41bSMartin Schwidefsky 	case GMAP_FAULT:
4330aaba41bSMartin Schwidefsky 		if (faulthandler_disabled() || !mm)
4340aaba41bSMartin Schwidefsky 			goto out;
4350aaba41bSMartin Schwidefsky 		break;
4360aaba41bSMartin Schwidefsky 	}
4371da177e4SLinus Torvalds 
43861365e13SMartin Schwidefsky 	address = trans_exc_code & __FAIL_ADDR_MASK;
439a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
440f2c76e3bSHeiko Carstens 	flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
441759496baSJohannes Weiner 	if (user_mode(regs))
442759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
44333ce6140SHeiko Carstens 	if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
44433ce6140SHeiko Carstens 		flags |= FAULT_FLAG_WRITE;
4451da177e4SLinus Torvalds 	down_read(&mm->mmap_sem);
4461da177e4SLinus Torvalds 
4470aaba41bSMartin Schwidefsky 	gmap = NULL;
4480aaba41bSMartin Schwidefsky 	if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) {
4490aaba41bSMartin Schwidefsky 		gmap = (struct gmap *) S390_lowcore.gmap;
450527e30b4SMartin Schwidefsky 		current->thread.gmap_addr = address;
4514be130a0SMartin Schwidefsky 		current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
4524a494439SDavid Hildenbrand 		current->thread.gmap_int_code = regs->int_code & 0xffff;
453527e30b4SMartin Schwidefsky 		address = __gmap_translate(gmap, address);
454e5992f2eSMartin Schwidefsky 		if (address == -EFAULT) {
455e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
456e5992f2eSMartin Schwidefsky 			goto out_up;
457e5992f2eSMartin Schwidefsky 		}
45824eb3a82SDominik Dingel 		if (gmap->pfault_enabled)
45924eb3a82SDominik Dingel 			flags |= FAULT_FLAG_RETRY_NOWAIT;
460e5992f2eSMartin Schwidefsky 	}
461e5992f2eSMartin Schwidefsky 
462e5992f2eSMartin Schwidefsky retry:
46350d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADMAP;
4641da177e4SLinus Torvalds 	vma = find_vma(mm, address);
4651da177e4SLinus Torvalds 	if (!vma)
46650d7280dSMartin Schwidefsky 		goto out_up;
467c1821c2eSGerald Schaefer 
46850d7280dSMartin Schwidefsky 	if (unlikely(vma->vm_start > address)) {
4691da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
47050d7280dSMartin Schwidefsky 			goto out_up;
4711da177e4SLinus Torvalds 		if (expand_stack(vma, address))
47250d7280dSMartin Schwidefsky 			goto out_up;
47350d7280dSMartin Schwidefsky 	}
47450d7280dSMartin Schwidefsky 
4751da177e4SLinus Torvalds 	/*
4761da177e4SLinus Torvalds 	 * Ok, we have a good vm_area for this memory access, so
4771da177e4SLinus Torvalds 	 * we can handle it..
4781da177e4SLinus Torvalds 	 */
47950d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADACCESS;
4801ab947deSMartin Schwidefsky 	if (unlikely(!(vma->vm_flags & access)))
48150d7280dSMartin Schwidefsky 		goto out_up;
4821da177e4SLinus Torvalds 
48353492b1dSGerald Schaefer 	if (is_vm_hugetlb_page(vma))
48453492b1dSGerald Schaefer 		address &= HPAGE_MASK;
4851da177e4SLinus Torvalds 	/*
4861da177e4SLinus Torvalds 	 * If for any reason at all we couldn't handle the fault,
4871da177e4SLinus Torvalds 	 * make sure we exit gracefully rather than endlessly redo
4881da177e4SLinus Torvalds 	 * the fault.
4891da177e4SLinus Torvalds 	 */
490dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
491f2c76e3bSHeiko Carstens 	/* No reason to continue if interrupted by SIGKILL. */
492f2c76e3bSHeiko Carstens 	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
493f2c76e3bSHeiko Carstens 		fault = VM_FAULT_SIGNAL;
494306d6c49SClaudio Imbrenda 		if (flags & FAULT_FLAG_RETRY_NOWAIT)
495306d6c49SClaudio Imbrenda 			goto out_up;
496f2c76e3bSHeiko Carstens 		goto out;
497f2c76e3bSHeiko Carstens 	}
49850d7280dSMartin Schwidefsky 	if (unlikely(fault & VM_FAULT_ERROR))
49950d7280dSMartin Schwidefsky 		goto out_up;
50050d7280dSMartin Schwidefsky 
50133ce6140SHeiko Carstens 	/*
50233ce6140SHeiko Carstens 	 * Major/minor page fault accounting is only done on the
50333ce6140SHeiko Carstens 	 * initial attempt. If we go through a retry, it is extremely
50433ce6140SHeiko Carstens 	 * likely that the page will be found in page cache at that point.
50533ce6140SHeiko Carstens 	 */
50633ce6140SHeiko Carstens 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
507bde69af2SHeiko Carstens 		if (fault & VM_FAULT_MAJOR) {
50883c54070SNick Piggin 			tsk->maj_flt++;
509a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
510bde69af2SHeiko Carstens 				      regs, address);
511bde69af2SHeiko Carstens 		} else {
51283c54070SNick Piggin 			tsk->min_flt++;
513a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
514bde69af2SHeiko Carstens 				      regs, address);
515bde69af2SHeiko Carstens 		}
51633ce6140SHeiko Carstens 		if (fault & VM_FAULT_RETRY) {
5170aaba41bSMartin Schwidefsky 			if (IS_ENABLED(CONFIG_PGSTE) && gmap &&
5180aaba41bSMartin Schwidefsky 			    (flags & FAULT_FLAG_RETRY_NOWAIT)) {
51924eb3a82SDominik Dingel 				/* FAULT_FLAG_RETRY_NOWAIT has been set,
52024eb3a82SDominik Dingel 				 * mmap_sem has not been released */
52124eb3a82SDominik Dingel 				current->thread.gmap_pfault = 1;
52224eb3a82SDominik Dingel 				fault = VM_FAULT_PFAULT;
52324eb3a82SDominik Dingel 				goto out_up;
52424eb3a82SDominik Dingel 			}
52533ce6140SHeiko Carstens 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
52633ce6140SHeiko Carstens 			 * of starvation. */
52724eb3a82SDominik Dingel 			flags &= ~(FAULT_FLAG_ALLOW_RETRY |
52824eb3a82SDominik Dingel 				   FAULT_FLAG_RETRY_NOWAIT);
52945cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
530e5992f2eSMartin Schwidefsky 			down_read(&mm->mmap_sem);
53133ce6140SHeiko Carstens 			goto retry;
53233ce6140SHeiko Carstens 		}
53333ce6140SHeiko Carstens 	}
5340aaba41bSMartin Schwidefsky 	if (IS_ENABLED(CONFIG_PGSTE) && gmap) {
535527e30b4SMartin Schwidefsky 		address =  __gmap_link(gmap, current->thread.gmap_addr,
536527e30b4SMartin Schwidefsky 				       address);
537527e30b4SMartin Schwidefsky 		if (address == -EFAULT) {
538527e30b4SMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
539527e30b4SMartin Schwidefsky 			goto out_up;
540527e30b4SMartin Schwidefsky 		}
541527e30b4SMartin Schwidefsky 		if (address == -ENOMEM) {
542527e30b4SMartin Schwidefsky 			fault = VM_FAULT_OOM;
543527e30b4SMartin Schwidefsky 			goto out_up;
544527e30b4SMartin Schwidefsky 		}
545527e30b4SMartin Schwidefsky 	}
54650d7280dSMartin Schwidefsky 	fault = 0;
54750d7280dSMartin Schwidefsky out_up:
5481da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
54950d7280dSMartin Schwidefsky out:
55050d7280dSMartin Schwidefsky 	return fault;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5537a5388deSHeiko Carstens void do_protection_exception(struct pt_regs *regs)
5541da177e4SLinus Torvalds {
555aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
55650a7ca3cSSouptick Joarder 	int access;
55750a7ca3cSSouptick Joarder 	vm_fault_t fault;
55861365e13SMartin Schwidefsky 
559aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
560f752ac4dSMartin Schwidefsky 	/*
561f752ac4dSMartin Schwidefsky 	 * Protection exceptions are suppressing, decrement psw address.
562f752ac4dSMartin Schwidefsky 	 * The exception to this rule are aborted transactions, for these
563f752ac4dSMartin Schwidefsky 	 * the PSW already points to the correct location.
564f752ac4dSMartin Schwidefsky 	 */
565f752ac4dSMartin Schwidefsky 	if (!(regs->int_code & 0x200))
566aa33c8cbSMartin Schwidefsky 		regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
56710c1031fSMartin Schwidefsky 	/*
56810c1031fSMartin Schwidefsky 	 * Check for low-address protection.  This needs to be treated
56910c1031fSMartin Schwidefsky 	 * as a special case because the translation exception code
57010c1031fSMartin Schwidefsky 	 * field is not guaranteed to contain valid data in this case.
57110c1031fSMartin Schwidefsky 	 */
57261365e13SMartin Schwidefsky 	if (unlikely(!(trans_exc_code & 4))) {
573aa33c8cbSMartin Schwidefsky 		do_low_address(regs);
57410c1031fSMartin Schwidefsky 		return;
57510c1031fSMartin Schwidefsky 	}
57657d7f939SMartin Schwidefsky 	if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) {
57757d7f939SMartin Schwidefsky 		regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) |
57857d7f939SMartin Schwidefsky 					(regs->psw.addr & PAGE_MASK);
57957d7f939SMartin Schwidefsky 		access = VM_EXEC;
58057d7f939SMartin Schwidefsky 		fault = VM_FAULT_BADACCESS;
58157d7f939SMartin Schwidefsky 	} else {
58257d7f939SMartin Schwidefsky 		access = VM_WRITE;
58357d7f939SMartin Schwidefsky 		fault = do_exception(regs, access);
58457d7f939SMartin Schwidefsky 	}
58550d7280dSMartin Schwidefsky 	if (unlikely(fault))
58657d7f939SMartin Schwidefsky 		do_fault_error(regs, access, fault);
5871da177e4SLinus Torvalds }
5887a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_protection_exception);
5891da177e4SLinus Torvalds 
5907a5388deSHeiko Carstens void do_dat_exception(struct pt_regs *regs)
5911da177e4SLinus Torvalds {
59250a7ca3cSSouptick Joarder 	int access;
59350a7ca3cSSouptick Joarder 	vm_fault_t fault;
59450d7280dSMartin Schwidefsky 
5951ab947deSMartin Schwidefsky 	access = VM_READ | VM_EXEC | VM_WRITE;
596aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, access);
59750d7280dSMartin Schwidefsky 	if (unlikely(fault))
59857d7f939SMartin Schwidefsky 		do_fault_error(regs, access, fault);
5991da177e4SLinus Torvalds }
6007a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_dat_exception);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds #ifdef CONFIG_PFAULT
6031da177e4SLinus Torvalds /*
6041da177e4SLinus Torvalds  * 'pfault' pseudo page faults routines.
6051da177e4SLinus Torvalds  */
606fb0a9d7eSHeiko Carstens static int pfault_disable;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds static int __init nopfault(char *str)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	pfault_disable = 1;
6111da177e4SLinus Torvalds 	return 1;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds __setup("nopfault", nopfault);
6151da177e4SLinus Torvalds 
6167dd8fe1fSHeiko Carstens struct pfault_refbk {
6177dd8fe1fSHeiko Carstens 	u16 refdiagc;
6187dd8fe1fSHeiko Carstens 	u16 reffcode;
6197dd8fe1fSHeiko Carstens 	u16 refdwlen;
6207dd8fe1fSHeiko Carstens 	u16 refversn;
6217dd8fe1fSHeiko Carstens 	u64 refgaddr;
6227dd8fe1fSHeiko Carstens 	u64 refselmk;
6237dd8fe1fSHeiko Carstens 	u64 refcmpmk;
6247dd8fe1fSHeiko Carstens 	u64 reserved;
6257dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8)));
6261da177e4SLinus Torvalds 
62700e9e664SMartin Schwidefsky static struct pfault_refbk pfault_init_refbk = {
6287dd8fe1fSHeiko Carstens 	.refdiagc = 0x258,
6297dd8fe1fSHeiko Carstens 	.reffcode = 0,
6307dd8fe1fSHeiko Carstens 	.refdwlen = 5,
6317dd8fe1fSHeiko Carstens 	.refversn = 2,
632e22cf8caSChristian Borntraeger 	.refgaddr = __LC_LPP,
6337dd8fe1fSHeiko Carstens 	.refselmk = 1ULL << 48,
6347dd8fe1fSHeiko Carstens 	.refcmpmk = 1ULL << 48,
63500e9e664SMartin Schwidefsky 	.reserved = __PF_RES_FIELD
63600e9e664SMartin Schwidefsky };
63700e9e664SMartin Schwidefsky 
63800e9e664SMartin Schwidefsky int pfault_init(void)
63900e9e664SMartin Schwidefsky {
6401da177e4SLinus Torvalds         int rc;
6411da177e4SLinus Torvalds 
642f32269a0SCarsten Otte 	if (pfault_disable)
6431da177e4SLinus Torvalds 		return -1;
6441ec2772eSMartin Schwidefsky 	diag_stat_inc(DIAG_STAT_X258);
64594c12cc7SMartin Schwidefsky 	asm volatile(
6461da177e4SLinus Torvalds 		"	diag	%1,%0,0x258\n"
6471da177e4SLinus Torvalds 		"0:	j	2f\n"
6481da177e4SLinus Torvalds 		"1:	la	%0,8\n"
6491da177e4SLinus Torvalds 		"2:\n"
65094c12cc7SMartin Schwidefsky 		EX_TABLE(0b,1b)
65100e9e664SMartin Schwidefsky 		: "=d" (rc)
65200e9e664SMartin Schwidefsky 		: "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc");
6531da177e4SLinus Torvalds         return rc;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
65600e9e664SMartin Schwidefsky static struct pfault_refbk pfault_fini_refbk = {
6577dd8fe1fSHeiko Carstens 	.refdiagc = 0x258,
6587dd8fe1fSHeiko Carstens 	.reffcode = 1,
6597dd8fe1fSHeiko Carstens 	.refdwlen = 5,
6607dd8fe1fSHeiko Carstens 	.refversn = 2,
6617dd8fe1fSHeiko Carstens };
6621da177e4SLinus Torvalds 
66300e9e664SMartin Schwidefsky void pfault_fini(void)
66400e9e664SMartin Schwidefsky {
66500e9e664SMartin Schwidefsky 
666f32269a0SCarsten Otte 	if (pfault_disable)
6671da177e4SLinus Torvalds 		return;
6681ec2772eSMartin Schwidefsky 	diag_stat_inc(DIAG_STAT_X258);
66994c12cc7SMartin Schwidefsky 	asm volatile(
6701da177e4SLinus Torvalds 		"	diag	%0,0,0x258\n"
6716c22c986SHeiko Carstens 		"0:	nopr	%%r7\n"
67294c12cc7SMartin Schwidefsky 		EX_TABLE(0b,0b)
67300e9e664SMartin Schwidefsky 		: : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc");
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
676f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock);
677f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list);
678f2db2e6cSHeiko Carstens 
6790227f7c4SPeter Zijlstra #define PF_COMPLETE	0x0080
6800227f7c4SPeter Zijlstra 
6810227f7c4SPeter Zijlstra /*
6820227f7c4SPeter Zijlstra  * The mechanism of our pfault code: if Linux is running as guest, runs a user
6830227f7c4SPeter Zijlstra  * space process and the user space process accesses a page that the host has
6840227f7c4SPeter Zijlstra  * paged out we get a pfault interrupt.
6850227f7c4SPeter Zijlstra  *
6860227f7c4SPeter Zijlstra  * This allows us, within the guest, to schedule a different process. Without
6870227f7c4SPeter Zijlstra  * this mechanism the host would have to suspend the whole virtual cpu until
6880227f7c4SPeter Zijlstra  * the page has been paged in.
6890227f7c4SPeter Zijlstra  *
6900227f7c4SPeter Zijlstra  * So when we get such an interrupt then we set the state of the current task
6910227f7c4SPeter Zijlstra  * to uninterruptible and also set the need_resched flag. Both happens within
6920227f7c4SPeter Zijlstra  * interrupt context(!). If we later on want to return to user space we
6930227f7c4SPeter Zijlstra  * recognize the need_resched flag and then call schedule().  It's not very
6940227f7c4SPeter Zijlstra  * obvious how this works...
6950227f7c4SPeter Zijlstra  *
6960227f7c4SPeter Zijlstra  * Of course we have a lot of additional fun with the completion interrupt (->
6970227f7c4SPeter Zijlstra  * host signals that a page of a process has been paged in and the process can
6980227f7c4SPeter Zijlstra  * continue to run). This interrupt can arrive on any cpu and, since we have
6990227f7c4SPeter Zijlstra  * virtual cpus, actually appear before the interrupt that signals that a page
7000227f7c4SPeter Zijlstra  * is missing.
7010227f7c4SPeter Zijlstra  */
702fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code,
703f6649a7eSMartin Schwidefsky 			     unsigned int param32, unsigned long param64)
7041da177e4SLinus Torvalds {
7051da177e4SLinus Torvalds 	struct task_struct *tsk;
7061da177e4SLinus Torvalds 	__u16 subcode;
707f2db2e6cSHeiko Carstens 	pid_t pid;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	/*
7100227f7c4SPeter Zijlstra 	 * Get the external interruption subcode & pfault initial/completion
7110227f7c4SPeter Zijlstra 	 * signal bit. VM stores this in the 'cpu address' field associated
7120227f7c4SPeter Zijlstra 	 * with the external interrupt.
7131da177e4SLinus Torvalds 	 */
714fde15c3aSHeiko Carstens 	subcode = ext_code.subcode;
7151da177e4SLinus Torvalds 	if ((subcode & 0xff00) != __SUBCODE_MASK)
7161da177e4SLinus Torvalds 		return;
717420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_PFL);
718f2db2e6cSHeiko Carstens 	/* Get the token (= pid of the affected task). */
719544e8dd7SHendrik Brueckner 	pid = param64 & LPP_PID_MASK;
720f2db2e6cSHeiko Carstens 	rcu_read_lock();
721f2db2e6cSHeiko Carstens 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
722f2db2e6cSHeiko Carstens 	if (tsk)
723f2db2e6cSHeiko Carstens 		get_task_struct(tsk);
724f2db2e6cSHeiko Carstens 	rcu_read_unlock();
725f2db2e6cSHeiko Carstens 	if (!tsk)
726f2db2e6cSHeiko Carstens 		return;
727f2db2e6cSHeiko Carstens 	spin_lock(&pfault_lock);
7280227f7c4SPeter Zijlstra 	if (subcode & PF_COMPLETE) {
7291da177e4SLinus Torvalds 		/* signal bit is set -> a page has been swapped in by VM */
730f2db2e6cSHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
7311da177e4SLinus Torvalds 			/* Initial interrupt was faster than the completion
7321da177e4SLinus Torvalds 			 * interrupt. pfault_wait is valid. Set pfault_wait
7331da177e4SLinus Torvalds 			 * back to zero and wake up the process. This can
7341da177e4SLinus Torvalds 			 * safely be done because the task is still sleeping
735b6d09449SMartin Schwidefsky 			 * and can't produce new pfaults. */
7361da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
737f2db2e6cSHeiko Carstens 			list_del(&tsk->thread.list);
7381da177e4SLinus Torvalds 			wake_up_process(tsk);
739d5e50a51SHeiko Carstens 			put_task_struct(tsk);
740f2db2e6cSHeiko Carstens 		} else {
741f2db2e6cSHeiko Carstens 			/* Completion interrupt was faster than initial
742f2db2e6cSHeiko Carstens 			 * interrupt. Set pfault_wait to -1 so the initial
743fa2fb2f4SHeiko Carstens 			 * interrupt doesn't put the task to sleep.
744fa2fb2f4SHeiko Carstens 			 * If the task is not running, ignore the completion
745fa2fb2f4SHeiko Carstens 			 * interrupt since it must be a leftover of a PFAULT
746fa2fb2f4SHeiko Carstens 			 * CANCEL operation which didn't remove all pending
747fa2fb2f4SHeiko Carstens 			 * completion interrupts. */
748fa2fb2f4SHeiko Carstens 			if (tsk->state == TASK_RUNNING)
749f2db2e6cSHeiko Carstens 				tsk->thread.pfault_wait = -1;
7501da177e4SLinus Torvalds 		}
7511da177e4SLinus Torvalds 	} else {
7521da177e4SLinus Torvalds 		/* signal bit not set -> a real page is missing. */
753d49f47f8SHeiko Carstens 		if (WARN_ON_ONCE(tsk != current))
754d49f47f8SHeiko Carstens 			goto out;
755d5e50a51SHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
756d5e50a51SHeiko Carstens 			/* Already on the list with a reference: put to sleep */
7570227f7c4SPeter Zijlstra 			goto block;
758d5e50a51SHeiko Carstens 		} else if (tsk->thread.pfault_wait == -1) {
7591da177e4SLinus Torvalds 			/* Completion interrupt was faster than the initial
760f2db2e6cSHeiko Carstens 			 * interrupt (pfault_wait == -1). Set pfault_wait
761f2db2e6cSHeiko Carstens 			 * back to zero and exit. */
7621da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
763f2db2e6cSHeiko Carstens 		} else {
764f2db2e6cSHeiko Carstens 			/* Initial interrupt arrived before completion
765d5e50a51SHeiko Carstens 			 * interrupt. Let the task sleep.
766d5e50a51SHeiko Carstens 			 * An extra task reference is needed since a different
767d5e50a51SHeiko Carstens 			 * cpu may set the task state to TASK_RUNNING again
768d5e50a51SHeiko Carstens 			 * before the scheduler is reached. */
769d5e50a51SHeiko Carstens 			get_task_struct(tsk);
770f2db2e6cSHeiko Carstens 			tsk->thread.pfault_wait = 1;
771f2db2e6cSHeiko Carstens 			list_add(&tsk->thread.list, &pfault_list);
7720227f7c4SPeter Zijlstra block:
7730227f7c4SPeter Zijlstra 			/* Since this must be a userspace fault, there
7740227f7c4SPeter Zijlstra 			 * is no kernel task state to trample. Rely on the
7750227f7c4SPeter Zijlstra 			 * return to userspace schedule() to block. */
7760227f7c4SPeter Zijlstra 			__set_current_state(TASK_UNINTERRUPTIBLE);
7771da177e4SLinus Torvalds 			set_tsk_need_resched(tsk);
778c360192bSMartin Schwidefsky 			set_preempt_need_resched();
7791da177e4SLinus Torvalds 		}
7801da177e4SLinus Torvalds 	}
781d49f47f8SHeiko Carstens out:
782f2db2e6cSHeiko Carstens 	spin_unlock(&pfault_lock);
78354c27791SHeiko Carstens 	put_task_struct(tsk);
784f2db2e6cSHeiko Carstens }
785f2db2e6cSHeiko Carstens 
78684c9ceefSSebastian Andrzej Siewior static int pfault_cpu_dead(unsigned int cpu)
787f2db2e6cSHeiko Carstens {
788f2db2e6cSHeiko Carstens 	struct thread_struct *thread, *next;
789f2db2e6cSHeiko Carstens 	struct task_struct *tsk;
790f2db2e6cSHeiko Carstens 
791f2db2e6cSHeiko Carstens 	spin_lock_irq(&pfault_lock);
792f2db2e6cSHeiko Carstens 	list_for_each_entry_safe(thread, next, &pfault_list, list) {
793f2db2e6cSHeiko Carstens 		thread->pfault_wait = 0;
794f2db2e6cSHeiko Carstens 		list_del(&thread->list);
795f2db2e6cSHeiko Carstens 		tsk = container_of(thread, struct task_struct, thread);
796f2db2e6cSHeiko Carstens 		wake_up_process(tsk);
797d5e50a51SHeiko Carstens 		put_task_struct(tsk);
798f2db2e6cSHeiko Carstens 	}
799f2db2e6cSHeiko Carstens 	spin_unlock_irq(&pfault_lock);
80084c9ceefSSebastian Andrzej Siewior 	return 0;
801f2db2e6cSHeiko Carstens }
8021da177e4SLinus Torvalds 
803fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void)
80429b08d2bSHeiko Carstens {
805fb0a9d7eSHeiko Carstens 	int rc;
80629b08d2bSHeiko Carstens 
8071dad093bSThomas Huth 	rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
8087dd8fe1fSHeiko Carstens 	if (rc)
8097dd8fe1fSHeiko Carstens 		goto out_extint;
8107dd8fe1fSHeiko Carstens 	rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
8117dd8fe1fSHeiko Carstens 	if (rc)
8127dd8fe1fSHeiko Carstens 		goto out_pfault;
81382003c3eSHeiko Carstens 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
81484c9ceefSSebastian Andrzej Siewior 	cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
81584c9ceefSSebastian Andrzej Siewior 				  NULL, pfault_cpu_dead);
8167dd8fe1fSHeiko Carstens 	return 0;
8177dd8fe1fSHeiko Carstens 
8187dd8fe1fSHeiko Carstens out_pfault:
8191dad093bSThomas Huth 	unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
8207dd8fe1fSHeiko Carstens out_extint:
821fb0a9d7eSHeiko Carstens 	pfault_disable = 1;
822fb0a9d7eSHeiko Carstens 	return rc;
823fb0a9d7eSHeiko Carstens }
824fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init);
825fb0a9d7eSHeiko Carstens 
8267dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */
827