1862674d4SLey Foon Tan /*
2862674d4SLey Foon Tan * Copyright (C) 2009 Wind River Systems Inc
3862674d4SLey Foon Tan * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
4862674d4SLey Foon Tan *
5862674d4SLey Foon Tan * based on arch/mips/mm/fault.c which is:
6862674d4SLey Foon Tan *
7862674d4SLey Foon Tan * Copyright (C) 1995-2000 Ralf Baechle
8862674d4SLey Foon Tan *
9862674d4SLey Foon Tan * This file is subject to the terms and conditions of the GNU General Public
10862674d4SLey Foon Tan * License. See the file "COPYING" in the main directory of this archive
11862674d4SLey Foon Tan * for more details.
12862674d4SLey Foon Tan */
13862674d4SLey Foon Tan
14862674d4SLey Foon Tan #include <linux/signal.h>
15862674d4SLey Foon Tan #include <linux/sched.h>
16b17b0153SIngo Molnar #include <linux/sched/debug.h>
17862674d4SLey Foon Tan #include <linux/interrupt.h>
18862674d4SLey Foon Tan #include <linux/kernel.h>
19862674d4SLey Foon Tan #include <linux/errno.h>
20862674d4SLey Foon Tan #include <linux/string.h>
21862674d4SLey Foon Tan #include <linux/types.h>
22862674d4SLey Foon Tan #include <linux/ptrace.h>
23862674d4SLey Foon Tan #include <linux/mman.h>
24862674d4SLey Foon Tan #include <linux/mm.h>
257e227e88SPaul Gortmaker #include <linux/extable.h>
26862674d4SLey Foon Tan #include <linux/uaccess.h>
274487dcf9SPeter Xu #include <linux/perf_event.h>
28862674d4SLey Foon Tan
29862674d4SLey Foon Tan #include <asm/mmu_context.h>
30862674d4SLey Foon Tan #include <asm/traps.h>
31862674d4SLey Foon Tan
32862674d4SLey Foon Tan #define EXC_SUPERV_INSN_ACCESS 9 /* Supervisor only instruction address */
33862674d4SLey Foon Tan #define EXC_SUPERV_DATA_ACCESS 11 /* Supervisor only data address */
34862674d4SLey Foon Tan #define EXC_X_PROTECTION_FAULT 13 /* TLB permission violation (x) */
35862674d4SLey Foon Tan #define EXC_R_PROTECTION_FAULT 14 /* TLB permission violation (r) */
36862674d4SLey Foon Tan #define EXC_W_PROTECTION_FAULT 15 /* TLB permission violation (w) */
37862674d4SLey Foon Tan
38862674d4SLey Foon Tan /*
39862674d4SLey Foon Tan * This routine handles page faults. It determines the address,
40862674d4SLey Foon Tan * and the problem, and then passes it off to one of the appropriate
41862674d4SLey Foon Tan * routines.
42862674d4SLey Foon Tan */
do_page_fault(struct pt_regs * regs,unsigned long cause,unsigned long address)43862674d4SLey Foon Tan asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,
44862674d4SLey Foon Tan unsigned long address)
45862674d4SLey Foon Tan {
46862674d4SLey Foon Tan struct vm_area_struct *vma = NULL;
47862674d4SLey Foon Tan struct task_struct *tsk = current;
48862674d4SLey Foon Tan struct mm_struct *mm = tsk->mm;
49862674d4SLey Foon Tan int code = SEGV_MAPERR;
5050a7ca3cSSouptick Joarder vm_fault_t fault;
51dde16072SPeter Xu unsigned int flags = FAULT_FLAG_DEFAULT;
52862674d4SLey Foon Tan
53862674d4SLey Foon Tan cause >>= 2;
54862674d4SLey Foon Tan
55862674d4SLey Foon Tan /* Restart the instruction */
56862674d4SLey Foon Tan regs->ea -= 4;
57862674d4SLey Foon Tan
58862674d4SLey Foon Tan /*
59862674d4SLey Foon Tan * We fault-in kernel-space virtual memory on-demand. The
60862674d4SLey Foon Tan * 'reference' page table is init_mm.pgd.
61862674d4SLey Foon Tan *
62862674d4SLey Foon Tan * NOTE! We MUST NOT take any locks for this case. We may
63862674d4SLey Foon Tan * be in an interrupt or a critical region, and should
64862674d4SLey Foon Tan * only copy the information from the master page table,
65862674d4SLey Foon Tan * nothing more.
66862674d4SLey Foon Tan */
67862674d4SLey Foon Tan if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) {
68862674d4SLey Foon Tan if (user_mode(regs))
69862674d4SLey Foon Tan goto bad_area_nosemaphore;
70862674d4SLey Foon Tan else
71862674d4SLey Foon Tan goto vmalloc_fault;
72862674d4SLey Foon Tan }
73862674d4SLey Foon Tan
74862674d4SLey Foon Tan if (unlikely(address >= TASK_SIZE))
75862674d4SLey Foon Tan goto bad_area_nosemaphore;
76862674d4SLey Foon Tan
77862674d4SLey Foon Tan /*
78862674d4SLey Foon Tan * If we're in an interrupt or have no user
79862674d4SLey Foon Tan * context, we must not take the fault..
80862674d4SLey Foon Tan */
8170ffdb93SDavid Hildenbrand if (faulthandler_disabled() || !mm)
82862674d4SLey Foon Tan goto bad_area_nosemaphore;
83862674d4SLey Foon Tan
84862674d4SLey Foon Tan if (user_mode(regs))
85862674d4SLey Foon Tan flags |= FAULT_FLAG_USER;
86862674d4SLey Foon Tan
874487dcf9SPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
884487dcf9SPeter Xu
8996f3a5ccSLey Foon Tan retry:
90*a050ba1eSLinus Torvalds vma = lock_mm_and_find_vma(mm, address, regs);
91862674d4SLey Foon Tan if (!vma)
92*a050ba1eSLinus Torvalds goto bad_area_nosemaphore;
93862674d4SLey Foon Tan /*
94862674d4SLey Foon Tan * Ok, we have a good vm_area for this memory access, so
95862674d4SLey Foon Tan * we can handle it..
96862674d4SLey Foon Tan */
97862674d4SLey Foon Tan code = SEGV_ACCERR;
98862674d4SLey Foon Tan
99862674d4SLey Foon Tan switch (cause) {
100862674d4SLey Foon Tan case EXC_SUPERV_INSN_ACCESS:
101862674d4SLey Foon Tan goto bad_area;
102862674d4SLey Foon Tan case EXC_SUPERV_DATA_ACCESS:
103862674d4SLey Foon Tan goto bad_area;
104862674d4SLey Foon Tan case EXC_X_PROTECTION_FAULT:
105862674d4SLey Foon Tan if (!(vma->vm_flags & VM_EXEC))
106862674d4SLey Foon Tan goto bad_area;
107862674d4SLey Foon Tan break;
108862674d4SLey Foon Tan case EXC_R_PROTECTION_FAULT:
109862674d4SLey Foon Tan if (!(vma->vm_flags & VM_READ))
110862674d4SLey Foon Tan goto bad_area;
111862674d4SLey Foon Tan break;
112862674d4SLey Foon Tan case EXC_W_PROTECTION_FAULT:
113862674d4SLey Foon Tan if (!(vma->vm_flags & VM_WRITE))
114862674d4SLey Foon Tan goto bad_area;
115862674d4SLey Foon Tan flags = FAULT_FLAG_WRITE;
116862674d4SLey Foon Tan break;
117862674d4SLey Foon Tan }
118862674d4SLey Foon Tan
119862674d4SLey Foon Tan /*
120862674d4SLey Foon Tan * If for any reason at all we couldn't handle the fault,
121862674d4SLey Foon Tan * make sure we exit gracefully rather than endlessly redo
122862674d4SLey Foon Tan * the fault.
123862674d4SLey Foon Tan */
1244487dcf9SPeter Xu fault = handle_mm_fault(vma, address, flags, regs);
12596f3a5ccSLey Foon Tan
126e902e508SAl Viro if (fault_signal_pending(fault, regs)) {
127e902e508SAl Viro if (!user_mode(regs))
128e902e508SAl Viro goto no_context;
12996f3a5ccSLey Foon Tan return;
130e902e508SAl Viro }
13196f3a5ccSLey Foon Tan
132d9272525SPeter Xu /* The fault is fully completed (including releasing mmap lock) */
133d9272525SPeter Xu if (fault & VM_FAULT_COMPLETED)
134d9272525SPeter Xu return;
135d9272525SPeter Xu
136862674d4SLey Foon Tan if (unlikely(fault & VM_FAULT_ERROR)) {
137862674d4SLey Foon Tan if (fault & VM_FAULT_OOM)
138862674d4SLey Foon Tan goto out_of_memory;
13933692f27SLinus Torvalds else if (fault & VM_FAULT_SIGSEGV)
14033692f27SLinus Torvalds goto bad_area;
141862674d4SLey Foon Tan else if (fault & VM_FAULT_SIGBUS)
142862674d4SLey Foon Tan goto do_sigbus;
143862674d4SLey Foon Tan BUG();
144862674d4SLey Foon Tan }
14596f3a5ccSLey Foon Tan
14696f3a5ccSLey Foon Tan if (fault & VM_FAULT_RETRY) {
14796f3a5ccSLey Foon Tan flags |= FAULT_FLAG_TRIED;
14896f3a5ccSLey Foon Tan
14996f3a5ccSLey Foon Tan /*
1503e4e28c5SMichel Lespinasse * No need to mmap_read_unlock(mm) as we would
15196f3a5ccSLey Foon Tan * have already released it in __lock_page_or_retry
15296f3a5ccSLey Foon Tan * in mm/filemap.c.
15396f3a5ccSLey Foon Tan */
15496f3a5ccSLey Foon Tan
15596f3a5ccSLey Foon Tan goto retry;
15696f3a5ccSLey Foon Tan }
157862674d4SLey Foon Tan
158d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
159862674d4SLey Foon Tan return;
160862674d4SLey Foon Tan
161862674d4SLey Foon Tan /*
162862674d4SLey Foon Tan * Something tried to access memory that isn't in our memory map..
163862674d4SLey Foon Tan * Fix it, but check if it's kernel or user first..
164862674d4SLey Foon Tan */
165862674d4SLey Foon Tan bad_area:
166d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
167862674d4SLey Foon Tan
168862674d4SLey Foon Tan bad_area_nosemaphore:
169862674d4SLey Foon Tan /* User mode accesses just cause a SIGSEGV */
170862674d4SLey Foon Tan if (user_mode(regs)) {
171a3248d60SChung-Ling Tang if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
172a3248d60SChung-Ling Tang pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
173862674d4SLey Foon Tan "cause %ld\n", current->comm, SIGSEGV, address, cause);
174862674d4SLey Foon Tan show_regs(regs);
175a3248d60SChung-Ling Tang }
176862674d4SLey Foon Tan _exception(SIGSEGV, regs, code, address);
177862674d4SLey Foon Tan return;
178862674d4SLey Foon Tan }
179862674d4SLey Foon Tan
180862674d4SLey Foon Tan no_context:
181862674d4SLey Foon Tan /* Are we prepared to handle this kernel fault? */
182862674d4SLey Foon Tan if (fixup_exception(regs))
183862674d4SLey Foon Tan return;
184862674d4SLey Foon Tan
185862674d4SLey Foon Tan /*
186862674d4SLey Foon Tan * Oops. The kernel tried to access some bad page. We'll have to
187862674d4SLey Foon Tan * terminate things with extreme prejudice.
188862674d4SLey Foon Tan */
189862674d4SLey Foon Tan bust_spinlocks(1);
190862674d4SLey Foon Tan
191862674d4SLey Foon Tan pr_alert("Unable to handle kernel %s at virtual address %08lx",
192862674d4SLey Foon Tan address < PAGE_SIZE ? "NULL pointer dereference" :
193862674d4SLey Foon Tan "paging request", address);
194862674d4SLey Foon Tan pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
195862674d4SLey Foon Tan cause);
196862674d4SLey Foon Tan panic("Oops");
197862674d4SLey Foon Tan return;
198862674d4SLey Foon Tan
199862674d4SLey Foon Tan /*
200862674d4SLey Foon Tan * We ran out of memory, or some other thing happened to us that made
201862674d4SLey Foon Tan * us unable to handle the page fault gracefully.
202862674d4SLey Foon Tan */
203862674d4SLey Foon Tan out_of_memory:
204d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
205862674d4SLey Foon Tan if (!user_mode(regs))
206862674d4SLey Foon Tan goto no_context;
207862674d4SLey Foon Tan pagefault_out_of_memory();
208862674d4SLey Foon Tan return;
209862674d4SLey Foon Tan
210862674d4SLey Foon Tan do_sigbus:
211d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
212862674d4SLey Foon Tan
213862674d4SLey Foon Tan /* Kernel mode? Handle exceptions or die */
214862674d4SLey Foon Tan if (!user_mode(regs))
215862674d4SLey Foon Tan goto no_context;
216862674d4SLey Foon Tan
217862674d4SLey Foon Tan _exception(SIGBUS, regs, BUS_ADRERR, address);
218862674d4SLey Foon Tan return;
219862674d4SLey Foon Tan
220862674d4SLey Foon Tan vmalloc_fault:
221862674d4SLey Foon Tan {
222862674d4SLey Foon Tan /*
223862674d4SLey Foon Tan * Synchronize this task's top level page-table
224862674d4SLey Foon Tan * with the 'reference' page table.
225862674d4SLey Foon Tan *
226862674d4SLey Foon Tan * Do _not_ use "tsk" here. We might be inside
227862674d4SLey Foon Tan * an interrupt in the middle of a task switch..
228862674d4SLey Foon Tan */
229862674d4SLey Foon Tan int offset = pgd_index(address);
230862674d4SLey Foon Tan pgd_t *pgd, *pgd_k;
2319f4e7037SMike Rapoport p4d_t *p4d, *p4d_k;
232862674d4SLey Foon Tan pud_t *pud, *pud_k;
233862674d4SLey Foon Tan pmd_t *pmd, *pmd_k;
234862674d4SLey Foon Tan pte_t *pte_k;
235862674d4SLey Foon Tan
236862674d4SLey Foon Tan pgd = pgd_current + offset;
237862674d4SLey Foon Tan pgd_k = init_mm.pgd + offset;
238862674d4SLey Foon Tan
239862674d4SLey Foon Tan if (!pgd_present(*pgd_k))
240862674d4SLey Foon Tan goto no_context;
241862674d4SLey Foon Tan set_pgd(pgd, *pgd_k);
242862674d4SLey Foon Tan
2439f4e7037SMike Rapoport p4d = p4d_offset(pgd, address);
2449f4e7037SMike Rapoport p4d_k = p4d_offset(pgd_k, address);
2459f4e7037SMike Rapoport if (!p4d_present(*p4d_k))
2469f4e7037SMike Rapoport goto no_context;
2479f4e7037SMike Rapoport pud = pud_offset(p4d, address);
2489f4e7037SMike Rapoport pud_k = pud_offset(p4d_k, address);
249862674d4SLey Foon Tan if (!pud_present(*pud_k))
250862674d4SLey Foon Tan goto no_context;
251862674d4SLey Foon Tan pmd = pmd_offset(pud, address);
252862674d4SLey Foon Tan pmd_k = pmd_offset(pud_k, address);
253862674d4SLey Foon Tan if (!pmd_present(*pmd_k))
254862674d4SLey Foon Tan goto no_context;
255862674d4SLey Foon Tan set_pmd(pmd, *pmd_k);
256862674d4SLey Foon Tan
257862674d4SLey Foon Tan pte_k = pte_offset_kernel(pmd_k, address);
258862674d4SLey Foon Tan if (!pte_present(*pte_k))
259862674d4SLey Foon Tan goto no_context;
260862674d4SLey Foon Tan
261195568a1SNicholas Piggin flush_tlb_kernel_page(address);
262862674d4SLey Foon Tan return;
263862674d4SLey Foon Tan }
264862674d4SLey Foon Tan }
265