xref: /openbmc/linux/arch/s390/mm/fault.c (revision 73a9260b)
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 
47 /*
48  * Allocate private vm_fault_reason from top.  Please make sure it won't
49  * collide with vm_fault_reason.
50  */
51 #define VM_FAULT_BADCONTEXT	((__force vm_fault_t)0x80000000)
52 #define VM_FAULT_BADMAP		((__force vm_fault_t)0x40000000)
53 #define VM_FAULT_BADACCESS	((__force vm_fault_t)0x20000000)
54 #define VM_FAULT_SIGNAL		((__force vm_fault_t)0x10000000)
55 #define VM_FAULT_PFAULT		((__force vm_fault_t)0x8000000)
56 
57 enum fault_type {
58 	KERNEL_FAULT,
59 	USER_FAULT,
60 	GMAP_FAULT,
61 };
62 
63 static unsigned long store_indication __read_mostly;
64 
fault_init(void)65 static int __init fault_init(void)
66 {
67 	if (test_facility(75))
68 		store_indication = 0xc00;
69 	return 0;
70 }
71 early_initcall(fault_init);
72 
73 /*
74  * Find out which address space caused the exception.
75  */
get_fault_type(struct pt_regs * regs)76 static enum fault_type get_fault_type(struct pt_regs *regs)
77 {
78 	unsigned long trans_exc_code;
79 
80 	trans_exc_code = regs->int_parm_long & 3;
81 	if (likely(trans_exc_code == 0)) {
82 		/* primary space exception */
83 		if (user_mode(regs))
84 			return USER_FAULT;
85 		if (!IS_ENABLED(CONFIG_PGSTE))
86 			return KERNEL_FAULT;
87 		if (test_pt_regs_flag(regs, PIF_GUEST_FAULT))
88 			return GMAP_FAULT;
89 		return KERNEL_FAULT;
90 	}
91 	if (trans_exc_code == 2)
92 		return USER_FAULT;
93 	if (trans_exc_code == 1) {
94 		/* access register mode, not used in the kernel */
95 		return USER_FAULT;
96 	}
97 	/* home space exception -> access via kernel ASCE */
98 	return KERNEL_FAULT;
99 }
100 
get_fault_address(struct pt_regs * regs)101 static unsigned long get_fault_address(struct pt_regs *regs)
102 {
103 	unsigned long trans_exc_code = regs->int_parm_long;
104 
105 	return trans_exc_code & __FAIL_ADDR_MASK;
106 }
107 
fault_is_write(struct pt_regs * regs)108 static bool fault_is_write(struct pt_regs *regs)
109 {
110 	unsigned long trans_exc_code = regs->int_parm_long;
111 
112 	return (trans_exc_code & store_indication) == 0x400;
113 }
114 
bad_address(void * p)115 static int bad_address(void *p)
116 {
117 	unsigned long dummy;
118 
119 	return get_kernel_nofault(dummy, (unsigned long *)p);
120 }
121 
dump_pagetable(unsigned long asce,unsigned long address)122 static void dump_pagetable(unsigned long asce, unsigned long address)
123 {
124 	unsigned long *table = __va(asce & _ASCE_ORIGIN);
125 
126 	pr_alert("AS:%016lx ", asce);
127 	switch (asce & _ASCE_TYPE_MASK) {
128 	case _ASCE_TYPE_REGION1:
129 		table += (address & _REGION1_INDEX) >> _REGION1_SHIFT;
130 		if (bad_address(table))
131 			goto bad;
132 		pr_cont("R1:%016lx ", *table);
133 		if (*table & _REGION_ENTRY_INVALID)
134 			goto out;
135 		table = __va(*table & _REGION_ENTRY_ORIGIN);
136 		fallthrough;
137 	case _ASCE_TYPE_REGION2:
138 		table += (address & _REGION2_INDEX) >> _REGION2_SHIFT;
139 		if (bad_address(table))
140 			goto bad;
141 		pr_cont("R2:%016lx ", *table);
142 		if (*table & _REGION_ENTRY_INVALID)
143 			goto out;
144 		table = __va(*table & _REGION_ENTRY_ORIGIN);
145 		fallthrough;
146 	case _ASCE_TYPE_REGION3:
147 		table += (address & _REGION3_INDEX) >> _REGION3_SHIFT;
148 		if (bad_address(table))
149 			goto bad;
150 		pr_cont("R3:%016lx ", *table);
151 		if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
152 			goto out;
153 		table = __va(*table & _REGION_ENTRY_ORIGIN);
154 		fallthrough;
155 	case _ASCE_TYPE_SEGMENT:
156 		table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
157 		if (bad_address(table))
158 			goto bad;
159 		pr_cont("S:%016lx ", *table);
160 		if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
161 			goto out;
162 		table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
163 	}
164 	table += (address & _PAGE_INDEX) >> _PAGE_SHIFT;
165 	if (bad_address(table))
166 		goto bad;
167 	pr_cont("P:%016lx ", *table);
168 out:
169 	pr_cont("\n");
170 	return;
171 bad:
172 	pr_cont("BAD\n");
173 }
174 
dump_fault_info(struct pt_regs * regs)175 static void dump_fault_info(struct pt_regs *regs)
176 {
177 	unsigned long asce;
178 
179 	pr_alert("Failing address: %016lx TEID: %016lx\n",
180 		 regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
181 	pr_alert("Fault in ");
182 	switch (regs->int_parm_long & 3) {
183 	case 3:
184 		pr_cont("home space ");
185 		break;
186 	case 2:
187 		pr_cont("secondary space ");
188 		break;
189 	case 1:
190 		pr_cont("access register ");
191 		break;
192 	case 0:
193 		pr_cont("primary space ");
194 		break;
195 	}
196 	pr_cont("mode while using ");
197 	switch (get_fault_type(regs)) {
198 	case USER_FAULT:
199 		asce = S390_lowcore.user_asce;
200 		pr_cont("user ");
201 		break;
202 	case GMAP_FAULT:
203 		asce = ((struct gmap *) S390_lowcore.gmap)->asce;
204 		pr_cont("gmap ");
205 		break;
206 	case KERNEL_FAULT:
207 		asce = S390_lowcore.kernel_asce;
208 		pr_cont("kernel ");
209 		break;
210 	default:
211 		unreachable();
212 	}
213 	pr_cont("ASCE.\n");
214 	dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
215 }
216 
217 int show_unhandled_signals = 1;
218 
report_user_fault(struct pt_regs * regs,long signr,int is_mm_fault)219 void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
220 {
221 	if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
222 		return;
223 	if (!unhandled_signal(current, signr))
224 		return;
225 	if (!printk_ratelimit())
226 		return;
227 	printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
228 	       regs->int_code & 0xffff, regs->int_code >> 17);
229 	print_vma_addr(KERN_CONT "in ", regs->psw.addr);
230 	printk(KERN_CONT "\n");
231 	if (is_mm_fault)
232 		dump_fault_info(regs);
233 	show_regs(regs);
234 }
235 
236 /*
237  * Send SIGSEGV to task.  This is an external routine
238  * to keep the stack usage of do_page_fault small.
239  */
do_sigsegv(struct pt_regs * regs,int si_code)240 static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
241 {
242 	report_user_fault(regs, SIGSEGV, 1);
243 	force_sig_fault(SIGSEGV, si_code,
244 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
245 }
246 
do_no_context(struct pt_regs * regs,vm_fault_t fault)247 static noinline void do_no_context(struct pt_regs *regs, vm_fault_t fault)
248 {
249 	enum fault_type fault_type;
250 	unsigned long address;
251 	bool is_write;
252 
253 	if (fixup_exception(regs))
254 		return;
255 	fault_type = get_fault_type(regs);
256 	if ((fault_type == KERNEL_FAULT) && (fault == VM_FAULT_BADCONTEXT)) {
257 		address = get_fault_address(regs);
258 		is_write = fault_is_write(regs);
259 		if (kfence_handle_page_fault(address, is_write, regs))
260 			return;
261 	}
262 	/*
263 	 * Oops. The kernel tried to access some bad page. We'll have to
264 	 * terminate things with extreme prejudice.
265 	 */
266 	if (fault_type == KERNEL_FAULT)
267 		printk(KERN_ALERT "Unable to handle kernel pointer dereference"
268 		       " in virtual kernel address space\n");
269 	else
270 		printk(KERN_ALERT "Unable to handle kernel paging request"
271 		       " in virtual user address space\n");
272 	dump_fault_info(regs);
273 	die(regs, "Oops");
274 }
275 
do_low_address(struct pt_regs * regs)276 static noinline void do_low_address(struct pt_regs *regs)
277 {
278 	/* Low-address protection hit in kernel mode means
279 	   NULL pointer write access in kernel mode.  */
280 	if (regs->psw.mask & PSW_MASK_PSTATE) {
281 		/* Low-address protection hit in user mode 'cannot happen'. */
282 		die (regs, "Low-address protection");
283 	}
284 
285 	do_no_context(regs, VM_FAULT_BADACCESS);
286 }
287 
do_sigbus(struct pt_regs * regs)288 static noinline void do_sigbus(struct pt_regs *regs)
289 {
290 	/*
291 	 * Send a sigbus, regardless of whether we were in kernel
292 	 * or user mode.
293 	 */
294 	force_sig_fault(SIGBUS, BUS_ADRERR,
295 			(void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
296 }
297 
do_fault_error(struct pt_regs * regs,vm_fault_t fault)298 static noinline void do_fault_error(struct pt_regs *regs, vm_fault_t fault)
299 {
300 	int si_code;
301 
302 	switch (fault) {
303 	case VM_FAULT_BADACCESS:
304 	case VM_FAULT_BADMAP:
305 		/* Bad memory access. Check if it is kernel or user space. */
306 		if (user_mode(regs)) {
307 			/* User mode accesses just cause a SIGSEGV */
308 			si_code = (fault == VM_FAULT_BADMAP) ?
309 				SEGV_MAPERR : SEGV_ACCERR;
310 			do_sigsegv(regs, si_code);
311 			break;
312 		}
313 		fallthrough;
314 	case VM_FAULT_BADCONTEXT:
315 	case VM_FAULT_PFAULT:
316 		do_no_context(regs, fault);
317 		break;
318 	case VM_FAULT_SIGNAL:
319 		if (!user_mode(regs))
320 			do_no_context(regs, fault);
321 		break;
322 	default: /* fault & VM_FAULT_ERROR */
323 		if (fault & VM_FAULT_OOM) {
324 			if (!user_mode(regs))
325 				do_no_context(regs, fault);
326 			else
327 				pagefault_out_of_memory();
328 		} else if (fault & VM_FAULT_SIGSEGV) {
329 			/* Kernel mode? Handle exceptions or die */
330 			if (!user_mode(regs))
331 				do_no_context(regs, fault);
332 			else
333 				do_sigsegv(regs, SEGV_MAPERR);
334 		} else if (fault & (VM_FAULT_SIGBUS | VM_FAULT_HWPOISON)) {
335 			/* Kernel mode? Handle exceptions or die */
336 			if (!user_mode(regs))
337 				do_no_context(regs, fault);
338 			else
339 				do_sigbus(regs);
340 		} else {
341 			pr_emerg("Unexpected fault flags: %08x\n", fault);
342 			BUG();
343 		}
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  */
do_exception(struct pt_regs * regs,int access)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 
do_protection_exception(struct pt_regs * regs)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 
do_dat_exception(struct pt_regs * regs)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 #if IS_ENABLED(CONFIG_PGSTE)
586 
do_secure_storage_access(struct pt_regs * regs)587 void do_secure_storage_access(struct pt_regs *regs)
588 {
589 	unsigned long addr = regs->int_parm_long & __FAIL_ADDR_MASK;
590 	struct vm_area_struct *vma;
591 	struct mm_struct *mm;
592 	struct page *page;
593 	struct gmap *gmap;
594 	int rc;
595 
596 	/*
597 	 * bit 61 tells us if the address is valid, if it's not we
598 	 * have a major problem and should stop the kernel or send a
599 	 * SIGSEGV to the process. Unfortunately bit 61 is not
600 	 * reliable without the misc UV feature so we need to check
601 	 * for that as well.
602 	 */
603 	if (uv_has_feature(BIT_UV_FEAT_MISC) &&
604 	    !test_bit_inv(61, &regs->int_parm_long)) {
605 		/*
606 		 * When this happens, userspace did something that it
607 		 * was not supposed to do, e.g. branching into secure
608 		 * memory. Trigger a segmentation fault.
609 		 */
610 		if (user_mode(regs)) {
611 			send_sig(SIGSEGV, current, 0);
612 			return;
613 		}
614 
615 		/*
616 		 * The kernel should never run into this case and we
617 		 * have no way out of this situation.
618 		 */
619 		panic("Unexpected PGM 0x3d with TEID bit 61=0");
620 	}
621 
622 	switch (get_fault_type(regs)) {
623 	case GMAP_FAULT:
624 		mm = current->mm;
625 		gmap = (struct gmap *)S390_lowcore.gmap;
626 		mmap_read_lock(mm);
627 		addr = __gmap_translate(gmap, addr);
628 		mmap_read_unlock(mm);
629 		if (IS_ERR_VALUE(addr)) {
630 			do_fault_error(regs, VM_FAULT_BADMAP);
631 			break;
632 		}
633 		fallthrough;
634 	case USER_FAULT:
635 		mm = current->mm;
636 		mmap_read_lock(mm);
637 		vma = find_vma(mm, addr);
638 		if (!vma) {
639 			mmap_read_unlock(mm);
640 			do_fault_error(regs, VM_FAULT_BADMAP);
641 			break;
642 		}
643 		page = follow_page(vma, addr, FOLL_WRITE | FOLL_GET);
644 		if (IS_ERR_OR_NULL(page)) {
645 			mmap_read_unlock(mm);
646 			break;
647 		}
648 		if (arch_make_page_accessible(page))
649 			send_sig(SIGSEGV, current, 0);
650 		put_page(page);
651 		mmap_read_unlock(mm);
652 		break;
653 	case KERNEL_FAULT:
654 		page = phys_to_page(addr);
655 		if (unlikely(!try_get_page(page)))
656 			break;
657 		rc = arch_make_page_accessible(page);
658 		put_page(page);
659 		if (rc)
660 			BUG();
661 		break;
662 	default:
663 		do_fault_error(regs, VM_FAULT_BADMAP);
664 		WARN_ON_ONCE(1);
665 	}
666 }
667 NOKPROBE_SYMBOL(do_secure_storage_access);
668 
do_non_secure_storage_access(struct pt_regs * regs)669 void do_non_secure_storage_access(struct pt_regs *regs)
670 {
671 	unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
672 	struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
673 
674 	if (get_fault_type(regs) != GMAP_FAULT) {
675 		do_fault_error(regs, VM_FAULT_BADMAP);
676 		WARN_ON_ONCE(1);
677 		return;
678 	}
679 
680 	if (gmap_convert_to_secure(gmap, gaddr) == -EINVAL)
681 		send_sig(SIGSEGV, current, 0);
682 }
683 NOKPROBE_SYMBOL(do_non_secure_storage_access);
684 
do_secure_storage_violation(struct pt_regs * regs)685 void do_secure_storage_violation(struct pt_regs *regs)
686 {
687 	unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
688 	struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
689 
690 	/*
691 	 * If the VM has been rebooted, its address space might still contain
692 	 * secure pages from the previous boot.
693 	 * Clear the page so it can be reused.
694 	 */
695 	if (!gmap_destroy_page(gmap, gaddr))
696 		return;
697 	/*
698 	 * Either KVM messed up the secure guest mapping or the same
699 	 * page is mapped into multiple secure guests.
700 	 *
701 	 * This exception is only triggered when a guest 2 is running
702 	 * and can therefore never occur in kernel context.
703 	 */
704 	printk_ratelimited(KERN_WARNING
705 			   "Secure storage violation in task: %s, pid %d\n",
706 			   current->comm, current->pid);
707 	send_sig(SIGSEGV, current, 0);
708 }
709 
710 #endif /* CONFIG_PGSTE */
711