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 <linux/hugetlb.h>
32 #include <asm/system.h>
33 #include <asm/pgtable.h>
34 #include <asm/s390_ext.h>
35 #include <asm/mmu_context.h>
36 #include "../kernel/entry.h"
39 #define __FAIL_ADDR_MASK 0x7ffff000
40 #define __FIXUP_MASK 0x7fffffff
41 #define __SUBCODE_MASK 0x0200
42 #define __PF_RES_FIELD 0ULL
43 #else /* CONFIG_64BIT */
44 #define __FAIL_ADDR_MASK -4096L
45 #define __FIXUP_MASK ~0L
46 #define __SUBCODE_MASK 0x0600
47 #define __PF_RES_FIELD 0x8000000000000000ULL
48 #endif /* CONFIG_64BIT */
51 extern int sysctl_userprocess_debug;
55 static inline int notify_page_fault(struct pt_regs *regs, long err)
59 /* kprobe_running() needs smp_processor_id() */
60 if (!user_mode(regs)) {
62 if (kprobe_running() && kprobe_fault_handler(regs, 14))
70 static inline int notify_page_fault(struct pt_regs *regs, long err)
78 * Unlock any spinlocks which will prevent us from getting the
81 void bust_spinlocks(int yes)
86 int loglevel_save = console_loglevel;
90 * OK, the message is on the console. Now we call printk()
91 * without oops_in_progress set so that printk will give klogd
92 * a poke. Hold onto your hats...
94 console_loglevel = 15;
96 console_loglevel = loglevel_save;
101 * Returns the address space associated with the fault.
102 * Returns 0 for kernel space, 1 for user space and
103 * 2 for code execution in user space with noexec=on.
105 static inline int check_space(struct task_struct *tsk)
108 * The lowest two bits of S390_lowcore.trans_exc_code
109 * indicate which paging table was used.
111 int desc = S390_lowcore.trans_exc_code & 3;
113 if (desc == 3) /* Home Segment Table Descriptor */
114 return switch_amode == 0;
115 if (desc == 2) /* Secondary Segment Table Descriptor */
116 return tsk->thread.mm_segment.ar4;
117 #ifdef CONFIG_S390_SWITCH_AMODE
118 if (unlikely(desc == 1)) { /* STD determined via access register */
119 /* %a0 always indicates primary space. */
120 if (S390_lowcore.exc_access_id != 0) {
121 save_access_regs(tsk->thread.acrs);
123 * An alet of 0 indicates primary space.
124 * An alet of 1 indicates secondary space.
125 * Any other alet values generate an
126 * alen-translation exception.
128 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
129 return tsk->thread.mm_segment.ar4;
133 /* Primary Segment Table Descriptor */
134 return switch_amode << s390_noexec;
138 * Send SIGSEGV to task. This is an external routine
139 * to keep the stack usage of do_page_fault small.
141 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
142 int si_code, unsigned long address)
146 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
147 #if defined(CONFIG_SYSCTL)
148 if (sysctl_userprocess_debug)
151 printk("User process fault: interruption code 0x%lX\n",
153 printk("failing address: %lX\n", address);
157 si.si_signo = SIGSEGV;
158 si.si_code = si_code;
159 si.si_addr = (void __user *) address;
160 force_sig_info(SIGSEGV, &si, current);
163 static void do_no_context(struct pt_regs *regs, unsigned long error_code,
164 unsigned long address)
166 const struct exception_table_entry *fixup;
168 /* Are we prepared to handle this kernel fault? */
169 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
171 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
176 * Oops. The kernel tried to access some bad page. We'll have to
177 * terminate things with extreme prejudice.
179 if (check_space(current) == 0)
180 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
181 " at virtual kernel address %p\n", (void *)address);
183 printk(KERN_ALERT "Unable to handle kernel paging request"
184 " at virtual user address %p\n", (void *)address);
186 die("Oops", regs, error_code);
190 static void do_low_address(struct pt_regs *regs, unsigned long error_code)
192 /* Low-address protection hit in kernel mode means
193 NULL pointer write access in kernel mode. */
194 if (regs->psw.mask & PSW_MASK_PSTATE) {
195 /* Low-address protection hit in user mode 'cannot happen'. */
196 die ("Low-address protection", regs, error_code);
200 do_no_context(regs, error_code, 0);
204 * We ran out of memory, or some other thing happened to us that made
205 * us unable to handle the page fault gracefully.
207 static int do_out_of_memory(struct pt_regs *regs, unsigned long error_code,
208 unsigned long address)
210 struct task_struct *tsk = current;
211 struct mm_struct *mm = tsk->mm;
213 up_read(&mm->mmap_sem);
214 if (is_global_init(tsk)) {
216 down_read(&mm->mmap_sem);
219 printk("VM: killing process %s\n", tsk->comm);
220 if (regs->psw.mask & PSW_MASK_PSTATE)
221 do_group_exit(SIGKILL);
222 do_no_context(regs, error_code, address);
226 static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
227 unsigned long address)
229 struct task_struct *tsk = current;
230 struct mm_struct *mm = tsk->mm;
232 up_read(&mm->mmap_sem);
234 * Send a sigbus, regardless of whether we were in kernel
237 tsk->thread.prot_addr = address;
238 tsk->thread.trap_no = error_code;
239 force_sig(SIGBUS, tsk);
241 /* Kernel mode? Handle exceptions or die */
242 if (!(regs->psw.mask & PSW_MASK_PSTATE))
243 do_no_context(regs, error_code, address);
246 #ifdef CONFIG_S390_EXEC_PROTECT
247 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
248 unsigned long address, unsigned long error_code)
257 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
262 up_read(&mm->mmap_sem);
263 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
265 compat = test_tsk_thread_flag(current, TIF_31BIT);
266 if (compat && instruction == 0x0a77)
268 else if (compat && instruction == 0x0aad)
269 sys32_rt_sigreturn();
272 if (instruction == 0x0a77)
274 else if (instruction == 0x0aad)
277 current->thread.prot_addr = address;
278 current->thread.trap_no = error_code;
279 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
283 #endif /* CONFIG_S390_EXEC_PROTECT */
286 * This routine handles page faults. It determines the address,
287 * and the problem, and then passes it off to one of the appropriate
291 * 04 Protection -> Write-Protection (suprression)
292 * 10 Segment translation -> Not present (nullification)
293 * 11 Page translation -> Not present (nullification)
294 * 3b Region third trans. -> Not present (nullification)
297 do_exception(struct pt_regs *regs, unsigned long error_code, int write)
299 struct task_struct *tsk;
300 struct mm_struct *mm;
301 struct vm_area_struct *vma;
302 unsigned long address;
307 if (notify_page_fault(regs, error_code))
313 /* get the failing address and the affected space */
314 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
315 space = check_space(tsk);
318 * Verify that the fault happened in user space, that
319 * we are not in an interrupt and that there is a
322 if (unlikely(space == 0 || in_atomic() || !mm))
326 * When we get here, the fault happened in the current
327 * task's user address space, so we can switch on the
328 * interrupts again and then search the VMAs
332 down_read(&mm->mmap_sem);
334 si_code = SEGV_MAPERR;
335 vma = find_vma(mm, address);
339 #ifdef CONFIG_S390_EXEC_PROTECT
340 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
341 if (!signal_return(mm, regs, address, error_code))
343 * signal_return() has done an up_read(&mm->mmap_sem)
349 if (vma->vm_start <= address)
351 if (!(vma->vm_flags & VM_GROWSDOWN))
353 if (expand_stack(vma, address))
356 * Ok, we have a good vm_area for this memory access, so
360 si_code = SEGV_ACCERR;
362 /* page not present, check vm flags */
363 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
366 if (!(vma->vm_flags & VM_WRITE))
371 if (is_vm_hugetlb_page(vma))
372 address &= HPAGE_MASK;
374 * If for any reason at all we couldn't handle the fault,
375 * make sure we exit gracefully rather than endlessly redo
378 fault = handle_mm_fault(mm, vma, address, write);
379 if (unlikely(fault & VM_FAULT_ERROR)) {
380 if (fault & VM_FAULT_OOM) {
381 if (do_out_of_memory(regs, error_code, address))
384 } else if (fault & VM_FAULT_SIGBUS) {
385 do_sigbus(regs, error_code, address);
390 if (fault & VM_FAULT_MAJOR)
395 up_read(&mm->mmap_sem);
397 * The instruction that caused the program check will
398 * be repeated. Don't signal single step via SIGTRAP.
400 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
404 * Something tried to access memory that isn't in our memory map..
405 * Fix it, but check if it's kernel or user first..
408 up_read(&mm->mmap_sem);
410 /* User mode accesses just cause a SIGSEGV */
411 if (regs->psw.mask & PSW_MASK_PSTATE) {
412 tsk->thread.prot_addr = address;
413 tsk->thread.trap_no = error_code;
414 do_sigsegv(regs, error_code, si_code, address);
419 do_no_context(regs, error_code, address);
422 void __kprobes do_protection_exception(struct pt_regs *regs,
425 /* Protection exception is supressing, decrement psw address. */
426 regs->psw.addr -= (error_code >> 16);
428 * Check for low-address protection. This needs to be treated
429 * as a special case because the translation exception code
430 * field is not guaranteed to contain valid data in this case.
432 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
433 do_low_address(regs, error_code);
436 do_exception(regs, 4, 1);
439 void __kprobes do_dat_exception(struct pt_regs *regs, long error_code)
441 do_exception(regs, error_code & 0xff, 0);
445 void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
447 struct mm_struct *mm;
448 struct vm_area_struct *vma;
449 unsigned long address;
453 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
454 space = check_space(current);
456 if (unlikely(space == 0 || in_atomic() || !mm))
461 down_read(&mm->mmap_sem);
462 vma = find_vma(mm, address);
463 up_read(&mm->mmap_sem);
466 update_mm(mm, current);
470 /* User mode accesses just cause a SIGSEGV */
471 if (regs->psw.mask & PSW_MASK_PSTATE) {
472 current->thread.prot_addr = address;
473 current->thread.trap_no = error_code;
474 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
479 do_no_context(regs, error_code, address);
485 * 'pfault' pseudo page faults routines.
487 static ext_int_info_t ext_int_pfault;
488 static int pfault_disable = 0;
490 static int __init nopfault(char *str)
496 __setup("nopfault", nopfault);
507 } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
509 int pfault_init(void)
511 pfault_refbk_t refbk =
512 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
516 if (!MACHINE_IS_VM || pfault_disable)
519 " diag %1,%0,0x258\n"
524 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
529 void pfault_fini(void)
531 pfault_refbk_t refbk =
532 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
534 if (!MACHINE_IS_VM || pfault_disable)
536 __ctl_clear_bit(0,9);
541 : : "a" (&refbk), "m" (refbk) : "cc");
544 static void pfault_interrupt(__u16 error_code)
546 struct task_struct *tsk;
550 * Get the external interruption subcode & pfault
551 * initial/completion signal bit. VM stores this
552 * in the 'cpu address' field associated with the
553 * external interrupt.
555 subcode = S390_lowcore.cpu_addr;
556 if ((subcode & 0xff00) != __SUBCODE_MASK)
560 * Get the token (= address of the task structure of the affected task).
562 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
564 if (subcode & 0x0080) {
565 /* signal bit is set -> a page has been swapped in by VM */
566 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
567 /* Initial interrupt was faster than the completion
568 * interrupt. pfault_wait is valid. Set pfault_wait
569 * back to zero and wake up the process. This can
570 * safely be done because the task is still sleeping
571 * and can't produce new pfaults. */
572 tsk->thread.pfault_wait = 0;
573 wake_up_process(tsk);
574 put_task_struct(tsk);
577 /* signal bit not set -> a real page is missing. */
578 get_task_struct(tsk);
579 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
580 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
581 /* Completion interrupt was faster than the initial
582 * interrupt (swapped in a -1 for pfault_wait). Set
583 * pfault_wait back to zero and exit. This can be
584 * done safely because tsk is running in kernel
585 * mode and can't produce new pfaults. */
586 tsk->thread.pfault_wait = 0;
587 set_task_state(tsk, TASK_RUNNING);
588 put_task_struct(tsk);
590 set_tsk_need_resched(tsk);
594 void __init pfault_irq_init(void)
600 * Try to get pfault pseudo page faults going.
602 if (register_early_external_interrupt(0x2603, pfault_interrupt,
603 &ext_int_pfault) != 0)
604 panic("Couldn't request external interrupt 0x2603");
606 if (pfault_init() == 0)
609 /* Tough luck, no pfault. */
611 unregister_early_external_interrupt(0x2603, pfault_interrupt,