fault.c 24 KB

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