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_counter.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, 1 for user space and
104 * 2 for code execution in user space with noexec=on.
106 static inline int check_space(struct task_struct *tsk)
109 * The lowest two bits of S390_lowcore.trans_exc_code
110 * indicate which paging table was used.
112 int desc = S390_lowcore.trans_exc_code & 3;
114 if (desc == 3) /* Home Segment Table Descriptor */
115 return switch_amode == 0;
116 if (desc == 2) /* Secondary Segment Table Descriptor */
117 return tsk->thread.mm_segment.ar4;
118 #ifdef CONFIG_S390_SWITCH_AMODE
119 if (unlikely(desc == 1)) { /* STD determined via access register */
120 /* %a0 always indicates primary space. */
121 if (S390_lowcore.exc_access_id != 0) {
122 save_access_regs(tsk->thread.acrs);
124 * An alet of 0 indicates primary space.
125 * An alet of 1 indicates secondary space.
126 * Any other alet values generate an
127 * alen-translation exception.
129 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
130 return tsk->thread.mm_segment.ar4;
134 /* Primary Segment Table Descriptor */
135 return switch_amode << s390_noexec;
139 * Send SIGSEGV to task. This is an external routine
140 * to keep the stack usage of do_page_fault small.
142 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
143 int si_code, unsigned long address)
147 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
148 #if defined(CONFIG_SYSCTL)
149 if (sysctl_userprocess_debug)
152 printk("User process fault: interruption code 0x%lX\n",
154 printk("failing address: %lX\n", address);
158 si.si_signo = SIGSEGV;
159 si.si_code = si_code;
160 si.si_addr = (void __user *) address;
161 force_sig_info(SIGSEGV, &si, current);
164 static void do_no_context(struct pt_regs *regs, unsigned long error_code,
165 unsigned long address)
167 const struct exception_table_entry *fixup;
169 /* Are we prepared to handle this kernel fault? */
170 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
172 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
177 * Oops. The kernel tried to access some bad page. We'll have to
178 * terminate things with extreme prejudice.
180 if (check_space(current) == 0)
181 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
182 " at virtual kernel address %p\n", (void *)address);
184 printk(KERN_ALERT "Unable to handle kernel paging request"
185 " at virtual user address %p\n", (void *)address);
187 die("Oops", regs, error_code);
191 static void do_low_address(struct pt_regs *regs, unsigned long error_code)
193 /* Low-address protection hit in kernel mode means
194 NULL pointer write access in kernel mode. */
195 if (regs->psw.mask & PSW_MASK_PSTATE) {
196 /* Low-address protection hit in user mode 'cannot happen'. */
197 die ("Low-address protection", regs, error_code);
201 do_no_context(regs, error_code, 0);
204 static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
205 unsigned long address)
207 struct task_struct *tsk = current;
208 struct mm_struct *mm = tsk->mm;
210 up_read(&mm->mmap_sem);
212 * Send a sigbus, regardless of whether we were in kernel
215 tsk->thread.prot_addr = address;
216 tsk->thread.trap_no = error_code;
217 force_sig(SIGBUS, tsk);
219 /* Kernel mode? Handle exceptions or die */
220 if (!(regs->psw.mask & PSW_MASK_PSTATE))
221 do_no_context(regs, error_code, address);
224 #ifdef CONFIG_S390_EXEC_PROTECT
225 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
226 unsigned long address, unsigned long error_code)
235 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
240 up_read(&mm->mmap_sem);
241 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
243 compat = is_compat_task();
244 if (compat && instruction == 0x0a77)
246 else if (compat && instruction == 0x0aad)
247 sys32_rt_sigreturn();
250 if (instruction == 0x0a77)
252 else if (instruction == 0x0aad)
255 current->thread.prot_addr = address;
256 current->thread.trap_no = error_code;
257 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
261 #endif /* CONFIG_S390_EXEC_PROTECT */
264 * This routine handles page faults. It determines the address,
265 * and the problem, and then passes it off to one of the appropriate
269 * 04 Protection -> Write-Protection (suprression)
270 * 10 Segment translation -> Not present (nullification)
271 * 11 Page translation -> Not present (nullification)
272 * 3b Region third trans. -> Not present (nullification)
275 do_exception(struct pt_regs *regs, unsigned long error_code, int write)
277 struct task_struct *tsk;
278 struct mm_struct *mm;
279 struct vm_area_struct *vma;
280 unsigned long address;
285 if (notify_page_fault(regs, error_code))
291 /* get the failing address and the affected space */
292 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
293 space = check_space(tsk);
296 * Verify that the fault happened in user space, that
297 * we are not in an interrupt and that there is a
300 if (unlikely(space == 0 || in_atomic() || !mm))
304 * When we get here, the fault happened in the current
305 * task's user address space, so we can switch on the
306 * interrupts again and then search the VMAs
309 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
310 down_read(&mm->mmap_sem);
312 si_code = SEGV_MAPERR;
313 vma = find_vma(mm, address);
317 #ifdef CONFIG_S390_EXEC_PROTECT
318 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
319 if (!signal_return(mm, regs, address, error_code))
321 * signal_return() has done an up_read(&mm->mmap_sem)
327 if (vma->vm_start <= address)
329 if (!(vma->vm_flags & VM_GROWSDOWN))
331 if (expand_stack(vma, address))
334 * Ok, we have a good vm_area for this memory access, so
338 si_code = SEGV_ACCERR;
340 /* page not present, check vm flags */
341 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
344 if (!(vma->vm_flags & VM_WRITE))
348 if (is_vm_hugetlb_page(vma))
349 address &= HPAGE_MASK;
351 * If for any reason at all we couldn't handle the fault,
352 * make sure we exit gracefully rather than endlessly redo
355 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
356 if (unlikely(fault & VM_FAULT_ERROR)) {
357 if (fault & VM_FAULT_OOM) {
358 up_read(&mm->mmap_sem);
359 pagefault_out_of_memory();
361 } else if (fault & VM_FAULT_SIGBUS) {
362 do_sigbus(regs, error_code, address);
367 if (fault & VM_FAULT_MAJOR) {
369 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
373 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
376 up_read(&mm->mmap_sem);
378 * The instruction that caused the program check will
379 * be repeated. Don't signal single step via SIGTRAP.
381 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
385 * Something tried to access memory that isn't in our memory map..
386 * Fix it, but check if it's kernel or user first..
389 up_read(&mm->mmap_sem);
391 /* User mode accesses just cause a SIGSEGV */
392 if (regs->psw.mask & PSW_MASK_PSTATE) {
393 tsk->thread.prot_addr = address;
394 tsk->thread.trap_no = error_code;
395 do_sigsegv(regs, error_code, si_code, address);
400 do_no_context(regs, error_code, address);
403 void __kprobes do_protection_exception(struct pt_regs *regs,
406 /* Protection exception is supressing, decrement psw address. */
407 regs->psw.addr -= (error_code >> 16);
409 * Check for low-address protection. This needs to be treated
410 * as a special case because the translation exception code
411 * field is not guaranteed to contain valid data in this case.
413 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
414 do_low_address(regs, error_code);
417 do_exception(regs, 4, 1);
420 void __kprobes do_dat_exception(struct pt_regs *regs, long error_code)
422 do_exception(regs, error_code & 0xff, 0);
426 void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
428 struct mm_struct *mm;
429 struct vm_area_struct *vma;
430 unsigned long address;
434 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
435 space = check_space(current);
437 if (unlikely(space == 0 || in_atomic() || !mm))
442 down_read(&mm->mmap_sem);
443 vma = find_vma(mm, address);
444 up_read(&mm->mmap_sem);
447 update_mm(mm, current);
451 /* User mode accesses just cause a SIGSEGV */
452 if (regs->psw.mask & PSW_MASK_PSTATE) {
453 current->thread.prot_addr = address;
454 current->thread.trap_no = error_code;
455 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
460 do_no_context(regs, error_code, address);
466 * 'pfault' pseudo page faults routines.
468 static ext_int_info_t ext_int_pfault;
469 static int pfault_disable = 0;
471 static int __init nopfault(char *str)
477 __setup("nopfault", nopfault);
488 } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
490 int pfault_init(void)
492 pfault_refbk_t refbk =
493 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
497 if (!MACHINE_IS_VM || pfault_disable)
500 " diag %1,%0,0x258\n"
505 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
510 void pfault_fini(void)
512 pfault_refbk_t refbk =
513 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
515 if (!MACHINE_IS_VM || pfault_disable)
517 __ctl_clear_bit(0,9);
522 : : "a" (&refbk), "m" (refbk) : "cc");
525 static void pfault_interrupt(__u16 error_code)
527 struct task_struct *tsk;
531 * Get the external interruption subcode & pfault
532 * initial/completion signal bit. VM stores this
533 * in the 'cpu address' field associated with the
534 * external interrupt.
536 subcode = S390_lowcore.cpu_addr;
537 if ((subcode & 0xff00) != __SUBCODE_MASK)
541 * Get the token (= address of the task structure of the affected task).
543 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
545 if (subcode & 0x0080) {
546 /* signal bit is set -> a page has been swapped in by VM */
547 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
548 /* Initial interrupt was faster than the completion
549 * interrupt. pfault_wait is valid. Set pfault_wait
550 * back to zero and wake up the process. This can
551 * safely be done because the task is still sleeping
552 * and can't produce new pfaults. */
553 tsk->thread.pfault_wait = 0;
554 wake_up_process(tsk);
555 put_task_struct(tsk);
558 /* signal bit not set -> a real page is missing. */
559 get_task_struct(tsk);
560 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
561 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
562 /* Completion interrupt was faster than the initial
563 * interrupt (swapped in a -1 for pfault_wait). Set
564 * pfault_wait back to zero and exit. This can be
565 * done safely because tsk is running in kernel
566 * mode and can't produce new pfaults. */
567 tsk->thread.pfault_wait = 0;
568 set_task_state(tsk, TASK_RUNNING);
569 put_task_struct(tsk);
571 set_tsk_need_resched(tsk);
575 void __init pfault_irq_init(void)
581 * Try to get pfault pseudo page faults going.
583 if (register_early_external_interrupt(0x2603, pfault_interrupt,
584 &ext_int_pfault) != 0)
585 panic("Couldn't request external interrupt 0x2603");
587 if (pfault_init() == 0)
590 /* Tough luck, no pfault. */
592 unregister_early_external_interrupt(0x2603, pfault_interrupt,