blob: 2b76a879a7b5455f92c3ccd9e2009b98e8393b8c [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
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>
22#include <linux/smp.h>
23#include <linux/smp_lock.h>
24#include <linux/init.h>
25#include <linux/console.h>
26#include <linux/module.h>
27#include <linux/hardirq.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020028#include <linux/kprobes.h>
Martin Schwidefskybe5ec362007-04-27 16:01:44 +020029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/pgtable.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020033#include <asm/kdebug.h>
Heiko Carstens29b08d22006-12-04 15:40:40 +010034#include <asm/s390_ext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080036#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define __FAIL_ADDR_MASK 0x7ffff000
38#define __FIXUP_MASK 0x7fffffff
39#define __SUBCODE_MASK 0x0200
40#define __PF_RES_FIELD 0ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080041#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define __FAIL_ADDR_MASK -4096L
43#define __FIXUP_MASK ~0L
44#define __SUBCODE_MASK 0x0600
45#define __PF_RES_FIELD 0x8000000000000000ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080046#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#ifdef CONFIG_SYSCTL
49extern int sysctl_userprocess_debug;
50#endif
51
52extern void die(const char *,struct pt_regs *,long);
53
Michael Grundy4ba069b2006-09-20 15:58:39 +020054#ifdef CONFIG_KPROBES
Heiko Carstens2b67fc42007-02-05 21:16:47 +010055static ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
Michael Grundy4ba069b2006-09-20 15:58:39 +020056int register_page_fault_notifier(struct notifier_block *nb)
57{
58 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
59}
60
61int unregister_page_fault_notifier(struct notifier_block *nb)
62{
63 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
64}
65
Martin Schwidefsky10c10312007-04-27 16:01:43 +020066static int __kprobes __notify_page_fault(struct pt_regs *regs, long err)
Michael Grundy4ba069b2006-09-20 15:58:39 +020067{
Martin Schwidefsky10c10312007-04-27 16:01:43 +020068 struct die_args args = { .str = "page fault",
69 .trapnr = 14,
70 .signr = SIGSEGV };
71 args.regs = regs;
72 args.err = err;
73 return atomic_notifier_call_chain(&notify_page_fault_chain,
74 DIE_PAGE_FAULT, &args);
75}
76
77static inline int notify_page_fault(struct pt_regs *regs, long err)
78{
79 if (unlikely(kprobe_running()))
80 return __notify_page_fault(regs, err);
81 return NOTIFY_DONE;
Michael Grundy4ba069b2006-09-20 15:58:39 +020082}
83#else
Martin Schwidefsky10c10312007-04-27 16:01:43 +020084static inline int notify_page_fault(struct pt_regs *regs, long err)
Michael Grundy4ba069b2006-09-20 15:58:39 +020085{
86 return NOTIFY_DONE;
87}
88#endif
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * Unlock any spinlocks which will prevent us from getting the
Kirill Korotaevcefc8be2007-02-10 01:46:18 -080093 * message out.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95void bust_spinlocks(int yes)
96{
97 if (yes) {
98 oops_in_progress = 1;
99 } else {
100 int loglevel_save = console_loglevel;
101 console_unblank();
102 oops_in_progress = 0;
103 /*
104 * OK, the message is on the console. Now we call printk()
105 * without oops_in_progress set so that printk will give klogd
106 * a poke. Hold onto your hats...
107 */
108 console_loglevel = 15;
109 printk(" ");
110 console_loglevel = loglevel_save;
111 }
112}
113
114/*
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100115 * Returns the address space associated with the fault.
116 * Returns 0 for kernel space, 1 for user space and
117 * 2 for code execution in user space with noexec=on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100119static inline int check_space(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 /*
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100122 * The lowest two bits of S390_lowcore.trans_exc_code
123 * indicate which paging table was used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100125 int desc = S390_lowcore.trans_exc_code & 3;
126
127 if (desc == 3) /* Home Segment Table Descriptor */
128 return switch_amode == 0;
129 if (desc == 2) /* Secondary Segment Table Descriptor */
130 return tsk->thread.mm_segment.ar4;
131#ifdef CONFIG_S390_SWITCH_AMODE
132 if (unlikely(desc == 1)) { /* STD determined via access register */
133 /* %a0 always indicates primary space. */
134 if (S390_lowcore.exc_access_id != 0) {
135 save_access_regs(tsk->thread.acrs);
136 /*
137 * An alet of 0 indicates primary space.
138 * An alet of 1 indicates secondary space.
139 * Any other alet values generate an
140 * alen-translation exception.
141 */
142 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
143 return tsk->thread.mm_segment.ar4;
144 }
145 }
146#endif
147 /* Primary Segment Table Descriptor */
148 return switch_amode << s390_noexec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151/*
152 * Send SIGSEGV to task. This is an external routine
153 * to keep the stack usage of do_page_fault small.
154 */
155static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
156 int si_code, unsigned long address)
157{
158 struct siginfo si;
159
160#if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
161#if defined(CONFIG_SYSCTL)
162 if (sysctl_userprocess_debug)
163#endif
164 {
165 printk("User process fault: interruption code 0x%lX\n",
166 error_code);
167 printk("failing address: %lX\n", address);
168 show_regs(regs);
169 }
170#endif
171 si.si_signo = SIGSEGV;
172 si.si_code = si_code;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200173 si.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 force_sig_info(SIGSEGV, &si, current);
175}
176
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200177static void do_no_context(struct pt_regs *regs, unsigned long error_code,
178 unsigned long address)
179{
180 const struct exception_table_entry *fixup;
181
182 /* Are we prepared to handle this kernel fault? */
183 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
184 if (fixup) {
185 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
186 return;
187 }
188
189 /*
190 * Oops. The kernel tried to access some bad page. We'll have to
191 * terminate things with extreme prejudice.
192 */
193 if (check_space(current) == 0)
194 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
195 " at virtual kernel address %p\n", (void *)address);
196 else
197 printk(KERN_ALERT "Unable to handle kernel paging request"
198 " at virtual user address %p\n", (void *)address);
199
200 die("Oops", regs, error_code);
201 do_exit(SIGKILL);
202}
203
204static void do_low_address(struct pt_regs *regs, unsigned long error_code)
205{
206 /* Low-address protection hit in kernel mode means
207 NULL pointer write access in kernel mode. */
208 if (regs->psw.mask & PSW_MASK_PSTATE) {
209 /* Low-address protection hit in user mode 'cannot happen'. */
210 die ("Low-address protection", regs, error_code);
211 do_exit(SIGKILL);
212 }
213
214 do_no_context(regs, error_code, 0);
215}
216
217/*
218 * We ran out of memory, or some other thing happened to us that made
219 * us unable to handle the page fault gracefully.
220 */
221static int do_out_of_memory(struct pt_regs *regs, unsigned long error_code,
222 unsigned long address)
223{
224 struct task_struct *tsk = current;
225 struct mm_struct *mm = tsk->mm;
226
227 up_read(&mm->mmap_sem);
228 if (is_init(tsk)) {
229 yield();
230 down_read(&mm->mmap_sem);
231 return 1;
232 }
233 printk("VM: killing process %s\n", tsk->comm);
234 if (regs->psw.mask & PSW_MASK_PSTATE)
235 do_exit(SIGKILL);
236 do_no_context(regs, error_code, address);
237 return 0;
238}
239
240static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
241 unsigned long address)
242{
243 struct task_struct *tsk = current;
244 struct mm_struct *mm = tsk->mm;
245
246 up_read(&mm->mmap_sem);
247 /*
248 * Send a sigbus, regardless of whether we were in kernel
249 * or user mode.
250 */
251 tsk->thread.prot_addr = address;
252 tsk->thread.trap_no = error_code;
253 force_sig(SIGBUS, tsk);
254
255 /* Kernel mode? Handle exceptions or die */
256 if (!(regs->psw.mask & PSW_MASK_PSTATE))
257 do_no_context(regs, error_code, address);
258}
259
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100260#ifdef CONFIG_S390_EXEC_PROTECT
261extern long sys_sigreturn(struct pt_regs *regs);
262extern long sys_rt_sigreturn(struct pt_regs *regs);
263extern long sys32_sigreturn(struct pt_regs *regs);
264extern long sys32_rt_sigreturn(struct pt_regs *regs);
265
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100266static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
267 unsigned long address, unsigned long error_code)
268{
Martin Schwidefskybe5ec362007-04-27 16:01:44 +0200269 u16 instruction;
270 int rc, compat;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100271
Martin Schwidefskybe5ec362007-04-27 16:01:44 +0200272 pagefault_disable();
273 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
274 pagefault_enable();
275 if (rc)
276 return -EFAULT;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100277
Martin Schwidefskybe5ec362007-04-27 16:01:44 +0200278 up_read(&mm->mmap_sem);
279 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
280#ifdef CONFIG_COMPAT
281 compat = test_tsk_thread_flag(current, TIF_31BIT);
282 if (compat && instruction == 0x0a77)
283 sys32_sigreturn(regs);
284 else if (compat && instruction == 0x0aad)
285 sys32_rt_sigreturn(regs);
286 else
287#endif
288 if (instruction == 0x0a77)
289 sys_sigreturn(regs);
290 else if (instruction == 0x0aad)
291 sys_rt_sigreturn(regs);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100292 else {
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100293 current->thread.prot_addr = address;
294 current->thread.trap_no = error_code;
295 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
296 }
297 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100298}
299#endif /* CONFIG_S390_EXEC_PROTECT */
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
302 * This routine handles page faults. It determines the address,
303 * and the problem, and then passes it off to one of the appropriate
304 * routines.
305 *
306 * error_code:
307 * 04 Protection -> Write-Protection (suprression)
308 * 10 Segment translation -> Not present (nullification)
309 * 11 Page translation -> Not present (nullification)
310 * 3b Region third trans. -> Not present (nullification)
311 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100312static inline void
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200313do_exception(struct pt_regs *regs, unsigned long error_code, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200315 struct task_struct *tsk;
316 struct mm_struct *mm;
317 struct vm_area_struct *vma;
318 unsigned long address;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100319 int space;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200320 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200322 if (notify_page_fault(regs, error_code) == NOTIFY_STOP)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200323 return;
324
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200325 tsk = current;
326 mm = tsk->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200328 /* get the failing address and the affected space */
329 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100330 space = check_space(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /*
333 * Verify that the fault happened in user space, that
334 * we are not in an interrupt and that there is a
335 * user context.
336 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100337 if (unlikely(space == 0 || in_atomic() || !mm))
338 goto no_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /*
341 * When we get here, the fault happened in the current
342 * task's user address space, so we can switch on the
343 * interrupts again and then search the VMAs
344 */
345 local_irq_enable();
346
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200347 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100349 si_code = SEGV_MAPERR;
350 vma = find_vma(mm, address);
351 if (!vma)
352 goto bad_area;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100353
354#ifdef CONFIG_S390_EXEC_PROTECT
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100355 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100356 if (!signal_return(mm, regs, address, error_code))
357 /*
358 * signal_return() has done an up_read(&mm->mmap_sem)
359 * if it returns 0.
360 */
361 return;
362#endif
363
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200364 if (vma->vm_start <= address)
365 goto good_area;
366 if (!(vma->vm_flags & VM_GROWSDOWN))
367 goto bad_area;
368 if (expand_stack(vma, address))
369 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*
371 * Ok, we have a good vm_area for this memory access, so
372 * we can handle it..
373 */
374good_area:
375 si_code = SEGV_ACCERR;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200376 if (!write) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* page not present, check vm flags */
378 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
379 goto bad_area;
380 } else {
381 if (!(vma->vm_flags & VM_WRITE))
382 goto bad_area;
383 }
384
385survive:
386 /*
387 * If for any reason at all we couldn't handle the fault,
388 * make sure we exit gracefully rather than endlessly redo
389 * the fault.
390 */
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200391 switch (handle_mm_fault(mm, vma, address, write)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 case VM_FAULT_MINOR:
393 tsk->min_flt++;
394 break;
395 case VM_FAULT_MAJOR:
396 tsk->maj_flt++;
397 break;
398 case VM_FAULT_SIGBUS:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200399 do_sigbus(regs, error_code, address);
400 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case VM_FAULT_OOM:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200402 if (do_out_of_memory(regs, error_code, address))
403 goto survive;
404 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 default:
406 BUG();
407 }
408
409 up_read(&mm->mmap_sem);
410 /*
411 * The instruction that caused the program check will
412 * be repeated. Don't signal single step via SIGTRAP.
413 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100414 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return;
416
417/*
418 * Something tried to access memory that isn't in our memory map..
419 * Fix it, but check if it's kernel or user first..
420 */
421bad_area:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200422 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200424 /* User mode accesses just cause a SIGSEGV */
425 if (regs->psw.mask & PSW_MASK_PSTATE) {
426 tsk->thread.prot_addr = address;
427 tsk->thread.trap_no = error_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 do_sigsegv(regs, error_code, si_code, address);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432no_context:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200433 do_no_context(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100436void __kprobes do_protection_exception(struct pt_regs *regs,
437 unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200439 /* Protection exception is supressing, decrement psw address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 regs->psw.addr -= (error_code >> 16);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200441 /*
442 * Check for low-address protection. This needs to be treated
443 * as a special case because the translation exception code
444 * field is not guaranteed to contain valid data in this case.
445 */
446 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
447 do_low_address(regs, error_code);
448 return;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 do_exception(regs, 4, 1);
451}
452
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100453void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 do_exception(regs, error_code & 0xff, 0);
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#ifdef CONFIG_PFAULT
459/*
460 * 'pfault' pseudo page faults routines.
461 */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100462static ext_int_info_t ext_int_pfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static int pfault_disable = 0;
464
465static int __init nopfault(char *str)
466{
467 pfault_disable = 1;
468 return 1;
469}
470
471__setup("nopfault", nopfault);
472
473typedef struct {
474 __u16 refdiagc;
475 __u16 reffcode;
476 __u16 refdwlen;
477 __u16 refversn;
478 __u64 refgaddr;
479 __u64 refselmk;
480 __u64 refcmpmk;
481 __u64 reserved;
482} __attribute__ ((packed)) pfault_refbk_t;
483
484int pfault_init(void)
485{
486 pfault_refbk_t refbk =
487 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
488 __PF_RES_FIELD };
489 int rc;
490
Heiko Carstens29b08d22006-12-04 15:40:40 +0100491 if (!MACHINE_IS_VM || pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -1;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200493 asm volatile(
494 " diag %1,%0,0x258\n"
495 "0: j 2f\n"
496 "1: la %0,8\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 "2:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200498 EX_TABLE(0b,1b)
499 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 __ctl_set_bit(0, 9);
501 return rc;
502}
503
504void pfault_fini(void)
505{
506 pfault_refbk_t refbk =
507 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
508
Heiko Carstens29b08d22006-12-04 15:40:40 +0100509 if (!MACHINE_IS_VM || pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return;
511 __ctl_clear_bit(0,9);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200512 asm volatile(
513 " diag %0,0,0x258\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 "0:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200515 EX_TABLE(0b,0b)
516 : : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100519static void pfault_interrupt(__u16 error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct task_struct *tsk;
522 __u16 subcode;
523
524 /*
525 * Get the external interruption subcode & pfault
526 * initial/completion signal bit. VM stores this
527 * in the 'cpu address' field associated with the
528 * external interrupt.
529 */
530 subcode = S390_lowcore.cpu_addr;
531 if ((subcode & 0xff00) != __SUBCODE_MASK)
532 return;
533
534 /*
535 * Get the token (= address of the task structure of the affected task).
536 */
537 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
538
539 if (subcode & 0x0080) {
540 /* signal bit is set -> a page has been swapped in by VM */
541 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
542 /* Initial interrupt was faster than the completion
543 * interrupt. pfault_wait is valid. Set pfault_wait
544 * back to zero and wake up the process. This can
545 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700546 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 tsk->thread.pfault_wait = 0;
548 wake_up_process(tsk);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700549 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 } else {
552 /* signal bit not set -> a real page is missing. */
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700553 get_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
555 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
556 /* Completion interrupt was faster than the initial
557 * interrupt (swapped in a -1 for pfault_wait). Set
558 * pfault_wait back to zero and exit. This can be
559 * done safely because tsk is running in kernel
560 * mode and can't produce new pfaults. */
561 tsk->thread.pfault_wait = 0;
562 set_task_state(tsk, TASK_RUNNING);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700563 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 } else
565 set_tsk_need_resched(tsk);
566 }
567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Heiko Carstens29b08d22006-12-04 15:40:40 +0100569void __init pfault_irq_init(void)
570{
571 if (!MACHINE_IS_VM)
572 return;
573
574 /*
575 * Try to get pfault pseudo page faults going.
576 */
577 if (register_early_external_interrupt(0x2603, pfault_interrupt,
578 &ext_int_pfault) != 0)
579 panic("Couldn't request external interrupt 0x2603");
580
581 if (pfault_init() == 0)
582 return;
583
584 /* Tough luck, no pfault. */
585 pfault_disable = 1;
586 unregister_early_external_interrupt(0x2603, pfault_interrupt,
587 &ext_int_pfault);
588}
589#endif