fault.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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, [email protected])
  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 <linux/perf_event.h>
  21. #include <asm/traps.h>
  22. #define DEBUG_NATLB 0
  23. /* Various important other fields */
  24. #define bit22set(x) (x & 0x00000200)
  25. #define bits23_25set(x) (x & 0x000001c0)
  26. #define isGraphicsFlushRead(x) ((x & 0xfc003fdf) == 0x04001a80)
  27. /* extended opcode is 0x6a */
  28. #define BITSSET 0x1c0 /* for identifying LDCW */
  29. int show_unhandled_signals = 1;
  30. /*
  31. * parisc_acctyp(unsigned int inst) --
  32. * Given a PA-RISC memory access instruction, determine if the
  33. * instruction would perform a memory read or memory write
  34. * operation.
  35. *
  36. * This function assumes that the given instruction is a memory access
  37. * instruction (i.e. you should really only call it if you know that
  38. * the instruction has generated some sort of a memory access fault).
  39. *
  40. * Returns:
  41. * VM_READ if read operation
  42. * VM_WRITE if write operation
  43. * VM_EXEC if execute operation
  44. */
  45. unsigned long
  46. parisc_acctyp(unsigned long code, unsigned int inst)
  47. {
  48. if (code == 6 || code == 16)
  49. return VM_EXEC;
  50. switch (inst & 0xf0000000) {
  51. case 0x40000000: /* load */
  52. case 0x50000000: /* new load */
  53. return VM_READ;
  54. case 0x60000000: /* store */
  55. case 0x70000000: /* new store */
  56. return VM_WRITE;
  57. case 0x20000000: /* coproc */
  58. case 0x30000000: /* coproc2 */
  59. if (bit22set(inst))
  60. return VM_WRITE;
  61. fallthrough;
  62. case 0x0: /* indexed/memory management */
  63. if (bit22set(inst)) {
  64. /*
  65. * Check for the 'Graphics Flush Read' instruction.
  66. * It resembles an FDC instruction, except for bits
  67. * 20 and 21. Any combination other than zero will
  68. * utilize the block mover functionality on some
  69. * older PA-RISC platforms. The case where a block
  70. * move is performed from VM to graphics IO space
  71. * should be treated as a READ.
  72. *
  73. * The significance of bits 20,21 in the FDC
  74. * instruction is:
  75. *
  76. * 00 Flush data cache (normal instruction behavior)
  77. * 01 Graphics flush write (IO space -> VM)
  78. * 10 Graphics flush read (VM -> IO space)
  79. * 11 Graphics flush read/write (VM <-> IO space)
  80. */
  81. if (isGraphicsFlushRead(inst))
  82. return VM_READ;
  83. return VM_WRITE;
  84. } else {
  85. /*
  86. * Check for LDCWX and LDCWS (semaphore instructions).
  87. * If bits 23 through 25 are all 1's it is one of
  88. * the above two instructions and is a write.
  89. *
  90. * Note: With the limited bits we are looking at,
  91. * this will also catch PROBEW and PROBEWI. However,
  92. * these should never get in here because they don't
  93. * generate exceptions of the type:
  94. * Data TLB miss fault/data page fault
  95. * Data memory protection trap
  96. */
  97. if (bits23_25set(inst) == BITSSET)
  98. return VM_WRITE;
  99. }
  100. return VM_READ; /* Default */
  101. }
  102. return VM_READ; /* Default */
  103. }
  104. #undef bit22set
  105. #undef bits23_25set
  106. #undef isGraphicsFlushRead
  107. #undef BITSSET
  108. #if 0
  109. /* This is the treewalk to find a vma which is the highest that has
  110. * a start < addr. We're using find_vma_prev instead right now, but
  111. * we might want to use this at some point in the future. Probably
  112. * not, but I want it committed to CVS so I don't lose it :-)
  113. */
  114. while (tree != vm_avl_empty) {
  115. if (tree->vm_start > addr) {
  116. tree = tree->vm_avl_left;
  117. } else {
  118. prev = tree;
  119. if (prev->vm_next == NULL)
  120. break;
  121. if (prev->vm_next->vm_start > addr)
  122. break;
  123. tree = tree->vm_avl_right;
  124. }
  125. }
  126. #endif
  127. int fixup_exception(struct pt_regs *regs)
  128. {
  129. const struct exception_table_entry *fix;
  130. fix = search_exception_tables(regs->iaoq[0]);
  131. if (fix) {
  132. /*
  133. * Fix up get_user() and put_user().
  134. * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() sets the least-significant
  135. * bit in the relative address of the fixup routine to indicate
  136. * that gr[ASM_EXCEPTIONTABLE_REG] should be loaded with
  137. * -EFAULT to report a userspace access error.
  138. */
  139. if (fix->fixup & 1) {
  140. regs->gr[ASM_EXCEPTIONTABLE_REG] = -EFAULT;
  141. /* zero target register for get_user() */
  142. if (parisc_acctyp(0, regs->iir) == VM_READ) {
  143. int treg = regs->iir & 0x1f;
  144. BUG_ON(treg == 0);
  145. regs->gr[treg] = 0;
  146. }
  147. }
  148. regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
  149. regs->iaoq[0] &= ~3;
  150. /*
  151. * NOTE: In some cases the faulting instruction
  152. * may be in the delay slot of a branch. We
  153. * don't want to take the branch, so we don't
  154. * increment iaoq[1], instead we set it to be
  155. * iaoq[0]+4, and clear the B bit in the PSW
  156. */
  157. regs->iaoq[1] = regs->iaoq[0] + 4;
  158. regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. /*
  164. * parisc hardware trap list
  165. *
  166. * Documented in section 3 "Addressing and Access Control" of the
  167. * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
  168. * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
  169. *
  170. * For implementation see handle_interruption() in traps.c
  171. */
  172. static const char * const trap_description[] = {
  173. [1] "High-priority machine check (HPMC)",
  174. [2] "Power failure interrupt",
  175. [3] "Recovery counter trap",
  176. [5] "Low-priority machine check",
  177. [6] "Instruction TLB miss fault",
  178. [7] "Instruction access rights / protection trap",
  179. [8] "Illegal instruction trap",
  180. [9] "Break instruction trap",
  181. [10] "Privileged operation trap",
  182. [11] "Privileged register trap",
  183. [12] "Overflow trap",
  184. [13] "Conditional trap",
  185. [14] "FP Assist Exception trap",
  186. [15] "Data TLB miss fault",
  187. [16] "Non-access ITLB miss fault",
  188. [17] "Non-access DTLB miss fault",
  189. [18] "Data memory protection/unaligned access trap",
  190. [19] "Data memory break trap",
  191. [20] "TLB dirty bit trap",
  192. [21] "Page reference trap",
  193. [22] "Assist emulation trap",
  194. [25] "Taken branch trap",
  195. [26] "Data memory access rights trap",
  196. [27] "Data memory protection ID trap",
  197. [28] "Unaligned data reference trap",
  198. };
  199. const char *trap_name(unsigned long code)
  200. {
  201. const char *t = NULL;
  202. if (code < ARRAY_SIZE(trap_description))
  203. t = trap_description[code];
  204. return t ? t : "Unknown trap";
  205. }
  206. /*
  207. * Print out info about fatal segfaults, if the show_unhandled_signals
  208. * sysctl is set:
  209. */
  210. static inline void
  211. show_signal_msg(struct pt_regs *regs, unsigned long code,
  212. unsigned long address, struct task_struct *tsk,
  213. struct vm_area_struct *vma)
  214. {
  215. if (!unhandled_signal(tsk, SIGSEGV))
  216. return;
  217. if (!printk_ratelimit())
  218. return;
  219. pr_warn("\n");
  220. pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
  221. tsk->comm, code, address);
  222. print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
  223. pr_cont("\ntrap #%lu: %s%c", code, trap_name(code),
  224. vma ? ',':'\n');
  225. if (vma)
  226. pr_cont(" vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  227. vma->vm_start, vma->vm_end);
  228. show_regs(regs);
  229. }
  230. void do_page_fault(struct pt_regs *regs, unsigned long code,
  231. unsigned long address)
  232. {
  233. struct vm_area_struct *vma, *prev_vma;
  234. struct task_struct *tsk;
  235. struct mm_struct *mm;
  236. unsigned long acc_type;
  237. vm_fault_t fault = 0;
  238. unsigned int flags;
  239. char *msg;
  240. tsk = current;
  241. mm = tsk->mm;
  242. if (!mm) {
  243. msg = "Page fault: no context";
  244. goto no_context;
  245. }
  246. flags = FAULT_FLAG_DEFAULT;
  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. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  253. retry:
  254. mmap_read_lock(mm);
  255. vma = find_vma_prev(mm, address, &prev_vma);
  256. if (!vma || address < vma->vm_start) {
  257. if (!prev || !(prev->vm_flags & VM_GROWSUP))
  258. goto bad_area;
  259. vma = expand_stack(mm, address);
  260. if (!vma)
  261. goto bad_area_nosemaphore;
  262. }
  263. /*
  264. * Ok, we have a good vm_area for this memory access. We still need to
  265. * check the access permissions.
  266. */
  267. if ((vma->vm_flags & acc_type) != acc_type)
  268. goto bad_area;
  269. /*
  270. * If for any reason at all we couldn't handle the fault, make
  271. * sure we exit gracefully rather than endlessly redo the
  272. * fault.
  273. */
  274. fault = handle_mm_fault(vma, address, flags, regs);
  275. if (fault_signal_pending(fault, regs))
  276. return;
  277. /* The fault is fully completed (including releasing mmap lock) */
  278. if (fault & VM_FAULT_COMPLETED)
  279. return;
  280. if (unlikely(fault & VM_FAULT_ERROR)) {
  281. /*
  282. * We hit a shared mapping outside of the file, or some
  283. * other thing happened to us that made us unable to
  284. * handle the page fault gracefully.
  285. */
  286. if (fault & VM_FAULT_OOM)
  287. goto out_of_memory;
  288. else if (fault & VM_FAULT_SIGSEGV)
  289. goto bad_area;
  290. else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  291. VM_FAULT_HWPOISON_LARGE))
  292. goto bad_area;
  293. BUG();
  294. }
  295. if (fault & VM_FAULT_RETRY) {
  296. /*
  297. * No need to mmap_read_unlock(mm) as we would
  298. * have already released it in __lock_page_or_retry
  299. * in mm/filemap.c.
  300. */
  301. flags |= FAULT_FLAG_TRIED;
  302. goto retry;
  303. }
  304. mmap_read_unlock(mm);
  305. return;
  306. /*
  307. * Something tried to access memory that isn't in our memory map..
  308. */
  309. bad_area:
  310. mmap_read_unlock(mm);
  311. bad_area_nosemaphore:
  312. if (user_mode(regs)) {
  313. int signo, si_code;
  314. switch (code) {
  315. case 15: /* Data TLB miss fault/Data page fault */
  316. /* send SIGSEGV when outside of vma */
  317. if (!vma ||
  318. address < vma->vm_start || address >= vma->vm_end) {
  319. signo = SIGSEGV;
  320. si_code = SEGV_MAPERR;
  321. break;
  322. }
  323. /* send SIGSEGV for wrong permissions */
  324. if ((vma->vm_flags & acc_type) != acc_type) {
  325. signo = SIGSEGV;
  326. si_code = SEGV_ACCERR;
  327. break;
  328. }
  329. /* probably address is outside of mapped file */
  330. fallthrough;
  331. case 17: /* NA data TLB miss / page fault */
  332. case 18: /* Unaligned access - PCXS only */
  333. signo = SIGBUS;
  334. si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
  335. break;
  336. case 16: /* Non-access instruction TLB miss fault */
  337. case 26: /* PCXL: Data memory access rights trap */
  338. default:
  339. signo = SIGSEGV;
  340. si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
  341. break;
  342. }
  343. #ifdef CONFIG_MEMORY_FAILURE
  344. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  345. unsigned int lsb = 0;
  346. printk(KERN_ERR
  347. "MCE: Killing %s:%d due to hardware memory corruption fault at %08lx\n",
  348. tsk->comm, tsk->pid, address);
  349. /*
  350. * Either small page or large page may be poisoned.
  351. * In other words, VM_FAULT_HWPOISON_LARGE and
  352. * VM_FAULT_HWPOISON are mutually exclusive.
  353. */
  354. if (fault & VM_FAULT_HWPOISON_LARGE)
  355. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  356. else if (fault & VM_FAULT_HWPOISON)
  357. lsb = PAGE_SHIFT;
  358. force_sig_mceerr(BUS_MCEERR_AR, (void __user *) address,
  359. lsb);
  360. return;
  361. }
  362. #endif
  363. show_signal_msg(regs, code, address, tsk, vma);
  364. force_sig_fault(signo, si_code, (void __user *) address);
  365. return;
  366. }
  367. msg = "Page fault: bad address";
  368. no_context:
  369. if (!user_mode(regs) && fixup_exception(regs)) {
  370. return;
  371. }
  372. parisc_terminate(msg, regs, code, address);
  373. out_of_memory:
  374. mmap_read_unlock(mm);
  375. if (!user_mode(regs)) {
  376. msg = "Page fault: out of memory";
  377. goto no_context;
  378. }
  379. pagefault_out_of_memory();
  380. }
  381. /* Handle non-access data TLB miss faults.
  382. *
  383. * For probe instructions, accesses to userspace are considered allowed
  384. * if they lie in a valid VMA and the access type matches. We are not
  385. * allowed to handle MM faults here so there may be situations where an
  386. * actual access would fail even though a probe was successful.
  387. */
  388. int
  389. handle_nadtlb_fault(struct pt_regs *regs)
  390. {
  391. unsigned long insn = regs->iir;
  392. int breg, treg, xreg, val = 0;
  393. struct vm_area_struct *vma;
  394. struct task_struct *tsk;
  395. struct mm_struct *mm;
  396. unsigned long address;
  397. unsigned long acc_type;
  398. switch (insn & 0x380) {
  399. case 0x280:
  400. /* FDC instruction */
  401. fallthrough;
  402. case 0x380:
  403. /* PDC and FIC instructions */
  404. if (DEBUG_NATLB && printk_ratelimit()) {
  405. pr_warn("WARNING: nullifying cache flush/purge instruction\n");
  406. show_regs(regs);
  407. }
  408. if (insn & 0x20) {
  409. /* Base modification */
  410. breg = (insn >> 21) & 0x1f;
  411. xreg = (insn >> 16) & 0x1f;
  412. if (breg && xreg)
  413. regs->gr[breg] += regs->gr[xreg];
  414. }
  415. regs->gr[0] |= PSW_N;
  416. return 1;
  417. case 0x180:
  418. /* PROBE instruction */
  419. treg = insn & 0x1f;
  420. if (regs->isr) {
  421. tsk = current;
  422. mm = tsk->mm;
  423. if (mm) {
  424. /* Search for VMA */
  425. address = regs->ior;
  426. mmap_read_lock(mm);
  427. vma = vma_lookup(mm, address);
  428. mmap_read_unlock(mm);
  429. /*
  430. * Check if access to the VMA is okay.
  431. * We don't allow for stack expansion.
  432. */
  433. acc_type = (insn & 0x40) ? VM_WRITE : VM_READ;
  434. if (vma
  435. && (vma->vm_flags & acc_type) == acc_type)
  436. val = 1;
  437. }
  438. }
  439. if (treg)
  440. regs->gr[treg] = val;
  441. regs->gr[0] |= PSW_N;
  442. return 1;
  443. case 0x300:
  444. /* LPA instruction */
  445. if (insn & 0x20) {
  446. /* Base modification */
  447. breg = (insn >> 21) & 0x1f;
  448. xreg = (insn >> 16) & 0x1f;
  449. if (breg && xreg)
  450. regs->gr[breg] += regs->gr[xreg];
  451. }
  452. treg = insn & 0x1f;
  453. if (treg)
  454. regs->gr[treg] = 0;
  455. regs->gr[0] |= PSW_N;
  456. return 1;
  457. default:
  458. break;
  459. }
  460. return 0;
  461. }