fault.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/system.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/hardirq.h>
  29. #include <asm/cpu-regs.h>
  30. #include <asm/debugger.h>
  31. #include <asm/gdb-stub.h>
  32. /*
  33. * Unlock any spinlocks which will prevent us from getting the
  34. * message out
  35. */
  36. void bust_spinlocks(int yes)
  37. {
  38. if (yes) {
  39. oops_in_progress = 1;
  40. } else {
  41. int loglevel_save = console_loglevel;
  42. #ifdef CONFIG_VT
  43. unblank_screen();
  44. #endif
  45. oops_in_progress = 0;
  46. /*
  47. * OK, the message is on the console. Now we call printk()
  48. * without oops_in_progress set so that printk will give klogd
  49. * a poke. Hold onto your hats...
  50. */
  51. console_loglevel = 15; /* NMI oopser may have shut the console
  52. * up */
  53. printk(" ");
  54. console_loglevel = loglevel_save;
  55. }
  56. }
  57. void do_BUG(const char *file, int line)
  58. {
  59. bust_spinlocks(1);
  60. printk(KERN_EMERG "------------[ cut here ]------------\n");
  61. printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
  62. }
  63. #if 0
  64. static void print_pagetable_entries(pgd_t *pgdir, unsigned long address)
  65. {
  66. pgd_t *pgd;
  67. pmd_t *pmd;
  68. pte_t *pte;
  69. pgd = pgdir + __pgd_offset(address);
  70. printk(KERN_DEBUG "pgd entry %p: %016Lx\n",
  71. pgd, (long long) pgd_val(*pgd));
  72. if (!pgd_present(*pgd)) {
  73. printk(KERN_DEBUG "... pgd not present!\n");
  74. return;
  75. }
  76. pmd = pmd_offset(pgd, address);
  77. printk(KERN_DEBUG "pmd entry %p: %016Lx\n",
  78. pmd, (long long)pmd_val(*pmd));
  79. if (!pmd_present(*pmd)) {
  80. printk(KERN_DEBUG "... pmd not present!\n");
  81. return;
  82. }
  83. pte = pte_offset(pmd, address);
  84. printk(KERN_DEBUG "pte entry %p: %016Lx\n",
  85. pte, (long long) pte_val(*pte));
  86. if (!pte_present(*pte))
  87. printk(KERN_DEBUG "... pte not present!\n");
  88. }
  89. #endif
  90. /*
  91. * This routine handles page faults. It determines the address,
  92. * and the problem, and then passes it off to one of the appropriate
  93. * routines.
  94. *
  95. * fault_code:
  96. * - LSW: either MMUFCR_IFC or MMUFCR_DFC as appropriate
  97. * - MSW: 0 if data access, 1 if instruction access
  98. * - bit 0: TLB miss flag
  99. * - bit 1: initial write
  100. * - bit 2: page invalid
  101. * - bit 3: protection violation
  102. * - bit 4: accessor (0=user 1=kernel)
  103. * - bit 5: 0=read 1=write
  104. * - bit 6-8: page protection spec
  105. * - bit 9: illegal address
  106. * - bit 16: 0=data 1=ins
  107. *
  108. */
  109. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long fault_code,
  110. unsigned long address)
  111. {
  112. struct vm_area_struct *vma;
  113. struct task_struct *tsk;
  114. struct mm_struct *mm;
  115. unsigned long page;
  116. siginfo_t info;
  117. int write, fault;
  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 (in_atomic() || !mm)
  156. goto no_context;
  157. down_read(&mm->mmap_sem);
  158. vma = find_vma(mm, address);
  159. if (!vma)
  160. goto bad_area;
  161. if (vma->vm_start <= address)
  162. goto good_area;
  163. if (!(vma->vm_flags & VM_GROWSDOWN))
  164. goto bad_area;
  165. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  166. /* accessing the stack below the stack pointer is always a
  167. * bug */
  168. if ((address & PAGE_MASK) + 2 * PAGE_SIZE < regs->sp) {
  169. #if 0
  170. printk(KERN_WARNING
  171. "[%d] ### Access below stack @%lx (sp=%lx)\n",
  172. current->pid, address, regs->sp);
  173. printk(KERN_WARNING
  174. "vma [%08x - %08x]\n",
  175. vma->vm_start, vma->vm_end);
  176. show_registers(regs);
  177. printk(KERN_WARNING
  178. "[%d] ### Code: [%08lx]"
  179. " %02x %02x %02x %02x %02x %02x %02x %02x\n",
  180. current->pid,
  181. regs->pc,
  182. ((u8 *) regs->pc)[0],
  183. ((u8 *) regs->pc)[1],
  184. ((u8 *) regs->pc)[2],
  185. ((u8 *) regs->pc)[3],
  186. ((u8 *) regs->pc)[4],
  187. ((u8 *) regs->pc)[5],
  188. ((u8 *) regs->pc)[6],
  189. ((u8 *) regs->pc)[7]
  190. );
  191. #endif
  192. goto bad_area;
  193. }
  194. }
  195. if (expand_stack(vma, address))
  196. goto bad_area;
  197. /*
  198. * Ok, we have a good vm_area for this memory access, so
  199. * we can handle it..
  200. */
  201. good_area:
  202. info.si_code = SEGV_ACCERR;
  203. write = 0;
  204. switch (fault_code & (MMUFCR_xFC_PGINVAL|MMUFCR_xFC_TYPE)) {
  205. default: /* 3: write, present */
  206. case MMUFCR_xFC_TYPE_WRITE:
  207. #ifdef TEST_VERIFY_AREA
  208. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
  209. printk(KERN_DEBUG "WP fault at %08lx\n", regs->pc);
  210. #endif
  211. /* write to absent page */
  212. case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_WRITE:
  213. if (!(vma->vm_flags & VM_WRITE))
  214. goto bad_area;
  215. write++;
  216. break;
  217. /* read from protected page */
  218. case MMUFCR_xFC_TYPE_READ:
  219. goto bad_area;
  220. /* read from absent page present */
  221. case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_READ:
  222. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  223. goto bad_area;
  224. break;
  225. }
  226. /*
  227. * If for any reason at all we couldn't handle the fault,
  228. * make sure we exit gracefully rather than endlessly redo
  229. * the fault.
  230. */
  231. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  232. if (unlikely(fault & VM_FAULT_ERROR)) {
  233. if (fault & VM_FAULT_OOM)
  234. goto out_of_memory;
  235. else if (fault & VM_FAULT_SIGBUS)
  236. goto do_sigbus;
  237. BUG();
  238. }
  239. if (fault & VM_FAULT_MAJOR)
  240. current->maj_flt++;
  241. else
  242. current->min_flt++;
  243. up_read(&mm->mmap_sem);
  244. return;
  245. /*
  246. * Something tried to access memory that isn't in our memory map..
  247. * Fix it, but check if it's kernel or user first..
  248. */
  249. bad_area:
  250. up_read(&mm->mmap_sem);
  251. /* User mode accesses just cause a SIGSEGV */
  252. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
  253. info.si_signo = SIGSEGV;
  254. info.si_errno = 0;
  255. /* info.si_code has been set above */
  256. info.si_addr = (void *)address;
  257. force_sig_info(SIGSEGV, &info, tsk);
  258. return;
  259. }
  260. no_context:
  261. /* Are we prepared to handle this kernel fault? */
  262. if (fixup_exception(regs))
  263. return;
  264. /*
  265. * Oops. The kernel tried to access some bad page. We'll have to
  266. * terminate things with extreme prejudice.
  267. */
  268. bust_spinlocks(1);
  269. if (address < PAGE_SIZE)
  270. printk(KERN_ALERT
  271. "Unable to handle kernel NULL pointer dereference");
  272. else
  273. printk(KERN_ALERT
  274. "Unable to handle kernel paging request");
  275. printk(" at virtual address %08lx\n", address);
  276. printk(" printing pc:\n");
  277. printk(KERN_ALERT "%08lx\n", regs->pc);
  278. debugger_intercept(fault_code & 0x00010000 ? EXCEP_IAERROR : EXCEP_DAERROR,
  279. SIGSEGV, SEGV_ACCERR, regs);
  280. page = PTBR;
  281. page = ((unsigned long *) __va(page))[address >> 22];
  282. printk(KERN_ALERT "*pde = %08lx\n", page);
  283. if (page & 1) {
  284. page &= PAGE_MASK;
  285. address &= 0x003ff000;
  286. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  287. printk(KERN_ALERT "*pte = %08lx\n", page);
  288. }
  289. die("Oops", regs, fault_code);
  290. do_exit(SIGKILL);
  291. /*
  292. * We ran out of memory, or some other thing happened to us that made
  293. * us unable to handle the page fault gracefully.
  294. */
  295. out_of_memory:
  296. up_read(&mm->mmap_sem);
  297. printk(KERN_ALERT "VM: killing process %s\n", tsk->comm);
  298. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
  299. do_exit(SIGKILL);
  300. goto no_context;
  301. do_sigbus:
  302. up_read(&mm->mmap_sem);
  303. /*
  304. * Send a sigbus, regardless of whether we were in kernel
  305. * or user mode.
  306. */
  307. info.si_signo = SIGBUS;
  308. info.si_errno = 0;
  309. info.si_code = BUS_ADRERR;
  310. info.si_addr = (void *)address;
  311. force_sig_info(SIGBUS, &info, tsk);
  312. /* Kernel mode? Handle exceptions or die */
  313. if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
  314. goto no_context;
  315. return;
  316. vmalloc_fault:
  317. {
  318. /*
  319. * Synchronize this task's top level page-table
  320. * with the 'reference' page table.
  321. *
  322. * Do _not_ use "tsk" here. We might be inside
  323. * an interrupt in the middle of a task switch..
  324. */
  325. int index = pgd_index(address);
  326. pgd_t *pgd, *pgd_k;
  327. pud_t *pud, *pud_k;
  328. pmd_t *pmd, *pmd_k;
  329. pte_t *pte_k;
  330. pgd_k = init_mm.pgd + index;
  331. if (!pgd_present(*pgd_k))
  332. goto no_context;
  333. pud_k = pud_offset(pgd_k, address);
  334. if (!pud_present(*pud_k))
  335. goto no_context;
  336. pmd_k = pmd_offset(pud_k, address);
  337. if (!pmd_present(*pmd_k))
  338. goto no_context;
  339. pgd = (pgd_t *) PTBR + index;
  340. pud = pud_offset(pgd, address);
  341. pmd = pmd_offset(pud, address);
  342. set_pmd(pmd, *pmd_k);
  343. pte_k = pte_offset_kernel(pmd_k, address);
  344. if (!pte_present(*pte_k))
  345. goto no_context;
  346. return;
  347. }
  348. }