blob: ac9122ca11529217e3843483042306a7a09a74cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S390 version
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * Copyright IBM Corp. 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Author(s): Hartmut Penner (hp@de.ibm.com)
5 * Ulrich Weigand (uweigand@de.ibm.com)
6 *
7 * Derived from "arch/i386/mm/fault.c"
8 * Copyright (C) 1995 Linus Torvalds
9 */
10
Heiko Carstens052ff462011-01-05 12:47:28 +010011#include <linux/kernel_stat.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/signal.h>
14#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/string.h>
18#include <linux/types.h>
19#include <linux/ptrace.h>
20#include <linux/mman.h>
21#include <linux/mm.h>
Heiko Carstens77575912009-06-12 10:26:25 +020022#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/smp.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070024#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/module.h>
28#include <linux/hardirq.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020029#include <linux/kprobes.h>
Martin Schwidefskybe5ec362007-04-27 16:01:44 +020030#include <linux/uaccess.h>
Gerald Schaefer53492b12008-04-30 13:38:46 +020031#include <linux/hugetlb.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010032#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/pgtable.h>
Heiko Carstensd7b250e2011-05-26 09:48:24 +020034#include <asm/irq.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010035#include <asm/mmu_context.h>
David Howellsa0616cd2012-03-28 18:30:02 +010036#include <asm/facility.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020037#include "../kernel/entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080039#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define __FAIL_ADDR_MASK 0x7ffff000
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define __SUBCODE_MASK 0x0200
42#define __PF_RES_FIELD 0ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080043#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define __FAIL_ADDR_MASK -4096L
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define __SUBCODE_MASK 0x0600
46#define __PF_RES_FIELD 0x8000000000000000ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080047#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Martin Schwidefsky50d72802009-12-07 12:51:45 +010049#define VM_FAULT_BADCONTEXT 0x010000
50#define VM_FAULT_BADMAP 0x020000
51#define VM_FAULT_BADACCESS 0x040000
Heiko Carstensf2c76e32012-07-27 08:54:20 +020052#define VM_FAULT_SIGNAL 0x080000
Martin Schwidefsky50d72802009-12-07 12:51:45 +010053
Martin Schwidefsky92f842e2010-10-25 16:10:13 +020054static unsigned long store_indication;
55
56void fault_init(void)
57{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +020058 if (test_facility(2) && test_facility(75))
Martin Schwidefsky92f842e2010-10-25 16:10:13 +020059 store_indication = 0xc00;
60}
61
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +010062static inline int notify_page_fault(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +020063{
Christoph Hellwig33464e32007-05-04 18:47:46 +020064 int ret = 0;
65
66 /* kprobe_running() needs smp_processor_id() */
Heiko Carstens22e0a042010-02-26 22:37:45 +010067 if (kprobes_built_in() && !user_mode(regs)) {
Christoph Hellwig33464e32007-05-04 18:47:46 +020068 preempt_disable();
69 if (kprobe_running() && kprobe_fault_handler(regs, 14))
70 ret = 1;
71 preempt_enable();
72 }
Christoph Hellwig33464e32007-05-04 18:47:46 +020073 return ret;
Michael Grundy4ba069b2006-09-20 15:58:39 +020074}
Michael Grundy4ba069b2006-09-20 15:58:39 +020075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Unlock any spinlocks which will prevent us from getting the
Kirill Korotaevcefc8be2007-02-10 01:46:18 -080079 * message out.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81void bust_spinlocks(int yes)
82{
83 if (yes) {
84 oops_in_progress = 1;
85 } else {
86 int loglevel_save = console_loglevel;
87 console_unblank();
88 oops_in_progress = 0;
89 /*
90 * OK, the message is on the console. Now we call printk()
91 * without oops_in_progress set so that printk will give klogd
92 * a poke. Hold onto your hats...
93 */
94 console_loglevel = 15;
95 printk(" ");
96 console_loglevel = loglevel_save;
97 }
98}
99
100/*
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100101 * Returns the address space associated with the fault.
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100102 * Returns 0 for kernel space and 1 for user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100104static inline int user_space_fault(unsigned long trans_exc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 /*
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100107 * The lowest two bits of the translation exception
108 * identification indicate which paging table was used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100110 trans_exc_code &= 3;
111 if (trans_exc_code == 2)
112 /* Access via secondary space, set_fs setting decides */
113 return current->thread.mm_segment.ar4;
Heiko Carstensd1b0d842012-09-02 11:02:23 +0200114 if (s390_user_mode == HOME_SPACE_MODE)
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100115 /* User space if the access has been done via home space. */
116 return trans_exc_code == 3;
117 /*
118 * If the user space is not the home space the kernel runs in home
119 * space. Access via secondary space has already been covered,
120 * access via primary space or access register is from user space
121 * and access via home space is from the kernel.
122 */
123 return trans_exc_code != 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100126static inline void report_user_fault(struct pt_regs *regs, long signr)
Heiko Carstensab3c68e2010-05-17 10:00:21 +0200127{
128 if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
129 return;
130 if (!unhandled_signal(current, signr))
131 return;
132 if (!printk_ratelimit())
133 return;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100134 printk(KERN_ALERT "User process fault: interruption code 0x%X ",
135 regs->int_code);
Heiko Carstensab3c68e2010-05-17 10:00:21 +0200136 print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN);
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100137 printk(KERN_CONT "\n");
138 printk(KERN_ALERT "failing address: %lX\n",
139 regs->int_parm_long & __FAIL_ADDR_MASK);
Heiko Carstensab3c68e2010-05-17 10:00:21 +0200140 show_regs(regs);
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * Send SIGSEGV to task. This is an external routine
145 * to keep the stack usage of do_page_fault small.
146 */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100147static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct siginfo si;
150
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100151 report_user_fault(regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 si.si_signo = SIGSEGV;
153 si.si_code = si_code;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100154 si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 force_sig_info(SIGSEGV, &si, current);
156}
157
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100158static noinline void do_no_context(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200159{
160 const struct exception_table_entry *fixup;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100161 unsigned long address;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200162
163 /* Are we prepared to handle this kernel fault? */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100164 fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200165 if (fixup) {
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200166 regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200167 return;
168 }
169
170 /*
171 * Oops. The kernel tried to access some bad page. We'll have to
172 * terminate things with extreme prejudice.
173 */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100174 address = regs->int_parm_long & __FAIL_ADDR_MASK;
175 if (!user_space_fault(regs->int_parm_long))
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200176 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
177 " at virtual kernel address %p\n", (void *)address);
178 else
179 printk(KERN_ALERT "Unable to handle kernel paging request"
180 " at virtual user address %p\n", (void *)address);
181
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100182 die(regs, "Oops");
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200183 do_exit(SIGKILL);
184}
185
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100186static noinline void do_low_address(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200187{
188 /* Low-address protection hit in kernel mode means
189 NULL pointer write access in kernel mode. */
190 if (regs->psw.mask & PSW_MASK_PSTATE) {
191 /* Low-address protection hit in user mode 'cannot happen'. */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100192 die (regs, "Low-address protection");
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200193 do_exit(SIGKILL);
194 }
195
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100196 do_no_context(regs);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200197}
198
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100199static noinline void do_sigbus(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200200{
201 struct task_struct *tsk = current;
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200202 struct siginfo si;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200203
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200204 /*
205 * Send a sigbus, regardless of whether we were in kernel
206 * or user mode.
207 */
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200208 si.si_signo = SIGBUS;
209 si.si_errno = 0;
210 si.si_code = BUS_ADRERR;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100211 si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200212 force_sig_info(SIGBUS, &si, tsk);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200213}
214
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100215static noinline void do_fault_error(struct pt_regs *regs, int fault)
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100216{
217 int si_code;
218
219 switch (fault) {
220 case VM_FAULT_BADACCESS:
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100221 case VM_FAULT_BADMAP:
222 /* Bad memory access. Check if it is kernel or user space. */
Heiko Carstens7d256172012-07-27 10:31:12 +0200223 if (user_mode(regs)) {
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100224 /* User mode accesses just cause a SIGSEGV */
225 si_code = (fault == VM_FAULT_BADMAP) ?
226 SEGV_MAPERR : SEGV_ACCERR;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100227 do_sigsegv(regs, si_code);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100228 return;
229 }
230 case VM_FAULT_BADCONTEXT:
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100231 do_no_context(regs);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100232 break;
Heiko Carstensf2c76e32012-07-27 08:54:20 +0200233 case VM_FAULT_SIGNAL:
234 if (!user_mode(regs))
235 do_no_context(regs);
236 break;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100237 default: /* fault & VM_FAULT_ERROR */
Heiko Carstens99583182011-05-26 09:48:29 +0200238 if (fault & VM_FAULT_OOM) {
Heiko Carstens7d256172012-07-27 10:31:12 +0200239 if (!user_mode(regs))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100240 do_no_context(regs);
Heiko Carstens99583182011-05-26 09:48:29 +0200241 else
242 pagefault_out_of_memory();
243 } else if (fault & VM_FAULT_SIGBUS) {
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100244 /* Kernel mode? Handle exceptions or die */
Heiko Carstens7d256172012-07-27 10:31:12 +0200245 if (!user_mode(regs))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100246 do_no_context(regs);
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200247 else
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100248 do_sigbus(regs);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100249 } else
250 BUG();
251 break;
252 }
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * This routine handles page faults. It determines the address,
257 * and the problem, and then passes it off to one of the appropriate
258 * routines.
259 *
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100260 * interruption code (int_code):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * 04 Protection -> Write-Protection (suprression)
262 * 10 Segment translation -> Not present (nullification)
263 * 11 Page translation -> Not present (nullification)
264 * 3b Region third trans. -> Not present (nullification)
265 */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100266static inline int do_exception(struct pt_regs *regs, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200268 struct task_struct *tsk;
269 struct mm_struct *mm;
270 struct vm_area_struct *vma;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100271 unsigned long trans_exc_code;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200272 unsigned long address;
Heiko Carstens33ce6142011-05-26 09:48:30 +0200273 unsigned int flags;
274 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +0100276 if (notify_page_fault(regs))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100277 return 0;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200278
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200279 tsk = current;
280 mm = tsk->mm;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100281 trans_exc_code = regs->int_parm_long;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
284 * Verify that the fault happened in user space, that
285 * we are not in an interrupt and that there is a
286 * user context.
287 */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100288 fault = VM_FAULT_BADCONTEXT;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100289 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100292 address = trans_exc_code & __FAIL_ADDR_MASK;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200293 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Heiko Carstensf2c76e32012-07-27 08:54:20 +0200294 flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Heiko Carstens33ce6142011-05-26 09:48:30 +0200295 if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
296 flags |= FAULT_FLAG_WRITE;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200297 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200299#ifdef CONFIG_PGSTE
Martin Schwidefsky5e8010c2012-05-03 15:50:44 +0200300 if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
Carsten Otte499069e2011-10-30 15:17:02 +0100301 address = __gmap_fault(address,
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200302 (struct gmap *) S390_lowcore.gmap);
303 if (address == -EFAULT) {
304 fault = VM_FAULT_BADMAP;
305 goto out_up;
306 }
307 if (address == -ENOMEM) {
308 fault = VM_FAULT_OOM;
309 goto out_up;
310 }
311 }
312#endif
313
314retry:
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100315 fault = VM_FAULT_BADMAP;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100316 vma = find_vma(mm, address);
317 if (!vma)
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100318 goto out_up;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100319
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100320 if (unlikely(vma->vm_start > address)) {
321 if (!(vma->vm_flags & VM_GROWSDOWN))
322 goto out_up;
323 if (expand_stack(vma, address))
324 goto out_up;
325 }
326
327 /*
328 * Ok, we have a good vm_area for this memory access, so
329 * we can handle it..
330 */
331 fault = VM_FAULT_BADACCESS;
Martin Schwidefsky1ab947d2009-12-07 12:51:46 +0100332 if (unlikely(!(vma->vm_flags & access)))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100333 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Gerald Schaefer53492b12008-04-30 13:38:46 +0200335 if (is_vm_hugetlb_page(vma))
336 address &= HPAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /*
338 * If for any reason at all we couldn't handle the fault,
339 * make sure we exit gracefully rather than endlessly redo
340 * the fault.
341 */
Heiko Carstens33ce6142011-05-26 09:48:30 +0200342 fault = handle_mm_fault(mm, vma, address, flags);
Heiko Carstensf2c76e32012-07-27 08:54:20 +0200343 /* No reason to continue if interrupted by SIGKILL. */
344 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
345 fault = VM_FAULT_SIGNAL;
346 goto out;
347 }
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100348 if (unlikely(fault & VM_FAULT_ERROR))
349 goto out_up;
350
Heiko Carstens33ce6142011-05-26 09:48:30 +0200351 /*
352 * Major/minor page fault accounting is only done on the
353 * initial attempt. If we go through a retry, it is extremely
354 * likely that the page will be found in page cache at that point.
355 */
356 if (flags & FAULT_FLAG_ALLOW_RETRY) {
357 if (fault & VM_FAULT_MAJOR) {
358 tsk->maj_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200359 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
Heiko Carstens33ce6142011-05-26 09:48:30 +0200360 regs, address);
361 } else {
362 tsk->min_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200363 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
Heiko Carstens33ce6142011-05-26 09:48:30 +0200364 regs, address);
365 }
366 if (fault & VM_FAULT_RETRY) {
367 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
368 * of starvation. */
369 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200370 down_read(&mm->mmap_sem);
Heiko Carstens33ce6142011-05-26 09:48:30 +0200371 goto retry;
372 }
Heiko Carstensbde69af2009-09-11 10:29:06 +0200373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * The instruction that caused the program check will
376 * be repeated. Don't signal single step via SIGTRAP.
377 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100378 clear_tsk_thread_flag(tsk, TIF_PER_TRAP);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100379 fault = 0;
380out_up:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200381 up_read(&mm->mmap_sem);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100382out:
383 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100386void __kprobes do_protection_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100388 unsigned long trans_exc_code;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100389 int fault;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100390
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100391 trans_exc_code = regs->int_parm_long;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300392 /* Protection exception is suppressing, decrement psw address. */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100393 regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200394 /*
395 * Check for low-address protection. This needs to be treated
396 * as a special case because the translation exception code
397 * field is not guaranteed to contain valid data in this case.
398 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100399 if (unlikely(!(trans_exc_code & 4))) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100400 do_low_address(regs);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200401 return;
402 }
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100403 fault = do_exception(regs, VM_WRITE);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100404 if (unlikely(fault))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100405 do_fault_error(regs, fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100408void __kprobes do_dat_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Martin Schwidefsky1ab947d2009-12-07 12:51:46 +0100410 int access, fault;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100411
Martin Schwidefsky1ab947d2009-12-07 12:51:46 +0100412 access = VM_READ | VM_EXEC | VM_WRITE;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100413 fault = do_exception(regs, access);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100414 if (unlikely(fault))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100415 do_fault_error(regs, fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100418#ifdef CONFIG_64BIT
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100419void __kprobes do_asce_exception(struct pt_regs *regs)
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100420{
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100421 struct mm_struct *mm = current->mm;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100422 struct vm_area_struct *vma;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100423 unsigned long trans_exc_code;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100424
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100425 trans_exc_code = regs->int_parm_long;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100426 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100427 goto no_context;
428
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100429 down_read(&mm->mmap_sem);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100430 vma = find_vma(mm, trans_exc_code & __FAIL_ADDR_MASK);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100431 up_read(&mm->mmap_sem);
432
433 if (vma) {
434 update_mm(mm, current);
435 return;
436 }
437
438 /* User mode accesses just cause a SIGSEGV */
Heiko Carstens7d256172012-07-27 10:31:12 +0200439 if (user_mode(regs)) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100440 do_sigsegv(regs, SEGV_MAPERR);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100441 return;
442 }
443
444no_context:
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100445 do_no_context(regs);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100446}
447#endif
448
Martin Schwidefsky1e546222010-10-25 16:10:37 +0200449int __handle_fault(unsigned long uaddr, unsigned long pgm_int_code, int write)
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100450{
451 struct pt_regs regs;
452 int access, fault;
453
Heiko Carstens008c2e82012-07-27 09:45:39 +0200454 /* Emulate a uaccess fault from kernel mode. */
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100455 regs.psw.mask = psw_kernel_bits | PSW_MASK_DAT | PSW_MASK_MCHECK;
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100456 if (!irqs_disabled())
457 regs.psw.mask |= PSW_MASK_IO | PSW_MASK_EXT;
458 regs.psw.addr = (unsigned long) __builtin_return_address(0);
459 regs.psw.addr |= PSW_ADDR_AMODE;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100460 regs.int_code = pgm_int_code;
461 regs.int_parm_long = (uaddr & PAGE_MASK) | 2;
Martin Schwidefsky1e546222010-10-25 16:10:37 +0200462 access = write ? VM_WRITE : VM_READ;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100463 fault = do_exception(&regs, access);
Heiko Carstens008c2e82012-07-27 09:45:39 +0200464 /*
465 * Since the fault happened in kernel mode while performing a uaccess
466 * all we need to do now is emulating a fixup in case "fault" is not
467 * zero.
468 * For the calling uaccess functions this results always in -EFAULT.
469 */
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100470 return fault ? -EFAULT : 0;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#ifdef CONFIG_PFAULT
474/*
475 * 'pfault' pseudo page faults routines.
476 */
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100477static int pfault_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479static int __init nopfault(char *str)
480{
481 pfault_disable = 1;
482 return 1;
483}
484
485__setup("nopfault", nopfault);
486
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200487struct pfault_refbk {
488 u16 refdiagc;
489 u16 reffcode;
490 u16 refdwlen;
491 u16 refversn;
492 u64 refgaddr;
493 u64 refselmk;
494 u64 refcmpmk;
495 u64 reserved;
496} __attribute__ ((packed, aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498int pfault_init(void)
499{
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200500 struct pfault_refbk refbk = {
501 .refdiagc = 0x258,
502 .reffcode = 0,
503 .refdwlen = 5,
504 .refversn = 2,
505 .refgaddr = __LC_CURRENT_PID,
506 .refselmk = 1ULL << 48,
507 .refcmpmk = 1ULL << 48,
508 .reserved = __PF_RES_FIELD };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int rc;
510
Carsten Ottef32269a2011-12-27 11:27:11 +0100511 if (pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -1;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200513 asm volatile(
514 " diag %1,%0,0x258\n"
515 "0: j 2f\n"
516 "1: la %0,8\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 "2:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200518 EX_TABLE(0b,1b)
519 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return rc;
521}
522
523void pfault_fini(void)
524{
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200525 struct pfault_refbk refbk = {
526 .refdiagc = 0x258,
527 .reffcode = 1,
528 .refdwlen = 5,
529 .refversn = 2,
530 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Carsten Ottef32269a2011-12-27 11:27:11 +0100532 if (pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200534 asm volatile(
535 " diag %0,0,0x258\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 "0:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200537 EX_TABLE(0b,0b)
538 : : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200541static DEFINE_SPINLOCK(pfault_lock);
542static LIST_HEAD(pfault_list);
543
Heiko Carstensfde15c32012-03-11 11:59:31 -0400544static void pfault_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200545 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct task_struct *tsk;
548 __u16 subcode;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200549 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * Get the external interruption subcode & pfault
553 * initial/completion signal bit. VM stores this
554 * in the 'cpu address' field associated with the
555 * external interrupt.
556 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400557 subcode = ext_code.subcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if ((subcode & 0xff00) != __SUBCODE_MASK)
559 return;
Heiko Carstensa9851832011-04-29 10:42:19 +0200560 kstat_cpu(smp_processor_id()).irqs[EXTINT_PFL]++;
Heiko Carstens54c27792012-05-10 09:44:35 +0200561 /* Get the token (= pid of the affected task). */
562 pid = sizeof(void *) == 4 ? param32 : param64;
563 rcu_read_lock();
564 tsk = find_task_by_pid_ns(pid, &init_pid_ns);
565 if (tsk)
566 get_task_struct(tsk);
567 rcu_read_unlock();
568 if (!tsk)
569 return;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200570 spin_lock(&pfault_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (subcode & 0x0080) {
572 /* signal bit is set -> a page has been swapped in by VM */
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200573 if (tsk->thread.pfault_wait == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Initial interrupt was faster than the completion
575 * interrupt. pfault_wait is valid. Set pfault_wait
576 * back to zero and wake up the process. This can
577 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700578 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 tsk->thread.pfault_wait = 0;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200580 list_del(&tsk->thread.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 wake_up_process(tsk);
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200582 put_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200583 } else {
584 /* Completion interrupt was faster than initial
585 * interrupt. Set pfault_wait to -1 so the initial
Heiko Carstensfa2fb2f2011-11-14 11:19:01 +0100586 * interrupt doesn't put the task to sleep.
587 * If the task is not running, ignore the completion
588 * interrupt since it must be a leftover of a PFAULT
589 * CANCEL operation which didn't remove all pending
590 * completion interrupts. */
591 if (tsk->state == TASK_RUNNING)
592 tsk->thread.pfault_wait = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 } else {
595 /* signal bit not set -> a real page is missing. */
Heiko Carstensd49f47f2012-05-10 10:47:21 +0200596 if (WARN_ON_ONCE(tsk != current))
597 goto out;
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200598 if (tsk->thread.pfault_wait == 1) {
599 /* Already on the list with a reference: put to sleep */
Heiko Carstens0a16ba72012-05-10 09:56:34 +0200600 __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200601 set_tsk_need_resched(tsk);
602 } else if (tsk->thread.pfault_wait == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Completion interrupt was faster than the initial
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200604 * interrupt (pfault_wait == -1). Set pfault_wait
605 * back to zero and exit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tsk->thread.pfault_wait = 0;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200607 } else {
608 /* Initial interrupt arrived before completion
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200609 * interrupt. Let the task sleep.
610 * An extra task reference is needed since a different
611 * cpu may set the task state to TASK_RUNNING again
612 * before the scheduler is reached. */
613 get_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200614 tsk->thread.pfault_wait = 1;
615 list_add(&tsk->thread.list, &pfault_list);
Heiko Carstens0a16ba72012-05-10 09:56:34 +0200616 __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 set_tsk_need_resched(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Heiko Carstensd49f47f2012-05-10 10:47:21 +0200620out:
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200621 spin_unlock(&pfault_lock);
Heiko Carstens54c27792012-05-10 09:44:35 +0200622 put_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200623}
624
625static int __cpuinit pfault_cpu_notify(struct notifier_block *self,
626 unsigned long action, void *hcpu)
627{
628 struct thread_struct *thread, *next;
629 struct task_struct *tsk;
630
Heiko Carstens1c725922012-08-27 15:43:49 +0200631 switch (action & ~CPU_TASKS_FROZEN) {
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200632 case CPU_DEAD:
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200633 spin_lock_irq(&pfault_lock);
634 list_for_each_entry_safe(thread, next, &pfault_list, list) {
635 thread->pfault_wait = 0;
636 list_del(&thread->list);
637 tsk = container_of(thread, struct task_struct, thread);
638 wake_up_process(tsk);
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200639 put_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200640 }
641 spin_unlock_irq(&pfault_lock);
642 break;
643 default:
644 break;
645 }
646 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100649static int __init pfault_irq_init(void)
Heiko Carstens29b08d22006-12-04 15:40:40 +0100650{
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100651 int rc;
Heiko Carstens29b08d22006-12-04 15:40:40 +0100652
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100653 rc = register_external_interrupt(0x2603, pfault_interrupt);
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200654 if (rc)
655 goto out_extint;
656 rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
657 if (rc)
658 goto out_pfault;
Heiko Carstensdf7997a2011-05-26 09:48:23 +0200659 service_subclass_irq_register();
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200660 hotcpu_notifier(pfault_cpu_notify, 0);
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100661 return 0;
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200662
663out_pfault:
664 unregister_external_interrupt(0x2603, pfault_interrupt);
665out_extint:
666 pfault_disable = 1;
667 return rc;
Heiko Carstens29b08d22006-12-04 15:40:40 +0100668}
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100669early_initcall(pfault_irq_init);
670
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200671#endif /* CONFIG_PFAULT */