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/config.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/smp.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>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
35 #define __FAIL_ADDR_MASK 0x7ffff000
36 #define __FIXUP_MASK 0x7fffffff
37 #define __SUBCODE_MASK 0x0200
38 #define __PF_RES_FIELD 0ULL
39 #else /* CONFIG_64BIT */
40 #define __FAIL_ADDR_MASK -4096L
41 #define __FIXUP_MASK ~0L
42 #define __SUBCODE_MASK 0x0600
43 #define __PF_RES_FIELD 0x8000000000000000ULL
44 #endif /* CONFIG_64BIT */
47 extern int sysctl_userprocess_debug
;
50 extern void die(const char *,struct pt_regs
*,long);
52 extern spinlock_t timerlist_lock
;
55 * Unlock any spinlocks which will prevent us from getting the
56 * message out (timerlist_lock is acquired through the
57 * console unblank code)
59 void bust_spinlocks(int yes
)
64 int loglevel_save
= console_loglevel
;
68 * OK, the message is on the console. Now we call printk()
69 * without oops_in_progress set so that printk will give klogd
70 * a poke. Hold onto your hats...
72 console_loglevel
= 15;
74 console_loglevel
= loglevel_save
;
79 * Check which address space is addressed by the access
80 * register in S390_lowcore.exc_access_id.
81 * Returns 1 for user space and 0 for kernel space.
83 static int __check_access_register(struct pt_regs
*regs
, int error_code
)
85 int areg
= S390_lowcore
.exc_access_id
;
88 /* Access via access register 0 -> kernel address */
90 save_access_regs(current
->thread
.acrs
);
91 if (regs
&& areg
< NUM_ACRS
&& current
->thread
.acrs
[areg
] <= 1)
93 * access register contains 0 -> kernel address,
94 * access register contains 1 -> user space address
96 return current
->thread
.acrs
[areg
];
98 /* Something unhealthy was done with the access registers... */
99 die("page fault via unknown access register", regs
, error_code
);
105 * Check which address space the address belongs to.
106 * Returns 1 for user space and 0 for kernel space.
108 static inline int check_user_space(struct pt_regs
*regs
, int error_code
)
111 * The lowest two bits of S390_lowcore.trans_exc_code indicate
112 * which paging table was used:
113 * 0: Primary Segment Table Descriptor
114 * 1: STD determined via access register
115 * 2: Secondary Segment Table Descriptor
116 * 3: Home Segment Table Descriptor
118 int descriptor
= S390_lowcore
.trans_exc_code
& 3;
119 if (unlikely(descriptor
== 1))
120 return __check_access_register(regs
, error_code
);
122 return current
->thread
.mm_segment
.ar4
;
123 return descriptor
!= 0;
127 * Send SIGSEGV to task. This is an external routine
128 * to keep the stack usage of do_page_fault small.
130 static void do_sigsegv(struct pt_regs
*regs
, unsigned long error_code
,
131 int si_code
, unsigned long address
)
135 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
136 #if defined(CONFIG_SYSCTL)
137 if (sysctl_userprocess_debug
)
140 printk("User process fault: interruption code 0x%lX\n",
142 printk("failing address: %lX\n", address
);
146 si
.si_signo
= SIGSEGV
;
147 si
.si_code
= si_code
;
148 si
.si_addr
= (void *) address
;
149 force_sig_info(SIGSEGV
, &si
, current
);
153 * This routine handles page faults. It determines the address,
154 * and the problem, and then passes it off to one of the appropriate
158 * 04 Protection -> Write-Protection (suprression)
159 * 10 Segment translation -> Not present (nullification)
160 * 11 Page translation -> Not present (nullification)
161 * 3b Region third trans. -> Not present (nullification)
164 do_exception(struct pt_regs
*regs
, unsigned long error_code
, int is_protection
)
166 struct task_struct
*tsk
;
167 struct mm_struct
*mm
;
168 struct vm_area_struct
* vma
;
169 unsigned long address
;
171 const struct exception_table_entry
*fixup
;
172 int si_code
= SEGV_MAPERR
;
178 * Check for low-address protection. This needs to be treated
179 * as a special case because the translation exception code
180 * field is not guaranteed to contain valid data in this case.
182 if (is_protection
&& !(S390_lowcore
.trans_exc_code
& 4)) {
184 /* Low-address protection hit in kernel mode means
185 NULL pointer write access in kernel mode. */
186 if (!(regs
->psw
.mask
& PSW_MASK_PSTATE
)) {
192 /* Low-address protection hit in user mode 'cannot happen'. */
193 die ("Low-address protection", regs
, error_code
);
198 * get the failing address
199 * more specific the segment and page table portion of
202 address
= S390_lowcore
.trans_exc_code
& __FAIL_ADDR_MASK
;
203 user_address
= check_user_space(regs
, error_code
);
206 * Verify that the fault happened in user space, that
207 * we are not in an interrupt and that there is a
210 if (user_address
== 0 || in_atomic() || !mm
)
214 * When we get here, the fault happened in the current
215 * task's user address space, so we can switch on the
216 * interrupts again and then search the VMAs
220 down_read(&mm
->mmap_sem
);
222 vma
= find_vma(mm
, address
);
225 if (vma
->vm_start
<= address
)
227 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
229 if (expand_stack(vma
, address
))
232 * Ok, we have a good vm_area for this memory access, so
236 si_code
= SEGV_ACCERR
;
237 if (!is_protection
) {
238 /* page not present, check vm flags */
239 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)))
242 if (!(vma
->vm_flags
& VM_WRITE
))
248 * If for any reason at all we couldn't handle the fault,
249 * make sure we exit gracefully rather than endlessly redo
252 switch (handle_mm_fault(mm
, vma
, address
, is_protection
)) {
259 case VM_FAULT_SIGBUS
:
267 up_read(&mm
->mmap_sem
);
269 * The instruction that caused the program check will
270 * be repeated. Don't signal single step via SIGTRAP.
272 clear_tsk_thread_flag(current
, TIF_SINGLE_STEP
);
276 * Something tried to access memory that isn't in our memory map..
277 * Fix it, but check if it's kernel or user first..
280 up_read(&mm
->mmap_sem
);
282 /* User mode accesses just cause a SIGSEGV */
283 if (regs
->psw
.mask
& PSW_MASK_PSTATE
) {
284 tsk
->thread
.prot_addr
= address
;
285 tsk
->thread
.trap_no
= error_code
;
286 do_sigsegv(regs
, error_code
, si_code
, address
);
291 /* Are we prepared to handle this kernel fault? */
292 fixup
= search_exception_tables(regs
->psw
.addr
& __FIXUP_MASK
);
294 regs
->psw
.addr
= fixup
->fixup
| PSW_ADDR_AMODE
;
299 * Oops. The kernel tried to access some bad page. We'll have to
300 * terminate things with extreme prejudice.
302 if (user_address
== 0)
303 printk(KERN_ALERT
"Unable to handle kernel pointer dereference"
304 " at virtual kernel address %p\n", (void *)address
);
306 printk(KERN_ALERT
"Unable to handle kernel paging request"
307 " at virtual user address %p\n", (void *)address
);
309 die("Oops", regs
, error_code
);
314 * We ran out of memory, or some other thing happened to us that made
315 * us unable to handle the page fault gracefully.
318 up_read(&mm
->mmap_sem
);
323 printk("VM: killing process %s\n", tsk
->comm
);
324 if (regs
->psw
.mask
& PSW_MASK_PSTATE
)
329 up_read(&mm
->mmap_sem
);
332 * Send a sigbus, regardless of whether we were in kernel
335 tsk
->thread
.prot_addr
= address
;
336 tsk
->thread
.trap_no
= error_code
;
337 force_sig(SIGBUS
, tsk
);
339 /* Kernel mode? Handle exceptions or die */
340 if (!(regs
->psw
.mask
& PSW_MASK_PSTATE
))
344 void do_protection_exception(struct pt_regs
*regs
, unsigned long error_code
)
346 regs
->psw
.addr
-= (error_code
>> 16);
347 do_exception(regs
, 4, 1);
350 void do_dat_exception(struct pt_regs
*regs
, unsigned long error_code
)
352 do_exception(regs
, error_code
& 0xff, 0);
357 * 'pfault' pseudo page faults routines.
359 static int pfault_disable
= 0;
361 static int __init
nopfault(char *str
)
367 __setup("nopfault", nopfault
);
378 } __attribute__ ((packed
)) pfault_refbk_t
;
380 int pfault_init(void)
382 pfault_refbk_t refbk
=
383 { 0x258, 0, 5, 2, __LC_CURRENT
, 1ULL << 48, 1ULL << 48,
389 __asm__
__volatile__(
390 " diag %1,%0,0x258\n"
394 ".section __ex_table,\"a\"\n"
398 #else /* CONFIG_64BIT */
400 #endif /* CONFIG_64BIT */
402 : "=d" (rc
) : "a" (&refbk
), "m" (refbk
) : "cc" );
407 void pfault_fini(void)
409 pfault_refbk_t refbk
=
410 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
414 __ctl_clear_bit(0,9);
415 __asm__
__volatile__(
418 ".section __ex_table,\"a\"\n"
422 #else /* CONFIG_64BIT */
424 #endif /* CONFIG_64BIT */
426 : : "a" (&refbk
), "m" (refbk
) : "cc" );
430 pfault_interrupt(struct pt_regs
*regs
, __u16 error_code
)
432 struct task_struct
*tsk
;
436 * Get the external interruption subcode & pfault
437 * initial/completion signal bit. VM stores this
438 * in the 'cpu address' field associated with the
439 * external interrupt.
441 subcode
= S390_lowcore
.cpu_addr
;
442 if ((subcode
& 0xff00) != __SUBCODE_MASK
)
446 * Get the token (= address of the task structure of the affected task).
448 tsk
= *(struct task_struct
**) __LC_PFAULT_INTPARM
;
450 if (subcode
& 0x0080) {
451 /* signal bit is set -> a page has been swapped in by VM */
452 if (xchg(&tsk
->thread
.pfault_wait
, -1) != 0) {
453 /* Initial interrupt was faster than the completion
454 * interrupt. pfault_wait is valid. Set pfault_wait
455 * back to zero and wake up the process. This can
456 * safely be done because the task is still sleeping
457 * and can't produce new pfaults. */
458 tsk
->thread
.pfault_wait
= 0;
459 wake_up_process(tsk
);
460 put_task_struct(tsk
);
463 /* signal bit not set -> a real page is missing. */
464 get_task_struct(tsk
);
465 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
466 if (xchg(&tsk
->thread
.pfault_wait
, 1) != 0) {
467 /* Completion interrupt was faster than the initial
468 * interrupt (swapped in a -1 for pfault_wait). Set
469 * pfault_wait back to zero and exit. This can be
470 * done safely because tsk is running in kernel
471 * mode and can't produce new pfaults. */
472 tsk
->thread
.pfault_wait
= 0;
473 set_task_state(tsk
, TASK_RUNNING
);
474 put_task_struct(tsk
);
476 set_tsk_need_resched(tsk
);