xref: /linux/arch/arm64/mm/fault.c (revision 3ae8cef210dd52ae95fd5a87f9bea0932bd4e470)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/mm/fault.c
4  *
5  * Copyright (C) 1995  Linus Torvalds
6  * Copyright (C) 1995-2004 Russell King
7  * Copyright (C) 2012 ARM Ltd.
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/bitfield.h>
12 #include <linux/extable.h>
13 #include <linux/kfence.h>
14 #include <linux/signal.h>
15 #include <linux/mm.h>
16 #include <linux/hardirq.h>
17 #include <linux/init.h>
18 #include <linux/kasan.h>
19 #include <linux/kprobes.h>
20 #include <linux/uaccess.h>
21 #include <linux/page-flags.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/debug.h>
24 #include <linux/highmem.h>
25 #include <linux/perf_event.h>
26 #include <linux/pkeys.h>
27 #include <linux/preempt.h>
28 #include <linux/hugetlb.h>
29 
30 #include <asm/acpi.h>
31 #include <asm/bug.h>
32 #include <asm/cmpxchg.h>
33 #include <asm/cpufeature.h>
34 #include <asm/efi.h>
35 #include <asm/exception.h>
36 #include <asm/daifflags.h>
37 #include <asm/debug-monitors.h>
38 #include <asm/esr.h>
39 #include <asm/kprobes.h>
40 #include <asm/mte.h>
41 #include <asm/processor.h>
42 #include <asm/sysreg.h>
43 #include <asm/system_misc.h>
44 #include <asm/tlbflush.h>
45 #include <asm/traps.h>
46 
47 struct fault_info {
48 	int	(*fn)(unsigned long far, unsigned long esr,
49 		      struct pt_regs *regs);
50 	int	sig;
51 	int	code;
52 	const char *name;
53 };
54 
55 static const struct fault_info fault_info[];
56 
57 static inline const struct fault_info *esr_to_fault_info(unsigned long esr)
58 {
59 	return fault_info + (esr & ESR_ELx_FSC);
60 }
61 
62 static void data_abort_decode(unsigned long esr)
63 {
64 	unsigned long iss2 = ESR_ELx_ISS2(esr);
65 
66 	pr_alert("Data abort info:\n");
67 
68 	if (esr & ESR_ELx_ISV) {
69 		pr_alert("  Access size = %u byte(s)\n",
70 			 1U << ((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT));
71 		pr_alert("  SSE = %lu, SRT = %lu\n",
72 			 (esr & ESR_ELx_SSE) >> ESR_ELx_SSE_SHIFT,
73 			 (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT);
74 		pr_alert("  SF = %lu, AR = %lu\n",
75 			 (esr & ESR_ELx_SF) >> ESR_ELx_SF_SHIFT,
76 			 (esr & ESR_ELx_AR) >> ESR_ELx_AR_SHIFT);
77 	} else {
78 		pr_alert("  ISV = 0, ISS = 0x%08lx, ISS2 = 0x%08lx\n",
79 			 esr & ESR_ELx_ISS_MASK, iss2);
80 	}
81 
82 	pr_alert("  CM = %lu, WnR = %lu, TnD = %lu, TagAccess = %lu\n",
83 		 (esr & ESR_ELx_CM) >> ESR_ELx_CM_SHIFT,
84 		 (esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT,
85 		 (iss2 & ESR_ELx_TnD) >> ESR_ELx_TnD_SHIFT,
86 		 (iss2 & ESR_ELx_TagAccess) >> ESR_ELx_TagAccess_SHIFT);
87 
88 	pr_alert("  GCS = %ld, Overlay = %lu, DirtyBit = %lu, Xs = %llu\n",
89 		 (iss2 & ESR_ELx_GCS) >> ESR_ELx_GCS_SHIFT,
90 		 (iss2 & ESR_ELx_Overlay) >> ESR_ELx_Overlay_SHIFT,
91 		 (iss2 & ESR_ELx_DirtyBit) >> ESR_ELx_DirtyBit_SHIFT,
92 		 (iss2 & ESR_ELx_Xs_MASK) >> ESR_ELx_Xs_SHIFT);
93 }
94 
95 static void mem_abort_decode(unsigned long esr)
96 {
97 	pr_alert("Mem abort info:\n");
98 
99 	pr_alert("  ESR = 0x%016lx\n", esr);
100 	pr_alert("  EC = 0x%02lx: %s, IL = %u bits\n",
101 		 ESR_ELx_EC(esr), esr_get_class_string(esr),
102 		 (esr & ESR_ELx_IL) ? 32 : 16);
103 	pr_alert("  SET = %lu, FnV = %lu\n",
104 		 (esr & ESR_ELx_SET_MASK) >> ESR_ELx_SET_SHIFT,
105 		 (esr & ESR_ELx_FnV) >> ESR_ELx_FnV_SHIFT);
106 	pr_alert("  EA = %lu, S1PTW = %lu\n",
107 		 (esr & ESR_ELx_EA) >> ESR_ELx_EA_SHIFT,
108 		 (esr & ESR_ELx_S1PTW) >> ESR_ELx_S1PTW_SHIFT);
109 	pr_alert("  FSC = 0x%02lx: %s\n", (esr & ESR_ELx_FSC),
110 		 esr_to_fault_info(esr)->name);
111 
112 	if (esr_is_data_abort(esr))
113 		data_abort_decode(esr);
114 }
115 
116 static inline unsigned long mm_to_pgd_phys(struct mm_struct *mm)
117 {
118 	/* Either init_pg_dir or swapper_pg_dir */
119 	if (mm == &init_mm)
120 		return __pa_symbol(mm->pgd);
121 
122 	return (unsigned long)virt_to_phys(mm->pgd);
123 }
124 
125 /*
126  * Dump out the page tables associated with 'addr' in the currently active mm.
127  */
128 static void show_pte(unsigned long addr)
129 {
130 	struct mm_struct *mm;
131 	pgd_t *pgdp;
132 	pgd_t pgd;
133 
134 	if (is_ttbr0_addr(addr)) {
135 		/* TTBR0 */
136 		mm = current->active_mm;
137 		if (mm == &init_mm) {
138 			pr_alert("[%016lx] user address but active_mm is swapper\n",
139 				 addr);
140 			return;
141 		}
142 	} else if (is_ttbr1_addr(addr)) {
143 		/* TTBR1 */
144 		mm = &init_mm;
145 	} else {
146 		pr_alert("[%016lx] address between user and kernel address ranges\n",
147 			 addr);
148 		return;
149 	}
150 
151 	pr_alert("%s pgtable: %luk pages, %llu-bit VAs, pgdp=%016lx\n",
152 		 mm == &init_mm ? "swapper" : "user", PAGE_SIZE / SZ_1K,
153 		 vabits_actual, mm_to_pgd_phys(mm));
154 	pgdp = pgd_offset(mm, addr);
155 	pgd = READ_ONCE(*pgdp);
156 	pr_alert("[%016lx] pgd=%016llx", addr, pgd_val(pgd));
157 
158 	do {
159 		p4d_t *p4dp, p4d;
160 		pud_t *pudp, pud;
161 		pmd_t *pmdp, pmd;
162 		pte_t *ptep, pte;
163 
164 		if (pgd_none(pgd) || pgd_bad(pgd))
165 			break;
166 
167 		p4dp = p4d_offset(pgdp, addr);
168 		p4d = READ_ONCE(*p4dp);
169 		pr_cont(", p4d=%016llx", p4d_val(p4d));
170 		if (p4d_none(p4d) || p4d_bad(p4d))
171 			break;
172 
173 		pudp = pud_offset(p4dp, addr);
174 		pud = READ_ONCE(*pudp);
175 		pr_cont(", pud=%016llx", pud_val(pud));
176 		if (pud_none(pud) || pud_bad(pud))
177 			break;
178 
179 		pmdp = pmd_offset(pudp, addr);
180 		pmd = READ_ONCE(*pmdp);
181 		pr_cont(", pmd=%016llx", pmd_val(pmd));
182 		if (pmd_none(pmd) || pmd_bad(pmd))
183 			break;
184 
185 		ptep = pte_offset_map(pmdp, addr);
186 		if (!ptep)
187 			break;
188 
189 		pte = __ptep_get(ptep);
190 		pr_cont(", pte=%016llx", pte_val(pte));
191 		pte_unmap(ptep);
192 	} while(0);
193 
194 	pr_cont("\n");
195 }
196 
197 /*
198  * This function sets the access flags (dirty, accessed), as well as write
199  * permission, and only to a more permissive setting.
200  *
201  * It needs to cope with hardware update of the accessed/dirty state by other
202  * agents in the system and can safely skip the __sync_icache_dcache() call as,
203  * like __set_ptes(), the PTE is never changed from no-exec to exec here.
204  *
205  * Returns whether or not the PTE actually changed.
206  */
207 int __ptep_set_access_flags(struct vm_area_struct *vma,
208 			    unsigned long address, pte_t *ptep,
209 			    pte_t entry, int dirty)
210 {
211 	pteval_t old_pteval, pteval;
212 	pte_t pte = __ptep_get(ptep);
213 
214 	if (pte_same(pte, entry))
215 		return 0;
216 
217 	/* only preserve the access flags and write permission */
218 	pte_val(entry) &= PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY;
219 
220 	/*
221 	 * Setting the flags must be done atomically to avoid racing with the
222 	 * hardware update of the access/dirty state. The PTE_RDONLY bit must
223 	 * be set to the most permissive (lowest value) of *ptep and entry
224 	 * (calculated as: a & b == ~(~a | ~b)).
225 	 */
226 	pte_val(entry) ^= PTE_RDONLY;
227 	pteval = pte_val(pte);
228 	do {
229 		old_pteval = pteval;
230 		pteval ^= PTE_RDONLY;
231 		pteval |= pte_val(entry);
232 		pteval ^= PTE_RDONLY;
233 		pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
234 	} while (pteval != old_pteval);
235 
236 	/* Invalidate a stale read-only entry */
237 	if (dirty)
238 		flush_tlb_page(vma, address);
239 	return 1;
240 }
241 
242 static bool is_el1_instruction_abort(unsigned long esr)
243 {
244 	return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
245 }
246 
247 static bool is_el1_data_abort(unsigned long esr)
248 {
249 	return ESR_ELx_EC(esr) == ESR_ELx_EC_DABT_CUR;
250 }
251 
252 static inline bool is_el1_permission_fault(unsigned long addr, unsigned long esr,
253 					   struct pt_regs *regs)
254 {
255 	if (!is_el1_data_abort(esr) && !is_el1_instruction_abort(esr))
256 		return false;
257 
258 	if (esr_fsc_is_permission_fault(esr))
259 		return true;
260 
261 	if (is_ttbr0_addr(addr) && system_uses_ttbr0_pan())
262 		return esr_fsc_is_translation_fault(esr) &&
263 			(regs->pstate & PSR_PAN_BIT);
264 
265 	return false;
266 }
267 
268 static bool __kprobes is_spurious_el1_translation_fault(unsigned long addr,
269 							unsigned long esr,
270 							struct pt_regs *regs)
271 {
272 	unsigned long flags;
273 	u64 par, dfsc;
274 
275 	if (!is_el1_data_abort(esr) || !esr_fsc_is_translation_fault(esr))
276 		return false;
277 
278 	local_irq_save(flags);
279 	asm volatile("at s1e1r, %0" :: "r" (addr));
280 	isb();
281 	par = read_sysreg_par();
282 	local_irq_restore(flags);
283 
284 	/*
285 	 * If we now have a valid translation, treat the translation fault as
286 	 * spurious.
287 	 */
288 	if (!(par & SYS_PAR_EL1_F))
289 		return true;
290 
291 	/*
292 	 * If we got a different type of fault from the AT instruction,
293 	 * treat the translation fault as spurious.
294 	 */
295 	dfsc = FIELD_GET(SYS_PAR_EL1_FST, par);
296 	return !esr_fsc_is_translation_fault(dfsc);
297 }
298 
299 static void die_kernel_fault(const char *msg, unsigned long addr,
300 			     unsigned long esr, struct pt_regs *regs)
301 {
302 	bust_spinlocks(1);
303 
304 	pr_alert("Unable to handle kernel %s at virtual address %016lx\n", msg,
305 		 addr);
306 
307 	kasan_non_canonical_hook(addr);
308 
309 	mem_abort_decode(esr);
310 
311 	show_pte(addr);
312 	die("Oops", regs, esr);
313 	bust_spinlocks(0);
314 	make_task_dead(SIGKILL);
315 }
316 
317 #ifdef CONFIG_KASAN_HW_TAGS
318 static void report_tag_fault(unsigned long addr, unsigned long esr,
319 			     struct pt_regs *regs)
320 {
321 	/*
322 	 * SAS bits aren't set for all faults reported in EL1, so we can't
323 	 * find out access size.
324 	 */
325 	bool is_write = !!(esr & ESR_ELx_WNR);
326 	kasan_report((void *)addr, 0, is_write, regs->pc);
327 }
328 #else
329 /* Tag faults aren't enabled without CONFIG_KASAN_HW_TAGS. */
330 static inline void report_tag_fault(unsigned long addr, unsigned long esr,
331 				    struct pt_regs *regs) { }
332 #endif
333 
334 static void do_tag_recovery(unsigned long addr, unsigned long esr,
335 			   struct pt_regs *regs)
336 {
337 
338 	report_tag_fault(addr, esr, regs);
339 
340 	/*
341 	 * Disable MTE Tag Checking on the local CPU for the current EL.
342 	 * It will be done lazily on the other CPUs when they will hit a
343 	 * tag fault.
344 	 */
345 	sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF_MASK,
346 			 SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF, NONE));
347 	isb();
348 }
349 
350 static bool is_el1_mte_sync_tag_check_fault(unsigned long esr)
351 {
352 	unsigned long fsc = esr & ESR_ELx_FSC;
353 
354 	if (!is_el1_data_abort(esr))
355 		return false;
356 
357 	if (fsc == ESR_ELx_FSC_MTE)
358 		return true;
359 
360 	return false;
361 }
362 
363 static void __do_kernel_fault(unsigned long addr, unsigned long esr,
364 			      struct pt_regs *regs)
365 {
366 	const char *msg;
367 
368 	/*
369 	 * Are we prepared to handle this kernel fault?
370 	 * We are almost certainly not prepared to handle instruction faults.
371 	 */
372 	if (!is_el1_instruction_abort(esr) && fixup_exception(regs, esr))
373 		return;
374 
375 	if (WARN_RATELIMIT(is_spurious_el1_translation_fault(addr, esr, regs),
376 	    "Ignoring spurious kernel translation fault at virtual address %016lx\n", addr))
377 		return;
378 
379 	if (is_el1_mte_sync_tag_check_fault(esr)) {
380 		do_tag_recovery(addr, esr, regs);
381 
382 		return;
383 	}
384 
385 	if (is_el1_permission_fault(addr, esr, regs)) {
386 		if (esr & ESR_ELx_WNR)
387 			msg = "write to read-only memory";
388 		else if (is_el1_instruction_abort(esr))
389 			msg = "execute from non-executable memory";
390 		else
391 			msg = "read from unreadable memory";
392 	} else if (addr < PAGE_SIZE) {
393 		msg = "NULL pointer dereference";
394 	} else {
395 		if (esr_fsc_is_translation_fault(esr) &&
396 		    kfence_handle_page_fault(addr, esr & ESR_ELx_WNR, regs))
397 			return;
398 
399 		msg = "paging request";
400 	}
401 
402 	if (efi_runtime_fixup_exception(regs, msg))
403 		return;
404 
405 	die_kernel_fault(msg, addr, esr, regs);
406 }
407 
408 static void set_thread_esr(unsigned long address, unsigned long esr)
409 {
410 	current->thread.fault_address = address;
411 
412 	/*
413 	 * If the faulting address is in the kernel, we must sanitize the ESR.
414 	 * From userspace's point of view, kernel-only mappings don't exist
415 	 * at all, so we report them as level 0 translation faults.
416 	 * (This is not quite the way that "no mapping there at all" behaves:
417 	 * an alignment fault not caused by the memory type would take
418 	 * precedence over translation fault for a real access to empty
419 	 * space. Unfortunately we can't easily distinguish "alignment fault
420 	 * not caused by memory type" from "alignment fault caused by memory
421 	 * type", so we ignore this wrinkle and just return the translation
422 	 * fault.)
423 	 */
424 	if (!is_ttbr0_addr(current->thread.fault_address)) {
425 		switch (ESR_ELx_EC(esr)) {
426 		case ESR_ELx_EC_DABT_LOW:
427 			/*
428 			 * These bits provide only information about the
429 			 * faulting instruction, which userspace knows already.
430 			 * We explicitly clear bits which are architecturally
431 			 * RES0 in case they are given meanings in future.
432 			 * We always report the ESR as if the fault was taken
433 			 * to EL1 and so ISV and the bits in ISS[23:14] are
434 			 * clear. (In fact it always will be a fault to EL1.)
435 			 */
436 			esr &= ESR_ELx_EC_MASK | ESR_ELx_IL |
437 				ESR_ELx_CM | ESR_ELx_WNR;
438 			esr |= ESR_ELx_FSC_FAULT;
439 			break;
440 		case ESR_ELx_EC_IABT_LOW:
441 			/*
442 			 * Claim a level 0 translation fault.
443 			 * All other bits are architecturally RES0 for faults
444 			 * reported with that DFSC value, so we clear them.
445 			 */
446 			esr &= ESR_ELx_EC_MASK | ESR_ELx_IL;
447 			esr |= ESR_ELx_FSC_FAULT;
448 			break;
449 		default:
450 			/*
451 			 * This should never happen (entry.S only brings us
452 			 * into this code for insn and data aborts from a lower
453 			 * exception level). Fail safe by not providing an ESR
454 			 * context record at all.
455 			 */
456 			WARN(1, "ESR 0x%lx is not DABT or IABT from EL0\n", esr);
457 			esr = 0;
458 			break;
459 		}
460 	}
461 
462 	current->thread.fault_code = esr;
463 }
464 
465 static void do_bad_area(unsigned long far, unsigned long esr,
466 			struct pt_regs *regs)
467 {
468 	unsigned long addr = untagged_addr(far);
469 
470 	/*
471 	 * If we are in kernel mode at this point, we have no context to
472 	 * handle this fault with.
473 	 */
474 	if (user_mode(regs)) {
475 		const struct fault_info *inf = esr_to_fault_info(esr);
476 
477 		set_thread_esr(addr, esr);
478 		arm64_force_sig_fault(inf->sig, inf->code, far, inf->name);
479 	} else {
480 		__do_kernel_fault(addr, esr, regs);
481 	}
482 }
483 
484 static bool fault_from_pkey(unsigned long esr, struct vm_area_struct *vma,
485 			unsigned int mm_flags)
486 {
487 	unsigned long iss2 = ESR_ELx_ISS2(esr);
488 
489 	if (!system_supports_poe())
490 		return false;
491 
492 	if (esr_fsc_is_permission_fault(esr) && (iss2 & ESR_ELx_Overlay))
493 		return true;
494 
495 	return !arch_vma_access_permitted(vma,
496 			mm_flags & FAULT_FLAG_WRITE,
497 			mm_flags & FAULT_FLAG_INSTRUCTION,
498 			false);
499 }
500 
501 static bool is_gcs_fault(unsigned long esr)
502 {
503 	if (!esr_is_data_abort(esr))
504 		return false;
505 
506 	return ESR_ELx_ISS2(esr) & ESR_ELx_GCS;
507 }
508 
509 static bool is_el0_instruction_abort(unsigned long esr)
510 {
511 	return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
512 }
513 
514 /*
515  * Note: not valid for EL1 DC IVAC, but we never use that such that it
516  * should fault. EL0 cannot issue DC IVAC (undef).
517  */
518 static bool is_write_abort(unsigned long esr)
519 {
520 	return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM);
521 }
522 
523 static bool is_invalid_gcs_access(struct vm_area_struct *vma, u64 esr)
524 {
525 	if (!system_supports_gcs())
526 		return false;
527 
528 	if (unlikely(is_gcs_fault(esr))) {
529 		/* GCS accesses must be performed on a GCS page */
530 		if (!(vma->vm_flags & VM_SHADOW_STACK))
531 			return true;
532 	} else if (unlikely(vma->vm_flags & VM_SHADOW_STACK)) {
533 		/* Only GCS operations can write to a GCS page */
534 		return esr_is_data_abort(esr) && is_write_abort(esr);
535 	}
536 
537 	return false;
538 }
539 
540 static int __kprobes do_page_fault(unsigned long far, unsigned long esr,
541 				   struct pt_regs *regs)
542 {
543 	const struct fault_info *inf;
544 	struct mm_struct *mm = current->mm;
545 	vm_fault_t fault;
546 	unsigned long vm_flags;
547 	unsigned int mm_flags = FAULT_FLAG_DEFAULT;
548 	unsigned long addr = untagged_addr(far);
549 	struct vm_area_struct *vma;
550 	int si_code;
551 	int pkey = -1;
552 
553 	if (kprobe_page_fault(regs, esr))
554 		return 0;
555 
556 	/*
557 	 * If we're in an interrupt or have no user context, we must not take
558 	 * the fault.
559 	 */
560 	if (faulthandler_disabled() || !mm)
561 		goto no_context;
562 
563 	if (user_mode(regs))
564 		mm_flags |= FAULT_FLAG_USER;
565 
566 	/*
567 	 * vm_flags tells us what bits we must have in vma->vm_flags
568 	 * for the fault to be benign, __do_page_fault() would check
569 	 * vma->vm_flags & vm_flags and returns an error if the
570 	 * intersection is empty
571 	 */
572 	if (is_el0_instruction_abort(esr)) {
573 		/* It was exec fault */
574 		vm_flags = VM_EXEC;
575 		mm_flags |= FAULT_FLAG_INSTRUCTION;
576 	} else if (is_gcs_fault(esr)) {
577 		/*
578 		 * The GCS permission on a page implies both read and
579 		 * write so always handle any GCS fault as a write fault,
580 		 * we need to trigger CoW even for GCS reads.
581 		 */
582 		vm_flags = VM_WRITE;
583 		mm_flags |= FAULT_FLAG_WRITE;
584 	} else if (is_write_abort(esr)) {
585 		/* It was write fault */
586 		vm_flags = VM_WRITE;
587 		mm_flags |= FAULT_FLAG_WRITE;
588 	} else {
589 		/* It was read fault */
590 		vm_flags = VM_READ;
591 		/* Write implies read */
592 		vm_flags |= VM_WRITE;
593 		/* If EPAN is absent then exec implies read */
594 		if (!alternative_has_cap_unlikely(ARM64_HAS_EPAN))
595 			vm_flags |= VM_EXEC;
596 	}
597 
598 	if (is_ttbr0_addr(addr) && is_el1_permission_fault(addr, esr, regs)) {
599 		if (is_el1_instruction_abort(esr))
600 			die_kernel_fault("execution of user memory",
601 					 addr, esr, regs);
602 
603 		if (!insn_may_access_user(regs->pc, esr))
604 			die_kernel_fault("access to user memory outside uaccess routines",
605 					 addr, esr, regs);
606 	}
607 
608 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
609 
610 	if (!(mm_flags & FAULT_FLAG_USER))
611 		goto lock_mmap;
612 
613 	vma = lock_vma_under_rcu(mm, addr);
614 	if (!vma)
615 		goto lock_mmap;
616 
617 	if (is_invalid_gcs_access(vma, esr)) {
618 		vma_end_read(vma);
619 		fault = 0;
620 		si_code = SEGV_ACCERR;
621 		goto bad_area;
622 	}
623 
624 	if (!(vma->vm_flags & vm_flags)) {
625 		vma_end_read(vma);
626 		fault = 0;
627 		si_code = SEGV_ACCERR;
628 		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
629 		goto bad_area;
630 	}
631 
632 	if (fault_from_pkey(esr, vma, mm_flags)) {
633 		pkey = vma_pkey(vma);
634 		vma_end_read(vma);
635 		fault = 0;
636 		si_code = SEGV_PKUERR;
637 		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
638 		goto bad_area;
639 	}
640 
641 	fault = handle_mm_fault(vma, addr, mm_flags | FAULT_FLAG_VMA_LOCK, regs);
642 	if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
643 		vma_end_read(vma);
644 
645 	if (!(fault & VM_FAULT_RETRY)) {
646 		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
647 		goto done;
648 	}
649 	count_vm_vma_lock_event(VMA_LOCK_RETRY);
650 	if (fault & VM_FAULT_MAJOR)
651 		mm_flags |= FAULT_FLAG_TRIED;
652 
653 	/* Quick path to respond to signals */
654 	if (fault_signal_pending(fault, regs)) {
655 		if (!user_mode(regs))
656 			goto no_context;
657 		return 0;
658 	}
659 lock_mmap:
660 
661 retry:
662 	vma = lock_mm_and_find_vma(mm, addr, regs);
663 	if (unlikely(!vma)) {
664 		fault = 0;
665 		si_code = SEGV_MAPERR;
666 		goto bad_area;
667 	}
668 
669 	if (!(vma->vm_flags & vm_flags)) {
670 		mmap_read_unlock(mm);
671 		fault = 0;
672 		si_code = SEGV_ACCERR;
673 		goto bad_area;
674 	}
675 
676 	if (fault_from_pkey(esr, vma, mm_flags)) {
677 		pkey = vma_pkey(vma);
678 		mmap_read_unlock(mm);
679 		fault = 0;
680 		si_code = SEGV_PKUERR;
681 		goto bad_area;
682 	}
683 
684 	fault = handle_mm_fault(vma, addr, mm_flags, regs);
685 
686 	/* Quick path to respond to signals */
687 	if (fault_signal_pending(fault, regs)) {
688 		if (!user_mode(regs))
689 			goto no_context;
690 		return 0;
691 	}
692 
693 	/* The fault is fully completed (including releasing mmap lock) */
694 	if (fault & VM_FAULT_COMPLETED)
695 		return 0;
696 
697 	if (fault & VM_FAULT_RETRY) {
698 		mm_flags |= FAULT_FLAG_TRIED;
699 		goto retry;
700 	}
701 	mmap_read_unlock(mm);
702 
703 done:
704 	/* Handle the "normal" (no error) case first. */
705 	if (likely(!(fault & VM_FAULT_ERROR)))
706 		return 0;
707 
708 	si_code = SEGV_MAPERR;
709 bad_area:
710 	/*
711 	 * If we are in kernel mode at this point, we have no context to
712 	 * handle this fault with.
713 	 */
714 	if (!user_mode(regs))
715 		goto no_context;
716 
717 	if (fault & VM_FAULT_OOM) {
718 		/*
719 		 * We ran out of memory, call the OOM killer, and return to
720 		 * userspace (which will retry the fault, or kill us if we got
721 		 * oom-killed).
722 		 */
723 		pagefault_out_of_memory();
724 		return 0;
725 	}
726 
727 	inf = esr_to_fault_info(esr);
728 	set_thread_esr(addr, esr);
729 	if (fault & VM_FAULT_SIGBUS) {
730 		/*
731 		 * We had some memory, but were unable to successfully fix up
732 		 * this page fault.
733 		 */
734 		arm64_force_sig_fault(SIGBUS, BUS_ADRERR, far, inf->name);
735 	} else if (fault & (VM_FAULT_HWPOISON_LARGE | VM_FAULT_HWPOISON)) {
736 		unsigned int lsb;
737 
738 		lsb = PAGE_SHIFT;
739 		if (fault & VM_FAULT_HWPOISON_LARGE)
740 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
741 
742 		arm64_force_sig_mceerr(BUS_MCEERR_AR, far, lsb, inf->name);
743 	} else {
744 		/*
745 		 * The pkey value that we return to userspace can be different
746 		 * from the pkey that caused the fault.
747 		 *
748 		 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
749 		 * 2. T1   : set POR_EL0 to deny access to pkey=4, touches, page
750 		 * 3. T1   : faults...
751 		 * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
752 		 * 5. T1   : enters fault handler, takes mmap_lock, etc...
753 		 * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
754 		 *	     faulted on a pte with its pkey=4.
755 		 */
756 		/* Something tried to access memory that out of memory map */
757 		if (si_code == SEGV_PKUERR)
758 			arm64_force_sig_fault_pkey(far, inf->name, pkey);
759 		else
760 			arm64_force_sig_fault(SIGSEGV, si_code, far, inf->name);
761 	}
762 
763 	return 0;
764 
765 no_context:
766 	__do_kernel_fault(addr, esr, regs);
767 	return 0;
768 }
769 
770 static int __kprobes do_translation_fault(unsigned long far,
771 					  unsigned long esr,
772 					  struct pt_regs *regs)
773 {
774 	unsigned long addr = untagged_addr(far);
775 
776 	if (is_ttbr0_addr(addr))
777 		return do_page_fault(far, esr, regs);
778 
779 	do_bad_area(far, esr, regs);
780 	return 0;
781 }
782 
783 static int do_alignment_fault(unsigned long far, unsigned long esr,
784 			      struct pt_regs *regs)
785 {
786 	if (IS_ENABLED(CONFIG_COMPAT_ALIGNMENT_FIXUPS) &&
787 	    compat_user_mode(regs))
788 		return do_compat_alignment_fixup(far, regs);
789 	do_bad_area(far, esr, regs);
790 	return 0;
791 }
792 
793 static int do_bad(unsigned long far, unsigned long esr, struct pt_regs *regs)
794 {
795 	return 1; /* "fault" */
796 }
797 
798 static int do_sea(unsigned long far, unsigned long esr, struct pt_regs *regs)
799 {
800 	const struct fault_info *inf;
801 	unsigned long siaddr;
802 
803 	inf = esr_to_fault_info(esr);
804 
805 	if (user_mode(regs) && apei_claim_sea(regs) == 0) {
806 		/*
807 		 * APEI claimed this as a firmware-first notification.
808 		 * Some processing deferred to task_work before ret_to_user().
809 		 */
810 		return 0;
811 	}
812 
813 	if (esr & ESR_ELx_FnV) {
814 		siaddr = 0;
815 	} else {
816 		/*
817 		 * The architecture specifies that the tag bits of FAR_EL1 are
818 		 * UNKNOWN for synchronous external aborts. Mask them out now
819 		 * so that userspace doesn't see them.
820 		 */
821 		siaddr  = untagged_addr(far);
822 	}
823 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
824 	arm64_notify_die(inf->name, regs, inf->sig, inf->code, siaddr, esr);
825 
826 	return 0;
827 }
828 
829 static int do_tag_check_fault(unsigned long far, unsigned long esr,
830 			      struct pt_regs *regs)
831 {
832 	/*
833 	 * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
834 	 * for tag check faults. Set them to corresponding bits in the untagged
835 	 * address if ARM64_MTE_FAR isn't supported.
836 	 * Otherwise, bits 63:60 of FAR_EL1 are not UNKNOWN.
837 	 */
838 	if (!cpus_have_cap(ARM64_MTE_FAR))
839 		far = (__untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK);
840 
841 	do_bad_area(far, esr, regs);
842 	return 0;
843 }
844 
845 static const struct fault_info fault_info[] = {
846 	{ do_bad,		SIGKILL, SI_KERNEL,	"ttbr address size fault"	},
847 	{ do_bad,		SIGKILL, SI_KERNEL,	"level 1 address size fault"	},
848 	{ do_bad,		SIGKILL, SI_KERNEL,	"level 2 address size fault"	},
849 	{ do_bad,		SIGKILL, SI_KERNEL,	"level 3 address size fault"	},
850 	{ do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"level 0 translation fault"	},
851 	{ do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"level 1 translation fault"	},
852 	{ do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"level 2 translation fault"	},
853 	{ do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"level 3 translation fault"	},
854 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 0 access flag fault"	},
855 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 1 access flag fault"	},
856 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 2 access flag fault"	},
857 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 3 access flag fault"	},
858 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 0 permission fault"	},
859 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 1 permission fault"	},
860 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 2 permission fault"	},
861 	{ do_page_fault,	SIGSEGV, SEGV_ACCERR,	"level 3 permission fault"	},
862 	{ do_sea,		SIGBUS,  BUS_OBJERR,	"synchronous external abort"	},
863 	{ do_tag_check_fault,	SIGSEGV, SEGV_MTESERR,	"synchronous tag check fault"	},
864 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 18"			},
865 	{ do_sea,		SIGKILL, SI_KERNEL,	"level -1 (translation table walk)"	},
866 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 0 (translation table walk)"	},
867 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 1 (translation table walk)"	},
868 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 2 (translation table walk)"	},
869 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 3 (translation table walk)"	},
870 	{ do_sea,		SIGBUS,  BUS_OBJERR,	"synchronous parity or ECC error" },	// Reserved when RAS is implemented
871 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 25"			},
872 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 26"			},
873 	{ do_sea,		SIGKILL, SI_KERNEL,	"level -1 synchronous parity error (translation table walk)"	},	// Reserved when RAS is implemented
874 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 0 synchronous parity error (translation table walk)"	},	// Reserved when RAS is implemented
875 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 1 synchronous parity error (translation table walk)"	},	// Reserved when RAS is implemented
876 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 2 synchronous parity error (translation table walk)"	},	// Reserved when RAS is implemented
877 	{ do_sea,		SIGKILL, SI_KERNEL,	"level 3 synchronous parity error (translation table walk)"	},	// Reserved when RAS is implemented
878 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 32"			},
879 	{ do_alignment_fault,	SIGBUS,  BUS_ADRALN,	"alignment fault"		},
880 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 34"			},
881 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 35"			},
882 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 36"			},
883 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 37"			},
884 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 38"			},
885 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 39"			},
886 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 40"			},
887 	{ do_bad,		SIGKILL, SI_KERNEL,	"level -1 address size fault"	},
888 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 42"			},
889 	{ do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"level -1 translation fault"	},
890 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 44"			},
891 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 45"			},
892 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 46"			},
893 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 47"			},
894 	{ do_bad,		SIGKILL, SI_KERNEL,	"TLB conflict abort"		},
895 	{ do_bad,		SIGKILL, SI_KERNEL,	"Unsupported atomic hardware update fault"	},
896 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 50"			},
897 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 51"			},
898 	{ do_bad,		SIGKILL, SI_KERNEL,	"implementation fault (lockdown abort)" },
899 	{ do_bad,		SIGBUS,  BUS_OBJERR,	"implementation fault (unsupported exclusive)" },
900 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 54"			},
901 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 55"			},
902 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 56"			},
903 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 57"			},
904 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 58" 			},
905 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 59"			},
906 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 60"			},
907 	{ do_bad,		SIGKILL, SI_KERNEL,	"section domain fault"		},
908 	{ do_bad,		SIGKILL, SI_KERNEL,	"page domain fault"		},
909 	{ do_bad,		SIGKILL, SI_KERNEL,	"unknown 63"			},
910 };
911 
912 void do_mem_abort(unsigned long far, unsigned long esr, struct pt_regs *regs)
913 {
914 	const struct fault_info *inf = esr_to_fault_info(esr);
915 	unsigned long addr = untagged_addr(far);
916 
917 	if (!inf->fn(far, esr, regs))
918 		return;
919 
920 	if (!user_mode(regs))
921 		die_kernel_fault(inf->name, addr, esr, regs);
922 
923 	/*
924 	 * At this point we have an unrecognized fault type whose tag bits may
925 	 * have been defined as UNKNOWN. Therefore we only expose the untagged
926 	 * address to the signal handler.
927 	 */
928 	arm64_notify_die(inf->name, regs, inf->sig, inf->code, addr, esr);
929 }
930 NOKPROBE_SYMBOL(do_mem_abort);
931 
932 void do_sp_pc_abort(unsigned long addr, unsigned long esr, struct pt_regs *regs)
933 {
934 	arm64_notify_die("SP/PC alignment exception", regs, SIGBUS, BUS_ADRALN,
935 			 addr, esr);
936 }
937 NOKPROBE_SYMBOL(do_sp_pc_abort);
938 
939 /*
940  * Used during anonymous page fault handling.
941  */
942 struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
943 						unsigned long vaddr)
944 {
945 	gfp_t flags = GFP_HIGHUSER_MOVABLE | __GFP_ZERO;
946 
947 	/*
948 	 * If the page is mapped with PROT_MTE, initialise the tags at the
949 	 * point of allocation and page zeroing as this is usually faster than
950 	 * separate DC ZVA and STGM.
951 	 */
952 	if (vma->vm_flags & VM_MTE)
953 		flags |= __GFP_ZEROTAGS;
954 
955 	return vma_alloc_folio(flags, 0, vma, vaddr);
956 }
957 
958 void tag_clear_highpage(struct page *page)
959 {
960 	/* Newly allocated page, shouldn't have been tagged yet */
961 	WARN_ON_ONCE(!try_page_mte_tagging(page));
962 	mte_zero_clear_page_tags(page_address(page));
963 	set_page_mte_tagged(page);
964 }
965