fault.c 9.9 KB

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