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