fault.c 5.9 KB

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