xref: /linux/arch/mips/mm/fault.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995 - 2000 by Ralf Baechle
7  */
8 #include <linux/context_tracking.h>
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/ptrace.h>
17 #include <linux/ratelimit.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/kprobes.h>
22 #include <linux/perf_event.h>
23 #include <linux/uaccess.h>
24 
25 #include <asm/branch.h>
26 #include <asm/mmu_context.h>
27 #include <asm/ptrace.h>
28 #include <asm/highmem.h>		/* For VMALLOC_END */
29 #include <linux/kdebug.h>
30 
31 int show_unhandled_signals = 1;
32 
33 /*
34  * This routine handles page faults.  It determines the address,
35  * and the problem, and then passes it off to one of the appropriate
36  * routines.
37  */
38 static void __do_page_fault(struct pt_regs *regs, unsigned long write,
39 	unsigned long address)
40 {
41 	struct vm_area_struct * vma = NULL;
42 	struct task_struct *tsk = current;
43 	struct mm_struct *mm = tsk->mm;
44 	const int field = sizeof(unsigned long) * 2;
45 	int si_code;
46 	vm_fault_t fault;
47 	unsigned int flags = FAULT_FLAG_DEFAULT;
48 
49 	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
50 
51 #if 0
52 	printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
53 	       current->comm, current->pid, field, address, write,
54 	       field, regs->cp0_epc);
55 #endif
56 
57 #ifdef CONFIG_KPROBES
58 	/*
59 	 * This is to notify the fault handler of the kprobes.
60 	 */
61 	if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
62 		       current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
63 		return;
64 #endif
65 
66 	si_code = SEGV_MAPERR;
67 
68 	/*
69 	 * We fault-in kernel-space virtual memory on-demand. The
70 	 * 'reference' page table is init_mm.pgd.
71 	 *
72 	 * NOTE! We MUST NOT take any locks for this case. We may
73 	 * be in an interrupt or a critical region, and should
74 	 * only copy the information from the master page table,
75 	 * nothing more.
76 	 */
77 #ifdef CONFIG_64BIT
78 # define VMALLOC_FAULT_TARGET no_context
79 #else
80 # define VMALLOC_FAULT_TARGET vmalloc_fault
81 #endif
82 
83 	if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
84 		goto VMALLOC_FAULT_TARGET;
85 #ifdef MODULE_START
86 	if (unlikely(address >= MODULE_START && address < MODULE_END))
87 		goto VMALLOC_FAULT_TARGET;
88 #endif
89 
90 	/*
91 	 * If we're in an interrupt or have no user
92 	 * context, we must not take the fault..
93 	 */
94 	if (faulthandler_disabled() || !mm)
95 		goto bad_area_nosemaphore;
96 
97 	if (user_mode(regs))
98 		flags |= FAULT_FLAG_USER;
99 
100 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
101 retry:
102 	vma = lock_mm_and_find_vma(mm, address, regs);
103 	if (!vma)
104 		goto bad_area_nosemaphore;
105 /*
106  * Ok, we have a good vm_area for this memory access, so
107  * we can handle it..
108  */
109 	si_code = SEGV_ACCERR;
110 
111 	if (write) {
112 		if (!(vma->vm_flags & VM_WRITE))
113 			goto bad_area;
114 		flags |= FAULT_FLAG_WRITE;
115 	} else {
116 		if (cpu_has_rixi) {
117 			if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
118 #if 0
119 				pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
120 					  raw_smp_processor_id(),
121 					  current->comm, current->pid,
122 					  field, address, write,
123 					  field, regs->cp0_epc);
124 #endif
125 				goto bad_area;
126 			}
127 			if (!(vma->vm_flags & VM_READ) &&
128 			    exception_epc(regs) != address) {
129 #if 0
130 				pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
131 					  raw_smp_processor_id(),
132 					  current->comm, current->pid,
133 					  field, address, write,
134 					  field, regs->cp0_epc);
135 #endif
136 				goto bad_area;
137 			}
138 		} else {
139 			if (unlikely(!vma_is_accessible(vma)))
140 				goto bad_area;
141 		}
142 	}
143 
144 	/*
145 	 * If for any reason at all we couldn't handle the fault,
146 	 * make sure we exit gracefully rather than endlessly redo
147 	 * the fault.
148 	 */
149 	fault = handle_mm_fault(vma, address, flags, regs);
150 
151 	if (fault_signal_pending(fault, regs)) {
152 		if (!user_mode(regs))
153 			goto no_context;
154 		return;
155 	}
156 
157 	/* The fault is fully completed (including releasing mmap lock) */
158 	if (fault & VM_FAULT_COMPLETED)
159 		return;
160 
161 	if (unlikely(fault & VM_FAULT_ERROR)) {
162 		if (fault & VM_FAULT_OOM)
163 			goto out_of_memory;
164 		else if (fault & VM_FAULT_SIGSEGV)
165 			goto bad_area;
166 		else if (fault & VM_FAULT_SIGBUS)
167 			goto do_sigbus;
168 		BUG();
169 	}
170 
171 	if (fault & VM_FAULT_RETRY) {
172 		flags |= FAULT_FLAG_TRIED;
173 
174 		/*
175 		 * No need to mmap_read_unlock(mm) as we would
176 		 * have already released it in __lock_page_or_retry
177 		 * in mm/filemap.c.
178 		 */
179 
180 		goto retry;
181 	}
182 
183 	mmap_read_unlock(mm);
184 	return;
185 
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  */
190 bad_area:
191 	mmap_read_unlock(mm);
192 
193 bad_area_nosemaphore:
194 	/* User mode accesses just cause a SIGSEGV */
195 	if (user_mode(regs)) {
196 		tsk->thread.cp0_badvaddr = address;
197 		tsk->thread.error_code = write;
198 		if (show_unhandled_signals &&
199 		    unhandled_signal(tsk, SIGSEGV) &&
200 		    __ratelimit(&ratelimit_state)) {
201 			pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
202 				tsk->comm,
203 				write ? "write access to" : "read access from",
204 				field, address);
205 			pr_info("epc = %0*lx in", field,
206 				(unsigned long) regs->cp0_epc);
207 			print_vma_addr(KERN_CONT " ", regs->cp0_epc);
208 			pr_cont("\n");
209 			pr_info("ra  = %0*lx in", field,
210 				(unsigned long) regs->regs[31]);
211 			print_vma_addr(KERN_CONT " ", regs->regs[31]);
212 			pr_cont("\n");
213 		}
214 		current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
215 		force_sig_fault(SIGSEGV, si_code, (void __user *)address);
216 		return;
217 	}
218 
219 no_context:
220 	/* Are we prepared to handle this kernel fault?	 */
221 	if (fixup_exception(regs)) {
222 		current->thread.cp0_baduaddr = address;
223 		return;
224 	}
225 
226 	/*
227 	 * Oops. The kernel tried to access some bad page. We'll have to
228 	 * terminate things with extreme prejudice.
229 	 */
230 	bust_spinlocks(1);
231 
232 	printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
233 	       "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
234 	       raw_smp_processor_id(), field, address, field, regs->cp0_epc,
235 	       field,  regs->regs[31]);
236 	die("Oops", regs);
237 
238 out_of_memory:
239 	/*
240 	 * We ran out of memory, call the OOM killer, and return the userspace
241 	 * (which will retry the fault, or kill us if we got oom-killed).
242 	 */
243 	mmap_read_unlock(mm);
244 	if (!user_mode(regs))
245 		goto no_context;
246 	pagefault_out_of_memory();
247 	return;
248 
249 do_sigbus:
250 	mmap_read_unlock(mm);
251 
252 	/* Kernel mode? Handle exceptions or die */
253 	if (!user_mode(regs))
254 		goto no_context;
255 
256 	/*
257 	 * Send a sigbus, regardless of whether we were in kernel
258 	 * or user mode.
259 	 */
260 #if 0
261 	printk("do_page_fault() #3: sending SIGBUS to %s for "
262 	       "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
263 	       tsk->comm,
264 	       write ? "write access to" : "read access from",
265 	       field, address,
266 	       field, (unsigned long) regs->cp0_epc,
267 	       field, (unsigned long) regs->regs[31]);
268 #endif
269 	current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
270 	tsk->thread.cp0_badvaddr = address;
271 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
272 
273 	return;
274 #ifndef CONFIG_64BIT
275 vmalloc_fault:
276 	{
277 		/*
278 		 * Synchronize this task's top level page-table
279 		 * with the 'reference' page table.
280 		 *
281 		 * Do _not_ use "tsk" here. We might be inside
282 		 * an interrupt in the middle of a task switch..
283 		 */
284 		int offset = pgd_index(address);
285 		pgd_t *pgd, *pgd_k;
286 		p4d_t *p4d, *p4d_k;
287 		pud_t *pud, *pud_k;
288 		pmd_t *pmd, *pmd_k;
289 		pte_t *pte_k;
290 
291 		pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
292 		pgd_k = init_mm.pgd + offset;
293 
294 		if (!pgd_present(*pgd_k))
295 			goto no_context;
296 		set_pgd(pgd, *pgd_k);
297 
298 		p4d = p4d_offset(pgd, address);
299 		p4d_k = p4d_offset(pgd_k, address);
300 		if (!p4d_present(*p4d_k))
301 			goto no_context;
302 
303 		pud = pud_offset(p4d, address);
304 		pud_k = pud_offset(p4d_k, address);
305 		if (!pud_present(*pud_k))
306 			goto no_context;
307 
308 		pmd = pmd_offset(pud, address);
309 		pmd_k = pmd_offset(pud_k, address);
310 		if (!pmd_present(*pmd_k))
311 			goto no_context;
312 		set_pmd(pmd, *pmd_k);
313 
314 		pte_k = pte_offset_kernel(pmd_k, address);
315 		if (!pte_present(*pte_k))
316 			goto no_context;
317 		return;
318 	}
319 #endif
320 }
321 NOKPROBE_SYMBOL(__do_page_fault);
322 
323 asmlinkage void do_page_fault(struct pt_regs *regs,
324 	unsigned long write, unsigned long address)
325 {
326 	enum ctx_state prev_state;
327 
328 	prev_state = exception_enter();
329 	__do_page_fault(regs, write, address);
330 	exception_exit(prev_state);
331 }
332 NOKPROBE_SYMBOL(do_page_fault);
333