blob: 7863298dad4df9c5e7978fd02c41141c9ebe4ff7 [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 Metcalf6b14e412012-10-23 13:30:54 -040073SYSCALL_DEFINE1(cmpxchg_badaddr, unsigned long, address)
Chris Metcalf867e3592010-05-28 23:09:12 -040074{
Chris Metcalf6b14e412012-10-23 13:30:54 -040075 struct pt_regs *regs = current_pt_regs();
76
Chris Metcalf867e3592010-05-28 23:09:12 -040077 if (address >= PAGE_OFFSET)
Chris Metcalf571d76a2011-05-16 14:23:44 -040078 force_sig_info_fault("atomic segfault", SIGSEGV, SEGV_MAPERR,
79 address, INT_DTLB_MISS, current, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -040080 else
Chris Metcalf571d76a2011-05-16 14:23:44 -040081 force_sig_info_fault("atomic alignment fault", SIGBUS,
82 BUS_ADRALN, address,
83 INT_UNALIGN_DATA, current, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -040084
85 /*
86 * Adjust pc to point at the actual instruction, which is unusual
87 * for syscalls normally, but is appropriate when we are claiming
88 * that a syscall swint1 caused a page fault or bus error.
89 */
90 regs->pc -= 8;
91
92 /*
93 * Mark this as a caller-save interrupt, like a normal page fault,
94 * so that when we go through the signal handler path we will
95 * properly restore r0, r1, and r2 for the signal handler arguments.
96 */
97 regs->flags |= PT_FLAGS_CALLER_SAVES;
98
99 return 0;
100}
101#endif
102
103static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
104{
105 unsigned index = pgd_index(address);
106 pgd_t *pgd_k;
107 pud_t *pud, *pud_k;
108 pmd_t *pmd, *pmd_k;
109
110 pgd += index;
111 pgd_k = init_mm.pgd + index;
112
113 if (!pgd_present(*pgd_k))
114 return NULL;
115
116 pud = pud_offset(pgd, address);
117 pud_k = pud_offset(pgd_k, address);
118 if (!pud_present(*pud_k))
119 return NULL;
120
121 pmd = pmd_offset(pud, address);
122 pmd_k = pmd_offset(pud_k, address);
123 if (!pmd_present(*pmd_k))
124 return NULL;
125 if (!pmd_present(*pmd)) {
126 set_pmd(pmd, *pmd_k);
127 arch_flush_lazy_mmu_mode();
128 } else
129 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
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400283 flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
284 (write ? FAULT_FLAG_WRITE : 0));
285
Chris Metcalf867e3592010-05-28 23:09:12 -0400286 is_kernel_mode = (EX1_PL(regs->ex1) != USER_PL);
287
288 tsk = validate_current();
289
290 /*
291 * Check to see if we might be overwriting the stack, and bail
292 * out if so. The page fault code is a relatively likely
293 * place to get trapped in an infinite regress, and once we
294 * overwrite the whole stack, it becomes very hard to recover.
295 */
296 stack_offset = stack_pointer & (THREAD_SIZE-1);
297 if (stack_offset < THREAD_SIZE / 8) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400298 pr_alert("Potential stack overrun: sp %#lx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400299 stack_pointer);
300 show_regs(regs);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400301 pr_alert("Killing current process %d/%s\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400302 tsk->pid, tsk->comm);
303 do_group_exit(SIGKILL);
304 }
305
306 /*
307 * Early on, we need to check for migrating PTE entries;
308 * see homecache.c. If we find a migrating PTE, we wait until
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300309 * the backing page claims to be done migrating, then we proceed.
Chris Metcalf867e3592010-05-28 23:09:12 -0400310 * For kernel PTEs, we rewrite the PTE and return and retry.
311 * Otherwise, we treat the fault like a normal "no PTE" fault,
312 * rather than trying to patch up the existing PTE.
313 */
314 pgd = get_current_pgd();
Chris Metcalf48292732012-03-29 15:34:52 -0400315 if (handle_migrating_pte(pgd, fault_num, address, regs->pc,
Chris Metcalf867e3592010-05-28 23:09:12 -0400316 is_kernel_mode, write))
317 return 1;
318
319 si_code = SEGV_MAPERR;
320
321 /*
322 * We fault-in kernel-space virtual memory on-demand. The
323 * 'reference' page table is init_mm.pgd.
324 *
325 * NOTE! We MUST NOT take any locks for this case. We may
326 * be in an interrupt or a critical region, and should
327 * only copy the information from the master page table,
328 * nothing more.
329 *
330 * This verifies that the fault happens in kernel space
331 * and that the fault was not a protection fault.
332 */
333 if (unlikely(address >= TASK_SIZE &&
334 !is_arch_mappable_range(address, 0))) {
335 if (is_kernel_mode && is_page_fault &&
336 vmalloc_fault(pgd, address) >= 0)
337 return 1;
338 /*
339 * Don't take the mm semaphore here. If we fixup a prefetch
340 * fault we could otherwise deadlock.
341 */
342 mm = NULL; /* happy compiler */
343 vma = NULL;
344 goto bad_area_nosemaphore;
345 }
346
347 /*
348 * If we're trying to touch user-space addresses, we must
349 * be either at PL0, or else with interrupts enabled in the
Chris Metcalfb230ff22012-03-29 15:40:50 -0400350 * kernel, so either way we can re-enable interrupts here
351 * unless we are doing atomic access to user space with
352 * interrupts disabled.
Chris Metcalf867e3592010-05-28 23:09:12 -0400353 */
Chris Metcalfb230ff22012-03-29 15:40:50 -0400354 if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
355 local_irq_enable();
Chris Metcalf867e3592010-05-28 23:09:12 -0400356
357 mm = tsk->mm;
358
359 /*
360 * If we're in an interrupt, have no user context or are running in an
361 * atomic region then we must not take the fault.
362 */
363 if (in_atomic() || !mm) {
364 vma = NULL; /* happy compiler */
365 goto bad_area_nosemaphore;
366 }
367
368 /*
369 * When running in the kernel we expect faults to occur only to
370 * addresses in user space. All other faults represent errors in the
371 * kernel and should generate an OOPS. Unfortunately, in the case of an
372 * erroneous fault occurring in a code path which already holds mmap_sem
373 * we will deadlock attempting to validate the fault against the
374 * address space. Luckily the kernel only validly references user
375 * space from well defined areas of code, which are listed in the
376 * exceptions table.
377 *
378 * As the vast majority of faults will be valid we will only perform
379 * the source reference check when there is a possibility of a deadlock.
380 * Attempt to lock the address space, if we cannot we then validate the
381 * source. If this is invalid we can skip the address space check,
382 * thus avoiding the deadlock.
383 */
384 if (!down_read_trylock(&mm->mmap_sem)) {
385 if (is_kernel_mode &&
386 !search_exception_tables(regs->pc)) {
387 vma = NULL; /* happy compiler */
388 goto bad_area_nosemaphore;
389 }
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400390
391retry:
Chris Metcalf867e3592010-05-28 23:09:12 -0400392 down_read(&mm->mmap_sem);
393 }
394
395 vma = find_vma(mm, address);
396 if (!vma)
397 goto bad_area;
398 if (vma->vm_start <= address)
399 goto good_area;
400 if (!(vma->vm_flags & VM_GROWSDOWN))
401 goto bad_area;
402 if (regs->sp < PAGE_OFFSET) {
403 /*
404 * accessing the stack below sp is always a bug.
405 */
406 if (address < regs->sp)
407 goto bad_area;
408 }
409 if (expand_stack(vma, address))
410 goto bad_area;
411
412/*
413 * Ok, we have a good vm_area for this memory access, so
414 * we can handle it..
415 */
416good_area:
417 si_code = SEGV_ACCERR;
418 if (fault_num == INT_ITLB_MISS) {
419 if (!(vma->vm_flags & VM_EXEC))
420 goto bad_area;
421 } else if (write) {
422#ifdef TEST_VERIFY_AREA
423 if (!is_page_fault && regs->cs == KERNEL_CS)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400424 pr_err("WP fault at "REGFMT"\n", regs->eip);
Chris Metcalf867e3592010-05-28 23:09:12 -0400425#endif
426 if (!(vma->vm_flags & VM_WRITE))
427 goto bad_area;
428 } else {
429 if (!is_page_fault || !(vma->vm_flags & VM_READ))
430 goto bad_area;
431 }
432
433 survive:
434 /*
435 * If for any reason at all we couldn't handle the fault,
436 * make sure we exit gracefully rather than endlessly redo
437 * the fault.
438 */
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400439 fault = handle_mm_fault(mm, vma, address, flags);
440
441 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
442 return 0;
443
Chris Metcalf867e3592010-05-28 23:09:12 -0400444 if (unlikely(fault & VM_FAULT_ERROR)) {
445 if (fault & VM_FAULT_OOM)
446 goto out_of_memory;
447 else if (fault & VM_FAULT_SIGBUS)
448 goto do_sigbus;
449 BUG();
450 }
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400451 if (flags & FAULT_FLAG_ALLOW_RETRY) {
452 if (fault & VM_FAULT_MAJOR)
453 tsk->maj_flt++;
454 else
455 tsk->min_flt++;
456 if (fault & VM_FAULT_RETRY) {
457 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -0700458 flags |= FAULT_FLAG_TRIED;
Kautuk Consul4ce6bea2012-03-31 08:05:39 -0400459
460 /*
461 * No need to up_read(&mm->mmap_sem) as we would
462 * have already released it in __lock_page_or_retry
463 * in mm/filemap.c.
464 */
465 goto retry;
466 }
467 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400468
Chris Metcalf0707ad32010-06-25 17:04:17 -0400469#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
Chris Metcalf867e3592010-05-28 23:09:12 -0400470 /*
471 * If this was an asynchronous fault,
472 * restart the appropriate engine.
473 */
474 switch (fault_num) {
475#if CHIP_HAS_TILE_DMA()
476 case INT_DMATLB_MISS:
477 case INT_DMATLB_MISS_DWNCL:
478 case INT_DMATLB_ACCESS:
479 case INT_DMATLB_ACCESS_DWNCL:
480 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
481 break;
482#endif
483#if CHIP_HAS_SN_PROC()
484 case INT_SNITLB_MISS:
485 case INT_SNITLB_MISS_DWNCL:
486 __insn_mtspr(SPR_SNCTL,
487 __insn_mfspr(SPR_SNCTL) &
488 ~SPR_SNCTL__FRZPROC_MASK);
489 break;
490#endif
491 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400492#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400493
494 up_read(&mm->mmap_sem);
495 return 1;
496
497/*
498 * Something tried to access memory that isn't in our memory map..
499 * Fix it, but check if it's kernel or user first..
500 */
501bad_area:
502 up_read(&mm->mmap_sem);
503
504bad_area_nosemaphore:
505 /* User mode accesses just cause a SIGSEGV */
506 if (!is_kernel_mode) {
507 /*
508 * It's possible to have interrupts off here.
509 */
510 local_irq_enable();
511
Chris Metcalf571d76a2011-05-16 14:23:44 -0400512 force_sig_info_fault("segfault", SIGSEGV, si_code, address,
513 fault_num, tsk, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400514 return 0;
515 }
516
517no_context:
518 /* Are we prepared to handle this kernel fault? */
519 if (fixup_exception(regs))
520 return 0;
521
522/*
523 * Oops. The kernel tried to access some bad page. We'll have to
524 * terminate things with extreme prejudice.
525 */
526
527 bust_spinlocks(1);
528
529 /* FIXME: no lookup_address() yet */
530#ifdef SUPPORT_LOOKUP_ADDRESS
531 if (fault_num == INT_ITLB_MISS) {
532 pte_t *pte = lookup_address(address);
533
534 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400535 pr_crit("kernel tried to execute"
Chris Metcalf867e3592010-05-28 23:09:12 -0400536 " non-executable page - exploit attempt?"
537 " (uid: %d)\n", current->uid);
538 }
539#endif
540 if (address < PAGE_SIZE)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400541 pr_alert("Unable to handle kernel NULL pointer dereference\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400542 else
Chris Metcalf0707ad32010-06-25 17:04:17 -0400543 pr_alert("Unable to handle kernel paging request\n");
544 pr_alert(" at virtual address "REGFMT", pc "REGFMT"\n",
545 address, regs->pc);
Chris Metcalf867e3592010-05-28 23:09:12 -0400546
547 show_regs(regs);
548
549 if (unlikely(tsk->pid < 2)) {
550 panic("Kernel page fault running %s!",
Paul E. McKenneya95f8812011-11-10 16:16:13 -0800551 is_idle_task(tsk) ? "the idle task" : "init");
Chris Metcalf867e3592010-05-28 23:09:12 -0400552 }
553
554 /*
555 * More FIXME: we should probably copy the i386 here and
556 * implement a generic die() routine. Not today.
557 */
558#ifdef SUPPORT_DIE
559 die("Oops", regs);
560#endif
561 bust_spinlocks(1);
562
563 do_group_exit(SIGKILL);
564
565/*
566 * We ran out of memory, or some other thing happened to us that made
567 * us unable to handle the page fault gracefully.
568 */
569out_of_memory:
570 up_read(&mm->mmap_sem);
571 if (is_global_init(tsk)) {
572 yield();
573 down_read(&mm->mmap_sem);
574 goto survive;
575 }
Johannes Weiner609838c2013-07-08 15:59:50 -0700576 if (is_kernel_mode)
577 goto no_context;
578 pagefault_out_of_memory();
579 return 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400580
581do_sigbus:
582 up_read(&mm->mmap_sem);
583
584 /* Kernel mode? Handle exceptions or die */
585 if (is_kernel_mode)
586 goto no_context;
587
Chris Metcalf571d76a2011-05-16 14:23:44 -0400588 force_sig_info_fault("bus error", SIGBUS, BUS_ADRERR, address,
589 fault_num, tsk, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400590 return 0;
591}
592
593#ifndef __tilegx__
594
Chris Metcalf867e3592010-05-28 23:09:12 -0400595/* We must release ICS before panicking or we won't get anywhere. */
596#define ics_panic(fmt, ...) do { \
597 __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0); \
598 panic(fmt, __VA_ARGS__); \
599} while (0)
600
Chris Metcalf867e3592010-05-28 23:09:12 -0400601/*
602 * When we take an ITLB or DTLB fault or access violation in the
603 * supervisor while the critical section bit is set, the hypervisor is
Chris Metcalfa78c9422010-10-14 16:23:03 -0400604 * reluctant to write new values into the EX_CONTEXT_K_x registers,
Chris Metcalf867e3592010-05-28 23:09:12 -0400605 * since that might indicate we have not yet squirreled the SPR
606 * contents away and can thus safely take a recursive interrupt.
Chris Metcalfa78c9422010-10-14 16:23:03 -0400607 * Accordingly, the hypervisor passes us the PC via SYSTEM_SAVE_K_2.
Chris Metcalfc745a8a2010-08-13 08:52:19 -0400608 *
609 * Note that this routine is called before homecache_tlb_defer_enter(),
610 * which means that we can properly unlock any atomics that might
611 * be used there (good), but also means we must be very sensitive
612 * to not touch any data structures that might be located in memory
613 * that could migrate, as we could be entering the kernel on a dataplane
614 * cpu that has been deferring kernel TLB updates. This means, for
615 * example, that we can't migrate init_mm or its pgd.
Chris Metcalf867e3592010-05-28 23:09:12 -0400616 */
617struct intvec_state do_page_fault_ics(struct pt_regs *regs, int fault_num,
618 unsigned long address,
619 unsigned long info)
620{
621 unsigned long pc = info & ~1;
622 int write = info & 1;
623 pgd_t *pgd = get_current_pgd();
624
625 /* Retval is 1 at first since we will handle the fault fully. */
626 struct intvec_state state = {
627 do_page_fault, fault_num, address, write, 1
628 };
629
630 /* Validate that we are plausibly in the right routine. */
631 if ((pc & 0x7) != 0 || pc < PAGE_OFFSET ||
632 (fault_num != INT_DTLB_MISS &&
633 fault_num != INT_DTLB_ACCESS)) {
634 unsigned long old_pc = regs->pc;
635 regs->pc = pc;
636 ics_panic("Bad ICS page fault args:"
637 " old PC %#lx, fault %d/%d at %#lx\n",
638 old_pc, fault_num, write, address);
639 }
640
641 /* We might be faulting on a vmalloc page, so check that first. */
642 if (fault_num != INT_DTLB_ACCESS && vmalloc_fault(pgd, address) >= 0)
643 return state;
644
645 /*
646 * If we faulted with ICS set in sys_cmpxchg, we are providing
647 * a user syscall service that should generate a signal on
648 * fault. We didn't set up a kernel stack on initial entry to
649 * sys_cmpxchg, but instead had one set up by the fault, which
650 * (because sys_cmpxchg never releases ICS) came to us via the
Chris Metcalfa78c9422010-10-14 16:23:03 -0400651 * SYSTEM_SAVE_K_2 mechanism, and thus EX_CONTEXT_K_[01] are
Chris Metcalf867e3592010-05-28 23:09:12 -0400652 * still referencing the original user code. We release the
653 * atomic lock and rewrite pt_regs so that it appears that we
654 * came from user-space directly, and after we finish the
655 * fault we'll go back to user space and re-issue the swint.
656 * This way the backtrace information is correct if we need to
657 * emit a stack dump at any point while handling this.
658 *
659 * Must match register use in sys_cmpxchg().
660 */
661 if (pc >= (unsigned long) sys_cmpxchg &&
662 pc < (unsigned long) __sys_cmpxchg_end) {
663#ifdef CONFIG_SMP
664 /* Don't unlock before we could have locked. */
665 if (pc >= (unsigned long)__sys_cmpxchg_grab_lock) {
666 int *lock_ptr = (int *)(regs->regs[ATOMIC_LOCK_REG]);
667 __atomic_fault_unlock(lock_ptr);
668 }
669#endif
670 regs->sp = regs->regs[27];
671 }
672
673 /*
674 * We can also fault in the atomic assembly, in which
675 * case we use the exception table to do the first-level fixup.
676 * We may re-fixup again in the real fault handler if it
677 * turns out the faulting address is just bad, and not,
678 * for example, migrating.
679 */
680 else if (pc >= (unsigned long) __start_atomic_asm_code &&
681 pc < (unsigned long) __end_atomic_asm_code) {
682 const struct exception_table_entry *fixup;
683#ifdef CONFIG_SMP
684 /* Unlock the atomic lock. */
685 int *lock_ptr = (int *)(regs->regs[ATOMIC_LOCK_REG]);
686 __atomic_fault_unlock(lock_ptr);
687#endif
688 fixup = search_exception_tables(pc);
689 if (!fixup)
690 ics_panic("ICS atomic fault not in table:"
691 " PC %#lx, fault %d", pc, fault_num);
692 regs->pc = fixup->fixup;
693 regs->ex1 = PL_ICS_EX1(KERNEL_PL, 0);
694 }
695
696 /*
Chris Metcalf867e3592010-05-28 23:09:12 -0400697 * Now that we have released the atomic lock (if necessary),
698 * it's safe to spin if the PTE that caused the fault was migrating.
699 */
700 if (fault_num == INT_DTLB_ACCESS)
701 write = 1;
Chris Metcalf48292732012-03-29 15:34:52 -0400702 if (handle_migrating_pte(pgd, fault_num, address, pc, 1, write))
Chris Metcalf867e3592010-05-28 23:09:12 -0400703 return state;
704
705 /* Return zero so that we continue on with normal fault handling. */
706 state.retval = 0;
707 return state;
708}
709
710#endif /* !__tilegx__ */
711
712/*
713 * This routine handles page faults. It determines the address, and the
714 * problem, and then passes it handle_page_fault() for normal DTLB and
715 * ITLB issues, and for DMA or SN processor faults when we are in user
716 * space. For the latter, if we're in kernel mode, we just save the
717 * interrupt away appropriately and return immediately. We can't do
718 * page faults for user code while in kernel mode.
719 */
720void do_page_fault(struct pt_regs *regs, int fault_num,
721 unsigned long address, unsigned long write)
722{
723 int is_page_fault;
724
Chris Metcalf2f9ac292013-08-06 16:04:13 -0400725#ifdef __tilegx__
726 /*
727 * We don't need early do_page_fault_ics() support, since unlike
728 * Pro we don't need to worry about unlocking the atomic locks.
729 * There is only one current case in GX where we touch any memory
730 * under ICS other than our own kernel stack, and we handle that
731 * here. (If we crash due to trying to touch our own stack,
732 * we're in too much trouble for C code to help out anyway.)
733 */
734 if (write & ~1) {
735 unsigned long pc = write & ~1;
736 if (pc >= (unsigned long) __start_unalign_asm_code &&
737 pc < (unsigned long) __end_unalign_asm_code) {
738 struct thread_info *ti = current_thread_info();
739 /*
740 * Our EX_CONTEXT is still what it was from the
741 * initial unalign exception, but now we've faulted
742 * on the JIT page. We would like to complete the
743 * page fault however is appropriate, and then retry
744 * the instruction that caused the unalign exception.
745 * Our state has been "corrupted" by setting the low
746 * bit in "sp", and stashing r0..r3 in the
747 * thread_info area, so we revert all of that, then
748 * continue as if this were a normal page fault.
749 */
750 regs->sp &= ~1UL;
751 regs->regs[0] = ti->unalign_jit_tmp[0];
752 regs->regs[1] = ti->unalign_jit_tmp[1];
753 regs->regs[2] = ti->unalign_jit_tmp[2];
754 regs->regs[3] = ti->unalign_jit_tmp[3];
755 write &= 1;
756 } else {
757 pr_alert("%s/%d: ICS set at page fault at %#lx: %#lx\n",
758 current->comm, current->pid, pc, address);
759 show_regs(regs);
760 do_group_exit(SIGKILL);
761 return;
762 }
763 }
764#else
Chris Metcalf867e3592010-05-28 23:09:12 -0400765 /* This case should have been handled by do_page_fault_ics(). */
766 BUG_ON(write & ~1);
Chris Metcalf2f9ac292013-08-06 16:04:13 -0400767#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400768
769#if CHIP_HAS_TILE_DMA()
770 /*
771 * If it's a DMA fault, suspend the transfer while we're
772 * handling the miss; we'll restart after it's handled. If we
773 * don't suspend, it's possible that this process could swap
774 * out and back in, and restart the engine since the DMA is
775 * still 'running'.
776 */
777 if (fault_num == INT_DMATLB_MISS ||
778 fault_num == INT_DMATLB_ACCESS ||
779 fault_num == INT_DMATLB_MISS_DWNCL ||
780 fault_num == INT_DMATLB_ACCESS_DWNCL) {
781 __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__SUSPEND_MASK);
782 while (__insn_mfspr(SPR_DMA_USER_STATUS) &
783 SPR_DMA_STATUS__BUSY_MASK)
784 ;
785 }
786#endif
787
788 /* Validate fault num and decide if this is a first-time page fault. */
789 switch (fault_num) {
790 case INT_ITLB_MISS:
791 case INT_DTLB_MISS:
792#if CHIP_HAS_TILE_DMA()
793 case INT_DMATLB_MISS:
794 case INT_DMATLB_MISS_DWNCL:
795#endif
796#if CHIP_HAS_SN_PROC()
797 case INT_SNITLB_MISS:
798 case INT_SNITLB_MISS_DWNCL:
799#endif
800 is_page_fault = 1;
801 break;
802
803 case INT_DTLB_ACCESS:
804#if CHIP_HAS_TILE_DMA()
805 case INT_DMATLB_ACCESS:
806 case INT_DMATLB_ACCESS_DWNCL:
807#endif
808 is_page_fault = 0;
809 break;
810
811 default:
812 panic("Bad fault number %d in do_page_fault", fault_num);
813 }
814
Chris Metcalf313ce672011-05-02 14:50:06 -0400815#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
Chris Metcalf867e3592010-05-28 23:09:12 -0400816 if (EX1_PL(regs->ex1) != USER_PL) {
817 struct async_tlb *async;
818 switch (fault_num) {
819#if CHIP_HAS_TILE_DMA()
820 case INT_DMATLB_MISS:
821 case INT_DMATLB_ACCESS:
822 case INT_DMATLB_MISS_DWNCL:
823 case INT_DMATLB_ACCESS_DWNCL:
824 async = &current->thread.dma_async_tlb;
825 break;
826#endif
827#if CHIP_HAS_SN_PROC()
828 case INT_SNITLB_MISS:
829 case INT_SNITLB_MISS_DWNCL:
830 async = &current->thread.sn_async_tlb;
831 break;
832#endif
833 default:
834 async = NULL;
835 }
836 if (async) {
837
838 /*
839 * No vmalloc check required, so we can allow
840 * interrupts immediately at this point.
841 */
842 local_irq_enable();
843
844 set_thread_flag(TIF_ASYNC_TLB);
845 if (async->fault_num != 0) {
846 panic("Second async fault %d;"
847 " old fault was %d (%#lx/%ld)",
848 fault_num, async->fault_num,
849 address, write);
850 }
851 BUG_ON(fault_num == 0);
852 async->fault_num = fault_num;
853 async->is_fault = is_page_fault;
854 async->is_write = write;
855 async->address = address;
856 return;
857 }
858 }
Chris Metcalf313ce672011-05-02 14:50:06 -0400859#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400860
861 handle_page_fault(regs, fault_num, is_page_fault, address, write);
862}
863
864
865#if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
866/*
867 * Check an async_tlb structure to see if a deferred fault is waiting,
868 * and if so pass it to the page-fault code.
869 */
870static void handle_async_page_fault(struct pt_regs *regs,
871 struct async_tlb *async)
872{
873 if (async->fault_num) {
874 /*
875 * Clear async->fault_num before calling the page-fault
876 * handler so that if we re-interrupt before returning
877 * from the function we have somewhere to put the
878 * information from the new interrupt.
879 */
880 int fault_num = async->fault_num;
881 async->fault_num = 0;
882 handle_page_fault(regs, fault_num, async->is_fault,
883 async->address, async->is_write);
884 }
885}
Chris Metcalf867e3592010-05-28 23:09:12 -0400886
887/*
888 * This routine effectively re-issues asynchronous page faults
889 * when we are returning to user space.
890 */
891void do_async_page_fault(struct pt_regs *regs)
892{
893 /*
894 * Clear thread flag early. If we re-interrupt while processing
895 * code here, we will reset it and recall this routine before
896 * returning to user space.
897 */
898 clear_thread_flag(TIF_ASYNC_TLB);
899
900#if CHIP_HAS_TILE_DMA()
901 handle_async_page_fault(regs, &current->thread.dma_async_tlb);
902#endif
903#if CHIP_HAS_SN_PROC()
904 handle_async_page_fault(regs, &current->thread.sn_async_tlb);
905#endif
906}
Chris Metcalf313ce672011-05-02 14:50:06 -0400907#endif /* CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC() */
908
Chris Metcalf867e3592010-05-28 23:09:12 -0400909
910void vmalloc_sync_all(void)
911{
912#ifdef __tilegx__
913 /* Currently all L1 kernel pmd's are static and shared. */
Chris Metcalfe5f7bd42013-08-06 14:34:08 -0400914 BUILD_BUG_ON(pgd_index(VMALLOC_END - PAGE_SIZE) !=
915 pgd_index(VMALLOC_START));
Chris Metcalf867e3592010-05-28 23:09:12 -0400916#else
917 /*
918 * Note that races in the updates of insync and start aren't
919 * problematic: insync can only get set bits added, and updates to
920 * start are only improving performance (without affecting correctness
921 * if undone).
922 */
923 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
924 static unsigned long start = PAGE_OFFSET;
925 unsigned long address;
926
927 BUILD_BUG_ON(PAGE_OFFSET & ~PGDIR_MASK);
928 for (address = start; address >= PAGE_OFFSET; address += PGDIR_SIZE) {
929 if (!test_bit(pgd_index(address), insync)) {
930 unsigned long flags;
931 struct list_head *pos;
932
933 spin_lock_irqsave(&pgd_lock, flags);
934 list_for_each(pos, &pgd_list)
935 if (!vmalloc_sync_one(list_to_pgd(pos),
936 address)) {
937 /* Must be at first entry in list. */
938 BUG_ON(pos != pgd_list.next);
939 break;
940 }
941 spin_unlock_irqrestore(&pgd_lock, flags);
942 if (pos != pgd_list.next)
943 set_bit(pgd_index(address), insync);
944 }
945 if (address == start && test_bit(pgd_index(address), insync))
946 start = address + PGDIR_SIZE;
947 }
948#endif
949}