fault.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. 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 si_code = SEGV_MAPERR;
  76. vm_fault_t fault;
  77. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  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 || faulthandler_disabled())
  94. goto no_context;
  95. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  96. if (address >= TASK_SIZE)
  97. goto vmalloc_fault;
  98. #endif
  99. if (user_mode(regs))
  100. flags |= FAULT_FLAG_USER;
  101. retry:
  102. down_read(&mm->mmap_sem);
  103. vma = find_vma(mm, address);
  104. if (!vma)
  105. goto bad_area;
  106. if (vma->vm_start <= address)
  107. goto good_area;
  108. if (!(vma->vm_flags & VM_GROWSDOWN))
  109. goto bad_area;
  110. if (expand_stack(vma, address))
  111. goto bad_area;
  112. /* Ok, we have a good vm_area for this memory access, so
  113. we can handle it. */
  114. good_area:
  115. si_code = SEGV_ACCERR;
  116. if (cause < 0) {
  117. if (!(vma->vm_flags & VM_EXEC))
  118. goto bad_area;
  119. } else if (!cause) {
  120. /* Allow reads even for write-only mappings */
  121. if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
  122. goto bad_area;
  123. } else {
  124. if (!(vma->vm_flags & VM_WRITE))
  125. goto bad_area;
  126. flags |= FAULT_FLAG_WRITE;
  127. }
  128. /* If for any reason at all we couldn't handle the fault,
  129. make sure we exit gracefully rather than endlessly redo
  130. the fault. */
  131. fault = handle_mm_fault(vma, address, flags);
  132. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  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 (flags & FAULT_FLAG_ALLOW_RETRY) {
  144. if (fault & VM_FAULT_MAJOR)
  145. current->maj_flt++;
  146. else
  147. current->min_flt++;
  148. if (fault & VM_FAULT_RETRY) {
  149. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  150. /* No need to up_read(&mm->mmap_sem) as we would
  151. * have already released it in __lock_page_or_retry
  152. * in mm/filemap.c.
  153. */
  154. goto retry;
  155. }
  156. }
  157. up_read(&mm->mmap_sem);
  158. return;
  159. /* Something tried to access memory that isn't in our memory map.
  160. Fix it, but check if it's kernel or user first. */
  161. bad_area:
  162. up_read(&mm->mmap_sem);
  163. if (user_mode(regs))
  164. goto do_sigsegv;
  165. no_context:
  166. /* Are we prepared to handle this fault as an exception? */
  167. if ((fixup = search_exception_tables(regs->pc)) != 0) {
  168. unsigned long newpc;
  169. newpc = fixup_exception(dpf_reg, fixup, regs->pc);
  170. regs->pc = newpc;
  171. return;
  172. }
  173. /* Oops. The kernel tried to access some bad page. We'll have to
  174. terminate things with extreme prejudice. */
  175. printk(KERN_ALERT "Unable to handle kernel paging request at "
  176. "virtual address %016lx\n", address);
  177. die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
  178. do_exit(SIGKILL);
  179. /* We ran out of memory, or some other thing happened to us that
  180. made us unable to handle the page fault gracefully. */
  181. out_of_memory:
  182. up_read(&mm->mmap_sem);
  183. if (!user_mode(regs))
  184. goto no_context;
  185. pagefault_out_of_memory();
  186. return;
  187. do_sigbus:
  188. up_read(&mm->mmap_sem);
  189. /* Send a sigbus, regardless of whether we were in kernel
  190. or user mode. */
  191. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address, 0, current);
  192. if (!user_mode(regs))
  193. goto no_context;
  194. return;
  195. do_sigsegv:
  196. force_sig_fault(SIGSEGV, si_code, (void __user *) address, 0, current);
  197. return;
  198. #ifdef CONFIG_ALPHA_LARGE_VMALLOC
  199. vmalloc_fault:
  200. if (user_mode(regs))
  201. goto do_sigsegv;
  202. else {
  203. /* Synchronize this task's top level page-table
  204. with the "reference" page table from init. */
  205. long index = pgd_index(address);
  206. pgd_t *pgd, *pgd_k;
  207. pgd = current->active_mm->pgd + index;
  208. pgd_k = swapper_pg_dir + index;
  209. if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
  210. pgd_val(*pgd) = pgd_val(*pgd_k);
  211. return;
  212. }
  213. goto no_context;
  214. }
  215. #endif
  216. }