fault.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/alpha/mm/fault.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. */
  7. #include <linux/sched/signal.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/interrupt.h>
  23. #include <linux/extable.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/perf_event.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)-17 : \
  68. (r) <= 18 ? (r)+11 : (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 si_code = SEGV_MAPERR;
  77. vm_fault_t fault;
  78. unsigned int flags = FAULT_FLAG_DEFAULT;
  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 || faulthandler_disabled())
  95. goto no_context;
  96. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  97. if (address >= TASK_SIZE)
  98. goto vmalloc_fault;
  99. #endif
  100. if (user_mode(regs))
  101. flags |= FAULT_FLAG_USER;
  102. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  103. retry:
  104. vma = lock_mm_and_find_vma(mm, address, regs);
  105. if (!vma)
  106. goto bad_area_nosemaphore;
  107. /* Ok, we have a good vm_area for this memory access, so
  108. we can handle it. */
  109. si_code = SEGV_ACCERR;
  110. if (cause < 0) {
  111. if (!(vma->vm_flags & VM_EXEC))
  112. goto bad_area;
  113. } else if (!cause) {
  114. /* Allow reads even for write-only mappings */
  115. if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
  116. goto bad_area;
  117. } else {
  118. if (!(vma->vm_flags & VM_WRITE))
  119. goto bad_area;
  120. flags |= FAULT_FLAG_WRITE;
  121. }
  122. /* If for any reason at all we couldn't handle the fault,
  123. make sure we exit gracefully rather than endlessly redo
  124. the fault. */
  125. fault = handle_mm_fault(vma, address, flags, regs);
  126. if (fault_signal_pending(fault, regs)) {
  127. if (!user_mode(regs))
  128. goto no_context;
  129. return;
  130. }
  131. /* The fault is fully completed (including releasing mmap lock) */
  132. if (fault & VM_FAULT_COMPLETED)
  133. return;
  134. if (unlikely(fault & VM_FAULT_ERROR)) {
  135. if (fault & VM_FAULT_OOM)
  136. goto out_of_memory;
  137. else if (fault & VM_FAULT_SIGSEGV)
  138. goto bad_area;
  139. else if (fault & VM_FAULT_SIGBUS)
  140. goto do_sigbus;
  141. BUG();
  142. }
  143. if (fault & VM_FAULT_RETRY) {
  144. flags |= FAULT_FLAG_TRIED;
  145. /* No need to mmap_read_unlock(mm) as we would
  146. * have already released it in __lock_page_or_retry
  147. * in mm/filemap.c.
  148. */
  149. goto retry;
  150. }
  151. mmap_read_unlock(mm);
  152. return;
  153. /* Something tried to access memory that isn't in our memory map.
  154. Fix it, but check if it's kernel or user first. */
  155. bad_area:
  156. mmap_read_unlock(mm);
  157. bad_area_nosemaphore:
  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. make_task_dead(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. mmap_read_unlock(mm);
  178. if (!user_mode(regs))
  179. goto no_context;
  180. pagefault_out_of_memory();
  181. return;
  182. do_sigbus:
  183. mmap_read_unlock(mm);
  184. /* Send a sigbus, regardless of whether we were in kernel
  185. or user mode. */
  186. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address);
  187. if (!user_mode(regs))
  188. goto no_context;
  189. return;
  190. do_sigsegv:
  191. force_sig_fault(SIGSEGV, si_code, (void __user *) address);
  192. return;
  193. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  194. vmalloc_fault:
  195. if (user_mode(regs))
  196. goto do_sigsegv;
  197. else {
  198. /* Synchronize this task's top level page-table
  199. with the "reference" page table from init. */
  200. long index = pgd_index(address);
  201. pgd_t *pgd, *pgd_k;
  202. pgd = current->active_mm->pgd + index;
  203. pgd_k = swapper_pg_dir + index;
  204. if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
  205. pgd_val(*pgd) = pgd_val(*pgd_k);
  206. return;
  207. }
  208. goto no_context;
  209. }
  210. #endif
  211. }