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/perf_event.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/ptrace.h>
21 #include <linux/mman.h>
23 #include <linux/compat.h>
24 #include <linux/smp.h>
25 #include <linux/kdebug.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/module.h>
29 #include <linux/hardirq.h>
30 #include <linux/kprobes.h>
31 #include <linux/uaccess.h>
32 #include <linux/hugetlb.h>
33 #include <asm/system.h>
34 #include <asm/pgtable.h>
35 #include <asm/s390_ext.h>
36 #include <asm/mmu_context.h>
37 #include "../kernel/entry.h"
40 #define __FAIL_ADDR_MASK 0x7ffff000
41 #define __FIXUP_MASK 0x7fffffff
42 #define __SUBCODE_MASK 0x0200
43 #define __PF_RES_FIELD 0ULL
44 #else /* CONFIG_64BIT */
45 #define __FAIL_ADDR_MASK -4096L
46 #define __FIXUP_MASK ~0L
47 #define __SUBCODE_MASK 0x0600
48 #define __PF_RES_FIELD 0x8000000000000000ULL
49 #endif /* CONFIG_64BIT */
52 extern int sysctl_userprocess_debug;
56 static inline int notify_page_fault(struct pt_regs *regs, long err)
60 /* kprobe_running() needs smp_processor_id() */
61 if (!user_mode(regs)) {
63 if (kprobe_running() && kprobe_fault_handler(regs, 14))
71 static inline int notify_page_fault(struct pt_regs *regs, long err)
79 * Unlock any spinlocks which will prevent us from getting the
82 void bust_spinlocks(int yes)
87 int loglevel_save = console_loglevel;
91 * OK, the message is on the console. Now we call printk()
92 * without oops_in_progress set so that printk will give klogd
93 * a poke. Hold onto your hats...
95 console_loglevel = 15;
97 console_loglevel = loglevel_save;
102 * Returns the address space associated with the fault.
103 * Returns 0 for kernel space and 1 for user space.
105 static inline int user_space_fault(unsigned long trans_exc_code)
108 * The lowest two bits of the translation exception
109 * identification indicate which paging table was used.
112 if (trans_exc_code == 2)
113 /* Access via secondary space, set_fs setting decides */
114 return current->thread.mm_segment.ar4;
116 /* User space if the access has been done via home space. */
117 return trans_exc_code == 3;
119 * If the user space is not the home space the kernel runs in home
120 * space. Access via secondary space has already been covered,
121 * access via primary space or access register is from user space
122 * and access via home space is from the kernel.
124 return trans_exc_code != 3;
128 * Send SIGSEGV to task. This is an external routine
129 * to keep the stack usage of do_page_fault small.
131 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
132 int si_code, unsigned long address)
136 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
137 #if defined(CONFIG_SYSCTL)
138 if (sysctl_userprocess_debug)
141 printk("User process fault: interruption code 0x%lX\n",
143 printk("failing address: %lX\n", address);
147 si.si_signo = SIGSEGV;
148 si.si_code = si_code;
149 si.si_addr = (void __user *) address;
150 force_sig_info(SIGSEGV, &si, current);
153 static void do_no_context(struct pt_regs *regs, unsigned long error_code,
154 unsigned long trans_exc_code)
156 const struct exception_table_entry *fixup;
157 unsigned long address;
159 /* Are we prepared to handle this kernel fault? */
160 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
162 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
167 * Oops. The kernel tried to access some bad page. We'll have to
168 * terminate things with extreme prejudice.
170 address = trans_exc_code & __FAIL_ADDR_MASK;
171 if (user_space_fault(trans_exc_code) == 0)
172 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
173 " at virtual kernel address %p\n", (void *)address);
175 printk(KERN_ALERT "Unable to handle kernel paging request"
176 " at virtual user address %p\n", (void *)address);
178 die("Oops", regs, error_code);
182 static void do_low_address(struct pt_regs *regs, unsigned long error_code,
183 unsigned long trans_exc_code)
185 /* Low-address protection hit in kernel mode means
186 NULL pointer write access in kernel mode. */
187 if (regs->psw.mask & PSW_MASK_PSTATE) {
188 /* Low-address protection hit in user mode 'cannot happen'. */
189 die ("Low-address protection", regs, error_code);
193 do_no_context(regs, error_code, trans_exc_code);
196 static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
197 unsigned long trans_exc_code)
199 struct task_struct *tsk = current;
200 struct mm_struct *mm = tsk->mm;
202 up_read(&mm->mmap_sem);
204 * Send a sigbus, regardless of whether we were in kernel
207 tsk->thread.prot_addr = trans_exc_code & __FAIL_ADDR_MASK;
208 tsk->thread.trap_no = error_code;
209 force_sig(SIGBUS, tsk);
211 /* Kernel mode? Handle exceptions or die */
212 if (!(regs->psw.mask & PSW_MASK_PSTATE))
213 do_no_context(regs, error_code, trans_exc_code);
216 #ifdef CONFIG_S390_EXEC_PROTECT
217 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
218 unsigned long address, unsigned long error_code)
227 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
232 up_read(&mm->mmap_sem);
233 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
235 compat = is_compat_task();
236 if (compat && instruction == 0x0a77)
238 else if (compat && instruction == 0x0aad)
239 sys32_rt_sigreturn();
242 if (instruction == 0x0a77)
244 else if (instruction == 0x0aad)
247 current->thread.prot_addr = address;
248 current->thread.trap_no = error_code;
249 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
253 #endif /* CONFIG_S390_EXEC_PROTECT */
256 * This routine handles page faults. It determines the address,
257 * and the problem, and then passes it off to one of the appropriate
261 * 04 Protection -> Write-Protection (suprression)
262 * 10 Segment translation -> Not present (nullification)
263 * 11 Page translation -> Not present (nullification)
264 * 3b Region third trans. -> Not present (nullification)
267 do_exception(struct pt_regs *regs, unsigned long error_code, int write,
268 unsigned long trans_exc_code)
270 struct task_struct *tsk;
271 struct mm_struct *mm;
272 struct vm_area_struct *vma;
273 unsigned long address;
277 if (notify_page_fault(regs, error_code))
284 * Verify that the fault happened in user space, that
285 * we are not in an interrupt and that there is a
288 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
291 address = trans_exc_code & __FAIL_ADDR_MASK;
293 * When we get here, the fault happened in the current
294 * task's user address space, so we can switch on the
295 * interrupts again and then search the VMAs
298 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
299 down_read(&mm->mmap_sem);
301 si_code = SEGV_MAPERR;
302 vma = find_vma(mm, address);
306 #ifdef CONFIG_S390_EXEC_PROTECT
307 if (unlikely((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_SECONDARY &&
308 (trans_exc_code & 3) == 0 && !(vma->vm_flags & VM_EXEC)))
309 if (!signal_return(mm, regs, address, error_code))
311 * signal_return() has done an up_read(&mm->mmap_sem)
317 if (vma->vm_start <= address)
319 if (!(vma->vm_flags & VM_GROWSDOWN))
321 if (expand_stack(vma, address))
324 * Ok, we have a good vm_area for this memory access, so
328 si_code = SEGV_ACCERR;
330 /* page not present, check vm flags */
331 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
334 if (!(vma->vm_flags & VM_WRITE))
338 if (is_vm_hugetlb_page(vma))
339 address &= HPAGE_MASK;
341 * If for any reason at all we couldn't handle the fault,
342 * make sure we exit gracefully rather than endlessly redo
345 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
346 if (unlikely(fault & VM_FAULT_ERROR)) {
347 if (fault & VM_FAULT_OOM) {
348 up_read(&mm->mmap_sem);
349 pagefault_out_of_memory();
351 } else if (fault & VM_FAULT_SIGBUS) {
352 do_sigbus(regs, error_code, address);
357 if (fault & VM_FAULT_MAJOR) {
359 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
363 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
366 up_read(&mm->mmap_sem);
368 * The instruction that caused the program check will
369 * be repeated. Don't signal single step via SIGTRAP.
371 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
375 * Something tried to access memory that isn't in our memory map..
376 * Fix it, but check if it's kernel or user first..
379 up_read(&mm->mmap_sem);
381 /* User mode accesses just cause a SIGSEGV */
382 if (regs->psw.mask & PSW_MASK_PSTATE) {
383 tsk->thread.prot_addr = address;
384 tsk->thread.trap_no = error_code;
385 do_sigsegv(regs, error_code, si_code, address);
390 do_no_context(regs, error_code, trans_exc_code);
393 void __kprobes do_protection_exception(struct pt_regs *regs,
396 unsigned long trans_exc_code = S390_lowcore.trans_exc_code;
398 /* Protection exception is supressing, decrement psw address. */
399 regs->psw.addr -= (error_code >> 16);
401 * Check for low-address protection. This needs to be treated
402 * as a special case because the translation exception code
403 * field is not guaranteed to contain valid data in this case.
405 if (unlikely(!(trans_exc_code & 4))) {
406 do_low_address(regs, error_code, trans_exc_code);
409 do_exception(regs, 4, 1, trans_exc_code);
412 void __kprobes do_dat_exception(struct pt_regs *regs, long error_code)
414 do_exception(regs, error_code & 0xff, 0, S390_lowcore.trans_exc_code);
418 void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
420 unsigned long trans_exc_code = S390_lowcore.trans_exc_code;
421 struct mm_struct *mm;
422 struct vm_area_struct *vma;
423 unsigned long address;
426 address = trans_exc_code & __FAIL_ADDR_MASK;
428 if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
433 down_read(&mm->mmap_sem);
434 vma = find_vma(mm, address);
435 up_read(&mm->mmap_sem);
438 update_mm(mm, current);
442 /* User mode accesses just cause a SIGSEGV */
443 if (regs->psw.mask & PSW_MASK_PSTATE) {
444 current->thread.prot_addr = address;
445 current->thread.trap_no = error_code;
446 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
451 do_no_context(regs, error_code, trans_exc_code);
457 * 'pfault' pseudo page faults routines.
459 static ext_int_info_t ext_int_pfault;
460 static int pfault_disable = 0;
462 static int __init nopfault(char *str)
468 __setup("nopfault", nopfault);
479 } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
481 int pfault_init(void)
483 pfault_refbk_t refbk =
484 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
488 if (!MACHINE_IS_VM || pfault_disable)
491 " diag %1,%0,0x258\n"
496 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
501 void pfault_fini(void)
503 pfault_refbk_t refbk =
504 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
506 if (!MACHINE_IS_VM || pfault_disable)
508 __ctl_clear_bit(0,9);
513 : : "a" (&refbk), "m" (refbk) : "cc");
516 static void pfault_interrupt(__u16 error_code)
518 struct task_struct *tsk;
522 * Get the external interruption subcode & pfault
523 * initial/completion signal bit. VM stores this
524 * in the 'cpu address' field associated with the
525 * external interrupt.
527 subcode = S390_lowcore.cpu_addr;
528 if ((subcode & 0xff00) != __SUBCODE_MASK)
532 * Get the token (= address of the task structure of the affected task).
534 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
536 if (subcode & 0x0080) {
537 /* signal bit is set -> a page has been swapped in by VM */
538 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
539 /* Initial interrupt was faster than the completion
540 * interrupt. pfault_wait is valid. Set pfault_wait
541 * back to zero and wake up the process. This can
542 * safely be done because the task is still sleeping
543 * and can't produce new pfaults. */
544 tsk->thread.pfault_wait = 0;
545 wake_up_process(tsk);
546 put_task_struct(tsk);
549 /* signal bit not set -> a real page is missing. */
550 get_task_struct(tsk);
551 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
552 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
553 /* Completion interrupt was faster than the initial
554 * interrupt (swapped in a -1 for pfault_wait). Set
555 * pfault_wait back to zero and exit. This can be
556 * done safely because tsk is running in kernel
557 * mode and can't produce new pfaults. */
558 tsk->thread.pfault_wait = 0;
559 set_task_state(tsk, TASK_RUNNING);
560 put_task_struct(tsk);
562 set_tsk_need_resched(tsk);
566 void __init pfault_irq_init(void)
572 * Try to get pfault pseudo page faults going.
574 if (register_early_external_interrupt(0x2603, pfault_interrupt,
575 &ext_int_pfault) != 0)
576 panic("Couldn't request external interrupt 0x2603");
578 if (pfault_init() == 0)
581 /* Tough luck, no pfault. */
583 unregister_early_external_interrupt(0x2603, pfault_interrupt,