fault.c 5.8 KB

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