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