fault.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/smp_lock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
  27. /*
  28. * Force a new ASN for a task.
  29. */
  30. #ifndef CONFIG_SMP
  31. unsigned long last_asn = ASN_FIRST_VERSION;
  32. #endif
  33. void
  34. __load_new_mm_context(struct mm_struct *next_mm)
  35. {
  36. unsigned long mmc;
  37. struct pcb_struct *pcb;
  38. mmc = __get_new_mm_context(next_mm, smp_processor_id());
  39. next_mm->context[smp_processor_id()] = mmc;
  40. pcb = &current_thread_info()->pcb;
  41. pcb->asn = mmc & HARDWARE_ASN_MASK;
  42. pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
  43. __reload_thread(pcb);
  44. }
  45. /*
  46. * This routine handles page faults. It determines the address,
  47. * and the problem, and then passes it off to handle_mm_fault().
  48. *
  49. * mmcsr:
  50. * 0 = translation not valid
  51. * 1 = access violation
  52. * 2 = fault-on-read
  53. * 3 = fault-on-execute
  54. * 4 = fault-on-write
  55. *
  56. * cause:
  57. * -1 = instruction fetch
  58. * 0 = load
  59. * 1 = store
  60. *
  61. * Registers $9 through $15 are saved in a block just prior to `regs' and
  62. * are saved and restored around the call to allow exception code to
  63. * modify them.
  64. */
  65. /* Macro for exception fixup code to access integer registers. */
  66. #define dpf_reg(r) \
  67. (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 : \
  68. (r) <= 18 ? (r)+8 : (r)-10])
  69. asmlinkage void
  70. do_page_fault(unsigned long address, unsigned long mmcsr,
  71. long cause, struct pt_regs *regs)
  72. {
  73. struct vm_area_struct * vma;
  74. struct mm_struct *mm = current->mm;
  75. const struct exception_table_entry *fixup;
  76. int fault, si_code = SEGV_MAPERR;
  77. siginfo_t info;
  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. down_read(&mm->mmap_sem);
  100. vma = find_vma(mm, address);
  101. if (!vma)
  102. goto bad_area;
  103. if (vma->vm_start <= address)
  104. goto good_area;
  105. if (!(vma->vm_flags & VM_GROWSDOWN))
  106. goto bad_area;
  107. if (expand_stack(vma, address))
  108. goto bad_area;
  109. /* Ok, we have a good vm_area for this memory access, so
  110. we can handle it. */
  111. good_area:
  112. si_code = SEGV_ACCERR;
  113. if (cause < 0) {
  114. if (!(vma->vm_flags & VM_EXEC))
  115. goto bad_area;
  116. } else if (!cause) {
  117. /* Allow reads even for write-only mappings */
  118. if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
  119. goto bad_area;
  120. } else {
  121. if (!(vma->vm_flags & VM_WRITE))
  122. goto bad_area;
  123. }
  124. survive:
  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, cause > 0);
  129. up_read(&mm->mmap_sem);
  130. switch (fault) {
  131. case VM_FAULT_MINOR:
  132. current->min_flt++;
  133. break;
  134. case VM_FAULT_MAJOR:
  135. current->maj_flt++;
  136. break;
  137. case VM_FAULT_SIGBUS:
  138. goto do_sigbus;
  139. case VM_FAULT_OOM:
  140. goto out_of_memory;
  141. default:
  142. BUG();
  143. }
  144. return;
  145. /* Something tried to access memory that isn't in our memory map.
  146. Fix it, but check if it's kernel or user first. */
  147. bad_area:
  148. up_read(&mm->mmap_sem);
  149. if (user_mode(regs))
  150. goto do_sigsegv;
  151. no_context:
  152. /* Are we prepared to handle this fault as an exception? */
  153. if ((fixup = search_exception_tables(regs->pc)) != 0) {
  154. unsigned long newpc;
  155. newpc = fixup_exception(dpf_reg, fixup, regs->pc);
  156. regs->pc = newpc;
  157. return;
  158. }
  159. /* Oops. The kernel tried to access some bad page. We'll have to
  160. terminate things with extreme prejudice. */
  161. printk(KERN_ALERT "Unable to handle kernel paging request at "
  162. "virtual address %016lx\n", address);
  163. die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
  164. do_exit(SIGKILL);
  165. /* We ran out of memory, or some other thing happened to us that
  166. made us unable to handle the page fault gracefully. */
  167. out_of_memory:
  168. if (is_init(current)) {
  169. yield();
  170. down_read(&mm->mmap_sem);
  171. goto survive;
  172. }
  173. printk(KERN_ALERT "VM: killing process %s(%d)\n",
  174. current->comm, current->pid);
  175. if (!user_mode(regs))
  176. goto no_context;
  177. do_exit(SIGKILL);
  178. do_sigbus:
  179. /* Send a sigbus, regardless of whether we were in kernel
  180. or user mode. */
  181. info.si_signo = SIGBUS;
  182. info.si_errno = 0;
  183. info.si_code = BUS_ADRERR;
  184. info.si_addr = (void __user *) address;
  185. force_sig_info(SIGBUS, &info, current);
  186. if (!user_mode(regs))
  187. goto no_context;
  188. return;
  189. do_sigsegv:
  190. info.si_signo = SIGSEGV;
  191. info.si_errno = 0;
  192. info.si_code = si_code;
  193. info.si_addr = (void __user *) address;
  194. force_sig_info(SIGSEGV, &info, current);
  195. return;
  196. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  197. vmalloc_fault:
  198. if (user_mode(regs))
  199. goto do_sigsegv;
  200. else {
  201. /* Synchronize this task's top level page-table
  202. with the "reference" page table from init. */
  203. long index = pgd_index(address);
  204. pgd_t *pgd, *pgd_k;
  205. pgd = current->active_mm->pgd + index;
  206. pgd_k = swapper_pg_dir + index;
  207. if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
  208. pgd_val(*pgd) = pgd_val(*pgd_k);
  209. return;
  210. }
  211. goto no_context;
  212. }
  213. #endif
  214. }