fault.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * arch/score/mm/fault.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Lennox Wu <lennox.wu@sunplusct.com>
  8. * Chen Liqin <liqin.chen@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/kernel.h>
  28. #include <linux/mm.h>
  29. #include <linux/mman.h>
  30. #include <linux/module.h>
  31. #include <linux/signal.h>
  32. #include <linux/sched.h>
  33. #include <linux/string.h>
  34. #include <linux/types.h>
  35. #include <linux/ptrace.h>
  36. /*
  37. * This routine handles page faults. It determines the address,
  38. * and the problem, and then passes it off to one of the appropriate
  39. * routines.
  40. */
  41. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
  42. unsigned long address)
  43. {
  44. struct vm_area_struct *vma = NULL;
  45. struct task_struct *tsk = current;
  46. struct mm_struct *mm = tsk->mm;
  47. const int field = sizeof(unsigned long) * 2;
  48. unsigned long flags = 0;
  49. siginfo_t info;
  50. int fault;
  51. info.si_code = SEGV_MAPERR;
  52. /*
  53. * We fault-in kernel-space virtual memory on-demand. The
  54. * 'reference' page table is init_mm.pgd.
  55. *
  56. * NOTE! We MUST NOT take any locks for this case. We may
  57. * be in an interrupt or a critical region, and should
  58. * only copy the information from the master page table,
  59. * nothing more.
  60. */
  61. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  62. goto vmalloc_fault;
  63. #ifdef MODULE_START
  64. if (unlikely(address >= MODULE_START && address < MODULE_END))
  65. goto vmalloc_fault;
  66. #endif
  67. /*
  68. * If we're in an interrupt or have no user
  69. * context, we must not take the fault..
  70. */
  71. if (in_atomic() || !mm)
  72. goto bad_area_nosemaphore;
  73. if (user_mode(regs))
  74. flags |= FAULT_FLAG_USER;
  75. down_read(&mm->mmap_sem);
  76. vma = find_vma(mm, address);
  77. if (!vma)
  78. goto bad_area;
  79. if (vma->vm_start <= address)
  80. goto good_area;
  81. if (!(vma->vm_flags & VM_GROWSDOWN))
  82. goto bad_area;
  83. if (expand_stack(vma, address))
  84. goto bad_area;
  85. /*
  86. * Ok, we have a good vm_area for this memory access, so
  87. * we can handle it..
  88. */
  89. good_area:
  90. info.si_code = SEGV_ACCERR;
  91. if (write) {
  92. if (!(vma->vm_flags & VM_WRITE))
  93. goto bad_area;
  94. flags |= FAULT_FLAG_WRITE;
  95. } else {
  96. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  97. goto bad_area;
  98. }
  99. /*
  100. * If for any reason at all we couldn't handle the fault,
  101. * make sure we exit gracefully rather than endlessly redo
  102. * the fault.
  103. */
  104. fault = handle_mm_fault(mm, vma, address, flags);
  105. if (unlikely(fault & VM_FAULT_ERROR)) {
  106. if (fault & VM_FAULT_OOM)
  107. goto out_of_memory;
  108. else if (fault & VM_FAULT_SIGBUS)
  109. goto do_sigbus;
  110. BUG();
  111. }
  112. if (fault & VM_FAULT_MAJOR)
  113. tsk->maj_flt++;
  114. else
  115. tsk->min_flt++;
  116. up_read(&mm->mmap_sem);
  117. return;
  118. /*
  119. * Something tried to access memory that isn't in our memory map..
  120. * Fix it, but check if it's kernel or user first..
  121. */
  122. bad_area:
  123. up_read(&mm->mmap_sem);
  124. bad_area_nosemaphore:
  125. /* User mode accesses just cause a SIGSEGV */
  126. if (user_mode(regs)) {
  127. tsk->thread.cp0_badvaddr = address;
  128. tsk->thread.error_code = write;
  129. info.si_signo = SIGSEGV;
  130. info.si_errno = 0;
  131. /* info.si_code has been set above */
  132. info.si_addr = (void __user *) address;
  133. force_sig_info(SIGSEGV, &info, tsk);
  134. return;
  135. }
  136. no_context:
  137. /* Are we prepared to handle this kernel fault? */
  138. if (fixup_exception(regs)) {
  139. current->thread.cp0_baduaddr = address;
  140. return;
  141. }
  142. /*
  143. * Oops. The kernel tried to access some bad page. We'll have to
  144. * terminate things with extreme prejudice.
  145. */
  146. bust_spinlocks(1);
  147. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  148. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  149. 0, field, address, field, regs->cp0_epc,
  150. field, regs->regs[3]);
  151. die("Oops", regs);
  152. /*
  153. * We ran out of memory, or some other thing happened to us that made
  154. * us unable to handle the page fault gracefully.
  155. */
  156. out_of_memory:
  157. up_read(&mm->mmap_sem);
  158. if (!user_mode(regs))
  159. goto no_context;
  160. pagefault_out_of_memory();
  161. return;
  162. do_sigbus:
  163. up_read(&mm->mmap_sem);
  164. /* Kernel mode? Handle exceptions or die */
  165. if (!user_mode(regs))
  166. goto no_context;
  167. else
  168. /*
  169. * Send a sigbus, regardless of whether we were in kernel
  170. * or user mode.
  171. */
  172. tsk->thread.cp0_badvaddr = address;
  173. info.si_signo = SIGBUS;
  174. info.si_errno = 0;
  175. info.si_code = BUS_ADRERR;
  176. info.si_addr = (void __user *) address;
  177. force_sig_info(SIGBUS, &info, tsk);
  178. return;
  179. vmalloc_fault:
  180. {
  181. /*
  182. * Synchronize this task's top level page-table
  183. * with the 'reference' page table.
  184. *
  185. * Do _not_ use "tsk" here. We might be inside
  186. * an interrupt in the middle of a task switch..
  187. */
  188. int offset = __pgd_offset(address);
  189. pgd_t *pgd, *pgd_k;
  190. pud_t *pud, *pud_k;
  191. pmd_t *pmd, *pmd_k;
  192. pte_t *pte_k;
  193. pgd = (pgd_t *) pgd_current + offset;
  194. pgd_k = init_mm.pgd + offset;
  195. if (!pgd_present(*pgd_k))
  196. goto no_context;
  197. set_pgd(pgd, *pgd_k);
  198. pud = pud_offset(pgd, address);
  199. pud_k = pud_offset(pgd_k, address);
  200. if (!pud_present(*pud_k))
  201. goto no_context;
  202. pmd = pmd_offset(pud, address);
  203. pmd_k = pmd_offset(pud_k, address);
  204. if (!pmd_present(*pmd_k))
  205. goto no_context;
  206. set_pmd(pmd, *pmd_k);
  207. pte_k = pte_offset_kernel(pmd_k, address);
  208. if (!pte_present(*pte_k))
  209. goto no_context;
  210. return;
  211. }
  212. }