fault.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. *
  7. * Copyright (C) 1995, 1996, 1997, 1998 by Ralf Baechle
  8. * Copyright 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
  9. * Copyright 1999 Hewlett Packard Co.
  10. *
  11. */
  12. #include <linux/mm.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/extable.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/hugetlb.h>
  20. #include <asm/traps.h>
  21. /* Various important other fields */
  22. #define bit22set(x) (x & 0x00000200)
  23. #define bits23_25set(x) (x & 0x000001c0)
  24. #define isGraphicsFlushRead(x) ((x & 0xfc003fdf) == 0x04001a80)
  25. /* extended opcode is 0x6a */
  26. #define BITSSET 0x1c0 /* for identifying LDCW */
  27. int show_unhandled_signals = 1;
  28. /*
  29. * parisc_acctyp(unsigned int inst) --
  30. * Given a PA-RISC memory access instruction, determine if the
  31. * the instruction would perform a memory read or memory write
  32. * operation.
  33. *
  34. * This function assumes that the given instruction is a memory access
  35. * instruction (i.e. you should really only call it if you know that
  36. * the instruction has generated some sort of a memory access fault).
  37. *
  38. * Returns:
  39. * VM_READ if read operation
  40. * VM_WRITE if write operation
  41. * VM_EXEC if execute operation
  42. */
  43. static unsigned long
  44. parisc_acctyp(unsigned long code, unsigned int inst)
  45. {
  46. if (code == 6 || code == 16)
  47. return VM_EXEC;
  48. switch (inst & 0xf0000000) {
  49. case 0x40000000: /* load */
  50. case 0x50000000: /* new load */
  51. return VM_READ;
  52. case 0x60000000: /* store */
  53. case 0x70000000: /* new store */
  54. return VM_WRITE;
  55. case 0x20000000: /* coproc */
  56. case 0x30000000: /* coproc2 */
  57. if (bit22set(inst))
  58. return VM_WRITE;
  59. case 0x0: /* indexed/memory management */
  60. if (bit22set(inst)) {
  61. /*
  62. * Check for the 'Graphics Flush Read' instruction.
  63. * It resembles an FDC instruction, except for bits
  64. * 20 and 21. Any combination other than zero will
  65. * utilize the block mover functionality on some
  66. * older PA-RISC platforms. The case where a block
  67. * move is performed from VM to graphics IO space
  68. * should be treated as a READ.
  69. *
  70. * The significance of bits 20,21 in the FDC
  71. * instruction is:
  72. *
  73. * 00 Flush data cache (normal instruction behavior)
  74. * 01 Graphics flush write (IO space -> VM)
  75. * 10 Graphics flush read (VM -> IO space)
  76. * 11 Graphics flush read/write (VM <-> IO space)
  77. */
  78. if (isGraphicsFlushRead(inst))
  79. return VM_READ;
  80. return VM_WRITE;
  81. } else {
  82. /*
  83. * Check for LDCWX and LDCWS (semaphore instructions).
  84. * If bits 23 through 25 are all 1's it is one of
  85. * the above two instructions and is a write.
  86. *
  87. * Note: With the limited bits we are looking at,
  88. * this will also catch PROBEW and PROBEWI. However,
  89. * these should never get in here because they don't
  90. * generate exceptions of the type:
  91. * Data TLB miss fault/data page fault
  92. * Data memory protection trap
  93. */
  94. if (bits23_25set(inst) == BITSSET)
  95. return VM_WRITE;
  96. }
  97. return VM_READ; /* Default */
  98. }
  99. return VM_READ; /* Default */
  100. }
  101. #undef bit22set
  102. #undef bits23_25set
  103. #undef isGraphicsFlushRead
  104. #undef BITSSET
  105. #if 0
  106. /* This is the treewalk to find a vma which is the highest that has
  107. * a start < addr. We're using find_vma_prev instead right now, but
  108. * we might want to use this at some point in the future. Probably
  109. * not, but I want it committed to CVS so I don't lose it :-)
  110. */
  111. while (tree != vm_avl_empty) {
  112. if (tree->vm_start > addr) {
  113. tree = tree->vm_avl_left;
  114. } else {
  115. prev = tree;
  116. if (prev->vm_next == NULL)
  117. break;
  118. if (prev->vm_next->vm_start > addr)
  119. break;
  120. tree = tree->vm_avl_right;
  121. }
  122. }
  123. #endif
  124. int fixup_exception(struct pt_regs *regs)
  125. {
  126. const struct exception_table_entry *fix;
  127. fix = search_exception_tables(regs->iaoq[0]);
  128. if (fix) {
  129. /*
  130. * Fix up get_user() and put_user().
  131. * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() sets the least-significant
  132. * bit in the relative address of the fixup routine to indicate
  133. * that %r8 should be loaded with -EFAULT to report a userspace
  134. * access error.
  135. */
  136. if (fix->fixup & 1) {
  137. regs->gr[8] = -EFAULT;
  138. /* zero target register for get_user() */
  139. if (parisc_acctyp(0, regs->iir) == VM_READ) {
  140. int treg = regs->iir & 0x1f;
  141. BUG_ON(treg == 0);
  142. regs->gr[treg] = 0;
  143. }
  144. }
  145. regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
  146. regs->iaoq[0] &= ~3;
  147. /*
  148. * NOTE: In some cases the faulting instruction
  149. * may be in the delay slot of a branch. We
  150. * don't want to take the branch, so we don't
  151. * increment iaoq[1], instead we set it to be
  152. * iaoq[0]+4, and clear the B bit in the PSW
  153. */
  154. regs->iaoq[1] = regs->iaoq[0] + 4;
  155. regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. /*
  161. * parisc hardware trap list
  162. *
  163. * Documented in section 3 "Addressing and Access Control" of the
  164. * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
  165. * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
  166. *
  167. * For implementation see handle_interruption() in traps.c
  168. */
  169. static const char * const trap_description[] = {
  170. [1] "High-priority machine check (HPMC)",
  171. [2] "Power failure interrupt",
  172. [3] "Recovery counter trap",
  173. [5] "Low-priority machine check",
  174. [6] "Instruction TLB miss fault",
  175. [7] "Instruction access rights / protection trap",
  176. [8] "Illegal instruction trap",
  177. [9] "Break instruction trap",
  178. [10] "Privileged operation trap",
  179. [11] "Privileged register trap",
  180. [12] "Overflow trap",
  181. [13] "Conditional trap",
  182. [14] "FP Assist Exception trap",
  183. [15] "Data TLB miss fault",
  184. [16] "Non-access ITLB miss fault",
  185. [17] "Non-access DTLB miss fault",
  186. [18] "Data memory protection/unaligned access trap",
  187. [19] "Data memory break trap",
  188. [20] "TLB dirty bit trap",
  189. [21] "Page reference trap",
  190. [22] "Assist emulation trap",
  191. [25] "Taken branch trap",
  192. [26] "Data memory access rights trap",
  193. [27] "Data memory protection ID trap",
  194. [28] "Unaligned data reference trap",
  195. };
  196. const char *trap_name(unsigned long code)
  197. {
  198. const char *t = NULL;
  199. if (code < ARRAY_SIZE(trap_description))
  200. t = trap_description[code];
  201. return t ? t : "Unknown trap";
  202. }
  203. /*
  204. * Print out info about fatal segfaults, if the show_unhandled_signals
  205. * sysctl is set:
  206. */
  207. static inline void
  208. show_signal_msg(struct pt_regs *regs, unsigned long code,
  209. unsigned long address, struct task_struct *tsk,
  210. struct vm_area_struct *vma)
  211. {
  212. if (!unhandled_signal(tsk, SIGSEGV))
  213. return;
  214. if (!printk_ratelimit())
  215. return;
  216. pr_warn("\n");
  217. pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
  218. tsk->comm, code, address);
  219. print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
  220. pr_cont("\ntrap #%lu: %s%c", code, trap_name(code),
  221. vma ? ',':'\n');
  222. if (vma)
  223. pr_cont(" vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  224. vma->vm_start, vma->vm_end);
  225. show_regs(regs);
  226. }
  227. void do_page_fault(struct pt_regs *regs, unsigned long code,
  228. unsigned long address)
  229. {
  230. struct vm_area_struct *vma, *prev_vma;
  231. struct task_struct *tsk;
  232. struct mm_struct *mm;
  233. unsigned long acc_type;
  234. vm_fault_t fault = 0;
  235. unsigned int flags;
  236. if (faulthandler_disabled())
  237. goto no_context;
  238. tsk = current;
  239. mm = tsk->mm;
  240. if (!mm)
  241. goto no_context;
  242. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  243. if (user_mode(regs))
  244. flags |= FAULT_FLAG_USER;
  245. acc_type = parisc_acctyp(code, regs->iir);
  246. if (acc_type & VM_WRITE)
  247. flags |= FAULT_FLAG_WRITE;
  248. retry:
  249. down_read(&mm->mmap_sem);
  250. vma = find_vma_prev(mm, address, &prev_vma);
  251. if (!vma || address < vma->vm_start)
  252. goto check_expansion;
  253. /*
  254. * Ok, we have a good vm_area for this memory access. We still need to
  255. * check the access permissions.
  256. */
  257. good_area:
  258. if ((vma->vm_flags & acc_type) != acc_type)
  259. goto bad_area;
  260. /*
  261. * If for any reason at all we couldn't handle the fault, make
  262. * sure we exit gracefully rather than endlessly redo the
  263. * fault.
  264. */
  265. fault = handle_mm_fault(vma, address, flags);
  266. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  267. return;
  268. if (unlikely(fault & VM_FAULT_ERROR)) {
  269. /*
  270. * We hit a shared mapping outside of the file, or some
  271. * other thing happened to us that made us unable to
  272. * handle the page fault gracefully.
  273. */
  274. if (fault & VM_FAULT_OOM)
  275. goto out_of_memory;
  276. else if (fault & VM_FAULT_SIGSEGV)
  277. goto bad_area;
  278. else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  279. VM_FAULT_HWPOISON_LARGE))
  280. goto bad_area;
  281. BUG();
  282. }
  283. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  284. if (fault & VM_FAULT_MAJOR)
  285. current->maj_flt++;
  286. else
  287. current->min_flt++;
  288. if (fault & VM_FAULT_RETRY) {
  289. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  290. /*
  291. * No need to up_read(&mm->mmap_sem) as we would
  292. * have already released it in __lock_page_or_retry
  293. * in mm/filemap.c.
  294. */
  295. goto retry;
  296. }
  297. }
  298. up_read(&mm->mmap_sem);
  299. return;
  300. check_expansion:
  301. vma = prev_vma;
  302. if (vma && (expand_stack(vma, address) == 0))
  303. goto good_area;
  304. /*
  305. * Something tried to access memory that isn't in our memory map..
  306. */
  307. bad_area:
  308. up_read(&mm->mmap_sem);
  309. if (user_mode(regs)) {
  310. int signo, si_code;
  311. switch (code) {
  312. case 15: /* Data TLB miss fault/Data page fault */
  313. /* send SIGSEGV when outside of vma */
  314. if (!vma ||
  315. address < vma->vm_start || address >= vma->vm_end) {
  316. signo = SIGSEGV;
  317. si_code = SEGV_MAPERR;
  318. break;
  319. }
  320. /* send SIGSEGV for wrong permissions */
  321. if ((vma->vm_flags & acc_type) != acc_type) {
  322. signo = SIGSEGV;
  323. si_code = SEGV_ACCERR;
  324. break;
  325. }
  326. /* probably address is outside of mapped file */
  327. /* fall through */
  328. case 17: /* NA data TLB miss / page fault */
  329. case 18: /* Unaligned access - PCXS only */
  330. signo = SIGBUS;
  331. si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
  332. break;
  333. case 16: /* Non-access instruction TLB miss fault */
  334. case 26: /* PCXL: Data memory access rights trap */
  335. default:
  336. signo = SIGSEGV;
  337. si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
  338. break;
  339. }
  340. #ifdef CONFIG_MEMORY_FAILURE
  341. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  342. unsigned int lsb = 0;
  343. printk(KERN_ERR
  344. "MCE: Killing %s:%d due to hardware memory corruption fault at %08lx\n",
  345. tsk->comm, tsk->pid, address);
  346. /*
  347. * Either small page or large page may be poisoned.
  348. * In other words, VM_FAULT_HWPOISON_LARGE and
  349. * VM_FAULT_HWPOISON are mutually exclusive.
  350. */
  351. if (fault & VM_FAULT_HWPOISON_LARGE)
  352. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  353. else if (fault & VM_FAULT_HWPOISON)
  354. lsb = PAGE_SHIFT;
  355. force_sig_mceerr(BUS_MCEERR_AR, (void __user *) address,
  356. lsb, current);
  357. return;
  358. }
  359. #endif
  360. show_signal_msg(regs, code, address, tsk, vma);
  361. force_sig_fault(signo, si_code, (void __user *) address, current);
  362. return;
  363. }
  364. no_context:
  365. if (!user_mode(regs) && fixup_exception(regs)) {
  366. return;
  367. }
  368. parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
  369. out_of_memory:
  370. up_read(&mm->mmap_sem);
  371. if (!user_mode(regs))
  372. goto no_context;
  373. pagefault_out_of_memory();
  374. }