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>
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>
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 */
48 extern int sysctl_userprocess_debug;
51 extern void die(const char *,struct pt_regs *,long);
54 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
55 int register_page_fault_notifier(struct notifier_block *nb)
57 return atomic_notifier_chain_register(¬ify_page_fault_chain, nb);
60 int unregister_page_fault_notifier(struct notifier_block *nb)
62 return atomic_notifier_chain_unregister(¬ify_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 = {
75 return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args);
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)
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)
97 int loglevel_save = console_loglevel;
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;
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;
121 /* Access via access register 0 -> kernel address */
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);
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);
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)
168 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
169 #if defined(CONFIG_SYSCTL)
170 if (sysctl_userprocess_debug)
173 printk("User process fault: interruption code 0x%lX\n",
175 printk("failing address: %lX\n", address);
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
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;
204 const struct exception_table_entry *fixup;
205 int si_code = SEGV_MAPERR;
210 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
211 SIGSEGV) == NOTIFY_STOP)
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)) {
229 /* Low-address protection hit in user mode 'cannot happen'. */
230 die ("Low-address protection", regs, error_code);
235 * get the failing address
236 * more specific the segment and page table portion of
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
247 if (user_address == 0 || in_atomic() || !mm)
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
257 down_read(&mm->mmap_sem);
259 vma = find_vma(mm, address);
262 if (vma->vm_start <= address)
264 if (!(vma->vm_flags & VM_GROWSDOWN))
266 if (expand_stack(vma, address))
269 * Ok, we have a good vm_area for this memory access, so
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)))
279 if (!(vma->vm_flags & VM_WRITE))
285 * If for any reason at all we couldn't handle the fault,
286 * make sure we exit gracefully rather than endlessly redo
289 switch (handle_mm_fault(mm, vma, address, is_protection)) {
296 case VM_FAULT_SIGBUS:
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);
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..
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);
328 /* Are we prepared to handle this kernel fault? */
329 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
331 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
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);
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);
351 * We ran out of memory, or some other thing happened to us that made
352 * us unable to handle the page fault gracefully.
355 up_read(&mm->mmap_sem);
360 printk("VM: killing process %s\n", tsk->comm);
361 if (regs->psw.mask & PSW_MASK_PSTATE)
366 up_read(&mm->mmap_sem);
369 * Send a sigbus, regardless of whether we were in kernel
372 tsk->thread.prot_addr = address;
373 tsk->thread.trap_no = error_code;
374 force_sig(SIGBUS, tsk);
376 /* Kernel mode? Handle exceptions or die */
377 if (!(regs->psw.mask & PSW_MASK_PSTATE))
381 void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
383 regs->psw.addr -= (error_code >> 16);
384 do_exception(regs, 4, 1);
387 void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
389 do_exception(regs, error_code & 0xff, 0);
394 * 'pfault' pseudo page faults routines.
396 static int pfault_disable = 0;
398 static int __init nopfault(char *str)
404 __setup("nopfault", nopfault);
415 } __attribute__ ((packed)) pfault_refbk_t;
417 int pfault_init(void)
419 pfault_refbk_t refbk =
420 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
426 __asm__ __volatile__(
427 " diag %1,%0,0x258\n"
431 ".section __ex_table,\"a\"\n"
435 #else /* CONFIG_64BIT */
437 #endif /* CONFIG_64BIT */
439 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc" );
444 void pfault_fini(void)
446 pfault_refbk_t refbk =
447 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
451 __ctl_clear_bit(0,9);
452 __asm__ __volatile__(
455 ".section __ex_table,\"a\"\n"
459 #else /* CONFIG_64BIT */
461 #endif /* CONFIG_64BIT */
463 : : "a" (&refbk), "m" (refbk) : "cc" );
467 pfault_interrupt(struct pt_regs *regs, __u16 error_code)
469 struct task_struct *tsk;
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.
478 subcode = S390_lowcore.cpu_addr;
479 if ((subcode & 0xff00) != __SUBCODE_MASK)
483 * Get the token (= address of the task structure of the affected task).
485 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
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
494 * and can't produce new pfaults. */
495 tsk->thread.pfault_wait = 0;
496 wake_up_process(tsk);
497 put_task_struct(tsk);
500 /* signal bit not set -> a real page is missing. */
501 get_task_struct(tsk);
502 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);
511 put_task_struct(tsk);
513 set_tsk_need_resched(tsk);