traps.c 11 KB

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