fault.c 10 KB

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