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/smp_lock.h>
24 #include <linux/init.h>
25 #include <linux/console.h>
26 #include <linux/module.h>
27 #include <linux/hardirq.h>
28 #include <linux/kprobes.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/kdebug.h>
34 #include <asm/s390_ext.h>
37 #define __FAIL_ADDR_MASK 0x7ffff000
38 #define __FIXUP_MASK 0x7fffffff
39 #define __SUBCODE_MASK 0x0200
40 #define __PF_RES_FIELD 0ULL
41 #else /* CONFIG_64BIT */
42 #define __FAIL_ADDR_MASK -4096L
43 #define __FIXUP_MASK ~0L
44 #define __SUBCODE_MASK 0x0600
45 #define __PF_RES_FIELD 0x8000000000000000ULL
46 #endif /* CONFIG_64BIT */
49 extern int sysctl_userprocess_debug;
52 extern void die(const char *,struct pt_regs *,long);
55 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
56 int register_page_fault_notifier(struct notifier_block *nb)
58 return atomic_notifier_chain_register(¬ify_page_fault_chain, nb);
61 int unregister_page_fault_notifier(struct notifier_block *nb)
63 return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb);
66 static inline int notify_page_fault(enum die_val val, const char *str,
67 struct pt_regs *regs, long err, int trap, int sig)
69 struct die_args args = {
76 return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args);
79 static inline int notify_page_fault(enum die_val val, const char *str,
80 struct pt_regs *regs, long err, int trap, int sig)
86 extern spinlock_t timerlist_lock;
89 * Unlock any spinlocks which will prevent us from getting the
90 * message out (timerlist_lock is acquired through the
91 * console unblank code)
93 void bust_spinlocks(int yes)
98 int loglevel_save = console_loglevel;
100 oops_in_progress = 0;
102 * OK, the message is on the console. Now we call printk()
103 * without oops_in_progress set so that printk will give klogd
104 * a poke. Hold onto your hats...
106 console_loglevel = 15;
108 console_loglevel = loglevel_save;
113 * Check which address space is addressed by the access
114 * register in S390_lowcore.exc_access_id.
115 * Returns 1 for user space and 0 for kernel space.
117 static int __check_access_register(struct pt_regs *regs, int error_code)
119 int areg = S390_lowcore.exc_access_id;
122 /* Access via access register 0 -> kernel address */
124 save_access_regs(current->thread.acrs);
125 if (regs && areg < NUM_ACRS && current->thread.acrs[areg] <= 1)
127 * access register contains 0 -> kernel address,
128 * access register contains 1 -> user space address
130 return current->thread.acrs[areg];
132 /* Something unhealthy was done with the access registers... */
133 die("page fault via unknown access register", regs, error_code);
139 * Check which address space the address belongs to.
140 * Returns 1 for user space and 0 for kernel space.
142 static inline int check_user_space(struct pt_regs *regs, int error_code)
145 * The lowest two bits of S390_lowcore.trans_exc_code indicate
146 * which paging table was used:
147 * 0: Primary Segment Table Descriptor
148 * 1: STD determined via access register
149 * 2: Secondary Segment Table Descriptor
150 * 3: Home Segment Table Descriptor
152 int descriptor = S390_lowcore.trans_exc_code & 3;
153 if (unlikely(descriptor == 1))
154 return __check_access_register(regs, error_code);
156 return current->thread.mm_segment.ar4;
157 return descriptor != 0;
161 * Send SIGSEGV to task. This is an external routine
162 * to keep the stack usage of do_page_fault small.
164 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
165 int si_code, unsigned long address)
169 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
170 #if defined(CONFIG_SYSCTL)
171 if (sysctl_userprocess_debug)
174 printk("User process fault: interruption code 0x%lX\n",
176 printk("failing address: %lX\n", address);
180 si.si_signo = SIGSEGV;
181 si.si_code = si_code;
182 si.si_addr = (void __user *) address;
183 force_sig_info(SIGSEGV, &si, current);
187 * This routine handles page faults. It determines the address,
188 * and the problem, and then passes it off to one of the appropriate
192 * 04 Protection -> Write-Protection (suprression)
193 * 10 Segment translation -> Not present (nullification)
194 * 11 Page translation -> Not present (nullification)
195 * 3b Region third trans. -> Not present (nullification)
197 static inline void __kprobes
198 do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
200 struct task_struct *tsk;
201 struct mm_struct *mm;
202 struct vm_area_struct * vma;
203 unsigned long address;
205 const struct exception_table_entry *fixup;
206 int si_code = SEGV_MAPERR;
211 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
212 SIGSEGV) == NOTIFY_STOP)
216 * Check for low-address protection. This needs to be treated
217 * as a special case because the translation exception code
218 * field is not guaranteed to contain valid data in this case.
220 if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
222 /* Low-address protection hit in kernel mode means
223 NULL pointer write access in kernel mode. */
224 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
230 /* Low-address protection hit in user mode 'cannot happen'. */
231 die ("Low-address protection", regs, error_code);
236 * get the failing address
237 * more specific the segment and page table portion of
240 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
241 user_address = check_user_space(regs, error_code);
244 * Verify that the fault happened in user space, that
245 * we are not in an interrupt and that there is a
248 if (user_address == 0 || in_atomic() || !mm)
252 * When we get here, the fault happened in the current
253 * task's user address space, so we can switch on the
254 * interrupts again and then search the VMAs
258 down_read(&mm->mmap_sem);
260 vma = find_vma(mm, address);
263 if (vma->vm_start <= address)
265 if (!(vma->vm_flags & VM_GROWSDOWN))
267 if (expand_stack(vma, address))
270 * Ok, we have a good vm_area for this memory access, so
274 si_code = SEGV_ACCERR;
275 if (!is_protection) {
276 /* page not present, check vm flags */
277 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
280 if (!(vma->vm_flags & VM_WRITE))
286 * If for any reason at all we couldn't handle the fault,
287 * make sure we exit gracefully rather than endlessly redo
290 switch (handle_mm_fault(mm, vma, address, is_protection)) {
297 case VM_FAULT_SIGBUS:
305 up_read(&mm->mmap_sem);
307 * The instruction that caused the program check will
308 * be repeated. Don't signal single step via SIGTRAP.
310 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
314 * Something tried to access memory that isn't in our memory map..
315 * Fix it, but check if it's kernel or user first..
318 up_read(&mm->mmap_sem);
320 /* User mode accesses just cause a SIGSEGV */
321 if (regs->psw.mask & PSW_MASK_PSTATE) {
322 tsk->thread.prot_addr = address;
323 tsk->thread.trap_no = error_code;
324 do_sigsegv(regs, error_code, si_code, address);
329 /* Are we prepared to handle this kernel fault? */
330 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
332 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
337 * Oops. The kernel tried to access some bad page. We'll have to
338 * terminate things with extreme prejudice.
340 if (user_address == 0)
341 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
342 " at virtual kernel address %p\n", (void *)address);
344 printk(KERN_ALERT "Unable to handle kernel paging request"
345 " at virtual user address %p\n", (void *)address);
347 die("Oops", regs, error_code);
352 * We ran out of memory, or some other thing happened to us that made
353 * us unable to handle the page fault gracefully.
356 up_read(&mm->mmap_sem);
359 down_read(&mm->mmap_sem);
362 printk("VM: killing process %s\n", tsk->comm);
363 if (regs->psw.mask & PSW_MASK_PSTATE)
368 up_read(&mm->mmap_sem);
371 * Send a sigbus, regardless of whether we were in kernel
374 tsk->thread.prot_addr = address;
375 tsk->thread.trap_no = error_code;
376 force_sig(SIGBUS, tsk);
378 /* Kernel mode? Handle exceptions or die */
379 if (!(regs->psw.mask & PSW_MASK_PSTATE))
383 void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
385 regs->psw.addr -= (error_code >> 16);
386 do_exception(regs, 4, 1);
389 void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
391 do_exception(regs, error_code & 0xff, 0);
396 * 'pfault' pseudo page faults routines.
398 static ext_int_info_t ext_int_pfault;
399 static int pfault_disable = 0;
401 static int __init nopfault(char *str)
407 __setup("nopfault", nopfault);
418 } __attribute__ ((packed)) pfault_refbk_t;
420 int pfault_init(void)
422 pfault_refbk_t refbk =
423 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
427 if (!MACHINE_IS_VM || pfault_disable)
430 " diag %1,%0,0x258\n"
435 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
440 void pfault_fini(void)
442 pfault_refbk_t refbk =
443 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
445 if (!MACHINE_IS_VM || pfault_disable)
447 __ctl_clear_bit(0,9);
452 : : "a" (&refbk), "m" (refbk) : "cc");
456 pfault_interrupt(__u16 error_code)
458 struct task_struct *tsk;
462 * Get the external interruption subcode & pfault
463 * initial/completion signal bit. VM stores this
464 * in the 'cpu address' field associated with the
465 * external interrupt.
467 subcode = S390_lowcore.cpu_addr;
468 if ((subcode & 0xff00) != __SUBCODE_MASK)
472 * Get the token (= address of the task structure of the affected task).
474 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
476 if (subcode & 0x0080) {
477 /* signal bit is set -> a page has been swapped in by VM */
478 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
479 /* Initial interrupt was faster than the completion
480 * interrupt. pfault_wait is valid. Set pfault_wait
481 * back to zero and wake up the process. This can
482 * safely be done because the task is still sleeping
483 * and can't produce new pfaults. */
484 tsk->thread.pfault_wait = 0;
485 wake_up_process(tsk);
486 put_task_struct(tsk);
489 /* signal bit not set -> a real page is missing. */
490 get_task_struct(tsk);
491 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
492 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
493 /* Completion interrupt was faster than the initial
494 * interrupt (swapped in a -1 for pfault_wait). Set
495 * pfault_wait back to zero and exit. This can be
496 * done safely because tsk is running in kernel
497 * mode and can't produce new pfaults. */
498 tsk->thread.pfault_wait = 0;
499 set_task_state(tsk, TASK_RUNNING);
500 put_task_struct(tsk);
502 set_tsk_need_resched(tsk);
506 void __init pfault_irq_init(void)
512 * Try to get pfault pseudo page faults going.
514 if (register_early_external_interrupt(0x2603, pfault_interrupt,
515 &ext_int_pfault) != 0)
516 panic("Couldn't request external interrupt 0x2603");
518 if (pfault_init() == 0)
521 /* Tough luck, no pfault. */
523 unregister_early_external_interrupt(0x2603, pfault_interrupt,