fault.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/interrupt.h>
  16. #include <linux/extable.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/traps.h>
  19. /* Various important other fields */
  20. #define bit22set(x) (x & 0x00000200)
  21. #define bits23_25set(x) (x & 0x000001c0)
  22. #define isGraphicsFlushRead(x) ((x & 0xfc003fdf) == 0x04001a80)
  23. /* extended opcode is 0x6a */
  24. #define BITSSET 0x1c0 /* for identifying LDCW */
  25. DEFINE_PER_CPU(struct exception_data, exception_data);
  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. struct exception_data *d;
  129. d = this_cpu_ptr(&exception_data);
  130. d->fault_ip = regs->iaoq[0];
  131. d->fault_gp = regs->gr[27];
  132. d->fault_space = regs->isr;
  133. d->fault_addr = regs->ior;
  134. /*
  135. * Fix up get_user() and put_user().
  136. * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() sets the least-significant
  137. * bit in the relative address of the fixup routine to indicate
  138. * that %r8 should be loaded with -EFAULT to report a userspace
  139. * access error.
  140. */
  141. if (fix->fixup & 1) {
  142. regs->gr[8] = -EFAULT;
  143. /* zero target register for get_user() */
  144. if (parisc_acctyp(0, regs->iir) == VM_READ) {
  145. int treg = regs->iir & 0x1f;
  146. regs->gr[treg] = 0;
  147. }
  148. }
  149. regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
  150. regs->iaoq[0] &= ~3;
  151. /*
  152. * NOTE: In some cases the faulting instruction
  153. * may be in the delay slot of a branch. We
  154. * don't want to take the branch, so we don't
  155. * increment iaoq[1], instead we set it to be
  156. * iaoq[0]+4, and clear the B bit in the PSW
  157. */
  158. regs->iaoq[1] = regs->iaoq[0] + 4;
  159. regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. /*
  165. * parisc hardware trap list
  166. *
  167. * Documented in section 3 "Addressing and Access Control" of the
  168. * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
  169. * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
  170. *
  171. * For implementation see handle_interruption() in traps.c
  172. */
  173. static const char * const trap_description[] = {
  174. [1] "High-priority machine check (HPMC)",
  175. [2] "Power failure interrupt",
  176. [3] "Recovery counter trap",
  177. [5] "Low-priority machine check",
  178. [6] "Instruction TLB miss fault",
  179. [7] "Instruction access rights / protection trap",
  180. [8] "Illegal instruction trap",
  181. [9] "Break instruction trap",
  182. [10] "Privileged operation trap",
  183. [11] "Privileged register trap",
  184. [12] "Overflow trap",
  185. [13] "Conditional trap",
  186. [14] "FP Assist Exception trap",
  187. [15] "Data TLB miss fault",
  188. [16] "Non-access ITLB miss fault",
  189. [17] "Non-access DTLB miss fault",
  190. [18] "Data memory protection/unaligned access trap",
  191. [19] "Data memory break trap",
  192. [20] "TLB dirty bit trap",
  193. [21] "Page reference trap",
  194. [22] "Assist emulation trap",
  195. [25] "Taken branch trap",
  196. [26] "Data memory access rights trap",
  197. [27] "Data memory protection ID trap",
  198. [28] "Unaligned data reference trap",
  199. };
  200. const char *trap_name(unsigned long code)
  201. {
  202. const char *t = NULL;
  203. if (code < ARRAY_SIZE(trap_description))
  204. t = trap_description[code];
  205. return t ? t : "Unknown trap";
  206. }
  207. /*
  208. * Print out info about fatal segfaults, if the show_unhandled_signals
  209. * sysctl is set:
  210. */
  211. static inline void
  212. show_signal_msg(struct pt_regs *regs, unsigned long code,
  213. unsigned long address, struct task_struct *tsk,
  214. struct vm_area_struct *vma)
  215. {
  216. if (!unhandled_signal(tsk, SIGSEGV))
  217. return;
  218. if (!printk_ratelimit())
  219. return;
  220. pr_warn("\n");
  221. pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
  222. tsk->comm, code, address);
  223. print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
  224. pr_cont("\ntrap #%lu: %s%c", code, trap_name(code),
  225. vma ? ',':'\n');
  226. if (vma)
  227. pr_warn(KERN_CONT " vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  228. vma->vm_start, vma->vm_end);
  229. show_regs(regs);
  230. }
  231. void do_page_fault(struct pt_regs *regs, unsigned long code,
  232. unsigned long address)
  233. {
  234. struct vm_area_struct *vma, *prev_vma;
  235. struct task_struct *tsk;
  236. struct mm_struct *mm;
  237. unsigned long acc_type;
  238. int fault;
  239. unsigned int flags;
  240. if (faulthandler_disabled())
  241. goto no_context;
  242. tsk = current;
  243. mm = tsk->mm;
  244. if (!mm)
  245. goto no_context;
  246. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  247. if (user_mode(regs))
  248. flags |= FAULT_FLAG_USER;
  249. acc_type = parisc_acctyp(code, regs->iir);
  250. if (acc_type & VM_WRITE)
  251. flags |= FAULT_FLAG_WRITE;
  252. retry:
  253. down_read(&mm->mmap_sem);
  254. vma = find_vma_prev(mm, address, &prev_vma);
  255. if (!vma || address < vma->vm_start)
  256. goto check_expansion;
  257. /*
  258. * Ok, we have a good vm_area for this memory access. We still need to
  259. * check the access permissions.
  260. */
  261. good_area:
  262. if ((vma->vm_flags & acc_type) != acc_type)
  263. goto bad_area;
  264. /*
  265. * If for any reason at all we couldn't handle the fault, make
  266. * sure we exit gracefully rather than endlessly redo the
  267. * fault.
  268. */
  269. fault = handle_mm_fault(vma, address, flags);
  270. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  271. return;
  272. if (unlikely(fault & VM_FAULT_ERROR)) {
  273. /*
  274. * We hit a shared mapping outside of the file, or some
  275. * other thing happened to us that made us unable to
  276. * handle the page fault gracefully.
  277. */
  278. if (fault & VM_FAULT_OOM)
  279. goto out_of_memory;
  280. else if (fault & VM_FAULT_SIGSEGV)
  281. goto bad_area;
  282. else if (fault & VM_FAULT_SIGBUS)
  283. goto bad_area;
  284. BUG();
  285. }
  286. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  287. if (fault & VM_FAULT_MAJOR)
  288. current->maj_flt++;
  289. else
  290. current->min_flt++;
  291. if (fault & VM_FAULT_RETRY) {
  292. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  293. /*
  294. * No need to up_read(&mm->mmap_sem) as we would
  295. * have already released it in __lock_page_or_retry
  296. * in mm/filemap.c.
  297. */
  298. goto retry;
  299. }
  300. }
  301. up_read(&mm->mmap_sem);
  302. return;
  303. check_expansion:
  304. vma = prev_vma;
  305. if (vma && (expand_stack(vma, address) == 0))
  306. goto good_area;
  307. /*
  308. * Something tried to access memory that isn't in our memory map..
  309. */
  310. bad_area:
  311. up_read(&mm->mmap_sem);
  312. if (user_mode(regs)) {
  313. struct siginfo si;
  314. show_signal_msg(regs, code, address, tsk, vma);
  315. switch (code) {
  316. case 15: /* Data TLB miss fault/Data page fault */
  317. /* send SIGSEGV when outside of vma */
  318. if (!vma ||
  319. address < vma->vm_start || address >= vma->vm_end) {
  320. si.si_signo = SIGSEGV;
  321. si.si_code = SEGV_MAPERR;
  322. break;
  323. }
  324. /* send SIGSEGV for wrong permissions */
  325. if ((vma->vm_flags & acc_type) != acc_type) {
  326. si.si_signo = SIGSEGV;
  327. si.si_code = SEGV_ACCERR;
  328. break;
  329. }
  330. /* probably address is outside of mapped file */
  331. /* fall through */
  332. case 17: /* NA data TLB miss / page fault */
  333. case 18: /* Unaligned access - PCXS only */
  334. si.si_signo = SIGBUS;
  335. si.si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
  336. break;
  337. case 16: /* Non-access instruction TLB miss fault */
  338. case 26: /* PCXL: Data memory access rights trap */
  339. default:
  340. si.si_signo = SIGSEGV;
  341. si.si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
  342. break;
  343. }
  344. si.si_errno = 0;
  345. si.si_addr = (void __user *) address;
  346. force_sig_info(si.si_signo, &si, current);
  347. return;
  348. }
  349. no_context:
  350. if (!user_mode(regs) && fixup_exception(regs)) {
  351. return;
  352. }
  353. parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
  354. out_of_memory:
  355. up_read(&mm->mmap_sem);
  356. if (!user_mode(regs))
  357. goto no_context;
  358. pagefault_out_of_memory();
  359. }