blob: a362926d513acdbe5175f521cb833da5b7925bb5 [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>
37
Chris Metcalf867e3592010-05-28 23:09:12 -040038#include <asm/pgalloc.h>
39#include <asm/sections.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040040#include <asm/traps.h>
41#include <asm/syscalls.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040042
43#include <arch/interrupts.h>
44
Chris Metcalf571d76a2011-05-16 14:23:44 -040045static noinline void force_sig_info_fault(const char *type, int si_signo,
46 int si_code, unsigned long address,
47 int fault_num,
48 struct task_struct *tsk,
49 struct pt_regs *regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040050{
51 siginfo_t info;
52
53 if (unlikely(tsk->pid < 2)) {
54 panic("Signal %d (code %d) at %#lx sent to %s!",
55 si_signo, si_code & 0xffff, address,
Paul E. McKenneya95f8812011-11-10 16:16:13 -080056 is_idle_task(tsk) ? "the idle task" : "init");
Chris Metcalf867e3592010-05-28 23:09:12 -040057 }
58
59 info.si_signo = si_signo;
60 info.si_errno = 0;
61 info.si_code = si_code;
62 info.si_addr = (void __user *)address;
63 info.si_trapno = fault_num;
Chris Metcalf571d76a2011-05-16 14:23:44 -040064 trace_unhandled_signal(type, regs, address, si_signo);
Chris Metcalf867e3592010-05-28 23:09:12 -040065 force_sig_info(si_signo, &info, tsk);
66}
67
68#ifndef __tilegx__
69/*
70 * Synthesize the fault a PL0 process would get by doing a word-load of
Chris Metcalfd929b6a2010-10-14 14:34:33 -040071 * an unaligned address or a high kernel address.
Chris Metcalf867e3592010-05-28 23:09:12 -040072 */
Chris Metcalfd929b6a2010-10-14 14:34:33 -040073SYSCALL_DEFINE2(cmpxchg_badaddr, unsigned long, address,
74 struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040075{
76 if (address >= PAGE_OFFSET)
Chris Metcalf571d76a2011-05-16 14:23:44 -040077 force_sig_info_fault("atomic segfault", SIGSEGV, SEGV_MAPERR,
78 address, INT_DTLB_MISS, current, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -040079 else
Chris Metcalf571d76a2011-05-16 14:23:44 -040080 force_sig_info_fault("atomic alignment fault", SIGBUS,
81 BUS_ADRALN, address,
82 INT_UNALIGN_DATA, current, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -040083
84 /*
85 * Adjust pc to point at the actual instruction, which is unusual
86 * for syscalls normally, but is appropriate when we are claiming
87 * that a syscall swint1 caused a page fault or bus error.
88 */
89 regs->pc -= 8;
90
91 /*
92 * Mark this as a caller-save interrupt, like a normal page fault,
93 * so that when we go through the signal handler path we will
94 * properly restore r0, r1, and r2 for the signal handler arguments.
95 */
96 regs->flags |= PT_FLAGS_CALLER_SAVES;
97
98 return 0;
99}
100#endif
101
102static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
103{
104 unsigned index = pgd_index(address);
105 pgd_t *pgd_k;
106 pud_t *pud, *pud_k;
107 pmd_t *pmd, *pmd_k;
108
109 pgd += index;
110 pgd_k = init_mm.pgd + index;
111
112 if (!pgd_present(*pgd_k))
113 return NULL;
114
115 pud = pud_offset(pgd, address);
116 pud_k = pud_offset(pgd_k, address);
117 if (!pud_present(*pud_k))
118 return NULL;
119
120 pmd = pmd_offset(pud, address);
121 pmd_k = pmd_offset(pud_k, address);
122 if (!pmd_present(*pmd_k))
123 return NULL;
124 if (!pmd_present(*pmd)) {
125 set_pmd(pmd, *pmd_k);
126 arch_flush_lazy_mmu_mode();
127 } else
128 BUG_ON(pmd_ptfn(*pmd) != pmd_ptfn(*pmd_k));
129 return pmd_k;
130}
131
132/*
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400133 * Handle a fault on the vmalloc area.
Chris Metcalf867e3592010-05-28 23:09:12 -0400134 */
135static inline int vmalloc_fault(pgd_t *pgd, unsigned long address)
136{
137 pmd_t *pmd_k;
138 pte_t *pte_k;
139
140 /* Make sure we are in vmalloc area */
141 if (!(address >= VMALLOC_START && address < VMALLOC_END))
142 return -1;
143
144 /*
145 * Synchronize this task's top level page-table
146 * with the 'reference' page table.
147 */
148 pmd_k = vmalloc_sync_one(pgd, address);
149 if (!pmd_k)
150 return -1;
151 if (pmd_huge(*pmd_k))
152 return 0; /* support TILE huge_vmap() API */
153 pte_k = pte_offset_kernel(pmd_k, address);
154 if (!pte_present(*pte_k))
155 return -1;
156 return 0;
157}
158
159/* Wait until this PTE has completed migration. */
160static void wait_for_migration(pte_t *pte)
161{
162 if (pte_migrating(*pte)) {
163 /*
164 * Wait until the migrater fixes up this pte.
165 * We scale the loop count by the clock rate so we'll wait for
166 * a few seconds here.
167 */
168 int retries = 0;
169 int bound = get_clock_rate();
170 while (pte_migrating(*pte)) {
171 barrier();
172 if (++retries > bound)
173 panic("Hit migrating PTE (%#llx) and"
174 " page PFN %#lx still migrating",
175 pte->val, pte_pfn(*pte));
176 }
177 }
178}
179
180/*
181 * It's not generally safe to use "current" to get the page table pointer,
182 * since we might be running an oprofile interrupt in the middle of a
183 * task switch.
184 */
185static pgd_t *get_current_pgd(void)
186{
187 HV_Context ctx = hv_inquire_context();
188 unsigned long pgd_pfn = ctx.page_table >> PAGE_SHIFT;
189 struct page *pgd_page = pfn_to_page(pgd_pfn);
190 BUG_ON(PageHighMem(pgd_page)); /* oops, HIGHPTE? */
191 return (pgd_t *) __va(ctx.page_table);
192}
193
194/*
195 * We can receive a page fault from a migrating PTE at any time.
196 * Handle it by just waiting until the fault resolves.
197 *
198 * It's also possible to get a migrating kernel PTE that resolves
199 * itself during the downcall from hypervisor to Linux. We just check
200 * here to see if the PTE seems valid, and if so we retry it.
201 *
202 * NOTE! We MUST NOT take any locks for this case. We may be in an
203 * interrupt or a critical region, and must do as little as possible.
204 * Similarly, we can't use atomic ops here, since we may be handling a
205 * fault caused by an atomic op access.
Chris Metcalf48292732012-03-29 15:34:52 -0400206 *
207 * If we find a migrating PTE while we're in an NMI context, and we're
208 * at a PC that has a registered exception handler, we don't wait,
209 * since this thread may (e.g.) have been interrupted while migrating
210 * its own stack, which would then cause us to self-deadlock.
Chris Metcalf867e3592010-05-28 23:09:12 -0400211 */
212static int handle_migrating_pte(pgd_t *pgd, int fault_num,
Chris Metcalf48292732012-03-29 15:34:52 -0400213 unsigned long address, unsigned long pc,
Chris Metcalf867e3592010-05-28 23:09:12 -0400214 int is_kernel_mode, int write)
215{
216 pud_t *pud;
217 pmd_t *pmd;
218 pte_t *pte;
219 pte_t pteval;
220
221 if (pgd_addr_invalid(address))
222 return 0;
223
224 pgd += pgd_index(address);
225 pud = pud_offset(pgd, address);
226 if (!pud || !pud_present(*pud))
227 return 0;
228 pmd = pmd_offset(pud, address);
229 if (!pmd || !pmd_present(*pmd))
230 return 0;
231 pte = pmd_huge_page(*pmd) ? ((pte_t *)pmd) :
232 pte_offset_kernel(pmd, address);
233 pteval = *pte;
234 if (pte_migrating(pteval)) {
Chris Metcalf48292732012-03-29 15:34:52 -0400235 if (in_nmi() && search_exception_tables(pc))
236 return 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400237 wait_for_migration(pte);
238 return 1;
239 }
240
241 if (!is_kernel_mode || !pte_present(pteval))
242 return 0;
243 if (fault_num == INT_ITLB_MISS) {
244 if (pte_exec(pteval))
245 return 1;
246 } else if (write) {
247 if (pte_write(pteval))
248 return 1;
249 } else {
250 if (pte_read(pteval))
251 return 1;
252 }
253
254 return 0;
255}
256
257/*
258 * This routine is responsible for faulting in user pages.
259 * It passes the work off to one of the appropriate routines.
260 * It returns true if the fault was successfully handled.
261 */
262static int handle_page_fault(struct pt_regs *regs,
263 int fault_num,
264 int is_page_fault,
265 unsigned long address,
266 int write)
267{
268 struct task_struct *tsk;
269 struct mm_struct *mm;
270 struct vm_area_struct *vma;
271 unsigned long stack_offset;
272 int fault;
273 int si_code;
274 int is_kernel_mode;
275 pgd_t *pgd;
276
277 /* on TILE, protection faults are always writes */
278 if (!is_page_fault)
279 write = 1;
280
281 is_kernel_mode = (EX1_PL(regs->ex1) != USER_PL);
282
283 tsk = validate_current();
284
285 /*
286 * Check to see if we might be overwriting the stack, and bail
287 * out if so. The page fault code is a relatively likely
288 * place to get trapped in an infinite regress, and once we
289 * overwrite the whole stack, it becomes very hard to recover.
290 */
291 stack_offset = stack_pointer & (THREAD_SIZE-1);
292 if (stack_offset < THREAD_SIZE / 8) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400293 pr_alert("Potential stack overrun: sp %#lx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400294 stack_pointer);
295 show_regs(regs);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400296 pr_alert("Killing current process %d/%s\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400297 tsk->pid, tsk->comm);
298 do_group_exit(SIGKILL);
299 }
300
301 /*
302 * Early on, we need to check for migrating PTE entries;
303 * see homecache.c. If we find a migrating PTE, we wait until
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300304 * the backing page claims to be done migrating, then we proceed.
Chris Metcalf867e3592010-05-28 23:09:12 -0400305 * For kernel PTEs, we rewrite the PTE and return and retry.
306 * Otherwise, we treat the fault like a normal "no PTE" fault,
307 * rather than trying to patch up the existing PTE.
308 */
309 pgd = get_current_pgd();
Chris Metcalf48292732012-03-29 15:34:52 -0400310 if (handle_migrating_pte(pgd, fault_num, address, regs->pc,
Chris Metcalf867e3592010-05-28 23:09:12 -0400311 is_kernel_mode, write))
312 return 1;
313
314 si_code = SEGV_MAPERR;
315
316 /*
317 * We fault-in kernel-space virtual memory on-demand. The
318 * 'reference' page table is init_mm.pgd.
319 *
320 * NOTE! We MUST NOT take any locks for this case. We may
321 * be in an interrupt or a critical region, and should
322 * only copy the information from the master page table,
323 * nothing more.
324 *
325 * This verifies that the fault happens in kernel space
326 * and that the fault was not a protection fault.
327 */
328 if (unlikely(address >= TASK_SIZE &&
329 !is_arch_mappable_range(address, 0))) {
330 if (is_kernel_mode && is_page_fault &&
331 vmalloc_fault(pgd, address) >= 0)
332 return 1;
333 /*
334 * Don't take the mm semaphore here. If we fixup a prefetch
335 * fault we could otherwise deadlock.
336 */
337 mm = NULL; /* happy compiler */
338 vma = NULL;
339 goto bad_area_nosemaphore;
340 }
341
342 /*
343 * If we're trying to touch user-space addresses, we must
344 * be either at PL0, or else with interrupts enabled in the
Chris Metcalfb230ff22012-03-29 15:40:50 -0400345 * kernel, so either way we can re-enable interrupts here
346 * unless we are doing atomic access to user space with
347 * interrupts disabled.
Chris Metcalf867e3592010-05-28 23:09:12 -0400348 */
Chris Metcalfb230ff22012-03-29 15:40:50 -0400349 if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
350 local_irq_enable();
Chris Metcalf867e3592010-05-28 23:09:12 -0400351
352 mm = tsk->mm;
353
354 /*
355 * If we're in an interrupt, have no user context or are running in an
356 * atomic region then we must not take the fault.
357 */
358 if (in_atomic() || !mm) {
359 vma = NULL; /* happy compiler */
360 goto bad_area_nosemaphore;
361 }
362
363 /*
364 * When running in the kernel we expect faults to occur only to
365 * addresses in user space. All other faults represent errors in the
366 * kernel and should generate an OOPS. Unfortunately, in the case of an
367 * erroneous fault occurring in a code path which already holds mmap_sem
368 * we will deadlock attempting to validate the fault against the
369 * address space. Luckily the kernel only validly references user
370 * space from well defined areas of code, which are listed in the
371 * exceptions table.
372 *
373 * As the vast majority of faults will be valid we will only perform
374 * the source reference check when there is a possibility of a deadlock.
375 * Attempt to lock the address space, if we cannot we then validate the
376 * source. If this is invalid we can skip the address space check,
377 * thus avoiding the deadlock.
378 */
379 if (!down_read_trylock(&mm->mmap_sem)) {
380 if (is_kernel_mode &&
381 !search_exception_tables(regs->pc)) {
382 vma = NULL; /* happy compiler */
383 goto bad_area_nosemaphore;
384 }
385 down_read(&mm->mmap_sem);
386 }
387
388 vma = find_vma(mm, address);
389 if (!vma)
390 goto bad_area;
391 if (vma->vm_start <= address)
392 goto good_area;
393 if (!(vma->vm_flags & VM_GROWSDOWN))
394 goto bad_area;
395 if (regs->sp < PAGE_OFFSET) {
396 /*
397 * accessing the stack below sp is always a bug.
398 */
399 if (address < regs->sp)
400 goto bad_area;
401 }
402 if (expand_stack(vma, address))
403 goto bad_area;
404
405/*
406 * Ok, we have a good vm_area for this memory access, so
407 * we can handle it..
408 */
409good_area:
410 si_code = SEGV_ACCERR;
411 if (fault_num == INT_ITLB_MISS) {
412 if (!(vma->vm_flags & VM_EXEC))
413 goto bad_area;
414 } else if (write) {
415#ifdef TEST_VERIFY_AREA
416 if (!is_page_fault && regs->cs == KERNEL_CS)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400417 pr_err("WP fault at "REGFMT"\n", regs->eip);
Chris Metcalf867e3592010-05-28 23:09:12 -0400418#endif
419 if (!(vma->vm_flags & VM_WRITE))
420 goto bad_area;
421 } else {
422 if (!is_page_fault || !(vma->vm_flags & VM_READ))
423 goto bad_area;
424 }
425
426 survive:
427 /*
428 * If for any reason at all we couldn't handle the fault,
429 * make sure we exit gracefully rather than endlessly redo
430 * the fault.
431 */
432 fault = handle_mm_fault(mm, vma, address, write);
433 if (unlikely(fault & VM_FAULT_ERROR)) {
434 if (fault & VM_FAULT_OOM)
435 goto out_of_memory;
Linus Torvalds3f7e5102015-01-29 10:51:32 -0800436 else if (fault & VM_FAULT_SIGSEGV)
437 goto bad_area;
Chris Metcalf867e3592010-05-28 23:09:12 -0400438 else if (fault & VM_FAULT_SIGBUS)
439 goto do_sigbus;
440 BUG();
441 }
442 if (fault & VM_FAULT_MAJOR)
443 tsk->maj_flt++;
444 else
445 tsk->min_flt++;
446
Chris Metcalf0707ad32010-06-25 17:04:17 -0400447#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
Chris Metcalf867e3592010-05-28 23:09:12 -0400448 /*
449 * If this was an asynchronous fault,
450 * restart the appropriate engine.
451 */
452 switch (fault_num) {
453#if CHIP_HAS_TILE_DMA()
454 case INT_DMATLB_MISS:
455 case INT_DMATLB_MISS_DWNCL:
456 case INT_DMATLB_ACCESS:
457 case INT_DMATLB_ACCESS_DWNCL:
458 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
459 break;
460#endif
461#if CHIP_HAS_SN_PROC()
462 case INT_SNITLB_MISS:
463 case INT_SNITLB_MISS_DWNCL:
464 __insn_mtspr(SPR_SNCTL,
465 __insn_mfspr(SPR_SNCTL) &
466 ~SPR_SNCTL__FRZPROC_MASK);
467 break;
468#endif
469 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400470#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400471
472 up_read(&mm->mmap_sem);
473 return 1;
474
475/*
476 * Something tried to access memory that isn't in our memory map..
477 * Fix it, but check if it's kernel or user first..
478 */
479bad_area:
480 up_read(&mm->mmap_sem);
481
482bad_area_nosemaphore:
483 /* User mode accesses just cause a SIGSEGV */
484 if (!is_kernel_mode) {
485 /*
486 * It's possible to have interrupts off here.
487 */
488 local_irq_enable();
489
Chris Metcalf571d76a2011-05-16 14:23:44 -0400490 force_sig_info_fault("segfault", SIGSEGV, si_code, address,
491 fault_num, tsk, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400492 return 0;
493 }
494
495no_context:
496 /* Are we prepared to handle this kernel fault? */
497 if (fixup_exception(regs))
498 return 0;
499
500/*
501 * Oops. The kernel tried to access some bad page. We'll have to
502 * terminate things with extreme prejudice.
503 */
504
505 bust_spinlocks(1);
506
507 /* FIXME: no lookup_address() yet */
508#ifdef SUPPORT_LOOKUP_ADDRESS
509 if (fault_num == INT_ITLB_MISS) {
510 pte_t *pte = lookup_address(address);
511
512 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400513 pr_crit("kernel tried to execute"
Chris Metcalf867e3592010-05-28 23:09:12 -0400514 " non-executable page - exploit attempt?"
515 " (uid: %d)\n", current->uid);
516 }
517#endif
518 if (address < PAGE_SIZE)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400519 pr_alert("Unable to handle kernel NULL pointer dereference\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400520 else
Chris Metcalf0707ad32010-06-25 17:04:17 -0400521 pr_alert("Unable to handle kernel paging request\n");
522 pr_alert(" at virtual address "REGFMT", pc "REGFMT"\n",
523 address, regs->pc);
Chris Metcalf867e3592010-05-28 23:09:12 -0400524
525 show_regs(regs);
526
527 if (unlikely(tsk->pid < 2)) {
528 panic("Kernel page fault running %s!",
Paul E. McKenneya95f8812011-11-10 16:16:13 -0800529 is_idle_task(tsk) ? "the idle task" : "init");
Chris Metcalf867e3592010-05-28 23:09:12 -0400530 }
531
532 /*
533 * More FIXME: we should probably copy the i386 here and
534 * implement a generic die() routine. Not today.
535 */
536#ifdef SUPPORT_DIE
537 die("Oops", regs);
538#endif
539 bust_spinlocks(1);
540
541 do_group_exit(SIGKILL);
542
543/*
544 * We ran out of memory, or some other thing happened to us that made
545 * us unable to handle the page fault gracefully.
546 */
547out_of_memory:
548 up_read(&mm->mmap_sem);
549 if (is_global_init(tsk)) {
550 yield();
551 down_read(&mm->mmap_sem);
552 goto survive;
553 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400554 pr_alert("VM: killing process %s\n", tsk->comm);
Chris Metcalf867e3592010-05-28 23:09:12 -0400555 if (!is_kernel_mode)
556 do_group_exit(SIGKILL);
557 goto no_context;
558
559do_sigbus:
560 up_read(&mm->mmap_sem);
561
562 /* Kernel mode? Handle exceptions or die */
563 if (is_kernel_mode)
564 goto no_context;
565
Chris Metcalf571d76a2011-05-16 14:23:44 -0400566 force_sig_info_fault("bus error", SIGBUS, BUS_ADRERR, address,
567 fault_num, tsk, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400568 return 0;
569}
570
571#ifndef __tilegx__
572
Chris Metcalf867e3592010-05-28 23:09:12 -0400573/* We must release ICS before panicking or we won't get anywhere. */
574#define ics_panic(fmt, ...) do { \
575 __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0); \
576 panic(fmt, __VA_ARGS__); \
577} while (0)
578
Chris Metcalf867e3592010-05-28 23:09:12 -0400579/*
580 * When we take an ITLB or DTLB fault or access violation in the
581 * supervisor while the critical section bit is set, the hypervisor is
Chris Metcalfa78c9422010-10-14 16:23:03 -0400582 * reluctant to write new values into the EX_CONTEXT_K_x registers,
Chris Metcalf867e3592010-05-28 23:09:12 -0400583 * since that might indicate we have not yet squirreled the SPR
584 * contents away and can thus safely take a recursive interrupt.
Chris Metcalfa78c9422010-10-14 16:23:03 -0400585 * Accordingly, the hypervisor passes us the PC via SYSTEM_SAVE_K_2.
Chris Metcalfc745a8a2010-08-13 08:52:19 -0400586 *
587 * Note that this routine is called before homecache_tlb_defer_enter(),
588 * which means that we can properly unlock any atomics that might
589 * be used there (good), but also means we must be very sensitive
590 * to not touch any data structures that might be located in memory
591 * that could migrate, as we could be entering the kernel on a dataplane
592 * cpu that has been deferring kernel TLB updates. This means, for
593 * example, that we can't migrate init_mm or its pgd.
Chris Metcalf867e3592010-05-28 23:09:12 -0400594 */
595struct intvec_state do_page_fault_ics(struct pt_regs *regs, int fault_num,
596 unsigned long address,
597 unsigned long info)
598{
599 unsigned long pc = info & ~1;
600 int write = info & 1;
601 pgd_t *pgd = get_current_pgd();
602
603 /* Retval is 1 at first since we will handle the fault fully. */
604 struct intvec_state state = {
605 do_page_fault, fault_num, address, write, 1
606 };
607
608 /* Validate that we are plausibly in the right routine. */
609 if ((pc & 0x7) != 0 || pc < PAGE_OFFSET ||
610 (fault_num != INT_DTLB_MISS &&
611 fault_num != INT_DTLB_ACCESS)) {
612 unsigned long old_pc = regs->pc;
613 regs->pc = pc;
614 ics_panic("Bad ICS page fault args:"
615 " old PC %#lx, fault %d/%d at %#lx\n",
616 old_pc, fault_num, write, address);
617 }
618
619 /* We might be faulting on a vmalloc page, so check that first. */
620 if (fault_num != INT_DTLB_ACCESS && vmalloc_fault(pgd, address) >= 0)
621 return state;
622
623 /*
624 * If we faulted with ICS set in sys_cmpxchg, we are providing
625 * a user syscall service that should generate a signal on
626 * fault. We didn't set up a kernel stack on initial entry to
627 * sys_cmpxchg, but instead had one set up by the fault, which
628 * (because sys_cmpxchg never releases ICS) came to us via the
Chris Metcalfa78c9422010-10-14 16:23:03 -0400629 * SYSTEM_SAVE_K_2 mechanism, and thus EX_CONTEXT_K_[01] are
Chris Metcalf867e3592010-05-28 23:09:12 -0400630 * still referencing the original user code. We release the
631 * atomic lock and rewrite pt_regs so that it appears that we
632 * came from user-space directly, and after we finish the
633 * fault we'll go back to user space and re-issue the swint.
634 * This way the backtrace information is correct if we need to
635 * emit a stack dump at any point while handling this.
636 *
637 * Must match register use in sys_cmpxchg().
638 */
639 if (pc >= (unsigned long) sys_cmpxchg &&
640 pc < (unsigned long) __sys_cmpxchg_end) {
641#ifdef CONFIG_SMP
642 /* Don't unlock before we could have locked. */
643 if (pc >= (unsigned long)__sys_cmpxchg_grab_lock) {
644 int *lock_ptr = (int *)(regs->regs[ATOMIC_LOCK_REG]);
645 __atomic_fault_unlock(lock_ptr);
646 }
647#endif
648 regs->sp = regs->regs[27];
649 }
650
651 /*
652 * We can also fault in the atomic assembly, in which
653 * case we use the exception table to do the first-level fixup.
654 * We may re-fixup again in the real fault handler if it
655 * turns out the faulting address is just bad, and not,
656 * for example, migrating.
657 */
658 else if (pc >= (unsigned long) __start_atomic_asm_code &&
659 pc < (unsigned long) __end_atomic_asm_code) {
660 const struct exception_table_entry *fixup;
661#ifdef CONFIG_SMP
662 /* Unlock the atomic lock. */
663 int *lock_ptr = (int *)(regs->regs[ATOMIC_LOCK_REG]);
664 __atomic_fault_unlock(lock_ptr);
665#endif
666 fixup = search_exception_tables(pc);
667 if (!fixup)
668 ics_panic("ICS atomic fault not in table:"
669 " PC %#lx, fault %d", pc, fault_num);
670 regs->pc = fixup->fixup;
671 regs->ex1 = PL_ICS_EX1(KERNEL_PL, 0);
672 }
673
674 /*
Chris Metcalf867e3592010-05-28 23:09:12 -0400675 * Now that we have released the atomic lock (if necessary),
676 * it's safe to spin if the PTE that caused the fault was migrating.
677 */
678 if (fault_num == INT_DTLB_ACCESS)
679 write = 1;
Chris Metcalf48292732012-03-29 15:34:52 -0400680 if (handle_migrating_pte(pgd, fault_num, address, pc, 1, write))
Chris Metcalf867e3592010-05-28 23:09:12 -0400681 return state;
682
683 /* Return zero so that we continue on with normal fault handling. */
684 state.retval = 0;
685 return state;
686}
687
688#endif /* !__tilegx__ */
689
690/*
691 * This routine handles page faults. It determines the address, and the
692 * problem, and then passes it handle_page_fault() for normal DTLB and
693 * ITLB issues, and for DMA or SN processor faults when we are in user
694 * space. For the latter, if we're in kernel mode, we just save the
695 * interrupt away appropriately and return immediately. We can't do
696 * page faults for user code while in kernel mode.
697 */
698void do_page_fault(struct pt_regs *regs, int fault_num,
699 unsigned long address, unsigned long write)
700{
701 int is_page_fault;
702
703 /* This case should have been handled by do_page_fault_ics(). */
704 BUG_ON(write & ~1);
705
706#if CHIP_HAS_TILE_DMA()
707 /*
708 * If it's a DMA fault, suspend the transfer while we're
709 * handling the miss; we'll restart after it's handled. If we
710 * don't suspend, it's possible that this process could swap
711 * out and back in, and restart the engine since the DMA is
712 * still 'running'.
713 */
714 if (fault_num == INT_DMATLB_MISS ||
715 fault_num == INT_DMATLB_ACCESS ||
716 fault_num == INT_DMATLB_MISS_DWNCL ||
717 fault_num == INT_DMATLB_ACCESS_DWNCL) {
718 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__SUSPEND_MASK);
719 while (__insn_mfspr(SPR_DMA_USER_STATUS) &
720 SPR_DMA_STATUS__BUSY_MASK)
721 ;
722 }
723#endif
724
725 /* Validate fault num and decide if this is a first-time page fault. */
726 switch (fault_num) {
727 case INT_ITLB_MISS:
728 case INT_DTLB_MISS:
729#if CHIP_HAS_TILE_DMA()
730 case INT_DMATLB_MISS:
731 case INT_DMATLB_MISS_DWNCL:
732#endif
733#if CHIP_HAS_SN_PROC()
734 case INT_SNITLB_MISS:
735 case INT_SNITLB_MISS_DWNCL:
736#endif
737 is_page_fault = 1;
738 break;
739
740 case INT_DTLB_ACCESS:
741#if CHIP_HAS_TILE_DMA()
742 case INT_DMATLB_ACCESS:
743 case INT_DMATLB_ACCESS_DWNCL:
744#endif
745 is_page_fault = 0;
746 break;
747
748 default:
749 panic("Bad fault number %d in do_page_fault", fault_num);
750 }
751
Chris Metcalf313ce672011-05-02 14:50:06 -0400752#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
Chris Metcalf867e3592010-05-28 23:09:12 -0400753 if (EX1_PL(regs->ex1) != USER_PL) {
754 struct async_tlb *async;
755 switch (fault_num) {
756#if CHIP_HAS_TILE_DMA()
757 case INT_DMATLB_MISS:
758 case INT_DMATLB_ACCESS:
759 case INT_DMATLB_MISS_DWNCL:
760 case INT_DMATLB_ACCESS_DWNCL:
761 async = &current->thread.dma_async_tlb;
762 break;
763#endif
764#if CHIP_HAS_SN_PROC()
765 case INT_SNITLB_MISS:
766 case INT_SNITLB_MISS_DWNCL:
767 async = &current->thread.sn_async_tlb;
768 break;
769#endif
770 default:
771 async = NULL;
772 }
773 if (async) {
774
775 /*
776 * No vmalloc check required, so we can allow
777 * interrupts immediately at this point.
778 */
779 local_irq_enable();
780
781 set_thread_flag(TIF_ASYNC_TLB);
782 if (async->fault_num != 0) {
783 panic("Second async fault %d;"
784 " old fault was %d (%#lx/%ld)",
785 fault_num, async->fault_num,
786 address, write);
787 }
788 BUG_ON(fault_num == 0);
789 async->fault_num = fault_num;
790 async->is_fault = is_page_fault;
791 async->is_write = write;
792 async->address = address;
793 return;
794 }
795 }
Chris Metcalf313ce672011-05-02 14:50:06 -0400796#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400797
798 handle_page_fault(regs, fault_num, is_page_fault, address, write);
799}
800
801
802#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
803/*
804 * Check an async_tlb structure to see if a deferred fault is waiting,
805 * and if so pass it to the page-fault code.
806 */
807static void handle_async_page_fault(struct pt_regs *regs,
808 struct async_tlb *async)
809{
810 if (async->fault_num) {
811 /*
812 * Clear async->fault_num before calling the page-fault
813 * handler so that if we re-interrupt before returning
814 * from the function we have somewhere to put the
815 * information from the new interrupt.
816 */
817 int fault_num = async->fault_num;
818 async->fault_num = 0;
819 handle_page_fault(regs, fault_num, async->is_fault,
820 async->address, async->is_write);
821 }
822}
Chris Metcalf867e3592010-05-28 23:09:12 -0400823
824/*
825 * This routine effectively re-issues asynchronous page faults
826 * when we are returning to user space.
827 */
828void do_async_page_fault(struct pt_regs *regs)
829{
830 /*
831 * Clear thread flag early. If we re-interrupt while processing
832 * code here, we will reset it and recall this routine before
833 * returning to user space.
834 */
835 clear_thread_flag(TIF_ASYNC_TLB);
836
837#if CHIP_HAS_TILE_DMA()
838 handle_async_page_fault(regs, &current->thread.dma_async_tlb);
839#endif
840#if CHIP_HAS_SN_PROC()
841 handle_async_page_fault(regs, &current->thread.sn_async_tlb);
842#endif
843}
Chris Metcalf313ce672011-05-02 14:50:06 -0400844#endif /* CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC() */
845
Chris Metcalf867e3592010-05-28 23:09:12 -0400846
847void vmalloc_sync_all(void)
848{
849#ifdef __tilegx__
850 /* Currently all L1 kernel pmd's are static and shared. */
851 BUG_ON(pgd_index(VMALLOC_END) != pgd_index(VMALLOC_START));
852#else
853 /*
854 * Note that races in the updates of insync and start aren't
855 * problematic: insync can only get set bits added, and updates to
856 * start are only improving performance (without affecting correctness
857 * if undone).
858 */
859 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
860 static unsigned long start = PAGE_OFFSET;
861 unsigned long address;
862
863 BUILD_BUG_ON(PAGE_OFFSET & ~PGDIR_MASK);
864 for (address = start; address >= PAGE_OFFSET; address += PGDIR_SIZE) {
865 if (!test_bit(pgd_index(address), insync)) {
866 unsigned long flags;
867 struct list_head *pos;
868
869 spin_lock_irqsave(&pgd_lock, flags);
870 list_for_each(pos, &pgd_list)
871 if (!vmalloc_sync_one(list_to_pgd(pos),
872 address)) {
873 /* Must be at first entry in list. */
874 BUG_ON(pos != pgd_list.next);
875 break;
876 }
877 spin_unlock_irqrestore(&pgd_lock, flags);
878 if (pos != pgd_list.next)
879 set_bit(pgd_index(address), insync);
880 }
881 if (address == start && test_bit(pgd_index(address), insync))
882 start = address + PGDIR_SIZE;
883 }
884#endif
885}