fault.c 5.9 KB

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