traps.c 10 KB

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