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 int __kprobes __notify_page_fault(struct pt_regs *regs, long err)
68 struct die_args args = { .str = "page fault",
73 return atomic_notifier_call_chain(¬ify_page_fault_chain,
74 DIE_PAGE_FAULT, &args);
77 static inline int notify_page_fault(struct pt_regs *regs, long err)
79 if (unlikely(kprobe_running()))
80 return __notify_page_fault(regs, err);
84 static inline int notify_page_fault(struct pt_regs *regs, long err)
92 * Unlock any spinlocks which will prevent us from getting the
95 void bust_spinlocks(int yes)
100 int loglevel_save = console_loglevel;
102 oops_in_progress = 0;
104 * OK, the message is on the console. Now we call printk()
105 * without oops_in_progress set so that printk will give klogd
106 * a poke. Hold onto your hats...
108 console_loglevel = 15;
110 console_loglevel = loglevel_save;
115 * Returns the address space associated with the fault.
116 * Returns 0 for kernel space, 1 for user space and
117 * 2 for code execution in user space with noexec=on.
119 static inline int check_space(struct task_struct *tsk)
122 * The lowest two bits of S390_lowcore.trans_exc_code
123 * indicate which paging table was used.
125 int desc = S390_lowcore.trans_exc_code & 3;
127 if (desc == 3) /* Home Segment Table Descriptor */
128 return switch_amode == 0;
129 if (desc == 2) /* Secondary Segment Table Descriptor */
130 return tsk->thread.mm_segment.ar4;
131 #ifdef CONFIG_S390_SWITCH_AMODE
132 if (unlikely(desc == 1)) { /* STD determined via access register */
133 /* %a0 always indicates primary space. */
134 if (S390_lowcore.exc_access_id != 0) {
135 save_access_regs(tsk->thread.acrs);
137 * An alet of 0 indicates primary space.
138 * An alet of 1 indicates secondary space.
139 * Any other alet values generate an
140 * alen-translation exception.
142 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
143 return tsk->thread.mm_segment.ar4;
147 /* Primary Segment Table Descriptor */
148 return switch_amode << s390_noexec;
152 * Send SIGSEGV to task. This is an external routine
153 * to keep the stack usage of do_page_fault small.
155 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
156 int si_code, unsigned long address)
160 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
161 #if defined(CONFIG_SYSCTL)
162 if (sysctl_userprocess_debug)
165 printk("User process fault: interruption code 0x%lX\n",
167 printk("failing address: %lX\n", address);
171 si.si_signo = SIGSEGV;
172 si.si_code = si_code;
173 si.si_addr = (void __user *) address;
174 force_sig_info(SIGSEGV, &si, current);
177 static void do_no_context(struct pt_regs *regs, unsigned long error_code,
178 unsigned long address)
180 const struct exception_table_entry *fixup;
182 /* Are we prepared to handle this kernel fault? */
183 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
185 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
190 * Oops. The kernel tried to access some bad page. We'll have to
191 * terminate things with extreme prejudice.
193 if (check_space(current) == 0)
194 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
195 " at virtual kernel address %p\n", (void *)address);
197 printk(KERN_ALERT "Unable to handle kernel paging request"
198 " at virtual user address %p\n", (void *)address);
200 die("Oops", regs, error_code);
204 static void do_low_address(struct pt_regs *regs, unsigned long error_code)
206 /* Low-address protection hit in kernel mode means
207 NULL pointer write access in kernel mode. */
208 if (regs->psw.mask & PSW_MASK_PSTATE) {
209 /* Low-address protection hit in user mode 'cannot happen'. */
210 die ("Low-address protection", regs, error_code);
214 do_no_context(regs, error_code, 0);
218 * We ran out of memory, or some other thing happened to us that made
219 * us unable to handle the page fault gracefully.
221 static int do_out_of_memory(struct pt_regs *regs, unsigned long error_code,
222 unsigned long address)
224 struct task_struct *tsk = current;
225 struct mm_struct *mm = tsk->mm;
227 up_read(&mm->mmap_sem);
230 down_read(&mm->mmap_sem);
233 printk("VM: killing process %s\n", tsk->comm);
234 if (regs->psw.mask & PSW_MASK_PSTATE)
236 do_no_context(regs, error_code, address);
240 static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
241 unsigned long address)
243 struct task_struct *tsk = current;
244 struct mm_struct *mm = tsk->mm;
246 up_read(&mm->mmap_sem);
248 * Send a sigbus, regardless of whether we were in kernel
251 tsk->thread.prot_addr = address;
252 tsk->thread.trap_no = error_code;
253 force_sig(SIGBUS, tsk);
255 /* Kernel mode? Handle exceptions or die */
256 if (!(regs->psw.mask & PSW_MASK_PSTATE))
257 do_no_context(regs, error_code, address);
260 #ifdef CONFIG_S390_EXEC_PROTECT
261 extern long sys_sigreturn(struct pt_regs *regs);
262 extern long sys_rt_sigreturn(struct pt_regs *regs);
263 extern long sys32_sigreturn(struct pt_regs *regs);
264 extern long sys32_rt_sigreturn(struct pt_regs *regs);
266 static inline void do_sigreturn(struct mm_struct *mm, struct pt_regs *regs,
269 up_read(&mm->mmap_sem);
270 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
272 if (test_tsk_thread_flag(current, TIF_31BIT)) {
274 sys32_rt_sigreturn(regs);
276 sys32_sigreturn(regs);
279 #endif /* CONFIG_COMPAT */
281 sys_rt_sigreturn(regs);
287 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
288 unsigned long address, unsigned long error_code)
294 unsigned long pfn, uaddr = regs->psw.addr;
296 spin_lock(&mm->page_table_lock);
297 pgd = pgd_offset(mm, uaddr);
298 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
300 pmd = pmd_offset(pgd, uaddr);
301 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
303 pte = pte_offset_map(pmd_offset(pgd_offset(mm, uaddr), uaddr), uaddr);
304 if (!pte || !pte_present(*pte))
309 spin_unlock(&mm->page_table_lock);
311 instruction = (u16 *) ((pfn << PAGE_SHIFT) + (uaddr & (PAGE_SIZE-1)));
312 if (*instruction == 0x0a77)
313 do_sigreturn(mm, regs, 0);
314 else if (*instruction == 0x0aad)
315 do_sigreturn(mm, regs, 1);
317 printk("- XXX - do_exception: task = %s, primary, NO EXEC "
318 "-> SIGSEGV\n", current->comm);
319 up_read(&mm->mmap_sem);
320 current->thread.prot_addr = address;
321 current->thread.trap_no = error_code;
322 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
326 spin_unlock(&mm->page_table_lock);
329 #endif /* CONFIG_S390_EXEC_PROTECT */
332 * This routine handles page faults. It determines the address,
333 * and the problem, and then passes it off to one of the appropriate
337 * 04 Protection -> Write-Protection (suprression)
338 * 10 Segment translation -> Not present (nullification)
339 * 11 Page translation -> Not present (nullification)
340 * 3b Region third trans. -> Not present (nullification)
343 do_exception(struct pt_regs *regs, unsigned long error_code, int write)
345 struct task_struct *tsk;
346 struct mm_struct *mm;
347 struct vm_area_struct *vma;
348 unsigned long address;
352 if (notify_page_fault(regs, error_code) == NOTIFY_STOP)
358 /* get the failing address and the affected space */
359 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
360 space = check_space(tsk);
363 * Verify that the fault happened in user space, that
364 * we are not in an interrupt and that there is a
367 if (unlikely(space == 0 || in_atomic() || !mm))
371 * When we get here, the fault happened in the current
372 * task's user address space, so we can switch on the
373 * interrupts again and then search the VMAs
377 down_read(&mm->mmap_sem);
379 si_code = SEGV_MAPERR;
380 vma = find_vma(mm, address);
384 #ifdef CONFIG_S390_EXEC_PROTECT
385 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
386 if (!signal_return(mm, regs, address, error_code))
388 * signal_return() has done an up_read(&mm->mmap_sem)
394 if (vma->vm_start <= address)
396 if (!(vma->vm_flags & VM_GROWSDOWN))
398 if (expand_stack(vma, address))
401 * Ok, we have a good vm_area for this memory access, so
405 si_code = SEGV_ACCERR;
407 /* page not present, check vm flags */
408 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
411 if (!(vma->vm_flags & VM_WRITE))
417 * If for any reason at all we couldn't handle the fault,
418 * make sure we exit gracefully rather than endlessly redo
421 switch (handle_mm_fault(mm, vma, address, write)) {
428 case VM_FAULT_SIGBUS:
429 do_sigbus(regs, error_code, address);
432 if (do_out_of_memory(regs, error_code, address))
439 up_read(&mm->mmap_sem);
441 * The instruction that caused the program check will
442 * be repeated. Don't signal single step via SIGTRAP.
444 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
448 * Something tried to access memory that isn't in our memory map..
449 * Fix it, but check if it's kernel or user first..
452 up_read(&mm->mmap_sem);
454 /* User mode accesses just cause a SIGSEGV */
455 if (regs->psw.mask & PSW_MASK_PSTATE) {
456 tsk->thread.prot_addr = address;
457 tsk->thread.trap_no = error_code;
458 do_sigsegv(regs, error_code, si_code, address);
463 do_no_context(regs, error_code, address);
466 void __kprobes do_protection_exception(struct pt_regs *regs,
467 unsigned long error_code)
469 /* Protection exception is supressing, decrement psw address. */
470 regs->psw.addr -= (error_code >> 16);
472 * Check for low-address protection. This needs to be treated
473 * as a special case because the translation exception code
474 * field is not guaranteed to contain valid data in this case.
476 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
477 do_low_address(regs, error_code);
480 do_exception(regs, 4, 1);
483 void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
485 do_exception(regs, error_code & 0xff, 0);
490 * 'pfault' pseudo page faults routines.
492 static ext_int_info_t ext_int_pfault;
493 static int pfault_disable = 0;
495 static int __init nopfault(char *str)
501 __setup("nopfault", nopfault);
512 } __attribute__ ((packed)) pfault_refbk_t;
514 int pfault_init(void)
516 pfault_refbk_t refbk =
517 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
521 if (!MACHINE_IS_VM || pfault_disable)
524 " diag %1,%0,0x258\n"
529 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
534 void pfault_fini(void)
536 pfault_refbk_t refbk =
537 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
539 if (!MACHINE_IS_VM || pfault_disable)
541 __ctl_clear_bit(0,9);
546 : : "a" (&refbk), "m" (refbk) : "cc");
549 static void pfault_interrupt(__u16 error_code)
551 struct task_struct *tsk;
555 * Get the external interruption subcode & pfault
556 * initial/completion signal bit. VM stores this
557 * in the 'cpu address' field associated with the
558 * external interrupt.
560 subcode = S390_lowcore.cpu_addr;
561 if ((subcode & 0xff00) != __SUBCODE_MASK)
565 * Get the token (= address of the task structure of the affected task).
567 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
569 if (subcode & 0x0080) {
570 /* signal bit is set -> a page has been swapped in by VM */
571 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
572 /* Initial interrupt was faster than the completion
573 * interrupt. pfault_wait is valid. Set pfault_wait
574 * back to zero and wake up the process. This can
575 * safely be done because the task is still sleeping
576 * and can't produce new pfaults. */
577 tsk->thread.pfault_wait = 0;
578 wake_up_process(tsk);
579 put_task_struct(tsk);
582 /* signal bit not set -> a real page is missing. */
583 get_task_struct(tsk);
584 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
585 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
586 /* Completion interrupt was faster than the initial
587 * interrupt (swapped in a -1 for pfault_wait). Set
588 * pfault_wait back to zero and exit. This can be
589 * done safely because tsk is running in kernel
590 * mode and can't produce new pfaults. */
591 tsk->thread.pfault_wait = 0;
592 set_task_state(tsk, TASK_RUNNING);
593 put_task_struct(tsk);
595 set_tsk_need_resched(tsk);
599 void __init pfault_irq_init(void)
605 * Try to get pfault pseudo page faults going.
607 if (register_early_external_interrupt(0x2603, pfault_interrupt,
608 &ext_int_pfault) != 0)
609 panic("Couldn't request external interrupt 0x2603");
611 if (pfault_init() == 0)
614 /* Tough luck, no pfault. */
616 unregister_early_external_interrupt(0x2603, pfault_interrupt,