blob: aea33b599037c24162ec9d5f688afa26b479b421 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/alpha/mm/fault.c
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 */
7
Ingo Molnar3f07c012017-02-08 18:51:30 +01008#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/mm.h>
11#include <asm/io.h>
12
13#define __EXTERN_INLINE inline
14#include <asm/mmu_context.h>
15#include <asm/tlbflush.h>
16#undef __EXTERN_INLINE
17
18#include <linux/signal.h>
19#include <linux/errno.h>
20#include <linux/string.h>
21#include <linux/types.h>
22#include <linux/ptrace.h>
23#include <linux/mman.h>
24#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
Paul Gortmaker9c14f832016-07-23 14:01:45 -040026#include <linux/extable.h>
David Hildenbrand70ffdb92015-05-11 17:52:11 +020027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
30
31
32/*
33 * Force a new ASN for a task.
34 */
35
36#ifndef CONFIG_SMP
37unsigned long last_asn = ASN_FIRST_VERSION;
38#endif
39
40void
41__load_new_mm_context(struct mm_struct *next_mm)
42{
43 unsigned long mmc;
44 struct pcb_struct *pcb;
45
46 mmc = __get_new_mm_context(next_mm, smp_processor_id());
47 next_mm->context[smp_processor_id()] = mmc;
48
49 pcb = &current_thread_info()->pcb;
50 pcb->asn = mmc & HARDWARE_ASN_MASK;
51 pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
52
53 __reload_thread(pcb);
54}
55
56
57/*
58 * This routine handles page faults. It determines the address,
59 * and the problem, and then passes it off to handle_mm_fault().
60 *
61 * mmcsr:
62 * 0 = translation not valid
63 * 1 = access violation
64 * 2 = fault-on-read
65 * 3 = fault-on-execute
66 * 4 = fault-on-write
67 *
68 * cause:
69 * -1 = instruction fetch
70 * 0 = load
71 * 1 = store
72 *
73 * Registers $9 through $15 are saved in a block just prior to `regs' and
74 * are saved and restored around the call to allow exception code to
75 * modify them.
76 */
77
78/* Macro for exception fixup code to access integer registers. */
79#define dpf_reg(r) \
80 (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 : \
Sergei Trofimovich491af60f2018-12-31 11:53:55 +000081 (r) <= 18 ? (r)+10 : (r)-10])
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83asmlinkage void
84do_page_fault(unsigned long address, unsigned long mmcsr,
85 long cause, struct pt_regs *regs)
86{
87 struct vm_area_struct * vma;
88 struct mm_struct *mm = current->mm;
89 const struct exception_table_entry *fixup;
Souptick Joarder50a7ca32018-08-17 15:44:47 -070090 int si_code = SEGV_MAPERR;
91 vm_fault_t fault;
Johannes Weiner759496b2013-09-12 15:13:39 -070092 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* As of EV6, a load into $31/$f31 is a prefetch, and never faults
95 (or is suppressed by the PALcode). Support that for older CPUs
96 by ignoring such an instruction. */
97 if (cause == 0) {
98 unsigned int insn;
99 __get_user(insn, (unsigned int __user *)regs->pc);
100 if ((insn >> 21 & 0x1f) == 0x1f &&
101 /* ldq ldl ldt lds ldg ldf ldwu ldbu */
102 (1ul << (insn >> 26) & 0x30f00001400ul)) {
103 regs->pc += 4;
104 return;
105 }
106 }
107
108 /* If we're in an interrupt context, or have no user context,
109 we must not take the fault. */
David Hildenbrand70ffdb92015-05-11 17:52:11 +0200110 if (!mm || faulthandler_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 goto no_context;
112
113#ifdef CONFIG_ALPHA_LARGE_VMALLOC
114 if (address >= TASK_SIZE)
115 goto vmalloc_fault;
116#endif
Johannes Weiner759496b2013-09-12 15:13:39 -0700117 if (user_mode(regs))
118 flags |= FAULT_FLAG_USER;
Kautuk Consul0bcc4262012-08-19 14:41:01 +1200119retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 down_read(&mm->mmap_sem);
121 vma = find_vma(mm, address);
122 if (!vma)
123 goto bad_area;
124 if (vma->vm_start <= address)
125 goto good_area;
126 if (!(vma->vm_flags & VM_GROWSDOWN))
127 goto bad_area;
128 if (expand_stack(vma, address))
129 goto bad_area;
130
131 /* Ok, we have a good vm_area for this memory access, so
132 we can handle it. */
133 good_area:
134 si_code = SEGV_ACCERR;
135 if (cause < 0) {
136 if (!(vma->vm_flags & VM_EXEC))
137 goto bad_area;
138 } else if (!cause) {
139 /* Allow reads even for write-only mappings */
140 if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
141 goto bad_area;
142 } else {
143 if (!(vma->vm_flags & VM_WRITE))
144 goto bad_area;
Johannes Weiner759496b2013-09-12 15:13:39 -0700145 flags |= FAULT_FLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* If for any reason at all we couldn't handle the fault,
149 make sure we exit gracefully rather than endlessly redo
150 the fault. */
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700151 fault = handle_mm_fault(vma, address, flags);
Kautuk Consul0bcc4262012-08-19 14:41:01 +1200152
Peter Xu4ef87322020-04-01 21:08:06 -0700153 if (fault_signal_pending(fault, regs))
Kautuk Consul0bcc4262012-08-19 14:41:01 +1200154 return;
155
Nick Piggin83c54072007-07-19 01:47:05 -0700156 if (unlikely(fault & VM_FAULT_ERROR)) {
157 if (fault & VM_FAULT_OOM)
158 goto out_of_memory;
Linus Torvalds33692f22015-01-29 10:51:32 -0800159 else if (fault & VM_FAULT_SIGSEGV)
160 goto bad_area;
Nick Piggin83c54072007-07-19 01:47:05 -0700161 else if (fault & VM_FAULT_SIGBUS)
162 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 BUG();
164 }
Kautuk Consul0bcc4262012-08-19 14:41:01 +1200165
166 if (flags & FAULT_FLAG_ALLOW_RETRY) {
167 if (fault & VM_FAULT_MAJOR)
168 current->maj_flt++;
169 else
170 current->min_flt++;
171 if (fault & VM_FAULT_RETRY) {
172 flags &= ~FAULT_FLAG_ALLOW_RETRY;
173
174 /* No need to up_read(&mm->mmap_sem) as we would
175 * have already released it in __lock_page_or_retry
176 * in mm/filemap.c.
177 */
178
179 goto retry;
180 }
181 }
182
183 up_read(&mm->mmap_sem);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return;
186
187 /* Something tried to access memory that isn't in our memory map.
188 Fix it, but check if it's kernel or user first. */
189 bad_area:
190 up_read(&mm->mmap_sem);
191
192 if (user_mode(regs))
193 goto do_sigsegv;
194
195 no_context:
196 /* Are we prepared to handle this fault as an exception? */
197 if ((fixup = search_exception_tables(regs->pc)) != 0) {
198 unsigned long newpc;
199 newpc = fixup_exception(dpf_reg, fixup, regs->pc);
200 regs->pc = newpc;
201 return;
202 }
203
204 /* Oops. The kernel tried to access some bad page. We'll have to
205 terminate things with extreme prejudice. */
206 printk(KERN_ALERT "Unable to handle kernel paging request at "
207 "virtual address %016lx\n", address);
208 die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
209 do_exit(SIGKILL);
210
211 /* We ran out of memory, or some other thing happened to us that
212 made us unable to handle the page fault gracefully. */
213 out_of_memory:
Kautuk Consul0bcc4262012-08-19 14:41:01 +1200214 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!user_mode(regs))
216 goto no_context;
Nick Piggin1cb3d8e2010-04-29 17:48:18 -0400217 pagefault_out_of_memory();
218 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 do_sigbus:
Kautuk Consul0bcc4262012-08-19 14:41:01 +1200221 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* Send a sigbus, regardless of whether we were in kernel
223 or user mode. */
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500224 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!user_mode(regs))
226 goto no_context;
227 return;
228
229 do_sigsegv:
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500230 force_sig_fault(SIGSEGV, si_code, (void __user *) address, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return;
232
233#ifdef CONFIG_ALPHA_LARGE_VMALLOC
234 vmalloc_fault:
235 if (user_mode(regs))
236 goto do_sigsegv;
237 else {
238 /* Synchronize this task's top level page-table
239 with the "reference" page table from init. */
240 long index = pgd_index(address);
241 pgd_t *pgd, *pgd_k;
242
243 pgd = current->active_mm->pgd + index;
244 pgd_k = swapper_pg_dir + index;
245 if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
246 pgd_val(*pgd) = pgd_val(*pgd_k);
247 return;
248 }
249 goto no_context;
250 }
251#endif
252}