fault.c 24 KB

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