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>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
34 #define __FAIL_ADDR_MASK 0x7ffff000
35 #define __FIXUP_MASK 0x7fffffff
36 #define __SUBCODE_MASK 0x0200
37 #define __PF_RES_FIELD 0ULL
38 #else /* CONFIG_64BIT */
39 #define __FAIL_ADDR_MASK -4096L
40 #define __FIXUP_MASK ~0L
41 #define __SUBCODE_MASK 0x0600
42 #define __PF_RES_FIELD 0x8000000000000000ULL
43 #endif /* CONFIG_64BIT */
46 extern int sysctl_userprocess_debug
;
49 extern void die(const char *,struct pt_regs
*,long);
51 extern spinlock_t timerlist_lock
;
54 * Unlock any spinlocks which will prevent us from getting the
55 * message out (timerlist_lock is acquired through the
56 * console unblank code)
58 void bust_spinlocks(int yes
)
63 int loglevel_save
= console_loglevel
;
67 * OK, the message is on the console. Now we call printk()
68 * without oops_in_progress set so that printk will give klogd
69 * a poke. Hold onto your hats...
71 console_loglevel
= 15;
73 console_loglevel
= loglevel_save
;
78 * Check which address space is addressed by the access
79 * register in S390_lowcore.exc_access_id.
80 * Returns 1 for user space and 0 for kernel space.
82 static int __check_access_register(struct pt_regs
*regs
, int error_code
)
84 int areg
= S390_lowcore
.exc_access_id
;
87 /* Access via access register 0 -> kernel address */
89 save_access_regs(current
->thread
.acrs
);
90 if (regs
&& areg
< NUM_ACRS
&& current
->thread
.acrs
[areg
] <= 1)
92 * access register contains 0 -> kernel address,
93 * access register contains 1 -> user space address
95 return current
->thread
.acrs
[areg
];
97 /* Something unhealthy was done with the access registers... */
98 die("page fault via unknown access register", regs
, error_code
);
104 * Check which address space the address belongs to.
105 * Returns 1 for user space and 0 for kernel space.
107 static inline int check_user_space(struct pt_regs
*regs
, int error_code
)
110 * The lowest two bits of S390_lowcore.trans_exc_code indicate
111 * which paging table was used:
112 * 0: Primary Segment Table Descriptor
113 * 1: STD determined via access register
114 * 2: Secondary Segment Table Descriptor
115 * 3: Home Segment Table Descriptor
117 int descriptor
= S390_lowcore
.trans_exc_code
& 3;
118 if (unlikely(descriptor
== 1))
119 return __check_access_register(regs
, error_code
);
121 return current
->thread
.mm_segment
.ar4
;
122 return descriptor
!= 0;
126 * Send SIGSEGV to task. This is an external routine
127 * to keep the stack usage of do_page_fault small.
129 static void do_sigsegv(struct pt_regs
*regs
, unsigned long error_code
,
130 int si_code
, unsigned long address
)
134 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
135 #if defined(CONFIG_SYSCTL)
136 if (sysctl_userprocess_debug
)
139 printk("User process fault: interruption code 0x%lX\n",
141 printk("failing address: %lX\n", address
);
145 si
.si_signo
= SIGSEGV
;
146 si
.si_code
= si_code
;
147 si
.si_addr
= (void *) address
;
148 force_sig_info(SIGSEGV
, &si
, current
);
152 * This routine handles page faults. It determines the address,
153 * and the problem, and then passes it off to one of the appropriate
157 * 04 Protection -> Write-Protection (suprression)
158 * 10 Segment translation -> Not present (nullification)
159 * 11 Page translation -> Not present (nullification)
160 * 3b Region third trans. -> Not present (nullification)
163 do_exception(struct pt_regs
*regs
, unsigned long error_code
, int is_protection
)
165 struct task_struct
*tsk
;
166 struct mm_struct
*mm
;
167 struct vm_area_struct
* vma
;
168 unsigned long address
;
170 const struct exception_table_entry
*fixup
;
171 int si_code
= SEGV_MAPERR
;
177 * Check for low-address protection. This needs to be treated
178 * as a special case because the translation exception code
179 * field is not guaranteed to contain valid data in this case.
181 if (is_protection
&& !(S390_lowcore
.trans_exc_code
& 4)) {
183 /* Low-address protection hit in kernel mode means
184 NULL pointer write access in kernel mode. */
185 if (!(regs
->psw
.mask
& PSW_MASK_PSTATE
)) {
191 /* Low-address protection hit in user mode 'cannot happen'. */
192 die ("Low-address protection", regs
, error_code
);
197 * get the failing address
198 * more specific the segment and page table portion of
201 address
= S390_lowcore
.trans_exc_code
& __FAIL_ADDR_MASK
;
202 user_address
= check_user_space(regs
, error_code
);
205 * Verify that the fault happened in user space, that
206 * we are not in an interrupt and that there is a
209 if (user_address
== 0 || in_atomic() || !mm
)
213 * When we get here, the fault happened in the current
214 * task's user address space, so we can switch on the
215 * interrupts again and then search the VMAs
219 down_read(&mm
->mmap_sem
);
221 vma
= find_vma(mm
, address
);
224 if (vma
->vm_start
<= address
)
226 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
228 if (expand_stack(vma
, address
))
231 * Ok, we have a good vm_area for this memory access, so
235 si_code
= SEGV_ACCERR
;
236 if (!is_protection
) {
237 /* page not present, check vm flags */
238 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)))
241 if (!(vma
->vm_flags
& VM_WRITE
))
247 * If for any reason at all we couldn't handle the fault,
248 * make sure we exit gracefully rather than endlessly redo
251 switch (handle_mm_fault(mm
, vma
, address
, is_protection
)) {
258 case VM_FAULT_SIGBUS
:
266 up_read(&mm
->mmap_sem
);
268 * The instruction that caused the program check will
269 * be repeated. Don't signal single step via SIGTRAP.
271 clear_tsk_thread_flag(current
, TIF_SINGLE_STEP
);
275 * Something tried to access memory that isn't in our memory map..
276 * Fix it, but check if it's kernel or user first..
279 up_read(&mm
->mmap_sem
);
281 /* User mode accesses just cause a SIGSEGV */
282 if (regs
->psw
.mask
& PSW_MASK_PSTATE
) {
283 tsk
->thread
.prot_addr
= address
;
284 tsk
->thread
.trap_no
= error_code
;
285 do_sigsegv(regs
, error_code
, si_code
, address
);
290 /* Are we prepared to handle this kernel fault? */
291 fixup
= search_exception_tables(regs
->psw
.addr
& __FIXUP_MASK
);
293 regs
->psw
.addr
= fixup
->fixup
| PSW_ADDR_AMODE
;
298 * Oops. The kernel tried to access some bad page. We'll have to
299 * terminate things with extreme prejudice.
301 if (user_address
== 0)
302 printk(KERN_ALERT
"Unable to handle kernel pointer dereference"
303 " at virtual kernel address %p\n", (void *)address
);
305 printk(KERN_ALERT
"Unable to handle kernel paging request"
306 " at virtual user address %p\n", (void *)address
);
308 die("Oops", regs
, error_code
);
313 * We ran out of memory, or some other thing happened to us that made
314 * us unable to handle the page fault gracefully.
317 up_read(&mm
->mmap_sem
);
322 printk("VM: killing process %s\n", tsk
->comm
);
323 if (regs
->psw
.mask
& PSW_MASK_PSTATE
)
328 up_read(&mm
->mmap_sem
);
331 * Send a sigbus, regardless of whether we were in kernel
334 tsk
->thread
.prot_addr
= address
;
335 tsk
->thread
.trap_no
= error_code
;
336 force_sig(SIGBUS
, tsk
);
338 /* Kernel mode? Handle exceptions or die */
339 if (!(regs
->psw
.mask
& PSW_MASK_PSTATE
))
343 void do_protection_exception(struct pt_regs
*regs
, unsigned long error_code
)
345 regs
->psw
.addr
-= (error_code
>> 16);
346 do_exception(regs
, 4, 1);
349 void do_dat_exception(struct pt_regs
*regs
, unsigned long error_code
)
351 do_exception(regs
, error_code
& 0xff, 0);
356 * 'pfault' pseudo page faults routines.
358 static int pfault_disable
= 0;
360 static int __init
nopfault(char *str
)
366 __setup("nopfault", nopfault
);
377 } __attribute__ ((packed
)) pfault_refbk_t
;
379 int pfault_init(void)
381 pfault_refbk_t refbk
=
382 { 0x258, 0, 5, 2, __LC_CURRENT
, 1ULL << 48, 1ULL << 48,
388 __asm__
__volatile__(
389 " diag %1,%0,0x258\n"
393 ".section __ex_table,\"a\"\n"
397 #else /* CONFIG_64BIT */
399 #endif /* CONFIG_64BIT */
401 : "=d" (rc
) : "a" (&refbk
), "m" (refbk
) : "cc" );
406 void pfault_fini(void)
408 pfault_refbk_t refbk
=
409 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
413 __ctl_clear_bit(0,9);
414 __asm__
__volatile__(
417 ".section __ex_table,\"a\"\n"
421 #else /* CONFIG_64BIT */
423 #endif /* CONFIG_64BIT */
425 : : "a" (&refbk
), "m" (refbk
) : "cc" );
429 pfault_interrupt(struct pt_regs
*regs
, __u16 error_code
)
431 struct task_struct
*tsk
;
435 * Get the external interruption subcode & pfault
436 * initial/completion signal bit. VM stores this
437 * in the 'cpu address' field associated with the
438 * external interrupt.
440 subcode
= S390_lowcore
.cpu_addr
;
441 if ((subcode
& 0xff00) != __SUBCODE_MASK
)
445 * Get the token (= address of the task structure of the affected task).
447 tsk
= *(struct task_struct
**) __LC_PFAULT_INTPARM
;
449 if (subcode
& 0x0080) {
450 /* signal bit is set -> a page has been swapped in by VM */
451 if (xchg(&tsk
->thread
.pfault_wait
, -1) != 0) {
452 /* Initial interrupt was faster than the completion
453 * interrupt. pfault_wait is valid. Set pfault_wait
454 * back to zero and wake up the process. This can
455 * safely be done because the task is still sleeping
456 * and can't produce new pfaults. */
457 tsk
->thread
.pfault_wait
= 0;
458 wake_up_process(tsk
);
459 put_task_struct(tsk
);
462 /* signal bit not set -> a real page is missing. */
463 get_task_struct(tsk
);
464 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
465 if (xchg(&tsk
->thread
.pfault_wait
, 1) != 0) {
466 /* Completion interrupt was faster than the initial
467 * interrupt (swapped in a -1 for pfault_wait). Set
468 * pfault_wait back to zero and exit. This can be
469 * done safely because tsk is running in kernel
470 * mode and can't produce new pfaults. */
471 tsk
->thread
.pfault_wait
= 0;
472 set_task_state(tsk
, TASK_RUNNING
);
473 put_task_struct(tsk
);
475 set_tsk_need_resched(tsk
);