blob: 8bc35183db5943a62ae6c4ff9b552b3059210c18 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/system.h>
31#include <asm/uaccess.h>
32#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
266static inline void do_sigreturn(struct mm_struct *mm, struct pt_regs *regs,
267 int rt)
268{
269 up_read(&mm->mmap_sem);
270 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
271#ifdef CONFIG_COMPAT
272 if (test_tsk_thread_flag(current, TIF_31BIT)) {
273 if (rt)
274 sys32_rt_sigreturn(regs);
275 else
276 sys32_sigreturn(regs);
277 return;
278 }
279#endif /* CONFIG_COMPAT */
280 if (rt)
281 sys_rt_sigreturn(regs);
282 else
283 sys_sigreturn(regs);
284 return;
285}
286
287static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
288 unsigned long address, unsigned long error_code)
289{
290 pgd_t *pgd;
291 pmd_t *pmd;
292 pte_t *pte;
293 u16 *instruction;
294 unsigned long pfn, uaddr = regs->psw.addr;
295
296 spin_lock(&mm->page_table_lock);
297 pgd = pgd_offset(mm, uaddr);
298 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
299 goto out_fault;
300 pmd = pmd_offset(pgd, uaddr);
301 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
302 goto out_fault;
303 pte = pte_offset_map(pmd_offset(pgd_offset(mm, uaddr), uaddr), uaddr);
304 if (!pte || !pte_present(*pte))
305 goto out_fault;
306 pfn = pte_pfn(*pte);
307 if (!pfn_valid(pfn))
308 goto out_fault;
309 spin_unlock(&mm->page_table_lock);
310
311 instruction = (u16 *) ((pfn << PAGE_SHIFT) + (uaddr & (PAGE_SIZE-1)));
312 if (*instruction == 0x0a77)
313 do_sigreturn(mm, regs, 0);
314 else if (*instruction == 0x0aad)
315 do_sigreturn(mm, regs, 1);
316 else {
317 printk("- XXX - do_exception: task = %s, primary, NO EXEC "
318 "-> SIGSEGV\n", current->comm);
319 up_read(&mm->mmap_sem);
320 current->thread.prot_addr = address;
321 current->thread.trap_no = error_code;
322 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
323 }
324 return 0;
325out_fault:
326 spin_unlock(&mm->page_table_lock);
327 return -EFAULT;
328}
329#endif /* CONFIG_S390_EXEC_PROTECT */
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
332 * This routine handles page faults. It determines the address,
333 * and the problem, and then passes it off to one of the appropriate
334 * routines.
335 *
336 * error_code:
337 * 04 Protection -> Write-Protection (suprression)
338 * 10 Segment translation -> Not present (nullification)
339 * 11 Page translation -> Not present (nullification)
340 * 3b Region third trans. -> Not present (nullification)
341 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100342static inline void
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200343do_exception(struct pt_regs *regs, unsigned long error_code, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200345 struct task_struct *tsk;
346 struct mm_struct *mm;
347 struct vm_area_struct *vma;
348 unsigned long address;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100349 int space;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200350 int si_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200352 if (notify_page_fault(regs, error_code) == NOTIFY_STOP)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200353 return;
354
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200355 tsk = current;
356 mm = tsk->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200358 /* get the failing address and the affected space */
359 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100360 space = check_space(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /*
363 * Verify that the fault happened in user space, that
364 * we are not in an interrupt and that there is a
365 * user context.
366 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100367 if (unlikely(space == 0 || in_atomic() || !mm))
368 goto no_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /*
371 * When we get here, the fault happened in the current
372 * task's user address space, so we can switch on the
373 * interrupts again and then search the VMAs
374 */
375 local_irq_enable();
376
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200377 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100379 si_code = SEGV_MAPERR;
380 vma = find_vma(mm, address);
381 if (!vma)
382 goto bad_area;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100383
384#ifdef CONFIG_S390_EXEC_PROTECT
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100385 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100386 if (!signal_return(mm, regs, address, error_code))
387 /*
388 * signal_return() has done an up_read(&mm->mmap_sem)
389 * if it returns 0.
390 */
391 return;
392#endif
393
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200394 if (vma->vm_start <= address)
395 goto good_area;
396 if (!(vma->vm_flags & VM_GROWSDOWN))
397 goto bad_area;
398 if (expand_stack(vma, address))
399 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
401 * Ok, we have a good vm_area for this memory access, so
402 * we can handle it..
403 */
404good_area:
405 si_code = SEGV_ACCERR;
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200406 if (!write) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* page not present, check vm flags */
408 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
409 goto bad_area;
410 } else {
411 if (!(vma->vm_flags & VM_WRITE))
412 goto bad_area;
413 }
414
415survive:
416 /*
417 * If for any reason at all we couldn't handle the fault,
418 * make sure we exit gracefully rather than endlessly redo
419 * the fault.
420 */
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200421 switch (handle_mm_fault(mm, vma, address, write)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 case VM_FAULT_MINOR:
423 tsk->min_flt++;
424 break;
425 case VM_FAULT_MAJOR:
426 tsk->maj_flt++;
427 break;
428 case VM_FAULT_SIGBUS:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200429 do_sigbus(regs, error_code, address);
430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 case VM_FAULT_OOM:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200432 if (do_out_of_memory(regs, error_code, address))
433 goto survive;
434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 default:
436 BUG();
437 }
438
439 up_read(&mm->mmap_sem);
440 /*
441 * The instruction that caused the program check will
442 * be repeated. Don't signal single step via SIGTRAP.
443 */
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100444 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return;
446
447/*
448 * Something tried to access memory that isn't in our memory map..
449 * Fix it, but check if it's kernel or user first..
450 */
451bad_area:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200452 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200454 /* User mode accesses just cause a SIGSEGV */
455 if (regs->psw.mask & PSW_MASK_PSTATE) {
456 tsk->thread.prot_addr = address;
457 tsk->thread.trap_no = error_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 do_sigsegv(regs, error_code, si_code, address);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200459 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462no_context:
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200463 do_no_context(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100466void __kprobes do_protection_exception(struct pt_regs *regs,
467 unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200469 /* Protection exception is supressing, decrement psw address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 regs->psw.addr -= (error_code >> 16);
Martin Schwidefsky10c10312007-04-27 16:01:43 +0200471 /*
472 * Check for low-address protection. This needs to be treated
473 * as a special case because the translation exception code
474 * field is not guaranteed to contain valid data in this case.
475 */
476 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
477 do_low_address(regs, error_code);
478 return;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 do_exception(regs, 4, 1);
481}
482
Gerald Schaefer482b05d2007-03-05 23:35:54 +0100483void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 do_exception(regs, error_code & 0xff, 0);
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#ifdef CONFIG_PFAULT
489/*
490 * 'pfault' pseudo page faults routines.
491 */
Heiko Carstens29b08d22006-12-04 15:40:40 +0100492static ext_int_info_t ext_int_pfault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int pfault_disable = 0;
494
495static int __init nopfault(char *str)
496{
497 pfault_disable = 1;
498 return 1;
499}
500
501__setup("nopfault", nopfault);
502
503typedef struct {
504 __u16 refdiagc;
505 __u16 reffcode;
506 __u16 refdwlen;
507 __u16 refversn;
508 __u64 refgaddr;
509 __u64 refselmk;
510 __u64 refcmpmk;
511 __u64 reserved;
512} __attribute__ ((packed)) pfault_refbk_t;
513
514int pfault_init(void)
515{
516 pfault_refbk_t refbk =
517 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
518 __PF_RES_FIELD };
519 int rc;
520
Heiko Carstens29b08d22006-12-04 15:40:40 +0100521 if (!MACHINE_IS_VM || pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -1;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200523 asm volatile(
524 " diag %1,%0,0x258\n"
525 "0: j 2f\n"
526 "1: la %0,8\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 "2:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200528 EX_TABLE(0b,1b)
529 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 __ctl_set_bit(0, 9);
531 return rc;
532}
533
534void pfault_fini(void)
535{
536 pfault_refbk_t refbk =
537 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
538
Heiko Carstens29b08d22006-12-04 15:40:40 +0100539 if (!MACHINE_IS_VM || pfault_disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return;
541 __ctl_clear_bit(0,9);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200542 asm volatile(
543 " diag %0,0,0x258\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 "0:\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200545 EX_TABLE(0b,0b)
546 : : "a" (&refbk), "m" (refbk) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100549static void pfault_interrupt(__u16 error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct task_struct *tsk;
552 __u16 subcode;
553
554 /*
555 * Get the external interruption subcode & pfault
556 * initial/completion signal bit. VM stores this
557 * in the 'cpu address' field associated with the
558 * external interrupt.
559 */
560 subcode = S390_lowcore.cpu_addr;
561 if ((subcode & 0xff00) != __SUBCODE_MASK)
562 return;
563
564 /*
565 * Get the token (= address of the task structure of the affected task).
566 */
567 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
568
569 if (subcode & 0x0080) {
570 /* signal bit is set -> a page has been swapped in by VM */
571 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
572 /* Initial interrupt was faster than the completion
573 * interrupt. pfault_wait is valid. Set pfault_wait
574 * back to zero and wake up the process. This can
575 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700576 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 tsk->thread.pfault_wait = 0;
578 wake_up_process(tsk);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700579 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 } else {
582 /* signal bit not set -> a real page is missing. */
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700583 get_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
585 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
586 /* Completion interrupt was faster than the initial
587 * interrupt (swapped in a -1 for pfault_wait). Set
588 * pfault_wait back to zero and exit. This can be
589 * done safely because tsk is running in kernel
590 * mode and can't produce new pfaults. */
591 tsk->thread.pfault_wait = 0;
592 set_task_state(tsk, TASK_RUNNING);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700593 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else
595 set_tsk_need_resched(tsk);
596 }
597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Heiko Carstens29b08d22006-12-04 15:40:40 +0100599void __init pfault_irq_init(void)
600{
601 if (!MACHINE_IS_VM)
602 return;
603
604 /*
605 * Try to get pfault pseudo page faults going.
606 */
607 if (register_early_external_interrupt(0x2603, pfault_interrupt,
608 &ext_int_pfault) != 0)
609 panic("Couldn't request external interrupt 0x2603");
610
611 if (pfault_init() == 0)
612 return;
613
614 /* Tough luck, no pfault. */
615 pfault_disable = 1;
616 unregister_early_external_interrupt(0x2603, pfault_interrupt,
617 &ext_int_pfault);
618}
619#endif