traps.c 24 KB

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