traps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/traps.c
  4. *
  5. * Copyright (C) 1995-2009 Russell King
  6. * Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds
  7. *
  8. * 'traps.c' handles hardware exceptions after we have saved some state in
  9. * 'linux/arch/arm/lib/traps.S'. Mostly a debugging aid, but will probably
  10. * kill the offending process.
  11. */
  12. #include <linux/signal.h>
  13. #include <linux/personality.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/kprobes.h>
  20. #include <linux/module.h>
  21. #include <linux/kexec.h>
  22. #include <linux/bug.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/sched/debug.h>
  27. #include <linux/sched/task_stack.h>
  28. #include <linux/irq.h>
  29. #include <linux/atomic.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/exception.h>
  32. #include <asm/spectre.h>
  33. #include <asm/unistd.h>
  34. #include <asm/traps.h>
  35. #include <asm/ptrace.h>
  36. #include <asm/unwind.h>
  37. #include <asm/tls.h>
  38. #include <asm/stacktrace.h>
  39. #include <asm/system_misc.h>
  40. #include <asm/opcodes.h>
  41. static const char *handler[]= {
  42. "prefetch abort",
  43. "data abort",
  44. "address exception",
  45. "interrupt",
  46. "undefined instruction",
  47. };
  48. void *vectors_page;
  49. #ifdef CONFIG_DEBUG_USER
  50. unsigned int user_debug;
  51. static int __init user_debug_setup(char *str)
  52. {
  53. get_option(&str, &user_debug);
  54. return 1;
  55. }
  56. __setup("user_debug=", user_debug_setup);
  57. #endif
  58. void dump_backtrace_entry(unsigned long where, unsigned long from,
  59. unsigned long frame, const char *loglvl)
  60. {
  61. unsigned long end = frame + 4 + sizeof(struct pt_regs);
  62. if (IS_ENABLED(CONFIG_UNWINDER_FRAME_POINTER) &&
  63. IS_ENABLED(CONFIG_CC_IS_GCC) &&
  64. end > ALIGN(frame, THREAD_SIZE)) {
  65. /*
  66. * If we are walking past the end of the stack, it may be due
  67. * to the fact that we are on an IRQ or overflow stack. In this
  68. * case, we can load the address of the other stack from the
  69. * frame record.
  70. */
  71. frame = ((unsigned long *)frame)[-2] - 4;
  72. end = frame + 4 + sizeof(struct pt_regs);
  73. }
  74. #ifndef CONFIG_KALLSYMS
  75. printk("%sFunction entered at [<%08lx>] from [<%08lx>]\n",
  76. loglvl, where, from);
  77. #elif defined CONFIG_BACKTRACE_VERBOSE
  78. printk("%s[<%08lx>] (%ps) from [<%08lx>] (%pS)\n",
  79. loglvl, where, (void *)where, from, (void *)from);
  80. #else
  81. printk("%s %ps from %pS\n", loglvl, (void *)where, (void *)from);
  82. #endif
  83. if (in_entry_text(from) && end <= ALIGN(frame, THREAD_SIZE))
  84. dump_mem(loglvl, "Exception stack", frame + 4, end);
  85. }
  86. void dump_backtrace_stm(u32 *stack, u32 instruction, const char *loglvl)
  87. {
  88. char str[80], *p;
  89. unsigned int x;
  90. int reg;
  91. for (reg = 10, x = 0, p = str; reg >= 0; reg--) {
  92. if (instruction & BIT(reg)) {
  93. p += sprintf(p, " r%d:%08x", reg, *stack--);
  94. if (++x == 6) {
  95. x = 0;
  96. p = str;
  97. printk("%s%s\n", loglvl, str);
  98. }
  99. }
  100. }
  101. if (p != str)
  102. printk("%s%s\n", loglvl, str);
  103. }
  104. #ifndef CONFIG_ARM_UNWIND
  105. /*
  106. * Stack pointers should always be within the kernels view of
  107. * physical memory. If it is not there, then we can't dump
  108. * out any information relating to the stack.
  109. */
  110. static int verify_stack(unsigned long sp)
  111. {
  112. if (sp < PAGE_OFFSET ||
  113. (!IS_ENABLED(CONFIG_VMAP_STACK) &&
  114. sp > (unsigned long)high_memory && high_memory != NULL))
  115. return -EFAULT;
  116. return 0;
  117. }
  118. #endif
  119. /*
  120. * Dump out the contents of some memory nicely...
  121. */
  122. void dump_mem(const char *lvl, const char *str, unsigned long bottom,
  123. unsigned long top)
  124. {
  125. unsigned long first;
  126. int i;
  127. printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
  128. for (first = bottom & ~31; first < top; first += 32) {
  129. unsigned long p;
  130. char str[sizeof(" 12345678") * 8 + 1];
  131. memset(str, ' ', sizeof(str));
  132. str[sizeof(str) - 1] = '\0';
  133. for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
  134. if (p >= bottom && p < top) {
  135. unsigned long val;
  136. if (!get_kernel_nofault(val, (unsigned long *)p))
  137. sprintf(str + i * 9, " %08lx", val);
  138. else
  139. sprintf(str + i * 9, " ????????");
  140. }
  141. }
  142. printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
  143. }
  144. }
  145. static void dump_instr(const char *lvl, struct pt_regs *regs)
  146. {
  147. unsigned long addr = instruction_pointer(regs);
  148. const int thumb = thumb_mode(regs);
  149. const int width = thumb ? 4 : 8;
  150. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  151. int i;
  152. /*
  153. * Note that we now dump the code first, just in case the backtrace
  154. * kills us.
  155. */
  156. for (i = -4; i < 1 + !!thumb; i++) {
  157. unsigned int val, bad;
  158. if (!user_mode(regs)) {
  159. if (thumb) {
  160. u16 val16;
  161. bad = get_kernel_nofault(val16, &((u16 *)addr)[i]);
  162. val = val16;
  163. } else {
  164. bad = get_kernel_nofault(val, &((u32 *)addr)[i]);
  165. }
  166. } else {
  167. if (thumb)
  168. bad = get_user(val, &((u16 *)addr)[i]);
  169. else
  170. bad = get_user(val, &((u32 *)addr)[i]);
  171. }
  172. if (!bad)
  173. p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
  174. width, val);
  175. else {
  176. p += sprintf(p, "bad PC value");
  177. break;
  178. }
  179. }
  180. printk("%sCode: %s\n", lvl, str);
  181. }
  182. #ifdef CONFIG_ARM_UNWIND
  183. void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
  184. const char *loglvl)
  185. {
  186. unwind_backtrace(regs, tsk, loglvl);
  187. }
  188. #else
  189. void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
  190. const char *loglvl)
  191. {
  192. unsigned int fp, mode;
  193. int ok = 1;
  194. printk("%sBacktrace: ", loglvl);
  195. if (!tsk)
  196. tsk = current;
  197. if (regs) {
  198. fp = frame_pointer(regs);
  199. mode = processor_mode(regs);
  200. } else if (tsk != current) {
  201. fp = thread_saved_fp(tsk);
  202. mode = 0x10;
  203. } else {
  204. asm("mov %0, fp" : "=r" (fp) : : "cc");
  205. mode = 0x10;
  206. }
  207. if (!fp) {
  208. pr_cont("no frame pointer");
  209. ok = 0;
  210. } else if (verify_stack(fp)) {
  211. pr_cont("invalid frame pointer 0x%08x", fp);
  212. ok = 0;
  213. } else if (fp < (unsigned long)end_of_stack(tsk))
  214. pr_cont("frame pointer underflow");
  215. pr_cont("\n");
  216. if (ok)
  217. c_backtrace(fp, mode, loglvl);
  218. }
  219. #endif
  220. void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
  221. {
  222. dump_backtrace(NULL, tsk, loglvl);
  223. barrier();
  224. }
  225. #ifdef CONFIG_PREEMPT
  226. #define S_PREEMPT " PREEMPT"
  227. #elif defined(CONFIG_PREEMPT_RT)
  228. #define S_PREEMPT " PREEMPT_RT"
  229. #else
  230. #define S_PREEMPT ""
  231. #endif
  232. #ifdef CONFIG_SMP
  233. #define S_SMP " SMP"
  234. #else
  235. #define S_SMP ""
  236. #endif
  237. #ifdef CONFIG_THUMB2_KERNEL
  238. #define S_ISA " THUMB2"
  239. #else
  240. #define S_ISA " ARM"
  241. #endif
  242. static int __die(const char *str, int err, struct pt_regs *regs)
  243. {
  244. struct task_struct *tsk = current;
  245. static int die_counter;
  246. int ret;
  247. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP S_ISA "\n",
  248. str, err, ++die_counter);
  249. /* trap and error numbers are mostly meaningless on ARM */
  250. ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
  251. if (ret == NOTIFY_STOP)
  252. return 1;
  253. print_modules();
  254. __show_regs(regs);
  255. __show_regs_alloc_free(regs);
  256. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  257. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), end_of_stack(tsk));
  258. if (!user_mode(regs) || in_interrupt()) {
  259. dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
  260. ALIGN(regs->ARM_sp - THREAD_SIZE, THREAD_ALIGN)
  261. + THREAD_SIZE);
  262. dump_backtrace(regs, tsk, KERN_EMERG);
  263. dump_instr(KERN_EMERG, regs);
  264. }
  265. return 0;
  266. }
  267. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  268. static int die_owner = -1;
  269. static unsigned int die_nest_count;
  270. static unsigned long oops_begin(void)
  271. {
  272. int cpu;
  273. unsigned long flags;
  274. oops_enter();
  275. /* racy, but better than risking deadlock. */
  276. raw_local_irq_save(flags);
  277. cpu = smp_processor_id();
  278. if (!arch_spin_trylock(&die_lock)) {
  279. if (cpu == die_owner)
  280. /* nested oops. should stop eventually */;
  281. else
  282. arch_spin_lock(&die_lock);
  283. }
  284. die_nest_count++;
  285. die_owner = cpu;
  286. console_verbose();
  287. bust_spinlocks(1);
  288. return flags;
  289. }
  290. static void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  291. {
  292. if (regs && kexec_should_crash(current))
  293. crash_kexec(regs);
  294. bust_spinlocks(0);
  295. die_owner = -1;
  296. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  297. die_nest_count--;
  298. if (!die_nest_count)
  299. /* Nest count reaches zero, release the lock. */
  300. arch_spin_unlock(&die_lock);
  301. raw_local_irq_restore(flags);
  302. oops_exit();
  303. if (in_interrupt())
  304. panic("Fatal exception in interrupt");
  305. if (panic_on_oops)
  306. panic("Fatal exception");
  307. if (signr)
  308. make_task_dead(signr);
  309. }
  310. /*
  311. * This function is protected against re-entrancy.
  312. */
  313. void die(const char *str, struct pt_regs *regs, int err)
  314. {
  315. enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE;
  316. unsigned long flags = oops_begin();
  317. int sig = SIGSEGV;
  318. if (!user_mode(regs))
  319. bug_type = report_bug(regs->ARM_pc, regs);
  320. if (bug_type != BUG_TRAP_TYPE_NONE)
  321. str = "Oops - BUG";
  322. if (__die(str, err, regs))
  323. sig = 0;
  324. oops_end(flags, regs, sig);
  325. }
  326. void arm_notify_die(const char *str, struct pt_regs *regs,
  327. int signo, int si_code, void __user *addr,
  328. unsigned long err, unsigned long trap)
  329. {
  330. if (user_mode(regs)) {
  331. current->thread.error_code = err;
  332. current->thread.trap_no = trap;
  333. force_sig_fault(signo, si_code, addr);
  334. } else {
  335. die(str, regs, err);
  336. }
  337. }
  338. #ifdef CONFIG_GENERIC_BUG
  339. int is_valid_bugaddr(unsigned long pc)
  340. {
  341. #ifdef CONFIG_THUMB2_KERNEL
  342. u16 bkpt;
  343. u16 insn = __opcode_to_mem_thumb16(BUG_INSTR_VALUE);
  344. #else
  345. u32 bkpt;
  346. u32 insn = __opcode_to_mem_arm(BUG_INSTR_VALUE);
  347. #endif
  348. if (get_kernel_nofault(bkpt, (void *)pc))
  349. return 0;
  350. return bkpt == insn;
  351. }
  352. #endif
  353. static LIST_HEAD(undef_hook);
  354. static DEFINE_RAW_SPINLOCK(undef_lock);
  355. void register_undef_hook(struct undef_hook *hook)
  356. {
  357. unsigned long flags;
  358. raw_spin_lock_irqsave(&undef_lock, flags);
  359. list_add(&hook->node, &undef_hook);
  360. raw_spin_unlock_irqrestore(&undef_lock, flags);
  361. }
  362. void unregister_undef_hook(struct undef_hook *hook)
  363. {
  364. unsigned long flags;
  365. raw_spin_lock_irqsave(&undef_lock, flags);
  366. list_del(&hook->node);
  367. raw_spin_unlock_irqrestore(&undef_lock, flags);
  368. }
  369. static nokprobe_inline
  370. int call_undef_hook(struct pt_regs *regs, unsigned int instr)
  371. {
  372. struct undef_hook *hook;
  373. unsigned long flags;
  374. int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
  375. raw_spin_lock_irqsave(&undef_lock, flags);
  376. list_for_each_entry(hook, &undef_hook, node)
  377. if ((instr & hook->instr_mask) == hook->instr_val &&
  378. (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
  379. fn = hook->fn;
  380. raw_spin_unlock_irqrestore(&undef_lock, flags);
  381. return fn ? fn(regs, instr) : 1;
  382. }
  383. asmlinkage void do_undefinstr(struct pt_regs *regs)
  384. {
  385. unsigned int instr;
  386. void __user *pc;
  387. pc = (void __user *)instruction_pointer(regs);
  388. if (processor_mode(regs) == SVC_MODE) {
  389. #ifdef CONFIG_THUMB2_KERNEL
  390. if (thumb_mode(regs)) {
  391. instr = __mem_to_opcode_thumb16(((u16 *)pc)[0]);
  392. if (is_wide_instruction(instr)) {
  393. u16 inst2;
  394. inst2 = __mem_to_opcode_thumb16(((u16 *)pc)[1]);
  395. instr = __opcode_thumb32_compose(instr, inst2);
  396. }
  397. } else
  398. #endif
  399. instr = __mem_to_opcode_arm(*(u32 *) pc);
  400. } else if (thumb_mode(regs)) {
  401. if (get_user(instr, (u16 __user *)pc))
  402. goto die_sig;
  403. instr = __mem_to_opcode_thumb16(instr);
  404. if (is_wide_instruction(instr)) {
  405. unsigned int instr2;
  406. if (get_user(instr2, (u16 __user *)pc+1))
  407. goto die_sig;
  408. instr2 = __mem_to_opcode_thumb16(instr2);
  409. instr = __opcode_thumb32_compose(instr, instr2);
  410. }
  411. } else {
  412. if (get_user(instr, (u32 __user *)pc))
  413. goto die_sig;
  414. instr = __mem_to_opcode_arm(instr);
  415. }
  416. if (call_undef_hook(regs, instr) == 0)
  417. return;
  418. die_sig:
  419. #ifdef CONFIG_DEBUG_USER
  420. if (user_debug & UDBG_UNDEFINED) {
  421. pr_info("%s (%d): undefined instruction: pc=%px\n",
  422. current->comm, task_pid_nr(current), pc);
  423. __show_regs(regs);
  424. dump_instr(KERN_INFO, regs);
  425. }
  426. #endif
  427. arm_notify_die("Oops - undefined instruction", regs,
  428. SIGILL, ILL_ILLOPC, pc, 0, 6);
  429. }
  430. NOKPROBE_SYMBOL(do_undefinstr)
  431. /*
  432. * Handle FIQ similarly to NMI on x86 systems.
  433. *
  434. * The runtime environment for NMIs is extremely restrictive
  435. * (NMIs can pre-empt critical sections meaning almost all locking is
  436. * forbidden) meaning this default FIQ handling must only be used in
  437. * circumstances where non-maskability improves robustness, such as
  438. * watchdog or debug logic.
  439. *
  440. * This handler is not appropriate for general purpose use in drivers
  441. * platform code and can be overrideen using set_fiq_handler.
  442. */
  443. asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
  444. {
  445. struct pt_regs *old_regs = set_irq_regs(regs);
  446. nmi_enter();
  447. /* nop. FIQ handlers for special arch/arm features can be added here. */
  448. nmi_exit();
  449. set_irq_regs(old_regs);
  450. }
  451. /*
  452. * bad_mode handles the impossible case in the vectors. If you see one of
  453. * these, then it's extremely serious, and could mean you have buggy hardware.
  454. * It never returns, and never tries to sync. We hope that we can at least
  455. * dump out some state information...
  456. */
  457. asmlinkage void bad_mode(struct pt_regs *regs, int reason)
  458. {
  459. console_verbose();
  460. pr_crit("Bad mode in %s handler detected\n", handler[reason]);
  461. die("Oops - bad mode", regs, 0);
  462. local_irq_disable();
  463. panic("bad mode");
  464. }
  465. static int bad_syscall(int n, struct pt_regs *regs)
  466. {
  467. if ((current->personality & PER_MASK) != PER_LINUX) {
  468. send_sig(SIGSEGV, current, 1);
  469. return regs->ARM_r0;
  470. }
  471. #ifdef CONFIG_DEBUG_USER
  472. if (user_debug & UDBG_SYSCALL) {
  473. pr_err("[%d] %s: obsolete system call %08x.\n",
  474. task_pid_nr(current), current->comm, n);
  475. dump_instr(KERN_ERR, regs);
  476. }
  477. #endif
  478. arm_notify_die("Oops - bad syscall", regs, SIGILL, ILL_ILLTRP,
  479. (void __user *)instruction_pointer(regs) -
  480. (thumb_mode(regs) ? 2 : 4),
  481. n, 0);
  482. return regs->ARM_r0;
  483. }
  484. static inline int
  485. __do_cache_op(unsigned long start, unsigned long end)
  486. {
  487. int ret;
  488. do {
  489. unsigned long chunk = min(PAGE_SIZE, end - start);
  490. if (fatal_signal_pending(current))
  491. return 0;
  492. ret = flush_icache_user_range(start, start + chunk);
  493. if (ret)
  494. return ret;
  495. cond_resched();
  496. start += chunk;
  497. } while (start < end);
  498. return 0;
  499. }
  500. static inline int
  501. do_cache_op(unsigned long start, unsigned long end, int flags)
  502. {
  503. if (end < start || flags)
  504. return -EINVAL;
  505. if (!access_ok((void __user *)start, end - start))
  506. return -EFAULT;
  507. return __do_cache_op(start, end);
  508. }
  509. /*
  510. * Handle all unrecognised system calls.
  511. * 0x9f0000 - 0x9fffff are some more esoteric system calls
  512. */
  513. #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
  514. asmlinkage int arm_syscall(int no, struct pt_regs *regs)
  515. {
  516. if ((no >> 16) != (__ARM_NR_BASE>> 16))
  517. return bad_syscall(no, regs);
  518. switch (no & 0xffff) {
  519. case 0: /* branch through 0 */
  520. arm_notify_die("branch through zero", regs,
  521. SIGSEGV, SEGV_MAPERR, NULL, 0, 0);
  522. return 0;
  523. case NR(breakpoint): /* SWI BREAK_POINT */
  524. regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
  525. ptrace_break(regs);
  526. return regs->ARM_r0;
  527. /*
  528. * Flush a region from virtual address 'r0' to virtual address 'r1'
  529. * _exclusive_. There is no alignment requirement on either address;
  530. * user space does not need to know the hardware cache layout.
  531. *
  532. * r2 contains flags. It should ALWAYS be passed as ZERO until it
  533. * is defined to be something else. For now we ignore it, but may
  534. * the fires of hell burn in your belly if you break this rule. ;)
  535. *
  536. * (at a later date, we may want to allow this call to not flush
  537. * various aspects of the cache. Passing '0' will guarantee that
  538. * everything necessary gets flushed to maintain consistency in
  539. * the specified region).
  540. */
  541. case NR(cacheflush):
  542. return do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
  543. case NR(usr26):
  544. if (!(elf_hwcap & HWCAP_26BIT))
  545. break;
  546. regs->ARM_cpsr &= ~MODE32_BIT;
  547. return regs->ARM_r0;
  548. case NR(usr32):
  549. if (!(elf_hwcap & HWCAP_26BIT))
  550. break;
  551. regs->ARM_cpsr |= MODE32_BIT;
  552. return regs->ARM_r0;
  553. case NR(set_tls):
  554. set_tls(regs->ARM_r0);
  555. return 0;
  556. case NR(get_tls):
  557. return current_thread_info()->tp_value[0];
  558. default:
  559. /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
  560. if not implemented, rather than raising SIGILL. This
  561. way the calling program can gracefully determine whether
  562. a feature is supported. */
  563. if ((no & 0xffff) <= 0x7ff)
  564. return -ENOSYS;
  565. break;
  566. }
  567. #ifdef CONFIG_DEBUG_USER
  568. /*
  569. * experience shows that these seem to indicate that
  570. * something catastrophic has happened
  571. */
  572. if (user_debug & UDBG_SYSCALL) {
  573. pr_err("[%d] %s: arm syscall %d\n",
  574. task_pid_nr(current), current->comm, no);
  575. dump_instr(KERN_ERR, regs);
  576. if (user_mode(regs)) {
  577. __show_regs(regs);
  578. c_backtrace(frame_pointer(regs), processor_mode(regs), KERN_ERR);
  579. }
  580. }
  581. #endif
  582. arm_notify_die("Oops - bad syscall(2)", regs, SIGILL, ILL_ILLTRP,
  583. (void __user *)instruction_pointer(regs) -
  584. (thumb_mode(regs) ? 2 : 4),
  585. no, 0);
  586. return 0;
  587. }
  588. #ifdef CONFIG_TLS_REG_EMUL
  589. /*
  590. * We might be running on an ARMv6+ processor which should have the TLS
  591. * register but for some reason we can't use it, or maybe an SMP system
  592. * using a pre-ARMv6 processor (there are apparently a few prototypes like
  593. * that in existence) and therefore access to that register must be
  594. * emulated.
  595. */
  596. static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
  597. {
  598. int reg = (instr >> 12) & 15;
  599. if (reg == 15)
  600. return 1;
  601. regs->uregs[reg] = current_thread_info()->tp_value[0];
  602. regs->ARM_pc += 4;
  603. return 0;
  604. }
  605. static struct undef_hook arm_mrc_hook = {
  606. .instr_mask = 0x0fff0fff,
  607. .instr_val = 0x0e1d0f70,
  608. .cpsr_mask = PSR_T_BIT,
  609. .cpsr_val = 0,
  610. .fn = get_tp_trap,
  611. };
  612. static int __init arm_mrc_hook_init(void)
  613. {
  614. register_undef_hook(&arm_mrc_hook);
  615. return 0;
  616. }
  617. late_initcall(arm_mrc_hook_init);
  618. #endif
  619. /*
  620. * A data abort trap was taken, but we did not handle the instruction.
  621. * Try to abort the user program, or panic if it was the kernel.
  622. */
  623. asmlinkage void
  624. baddataabort(int code, unsigned long instr, struct pt_regs *regs)
  625. {
  626. unsigned long addr = instruction_pointer(regs);
  627. #ifdef CONFIG_DEBUG_USER
  628. if (user_debug & UDBG_BADABORT) {
  629. pr_err("8<--- cut here ---\n");
  630. pr_err("[%d] %s: bad data abort: code %d instr 0x%08lx\n",
  631. task_pid_nr(current), current->comm, code, instr);
  632. dump_instr(KERN_ERR, regs);
  633. show_pte(KERN_ERR, current->mm, addr);
  634. }
  635. #endif
  636. arm_notify_die("unknown data abort code", regs,
  637. SIGILL, ILL_ILLOPC, (void __user *)addr, instr, 0);
  638. }
  639. void __readwrite_bug(const char *fn)
  640. {
  641. pr_err("%s called, but not implemented\n", fn);
  642. BUG();
  643. }
  644. EXPORT_SYMBOL(__readwrite_bug);
  645. void __pte_error(const char *file, int line, pte_t pte)
  646. {
  647. pr_err("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
  648. }
  649. void __pmd_error(const char *file, int line, pmd_t pmd)
  650. {
  651. pr_err("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
  652. }
  653. void __pgd_error(const char *file, int line, pgd_t pgd)
  654. {
  655. pr_err("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
  656. }
  657. asmlinkage void __div0(void)
  658. {
  659. pr_err("Division by zero in kernel.\n");
  660. dump_stack();
  661. }
  662. EXPORT_SYMBOL(__div0);
  663. void abort(void)
  664. {
  665. BUG();
  666. /* if that doesn't kill us, halt */
  667. panic("Oops failed to kill thread");
  668. }
  669. #ifdef CONFIG_KUSER_HELPERS
  670. static void __init kuser_init(void *vectors)
  671. {
  672. extern char __kuser_helper_start[], __kuser_helper_end[];
  673. int kuser_sz = __kuser_helper_end - __kuser_helper_start;
  674. memcpy(vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
  675. /*
  676. * vectors + 0xfe0 = __kuser_get_tls
  677. * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
  678. */
  679. if (tls_emu || has_tls_reg)
  680. memcpy(vectors + 0xfe0, vectors + 0xfe8, 4);
  681. }
  682. #else
  683. static inline void __init kuser_init(void *vectors)
  684. {
  685. }
  686. #endif
  687. #ifndef CONFIG_CPU_V7M
  688. static void copy_from_lma(void *vma, void *lma_start, void *lma_end)
  689. {
  690. memcpy(vma, lma_start, lma_end - lma_start);
  691. }
  692. static void flush_vectors(void *vma, size_t offset, size_t size)
  693. {
  694. unsigned long start = (unsigned long)vma + offset;
  695. unsigned long end = start + size;
  696. flush_icache_range(start, end);
  697. }
  698. #ifdef CONFIG_HARDEN_BRANCH_HISTORY
  699. int spectre_bhb_update_vectors(unsigned int method)
  700. {
  701. extern char __vectors_bhb_bpiall_start[], __vectors_bhb_bpiall_end[];
  702. extern char __vectors_bhb_loop8_start[], __vectors_bhb_loop8_end[];
  703. void *vec_start, *vec_end;
  704. if (system_state >= SYSTEM_FREEING_INITMEM) {
  705. pr_err("CPU%u: Spectre BHB workaround too late - system vulnerable\n",
  706. smp_processor_id());
  707. return SPECTRE_VULNERABLE;
  708. }
  709. switch (method) {
  710. case SPECTRE_V2_METHOD_LOOP8:
  711. vec_start = __vectors_bhb_loop8_start;
  712. vec_end = __vectors_bhb_loop8_end;
  713. break;
  714. case SPECTRE_V2_METHOD_BPIALL:
  715. vec_start = __vectors_bhb_bpiall_start;
  716. vec_end = __vectors_bhb_bpiall_end;
  717. break;
  718. default:
  719. pr_err("CPU%u: unknown Spectre BHB state %d\n",
  720. smp_processor_id(), method);
  721. return SPECTRE_VULNERABLE;
  722. }
  723. copy_from_lma(vectors_page, vec_start, vec_end);
  724. flush_vectors(vectors_page, 0, vec_end - vec_start);
  725. return SPECTRE_MITIGATED;
  726. }
  727. #endif
  728. void __init early_trap_init(void *vectors_base)
  729. {
  730. extern char __stubs_start[], __stubs_end[];
  731. extern char __vectors_start[], __vectors_end[];
  732. unsigned i;
  733. vectors_page = vectors_base;
  734. /*
  735. * Poison the vectors page with an undefined instruction. This
  736. * instruction is chosen to be undefined for both ARM and Thumb
  737. * ISAs. The Thumb version is an undefined instruction with a
  738. * branch back to the undefined instruction.
  739. */
  740. for (i = 0; i < PAGE_SIZE / sizeof(u32); i++)
  741. ((u32 *)vectors_base)[i] = 0xe7fddef1;
  742. /*
  743. * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
  744. * into the vector page, mapped at 0xffff0000, and ensure these
  745. * are visible to the instruction stream.
  746. */
  747. copy_from_lma(vectors_base, __vectors_start, __vectors_end);
  748. copy_from_lma(vectors_base + 0x1000, __stubs_start, __stubs_end);
  749. kuser_init(vectors_base);
  750. flush_vectors(vectors_base, 0, PAGE_SIZE * 2);
  751. }
  752. #else /* ifndef CONFIG_CPU_V7M */
  753. void __init early_trap_init(void *vectors_base)
  754. {
  755. /*
  756. * on V7-M there is no need to copy the vector table to a dedicated
  757. * memory area. The address is configurable and so a table in the kernel
  758. * image can be used.
  759. */
  760. }
  761. #endif
  762. #ifdef CONFIG_VMAP_STACK
  763. DECLARE_PER_CPU(u8 *, irq_stack_ptr);
  764. asmlinkage DEFINE_PER_CPU(u8 *, overflow_stack_ptr);
  765. static int __init allocate_overflow_stacks(void)
  766. {
  767. u8 *stack;
  768. int cpu;
  769. for_each_possible_cpu(cpu) {
  770. stack = (u8 *)__get_free_page(GFP_KERNEL);
  771. if (WARN_ON(!stack))
  772. return -ENOMEM;
  773. per_cpu(overflow_stack_ptr, cpu) = &stack[OVERFLOW_STACK_SIZE];
  774. }
  775. return 0;
  776. }
  777. early_initcall(allocate_overflow_stacks);
  778. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  779. {
  780. unsigned long tsk_stk = (unsigned long)current->stack;
  781. #ifdef CONFIG_IRQSTACKS
  782. unsigned long irq_stk = (unsigned long)raw_cpu_read(irq_stack_ptr);
  783. #endif
  784. unsigned long ovf_stk = (unsigned long)raw_cpu_read(overflow_stack_ptr);
  785. console_verbose();
  786. pr_emerg("Insufficient stack space to handle exception!");
  787. pr_emerg("Task stack: [0x%08lx..0x%08lx]\n",
  788. tsk_stk, tsk_stk + THREAD_SIZE);
  789. #ifdef CONFIG_IRQSTACKS
  790. pr_emerg("IRQ stack: [0x%08lx..0x%08lx]\n",
  791. irq_stk - THREAD_SIZE, irq_stk);
  792. #endif
  793. pr_emerg("Overflow stack: [0x%08lx..0x%08lx]\n",
  794. ovf_stk - OVERFLOW_STACK_SIZE, ovf_stk);
  795. die("kernel stack overflow", regs, 0);
  796. }
  797. #ifndef CONFIG_ARM_LPAE
  798. /*
  799. * Normally, we rely on the logic in do_translation_fault() to update stale PMD
  800. * entries covering the vmalloc space in a task's page tables when it first
  801. * accesses the region in question. Unfortunately, this is not sufficient when
  802. * the task stack resides in the vmalloc region, as do_translation_fault() is a
  803. * C function that needs a stack to run.
  804. *
  805. * So we need to ensure that these PMD entries are up to date *before* the MM
  806. * switch. As we already have some logic in the MM switch path that takes care
  807. * of this, let's trigger it by bumping the counter every time the core vmalloc
  808. * code modifies a PMD entry in the vmalloc region. Use release semantics on
  809. * the store so that other CPUs observing the counter's new value are
  810. * guaranteed to see the updated page table entries as well.
  811. */
  812. void arch_sync_kernel_mappings(unsigned long start, unsigned long end)
  813. {
  814. if (start < VMALLOC_END && end > VMALLOC_START)
  815. atomic_inc_return_release(&init_mm.context.vmalloc_seq);
  816. }
  817. #endif
  818. #endif