traps.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. return 1;
  172. print_modules();
  173. show_regs(regs);
  174. do_show_stack(current, &regs->r30, pt_elr(regs), KERN_EMERG);
  175. bust_spinlocks(0);
  176. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  177. spin_unlock_irq(&die.lock);
  178. if (in_interrupt())
  179. panic("Fatal exception in interrupt");
  180. if (panic_on_oops)
  181. panic("Fatal exception");
  182. oops_exit();
  183. make_task_dead(err);
  184. return 0;
  185. }
  186. int die_if_kernel(char *str, struct pt_regs *regs, long err)
  187. {
  188. if (!user_mode(regs))
  189. return die(str, regs, err);
  190. else
  191. return 0;
  192. }
  193. /*
  194. * It's not clear that misaligned fetches are ever recoverable.
  195. */
  196. static void misaligned_instruction(struct pt_regs *regs)
  197. {
  198. die_if_kernel("Misaligned Instruction", regs, 0);
  199. force_sig(SIGBUS);
  200. }
  201. /*
  202. * Misaligned loads and stores, on the other hand, can be
  203. * emulated, and probably should be, some day. But for now
  204. * they will be considered fatal.
  205. */
  206. static void misaligned_data_load(struct pt_regs *regs)
  207. {
  208. die_if_kernel("Misaligned Data Load", regs, 0);
  209. force_sig(SIGBUS);
  210. }
  211. static void misaligned_data_store(struct pt_regs *regs)
  212. {
  213. die_if_kernel("Misaligned Data Store", regs, 0);
  214. force_sig(SIGBUS);
  215. }
  216. static void illegal_instruction(struct pt_regs *regs)
  217. {
  218. die_if_kernel("Illegal Instruction", regs, 0);
  219. force_sig(SIGILL);
  220. }
  221. /*
  222. * Precise bus errors may be recoverable with a a retry,
  223. * but for now, treat them as irrecoverable.
  224. */
  225. static void precise_bus_error(struct pt_regs *regs)
  226. {
  227. die_if_kernel("Precise Bus Error", regs, 0);
  228. force_sig(SIGBUS);
  229. }
  230. /*
  231. * If anything is to be done here other than panic,
  232. * it will probably be complex and migrate to another
  233. * source module. For now, just die.
  234. */
  235. static void cache_error(struct pt_regs *regs)
  236. {
  237. die("Cache Error", regs, 0);
  238. }
  239. /*
  240. * General exception handler
  241. */
  242. void do_genex(struct pt_regs *regs)
  243. {
  244. /*
  245. * Decode Cause and Dispatch
  246. */
  247. switch (pt_cause(regs)) {
  248. case HVM_GE_C_XPROT:
  249. case HVM_GE_C_XUSER:
  250. execute_protection_fault(regs);
  251. break;
  252. case HVM_GE_C_RPROT:
  253. case HVM_GE_C_RUSER:
  254. read_protection_fault(regs);
  255. break;
  256. case HVM_GE_C_WPROT:
  257. case HVM_GE_C_WUSER:
  258. write_protection_fault(regs);
  259. break;
  260. case HVM_GE_C_XMAL:
  261. misaligned_instruction(regs);
  262. break;
  263. case HVM_GE_C_WREG:
  264. illegal_instruction(regs);
  265. break;
  266. case HVM_GE_C_PCAL:
  267. misaligned_instruction(regs);
  268. break;
  269. case HVM_GE_C_RMAL:
  270. misaligned_data_load(regs);
  271. break;
  272. case HVM_GE_C_WMAL:
  273. misaligned_data_store(regs);
  274. break;
  275. case HVM_GE_C_INVI:
  276. case HVM_GE_C_PRIVI:
  277. illegal_instruction(regs);
  278. break;
  279. case HVM_GE_C_BUS:
  280. precise_bus_error(regs);
  281. break;
  282. case HVM_GE_C_CACHE:
  283. cache_error(regs);
  284. break;
  285. default:
  286. /* Halt and catch fire */
  287. panic("Unrecognized exception 0x%lx\n", pt_cause(regs));
  288. break;
  289. }
  290. }
  291. /* Indirect system call dispatch */
  292. long sys_syscall(void)
  293. {
  294. printk(KERN_ERR "sys_syscall invoked!\n");
  295. return -ENOSYS;
  296. }
  297. void do_trap0(struct pt_regs *regs)
  298. {
  299. syscall_fn syscall;
  300. switch (pt_cause(regs)) {
  301. case TRAP_SYSCALL:
  302. /* System call is trap0 #1 */
  303. /* allow strace to catch syscall args */
  304. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE) &&
  305. ptrace_report_syscall_entry(regs)))
  306. return; /* return -ENOSYS somewhere? */
  307. /* Interrupts should be re-enabled for syscall processing */
  308. __vmsetie(VM_INT_ENABLE);
  309. /*
  310. * System call number is in r6, arguments in r0..r5.
  311. * Fortunately, no Linux syscall has more than 6 arguments,
  312. * and Hexagon ABI passes first 6 arguments in registers.
  313. * 64-bit arguments are passed in odd/even register pairs.
  314. * Fortunately, we have no system calls that take more
  315. * than three arguments with more than one 64-bit value.
  316. * Should that change, we'd need to redesign to copy
  317. * between user and kernel stacks.
  318. */
  319. regs->syscall_nr = regs->r06;
  320. /*
  321. * GPR R0 carries the first parameter, and is also used
  322. * to report the return value. We need a backup of
  323. * the user's value in case we need to do a late restart
  324. * of the system call.
  325. */
  326. regs->restart_r0 = regs->r00;
  327. if ((unsigned long) regs->syscall_nr >= __NR_syscalls) {
  328. regs->r00 = -1;
  329. } else {
  330. syscall = (syscall_fn)
  331. (sys_call_table[regs->syscall_nr]);
  332. regs->r00 = syscall(regs->r00, regs->r01,
  333. regs->r02, regs->r03,
  334. regs->r04, regs->r05);
  335. }
  336. /* allow strace to get the syscall return state */
  337. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
  338. ptrace_report_syscall_exit(regs, 0);
  339. break;
  340. case TRAP_DEBUG:
  341. /* Trap0 0xdb is debug breakpoint */
  342. if (user_mode(regs)) {
  343. /*
  344. * Some architecures add some per-thread state
  345. * to distinguish between breakpoint traps and
  346. * trace traps. We may want to do that, and
  347. * set the si_code value appropriately, or we
  348. * may want to use a different trap0 flavor.
  349. */
  350. force_sig_fault(SIGTRAP, TRAP_BRKPT,
  351. (void __user *) pt_elr(regs));
  352. } else {
  353. #ifdef CONFIG_KGDB
  354. kgdb_handle_exception(pt_cause(regs), SIGTRAP,
  355. TRAP_BRKPT, regs);
  356. #endif
  357. }
  358. break;
  359. }
  360. /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
  361. }
  362. /*
  363. * Machine check exception handler
  364. */
  365. void do_machcheck(struct pt_regs *regs)
  366. {
  367. /* Halt and catch fire */
  368. __vmstop();
  369. }
  370. /*
  371. * Treat this like the old 0xdb trap.
  372. */
  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. }