]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/s390/mm/fault.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 into for-linus
[mirror_ubuntu-jammy-kernel.git] / arch / s390 / mm / fault.c
CommitLineData
1da177e4
LT
1/*
2 * arch/s390/mm/fault.c
3 *
4 * S390 version
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)
8 *
9 * Derived from "arch/i386/mm/fault.c"
10 * Copyright (C) 1995 Linus Torvalds
11 */
12
1da177e4
LT
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>
21#include <linux/mm.h>
22#include <linux/smp.h>
1eeb66a1 23#include <linux/kdebug.h>
1da177e4
LT
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>
4ba069b8 29#include <linux/kprobes.h>
be5ec363 30#include <linux/uaccess.h>
1da177e4
LT
31
32#include <asm/system.h>
1da177e4 33#include <asm/pgtable.h>
29b08d2b 34#include <asm/s390_ext.h>
6252d702 35#include <asm/mmu_context.h>
1da177e4 36
347a8dc3 37#ifndef CONFIG_64BIT
1da177e4
LT
38#define __FAIL_ADDR_MASK 0x7ffff000
39#define __FIXUP_MASK 0x7fffffff
40#define __SUBCODE_MASK 0x0200
41#define __PF_RES_FIELD 0ULL
347a8dc3 42#else /* CONFIG_64BIT */
1da177e4
LT
43#define __FAIL_ADDR_MASK -4096L
44#define __FIXUP_MASK ~0L
45#define __SUBCODE_MASK 0x0600
46#define __PF_RES_FIELD 0x8000000000000000ULL
347a8dc3 47#endif /* CONFIG_64BIT */
1da177e4
LT
48
49#ifdef CONFIG_SYSCTL
50extern int sysctl_userprocess_debug;
51#endif
52
53extern void die(const char *,struct pt_regs *,long);
54
4ba069b8 55#ifdef CONFIG_KPROBES
10c1031f
MS
56static inline int notify_page_fault(struct pt_regs *regs, long err)
57{
33464e3b
CH
58 int ret = 0;
59
60 /* kprobe_running() needs smp_processor_id() */
61 if (!user_mode(regs)) {
62 preempt_disable();
63 if (kprobe_running() && kprobe_fault_handler(regs, 14))
64 ret = 1;
65 preempt_enable();
66 }
67
68 return ret;
4ba069b8
MG
69}
70#else
10c1031f 71static inline int notify_page_fault(struct pt_regs *regs, long err)
4ba069b8 72{
33464e3b 73 return 0;
4ba069b8
MG
74}
75#endif
76
1da177e4
LT
77
78/*
79 * Unlock any spinlocks which will prevent us from getting the
cefc8be8 80 * message out.
1da177e4
LT
81 */
82void bust_spinlocks(int yes)
83{
84 if (yes) {
85 oops_in_progress = 1;
86 } else {
87 int loglevel_save = console_loglevel;
88 console_unblank();
89 oops_in_progress = 0;
90 /*
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...
94 */
95 console_loglevel = 15;
96 printk(" ");
97 console_loglevel = loglevel_save;
98 }
99}
100
101/*
482b05dd
GS
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.
1da177e4 105 */
482b05dd 106static inline int check_space(struct task_struct *tsk)
1da177e4
LT
107{
108 /*
482b05dd
GS
109 * The lowest two bits of S390_lowcore.trans_exc_code
110 * indicate which paging table was used.
1da177e4 111 */
482b05dd
GS
112 int desc = S390_lowcore.trans_exc_code & 3;
113
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);
123 /*
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.
128 */
129 if (tsk->thread.acrs[S390_lowcore.exc_access_id])
130 return tsk->thread.mm_segment.ar4;
131 }
132 }
133#endif
134 /* Primary Segment Table Descriptor */
135 return switch_amode << s390_noexec;
1da177e4
LT
136}
137
138/*
139 * Send SIGSEGV to task. This is an external routine
140 * to keep the stack usage of do_page_fault small.
141 */
142static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
143 int si_code, unsigned long address)
144{
145 struct siginfo si;
146
147#if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
148#if defined(CONFIG_SYSCTL)
149 if (sysctl_userprocess_debug)
150#endif
151 {
152 printk("User process fault: interruption code 0x%lX\n",
153 error_code);
154 printk("failing address: %lX\n", address);
155 show_regs(regs);
156 }
157#endif
158 si.si_signo = SIGSEGV;
159 si.si_code = si_code;
d2c993d8 160 si.si_addr = (void __user *) address;
1da177e4
LT
161 force_sig_info(SIGSEGV, &si, current);
162}
163
10c1031f
MS
164static void do_no_context(struct pt_regs *regs, unsigned long error_code,
165 unsigned long address)
166{
167 const struct exception_table_entry *fixup;
168
169 /* Are we prepared to handle this kernel fault? */
170 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
171 if (fixup) {
172 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
173 return;
174 }
175
176 /*
177 * Oops. The kernel tried to access some bad page. We'll have to
178 * terminate things with extreme prejudice.
179 */
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);
183 else
184 printk(KERN_ALERT "Unable to handle kernel paging request"
185 " at virtual user address %p\n", (void *)address);
186
187 die("Oops", regs, error_code);
188 do_exit(SIGKILL);
189}
190
191static void do_low_address(struct pt_regs *regs, unsigned long error_code)
192{
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);
198 do_exit(SIGKILL);
199 }
200
201 do_no_context(regs, error_code, 0);
202}
203
204/*
205 * We ran out of memory, or some other thing happened to us that made
206 * us unable to handle the page fault gracefully.
207 */
208static int do_out_of_memory(struct pt_regs *regs, unsigned long error_code,
209 unsigned long address)
210{
211 struct task_struct *tsk = current;
212 struct mm_struct *mm = tsk->mm;
213
214 up_read(&mm->mmap_sem);
b460cbc5 215 if (is_global_init(tsk)) {
10c1031f
MS
216 yield();
217 down_read(&mm->mmap_sem);
218 return 1;
219 }
220 printk("VM: killing process %s\n", tsk->comm);
221 if (regs->psw.mask & PSW_MASK_PSTATE)
dcca2bde 222 do_group_exit(SIGKILL);
10c1031f
MS
223 do_no_context(regs, error_code, address);
224 return 0;
225}
226
227static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
228 unsigned long address)
229{
230 struct task_struct *tsk = current;
231 struct mm_struct *mm = tsk->mm;
232
233 up_read(&mm->mmap_sem);
234 /*
235 * Send a sigbus, regardless of whether we were in kernel
236 * or user mode.
237 */
238 tsk->thread.prot_addr = address;
239 tsk->thread.trap_no = error_code;
240 force_sig(SIGBUS, tsk);
241
242 /* Kernel mode? Handle exceptions or die */
243 if (!(regs->psw.mask & PSW_MASK_PSTATE))
244 do_no_context(regs, error_code, address);
245}
246
c1821c2e
GS
247#ifdef CONFIG_S390_EXEC_PROTECT
248extern long sys_sigreturn(struct pt_regs *regs);
249extern long sys_rt_sigreturn(struct pt_regs *regs);
250extern long sys32_sigreturn(struct pt_regs *regs);
251extern long sys32_rt_sigreturn(struct pt_regs *regs);
252
be5ec363
MS
253static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
254 unsigned long address, unsigned long error_code)
c1821c2e 255{
be5ec363 256 u16 instruction;
490f03d6
HC
257 int rc;
258#ifdef CONFIG_COMPAT
259 int compat;
260#endif
be5ec363
MS
261
262 pagefault_disable();
263 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
264 pagefault_enable();
265 if (rc)
266 return -EFAULT;
267
c1821c2e
GS
268 up_read(&mm->mmap_sem);
269 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
270#ifdef CONFIG_COMPAT
be5ec363
MS
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);
c1821c2e 276 else
be5ec363
MS
277#endif
278 if (instruction == 0x0a77)
c1821c2e 279 sys_sigreturn(regs);
be5ec363
MS
280 else if (instruction == 0x0aad)
281 sys_rt_sigreturn(regs);
c1821c2e 282 else {
c1821c2e
GS
283 current->thread.prot_addr = address;
284 current->thread.trap_no = error_code;
285 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
286 }
287 return 0;
c1821c2e
GS
288}
289#endif /* CONFIG_S390_EXEC_PROTECT */
290
1da177e4
LT
291/*
292 * This routine handles page faults. It determines the address,
293 * and the problem, and then passes it off to one of the appropriate
294 * routines.
295 *
296 * error_code:
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)
301 */
482b05dd 302static inline void
10c1031f 303do_exception(struct pt_regs *regs, unsigned long error_code, int write)
1da177e4 304{
10c1031f
MS
305 struct task_struct *tsk;
306 struct mm_struct *mm;
307 struct vm_area_struct *vma;
308 unsigned long address;
482b05dd 309 int space;
10c1031f 310 int si_code;
83c54070 311 int fault;
1da177e4 312
33464e3b 313 if (notify_page_fault(regs, error_code))
4ba069b8
MG
314 return;
315
10c1031f
MS
316 tsk = current;
317 mm = tsk->mm;
1da177e4 318
10c1031f
MS
319 /* get the failing address and the affected space */
320 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
482b05dd 321 space = check_space(tsk);
1da177e4
LT
322
323 /*
324 * Verify that the fault happened in user space, that
325 * we are not in an interrupt and that there is a
326 * user context.
327 */
482b05dd
GS
328 if (unlikely(space == 0 || in_atomic() || !mm))
329 goto no_context;
1da177e4
LT
330
331 /*
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
335 */
336 local_irq_enable();
337
10c1031f 338 down_read(&mm->mmap_sem);
1da177e4 339
482b05dd
GS
340 si_code = SEGV_MAPERR;
341 vma = find_vma(mm, address);
342 if (!vma)
343 goto bad_area;
c1821c2e
GS
344
345#ifdef CONFIG_S390_EXEC_PROTECT
482b05dd 346 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
c1821c2e
GS
347 if (!signal_return(mm, regs, address, error_code))
348 /*
349 * signal_return() has done an up_read(&mm->mmap_sem)
350 * if it returns 0.
351 */
352 return;
353#endif
354
10c1031f
MS
355 if (vma->vm_start <= address)
356 goto good_area;
357 if (!(vma->vm_flags & VM_GROWSDOWN))
358 goto bad_area;
359 if (expand_stack(vma, address))
360 goto bad_area;
1da177e4
LT
361/*
362 * Ok, we have a good vm_area for this memory access, so
363 * we can handle it..
364 */
365good_area:
366 si_code = SEGV_ACCERR;
10c1031f 367 if (!write) {
1da177e4
LT
368 /* page not present, check vm flags */
369 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
370 goto bad_area;
371 } else {
372 if (!(vma->vm_flags & VM_WRITE))
373 goto bad_area;
374 }
375
376survive:
377 /*
378 * If for any reason at all we couldn't handle the fault,
379 * make sure we exit gracefully rather than endlessly redo
380 * the fault.
381 */
83c54070
NP
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))
386 goto survive;
387 return;
388 } else if (fault & VM_FAULT_SIGBUS) {
389 do_sigbus(regs, error_code, address);
390 return;
391 }
1da177e4
LT
392 BUG();
393 }
83c54070
NP
394 if (fault & VM_FAULT_MAJOR)
395 tsk->maj_flt++;
396 else
397 tsk->min_flt++;
1da177e4
LT
398
399 up_read(&mm->mmap_sem);
400 /*
401 * The instruction that caused the program check will
402 * be repeated. Don't signal single step via SIGTRAP.
403 */
482b05dd 404 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
1da177e4
LT
405 return;
406
407/*
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..
410 */
411bad_area:
10c1031f 412 up_read(&mm->mmap_sem);
1da177e4 413
10c1031f
MS
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;
1da177e4 418 do_sigsegv(regs, error_code, si_code, address);
10c1031f 419 return;
1da177e4
LT
420 }
421
422no_context:
10c1031f 423 do_no_context(regs, error_code, address);
1da177e4
LT
424}
425
482b05dd
GS
426void __kprobes do_protection_exception(struct pt_regs *regs,
427 unsigned long error_code)
1da177e4 428{
10c1031f 429 /* Protection exception is supressing, decrement psw address. */
1da177e4 430 regs->psw.addr -= (error_code >> 16);
10c1031f
MS
431 /*
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.
435 */
436 if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
437 do_low_address(regs, error_code);
438 return;
439 }
1da177e4
LT
440 do_exception(regs, 4, 1);
441}
442
482b05dd 443void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
1da177e4
LT
444{
445 do_exception(regs, error_code & 0xff, 0);
446}
447
6252d702
MS
448#ifdef CONFIG_64BIT
449void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
450{
451 struct mm_struct *mm;
452 struct vm_area_struct *vma;
453 unsigned long address;
454 int space;
455
456 mm = current->mm;
457 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
458 space = check_space(current);
459
460 if (unlikely(space == 0 || in_atomic() || !mm))
461 goto no_context;
462
463 local_irq_enable();
464
465 down_read(&mm->mmap_sem);
466 vma = find_vma(mm, address);
467 up_read(&mm->mmap_sem);
468
469 if (vma) {
470 update_mm(mm, current);
471 return;
472 }
473
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);
479 return;
480 }
481
482no_context:
483 do_no_context(regs, error_code, address);
484}
485#endif
486
1da177e4
LT
487#ifdef CONFIG_PFAULT
488/*
489 * 'pfault' pseudo page faults routines.
490 */
29b08d2b 491static ext_int_info_t ext_int_pfault;
1da177e4
LT
492static int pfault_disable = 0;
493
494static int __init nopfault(char *str)
495{
496 pfault_disable = 1;
497 return 1;
498}
499
500__setup("nopfault", nopfault);
501
502typedef struct {
503 __u16 refdiagc;
504 __u16 reffcode;
505 __u16 refdwlen;
506 __u16 refversn;
507 __u64 refgaddr;
508 __u64 refselmk;
509 __u64 refcmpmk;
510 __u64 reserved;
c41fbc69 511} __attribute__ ((packed, aligned(8))) pfault_refbk_t;
1da177e4
LT
512
513int pfault_init(void)
514{
515 pfault_refbk_t refbk =
516 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
517 __PF_RES_FIELD };
518 int rc;
519
29b08d2b 520 if (!MACHINE_IS_VM || pfault_disable)
1da177e4 521 return -1;
94c12cc7
MS
522 asm volatile(
523 " diag %1,%0,0x258\n"
524 "0: j 2f\n"
525 "1: la %0,8\n"
1da177e4 526 "2:\n"
94c12cc7
MS
527 EX_TABLE(0b,1b)
528 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
1da177e4
LT
529 __ctl_set_bit(0, 9);
530 return rc;
531}
532
533void pfault_fini(void)
534{
535 pfault_refbk_t refbk =
536 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
537
29b08d2b 538 if (!MACHINE_IS_VM || pfault_disable)
1da177e4
LT
539 return;
540 __ctl_clear_bit(0,9);
94c12cc7
MS
541 asm volatile(
542 " diag %0,0,0x258\n"
1da177e4 543 "0:\n"
94c12cc7
MS
544 EX_TABLE(0b,0b)
545 : : "a" (&refbk), "m" (refbk) : "cc");
1da177e4
LT
546}
547
2b67fc46 548static void pfault_interrupt(__u16 error_code)
1da177e4
LT
549{
550 struct task_struct *tsk;
551 __u16 subcode;
552
553 /*
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.
558 */
559 subcode = S390_lowcore.cpu_addr;
560 if ((subcode & 0xff00) != __SUBCODE_MASK)
561 return;
562
563 /*
564 * Get the token (= address of the task structure of the affected task).
565 */
566 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
567
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
b6d09449 575 * and can't produce new pfaults. */
1da177e4
LT
576 tsk->thread.pfault_wait = 0;
577 wake_up_process(tsk);
b6d09449 578 put_task_struct(tsk);
1da177e4
LT
579 }
580 } else {
581 /* signal bit not set -> a real page is missing. */
b6d09449 582 get_task_struct(tsk);
1da177e4
LT
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);
b6d09449 592 put_task_struct(tsk);
1da177e4
LT
593 } else
594 set_tsk_need_resched(tsk);
595 }
596}
1da177e4 597
29b08d2b
HC
598void __init pfault_irq_init(void)
599{
600 if (!MACHINE_IS_VM)
601 return;
602
603 /*
604 * Try to get pfault pseudo page faults going.
605 */
606 if (register_early_external_interrupt(0x2603, pfault_interrupt,
607 &ext_int_pfault) != 0)
608 panic("Couldn't request external interrupt 0x2603");
609
610 if (pfault_init() == 0)
611 return;
612
613 /* Tough luck, no pfault. */
614 pfault_disable = 1;
615 unregister_early_external_interrupt(0x2603, pfault_interrupt,
616 &ext_int_pfault);
617}
618#endif