]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/s390/mm/fault.c
s390/zcrypt: Add some debug messages on failure.
[mirror_ubuntu-focal-kernel.git] / arch / s390 / mm / fault.c
CommitLineData
1da177e4 1/*
1da177e4 2 * S390 version
a53c8fab 3 * Copyright IBM Corp. 1999
1da177e4
LT
4 * Author(s): Hartmut Penner (hp@de.ibm.com)
5 * Ulrich Weigand (uweigand@de.ibm.com)
6 *
7 * Derived from "arch/i386/mm/fault.c"
8 * Copyright (C) 1995 Linus Torvalds
9 */
10
052ff461 11#include <linux/kernel_stat.h>
cdd6c482 12#include <linux/perf_event.h>
1da177e4
LT
13#include <linux/signal.h>
14#include <linux/sched.h>
b17b0153 15#include <linux/sched/debug.h>
1da177e4
LT
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>
22#include <linux/mm.h>
7757591a 23#include <linux/compat.h>
1da177e4 24#include <linux/smp.h>
1eeb66a1 25#include <linux/kdebug.h>
1da177e4
LT
26#include <linux/init.h>
27#include <linux/console.h>
dcc096c5 28#include <linux/extable.h>
1da177e4 29#include <linux/hardirq.h>
4ba069b8 30#include <linux/kprobes.h>
be5ec363 31#include <linux/uaccess.h>
53492b1d 32#include <linux/hugetlb.h>
cbb870c8 33#include <asm/asm-offsets.h>
1ec2772e 34#include <asm/diag.h>
1da177e4 35#include <asm/pgtable.h>
1e133ab2 36#include <asm/gmap.h>
d7b250e2 37#include <asm/irq.h>
6252d702 38#include <asm/mmu_context.h>
a0616cde 39#include <asm/facility.h>
a806170e 40#include "../kernel/entry.h"
1da177e4 41
1da177e4 42#define __FAIL_ADDR_MASK -4096L
1da177e4
LT
43#define __SUBCODE_MASK 0x0600
44#define __PF_RES_FIELD 0x8000000000000000ULL
1da177e4 45
50d7280d
MS
46#define VM_FAULT_BADCONTEXT 0x010000
47#define VM_FAULT_BADMAP 0x020000
48#define VM_FAULT_BADACCESS 0x040000
a4f32bdb 49#define VM_FAULT_SIGNAL 0x080000
24eb3a82 50#define VM_FAULT_PFAULT 0x100000
50d7280d 51
a4f32bdb 52static unsigned long store_indication __read_mostly;
92f842ea 53
a4f32bdb 54static int __init fault_init(void)
92f842ea 55{
a4f32bdb 56 if (test_facility(75))
92f842ea 57 store_indication = 0xc00;
a4f32bdb 58 return 0;
92f842ea 59}
a4f32bdb 60early_initcall(fault_init);
92f842ea 61
7ecb344a 62static inline int notify_page_fault(struct pt_regs *regs)
10c1031f 63{
33464e3b
CH
64 int ret = 0;
65
66 /* kprobe_running() needs smp_processor_id() */
22e0a046 67 if (kprobes_built_in() && !user_mode(regs)) {
33464e3b
CH
68 preempt_disable();
69 if (kprobe_running() && kprobe_fault_handler(regs, 14))
70 ret = 1;
71 preempt_enable();
72 }
33464e3b 73 return ret;
4ba069b8 74}
4ba069b8 75
1da177e4
LT
76
77/*
78 * Unlock any spinlocks which will prevent us from getting the
cefc8be8 79 * message out.
1da177e4
LT
80 */
81void bust_spinlocks(int yes)
82{
83 if (yes) {
84 oops_in_progress = 1;
85 } else {
86 int loglevel_save = console_loglevel;
87 console_unblank();
88 oops_in_progress = 0;
89 /*
90 * OK, the message is on the console. Now we call printk()
91 * without oops_in_progress set so that printk will give klogd
92 * a poke. Hold onto your hats...
93 */
94 console_loglevel = 15;
95 printk(" ");
96 console_loglevel = loglevel_save;
97 }
98}
99
100/*
482b05dd 101 * Returns the address space associated with the fault.
61365e13 102 * Returns 0 for kernel space and 1 for user space.
1da177e4 103 */
457f2180 104static inline int user_space_fault(struct pt_regs *regs)
1da177e4 105{
457f2180
HC
106 unsigned long trans_exc_code;
107
1da177e4 108 /*
61365e13
MS
109 * The lowest two bits of the translation exception
110 * identification indicate which paging table was used.
1da177e4 111 */
457f2180
HC
112 trans_exc_code = regs->int_parm_long & 3;
113 if (trans_exc_code == 3) /* home space -> kernel */
114 return 0;
115 if (user_mode(regs))
116 return 1;
117 if (trans_exc_code == 2) /* secondary space -> set_fs */
61365e13 118 return current->thread.mm_segment.ar4;
457f2180
HC
119 if (current->flags & PF_VCPU)
120 return 1;
121 return 0;
1da177e4
LT
122}
123
3b7df342
HC
124static int bad_address(void *p)
125{
126 unsigned long dummy;
127
128 return probe_kernel_address((unsigned long *)p, dummy);
129}
130
3b7df342
HC
131static void dump_pagetable(unsigned long asce, unsigned long address)
132{
133 unsigned long *table = __va(asce & PAGE_MASK);
134
135 pr_alert("AS:%016lx ", asce);
136 switch (asce & _ASCE_TYPE_MASK) {
137 case _ASCE_TYPE_REGION1:
138 table = table + ((address >> 53) & 0x7ff);
139 if (bad_address(table))
140 goto bad;
141 pr_cont("R1:%016lx ", *table);
142 if (*table & _REGION_ENTRY_INVALID)
143 goto out;
144 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
145 /* fallthrough */
146 case _ASCE_TYPE_REGION2:
147 table = table + ((address >> 42) & 0x7ff);
148 if (bad_address(table))
149 goto bad;
150 pr_cont("R2:%016lx ", *table);
151 if (*table & _REGION_ENTRY_INVALID)
152 goto out;
153 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
154 /* fallthrough */
155 case _ASCE_TYPE_REGION3:
156 table = table + ((address >> 31) & 0x7ff);
157 if (bad_address(table))
158 goto bad;
159 pr_cont("R3:%016lx ", *table);
160 if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
161 goto out;
162 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
163 /* fallthrough */
164 case _ASCE_TYPE_SEGMENT:
165 table = table + ((address >> 20) & 0x7ff);
166 if (bad_address(table))
167 goto bad;
91c0837e 168 pr_cont("S:%016lx ", *table);
3b7df342
HC
169 if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
170 goto out;
171 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
172 }
173 table = table + ((address >> 12) & 0xff);
174 if (bad_address(table))
175 goto bad;
176 pr_cont("P:%016lx ", *table);
177out:
178 pr_cont("\n");
179 return;
180bad:
181 pr_cont("BAD\n");
182}
183
3b7df342
HC
184static void dump_fault_info(struct pt_regs *regs)
185{
186 unsigned long asce;
187
5d7eccec
HC
188 pr_alert("Failing address: %016lx TEID: %016lx\n",
189 regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
3b7df342
HC
190 pr_alert("Fault in ");
191 switch (regs->int_parm_long & 3) {
192 case 3:
193 pr_cont("home space ");
194 break;
195 case 2:
196 pr_cont("secondary space ");
197 break;
198 case 1:
199 pr_cont("access register ");
200 break;
201 case 0:
202 pr_cont("primary space ");
203 break;
204 }
205 pr_cont("mode while using ");
206 if (!user_space_fault(regs)) {
207 asce = S390_lowcore.kernel_asce;
208 pr_cont("kernel ");
209 }
210#ifdef CONFIG_PGSTE
211 else if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
212 struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
213 asce = gmap->asce;
214 pr_cont("gmap ");
215 }
216#endif
217 else {
218 asce = S390_lowcore.user_asce;
219 pr_cont("user ");
220 }
221 pr_cont("ASCE.\n");
222 dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
223}
224
5d7eccec
HC
225int show_unhandled_signals = 1;
226
227void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
ab3c68ee
HC
228{
229 if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
230 return;
231 if (!unhandled_signal(current, signr))
232 return;
233 if (!printk_ratelimit())
234 return;
db1177ee 235 printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
413d4047 236 regs->int_code & 0xffff, regs->int_code >> 17);
9cb1ccec 237 print_vma_addr(KERN_CONT "in ", regs->psw.addr);
aa33c8cb 238 printk(KERN_CONT "\n");
5d7eccec
HC
239 if (is_mm_fault)
240 dump_fault_info(regs);
ab3c68ee
HC
241 show_regs(regs);
242}
243
1da177e4
LT
244/*
245 * Send SIGSEGV to task. This is an external routine
246 * to keep the stack usage of do_page_fault small.
247 */
aa33c8cb 248static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
1da177e4
LT
249{
250 struct siginfo si;
251
5d7eccec 252 report_user_fault(regs, SIGSEGV, 1);
1da177e4 253 si.si_signo = SIGSEGV;
cf0d44d5 254 si.si_errno = 0;
1da177e4 255 si.si_code = si_code;
aa33c8cb 256 si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
1da177e4
LT
257 force_sig_info(SIGSEGV, &si, current);
258}
259
aa33c8cb 260static noinline void do_no_context(struct pt_regs *regs)
10c1031f
MS
261{
262 const struct exception_table_entry *fixup;
263
264 /* Are we prepared to handle this kernel fault? */
9cb1ccec 265 fixup = search_exception_tables(regs->psw.addr);
10c1031f 266 if (fixup) {
fecc868a 267 regs->psw.addr = extable_fixup(fixup);
10c1031f
MS
268 return;
269 }
270
271 /*
272 * Oops. The kernel tried to access some bad page. We'll have to
273 * terminate things with extreme prejudice.
274 */
457f2180 275 if (!user_space_fault(regs))
10c1031f 276 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
3b7df342 277 " in virtual kernel address space\n");
10c1031f
MS
278 else
279 printk(KERN_ALERT "Unable to handle kernel paging request"
3b7df342 280 " in virtual user address space\n");
3b7df342 281 dump_fault_info(regs);
aa33c8cb 282 die(regs, "Oops");
10c1031f
MS
283 do_exit(SIGKILL);
284}
285
aa33c8cb 286static noinline void do_low_address(struct pt_regs *regs)
10c1031f
MS
287{
288 /* Low-address protection hit in kernel mode means
289 NULL pointer write access in kernel mode. */
290 if (regs->psw.mask & PSW_MASK_PSTATE) {
291 /* Low-address protection hit in user mode 'cannot happen'. */
aa33c8cb 292 die (regs, "Low-address protection");
10c1031f
MS
293 do_exit(SIGKILL);
294 }
295
aa33c8cb 296 do_no_context(regs);
10c1031f
MS
297}
298
aa33c8cb 299static noinline void do_sigbus(struct pt_regs *regs)
10c1031f
MS
300{
301 struct task_struct *tsk = current;
36bf9680 302 struct siginfo si;
10c1031f 303
10c1031f
MS
304 /*
305 * Send a sigbus, regardless of whether we were in kernel
306 * or user mode.
307 */
36bf9680
MS
308 si.si_signo = SIGBUS;
309 si.si_errno = 0;
310 si.si_code = BUS_ADRERR;
aa33c8cb 311 si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
36bf9680 312 force_sig_info(SIGBUS, &si, tsk);
10c1031f
MS
313}
314
57d7f939
MS
315static noinline int signal_return(struct pt_regs *regs)
316{
317 u16 instruction;
318 int rc;
319
320 rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
321 if (rc)
322 return rc;
323 if (instruction == 0x0a77) {
324 set_pt_regs_flag(regs, PIF_SYSCALL);
325 regs->int_code = 0x00040077;
326 return 0;
327 } else if (instruction == 0x0aad) {
328 set_pt_regs_flag(regs, PIF_SYSCALL);
329 regs->int_code = 0x000400ad;
330 return 0;
331 }
332 return -EACCES;
333}
334
335static noinline void do_fault_error(struct pt_regs *regs, int access, int fault)
50d7280d
MS
336{
337 int si_code;
338
339 switch (fault) {
340 case VM_FAULT_BADACCESS:
57d7f939
MS
341 if (access == VM_EXEC && signal_return(regs) == 0)
342 break;
50d7280d
MS
343 case VM_FAULT_BADMAP:
344 /* Bad memory access. Check if it is kernel or user space. */
7d256175 345 if (user_mode(regs)) {
50d7280d
MS
346 /* User mode accesses just cause a SIGSEGV */
347 si_code = (fault == VM_FAULT_BADMAP) ?
348 SEGV_MAPERR : SEGV_ACCERR;
aa33c8cb 349 do_sigsegv(regs, si_code);
57d7f939 350 break;
50d7280d
MS
351 }
352 case VM_FAULT_BADCONTEXT:
24eb3a82 353 case VM_FAULT_PFAULT:
aa33c8cb 354 do_no_context(regs);
50d7280d 355 break;
f2c76e3b
HC
356 case VM_FAULT_SIGNAL:
357 if (!user_mode(regs))
358 do_no_context(regs);
359 break;
50d7280d 360 default: /* fault & VM_FAULT_ERROR */
99583181 361 if (fault & VM_FAULT_OOM) {
7d256175 362 if (!user_mode(regs))
aa33c8cb 363 do_no_context(regs);
99583181
HC
364 else
365 pagefault_out_of_memory();
33692f27
LT
366 } else if (fault & VM_FAULT_SIGSEGV) {
367 /* Kernel mode? Handle exceptions or die */
368 if (!user_mode(regs))
369 do_no_context(regs);
370 else
371 do_sigsegv(regs, SEGV_MAPERR);
99583181 372 } else if (fault & VM_FAULT_SIGBUS) {
50d7280d 373 /* Kernel mode? Handle exceptions or die */
7d256175 374 if (!user_mode(regs))
aa33c8cb 375 do_no_context(regs);
36bf9680 376 else
aa33c8cb 377 do_sigbus(regs);
50d7280d
MS
378 } else
379 BUG();
380 break;
381 }
382}
383
1da177e4
LT
384/*
385 * This routine handles page faults. It determines the address,
386 * and the problem, and then passes it off to one of the appropriate
387 * routines.
388 *
50d7280d 389 * interruption code (int_code):
1da177e4
LT
390 * 04 Protection -> Write-Protection (suprression)
391 * 10 Segment translation -> Not present (nullification)
392 * 11 Page translation -> Not present (nullification)
393 * 3b Region third trans. -> Not present (nullification)
394 */
aa33c8cb 395static inline int do_exception(struct pt_regs *regs, int access)
1da177e4 396{
24eb3a82
DD
397#ifdef CONFIG_PGSTE
398 struct gmap *gmap;
399#endif
10c1031f
MS
400 struct task_struct *tsk;
401 struct mm_struct *mm;
402 struct vm_area_struct *vma;
aa33c8cb 403 unsigned long trans_exc_code;
10c1031f 404 unsigned long address;
33ce6140
HC
405 unsigned int flags;
406 int fault;
1da177e4 407
39efd4ec
MS
408 tsk = current;
409 /*
410 * The instruction that caused the program check has
411 * been nullified. Don't signal single step via SIGTRAP.
412 */
d3a73acb 413 clear_pt_regs_flag(regs, PIF_PER_TRAP);
39efd4ec 414
7ecb344a 415 if (notify_page_fault(regs))
50d7280d 416 return 0;
4ba069b8 417
10c1031f 418 mm = tsk->mm;
aa33c8cb 419 trans_exc_code = regs->int_parm_long;
1da177e4 420
1da177e4
LT
421 /*
422 * Verify that the fault happened in user space, that
423 * we are not in an interrupt and that there is a
424 * user context.
425 */
50d7280d 426 fault = VM_FAULT_BADCONTEXT;
70ffdb93 427 if (unlikely(!user_space_fault(regs) || faulthandler_disabled() || !mm))
50d7280d 428 goto out;
1da177e4 429
61365e13 430 address = trans_exc_code & __FAIL_ADDR_MASK;
a8b0ca17 431 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
f2c76e3b 432 flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
759496ba
JW
433 if (user_mode(regs))
434 flags |= FAULT_FLAG_USER;
33ce6140
HC
435 if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
436 flags |= FAULT_FLAG_WRITE;
10c1031f 437 down_read(&mm->mmap_sem);
1da177e4 438
e5992f2e 439#ifdef CONFIG_PGSTE
527e30b4
MS
440 gmap = (current->flags & PF_VCPU) ?
441 (struct gmap *) S390_lowcore.gmap : NULL;
24eb3a82 442 if (gmap) {
527e30b4 443 current->thread.gmap_addr = address;
4be130a0 444 current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
4a494439 445 current->thread.gmap_int_code = regs->int_code & 0xffff;
527e30b4 446 address = __gmap_translate(gmap, address);
e5992f2e
MS
447 if (address == -EFAULT) {
448 fault = VM_FAULT_BADMAP;
449 goto out_up;
450 }
24eb3a82
DD
451 if (gmap->pfault_enabled)
452 flags |= FAULT_FLAG_RETRY_NOWAIT;
e5992f2e
MS
453 }
454#endif
455
456retry:
50d7280d 457 fault = VM_FAULT_BADMAP;
482b05dd
GS
458 vma = find_vma(mm, address);
459 if (!vma)
50d7280d 460 goto out_up;
c1821c2e 461
50d7280d
MS
462 if (unlikely(vma->vm_start > address)) {
463 if (!(vma->vm_flags & VM_GROWSDOWN))
464 goto out_up;
465 if (expand_stack(vma, address))
466 goto out_up;
467 }
468
469 /*
470 * Ok, we have a good vm_area for this memory access, so
471 * we can handle it..
472 */
473 fault = VM_FAULT_BADACCESS;
1ab947de 474 if (unlikely(!(vma->vm_flags & access)))
50d7280d 475 goto out_up;
1da177e4 476
53492b1d
GS
477 if (is_vm_hugetlb_page(vma))
478 address &= HPAGE_MASK;
1da177e4
LT
479 /*
480 * If for any reason at all we couldn't handle the fault,
481 * make sure we exit gracefully rather than endlessly redo
482 * the fault.
483 */
dcddffd4 484 fault = handle_mm_fault(vma, address, flags);
f2c76e3b
HC
485 /* No reason to continue if interrupted by SIGKILL. */
486 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
487 fault = VM_FAULT_SIGNAL;
488 goto out;
489 }
50d7280d
MS
490 if (unlikely(fault & VM_FAULT_ERROR))
491 goto out_up;
492
33ce6140
HC
493 /*
494 * Major/minor page fault accounting is only done on the
495 * initial attempt. If we go through a retry, it is extremely
496 * likely that the page will be found in page cache at that point.
497 */
498 if (flags & FAULT_FLAG_ALLOW_RETRY) {
499 if (fault & VM_FAULT_MAJOR) {
500 tsk->maj_flt++;
a8b0ca17 501 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
33ce6140
HC
502 regs, address);
503 } else {
504 tsk->min_flt++;
a8b0ca17 505 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
33ce6140
HC
506 regs, address);
507 }
508 if (fault & VM_FAULT_RETRY) {
24eb3a82
DD
509#ifdef CONFIG_PGSTE
510 if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
511 /* FAULT_FLAG_RETRY_NOWAIT has been set,
512 * mmap_sem has not been released */
513 current->thread.gmap_pfault = 1;
514 fault = VM_FAULT_PFAULT;
515 goto out_up;
516 }
517#endif
33ce6140
HC
518 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
519 * of starvation. */
24eb3a82
DD
520 flags &= ~(FAULT_FLAG_ALLOW_RETRY |
521 FAULT_FLAG_RETRY_NOWAIT);
45cac65b 522 flags |= FAULT_FLAG_TRIED;
e5992f2e 523 down_read(&mm->mmap_sem);
33ce6140
HC
524 goto retry;
525 }
bde69af2 526 }
527e30b4
MS
527#ifdef CONFIG_PGSTE
528 if (gmap) {
529 address = __gmap_link(gmap, current->thread.gmap_addr,
530 address);
531 if (address == -EFAULT) {
532 fault = VM_FAULT_BADMAP;
533 goto out_up;
534 }
535 if (address == -ENOMEM) {
536 fault = VM_FAULT_OOM;
537 goto out_up;
538 }
539 }
540#endif
50d7280d
MS
541 fault = 0;
542out_up:
10c1031f 543 up_read(&mm->mmap_sem);
50d7280d
MS
544out:
545 return fault;
1da177e4
LT
546}
547
7a5388de 548void do_protection_exception(struct pt_regs *regs)
1da177e4 549{
aa33c8cb 550 unsigned long trans_exc_code;
57d7f939 551 int access, fault;
61365e13 552
aa33c8cb 553 trans_exc_code = regs->int_parm_long;
f752ac4d
MS
554 /*
555 * Protection exceptions are suppressing, decrement psw address.
556 * The exception to this rule are aborted transactions, for these
557 * the PSW already points to the correct location.
558 */
559 if (!(regs->int_code & 0x200))
560 regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
10c1031f
MS
561 /*
562 * Check for low-address protection. This needs to be treated
563 * as a special case because the translation exception code
564 * field is not guaranteed to contain valid data in this case.
565 */
61365e13 566 if (unlikely(!(trans_exc_code & 4))) {
aa33c8cb 567 do_low_address(regs);
10c1031f
MS
568 return;
569 }
57d7f939
MS
570 if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) {
571 regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) |
572 (regs->psw.addr & PAGE_MASK);
573 access = VM_EXEC;
574 fault = VM_FAULT_BADACCESS;
575 } else {
576 access = VM_WRITE;
577 fault = do_exception(regs, access);
578 }
50d7280d 579 if (unlikely(fault))
57d7f939 580 do_fault_error(regs, access, fault);
1da177e4 581}
7a5388de 582NOKPROBE_SYMBOL(do_protection_exception);
1da177e4 583
7a5388de 584void do_dat_exception(struct pt_regs *regs)
1da177e4 585{
1ab947de 586 int access, fault;
50d7280d 587
1ab947de 588 access = VM_READ | VM_EXEC | VM_WRITE;
aa33c8cb 589 fault = do_exception(regs, access);
50d7280d 590 if (unlikely(fault))
57d7f939 591 do_fault_error(regs, access, fault);
1da177e4 592}
7a5388de 593NOKPROBE_SYMBOL(do_dat_exception);
1da177e4 594
1da177e4
LT
595#ifdef CONFIG_PFAULT
596/*
597 * 'pfault' pseudo page faults routines.
598 */
fb0a9d7e 599static int pfault_disable;
1da177e4
LT
600
601static int __init nopfault(char *str)
602{
603 pfault_disable = 1;
604 return 1;
605}
606
607__setup("nopfault", nopfault);
608
7dd8fe1f
HC
609struct pfault_refbk {
610 u16 refdiagc;
611 u16 reffcode;
612 u16 refdwlen;
613 u16 refversn;
614 u64 refgaddr;
615 u64 refselmk;
616 u64 refcmpmk;
617 u64 reserved;
618} __attribute__ ((packed, aligned(8)));
1da177e4
LT
619
620int pfault_init(void)
621{
7dd8fe1f
HC
622 struct pfault_refbk refbk = {
623 .refdiagc = 0x258,
624 .reffcode = 0,
625 .refdwlen = 5,
626 .refversn = 2,
e22cf8ca 627 .refgaddr = __LC_LPP,
7dd8fe1f
HC
628 .refselmk = 1ULL << 48,
629 .refcmpmk = 1ULL << 48,
630 .reserved = __PF_RES_FIELD };
1da177e4
LT
631 int rc;
632
f32269a0 633 if (pfault_disable)
1da177e4 634 return -1;
1ec2772e 635 diag_stat_inc(DIAG_STAT_X258);
94c12cc7
MS
636 asm volatile(
637 " diag %1,%0,0x258\n"
638 "0: j 2f\n"
639 "1: la %0,8\n"
1da177e4 640 "2:\n"
94c12cc7
MS
641 EX_TABLE(0b,1b)
642 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
1da177e4
LT
643 return rc;
644}
645
646void pfault_fini(void)
647{
7dd8fe1f
HC
648 struct pfault_refbk refbk = {
649 .refdiagc = 0x258,
650 .reffcode = 1,
651 .refdwlen = 5,
652 .refversn = 2,
653 };
1da177e4 654
f32269a0 655 if (pfault_disable)
1da177e4 656 return;
1ec2772e 657 diag_stat_inc(DIAG_STAT_X258);
94c12cc7
MS
658 asm volatile(
659 " diag %0,0,0x258\n"
6c22c986 660 "0: nopr %%r7\n"
94c12cc7
MS
661 EX_TABLE(0b,0b)
662 : : "a" (&refbk), "m" (refbk) : "cc");
1da177e4
LT
663}
664
f2db2e6c
HC
665static DEFINE_SPINLOCK(pfault_lock);
666static LIST_HEAD(pfault_list);
667
0227f7c4
PZ
668#define PF_COMPLETE 0x0080
669
670/*
671 * The mechanism of our pfault code: if Linux is running as guest, runs a user
672 * space process and the user space process accesses a page that the host has
673 * paged out we get a pfault interrupt.
674 *
675 * This allows us, within the guest, to schedule a different process. Without
676 * this mechanism the host would have to suspend the whole virtual cpu until
677 * the page has been paged in.
678 *
679 * So when we get such an interrupt then we set the state of the current task
680 * to uninterruptible and also set the need_resched flag. Both happens within
681 * interrupt context(!). If we later on want to return to user space we
682 * recognize the need_resched flag and then call schedule(). It's not very
683 * obvious how this works...
684 *
685 * Of course we have a lot of additional fun with the completion interrupt (->
686 * host signals that a page of a process has been paged in and the process can
687 * continue to run). This interrupt can arrive on any cpu and, since we have
688 * virtual cpus, actually appear before the interrupt that signals that a page
689 * is missing.
690 */
fde15c3a 691static void pfault_interrupt(struct ext_code ext_code,
f6649a7e 692 unsigned int param32, unsigned long param64)
1da177e4
LT
693{
694 struct task_struct *tsk;
695 __u16 subcode;
f2db2e6c 696 pid_t pid;
1da177e4
LT
697
698 /*
0227f7c4
PZ
699 * Get the external interruption subcode & pfault initial/completion
700 * signal bit. VM stores this in the 'cpu address' field associated
701 * with the external interrupt.
1da177e4 702 */
fde15c3a 703 subcode = ext_code.subcode;
1da177e4
LT
704 if ((subcode & 0xff00) != __SUBCODE_MASK)
705 return;
420f42ec 706 inc_irq_stat(IRQEXT_PFL);
54c27791 707 /* Get the token (= pid of the affected task). */
e22cf8ca 708 pid = param64 & LPP_PFAULT_PID_MASK;
54c27791
HC
709 rcu_read_lock();
710 tsk = find_task_by_pid_ns(pid, &init_pid_ns);
711 if (tsk)
712 get_task_struct(tsk);
713 rcu_read_unlock();
714 if (!tsk)
715 return;
f2db2e6c 716 spin_lock(&pfault_lock);
0227f7c4 717 if (subcode & PF_COMPLETE) {
1da177e4 718 /* signal bit is set -> a page has been swapped in by VM */
f2db2e6c 719 if (tsk->thread.pfault_wait == 1) {
1da177e4
LT
720 /* Initial interrupt was faster than the completion
721 * interrupt. pfault_wait is valid. Set pfault_wait
722 * back to zero and wake up the process. This can
723 * safely be done because the task is still sleeping
b6d09449 724 * and can't produce new pfaults. */
1da177e4 725 tsk->thread.pfault_wait = 0;
f2db2e6c 726 list_del(&tsk->thread.list);
1da177e4 727 wake_up_process(tsk);
d5e50a51 728 put_task_struct(tsk);
f2db2e6c
HC
729 } else {
730 /* Completion interrupt was faster than initial
731 * interrupt. Set pfault_wait to -1 so the initial
fa2fb2f4
HC
732 * interrupt doesn't put the task to sleep.
733 * If the task is not running, ignore the completion
734 * interrupt since it must be a leftover of a PFAULT
735 * CANCEL operation which didn't remove all pending
736 * completion interrupts. */
737 if (tsk->state == TASK_RUNNING)
738 tsk->thread.pfault_wait = -1;
1da177e4
LT
739 }
740 } else {
741 /* signal bit not set -> a real page is missing. */
d49f47f8
HC
742 if (WARN_ON_ONCE(tsk != current))
743 goto out;
d5e50a51
HC
744 if (tsk->thread.pfault_wait == 1) {
745 /* Already on the list with a reference: put to sleep */
0227f7c4 746 goto block;
d5e50a51 747 } else if (tsk->thread.pfault_wait == -1) {
1da177e4 748 /* Completion interrupt was faster than the initial
f2db2e6c
HC
749 * interrupt (pfault_wait == -1). Set pfault_wait
750 * back to zero and exit. */
1da177e4 751 tsk->thread.pfault_wait = 0;
f2db2e6c
HC
752 } else {
753 /* Initial interrupt arrived before completion
d5e50a51
HC
754 * interrupt. Let the task sleep.
755 * An extra task reference is needed since a different
756 * cpu may set the task state to TASK_RUNNING again
757 * before the scheduler is reached. */
758 get_task_struct(tsk);
f2db2e6c
HC
759 tsk->thread.pfault_wait = 1;
760 list_add(&tsk->thread.list, &pfault_list);
0227f7c4
PZ
761block:
762 /* Since this must be a userspace fault, there
763 * is no kernel task state to trample. Rely on the
764 * return to userspace schedule() to block. */
765 __set_current_state(TASK_UNINTERRUPTIBLE);
1da177e4 766 set_tsk_need_resched(tsk);
c360192b 767 set_preempt_need_resched();
f2db2e6c
HC
768 }
769 }
d49f47f8 770out:
f2db2e6c 771 spin_unlock(&pfault_lock);
54c27791 772 put_task_struct(tsk);
f2db2e6c
HC
773}
774
84c9ceef 775static int pfault_cpu_dead(unsigned int cpu)
f2db2e6c
HC
776{
777 struct thread_struct *thread, *next;
778 struct task_struct *tsk;
779
84c9ceef
SAS
780 spin_lock_irq(&pfault_lock);
781 list_for_each_entry_safe(thread, next, &pfault_list, list) {
782 thread->pfault_wait = 0;
783 list_del(&thread->list);
784 tsk = container_of(thread, struct task_struct, thread);
785 wake_up_process(tsk);
786 put_task_struct(tsk);
1da177e4 787 }
84c9ceef
SAS
788 spin_unlock_irq(&pfault_lock);
789 return 0;
1da177e4 790}
1da177e4 791
fb0a9d7e 792static int __init pfault_irq_init(void)
29b08d2b 793{
fb0a9d7e 794 int rc;
29b08d2b 795
1dad093b 796 rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
7dd8fe1f
HC
797 if (rc)
798 goto out_extint;
799 rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
800 if (rc)
801 goto out_pfault;
82003c3e 802 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
84c9ceef
SAS
803 cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
804 NULL, pfault_cpu_dead);
7dd8fe1f 805 return 0;
29b08d2b 806
7dd8fe1f 807out_pfault:
1dad093b 808 unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
7dd8fe1f
HC
809out_extint:
810 pfault_disable = 1;
811 return rc;
29b08d2b 812}
fb0a9d7e
HC
813early_initcall(pfault_irq_init);
814
7dd8fe1f 815#endif /* CONFIG_PFAULT */