xref: /openbmc/linux/arch/s390/mm/fault.c (revision cd1adf1b)
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>
34e41ba111SSven Schnelle #include <linux/kfence.h>
35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
361ec2772eSMartin Schwidefsky #include <asm/diag.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>
41084ea4d6SVasily Gorbik #include <asm/uv.h>
42a806170eSHeiko Carstens #include "../kernel/entry.h"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define __FAIL_ADDR_MASK -4096L
451da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0600
461da177e4SLinus Torvalds #define __PF_RES_FIELD 0x8000000000000000ULL
471da177e4SLinus Torvalds 
4812437759SChristian Borntraeger #define VM_FAULT_BADCONTEXT	((__force vm_fault_t) 0x010000)
4912437759SChristian Borntraeger #define VM_FAULT_BADMAP		((__force vm_fault_t) 0x020000)
5012437759SChristian Borntraeger #define VM_FAULT_BADACCESS	((__force vm_fault_t) 0x040000)
5112437759SChristian Borntraeger #define VM_FAULT_SIGNAL		((__force vm_fault_t) 0x080000)
5212437759SChristian Borntraeger #define VM_FAULT_PFAULT		((__force vm_fault_t) 0x100000)
5350d7280dSMartin Schwidefsky 
540aaba41bSMartin Schwidefsky enum fault_type {
550aaba41bSMartin Schwidefsky 	KERNEL_FAULT,
560aaba41bSMartin Schwidefsky 	USER_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 */
8087d59863SHeiko Carstens 		if (user_mode(regs))
8187d59863SHeiko Carstens 			return USER_FAULT;
8287d59863SHeiko Carstens 		if (!IS_ENABLED(CONFIG_PGSTE))
8387d59863SHeiko Carstens 			return KERNEL_FAULT;
8487d59863SHeiko Carstens 		if (test_pt_regs_flag(regs, PIF_GUEST_FAULT))
850aaba41bSMartin Schwidefsky 			return GMAP_FAULT;
860aaba41bSMartin Schwidefsky 		return KERNEL_FAULT;
870aaba41bSMartin Schwidefsky 	}
8887d59863SHeiko Carstens 	if (trans_exc_code == 2)
890aaba41bSMartin Schwidefsky 		return USER_FAULT;
90962f0af8SGerald Schaefer 	if (trans_exc_code == 1) {
91962f0af8SGerald Schaefer 		/* access register mode, not used in the kernel */
92962f0af8SGerald Schaefer 		return USER_FAULT;
93962f0af8SGerald Schaefer 	}
940aaba41bSMartin Schwidefsky 	/* home space exception -> access via kernel ASCE */
950aaba41bSMartin Schwidefsky 	return KERNEL_FAULT;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
983b7df342SHeiko Carstens static int bad_address(void *p)
993b7df342SHeiko Carstens {
1003b7df342SHeiko Carstens 	unsigned long dummy;
1013b7df342SHeiko Carstens 
10225f12ae4SChristoph Hellwig 	return get_kernel_nofault(dummy, (unsigned long *)p);
1033b7df342SHeiko Carstens }
1043b7df342SHeiko Carstens 
1053b7df342SHeiko Carstens static void dump_pagetable(unsigned long asce, unsigned long address)
1063b7df342SHeiko Carstens {
107fe7b2747SHeiko Carstens 	unsigned long *table = __va(asce & _ASCE_ORIGIN);
1083b7df342SHeiko Carstens 
1093b7df342SHeiko Carstens 	pr_alert("AS:%016lx ", asce);
1103b7df342SHeiko Carstens 	switch (asce & _ASCE_TYPE_MASK) {
1113b7df342SHeiko Carstens 	case _ASCE_TYPE_REGION1:
112f1c1174fSHeiko Carstens 		table += (address & _REGION1_INDEX) >> _REGION1_SHIFT;
1133b7df342SHeiko Carstens 		if (bad_address(table))
1143b7df342SHeiko Carstens 			goto bad;
1153b7df342SHeiko Carstens 		pr_cont("R1:%016lx ", *table);
1163b7df342SHeiko Carstens 		if (*table & _REGION_ENTRY_INVALID)
1173b7df342SHeiko Carstens 			goto out;
1183b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
1192c7749b9SJoe Perches 		fallthrough;
1203b7df342SHeiko Carstens 	case _ASCE_TYPE_REGION2:
121f1c1174fSHeiko Carstens 		table += (address & _REGION2_INDEX) >> _REGION2_SHIFT;
1223b7df342SHeiko Carstens 		if (bad_address(table))
1233b7df342SHeiko Carstens 			goto bad;
1243b7df342SHeiko Carstens 		pr_cont("R2:%016lx ", *table);
1253b7df342SHeiko Carstens 		if (*table & _REGION_ENTRY_INVALID)
1263b7df342SHeiko Carstens 			goto out;
1273b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
1282c7749b9SJoe Perches 		fallthrough;
1293b7df342SHeiko Carstens 	case _ASCE_TYPE_REGION3:
130f1c1174fSHeiko Carstens 		table += (address & _REGION3_INDEX) >> _REGION3_SHIFT;
1313b7df342SHeiko Carstens 		if (bad_address(table))
1323b7df342SHeiko Carstens 			goto bad;
1333b7df342SHeiko Carstens 		pr_cont("R3:%016lx ", *table);
1343b7df342SHeiko Carstens 		if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
1353b7df342SHeiko Carstens 			goto out;
1363b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
1372c7749b9SJoe Perches 		fallthrough;
1383b7df342SHeiko Carstens 	case _ASCE_TYPE_SEGMENT:
139f1c1174fSHeiko Carstens 		table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
1403b7df342SHeiko Carstens 		if (bad_address(table))
1413b7df342SHeiko Carstens 			goto bad;
14291c0837eSJoe Perches 		pr_cont("S:%016lx ", *table);
1433b7df342SHeiko Carstens 		if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
1443b7df342SHeiko Carstens 			goto out;
1453b7df342SHeiko Carstens 		table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
1463b7df342SHeiko Carstens 	}
147f1c1174fSHeiko Carstens 	table += (address & _PAGE_INDEX) >> _PAGE_SHIFT;
1483b7df342SHeiko Carstens 	if (bad_address(table))
1493b7df342SHeiko Carstens 		goto bad;
1503b7df342SHeiko Carstens 	pr_cont("P:%016lx ", *table);
1513b7df342SHeiko Carstens out:
1523b7df342SHeiko Carstens 	pr_cont("\n");
1533b7df342SHeiko Carstens 	return;
1543b7df342SHeiko Carstens bad:
1553b7df342SHeiko Carstens 	pr_cont("BAD\n");
1563b7df342SHeiko Carstens }
1573b7df342SHeiko Carstens 
1583b7df342SHeiko Carstens static void dump_fault_info(struct pt_regs *regs)
1593b7df342SHeiko Carstens {
1603b7df342SHeiko Carstens 	unsigned long asce;
1613b7df342SHeiko Carstens 
1625d7eccecSHeiko Carstens 	pr_alert("Failing address: %016lx TEID: %016lx\n",
1635d7eccecSHeiko Carstens 		 regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
1643b7df342SHeiko Carstens 	pr_alert("Fault in ");
1653b7df342SHeiko Carstens 	switch (regs->int_parm_long & 3) {
1663b7df342SHeiko Carstens 	case 3:
1673b7df342SHeiko Carstens 		pr_cont("home space ");
1683b7df342SHeiko Carstens 		break;
1693b7df342SHeiko Carstens 	case 2:
1703b7df342SHeiko Carstens 		pr_cont("secondary space ");
1713b7df342SHeiko Carstens 		break;
1723b7df342SHeiko Carstens 	case 1:
1733b7df342SHeiko Carstens 		pr_cont("access register ");
1743b7df342SHeiko Carstens 		break;
1753b7df342SHeiko Carstens 	case 0:
1763b7df342SHeiko Carstens 		pr_cont("primary space ");
1773b7df342SHeiko Carstens 		break;
1783b7df342SHeiko Carstens 	}
1793b7df342SHeiko Carstens 	pr_cont("mode while using ");
1800aaba41bSMartin Schwidefsky 	switch (get_fault_type(regs)) {
1810aaba41bSMartin Schwidefsky 	case USER_FAULT:
1823b7df342SHeiko Carstens 		asce = S390_lowcore.user_asce;
1833b7df342SHeiko Carstens 		pr_cont("user ");
1840aaba41bSMartin Schwidefsky 		break;
1850aaba41bSMartin Schwidefsky 	case GMAP_FAULT:
1860aaba41bSMartin Schwidefsky 		asce = ((struct gmap *) S390_lowcore.gmap)->asce;
1870aaba41bSMartin Schwidefsky 		pr_cont("gmap ");
1880aaba41bSMartin Schwidefsky 		break;
1890aaba41bSMartin Schwidefsky 	case KERNEL_FAULT:
1900aaba41bSMartin Schwidefsky 		asce = S390_lowcore.kernel_asce;
1910aaba41bSMartin Schwidefsky 		pr_cont("kernel ");
1920aaba41bSMartin Schwidefsky 		break;
193bf2f1eeeSMasahiro Yamada 	default:
194bf2f1eeeSMasahiro Yamada 		unreachable();
1953b7df342SHeiko Carstens 	}
1963b7df342SHeiko Carstens 	pr_cont("ASCE.\n");
1973b7df342SHeiko Carstens 	dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
1983b7df342SHeiko Carstens }
1993b7df342SHeiko Carstens 
2005d7eccecSHeiko Carstens int show_unhandled_signals = 1;
2015d7eccecSHeiko Carstens 
2025d7eccecSHeiko Carstens void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
203ab3c68eeSHeiko Carstens {
204ab3c68eeSHeiko Carstens 	if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
205ab3c68eeSHeiko Carstens 		return;
206ab3c68eeSHeiko Carstens 	if (!unhandled_signal(current, signr))
207ab3c68eeSHeiko Carstens 		return;
208ab3c68eeSHeiko Carstens 	if (!printk_ratelimit())
209ab3c68eeSHeiko Carstens 		return;
210413d4047SHeiko Carstens 	printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
211413d4047SHeiko Carstens 	       regs->int_code & 0xffff, regs->int_code >> 17);
2129cb1ccecSHeiko Carstens 	print_vma_addr(KERN_CONT "in ", regs->psw.addr);
213aa33c8cbSMartin Schwidefsky 	printk(KERN_CONT "\n");
2145d7eccecSHeiko Carstens 	if (is_mm_fault)
2153b7df342SHeiko Carstens 		dump_fault_info(regs);
216ab3c68eeSHeiko Carstens 	show_regs(regs);
217ab3c68eeSHeiko Carstens }
218ab3c68eeSHeiko Carstens 
2191da177e4SLinus Torvalds /*
2201da177e4SLinus Torvalds  * Send SIGSEGV to task.  This is an external routine
2211da177e4SLinus Torvalds  * to keep the stack usage of do_page_fault small.
2221da177e4SLinus Torvalds  */
223aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
2241da177e4SLinus Torvalds {
2255d7eccecSHeiko Carstens 	report_user_fault(regs, SIGSEGV, 1);
2269507a5d0SEric W. Biederman 	force_sig_fault(SIGSEGV, si_code,
2272e1661d2SEric W. Biederman 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230a80313ffSGerald Schaefer const struct exception_table_entry *s390_search_extables(unsigned long addr)
231a80313ffSGerald Schaefer {
232a80313ffSGerald Schaefer 	const struct exception_table_entry *fixup;
233a80313ffSGerald Schaefer 
234c78d0c74SHeiko Carstens 	fixup = search_extable(__start_amode31_ex_table,
235c78d0c74SHeiko Carstens 			       __stop_amode31_ex_table - __start_amode31_ex_table,
236a80313ffSGerald Schaefer 			       addr);
237a80313ffSGerald Schaefer 	if (!fixup)
238a80313ffSGerald Schaefer 		fixup = search_exception_tables(addr);
239a80313ffSGerald Schaefer 	return fixup;
240a80313ffSGerald Schaefer }
241a80313ffSGerald Schaefer 
242aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs)
24310c1031fSMartin Schwidefsky {
24410c1031fSMartin Schwidefsky 	const struct exception_table_entry *fixup;
24510c1031fSMartin Schwidefsky 
24610c1031fSMartin Schwidefsky 	/* Are we prepared to handle this kernel fault?  */
247a80313ffSGerald Schaefer 	fixup = s390_search_extables(regs->psw.addr);
24805a68e89SIlya Leoshkevich 	if (fixup && ex_handle(fixup, regs))
24910c1031fSMartin Schwidefsky 		return;
25010c1031fSMartin Schwidefsky 
25110c1031fSMartin Schwidefsky 	/*
25210c1031fSMartin Schwidefsky 	 * Oops. The kernel tried to access some bad page. We'll have to
25310c1031fSMartin Schwidefsky 	 * terminate things with extreme prejudice.
25410c1031fSMartin Schwidefsky 	 */
2550aaba41bSMartin Schwidefsky 	if (get_fault_type(regs) == KERNEL_FAULT)
25610c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
2573b7df342SHeiko Carstens 		       " in virtual kernel address space\n");
25810c1031fSMartin Schwidefsky 	else
25910c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel paging request"
2603b7df342SHeiko Carstens 		       " in virtual user address space\n");
2613b7df342SHeiko Carstens 	dump_fault_info(regs);
262aa33c8cbSMartin Schwidefsky 	die(regs, "Oops");
26310c1031fSMartin Schwidefsky 	do_exit(SIGKILL);
26410c1031fSMartin Schwidefsky }
26510c1031fSMartin Schwidefsky 
266aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs)
26710c1031fSMartin Schwidefsky {
26810c1031fSMartin Schwidefsky 	/* Low-address protection hit in kernel mode means
26910c1031fSMartin Schwidefsky 	   NULL pointer write access in kernel mode.  */
27010c1031fSMartin Schwidefsky 	if (regs->psw.mask & PSW_MASK_PSTATE) {
27110c1031fSMartin Schwidefsky 		/* Low-address protection hit in user mode 'cannot happen'. */
272aa33c8cbSMartin Schwidefsky 		die (regs, "Low-address protection");
27310c1031fSMartin Schwidefsky 		do_exit(SIGKILL);
27410c1031fSMartin Schwidefsky 	}
27510c1031fSMartin Schwidefsky 
276aa33c8cbSMartin Schwidefsky 	do_no_context(regs);
27710c1031fSMartin Schwidefsky }
27810c1031fSMartin Schwidefsky 
279aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs)
28010c1031fSMartin Schwidefsky {
28110c1031fSMartin Schwidefsky 	/*
28210c1031fSMartin Schwidefsky 	 * Send a sigbus, regardless of whether we were in kernel
28310c1031fSMartin Schwidefsky 	 * or user mode.
28410c1031fSMartin Schwidefsky 	 */
2859507a5d0SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR,
2862e1661d2SEric W. Biederman 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
28710c1031fSMartin Schwidefsky }
28810c1031fSMartin Schwidefsky 
28950a7ca3cSSouptick Joarder static noinline void do_fault_error(struct pt_regs *regs, int access,
29050a7ca3cSSouptick Joarder 					vm_fault_t fault)
29150d7280dSMartin Schwidefsky {
29250d7280dSMartin Schwidefsky 	int si_code;
29350d7280dSMartin Schwidefsky 
29450d7280dSMartin Schwidefsky 	switch (fault) {
29550d7280dSMartin Schwidefsky 	case VM_FAULT_BADACCESS:
29650d7280dSMartin Schwidefsky 	case VM_FAULT_BADMAP:
29750d7280dSMartin Schwidefsky 		/* Bad memory access. Check if it is kernel or user space. */
2987d256175SHeiko Carstens 		if (user_mode(regs)) {
29950d7280dSMartin Schwidefsky 			/* User mode accesses just cause a SIGSEGV */
30050d7280dSMartin Schwidefsky 			si_code = (fault == VM_FAULT_BADMAP) ?
30150d7280dSMartin Schwidefsky 				SEGV_MAPERR : SEGV_ACCERR;
302aa33c8cbSMartin Schwidefsky 			do_sigsegv(regs, si_code);
30357d7f939SMartin Schwidefsky 			break;
30450d7280dSMartin Schwidefsky 		}
3052c7749b9SJoe Perches 		fallthrough;
30650d7280dSMartin Schwidefsky 	case VM_FAULT_BADCONTEXT:
30724eb3a82SDominik Dingel 	case VM_FAULT_PFAULT:
308aa33c8cbSMartin Schwidefsky 		do_no_context(regs);
30950d7280dSMartin Schwidefsky 		break;
310f2c76e3bSHeiko Carstens 	case VM_FAULT_SIGNAL:
311f2c76e3bSHeiko Carstens 		if (!user_mode(regs))
312f2c76e3bSHeiko Carstens 			do_no_context(regs);
313f2c76e3bSHeiko Carstens 		break;
31450d7280dSMartin Schwidefsky 	default: /* fault & VM_FAULT_ERROR */
31599583181SHeiko Carstens 		if (fault & VM_FAULT_OOM) {
3167d256175SHeiko Carstens 			if (!user_mode(regs))
317aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
31899583181SHeiko Carstens 			else
31950d7280dSMartin Schwidefsky 				pagefault_out_of_memory();
32033692f27SLinus Torvalds 		} else if (fault & VM_FAULT_SIGSEGV) {
32133692f27SLinus Torvalds 			/* Kernel mode? Handle exceptions or die */
32233692f27SLinus Torvalds 			if (!user_mode(regs))
32333692f27SLinus Torvalds 				do_no_context(regs);
32433692f27SLinus Torvalds 			else
32533692f27SLinus Torvalds 				do_sigsegv(regs, SEGV_MAPERR);
32699583181SHeiko Carstens 		} else if (fault & VM_FAULT_SIGBUS) {
32750d7280dSMartin Schwidefsky 			/* Kernel mode? Handle exceptions or die */
3287d256175SHeiko Carstens 			if (!user_mode(regs))
329aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
33036bf9680SMartin Schwidefsky 			else
331aa33c8cbSMartin Schwidefsky 				do_sigbus(regs);
33250d7280dSMartin Schwidefsky 		} else
33350d7280dSMartin Schwidefsky 			BUG();
33450d7280dSMartin Schwidefsky 		break;
33550d7280dSMartin Schwidefsky 	}
33650d7280dSMartin Schwidefsky }
33750d7280dSMartin Schwidefsky 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * This routine handles page faults.  It determines the address,
3401da177e4SLinus Torvalds  * and the problem, and then passes it off to one of the appropriate
3411da177e4SLinus Torvalds  * routines.
3421da177e4SLinus Torvalds  *
34350d7280dSMartin Schwidefsky  * interruption code (int_code):
3447904aaa8SHeiko Carstens  *   04       Protection           ->  Write-Protection  (suppression)
3451da177e4SLinus Torvalds  *   10       Segment translation  ->  Not present       (nullification)
3461da177e4SLinus Torvalds  *   11       Page translation     ->  Not present       (nullification)
3471da177e4SLinus Torvalds  *   3b       Region third trans.  ->  Not present       (nullification)
3481da177e4SLinus Torvalds  */
34950a7ca3cSSouptick Joarder static inline vm_fault_t do_exception(struct pt_regs *regs, int access)
3501da177e4SLinus Torvalds {
35124eb3a82SDominik Dingel 	struct gmap *gmap;
3521da177e4SLinus Torvalds 	struct task_struct *tsk;
3531da177e4SLinus Torvalds 	struct mm_struct *mm;
3541da177e4SLinus Torvalds 	struct vm_area_struct *vma;
3550aaba41bSMartin Schwidefsky 	enum fault_type type;
356aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
3571da177e4SLinus Torvalds 	unsigned long address;
35833ce6140SHeiko Carstens 	unsigned int flags;
35950a7ca3cSSouptick Joarder 	vm_fault_t fault;
360e41ba111SSven Schnelle 	bool is_write;
36110c1031fSMartin Schwidefsky 
36239efd4ecSMartin Schwidefsky 	tsk = current;
36339efd4ecSMartin Schwidefsky 	/*
36439efd4ecSMartin Schwidefsky 	 * The instruction that caused the program check has
36539efd4ecSMartin Schwidefsky 	 * been nullified. Don't signal single step via SIGTRAP.
36639efd4ecSMartin Schwidefsky 	 */
36756e62a73SSven Schnelle 	clear_thread_flag(TIF_PER_TRAP);
36839efd4ecSMartin Schwidefsky 
369b98cca44SAnshuman Khandual 	if (kprobe_page_fault(regs, 14))
37050d7280dSMartin Schwidefsky 		return 0;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	mm = tsk->mm;
373aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
374e41ba111SSven Schnelle 	address = trans_exc_code & __FAIL_ADDR_MASK;
375e41ba111SSven Schnelle 	is_write = (trans_exc_code & store_indication) == 0x400;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/*
3781da177e4SLinus Torvalds 	 * Verify that the fault happened in user space, that
3791da177e4SLinus Torvalds 	 * we are not in an interrupt and that there is a
3801da177e4SLinus Torvalds 	 * user context.
3811da177e4SLinus Torvalds 	 */
38250d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADCONTEXT;
3830aaba41bSMartin Schwidefsky 	type = get_fault_type(regs);
3840aaba41bSMartin Schwidefsky 	switch (type) {
3850aaba41bSMartin Schwidefsky 	case KERNEL_FAULT:
386e41ba111SSven Schnelle 		if (kfence_handle_page_fault(address, is_write, regs))
387e41ba111SSven Schnelle 			return 0;
38850d7280dSMartin Schwidefsky 		goto out;
3890aaba41bSMartin Schwidefsky 	case USER_FAULT:
3900aaba41bSMartin Schwidefsky 	case GMAP_FAULT:
3910aaba41bSMartin Schwidefsky 		if (faulthandler_disabled() || !mm)
3920aaba41bSMartin Schwidefsky 			goto out;
3930aaba41bSMartin Schwidefsky 		break;
3940aaba41bSMartin Schwidefsky 	}
3951da177e4SLinus Torvalds 
396a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
397dde16072SPeter Xu 	flags = FAULT_FLAG_DEFAULT;
398759496baSJohannes Weiner 	if (user_mode(regs))
399759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
400e41ba111SSven Schnelle 	if (access == VM_WRITE || is_write)
40133ce6140SHeiko Carstens 		flags |= FAULT_FLAG_WRITE;
402d8ed45c5SMichel Lespinasse 	mmap_read_lock(mm);
4031da177e4SLinus Torvalds 
4040aaba41bSMartin Schwidefsky 	gmap = NULL;
4050aaba41bSMartin Schwidefsky 	if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) {
4060aaba41bSMartin Schwidefsky 		gmap = (struct gmap *) S390_lowcore.gmap;
407527e30b4SMartin Schwidefsky 		current->thread.gmap_addr = address;
4084be130a0SMartin Schwidefsky 		current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
4094a494439SDavid Hildenbrand 		current->thread.gmap_int_code = regs->int_code & 0xffff;
410527e30b4SMartin Schwidefsky 		address = __gmap_translate(gmap, address);
411e5992f2eSMartin Schwidefsky 		if (address == -EFAULT) {
412e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
413e5992f2eSMartin Schwidefsky 			goto out_up;
414e5992f2eSMartin Schwidefsky 		}
41524eb3a82SDominik Dingel 		if (gmap->pfault_enabled)
41624eb3a82SDominik Dingel 			flags |= FAULT_FLAG_RETRY_NOWAIT;
417e5992f2eSMartin Schwidefsky 	}
418e5992f2eSMartin Schwidefsky 
419e5992f2eSMartin Schwidefsky retry:
42050d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADMAP;
4211da177e4SLinus Torvalds 	vma = find_vma(mm, address);
4221da177e4SLinus Torvalds 	if (!vma)
42350d7280dSMartin Schwidefsky 		goto out_up;
424c1821c2eSGerald Schaefer 
42550d7280dSMartin Schwidefsky 	if (unlikely(vma->vm_start > address)) {
4261da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
42750d7280dSMartin Schwidefsky 			goto out_up;
4281da177e4SLinus Torvalds 		if (expand_stack(vma, address))
42950d7280dSMartin Schwidefsky 			goto out_up;
43050d7280dSMartin Schwidefsky 	}
43150d7280dSMartin Schwidefsky 
4321da177e4SLinus Torvalds 	/*
4331da177e4SLinus Torvalds 	 * Ok, we have a good vm_area for this memory access, so
4341da177e4SLinus Torvalds 	 * we can handle it..
4351da177e4SLinus Torvalds 	 */
43650d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADACCESS;
4371ab947deSMartin Schwidefsky 	if (unlikely(!(vma->vm_flags & access)))
43850d7280dSMartin Schwidefsky 		goto out_up;
4391da177e4SLinus Torvalds 
44053492b1dSGerald Schaefer 	if (is_vm_hugetlb_page(vma))
44153492b1dSGerald Schaefer 		address &= HPAGE_MASK;
4421da177e4SLinus Torvalds 	/*
4431da177e4SLinus Torvalds 	 * If for any reason at all we couldn't handle the fault,
4441da177e4SLinus Torvalds 	 * make sure we exit gracefully rather than endlessly redo
4451da177e4SLinus Torvalds 	 * the fault.
4461da177e4SLinus Torvalds 	 */
44735e45f3eSPeter Xu 	fault = handle_mm_fault(vma, address, flags, regs);
4484ef87322SPeter Xu 	if (fault_signal_pending(fault, regs)) {
449f2c76e3bSHeiko Carstens 		fault = VM_FAULT_SIGNAL;
450306d6c49SClaudio Imbrenda 		if (flags & FAULT_FLAG_RETRY_NOWAIT)
451306d6c49SClaudio Imbrenda 			goto out_up;
452f2c76e3bSHeiko Carstens 		goto out;
453f2c76e3bSHeiko Carstens 	}
45450d7280dSMartin Schwidefsky 	if (unlikely(fault & VM_FAULT_ERROR))
45550d7280dSMartin Schwidefsky 		goto out_up;
45650d7280dSMartin Schwidefsky 
45733ce6140SHeiko Carstens 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
45833ce6140SHeiko Carstens 		if (fault & VM_FAULT_RETRY) {
4590aaba41bSMartin Schwidefsky 			if (IS_ENABLED(CONFIG_PGSTE) && gmap &&
4600aaba41bSMartin Schwidefsky 			    (flags & FAULT_FLAG_RETRY_NOWAIT)) {
46124eb3a82SDominik Dingel 				/* FAULT_FLAG_RETRY_NOWAIT has been set,
462c1e8d7c6SMichel Lespinasse 				 * mmap_lock has not been released */
46324eb3a82SDominik Dingel 				current->thread.gmap_pfault = 1;
46424eb3a82SDominik Dingel 				fault = VM_FAULT_PFAULT;
46524eb3a82SDominik Dingel 				goto out_up;
46624eb3a82SDominik Dingel 			}
4674064b982SPeter Xu 			flags &= ~FAULT_FLAG_RETRY_NOWAIT;
46845cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
469d8ed45c5SMichel Lespinasse 			mmap_read_lock(mm);
47033ce6140SHeiko Carstens 			goto retry;
47133ce6140SHeiko Carstens 		}
47233ce6140SHeiko Carstens 	}
4730aaba41bSMartin Schwidefsky 	if (IS_ENABLED(CONFIG_PGSTE) && gmap) {
474527e30b4SMartin Schwidefsky 		address =  __gmap_link(gmap, current->thread.gmap_addr,
475527e30b4SMartin Schwidefsky 				       address);
476527e30b4SMartin Schwidefsky 		if (address == -EFAULT) {
477527e30b4SMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
478527e30b4SMartin Schwidefsky 			goto out_up;
479527e30b4SMartin Schwidefsky 		}
480527e30b4SMartin Schwidefsky 		if (address == -ENOMEM) {
481527e30b4SMartin Schwidefsky 			fault = VM_FAULT_OOM;
482527e30b4SMartin Schwidefsky 			goto out_up;
483527e30b4SMartin Schwidefsky 		}
484527e30b4SMartin Schwidefsky 	}
48550d7280dSMartin Schwidefsky 	fault = 0;
48650d7280dSMartin Schwidefsky out_up:
487d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
48850d7280dSMartin Schwidefsky out:
48950d7280dSMartin Schwidefsky 	return fault;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4927a5388deSHeiko Carstens void do_protection_exception(struct pt_regs *regs)
4931da177e4SLinus Torvalds {
494aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
49550a7ca3cSSouptick Joarder 	int access;
49650a7ca3cSSouptick Joarder 	vm_fault_t fault;
49761365e13SMartin Schwidefsky 
498aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
499f752ac4dSMartin Schwidefsky 	/*
500f752ac4dSMartin Schwidefsky 	 * Protection exceptions are suppressing, decrement psw address.
501f752ac4dSMartin Schwidefsky 	 * The exception to this rule are aborted transactions, for these
502f752ac4dSMartin Schwidefsky 	 * the PSW already points to the correct location.
503f752ac4dSMartin Schwidefsky 	 */
504f752ac4dSMartin Schwidefsky 	if (!(regs->int_code & 0x200))
505aa33c8cbSMartin Schwidefsky 		regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
50610c1031fSMartin Schwidefsky 	/*
50710c1031fSMartin Schwidefsky 	 * Check for low-address protection.  This needs to be treated
50810c1031fSMartin Schwidefsky 	 * as a special case because the translation exception code
50910c1031fSMartin Schwidefsky 	 * field is not guaranteed to contain valid data in this case.
51010c1031fSMartin Schwidefsky 	 */
51161365e13SMartin Schwidefsky 	if (unlikely(!(trans_exc_code & 4))) {
512aa33c8cbSMartin Schwidefsky 		do_low_address(regs);
51310c1031fSMartin Schwidefsky 		return;
51410c1031fSMartin Schwidefsky 	}
51557d7f939SMartin Schwidefsky 	if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) {
51657d7f939SMartin Schwidefsky 		regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) |
51757d7f939SMartin Schwidefsky 					(regs->psw.addr & PAGE_MASK);
51857d7f939SMartin Schwidefsky 		access = VM_EXEC;
51957d7f939SMartin Schwidefsky 		fault = VM_FAULT_BADACCESS;
52057d7f939SMartin Schwidefsky 	} else {
52157d7f939SMartin Schwidefsky 		access = VM_WRITE;
52257d7f939SMartin Schwidefsky 		fault = do_exception(regs, access);
52357d7f939SMartin Schwidefsky 	}
52450d7280dSMartin Schwidefsky 	if (unlikely(fault))
52557d7f939SMartin Schwidefsky 		do_fault_error(regs, access, fault);
5261da177e4SLinus Torvalds }
5277a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_protection_exception);
5281da177e4SLinus Torvalds 
5297a5388deSHeiko Carstens void do_dat_exception(struct pt_regs *regs)
5301da177e4SLinus Torvalds {
53150a7ca3cSSouptick Joarder 	int access;
53250a7ca3cSSouptick Joarder 	vm_fault_t fault;
53350d7280dSMartin Schwidefsky 
5346cb4d9a2SAnshuman Khandual 	access = VM_ACCESS_FLAGS;
535aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, access);
53650d7280dSMartin Schwidefsky 	if (unlikely(fault))
53757d7f939SMartin Schwidefsky 		do_fault_error(regs, access, fault);
5381da177e4SLinus Torvalds }
5397a5388deSHeiko Carstens NOKPROBE_SYMBOL(do_dat_exception);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds #ifdef CONFIG_PFAULT
5421da177e4SLinus Torvalds /*
5431da177e4SLinus Torvalds  * 'pfault' pseudo page faults routines.
5441da177e4SLinus Torvalds  */
545fb0a9d7eSHeiko Carstens static int pfault_disable;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static int __init nopfault(char *str)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	pfault_disable = 1;
5501da177e4SLinus Torvalds 	return 1;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds __setup("nopfault", nopfault);
5541da177e4SLinus Torvalds 
5557dd8fe1fSHeiko Carstens struct pfault_refbk {
5567dd8fe1fSHeiko Carstens 	u16 refdiagc;
5577dd8fe1fSHeiko Carstens 	u16 reffcode;
5587dd8fe1fSHeiko Carstens 	u16 refdwlen;
5597dd8fe1fSHeiko Carstens 	u16 refversn;
5607dd8fe1fSHeiko Carstens 	u64 refgaddr;
5617dd8fe1fSHeiko Carstens 	u64 refselmk;
5627dd8fe1fSHeiko Carstens 	u64 refcmpmk;
5637dd8fe1fSHeiko Carstens 	u64 reserved;
5647dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8)));
5651da177e4SLinus Torvalds 
56600e9e664SMartin Schwidefsky static struct pfault_refbk pfault_init_refbk = {
5677dd8fe1fSHeiko Carstens 	.refdiagc = 0x258,
5687dd8fe1fSHeiko Carstens 	.reffcode = 0,
5697dd8fe1fSHeiko Carstens 	.refdwlen = 5,
5707dd8fe1fSHeiko Carstens 	.refversn = 2,
571e22cf8caSChristian Borntraeger 	.refgaddr = __LC_LPP,
5727dd8fe1fSHeiko Carstens 	.refselmk = 1ULL << 48,
5737dd8fe1fSHeiko Carstens 	.refcmpmk = 1ULL << 48,
57400e9e664SMartin Schwidefsky 	.reserved = __PF_RES_FIELD
57500e9e664SMartin Schwidefsky };
57600e9e664SMartin Schwidefsky 
57700e9e664SMartin Schwidefsky int pfault_init(void)
57800e9e664SMartin Schwidefsky {
5791da177e4SLinus Torvalds         int rc;
5801da177e4SLinus Torvalds 
581f32269a0SCarsten Otte 	if (pfault_disable)
5821da177e4SLinus Torvalds 		return -1;
5831ec2772eSMartin Schwidefsky 	diag_stat_inc(DIAG_STAT_X258);
58494c12cc7SMartin Schwidefsky 	asm volatile(
5851da177e4SLinus Torvalds 		"	diag	%1,%0,0x258\n"
5861da177e4SLinus Torvalds 		"0:	j	2f\n"
5871da177e4SLinus Torvalds 		"1:	la	%0,8\n"
5881da177e4SLinus Torvalds 		"2:\n"
58994c12cc7SMartin Schwidefsky 		EX_TABLE(0b,1b)
59000e9e664SMartin Schwidefsky 		: "=d" (rc)
59100e9e664SMartin Schwidefsky 		: "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc");
5921da177e4SLinus Torvalds         return rc;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
59500e9e664SMartin Schwidefsky static struct pfault_refbk pfault_fini_refbk = {
5967dd8fe1fSHeiko Carstens 	.refdiagc = 0x258,
5977dd8fe1fSHeiko Carstens 	.reffcode = 1,
5987dd8fe1fSHeiko Carstens 	.refdwlen = 5,
5997dd8fe1fSHeiko Carstens 	.refversn = 2,
6007dd8fe1fSHeiko Carstens };
6011da177e4SLinus Torvalds 
60200e9e664SMartin Schwidefsky void pfault_fini(void)
60300e9e664SMartin Schwidefsky {
60400e9e664SMartin Schwidefsky 
605f32269a0SCarsten Otte 	if (pfault_disable)
6061da177e4SLinus Torvalds 		return;
6071ec2772eSMartin Schwidefsky 	diag_stat_inc(DIAG_STAT_X258);
60894c12cc7SMartin Schwidefsky 	asm volatile(
6091da177e4SLinus Torvalds 		"	diag	%0,0,0x258\n"
6106c22c986SHeiko Carstens 		"0:	nopr	%%r7\n"
61194c12cc7SMartin Schwidefsky 		EX_TABLE(0b,0b)
61200e9e664SMartin Schwidefsky 		: : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc");
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
615f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock);
616f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list);
617f2db2e6cSHeiko Carstens 
6180227f7c4SPeter Zijlstra #define PF_COMPLETE	0x0080
6190227f7c4SPeter Zijlstra 
6200227f7c4SPeter Zijlstra /*
6210227f7c4SPeter Zijlstra  * The mechanism of our pfault code: if Linux is running as guest, runs a user
6220227f7c4SPeter Zijlstra  * space process and the user space process accesses a page that the host has
6230227f7c4SPeter Zijlstra  * paged out we get a pfault interrupt.
6240227f7c4SPeter Zijlstra  *
6250227f7c4SPeter Zijlstra  * This allows us, within the guest, to schedule a different process. Without
6260227f7c4SPeter Zijlstra  * this mechanism the host would have to suspend the whole virtual cpu until
6270227f7c4SPeter Zijlstra  * the page has been paged in.
6280227f7c4SPeter Zijlstra  *
6290227f7c4SPeter Zijlstra  * So when we get such an interrupt then we set the state of the current task
6300227f7c4SPeter Zijlstra  * to uninterruptible and also set the need_resched flag. Both happens within
6310227f7c4SPeter Zijlstra  * interrupt context(!). If we later on want to return to user space we
6320227f7c4SPeter Zijlstra  * recognize the need_resched flag and then call schedule().  It's not very
6330227f7c4SPeter Zijlstra  * obvious how this works...
6340227f7c4SPeter Zijlstra  *
6350227f7c4SPeter Zijlstra  * Of course we have a lot of additional fun with the completion interrupt (->
6360227f7c4SPeter Zijlstra  * host signals that a page of a process has been paged in and the process can
6370227f7c4SPeter Zijlstra  * continue to run). This interrupt can arrive on any cpu and, since we have
6380227f7c4SPeter Zijlstra  * virtual cpus, actually appear before the interrupt that signals that a page
6390227f7c4SPeter Zijlstra  * is missing.
6400227f7c4SPeter Zijlstra  */
641fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code,
642f6649a7eSMartin Schwidefsky 			     unsigned int param32, unsigned long param64)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	struct task_struct *tsk;
6451da177e4SLinus Torvalds 	__u16 subcode;
646f2db2e6cSHeiko Carstens 	pid_t pid;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	/*
6490227f7c4SPeter Zijlstra 	 * Get the external interruption subcode & pfault initial/completion
6500227f7c4SPeter Zijlstra 	 * signal bit. VM stores this in the 'cpu address' field associated
6510227f7c4SPeter Zijlstra 	 * with the external interrupt.
6521da177e4SLinus Torvalds 	 */
653fde15c3aSHeiko Carstens 	subcode = ext_code.subcode;
6541da177e4SLinus Torvalds 	if ((subcode & 0xff00) != __SUBCODE_MASK)
6551da177e4SLinus Torvalds 		return;
656420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_PFL);
657f2db2e6cSHeiko Carstens 	/* Get the token (= pid of the affected task). */
658544e8dd7SHendrik Brueckner 	pid = param64 & LPP_PID_MASK;
659f2db2e6cSHeiko Carstens 	rcu_read_lock();
660f2db2e6cSHeiko Carstens 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
661f2db2e6cSHeiko Carstens 	if (tsk)
662f2db2e6cSHeiko Carstens 		get_task_struct(tsk);
663f2db2e6cSHeiko Carstens 	rcu_read_unlock();
664f2db2e6cSHeiko Carstens 	if (!tsk)
665f2db2e6cSHeiko Carstens 		return;
666f2db2e6cSHeiko Carstens 	spin_lock(&pfault_lock);
6670227f7c4SPeter Zijlstra 	if (subcode & PF_COMPLETE) {
6681da177e4SLinus Torvalds 		/* signal bit is set -> a page has been swapped in by VM */
669f2db2e6cSHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
6701da177e4SLinus Torvalds 			/* Initial interrupt was faster than the completion
6711da177e4SLinus Torvalds 			 * interrupt. pfault_wait is valid. Set pfault_wait
6721da177e4SLinus Torvalds 			 * back to zero and wake up the process. This can
6731da177e4SLinus Torvalds 			 * safely be done because the task is still sleeping
674b6d09449SMartin Schwidefsky 			 * and can't produce new pfaults. */
6751da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
676f2db2e6cSHeiko Carstens 			list_del(&tsk->thread.list);
6771da177e4SLinus Torvalds 			wake_up_process(tsk);
678d5e50a51SHeiko Carstens 			put_task_struct(tsk);
679f2db2e6cSHeiko Carstens 		} else {
680f2db2e6cSHeiko Carstens 			/* Completion interrupt was faster than initial
681f2db2e6cSHeiko Carstens 			 * interrupt. Set pfault_wait to -1 so the initial
682fa2fb2f4SHeiko Carstens 			 * interrupt doesn't put the task to sleep.
683fa2fb2f4SHeiko Carstens 			 * If the task is not running, ignore the completion
684fa2fb2f4SHeiko Carstens 			 * interrupt since it must be a leftover of a PFAULT
685fa2fb2f4SHeiko Carstens 			 * CANCEL operation which didn't remove all pending
686fa2fb2f4SHeiko Carstens 			 * completion interrupts. */
687b03fbd4fSPeter Zijlstra 			if (task_is_running(tsk))
688f2db2e6cSHeiko Carstens 				tsk->thread.pfault_wait = -1;
6891da177e4SLinus Torvalds 		}
6901da177e4SLinus Torvalds 	} else {
6911da177e4SLinus Torvalds 		/* signal bit not set -> a real page is missing. */
692d49f47f8SHeiko Carstens 		if (WARN_ON_ONCE(tsk != current))
693d49f47f8SHeiko Carstens 			goto out;
694d5e50a51SHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
695d5e50a51SHeiko Carstens 			/* Already on the list with a reference: put to sleep */
6960227f7c4SPeter Zijlstra 			goto block;
697d5e50a51SHeiko Carstens 		} else if (tsk->thread.pfault_wait == -1) {
6981da177e4SLinus Torvalds 			/* Completion interrupt was faster than the initial
699f2db2e6cSHeiko Carstens 			 * interrupt (pfault_wait == -1). Set pfault_wait
700f2db2e6cSHeiko Carstens 			 * back to zero and exit. */
7011da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
702f2db2e6cSHeiko Carstens 		} else {
703f2db2e6cSHeiko Carstens 			/* Initial interrupt arrived before completion
704d5e50a51SHeiko Carstens 			 * interrupt. Let the task sleep.
705d5e50a51SHeiko Carstens 			 * An extra task reference is needed since a different
706d5e50a51SHeiko Carstens 			 * cpu may set the task state to TASK_RUNNING again
707d5e50a51SHeiko Carstens 			 * before the scheduler is reached. */
708d5e50a51SHeiko Carstens 			get_task_struct(tsk);
709f2db2e6cSHeiko Carstens 			tsk->thread.pfault_wait = 1;
710f2db2e6cSHeiko Carstens 			list_add(&tsk->thread.list, &pfault_list);
7110227f7c4SPeter Zijlstra block:
7120227f7c4SPeter Zijlstra 			/* Since this must be a userspace fault, there
7130227f7c4SPeter Zijlstra 			 * is no kernel task state to trample. Rely on the
7140227f7c4SPeter Zijlstra 			 * return to userspace schedule() to block. */
7150227f7c4SPeter Zijlstra 			__set_current_state(TASK_UNINTERRUPTIBLE);
7161da177e4SLinus Torvalds 			set_tsk_need_resched(tsk);
717c360192bSMartin Schwidefsky 			set_preempt_need_resched();
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 	}
720d49f47f8SHeiko Carstens out:
721f2db2e6cSHeiko Carstens 	spin_unlock(&pfault_lock);
72254c27791SHeiko Carstens 	put_task_struct(tsk);
723f2db2e6cSHeiko Carstens }
724f2db2e6cSHeiko Carstens 
72584c9ceefSSebastian Andrzej Siewior static int pfault_cpu_dead(unsigned int cpu)
726f2db2e6cSHeiko Carstens {
727f2db2e6cSHeiko Carstens 	struct thread_struct *thread, *next;
728f2db2e6cSHeiko Carstens 	struct task_struct *tsk;
729f2db2e6cSHeiko Carstens 
730f2db2e6cSHeiko Carstens 	spin_lock_irq(&pfault_lock);
731f2db2e6cSHeiko Carstens 	list_for_each_entry_safe(thread, next, &pfault_list, list) {
732f2db2e6cSHeiko Carstens 		thread->pfault_wait = 0;
733f2db2e6cSHeiko Carstens 		list_del(&thread->list);
734f2db2e6cSHeiko Carstens 		tsk = container_of(thread, struct task_struct, thread);
735f2db2e6cSHeiko Carstens 		wake_up_process(tsk);
736d5e50a51SHeiko Carstens 		put_task_struct(tsk);
737f2db2e6cSHeiko Carstens 	}
738f2db2e6cSHeiko Carstens 	spin_unlock_irq(&pfault_lock);
73984c9ceefSSebastian Andrzej Siewior 	return 0;
740f2db2e6cSHeiko Carstens }
7411da177e4SLinus Torvalds 
742fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void)
74329b08d2bSHeiko Carstens {
744fb0a9d7eSHeiko Carstens 	int rc;
74529b08d2bSHeiko Carstens 
7461dad093bSThomas Huth 	rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
7477dd8fe1fSHeiko Carstens 	if (rc)
7487dd8fe1fSHeiko Carstens 		goto out_extint;
7497dd8fe1fSHeiko Carstens 	rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
7507dd8fe1fSHeiko Carstens 	if (rc)
7517dd8fe1fSHeiko Carstens 		goto out_pfault;
75282003c3eSHeiko Carstens 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
75384c9ceefSSebastian Andrzej Siewior 	cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
75484c9ceefSSebastian Andrzej Siewior 				  NULL, pfault_cpu_dead);
7557dd8fe1fSHeiko Carstens 	return 0;
7567dd8fe1fSHeiko Carstens 
7577dd8fe1fSHeiko Carstens out_pfault:
7581dad093bSThomas Huth 	unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
7597dd8fe1fSHeiko Carstens out_extint:
760fb0a9d7eSHeiko Carstens 	pfault_disable = 1;
761fb0a9d7eSHeiko Carstens 	return rc;
762fb0a9d7eSHeiko Carstens }
763fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init);
764fb0a9d7eSHeiko Carstens 
7657dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */
766084ea4d6SVasily Gorbik 
767084ea4d6SVasily Gorbik #if IS_ENABLED(CONFIG_PGSTE)
76817a363dcSHeiko Carstens 
769084ea4d6SVasily Gorbik void do_secure_storage_access(struct pt_regs *regs)
770084ea4d6SVasily Gorbik {
771084ea4d6SVasily Gorbik 	unsigned long addr = regs->int_parm_long & __FAIL_ADDR_MASK;
772084ea4d6SVasily Gorbik 	struct vm_area_struct *vma;
773084ea4d6SVasily Gorbik 	struct mm_struct *mm;
774084ea4d6SVasily Gorbik 	struct page *page;
775084ea4d6SVasily Gorbik 	int rc;
776084ea4d6SVasily Gorbik 
77785b18d7bSJanosch Frank 	/*
77885b18d7bSJanosch Frank 	 * bit 61 tells us if the address is valid, if it's not we
77985b18d7bSJanosch Frank 	 * have a major problem and should stop the kernel or send a
78085b18d7bSJanosch Frank 	 * SIGSEGV to the process. Unfortunately bit 61 is not
78185b18d7bSJanosch Frank 	 * reliable without the misc UV feature so we need to check
78285b18d7bSJanosch Frank 	 * for that as well.
78385b18d7bSJanosch Frank 	 */
78485b18d7bSJanosch Frank 	if (test_bit_inv(BIT_UV_FEAT_MISC, &uv_info.uv_feature_indications) &&
78585b18d7bSJanosch Frank 	    !test_bit_inv(61, &regs->int_parm_long)) {
78685b18d7bSJanosch Frank 		/*
78785b18d7bSJanosch Frank 		 * When this happens, userspace did something that it
78885b18d7bSJanosch Frank 		 * was not supposed to do, e.g. branching into secure
78985b18d7bSJanosch Frank 		 * memory. Trigger a segmentation fault.
79085b18d7bSJanosch Frank 		 */
79185b18d7bSJanosch Frank 		if (user_mode(regs)) {
79285b18d7bSJanosch Frank 			send_sig(SIGSEGV, current, 0);
79385b18d7bSJanosch Frank 			return;
79485b18d7bSJanosch Frank 		}
79585b18d7bSJanosch Frank 
79685b18d7bSJanosch Frank 		/*
79785b18d7bSJanosch Frank 		 * The kernel should never run into this case and we
79885b18d7bSJanosch Frank 		 * have no way out of this situation.
79985b18d7bSJanosch Frank 		 */
80085b18d7bSJanosch Frank 		panic("Unexpected PGM 0x3d with TEID bit 61=0");
80185b18d7bSJanosch Frank 	}
80285b18d7bSJanosch Frank 
803084ea4d6SVasily Gorbik 	switch (get_fault_type(regs)) {
804084ea4d6SVasily Gorbik 	case USER_FAULT:
805084ea4d6SVasily Gorbik 		mm = current->mm;
806d8ed45c5SMichel Lespinasse 		mmap_read_lock(mm);
807084ea4d6SVasily Gorbik 		vma = find_vma(mm, addr);
808084ea4d6SVasily Gorbik 		if (!vma) {
809d8ed45c5SMichel Lespinasse 			mmap_read_unlock(mm);
810084ea4d6SVasily Gorbik 			do_fault_error(regs, VM_READ | VM_WRITE, VM_FAULT_BADMAP);
811084ea4d6SVasily Gorbik 			break;
812084ea4d6SVasily Gorbik 		}
813084ea4d6SVasily Gorbik 		page = follow_page(vma, addr, FOLL_WRITE | FOLL_GET);
814084ea4d6SVasily Gorbik 		if (IS_ERR_OR_NULL(page)) {
815d8ed45c5SMichel Lespinasse 			mmap_read_unlock(mm);
816084ea4d6SVasily Gorbik 			break;
817084ea4d6SVasily Gorbik 		}
818084ea4d6SVasily Gorbik 		if (arch_make_page_accessible(page))
819084ea4d6SVasily Gorbik 			send_sig(SIGSEGV, current, 0);
820084ea4d6SVasily Gorbik 		put_page(page);
821d8ed45c5SMichel Lespinasse 		mmap_read_unlock(mm);
822084ea4d6SVasily Gorbik 		break;
823084ea4d6SVasily Gorbik 	case KERNEL_FAULT:
824084ea4d6SVasily Gorbik 		page = phys_to_page(addr);
825*cd1adf1bSLinus Torvalds 		if (unlikely(!try_get_page(page)))
826084ea4d6SVasily Gorbik 			break;
827084ea4d6SVasily Gorbik 		rc = arch_make_page_accessible(page);
828084ea4d6SVasily Gorbik 		put_page(page);
829084ea4d6SVasily Gorbik 		if (rc)
830084ea4d6SVasily Gorbik 			BUG();
831084ea4d6SVasily Gorbik 		break;
832084ea4d6SVasily Gorbik 	case GMAP_FAULT:
833084ea4d6SVasily Gorbik 	default:
834084ea4d6SVasily Gorbik 		do_fault_error(regs, VM_READ | VM_WRITE, VM_FAULT_BADMAP);
835084ea4d6SVasily Gorbik 		WARN_ON_ONCE(1);
836084ea4d6SVasily Gorbik 	}
837084ea4d6SVasily Gorbik }
838084ea4d6SVasily Gorbik NOKPROBE_SYMBOL(do_secure_storage_access);
839084ea4d6SVasily Gorbik 
840084ea4d6SVasily Gorbik void do_non_secure_storage_access(struct pt_regs *regs)
841084ea4d6SVasily Gorbik {
842084ea4d6SVasily Gorbik 	unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
843084ea4d6SVasily Gorbik 	struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
844084ea4d6SVasily Gorbik 
845084ea4d6SVasily Gorbik 	if (get_fault_type(regs) != GMAP_FAULT) {
846084ea4d6SVasily Gorbik 		do_fault_error(regs, VM_READ | VM_WRITE, VM_FAULT_BADMAP);
847084ea4d6SVasily Gorbik 		WARN_ON_ONCE(1);
848084ea4d6SVasily Gorbik 		return;
849084ea4d6SVasily Gorbik 	}
850084ea4d6SVasily Gorbik 
851084ea4d6SVasily Gorbik 	if (gmap_convert_to_secure(gmap, gaddr) == -EINVAL)
852084ea4d6SVasily Gorbik 		send_sig(SIGSEGV, current, 0);
853084ea4d6SVasily Gorbik }
854084ea4d6SVasily Gorbik NOKPROBE_SYMBOL(do_non_secure_storage_access);
855084ea4d6SVasily Gorbik 
856cd4d3d5fSJanosch Frank void do_secure_storage_violation(struct pt_regs *regs)
857cd4d3d5fSJanosch Frank {
858cd4d3d5fSJanosch Frank 	/*
859cd4d3d5fSJanosch Frank 	 * Either KVM messed up the secure guest mapping or the same
860cd4d3d5fSJanosch Frank 	 * page is mapped into multiple secure guests.
861cd4d3d5fSJanosch Frank 	 *
862cd4d3d5fSJanosch Frank 	 * This exception is only triggered when a guest 2 is running
863cd4d3d5fSJanosch Frank 	 * and can therefore never occur in kernel context.
864cd4d3d5fSJanosch Frank 	 */
865cd4d3d5fSJanosch Frank 	printk_ratelimited(KERN_WARNING
866cd4d3d5fSJanosch Frank 			   "Secure storage violation in task: %s, pid %d\n",
867cd4d3d5fSJanosch Frank 			   current->comm, current->pid);
868cd4d3d5fSJanosch Frank 	send_sig(SIGSEGV, current, 0);
869cd4d3d5fSJanosch Frank }
870cd4d3d5fSJanosch Frank 
87117a363dcSHeiko Carstens #endif /* CONFIG_PGSTE */
872