fault.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Page fault handler for SH with an MMU.
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2003 Paul Mundt
  6. *
  7. * Based on linux/arch/i386/mm/fault.c:
  8. * Copyright (C) 1995 Linus Torvalds
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/hardirq.h>
  17. #include <linux/kprobes.h>
  18. #include <asm/system.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/kgdb.h>
  21. extern void die(const char *,struct pt_regs *,long);
  22. /*
  23. * This routine handles page faults. It determines the address,
  24. * and the problem, and then passes it off to one of the appropriate
  25. * routines.
  26. */
  27. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  28. unsigned long writeaccess,
  29. unsigned long address)
  30. {
  31. struct task_struct *tsk;
  32. struct mm_struct *mm;
  33. struct vm_area_struct * vma;
  34. unsigned long page;
  35. int si_code;
  36. siginfo_t info;
  37. #ifdef CONFIG_SH_KGDB
  38. if (kgdb_nofault && kgdb_bus_err_hook)
  39. kgdb_bus_err_hook();
  40. #endif
  41. tsk = current;
  42. mm = tsk->mm;
  43. si_code = SEGV_MAPERR;
  44. if (unlikely(address >= TASK_SIZE)) {
  45. /*
  46. * Synchronize this task's top level page-table
  47. * with the 'reference' page table.
  48. *
  49. * Do _not_ use "tsk" here. We might be inside
  50. * an interrupt in the middle of a task switch..
  51. */
  52. int offset = pgd_index(address);
  53. pgd_t *pgd, *pgd_k;
  54. pud_t *pud, *pud_k;
  55. pmd_t *pmd, *pmd_k;
  56. pgd = get_TTB() + offset;
  57. pgd_k = swapper_pg_dir + offset;
  58. /* This will never happen with the folded page table. */
  59. if (!pgd_present(*pgd)) {
  60. if (!pgd_present(*pgd_k))
  61. goto bad_area_nosemaphore;
  62. set_pgd(pgd, *pgd_k);
  63. return;
  64. }
  65. pud = pud_offset(pgd, address);
  66. pud_k = pud_offset(pgd_k, address);
  67. if (pud_present(*pud) || !pud_present(*pud_k))
  68. goto bad_area_nosemaphore;
  69. set_pud(pud, *pud_k);
  70. pmd = pmd_offset(pud, address);
  71. pmd_k = pmd_offset(pud_k, address);
  72. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  73. goto bad_area_nosemaphore;
  74. set_pmd(pmd, *pmd_k);
  75. return;
  76. }
  77. /*
  78. * If we're in an interrupt or have no user
  79. * context, we must not take the fault..
  80. */
  81. if (in_atomic() || !mm)
  82. goto no_context;
  83. down_read(&mm->mmap_sem);
  84. vma = find_vma(mm, address);
  85. if (!vma)
  86. goto bad_area;
  87. if (vma->vm_start <= address)
  88. goto good_area;
  89. if (!(vma->vm_flags & VM_GROWSDOWN))
  90. goto bad_area;
  91. if (expand_stack(vma, address))
  92. goto bad_area;
  93. /*
  94. * Ok, we have a good vm_area for this memory access, so
  95. * we can handle it..
  96. */
  97. good_area:
  98. si_code = SEGV_ACCERR;
  99. if (writeaccess) {
  100. if (!(vma->vm_flags & VM_WRITE))
  101. goto bad_area;
  102. } else {
  103. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  104. goto bad_area;
  105. }
  106. /*
  107. * If for any reason at all we couldn't handle the fault,
  108. * make sure we exit gracefully rather than endlessly redo
  109. * the fault.
  110. */
  111. survive:
  112. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  113. case VM_FAULT_MINOR:
  114. tsk->min_flt++;
  115. break;
  116. case VM_FAULT_MAJOR:
  117. tsk->maj_flt++;
  118. break;
  119. case VM_FAULT_SIGBUS:
  120. goto do_sigbus;
  121. case VM_FAULT_OOM:
  122. goto out_of_memory;
  123. default:
  124. BUG();
  125. }
  126. up_read(&mm->mmap_sem);
  127. return;
  128. /*
  129. * Something tried to access memory that isn't in our memory map..
  130. * Fix it, but check if it's kernel or user first..
  131. */
  132. bad_area:
  133. up_read(&mm->mmap_sem);
  134. bad_area_nosemaphore:
  135. if (user_mode(regs)) {
  136. info.si_signo = SIGSEGV;
  137. info.si_errno = 0;
  138. info.si_code = si_code;
  139. info.si_addr = (void *) address;
  140. force_sig_info(SIGSEGV, &info, tsk);
  141. return;
  142. }
  143. no_context:
  144. /* Are we prepared to handle this kernel fault? */
  145. if (fixup_exception(regs))
  146. return;
  147. /*
  148. * Oops. The kernel tried to access some bad page. We'll have to
  149. * terminate things with extreme prejudice.
  150. *
  151. */
  152. if (address < PAGE_SIZE)
  153. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  154. else
  155. printk(KERN_ALERT "Unable to handle kernel paging request");
  156. printk(" at virtual address %08lx\n", address);
  157. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  158. asm volatile("mov.l %1, %0"
  159. : "=r" (page)
  160. : "m" (__m(MMU_TTB)));
  161. if (page) {
  162. page = ((unsigned long *) page)[address >> 22];
  163. printk(KERN_ALERT "*pde = %08lx\n", page);
  164. if (page & _PAGE_PRESENT) {
  165. page &= PAGE_MASK;
  166. address &= 0x003ff000;
  167. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  168. printk(KERN_ALERT "*pte = %08lx\n", page);
  169. }
  170. }
  171. die("Oops", regs, writeaccess);
  172. do_exit(SIGKILL);
  173. /*
  174. * We ran out of memory, or some other thing happened to us that made
  175. * us unable to handle the page fault gracefully.
  176. */
  177. out_of_memory:
  178. up_read(&mm->mmap_sem);
  179. if (is_init(current)) {
  180. yield();
  181. down_read(&mm->mmap_sem);
  182. goto survive;
  183. }
  184. printk("VM: killing process %s\n", tsk->comm);
  185. if (user_mode(regs))
  186. do_exit(SIGKILL);
  187. goto no_context;
  188. do_sigbus:
  189. up_read(&mm->mmap_sem);
  190. /*
  191. * Send a sigbus, regardless of whether we were in kernel
  192. * or user mode.
  193. */
  194. info.si_signo = SIGBUS;
  195. info.si_errno = 0;
  196. info.si_code = BUS_ADRERR;
  197. info.si_addr = (void *)address;
  198. force_sig_info(SIGBUS, &info, tsk);
  199. /* Kernel mode? Handle exceptions or die */
  200. if (!user_mode(regs))
  201. goto no_context;
  202. }