fault.c 26 KB

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