blob: 6a12d1bb6e09d065e5a4b2625ce79ba7209b5c2b [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
52
Martin Schwidefsky92f842e2010-10-25 16:10:13 +020053static unsigned long store_indication;
54
55void fault_init(void)
56{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +020057 if (test_facility(2) && test_facility(75))
Martin Schwidefsky92f842e2010-10-25 16:10:13 +020058 store_indication = 0xc00;
59}
60
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +010061static inline int notify_page_fault(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +020062{
Christoph Hellwig33464e32007-05-04 18:47:46 +020063 int ret = 0;
64
65 /* kprobe_running() needs smp_processor_id() */
Heiko Carstens22e0a042010-02-26 22:37:45 +010066 if (kprobes_built_in() && !user_mode(regs)) {
Christoph Hellwig33464e32007-05-04 18:47:46 +020067 preempt_disable();
68 if (kprobe_running() && kprobe_fault_handler(regs, 14))
69 ret = 1;
70 preempt_enable();
71 }
Christoph Hellwig33464e32007-05-04 18:47:46 +020072 return ret;
Michael Grundy4ba069b2006-09-20 15:58:39 +020073}
Michael Grundy4ba069b2006-09-20 15:58:39 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
77 * Unlock any spinlocks which will prevent us from getting the
Kirill Korotaevcefc8be2007-02-10 01:46:18 -080078 * message out.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80void bust_spinlocks(int yes)
81{
82 if (yes) {
83 oops_in_progress = 1;
84 } else {
85 int loglevel_save = console_loglevel;
86 console_unblank();
87 oops_in_progress = 0;
88 /*
89 * OK, the message is on the console. Now we call printk()
90 * without oops_in_progress set so that printk will give klogd
91 * a poke. Hold onto your hats...
92 */
93 console_loglevel = 15;
94 printk(" ");
95 console_loglevel = loglevel_save;
96 }
97}
98
99/*
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100100 * Returns the address space associated with the fault.
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100101 * Returns 0 for kernel space and 1 for user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100103static inline int user_space_fault(unsigned long trans_exc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 /*
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100106 * The lowest two bits of the translation exception
107 * identification indicate which paging table was used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100109 trans_exc_code &= 3;
110 if (trans_exc_code == 2)
111 /* Access via secondary space, set_fs setting decides */
112 return current->thread.mm_segment.ar4;
Martin Schwidefskyb11b5332009-12-07 12:51:43 +0100113 if (user_mode == HOME_SPACE_MODE)
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100114 /* User space if the access has been done via home space. */
115 return trans_exc_code == 3;
116 /*
117 * If the user space is not the home space the kernel runs in home
118 * space. Access via secondary space has already been covered,
119 * access via primary space or access register is from user space
120 * and access via home space is from the kernel.
121 */
122 return trans_exc_code != 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100125static inline void report_user_fault(struct pt_regs *regs, long signr)
Heiko Carstensab3c68e2010-05-17 10:00:21 +0200126{
127 if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
128 return;
129 if (!unhandled_signal(current, signr))
130 return;
131 if (!printk_ratelimit())
132 return;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100133 printk(KERN_ALERT "User process fault: interruption code 0x%X ",
134 regs->int_code);
Heiko Carstensab3c68e2010-05-17 10:00:21 +0200135 print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN);
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100136 printk(KERN_CONT "\n");
137 printk(KERN_ALERT "failing address: %lX\n",
138 regs->int_parm_long & __FAIL_ADDR_MASK);
Heiko Carstensab3c68e2010-05-17 10:00:21 +0200139 show_regs(regs);
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Send SIGSEGV to task. This is an external routine
144 * to keep the stack usage of do_page_fault small.
145 */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100146static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct siginfo si;
149
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100150 report_user_fault(regs, SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 si.si_signo = SIGSEGV;
152 si.si_code = si_code;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100153 si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 force_sig_info(SIGSEGV, &si, current);
155}
156
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100157static noinline void do_no_context(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200158{
159 const struct exception_table_entry *fixup;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100160 unsigned long address;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200161
162 /* Are we prepared to handle this kernel fault? */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100163 fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200164 if (fixup) {
165 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
166 return;
167 }
168
169 /*
170 * Oops. The kernel tried to access some bad page. We'll have to
171 * terminate things with extreme prejudice.
172 */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100173 address = regs->int_parm_long & __FAIL_ADDR_MASK;
174 if (!user_space_fault(regs->int_parm_long))
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200175 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
176 " at virtual kernel address %p\n", (void *)address);
177 else
178 printk(KERN_ALERT "Unable to handle kernel paging request"
179 " at virtual user address %p\n", (void *)address);
180
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100181 die(regs, "Oops");
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200182 do_exit(SIGKILL);
183}
184
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100185static noinline void do_low_address(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200186{
187 /* Low-address protection hit in kernel mode means
188 NULL pointer write access in kernel mode. */
189 if (regs->psw.mask & PSW_MASK_PSTATE) {
190 /* Low-address protection hit in user mode 'cannot happen'. */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100191 die (regs, "Low-address protection");
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200192 do_exit(SIGKILL);
193 }
194
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100195 do_no_context(regs);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200196}
197
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100198static noinline void do_sigbus(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200199{
200 struct task_struct *tsk = current;
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200201 struct siginfo si;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200202
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200203 /*
204 * Send a sigbus, regardless of whether we were in kernel
205 * or user mode.
206 */
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200207 si.si_signo = SIGBUS;
208 si.si_errno = 0;
209 si.si_code = BUS_ADRERR;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100210 si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200211 force_sig_info(SIGBUS, &si, tsk);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200212}
213
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100214static noinline void do_fault_error(struct pt_regs *regs, int fault)
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100215{
216 int si_code;
217
218 switch (fault) {
219 case VM_FAULT_BADACCESS:
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100220 case VM_FAULT_BADMAP:
221 /* Bad memory access. Check if it is kernel or user space. */
222 if (regs->psw.mask & PSW_MASK_PSTATE) {
223 /* User mode accesses just cause a SIGSEGV */
224 si_code = (fault == VM_FAULT_BADMAP) ?
225 SEGV_MAPERR : SEGV_ACCERR;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100226 do_sigsegv(regs, si_code);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100227 return;
228 }
229 case VM_FAULT_BADCONTEXT:
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100230 do_no_context(regs);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100231 break;
232 default: /* fault & VM_FAULT_ERROR */
Heiko Carstens99583182011-05-26 09:48:29 +0200233 if (fault & VM_FAULT_OOM) {
234 if (!(regs->psw.mask & PSW_MASK_PSTATE))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100235 do_no_context(regs);
Heiko Carstens99583182011-05-26 09:48:29 +0200236 else
237 pagefault_out_of_memory();
238 } else if (fault & VM_FAULT_SIGBUS) {
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100239 /* Kernel mode? Handle exceptions or die */
240 if (!(regs->psw.mask & PSW_MASK_PSTATE))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100241 do_no_context(regs);
Martin Schwidefsky36bf9682010-10-25 16:10:35 +0200242 else
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100243 do_sigbus(regs);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100244 } else
245 BUG();
246 break;
247 }
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * This routine handles page faults. It determines the address,
252 * and the problem, and then passes it off to one of the appropriate
253 * routines.
254 *
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100255 * interruption code (int_code):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * 04 Protection -> Write-Protection (suprression)
257 * 10 Segment translation -> Not present (nullification)
258 * 11 Page translation -> Not present (nullification)
259 * 3b Region third trans. -> Not present (nullification)
260 */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100261static inline int do_exception(struct pt_regs *regs, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200263 struct task_struct *tsk;
264 struct mm_struct *mm;
265 struct vm_area_struct *vma;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100266 unsigned long trans_exc_code;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200267 unsigned long address;
Heiko Carstens33ce6142011-05-26 09:48:30 +0200268 unsigned int flags;
269 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +0100271 if (notify_page_fault(regs))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100272 return 0;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200273
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200274 tsk = current;
275 mm = tsk->mm;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100276 trans_exc_code = regs->int_parm_long;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /*
279 * Verify that the fault happened in user space, that
280 * we are not in an interrupt and that there is a
281 * user context.
282 */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100283 fault = VM_FAULT_BADCONTEXT;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100284 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100287 address = trans_exc_code & __FAIL_ADDR_MASK;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200288 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Heiko Carstens33ce6142011-05-26 09:48:30 +0200289 flags = FAULT_FLAG_ALLOW_RETRY;
290 if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
291 flags |= FAULT_FLAG_WRITE;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200292 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200294#ifdef CONFIG_PGSTE
Martin Schwidefsky5e8010c2012-05-03 15:50:44 +0200295 if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
Carsten Otte499069e2011-10-30 15:17:02 +0100296 address = __gmap_fault(address,
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200297 (struct gmap *) S390_lowcore.gmap);
298 if (address == -EFAULT) {
299 fault = VM_FAULT_BADMAP;
300 goto out_up;
301 }
302 if (address == -ENOMEM) {
303 fault = VM_FAULT_OOM;
304 goto out_up;
305 }
306 }
307#endif
308
309retry:
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100310 fault = VM_FAULT_BADMAP;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100311 vma = find_vma(mm, address);
312 if (!vma)
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100313 goto out_up;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100314
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100315 if (unlikely(vma->vm_start > address)) {
316 if (!(vma->vm_flags & VM_GROWSDOWN))
317 goto out_up;
318 if (expand_stack(vma, address))
319 goto out_up;
320 }
321
322 /*
323 * Ok, we have a good vm_area for this memory access, so
324 * we can handle it..
325 */
326 fault = VM_FAULT_BADACCESS;
Martin Schwidefsky1ab947d2009-12-07 12:51:46 +0100327 if (unlikely(!(vma->vm_flags & access)))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100328 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Gerald Schaefer53492b12008-04-30 13:38:46 +0200330 if (is_vm_hugetlb_page(vma))
331 address &= HPAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * If for any reason at all we couldn't handle the fault,
334 * make sure we exit gracefully rather than endlessly redo
335 * the fault.
336 */
Heiko Carstens33ce6142011-05-26 09:48:30 +0200337 fault = handle_mm_fault(mm, vma, address, flags);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100338 if (unlikely(fault & VM_FAULT_ERROR))
339 goto out_up;
340
Heiko Carstens33ce6142011-05-26 09:48:30 +0200341 /*
342 * Major/minor page fault accounting is only done on the
343 * initial attempt. If we go through a retry, it is extremely
344 * likely that the page will be found in page cache at that point.
345 */
346 if (flags & FAULT_FLAG_ALLOW_RETRY) {
347 if (fault & VM_FAULT_MAJOR) {
348 tsk->maj_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200349 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
Heiko Carstens33ce6142011-05-26 09:48:30 +0200350 regs, address);
351 } else {
352 tsk->min_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200353 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
Heiko Carstens33ce6142011-05-26 09:48:30 +0200354 regs, address);
355 }
356 if (fault & VM_FAULT_RETRY) {
357 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
358 * of starvation. */
359 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200360 down_read(&mm->mmap_sem);
Heiko Carstens33ce6142011-05-26 09:48:30 +0200361 goto retry;
362 }
Heiko Carstensbde69af2009-09-11 10:29:06 +0200363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * The instruction that caused the program check will
366 * be repeated. Don't signal single step via SIGTRAP.
367 */
Martin Schwidefsky5e9a2692011-01-05 12:48:10 +0100368 clear_tsk_thread_flag(tsk, TIF_PER_TRAP);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100369 fault = 0;
370out_up:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200371 up_read(&mm->mmap_sem);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100372out:
373 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100376void __kprobes do_protection_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100378 unsigned long trans_exc_code;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100379 int fault;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100380
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100381 trans_exc_code = regs->int_parm_long;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300382 /* Protection exception is suppressing, decrement psw address. */
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100383 regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200384 /*
385 * Check for low-address protection. This needs to be treated
386 * as a special case because the translation exception code
387 * field is not guaranteed to contain valid data in this case.
388 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100389 if (unlikely(!(trans_exc_code & 4))) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100390 do_low_address(regs);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200391 return;
392 }
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100393 fault = do_exception(regs, VM_WRITE);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100394 if (unlikely(fault))
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100395 do_fault_error(regs, fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100398void __kprobes do_dat_exception(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Martin Schwidefsky1ab947d2009-12-07 12:51:46 +0100400 int access, fault;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100401
Martin Schwidefsky1ab947d2009-12-07 12:51:46 +0100402 access = VM_READ | VM_EXEC | VM_WRITE;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100403 fault = do_exception(regs, access);
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 Schwidefsky6252d702008-02-09 18:24:37 +0100408#ifdef CONFIG_64BIT
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100409void __kprobes do_asce_exception(struct pt_regs *regs)
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100410{
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100411 struct mm_struct *mm = current->mm;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100412 struct vm_area_struct *vma;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100413 unsigned long trans_exc_code;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100414
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100415 trans_exc_code = regs->int_parm_long;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100416 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100417 goto no_context;
418
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100419 down_read(&mm->mmap_sem);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100420 vma = find_vma(mm, trans_exc_code & __FAIL_ADDR_MASK);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100421 up_read(&mm->mmap_sem);
422
423 if (vma) {
424 update_mm(mm, current);
425 return;
426 }
427
428 /* User mode accesses just cause a SIGSEGV */
429 if (regs->psw.mask & PSW_MASK_PSTATE) {
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100430 do_sigsegv(regs, SEGV_MAPERR);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100431 return;
432 }
433
434no_context:
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100435 do_no_context(regs);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100436}
437#endif
438
Martin Schwidefsky1e546222010-10-25 16:10:37 +0200439int __handle_fault(unsigned long uaddr, unsigned long pgm_int_code, int write)
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100440{
441 struct pt_regs regs;
442 int access, fault;
443
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100444 regs.psw.mask = psw_kernel_bits | PSW_MASK_DAT | PSW_MASK_MCHECK;
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100445 if (!irqs_disabled())
446 regs.psw.mask |= PSW_MASK_IO | PSW_MASK_EXT;
447 regs.psw.addr = (unsigned long) __builtin_return_address(0);
448 regs.psw.addr |= PSW_ADDR_AMODE;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100449 regs.int_code = pgm_int_code;
450 regs.int_parm_long = (uaddr & PAGE_MASK) | 2;
Martin Schwidefsky1e546222010-10-25 16:10:37 +0200451 access = write ? VM_WRITE : VM_READ;
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100452 fault = do_exception(&regs, access);
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100453 if (unlikely(fault)) {
Heiko Carstens99583182011-05-26 09:48:29 +0200454 if (fault & VM_FAULT_OOM)
455 return -EFAULT;
456 else if (fault & VM_FAULT_SIGBUS)
Martin Schwidefskyaa33c8c2011-12-27 11:27:18 +0100457 do_sigbus(&regs);
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100458 }
459 return fault ? -EFAULT : 0;
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#ifdef CONFIG_PFAULT
463/*
464 * 'pfault' pseudo page faults routines.
465 */
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100466static int pfault_disable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468static int __init nopfault(char *str)
469{
470 pfault_disable = 1;
471 return 1;
472}
473
474__setup("nopfault", nopfault);
475
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200476struct pfault_refbk {
477 u16 refdiagc;
478 u16 reffcode;
479 u16 refdwlen;
480 u16 refversn;
481 u64 refgaddr;
482 u64 refselmk;
483 u64 refcmpmk;
484 u64 reserved;
485} __attribute__ ((packed, aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487int pfault_init(void)
488{
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200489 struct pfault_refbk refbk = {
490 .refdiagc = 0x258,
491 .reffcode = 0,
492 .refdwlen = 5,
493 .refversn = 2,
494 .refgaddr = __LC_CURRENT_PID,
495 .refselmk = 1ULL << 48,
496 .refcmpmk = 1ULL << 48,
497 .reserved = __PF_RES_FIELD };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int rc;
499
Carsten Ottef32269a2011-12-27 11:27:11 +0100500 if (pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -1;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200502 asm volatile(
503 " diag %1,%0,0x258\n"
504 "0: j 2f\n"
505 "1: la %0,8\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 "2:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200507 EX_TABLE(0b,1b)
508 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return rc;
510}
511
512void pfault_fini(void)
513{
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200514 struct pfault_refbk refbk = {
515 .refdiagc = 0x258,
516 .reffcode = 1,
517 .refdwlen = 5,
518 .refversn = 2,
519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Carsten Ottef32269a2011-12-27 11:27:11 +0100521 if (pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200523 asm volatile(
524 " diag %0,0,0x258\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 "0:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200526 EX_TABLE(0b,0b)
527 : : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200530static DEFINE_SPINLOCK(pfault_lock);
531static LIST_HEAD(pfault_list);
532
Heiko Carstensfde15c32012-03-11 11:59:31 -0400533static void pfault_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200534 unsigned int param32, unsigned long param64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct task_struct *tsk;
537 __u16 subcode;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200538 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /*
541 * Get the external interruption subcode & pfault
542 * initial/completion signal bit. VM stores this
543 * in the 'cpu address' field associated with the
544 * external interrupt.
545 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400546 subcode = ext_code.subcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if ((subcode & 0xff00) != __SUBCODE_MASK)
548 return;
Heiko Carstensa9851832011-04-29 10:42:19 +0200549 kstat_cpu(smp_processor_id()).irqs[EXTINT_PFL]++;
Heiko Carstens54c27792012-05-10 09:44:35 +0200550 /* Get the token (= pid of the affected task). */
551 pid = sizeof(void *) == 4 ? param32 : param64;
552 rcu_read_lock();
553 tsk = find_task_by_pid_ns(pid, &init_pid_ns);
554 if (tsk)
555 get_task_struct(tsk);
556 rcu_read_unlock();
557 if (!tsk)
558 return;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200559 spin_lock(&pfault_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (subcode & 0x0080) {
561 /* signal bit is set -> a page has been swapped in by VM */
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200562 if (tsk->thread.pfault_wait == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Initial interrupt was faster than the completion
564 * interrupt. pfault_wait is valid. Set pfault_wait
565 * back to zero and wake up the process. This can
566 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700567 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 tsk->thread.pfault_wait = 0;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200569 list_del(&tsk->thread.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 wake_up_process(tsk);
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200571 put_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200572 } else {
573 /* Completion interrupt was faster than initial
574 * interrupt. Set pfault_wait to -1 so the initial
Heiko Carstensfa2fb2f2011-11-14 11:19:01 +0100575 * interrupt doesn't put the task to sleep.
576 * If the task is not running, ignore the completion
577 * interrupt since it must be a leftover of a PFAULT
578 * CANCEL operation which didn't remove all pending
579 * completion interrupts. */
580 if (tsk->state == TASK_RUNNING)
581 tsk->thread.pfault_wait = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 } else {
584 /* signal bit not set -> a real page is missing. */
Heiko Carstensd49f47f2012-05-10 10:47:21 +0200585 if (WARN_ON_ONCE(tsk != current))
586 goto out;
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200587 if (tsk->thread.pfault_wait == 1) {
588 /* Already on the list with a reference: put to sleep */
Heiko Carstens0a16ba72012-05-10 09:56:34 +0200589 __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200590 set_tsk_need_resched(tsk);
591 } else if (tsk->thread.pfault_wait == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* Completion interrupt was faster than the initial
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200593 * interrupt (pfault_wait == -1). Set pfault_wait
594 * back to zero and exit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 tsk->thread.pfault_wait = 0;
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200596 } else {
597 /* Initial interrupt arrived before completion
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200598 * interrupt. Let the task sleep.
599 * An extra task reference is needed since a different
600 * cpu may set the task state to TASK_RUNNING again
601 * before the scheduler is reached. */
602 get_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200603 tsk->thread.pfault_wait = 1;
604 list_add(&tsk->thread.list, &pfault_list);
Heiko Carstens0a16ba72012-05-10 09:56:34 +0200605 __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 set_tsk_need_resched(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Heiko Carstensd49f47f2012-05-10 10:47:21 +0200609out:
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200610 spin_unlock(&pfault_lock);
Heiko Carstens54c27792012-05-10 09:44:35 +0200611 put_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200612}
613
614static int __cpuinit pfault_cpu_notify(struct notifier_block *self,
615 unsigned long action, void *hcpu)
616{
617 struct thread_struct *thread, *next;
618 struct task_struct *tsk;
619
620 switch (action) {
621 case CPU_DEAD:
622 case CPU_DEAD_FROZEN:
623 spin_lock_irq(&pfault_lock);
624 list_for_each_entry_safe(thread, next, &pfault_list, list) {
625 thread->pfault_wait = 0;
626 list_del(&thread->list);
627 tsk = container_of(thread, struct task_struct, thread);
628 wake_up_process(tsk);
Heiko Carstensd5e50a52012-05-09 09:37:30 +0200629 put_task_struct(tsk);
Heiko Carstensf2db2e62011-05-23 10:24:34 +0200630 }
631 spin_unlock_irq(&pfault_lock);
632 break;
633 default:
634 break;
635 }
636 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100639static int __init pfault_irq_init(void)
Heiko Carstens29b08d22006-12-04 15:40:40 +0100640{
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100641 int rc;
Heiko Carstens29b08d22006-12-04 15:40:40 +0100642
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100643 rc = register_external_interrupt(0x2603, pfault_interrupt);
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200644 if (rc)
645 goto out_extint;
646 rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
647 if (rc)
648 goto out_pfault;
Heiko Carstensdf7997a2011-05-26 09:48:23 +0200649 service_subclass_irq_register();
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200650 hotcpu_notifier(pfault_cpu_notify, 0);
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100651 return 0;
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200652
653out_pfault:
654 unregister_external_interrupt(0x2603, pfault_interrupt);
655out_extint:
656 pfault_disable = 1;
657 return rc;
Heiko Carstens29b08d22006-12-04 15:40:40 +0100658}
Heiko Carstensfb0a9d72011-01-05 12:47:39 +0100659early_initcall(pfault_irq_init);
660
Heiko Carstens7dd8fe12011-05-23 10:24:35 +0200661#endif /* CONFIG_PFAULT */