blob: 0dcfcfb5b5be31faa8c3e84585877f020ed6e285 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/mm/fault.c
3 *
4 * S390 version
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Hartmut Penner (hp@de.ibm.com)
7 * Ulrich Weigand (uweigand@de.ibm.com)
8 *
9 * Derived from "arch/i386/mm/fault.c"
10 * Copyright (C) 1995 Linus Torvalds
11 */
12
Ingo Molnarcdd6c482009-09-21 12:02:48 +020013#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/string.h>
19#include <linux/types.h>
20#include <linux/ptrace.h>
21#include <linux/mman.h>
22#include <linux/mm.h>
Heiko Carstens77575912009-06-12 10:26:25 +020023#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/smp.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070025#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/module.h>
29#include <linux/hardirq.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020030#include <linux/kprobes.h>
Martin Schwidefskybe5ec362007-04-27 16:01:44 +020031#include <linux/uaccess.h>
Gerald Schaefer53492b12008-04-30 13:38:46 +020032#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/pgtable.h>
Heiko Carstens29b08d22006-12-04 15:40:40 +010035#include <asm/s390_ext.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010036#include <asm/mmu_context.h>
Martin Schwidefsky50d72802009-12-07 12:51:45 +010037#include <asm/compat.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020038#include "../kernel/entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080040#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define __FAIL_ADDR_MASK 0x7ffff000
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define __SUBCODE_MASK 0x0200
43#define __PF_RES_FIELD 0ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080044#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define __FAIL_ADDR_MASK -4096L
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define __SUBCODE_MASK 0x0600
47#define __PF_RES_FIELD 0x8000000000000000ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080048#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#ifdef CONFIG_SYSCTL
51extern int sysctl_userprocess_debug;
52#endif
53
Martin Schwidefsky50d72802009-12-07 12:51:45 +010054#define VM_FAULT_BADCONTEXT 0x010000
55#define VM_FAULT_BADMAP 0x020000
56#define VM_FAULT_BADACCESS 0x040000
57
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +010058static inline int notify_page_fault(struct pt_regs *regs)
Martin Schwidefsky10c10312007-04-27 16:01:43 +020059{
Christoph Hellwig33464e32007-05-04 18:47:46 +020060 int ret = 0;
61
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +010062#ifdef CONFIG_KPROBES
Christoph Hellwig33464e32007-05-04 18:47:46 +020063 /* kprobe_running() needs smp_processor_id() */
64 if (!user_mode(regs)) {
65 preempt_disable();
66 if (kprobe_running() && kprobe_fault_handler(regs, 14))
67 ret = 1;
68 preempt_enable();
69 }
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +010070#endif
Christoph Hellwig33464e32007-05-04 18:47:46 +020071 return ret;
Michael Grundy4ba069b2006-09-20 15:58:39 +020072}
Michael Grundy4ba069b2006-09-20 15:58:39 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Unlock any spinlocks which will prevent us from getting the
Kirill Korotaevcefc8be2007-02-10 01:46:18 -080077 * message out.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
79void bust_spinlocks(int yes)
80{
81 if (yes) {
82 oops_in_progress = 1;
83 } else {
84 int loglevel_save = console_loglevel;
85 console_unblank();
86 oops_in_progress = 0;
87 /*
88 * OK, the message is on the console. Now we call printk()
89 * without oops_in_progress set so that printk will give klogd
90 * a poke. Hold onto your hats...
91 */
92 console_loglevel = 15;
93 printk(" ");
94 console_loglevel = loglevel_save;
95 }
96}
97
98/*
Gerald Schaefer482b05d2007-03-05 23:35:54 +010099 * Returns the address space associated with the fault.
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100100 * Returns 0 for kernel space and 1 for user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100102static inline int user_space_fault(unsigned long trans_exc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 /*
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100105 * The lowest two bits of the translation exception
106 * identification indicate which paging table was used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100108 trans_exc_code &= 3;
109 if (trans_exc_code == 2)
110 /* Access via secondary space, set_fs setting decides */
111 return current->thread.mm_segment.ar4;
Martin Schwidefskyb11b5332009-12-07 12:51:43 +0100112 if (user_mode == HOME_SPACE_MODE)
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100113 /* User space if the access has been done via home space. */
114 return trans_exc_code == 3;
115 /*
116 * If the user space is not the home space the kernel runs in home
117 * space. Access via secondary space has already been covered,
118 * access via primary space or access register is from user space
119 * and access via home space is from the kernel.
120 */
121 return trans_exc_code != 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/*
125 * Send SIGSEGV to task. This is an external routine
126 * to keep the stack usage of do_page_fault small.
127 */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100128static noinline void do_sigsegv(struct pt_regs *regs, long int_code,
129 int si_code, unsigned long trans_exc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct siginfo si;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100132 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100134 address = trans_exc_code & __FAIL_ADDR_MASK;
135 current->thread.prot_addr = address;
136 current->thread.trap_no = int_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
138#if defined(CONFIG_SYSCTL)
139 if (sysctl_userprocess_debug)
140#endif
141 {
142 printk("User process fault: interruption code 0x%lX\n",
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100143 int_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 printk("failing address: %lX\n", address);
145 show_regs(regs);
146 }
147#endif
148 si.si_signo = SIGSEGV;
149 si.si_code = si_code;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200150 si.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 force_sig_info(SIGSEGV, &si, current);
152}
153
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100154static noinline void do_no_context(struct pt_regs *regs, long int_code,
155 unsigned long trans_exc_code)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200156{
157 const struct exception_table_entry *fixup;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100158 unsigned long address;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200159
160 /* Are we prepared to handle this kernel fault? */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100161 fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200162 if (fixup) {
163 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
164 return;
165 }
166
167 /*
168 * Oops. The kernel tried to access some bad page. We'll have to
169 * terminate things with extreme prejudice.
170 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100171 address = trans_exc_code & __FAIL_ADDR_MASK;
Martin Schwidefskyb11b5332009-12-07 12:51:43 +0100172 if (!user_space_fault(trans_exc_code))
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200173 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
174 " at virtual kernel address %p\n", (void *)address);
175 else
176 printk(KERN_ALERT "Unable to handle kernel paging request"
177 " at virtual user address %p\n", (void *)address);
178
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100179 die("Oops", regs, int_code);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200180 do_exit(SIGKILL);
181}
182
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100183static noinline void do_low_address(struct pt_regs *regs, long int_code,
184 unsigned long trans_exc_code)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200185{
186 /* Low-address protection hit in kernel mode means
187 NULL pointer write access in kernel mode. */
188 if (regs->psw.mask & PSW_MASK_PSTATE) {
189 /* Low-address protection hit in user mode 'cannot happen'. */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100190 die ("Low-address protection", regs, int_code);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200191 do_exit(SIGKILL);
192 }
193
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100194 do_no_context(regs, int_code, trans_exc_code);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200195}
196
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100197static noinline void do_sigbus(struct pt_regs *regs, long int_code,
198 unsigned long trans_exc_code)
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200199{
200 struct task_struct *tsk = current;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200201
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200202 /*
203 * Send a sigbus, regardless of whether we were in kernel
204 * or user mode.
205 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100206 tsk->thread.prot_addr = trans_exc_code & __FAIL_ADDR_MASK;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100207 tsk->thread.trap_no = int_code;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200208 force_sig(SIGBUS, tsk);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200209}
210
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100211#ifdef CONFIG_S390_EXEC_PROTECT
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100212static noinline int signal_return(struct pt_regs *regs, long int_code,
213 unsigned long trans_exc_code)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100214{
Martin Schwidefskybe5ec362007-04-27 16:01:44 +0200215 u16 instruction;
Heiko Carstens490f03d2007-05-10 15:45:48 +0200216 int rc;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100217
Martin Schwidefskybe5ec362007-04-27 16:01:44 +0200218 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100219
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100220 if (!rc && instruction == 0x0a77) {
221 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
222 if (is_compat_task())
223 sys32_sigreturn();
224 else
225 sys_sigreturn();
226 } else if (!rc && instruction == 0x0aad) {
227 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
228 if (is_compat_task())
229 sys32_rt_sigreturn();
230 else
231 sys_rt_sigreturn();
232 } else
233 do_sigsegv(regs, int_code, SEGV_MAPERR, trans_exc_code);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100234 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100235}
236#endif /* CONFIG_S390_EXEC_PROTECT */
237
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100238static noinline void do_fault_error(struct pt_regs *regs, long int_code,
239 unsigned long trans_exc_code, int fault)
240{
241 int si_code;
242
243 switch (fault) {
244 case VM_FAULT_BADACCESS:
245#ifdef CONFIG_S390_EXEC_PROTECT
246 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_SECONDARY &&
247 (trans_exc_code & 3) == 0) {
248 signal_return(regs, int_code, trans_exc_code);
249 break;
250 }
251#endif /* CONFIG_S390_EXEC_PROTECT */
252 case VM_FAULT_BADMAP:
253 /* Bad memory access. Check if it is kernel or user space. */
254 if (regs->psw.mask & PSW_MASK_PSTATE) {
255 /* User mode accesses just cause a SIGSEGV */
256 si_code = (fault == VM_FAULT_BADMAP) ?
257 SEGV_MAPERR : SEGV_ACCERR;
258 do_sigsegv(regs, int_code, si_code, trans_exc_code);
259 return;
260 }
261 case VM_FAULT_BADCONTEXT:
262 do_no_context(regs, int_code, trans_exc_code);
263 break;
264 default: /* fault & VM_FAULT_ERROR */
265 if (fault & VM_FAULT_OOM)
266 pagefault_out_of_memory();
267 else if (fault & VM_FAULT_SIGBUS) {
268 do_sigbus(regs, int_code, trans_exc_code);
269 /* Kernel mode? Handle exceptions or die */
270 if (!(regs->psw.mask & PSW_MASK_PSTATE))
271 do_no_context(regs, int_code, trans_exc_code);
272 } else
273 BUG();
274 break;
275 }
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * This routine handles page faults. It determines the address,
280 * and the problem, and then passes it off to one of the appropriate
281 * routines.
282 *
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100283 * interruption code (int_code):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * 04 Protection -> Write-Protection (suprression)
285 * 10 Segment translation -> Not present (nullification)
286 * 11 Page translation -> Not present (nullification)
287 * 3b Region third trans. -> Not present (nullification)
288 */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100289static inline int do_exception(struct pt_regs *regs, int write,
290 unsigned long trans_exc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200292 struct task_struct *tsk;
293 struct mm_struct *mm;
294 struct vm_area_struct *vma;
295 unsigned long address;
Nick Piggin83c54072007-07-19 01:47:05 -0700296 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Martin Schwidefsky7ecb3442009-12-07 12:51:44 +0100298 if (notify_page_fault(regs))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100299 return 0;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200300
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200301 tsk = current;
302 mm = tsk->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
305 * Verify that the fault happened in user space, that
306 * we are not in an interrupt and that there is a
307 * user context.
308 */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100309 fault = VM_FAULT_BADCONTEXT;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100310 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100313 address = trans_exc_code & __FAIL_ADDR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
315 * When we get here, the fault happened in the current
316 * task's user address space, so we can switch on the
317 * interrupts again and then search the VMAs
318 */
319 local_irq_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200320 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200321 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100323 fault = VM_FAULT_BADMAP;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100324 vma = find_vma(mm, address);
325 if (!vma)
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100326 goto out_up;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100327
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100328 if (unlikely(vma->vm_start > address)) {
329 if (!(vma->vm_flags & VM_GROWSDOWN))
330 goto out_up;
331 if (expand_stack(vma, address))
332 goto out_up;
333 }
334
335 /*
336 * Ok, we have a good vm_area for this memory access, so
337 * we can handle it..
338 */
339 fault = VM_FAULT_BADACCESS;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100340#ifdef CONFIG_S390_EXEC_PROTECT
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100341 if (unlikely((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_SECONDARY &&
342 (trans_exc_code & 3) == 0 && !(vma->vm_flags & VM_EXEC)))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100343 goto out_up;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100344#endif
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200345 if (!write) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* page not present, check vm flags */
347 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100348 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
350 if (!(vma->vm_flags & VM_WRITE))
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100351 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Gerald Schaefer53492b12008-04-30 13:38:46 +0200354 if (is_vm_hugetlb_page(vma))
355 address &= HPAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /*
357 * If for any reason at all we couldn't handle the fault,
358 * make sure we exit gracefully rather than endlessly redo
359 * the fault.
360 */
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700361 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100362 if (unlikely(fault & VM_FAULT_ERROR))
363 goto out_up;
364
Heiko Carstensbde69af2009-09-11 10:29:06 +0200365 if (fault & VM_FAULT_MAJOR) {
Nick Piggin83c54072007-07-19 01:47:05 -0700366 tsk->maj_flt++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200367 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
Heiko Carstensbde69af2009-09-11 10:29:06 +0200368 regs, address);
369 } else {
Nick Piggin83c54072007-07-19 01:47:05 -0700370 tsk->min_flt++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200371 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
Heiko Carstensbde69af2009-09-11 10:29:06 +0200372 regs, address);
373 }
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 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100378 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
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 Schwidefsky50d72802009-12-07 12:51:45 +0100386void __kprobes do_protection_exception(struct pt_regs *regs, long int_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100388 unsigned long trans_exc_code = S390_lowcore.trans_exc_code;
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100389 int fault;
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100390
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200391 /* Protection exception is supressing, decrement psw address. */
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100392 regs->psw.addr -= (int_code >> 16);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200393 /*
394 * Check for low-address protection. This needs to be treated
395 * as a special case because the translation exception code
396 * field is not guaranteed to contain valid data in this case.
397 */
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100398 if (unlikely(!(trans_exc_code & 4))) {
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100399 do_low_address(regs, int_code, trans_exc_code);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200400 return;
401 }
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100402 fault = do_exception(regs, 1, trans_exc_code);
403 if (unlikely(fault))
404 do_fault_error(regs, 4, trans_exc_code, fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100407void __kprobes do_dat_exception(struct pt_regs *regs, long int_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100409 unsigned long trans_exc_code = S390_lowcore.trans_exc_code;
410 int fault;
411
412 fault = do_exception(regs, 0, trans_exc_code);
413 if (unlikely(fault))
414 do_fault_error(regs, int_code & 255, trans_exc_code, fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100417#ifdef CONFIG_64BIT
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100418void __kprobes do_asce_exception(struct pt_regs *regs, long int_code)
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100419{
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100420 unsigned long trans_exc_code = S390_lowcore.trans_exc_code;
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 Schwidefsky6252d702008-02-09 18:24:37 +0100423
Martin Schwidefsky61365e12009-12-07 12:51:42 +0100424 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100425 goto no_context;
426
427 local_irq_enable();
428
429 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 */
439 if (regs->psw.mask & PSW_MASK_PSTATE) {
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100440 do_sigsegv(regs, int_code, SEGV_MAPERR, trans_exc_code);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100441 return;
442 }
443
444no_context:
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100445 do_no_context(regs, int_code, trans_exc_code);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100446}
447#endif
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#ifdef CONFIG_PFAULT
450/*
451 * 'pfault' pseudo page faults routines.
452 */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100453static ext_int_info_t ext_int_pfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int pfault_disable = 0;
455
456static int __init nopfault(char *str)
457{
458 pfault_disable = 1;
459 return 1;
460}
461
462__setup("nopfault", nopfault);
463
464typedef struct {
465 __u16 refdiagc;
466 __u16 reffcode;
467 __u16 refdwlen;
468 __u16 refversn;
469 __u64 refgaddr;
470 __u64 refselmk;
471 __u64 refcmpmk;
472 __u64 reserved;
Heiko Carstensc41fbc62007-10-12 16:11:51 +0200473} __attribute__ ((packed, aligned(8))) pfault_refbk_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475int pfault_init(void)
476{
477 pfault_refbk_t refbk =
478 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
479 __PF_RES_FIELD };
480 int rc;
481
Heiko Carstens29b08d22006-12-04 15:40:40 +0100482 if (!MACHINE_IS_VM || pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -1;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200484 asm volatile(
485 " diag %1,%0,0x258\n"
486 "0: j 2f\n"
487 "1: la %0,8\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 "2:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200489 EX_TABLE(0b,1b)
490 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 __ctl_set_bit(0, 9);
492 return rc;
493}
494
495void pfault_fini(void)
496{
497 pfault_refbk_t refbk =
498 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
499
Heiko Carstens29b08d22006-12-04 15:40:40 +0100500 if (!MACHINE_IS_VM || pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return;
502 __ctl_clear_bit(0,9);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200503 asm volatile(
504 " diag %0,0,0x258\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 "0:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200506 EX_TABLE(0b,0b)
507 : : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Martin Schwidefsky50d72802009-12-07 12:51:45 +0100510static void pfault_interrupt(__u16 int_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct task_struct *tsk;
513 __u16 subcode;
514
515 /*
516 * Get the external interruption subcode & pfault
517 * initial/completion signal bit. VM stores this
518 * in the 'cpu address' field associated with the
519 * external interrupt.
520 */
521 subcode = S390_lowcore.cpu_addr;
522 if ((subcode & 0xff00) != __SUBCODE_MASK)
523 return;
524
525 /*
526 * Get the token (= address of the task structure of the affected task).
527 */
528 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
529
530 if (subcode & 0x0080) {
531 /* signal bit is set -> a page has been swapped in by VM */
532 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
533 /* Initial interrupt was faster than the completion
534 * interrupt. pfault_wait is valid. Set pfault_wait
535 * back to zero and wake up the process. This can
536 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700537 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 tsk->thread.pfault_wait = 0;
539 wake_up_process(tsk);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700540 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 } else {
543 /* signal bit not set -> a real page is missing. */
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700544 get_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
546 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
547 /* Completion interrupt was faster than the initial
548 * interrupt (swapped in a -1 for pfault_wait). Set
549 * pfault_wait back to zero and exit. This can be
550 * done safely because tsk is running in kernel
551 * mode and can't produce new pfaults. */
552 tsk->thread.pfault_wait = 0;
553 set_task_state(tsk, TASK_RUNNING);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700554 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else
556 set_tsk_need_resched(tsk);
557 }
558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Heiko Carstens29b08d22006-12-04 15:40:40 +0100560void __init pfault_irq_init(void)
561{
562 if (!MACHINE_IS_VM)
563 return;
564
565 /*
566 * Try to get pfault pseudo page faults going.
567 */
568 if (register_early_external_interrupt(0x2603, pfault_interrupt,
569 &ext_int_pfault) != 0)
570 panic("Couldn't request external interrupt 0x2603");
571
572 if (pfault_init() == 0)
573 return;
574
575 /* Tough luck, no pfault. */
576 pfault_disable = 1;
577 unregister_early_external_interrupt(0x2603, pfault_interrupt,
578 &ext_int_pfault);
579}
580#endif