fault.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. trace_hardirqs_on();
  38. local_irq_enable();
  39. #ifdef CONFIG_SH_KGDB
  40. if (kgdb_nofault && kgdb_bus_err_hook)
  41. kgdb_bus_err_hook();
  42. #endif
  43. tsk = current;
  44. mm = tsk->mm;
  45. si_code = SEGV_MAPERR;
  46. if (unlikely(address >= TASK_SIZE)) {
  47. /*
  48. * Synchronize this task's top level page-table
  49. * with the 'reference' page table.
  50. *
  51. * Do _not_ use "tsk" here. We might be inside
  52. * an interrupt in the middle of a task switch..
  53. */
  54. int offset = pgd_index(address);
  55. pgd_t *pgd, *pgd_k;
  56. pud_t *pud, *pud_k;
  57. pmd_t *pmd, *pmd_k;
  58. pgd = get_TTB() + offset;
  59. pgd_k = swapper_pg_dir + offset;
  60. /* This will never happen with the folded page table. */
  61. if (!pgd_present(*pgd)) {
  62. if (!pgd_present(*pgd_k))
  63. goto bad_area_nosemaphore;
  64. set_pgd(pgd, *pgd_k);
  65. return;
  66. }
  67. pud = pud_offset(pgd, address);
  68. pud_k = pud_offset(pgd_k, address);
  69. if (pud_present(*pud) || !pud_present(*pud_k))
  70. goto bad_area_nosemaphore;
  71. set_pud(pud, *pud_k);
  72. pmd = pmd_offset(pud, address);
  73. pmd_k = pmd_offset(pud_k, address);
  74. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  75. goto bad_area_nosemaphore;
  76. set_pmd(pmd, *pmd_k);
  77. return;
  78. }
  79. /*
  80. * If we're in an interrupt or have no user
  81. * context, we must not take the fault..
  82. */
  83. if (in_atomic() || !mm)
  84. goto no_context;
  85. down_read(&mm->mmap_sem);
  86. vma = find_vma(mm, address);
  87. if (!vma)
  88. goto bad_area;
  89. if (vma->vm_start <= address)
  90. goto good_area;
  91. if (!(vma->vm_flags & VM_GROWSDOWN))
  92. goto bad_area;
  93. if (expand_stack(vma, address))
  94. goto bad_area;
  95. /*
  96. * Ok, we have a good vm_area for this memory access, so
  97. * we can handle it..
  98. */
  99. good_area:
  100. si_code = SEGV_ACCERR;
  101. if (writeaccess) {
  102. if (!(vma->vm_flags & VM_WRITE))
  103. goto bad_area;
  104. } else {
  105. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  106. goto bad_area;
  107. }
  108. /*
  109. * If for any reason at all we couldn't handle the fault,
  110. * make sure we exit gracefully rather than endlessly redo
  111. * the fault.
  112. */
  113. survive:
  114. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  115. case VM_FAULT_MINOR:
  116. tsk->min_flt++;
  117. break;
  118. case VM_FAULT_MAJOR:
  119. tsk->maj_flt++;
  120. break;
  121. case VM_FAULT_SIGBUS:
  122. goto do_sigbus;
  123. case VM_FAULT_OOM:
  124. goto out_of_memory;
  125. default:
  126. BUG();
  127. }
  128. up_read(&mm->mmap_sem);
  129. return;
  130. /*
  131. * Something tried to access memory that isn't in our memory map..
  132. * Fix it, but check if it's kernel or user first..
  133. */
  134. bad_area:
  135. up_read(&mm->mmap_sem);
  136. bad_area_nosemaphore:
  137. if (user_mode(regs)) {
  138. info.si_signo = SIGSEGV;
  139. info.si_errno = 0;
  140. info.si_code = si_code;
  141. info.si_addr = (void *) address;
  142. force_sig_info(SIGSEGV, &info, tsk);
  143. return;
  144. }
  145. no_context:
  146. /* Are we prepared to handle this kernel fault? */
  147. if (fixup_exception(regs))
  148. return;
  149. /*
  150. * Oops. The kernel tried to access some bad page. We'll have to
  151. * terminate things with extreme prejudice.
  152. *
  153. */
  154. if (address < PAGE_SIZE)
  155. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  156. else
  157. printk(KERN_ALERT "Unable to handle kernel paging request");
  158. printk(" at virtual address %08lx\n", address);
  159. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  160. page = (unsigned long)get_TTB();
  161. if (page) {
  162. page = ((unsigned long *) page)[address >> PGDIR_SHIFT];
  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. }