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/kdebug.h>
24 #include <linux/smp_lock.h>
25 #include <linux/init.h>
26 #include <linux/console.h>
27 #include <linux/module.h>
28 #include <linux/hardirq.h>
29 #include <linux/kprobes.h>
30 #include <linux/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/pgtable.h>
33 #include <asm/s390_ext.h>
34 #include <asm/mmu_context.h>
35 #include "../kernel/entry.h"
38 #define __FAIL_ADDR_MASK 0x7ffff000
39 #define __FIXUP_MASK 0x7fffffff
40 #define __SUBCODE_MASK 0x0200
41 #define __PF_RES_FIELD 0ULL
42 #else /* CONFIG_64BIT */
43 #define __FAIL_ADDR_MASK -4096L
44 #define __FIXUP_MASK ~0L
45 #define __SUBCODE_MASK 0x0600
46 #define __PF_RES_FIELD 0x8000000000000000ULL
47 #endif /* CONFIG_64BIT */
50 extern int sysctl_userprocess_debug;
54 static inline int notify_page_fault(struct pt_regs *regs, long err)
58 /* kprobe_running() needs smp_processor_id() */
59 if (!user_mode(regs)) {
61 if (kprobe_running() && kprobe_fault_handler(regs, 14))
69 static inline int notify_page_fault(struct pt_regs *regs, long err)
77 * Unlock any spinlocks which will prevent us from getting the
80 void bust_spinlocks(int yes)
85 int loglevel_save = console_loglevel;
89 * OK, the message is on the console. Now we call printk()
90 * without oops_in_progress set so that printk will give klogd
91 * a poke. Hold onto your hats...
93 console_loglevel = 15;
95 console_loglevel = loglevel_save;
100 * Returns the address space associated with the fault.
101 * Returns 0 for kernel space, 1 for user space and
102 * 2 for code execution in user space with noexec=on.
104 static inline int check_space(struct task_struct *tsk)
107 * The lowest two bits of S390_lowcore.trans_exc_code
108 * indicate which paging table was used.
110 int desc = S390_lowcore.trans_exc_code & 3;
112 if (desc == 3) /* Home Segment Table Descriptor */
113 return switch_amode == 0;
114 if (desc == 2) /* Secondary Segment Table Descriptor */
115 return tsk->thread.mm_segment.ar4;
116 #ifdef CONFIG_S390_SWITCH_AMODE
117 if (unlikely(desc == 1)) { /* STD determined via access register */
118 /* %a0 always indicates primary space. */
119 if (S390_lowcore.exc_access_id != 0) {
120 save_access_regs(tsk->thread.acrs);
122 * An alet of 0 indicates primary space.
123 * An alet of 1 indicates secondary space.
124 * Any other alet values generate an
125 * alen-translation exception.
127 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
128 return tsk->thread.mm_segment.ar4;
132 /* Primary Segment Table Descriptor */
133 return switch_amode << s390_noexec;
137 * Send SIGSEGV to task. This is an external routine
138 * to keep the stack usage of do_page_fault small.
140 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
141 int si_code, unsigned long address)
145 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
146 #if defined(CONFIG_SYSCTL)
147 if (sysctl_userprocess_debug)
150 printk("User process fault: interruption code 0x%lX\n",
152 printk("failing address: %lX\n", address);
156 si.si_signo = SIGSEGV;
157 si.si_code = si_code;
158 si.si_addr = (void __user *) address;
159 force_sig_info(SIGSEGV, &si, current);
162 static void do_no_context(struct pt_regs *regs, unsigned long error_code,
163 unsigned long address)
165 const struct exception_table_entry *fixup;
167 /* Are we prepared to handle this kernel fault? */
168 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
170 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
175 * Oops. The kernel tried to access some bad page. We'll have to
176 * terminate things with extreme prejudice.
178 if (check_space(current) == 0)
179 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
180 " at virtual kernel address %p\n", (void *)address);
182 printk(KERN_ALERT "Unable to handle kernel paging request"
183 " at virtual user address %p\n", (void *)address);
185 die("Oops", regs, error_code);
189 static void do_low_address(struct pt_regs *regs, unsigned long error_code)
191 /* Low-address protection hit in kernel mode means
192 NULL pointer write access in kernel mode. */
193 if (regs->psw.mask & PSW_MASK_PSTATE) {
194 /* Low-address protection hit in user mode 'cannot happen'. */
195 die ("Low-address protection", regs, error_code);
199 do_no_context(regs, error_code, 0);
203 * We ran out of memory, or some other thing happened to us that made
204 * us unable to handle the page fault gracefully.
206 static int do_out_of_memory(struct pt_regs *regs, unsigned long error_code,
207 unsigned long address)
209 struct task_struct *tsk = current;
210 struct mm_struct *mm = tsk->mm;
212 up_read(&mm->mmap_sem);
213 if (is_global_init(tsk)) {
215 down_read(&mm->mmap_sem);
218 printk("VM: killing process %s\n", tsk->comm);
219 if (regs->psw.mask & PSW_MASK_PSTATE)
220 do_group_exit(SIGKILL);
221 do_no_context(regs, error_code, address);
225 static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
226 unsigned long address)
228 struct task_struct *tsk = current;
229 struct mm_struct *mm = tsk->mm;
231 up_read(&mm->mmap_sem);
233 * Send a sigbus, regardless of whether we were in kernel
236 tsk->thread.prot_addr = address;
237 tsk->thread.trap_no = error_code;
238 force_sig(SIGBUS, tsk);
240 /* Kernel mode? Handle exceptions or die */
241 if (!(regs->psw.mask & PSW_MASK_PSTATE))
242 do_no_context(regs, error_code, address);
245 #ifdef CONFIG_S390_EXEC_PROTECT
246 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
247 unsigned long address, unsigned long error_code)
256 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
261 up_read(&mm->mmap_sem);
262 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
264 compat = test_tsk_thread_flag(current, TIF_31BIT);
265 if (compat && instruction == 0x0a77)
267 else if (compat && instruction == 0x0aad)
268 sys32_rt_sigreturn();
271 if (instruction == 0x0a77)
273 else if (instruction == 0x0aad)
276 current->thread.prot_addr = address;
277 current->thread.trap_no = error_code;
278 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
282 #endif /* CONFIG_S390_EXEC_PROTECT */
285 * This routine handles page faults. It determines the address,
286 * and the problem, and then passes it off to one of the appropriate
290 * 04 Protection -> Write-Protection (suprression)
291 * 10 Segment translation -> Not present (nullification)
292 * 11 Page translation -> Not present (nullification)
293 * 3b Region third trans. -> Not present (nullification)
296 do_exception(struct pt_regs *regs, unsigned long error_code, int write)
298 struct task_struct *tsk;
299 struct mm_struct *mm;
300 struct vm_area_struct *vma;
301 unsigned long address;
306 if (notify_page_fault(regs, error_code))
312 /* get the failing address and the affected space */
313 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
314 space = check_space(tsk);
317 * Verify that the fault happened in user space, that
318 * we are not in an interrupt and that there is a
321 if (unlikely(space == 0 || in_atomic() || !mm))
325 * When we get here, the fault happened in the current
326 * task's user address space, so we can switch on the
327 * interrupts again and then search the VMAs
331 down_read(&mm->mmap_sem);
333 si_code = SEGV_MAPERR;
334 vma = find_vma(mm, address);
338 #ifdef CONFIG_S390_EXEC_PROTECT
339 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
340 if (!signal_return(mm, regs, address, error_code))
342 * signal_return() has done an up_read(&mm->mmap_sem)
348 if (vma->vm_start <= address)
350 if (!(vma->vm_flags & VM_GROWSDOWN))
352 if (expand_stack(vma, address))
355 * Ok, we have a good vm_area for this memory access, so
359 si_code = SEGV_ACCERR;
361 /* page not present, check vm flags */
362 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
365 if (!(vma->vm_flags & VM_WRITE))
371 * If for any reason at all we couldn't handle the fault,
372 * make sure we exit gracefully rather than endlessly redo
375 fault = handle_mm_fault(mm, vma, address, write);
376 if (unlikely(fault & VM_FAULT_ERROR)) {
377 if (fault & VM_FAULT_OOM) {
378 if (do_out_of_memory(regs, error_code, address))
381 } else if (fault & VM_FAULT_SIGBUS) {
382 do_sigbus(regs, error_code, address);
387 if (fault & VM_FAULT_MAJOR)
392 up_read(&mm->mmap_sem);
394 * The instruction that caused the program check will
395 * be repeated. Don't signal single step via SIGTRAP.
397 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
401 * Something tried to access memory that isn't in our memory map..
402 * Fix it, but check if it's kernel or user first..
405 up_read(&mm->mmap_sem);
407 /* User mode accesses just cause a SIGSEGV */
408 if (regs->psw.mask & PSW_MASK_PSTATE) {
409 tsk->thread.prot_addr = address;
410 tsk->thread.trap_no = error_code;
411 do_sigsegv(regs, error_code, si_code, address);
416 do_no_context(regs, error_code, address);
419 void __kprobes do_protection_exception(struct pt_regs *regs,
422 /* Protection exception is supressing, decrement psw address. */
423 regs->psw.addr -= (error_code >> 16);
425 * Check for low-address protection. This needs to be treated
426 * as a special case because the translation exception code
427 * field is not guaranteed to contain valid data in this case.
429 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
430 do_low_address(regs, error_code);
433 do_exception(regs, 4, 1);
436 void __kprobes do_dat_exception(struct pt_regs *regs, long error_code)
438 do_exception(regs, error_code & 0xff, 0);
442 void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
444 struct mm_struct *mm;
445 struct vm_area_struct *vma;
446 unsigned long address;
450 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
451 space = check_space(current);
453 if (unlikely(space == 0 || in_atomic() || !mm))
458 down_read(&mm->mmap_sem);
459 vma = find_vma(mm, address);
460 up_read(&mm->mmap_sem);
463 update_mm(mm, current);
467 /* User mode accesses just cause a SIGSEGV */
468 if (regs->psw.mask & PSW_MASK_PSTATE) {
469 current->thread.prot_addr = address;
470 current->thread.trap_no = error_code;
471 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
476 do_no_context(regs, error_code, address);
482 * 'pfault' pseudo page faults routines.
484 static ext_int_info_t ext_int_pfault;
485 static int pfault_disable = 0;
487 static int __init nopfault(char *str)
493 __setup("nopfault", nopfault);
504 } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
506 int pfault_init(void)
508 pfault_refbk_t refbk =
509 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
513 if (!MACHINE_IS_VM || pfault_disable)
516 " diag %1,%0,0x258\n"
521 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
526 void pfault_fini(void)
528 pfault_refbk_t refbk =
529 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
531 if (!MACHINE_IS_VM || pfault_disable)
533 __ctl_clear_bit(0,9);
538 : : "a" (&refbk), "m" (refbk) : "cc");
541 static void pfault_interrupt(__u16 error_code)
543 struct task_struct *tsk;
547 * Get the external interruption subcode & pfault
548 * initial/completion signal bit. VM stores this
549 * in the 'cpu address' field associated with the
550 * external interrupt.
552 subcode = S390_lowcore.cpu_addr;
553 if ((subcode & 0xff00) != __SUBCODE_MASK)
557 * Get the token (= address of the task structure of the affected task).
559 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
561 if (subcode & 0x0080) {
562 /* signal bit is set -> a page has been swapped in by VM */
563 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
564 /* Initial interrupt was faster than the completion
565 * interrupt. pfault_wait is valid. Set pfault_wait
566 * back to zero and wake up the process. This can
567 * safely be done because the task is still sleeping
568 * and can't produce new pfaults. */
569 tsk->thread.pfault_wait = 0;
570 wake_up_process(tsk);
571 put_task_struct(tsk);
574 /* signal bit not set -> a real page is missing. */
575 get_task_struct(tsk);
576 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
577 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
578 /* Completion interrupt was faster than the initial
579 * interrupt (swapped in a -1 for pfault_wait). Set
580 * pfault_wait back to zero and exit. This can be
581 * done safely because tsk is running in kernel
582 * mode and can't produce new pfaults. */
583 tsk->thread.pfault_wait = 0;
584 set_task_state(tsk, TASK_RUNNING);
585 put_task_struct(tsk);
587 set_tsk_need_resched(tsk);
591 void __init pfault_irq_init(void)
597 * Try to get pfault pseudo page faults going.
599 if (register_early_external_interrupt(0x2603, pfault_interrupt,
600 &ext_int_pfault) != 0)
601 panic("Couldn't request external interrupt 0x2603");
603 if (pfault_init() == 0)
606 /* Tough luck, no pfault. */
608 unregister_early_external_interrupt(0x2603, pfault_interrupt,