fault.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* MN10300 MMU Fault handler
  2. *
  3. * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Modified by David Howells (dhowells@redhat.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. */
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/vt_kern.h> /* For unblank_screen() */
  25. #include <linux/uaccess.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/hardirq.h>
  28. #include <asm/cpu-regs.h>
  29. #include <asm/debugger.h>
  30. #include <asm/gdb-stub.h>
  31. /*
  32. * Unlock any spinlocks which will prevent us from getting the
  33. * message out
  34. */
  35. void bust_spinlocks(int yes)
  36. {
  37. if (yes) {
  38. oops_in_progress = 1;
  39. } else {
  40. int loglevel_save = console_loglevel;
  41. #ifdef CONFIG_VT
  42. unblank_screen();
  43. #endif
  44. oops_in_progress = 0;
  45. /*
  46. * OK, the message is on the console. Now we call printk()
  47. * without oops_in_progress set so that printk will give klogd
  48. * a poke. Hold onto your hats...
  49. */
  50. console_loglevel = 15; /* NMI oopser may have shut the console
  51. * up */
  52. printk(" ");
  53. console_loglevel = loglevel_save;
  54. }
  55. }
  56. void do_BUG(const char *file, int line)
  57. {
  58. bust_spinlocks(1);
  59. printk(KERN_EMERG "------------[ cut here ]------------\n");
  60. printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
  61. }
  62. #if 0
  63. static void print_pagetable_entries(pgd_t *pgdir, unsigned long address)
  64. {
  65. pgd_t *pgd;
  66. pmd_t *pmd;
  67. pte_t *pte;
  68. pgd = pgdir + __pgd_offset(address);
  69. printk(KERN_DEBUG "pgd entry %p: %016Lx\n",
  70. pgd, (long long) pgd_val(*pgd));
  71. if (!pgd_present(*pgd)) {
  72. printk(KERN_DEBUG "... pgd not present!\n");
  73. return;
  74. }
  75. pmd = pmd_offset(pgd, address);
  76. printk(KERN_DEBUG "pmd entry %p: %016Lx\n",
  77. pmd, (long long)pmd_val(*pmd));
  78. if (!pmd_present(*pmd)) {
  79. printk(KERN_DEBUG "... pmd not present!\n");
  80. return;
  81. }
  82. pte = pte_offset(pmd, address);
  83. printk(KERN_DEBUG "pte entry %p: %016Lx\n",
  84. pte, (long long) pte_val(*pte));
  85. if (!pte_present(*pte))
  86. printk(KERN_DEBUG "... pte not present!\n");
  87. }
  88. #endif
  89. /*
  90. * This routine handles page faults. It determines the address,
  91. * and the problem, and then passes it off to one of the appropriate
  92. * routines.
  93. *
  94. * fault_code:
  95. * - LSW: either MMUFCR_IFC or MMUFCR_DFC as appropriate
  96. * - MSW: 0 if data access, 1 if instruction access
  97. * - bit 0: TLB miss flag
  98. * - bit 1: initial write
  99. * - bit 2: page invalid
  100. * - bit 3: protection violation
  101. * - bit 4: accessor (0=user 1=kernel)
  102. * - bit 5: 0=read 1=write
  103. * - bit 6-8: page protection spec
  104. * - bit 9: illegal address
  105. * - bit 16: 0=data 1=ins
  106. *
  107. */
  108. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long fault_code,
  109. unsigned long address)
  110. {
  111. struct vm_area_struct *vma;
  112. struct task_struct *tsk;
  113. struct mm_struct *mm;
  114. unsigned long page;
  115. siginfo_t info;
  116. int fault;
  117. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  118. #ifdef CONFIG_GDBSTUB
  119. /* handle GDB stub causing a fault */
  120. if (gdbstub_busy) {
  121. gdbstub_exception(regs, TBR & TBR_INT_CODE);
  122. return;
  123. }
  124. #endif
  125. #if 0
  126. printk(KERN_DEBUG "--- do_page_fault(%p,%s:%04lx,%08lx)\n",
  127. regs,
  128. fault_code & 0x10000 ? "ins" : "data",
  129. fault_code & 0xffff, address);
  130. #endif
  131. tsk = current;
  132. /*
  133. * We fault-in kernel-space virtual memory on-demand. The
  134. * 'reference' page table is init_mm.pgd.
  135. *
  136. * NOTE! We MUST NOT take any locks for this case. We may
  137. * be in an interrupt or a critical region, and should
  138. * only copy the information from the master page table,
  139. * nothing more.
  140. *
  141. * This verifies that the fault happens in kernel space
  142. * and that the fault was a page not present (invalid) error
  143. */
  144. if (address >= VMALLOC_START && address < VMALLOC_END &&
  145. (fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR &&
  146. (fault_code & MMUFCR_xFC_PGINVAL) == MMUFCR_xFC_PGINVAL
  147. )
  148. goto vmalloc_fault;
  149. mm = tsk->mm;
  150. info.si_code = SEGV_MAPERR;
  151. /*
  152. * If we're in an interrupt or have no user
  153. * context, we must not take the fault..
  154. */
  155. if (faulthandler_disabled() || !mm)
  156. goto no_context;
  157. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
  158. flags |= FAULT_FLAG_USER;
  159. retry:
  160. down_read(&mm->mmap_sem);
  161. vma = find_vma(mm, address);
  162. if (!vma)
  163. goto bad_area;
  164. if (vma->vm_start <= address)
  165. goto good_area;
  166. if (!(vma->vm_flags & VM_GROWSDOWN))
  167. goto bad_area;
  168. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  169. /* accessing the stack below the stack pointer is always a
  170. * bug */
  171. if ((address & PAGE_MASK) + 2 * PAGE_SIZE < regs->sp) {
  172. #if 0
  173. printk(KERN_WARNING
  174. "[%d] ### Access below stack @%lx (sp=%lx)\n",
  175. current->pid, address, regs->sp);
  176. printk(KERN_WARNING
  177. "vma [%08x - %08x]\n",
  178. vma->vm_start, vma->vm_end);
  179. show_registers(regs);
  180. printk(KERN_WARNING
  181. "[%d] ### Code: [%08lx]"
  182. " %02x %02x %02x %02x %02x %02x %02x %02x\n",
  183. current->pid,
  184. regs->pc,
  185. ((u8 *) regs->pc)[0],
  186. ((u8 *) regs->pc)[1],
  187. ((u8 *) regs->pc)[2],
  188. ((u8 *) regs->pc)[3],
  189. ((u8 *) regs->pc)[4],
  190. ((u8 *) regs->pc)[5],
  191. ((u8 *) regs->pc)[6],
  192. ((u8 *) regs->pc)[7]
  193. );
  194. #endif
  195. goto bad_area;
  196. }
  197. }
  198. if (expand_stack(vma, address))
  199. goto bad_area;
  200. /*
  201. * Ok, we have a good vm_area for this memory access, so
  202. * we can handle it..
  203. */
  204. good_area:
  205. info.si_code = SEGV_ACCERR;
  206. switch (fault_code & (MMUFCR_xFC_PGINVAL|MMUFCR_xFC_TYPE)) {
  207. default: /* 3: write, present */
  208. case MMUFCR_xFC_TYPE_WRITE:
  209. #ifdef TEST_VERIFY_AREA
  210. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
  211. printk(KERN_DEBUG "WP fault at %08lx\n", regs->pc);
  212. #endif
  213. /* write to absent page */
  214. case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_WRITE:
  215. if (!(vma->vm_flags & VM_WRITE))
  216. goto bad_area;
  217. flags |= FAULT_FLAG_WRITE;
  218. break;
  219. /* read from protected page */
  220. case MMUFCR_xFC_TYPE_READ:
  221. goto bad_area;
  222. /* read from absent page present */
  223. case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_READ:
  224. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  225. goto bad_area;
  226. break;
  227. }
  228. /*
  229. * If for any reason at all we couldn't handle the fault,
  230. * make sure we exit gracefully rather than endlessly redo
  231. * the fault.
  232. */
  233. fault = handle_mm_fault(mm, vma, address, flags);
  234. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  235. return;
  236. if (unlikely(fault & VM_FAULT_ERROR)) {
  237. if (fault & VM_FAULT_OOM)
  238. goto out_of_memory;
  239. else if (fault & VM_FAULT_SIGSEGV)
  240. goto bad_area;
  241. else if (fault & VM_FAULT_SIGBUS)
  242. goto do_sigbus;
  243. BUG();
  244. }
  245. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  246. if (fault & VM_FAULT_MAJOR)
  247. current->maj_flt++;
  248. else
  249. current->min_flt++;
  250. if (fault & VM_FAULT_RETRY) {
  251. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  252. /* No need to up_read(&mm->mmap_sem) as we would
  253. * have already released it in __lock_page_or_retry
  254. * in mm/filemap.c.
  255. */
  256. goto retry;
  257. }
  258. }
  259. up_read(&mm->mmap_sem);
  260. return;
  261. /*
  262. * Something tried to access memory that isn't in our memory map..
  263. * Fix it, but check if it's kernel or user first..
  264. */
  265. bad_area:
  266. up_read(&mm->mmap_sem);
  267. /* User mode accesses just cause a SIGSEGV */
  268. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  269. info.si_signo = SIGSEGV;
  270. info.si_errno = 0;
  271. /* info.si_code has been set above */
  272. info.si_addr = (void *)address;
  273. force_sig_info(SIGSEGV, &info, tsk);
  274. return;
  275. }
  276. no_context:
  277. /* Are we prepared to handle this kernel fault? */
  278. if (fixup_exception(regs))
  279. return;
  280. /*
  281. * Oops. The kernel tried to access some bad page. We'll have to
  282. * terminate things with extreme prejudice.
  283. */
  284. bust_spinlocks(1);
  285. if (address < PAGE_SIZE)
  286. printk(KERN_ALERT
  287. "Unable to handle kernel NULL pointer dereference");
  288. else
  289. printk(KERN_ALERT
  290. "Unable to handle kernel paging request");
  291. printk(" at virtual address %08lx\n", address);
  292. printk(" printing pc:\n");
  293. printk(KERN_ALERT "%08lx\n", regs->pc);
  294. debugger_intercept(fault_code & 0x00010000 ? EXCEP_IAERROR : EXCEP_DAERROR,
  295. SIGSEGV, SEGV_ACCERR, regs);
  296. page = PTBR;
  297. page = ((unsigned long *) __va(page))[address >> 22];
  298. printk(KERN_ALERT "*pde = %08lx\n", page);
  299. if (page & 1) {
  300. page &= PAGE_MASK;
  301. address &= 0x003ff000;
  302. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  303. printk(KERN_ALERT "*pte = %08lx\n", page);
  304. }
  305. die("Oops", regs, fault_code);
  306. do_exit(SIGKILL);
  307. /*
  308. * We ran out of memory, or some other thing happened to us that made
  309. * us unable to handle the page fault gracefully.
  310. */
  311. out_of_memory:
  312. up_read(&mm->mmap_sem);
  313. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  314. pagefault_out_of_memory();
  315. return;
  316. }
  317. goto no_context;
  318. do_sigbus:
  319. up_read(&mm->mmap_sem);
  320. /*
  321. * Send a sigbus, regardless of whether we were in kernel
  322. * or user mode.
  323. */
  324. info.si_signo = SIGBUS;
  325. info.si_errno = 0;
  326. info.si_code = BUS_ADRERR;
  327. info.si_addr = (void *)address;
  328. force_sig_info(SIGBUS, &info, tsk);
  329. /* Kernel mode? Handle exceptions or die */
  330. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
  331. goto no_context;
  332. return;
  333. vmalloc_fault:
  334. {
  335. /*
  336. * Synchronize this task's top level page-table
  337. * with the 'reference' page table.
  338. *
  339. * Do _not_ use "tsk" here. We might be inside
  340. * an interrupt in the middle of a task switch..
  341. */
  342. int index = pgd_index(address);
  343. pgd_t *pgd, *pgd_k;
  344. pud_t *pud, *pud_k;
  345. pmd_t *pmd, *pmd_k;
  346. pte_t *pte_k;
  347. pgd_k = init_mm.pgd + index;
  348. if (!pgd_present(*pgd_k))
  349. goto no_context;
  350. pud_k = pud_offset(pgd_k, address);
  351. if (!pud_present(*pud_k))
  352. goto no_context;
  353. pmd_k = pmd_offset(pud_k, address);
  354. if (!pmd_present(*pmd_k))
  355. goto no_context;
  356. pgd = (pgd_t *) PTBR + index;
  357. pud = pud_offset(pgd, address);
  358. pmd = pmd_offset(pud, address);
  359. set_pmd(pmd, *pmd_k);
  360. pte_k = pte_offset_kernel(pmd_k, address);
  361. if (!pte_present(*pte_k))
  362. goto no_context;
  363. return;
  364. }
  365. }