fault.c 9.9 KB

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