blob: 4c288f199453a41792f450ead64f71425218d0f4 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * From i386 code copyright (C) 1995 Linus Torvalds
15 */
16
17#include <linux/signal.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/string.h>
22#include <linux/types.h>
23#include <linux/ptrace.h>
24#include <linux/mman.h>
25#include <linux/mm.h>
26#include <linux/smp.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040027#include <linux/interrupt.h>
28#include <linux/init.h>
29#include <linux/tty.h>
30#include <linux/vt_kern.h> /* For unblank_screen() */
31#include <linux/highmem.h>
32#include <linux/module.h>
33#include <linux/kprobes.h>
34#include <linux/hugetlb.h>
35#include <linux/syscalls.h>
36#include <linux/uaccess.h>
Tony Lu3fa17c32013-08-09 15:08:57 -040037#include <linux/kdebug.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040038
Chris Metcalf867e3592010-05-28 23:09:12 -040039#include <asm/pgalloc.h>
40#include <asm/sections.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040041#include <asm/traps.h>
42#include <asm/syscalls.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040043
44#include <arch/interrupts.h>
45
Chris Metcalf571d76a2011-05-16 14:23:44 -040046static noinline void force_sig_info_fault(const char *type, int si_signo,
47 int si_code, unsigned long address,
48 int fault_num,
49 struct task_struct *tsk,
50 struct pt_regs *regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040051{
52 siginfo_t info;
53
54 if (unlikely(tsk->pid < 2)) {
55 panic("Signal %d (code %d) at %#lx sent to %s!",
56 si_signo, si_code & 0xffff, address,
Paul E. McKenneya95f8812011-11-10 16:16:13 -080057 is_idle_task(tsk) ? "the idle task" : "init");
Chris Metcalf867e3592010-05-28 23:09:12 -040058 }
59
60 info.si_signo = si_signo;
61 info.si_errno = 0;
62 info.si_code = si_code;
63 info.si_addr = (void __user *)address;
64 info.si_trapno = fault_num;
Chris Metcalf571d76a2011-05-16 14:23:44 -040065 trace_unhandled_signal(type, regs, address, si_signo);
Chris Metcalf867e3592010-05-28 23:09:12 -040066 force_sig_info(si_signo, &info, tsk);
67}
68
69#ifndef __tilegx__
70/*
71 * Synthesize the fault a PL0 process would get by doing a word-load of
Chris Metcalfd929b6a2010-10-14 14:34:33 -040072 * an unaligned address or a high kernel address.
Chris Metcalf867e3592010-05-28 23:09:12 -040073 */
Chris Metcalf6b14e412012-10-23 13:30:54 -040074SYSCALL_DEFINE1(cmpxchg_badaddr, unsigned long, address)
Chris Metcalf867e3592010-05-28 23:09:12 -040075{
Chris Metcalf6b14e412012-10-23 13:30:54 -040076 struct pt_regs *regs = current_pt_regs();
77
Chris Metcalf867e3592010-05-28 23:09:12 -040078 if (address >= PAGE_OFFSET)
Chris Metcalf571d76a2011-05-16 14:23:44 -040079 force_sig_info_fault("atomic segfault", SIGSEGV, SEGV_MAPERR,
80 address, INT_DTLB_MISS, current, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -040081 else
Chris Metcalf571d76a2011-05-16 14:23:44 -040082 force_sig_info_fault("atomic alignment fault", SIGBUS,
83 BUS_ADRALN, address,
84 INT_UNALIGN_DATA, current, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -040085
86 /*
87 * Adjust pc to point at the actual instruction, which is unusual
88 * for syscalls normally, but is appropriate when we are claiming
89 * that a syscall swint1 caused a page fault or bus error.
90 */
91 regs->pc -= 8;
92
93 /*
94 * Mark this as a caller-save interrupt, like a normal page fault,
95 * so that when we go through the signal handler path we will
96 * properly restore r0, r1, and r2 for the signal handler arguments.
97 */
98 regs->flags |= PT_FLAGS_CALLER_SAVES;
99
100 return 0;
101}
102#endif
103
104static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
105{
106 unsigned index = pgd_index(address);
107 pgd_t *pgd_k;
108 pud_t *pud, *pud_k;
109 pmd_t *pmd, *pmd_k;
110
111 pgd += index;
112 pgd_k = init_mm.pgd + index;
113
114 if (!pgd_present(*pgd_k))
115 return NULL;
116
117 pud = pud_offset(pgd, address);
118 pud_k = pud_offset(pgd_k, address);
119 if (!pud_present(*pud_k))
120 return NULL;
121
122 pmd = pmd_offset(pud, address);
123 pmd_k = pmd_offset(pud_k, address);
124 if (!pmd_present(*pmd_k))
125 return NULL;
Chris Metcalf1182b692013-08-07 11:07:52 -0400126 if (!pmd_present(*pmd))
Chris Metcalf867e3592010-05-28 23:09:12 -0400127 set_pmd(pmd, *pmd_k);
Chris Metcalf1182b692013-08-07 11:07:52 -0400128 else
Chris Metcalf867e3592010-05-28 23:09:12 -0400129 BUG_ON(pmd_ptfn(*pmd) != pmd_ptfn(*pmd_k));
130 return pmd_k;
131}
132
133/*
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400134 * Handle a fault on the vmalloc area.
Chris Metcalf867e3592010-05-28 23:09:12 -0400135 */
136static inline int vmalloc_fault(pgd_t *pgd, unsigned long address)
137{
138 pmd_t *pmd_k;
139 pte_t *pte_k;
140
141 /* Make sure we are in vmalloc area */
142 if (!(address >= VMALLOC_START && address < VMALLOC_END))
143 return -1;
144
145 /*
146 * Synchronize this task's top level page-table
147 * with the 'reference' page table.
148 */
149 pmd_k = vmalloc_sync_one(pgd, address);
150 if (!pmd_k)
151 return -1;
152 if (pmd_huge(*pmd_k))
153 return 0; /* support TILE huge_vmap() API */
154 pte_k = pte_offset_kernel(pmd_k, address);
155 if (!pte_present(*pte_k))
156 return -1;
157 return 0;
158}
159
160/* Wait until this PTE has completed migration. */
161static void wait_for_migration(pte_t *pte)
162{
163 if (pte_migrating(*pte)) {
164 /*
165 * Wait until the migrater fixes up this pte.
166 * We scale the loop count by the clock rate so we'll wait for
167 * a few seconds here.
168 */
169 int retries = 0;
170 int bound = get_clock_rate();
171 while (pte_migrating(*pte)) {
172 barrier();
173 if (++retries > bound)
174 panic("Hit migrating PTE (%#llx) and"
175 " page PFN %#lx still migrating",
176 pte->val, pte_pfn(*pte));
177 }
178 }
179}
180
181/*
182 * It's not generally safe to use "current" to get the page table pointer,
183 * since we might be running an oprofile interrupt in the middle of a
184 * task switch.
185 */
186static pgd_t *get_current_pgd(void)
187{
188 HV_Context ctx = hv_inquire_context();
189 unsigned long pgd_pfn = ctx.page_table >> PAGE_SHIFT;
190 struct page *pgd_page = pfn_to_page(pgd_pfn);
Chris Metcalf621b1952012-04-01 14:04:21 -0400191 BUG_ON(PageHighMem(pgd_page));
Chris Metcalf867e3592010-05-28 23:09:12 -0400192 return (pgd_t *) __va(ctx.page_table);
193}
194
195/*
196 * We can receive a page fault from a migrating PTE at any time.
197 * Handle it by just waiting until the fault resolves.
198 *
199 * It's also possible to get a migrating kernel PTE that resolves
200 * itself during the downcall from hypervisor to Linux. We just check
201 * here to see if the PTE seems valid, and if so we retry it.
202 *
203 * NOTE! We MUST NOT take any locks for this case. We may be in an
204 * interrupt or a critical region, and must do as little as possible.
205 * Similarly, we can't use atomic ops here, since we may be handling a
206 * fault caused by an atomic op access.
Chris Metcalf48292732012-03-29 15:34:52 -0400207 *
208 * If we find a migrating PTE while we're in an NMI context, and we're
209 * at a PC that has a registered exception handler, we don't wait,
210 * since this thread may (e.g.) have been interrupted while migrating
211 * its own stack, which would then cause us to self-deadlock.
Chris Metcalf867e3592010-05-28 23:09:12 -0400212 */
213static int handle_migrating_pte(pgd_t *pgd, int fault_num,
Chris Metcalf48292732012-03-29 15:34:52 -0400214 unsigned long address, unsigned long pc,
Chris Metcalf867e3592010-05-28 23:09:12 -0400215 int is_kernel_mode, int write)
216{
217 pud_t *pud;
218 pmd_t *pmd;
219 pte_t *pte;
220 pte_t pteval;
221
222 if (pgd_addr_invalid(address))
223 return 0;
224
225 pgd += pgd_index(address);
226 pud = pud_offset(pgd, address);
227 if (!pud || !pud_present(*pud))
228 return 0;
229 pmd = pmd_offset(pud, address);
230 if (!pmd || !pmd_present(*pmd))
231 return 0;
232 pte = pmd_huge_page(*pmd) ? ((pte_t *)pmd) :
233 pte_offset_kernel(pmd, address);
234 pteval = *pte;
235 if (pte_migrating(pteval)) {
Chris Metcalf48292732012-03-29 15:34:52 -0400236 if (in_nmi() && search_exception_tables(pc))
237 return 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400238 wait_for_migration(pte);
239 return 1;
240 }
241
242 if (!is_kernel_mode || !pte_present(pteval))
243 return 0;
244 if (fault_num == INT_ITLB_MISS) {
245 if (pte_exec(pteval))
246 return 1;
247 } else if (write) {
248 if (pte_write(pteval))
249 return 1;
250 } else {
251 if (pte_read(pteval))
252 return 1;
253 }
254
255 return 0;
256}
257
258/*
259 * This routine is responsible for faulting in user pages.
260 * It passes the work off to one of the appropriate routines.
261 * It returns true if the fault was successfully handled.
262 */
263static int handle_page_fault(struct pt_regs *regs,
264 int fault_num,
265 int is_page_fault,
266 unsigned long address,
267 int write)
268{
269 struct task_struct *tsk;
270 struct mm_struct *mm;
271 struct vm_area_struct *vma;
272 unsigned long stack_offset;
273 int fault;
274 int si_code;
275 int is_kernel_mode;
276 pgd_t *pgd;
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400277 unsigned int flags;
Chris Metcalf867e3592010-05-28 23:09:12 -0400278
279 /* on TILE, protection faults are always writes */
280 if (!is_page_fault)
281 write = 1;
282
Johannes Weiner759496b2013-09-12 15:13:39 -0700283 flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400284
Chris Metcalf051168d2013-09-03 14:45:52 -0400285 is_kernel_mode = !user_mode(regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400286
287 tsk = validate_current();
288
289 /*
290 * Check to see if we might be overwriting the stack, and bail
291 * out if so. The page fault code is a relatively likely
292 * place to get trapped in an infinite regress, and once we
293 * overwrite the whole stack, it becomes very hard to recover.
294 */
295 stack_offset = stack_pointer & (THREAD_SIZE-1);
296 if (stack_offset < THREAD_SIZE / 8) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400297 pr_alert("Potential stack overrun: sp %#lx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400298 stack_pointer);
299 show_regs(regs);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400300 pr_alert("Killing current process %d/%s\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400301 tsk->pid, tsk->comm);
302 do_group_exit(SIGKILL);
303 }
304
305 /*
306 * Early on, we need to check for migrating PTE entries;
307 * see homecache.c. If we find a migrating PTE, we wait until
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300308 * the backing page claims to be done migrating, then we proceed.
Chris Metcalf867e3592010-05-28 23:09:12 -0400309 * For kernel PTEs, we rewrite the PTE and return and retry.
310 * Otherwise, we treat the fault like a normal "no PTE" fault,
311 * rather than trying to patch up the existing PTE.
312 */
313 pgd = get_current_pgd();
Chris Metcalf48292732012-03-29 15:34:52 -0400314 if (handle_migrating_pte(pgd, fault_num, address, regs->pc,
Chris Metcalf867e3592010-05-28 23:09:12 -0400315 is_kernel_mode, write))
316 return 1;
317
318 si_code = SEGV_MAPERR;
319
320 /*
321 * We fault-in kernel-space virtual memory on-demand. The
322 * 'reference' page table is init_mm.pgd.
323 *
324 * NOTE! We MUST NOT take any locks for this case. We may
325 * be in an interrupt or a critical region, and should
326 * only copy the information from the master page table,
327 * nothing more.
328 *
329 * This verifies that the fault happens in kernel space
330 * and that the fault was not a protection fault.
331 */
332 if (unlikely(address >= TASK_SIZE &&
333 !is_arch_mappable_range(address, 0))) {
334 if (is_kernel_mode && is_page_fault &&
335 vmalloc_fault(pgd, address) >= 0)
336 return 1;
337 /*
338 * Don't take the mm semaphore here. If we fixup a prefetch
339 * fault we could otherwise deadlock.
340 */
341 mm = NULL; /* happy compiler */
342 vma = NULL;
343 goto bad_area_nosemaphore;
344 }
345
346 /*
347 * If we're trying to touch user-space addresses, we must
348 * be either at PL0, or else with interrupts enabled in the
Chris Metcalfb230ff22012-03-29 15:40:50 -0400349 * kernel, so either way we can re-enable interrupts here
350 * unless we are doing atomic access to user space with
351 * interrupts disabled.
Chris Metcalf867e3592010-05-28 23:09:12 -0400352 */
Chris Metcalfb230ff22012-03-29 15:40:50 -0400353 if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
354 local_irq_enable();
Chris Metcalf867e3592010-05-28 23:09:12 -0400355
356 mm = tsk->mm;
357
358 /*
359 * If we're in an interrupt, have no user context or are running in an
360 * atomic region then we must not take the fault.
361 */
362 if (in_atomic() || !mm) {
363 vma = NULL; /* happy compiler */
364 goto bad_area_nosemaphore;
365 }
366
Johannes Weiner759496b2013-09-12 15:13:39 -0700367 if (!is_kernel_mode)
368 flags |= FAULT_FLAG_USER;
369
Chris Metcalf867e3592010-05-28 23:09:12 -0400370 /*
371 * When running in the kernel we expect faults to occur only to
372 * addresses in user space. All other faults represent errors in the
373 * kernel and should generate an OOPS. Unfortunately, in the case of an
374 * erroneous fault occurring in a code path which already holds mmap_sem
375 * we will deadlock attempting to validate the fault against the
376 * address space. Luckily the kernel only validly references user
377 * space from well defined areas of code, which are listed in the
378 * exceptions table.
379 *
380 * As the vast majority of faults will be valid we will only perform
381 * the source reference check when there is a possibility of a deadlock.
382 * Attempt to lock the address space, if we cannot we then validate the
383 * source. If this is invalid we can skip the address space check,
384 * thus avoiding the deadlock.
385 */
386 if (!down_read_trylock(&mm->mmap_sem)) {
387 if (is_kernel_mode &&
388 !search_exception_tables(regs->pc)) {
389 vma = NULL; /* happy compiler */
390 goto bad_area_nosemaphore;
391 }
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400392
393retry:
Chris Metcalf867e3592010-05-28 23:09:12 -0400394 down_read(&mm->mmap_sem);
395 }
396
397 vma = find_vma(mm, address);
398 if (!vma)
399 goto bad_area;
400 if (vma->vm_start <= address)
401 goto good_area;
402 if (!(vma->vm_flags & VM_GROWSDOWN))
403 goto bad_area;
404 if (regs->sp < PAGE_OFFSET) {
405 /*
406 * accessing the stack below sp is always a bug.
407 */
408 if (address < regs->sp)
409 goto bad_area;
410 }
411 if (expand_stack(vma, address))
412 goto bad_area;
413
414/*
415 * Ok, we have a good vm_area for this memory access, so
416 * we can handle it..
417 */
418good_area:
419 si_code = SEGV_ACCERR;
420 if (fault_num == INT_ITLB_MISS) {
421 if (!(vma->vm_flags & VM_EXEC))
422 goto bad_area;
423 } else if (write) {
424#ifdef TEST_VERIFY_AREA
425 if (!is_page_fault && regs->cs == KERNEL_CS)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400426 pr_err("WP fault at "REGFMT"\n", regs->eip);
Chris Metcalf867e3592010-05-28 23:09:12 -0400427#endif
428 if (!(vma->vm_flags & VM_WRITE))
429 goto bad_area;
Johannes Weiner759496b2013-09-12 15:13:39 -0700430 flags |= FAULT_FLAG_WRITE;
Chris Metcalf867e3592010-05-28 23:09:12 -0400431 } else {
432 if (!is_page_fault || !(vma->vm_flags & VM_READ))
433 goto bad_area;
434 }
435
Chris Metcalf867e3592010-05-28 23:09:12 -0400436 /*
437 * If for any reason at all we couldn't handle the fault,
438 * make sure we exit gracefully rather than endlessly redo
439 * the fault.
440 */
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400441 fault = handle_mm_fault(mm, vma, address, flags);
442
443 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
444 return 0;
445
Chris Metcalf867e3592010-05-28 23:09:12 -0400446 if (unlikely(fault & VM_FAULT_ERROR)) {
447 if (fault & VM_FAULT_OOM)
448 goto out_of_memory;
449 else if (fault & VM_FAULT_SIGBUS)
450 goto do_sigbus;
451 BUG();
452 }
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400453 if (flags & FAULT_FLAG_ALLOW_RETRY) {
454 if (fault & VM_FAULT_MAJOR)
455 tsk->maj_flt++;
456 else
457 tsk->min_flt++;
458 if (fault & VM_FAULT_RETRY) {
459 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -0700460 flags |= FAULT_FLAG_TRIED;
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400461
462 /*
463 * No need to up_read(&mm->mmap_sem) as we would
464 * have already released it in __lock_page_or_retry
465 * in mm/filemap.c.
466 */
467 goto retry;
468 }
469 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400470
Chris Metcalf867e3592010-05-28 23:09:12 -0400471#if CHIP_HAS_TILE_DMA()
Chris Metcalfd7c96612013-08-15 16:23:24 -0400472 /* If this was a DMA TLB fault, restart the DMA engine. */
473 switch (fault_num) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400474 case INT_DMATLB_MISS:
475 case INT_DMATLB_MISS_DWNCL:
476 case INT_DMATLB_ACCESS:
477 case INT_DMATLB_ACCESS_DWNCL:
478 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
479 break;
Chris Metcalf867e3592010-05-28 23:09:12 -0400480 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400481#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400482
483 up_read(&mm->mmap_sem);
484 return 1;
485
486/*
487 * Something tried to access memory that isn't in our memory map..
488 * Fix it, but check if it's kernel or user first..
489 */
490bad_area:
491 up_read(&mm->mmap_sem);
492
493bad_area_nosemaphore:
494 /* User mode accesses just cause a SIGSEGV */
495 if (!is_kernel_mode) {
496 /*
497 * It's possible to have interrupts off here.
498 */
499 local_irq_enable();
500
Chris Metcalf571d76a2011-05-16 14:23:44 -0400501 force_sig_info_fault("segfault", SIGSEGV, si_code, address,
502 fault_num, tsk, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400503 return 0;
504 }
505
506no_context:
507 /* Are we prepared to handle this kernel fault? */
508 if (fixup_exception(regs))
509 return 0;
510
511/*
512 * Oops. The kernel tried to access some bad page. We'll have to
513 * terminate things with extreme prejudice.
514 */
515
516 bust_spinlocks(1);
517
518 /* FIXME: no lookup_address() yet */
519#ifdef SUPPORT_LOOKUP_ADDRESS
520 if (fault_num == INT_ITLB_MISS) {
521 pte_t *pte = lookup_address(address);
522
523 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400524 pr_crit("kernel tried to execute"
Chris Metcalf867e3592010-05-28 23:09:12 -0400525 " non-executable page - exploit attempt?"
526 " (uid: %d)\n", current->uid);
527 }
528#endif
529 if (address < PAGE_SIZE)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400530 pr_alert("Unable to handle kernel NULL pointer dereference\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400531 else
Chris Metcalf0707ad32010-06-25 17:04:17 -0400532 pr_alert("Unable to handle kernel paging request\n");
533 pr_alert(" at virtual address "REGFMT", pc "REGFMT"\n",
534 address, regs->pc);
Chris Metcalf867e3592010-05-28 23:09:12 -0400535
536 show_regs(regs);
537
538 if (unlikely(tsk->pid < 2)) {
539 panic("Kernel page fault running %s!",
Paul E. McKenneya95f8812011-11-10 16:16:13 -0800540 is_idle_task(tsk) ? "the idle task" : "init");
Chris Metcalf867e3592010-05-28 23:09:12 -0400541 }
542
543 /*
544 * More FIXME: we should probably copy the i386 here and
545 * implement a generic die() routine. Not today.
546 */
547#ifdef SUPPORT_DIE
548 die("Oops", regs);
549#endif
550 bust_spinlocks(1);
551
552 do_group_exit(SIGKILL);
553
554/*
555 * We ran out of memory, or some other thing happened to us that made
556 * us unable to handle the page fault gracefully.
557 */
558out_of_memory:
559 up_read(&mm->mmap_sem);
Johannes Weiner609838c2013-07-08 15:59:50 -0700560 if (is_kernel_mode)
561 goto no_context;
562 pagefault_out_of_memory();
563 return 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400564
565do_sigbus:
566 up_read(&mm->mmap_sem);
567
568 /* Kernel mode? Handle exceptions or die */
569 if (is_kernel_mode)
570 goto no_context;
571
Chris Metcalf571d76a2011-05-16 14:23:44 -0400572 force_sig_info_fault("bus error", SIGBUS, BUS_ADRERR, address,
573 fault_num, tsk, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400574 return 0;
575}
576
577#ifndef __tilegx__
578
Chris Metcalf867e3592010-05-28 23:09:12 -0400579/* We must release ICS before panicking or we won't get anywhere. */
580#define ics_panic(fmt, ...) do { \
581 __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0); \
582 panic(fmt, __VA_ARGS__); \
583} while (0)
584
Chris Metcalf867e3592010-05-28 23:09:12 -0400585/*
586 * When we take an ITLB or DTLB fault or access violation in the
587 * supervisor while the critical section bit is set, the hypervisor is
Chris Metcalfa78c9422010-10-14 16:23:03 -0400588 * reluctant to write new values into the EX_CONTEXT_K_x registers,
Chris Metcalf867e3592010-05-28 23:09:12 -0400589 * since that might indicate we have not yet squirreled the SPR
590 * contents away and can thus safely take a recursive interrupt.
Chris Metcalfa78c9422010-10-14 16:23:03 -0400591 * Accordingly, the hypervisor passes us the PC via SYSTEM_SAVE_K_2.
Chris Metcalfc745a8a2010-08-13 08:52:19 -0400592 *
593 * Note that this routine is called before homecache_tlb_defer_enter(),
594 * which means that we can properly unlock any atomics that might
595 * be used there (good), but also means we must be very sensitive
596 * to not touch any data structures that might be located in memory
597 * that could migrate, as we could be entering the kernel on a dataplane
598 * cpu that has been deferring kernel TLB updates. This means, for
599 * example, that we can't migrate init_mm or its pgd.
Chris Metcalf867e3592010-05-28 23:09:12 -0400600 */
601struct intvec_state do_page_fault_ics(struct pt_regs *regs, int fault_num,
602 unsigned long address,
603 unsigned long info)
604{
605 unsigned long pc = info & ~1;
606 int write = info & 1;
607 pgd_t *pgd = get_current_pgd();
608
609 /* Retval is 1 at first since we will handle the fault fully. */
610 struct intvec_state state = {
611 do_page_fault, fault_num, address, write, 1
612 };
613
614 /* Validate that we are plausibly in the right routine. */
615 if ((pc & 0x7) != 0 || pc < PAGE_OFFSET ||
616 (fault_num != INT_DTLB_MISS &&
617 fault_num != INT_DTLB_ACCESS)) {
618 unsigned long old_pc = regs->pc;
619 regs->pc = pc;
620 ics_panic("Bad ICS page fault args:"
621 " old PC %#lx, fault %d/%d at %#lx\n",
622 old_pc, fault_num, write, address);
623 }
624
625 /* We might be faulting on a vmalloc page, so check that first. */
626 if (fault_num != INT_DTLB_ACCESS && vmalloc_fault(pgd, address) >= 0)
627 return state;
628
629 /*
630 * If we faulted with ICS set in sys_cmpxchg, we are providing
631 * a user syscall service that should generate a signal on
632 * fault. We didn't set up a kernel stack on initial entry to
633 * sys_cmpxchg, but instead had one set up by the fault, which
634 * (because sys_cmpxchg never releases ICS) came to us via the
Chris Metcalfa78c9422010-10-14 16:23:03 -0400635 * SYSTEM_SAVE_K_2 mechanism, and thus EX_CONTEXT_K_[01] are
Chris Metcalf867e3592010-05-28 23:09:12 -0400636 * still referencing the original user code. We release the
637 * atomic lock and rewrite pt_regs so that it appears that we
638 * came from user-space directly, and after we finish the
639 * fault we'll go back to user space and re-issue the swint.
640 * This way the backtrace information is correct if we need to
641 * emit a stack dump at any point while handling this.
642 *
643 * Must match register use in sys_cmpxchg().
644 */
645 if (pc >= (unsigned long) sys_cmpxchg &&
646 pc < (unsigned long) __sys_cmpxchg_end) {
647#ifdef CONFIG_SMP
648 /* Don't unlock before we could have locked. */
649 if (pc >= (unsigned long)__sys_cmpxchg_grab_lock) {
650 int *lock_ptr = (int *)(regs->regs[ATOMIC_LOCK_REG]);
651 __atomic_fault_unlock(lock_ptr);
652 }
653#endif
654 regs->sp = regs->regs[27];
655 }
656
657 /*
658 * We can also fault in the atomic assembly, in which
659 * case we use the exception table to do the first-level fixup.
660 * We may re-fixup again in the real fault handler if it
661 * turns out the faulting address is just bad, and not,
662 * for example, migrating.
663 */
664 else if (pc >= (unsigned long) __start_atomic_asm_code &&
665 pc < (unsigned long) __end_atomic_asm_code) {
666 const struct exception_table_entry *fixup;
667#ifdef CONFIG_SMP
668 /* Unlock the atomic lock. */
669 int *lock_ptr = (int *)(regs->regs[ATOMIC_LOCK_REG]);
670 __atomic_fault_unlock(lock_ptr);
671#endif
672 fixup = search_exception_tables(pc);
673 if (!fixup)
674 ics_panic("ICS atomic fault not in table:"
675 " PC %#lx, fault %d", pc, fault_num);
676 regs->pc = fixup->fixup;
677 regs->ex1 = PL_ICS_EX1(KERNEL_PL, 0);
678 }
679
680 /*
Chris Metcalf867e3592010-05-28 23:09:12 -0400681 * Now that we have released the atomic lock (if necessary),
682 * it's safe to spin if the PTE that caused the fault was migrating.
683 */
684 if (fault_num == INT_DTLB_ACCESS)
685 write = 1;
Chris Metcalf48292732012-03-29 15:34:52 -0400686 if (handle_migrating_pte(pgd, fault_num, address, pc, 1, write))
Chris Metcalf867e3592010-05-28 23:09:12 -0400687 return state;
688
689 /* Return zero so that we continue on with normal fault handling. */
690 state.retval = 0;
691 return state;
692}
693
694#endif /* !__tilegx__ */
695
696/*
697 * This routine handles page faults. It determines the address, and the
698 * problem, and then passes it handle_page_fault() for normal DTLB and
699 * ITLB issues, and for DMA or SN processor faults when we are in user
700 * space. For the latter, if we're in kernel mode, we just save the
701 * interrupt away appropriately and return immediately. We can't do
702 * page faults for user code while in kernel mode.
703 */
704void do_page_fault(struct pt_regs *regs, int fault_num,
705 unsigned long address, unsigned long write)
706{
707 int is_page_fault;
708
Tony Lu3fa17c32013-08-09 15:08:57 -0400709#ifdef CONFIG_KPROBES
710 /*
711 * This is to notify the fault handler of the kprobes. The
712 * exception code is redundant as it is also carried in REGS,
713 * but we pass it anyhow.
714 */
715 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
716 regs->faultnum, SIGSEGV) == NOTIFY_STOP)
717 return;
718#endif
719
Chris Metcalf2f9ac292013-08-06 16:04:13 -0400720#ifdef __tilegx__
721 /*
722 * We don't need early do_page_fault_ics() support, since unlike
723 * Pro we don't need to worry about unlocking the atomic locks.
724 * There is only one current case in GX where we touch any memory
725 * under ICS other than our own kernel stack, and we handle that
726 * here. (If we crash due to trying to touch our own stack,
727 * we're in too much trouble for C code to help out anyway.)
728 */
729 if (write & ~1) {
730 unsigned long pc = write & ~1;
731 if (pc >= (unsigned long) __start_unalign_asm_code &&
732 pc < (unsigned long) __end_unalign_asm_code) {
733 struct thread_info *ti = current_thread_info();
734 /*
735 * Our EX_CONTEXT is still what it was from the
736 * initial unalign exception, but now we've faulted
737 * on the JIT page. We would like to complete the
738 * page fault however is appropriate, and then retry
739 * the instruction that caused the unalign exception.
740 * Our state has been "corrupted" by setting the low
741 * bit in "sp", and stashing r0..r3 in the
742 * thread_info area, so we revert all of that, then
743 * continue as if this were a normal page fault.
744 */
745 regs->sp &= ~1UL;
746 regs->regs[0] = ti->unalign_jit_tmp[0];
747 regs->regs[1] = ti->unalign_jit_tmp[1];
748 regs->regs[2] = ti->unalign_jit_tmp[2];
749 regs->regs[3] = ti->unalign_jit_tmp[3];
750 write &= 1;
751 } else {
752 pr_alert("%s/%d: ICS set at page fault at %#lx: %#lx\n",
753 current->comm, current->pid, pc, address);
754 show_regs(regs);
755 do_group_exit(SIGKILL);
756 return;
757 }
758 }
759#else
Chris Metcalf867e3592010-05-28 23:09:12 -0400760 /* This case should have been handled by do_page_fault_ics(). */
761 BUG_ON(write & ~1);
Chris Metcalf2f9ac292013-08-06 16:04:13 -0400762#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400763
764#if CHIP_HAS_TILE_DMA()
765 /*
766 * If it's a DMA fault, suspend the transfer while we're
767 * handling the miss; we'll restart after it's handled. If we
768 * don't suspend, it's possible that this process could swap
769 * out and back in, and restart the engine since the DMA is
770 * still 'running'.
771 */
772 if (fault_num == INT_DMATLB_MISS ||
773 fault_num == INT_DMATLB_ACCESS ||
774 fault_num == INT_DMATLB_MISS_DWNCL ||
775 fault_num == INT_DMATLB_ACCESS_DWNCL) {
776 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__SUSPEND_MASK);
777 while (__insn_mfspr(SPR_DMA_USER_STATUS) &
778 SPR_DMA_STATUS__BUSY_MASK)
779 ;
780 }
781#endif
782
783 /* Validate fault num and decide if this is a first-time page fault. */
784 switch (fault_num) {
785 case INT_ITLB_MISS:
786 case INT_DTLB_MISS:
787#if CHIP_HAS_TILE_DMA()
788 case INT_DMATLB_MISS:
789 case INT_DMATLB_MISS_DWNCL:
790#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400791 is_page_fault = 1;
792 break;
793
794 case INT_DTLB_ACCESS:
795#if CHIP_HAS_TILE_DMA()
796 case INT_DMATLB_ACCESS:
797 case INT_DMATLB_ACCESS_DWNCL:
798#endif
799 is_page_fault = 0;
800 break;
801
802 default:
803 panic("Bad fault number %d in do_page_fault", fault_num);
804 }
805
Chris Metcalfd7c96612013-08-15 16:23:24 -0400806#if CHIP_HAS_TILE_DMA()
Chris Metcalf051168d2013-09-03 14:45:52 -0400807 if (!user_mode(regs)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400808 struct async_tlb *async;
809 switch (fault_num) {
810#if CHIP_HAS_TILE_DMA()
811 case INT_DMATLB_MISS:
812 case INT_DMATLB_ACCESS:
813 case INT_DMATLB_MISS_DWNCL:
814 case INT_DMATLB_ACCESS_DWNCL:
815 async = &current->thread.dma_async_tlb;
816 break;
817#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400818 default:
819 async = NULL;
820 }
821 if (async) {
822
823 /*
824 * No vmalloc check required, so we can allow
825 * interrupts immediately at this point.
826 */
827 local_irq_enable();
828
829 set_thread_flag(TIF_ASYNC_TLB);
830 if (async->fault_num != 0) {
831 panic("Second async fault %d;"
832 " old fault was %d (%#lx/%ld)",
833 fault_num, async->fault_num,
834 address, write);
835 }
836 BUG_ON(fault_num == 0);
837 async->fault_num = fault_num;
838 async->is_fault = is_page_fault;
839 async->is_write = write;
840 async->address = address;
841 return;
842 }
843 }
Chris Metcalf313ce672011-05-02 14:50:06 -0400844#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400845
846 handle_page_fault(regs, fault_num, is_page_fault, address, write);
847}
848
849
Chris Metcalfd7c96612013-08-15 16:23:24 -0400850#if CHIP_HAS_TILE_DMA()
Chris Metcalf867e3592010-05-28 23:09:12 -0400851/*
Chris Metcalfd7c96612013-08-15 16:23:24 -0400852 * This routine effectively re-issues asynchronous page faults
853 * when we are returning to user space.
Chris Metcalf867e3592010-05-28 23:09:12 -0400854 */
Chris Metcalfd7c96612013-08-15 16:23:24 -0400855void do_async_page_fault(struct pt_regs *regs)
Chris Metcalf867e3592010-05-28 23:09:12 -0400856{
Chris Metcalfd7c96612013-08-15 16:23:24 -0400857 struct async_tlb *async = &current->thread.dma_async_tlb;
858
859 /*
860 * Clear thread flag early. If we re-interrupt while processing
861 * code here, we will reset it and recall this routine before
862 * returning to user space.
863 */
864 clear_thread_flag(TIF_ASYNC_TLB);
865
Chris Metcalf867e3592010-05-28 23:09:12 -0400866 if (async->fault_num) {
867 /*
868 * Clear async->fault_num before calling the page-fault
869 * handler so that if we re-interrupt before returning
870 * from the function we have somewhere to put the
871 * information from the new interrupt.
872 */
873 int fault_num = async->fault_num;
874 async->fault_num = 0;
875 handle_page_fault(regs, fault_num, async->is_fault,
876 async->address, async->is_write);
877 }
878}
Chris Metcalfd7c96612013-08-15 16:23:24 -0400879#endif /* CHIP_HAS_TILE_DMA() */
Chris Metcalf313ce672011-05-02 14:50:06 -0400880
Chris Metcalf867e3592010-05-28 23:09:12 -0400881
882void vmalloc_sync_all(void)
883{
884#ifdef __tilegx__
885 /* Currently all L1 kernel pmd's are static and shared. */
Chris Metcalfe5f7bd42013-08-06 14:34:08 -0400886 BUILD_BUG_ON(pgd_index(VMALLOC_END - PAGE_SIZE) !=
887 pgd_index(VMALLOC_START));
Chris Metcalf867e3592010-05-28 23:09:12 -0400888#else
889 /*
890 * Note that races in the updates of insync and start aren't
891 * problematic: insync can only get set bits added, and updates to
892 * start are only improving performance (without affecting correctness
893 * if undone).
894 */
895 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
896 static unsigned long start = PAGE_OFFSET;
897 unsigned long address;
898
899 BUILD_BUG_ON(PAGE_OFFSET & ~PGDIR_MASK);
900 for (address = start; address >= PAGE_OFFSET; address += PGDIR_SIZE) {
901 if (!test_bit(pgd_index(address), insync)) {
902 unsigned long flags;
903 struct list_head *pos;
904
905 spin_lock_irqsave(&pgd_lock, flags);
906 list_for_each(pos, &pgd_list)
907 if (!vmalloc_sync_one(list_to_pgd(pos),
908 address)) {
909 /* Must be at first entry in list. */
910 BUG_ON(pos != pgd_list.next);
911 break;
912 }
913 spin_unlock_irqrestore(&pgd_lock, flags);
914 if (pos != pgd_list.next)
915 set_bit(pgd_index(address), insync);
916 }
917 if (address == start && test_bit(pgd_index(address), insync))
918 start = address + PGDIR_SIZE;
919 }
920#endif
921}