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>
32 #include <asm/system.h>
33 #include <asm/pgtable.h>
34 #include <asm/s390_ext.h>
35 #include <asm/mmu_context.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;
53 extern void die(const char *,struct pt_regs *,long);
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);
205 * We ran out of memory, or some other thing happened to us that made
206 * us unable to handle the page fault gracefully.
208 static int do_out_of_memory(struct pt_regs *regs, unsigned long error_code,
209 unsigned long address)
211 struct task_struct *tsk = current;
212 struct mm_struct *mm = tsk->mm;
214 up_read(&mm->mmap_sem);
215 if (is_global_init(tsk)) {
217 down_read(&mm->mmap_sem);
220 printk("VM: killing process %s\n", tsk->comm);
221 if (regs->psw.mask & PSW_MASK_PSTATE)
222 do_group_exit(SIGKILL);
223 do_no_context(regs, error_code, address);
227 static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
228 unsigned long address)
230 struct task_struct *tsk = current;
231 struct mm_struct *mm = tsk->mm;
233 up_read(&mm->mmap_sem);
235 * Send a sigbus, regardless of whether we were in kernel
238 tsk->thread.prot_addr = address;
239 tsk->thread.trap_no = error_code;
240 force_sig(SIGBUS, tsk);
242 /* Kernel mode? Handle exceptions or die */
243 if (!(regs->psw.mask & PSW_MASK_PSTATE))
244 do_no_context(regs, error_code, address);
247 #ifdef CONFIG_S390_EXEC_PROTECT
248 extern long sys_sigreturn(struct pt_regs *regs);
249 extern long sys_rt_sigreturn(struct pt_regs *regs);
250 extern long sys32_sigreturn(struct pt_regs *regs);
251 extern long sys32_rt_sigreturn(struct pt_regs *regs);
253 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
254 unsigned long address, unsigned long error_code)
263 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
268 up_read(&mm->mmap_sem);
269 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
271 compat = test_tsk_thread_flag(current, TIF_31BIT);
272 if (compat && instruction == 0x0a77)
273 sys32_sigreturn(regs);
274 else if (compat && instruction == 0x0aad)
275 sys32_rt_sigreturn(regs);
278 if (instruction == 0x0a77)
280 else if (instruction == 0x0aad)
281 sys_rt_sigreturn(regs);
283 current->thread.prot_addr = address;
284 current->thread.trap_no = error_code;
285 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
289 #endif /* CONFIG_S390_EXEC_PROTECT */
292 * This routine handles page faults. It determines the address,
293 * and the problem, and then passes it off to one of the appropriate
297 * 04 Protection -> Write-Protection (suprression)
298 * 10 Segment translation -> Not present (nullification)
299 * 11 Page translation -> Not present (nullification)
300 * 3b Region third trans. -> Not present (nullification)
303 do_exception(struct pt_regs *regs, unsigned long error_code, int write)
305 struct task_struct *tsk;
306 struct mm_struct *mm;
307 struct vm_area_struct *vma;
308 unsigned long address;
313 if (notify_page_fault(regs, error_code))
319 /* get the failing address and the affected space */
320 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
321 space = check_space(tsk);
324 * Verify that the fault happened in user space, that
325 * we are not in an interrupt and that there is a
328 if (unlikely(space == 0 || in_atomic() || !mm))
332 * When we get here, the fault happened in the current
333 * task's user address space, so we can switch on the
334 * interrupts again and then search the VMAs
338 down_read(&mm->mmap_sem);
340 si_code = SEGV_MAPERR;
341 vma = find_vma(mm, address);
345 #ifdef CONFIG_S390_EXEC_PROTECT
346 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
347 if (!signal_return(mm, regs, address, error_code))
349 * signal_return() has done an up_read(&mm->mmap_sem)
355 if (vma->vm_start <= address)
357 if (!(vma->vm_flags & VM_GROWSDOWN))
359 if (expand_stack(vma, address))
362 * Ok, we have a good vm_area for this memory access, so
366 si_code = SEGV_ACCERR;
368 /* page not present, check vm flags */
369 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
372 if (!(vma->vm_flags & VM_WRITE))
378 * If for any reason at all we couldn't handle the fault,
379 * make sure we exit gracefully rather than endlessly redo
382 fault = handle_mm_fault(mm, vma, address, write);
383 if (unlikely(fault & VM_FAULT_ERROR)) {
384 if (fault & VM_FAULT_OOM) {
385 if (do_out_of_memory(regs, error_code, address))
388 } else if (fault & VM_FAULT_SIGBUS) {
389 do_sigbus(regs, error_code, address);
394 if (fault & VM_FAULT_MAJOR)
399 up_read(&mm->mmap_sem);
401 * The instruction that caused the program check will
402 * be repeated. Don't signal single step via SIGTRAP.
404 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
408 * Something tried to access memory that isn't in our memory map..
409 * Fix it, but check if it's kernel or user first..
412 up_read(&mm->mmap_sem);
414 /* User mode accesses just cause a SIGSEGV */
415 if (regs->psw.mask & PSW_MASK_PSTATE) {
416 tsk->thread.prot_addr = address;
417 tsk->thread.trap_no = error_code;
418 do_sigsegv(regs, error_code, si_code, address);
423 do_no_context(regs, error_code, address);
426 void __kprobes do_protection_exception(struct pt_regs *regs,
427 unsigned long error_code)
429 /* Protection exception is supressing, decrement psw address. */
430 regs->psw.addr -= (error_code >> 16);
432 * Check for low-address protection. This needs to be treated
433 * as a special case because the translation exception code
434 * field is not guaranteed to contain valid data in this case.
436 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
437 do_low_address(regs, error_code);
440 do_exception(regs, 4, 1);
443 void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
445 do_exception(regs, error_code & 0xff, 0);
449 void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
451 struct mm_struct *mm;
452 struct vm_area_struct *vma;
453 unsigned long address;
457 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
458 space = check_space(current);
460 if (unlikely(space == 0 || in_atomic() || !mm))
465 down_read(&mm->mmap_sem);
466 vma = find_vma(mm, address);
467 up_read(&mm->mmap_sem);
470 update_mm(mm, current);
474 /* User mode accesses just cause a SIGSEGV */
475 if (regs->psw.mask & PSW_MASK_PSTATE) {
476 current->thread.prot_addr = address;
477 current->thread.trap_no = error_code;
478 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
483 do_no_context(regs, error_code, address);
489 * 'pfault' pseudo page faults routines.
491 static ext_int_info_t ext_int_pfault;
492 static int pfault_disable = 0;
494 static int __init nopfault(char *str)
500 __setup("nopfault", nopfault);
511 } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
513 int pfault_init(void)
515 pfault_refbk_t refbk =
516 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
520 if (!MACHINE_IS_VM || pfault_disable)
523 " diag %1,%0,0x258\n"
528 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
533 void pfault_fini(void)
535 pfault_refbk_t refbk =
536 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
538 if (!MACHINE_IS_VM || pfault_disable)
540 __ctl_clear_bit(0,9);
545 : : "a" (&refbk), "m" (refbk) : "cc");
548 static void pfault_interrupt(__u16 error_code)
550 struct task_struct *tsk;
554 * Get the external interruption subcode & pfault
555 * initial/completion signal bit. VM stores this
556 * in the 'cpu address' field associated with the
557 * external interrupt.
559 subcode = S390_lowcore.cpu_addr;
560 if ((subcode & 0xff00) != __SUBCODE_MASK)
564 * Get the token (= address of the task structure of the affected task).
566 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
568 if (subcode & 0x0080) {
569 /* signal bit is set -> a page has been swapped in by VM */
570 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
571 /* Initial interrupt was faster than the completion
572 * interrupt. pfault_wait is valid. Set pfault_wait
573 * back to zero and wake up the process. This can
574 * safely be done because the task is still sleeping
575 * and can't produce new pfaults. */
576 tsk->thread.pfault_wait = 0;
577 wake_up_process(tsk);
578 put_task_struct(tsk);
581 /* signal bit not set -> a real page is missing. */
582 get_task_struct(tsk);
583 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
584 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
585 /* Completion interrupt was faster than the initial
586 * interrupt (swapped in a -1 for pfault_wait). Set
587 * pfault_wait back to zero and exit. This can be
588 * done safely because tsk is running in kernel
589 * mode and can't produce new pfaults. */
590 tsk->thread.pfault_wait = 0;
591 set_task_state(tsk, TASK_RUNNING);
592 put_task_struct(tsk);
594 set_tsk_need_resched(tsk);
598 void __init pfault_irq_init(void)
604 * Try to get pfault pseudo page faults going.
606 if (register_early_external_interrupt(0x2603, pfault_interrupt,
607 &ext_int_pfault) != 0)
608 panic("Couldn't request external interrupt 0x2603");
610 if (pfault_init() == 0)
613 /* Tough luck, no pfault. */
615 unregister_early_external_interrupt(0x2603, pfault_interrupt,