]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/powerpc/mm/fault.c
powerpc/mm: Set fault flags earlier
[mirror_ubuntu-jammy-kernel.git] / arch / powerpc / mm / fault.c
CommitLineData
14cf11af 1/*
14cf11af
PM
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
5 * Derived from "arch/i386/mm/fault.c"
6 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
7 *
8 * Modified by Cort Dougan and Paul Mackerras.
9 *
10 * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
14cf11af
PM
18#include <linux/signal.h>
19#include <linux/sched.h>
68db0cf1 20#include <linux/sched/task_stack.h>
14cf11af
PM
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/types.h>
25#include <linux/ptrace.h>
26#include <linux/mman.h>
27#include <linux/mm.h>
28#include <linux/interrupt.h>
29#include <linux/highmem.h>
8a39b05f 30#include <linux/extable.h>
14cf11af 31#include <linux/kprobes.h>
1eeb66a1 32#include <linux/kdebug.h>
cdd6c482 33#include <linux/perf_event.h>
76462232 34#include <linux/ratelimit.h>
ba12eede 35#include <linux/context_tracking.h>
9d57472f 36#include <linux/hugetlb.h>
70ffdb93 37#include <linux/uaccess.h>
14cf11af 38
40900194 39#include <asm/firmware.h>
14cf11af
PM
40#include <asm/page.h>
41#include <asm/pgtable.h>
42#include <asm/mmu.h>
43#include <asm/mmu_context.h>
14cf11af 44#include <asm/tlbflush.h>
14cf11af 45#include <asm/siginfo.h>
ae3a197e 46#include <asm/debug.h>
4f9e87c0 47
c3dcf53a
JX
48#include "icswx.h"
49
bb4be50e 50static inline bool notify_page_fault(struct pt_regs *regs)
4f9e87c0 51{
bb4be50e 52 bool ret = false;
9f90b997 53
bb4be50e 54#ifdef CONFIG_KPROBES
9f90b997
CH
55 /* kprobe_running() needs smp_processor_id() */
56 if (!user_mode(regs)) {
57 preempt_disable();
58 if (kprobe_running() && kprobe_fault_handler(regs, 11))
bb4be50e 59 ret = true;
9f90b997
CH
60 preempt_enable();
61 }
bb4be50e
BH
62#endif /* CONFIG_KPROBES */
63
64 if (unlikely(debugger_fault_handler(regs)))
65 ret = true;
4f9e87c0 66
9f90b997 67 return ret;
4f9e87c0 68}
4f9e87c0 69
14cf11af
PM
70/*
71 * Check whether the instruction at regs->nip is a store using
72 * an update addressing form which will update r1.
73 */
74static int store_updates_sp(struct pt_regs *regs)
75{
76 unsigned int inst;
77
78 if (get_user(inst, (unsigned int __user *)regs->nip))
79 return 0;
80 /* check for 1 in the rA field */
81 if (((inst >> 16) & 0x1f) != 1)
82 return 0;
83 /* check major opcode */
84 switch (inst >> 26) {
85 case 37: /* stwu */
86 case 39: /* stbu */
87 case 45: /* sthu */
88 case 53: /* stfsu */
89 case 55: /* stfdu */
90 return 1;
91 case 62: /* std or stdu */
92 return (inst & 3) == 1;
93 case 31:
94 /* check minor opcode */
95 switch ((inst >> 1) & 0x3ff) {
96 case 181: /* stdux */
97 case 183: /* stwux */
98 case 247: /* stbux */
99 case 439: /* sthux */
100 case 695: /* stfsux */
101 case 759: /* stfdux */
102 return 1;
103 }
104 }
105 return 0;
106}
9be72573
BH
107/*
108 * do_page_fault error handling helpers
109 */
110
c3350602
BH
111static int
112__bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
113{
114 /*
115 * If we are in kernel mode, bail out with a SEGV, this will
116 * be caught by the assembly which will restore the non-volatile
117 * registers before calling bad_page_fault()
118 */
119 if (!user_mode(regs))
120 return SIGSEGV;
121
122 _exception(SIGSEGV, regs, si_code, address);
123
124 return 0;
125}
126
127static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
128{
129 return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
130}
131
132static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
133{
134 struct mm_struct *mm = current->mm;
135
136 /*
137 * Something tried to access memory that isn't in our memory map..
138 * Fix it, but check if it's kernel or user first..
139 */
140 up_read(&mm->mmap_sem);
141
142 return __bad_area_nosemaphore(regs, address, si_code);
143}
144
145static noinline int bad_area(struct pt_regs *regs, unsigned long address)
146{
147 return __bad_area(regs, address, SEGV_MAPERR);
148}
149
3913fdd7
AB
150static int do_sigbus(struct pt_regs *regs, unsigned long address,
151 unsigned int fault)
9be72573
BH
152{
153 siginfo_t info;
9d57472f 154 unsigned int lsb = 0;
9be72573 155
63af5262 156 if (!user_mode(regs))
b5c8f0fd 157 return SIGBUS;
63af5262
AB
158
159 current->thread.trap_nr = BUS_ADRERR;
160 info.si_signo = SIGBUS;
161 info.si_errno = 0;
162 info.si_code = BUS_ADRERR;
163 info.si_addr = (void __user *)address;
3913fdd7
AB
164#ifdef CONFIG_MEMORY_FAILURE
165 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
166 pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
167 current->comm, current->pid, address);
168 info.si_code = BUS_MCEERR_AR;
169 }
9d57472f
AB
170
171 if (fault & VM_FAULT_HWPOISON_LARGE)
172 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
173 if (fault & VM_FAULT_HWPOISON)
174 lsb = PAGE_SHIFT;
3913fdd7 175#endif
9d57472f 176 info.si_addr_lsb = lsb;
63af5262 177 force_sig_info(SIGBUS, &info, current);
b5c8f0fd 178 return 0;
9be72573
BH
179}
180
181static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
182{
183 /*
b5c8f0fd
BH
184 * Kernel page fault interrupted by SIGKILL. We have no reason to
185 * continue processing.
9be72573 186 */
b5c8f0fd
BH
187 if (fatal_signal_pending(current) && !user_mode(regs))
188 return SIGKILL;
9be72573
BH
189
190 /* Out of memory */
c2d23f91 191 if (fault & VM_FAULT_OOM) {
c2d23f91
DR
192 /*
193 * We ran out of memory, or some other thing happened to us that
194 * made us unable to handle the page fault gracefully.
195 */
196 if (!user_mode(regs))
b5c8f0fd 197 return SIGSEGV;
c2d23f91 198 pagefault_out_of_memory();
b5c8f0fd
BH
199 } else {
200 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
201 VM_FAULT_HWPOISON_LARGE))
202 return do_sigbus(regs, addr, fault);
203 else if (fault & VM_FAULT_SIGSEGV)
204 return bad_area_nosemaphore(regs, addr);
205 else
206 BUG();
c2d23f91 207 }
b5c8f0fd 208 return 0;
9be72573 209}
14cf11af 210
d3ca5874
BH
211/* Is this a bad kernel fault ? */
212static bool bad_kernel_fault(bool is_exec, unsigned long error_code,
213 unsigned long address)
214{
215 if (is_exec && (error_code & (DSISR_NOEXEC_OR_G | DSISR_KEYFAULT))) {
216 printk_ratelimited(KERN_CRIT "kernel tried to execute"
217 " exec-protected page (%lx) -"
218 "exploit attempt? (uid: %d)\n",
219 address, from_kuid(&init_user_ns,
220 current_uid()));
221 }
222 return is_exec || (address >= TASK_SIZE);
223}
224
3da02648
BH
225#ifdef CONFIG_PPC_SMLPAR
226static inline void cmo_account_page_fault(void)
227{
228 if (firmware_has_feature(FW_FEATURE_CMO)) {
229 u32 page_ins;
230
231 preempt_disable();
232 page_ins = be32_to_cpu(get_lppaca()->page_ins);
233 page_ins += 1 << PAGE_FACTOR;
234 get_lppaca()->page_ins = cpu_to_be32(page_ins);
235 preempt_enable();
236 }
237}
238#else
239static inline void cmo_account_page_fault(void) { }
240#endif /* CONFIG_PPC_SMLPAR */
241
2865d08d
BH
242#ifdef CONFIG_PPC_STD_MMU
243static void sanity_check_fault(bool is_write, unsigned long error_code)
244{
245 /*
246 * For hash translation mode, we should never get a
247 * PROTFAULT. Any update to pte to reduce access will result in us
248 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
249 * fault instead of DSISR_PROTFAULT.
250 *
251 * A pte update to relax the access will not result in a hash page table
252 * entry invalidate and hence can result in DSISR_PROTFAULT.
253 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
254 * the special !is_write in the below conditional.
255 *
256 * For platforms that doesn't supports coherent icache and do support
257 * per page noexec bit, we do setup things such that we do the
258 * sync between D/I cache via fault. But that is handled via low level
259 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
260 * here in such case.
261 *
262 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
263 * check should handle those and hence we should fall to the bad_area
264 * handling correctly.
265 *
266 * For embedded with per page exec support that doesn't support coherent
267 * icache we do get PROTFAULT and we handle that D/I cache sync in
268 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
269 * is conditional for server MMU.
270 *
271 * For radix, we can get prot fault for autonuma case, because radix
272 * page table will have them marked noaccess for user.
273 */
274 if (!radix_enabled() && !is_write)
275 WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
276}
277#else
278static void sanity_check_fault(bool is_write, unsigned long error_code) { }
279#endif /* CONFIG_PPC_STD_MMU */
280
41b464e5
BH
281/*
282 * Define the correct "is_write" bit in error_code based
283 * on the processor family
284 */
285#if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
286#define page_fault_is_write(__err) ((__err) & ESR_DST)
f3d96e69 287#define page_fault_is_bad(__err) (0)
41b464e5
BH
288#else
289#define page_fault_is_write(__err) ((__err) & DSISR_ISSTORE)
f3d96e69
BH
290#if defined(CONFIG_8xx)
291#define page_fault_is_bad(__err) ((__err) & 0x10000000)
292#elif defined(CONFIG_PPC64)
293#define page_fault_is_bad(__err) ((__err) & DSISR_BAD_FAULT_64S)
294#else
295#define page_fault_is_bad(__err) ((__err) & DSISR_BAD_FAULT_32S)
296#endif
41b464e5
BH
297#endif
298
14cf11af
PM
299/*
300 * For 600- and 800-family processors, the error_code parameter is DSISR
301 * for a data fault, SRR1 for an instruction fault. For 400-family processors
302 * the error_code parameter is ESR for a data fault, 0 for an instruction
303 * fault.
304 * For 64-bit processors, the error_code parameter is
305 * - DSISR for a non-SLB data access fault,
306 * - SRR1 & 0x08000000 for a non-SLB instruction access fault
307 * - 0 any SLB fault.
308 *
309 * The return value is 0 if the fault was handled, or the signal
310 * number if this is a kernel fault that can't be handled here.
311 */
7afad422
BH
312static int __do_page_fault(struct pt_regs *regs, unsigned long address,
313 unsigned long error_code)
14cf11af
PM
314{
315 struct vm_area_struct * vma;
316 struct mm_struct *mm = current->mm;
9be72573 317 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
c433ec04 318 int is_exec = TRAP(regs) == 0x400;
da929f6a 319 int is_user = user_mode(regs);
41b464e5 320 int is_write = page_fault_is_write(error_code);
9be72573 321 int fault;
b5c8f0fd 322 int store_update_sp = 0;
14cf11af 323
c3dcf53a
JX
324#ifdef CONFIG_PPC_ICSWX
325 /*
326 * we need to do this early because this "data storage
327 * interrupt" does not update the DAR/DEAR so we don't want to
328 * look at it
329 */
330 if (error_code & ICSWX_DSI_UCT) {
b5c8f0fd 331 int rc = acop_handle_fault(regs, address, error_code);
9be72573 332 if (rc)
65d47fd4 333 return rc;
c3dcf53a 334 }
9be72573 335#endif /* CONFIG_PPC_ICSWX */
c3dcf53a 336
9f90b997 337 if (notify_page_fault(regs))
65d47fd4 338 return 0;
14cf11af 339
f3d96e69 340 if (unlikely(page_fault_is_bad(error_code))) {
65d47fd4 341 if (is_user) {
f3d96e69 342 _exception(SIGBUS, regs, BUS_OBJERR, address);
65d47fd4
BH
343 return 0;
344 }
345 return SIGBUS;
e6c8290a 346 }
e6c8290a 347
2865d08d
BH
348 /* Additional sanity check(s) */
349 sanity_check_fault(is_write, error_code);
350
d7df2443
BH
351 /*
352 * The kernel should never take an execute fault nor should it
353 * take a page fault to a kernel address.
354 */
d3ca5874 355 if (unlikely(!is_user && bad_kernel_fault(is_exec, error_code, address)))
65d47fd4 356 return SIGSEGV;
14cf11af 357
11ccdd33
BH
358 /*
359 * If we're in an interrupt, have no user context or are running
360 * in a region with pagefaults disabled then we must not take the fault
361 */
362 if (unlikely(faulthandler_disabled() || !mm)) {
363 if (is_user)
364 printk_ratelimited(KERN_ERR "Page fault in user mode"
365 " with faulthandler_disabled()=%d"
366 " mm=%p\n",
367 faulthandler_disabled(), mm);
368 return bad_area_nosemaphore(regs, address);
369 }
370
a546498f
BH
371 /* We restore the interrupt state now */
372 if (!arch_irq_disabled_regs(regs))
373 local_irq_enable();
374
a8b0ca17 375 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
7dd1fcc2 376
69e044dd
AK
377 /*
378 * We want to do this outside mmap_sem, because reading code around nip
379 * can result in fault, which will cause a deadlock when called with
380 * mmap_sem held
381 */
da929f6a 382 if (is_write && is_user)
69e044dd
AK
383 store_update_sp = store_updates_sp(regs);
384
da929f6a 385 if (is_user)
759496ba 386 flags |= FAULT_FLAG_USER;
d2e0d2c5
BH
387 if (is_write)
388 flags |= FAULT_FLAG_WRITE;
389 if (is_exec)
390 flags |= FAULT_FLAG_INSTRUCTION;
759496ba 391
14cf11af
PM
392 /* When running in the kernel we expect faults to occur only to
393 * addresses in user space. All other faults represent errors in the
fc5266ea
AB
394 * kernel and should generate an OOPS. Unfortunately, in the case of an
395 * erroneous fault occurring in a code path which already holds mmap_sem
14cf11af
PM
396 * we will deadlock attempting to validate the fault against the
397 * address space. Luckily the kernel only validly references user
398 * space from well defined areas of code, which are listed in the
399 * exceptions table.
400 *
401 * As the vast majority of faults will be valid we will only perform
fc5266ea 402 * the source reference check when there is a possibility of a deadlock.
14cf11af
PM
403 * Attempt to lock the address space, if we cannot we then validate the
404 * source. If this is invalid we can skip the address space check,
405 * thus avoiding the deadlock.
406 */
b15021d9 407 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
da929f6a 408 if (!is_user && !search_exception_tables(regs->nip))
c3350602 409 return bad_area_nosemaphore(regs, address);
14cf11af 410
9be72573 411retry:
14cf11af 412 down_read(&mm->mmap_sem);
a546498f
BH
413 } else {
414 /*
415 * The above down_read_trylock() might have succeeded in
416 * which case we'll have missed the might_sleep() from
417 * down_read():
418 */
419 might_sleep();
14cf11af
PM
420 }
421
422 vma = find_vma(mm, address);
b15021d9 423 if (unlikely(!vma))
c3350602 424 return bad_area(regs, address);
b15021d9 425 if (likely(vma->vm_start <= address))
14cf11af 426 goto good_area;
b15021d9 427 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
c3350602 428 return bad_area(regs, address);
14cf11af
PM
429
430 /*
431 * N.B. The POWER/Open ABI allows programs to access up to
432 * 288 bytes below the stack pointer.
433 * The kernel signal delivery code writes up to about 1.5kB
434 * below the stack pointer (r1) before decrementing it.
435 * The exec code can write slightly over 640kB to the stack
436 * before setting the user r1. Thus we allow the stack to
437 * expand to 1MB without further checks.
438 */
439 if (address + 0x100000 < vma->vm_end) {
440 /* get user regs even if this fault is in kernel mode */
441 struct pt_regs *uregs = current->thread.regs;
442 if (uregs == NULL)
c3350602 443 return bad_area(regs, address);
14cf11af
PM
444
445 /*
446 * A user-mode access to an address a long way below
447 * the stack pointer is only valid if the instruction
448 * is one which would update the stack pointer to the
449 * address accessed if the instruction completed,
450 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
451 * (or the byte, halfword, float or double forms).
452 *
453 * If we don't check this then any write to the area
454 * between the last mapped region and the stack will
455 * expand the stack rather than segfaulting.
456 */
69e044dd 457 if (address + 2048 < uregs->gpr[1] && !store_update_sp)
c3350602 458 return bad_area(regs, address);
14cf11af 459 }
b15021d9 460 if (unlikely(expand_stack(vma, address)))
c3350602 461 return bad_area(regs, address);
14cf11af
PM
462
463good_area:
14cf11af 464 if (is_exec) {
08ae6cc1
PM
465 /*
466 * Allow execution from readable areas if the MMU does not
467 * provide separate controls over reading and executing.
8d30c14c
BH
468 *
469 * Note: That code used to not be enabled for 4xx/BookE.
470 * It is now as I/D cache coherency for these is done at
471 * set_pte_at() time and I see no reason why the test
472 * below wouldn't be valid on those processors. This -may-
473 * break programs compiled with a really old ABI though.
08ae6cc1 474 */
b15021d9
BH
475 if (unlikely(!(vma->vm_flags & VM_EXEC) &&
476 (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
477 !(vma->vm_flags & (VM_READ | VM_WRITE)))))
c3350602 478 return bad_area(regs, address);
14cf11af
PM
479 /* a write */
480 } else if (is_write) {
b15021d9 481 if (unlikely(!(vma->vm_flags & VM_WRITE)))
c3350602 482 return bad_area(regs, address);
14cf11af
PM
483 /* a read */
484 } else {
b15021d9 485 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
c3350602 486 return bad_area(regs, address);
14cf11af
PM
487 }
488
489 /*
490 * If for any reason at all we couldn't handle the fault,
491 * make sure we exit gracefully rather than endlessly redo
492 * the fault.
493 */
dcddffd4 494 fault = handle_mm_fault(vma, address, flags);
14c02e41
LD
495
496 /*
497 * Handle the retry right now, the mmap_sem has been released in that
498 * case.
499 */
500 if (unlikely(fault & VM_FAULT_RETRY)) {
501 /* We retry only once */
502 if (flags & FAULT_FLAG_ALLOW_RETRY) {
503 /*
504 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
505 * of starvation.
506 */
507 flags &= ~FAULT_FLAG_ALLOW_RETRY;
508 flags |= FAULT_FLAG_TRIED;
509 if (!fatal_signal_pending(current))
510 goto retry;
511 }
14c02e41 512
b5c8f0fd
BH
513 /*
514 * User mode? Just return to handle the fatal exception otherwise
515 * return to bad_page_fault
516 */
517 return is_user ? 0 : SIGBUS;
14cf11af 518 }
9be72573 519
b5c8f0fd
BH
520 up_read(&current->mm->mmap_sem);
521
522 if (unlikely(fault & VM_FAULT_ERROR))
523 return mm_fault_error(regs, address, fault);
524
9be72573 525 /*
14c02e41 526 * Major/minor page fault accounting.
9be72573 527 */
14c02e41
LD
528 if (fault & VM_FAULT_MAJOR) {
529 current->maj_flt++;
04aafdc6 530 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
3da02648 531 cmo_account_page_fault();
14c02e41
LD
532 } else {
533 current->min_flt++;
04aafdc6 534 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
ac17dc8e 535 }
c3350602 536 return 0;
7afad422
BH
537}
538NOKPROBE_SYMBOL(__do_page_fault);
539
540int do_page_fault(struct pt_regs *regs, unsigned long address,
541 unsigned long error_code)
542{
543 enum ctx_state prev_state = exception_enter();
544 int rc = __do_page_fault(regs, address, error_code);
ba12eede
LZ
545 exception_exit(prev_state);
546 return rc;
14cf11af 547}
03465f89 548NOKPROBE_SYMBOL(do_page_fault);
14cf11af
PM
549
550/*
551 * bad_page_fault is called when we have a bad access from the kernel.
552 * It is called from the DSI and ISI handlers in head.S and from some
553 * of the procedures in traps.c.
554 */
555void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
556{
557 const struct exception_table_entry *entry;
558
559 /* Are we prepared to handle this fault? */
560 if ((entry = search_exception_tables(regs->nip)) != NULL) {
61a92f70 561 regs->nip = extable_fixup(entry);
14cf11af
PM
562 return;
563 }
564
565 /* kernel has accessed a bad area */
723925b7 566
723925b7 567 switch (regs->trap) {
a416dd8d
ME
568 case 0x300:
569 case 0x380:
570 printk(KERN_ALERT "Unable to handle kernel paging request for "
571 "data at address 0x%08lx\n", regs->dar);
572 break;
573 case 0x400:
574 case 0x480:
575 printk(KERN_ALERT "Unable to handle kernel paging request for "
576 "instruction fetch\n");
577 break;
eab861a7
AB
578 case 0x600:
579 printk(KERN_ALERT "Unable to handle kernel paging request for "
580 "unaligned access at address 0x%08lx\n", regs->dar);
581 break;
a416dd8d
ME
582 default:
583 printk(KERN_ALERT "Unable to handle kernel paging request for "
584 "unknown fault\n");
585 break;
723925b7
OJ
586 }
587 printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
588 regs->nip);
589
a70857e4 590 if (task_stack_end_corrupted(current))
28b54990
AB
591 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
592
14cf11af
PM
593 die("Kernel access of bad area", regs, sig);
594}