fault.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 <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 the register encoded in the "or %r0,%r0,register"
  137. * opcode should be loaded with -EFAULT to report a userspace
  138. * access error.
  139. */
  140. if (fix->fixup & 1) {
  141. int fault_error_reg = fix->err_opcode & 0x1f;
  142. if (!WARN_ON(!fault_error_reg))
  143. regs->gr[fault_error_reg] = -EFAULT;
  144. pr_debug("Unalignment fixup of register %d at %pS\n",
  145. fault_error_reg, (void*)regs->iaoq[0]);
  146. /* zero target register for get_user() */
  147. if (parisc_acctyp(0, regs->iir) == VM_READ) {
  148. int treg = regs->iir & 0x1f;
  149. BUG_ON(treg == 0);
  150. regs->gr[treg] = 0;
  151. }
  152. }
  153. regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
  154. regs->iaoq[0] &= ~3;
  155. /*
  156. * NOTE: In some cases the faulting instruction
  157. * may be in the delay slot of a branch. We
  158. * don't want to take the branch, so we don't
  159. * increment iaoq[1], instead we set it to be
  160. * iaoq[0]+4, and clear the B bit in the PSW
  161. */
  162. regs->iaoq[1] = regs->iaoq[0] + 4;
  163. regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. /*
  169. * parisc hardware trap list
  170. *
  171. * Documented in section 3 "Addressing and Access Control" of the
  172. * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
  173. * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
  174. *
  175. * For implementation see handle_interruption() in traps.c
  176. */
  177. static const char * const trap_description[] = {
  178. [1] = "High-priority machine check (HPMC)",
  179. [2] = "Power failure interrupt",
  180. [3] = "Recovery counter trap",
  181. [5] = "Low-priority machine check",
  182. [6] = "Instruction TLB miss fault",
  183. [7] = "Instruction access rights / protection trap",
  184. [8] = "Illegal instruction trap",
  185. [9] = "Break instruction trap",
  186. [10] = "Privileged operation trap",
  187. [11] = "Privileged register trap",
  188. [12] = "Overflow trap",
  189. [13] = "Conditional trap",
  190. [14] = "FP Assist Exception trap",
  191. [15] = "Data TLB miss fault",
  192. [16] = "Non-access ITLB miss fault",
  193. [17] = "Non-access DTLB miss fault",
  194. [18] = "Data memory protection/unaligned access trap",
  195. [19] = "Data memory break trap",
  196. [20] = "TLB dirty bit trap",
  197. [21] = "Page reference trap",
  198. [22] = "Assist emulation trap",
  199. [25] = "Taken branch trap",
  200. [26] = "Data memory access rights trap",
  201. [27] = "Data memory protection ID trap",
  202. [28] = "Unaligned data reference trap",
  203. };
  204. const char *trap_name(unsigned long code)
  205. {
  206. const char *t = NULL;
  207. if (code < ARRAY_SIZE(trap_description))
  208. t = trap_description[code];
  209. return t ? t : "Unknown trap";
  210. }
  211. /*
  212. * Print out info about fatal segfaults, if the show_unhandled_signals
  213. * sysctl is set:
  214. */
  215. static inline void
  216. show_signal_msg(struct pt_regs *regs, unsigned long code,
  217. unsigned long address, struct task_struct *tsk,
  218. struct vm_area_struct *vma)
  219. {
  220. if (!unhandled_signal(tsk, SIGSEGV))
  221. return;
  222. if (!printk_ratelimit())
  223. return;
  224. pr_warn("\n");
  225. pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
  226. tsk->comm, code, address);
  227. print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
  228. pr_cont("\ntrap #%lu: %s%c", code, trap_name(code),
  229. vma ? ',':'\n');
  230. if (vma)
  231. pr_cont(" vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  232. vma->vm_start, vma->vm_end);
  233. show_regs(regs);
  234. }
  235. void do_page_fault(struct pt_regs *regs, unsigned long code,
  236. unsigned long address)
  237. {
  238. struct vm_area_struct *vma, *prev_vma;
  239. struct task_struct *tsk;
  240. struct mm_struct *mm;
  241. unsigned long acc_type;
  242. vm_fault_t fault = 0;
  243. unsigned int flags;
  244. char *msg;
  245. tsk = current;
  246. mm = tsk->mm;
  247. if (!mm) {
  248. msg = "Page fault: no context";
  249. goto no_context;
  250. }
  251. flags = FAULT_FLAG_DEFAULT;
  252. if (user_mode(regs))
  253. flags |= FAULT_FLAG_USER;
  254. acc_type = parisc_acctyp(code, regs->iir);
  255. if (acc_type & VM_WRITE)
  256. flags |= FAULT_FLAG_WRITE;
  257. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  258. retry:
  259. mmap_read_lock(mm);
  260. vma = find_vma_prev(mm, address, &prev_vma);
  261. if (!vma || address < vma->vm_start) {
  262. if (!prev_vma || !(prev_vma->vm_flags & VM_GROWSUP))
  263. goto bad_area;
  264. vma = expand_stack(mm, address);
  265. if (!vma)
  266. goto bad_area_nosemaphore;
  267. }
  268. /*
  269. * Ok, we have a good vm_area for this memory access. We still need to
  270. * check the access permissions.
  271. */
  272. if ((vma->vm_flags & acc_type) != acc_type)
  273. goto bad_area;
  274. /*
  275. * If for any reason at all we couldn't handle the fault, make
  276. * sure we exit gracefully rather than endlessly redo the
  277. * fault.
  278. */
  279. fault = handle_mm_fault(vma, address, flags, regs);
  280. if (fault_signal_pending(fault, regs)) {
  281. if (!user_mode(regs)) {
  282. msg = "Page fault: fault signal on kernel memory";
  283. goto no_context;
  284. }
  285. return;
  286. }
  287. /* The fault is fully completed (including releasing mmap lock) */
  288. if (fault & VM_FAULT_COMPLETED)
  289. return;
  290. if (unlikely(fault & VM_FAULT_ERROR)) {
  291. /*
  292. * We hit a shared mapping outside of the file, or some
  293. * other thing happened to us that made us unable to
  294. * handle the page fault gracefully.
  295. */
  296. if (fault & VM_FAULT_OOM)
  297. goto out_of_memory;
  298. else if (fault & VM_FAULT_SIGSEGV)
  299. goto bad_area;
  300. else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  301. VM_FAULT_HWPOISON_LARGE))
  302. goto bad_area;
  303. BUG();
  304. }
  305. if (fault & VM_FAULT_RETRY) {
  306. /*
  307. * No need to mmap_read_unlock(mm) as we would
  308. * have already released it in __lock_page_or_retry
  309. * in mm/filemap.c.
  310. */
  311. flags |= FAULT_FLAG_TRIED;
  312. goto retry;
  313. }
  314. mmap_read_unlock(mm);
  315. return;
  316. /*
  317. * Something tried to access memory that isn't in our memory map..
  318. */
  319. bad_area:
  320. mmap_read_unlock(mm);
  321. bad_area_nosemaphore:
  322. if (!user_mode(regs) && fixup_exception(regs)) {
  323. return;
  324. }
  325. if (user_mode(regs)) {
  326. int signo, si_code;
  327. switch (code) {
  328. case 15: /* Data TLB miss fault/Data page fault */
  329. /* send SIGSEGV when outside of vma */
  330. if (!vma ||
  331. address < vma->vm_start || address >= vma->vm_end) {
  332. signo = SIGSEGV;
  333. si_code = SEGV_MAPERR;
  334. break;
  335. }
  336. /* send SIGSEGV for wrong permissions */
  337. if ((vma->vm_flags & acc_type) != acc_type) {
  338. signo = SIGSEGV;
  339. si_code = SEGV_ACCERR;
  340. break;
  341. }
  342. /* probably address is outside of mapped file */
  343. fallthrough;
  344. case 17: /* NA data TLB miss / page fault */
  345. case 18: /* Unaligned access - PCXS only */
  346. signo = SIGBUS;
  347. si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
  348. break;
  349. case 16: /* Non-access instruction TLB miss fault */
  350. case 26: /* PCXL: Data memory access rights trap */
  351. default:
  352. signo = SIGSEGV;
  353. si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
  354. break;
  355. }
  356. #ifdef CONFIG_MEMORY_FAILURE
  357. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  358. unsigned int lsb = 0;
  359. printk(KERN_ERR
  360. "MCE: Killing %s:%d due to hardware memory corruption fault at %08lx\n",
  361. tsk->comm, tsk->pid, address);
  362. /*
  363. * Either small page or large page may be poisoned.
  364. * In other words, VM_FAULT_HWPOISON_LARGE and
  365. * VM_FAULT_HWPOISON are mutually exclusive.
  366. */
  367. if (fault & VM_FAULT_HWPOISON_LARGE)
  368. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  369. else if (fault & VM_FAULT_HWPOISON)
  370. lsb = PAGE_SHIFT;
  371. force_sig_mceerr(BUS_MCEERR_AR, (void __user *) address,
  372. lsb);
  373. return;
  374. }
  375. #endif
  376. show_signal_msg(regs, code, address, tsk, vma);
  377. force_sig_fault(signo, si_code, (void __user *) address);
  378. return;
  379. }
  380. msg = "Page fault: bad address";
  381. no_context:
  382. if (!user_mode(regs) && fixup_exception(regs)) {
  383. return;
  384. }
  385. parisc_terminate(msg, regs, code, address);
  386. out_of_memory:
  387. mmap_read_unlock(mm);
  388. if (!user_mode(regs)) {
  389. msg = "Page fault: out of memory";
  390. goto no_context;
  391. }
  392. pagefault_out_of_memory();
  393. }
  394. /* Handle non-access data TLB miss faults.
  395. *
  396. * For probe instructions, accesses to userspace are considered allowed
  397. * if they lie in a valid VMA and the access type matches. We are not
  398. * allowed to handle MM faults here so there may be situations where an
  399. * actual access would fail even though a probe was successful.
  400. */
  401. int
  402. handle_nadtlb_fault(struct pt_regs *regs)
  403. {
  404. unsigned long insn = regs->iir;
  405. int breg, treg, xreg, val = 0;
  406. struct vm_area_struct *vma;
  407. struct task_struct *tsk;
  408. struct mm_struct *mm;
  409. unsigned long address;
  410. unsigned long acc_type;
  411. switch (insn & 0x380) {
  412. case 0x280:
  413. /* FDC instruction */
  414. fallthrough;
  415. case 0x380:
  416. /* PDC and FIC instructions */
  417. if (DEBUG_NATLB && printk_ratelimit()) {
  418. pr_warn("WARNING: nullifying cache flush/purge instruction\n");
  419. show_regs(regs);
  420. }
  421. if (insn & 0x20) {
  422. /* Base modification */
  423. breg = (insn >> 21) & 0x1f;
  424. xreg = (insn >> 16) & 0x1f;
  425. if (breg && xreg)
  426. regs->gr[breg] += regs->gr[xreg];
  427. }
  428. regs->gr[0] |= PSW_N;
  429. return 1;
  430. case 0x180:
  431. /* PROBE instruction */
  432. treg = insn & 0x1f;
  433. if (regs->isr) {
  434. tsk = current;
  435. mm = tsk->mm;
  436. if (mm) {
  437. /* Search for VMA */
  438. address = regs->ior;
  439. mmap_read_lock(mm);
  440. vma = vma_lookup(mm, address);
  441. mmap_read_unlock(mm);
  442. /*
  443. * Check if access to the VMA is okay.
  444. * We don't allow for stack expansion.
  445. */
  446. acc_type = (insn & 0x40) ? VM_WRITE : VM_READ;
  447. if (vma
  448. && (vma->vm_flags & acc_type) == acc_type)
  449. val = 1;
  450. }
  451. }
  452. if (treg)
  453. regs->gr[treg] = val;
  454. regs->gr[0] |= PSW_N;
  455. return 1;
  456. case 0x300:
  457. /* LPA instruction */
  458. if (insn & 0x20) {
  459. /* Base modification */
  460. breg = (insn >> 21) & 0x1f;
  461. xreg = (insn >> 16) & 0x1f;
  462. if (breg && xreg)
  463. regs->gr[breg] += regs->gr[xreg];
  464. }
  465. treg = insn & 0x1f;
  466. if (treg)
  467. regs->gr[treg] = 0;
  468. regs->gr[0] |= PSW_N;
  469. return 1;
  470. default:
  471. break;
  472. }
  473. return 0;
  474. }