fault.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * linux/arch/alpha/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/kernel.h>
  8. #include <linux/mm.h>
  9. #include <asm/io.h>
  10. #define __EXTERN_INLINE inline
  11. #include <asm/mmu_context.h>
  12. #include <asm/tlbflush.h>
  13. #undef __EXTERN_INLINE
  14. #include <linux/signal.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/smp.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <asm/uaccess.h>
  24. extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
  25. /*
  26. * Force a new ASN for a task.
  27. */
  28. #ifndef CONFIG_SMP
  29. unsigned long last_asn = ASN_FIRST_VERSION;
  30. #endif
  31. void
  32. __load_new_mm_context(struct mm_struct *next_mm)
  33. {
  34. unsigned long mmc;
  35. struct pcb_struct *pcb;
  36. mmc = __get_new_mm_context(next_mm, smp_processor_id());
  37. next_mm->context[smp_processor_id()] = mmc;
  38. pcb = &current_thread_info()->pcb;
  39. pcb->asn = mmc & HARDWARE_ASN_MASK;
  40. pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
  41. __reload_thread(pcb);
  42. }
  43. /*
  44. * This routine handles page faults. It determines the address,
  45. * and the problem, and then passes it off to handle_mm_fault().
  46. *
  47. * mmcsr:
  48. * 0 = translation not valid
  49. * 1 = access violation
  50. * 2 = fault-on-read
  51. * 3 = fault-on-execute
  52. * 4 = fault-on-write
  53. *
  54. * cause:
  55. * -1 = instruction fetch
  56. * 0 = load
  57. * 1 = store
  58. *
  59. * Registers $9 through $15 are saved in a block just prior to `regs' and
  60. * are saved and restored around the call to allow exception code to
  61. * modify them.
  62. */
  63. /* Macro for exception fixup code to access integer registers. */
  64. #define dpf_reg(r) \
  65. (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 : \
  66. (r) <= 18 ? (r)+8 : (r)-10])
  67. asmlinkage void
  68. do_page_fault(unsigned long address, unsigned long mmcsr,
  69. long cause, struct pt_regs *regs)
  70. {
  71. struct vm_area_struct * vma;
  72. struct mm_struct *mm = current->mm;
  73. const struct exception_table_entry *fixup;
  74. int fault, si_code = SEGV_MAPERR;
  75. siginfo_t info;
  76. unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  77. (cause > 0 ? FAULT_FLAG_WRITE : 0));
  78. /* As of EV6, a load into $31/$f31 is a prefetch, and never faults
  79. (or is suppressed by the PALcode). Support that for older CPUs
  80. by ignoring such an instruction. */
  81. if (cause == 0) {
  82. unsigned int insn;
  83. __get_user(insn, (unsigned int __user *)regs->pc);
  84. if ((insn >> 21 & 0x1f) == 0x1f &&
  85. /* ldq ldl ldt lds ldg ldf ldwu ldbu */
  86. (1ul << (insn >> 26) & 0x30f00001400ul)) {
  87. regs->pc += 4;
  88. return;
  89. }
  90. }
  91. /* If we're in an interrupt context, or have no user context,
  92. we must not take the fault. */
  93. if (!mm || in_atomic())
  94. goto no_context;
  95. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  96. if (address >= TASK_SIZE)
  97. goto vmalloc_fault;
  98. #endif
  99. retry:
  100. down_read(&mm->mmap_sem);
  101. vma = find_vma(mm, address);
  102. if (!vma)
  103. goto bad_area;
  104. if (vma->vm_start <= address)
  105. goto good_area;
  106. if (!(vma->vm_flags & VM_GROWSDOWN))
  107. goto bad_area;
  108. if (expand_stack(vma, address))
  109. goto bad_area;
  110. /* Ok, we have a good vm_area for this memory access, so
  111. we can handle it. */
  112. good_area:
  113. si_code = SEGV_ACCERR;
  114. if (cause < 0) {
  115. if (!(vma->vm_flags & VM_EXEC))
  116. goto bad_area;
  117. } else if (!cause) {
  118. /* Allow reads even for write-only mappings */
  119. if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
  120. goto bad_area;
  121. } else {
  122. if (!(vma->vm_flags & VM_WRITE))
  123. goto bad_area;
  124. }
  125. /* If for any reason at all we couldn't handle the fault,
  126. make sure we exit gracefully rather than endlessly redo
  127. the fault. */
  128. fault = handle_mm_fault(mm, vma, address, flags);
  129. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  130. return;
  131. if (unlikely(fault & VM_FAULT_ERROR)) {
  132. if (fault & VM_FAULT_OOM)
  133. goto out_of_memory;
  134. else if (fault & VM_FAULT_SIGBUS)
  135. goto do_sigbus;
  136. BUG();
  137. }
  138. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  139. if (fault & VM_FAULT_MAJOR)
  140. current->maj_flt++;
  141. else
  142. current->min_flt++;
  143. if (fault & VM_FAULT_RETRY) {
  144. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  145. /* No need to up_read(&mm->mmap_sem) as we would
  146. * have already released it in __lock_page_or_retry
  147. * in mm/filemap.c.
  148. */
  149. goto retry;
  150. }
  151. }
  152. up_read(&mm->mmap_sem);
  153. return;
  154. /* Something tried to access memory that isn't in our memory map.
  155. Fix it, but check if it's kernel or user first. */
  156. bad_area:
  157. up_read(&mm->mmap_sem);
  158. if (user_mode(regs))
  159. goto do_sigsegv;
  160. no_context:
  161. /* Are we prepared to handle this fault as an exception? */
  162. if ((fixup = search_exception_tables(regs->pc)) != 0) {
  163. unsigned long newpc;
  164. newpc = fixup_exception(dpf_reg, fixup, regs->pc);
  165. regs->pc = newpc;
  166. return;
  167. }
  168. /* Oops. The kernel tried to access some bad page. We'll have to
  169. terminate things with extreme prejudice. */
  170. printk(KERN_ALERT "Unable to handle kernel paging request at "
  171. "virtual address %016lx\n", address);
  172. die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
  173. do_exit(SIGKILL);
  174. /* We ran out of memory, or some other thing happened to us that
  175. made us unable to handle the page fault gracefully. */
  176. out_of_memory:
  177. up_read(&mm->mmap_sem);
  178. if (!user_mode(regs))
  179. goto no_context;
  180. pagefault_out_of_memory();
  181. return;
  182. do_sigbus:
  183. up_read(&mm->mmap_sem);
  184. /* Send a sigbus, regardless of whether we were in kernel
  185. or user mode. */
  186. info.si_signo = SIGBUS;
  187. info.si_errno = 0;
  188. info.si_code = BUS_ADRERR;
  189. info.si_addr = (void __user *) address;
  190. force_sig_info(SIGBUS, &info, current);
  191. if (!user_mode(regs))
  192. goto no_context;
  193. return;
  194. do_sigsegv:
  195. info.si_signo = SIGSEGV;
  196. info.si_errno = 0;
  197. info.si_code = si_code;
  198. info.si_addr = (void __user *) address;
  199. force_sig_info(SIGSEGV, &info, current);
  200. return;
  201. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  202. vmalloc_fault:
  203. if (user_mode(regs))
  204. goto do_sigsegv;
  205. else {
  206. /* Synchronize this task's top level page-table
  207. with the "reference" page table from init. */
  208. long index = pgd_index(address);
  209. pgd_t *pgd, *pgd_k;
  210. pgd = current->active_mm->pgd + index;
  211. pgd_k = swapper_pg_dir + index;
  212. if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
  213. pgd_val(*pgd) = pgd_val(*pgd_k);
  214. return;
  215. }
  216. goto no_context;
  217. }
  218. #endif
  219. }