]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/s390/mm/fault.c
[S390] move crypto options and some cleanup.
[mirror_ubuntu-focal-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>
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>
4ba069b8 28#include <linux/kprobes.h>
1da177e4
LT
29
30#include <asm/system.h>
31#include <asm/uaccess.h>
32#include <asm/pgtable.h>
4ba069b8 33#include <asm/kdebug.h>
29b08d2b 34#include <asm/s390_ext.h>
1da177e4 35
347a8dc3 36#ifndef CONFIG_64BIT
1da177e4
LT
37#define __FAIL_ADDR_MASK 0x7ffff000
38#define __FIXUP_MASK 0x7fffffff
39#define __SUBCODE_MASK 0x0200
40#define __PF_RES_FIELD 0ULL
347a8dc3 41#else /* CONFIG_64BIT */
1da177e4
LT
42#define __FAIL_ADDR_MASK -4096L
43#define __FIXUP_MASK ~0L
44#define __SUBCODE_MASK 0x0600
45#define __PF_RES_FIELD 0x8000000000000000ULL
347a8dc3 46#endif /* CONFIG_64BIT */
1da177e4
LT
47
48#ifdef CONFIG_SYSCTL
49extern int sysctl_userprocess_debug;
50#endif
51
52extern void die(const char *,struct pt_regs *,long);
53
4ba069b8 54#ifdef CONFIG_KPROBES
2b67fc46 55static ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
4ba069b8
MG
56int register_page_fault_notifier(struct notifier_block *nb)
57{
58 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
59}
60
61int unregister_page_fault_notifier(struct notifier_block *nb)
62{
63 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
64}
65
66static inline int notify_page_fault(enum die_val val, const char *str,
67 struct pt_regs *regs, long err, int trap, int sig)
68{
69 struct die_args args = {
70 .regs = regs,
71 .str = str,
72 .err = err,
73 .trapnr = trap,
74 .signr = sig
75 };
76 return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
77}
78#else
79static inline int notify_page_fault(enum die_val val, const char *str,
80 struct pt_regs *regs, long err, int trap, int sig)
81{
82 return NOTIFY_DONE;
83}
84#endif
85
1da177e4
LT
86extern spinlock_t timerlist_lock;
87
88/*
89 * Unlock any spinlocks which will prevent us from getting the
90 * message out (timerlist_lock is acquired through the
91 * console unblank code)
92 */
93void bust_spinlocks(int yes)
94{
95 if (yes) {
96 oops_in_progress = 1;
97 } else {
98 int loglevel_save = console_loglevel;
99 console_unblank();
100 oops_in_progress = 0;
101 /*
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...
105 */
106 console_loglevel = 15;
107 printk(" ");
108 console_loglevel = loglevel_save;
109 }
110}
111
112/*
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.
116 */
117static int __check_access_register(struct pt_regs *regs, int error_code)
118{
119 int areg = S390_lowcore.exc_access_id;
120
121 if (areg == 0)
122 /* Access via access register 0 -> kernel address */
123 return 0;
124 save_access_regs(current->thread.acrs);
125 if (regs && areg < NUM_ACRS && current->thread.acrs[areg] <= 1)
126 /*
127 * access register contains 0 -> kernel address,
128 * access register contains 1 -> user space address
129 */
130 return current->thread.acrs[areg];
131
132 /* Something unhealthy was done with the access registers... */
133 die("page fault via unknown access register", regs, error_code);
134 do_exit(SIGKILL);
135 return 0;
136}
137
138/*
139 * Check which address space the address belongs to.
140 * Returns 1 for user space and 0 for kernel space.
141 */
142static inline int check_user_space(struct pt_regs *regs, int error_code)
143{
144 /*
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
151 */
152 int descriptor = S390_lowcore.trans_exc_code & 3;
153 if (unlikely(descriptor == 1))
154 return __check_access_register(regs, error_code);
155 if (descriptor == 2)
156 return current->thread.mm_segment.ar4;
157 return descriptor != 0;
158}
159
160/*
161 * Send SIGSEGV to task. This is an external routine
162 * to keep the stack usage of do_page_fault small.
163 */
164static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
165 int si_code, unsigned long address)
166{
167 struct siginfo si;
168
169#if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
170#if defined(CONFIG_SYSCTL)
171 if (sysctl_userprocess_debug)
172#endif
173 {
174 printk("User process fault: interruption code 0x%lX\n",
175 error_code);
176 printk("failing address: %lX\n", address);
177 show_regs(regs);
178 }
179#endif
180 si.si_signo = SIGSEGV;
181 si.si_code = si_code;
d2c993d8 182 si.si_addr = (void __user *) address;
1da177e4
LT
183 force_sig_info(SIGSEGV, &si, current);
184}
185
186/*
187 * This routine handles page faults. It determines the address,
188 * and the problem, and then passes it off to one of the appropriate
189 * routines.
190 *
191 * error_code:
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)
196 */
4ba069b8 197static inline void __kprobes
1da177e4
LT
198do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
199{
200 struct task_struct *tsk;
201 struct mm_struct *mm;
202 struct vm_area_struct * vma;
203 unsigned long address;
204 int user_address;
205 const struct exception_table_entry *fixup;
206 int si_code = SEGV_MAPERR;
207
208 tsk = current;
209 mm = tsk->mm;
210
4ba069b8
MG
211 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
212 SIGSEGV) == NOTIFY_STOP)
213 return;
214
1da177e4
LT
215 /*
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.
219 */
220 if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
221
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)) {
225 address = 0;
226 user_address = 0;
227 goto no_context;
228 }
229
230 /* Low-address protection hit in user mode 'cannot happen'. */
231 die ("Low-address protection", regs, error_code);
232 do_exit(SIGKILL);
233 }
234
235 /*
236 * get the failing address
237 * more specific the segment and page table portion of
238 * the address
239 */
240 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
241 user_address = check_user_space(regs, error_code);
242
243 /*
244 * Verify that the fault happened in user space, that
245 * we are not in an interrupt and that there is a
246 * user context.
247 */
595bf2aa 248 if (user_address == 0 || in_atomic() || !mm)
1da177e4
LT
249 goto no_context;
250
251 /*
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
255 */
256 local_irq_enable();
257
258 down_read(&mm->mmap_sem);
259
260 vma = find_vma(mm, address);
261 if (!vma)
262 goto bad_area;
263 if (vma->vm_start <= address)
264 goto good_area;
265 if (!(vma->vm_flags & VM_GROWSDOWN))
266 goto bad_area;
267 if (expand_stack(vma, address))
268 goto bad_area;
269/*
270 * Ok, we have a good vm_area for this memory access, so
271 * we can handle it..
272 */
273good_area:
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)))
278 goto bad_area;
279 } else {
280 if (!(vma->vm_flags & VM_WRITE))
281 goto bad_area;
282 }
283
284survive:
285 /*
286 * If for any reason at all we couldn't handle the fault,
287 * make sure we exit gracefully rather than endlessly redo
288 * the fault.
289 */
290 switch (handle_mm_fault(mm, vma, address, is_protection)) {
291 case VM_FAULT_MINOR:
292 tsk->min_flt++;
293 break;
294 case VM_FAULT_MAJOR:
295 tsk->maj_flt++;
296 break;
297 case VM_FAULT_SIGBUS:
298 goto do_sigbus;
299 case VM_FAULT_OOM:
300 goto out_of_memory;
301 default:
302 BUG();
303 }
304
305 up_read(&mm->mmap_sem);
306 /*
307 * The instruction that caused the program check will
308 * be repeated. Don't signal single step via SIGTRAP.
309 */
310 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
311 return;
312
313/*
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..
316 */
317bad_area:
318 up_read(&mm->mmap_sem);
319
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);
325 return;
326 }
327
328no_context:
329 /* Are we prepared to handle this kernel fault? */
330 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
331 if (fixup) {
332 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
333 return;
334 }
335
336/*
337 * Oops. The kernel tried to access some bad page. We'll have to
338 * terminate things with extreme prejudice.
339 */
340 if (user_address == 0)
341 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
342 " at virtual kernel address %p\n", (void *)address);
343 else
344 printk(KERN_ALERT "Unable to handle kernel paging request"
345 " at virtual user address %p\n", (void *)address);
346
347 die("Oops", regs, error_code);
348 do_exit(SIGKILL);
349
350
351/*
352 * We ran out of memory, or some other thing happened to us that made
353 * us unable to handle the page fault gracefully.
354*/
355out_of_memory:
356 up_read(&mm->mmap_sem);
f400e198 357 if (is_init(tsk)) {
1da177e4 358 yield();
bac9c66c 359 down_read(&mm->mmap_sem);
1da177e4
LT
360 goto survive;
361 }
362 printk("VM: killing process %s\n", tsk->comm);
363 if (regs->psw.mask & PSW_MASK_PSTATE)
364 do_exit(SIGKILL);
365 goto no_context;
366
367do_sigbus:
368 up_read(&mm->mmap_sem);
369
370 /*
371 * Send a sigbus, regardless of whether we were in kernel
372 * or user mode.
373 */
374 tsk->thread.prot_addr = address;
375 tsk->thread.trap_no = error_code;
376 force_sig(SIGBUS, tsk);
377
378 /* Kernel mode? Handle exceptions or die */
379 if (!(regs->psw.mask & PSW_MASK_PSTATE))
380 goto no_context;
381}
382
383void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
384{
385 regs->psw.addr -= (error_code >> 16);
386 do_exception(regs, 4, 1);
387}
388
389void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
390{
391 do_exception(regs, error_code & 0xff, 0);
392}
393
1da177e4
LT
394#ifdef CONFIG_PFAULT
395/*
396 * 'pfault' pseudo page faults routines.
397 */
29b08d2b 398static ext_int_info_t ext_int_pfault;
1da177e4
LT
399static int pfault_disable = 0;
400
401static int __init nopfault(char *str)
402{
403 pfault_disable = 1;
404 return 1;
405}
406
407__setup("nopfault", nopfault);
408
409typedef struct {
410 __u16 refdiagc;
411 __u16 reffcode;
412 __u16 refdwlen;
413 __u16 refversn;
414 __u64 refgaddr;
415 __u64 refselmk;
416 __u64 refcmpmk;
417 __u64 reserved;
418} __attribute__ ((packed)) pfault_refbk_t;
419
420int pfault_init(void)
421{
422 pfault_refbk_t refbk =
423 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
424 __PF_RES_FIELD };
425 int rc;
426
29b08d2b 427 if (!MACHINE_IS_VM || pfault_disable)
1da177e4 428 return -1;
94c12cc7
MS
429 asm volatile(
430 " diag %1,%0,0x258\n"
431 "0: j 2f\n"
432 "1: la %0,8\n"
1da177e4 433 "2:\n"
94c12cc7
MS
434 EX_TABLE(0b,1b)
435 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
1da177e4
LT
436 __ctl_set_bit(0, 9);
437 return rc;
438}
439
440void pfault_fini(void)
441{
442 pfault_refbk_t refbk =
443 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
444
29b08d2b 445 if (!MACHINE_IS_VM || pfault_disable)
1da177e4
LT
446 return;
447 __ctl_clear_bit(0,9);
94c12cc7
MS
448 asm volatile(
449 " diag %0,0,0x258\n"
1da177e4 450 "0:\n"
94c12cc7
MS
451 EX_TABLE(0b,0b)
452 : : "a" (&refbk), "m" (refbk) : "cc");
1da177e4
LT
453}
454
2b67fc46 455static void pfault_interrupt(__u16 error_code)
1da177e4
LT
456{
457 struct task_struct *tsk;
458 __u16 subcode;
459
460 /*
461 * Get the external interruption subcode & pfault
462 * initial/completion signal bit. VM stores this
463 * in the 'cpu address' field associated with the
464 * external interrupt.
465 */
466 subcode = S390_lowcore.cpu_addr;
467 if ((subcode & 0xff00) != __SUBCODE_MASK)
468 return;
469
470 /*
471 * Get the token (= address of the task structure of the affected task).
472 */
473 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
474
475 if (subcode & 0x0080) {
476 /* signal bit is set -> a page has been swapped in by VM */
477 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
478 /* Initial interrupt was faster than the completion
479 * interrupt. pfault_wait is valid. Set pfault_wait
480 * back to zero and wake up the process. This can
481 * safely be done because the task is still sleeping
b6d09449 482 * and can't produce new pfaults. */
1da177e4
LT
483 tsk->thread.pfault_wait = 0;
484 wake_up_process(tsk);
b6d09449 485 put_task_struct(tsk);
1da177e4
LT
486 }
487 } else {
488 /* signal bit not set -> a real page is missing. */
b6d09449 489 get_task_struct(tsk);
1da177e4
LT
490 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
491 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
492 /* Completion interrupt was faster than the initial
493 * interrupt (swapped in a -1 for pfault_wait). Set
494 * pfault_wait back to zero and exit. This can be
495 * done safely because tsk is running in kernel
496 * mode and can't produce new pfaults. */
497 tsk->thread.pfault_wait = 0;
498 set_task_state(tsk, TASK_RUNNING);
b6d09449 499 put_task_struct(tsk);
1da177e4
LT
500 } else
501 set_tsk_need_resched(tsk);
502 }
503}
1da177e4 504
29b08d2b
HC
505void __init pfault_irq_init(void)
506{
507 if (!MACHINE_IS_VM)
508 return;
509
510 /*
511 * Try to get pfault pseudo page faults going.
512 */
513 if (register_early_external_interrupt(0x2603, pfault_interrupt,
514 &ext_int_pfault) != 0)
515 panic("Couldn't request external interrupt 0x2603");
516
517 if (pfault_init() == 0)
518 return;
519
520 /* Tough luck, no pfault. */
521 pfault_disable = 1;
522 unregister_early_external_interrupt(0x2603, pfault_interrupt,
523 &ext_int_pfault);
524}
525#endif