Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OpenRISC fault.c
4 *
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
7 * declaration.
8 *
9 * Modifications for the OpenRISC architecture:
10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12 */
13
14#include <linux/mm.h>
15#include <linux/interrupt.h>
16#include <linux/extable.h>
17#include <linux/sched/signal.h>
18#include <linux/perf_event.h>
19
20#include <linux/uaccess.h>
21#include <asm/mmu_context.h>
22#include <asm/siginfo.h>
23#include <asm/signal.h>
24
25#define NUM_TLB_ENTRIES 64
26#define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
27
28/* __PHX__ :: - check the vmalloc_fault in do_page_fault()
29 * - also look into include/asm/mmu_context.h
30 */
31volatile pgd_t *current_pgd[NR_CPUS];
32
33extern void __noreturn die(char *, struct pt_regs *, long);
34
35/*
36 * This routine handles page faults. It determines the address,
37 * and the problem, and then passes it off to one of the appropriate
38 * routines.
39 *
40 * If this routine detects a bad access, it returns 1, otherwise it
41 * returns 0.
42 */
43
44asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
45 unsigned long vector, int write_acc)
46{
47 struct task_struct *tsk;
48 struct mm_struct *mm;
49 struct vm_area_struct *vma;
50 int si_code;
51 vm_fault_t fault;
52 unsigned int flags = FAULT_FLAG_DEFAULT;
53
54 tsk = current;
55
56 /*
57 * We fault-in kernel-space virtual memory on-demand. The
58 * 'reference' page table is init_mm.pgd.
59 *
60 * NOTE! We MUST NOT take any locks for this case. We may
61 * be in an interrupt or a critical region, and should
62 * only copy the information from the master page table,
63 * nothing more.
64 *
65 * NOTE2: This is done so that, when updating the vmalloc
66 * mappings we don't have to walk all processes pgdirs and
67 * add the high mappings all at once. Instead we do it as they
68 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
69 * bit set so sometimes the TLB can use a lingering entry.
70 *
71 * This verifies that the fault happens in kernel space
72 * and that the fault was not a protection error.
73 */
74
75 if (address >= VMALLOC_START &&
76 (vector != 0x300 && vector != 0x400) &&
77 !user_mode(regs))
78 goto vmalloc_fault;
79
80 /* If exceptions were enabled, we can reenable them here */
81 if (user_mode(regs)) {
82 /* Exception was in userspace: reenable interrupts */
83 local_irq_enable();
84 flags |= FAULT_FLAG_USER;
85 } else {
86 /* If exception was in a syscall, then IRQ's may have
87 * been enabled or disabled. If they were enabled,
88 * reenable them.
89 */
90 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
91 local_irq_enable();
92 }
93
94 mm = tsk->mm;
95 si_code = SEGV_MAPERR;
96
97 /*
98 * If we're in an interrupt or have no user
99 * context, we must not take the fault..
100 */
101
102 if (in_interrupt() || !mm)
103 goto no_context;
104
105 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
106
107retry:
108 mmap_read_lock(mm);
109 vma = find_vma(mm, address);
110
111 if (!vma)
112 goto bad_area;
113
114 if (vma->vm_start <= address)
115 goto good_area;
116
117 if (!(vma->vm_flags & VM_GROWSDOWN))
118 goto bad_area;
119
120 if (user_mode(regs)) {
121 /*
122 * accessing the stack below usp is always a bug.
123 * we get page-aligned addresses so we can only check
124 * if we're within a page from usp, but that might be
125 * enough to catch brutal errors at least.
126 */
127 if (address + PAGE_SIZE < regs->sp)
128 goto bad_area;
129 }
130 if (expand_stack(vma, address))
131 goto bad_area;
132
133 /*
134 * Ok, we have a good vm_area for this memory access, so
135 * we can handle it..
136 */
137
138good_area:
139 si_code = SEGV_ACCERR;
140
141 /* first do some preliminary protection checks */
142
143 if (write_acc) {
144 if (!(vma->vm_flags & VM_WRITE))
145 goto bad_area;
146 flags |= FAULT_FLAG_WRITE;
147 } else {
148 /* not present */
149 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
150 goto bad_area;
151 }
152
153 /* are we trying to execute nonexecutable area */
154 if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
155 goto bad_area;
156
157 /*
158 * If for any reason at all we couldn't handle the fault,
159 * make sure we exit gracefully rather than endlessly redo
160 * the fault.
161 */
162
163 fault = handle_mm_fault(vma, address, flags, regs);
164
165 if (fault_signal_pending(fault, regs))
166 return;
167
168 /* The fault is fully completed (including releasing mmap lock) */
169 if (fault & VM_FAULT_COMPLETED)
170 return;
171
172 if (unlikely(fault & VM_FAULT_ERROR)) {
173 if (fault & VM_FAULT_OOM)
174 goto out_of_memory;
175 else if (fault & VM_FAULT_SIGSEGV)
176 goto bad_area;
177 else if (fault & VM_FAULT_SIGBUS)
178 goto do_sigbus;
179 BUG();
180 }
181
182 /*RGD modeled on Cris */
183 if (fault & VM_FAULT_RETRY) {
184 flags |= FAULT_FLAG_TRIED;
185
186 /* No need to mmap_read_unlock(mm) as we would
187 * have already released it in __lock_page_or_retry
188 * in mm/filemap.c.
189 */
190
191 goto retry;
192 }
193
194 mmap_read_unlock(mm);
195 return;
196
197 /*
198 * Something tried to access memory that isn't in our memory map..
199 * Fix it, but check if it's kernel or user first..
200 */
201
202bad_area:
203 mmap_read_unlock(mm);
204
205bad_area_nosemaphore:
206
207 /* User mode accesses just cause a SIGSEGV */
208
209 if (user_mode(regs)) {
210 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
211 return;
212 }
213
214no_context:
215
216 /* Are we prepared to handle this kernel fault?
217 *
218 * (The kernel has valid exception-points in the source
219 * when it acesses user-memory. When it fails in one
220 * of those points, we find it in a table and do a jump
221 * to some fixup code that loads an appropriate error
222 * code)
223 */
224
225 {
226 const struct exception_table_entry *entry;
227
228 if ((entry = search_exception_tables(regs->pc)) != NULL) {
229 /* Adjust the instruction pointer in the stackframe */
230 regs->pc = entry->fixup;
231 return;
232 }
233 }
234
235 /*
236 * Oops. The kernel tried to access some bad page. We'll have to
237 * terminate things with extreme prejudice.
238 */
239
240 if ((unsigned long)(address) < PAGE_SIZE)
241 printk(KERN_ALERT
242 "Unable to handle kernel NULL pointer dereference");
243 else
244 printk(KERN_ALERT "Unable to handle kernel access");
245 printk(" at virtual address 0x%08lx\n", address);
246
247 die("Oops", regs, write_acc);
248
249 /*
250 * We ran out of memory, or some other thing happened to us that made
251 * us unable to handle the page fault gracefully.
252 */
253
254out_of_memory:
255 mmap_read_unlock(mm);
256 if (!user_mode(regs))
257 goto no_context;
258 pagefault_out_of_memory();
259 return;
260
261do_sigbus:
262 mmap_read_unlock(mm);
263
264 /*
265 * Send a sigbus, regardless of whether we were in kernel
266 * or user mode.
267 */
268 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
269
270 /* Kernel mode? Handle exceptions or die */
271 if (!user_mode(regs))
272 goto no_context;
273 return;
274
275vmalloc_fault:
276 {
277 /*
278 * Synchronize this task's top level page-table
279 * with the 'reference' page table.
280 *
281 * Use current_pgd instead of tsk->active_mm->pgd
282 * since the latter might be unavailable if this
283 * code is executed in a misfortunately run irq
284 * (like inside schedule() between switch_mm and
285 * switch_to...).
286 */
287
288 int offset = pgd_index(address);
289 pgd_t *pgd, *pgd_k;
290 p4d_t *p4d, *p4d_k;
291 pud_t *pud, *pud_k;
292 pmd_t *pmd, *pmd_k;
293 pte_t *pte_k;
294
295/*
296 phx_warn("do_page_fault(): vmalloc_fault will not work, "
297 "since current_pgd assign a proper value somewhere\n"
298 "anyhow we don't need this at the moment\n");
299
300 phx_mmu("vmalloc_fault");
301*/
302 pgd = (pgd_t *)current_pgd[smp_processor_id()] + offset;
303 pgd_k = init_mm.pgd + offset;
304
305 /* Since we're two-level, we don't need to do both
306 * set_pgd and set_pmd (they do the same thing). If
307 * we go three-level at some point, do the right thing
308 * with pgd_present and set_pgd here.
309 *
310 * Also, since the vmalloc area is global, we don't
311 * need to copy individual PTE's, it is enough to
312 * copy the pgd pointer into the pte page of the
313 * root task. If that is there, we'll find our pte if
314 * it exists.
315 */
316
317 p4d = p4d_offset(pgd, address);
318 p4d_k = p4d_offset(pgd_k, address);
319 if (!p4d_present(*p4d_k))
320 goto no_context;
321
322 pud = pud_offset(p4d, address);
323 pud_k = pud_offset(p4d_k, address);
324 if (!pud_present(*pud_k))
325 goto no_context;
326
327 pmd = pmd_offset(pud, address);
328 pmd_k = pmd_offset(pud_k, address);
329
330 if (!pmd_present(*pmd_k))
331 goto bad_area_nosemaphore;
332
333 set_pmd(pmd, *pmd_k);
334
335 /* Make sure the actual PTE exists as well to
336 * catch kernel vmalloc-area accesses to non-mapped
337 * addresses. If we don't do this, this will just
338 * silently loop forever.
339 */
340
341 pte_k = pte_offset_kernel(pmd_k, address);
342 if (!pte_present(*pte_k))
343 goto no_context;
344
345 return;
346 }
347}
1/*
2 * OpenRISC fault.c
3 *
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
7 *
8 * Modifications for the OpenRISC architecture:
9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
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
18#include <linux/mm.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/sched.h>
22
23#include <asm/uaccess.h>
24#include <asm/siginfo.h>
25#include <asm/signal.h>
26
27#define NUM_TLB_ENTRIES 64
28#define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
29
30unsigned long pte_misses; /* updated by do_page_fault() */
31unsigned long pte_errors; /* updated by do_page_fault() */
32
33/* __PHX__ :: - check the vmalloc_fault in do_page_fault()
34 * - also look into include/asm-or32/mmu_context.h
35 */
36volatile pgd_t *current_pgd;
37
38extern void die(char *, struct pt_regs *, long);
39
40/*
41 * This routine handles page faults. It determines the address,
42 * and the problem, and then passes it off to one of the appropriate
43 * routines.
44 *
45 * If this routine detects a bad access, it returns 1, otherwise it
46 * returns 0.
47 */
48
49asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
50 unsigned long vector, int write_acc)
51{
52 struct task_struct *tsk;
53 struct mm_struct *mm;
54 struct vm_area_struct *vma;
55 siginfo_t info;
56 int fault;
57 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
58
59 tsk = current;
60
61 /*
62 * We fault-in kernel-space virtual memory on-demand. The
63 * 'reference' page table is init_mm.pgd.
64 *
65 * NOTE! We MUST NOT take any locks for this case. We may
66 * be in an interrupt or a critical region, and should
67 * only copy the information from the master page table,
68 * nothing more.
69 *
70 * NOTE2: This is done so that, when updating the vmalloc
71 * mappings we don't have to walk all processes pgdirs and
72 * add the high mappings all at once. Instead we do it as they
73 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
74 * bit set so sometimes the TLB can use a lingering entry.
75 *
76 * This verifies that the fault happens in kernel space
77 * and that the fault was not a protection error.
78 */
79
80 if (address >= VMALLOC_START &&
81 (vector != 0x300 && vector != 0x400) &&
82 !user_mode(regs))
83 goto vmalloc_fault;
84
85 /* If exceptions were enabled, we can reenable them here */
86 if (user_mode(regs)) {
87 /* Exception was in userspace: reenable interrupts */
88 local_irq_enable();
89 } else {
90 /* If exception was in a syscall, then IRQ's may have
91 * been enabled or disabled. If they were enabled,
92 * reenable them.
93 */
94 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
95 local_irq_enable();
96 }
97
98 mm = tsk->mm;
99 info.si_code = SEGV_MAPERR;
100
101 /*
102 * If we're in an interrupt or have no user
103 * context, we must not take the fault..
104 */
105
106 if (in_interrupt() || !mm)
107 goto no_context;
108
109retry:
110 down_read(&mm->mmap_sem);
111 vma = find_vma(mm, address);
112
113 if (!vma)
114 goto bad_area;
115
116 if (vma->vm_start <= address)
117 goto good_area;
118
119 if (!(vma->vm_flags & VM_GROWSDOWN))
120 goto bad_area;
121
122 if (user_mode(regs)) {
123 /*
124 * accessing the stack below usp is always a bug.
125 * we get page-aligned addresses so we can only check
126 * if we're within a page from usp, but that might be
127 * enough to catch brutal errors at least.
128 */
129 if (address + PAGE_SIZE < regs->sp)
130 goto bad_area;
131 }
132 if (expand_stack(vma, address))
133 goto bad_area;
134
135 /*
136 * Ok, we have a good vm_area for this memory access, so
137 * we can handle it..
138 */
139
140good_area:
141 info.si_code = SEGV_ACCERR;
142
143 /* first do some preliminary protection checks */
144
145 if (write_acc) {
146 if (!(vma->vm_flags & VM_WRITE))
147 goto bad_area;
148 flags |= FAULT_FLAG_WRITE;
149 } else {
150 /* not present */
151 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
152 goto bad_area;
153 }
154
155 /* are we trying to execute nonexecutable area */
156 if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
157 goto bad_area;
158
159 /*
160 * If for any reason at all we couldn't handle the fault,
161 * make sure we exit gracefully rather than endlessly redo
162 * the fault.
163 */
164
165 fault = handle_mm_fault(mm, vma, address, flags);
166
167 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
168 return;
169
170 if (unlikely(fault & VM_FAULT_ERROR)) {
171 if (fault & VM_FAULT_OOM)
172 goto out_of_memory;
173 else if (fault & VM_FAULT_SIGBUS)
174 goto do_sigbus;
175 BUG();
176 }
177
178 if (flags & FAULT_FLAG_ALLOW_RETRY) {
179 /*RGD modeled on Cris */
180 if (fault & VM_FAULT_MAJOR)
181 tsk->maj_flt++;
182 else
183 tsk->min_flt++;
184 if (fault & VM_FAULT_RETRY) {
185 flags &= ~FAULT_FLAG_ALLOW_RETRY;
186
187 /* No need to up_read(&mm->mmap_sem) as we would
188 * have already released it in __lock_page_or_retry
189 * in mm/filemap.c.
190 */
191
192 goto retry;
193 }
194 }
195
196 up_read(&mm->mmap_sem);
197 return;
198
199 /*
200 * Something tried to access memory that isn't in our memory map..
201 * Fix it, but check if it's kernel or user first..
202 */
203
204bad_area:
205 up_read(&mm->mmap_sem);
206
207bad_area_nosemaphore:
208
209 /* User mode accesses just cause a SIGSEGV */
210
211 if (user_mode(regs)) {
212 info.si_signo = SIGSEGV;
213 info.si_errno = 0;
214 /* info.si_code has been set above */
215 info.si_addr = (void *)address;
216 force_sig_info(SIGSEGV, &info, tsk);
217 return;
218 }
219
220no_context:
221
222 /* Are we prepared to handle this kernel fault?
223 *
224 * (The kernel has valid exception-points in the source
225 * when it acesses user-memory. When it fails in one
226 * of those points, we find it in a table and do a jump
227 * to some fixup code that loads an appropriate error
228 * code)
229 */
230
231 {
232 const struct exception_table_entry *entry;
233
234 __asm__ __volatile__("l.nop 42");
235
236 if ((entry = search_exception_tables(regs->pc)) != NULL) {
237 /* Adjust the instruction pointer in the stackframe */
238 regs->pc = entry->fixup;
239 return;
240 }
241 }
242
243 /*
244 * Oops. The kernel tried to access some bad page. We'll have to
245 * terminate things with extreme prejudice.
246 */
247
248 if ((unsigned long)(address) < PAGE_SIZE)
249 printk(KERN_ALERT
250 "Unable to handle kernel NULL pointer dereference");
251 else
252 printk(KERN_ALERT "Unable to handle kernel access");
253 printk(" at virtual address 0x%08lx\n", address);
254
255 die("Oops", regs, write_acc);
256
257 do_exit(SIGKILL);
258
259 /*
260 * We ran out of memory, or some other thing happened to us that made
261 * us unable to handle the page fault gracefully.
262 */
263
264out_of_memory:
265 __asm__ __volatile__("l.nop 42");
266 __asm__ __volatile__("l.nop 1");
267
268 up_read(&mm->mmap_sem);
269 printk("VM: killing process %s\n", tsk->comm);
270 if (user_mode(regs))
271 do_exit(SIGKILL);
272 goto no_context;
273
274do_sigbus:
275 up_read(&mm->mmap_sem);
276
277 /*
278 * Send a sigbus, regardless of whether we were in kernel
279 * or user mode.
280 */
281 info.si_signo = SIGBUS;
282 info.si_errno = 0;
283 info.si_code = BUS_ADRERR;
284 info.si_addr = (void *)address;
285 force_sig_info(SIGBUS, &info, tsk);
286
287 /* Kernel mode? Handle exceptions or die */
288 if (!user_mode(regs))
289 goto no_context;
290 return;
291
292vmalloc_fault:
293 {
294 /*
295 * Synchronize this task's top level page-table
296 * with the 'reference' page table.
297 *
298 * Use current_pgd instead of tsk->active_mm->pgd
299 * since the latter might be unavailable if this
300 * code is executed in a misfortunately run irq
301 * (like inside schedule() between switch_mm and
302 * switch_to...).
303 */
304
305 int offset = pgd_index(address);
306 pgd_t *pgd, *pgd_k;
307 pud_t *pud, *pud_k;
308 pmd_t *pmd, *pmd_k;
309 pte_t *pte_k;
310
311/*
312 phx_warn("do_page_fault(): vmalloc_fault will not work, "
313 "since current_pgd assign a proper value somewhere\n"
314 "anyhow we don't need this at the moment\n");
315
316 phx_mmu("vmalloc_fault");
317*/
318 pgd = (pgd_t *)current_pgd + offset;
319 pgd_k = init_mm.pgd + offset;
320
321 /* Since we're two-level, we don't need to do both
322 * set_pgd and set_pmd (they do the same thing). If
323 * we go three-level at some point, do the right thing
324 * with pgd_present and set_pgd here.
325 *
326 * Also, since the vmalloc area is global, we don't
327 * need to copy individual PTE's, it is enough to
328 * copy the pgd pointer into the pte page of the
329 * root task. If that is there, we'll find our pte if
330 * it exists.
331 */
332
333 pud = pud_offset(pgd, address);
334 pud_k = pud_offset(pgd_k, address);
335 if (!pud_present(*pud_k))
336 goto no_context;
337
338 pmd = pmd_offset(pud, address);
339 pmd_k = pmd_offset(pud_k, address);
340
341 if (!pmd_present(*pmd_k))
342 goto bad_area_nosemaphore;
343
344 set_pmd(pmd, *pmd_k);
345
346 /* Make sure the actual PTE exists as well to
347 * catch kernel vmalloc-area accesses to non-mapped
348 * addresses. If we don't do this, this will just
349 * silently loop forever.
350 */
351
352 pte_k = pte_offset_kernel(pmd_k, address);
353 if (!pte_present(*pte_k))
354 goto no_context;
355
356 return;
357 }
358}