fault.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 <asm/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 write, fault;
  117. #ifdef CONFIG_GDBSTUB
  118. /* handle GDB stub causing a fault */
  119. if (gdbstub_busy) {
  120. gdbstub_exception(regs, TBR & TBR_INT_CODE);
  121. return;
  122. }
  123. #endif
  124. #if 0
  125. printk(KERN_DEBUG "--- do_page_fault(%p,%s:%04lx,%08lx)\n",
  126. regs,
  127. fault_code & 0x10000 ? "ins" : "data",
  128. fault_code & 0xffff, address);
  129. #endif
  130. tsk = current;
  131. /*
  132. * We fault-in kernel-space virtual memory on-demand. The
  133. * 'reference' page table is init_mm.pgd.
  134. *
  135. * NOTE! We MUST NOT take any locks for this case. We may
  136. * be in an interrupt or a critical region, and should
  137. * only copy the information from the master page table,
  138. * nothing more.
  139. *
  140. * This verifies that the fault happens in kernel space
  141. * and that the fault was a page not present (invalid) error
  142. */
  143. if (address >= VMALLOC_START && address < VMALLOC_END &&
  144. (fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR &&
  145. (fault_code & MMUFCR_xFC_PGINVAL) == MMUFCR_xFC_PGINVAL
  146. )
  147. goto vmalloc_fault;
  148. mm = tsk->mm;
  149. info.si_code = SEGV_MAPERR;
  150. /*
  151. * If we're in an interrupt or have no user
  152. * context, we must not take the fault..
  153. */
  154. if (in_atomic() || !mm)
  155. goto no_context;
  156. down_read(&mm->mmap_sem);
  157. vma = find_vma(mm, address);
  158. if (!vma)
  159. goto bad_area;
  160. if (vma->vm_start <= address)
  161. goto good_area;
  162. if (!(vma->vm_flags & VM_GROWSDOWN))
  163. goto bad_area;
  164. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  165. /* accessing the stack below the stack pointer is always a
  166. * bug */
  167. if ((address & PAGE_MASK) + 2 * PAGE_SIZE < regs->sp) {
  168. #if 0
  169. printk(KERN_WARNING
  170. "[%d] ### Access below stack @%lx (sp=%lx)\n",
  171. current->pid, address, regs->sp);
  172. printk(KERN_WARNING
  173. "vma [%08x - %08x]\n",
  174. vma->vm_start, vma->vm_end);
  175. show_registers(regs);
  176. printk(KERN_WARNING
  177. "[%d] ### Code: [%08lx]"
  178. " %02x %02x %02x %02x %02x %02x %02x %02x\n",
  179. current->pid,
  180. regs->pc,
  181. ((u8 *) regs->pc)[0],
  182. ((u8 *) regs->pc)[1],
  183. ((u8 *) regs->pc)[2],
  184. ((u8 *) regs->pc)[3],
  185. ((u8 *) regs->pc)[4],
  186. ((u8 *) regs->pc)[5],
  187. ((u8 *) regs->pc)[6],
  188. ((u8 *) regs->pc)[7]
  189. );
  190. #endif
  191. goto bad_area;
  192. }
  193. }
  194. if (expand_stack(vma, address))
  195. goto bad_area;
  196. /*
  197. * Ok, we have a good vm_area for this memory access, so
  198. * we can handle it..
  199. */
  200. good_area:
  201. info.si_code = SEGV_ACCERR;
  202. write = 0;
  203. switch (fault_code & (MMUFCR_xFC_PGINVAL|MMUFCR_xFC_TYPE)) {
  204. default: /* 3: write, present */
  205. case MMUFCR_xFC_TYPE_WRITE:
  206. #ifdef TEST_VERIFY_AREA
  207. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
  208. printk(KERN_DEBUG "WP fault at %08lx\n", regs->pc);
  209. #endif
  210. /* write to absent page */
  211. case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_WRITE:
  212. if (!(vma->vm_flags & VM_WRITE))
  213. goto bad_area;
  214. write++;
  215. break;
  216. /* read from protected page */
  217. case MMUFCR_xFC_TYPE_READ:
  218. goto bad_area;
  219. /* read from absent page present */
  220. case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_READ:
  221. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  222. goto bad_area;
  223. break;
  224. }
  225. /*
  226. * If for any reason at all we couldn't handle the fault,
  227. * make sure we exit gracefully rather than endlessly redo
  228. * the fault.
  229. */
  230. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  231. if (unlikely(fault & VM_FAULT_ERROR)) {
  232. if (fault & VM_FAULT_OOM)
  233. goto out_of_memory;
  234. else if (fault & VM_FAULT_SIGSEGV)
  235. goto bad_area;
  236. else if (fault & VM_FAULT_SIGBUS)
  237. goto do_sigbus;
  238. BUG();
  239. }
  240. if (fault & VM_FAULT_MAJOR)
  241. current->maj_flt++;
  242. else
  243. current->min_flt++;
  244. up_read(&mm->mmap_sem);
  245. return;
  246. /*
  247. * Something tried to access memory that isn't in our memory map..
  248. * Fix it, but check if it's kernel or user first..
  249. */
  250. bad_area:
  251. up_read(&mm->mmap_sem);
  252. /* User mode accesses just cause a SIGSEGV */
  253. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  254. info.si_signo = SIGSEGV;
  255. info.si_errno = 0;
  256. /* info.si_code has been set above */
  257. info.si_addr = (void *)address;
  258. force_sig_info(SIGSEGV, &info, tsk);
  259. return;
  260. }
  261. no_context:
  262. /* Are we prepared to handle this kernel fault? */
  263. if (fixup_exception(regs))
  264. return;
  265. /*
  266. * Oops. The kernel tried to access some bad page. We'll have to
  267. * terminate things with extreme prejudice.
  268. */
  269. bust_spinlocks(1);
  270. if (address < PAGE_SIZE)
  271. printk(KERN_ALERT
  272. "Unable to handle kernel NULL pointer dereference");
  273. else
  274. printk(KERN_ALERT
  275. "Unable to handle kernel paging request");
  276. printk(" at virtual address %08lx\n", address);
  277. printk(" printing pc:\n");
  278. printk(KERN_ALERT "%08lx\n", regs->pc);
  279. debugger_intercept(fault_code & 0x00010000 ? EXCEP_IAERROR : EXCEP_DAERROR,
  280. SIGSEGV, SEGV_ACCERR, regs);
  281. page = PTBR;
  282. page = ((unsigned long *) __va(page))[address >> 22];
  283. printk(KERN_ALERT "*pde = %08lx\n", page);
  284. if (page & 1) {
  285. page &= PAGE_MASK;
  286. address &= 0x003ff000;
  287. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  288. printk(KERN_ALERT "*pte = %08lx\n", page);
  289. }
  290. die("Oops", regs, fault_code);
  291. do_exit(SIGKILL);
  292. /*
  293. * We ran out of memory, or some other thing happened to us that made
  294. * us unable to handle the page fault gracefully.
  295. */
  296. out_of_memory:
  297. up_read(&mm->mmap_sem);
  298. printk(KERN_ALERT "VM: killing process %s\n", tsk->comm);
  299. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
  300. do_exit(SIGKILL);
  301. goto no_context;
  302. do_sigbus:
  303. up_read(&mm->mmap_sem);
  304. /*
  305. * Send a sigbus, regardless of whether we were in kernel
  306. * or user mode.
  307. */
  308. info.si_signo = SIGBUS;
  309. info.si_errno = 0;
  310. info.si_code = BUS_ADRERR;
  311. info.si_addr = (void *)address;
  312. force_sig_info(SIGBUS, &info, tsk);
  313. /* Kernel mode? Handle exceptions or die */
  314. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
  315. goto no_context;
  316. return;
  317. vmalloc_fault:
  318. {
  319. /*
  320. * Synchronize this task's top level page-table
  321. * with the 'reference' page table.
  322. *
  323. * Do _not_ use "tsk" here. We might be inside
  324. * an interrupt in the middle of a task switch..
  325. */
  326. int index = pgd_index(address);
  327. pgd_t *pgd, *pgd_k;
  328. pud_t *pud, *pud_k;
  329. pmd_t *pmd, *pmd_k;
  330. pte_t *pte_k;
  331. pgd_k = init_mm.pgd + index;
  332. if (!pgd_present(*pgd_k))
  333. goto no_context;
  334. pud_k = pud_offset(pgd_k, address);
  335. if (!pud_present(*pud_k))
  336. goto no_context;
  337. pmd_k = pmd_offset(pud_k, address);
  338. if (!pmd_present(*pmd_k))
  339. goto no_context;
  340. pgd = (pgd_t *) PTBR + index;
  341. pud = pud_offset(pgd, address);
  342. pmd = pmd_offset(pud, address);
  343. set_pmd(pmd, *pmd_k);
  344. pte_k = pte_offset_kernel(pmd_k, address);
  345. if (!pte_present(*pte_k))
  346. goto no_context;
  347. return;
  348. }
  349. }