xref: /openbmc/linux/arch/s390/mm/fault.c (revision 1dad093b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  S390 version
3a53c8fabSHeiko Carstens  *    Copyright IBM Corp. 1999
41da177e4SLinus Torvalds  *    Author(s): Hartmut Penner (hp@de.ibm.com)
51da177e4SLinus Torvalds  *               Ulrich Weigand (uweigand@de.ibm.com)
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Derived from "arch/i386/mm/fault.c"
81da177e4SLinus Torvalds  *    Copyright (C) 1995  Linus Torvalds
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11052ff461SHeiko Carstens #include <linux/kernel_stat.h>
12cdd6c482SIngo Molnar #include <linux/perf_event.h>
131da177e4SLinus Torvalds #include <linux/signal.h>
141da177e4SLinus Torvalds #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/errno.h>
171da177e4SLinus Torvalds #include <linux/string.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/ptrace.h>
201da177e4SLinus Torvalds #include <linux/mman.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
227757591aSHeiko Carstens #include <linux/compat.h>
231da177e4SLinus Torvalds #include <linux/smp.h>
241eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/console.h>
271da177e4SLinus Torvalds #include <linux/module.h>
281da177e4SLinus Torvalds #include <linux/hardirq.h>
294ba069b8SMichael Grundy #include <linux/kprobes.h>
30be5ec363SMartin Schwidefsky #include <linux/uaccess.h>
3153492b1dSGerald Schaefer #include <linux/hugetlb.h>
32cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
331da177e4SLinus Torvalds #include <asm/pgtable.h>
34d7b250e2SHeiko Carstens #include <asm/irq.h>
356252d702SMartin Schwidefsky #include <asm/mmu_context.h>
36a0616cdeSDavid Howells #include <asm/facility.h>
37a806170eSHeiko Carstens #include "../kernel/entry.h"
381da177e4SLinus Torvalds 
39347a8dc3SMartin Schwidefsky #ifndef CONFIG_64BIT
401da177e4SLinus Torvalds #define __FAIL_ADDR_MASK 0x7ffff000
411da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0200
421da177e4SLinus Torvalds #define __PF_RES_FIELD 0ULL
43347a8dc3SMartin Schwidefsky #else /* CONFIG_64BIT */
441da177e4SLinus Torvalds #define __FAIL_ADDR_MASK -4096L
451da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0600
461da177e4SLinus Torvalds #define __PF_RES_FIELD 0x8000000000000000ULL
47347a8dc3SMartin Schwidefsky #endif /* CONFIG_64BIT */
481da177e4SLinus Torvalds 
4950d7280dSMartin Schwidefsky #define VM_FAULT_BADCONTEXT	0x010000
5050d7280dSMartin Schwidefsky #define VM_FAULT_BADMAP		0x020000
5150d7280dSMartin Schwidefsky #define VM_FAULT_BADACCESS	0x040000
52f2c76e3bSHeiko Carstens #define VM_FAULT_SIGNAL		0x080000
5350d7280dSMartin Schwidefsky 
54a4f32bdbSHeiko Carstens static unsigned long store_indication __read_mostly;
5592f842eaSMartin Schwidefsky 
56a4f32bdbSHeiko Carstens #ifdef CONFIG_64BIT
57a4f32bdbSHeiko Carstens static int __init fault_init(void)
5892f842eaSMartin Schwidefsky {
59a4f32bdbSHeiko Carstens 	if (test_facility(75))
6092f842eaSMartin Schwidefsky 		store_indication = 0xc00;
61a4f32bdbSHeiko Carstens 	return 0;
6292f842eaSMartin Schwidefsky }
63a4f32bdbSHeiko Carstens early_initcall(fault_init);
64a4f32bdbSHeiko Carstens #endif
6592f842eaSMartin Schwidefsky 
667ecb344aSMartin Schwidefsky static inline int notify_page_fault(struct pt_regs *regs)
6710c1031fSMartin Schwidefsky {
6833464e3bSChristoph Hellwig 	int ret = 0;
6933464e3bSChristoph Hellwig 
7033464e3bSChristoph Hellwig 	/* kprobe_running() needs smp_processor_id() */
7122e0a046SHeiko Carstens 	if (kprobes_built_in() && !user_mode(regs)) {
7233464e3bSChristoph Hellwig 		preempt_disable();
7333464e3bSChristoph Hellwig 		if (kprobe_running() && kprobe_fault_handler(regs, 14))
7433464e3bSChristoph Hellwig 			ret = 1;
7533464e3bSChristoph Hellwig 		preempt_enable();
7633464e3bSChristoph Hellwig 	}
7733464e3bSChristoph Hellwig 	return ret;
784ba069b8SMichael Grundy }
794ba069b8SMichael Grundy 
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * Unlock any spinlocks which will prevent us from getting the
83cefc8be8SKirill Korotaev  * message out.
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds void bust_spinlocks(int yes)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	if (yes) {
881da177e4SLinus Torvalds 		oops_in_progress = 1;
891da177e4SLinus Torvalds 	} else {
901da177e4SLinus Torvalds 		int loglevel_save = console_loglevel;
911da177e4SLinus Torvalds 		console_unblank();
921da177e4SLinus Torvalds 		oops_in_progress = 0;
931da177e4SLinus Torvalds 		/*
941da177e4SLinus Torvalds 		 * OK, the message is on the console.  Now we call printk()
951da177e4SLinus Torvalds 		 * without oops_in_progress set so that printk will give klogd
961da177e4SLinus Torvalds 		 * a poke.  Hold onto your hats...
971da177e4SLinus Torvalds 		 */
981da177e4SLinus Torvalds 		console_loglevel = 15;
991da177e4SLinus Torvalds 		printk(" ");
1001da177e4SLinus Torvalds 		console_loglevel = loglevel_save;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
105482b05ddSGerald Schaefer  * Returns the address space associated with the fault.
10661365e13SMartin Schwidefsky  * Returns 0 for kernel space and 1 for user space.
1071da177e4SLinus Torvalds  */
10861365e13SMartin Schwidefsky static inline int user_space_fault(unsigned long trans_exc_code)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	/*
11161365e13SMartin Schwidefsky 	 * The lowest two bits of the translation exception
11261365e13SMartin Schwidefsky 	 * identification indicate which paging table was used.
1131da177e4SLinus Torvalds 	 */
11461365e13SMartin Schwidefsky 	trans_exc_code &= 3;
11561365e13SMartin Schwidefsky 	if (trans_exc_code == 2)
11661365e13SMartin Schwidefsky 		/* Access via secondary space, set_fs setting decides */
11761365e13SMartin Schwidefsky 		return current->thread.mm_segment.ar4;
118482b05ddSGerald Schaefer 	/*
119e258d719SMartin Schwidefsky 	 * Access via primary space or access register is from user space
12061365e13SMartin Schwidefsky 	 * and access via home space is from the kernel.
121482b05ddSGerald Schaefer 	 */
12261365e13SMartin Schwidefsky 	return trans_exc_code != 3;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
125aa33c8cbSMartin Schwidefsky static inline void report_user_fault(struct pt_regs *regs, long signr)
126ab3c68eeSHeiko Carstens {
127ab3c68eeSHeiko Carstens 	if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
128ab3c68eeSHeiko Carstens 		return;
129ab3c68eeSHeiko Carstens 	if (!unhandled_signal(current, signr))
130ab3c68eeSHeiko Carstens 		return;
131ab3c68eeSHeiko Carstens 	if (!printk_ratelimit())
132ab3c68eeSHeiko Carstens 		return;
133aa33c8cbSMartin Schwidefsky 	printk(KERN_ALERT "User process fault: interruption code 0x%X ",
134aa33c8cbSMartin Schwidefsky 	       regs->int_code);
135ab3c68eeSHeiko Carstens 	print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN);
136aa33c8cbSMartin Schwidefsky 	printk(KERN_CONT "\n");
137aa33c8cbSMartin Schwidefsky 	printk(KERN_ALERT "failing address: %lX\n",
138aa33c8cbSMartin Schwidefsky 	       regs->int_parm_long & __FAIL_ADDR_MASK);
139ab3c68eeSHeiko Carstens 	show_regs(regs);
140ab3c68eeSHeiko Carstens }
141ab3c68eeSHeiko Carstens 
1421da177e4SLinus Torvalds /*
1431da177e4SLinus Torvalds  * Send SIGSEGV to task.  This is an external routine
1441da177e4SLinus Torvalds  * to keep the stack usage of do_page_fault small.
1451da177e4SLinus Torvalds  */
146aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	struct siginfo si;
1491da177e4SLinus Torvalds 
150aa33c8cbSMartin Schwidefsky 	report_user_fault(regs, SIGSEGV);
1511da177e4SLinus Torvalds 	si.si_signo = SIGSEGV;
1521da177e4SLinus Torvalds 	si.si_code = si_code;
153aa33c8cbSMartin Schwidefsky 	si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
1541da177e4SLinus Torvalds 	force_sig_info(SIGSEGV, &si, current);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
157aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs)
15810c1031fSMartin Schwidefsky {
15910c1031fSMartin Schwidefsky 	const struct exception_table_entry *fixup;
16061365e13SMartin Schwidefsky 	unsigned long address;
16110c1031fSMartin Schwidefsky 
16210c1031fSMartin Schwidefsky 	/* Are we prepared to handle this kernel fault?  */
16350d7280dSMartin Schwidefsky 	fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
16410c1031fSMartin Schwidefsky 	if (fixup) {
165eb608fb3SHeiko Carstens 		regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
16610c1031fSMartin Schwidefsky 		return;
16710c1031fSMartin Schwidefsky 	}
16810c1031fSMartin Schwidefsky 
16910c1031fSMartin Schwidefsky 	/*
17010c1031fSMartin Schwidefsky 	 * Oops. The kernel tried to access some bad page. We'll have to
17110c1031fSMartin Schwidefsky 	 * terminate things with extreme prejudice.
17210c1031fSMartin Schwidefsky 	 */
173aa33c8cbSMartin Schwidefsky 	address = regs->int_parm_long & __FAIL_ADDR_MASK;
174aa33c8cbSMartin Schwidefsky 	if (!user_space_fault(regs->int_parm_long))
17510c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
17610c1031fSMartin Schwidefsky 		       " at virtual kernel address %p\n", (void *)address);
17710c1031fSMartin Schwidefsky 	else
17810c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel paging request"
17910c1031fSMartin Schwidefsky 		       " at virtual user address %p\n", (void *)address);
18010c1031fSMartin Schwidefsky 
181aa33c8cbSMartin Schwidefsky 	die(regs, "Oops");
18210c1031fSMartin Schwidefsky 	do_exit(SIGKILL);
18310c1031fSMartin Schwidefsky }
18410c1031fSMartin Schwidefsky 
185aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs)
18610c1031fSMartin Schwidefsky {
18710c1031fSMartin Schwidefsky 	/* Low-address protection hit in kernel mode means
18810c1031fSMartin Schwidefsky 	   NULL pointer write access in kernel mode.  */
18910c1031fSMartin Schwidefsky 	if (regs->psw.mask & PSW_MASK_PSTATE) {
19010c1031fSMartin Schwidefsky 		/* Low-address protection hit in user mode 'cannot happen'. */
191aa33c8cbSMartin Schwidefsky 		die (regs, "Low-address protection");
19210c1031fSMartin Schwidefsky 		do_exit(SIGKILL);
19310c1031fSMartin Schwidefsky 	}
19410c1031fSMartin Schwidefsky 
195aa33c8cbSMartin Schwidefsky 	do_no_context(regs);
19610c1031fSMartin Schwidefsky }
19710c1031fSMartin Schwidefsky 
198aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs)
19910c1031fSMartin Schwidefsky {
20010c1031fSMartin Schwidefsky 	struct task_struct *tsk = current;
20136bf9680SMartin Schwidefsky 	struct siginfo si;
20210c1031fSMartin Schwidefsky 
20310c1031fSMartin Schwidefsky 	/*
20410c1031fSMartin Schwidefsky 	 * Send a sigbus, regardless of whether we were in kernel
20510c1031fSMartin Schwidefsky 	 * or user mode.
20610c1031fSMartin Schwidefsky 	 */
20736bf9680SMartin Schwidefsky 	si.si_signo = SIGBUS;
20836bf9680SMartin Schwidefsky 	si.si_errno = 0;
20936bf9680SMartin Schwidefsky 	si.si_code = BUS_ADRERR;
210aa33c8cbSMartin Schwidefsky 	si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
21136bf9680SMartin Schwidefsky 	force_sig_info(SIGBUS, &si, tsk);
21210c1031fSMartin Schwidefsky }
21310c1031fSMartin Schwidefsky 
214aa33c8cbSMartin Schwidefsky static noinline void do_fault_error(struct pt_regs *regs, int fault)
21550d7280dSMartin Schwidefsky {
21650d7280dSMartin Schwidefsky 	int si_code;
21750d7280dSMartin Schwidefsky 
21850d7280dSMartin Schwidefsky 	switch (fault) {
21950d7280dSMartin Schwidefsky 	case VM_FAULT_BADACCESS:
22050d7280dSMartin Schwidefsky 	case VM_FAULT_BADMAP:
22150d7280dSMartin Schwidefsky 		/* Bad memory access. Check if it is kernel or user space. */
2227d256175SHeiko Carstens 		if (user_mode(regs)) {
22350d7280dSMartin Schwidefsky 			/* User mode accesses just cause a SIGSEGV */
22450d7280dSMartin Schwidefsky 			si_code = (fault == VM_FAULT_BADMAP) ?
22550d7280dSMartin Schwidefsky 				SEGV_MAPERR : SEGV_ACCERR;
226aa33c8cbSMartin Schwidefsky 			do_sigsegv(regs, si_code);
22750d7280dSMartin Schwidefsky 			return;
22850d7280dSMartin Schwidefsky 		}
22950d7280dSMartin Schwidefsky 	case VM_FAULT_BADCONTEXT:
230aa33c8cbSMartin Schwidefsky 		do_no_context(regs);
23150d7280dSMartin Schwidefsky 		break;
232f2c76e3bSHeiko Carstens 	case VM_FAULT_SIGNAL:
233f2c76e3bSHeiko Carstens 		if (!user_mode(regs))
234f2c76e3bSHeiko Carstens 			do_no_context(regs);
235f2c76e3bSHeiko Carstens 		break;
23650d7280dSMartin Schwidefsky 	default: /* fault & VM_FAULT_ERROR */
23799583181SHeiko Carstens 		if (fault & VM_FAULT_OOM) {
2387d256175SHeiko Carstens 			if (!user_mode(regs))
239aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
24099583181SHeiko Carstens 			else
24150d7280dSMartin Schwidefsky 				pagefault_out_of_memory();
24299583181SHeiko Carstens 		} else if (fault & VM_FAULT_SIGBUS) {
24350d7280dSMartin Schwidefsky 			/* Kernel mode? Handle exceptions or die */
2447d256175SHeiko Carstens 			if (!user_mode(regs))
245aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
24636bf9680SMartin Schwidefsky 			else
247aa33c8cbSMartin Schwidefsky 				do_sigbus(regs);
24850d7280dSMartin Schwidefsky 		} else
24950d7280dSMartin Schwidefsky 			BUG();
25050d7280dSMartin Schwidefsky 		break;
25150d7280dSMartin Schwidefsky 	}
25250d7280dSMartin Schwidefsky }
25350d7280dSMartin Schwidefsky 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * This routine handles page faults.  It determines the address,
2561da177e4SLinus Torvalds  * and the problem, and then passes it off to one of the appropriate
2571da177e4SLinus Torvalds  * routines.
2581da177e4SLinus Torvalds  *
25950d7280dSMartin Schwidefsky  * interruption code (int_code):
2601da177e4SLinus Torvalds  *   04       Protection           ->  Write-Protection  (suprression)
2611da177e4SLinus Torvalds  *   10       Segment translation  ->  Not present       (nullification)
2621da177e4SLinus Torvalds  *   11       Page translation     ->  Not present       (nullification)
2631da177e4SLinus Torvalds  *   3b       Region third trans.  ->  Not present       (nullification)
2641da177e4SLinus Torvalds  */
265aa33c8cbSMartin Schwidefsky static inline int do_exception(struct pt_regs *regs, int access)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	struct task_struct *tsk;
2681da177e4SLinus Torvalds 	struct mm_struct *mm;
2691da177e4SLinus Torvalds 	struct vm_area_struct *vma;
270aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
2711da177e4SLinus Torvalds 	unsigned long address;
27233ce6140SHeiko Carstens 	unsigned int flags;
27333ce6140SHeiko Carstens 	int fault;
27410c1031fSMartin Schwidefsky 
27539efd4ecSMartin Schwidefsky 	tsk = current;
27639efd4ecSMartin Schwidefsky 	/*
27739efd4ecSMartin Schwidefsky 	 * The instruction that caused the program check has
27839efd4ecSMartin Schwidefsky 	 * been nullified. Don't signal single step via SIGTRAP.
27939efd4ecSMartin Schwidefsky 	 */
28039efd4ecSMartin Schwidefsky 	clear_tsk_thread_flag(tsk, TIF_PER_TRAP);
28139efd4ecSMartin Schwidefsky 
2827ecb344aSMartin Schwidefsky 	if (notify_page_fault(regs))
28350d7280dSMartin Schwidefsky 		return 0;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	mm = tsk->mm;
286aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/*
2891da177e4SLinus Torvalds 	 * Verify that the fault happened in user space, that
2901da177e4SLinus Torvalds 	 * we are not in an interrupt and that there is a
2911da177e4SLinus Torvalds 	 * user context.
2921da177e4SLinus Torvalds 	 */
29350d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADCONTEXT;
29461365e13SMartin Schwidefsky 	if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
29550d7280dSMartin Schwidefsky 		goto out;
2961da177e4SLinus Torvalds 
29761365e13SMartin Schwidefsky 	address = trans_exc_code & __FAIL_ADDR_MASK;
298a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
299f2c76e3bSHeiko Carstens 	flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
300759496baSJohannes Weiner 	if (user_mode(regs))
301759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
30233ce6140SHeiko Carstens 	if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
30333ce6140SHeiko Carstens 		flags |= FAULT_FLAG_WRITE;
3041da177e4SLinus Torvalds 	down_read(&mm->mmap_sem);
3051da177e4SLinus Torvalds 
306e5992f2eSMartin Schwidefsky #ifdef CONFIG_PGSTE
3075e8010cbSMartin Schwidefsky 	if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
308499069e1SCarsten Otte 		address = __gmap_fault(address,
309e5992f2eSMartin Schwidefsky 				     (struct gmap *) S390_lowcore.gmap);
310e5992f2eSMartin Schwidefsky 		if (address == -EFAULT) {
311e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
312e5992f2eSMartin Schwidefsky 			goto out_up;
313e5992f2eSMartin Schwidefsky 		}
314e5992f2eSMartin Schwidefsky 		if (address == -ENOMEM) {
315e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_OOM;
316e5992f2eSMartin Schwidefsky 			goto out_up;
317e5992f2eSMartin Schwidefsky 		}
318e5992f2eSMartin Schwidefsky 	}
319e5992f2eSMartin Schwidefsky #endif
320e5992f2eSMartin Schwidefsky 
321e5992f2eSMartin Schwidefsky retry:
32250d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADMAP;
3231da177e4SLinus Torvalds 	vma = find_vma(mm, address);
3241da177e4SLinus Torvalds 	if (!vma)
32550d7280dSMartin Schwidefsky 		goto out_up;
326c1821c2eSGerald Schaefer 
32750d7280dSMartin Schwidefsky 	if (unlikely(vma->vm_start > address)) {
3281da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
32950d7280dSMartin Schwidefsky 			goto out_up;
3301da177e4SLinus Torvalds 		if (expand_stack(vma, address))
33150d7280dSMartin Schwidefsky 			goto out_up;
33250d7280dSMartin Schwidefsky 	}
33350d7280dSMartin Schwidefsky 
3341da177e4SLinus Torvalds 	/*
3351da177e4SLinus Torvalds 	 * Ok, we have a good vm_area for this memory access, so
3361da177e4SLinus Torvalds 	 * we can handle it..
3371da177e4SLinus Torvalds 	 */
33850d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADACCESS;
3391ab947deSMartin Schwidefsky 	if (unlikely(!(vma->vm_flags & access)))
34050d7280dSMartin Schwidefsky 		goto out_up;
3411da177e4SLinus Torvalds 
34253492b1dSGerald Schaefer 	if (is_vm_hugetlb_page(vma))
34353492b1dSGerald Schaefer 		address &= HPAGE_MASK;
3441da177e4SLinus Torvalds 	/*
3451da177e4SLinus Torvalds 	 * If for any reason at all we couldn't handle the fault,
3461da177e4SLinus Torvalds 	 * make sure we exit gracefully rather than endlessly redo
3471da177e4SLinus Torvalds 	 * the fault.
3481da177e4SLinus Torvalds 	 */
34933ce6140SHeiko Carstens 	fault = handle_mm_fault(mm, vma, address, flags);
350f2c76e3bSHeiko Carstens 	/* No reason to continue if interrupted by SIGKILL. */
351f2c76e3bSHeiko Carstens 	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
352f2c76e3bSHeiko Carstens 		fault = VM_FAULT_SIGNAL;
353f2c76e3bSHeiko Carstens 		goto out;
354f2c76e3bSHeiko Carstens 	}
35550d7280dSMartin Schwidefsky 	if (unlikely(fault & VM_FAULT_ERROR))
35650d7280dSMartin Schwidefsky 		goto out_up;
35750d7280dSMartin Schwidefsky 
35833ce6140SHeiko Carstens 	/*
35933ce6140SHeiko Carstens 	 * Major/minor page fault accounting is only done on the
36033ce6140SHeiko Carstens 	 * initial attempt. If we go through a retry, it is extremely
36133ce6140SHeiko Carstens 	 * likely that the page will be found in page cache at that point.
36233ce6140SHeiko Carstens 	 */
36333ce6140SHeiko Carstens 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
364bde69af2SHeiko Carstens 		if (fault & VM_FAULT_MAJOR) {
36583c54070SNick Piggin 			tsk->maj_flt++;
366a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
367bde69af2SHeiko Carstens 				      regs, address);
368bde69af2SHeiko Carstens 		} else {
36983c54070SNick Piggin 			tsk->min_flt++;
370a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
371bde69af2SHeiko Carstens 				      regs, address);
372bde69af2SHeiko Carstens 		}
37333ce6140SHeiko Carstens 		if (fault & VM_FAULT_RETRY) {
37433ce6140SHeiko Carstens 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
37533ce6140SHeiko Carstens 			 * of starvation. */
37633ce6140SHeiko Carstens 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
37745cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
378e5992f2eSMartin Schwidefsky 			down_read(&mm->mmap_sem);
37933ce6140SHeiko Carstens 			goto retry;
38033ce6140SHeiko Carstens 		}
38133ce6140SHeiko Carstens 	}
38250d7280dSMartin Schwidefsky 	fault = 0;
38350d7280dSMartin Schwidefsky out_up:
3841da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
38550d7280dSMartin Schwidefsky out:
38650d7280dSMartin Schwidefsky 	return fault;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389aa33c8cbSMartin Schwidefsky void __kprobes do_protection_exception(struct pt_regs *regs)
3901da177e4SLinus Torvalds {
391aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
39250d7280dSMartin Schwidefsky 	int fault;
39361365e13SMartin Schwidefsky 
394aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
395f752ac4dSMartin Schwidefsky 	/*
396f752ac4dSMartin Schwidefsky 	 * Protection exceptions are suppressing, decrement psw address.
397f752ac4dSMartin Schwidefsky 	 * The exception to this rule are aborted transactions, for these
398f752ac4dSMartin Schwidefsky 	 * the PSW already points to the correct location.
399f752ac4dSMartin Schwidefsky 	 */
400f752ac4dSMartin Schwidefsky 	if (!(regs->int_code & 0x200))
401aa33c8cbSMartin Schwidefsky 		regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
40210c1031fSMartin Schwidefsky 	/*
40310c1031fSMartin Schwidefsky 	 * Check for low-address protection.  This needs to be treated
40410c1031fSMartin Schwidefsky 	 * as a special case because the translation exception code
40510c1031fSMartin Schwidefsky 	 * field is not guaranteed to contain valid data in this case.
40610c1031fSMartin Schwidefsky 	 */
40761365e13SMartin Schwidefsky 	if (unlikely(!(trans_exc_code & 4))) {
408aa33c8cbSMartin Schwidefsky 		do_low_address(regs);
40910c1031fSMartin Schwidefsky 		return;
41010c1031fSMartin Schwidefsky 	}
411aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, VM_WRITE);
41250d7280dSMartin Schwidefsky 	if (unlikely(fault))
413aa33c8cbSMartin Schwidefsky 		do_fault_error(regs, fault);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
416aa33c8cbSMartin Schwidefsky void __kprobes do_dat_exception(struct pt_regs *regs)
4171da177e4SLinus Torvalds {
4181ab947deSMartin Schwidefsky 	int access, fault;
41950d7280dSMartin Schwidefsky 
4201ab947deSMartin Schwidefsky 	access = VM_READ | VM_EXEC | VM_WRITE;
421aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, access);
42250d7280dSMartin Schwidefsky 	if (unlikely(fault))
423aa33c8cbSMartin Schwidefsky 		do_fault_error(regs, fault);
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261e54622eSMartin Schwidefsky int __handle_fault(unsigned long uaddr, unsigned long pgm_int_code, int write)
4276c1e3e79SGerald Schaefer {
4286c1e3e79SGerald Schaefer 	struct pt_regs regs;
4296c1e3e79SGerald Schaefer 	int access, fault;
4306c1e3e79SGerald Schaefer 
431008c2e8fSHeiko Carstens 	/* Emulate a uaccess fault from kernel mode. */
432e258d719SMartin Schwidefsky 	regs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT | PSW_MASK_MCHECK;
4336c1e3e79SGerald Schaefer 	if (!irqs_disabled())
4346c1e3e79SGerald Schaefer 		regs.psw.mask |= PSW_MASK_IO | PSW_MASK_EXT;
4356c1e3e79SGerald Schaefer 	regs.psw.addr = (unsigned long) __builtin_return_address(0);
4366c1e3e79SGerald Schaefer 	regs.psw.addr |= PSW_ADDR_AMODE;
437aa33c8cbSMartin Schwidefsky 	regs.int_code = pgm_int_code;
438aa33c8cbSMartin Schwidefsky 	regs.int_parm_long = (uaddr & PAGE_MASK) | 2;
4391e54622eSMartin Schwidefsky 	access = write ? VM_WRITE : VM_READ;
440aa33c8cbSMartin Schwidefsky 	fault = do_exception(&regs, access);
441008c2e8fSHeiko Carstens 	/*
442008c2e8fSHeiko Carstens 	 * Since the fault happened in kernel mode while performing a uaccess
443008c2e8fSHeiko Carstens 	 * all we need to do now is emulating a fixup in case "fault" is not
444008c2e8fSHeiko Carstens 	 * zero.
445008c2e8fSHeiko Carstens 	 * For the calling uaccess functions this results always in -EFAULT.
446008c2e8fSHeiko Carstens 	 */
4476c1e3e79SGerald Schaefer 	return fault ? -EFAULT : 0;
4486c1e3e79SGerald Schaefer }
4496c1e3e79SGerald Schaefer 
4501da177e4SLinus Torvalds #ifdef CONFIG_PFAULT
4511da177e4SLinus Torvalds /*
4521da177e4SLinus Torvalds  * 'pfault' pseudo page faults routines.
4531da177e4SLinus Torvalds  */
454fb0a9d7eSHeiko Carstens static int pfault_disable;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static int __init nopfault(char *str)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	pfault_disable = 1;
4591da177e4SLinus Torvalds 	return 1;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds __setup("nopfault", nopfault);
4631da177e4SLinus Torvalds 
4647dd8fe1fSHeiko Carstens struct pfault_refbk {
4657dd8fe1fSHeiko Carstens 	u16 refdiagc;
4667dd8fe1fSHeiko Carstens 	u16 reffcode;
4677dd8fe1fSHeiko Carstens 	u16 refdwlen;
4687dd8fe1fSHeiko Carstens 	u16 refversn;
4697dd8fe1fSHeiko Carstens 	u64 refgaddr;
4707dd8fe1fSHeiko Carstens 	u64 refselmk;
4717dd8fe1fSHeiko Carstens 	u64 refcmpmk;
4727dd8fe1fSHeiko Carstens 	u64 reserved;
4737dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8)));
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds int pfault_init(void)
4761da177e4SLinus Torvalds {
4777dd8fe1fSHeiko Carstens 	struct pfault_refbk refbk = {
4787dd8fe1fSHeiko Carstens 		.refdiagc = 0x258,
4797dd8fe1fSHeiko Carstens 		.reffcode = 0,
4807dd8fe1fSHeiko Carstens 		.refdwlen = 5,
4817dd8fe1fSHeiko Carstens 		.refversn = 2,
4827dd8fe1fSHeiko Carstens 		.refgaddr = __LC_CURRENT_PID,
4837dd8fe1fSHeiko Carstens 		.refselmk = 1ULL << 48,
4847dd8fe1fSHeiko Carstens 		.refcmpmk = 1ULL << 48,
4857dd8fe1fSHeiko Carstens 		.reserved = __PF_RES_FIELD };
4861da177e4SLinus Torvalds         int rc;
4871da177e4SLinus Torvalds 
488f32269a0SCarsten Otte 	if (pfault_disable)
4891da177e4SLinus Torvalds 		return -1;
49094c12cc7SMartin Schwidefsky 	asm volatile(
4911da177e4SLinus Torvalds 		"	diag	%1,%0,0x258\n"
4921da177e4SLinus Torvalds 		"0:	j	2f\n"
4931da177e4SLinus Torvalds 		"1:	la	%0,8\n"
4941da177e4SLinus Torvalds 		"2:\n"
49594c12cc7SMartin Schwidefsky 		EX_TABLE(0b,1b)
496d4b68996SMartin Schwidefsky 		: "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
4971da177e4SLinus Torvalds         return rc;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds void pfault_fini(void)
5011da177e4SLinus Torvalds {
5027dd8fe1fSHeiko Carstens 	struct pfault_refbk refbk = {
5037dd8fe1fSHeiko Carstens 		.refdiagc = 0x258,
5047dd8fe1fSHeiko Carstens 		.reffcode = 1,
5057dd8fe1fSHeiko Carstens 		.refdwlen = 5,
5067dd8fe1fSHeiko Carstens 		.refversn = 2,
5077dd8fe1fSHeiko Carstens 	};
5081da177e4SLinus Torvalds 
509f32269a0SCarsten Otte 	if (pfault_disable)
5101da177e4SLinus Torvalds 		return;
51194c12cc7SMartin Schwidefsky 	asm volatile(
5121da177e4SLinus Torvalds 		"	diag	%0,0,0x258\n"
5131da177e4SLinus Torvalds 		"0:\n"
51494c12cc7SMartin Schwidefsky 		EX_TABLE(0b,0b)
515d4b68996SMartin Schwidefsky 		: : "a" (&refbk), "m" (refbk) : "cc");
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock);
519f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list);
520f2db2e6cSHeiko Carstens 
521fde15c3aSHeiko Carstens static void pfault_interrupt(struct ext_code ext_code,
522f6649a7eSMartin Schwidefsky 			     unsigned int param32, unsigned long param64)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	struct task_struct *tsk;
5251da177e4SLinus Torvalds 	__u16 subcode;
526f2db2e6cSHeiko Carstens 	pid_t pid;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/*
5291da177e4SLinus Torvalds 	 * Get the external interruption subcode & pfault
5301da177e4SLinus Torvalds 	 * initial/completion signal bit. VM stores this
5311da177e4SLinus Torvalds 	 * in the 'cpu address' field associated with the
5321da177e4SLinus Torvalds          * external interrupt.
5331da177e4SLinus Torvalds 	 */
534fde15c3aSHeiko Carstens 	subcode = ext_code.subcode;
5351da177e4SLinus Torvalds 	if ((subcode & 0xff00) != __SUBCODE_MASK)
5361da177e4SLinus Torvalds 		return;
537420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_PFL);
538f2db2e6cSHeiko Carstens 	/* Get the token (= pid of the affected task). */
539f2db2e6cSHeiko Carstens 	pid = sizeof(void *) == 4 ? param32 : param64;
540f2db2e6cSHeiko Carstens 	rcu_read_lock();
541f2db2e6cSHeiko Carstens 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
542f2db2e6cSHeiko Carstens 	if (tsk)
543f2db2e6cSHeiko Carstens 		get_task_struct(tsk);
544f2db2e6cSHeiko Carstens 	rcu_read_unlock();
545f2db2e6cSHeiko Carstens 	if (!tsk)
546f2db2e6cSHeiko Carstens 		return;
547f2db2e6cSHeiko Carstens 	spin_lock(&pfault_lock);
5481da177e4SLinus Torvalds 	if (subcode & 0x0080) {
5491da177e4SLinus Torvalds 		/* signal bit is set -> a page has been swapped in by VM */
550f2db2e6cSHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
5511da177e4SLinus Torvalds 			/* Initial interrupt was faster than the completion
5521da177e4SLinus Torvalds 			 * interrupt. pfault_wait is valid. Set pfault_wait
5531da177e4SLinus Torvalds 			 * back to zero and wake up the process. This can
5541da177e4SLinus Torvalds 			 * safely be done because the task is still sleeping
555b6d09449SMartin Schwidefsky 			 * and can't produce new pfaults. */
5561da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
557f2db2e6cSHeiko Carstens 			list_del(&tsk->thread.list);
5581da177e4SLinus Torvalds 			wake_up_process(tsk);
559d5e50a51SHeiko Carstens 			put_task_struct(tsk);
560f2db2e6cSHeiko Carstens 		} else {
561f2db2e6cSHeiko Carstens 			/* Completion interrupt was faster than initial
562f2db2e6cSHeiko Carstens 			 * interrupt. Set pfault_wait to -1 so the initial
563fa2fb2f4SHeiko Carstens 			 * interrupt doesn't put the task to sleep.
564fa2fb2f4SHeiko Carstens 			 * If the task is not running, ignore the completion
565fa2fb2f4SHeiko Carstens 			 * interrupt since it must be a leftover of a PFAULT
566fa2fb2f4SHeiko Carstens 			 * CANCEL operation which didn't remove all pending
567fa2fb2f4SHeiko Carstens 			 * completion interrupts. */
568fa2fb2f4SHeiko Carstens 			if (tsk->state == TASK_RUNNING)
569f2db2e6cSHeiko Carstens 				tsk->thread.pfault_wait = -1;
5701da177e4SLinus Torvalds 		}
5711da177e4SLinus Torvalds 	} else {
5721da177e4SLinus Torvalds 		/* signal bit not set -> a real page is missing. */
573d49f47f8SHeiko Carstens 		if (WARN_ON_ONCE(tsk != current))
574d49f47f8SHeiko Carstens 			goto out;
575d5e50a51SHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
576d5e50a51SHeiko Carstens 			/* Already on the list with a reference: put to sleep */
5770a16ba78SHeiko Carstens 			__set_task_state(tsk, TASK_UNINTERRUPTIBLE);
578d5e50a51SHeiko Carstens 			set_tsk_need_resched(tsk);
579d5e50a51SHeiko Carstens 		} else if (tsk->thread.pfault_wait == -1) {
5801da177e4SLinus Torvalds 			/* Completion interrupt was faster than the initial
581f2db2e6cSHeiko Carstens 			 * interrupt (pfault_wait == -1). Set pfault_wait
582f2db2e6cSHeiko Carstens 			 * back to zero and exit. */
5831da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
584f2db2e6cSHeiko Carstens 		} else {
585f2db2e6cSHeiko Carstens 			/* Initial interrupt arrived before completion
586d5e50a51SHeiko Carstens 			 * interrupt. Let the task sleep.
587d5e50a51SHeiko Carstens 			 * An extra task reference is needed since a different
588d5e50a51SHeiko Carstens 			 * cpu may set the task state to TASK_RUNNING again
589d5e50a51SHeiko Carstens 			 * before the scheduler is reached. */
590d5e50a51SHeiko Carstens 			get_task_struct(tsk);
591f2db2e6cSHeiko Carstens 			tsk->thread.pfault_wait = 1;
592f2db2e6cSHeiko Carstens 			list_add(&tsk->thread.list, &pfault_list);
5930a16ba78SHeiko Carstens 			__set_task_state(tsk, TASK_UNINTERRUPTIBLE);
5941da177e4SLinus Torvalds 			set_tsk_need_resched(tsk);
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds 	}
597d49f47f8SHeiko Carstens out:
598f2db2e6cSHeiko Carstens 	spin_unlock(&pfault_lock);
59954c27791SHeiko Carstens 	put_task_struct(tsk);
600f2db2e6cSHeiko Carstens }
601f2db2e6cSHeiko Carstens 
602e2741f17SPaul Gortmaker static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
603e2741f17SPaul Gortmaker 			     void *hcpu)
604f2db2e6cSHeiko Carstens {
605f2db2e6cSHeiko Carstens 	struct thread_struct *thread, *next;
606f2db2e6cSHeiko Carstens 	struct task_struct *tsk;
607f2db2e6cSHeiko Carstens 
6081c725922SHeiko Carstens 	switch (action & ~CPU_TASKS_FROZEN) {
609f2db2e6cSHeiko Carstens 	case CPU_DEAD:
610f2db2e6cSHeiko Carstens 		spin_lock_irq(&pfault_lock);
611f2db2e6cSHeiko Carstens 		list_for_each_entry_safe(thread, next, &pfault_list, list) {
612f2db2e6cSHeiko Carstens 			thread->pfault_wait = 0;
613f2db2e6cSHeiko Carstens 			list_del(&thread->list);
614f2db2e6cSHeiko Carstens 			tsk = container_of(thread, struct task_struct, thread);
615f2db2e6cSHeiko Carstens 			wake_up_process(tsk);
616d5e50a51SHeiko Carstens 			put_task_struct(tsk);
617f2db2e6cSHeiko Carstens 		}
618f2db2e6cSHeiko Carstens 		spin_unlock_irq(&pfault_lock);
619f2db2e6cSHeiko Carstens 		break;
620f2db2e6cSHeiko Carstens 	default:
621f2db2e6cSHeiko Carstens 		break;
622f2db2e6cSHeiko Carstens 	}
623f2db2e6cSHeiko Carstens 	return NOTIFY_OK;
624f2db2e6cSHeiko Carstens }
6251da177e4SLinus Torvalds 
626fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void)
62729b08d2bSHeiko Carstens {
628fb0a9d7eSHeiko Carstens 	int rc;
62929b08d2bSHeiko Carstens 
6301dad093bSThomas Huth 	rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
6317dd8fe1fSHeiko Carstens 	if (rc)
6327dd8fe1fSHeiko Carstens 		goto out_extint;
6337dd8fe1fSHeiko Carstens 	rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
6347dd8fe1fSHeiko Carstens 	if (rc)
6357dd8fe1fSHeiko Carstens 		goto out_pfault;
63682003c3eSHeiko Carstens 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
6377dd8fe1fSHeiko Carstens 	hotcpu_notifier(pfault_cpu_notify, 0);
6387dd8fe1fSHeiko Carstens 	return 0;
6397dd8fe1fSHeiko Carstens 
6407dd8fe1fSHeiko Carstens out_pfault:
6411dad093bSThomas Huth 	unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
6427dd8fe1fSHeiko Carstens out_extint:
643fb0a9d7eSHeiko Carstens 	pfault_disable = 1;
644fb0a9d7eSHeiko Carstens 	return rc;
645fb0a9d7eSHeiko Carstens }
646fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init);
647fb0a9d7eSHeiko Carstens 
6487dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */
649