traps.c 11 KB

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