traps.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel traps/events for Hexagon processor
  4. *
  5. * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/init.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/module.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/kdebug.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/signal.h>
  16. #include <linux/ptrace.h>
  17. #include <asm/traps.h>
  18. #include <asm/vm_fault.h>
  19. #include <asm/syscall.h>
  20. #include <asm/registers.h>
  21. #include <asm/unistd.h>
  22. #include <asm/sections.h>
  23. #ifdef CONFIG_KGDB
  24. # include <linux/kgdb.h>
  25. #endif
  26. #define TRAP_SYSCALL 1
  27. #define TRAP_DEBUG 0xdb
  28. #ifdef CONFIG_GENERIC_BUG
  29. /* Maybe should resemble arch/sh/kernel/traps.c ?? */
  30. int is_valid_bugaddr(unsigned long addr)
  31. {
  32. return 1;
  33. }
  34. #endif /* CONFIG_GENERIC_BUG */
  35. static const char *ex_name(int ex)
  36. {
  37. switch (ex) {
  38. case HVM_GE_C_XPROT:
  39. case HVM_GE_C_XUSER:
  40. return "Execute protection fault";
  41. case HVM_GE_C_RPROT:
  42. case HVM_GE_C_RUSER:
  43. return "Read protection fault";
  44. case HVM_GE_C_WPROT:
  45. case HVM_GE_C_WUSER:
  46. return "Write protection fault";
  47. case HVM_GE_C_XMAL:
  48. return "Misaligned instruction";
  49. case HVM_GE_C_WREG:
  50. return "Multiple writes to same register in packet";
  51. case HVM_GE_C_PCAL:
  52. return "Program counter values that are not properly aligned";
  53. case HVM_GE_C_RMAL:
  54. return "Misaligned data load";
  55. case HVM_GE_C_WMAL:
  56. return "Misaligned data store";
  57. case HVM_GE_C_INVI:
  58. case HVM_GE_C_PRIVI:
  59. return "Illegal instruction";
  60. case HVM_GE_C_BUS:
  61. return "Precise bus error";
  62. case HVM_GE_C_CACHE:
  63. return "Cache error";
  64. case 0xdb:
  65. return "Debugger trap";
  66. default:
  67. return "Unrecognized exception";
  68. }
  69. }
  70. static void do_show_stack(struct task_struct *task, unsigned long *fp,
  71. unsigned long ip, const char *loglvl)
  72. {
  73. int kstack_depth_to_print = 24;
  74. unsigned long offset, size;
  75. const char *name = NULL;
  76. unsigned long *newfp;
  77. unsigned long low, high;
  78. char tmpstr[128];
  79. char *modname;
  80. int i;
  81. if (task == NULL)
  82. task = current;
  83. printk("%sCPU#%d, %s/%d, Call Trace:\n", loglvl, raw_smp_processor_id(),
  84. task->comm, task_pid_nr(task));
  85. if (fp == NULL) {
  86. if (task == current) {
  87. asm("%0 = r30" : "=r" (fp));
  88. } else {
  89. fp = (unsigned long *)
  90. ((struct hexagon_switch_stack *)
  91. task->thread.switch_sp)->fp;
  92. }
  93. }
  94. if ((((unsigned long) fp) & 0x3) || ((unsigned long) fp < 0x1000)) {
  95. printk("%s-- Corrupt frame pointer %p\n", loglvl, fp);
  96. return;
  97. }
  98. /* Saved link reg is one word above FP */
  99. if (!ip)
  100. ip = *(fp+1);
  101. /* Expect kernel stack to be in-bounds */
  102. low = (unsigned long)task_stack_page(task);
  103. high = low + THREAD_SIZE - 8;
  104. low += sizeof(struct thread_info);
  105. for (i = 0; i < kstack_depth_to_print; i++) {
  106. name = kallsyms_lookup(ip, &size, &offset, &modname, tmpstr);
  107. printk("%s[%p] 0x%lx: %s + 0x%lx", loglvl, fp, ip, name, offset);
  108. if (((unsigned long) fp < low) || (high < (unsigned long) fp))
  109. printk(KERN_CONT " (FP out of bounds!)");
  110. if (modname)
  111. printk(KERN_CONT " [%s] ", modname);
  112. printk(KERN_CONT "\n");
  113. newfp = (unsigned long *) *fp;
  114. if (((unsigned long) newfp) & 0x3) {
  115. printk("%s-- Corrupt frame pointer %p\n", loglvl, newfp);
  116. break;
  117. }
  118. /* Attempt to continue past exception. */
  119. if (0 == newfp) {
  120. struct pt_regs *regs = (struct pt_regs *) (((void *)fp)
  121. + 8);
  122. if (regs->syscall_nr != -1) {
  123. printk("%s-- trap0 -- syscall_nr: %ld", loglvl,
  124. regs->syscall_nr);
  125. printk(KERN_CONT " psp: %lx elr: %lx\n",
  126. pt_psp(regs), pt_elr(regs));
  127. break;
  128. } else {
  129. /* really want to see more ... */
  130. kstack_depth_to_print += 6;
  131. printk("%s-- %s (0x%lx) badva: %lx\n", loglvl,
  132. ex_name(pt_cause(regs)), pt_cause(regs),
  133. pt_badva(regs));
  134. }
  135. newfp = (unsigned long *) regs->r30;
  136. ip = pt_elr(regs);
  137. } else {
  138. ip = *(newfp + 1);
  139. }
  140. /* If link reg is null, we are done. */
  141. if (ip == 0x0)
  142. break;
  143. /* If newfp isn't larger, we're tracing garbage. */
  144. if (newfp > fp)
  145. fp = newfp;
  146. else
  147. break;
  148. }
  149. }
  150. void show_stack(struct task_struct *task, unsigned long *fp, const char *loglvl)
  151. {
  152. /* Saved link reg is one word above FP */
  153. do_show_stack(task, fp, 0, loglvl);
  154. }
  155. int die(const char *str, struct pt_regs *regs, long err)
  156. {
  157. static struct {
  158. spinlock_t lock;
  159. int counter;
  160. } die = {
  161. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  162. .counter = 0
  163. };
  164. console_verbose();
  165. oops_enter();
  166. spin_lock_irq(&die.lock);
  167. bust_spinlocks(1);
  168. printk(KERN_EMERG "Oops: %s[#%d]:\n", str, ++die.counter);
  169. if (notify_die(DIE_OOPS, str, regs, err, pt_cause(regs), SIGSEGV) ==
  170. NOTIFY_STOP) {
  171. spin_unlock_irq(&die.lock);
  172. return 1;
  173. }
  174. print_modules();
  175. show_regs(regs);
  176. do_show_stack(current, &regs->r30, pt_elr(regs), KERN_EMERG);
  177. bust_spinlocks(0);
  178. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  179. spin_unlock_irq(&die.lock);
  180. if (in_interrupt())
  181. panic("Fatal exception in interrupt");
  182. if (panic_on_oops)
  183. panic("Fatal exception");
  184. oops_exit();
  185. make_task_dead(err);
  186. return 0;
  187. }
  188. int die_if_kernel(char *str, struct pt_regs *regs, long err)
  189. {
  190. if (!user_mode(regs))
  191. return die(str, regs, err);
  192. else
  193. return 0;
  194. }
  195. /*
  196. * It's not clear that misaligned fetches are ever recoverable.
  197. */
  198. static void misaligned_instruction(struct pt_regs *regs)
  199. {
  200. die_if_kernel("Misaligned Instruction", regs, 0);
  201. force_sig(SIGBUS);
  202. }
  203. /*
  204. * Misaligned loads and stores, on the other hand, can be
  205. * emulated, and probably should be, some day. But for now
  206. * they will be considered fatal.
  207. */
  208. static void misaligned_data_load(struct pt_regs *regs)
  209. {
  210. die_if_kernel("Misaligned Data Load", regs, 0);
  211. force_sig(SIGBUS);
  212. }
  213. static void misaligned_data_store(struct pt_regs *regs)
  214. {
  215. die_if_kernel("Misaligned Data Store", regs, 0);
  216. force_sig(SIGBUS);
  217. }
  218. static void illegal_instruction(struct pt_regs *regs)
  219. {
  220. die_if_kernel("Illegal Instruction", regs, 0);
  221. force_sig(SIGILL);
  222. }
  223. /*
  224. * Precise bus errors may be recoverable with a a retry,
  225. * but for now, treat them as irrecoverable.
  226. */
  227. static void precise_bus_error(struct pt_regs *regs)
  228. {
  229. die_if_kernel("Precise Bus Error", regs, 0);
  230. force_sig(SIGBUS);
  231. }
  232. /*
  233. * If anything is to be done here other than panic,
  234. * it will probably be complex and migrate to another
  235. * source module. For now, just die.
  236. */
  237. static void cache_error(struct pt_regs *regs)
  238. {
  239. die("Cache Error", regs, 0);
  240. }
  241. /*
  242. * General exception handler
  243. */
  244. void do_genex(struct pt_regs *regs);
  245. void do_genex(struct pt_regs *regs)
  246. {
  247. /*
  248. * Decode Cause and Dispatch
  249. */
  250. switch (pt_cause(regs)) {
  251. case HVM_GE_C_XPROT:
  252. case HVM_GE_C_XUSER:
  253. execute_protection_fault(regs);
  254. break;
  255. case HVM_GE_C_RPROT:
  256. case HVM_GE_C_RUSER:
  257. read_protection_fault(regs);
  258. break;
  259. case HVM_GE_C_WPROT:
  260. case HVM_GE_C_WUSER:
  261. write_protection_fault(regs);
  262. break;
  263. case HVM_GE_C_XMAL:
  264. misaligned_instruction(regs);
  265. break;
  266. case HVM_GE_C_WREG:
  267. illegal_instruction(regs);
  268. break;
  269. case HVM_GE_C_PCAL:
  270. misaligned_instruction(regs);
  271. break;
  272. case HVM_GE_C_RMAL:
  273. misaligned_data_load(regs);
  274. break;
  275. case HVM_GE_C_WMAL:
  276. misaligned_data_store(regs);
  277. break;
  278. case HVM_GE_C_INVI:
  279. case HVM_GE_C_PRIVI:
  280. illegal_instruction(regs);
  281. break;
  282. case HVM_GE_C_BUS:
  283. precise_bus_error(regs);
  284. break;
  285. case HVM_GE_C_CACHE:
  286. cache_error(regs);
  287. break;
  288. default:
  289. /* Halt and catch fire */
  290. panic("Unrecognized exception 0x%lx\n", pt_cause(regs));
  291. break;
  292. }
  293. }
  294. void do_trap0(struct pt_regs *regs);
  295. void do_trap0(struct pt_regs *regs)
  296. {
  297. syscall_fn syscall;
  298. switch (pt_cause(regs)) {
  299. case TRAP_SYSCALL:
  300. /* System call is trap0 #1 */
  301. /* allow strace to catch syscall args */
  302. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE) &&
  303. ptrace_report_syscall_entry(regs)))
  304. return; /* return -ENOSYS somewhere? */
  305. /* Interrupts should be re-enabled for syscall processing */
  306. __vmsetie(VM_INT_ENABLE);
  307. /*
  308. * System call number is in r6, arguments in r0..r5.
  309. * Fortunately, no Linux syscall has more than 6 arguments,
  310. * and Hexagon ABI passes first 6 arguments in registers.
  311. * 64-bit arguments are passed in odd/even register pairs.
  312. * Fortunately, we have no system calls that take more
  313. * than three arguments with more than one 64-bit value.
  314. * Should that change, we'd need to redesign to copy
  315. * between user and kernel stacks.
  316. */
  317. regs->syscall_nr = regs->r06;
  318. /*
  319. * GPR R0 carries the first parameter, and is also used
  320. * to report the return value. We need a backup of
  321. * the user's value in case we need to do a late restart
  322. * of the system call.
  323. */
  324. regs->restart_r0 = regs->r00;
  325. if ((unsigned long) regs->syscall_nr >= __NR_syscalls) {
  326. regs->r00 = -1;
  327. } else {
  328. syscall = (syscall_fn)
  329. (sys_call_table[regs->syscall_nr]);
  330. regs->r00 = syscall(regs->r00, regs->r01,
  331. regs->r02, regs->r03,
  332. regs->r04, regs->r05);
  333. }
  334. /* allow strace to get the syscall return state */
  335. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
  336. ptrace_report_syscall_exit(regs, 0);
  337. break;
  338. case TRAP_DEBUG:
  339. /* Trap0 0xdb is debug breakpoint */
  340. if (user_mode(regs)) {
  341. /*
  342. * Some architecures add some per-thread state
  343. * to distinguish between breakpoint traps and
  344. * trace traps. We may want to do that, and
  345. * set the si_code value appropriately, or we
  346. * may want to use a different trap0 flavor.
  347. */
  348. force_sig_fault(SIGTRAP, TRAP_BRKPT,
  349. (void __user *) pt_elr(regs));
  350. } else {
  351. #ifdef CONFIG_KGDB
  352. kgdb_handle_exception(pt_cause(regs), SIGTRAP,
  353. TRAP_BRKPT, regs);
  354. #endif
  355. }
  356. break;
  357. }
  358. /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
  359. }
  360. /*
  361. * Machine check exception handler
  362. */
  363. void do_machcheck(struct pt_regs *regs);
  364. void do_machcheck(struct pt_regs *regs)
  365. {
  366. /* Halt and catch fire */
  367. __vmstop();
  368. }
  369. /*
  370. * Treat this like the old 0xdb trap.
  371. */
  372. void do_debug_exception(struct pt_regs *regs);
  373. void do_debug_exception(struct pt_regs *regs)
  374. {
  375. regs->hvmer.vmest &= ~HVM_VMEST_CAUSE_MSK;
  376. regs->hvmer.vmest |= (TRAP_DEBUG << HVM_VMEST_CAUSE_SFT);
  377. do_trap0(regs);
  378. }