xref: /linux/arch/s390/mm/fault.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  S390 version
4  *    Copyright IBM Corp. 1999
5  *    Author(s): Hartmut Penner (hp@de.ibm.com)
6  *               Ulrich Weigand (uweigand@de.ibm.com)
7  *
8  *  Derived from "arch/i386/mm/fault.c"
9  *    Copyright (C) 1995  Linus Torvalds
10  */
11 
12 #include <linux/kernel_stat.h>
13 #include <linux/perf_event.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/sched/debug.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
23 #include <linux/mm.h>
24 #include <linux/compat.h>
25 #include <linux/smp.h>
26 #include <linux/kdebug.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
29 #include <linux/extable.h>
30 #include <linux/hardirq.h>
31 #include <linux/kprobes.h>
32 #include <linux/uaccess.h>
33 #include <linux/hugetlb.h>
34 #include <linux/kfence.h>
35 #include <asm/asm-extable.h>
36 #include <asm/asm-offsets.h>
37 #include <asm/diag.h>
38 #include <asm/gmap.h>
39 #include <asm/irq.h>
40 #include <asm/mmu_context.h>
41 #include <asm/facility.h>
42 #include <asm/uv.h>
43 #include "../kernel/entry.h"
44 
45 #define __FAIL_ADDR_MASK -4096L
46 #define __SUBCODE_MASK 0x0600
47 #define __PF_RES_FIELD 0x8000000000000000ULL
48 
49 /*
50  * Allocate private vm_fault_reason from top.  Please make sure it won't
51  * collide with vm_fault_reason.
52  */
53 #define VM_FAULT_BADCONTEXT	((__force vm_fault_t)0x80000000)
54 #define VM_FAULT_BADMAP		((__force vm_fault_t)0x40000000)
55 #define VM_FAULT_BADACCESS	((__force vm_fault_t)0x20000000)
56 #define VM_FAULT_SIGNAL		((__force vm_fault_t)0x10000000)
57 #define VM_FAULT_PFAULT		((__force vm_fault_t)0x8000000)
58 
59 enum fault_type {
60 	KERNEL_FAULT,
61 	USER_FAULT,
62 	GMAP_FAULT,
63 };
64 
65 static unsigned long store_indication __read_mostly;
66 
67 static int __init fault_init(void)
68 {
69 	if (test_facility(75))
70 		store_indication = 0xc00;
71 	return 0;
72 }
73 early_initcall(fault_init);
74 
75 /*
76  * Find out which address space caused the exception.
77  */
78 static enum fault_type get_fault_type(struct pt_regs *regs)
79 {
80 	unsigned long trans_exc_code;
81 
82 	trans_exc_code = regs->int_parm_long & 3;
83 	if (likely(trans_exc_code == 0)) {
84 		/* primary space exception */
85 		if (user_mode(regs))
86 			return USER_FAULT;
87 		if (!IS_ENABLED(CONFIG_PGSTE))
88 			return KERNEL_FAULT;
89 		if (test_pt_regs_flag(regs, PIF_GUEST_FAULT))
90 			return GMAP_FAULT;
91 		return KERNEL_FAULT;
92 	}
93 	if (trans_exc_code == 2)
94 		return USER_FAULT;
95 	if (trans_exc_code == 1) {
96 		/* access register mode, not used in the kernel */
97 		return USER_FAULT;
98 	}
99 	/* home space exception -> access via kernel ASCE */
100 	return KERNEL_FAULT;
101 }
102 
103 static unsigned long get_fault_address(struct pt_regs *regs)
104 {
105 	unsigned long trans_exc_code = regs->int_parm_long;
106 
107 	return trans_exc_code & __FAIL_ADDR_MASK;
108 }
109 
110 static bool fault_is_write(struct pt_regs *regs)
111 {
112 	unsigned long trans_exc_code = regs->int_parm_long;
113 
114 	return (trans_exc_code & store_indication) == 0x400;
115 }
116 
117 static int bad_address(void *p)
118 {
119 	unsigned long dummy;
120 
121 	return get_kernel_nofault(dummy, (unsigned long *)p);
122 }
123 
124 static void dump_pagetable(unsigned long asce, unsigned long address)
125 {
126 	unsigned long *table = __va(asce & _ASCE_ORIGIN);
127 
128 	pr_alert("AS:%016lx ", asce);
129 	switch (asce & _ASCE_TYPE_MASK) {
130 	case _ASCE_TYPE_REGION1:
131 		table += (address & _REGION1_INDEX) >> _REGION1_SHIFT;
132 		if (bad_address(table))
133 			goto bad;
134 		pr_cont("R1:%016lx ", *table);
135 		if (*table & _REGION_ENTRY_INVALID)
136 			goto out;
137 		table = __va(*table & _REGION_ENTRY_ORIGIN);
138 		fallthrough;
139 	case _ASCE_TYPE_REGION2:
140 		table += (address & _REGION2_INDEX) >> _REGION2_SHIFT;
141 		if (bad_address(table))
142 			goto bad;
143 		pr_cont("R2:%016lx ", *table);
144 		if (*table & _REGION_ENTRY_INVALID)
145 			goto out;
146 		table = __va(*table & _REGION_ENTRY_ORIGIN);
147 		fallthrough;
148 	case _ASCE_TYPE_REGION3:
149 		table += (address & _REGION3_INDEX) >> _REGION3_SHIFT;
150 		if (bad_address(table))
151 			goto bad;
152 		pr_cont("R3:%016lx ", *table);
153 		if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
154 			goto out;
155 		table = __va(*table & _REGION_ENTRY_ORIGIN);
156 		fallthrough;
157 	case _ASCE_TYPE_SEGMENT:
158 		table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
159 		if (bad_address(table))
160 			goto bad;
161 		pr_cont("S:%016lx ", *table);
162 		if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
163 			goto out;
164 		table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
165 	}
166 	table += (address & _PAGE_INDEX) >> _PAGE_SHIFT;
167 	if (bad_address(table))
168 		goto bad;
169 	pr_cont("P:%016lx ", *table);
170 out:
171 	pr_cont("\n");
172 	return;
173 bad:
174 	pr_cont("BAD\n");
175 }
176 
177 static void dump_fault_info(struct pt_regs *regs)
178 {
179 	unsigned long asce;
180 
181 	pr_alert("Failing address: %016lx TEID: %016lx\n",
182 		 regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
183 	pr_alert("Fault in ");
184 	switch (regs->int_parm_long & 3) {
185 	case 3:
186 		pr_cont("home space ");
187 		break;
188 	case 2:
189 		pr_cont("secondary space ");
190 		break;
191 	case 1:
192 		pr_cont("access register ");
193 		break;
194 	case 0:
195 		pr_cont("primary space ");
196 		break;
197 	}
198 	pr_cont("mode while using ");
199 	switch (get_fault_type(regs)) {
200 	case USER_FAULT:
201 		asce = S390_lowcore.user_asce;
202 		pr_cont("user ");
203 		break;
204 	case GMAP_FAULT:
205 		asce = ((struct gmap *) S390_lowcore.gmap)->asce;
206 		pr_cont("gmap ");
207 		break;
208 	case KERNEL_FAULT:
209 		asce = S390_lowcore.kernel_asce;
210 		pr_cont("kernel ");
211 		break;
212 	default:
213 		unreachable();
214 	}
215 	pr_cont("ASCE.\n");
216 	dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
217 }
218 
219 int show_unhandled_signals = 1;
220 
221 void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
222 {
223 	if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
224 		return;
225 	if (!unhandled_signal(current, signr))
226 		return;
227 	if (!printk_ratelimit())
228 		return;
229 	printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
230 	       regs->int_code & 0xffff, regs->int_code >> 17);
231 	print_vma_addr(KERN_CONT "in ", regs->psw.addr);
232 	printk(KERN_CONT "\n");
233 	if (is_mm_fault)
234 		dump_fault_info(regs);
235 	show_regs(regs);
236 }
237 
238 /*
239  * Send SIGSEGV to task.  This is an external routine
240  * to keep the stack usage of do_page_fault small.
241  */
242 static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
243 {
244 	report_user_fault(regs, SIGSEGV, 1);
245 	force_sig_fault(SIGSEGV, si_code,
246 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
247 }
248 
249 static noinline void do_no_context(struct pt_regs *regs, vm_fault_t fault)
250 {
251 	enum fault_type fault_type;
252 	unsigned long address;
253 	bool is_write;
254 
255 	if (fixup_exception(regs))
256 		return;
257 	fault_type = get_fault_type(regs);
258 	if ((fault_type == KERNEL_FAULT) && (fault == VM_FAULT_BADCONTEXT)) {
259 		address = get_fault_address(regs);
260 		is_write = fault_is_write(regs);
261 		if (kfence_handle_page_fault(address, is_write, regs))
262 			return;
263 	}
264 	/*
265 	 * Oops. The kernel tried to access some bad page. We'll have to
266 	 * terminate things with extreme prejudice.
267 	 */
268 	if (fault_type == KERNEL_FAULT)
269 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
270 		       " in virtual kernel address space\n");
271 	else
272 		printk(KERN_ALERT "Unable to handle kernel paging request"
273 		       " in virtual user address space\n");
274 	dump_fault_info(regs);
275 	die(regs, "Oops");
276 }
277 
278 static noinline void do_low_address(struct pt_regs *regs)
279 {
280 	/* Low-address protection hit in kernel mode means
281 	   NULL pointer write access in kernel mode.  */
282 	if (regs->psw.mask & PSW_MASK_PSTATE) {
283 		/* Low-address protection hit in user mode 'cannot happen'. */
284 		die (regs, "Low-address protection");
285 	}
286 
287 	do_no_context(regs, VM_FAULT_BADACCESS);
288 }
289 
290 static noinline void do_sigbus(struct pt_regs *regs)
291 {
292 	/*
293 	 * Send a sigbus, regardless of whether we were in kernel
294 	 * or user mode.
295 	 */
296 	force_sig_fault(SIGBUS, BUS_ADRERR,
297 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
298 }
299 
300 static noinline void do_fault_error(struct pt_regs *regs, vm_fault_t fault)
301 {
302 	int si_code;
303 
304 	switch (fault) {
305 	case VM_FAULT_BADACCESS:
306 	case VM_FAULT_BADMAP:
307 		/* Bad memory access. Check if it is kernel or user space. */
308 		if (user_mode(regs)) {
309 			/* User mode accesses just cause a SIGSEGV */
310 			si_code = (fault == VM_FAULT_BADMAP) ?
311 				SEGV_MAPERR : SEGV_ACCERR;
312 			do_sigsegv(regs, si_code);
313 			break;
314 		}
315 		fallthrough;
316 	case VM_FAULT_BADCONTEXT:
317 	case VM_FAULT_PFAULT:
318 		do_no_context(regs, fault);
319 		break;
320 	case VM_FAULT_SIGNAL:
321 		if (!user_mode(regs))
322 			do_no_context(regs, fault);
323 		break;
324 	default: /* fault & VM_FAULT_ERROR */
325 		if (fault & VM_FAULT_OOM) {
326 			if (!user_mode(regs))
327 				do_no_context(regs, fault);
328 			else
329 				pagefault_out_of_memory();
330 		} else if (fault & VM_FAULT_SIGSEGV) {
331 			/* Kernel mode? Handle exceptions or die */
332 			if (!user_mode(regs))
333 				do_no_context(regs, fault);
334 			else
335 				do_sigsegv(regs, SEGV_MAPERR);
336 		} else if (fault & VM_FAULT_SIGBUS) {
337 			/* Kernel mode? Handle exceptions or die */
338 			if (!user_mode(regs))
339 				do_no_context(regs, fault);
340 			else
341 				do_sigbus(regs);
342 		} else
343 			BUG();
344 		break;
345 	}
346 }
347 
348 /*
349  * This routine handles page faults.  It determines the address,
350  * and the problem, and then passes it off to one of the appropriate
351  * routines.
352  *
353  * interruption code (int_code):
354  *   04       Protection           ->  Write-Protection  (suppression)
355  *   10       Segment translation  ->  Not present       (nullification)
356  *   11       Page translation     ->  Not present       (nullification)
357  *   3b       Region third trans.  ->  Not present       (nullification)
358  */
359 static inline vm_fault_t do_exception(struct pt_regs *regs, int access)
360 {
361 	struct gmap *gmap;
362 	struct task_struct *tsk;
363 	struct mm_struct *mm;
364 	struct vm_area_struct *vma;
365 	enum fault_type type;
366 	unsigned long address;
367 	unsigned int flags;
368 	vm_fault_t fault;
369 	bool is_write;
370 
371 	tsk = current;
372 	/*
373 	 * The instruction that caused the program check has
374 	 * been nullified. Don't signal single step via SIGTRAP.
375 	 */
376 	clear_thread_flag(TIF_PER_TRAP);
377 
378 	if (kprobe_page_fault(regs, 14))
379 		return 0;
380 
381 	mm = tsk->mm;
382 	address = get_fault_address(regs);
383 	is_write = fault_is_write(regs);
384 
385 	/*
386 	 * Verify that the fault happened in user space, that
387 	 * we are not in an interrupt and that there is a
388 	 * user context.
389 	 */
390 	fault = VM_FAULT_BADCONTEXT;
391 	type = get_fault_type(regs);
392 	switch (type) {
393 	case KERNEL_FAULT:
394 		goto out;
395 	case USER_FAULT:
396 	case GMAP_FAULT:
397 		if (faulthandler_disabled() || !mm)
398 			goto out;
399 		break;
400 	}
401 
402 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
403 	flags = FAULT_FLAG_DEFAULT;
404 	if (user_mode(regs))
405 		flags |= FAULT_FLAG_USER;
406 	if (is_write)
407 		access = VM_WRITE;
408 	if (access == VM_WRITE)
409 		flags |= FAULT_FLAG_WRITE;
410 	if (!(flags & FAULT_FLAG_USER))
411 		goto lock_mmap;
412 	vma = lock_vma_under_rcu(mm, address);
413 	if (!vma)
414 		goto lock_mmap;
415 	if (!(vma->vm_flags & access)) {
416 		vma_end_read(vma);
417 		goto lock_mmap;
418 	}
419 	fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs);
420 	if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
421 		vma_end_read(vma);
422 	if (!(fault & VM_FAULT_RETRY)) {
423 		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
424 		if (likely(!(fault & VM_FAULT_ERROR)))
425 			fault = 0;
426 		goto out;
427 	}
428 	count_vm_vma_lock_event(VMA_LOCK_RETRY);
429 	/* Quick path to respond to signals */
430 	if (fault_signal_pending(fault, regs)) {
431 		fault = VM_FAULT_SIGNAL;
432 		goto out;
433 	}
434 lock_mmap:
435 	mmap_read_lock(mm);
436 
437 	gmap = NULL;
438 	if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) {
439 		gmap = (struct gmap *) S390_lowcore.gmap;
440 		current->thread.gmap_addr = address;
441 		current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
442 		current->thread.gmap_int_code = regs->int_code & 0xffff;
443 		address = __gmap_translate(gmap, address);
444 		if (address == -EFAULT) {
445 			fault = VM_FAULT_BADMAP;
446 			goto out_up;
447 		}
448 		if (gmap->pfault_enabled)
449 			flags |= FAULT_FLAG_RETRY_NOWAIT;
450 	}
451 
452 retry:
453 	fault = VM_FAULT_BADMAP;
454 	vma = find_vma(mm, address);
455 	if (!vma)
456 		goto out_up;
457 
458 	if (unlikely(vma->vm_start > address)) {
459 		if (!(vma->vm_flags & VM_GROWSDOWN))
460 			goto out_up;
461 		vma = expand_stack(mm, address);
462 		if (!vma)
463 			goto out;
464 	}
465 
466 	/*
467 	 * Ok, we have a good vm_area for this memory access, so
468 	 * we can handle it..
469 	 */
470 	fault = VM_FAULT_BADACCESS;
471 	if (unlikely(!(vma->vm_flags & access)))
472 		goto out_up;
473 
474 	/*
475 	 * If for any reason at all we couldn't handle the fault,
476 	 * make sure we exit gracefully rather than endlessly redo
477 	 * the fault.
478 	 */
479 	fault = handle_mm_fault(vma, address, flags, regs);
480 	if (fault_signal_pending(fault, regs)) {
481 		fault = VM_FAULT_SIGNAL;
482 		if (flags & FAULT_FLAG_RETRY_NOWAIT)
483 			goto out_up;
484 		goto out;
485 	}
486 
487 	/* The fault is fully completed (including releasing mmap lock) */
488 	if (fault & VM_FAULT_COMPLETED) {
489 		if (gmap) {
490 			mmap_read_lock(mm);
491 			goto out_gmap;
492 		}
493 		fault = 0;
494 		goto out;
495 	}
496 
497 	if (unlikely(fault & VM_FAULT_ERROR))
498 		goto out_up;
499 
500 	if (fault & VM_FAULT_RETRY) {
501 		if (IS_ENABLED(CONFIG_PGSTE) && gmap &&
502 			(flags & FAULT_FLAG_RETRY_NOWAIT)) {
503 			/*
504 			 * FAULT_FLAG_RETRY_NOWAIT has been set, mmap_lock has
505 			 * not been released
506 			 */
507 			current->thread.gmap_pfault = 1;
508 			fault = VM_FAULT_PFAULT;
509 			goto out_up;
510 		}
511 		flags &= ~FAULT_FLAG_RETRY_NOWAIT;
512 		flags |= FAULT_FLAG_TRIED;
513 		mmap_read_lock(mm);
514 		goto retry;
515 	}
516 out_gmap:
517 	if (IS_ENABLED(CONFIG_PGSTE) && gmap) {
518 		address =  __gmap_link(gmap, current->thread.gmap_addr,
519 				       address);
520 		if (address == -EFAULT) {
521 			fault = VM_FAULT_BADMAP;
522 			goto out_up;
523 		}
524 		if (address == -ENOMEM) {
525 			fault = VM_FAULT_OOM;
526 			goto out_up;
527 		}
528 	}
529 	fault = 0;
530 out_up:
531 	mmap_read_unlock(mm);
532 out:
533 	return fault;
534 }
535 
536 void do_protection_exception(struct pt_regs *regs)
537 {
538 	unsigned long trans_exc_code;
539 	int access;
540 	vm_fault_t fault;
541 
542 	trans_exc_code = regs->int_parm_long;
543 	/*
544 	 * Protection exceptions are suppressing, decrement psw address.
545 	 * The exception to this rule are aborted transactions, for these
546 	 * the PSW already points to the correct location.
547 	 */
548 	if (!(regs->int_code & 0x200))
549 		regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
550 	/*
551 	 * Check for low-address protection.  This needs to be treated
552 	 * as a special case because the translation exception code
553 	 * field is not guaranteed to contain valid data in this case.
554 	 */
555 	if (unlikely(!(trans_exc_code & 4))) {
556 		do_low_address(regs);
557 		return;
558 	}
559 	if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) {
560 		regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) |
561 					(regs->psw.addr & PAGE_MASK);
562 		access = VM_EXEC;
563 		fault = VM_FAULT_BADACCESS;
564 	} else {
565 		access = VM_WRITE;
566 		fault = do_exception(regs, access);
567 	}
568 	if (unlikely(fault))
569 		do_fault_error(regs, fault);
570 }
571 NOKPROBE_SYMBOL(do_protection_exception);
572 
573 void do_dat_exception(struct pt_regs *regs)
574 {
575 	int access;
576 	vm_fault_t fault;
577 
578 	access = VM_ACCESS_FLAGS;
579 	fault = do_exception(regs, access);
580 	if (unlikely(fault))
581 		do_fault_error(regs, fault);
582 }
583 NOKPROBE_SYMBOL(do_dat_exception);
584 
585 #ifdef CONFIG_PFAULT
586 /*
587  * 'pfault' pseudo page faults routines.
588  */
589 static int pfault_disable;
590 
591 static int __init nopfault(char *str)
592 {
593 	pfault_disable = 1;
594 	return 1;
595 }
596 
597 __setup("nopfault", nopfault);
598 
599 struct pfault_refbk {
600 	u16 refdiagc;
601 	u16 reffcode;
602 	u16 refdwlen;
603 	u16 refversn;
604 	u64 refgaddr;
605 	u64 refselmk;
606 	u64 refcmpmk;
607 	u64 reserved;
608 } __attribute__ ((packed, aligned(8)));
609 
610 static struct pfault_refbk pfault_init_refbk = {
611 	.refdiagc = 0x258,
612 	.reffcode = 0,
613 	.refdwlen = 5,
614 	.refversn = 2,
615 	.refgaddr = __LC_LPP,
616 	.refselmk = 1ULL << 48,
617 	.refcmpmk = 1ULL << 48,
618 	.reserved = __PF_RES_FIELD
619 };
620 
621 int pfault_init(void)
622 {
623         int rc;
624 
625 	if (pfault_disable)
626 		return -1;
627 	diag_stat_inc(DIAG_STAT_X258);
628 	asm volatile(
629 		"	diag	%1,%0,0x258\n"
630 		"0:	j	2f\n"
631 		"1:	la	%0,8\n"
632 		"2:\n"
633 		EX_TABLE(0b,1b)
634 		: "=d" (rc)
635 		: "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc");
636         return rc;
637 }
638 
639 static struct pfault_refbk pfault_fini_refbk = {
640 	.refdiagc = 0x258,
641 	.reffcode = 1,
642 	.refdwlen = 5,
643 	.refversn = 2,
644 };
645 
646 void pfault_fini(void)
647 {
648 
649 	if (pfault_disable)
650 		return;
651 	diag_stat_inc(DIAG_STAT_X258);
652 	asm volatile(
653 		"	diag	%0,0,0x258\n"
654 		"0:	nopr	%%r7\n"
655 		EX_TABLE(0b,0b)
656 		: : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc");
657 }
658 
659 static DEFINE_SPINLOCK(pfault_lock);
660 static LIST_HEAD(pfault_list);
661 
662 #define PF_COMPLETE	0x0080
663 
664 /*
665  * The mechanism of our pfault code: if Linux is running as guest, runs a user
666  * space process and the user space process accesses a page that the host has
667  * paged out we get a pfault interrupt.
668  *
669  * This allows us, within the guest, to schedule a different process. Without
670  * this mechanism the host would have to suspend the whole virtual cpu until
671  * the page has been paged in.
672  *
673  * So when we get such an interrupt then we set the state of the current task
674  * to uninterruptible and also set the need_resched flag. Both happens within
675  * interrupt context(!). If we later on want to return to user space we
676  * recognize the need_resched flag and then call schedule().  It's not very
677  * obvious how this works...
678  *
679  * Of course we have a lot of additional fun with the completion interrupt (->
680  * host signals that a page of a process has been paged in and the process can
681  * continue to run). This interrupt can arrive on any cpu and, since we have
682  * virtual cpus, actually appear before the interrupt that signals that a page
683  * is missing.
684  */
685 static void pfault_interrupt(struct ext_code ext_code,
686 			     unsigned int param32, unsigned long param64)
687 {
688 	struct task_struct *tsk;
689 	__u16 subcode;
690 	pid_t pid;
691 
692 	/*
693 	 * Get the external interruption subcode & pfault initial/completion
694 	 * signal bit. VM stores this in the 'cpu address' field associated
695 	 * with the external interrupt.
696 	 */
697 	subcode = ext_code.subcode;
698 	if ((subcode & 0xff00) != __SUBCODE_MASK)
699 		return;
700 	inc_irq_stat(IRQEXT_PFL);
701 	/* Get the token (= pid of the affected task). */
702 	pid = param64 & LPP_PID_MASK;
703 	rcu_read_lock();
704 	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
705 	if (tsk)
706 		get_task_struct(tsk);
707 	rcu_read_unlock();
708 	if (!tsk)
709 		return;
710 	spin_lock(&pfault_lock);
711 	if (subcode & PF_COMPLETE) {
712 		/* signal bit is set -> a page has been swapped in by VM */
713 		if (tsk->thread.pfault_wait == 1) {
714 			/* Initial interrupt was faster than the completion
715 			 * interrupt. pfault_wait is valid. Set pfault_wait
716 			 * back to zero and wake up the process. This can
717 			 * safely be done because the task is still sleeping
718 			 * and can't produce new pfaults. */
719 			tsk->thread.pfault_wait = 0;
720 			list_del(&tsk->thread.list);
721 			wake_up_process(tsk);
722 			put_task_struct(tsk);
723 		} else {
724 			/* Completion interrupt was faster than initial
725 			 * interrupt. Set pfault_wait to -1 so the initial
726 			 * interrupt doesn't put the task to sleep.
727 			 * If the task is not running, ignore the completion
728 			 * interrupt since it must be a leftover of a PFAULT
729 			 * CANCEL operation which didn't remove all pending
730 			 * completion interrupts. */
731 			if (task_is_running(tsk))
732 				tsk->thread.pfault_wait = -1;
733 		}
734 	} else {
735 		/* signal bit not set -> a real page is missing. */
736 		if (WARN_ON_ONCE(tsk != current))
737 			goto out;
738 		if (tsk->thread.pfault_wait == 1) {
739 			/* Already on the list with a reference: put to sleep */
740 			goto block;
741 		} else if (tsk->thread.pfault_wait == -1) {
742 			/* Completion interrupt was faster than the initial
743 			 * interrupt (pfault_wait == -1). Set pfault_wait
744 			 * back to zero and exit. */
745 			tsk->thread.pfault_wait = 0;
746 		} else {
747 			/* Initial interrupt arrived before completion
748 			 * interrupt. Let the task sleep.
749 			 * An extra task reference is needed since a different
750 			 * cpu may set the task state to TASK_RUNNING again
751 			 * before the scheduler is reached. */
752 			get_task_struct(tsk);
753 			tsk->thread.pfault_wait = 1;
754 			list_add(&tsk->thread.list, &pfault_list);
755 block:
756 			/* Since this must be a userspace fault, there
757 			 * is no kernel task state to trample. Rely on the
758 			 * return to userspace schedule() to block. */
759 			__set_current_state(TASK_UNINTERRUPTIBLE);
760 			set_tsk_need_resched(tsk);
761 			set_preempt_need_resched();
762 		}
763 	}
764 out:
765 	spin_unlock(&pfault_lock);
766 	put_task_struct(tsk);
767 }
768 
769 static int pfault_cpu_dead(unsigned int cpu)
770 {
771 	struct thread_struct *thread, *next;
772 	struct task_struct *tsk;
773 
774 	spin_lock_irq(&pfault_lock);
775 	list_for_each_entry_safe(thread, next, &pfault_list, list) {
776 		thread->pfault_wait = 0;
777 		list_del(&thread->list);
778 		tsk = container_of(thread, struct task_struct, thread);
779 		wake_up_process(tsk);
780 		put_task_struct(tsk);
781 	}
782 	spin_unlock_irq(&pfault_lock);
783 	return 0;
784 }
785 
786 static int __init pfault_irq_init(void)
787 {
788 	int rc;
789 
790 	rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
791 	if (rc)
792 		goto out_extint;
793 	rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
794 	if (rc)
795 		goto out_pfault;
796 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
797 	cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
798 				  NULL, pfault_cpu_dead);
799 	return 0;
800 
801 out_pfault:
802 	unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
803 out_extint:
804 	pfault_disable = 1;
805 	return rc;
806 }
807 early_initcall(pfault_irq_init);
808 
809 #endif /* CONFIG_PFAULT */
810 
811 #if IS_ENABLED(CONFIG_PGSTE)
812 
813 void do_secure_storage_access(struct pt_regs *regs)
814 {
815 	unsigned long addr = regs->int_parm_long & __FAIL_ADDR_MASK;
816 	struct vm_area_struct *vma;
817 	struct mm_struct *mm;
818 	struct page *page;
819 	struct gmap *gmap;
820 	int rc;
821 
822 	/*
823 	 * bit 61 tells us if the address is valid, if it's not we
824 	 * have a major problem and should stop the kernel or send a
825 	 * SIGSEGV to the process. Unfortunately bit 61 is not
826 	 * reliable without the misc UV feature so we need to check
827 	 * for that as well.
828 	 */
829 	if (test_bit_inv(BIT_UV_FEAT_MISC, &uv_info.uv_feature_indications) &&
830 	    !test_bit_inv(61, &regs->int_parm_long)) {
831 		/*
832 		 * When this happens, userspace did something that it
833 		 * was not supposed to do, e.g. branching into secure
834 		 * memory. Trigger a segmentation fault.
835 		 */
836 		if (user_mode(regs)) {
837 			send_sig(SIGSEGV, current, 0);
838 			return;
839 		}
840 
841 		/*
842 		 * The kernel should never run into this case and we
843 		 * have no way out of this situation.
844 		 */
845 		panic("Unexpected PGM 0x3d with TEID bit 61=0");
846 	}
847 
848 	switch (get_fault_type(regs)) {
849 	case GMAP_FAULT:
850 		mm = current->mm;
851 		gmap = (struct gmap *)S390_lowcore.gmap;
852 		mmap_read_lock(mm);
853 		addr = __gmap_translate(gmap, addr);
854 		mmap_read_unlock(mm);
855 		if (IS_ERR_VALUE(addr)) {
856 			do_fault_error(regs, VM_FAULT_BADMAP);
857 			break;
858 		}
859 		fallthrough;
860 	case USER_FAULT:
861 		mm = current->mm;
862 		mmap_read_lock(mm);
863 		vma = find_vma(mm, addr);
864 		if (!vma) {
865 			mmap_read_unlock(mm);
866 			do_fault_error(regs, VM_FAULT_BADMAP);
867 			break;
868 		}
869 		page = follow_page(vma, addr, FOLL_WRITE | FOLL_GET);
870 		if (IS_ERR_OR_NULL(page)) {
871 			mmap_read_unlock(mm);
872 			break;
873 		}
874 		if (arch_make_page_accessible(page))
875 			send_sig(SIGSEGV, current, 0);
876 		put_page(page);
877 		mmap_read_unlock(mm);
878 		break;
879 	case KERNEL_FAULT:
880 		page = phys_to_page(addr);
881 		if (unlikely(!try_get_page(page)))
882 			break;
883 		rc = arch_make_page_accessible(page);
884 		put_page(page);
885 		if (rc)
886 			BUG();
887 		break;
888 	default:
889 		do_fault_error(regs, VM_FAULT_BADMAP);
890 		WARN_ON_ONCE(1);
891 	}
892 }
893 NOKPROBE_SYMBOL(do_secure_storage_access);
894 
895 void do_non_secure_storage_access(struct pt_regs *regs)
896 {
897 	unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
898 	struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
899 
900 	if (get_fault_type(regs) != GMAP_FAULT) {
901 		do_fault_error(regs, VM_FAULT_BADMAP);
902 		WARN_ON_ONCE(1);
903 		return;
904 	}
905 
906 	if (gmap_convert_to_secure(gmap, gaddr) == -EINVAL)
907 		send_sig(SIGSEGV, current, 0);
908 }
909 NOKPROBE_SYMBOL(do_non_secure_storage_access);
910 
911 void do_secure_storage_violation(struct pt_regs *regs)
912 {
913 	unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
914 	struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
915 
916 	/*
917 	 * If the VM has been rebooted, its address space might still contain
918 	 * secure pages from the previous boot.
919 	 * Clear the page so it can be reused.
920 	 */
921 	if (!gmap_destroy_page(gmap, gaddr))
922 		return;
923 	/*
924 	 * Either KVM messed up the secure guest mapping or the same
925 	 * page is mapped into multiple secure guests.
926 	 *
927 	 * This exception is only triggered when a guest 2 is running
928 	 * and can therefore never occur in kernel context.
929 	 */
930 	printk_ratelimited(KERN_WARNING
931 			   "Secure storage violation in task: %s, pid %d\n",
932 			   current->comm, current->pid);
933 	send_sig(SIGSEGV, current, 0);
934 }
935 
936 #endif /* CONFIG_PGSTE */
937