traps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 1999, 2000
  5. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  6. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  7. *
  8. * Derived from "arch/i386/kernel/traps.c"
  9. * Copyright (C) 1991, 1992 Linus Torvalds
  10. */
  11. /*
  12. * 'Traps.c' handles hardware traps and faults after we have saved some
  13. * state in 'asm.s'.
  14. */
  15. #include "asm/irqflags.h"
  16. #include "asm/ptrace.h"
  17. #include <linux/kprobes.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/randomize_kstack.h>
  20. #include <linux/extable.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/sched.h>
  23. #include <linux/sched/debug.h>
  24. #include <linux/mm.h>
  25. #include <linux/slab.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/cpu.h>
  28. #include <linux/entry-common.h>
  29. #include <linux/kmsan.h>
  30. #include <asm/asm-extable.h>
  31. #include <asm/vtime.h>
  32. #include <asm/fpu.h>
  33. #include "entry.h"
  34. static inline void __user *get_trap_ip(struct pt_regs *regs)
  35. {
  36. unsigned long address;
  37. if (regs->int_code & 0x200)
  38. address = current->thread.trap_tdb.data[3];
  39. else
  40. address = regs->psw.addr;
  41. return (void __user *) (address - (regs->int_code >> 16));
  42. }
  43. #ifdef CONFIG_GENERIC_BUG
  44. int is_valid_bugaddr(unsigned long addr)
  45. {
  46. return 1;
  47. }
  48. #endif
  49. void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  50. {
  51. if (user_mode(regs)) {
  52. force_sig_fault(si_signo, si_code, get_trap_ip(regs));
  53. report_user_fault(regs, si_signo, 0);
  54. } else {
  55. if (!fixup_exception(regs))
  56. die(regs, str);
  57. }
  58. }
  59. static void do_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  60. {
  61. if (notify_die(DIE_TRAP, str, regs, 0,
  62. regs->int_code, si_signo) == NOTIFY_STOP)
  63. return;
  64. do_report_trap(regs, si_signo, si_code, str);
  65. }
  66. NOKPROBE_SYMBOL(do_trap);
  67. void do_per_trap(struct pt_regs *regs)
  68. {
  69. if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  70. return;
  71. if (!current->ptrace)
  72. return;
  73. force_sig_fault(SIGTRAP, TRAP_HWBKPT,
  74. (void __force __user *) current->thread.per_event.address);
  75. }
  76. NOKPROBE_SYMBOL(do_per_trap);
  77. static void default_trap_handler(struct pt_regs *regs)
  78. {
  79. if (user_mode(regs)) {
  80. report_user_fault(regs, SIGSEGV, 0);
  81. force_exit_sig(SIGSEGV);
  82. } else
  83. die(regs, "Unknown program exception");
  84. }
  85. #define DO_ERROR_INFO(name, signr, sicode, str) \
  86. static void name(struct pt_regs *regs) \
  87. { \
  88. do_trap(regs, signr, sicode, str); \
  89. }
  90. DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
  91. "addressing exception")
  92. DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
  93. "execute exception")
  94. DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
  95. "fixpoint divide exception")
  96. DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
  97. "fixpoint overflow exception")
  98. DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
  99. "HFP overflow exception")
  100. DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
  101. "HFP underflow exception")
  102. DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
  103. "HFP significance exception")
  104. DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
  105. "HFP divide exception")
  106. DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
  107. "HFP square root exception")
  108. DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
  109. "operand exception")
  110. DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
  111. "privileged operation")
  112. DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
  113. "special operation exception")
  114. DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
  115. "transaction constraint exception")
  116. static inline void do_fp_trap(struct pt_regs *regs, __u32 fpc)
  117. {
  118. int si_code = 0;
  119. /* FPC[2] is Data Exception Code */
  120. if ((fpc & 0x00000300) == 0) {
  121. /* bits 6 and 7 of DXC are 0 iff IEEE exception */
  122. if (fpc & 0x8000) /* invalid fp operation */
  123. si_code = FPE_FLTINV;
  124. else if (fpc & 0x4000) /* div by 0 */
  125. si_code = FPE_FLTDIV;
  126. else if (fpc & 0x2000) /* overflow */
  127. si_code = FPE_FLTOVF;
  128. else if (fpc & 0x1000) /* underflow */
  129. si_code = FPE_FLTUND;
  130. else if (fpc & 0x0800) /* inexact */
  131. si_code = FPE_FLTRES;
  132. }
  133. do_trap(regs, SIGFPE, si_code, "floating point exception");
  134. }
  135. static void translation_specification_exception(struct pt_regs *regs)
  136. {
  137. /* May never happen. */
  138. panic("Translation-Specification Exception");
  139. }
  140. static void illegal_op(struct pt_regs *regs)
  141. {
  142. __u8 opcode[6];
  143. __u16 __user *location;
  144. int is_uprobe_insn = 0;
  145. int signal = 0;
  146. location = get_trap_ip(regs);
  147. if (user_mode(regs)) {
  148. if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
  149. return;
  150. if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
  151. if (current->ptrace)
  152. force_sig_fault(SIGTRAP, TRAP_BRKPT, location);
  153. else
  154. signal = SIGILL;
  155. #ifdef CONFIG_UPROBES
  156. } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
  157. is_uprobe_insn = 1;
  158. #endif
  159. } else
  160. signal = SIGILL;
  161. }
  162. /*
  163. * We got either an illegal op in kernel mode, or user space trapped
  164. * on a uprobes illegal instruction. See if kprobes or uprobes picks
  165. * it up. If not, SIGILL.
  166. */
  167. if (is_uprobe_insn || !user_mode(regs)) {
  168. if (notify_die(DIE_BPT, "bpt", regs, 0,
  169. 3, SIGTRAP) != NOTIFY_STOP)
  170. signal = SIGILL;
  171. }
  172. if (signal)
  173. do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
  174. }
  175. NOKPROBE_SYMBOL(illegal_op);
  176. DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
  177. "specification exception");
  178. static void vector_exception(struct pt_regs *regs)
  179. {
  180. int si_code, vic;
  181. if (!cpu_has_vx()) {
  182. do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
  183. return;
  184. }
  185. /* get vector interrupt code from fpc */
  186. save_user_fpu_regs();
  187. vic = (current->thread.ufpu.fpc & 0xf00) >> 8;
  188. switch (vic) {
  189. case 1: /* invalid vector operation */
  190. si_code = FPE_FLTINV;
  191. break;
  192. case 2: /* division by zero */
  193. si_code = FPE_FLTDIV;
  194. break;
  195. case 3: /* overflow */
  196. si_code = FPE_FLTOVF;
  197. break;
  198. case 4: /* underflow */
  199. si_code = FPE_FLTUND;
  200. break;
  201. case 5: /* inexact */
  202. si_code = FPE_FLTRES;
  203. break;
  204. default: /* unknown cause */
  205. si_code = 0;
  206. }
  207. do_trap(regs, SIGFPE, si_code, "vector exception");
  208. }
  209. static void data_exception(struct pt_regs *regs)
  210. {
  211. save_user_fpu_regs();
  212. if (current->thread.ufpu.fpc & FPC_DXC_MASK)
  213. do_fp_trap(regs, current->thread.ufpu.fpc);
  214. else
  215. do_trap(regs, SIGILL, ILL_ILLOPN, "data exception");
  216. }
  217. static void space_switch_exception(struct pt_regs *regs)
  218. {
  219. /* Set user psw back to home space mode. */
  220. if (user_mode(regs))
  221. regs->psw.mask |= PSW_ASC_HOME;
  222. /* Send SIGILL. */
  223. do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
  224. }
  225. static void monitor_event_exception(struct pt_regs *regs)
  226. {
  227. if (user_mode(regs))
  228. return;
  229. switch (report_bug(regs->psw.addr - (regs->int_code >> 16), regs)) {
  230. case BUG_TRAP_TYPE_NONE:
  231. fixup_exception(regs);
  232. break;
  233. case BUG_TRAP_TYPE_WARN:
  234. break;
  235. case BUG_TRAP_TYPE_BUG:
  236. die(regs, "monitor event");
  237. break;
  238. }
  239. }
  240. void kernel_stack_overflow(struct pt_regs *regs)
  241. {
  242. /*
  243. * Normally regs are unpoisoned by the generic entry code, but
  244. * kernel_stack_overflow() is a rare case that is called bypassing it.
  245. */
  246. kmsan_unpoison_entry_regs(regs);
  247. bust_spinlocks(1);
  248. printk("Kernel stack overflow.\n");
  249. show_regs(regs);
  250. bust_spinlocks(0);
  251. panic("Corrupt kernel stack, can't continue.");
  252. }
  253. NOKPROBE_SYMBOL(kernel_stack_overflow);
  254. static void __init test_monitor_call(void)
  255. {
  256. int val = 1;
  257. if (!IS_ENABLED(CONFIG_BUG))
  258. return;
  259. asm volatile(
  260. " mc 0,0\n"
  261. "0: lhi %[val],0\n"
  262. "1:\n"
  263. EX_TABLE(0b, 1b)
  264. : [val] "+d" (val));
  265. if (!val)
  266. panic("Monitor call doesn't work!\n");
  267. }
  268. void __init trap_init(void)
  269. {
  270. struct lowcore *lc = get_lowcore();
  271. unsigned long flags;
  272. struct ctlreg cr0;
  273. local_irq_save(flags);
  274. cr0 = local_ctl_clear_bit(0, CR0_LOW_ADDRESS_PROTECTION_BIT);
  275. psw_bits(lc->external_new_psw).mcheck = 1;
  276. psw_bits(lc->program_new_psw).mcheck = 1;
  277. psw_bits(lc->svc_new_psw).mcheck = 1;
  278. psw_bits(lc->io_new_psw).mcheck = 1;
  279. local_ctl_load(0, &cr0);
  280. local_irq_restore(flags);
  281. local_mcck_enable();
  282. test_monitor_call();
  283. }
  284. static void (*pgm_check_table[128])(struct pt_regs *regs);
  285. void noinstr __do_pgm_check(struct pt_regs *regs)
  286. {
  287. struct lowcore *lc = get_lowcore();
  288. irqentry_state_t state;
  289. unsigned int trapnr;
  290. regs->int_code = lc->pgm_int_code;
  291. regs->int_parm_long = lc->trans_exc_code;
  292. state = irqentry_enter(regs);
  293. if (user_mode(regs)) {
  294. update_timer_sys();
  295. if (!static_branch_likely(&cpu_has_bear)) {
  296. if (regs->last_break < 4096)
  297. regs->last_break = 1;
  298. }
  299. current->thread.last_break = regs->last_break;
  300. }
  301. if (lc->pgm_code & 0x0200) {
  302. /* transaction abort */
  303. current->thread.trap_tdb = lc->pgm_tdb;
  304. }
  305. if (lc->pgm_code & PGM_INT_CODE_PER) {
  306. if (user_mode(regs)) {
  307. struct per_event *ev = &current->thread.per_event;
  308. set_thread_flag(TIF_PER_TRAP);
  309. ev->address = lc->per_address;
  310. ev->cause = lc->per_code_combined;
  311. ev->paid = lc->per_access_id;
  312. } else {
  313. /* PER event in kernel is kprobes */
  314. __arch_local_irq_ssm(regs->psw.mask & ~PSW_MASK_PER);
  315. do_per_trap(regs);
  316. goto out;
  317. }
  318. }
  319. if (!irqs_disabled_flags(regs->psw.mask))
  320. trace_hardirqs_on();
  321. __arch_local_irq_ssm(regs->psw.mask & ~PSW_MASK_PER);
  322. trapnr = regs->int_code & PGM_INT_CODE_MASK;
  323. if (trapnr)
  324. pgm_check_table[trapnr](regs);
  325. out:
  326. local_irq_disable();
  327. irqentry_exit(regs, state);
  328. }
  329. /*
  330. * The program check table contains exactly 128 (0x00-0x7f) entries. Each
  331. * line defines the function to be called corresponding to the program check
  332. * interruption code.
  333. */
  334. static void (*pgm_check_table[128])(struct pt_regs *regs) = {
  335. [0x00] = default_trap_handler,
  336. [0x01] = illegal_op,
  337. [0x02] = privileged_op,
  338. [0x03] = execute_exception,
  339. [0x04] = do_protection_exception,
  340. [0x05] = addressing_exception,
  341. [0x06] = specification_exception,
  342. [0x07] = data_exception,
  343. [0x08] = overflow_exception,
  344. [0x09] = divide_exception,
  345. [0x0a] = overflow_exception,
  346. [0x0b] = divide_exception,
  347. [0x0c] = hfp_overflow_exception,
  348. [0x0d] = hfp_underflow_exception,
  349. [0x0e] = hfp_significance_exception,
  350. [0x0f] = hfp_divide_exception,
  351. [0x10] = do_dat_exception,
  352. [0x11] = do_dat_exception,
  353. [0x12] = translation_specification_exception,
  354. [0x13] = special_op_exception,
  355. [0x14] = default_trap_handler,
  356. [0x15] = operand_exception,
  357. [0x16] = default_trap_handler,
  358. [0x17] = default_trap_handler,
  359. [0x18] = transaction_exception,
  360. [0x19] = default_trap_handler,
  361. [0x1a] = default_trap_handler,
  362. [0x1b] = vector_exception,
  363. [0x1c] = space_switch_exception,
  364. [0x1d] = hfp_sqrt_exception,
  365. [0x1e ... 0x37] = default_trap_handler,
  366. [0x38] = do_dat_exception,
  367. [0x39] = do_dat_exception,
  368. [0x3a] = do_dat_exception,
  369. [0x3b] = do_dat_exception,
  370. [0x3c] = default_trap_handler,
  371. [0x3d] = do_secure_storage_access,
  372. [0x3e] = do_non_secure_storage_access,
  373. [0x3f] = do_secure_storage_violation,
  374. [0x40] = monitor_event_exception,
  375. [0x41 ... 0x7f] = default_trap_handler,
  376. };
  377. #define COND_TRAP(x) asm( \
  378. ".weak " __stringify(x) "\n\t" \
  379. ".set " __stringify(x) "," \
  380. __stringify(default_trap_handler))
  381. COND_TRAP(do_secure_storage_access);
  382. COND_TRAP(do_non_secure_storage_access);
  383. COND_TRAP(do_secure_storage_violation);