fault.c 26 KB

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