fault.c 24 KB

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