blob: 44f0cda7e72e44bb9ae47e6fc42fd284ceb430ab [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();
358 goto survive;
359 }
360 printk("VM: killing process %s\n", tsk->comm);
361 if (regs->psw.mask & PSW_MASK_PSTATE)
362 do_exit(SIGKILL);
363 goto no_context;
364
365do_sigbus:
366 up_read(&mm->mmap_sem);
367
368 /*
369 * Send a sigbus, regardless of whether we were in kernel
370 * or user mode.
371 */
372 tsk->thread.prot_addr = address;
373 tsk->thread.trap_no = error_code;
374 force_sig(SIGBUS, tsk);
375
376 /* Kernel mode? Handle exceptions or die */
377 if (!(regs->psw.mask & PSW_MASK_PSTATE))
378 goto no_context;
379}
380
381void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
382{
383 regs->psw.addr -= (error_code >> 16);
384 do_exception(regs, 4, 1);
385}
386
387void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
388{
389 do_exception(regs, error_code & 0xff, 0);
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#ifdef CONFIG_PFAULT
393/*
394 * 'pfault' pseudo page faults routines.
395 */
396static int pfault_disable = 0;
397
398static int __init nopfault(char *str)
399{
400 pfault_disable = 1;
401 return 1;
402}
403
404__setup("nopfault", nopfault);
405
406typedef struct {
407 __u16 refdiagc;
408 __u16 reffcode;
409 __u16 refdwlen;
410 __u16 refversn;
411 __u64 refgaddr;
412 __u64 refselmk;
413 __u64 refcmpmk;
414 __u64 reserved;
415} __attribute__ ((packed)) pfault_refbk_t;
416
417int pfault_init(void)
418{
419 pfault_refbk_t refbk =
420 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
421 __PF_RES_FIELD };
422 int rc;
423
424 if (pfault_disable)
425 return -1;
426 __asm__ __volatile__(
427 " diag %1,%0,0x258\n"
428 "0: j 2f\n"
429 "1: la %0,8\n"
430 "2:\n"
431 ".section __ex_table,\"a\"\n"
432 " .align 4\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800433#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 " .long 0b,1b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800435#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 " .quad 0b,1b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800437#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ".previous"
Martin Schwidefskyd4b68992005-11-07 00:59:06 -0800439 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 __ctl_set_bit(0, 9);
441 return rc;
442}
443
444void pfault_fini(void)
445{
446 pfault_refbk_t refbk =
447 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
448
449 if (pfault_disable)
450 return;
451 __ctl_clear_bit(0,9);
452 __asm__ __volatile__(
453 " diag %0,0,0x258\n"
454 "0:\n"
455 ".section __ex_table,\"a\"\n"
456 " .align 4\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800457#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 " .long 0b,0b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800459#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 " .quad 0b,0b\n"
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800461#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ".previous"
Martin Schwidefskyd4b68992005-11-07 00:59:06 -0800463 : : "a" (&refbk), "m" (refbk) : "cc" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466asmlinkage void
467pfault_interrupt(struct pt_regs *regs, __u16 error_code)
468{
469 struct task_struct *tsk;
470 __u16 subcode;
471
472 /*
473 * Get the external interruption subcode & pfault
474 * initial/completion signal bit. VM stores this
475 * in the 'cpu address' field associated with the
476 * external interrupt.
477 */
478 subcode = S390_lowcore.cpu_addr;
479 if ((subcode & 0xff00) != __SUBCODE_MASK)
480 return;
481
482 /*
483 * Get the token (= address of the task structure of the affected task).
484 */
485 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
486
487 if (subcode & 0x0080) {
488 /* signal bit is set -> a page has been swapped in by VM */
489 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
490 /* Initial interrupt was faster than the completion
491 * interrupt. pfault_wait is valid. Set pfault_wait
492 * back to zero and wake up the process. This can
493 * safely be done because the task is still sleeping
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700494 * and can't produce new pfaults. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 tsk->thread.pfault_wait = 0;
496 wake_up_process(tsk);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700497 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 } else {
500 /* signal bit not set -> a real page is missing. */
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700501 get_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
503 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
504 /* Completion interrupt was faster than the initial
505 * interrupt (swapped in a -1 for pfault_wait). Set
506 * pfault_wait back to zero and exit. This can be
507 * done safely because tsk is running in kernel
508 * mode and can't produce new pfaults. */
509 tsk->thread.pfault_wait = 0;
510 set_task_state(tsk, TASK_RUNNING);
Martin Schwidefskyb6d09442005-09-03 15:58:02 -0700511 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else
513 set_tsk_need_resched(tsk);
514 }
515}
516#endif
517