blob: a393c308bb290d1a0827310653ed205312857f34 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080035#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define __FAIL_ADDR_MASK 0x7ffff000
37#define __FIXUP_MASK 0x7fffffff
38#define __SUBCODE_MASK 0x0200
39#define __PF_RES_FIELD 0ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080040#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define __FAIL_ADDR_MASK -4096L
42#define __FIXUP_MASK ~0L
43#define __SUBCODE_MASK 0x0600
44#define __PF_RES_FIELD 0x8000000000000000ULL
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080045#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef CONFIG_SYSCTL
48extern int sysctl_userprocess_debug;
49#endif
50
51extern void die(const char *,struct pt_regs *,long);
52
Michael Grundy4ba069b2006-09-20 15:58:39 +020053#ifdef CONFIG_KPROBES
54ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
55int register_page_fault_notifier(struct notifier_block *nb)
56{
57 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
58}
59
60int unregister_page_fault_notifier(struct notifier_block *nb)
61{
62 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
63}
64
65static inline int notify_page_fault(enum die_val val, const char *str,
66 struct pt_regs *regs, long err, int trap, int sig)
67{
68 struct die_args args = {
69 .regs = regs,
70 .str = str,
71 .err = err,
72 .trapnr = trap,
73 .signr = sig
74 };
75 return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
76}
77#else
78static inline int notify_page_fault(enum die_val val, const char *str,
79 struct pt_regs *regs, long err, int trap, int sig)
80{
81 return NOTIFY_DONE;
82}
83#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085extern spinlock_t timerlist_lock;
86
87/*
88 * Unlock any spinlocks which will prevent us from getting the
89 * message out (timerlist_lock is acquired through the
90 * console unblank code)
91 */
92void bust_spinlocks(int yes)
93{
94 if (yes) {
95 oops_in_progress = 1;
96 } else {
97 int loglevel_save = console_loglevel;
98 console_unblank();
99 oops_in_progress = 0;
100 /*
101 * OK, the message is on the console. Now we call printk()
102 * without oops_in_progress set so that printk will give klogd
103 * a poke. Hold onto your hats...
104 */
105 console_loglevel = 15;
106 printk(" ");
107 console_loglevel = loglevel_save;
108 }
109}
110
111/*
112 * Check which address space is addressed by the access
113 * register in S390_lowcore.exc_access_id.
114 * Returns 1 for user space and 0 for kernel space.
115 */
116static int __check_access_register(struct pt_regs *regs, int error_code)
117{
118 int areg = S390_lowcore.exc_access_id;
119
120 if (areg == 0)
121 /* Access via access register 0 -> kernel address */
122 return 0;
123 save_access_regs(current->thread.acrs);
124 if (regs && areg < NUM_ACRS && current->thread.acrs[areg] <= 1)
125 /*
126 * access register contains 0 -> kernel address,
127 * access register contains 1 -> user space address
128 */
129 return current->thread.acrs[areg];
130
131 /* Something unhealthy was done with the access registers... */
132 die("page fault via unknown access register", regs, error_code);
133 do_exit(SIGKILL);
134 return 0;
135}
136
137/*
138 * Check which address space the address belongs to.
139 * Returns 1 for user space and 0 for kernel space.
140 */
141static inline int check_user_space(struct pt_regs *regs, int error_code)
142{
143 /*
144 * The lowest two bits of S390_lowcore.trans_exc_code indicate
145 * which paging table was used:
146 * 0: Primary Segment Table Descriptor
147 * 1: STD determined via access register
148 * 2: Secondary Segment Table Descriptor
149 * 3: Home Segment Table Descriptor
150 */
151 int descriptor = S390_lowcore.trans_exc_code & 3;
152 if (unlikely(descriptor == 1))
153 return __check_access_register(regs, error_code);
154 if (descriptor == 2)
155 return current->thread.mm_segment.ar4;
156 return descriptor != 0;
157}
158
159/*
160 * Send SIGSEGV to task. This is an external routine
161 * to keep the stack usage of do_page_fault small.
162 */
163static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
164 int si_code, unsigned long address)
165{
166 struct siginfo si;
167
168#if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
169#if defined(CONFIG_SYSCTL)
170 if (sysctl_userprocess_debug)
171#endif
172 {
173 printk("User process fault: interruption code 0x%lX\n",
174 error_code);
175 printk("failing address: %lX\n", address);
176 show_regs(regs);
177 }
178#endif
179 si.si_signo = SIGSEGV;
180 si.si_code = si_code;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200181 si.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 force_sig_info(SIGSEGV, &si, current);
183}
184
185/*
186 * This routine handles page faults. It determines the address,
187 * and the problem, and then passes it off to one of the appropriate
188 * routines.
189 *
190 * error_code:
191 * 04 Protection -> Write-Protection (suprression)
192 * 10 Segment translation -> Not present (nullification)
193 * 11 Page translation -> Not present (nullification)
194 * 3b Region third trans. -> Not present (nullification)
195 */
Michael Grundy4ba069b2006-09-20 15:58:39 +0200196static inline void __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
198{
199 struct task_struct *tsk;
200 struct mm_struct *mm;
201 struct vm_area_struct * vma;
202 unsigned long address;
203 int user_address;
204 const struct exception_table_entry *fixup;
205 int si_code = SEGV_MAPERR;
206
207 tsk = current;
208 mm = tsk->mm;
209
Michael Grundy4ba069b2006-09-20 15:58:39 +0200210 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
211 SIGSEGV) == NOTIFY_STOP)
212 return;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
215 * Check for low-address protection. This needs to be treated
216 * as a special case because the translation exception code
217 * field is not guaranteed to contain valid data in this case.
218 */
219 if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
220
221 /* Low-address protection hit in kernel mode means
222 NULL pointer write access in kernel mode. */
223 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
224 address = 0;
225 user_address = 0;
226 goto no_context;
227 }
228
229 /* Low-address protection hit in user mode 'cannot happen'. */
230 die ("Low-address protection", regs, error_code);
231 do_exit(SIGKILL);
232 }
233
234 /*
235 * get the failing address
236 * more specific the segment and page table portion of
237 * the address
238 */
239 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
240 user_address = check_user_space(regs, error_code);
241
242 /*
243 * Verify that the fault happened in user space, that
244 * we are not in an interrupt and that there is a
245 * user context.
246 */
Martin Schwidefsky595bf2a2005-06-04 15:43:32 -0700247 if (user_address == 0 || in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto no_context;
249
250 /*
251 * When we get here, the fault happened in the current
252 * task's user address space, so we can switch on the
253 * interrupts again and then search the VMAs
254 */
255 local_irq_enable();
256
257 down_read(&mm->mmap_sem);
258
259 vma = find_vma(mm, address);
260 if (!vma)
261 goto bad_area;
262 if (vma->vm_start <= address)
263 goto good_area;
264 if (!(vma->vm_flags & VM_GROWSDOWN))
265 goto bad_area;
266 if (expand_stack(vma, address))
267 goto bad_area;
268/*
269 * Ok, we have a good vm_area for this memory access, so
270 * we can handle it..
271 */
272good_area:
273 si_code = SEGV_ACCERR;
274 if (!is_protection) {
275 /* page not present, check vm flags */
276 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
277 goto bad_area;
278 } else {
279 if (!(vma->vm_flags & VM_WRITE))
280 goto bad_area;
281 }
282
283survive:
284 /*
285 * If for any reason at all we couldn't handle the fault,
286 * make sure we exit gracefully rather than endlessly redo
287 * the fault.
288 */
289 switch (handle_mm_fault(mm, vma, address, is_protection)) {
290 case VM_FAULT_MINOR:
291 tsk->min_flt++;
292 break;
293 case VM_FAULT_MAJOR:
294 tsk->maj_flt++;
295 break;
296 case VM_FAULT_SIGBUS:
297 goto do_sigbus;
298 case VM_FAULT_OOM:
299 goto out_of_memory;
300 default:
301 BUG();
302 }
303
304 up_read(&mm->mmap_sem);
305 /*
306 * The instruction that caused the program check will
307 * be repeated. Don't signal single step via SIGTRAP.
308 */
309 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
310 return;
311
312/*
313 * Something tried to access memory that isn't in our memory map..
314 * Fix it, but check if it's kernel or user first..
315 */
316bad_area:
317 up_read(&mm->mmap_sem);
318
319 /* User mode accesses just cause a SIGSEGV */
320 if (regs->psw.mask & PSW_MASK_PSTATE) {
321 tsk->thread.prot_addr = address;
322 tsk->thread.trap_no = error_code;
323 do_sigsegv(regs, error_code, si_code, address);
324 return;
325 }
326
327no_context:
328 /* Are we prepared to handle this kernel fault? */
329 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
330 if (fixup) {
331 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
332 return;
333 }
334
335/*
336 * Oops. The kernel tried to access some bad page. We'll have to
337 * terminate things with extreme prejudice.
338 */
339 if (user_address == 0)
340 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
341 " at virtual kernel address %p\n", (void *)address);
342 else
343 printk(KERN_ALERT "Unable to handle kernel paging request"
344 " at virtual user address %p\n", (void *)address);
345
346 die("Oops", regs, error_code);
347 do_exit(SIGKILL);
348
349
350/*
351 * We ran out of memory, or some other thing happened to us that made
352 * us unable to handle the page fault gracefully.
353*/
354out_of_memory:
355 up_read(&mm->mmap_sem);
356 if (tsk->pid == 1) {
357 yield();
Akinobu Mitabac9c662006-09-28 16:55:18 +0200358 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto survive;
360 }
361 printk("VM: killing process %s\n", tsk->comm);
362 if (regs->psw.mask & PSW_MASK_PSTATE)
363 do_exit(SIGKILL);
364 goto no_context;
365
366do_sigbus:
367 up_read(&mm->mmap_sem);
368
369 /*
370 * Send a sigbus, regardless of whether we were in kernel
371 * or user mode.
372 */
373 tsk->thread.prot_addr = address;
374 tsk->thread.trap_no = error_code;
375 force_sig(SIGBUS, tsk);
376
377 /* Kernel mode? Handle exceptions or die */
378 if (!(regs->psw.mask & PSW_MASK_PSTATE))
379 goto no_context;
380}
381
382void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
383{
384 regs->psw.addr -= (error_code >> 16);
385 do_exception(regs, 4, 1);
386}
387
388void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
389{
390 do_exception(regs, error_code & 0xff, 0);
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#ifdef CONFIG_PFAULT
394/*
395 * 'pfault' pseudo page faults routines.
396 */
397static int pfault_disable = 0;
398
399static int __init nopfault(char *str)
400{
401 pfault_disable = 1;
402 return 1;
403}
404
405__setup("nopfault", nopfault);
406
407typedef struct {
408 __u16 refdiagc;
409 __u16 reffcode;
410 __u16 refdwlen;
411 __u16 refversn;
412 __u64 refgaddr;
413 __u64 refselmk;
414 __u64 refcmpmk;
415 __u64 reserved;
416} __attribute__ ((packed)) pfault_refbk_t;
417
418int pfault_init(void)
419{
420 pfault_refbk_t refbk =
421 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
422 __PF_RES_FIELD };
423 int rc;
424
425 if (pfault_disable)
426 return -1;
427 __asm__ __volatile__(
428 " diag %1,%0,0x258\n"
429 "0: j 2f\n"
430 "1: la %0,8\n"
431 "2:\n"
432 ".section __ex_table,\"a\"\n"
433 " .align 4\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800434#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 " .long 0b,1b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800436#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 " .quad 0b,1b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800438#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ".previous"
Martin Schwidefskyd4b68992005-11-07 00:59:06 -0800440 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 __ctl_set_bit(0, 9);
442 return rc;
443}
444
445void pfault_fini(void)
446{
447 pfault_refbk_t refbk =
448 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
449
450 if (pfault_disable)
451 return;
452 __ctl_clear_bit(0,9);
453 __asm__ __volatile__(
454 " diag %0,0,0x258\n"
455 "0:\n"
456 ".section __ex_table,\"a\"\n"
457 " .align 4\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800458#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 " .long 0b,0b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800460#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 " .quad 0b,0b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800462#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ".previous"
Martin Schwidefskyd4b68992005-11-07 00:59:06 -0800464 : : "a" (&refbk), "m" (refbk) : "cc" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467asmlinkage void
468pfault_interrupt(struct pt_regs *regs, __u16 error_code)
469{
470 struct task_struct *tsk;
471 __u16 subcode;
472
473 /*
474 * Get the external interruption subcode & pfault
475 * initial/completion signal bit. VM stores this
476 * in the 'cpu address' field associated with the
477 * external interrupt.
478 */
479 subcode = S390_lowcore.cpu_addr;
480 if ((subcode & 0xff00) != __SUBCODE_MASK)
481 return;
482
483 /*
484 * Get the token (= address of the task structure of the affected task).
485 */
486 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
487
488 if (subcode & 0x0080) {
489 /* signal bit is set -> a page has been swapped in by VM */
490 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
491 /* Initial interrupt was faster than the completion
492 * interrupt. pfault_wait is valid. Set pfault_wait
493 * back to zero and wake up the process. This can
494 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700495 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 tsk->thread.pfault_wait = 0;
497 wake_up_process(tsk);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700498 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 } else {
501 /* signal bit not set -> a real page is missing. */
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700502 get_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
504 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
505 /* Completion interrupt was faster than the initial
506 * interrupt (swapped in a -1 for pfault_wait). Set
507 * pfault_wait back to zero and exit. This can be
508 * done safely because tsk is running in kernel
509 * mode and can't produce new pfaults. */
510 tsk->thread.pfault_wait = 0;
511 set_task_state(tsk, TASK_RUNNING);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700512 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else
514 set_tsk_need_resched(tsk);
515 }
516}
517#endif
518