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