fault.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. siginfo_t info;
  49. int fault;
  50. info.si_code = SEGV_MAPERR;
  51. /*
  52. * We fault-in kernel-space virtual memory on-demand. The
  53. * 'reference' page table is init_mm.pgd.
  54. *
  55. * NOTE! We MUST NOT take any locks for this case. We may
  56. * be in an interrupt or a critical region, and should
  57. * only copy the information from the master page table,
  58. * nothing more.
  59. */
  60. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  61. goto vmalloc_fault;
  62. #ifdef MODULE_START
  63. if (unlikely(address >= MODULE_START && address < MODULE_END))
  64. goto vmalloc_fault;
  65. #endif
  66. /*
  67. * If we're in an interrupt or have no user
  68. * context, we must not take the fault..
  69. */
  70. if (in_atomic() || !mm)
  71. goto bad_area_nosemaphore;
  72. down_read(&mm->mmap_sem);
  73. vma = find_vma(mm, address);
  74. if (!vma)
  75. goto bad_area;
  76. if (vma->vm_start <= address)
  77. goto good_area;
  78. if (!(vma->vm_flags & VM_GROWSDOWN))
  79. goto bad_area;
  80. if (expand_stack(vma, address))
  81. goto bad_area;
  82. /*
  83. * Ok, we have a good vm_area for this memory access, so
  84. * we can handle it..
  85. */
  86. good_area:
  87. info.si_code = SEGV_ACCERR;
  88. if (write) {
  89. if (!(vma->vm_flags & VM_WRITE))
  90. goto bad_area;
  91. } else {
  92. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  93. goto bad_area;
  94. }
  95. survive:
  96. /*
  97. * If for any reason at all we couldn't handle the fault,
  98. * make sure we exit gracefully rather than endlessly redo
  99. * the fault.
  100. */
  101. fault = handle_mm_fault(mm, vma, address, write);
  102. if (unlikely(fault & VM_FAULT_ERROR)) {
  103. if (fault & VM_FAULT_OOM)
  104. goto out_of_memory;
  105. else if (fault & VM_FAULT_SIGBUS)
  106. goto do_sigbus;
  107. BUG();
  108. }
  109. if (fault & VM_FAULT_MAJOR)
  110. tsk->maj_flt++;
  111. else
  112. tsk->min_flt++;
  113. up_read(&mm->mmap_sem);
  114. return;
  115. /*
  116. * Something tried to access memory that isn't in our memory map..
  117. * Fix it, but check if it's kernel or user first..
  118. */
  119. bad_area:
  120. up_read(&mm->mmap_sem);
  121. bad_area_nosemaphore:
  122. /* User mode accesses just cause a SIGSEGV */
  123. if (user_mode(regs)) {
  124. tsk->thread.cp0_badvaddr = address;
  125. tsk->thread.error_code = write;
  126. info.si_signo = SIGSEGV;
  127. info.si_errno = 0;
  128. /* info.si_code has been set above */
  129. info.si_addr = (void __user *) address;
  130. force_sig_info(SIGSEGV, &info, tsk);
  131. return;
  132. }
  133. no_context:
  134. /* Are we prepared to handle this kernel fault? */
  135. if (fixup_exception(regs)) {
  136. current->thread.cp0_baduaddr = address;
  137. return;
  138. }
  139. /*
  140. * Oops. The kernel tried to access some bad page. We'll have to
  141. * terminate things with extreme prejudice.
  142. */
  143. bust_spinlocks(1);
  144. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  145. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  146. 0, field, address, field, regs->cp0_epc,
  147. field, regs->regs[3]);
  148. die("Oops", regs);
  149. /*
  150. * We ran out of memory, or some other thing happened to us that made
  151. * us unable to handle the page fault gracefully.
  152. */
  153. out_of_memory:
  154. up_read(&mm->mmap_sem);
  155. if (is_global_init(tsk)) {
  156. yield();
  157. down_read(&mm->mmap_sem);
  158. goto survive;
  159. }
  160. printk("VM: killing process %s\n", tsk->comm);
  161. if (user_mode(regs))
  162. do_group_exit(SIGKILL);
  163. goto no_context;
  164. do_sigbus:
  165. up_read(&mm->mmap_sem);
  166. /* Kernel mode? Handle exceptions or die */
  167. if (!user_mode(regs))
  168. goto no_context;
  169. else
  170. /*
  171. * Send a sigbus, regardless of whether we were in kernel
  172. * or user mode.
  173. */
  174. tsk->thread.cp0_badvaddr = address;
  175. info.si_signo = SIGBUS;
  176. info.si_errno = 0;
  177. info.si_code = BUS_ADRERR;
  178. info.si_addr = (void __user *) address;
  179. force_sig_info(SIGBUS, &info, tsk);
  180. return;
  181. vmalloc_fault:
  182. {
  183. /*
  184. * Synchronize this task's top level page-table
  185. * with the 'reference' page table.
  186. *
  187. * Do _not_ use "tsk" here. We might be inside
  188. * an interrupt in the middle of a task switch..
  189. */
  190. int offset = __pgd_offset(address);
  191. pgd_t *pgd, *pgd_k;
  192. pud_t *pud, *pud_k;
  193. pmd_t *pmd, *pmd_k;
  194. pte_t *pte_k;
  195. pgd = (pgd_t *) pgd_current + offset;
  196. pgd_k = init_mm.pgd + offset;
  197. if (!pgd_present(*pgd_k))
  198. goto no_context;
  199. set_pgd(pgd, *pgd_k);
  200. pud = pud_offset(pgd, address);
  201. pud_k = pud_offset(pgd_k, address);
  202. if (!pud_present(*pud_k))
  203. goto no_context;
  204. pmd = pmd_offset(pud, address);
  205. pmd_k = pmd_offset(pud_k, address);
  206. if (!pmd_present(*pmd_k))
  207. goto no_context;
  208. set_pmd(pmd, *pmd_k);
  209. pte_k = pte_offset_kernel(pmd_k, address);
  210. if (!pte_present(*pte_k))
  211. goto no_context;
  212. return;
  213. }
  214. }