fault.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/extable.h>
  4. #include <linux/module.h>
  5. #include <linux/signal.h>
  6. #include <linux/ptrace.h>
  7. #include <linux/mm.h>
  8. #include <linux/init.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/uaccess.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/tlbflush.h>
  13. extern void die(const char *str, struct pt_regs *regs, long err);
  14. /*
  15. * This is useful to dump out the page tables associated with
  16. * 'addr' in mm 'mm'.
  17. */
  18. void show_pte(struct mm_struct *mm, unsigned long addr)
  19. {
  20. pgd_t *pgd;
  21. if (!mm)
  22. mm = &init_mm;
  23. pr_alert("pgd = %p\n", mm->pgd);
  24. pgd = pgd_offset(mm, addr);
  25. pr_alert("[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
  26. do {
  27. pmd_t *pmd;
  28. if (pgd_none(*pgd))
  29. break;
  30. if (pgd_bad(*pgd)) {
  31. pr_alert("(bad)");
  32. break;
  33. }
  34. pmd = pmd_offset(pgd, addr);
  35. #if PTRS_PER_PMD != 1
  36. pr_alert(", *pmd=%08lx", pmd_val(*pmd));
  37. #endif
  38. if (pmd_none(*pmd))
  39. break;
  40. if (pmd_bad(*pmd)) {
  41. pr_alert("(bad)");
  42. break;
  43. }
  44. if (IS_ENABLED(CONFIG_HIGHMEM))
  45. {
  46. pte_t *pte;
  47. /* We must not map this if we have highmem enabled */
  48. pte = pte_offset_map(pmd, addr);
  49. pr_alert(", *pte=%08lx", pte_val(*pte));
  50. pte_unmap(pte);
  51. }
  52. } while (0);
  53. pr_alert("\n");
  54. }
  55. void do_page_fault(unsigned long entry, unsigned long addr,
  56. unsigned int error_code, struct pt_regs *regs)
  57. {
  58. struct task_struct *tsk;
  59. struct mm_struct *mm;
  60. struct vm_area_struct *vma;
  61. int si_code;
  62. vm_fault_t fault;
  63. unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
  64. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  65. error_code = error_code & (ITYPE_mskINST | ITYPE_mskETYPE);
  66. tsk = current;
  67. mm = tsk->mm;
  68. si_code = SEGV_MAPERR;
  69. /*
  70. * We fault-in kernel-space virtual memory on-demand. The
  71. * 'reference' page table is init_mm.pgd.
  72. *
  73. * NOTE! We MUST NOT take any locks for this case. We may
  74. * be in an interrupt or a critical region, and should
  75. * only copy the information from the master page table,
  76. * nothing more.
  77. */
  78. if (addr >= TASK_SIZE) {
  79. if (user_mode(regs))
  80. goto bad_area_nosemaphore;
  81. if (addr >= TASK_SIZE && addr < VMALLOC_END
  82. && (entry == ENTRY_PTE_NOT_PRESENT))
  83. goto vmalloc_fault;
  84. else
  85. goto no_context;
  86. }
  87. /* Send a signal to the task for handling the unalignment access. */
  88. if (entry == ENTRY_GENERAL_EXCPETION
  89. && error_code == ETYPE_ALIGNMENT_CHECK) {
  90. if (user_mode(regs))
  91. goto bad_area_nosemaphore;
  92. else
  93. goto no_context;
  94. }
  95. /*
  96. * If we're in an interrupt or have no user
  97. * context, we must not take the fault..
  98. */
  99. if (unlikely(faulthandler_disabled() || !mm))
  100. goto no_context;
  101. /*
  102. * As per x86, we may deadlock here. However, since the kernel only
  103. * validly references user space from well defined areas of the code,
  104. * we can bug out early if this is from code which shouldn't.
  105. */
  106. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  107. if (!user_mode(regs) &&
  108. !search_exception_tables(instruction_pointer(regs)))
  109. goto no_context;
  110. retry:
  111. down_read(&mm->mmap_sem);
  112. } else {
  113. /*
  114. * The above down_read_trylock() might have succeeded in which
  115. * case, we'll have missed the might_sleep() from down_read().
  116. */
  117. might_sleep();
  118. if (IS_ENABLED(CONFIG_DEBUG_VM)) {
  119. if (!user_mode(regs) &&
  120. !search_exception_tables(instruction_pointer(regs)))
  121. goto no_context;
  122. }
  123. }
  124. vma = find_vma(mm, addr);
  125. if (unlikely(!vma))
  126. goto bad_area;
  127. if (vma->vm_start <= addr)
  128. goto good_area;
  129. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
  130. goto bad_area;
  131. if (unlikely(expand_stack(vma, addr)))
  132. goto bad_area;
  133. /*
  134. * Ok, we have a good vm_area for this memory access, so
  135. * we can handle it..
  136. */
  137. good_area:
  138. si_code = SEGV_ACCERR;
  139. /* first do some preliminary protection checks */
  140. if (entry == ENTRY_PTE_NOT_PRESENT) {
  141. if (error_code & ITYPE_mskINST)
  142. mask = VM_EXEC;
  143. else {
  144. mask = VM_READ | VM_WRITE;
  145. if (vma->vm_flags & VM_WRITE)
  146. flags |= FAULT_FLAG_WRITE;
  147. }
  148. } else if (entry == ENTRY_TLB_MISC) {
  149. switch (error_code & ITYPE_mskETYPE) {
  150. case RD_PROT:
  151. mask = VM_READ;
  152. break;
  153. case WRT_PROT:
  154. mask = VM_WRITE;
  155. flags |= FAULT_FLAG_WRITE;
  156. break;
  157. case NOEXEC:
  158. mask = VM_EXEC;
  159. break;
  160. case PAGE_MODIFY:
  161. mask = VM_WRITE;
  162. flags |= FAULT_FLAG_WRITE;
  163. break;
  164. case ACC_BIT:
  165. BUG();
  166. default:
  167. break;
  168. }
  169. }
  170. if (!(vma->vm_flags & mask))
  171. goto bad_area;
  172. /*
  173. * If for any reason at all we couldn't handle the fault,
  174. * make sure we exit gracefully rather than endlessly redo
  175. * the fault.
  176. */
  177. fault = handle_mm_fault(vma, addr, flags);
  178. /*
  179. * If we need to retry but a fatal signal is pending, handle the
  180. * signal first. We do not need to release the mmap_sem because it
  181. * would already be released in __lock_page_or_retry in mm/filemap.c.
  182. */
  183. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  184. if (!user_mode(regs))
  185. goto no_context;
  186. return;
  187. }
  188. if (unlikely(fault & VM_FAULT_ERROR)) {
  189. if (fault & VM_FAULT_OOM)
  190. goto out_of_memory;
  191. else if (fault & VM_FAULT_SIGBUS)
  192. goto do_sigbus;
  193. else
  194. goto bad_area;
  195. }
  196. /*
  197. * Major/minor page fault accounting is only done on the initial
  198. * attempt. If we go through a retry, it is extremely likely that the
  199. * page will be found in page cache at that point.
  200. */
  201. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  202. if (fault & VM_FAULT_MAJOR)
  203. tsk->maj_flt++;
  204. else
  205. tsk->min_flt++;
  206. if (fault & VM_FAULT_RETRY) {
  207. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  208. flags |= FAULT_FLAG_TRIED;
  209. /* No need to up_read(&mm->mmap_sem) as we would
  210. * have already released it in __lock_page_or_retry
  211. * in mm/filemap.c.
  212. */
  213. goto retry;
  214. }
  215. }
  216. up_read(&mm->mmap_sem);
  217. return;
  218. /*
  219. * Something tried to access memory that isn't in our memory map..
  220. * Fix it, but check if it's kernel or user first..
  221. */
  222. bad_area:
  223. up_read(&mm->mmap_sem);
  224. bad_area_nosemaphore:
  225. /* User mode accesses just cause a SIGSEGV */
  226. if (user_mode(regs)) {
  227. tsk->thread.address = addr;
  228. tsk->thread.error_code = error_code;
  229. tsk->thread.trap_no = entry;
  230. force_sig_fault(SIGSEGV, si_code, (void __user *)addr, tsk);
  231. return;
  232. }
  233. no_context:
  234. /* Are we prepared to handle this kernel fault?
  235. *
  236. * (The kernel has valid exception-points in the source
  237. * when it acesses user-memory. When it fails in one
  238. * of those points, we find it in a table and do a jump
  239. * to some fixup code that loads an appropriate error
  240. * code)
  241. */
  242. {
  243. const struct exception_table_entry *entry;
  244. if ((entry =
  245. search_exception_tables(instruction_pointer(regs))) !=
  246. NULL) {
  247. /* Adjust the instruction pointer in the stackframe */
  248. instruction_pointer(regs) = entry->fixup;
  249. return;
  250. }
  251. }
  252. /*
  253. * Oops. The kernel tried to access some bad page. We'll have to
  254. * terminate things with extreme prejudice.
  255. */
  256. bust_spinlocks(1);
  257. pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
  258. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  259. "paging request", addr);
  260. show_pte(mm, addr);
  261. die("Oops", regs, error_code);
  262. bust_spinlocks(0);
  263. do_exit(SIGKILL);
  264. return;
  265. /*
  266. * We ran out of memory, or some other thing happened to us that made
  267. * us unable to handle the page fault gracefully.
  268. */
  269. out_of_memory:
  270. up_read(&mm->mmap_sem);
  271. if (!user_mode(regs))
  272. goto no_context;
  273. pagefault_out_of_memory();
  274. return;
  275. do_sigbus:
  276. up_read(&mm->mmap_sem);
  277. /* Kernel mode? Handle exceptions or die */
  278. if (!user_mode(regs))
  279. goto no_context;
  280. /*
  281. * Send a sigbus
  282. */
  283. tsk->thread.address = addr;
  284. tsk->thread.error_code = error_code;
  285. tsk->thread.trap_no = entry;
  286. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)addr, tsk);
  287. return;
  288. vmalloc_fault:
  289. {
  290. /*
  291. * Synchronize this task's top level page-table
  292. * with the 'reference' page table.
  293. *
  294. * Use current_pgd instead of tsk->active_mm->pgd
  295. * since the latter might be unavailable if this
  296. * code is executed in a misfortunately run irq
  297. * (like inside schedule() between switch_mm and
  298. * switch_to...).
  299. */
  300. unsigned int index = pgd_index(addr);
  301. pgd_t *pgd, *pgd_k;
  302. pud_t *pud, *pud_k;
  303. pmd_t *pmd, *pmd_k;
  304. pte_t *pte_k;
  305. pgd = (pgd_t *) __va(__nds32__mfsr(NDS32_SR_L1_PPTB)) + index;
  306. pgd_k = init_mm.pgd + index;
  307. if (!pgd_present(*pgd_k))
  308. goto no_context;
  309. pud = pud_offset(pgd, addr);
  310. pud_k = pud_offset(pgd_k, addr);
  311. if (!pud_present(*pud_k))
  312. goto no_context;
  313. pmd = pmd_offset(pud, addr);
  314. pmd_k = pmd_offset(pud_k, addr);
  315. if (!pmd_present(*pmd_k))
  316. goto no_context;
  317. if (!pmd_present(*pmd))
  318. set_pmd(pmd, *pmd_k);
  319. else
  320. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  321. /*
  322. * Since the vmalloc area is global, we don't
  323. * need to copy individual PTE's, it is enough to
  324. * copy the pgd pointer into the pte page of the
  325. * root task. If that is there, we'll find our pte if
  326. * it exists.
  327. */
  328. /* Make sure the actual PTE exists as well to
  329. * catch kernel vmalloc-area accesses to non-mapped
  330. * addres. If we don't do this, this will just
  331. * silently loop forever.
  332. */
  333. pte_k = pte_offset_kernel(pmd_k, addr);
  334. if (!pte_present(*pte_k))
  335. goto no_context;
  336. return;
  337. }
  338. }