[S390] init task memory faults.
[linux-2.6/libata-dev.git] / arch / s390 / mm / fault.c
bloba393c308bb290d1a0827310653ed205312857f34
1 /*
2 * arch/s390/mm/fault.c
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)
9 * Derived from "arch/i386/mm/fault.c"
10 * Copyright (C) 1995 Linus Torvalds
13 #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>
28 #include <linux/kprobes.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/kdebug.h>
35 #ifndef CONFIG_64BIT
36 #define __FAIL_ADDR_MASK 0x7ffff000
37 #define __FIXUP_MASK 0x7fffffff
38 #define __SUBCODE_MASK 0x0200
39 #define __PF_RES_FIELD 0ULL
40 #else /* CONFIG_64BIT */
41 #define __FAIL_ADDR_MASK -4096L
42 #define __FIXUP_MASK ~0L
43 #define __SUBCODE_MASK 0x0600
44 #define __PF_RES_FIELD 0x8000000000000000ULL
45 #endif /* CONFIG_64BIT */
47 #ifdef CONFIG_SYSCTL
48 extern int sysctl_userprocess_debug;
49 #endif
51 extern void die(const char *,struct pt_regs *,long);
53 #ifdef CONFIG_KPROBES
54 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
55 int register_page_fault_notifier(struct notifier_block *nb)
57 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
60 int unregister_page_fault_notifier(struct notifier_block *nb)
62 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
65 static inline int notify_page_fault(enum die_val val, const char *str,
66 struct pt_regs *regs, long err, int trap, int sig)
68 struct die_args args = {
69 .regs = regs,
70 .str = str,
71 .err = err,
72 .trapnr = trap,
73 .signr = sig
75 return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
77 #else
78 static inline int notify_page_fault(enum die_val val, const char *str,
79 struct pt_regs *regs, long err, int trap, int sig)
81 return NOTIFY_DONE;
83 #endif
85 extern spinlock_t timerlist_lock;
88 * Unlock any spinlocks which will prevent us from getting the
89 * message out (timerlist_lock is acquired through the
90 * console unblank code)
92 void bust_spinlocks(int yes)
94 if (yes) {
95 oops_in_progress = 1;
96 } else {
97 int loglevel_save = console_loglevel;
98 console_unblank();
99 oops_in_progress = 0;
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...
105 console_loglevel = 15;
106 printk(" ");
107 console_loglevel = loglevel_save;
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.
116 static int __check_access_register(struct pt_regs *regs, int error_code)
118 int areg = S390_lowcore.exc_access_id;
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)
126 * access register contains 0 -> kernel address,
127 * access register contains 1 -> user space address
129 return current->thread.acrs[areg];
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;
138 * Check which address space the address belongs to.
139 * Returns 1 for user space and 0 for kernel space.
141 static inline int check_user_space(struct pt_regs *regs, int error_code)
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
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;
160 * Send SIGSEGV to task. This is an external routine
161 * to keep the stack usage of do_page_fault small.
163 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
164 int si_code, unsigned long address)
166 struct siginfo si;
168 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
169 #if defined(CONFIG_SYSCTL)
170 if (sysctl_userprocess_debug)
171 #endif
173 printk("User process fault: interruption code 0x%lX\n",
174 error_code);
175 printk("failing address: %lX\n", address);
176 show_regs(regs);
178 #endif
179 si.si_signo = SIGSEGV;
180 si.si_code = si_code;
181 si.si_addr = (void __user *) address;
182 force_sig_info(SIGSEGV, &si, current);
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.
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)
196 static inline void __kprobes
197 do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
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;
207 tsk = current;
208 mm = tsk->mm;
210 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
211 SIGSEGV) == NOTIFY_STOP)
212 return;
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.
219 if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
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;
229 /* Low-address protection hit in user mode 'cannot happen'. */
230 die ("Low-address protection", regs, error_code);
231 do_exit(SIGKILL);
235 * get the failing address
236 * more specific the segment and page table portion of
237 * the address
239 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
240 user_address = check_user_space(regs, error_code);
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.
247 if (user_address == 0 || in_atomic() || !mm)
248 goto no_context;
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
255 local_irq_enable();
257 down_read(&mm->mmap_sem);
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;
269 * Ok, we have a good vm_area for this memory access, so
270 * we can handle it..
272 good_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;
283 survive:
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.
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();
304 up_read(&mm->mmap_sem);
306 * The instruction that caused the program check will
307 * be repeated. Don't signal single step via SIGTRAP.
309 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
310 return;
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..
316 bad_area:
317 up_read(&mm->mmap_sem);
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;
327 no_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;
336 * Oops. The kernel tried to access some bad page. We'll have to
337 * terminate things with extreme prejudice.
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);
346 die("Oops", regs, error_code);
347 do_exit(SIGKILL);
351 * We ran out of memory, or some other thing happened to us that made
352 * us unable to handle the page fault gracefully.
354 out_of_memory:
355 up_read(&mm->mmap_sem);
356 if (tsk->pid == 1) {
357 yield();
358 down_read(&mm->mmap_sem);
359 goto survive;
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;
366 do_sigbus:
367 up_read(&mm->mmap_sem);
370 * Send a sigbus, regardless of whether we were in kernel
371 * or user mode.
373 tsk->thread.prot_addr = address;
374 tsk->thread.trap_no = error_code;
375 force_sig(SIGBUS, tsk);
377 /* Kernel mode? Handle exceptions or die */
378 if (!(regs->psw.mask & PSW_MASK_PSTATE))
379 goto no_context;
382 void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
384 regs->psw.addr -= (error_code >> 16);
385 do_exception(regs, 4, 1);
388 void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
390 do_exception(regs, error_code & 0xff, 0);
393 #ifdef CONFIG_PFAULT
395 * 'pfault' pseudo page faults routines.
397 static int pfault_disable = 0;
399 static int __init nopfault(char *str)
401 pfault_disable = 1;
402 return 1;
405 __setup("nopfault", nopfault);
407 typedef 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;
418 int pfault_init(void)
420 pfault_refbk_t refbk =
421 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
422 __PF_RES_FIELD };
423 int rc;
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"
434 #ifndef CONFIG_64BIT
435 " .long 0b,1b\n"
436 #else /* CONFIG_64BIT */
437 " .quad 0b,1b\n"
438 #endif /* CONFIG_64BIT */
439 ".previous"
440 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc" );
441 __ctl_set_bit(0, 9);
442 return rc;
445 void pfault_fini(void)
447 pfault_refbk_t refbk =
448 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
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"
458 #ifndef CONFIG_64BIT
459 " .long 0b,0b\n"
460 #else /* CONFIG_64BIT */
461 " .quad 0b,0b\n"
462 #endif /* CONFIG_64BIT */
463 ".previous"
464 : : "a" (&refbk), "m" (refbk) : "cc" );
467 asmlinkage void
468 pfault_interrupt(struct pt_regs *regs, __u16 error_code)
470 struct task_struct *tsk;
471 __u16 subcode;
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.
479 subcode = S390_lowcore.cpu_addr;
480 if ((subcode & 0xff00) != __SUBCODE_MASK)
481 return;
484 * Get the token (= address of the task structure of the affected task).
486 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
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
495 * and can't produce new pfaults. */
496 tsk->thread.pfault_wait = 0;
497 wake_up_process(tsk);
498 put_task_struct(tsk);
500 } else {
501 /* signal bit not set -> a real page is missing. */
502 get_task_struct(tsk);
503 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);
512 put_task_struct(tsk);
513 } else
514 set_tsk_need_resched(tsk);
517 #endif