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>
34 #include <asm/s390_ext.h>
37 #define __FAIL_ADDR_MASK 0x7ffff000
38 #define __FIXUP_MASK 0x7fffffff
39 #define __SUBCODE_MASK 0x0200
40 #define __PF_RES_FIELD 0ULL
41 #else /* CONFIG_64BIT */
42 #define __FAIL_ADDR_MASK -4096L
43 #define __FIXUP_MASK ~0L
44 #define __SUBCODE_MASK 0x0600
45 #define __PF_RES_FIELD 0x8000000000000000ULL
46 #endif /* CONFIG_64BIT */
49 extern int sysctl_userprocess_debug;
52 extern void die(const char *,struct pt_regs *,long);
55 static ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
56 int register_page_fault_notifier(struct notifier_block *nb)
58 return atomic_notifier_chain_register(¬ify_page_fault_chain, nb);
61 int unregister_page_fault_notifier(struct notifier_block *nb)
63 return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb);
66 static inline int notify_page_fault(enum die_val val, const char *str,
67 struct pt_regs *regs, long err, int trap, int sig)
69 struct die_args args = {
76 return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args);
79 static inline int notify_page_fault(enum die_val val, const char *str,
80 struct pt_regs *regs, long err, int trap, int sig)
88 * Unlock any spinlocks which will prevent us from getting the
91 void bust_spinlocks(int yes)
96 int loglevel_save = console_loglevel;
100 * OK, the message is on the console. Now we call printk()
101 * without oops_in_progress set so that printk will give klogd
102 * a poke. Hold onto your hats...
104 console_loglevel = 15;
106 console_loglevel = loglevel_save;
111 * Returns the address space associated with the fault.
112 * Returns 0 for kernel space, 1 for user space and
113 * 2 for code execution in user space with noexec=on.
115 static inline int check_space(struct task_struct *tsk)
118 * The lowest two bits of S390_lowcore.trans_exc_code
119 * indicate which paging table was used.
121 int desc = S390_lowcore.trans_exc_code & 3;
123 if (desc == 3) /* Home Segment Table Descriptor */
124 return switch_amode == 0;
125 if (desc == 2) /* Secondary Segment Table Descriptor */
126 return tsk->thread.mm_segment.ar4;
127 #ifdef CONFIG_S390_SWITCH_AMODE
128 if (unlikely(desc == 1)) { /* STD determined via access register */
129 /* %a0 always indicates primary space. */
130 if (S390_lowcore.exc_access_id != 0) {
131 save_access_regs(tsk->thread.acrs);
133 * An alet of 0 indicates primary space.
134 * An alet of 1 indicates secondary space.
135 * Any other alet values generate an
136 * alen-translation exception.
138 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
139 return tsk->thread.mm_segment.ar4;
143 /* Primary Segment Table Descriptor */
144 return switch_amode << s390_noexec;
148 * Send SIGSEGV to task. This is an external routine
149 * to keep the stack usage of do_page_fault small.
151 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
152 int si_code, unsigned long address)
156 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
157 #if defined(CONFIG_SYSCTL)
158 if (sysctl_userprocess_debug)
161 printk("User process fault: interruption code 0x%lX\n",
163 printk("failing address: %lX\n", address);
167 si.si_signo = SIGSEGV;
168 si.si_code = si_code;
169 si.si_addr = (void __user *) address;
170 force_sig_info(SIGSEGV, &si, current);
173 #ifdef CONFIG_S390_EXEC_PROTECT
174 extern long sys_sigreturn(struct pt_regs *regs);
175 extern long sys_rt_sigreturn(struct pt_regs *regs);
176 extern long sys32_sigreturn(struct pt_regs *regs);
177 extern long sys32_rt_sigreturn(struct pt_regs *regs);
179 static inline void do_sigreturn(struct mm_struct *mm, struct pt_regs *regs,
182 up_read(&mm->mmap_sem);
183 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
185 if (test_tsk_thread_flag(current, TIF_31BIT)) {
187 sys32_rt_sigreturn(regs);
189 sys32_sigreturn(regs);
192 #endif /* CONFIG_COMPAT */
194 sys_rt_sigreturn(regs);
200 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
201 unsigned long address, unsigned long error_code)
207 unsigned long pfn, uaddr = regs->psw.addr;
209 spin_lock(&mm->page_table_lock);
210 pgd = pgd_offset(mm, uaddr);
211 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
213 pmd = pmd_offset(pgd, uaddr);
214 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
216 pte = pte_offset_map(pmd_offset(pgd_offset(mm, uaddr), uaddr), uaddr);
217 if (!pte || !pte_present(*pte))
222 spin_unlock(&mm->page_table_lock);
224 instruction = (u16 *) ((pfn << PAGE_SHIFT) + (uaddr & (PAGE_SIZE-1)));
225 if (*instruction == 0x0a77)
226 do_sigreturn(mm, regs, 0);
227 else if (*instruction == 0x0aad)
228 do_sigreturn(mm, regs, 1);
230 printk("- XXX - do_exception: task = %s, primary, NO EXEC "
231 "-> SIGSEGV\n", current->comm);
232 up_read(&mm->mmap_sem);
233 current->thread.prot_addr = address;
234 current->thread.trap_no = error_code;
235 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
239 spin_unlock(&mm->page_table_lock);
242 #endif /* CONFIG_S390_EXEC_PROTECT */
245 * This routine handles page faults. It determines the address,
246 * and the problem, and then passes it off to one of the appropriate
250 * 04 Protection -> Write-Protection (suprression)
251 * 10 Segment translation -> Not present (nullification)
252 * 11 Page translation -> Not present (nullification)
253 * 3b Region third trans. -> Not present (nullification)
256 do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
258 struct task_struct *tsk;
259 struct mm_struct *mm;
260 struct vm_area_struct * vma;
261 unsigned long address;
262 const struct exception_table_entry *fixup;
269 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
270 SIGSEGV) == NOTIFY_STOP)
274 * Check for low-address protection. This needs to be treated
275 * as a special case because the translation exception code
276 * field is not guaranteed to contain valid data in this case.
278 if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
280 /* Low-address protection hit in kernel mode means
281 NULL pointer write access in kernel mode. */
282 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
288 /* Low-address protection hit in user mode 'cannot happen'. */
289 die ("Low-address protection", regs, error_code);
294 * get the failing address
295 * more specific the segment and page table portion of
298 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
299 space = check_space(tsk);
302 * Verify that the fault happened in user space, that
303 * we are not in an interrupt and that there is a
306 if (unlikely(space == 0 || in_atomic() || !mm))
310 * When we get here, the fault happened in the current
311 * task's user address space, so we can switch on the
312 * interrupts again and then search the VMAs
316 down_read(&mm->mmap_sem);
318 si_code = SEGV_MAPERR;
319 vma = find_vma(mm, address);
323 #ifdef CONFIG_S390_EXEC_PROTECT
324 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
325 if (!signal_return(mm, regs, address, error_code))
327 * signal_return() has done an up_read(&mm->mmap_sem)
333 if (vma->vm_start <= address)
335 if (!(vma->vm_flags & VM_GROWSDOWN))
337 if (expand_stack(vma, address))
340 * Ok, we have a good vm_area for this memory access, so
344 si_code = SEGV_ACCERR;
345 if (!is_protection) {
346 /* page not present, check vm flags */
347 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
350 if (!(vma->vm_flags & VM_WRITE))
356 * If for any reason at all we couldn't handle the fault,
357 * make sure we exit gracefully rather than endlessly redo
360 switch (handle_mm_fault(mm, vma, address, is_protection)) {
367 case VM_FAULT_SIGBUS:
375 up_read(&mm->mmap_sem);
377 * The instruction that caused the program check will
378 * be repeated. Don't signal single step via SIGTRAP.
380 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
384 * Something tried to access memory that isn't in our memory map..
385 * Fix it, but check if it's kernel or user first..
388 up_read(&mm->mmap_sem);
390 /* User mode accesses just cause a SIGSEGV */
391 if (regs->psw.mask & PSW_MASK_PSTATE) {
392 tsk->thread.prot_addr = address;
393 tsk->thread.trap_no = error_code;
394 do_sigsegv(regs, error_code, si_code, address);
399 /* Are we prepared to handle this kernel fault? */
400 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
402 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
407 * Oops. The kernel tried to access some bad page. We'll have to
408 * terminate things with extreme prejudice.
411 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
412 " at virtual kernel address %p\n", (void *)address);
414 printk(KERN_ALERT "Unable to handle kernel paging request"
415 " at virtual user address %p\n", (void *)address);
417 die("Oops", regs, error_code);
422 * We ran out of memory, or some other thing happened to us that made
423 * us unable to handle the page fault gracefully.
426 up_read(&mm->mmap_sem);
429 down_read(&mm->mmap_sem);
432 printk("VM: killing process %s\n", tsk->comm);
433 if (regs->psw.mask & PSW_MASK_PSTATE)
438 up_read(&mm->mmap_sem);
441 * Send a sigbus, regardless of whether we were in kernel
444 tsk->thread.prot_addr = address;
445 tsk->thread.trap_no = error_code;
446 force_sig(SIGBUS, tsk);
448 /* Kernel mode? Handle exceptions or die */
449 if (!(regs->psw.mask & PSW_MASK_PSTATE))
453 void __kprobes do_protection_exception(struct pt_regs *regs,
454 unsigned long error_code)
456 regs->psw.addr -= (error_code >> 16);
457 do_exception(regs, 4, 1);
460 void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
462 do_exception(regs, error_code & 0xff, 0);
467 * 'pfault' pseudo page faults routines.
469 static ext_int_info_t ext_int_pfault;
470 static int pfault_disable = 0;
472 static int __init nopfault(char *str)
478 __setup("nopfault", nopfault);
489 } __attribute__ ((packed)) pfault_refbk_t;
491 int pfault_init(void)
493 pfault_refbk_t refbk =
494 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
498 if (!MACHINE_IS_VM || pfault_disable)
501 " diag %1,%0,0x258\n"
506 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
511 void pfault_fini(void)
513 pfault_refbk_t refbk =
514 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
516 if (!MACHINE_IS_VM || pfault_disable)
518 __ctl_clear_bit(0,9);
523 : : "a" (&refbk), "m" (refbk) : "cc");
526 static void pfault_interrupt(__u16 error_code)
528 struct task_struct *tsk;
532 * Get the external interruption subcode & pfault
533 * initial/completion signal bit. VM stores this
534 * in the 'cpu address' field associated with the
535 * external interrupt.
537 subcode = S390_lowcore.cpu_addr;
538 if ((subcode & 0xff00) != __SUBCODE_MASK)
542 * Get the token (= address of the task structure of the affected task).
544 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
546 if (subcode & 0x0080) {
547 /* signal bit is set -> a page has been swapped in by VM */
548 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
549 /* Initial interrupt was faster than the completion
550 * interrupt. pfault_wait is valid. Set pfault_wait
551 * back to zero and wake up the process. This can
552 * safely be done because the task is still sleeping
553 * and can't produce new pfaults. */
554 tsk->thread.pfault_wait = 0;
555 wake_up_process(tsk);
556 put_task_struct(tsk);
559 /* signal bit not set -> a real page is missing. */
560 get_task_struct(tsk);
561 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
562 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
563 /* Completion interrupt was faster than the initial
564 * interrupt (swapped in a -1 for pfault_wait). Set
565 * pfault_wait back to zero and exit. This can be
566 * done safely because tsk is running in kernel
567 * mode and can't produce new pfaults. */
568 tsk->thread.pfault_wait = 0;
569 set_task_state(tsk, TASK_RUNNING);
570 put_task_struct(tsk);
572 set_tsk_need_resched(tsk);
576 void __init pfault_irq_init(void)
582 * Try to get pfault pseudo page faults going.
584 if (register_early_external_interrupt(0x2603, pfault_interrupt,
585 &ext_int_pfault) != 0)
586 panic("Couldn't request external interrupt 0x2603");
588 if (pfault_init() == 0)
591 /* Tough luck, no pfault. */
593 unregister_early_external_interrupt(0x2603, pfault_interrupt,