fault.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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)-16 : \
  68. (r) <= 18 ? (r)+10 : (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. return;
  128. /* The fault is fully completed (including releasing mmap lock) */
  129. if (fault & VM_FAULT_COMPLETED)
  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_SIGSEGV)
  135. goto bad_area;
  136. else if (fault & VM_FAULT_SIGBUS)
  137. goto do_sigbus;
  138. BUG();
  139. }
  140. if (fault & VM_FAULT_RETRY) {
  141. flags |= FAULT_FLAG_TRIED;
  142. /* No need to mmap_read_unlock(mm) as we would
  143. * have already released it in __lock_page_or_retry
  144. * in mm/filemap.c.
  145. */
  146. goto retry;
  147. }
  148. mmap_read_unlock(mm);
  149. return;
  150. /* Something tried to access memory that isn't in our memory map.
  151. Fix it, but check if it's kernel or user first. */
  152. bad_area:
  153. mmap_read_unlock(mm);
  154. bad_area_nosemaphore:
  155. if (user_mode(regs))
  156. goto do_sigsegv;
  157. no_context:
  158. /* Are we prepared to handle this fault as an exception? */
  159. if ((fixup = search_exception_tables(regs->pc)) != 0) {
  160. unsigned long newpc;
  161. newpc = fixup_exception(dpf_reg, fixup, regs->pc);
  162. regs->pc = newpc;
  163. return;
  164. }
  165. /* Oops. The kernel tried to access some bad page. We'll have to
  166. terminate things with extreme prejudice. */
  167. printk(KERN_ALERT "Unable to handle kernel paging request at "
  168. "virtual address %016lx\n", address);
  169. die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
  170. make_task_dead(SIGKILL);
  171. /* We ran out of memory, or some other thing happened to us that
  172. made us unable to handle the page fault gracefully. */
  173. out_of_memory:
  174. mmap_read_unlock(mm);
  175. if (!user_mode(regs))
  176. goto no_context;
  177. pagefault_out_of_memory();
  178. return;
  179. do_sigbus:
  180. mmap_read_unlock(mm);
  181. /* Send a sigbus, regardless of whether we were in kernel
  182. or user mode. */
  183. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address);
  184. if (!user_mode(regs))
  185. goto no_context;
  186. return;
  187. do_sigsegv:
  188. force_sig_fault(SIGSEGV, si_code, (void __user *) address);
  189. return;
  190. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  191. vmalloc_fault:
  192. if (user_mode(regs))
  193. goto do_sigsegv;
  194. else {
  195. /* Synchronize this task's top level page-table
  196. with the "reference" page table from init. */
  197. long index = pgd_index(address);
  198. pgd_t *pgd, *pgd_k;
  199. pgd = current->active_mm->pgd + index;
  200. pgd_k = swapper_pg_dir + index;
  201. if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
  202. pgd_val(*pgd) = pgd_val(*pgd_k);
  203. return;
  204. }
  205. goto no_context;
  206. }
  207. #endif
  208. }