traps.c 10 KB

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