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