xref: /openbmc/linux/arch/s390/mm/fault.c (revision aa33c8cb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  arch/s390/mm/fault.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  S390 version
51da177e4SLinus Torvalds  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
61da177e4SLinus Torvalds  *    Author(s): Hartmut Penner (hp@de.ibm.com)
71da177e4SLinus Torvalds  *               Ulrich Weigand (uweigand@de.ibm.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  Derived from "arch/i386/mm/fault.c"
101da177e4SLinus Torvalds  *    Copyright (C) 1995  Linus Torvalds
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13052ff461SHeiko Carstens #include <linux/kernel_stat.h>
14cdd6c482SIngo Molnar #include <linux/perf_event.h>
151da177e4SLinus Torvalds #include <linux/signal.h>
161da177e4SLinus Torvalds #include <linux/sched.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>
291da177e4SLinus Torvalds #include <linux/module.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>
351da177e4SLinus Torvalds #include <asm/system.h>
361da177e4SLinus Torvalds #include <asm/pgtable.h>
37d7b250e2SHeiko Carstens #include <asm/irq.h>
386252d702SMartin Schwidefsky #include <asm/mmu_context.h>
3950d7280dSMartin Schwidefsky #include <asm/compat.h>
40a806170eSHeiko Carstens #include "../kernel/entry.h"
411da177e4SLinus Torvalds 
42347a8dc3SMartin Schwidefsky #ifndef CONFIG_64BIT
431da177e4SLinus Torvalds #define __FAIL_ADDR_MASK 0x7ffff000
441da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0200
451da177e4SLinus Torvalds #define __PF_RES_FIELD 0ULL
46347a8dc3SMartin Schwidefsky #else /* CONFIG_64BIT */
471da177e4SLinus Torvalds #define __FAIL_ADDR_MASK -4096L
481da177e4SLinus Torvalds #define __SUBCODE_MASK 0x0600
491da177e4SLinus Torvalds #define __PF_RES_FIELD 0x8000000000000000ULL
50347a8dc3SMartin Schwidefsky #endif /* CONFIG_64BIT */
511da177e4SLinus Torvalds 
5250d7280dSMartin Schwidefsky #define VM_FAULT_BADCONTEXT	0x010000
5350d7280dSMartin Schwidefsky #define VM_FAULT_BADMAP		0x020000
5450d7280dSMartin Schwidefsky #define VM_FAULT_BADACCESS	0x040000
5550d7280dSMartin Schwidefsky 
5692f842eaSMartin Schwidefsky static unsigned long store_indication;
5792f842eaSMartin Schwidefsky 
5892f842eaSMartin Schwidefsky void fault_init(void)
5992f842eaSMartin Schwidefsky {
6014375bc4SMartin Schwidefsky 	if (test_facility(2) && test_facility(75))
6192f842eaSMartin Schwidefsky 		store_indication = 0xc00;
6292f842eaSMartin Schwidefsky }
6392f842eaSMartin Schwidefsky 
647ecb344aSMartin Schwidefsky static inline int notify_page_fault(struct pt_regs *regs)
6510c1031fSMartin Schwidefsky {
6633464e3bSChristoph Hellwig 	int ret = 0;
6733464e3bSChristoph Hellwig 
6833464e3bSChristoph Hellwig 	/* kprobe_running() needs smp_processor_id() */
6922e0a046SHeiko Carstens 	if (kprobes_built_in() && !user_mode(regs)) {
7033464e3bSChristoph Hellwig 		preempt_disable();
7133464e3bSChristoph Hellwig 		if (kprobe_running() && kprobe_fault_handler(regs, 14))
7233464e3bSChristoph Hellwig 			ret = 1;
7333464e3bSChristoph Hellwig 		preempt_enable();
7433464e3bSChristoph Hellwig 	}
7533464e3bSChristoph Hellwig 	return ret;
764ba069b8SMichael Grundy }
774ba069b8SMichael Grundy 
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  * Unlock any spinlocks which will prevent us from getting the
81cefc8be8SKirill Korotaev  * message out.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds void bust_spinlocks(int yes)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	if (yes) {
861da177e4SLinus Torvalds 		oops_in_progress = 1;
871da177e4SLinus Torvalds 	} else {
881da177e4SLinus Torvalds 		int loglevel_save = console_loglevel;
891da177e4SLinus Torvalds 		console_unblank();
901da177e4SLinus Torvalds 		oops_in_progress = 0;
911da177e4SLinus Torvalds 		/*
921da177e4SLinus Torvalds 		 * OK, the message is on the console.  Now we call printk()
931da177e4SLinus Torvalds 		 * without oops_in_progress set so that printk will give klogd
941da177e4SLinus Torvalds 		 * a poke.  Hold onto your hats...
951da177e4SLinus Torvalds 		 */
961da177e4SLinus Torvalds 		console_loglevel = 15;
971da177e4SLinus Torvalds 		printk(" ");
981da177e4SLinus Torvalds 		console_loglevel = loglevel_save;
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
103482b05ddSGerald Schaefer  * Returns the address space associated with the fault.
10461365e13SMartin Schwidefsky  * Returns 0 for kernel space and 1 for user space.
1051da177e4SLinus Torvalds  */
10661365e13SMartin Schwidefsky static inline int user_space_fault(unsigned long trans_exc_code)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	/*
10961365e13SMartin Schwidefsky 	 * The lowest two bits of the translation exception
11061365e13SMartin Schwidefsky 	 * identification indicate which paging table was used.
1111da177e4SLinus Torvalds 	 */
11261365e13SMartin Schwidefsky 	trans_exc_code &= 3;
11361365e13SMartin Schwidefsky 	if (trans_exc_code == 2)
11461365e13SMartin Schwidefsky 		/* Access via secondary space, set_fs setting decides */
11561365e13SMartin Schwidefsky 		return current->thread.mm_segment.ar4;
116b11b5334SMartin Schwidefsky 	if (user_mode == HOME_SPACE_MODE)
11761365e13SMartin Schwidefsky 		/* User space if the access has been done via home space. */
11861365e13SMartin Schwidefsky 		return trans_exc_code == 3;
119482b05ddSGerald Schaefer 	/*
12061365e13SMartin Schwidefsky 	 * If the user space is not the home space the kernel runs in home
12161365e13SMartin Schwidefsky 	 * space. Access via secondary space has already been covered,
12261365e13SMartin Schwidefsky 	 * access via primary space or access register is from user space
12361365e13SMartin Schwidefsky 	 * and access via home space is from the kernel.
124482b05ddSGerald Schaefer 	 */
12561365e13SMartin Schwidefsky 	return trans_exc_code != 3;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128*aa33c8cbSMartin Schwidefsky static inline void report_user_fault(struct pt_regs *regs, long signr)
129ab3c68eeSHeiko Carstens {
130ab3c68eeSHeiko Carstens 	if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
131ab3c68eeSHeiko Carstens 		return;
132ab3c68eeSHeiko Carstens 	if (!unhandled_signal(current, signr))
133ab3c68eeSHeiko Carstens 		return;
134ab3c68eeSHeiko Carstens 	if (!printk_ratelimit())
135ab3c68eeSHeiko Carstens 		return;
136*aa33c8cbSMartin Schwidefsky 	printk(KERN_ALERT "User process fault: interruption code 0x%X ",
137*aa33c8cbSMartin Schwidefsky 	       regs->int_code);
138ab3c68eeSHeiko Carstens 	print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN);
139*aa33c8cbSMartin Schwidefsky 	printk(KERN_CONT "\n");
140*aa33c8cbSMartin Schwidefsky 	printk(KERN_ALERT "failing address: %lX\n",
141*aa33c8cbSMartin Schwidefsky 	       regs->int_parm_long & __FAIL_ADDR_MASK);
142ab3c68eeSHeiko Carstens 	show_regs(regs);
143ab3c68eeSHeiko Carstens }
144ab3c68eeSHeiko Carstens 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds  * Send SIGSEGV to task.  This is an external routine
1471da177e4SLinus Torvalds  * to keep the stack usage of do_page_fault small.
1481da177e4SLinus Torvalds  */
149*aa33c8cbSMartin Schwidefsky static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct siginfo si;
1521da177e4SLinus Torvalds 
153*aa33c8cbSMartin Schwidefsky 	report_user_fault(regs, SIGSEGV);
1541da177e4SLinus Torvalds 	si.si_signo = SIGSEGV;
1551da177e4SLinus Torvalds 	si.si_code = si_code;
156*aa33c8cbSMartin Schwidefsky 	si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
1571da177e4SLinus Torvalds 	force_sig_info(SIGSEGV, &si, current);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160*aa33c8cbSMartin Schwidefsky static noinline void do_no_context(struct pt_regs *regs)
16110c1031fSMartin Schwidefsky {
16210c1031fSMartin Schwidefsky 	const struct exception_table_entry *fixup;
16361365e13SMartin Schwidefsky 	unsigned long address;
16410c1031fSMartin Schwidefsky 
16510c1031fSMartin Schwidefsky 	/* Are we prepared to handle this kernel fault?  */
16650d7280dSMartin Schwidefsky 	fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
16710c1031fSMartin Schwidefsky 	if (fixup) {
16810c1031fSMartin Schwidefsky 		regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
16910c1031fSMartin Schwidefsky 		return;
17010c1031fSMartin Schwidefsky 	}
17110c1031fSMartin Schwidefsky 
17210c1031fSMartin Schwidefsky 	/*
17310c1031fSMartin Schwidefsky 	 * Oops. The kernel tried to access some bad page. We'll have to
17410c1031fSMartin Schwidefsky 	 * terminate things with extreme prejudice.
17510c1031fSMartin Schwidefsky 	 */
176*aa33c8cbSMartin Schwidefsky 	address = regs->int_parm_long & __FAIL_ADDR_MASK;
177*aa33c8cbSMartin Schwidefsky 	if (!user_space_fault(regs->int_parm_long))
17810c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
17910c1031fSMartin Schwidefsky 		       " at virtual kernel address %p\n", (void *)address);
18010c1031fSMartin Schwidefsky 	else
18110c1031fSMartin Schwidefsky 		printk(KERN_ALERT "Unable to handle kernel paging request"
18210c1031fSMartin Schwidefsky 		       " at virtual user address %p\n", (void *)address);
18310c1031fSMartin Schwidefsky 
184*aa33c8cbSMartin Schwidefsky 	die(regs, "Oops");
18510c1031fSMartin Schwidefsky 	do_exit(SIGKILL);
18610c1031fSMartin Schwidefsky }
18710c1031fSMartin Schwidefsky 
188*aa33c8cbSMartin Schwidefsky static noinline void do_low_address(struct pt_regs *regs)
18910c1031fSMartin Schwidefsky {
19010c1031fSMartin Schwidefsky 	/* Low-address protection hit in kernel mode means
19110c1031fSMartin Schwidefsky 	   NULL pointer write access in kernel mode.  */
19210c1031fSMartin Schwidefsky 	if (regs->psw.mask & PSW_MASK_PSTATE) {
19310c1031fSMartin Schwidefsky 		/* Low-address protection hit in user mode 'cannot happen'. */
194*aa33c8cbSMartin Schwidefsky 		die (regs, "Low-address protection");
19510c1031fSMartin Schwidefsky 		do_exit(SIGKILL);
19610c1031fSMartin Schwidefsky 	}
19710c1031fSMartin Schwidefsky 
198*aa33c8cbSMartin Schwidefsky 	do_no_context(regs);
19910c1031fSMartin Schwidefsky }
20010c1031fSMartin Schwidefsky 
201*aa33c8cbSMartin Schwidefsky static noinline void do_sigbus(struct pt_regs *regs)
20210c1031fSMartin Schwidefsky {
20310c1031fSMartin Schwidefsky 	struct task_struct *tsk = current;
20436bf9680SMartin Schwidefsky 	struct siginfo si;
20510c1031fSMartin Schwidefsky 
20610c1031fSMartin Schwidefsky 	/*
20710c1031fSMartin Schwidefsky 	 * Send a sigbus, regardless of whether we were in kernel
20810c1031fSMartin Schwidefsky 	 * or user mode.
20910c1031fSMartin Schwidefsky 	 */
21036bf9680SMartin Schwidefsky 	si.si_signo = SIGBUS;
21136bf9680SMartin Schwidefsky 	si.si_errno = 0;
21236bf9680SMartin Schwidefsky 	si.si_code = BUS_ADRERR;
213*aa33c8cbSMartin Schwidefsky 	si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
21436bf9680SMartin Schwidefsky 	force_sig_info(SIGBUS, &si, tsk);
21510c1031fSMartin Schwidefsky }
21610c1031fSMartin Schwidefsky 
217*aa33c8cbSMartin Schwidefsky static noinline void do_fault_error(struct pt_regs *regs, int fault)
21850d7280dSMartin Schwidefsky {
21950d7280dSMartin Schwidefsky 	int si_code;
22050d7280dSMartin Schwidefsky 
22150d7280dSMartin Schwidefsky 	switch (fault) {
22250d7280dSMartin Schwidefsky 	case VM_FAULT_BADACCESS:
22350d7280dSMartin Schwidefsky 	case VM_FAULT_BADMAP:
22450d7280dSMartin Schwidefsky 		/* Bad memory access. Check if it is kernel or user space. */
22550d7280dSMartin Schwidefsky 		if (regs->psw.mask & PSW_MASK_PSTATE) {
22650d7280dSMartin Schwidefsky 			/* User mode accesses just cause a SIGSEGV */
22750d7280dSMartin Schwidefsky 			si_code = (fault == VM_FAULT_BADMAP) ?
22850d7280dSMartin Schwidefsky 				SEGV_MAPERR : SEGV_ACCERR;
229*aa33c8cbSMartin Schwidefsky 			do_sigsegv(regs, si_code);
23050d7280dSMartin Schwidefsky 			return;
23150d7280dSMartin Schwidefsky 		}
23250d7280dSMartin Schwidefsky 	case VM_FAULT_BADCONTEXT:
233*aa33c8cbSMartin Schwidefsky 		do_no_context(regs);
23450d7280dSMartin Schwidefsky 		break;
23550d7280dSMartin Schwidefsky 	default: /* fault & VM_FAULT_ERROR */
23699583181SHeiko Carstens 		if (fault & VM_FAULT_OOM) {
23799583181SHeiko Carstens 			if (!(regs->psw.mask & PSW_MASK_PSTATE))
238*aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
23999583181SHeiko Carstens 			else
24050d7280dSMartin Schwidefsky 				pagefault_out_of_memory();
24199583181SHeiko Carstens 		} else if (fault & VM_FAULT_SIGBUS) {
24250d7280dSMartin Schwidefsky 			/* Kernel mode? Handle exceptions or die */
24350d7280dSMartin Schwidefsky 			if (!(regs->psw.mask & PSW_MASK_PSTATE))
244*aa33c8cbSMartin Schwidefsky 				do_no_context(regs);
24536bf9680SMartin Schwidefsky 			else
246*aa33c8cbSMartin Schwidefsky 				do_sigbus(regs);
24750d7280dSMartin Schwidefsky 		} else
24850d7280dSMartin Schwidefsky 			BUG();
24950d7280dSMartin Schwidefsky 		break;
25050d7280dSMartin Schwidefsky 	}
25150d7280dSMartin Schwidefsky }
25250d7280dSMartin Schwidefsky 
2531da177e4SLinus Torvalds /*
2541da177e4SLinus Torvalds  * This routine handles page faults.  It determines the address,
2551da177e4SLinus Torvalds  * and the problem, and then passes it off to one of the appropriate
2561da177e4SLinus Torvalds  * routines.
2571da177e4SLinus Torvalds  *
25850d7280dSMartin Schwidefsky  * interruption code (int_code):
2591da177e4SLinus Torvalds  *   04       Protection           ->  Write-Protection  (suprression)
2601da177e4SLinus Torvalds  *   10       Segment translation  ->  Not present       (nullification)
2611da177e4SLinus Torvalds  *   11       Page translation     ->  Not present       (nullification)
2621da177e4SLinus Torvalds  *   3b       Region third trans.  ->  Not present       (nullification)
2631da177e4SLinus Torvalds  */
264*aa33c8cbSMartin Schwidefsky static inline int do_exception(struct pt_regs *regs, int access)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	struct task_struct *tsk;
2671da177e4SLinus Torvalds 	struct mm_struct *mm;
2681da177e4SLinus Torvalds 	struct vm_area_struct *vma;
269*aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
2701da177e4SLinus Torvalds 	unsigned long address;
27133ce6140SHeiko Carstens 	unsigned int flags;
27233ce6140SHeiko Carstens 	int fault;
27310c1031fSMartin Schwidefsky 
2747ecb344aSMartin Schwidefsky 	if (notify_page_fault(regs))
27550d7280dSMartin Schwidefsky 		return 0;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	tsk = current;
2781da177e4SLinus Torvalds 	mm = tsk->mm;
279*aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	/*
2821da177e4SLinus Torvalds 	 * Verify that the fault happened in user space, that
2831da177e4SLinus Torvalds 	 * we are not in an interrupt and that there is a
2841da177e4SLinus Torvalds 	 * user context.
2851da177e4SLinus Torvalds 	 */
28650d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADCONTEXT;
28761365e13SMartin Schwidefsky 	if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
28850d7280dSMartin Schwidefsky 		goto out;
2891da177e4SLinus Torvalds 
29061365e13SMartin Schwidefsky 	address = trans_exc_code & __FAIL_ADDR_MASK;
291a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
29233ce6140SHeiko Carstens 	flags = FAULT_FLAG_ALLOW_RETRY;
29333ce6140SHeiko Carstens 	if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
29433ce6140SHeiko Carstens 		flags |= FAULT_FLAG_WRITE;
2951da177e4SLinus Torvalds 	down_read(&mm->mmap_sem);
2961da177e4SLinus Torvalds 
297e5992f2eSMartin Schwidefsky #ifdef CONFIG_PGSTE
298e5992f2eSMartin Schwidefsky 	if (test_tsk_thread_flag(current, TIF_SIE) && S390_lowcore.gmap) {
299499069e1SCarsten Otte 		address = __gmap_fault(address,
300e5992f2eSMartin Schwidefsky 				     (struct gmap *) S390_lowcore.gmap);
301e5992f2eSMartin Schwidefsky 		if (address == -EFAULT) {
302e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_BADMAP;
303e5992f2eSMartin Schwidefsky 			goto out_up;
304e5992f2eSMartin Schwidefsky 		}
305e5992f2eSMartin Schwidefsky 		if (address == -ENOMEM) {
306e5992f2eSMartin Schwidefsky 			fault = VM_FAULT_OOM;
307e5992f2eSMartin Schwidefsky 			goto out_up;
308e5992f2eSMartin Schwidefsky 		}
309e5992f2eSMartin Schwidefsky 	}
310e5992f2eSMartin Schwidefsky #endif
311e5992f2eSMartin Schwidefsky 
312e5992f2eSMartin Schwidefsky retry:
31350d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADMAP;
3141da177e4SLinus Torvalds 	vma = find_vma(mm, address);
3151da177e4SLinus Torvalds 	if (!vma)
31650d7280dSMartin Schwidefsky 		goto out_up;
317c1821c2eSGerald Schaefer 
31850d7280dSMartin Schwidefsky 	if (unlikely(vma->vm_start > address)) {
3191da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
32050d7280dSMartin Schwidefsky 			goto out_up;
3211da177e4SLinus Torvalds 		if (expand_stack(vma, address))
32250d7280dSMartin Schwidefsky 			goto out_up;
32350d7280dSMartin Schwidefsky 	}
32450d7280dSMartin Schwidefsky 
3251da177e4SLinus Torvalds 	/*
3261da177e4SLinus Torvalds 	 * Ok, we have a good vm_area for this memory access, so
3271da177e4SLinus Torvalds 	 * we can handle it..
3281da177e4SLinus Torvalds 	 */
32950d7280dSMartin Schwidefsky 	fault = VM_FAULT_BADACCESS;
3301ab947deSMartin Schwidefsky 	if (unlikely(!(vma->vm_flags & access)))
33150d7280dSMartin Schwidefsky 		goto out_up;
3321da177e4SLinus Torvalds 
33353492b1dSGerald Schaefer 	if (is_vm_hugetlb_page(vma))
33453492b1dSGerald Schaefer 		address &= HPAGE_MASK;
3351da177e4SLinus Torvalds 	/*
3361da177e4SLinus Torvalds 	 * If for any reason at all we couldn't handle the fault,
3371da177e4SLinus Torvalds 	 * make sure we exit gracefully rather than endlessly redo
3381da177e4SLinus Torvalds 	 * the fault.
3391da177e4SLinus Torvalds 	 */
34033ce6140SHeiko Carstens 	fault = handle_mm_fault(mm, vma, address, flags);
34150d7280dSMartin Schwidefsky 	if (unlikely(fault & VM_FAULT_ERROR))
34250d7280dSMartin Schwidefsky 		goto out_up;
34350d7280dSMartin Schwidefsky 
34433ce6140SHeiko Carstens 	/*
34533ce6140SHeiko Carstens 	 * Major/minor page fault accounting is only done on the
34633ce6140SHeiko Carstens 	 * initial attempt. If we go through a retry, it is extremely
34733ce6140SHeiko Carstens 	 * likely that the page will be found in page cache at that point.
34833ce6140SHeiko Carstens 	 */
34933ce6140SHeiko Carstens 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
350bde69af2SHeiko Carstens 		if (fault & VM_FAULT_MAJOR) {
35183c54070SNick Piggin 			tsk->maj_flt++;
352a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
353bde69af2SHeiko Carstens 				      regs, address);
354bde69af2SHeiko Carstens 		} else {
35583c54070SNick Piggin 			tsk->min_flt++;
356a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
357bde69af2SHeiko Carstens 				      regs, address);
358bde69af2SHeiko Carstens 		}
35933ce6140SHeiko Carstens 		if (fault & VM_FAULT_RETRY) {
36033ce6140SHeiko Carstens 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
36133ce6140SHeiko Carstens 			 * of starvation. */
36233ce6140SHeiko Carstens 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
363e5992f2eSMartin Schwidefsky 			down_read(&mm->mmap_sem);
36433ce6140SHeiko Carstens 			goto retry;
36533ce6140SHeiko Carstens 		}
36633ce6140SHeiko Carstens 	}
3671da177e4SLinus Torvalds 	/*
3681da177e4SLinus Torvalds 	 * The instruction that caused the program check will
3691da177e4SLinus Torvalds 	 * be repeated. Don't signal single step via SIGTRAP.
3701da177e4SLinus Torvalds 	 */
3715e9a2692SMartin Schwidefsky 	clear_tsk_thread_flag(tsk, TIF_PER_TRAP);
37250d7280dSMartin Schwidefsky 	fault = 0;
37350d7280dSMartin Schwidefsky out_up:
3741da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
37550d7280dSMartin Schwidefsky out:
37650d7280dSMartin Schwidefsky 	return fault;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
379*aa33c8cbSMartin Schwidefsky void __kprobes do_protection_exception(struct pt_regs *regs)
3801da177e4SLinus Torvalds {
381*aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
38250d7280dSMartin Schwidefsky 	int fault;
38361365e13SMartin Schwidefsky 
384*aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
38525985edcSLucas De Marchi 	/* Protection exception is suppressing, decrement psw address. */
386*aa33c8cbSMartin Schwidefsky 	regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
38710c1031fSMartin Schwidefsky 	/*
38810c1031fSMartin Schwidefsky 	 * Check for low-address protection.  This needs to be treated
38910c1031fSMartin Schwidefsky 	 * as a special case because the translation exception code
39010c1031fSMartin Schwidefsky 	 * field is not guaranteed to contain valid data in this case.
39110c1031fSMartin Schwidefsky 	 */
39261365e13SMartin Schwidefsky 	if (unlikely(!(trans_exc_code & 4))) {
393*aa33c8cbSMartin Schwidefsky 		do_low_address(regs);
39410c1031fSMartin Schwidefsky 		return;
39510c1031fSMartin Schwidefsky 	}
396*aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, VM_WRITE);
39750d7280dSMartin Schwidefsky 	if (unlikely(fault))
398*aa33c8cbSMartin Schwidefsky 		do_fault_error(regs, fault);
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
401*aa33c8cbSMartin Schwidefsky void __kprobes do_dat_exception(struct pt_regs *regs)
4021da177e4SLinus Torvalds {
4031ab947deSMartin Schwidefsky 	int access, fault;
40450d7280dSMartin Schwidefsky 
4051ab947deSMartin Schwidefsky 	access = VM_READ | VM_EXEC | VM_WRITE;
406*aa33c8cbSMartin Schwidefsky 	fault = do_exception(regs, access);
40750d7280dSMartin Schwidefsky 	if (unlikely(fault))
408*aa33c8cbSMartin Schwidefsky 		do_fault_error(regs, fault);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4116252d702SMartin Schwidefsky #ifdef CONFIG_64BIT
412*aa33c8cbSMartin Schwidefsky void __kprobes do_asce_exception(struct pt_regs *regs)
4136252d702SMartin Schwidefsky {
41450d7280dSMartin Schwidefsky 	struct mm_struct *mm = current->mm;
4156252d702SMartin Schwidefsky 	struct vm_area_struct *vma;
416*aa33c8cbSMartin Schwidefsky 	unsigned long trans_exc_code;
4176252d702SMartin Schwidefsky 
418*aa33c8cbSMartin Schwidefsky 	trans_exc_code = regs->int_parm_long;
41961365e13SMartin Schwidefsky 	if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
4206252d702SMartin Schwidefsky 		goto no_context;
4216252d702SMartin Schwidefsky 
4226252d702SMartin Schwidefsky 	down_read(&mm->mmap_sem);
42350d7280dSMartin Schwidefsky 	vma = find_vma(mm, trans_exc_code & __FAIL_ADDR_MASK);
4246252d702SMartin Schwidefsky 	up_read(&mm->mmap_sem);
4256252d702SMartin Schwidefsky 
4266252d702SMartin Schwidefsky 	if (vma) {
4276252d702SMartin Schwidefsky 		update_mm(mm, current);
4286252d702SMartin Schwidefsky 		return;
4296252d702SMartin Schwidefsky 	}
4306252d702SMartin Schwidefsky 
4316252d702SMartin Schwidefsky 	/* User mode accesses just cause a SIGSEGV */
4326252d702SMartin Schwidefsky 	if (regs->psw.mask & PSW_MASK_PSTATE) {
433*aa33c8cbSMartin Schwidefsky 		do_sigsegv(regs, SEGV_MAPERR);
4346252d702SMartin Schwidefsky 		return;
4356252d702SMartin Schwidefsky 	}
4366252d702SMartin Schwidefsky 
4376252d702SMartin Schwidefsky no_context:
438*aa33c8cbSMartin Schwidefsky 	do_no_context(regs);
4396252d702SMartin Schwidefsky }
4406252d702SMartin Schwidefsky #endif
4416252d702SMartin Schwidefsky 
4421e54622eSMartin Schwidefsky int __handle_fault(unsigned long uaddr, unsigned long pgm_int_code, int write)
4436c1e3e79SGerald Schaefer {
4446c1e3e79SGerald Schaefer 	struct pt_regs regs;
4456c1e3e79SGerald Schaefer 	int access, fault;
4466c1e3e79SGerald Schaefer 
447b50511e4SMartin Schwidefsky 	regs.psw.mask = psw_kernel_bits | PSW_MASK_DAT | PSW_MASK_MCHECK;
4486c1e3e79SGerald Schaefer 	if (!irqs_disabled())
4496c1e3e79SGerald Schaefer 		regs.psw.mask |= PSW_MASK_IO | PSW_MASK_EXT;
4506c1e3e79SGerald Schaefer 	regs.psw.addr = (unsigned long) __builtin_return_address(0);
4516c1e3e79SGerald Schaefer 	regs.psw.addr |= PSW_ADDR_AMODE;
452*aa33c8cbSMartin Schwidefsky 	regs.int_code = pgm_int_code;
453*aa33c8cbSMartin Schwidefsky 	regs.int_parm_long = (uaddr & PAGE_MASK) | 2;
4541e54622eSMartin Schwidefsky 	access = write ? VM_WRITE : VM_READ;
455*aa33c8cbSMartin Schwidefsky 	fault = do_exception(&regs, access);
4566c1e3e79SGerald Schaefer 	if (unlikely(fault)) {
45799583181SHeiko Carstens 		if (fault & VM_FAULT_OOM)
45899583181SHeiko Carstens 			return -EFAULT;
45999583181SHeiko Carstens 		else if (fault & VM_FAULT_SIGBUS)
460*aa33c8cbSMartin Schwidefsky 			do_sigbus(&regs);
4616c1e3e79SGerald Schaefer 	}
4626c1e3e79SGerald Schaefer 	return fault ? -EFAULT : 0;
4636c1e3e79SGerald Schaefer }
4646c1e3e79SGerald Schaefer 
4651da177e4SLinus Torvalds #ifdef CONFIG_PFAULT
4661da177e4SLinus Torvalds /*
4671da177e4SLinus Torvalds  * 'pfault' pseudo page faults routines.
4681da177e4SLinus Torvalds  */
469fb0a9d7eSHeiko Carstens static int pfault_disable;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds static int __init nopfault(char *str)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	pfault_disable = 1;
4741da177e4SLinus Torvalds 	return 1;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds __setup("nopfault", nopfault);
4781da177e4SLinus Torvalds 
4797dd8fe1fSHeiko Carstens struct pfault_refbk {
4807dd8fe1fSHeiko Carstens 	u16 refdiagc;
4817dd8fe1fSHeiko Carstens 	u16 reffcode;
4827dd8fe1fSHeiko Carstens 	u16 refdwlen;
4837dd8fe1fSHeiko Carstens 	u16 refversn;
4847dd8fe1fSHeiko Carstens 	u64 refgaddr;
4857dd8fe1fSHeiko Carstens 	u64 refselmk;
4867dd8fe1fSHeiko Carstens 	u64 refcmpmk;
4877dd8fe1fSHeiko Carstens 	u64 reserved;
4887dd8fe1fSHeiko Carstens } __attribute__ ((packed, aligned(8)));
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds int pfault_init(void)
4911da177e4SLinus Torvalds {
4927dd8fe1fSHeiko Carstens 	struct pfault_refbk refbk = {
4937dd8fe1fSHeiko Carstens 		.refdiagc = 0x258,
4947dd8fe1fSHeiko Carstens 		.reffcode = 0,
4957dd8fe1fSHeiko Carstens 		.refdwlen = 5,
4967dd8fe1fSHeiko Carstens 		.refversn = 2,
4977dd8fe1fSHeiko Carstens 		.refgaddr = __LC_CURRENT_PID,
4987dd8fe1fSHeiko Carstens 		.refselmk = 1ULL << 48,
4997dd8fe1fSHeiko Carstens 		.refcmpmk = 1ULL << 48,
5007dd8fe1fSHeiko Carstens 		.reserved = __PF_RES_FIELD };
5011da177e4SLinus Torvalds         int rc;
5021da177e4SLinus Torvalds 
503f32269a0SCarsten Otte 	if (pfault_disable)
5041da177e4SLinus Torvalds 		return -1;
50594c12cc7SMartin Schwidefsky 	asm volatile(
5061da177e4SLinus Torvalds 		"	diag	%1,%0,0x258\n"
5071da177e4SLinus Torvalds 		"0:	j	2f\n"
5081da177e4SLinus Torvalds 		"1:	la	%0,8\n"
5091da177e4SLinus Torvalds 		"2:\n"
51094c12cc7SMartin Schwidefsky 		EX_TABLE(0b,1b)
511d4b68996SMartin Schwidefsky 		: "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
5121da177e4SLinus Torvalds         return rc;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds void pfault_fini(void)
5161da177e4SLinus Torvalds {
5177dd8fe1fSHeiko Carstens 	struct pfault_refbk refbk = {
5187dd8fe1fSHeiko Carstens 		.refdiagc = 0x258,
5197dd8fe1fSHeiko Carstens 		.reffcode = 1,
5207dd8fe1fSHeiko Carstens 		.refdwlen = 5,
5217dd8fe1fSHeiko Carstens 		.refversn = 2,
5227dd8fe1fSHeiko Carstens 	};
5231da177e4SLinus Torvalds 
524f32269a0SCarsten Otte 	if (pfault_disable)
5251da177e4SLinus Torvalds 		return;
52694c12cc7SMartin Schwidefsky 	asm volatile(
5271da177e4SLinus Torvalds 		"	diag	%0,0,0x258\n"
5281da177e4SLinus Torvalds 		"0:\n"
52994c12cc7SMartin Schwidefsky 		EX_TABLE(0b,0b)
530d4b68996SMartin Schwidefsky 		: : "a" (&refbk), "m" (refbk) : "cc");
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
533f2db2e6cSHeiko Carstens static DEFINE_SPINLOCK(pfault_lock);
534f2db2e6cSHeiko Carstens static LIST_HEAD(pfault_list);
535f2db2e6cSHeiko Carstens 
536f6649a7eSMartin Schwidefsky static void pfault_interrupt(unsigned int ext_int_code,
537f6649a7eSMartin Schwidefsky 			     unsigned int param32, unsigned long param64)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	struct task_struct *tsk;
5401da177e4SLinus Torvalds 	__u16 subcode;
541f2db2e6cSHeiko Carstens 	pid_t pid;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	/*
5441da177e4SLinus Torvalds 	 * Get the external interruption subcode & pfault
5451da177e4SLinus Torvalds 	 * initial/completion signal bit. VM stores this
5461da177e4SLinus Torvalds 	 * in the 'cpu address' field associated with the
5471da177e4SLinus Torvalds          * external interrupt.
5481da177e4SLinus Torvalds 	 */
549f6649a7eSMartin Schwidefsky 	subcode = ext_int_code >> 16;
5501da177e4SLinus Torvalds 	if ((subcode & 0xff00) != __SUBCODE_MASK)
5511da177e4SLinus Torvalds 		return;
552a9851832SHeiko Carstens 	kstat_cpu(smp_processor_id()).irqs[EXTINT_PFL]++;
553f2db2e6cSHeiko Carstens 	if (subcode & 0x0080) {
554f2db2e6cSHeiko Carstens 		/* Get the token (= pid of the affected task). */
555f2db2e6cSHeiko Carstens 		pid = sizeof(void *) == 4 ? param32 : param64;
556f2db2e6cSHeiko Carstens 		rcu_read_lock();
557f2db2e6cSHeiko Carstens 		tsk = find_task_by_pid_ns(pid, &init_pid_ns);
558f2db2e6cSHeiko Carstens 		if (tsk)
559f2db2e6cSHeiko Carstens 			get_task_struct(tsk);
560f2db2e6cSHeiko Carstens 		rcu_read_unlock();
561f2db2e6cSHeiko Carstens 		if (!tsk)
562f2db2e6cSHeiko Carstens 			return;
563f2db2e6cSHeiko Carstens 	} else {
564f2db2e6cSHeiko Carstens 		tsk = current;
565f2db2e6cSHeiko Carstens 	}
566f2db2e6cSHeiko Carstens 	spin_lock(&pfault_lock);
5671da177e4SLinus Torvalds 	if (subcode & 0x0080) {
5681da177e4SLinus Torvalds 		/* signal bit is set -> a page has been swapped in by VM */
569f2db2e6cSHeiko Carstens 		if (tsk->thread.pfault_wait == 1) {
5701da177e4SLinus Torvalds 			/* Initial interrupt was faster than the completion
5711da177e4SLinus Torvalds 			 * interrupt. pfault_wait is valid. Set pfault_wait
5721da177e4SLinus Torvalds 			 * back to zero and wake up the process. This can
5731da177e4SLinus Torvalds 			 * safely be done because the task is still sleeping
574b6d09449SMartin Schwidefsky 			 * and can't produce new pfaults. */
5751da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
576f2db2e6cSHeiko Carstens 			list_del(&tsk->thread.list);
5771da177e4SLinus Torvalds 			wake_up_process(tsk);
578f2db2e6cSHeiko Carstens 		} else {
579f2db2e6cSHeiko Carstens 			/* Completion interrupt was faster than initial
580f2db2e6cSHeiko Carstens 			 * interrupt. Set pfault_wait to -1 so the initial
581fa2fb2f4SHeiko Carstens 			 * interrupt doesn't put the task to sleep.
582fa2fb2f4SHeiko Carstens 			 * If the task is not running, ignore the completion
583fa2fb2f4SHeiko Carstens 			 * interrupt since it must be a leftover of a PFAULT
584fa2fb2f4SHeiko Carstens 			 * CANCEL operation which didn't remove all pending
585fa2fb2f4SHeiko Carstens 			 * completion interrupts. */
586fa2fb2f4SHeiko Carstens 			if (tsk->state == TASK_RUNNING)
587f2db2e6cSHeiko Carstens 				tsk->thread.pfault_wait = -1;
5881da177e4SLinus Torvalds 		}
589f2db2e6cSHeiko Carstens 		put_task_struct(tsk);
5901da177e4SLinus Torvalds 	} else {
5911da177e4SLinus Torvalds 		/* signal bit not set -> a real page is missing. */
592f2db2e6cSHeiko Carstens 		if (tsk->thread.pfault_wait == -1) {
5931da177e4SLinus Torvalds 			/* Completion interrupt was faster than the initial
594f2db2e6cSHeiko Carstens 			 * interrupt (pfault_wait == -1). Set pfault_wait
595f2db2e6cSHeiko Carstens 			 * back to zero and exit. */
5961da177e4SLinus Torvalds 			tsk->thread.pfault_wait = 0;
597f2db2e6cSHeiko Carstens 		} else {
598f2db2e6cSHeiko Carstens 			/* Initial interrupt arrived before completion
599f2db2e6cSHeiko Carstens 			 * interrupt. Let the task sleep. */
600f2db2e6cSHeiko Carstens 			tsk->thread.pfault_wait = 1;
601f2db2e6cSHeiko Carstens 			list_add(&tsk->thread.list, &pfault_list);
602f2db2e6cSHeiko Carstens 			set_task_state(tsk, TASK_UNINTERRUPTIBLE);
6031da177e4SLinus Torvalds 			set_tsk_need_resched(tsk);
6041da177e4SLinus Torvalds 		}
6051da177e4SLinus Torvalds 	}
606f2db2e6cSHeiko Carstens 	spin_unlock(&pfault_lock);
607f2db2e6cSHeiko Carstens }
608f2db2e6cSHeiko Carstens 
609f2db2e6cSHeiko Carstens static int __cpuinit pfault_cpu_notify(struct notifier_block *self,
610f2db2e6cSHeiko Carstens 				       unsigned long action, void *hcpu)
611f2db2e6cSHeiko Carstens {
612f2db2e6cSHeiko Carstens 	struct thread_struct *thread, *next;
613f2db2e6cSHeiko Carstens 	struct task_struct *tsk;
614f2db2e6cSHeiko Carstens 
615f2db2e6cSHeiko Carstens 	switch (action) {
616f2db2e6cSHeiko Carstens 	case CPU_DEAD:
617f2db2e6cSHeiko Carstens 	case CPU_DEAD_FROZEN:
618f2db2e6cSHeiko Carstens 		spin_lock_irq(&pfault_lock);
619f2db2e6cSHeiko Carstens 		list_for_each_entry_safe(thread, next, &pfault_list, list) {
620f2db2e6cSHeiko Carstens 			thread->pfault_wait = 0;
621f2db2e6cSHeiko Carstens 			list_del(&thread->list);
622f2db2e6cSHeiko Carstens 			tsk = container_of(thread, struct task_struct, thread);
623f2db2e6cSHeiko Carstens 			wake_up_process(tsk);
624f2db2e6cSHeiko Carstens 		}
625f2db2e6cSHeiko Carstens 		spin_unlock_irq(&pfault_lock);
626f2db2e6cSHeiko Carstens 		break;
627f2db2e6cSHeiko Carstens 	default:
628f2db2e6cSHeiko Carstens 		break;
629f2db2e6cSHeiko Carstens 	}
630f2db2e6cSHeiko Carstens 	return NOTIFY_OK;
631f2db2e6cSHeiko Carstens }
6321da177e4SLinus Torvalds 
633fb0a9d7eSHeiko Carstens static int __init pfault_irq_init(void)
63429b08d2bSHeiko Carstens {
635fb0a9d7eSHeiko Carstens 	int rc;
63629b08d2bSHeiko Carstens 
637fb0a9d7eSHeiko Carstens 	rc = register_external_interrupt(0x2603, pfault_interrupt);
6387dd8fe1fSHeiko Carstens 	if (rc)
6397dd8fe1fSHeiko Carstens 		goto out_extint;
6407dd8fe1fSHeiko Carstens 	rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
6417dd8fe1fSHeiko Carstens 	if (rc)
6427dd8fe1fSHeiko Carstens 		goto out_pfault;
643df7997abSHeiko Carstens 	service_subclass_irq_register();
6447dd8fe1fSHeiko Carstens 	hotcpu_notifier(pfault_cpu_notify, 0);
6457dd8fe1fSHeiko Carstens 	return 0;
6467dd8fe1fSHeiko Carstens 
6477dd8fe1fSHeiko Carstens out_pfault:
6487dd8fe1fSHeiko Carstens 	unregister_external_interrupt(0x2603, pfault_interrupt);
6497dd8fe1fSHeiko Carstens out_extint:
650fb0a9d7eSHeiko Carstens 	pfault_disable = 1;
651fb0a9d7eSHeiko Carstens 	return rc;
652fb0a9d7eSHeiko Carstens }
653fb0a9d7eSHeiko Carstens early_initcall(pfault_irq_init);
654fb0a9d7eSHeiko Carstens 
6557dd8fe1fSHeiko Carstens #endif /* CONFIG_PFAULT */
656