traps.c 11 KB

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