entry-common.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Exception handling code
  4. *
  5. * Copyright (C) 2019 ARM Ltd.
  6. */
  7. #include <linux/context_tracking.h>
  8. #include <linux/kasan.h>
  9. #include <linux/linkage.h>
  10. #include <linux/lockdep.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/resume_user_mode.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/thread_info.h>
  16. #include <asm/cpufeature.h>
  17. #include <asm/daifflags.h>
  18. #include <asm/esr.h>
  19. #include <asm/exception.h>
  20. #include <asm/irq_regs.h>
  21. #include <asm/kprobes.h>
  22. #include <asm/mmu.h>
  23. #include <asm/processor.h>
  24. #include <asm/sdei.h>
  25. #include <asm/stacktrace.h>
  26. #include <asm/sysreg.h>
  27. #include <asm/system_misc.h>
  28. /*
  29. * Handle IRQ/context state management when entering from kernel mode.
  30. * Before this function is called it is not safe to call regular kernel code,
  31. * instrumentable code, or any code which may trigger an exception.
  32. *
  33. * This is intended to match the logic in irqentry_enter(), handling the kernel
  34. * mode transitions only.
  35. */
  36. static __always_inline void __enter_from_kernel_mode(struct pt_regs *regs)
  37. {
  38. regs->exit_rcu = false;
  39. if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
  40. lockdep_hardirqs_off(CALLER_ADDR0);
  41. ct_irq_enter();
  42. trace_hardirqs_off_finish();
  43. regs->exit_rcu = true;
  44. return;
  45. }
  46. lockdep_hardirqs_off(CALLER_ADDR0);
  47. rcu_irq_enter_check_tick();
  48. trace_hardirqs_off_finish();
  49. }
  50. static void noinstr enter_from_kernel_mode(struct pt_regs *regs)
  51. {
  52. __enter_from_kernel_mode(regs);
  53. mte_check_tfsr_entry();
  54. mte_disable_tco_entry(current);
  55. }
  56. /*
  57. * Handle IRQ/context state management when exiting to kernel mode.
  58. * After this function returns it is not safe to call regular kernel code,
  59. * instrumentable code, or any code which may trigger an exception.
  60. *
  61. * This is intended to match the logic in irqentry_exit(), handling the kernel
  62. * mode transitions only, and with preemption handled elsewhere.
  63. */
  64. static __always_inline void __exit_to_kernel_mode(struct pt_regs *regs)
  65. {
  66. lockdep_assert_irqs_disabled();
  67. if (interrupts_enabled(regs)) {
  68. if (regs->exit_rcu) {
  69. trace_hardirqs_on_prepare();
  70. lockdep_hardirqs_on_prepare();
  71. ct_irq_exit();
  72. lockdep_hardirqs_on(CALLER_ADDR0);
  73. return;
  74. }
  75. trace_hardirqs_on();
  76. } else {
  77. if (regs->exit_rcu)
  78. ct_irq_exit();
  79. }
  80. }
  81. static void noinstr exit_to_kernel_mode(struct pt_regs *regs)
  82. {
  83. mte_check_tfsr_exit();
  84. __exit_to_kernel_mode(regs);
  85. }
  86. /*
  87. * Handle IRQ/context state management when entering from user mode.
  88. * Before this function is called it is not safe to call regular kernel code,
  89. * instrumentable code, or any code which may trigger an exception.
  90. */
  91. static __always_inline void __enter_from_user_mode(void)
  92. {
  93. lockdep_hardirqs_off(CALLER_ADDR0);
  94. CT_WARN_ON(ct_state() != CT_STATE_USER);
  95. user_exit_irqoff();
  96. trace_hardirqs_off_finish();
  97. mte_disable_tco_entry(current);
  98. }
  99. static __always_inline void enter_from_user_mode(struct pt_regs *regs)
  100. {
  101. __enter_from_user_mode();
  102. }
  103. /*
  104. * Handle IRQ/context state management when exiting to user mode.
  105. * After this function returns it is not safe to call regular kernel code,
  106. * instrumentable code, or any code which may trigger an exception.
  107. */
  108. static __always_inline void __exit_to_user_mode(void)
  109. {
  110. trace_hardirqs_on_prepare();
  111. lockdep_hardirqs_on_prepare();
  112. user_enter_irqoff();
  113. lockdep_hardirqs_on(CALLER_ADDR0);
  114. }
  115. static void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags)
  116. {
  117. do {
  118. local_irq_enable();
  119. if (thread_flags & _TIF_NEED_RESCHED)
  120. schedule();
  121. if (thread_flags & _TIF_UPROBE)
  122. uprobe_notify_resume(regs);
  123. if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
  124. clear_thread_flag(TIF_MTE_ASYNC_FAULT);
  125. send_sig_fault(SIGSEGV, SEGV_MTEAERR,
  126. (void __user *)NULL, current);
  127. }
  128. if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
  129. do_signal(regs);
  130. if (thread_flags & _TIF_NOTIFY_RESUME)
  131. resume_user_mode_work(regs);
  132. if (thread_flags & _TIF_FOREIGN_FPSTATE)
  133. fpsimd_restore_current_state();
  134. local_irq_disable();
  135. thread_flags = read_thread_flags();
  136. } while (thread_flags & _TIF_WORK_MASK);
  137. }
  138. static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs)
  139. {
  140. unsigned long flags;
  141. local_irq_disable();
  142. flags = read_thread_flags();
  143. if (unlikely(flags & _TIF_WORK_MASK))
  144. do_notify_resume(regs, flags);
  145. local_daif_mask();
  146. lockdep_sys_exit();
  147. }
  148. static __always_inline void exit_to_user_mode(struct pt_regs *regs)
  149. {
  150. exit_to_user_mode_prepare(regs);
  151. mte_check_tfsr_exit();
  152. __exit_to_user_mode();
  153. }
  154. asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs)
  155. {
  156. exit_to_user_mode(regs);
  157. }
  158. /*
  159. * Handle IRQ/context state management when entering an NMI from user/kernel
  160. * mode. Before this function is called it is not safe to call regular kernel
  161. * code, instrumentable code, or any code which may trigger an exception.
  162. */
  163. static void noinstr arm64_enter_nmi(struct pt_regs *regs)
  164. {
  165. regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
  166. __nmi_enter();
  167. lockdep_hardirqs_off(CALLER_ADDR0);
  168. lockdep_hardirq_enter();
  169. ct_nmi_enter();
  170. trace_hardirqs_off_finish();
  171. ftrace_nmi_enter();
  172. }
  173. /*
  174. * Handle IRQ/context state management when exiting an NMI from user/kernel
  175. * mode. After this function returns it is not safe to call regular kernel
  176. * code, instrumentable code, or any code which may trigger an exception.
  177. */
  178. static void noinstr arm64_exit_nmi(struct pt_regs *regs)
  179. {
  180. bool restore = regs->lockdep_hardirqs;
  181. ftrace_nmi_exit();
  182. if (restore) {
  183. trace_hardirqs_on_prepare();
  184. lockdep_hardirqs_on_prepare();
  185. }
  186. ct_nmi_exit();
  187. lockdep_hardirq_exit();
  188. if (restore)
  189. lockdep_hardirqs_on(CALLER_ADDR0);
  190. __nmi_exit();
  191. }
  192. /*
  193. * Handle IRQ/context state management when entering a debug exception from
  194. * kernel mode. Before this function is called it is not safe to call regular
  195. * kernel code, instrumentable code, or any code which may trigger an exception.
  196. */
  197. static void noinstr arm64_enter_el1_dbg(struct pt_regs *regs)
  198. {
  199. regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
  200. lockdep_hardirqs_off(CALLER_ADDR0);
  201. ct_nmi_enter();
  202. trace_hardirqs_off_finish();
  203. }
  204. /*
  205. * Handle IRQ/context state management when exiting a debug exception from
  206. * kernel mode. After this function returns it is not safe to call regular
  207. * kernel code, instrumentable code, or any code which may trigger an exception.
  208. */
  209. static void noinstr arm64_exit_el1_dbg(struct pt_regs *regs)
  210. {
  211. bool restore = regs->lockdep_hardirqs;
  212. if (restore) {
  213. trace_hardirqs_on_prepare();
  214. lockdep_hardirqs_on_prepare();
  215. }
  216. ct_nmi_exit();
  217. if (restore)
  218. lockdep_hardirqs_on(CALLER_ADDR0);
  219. }
  220. #ifdef CONFIG_PREEMPT_DYNAMIC
  221. DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
  222. #define need_irq_preemption() \
  223. (static_branch_unlikely(&sk_dynamic_irqentry_exit_cond_resched))
  224. #else
  225. #define need_irq_preemption() (IS_ENABLED(CONFIG_PREEMPTION))
  226. #endif
  227. static void __sched arm64_preempt_schedule_irq(void)
  228. {
  229. if (!need_irq_preemption())
  230. return;
  231. /*
  232. * Note: thread_info::preempt_count includes both thread_info::count
  233. * and thread_info::need_resched, and is not equivalent to
  234. * preempt_count().
  235. */
  236. if (READ_ONCE(current_thread_info()->preempt_count) != 0)
  237. return;
  238. /*
  239. * DAIF.DA are cleared at the start of IRQ/FIQ handling, and when GIC
  240. * priority masking is used the GIC irqchip driver will clear DAIF.IF
  241. * using gic_arch_enable_irqs() for normal IRQs. If anything is set in
  242. * DAIF we must have handled an NMI, so skip preemption.
  243. */
  244. if (system_uses_irq_prio_masking() && read_sysreg(daif))
  245. return;
  246. /*
  247. * Preempting a task from an IRQ means we leave copies of PSTATE
  248. * on the stack. cpufeature's enable calls may modify PSTATE, but
  249. * resuming one of these preempted tasks would undo those changes.
  250. *
  251. * Only allow a task to be preempted once cpufeatures have been
  252. * enabled.
  253. */
  254. if (system_capabilities_finalized())
  255. preempt_schedule_irq();
  256. }
  257. static void do_interrupt_handler(struct pt_regs *regs,
  258. void (*handler)(struct pt_regs *))
  259. {
  260. struct pt_regs *old_regs = set_irq_regs(regs);
  261. if (on_thread_stack())
  262. call_on_irq_stack(regs, handler);
  263. else
  264. handler(regs);
  265. set_irq_regs(old_regs);
  266. }
  267. extern void (*handle_arch_irq)(struct pt_regs *);
  268. extern void (*handle_arch_fiq)(struct pt_regs *);
  269. static void noinstr __panic_unhandled(struct pt_regs *regs, const char *vector,
  270. unsigned long esr)
  271. {
  272. arm64_enter_nmi(regs);
  273. console_verbose();
  274. pr_crit("Unhandled %s exception on CPU%d, ESR 0x%016lx -- %s\n",
  275. vector, smp_processor_id(), esr,
  276. esr_get_class_string(esr));
  277. __show_regs(regs);
  278. panic("Unhandled exception");
  279. }
  280. #define UNHANDLED(el, regsize, vector) \
  281. asmlinkage void noinstr el##_##regsize##_##vector##_handler(struct pt_regs *regs) \
  282. { \
  283. const char *desc = #regsize "-bit " #el " " #vector; \
  284. __panic_unhandled(regs, desc, read_sysreg(esr_el1)); \
  285. }
  286. #ifdef CONFIG_ARM64_ERRATUM_1463225
  287. static DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
  288. static void cortex_a76_erratum_1463225_svc_handler(void)
  289. {
  290. u32 reg, val;
  291. if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
  292. return;
  293. if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
  294. return;
  295. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
  296. reg = read_sysreg(mdscr_el1);
  297. val = reg | DBG_MDSCR_SS | DBG_MDSCR_KDE;
  298. write_sysreg(val, mdscr_el1);
  299. asm volatile("msr daifclr, #8");
  300. isb();
  301. /* We will have taken a single-step exception by this point */
  302. write_sysreg(reg, mdscr_el1);
  303. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
  304. }
  305. static __always_inline bool
  306. cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
  307. {
  308. if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
  309. return false;
  310. /*
  311. * We've taken a dummy step exception from the kernel to ensure
  312. * that interrupts are re-enabled on the syscall path. Return back
  313. * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
  314. * masked so that we can safely restore the mdscr and get on with
  315. * handling the syscall.
  316. */
  317. regs->pstate |= PSR_D_BIT;
  318. return true;
  319. }
  320. #else /* CONFIG_ARM64_ERRATUM_1463225 */
  321. static void cortex_a76_erratum_1463225_svc_handler(void) { }
  322. static bool cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
  323. {
  324. return false;
  325. }
  326. #endif /* CONFIG_ARM64_ERRATUM_1463225 */
  327. /*
  328. * As per the ABI exit SME streaming mode and clear the SVE state not
  329. * shared with FPSIMD on syscall entry.
  330. */
  331. static inline void fpsimd_syscall_enter(void)
  332. {
  333. /* Ensure PSTATE.SM is clear, but leave PSTATE.ZA as-is. */
  334. if (system_supports_sme())
  335. sme_smstop_sm();
  336. /*
  337. * The CPU is not in streaming mode. If non-streaming SVE is not
  338. * supported, there is no SVE state that needs to be discarded.
  339. */
  340. if (!system_supports_sve())
  341. return;
  342. if (test_thread_flag(TIF_SVE)) {
  343. unsigned int sve_vq_minus_one;
  344. sve_vq_minus_one = sve_vq_from_vl(task_get_sve_vl(current)) - 1;
  345. sve_flush_live(true, sve_vq_minus_one);
  346. }
  347. /*
  348. * Any live non-FPSIMD SVE state has been zeroed. Allow
  349. * fpsimd_save_user_state() to lazily discard SVE state until either
  350. * the live state is unbound or fpsimd_syscall_exit() is called.
  351. */
  352. __this_cpu_write(fpsimd_last_state.to_save, FP_STATE_FPSIMD);
  353. }
  354. static __always_inline void fpsimd_syscall_exit(void)
  355. {
  356. if (!system_supports_sve())
  357. return;
  358. /*
  359. * The current task's user FPSIMD/SVE/SME state is now bound to this
  360. * CPU. The fpsimd_last_state.to_save value is either:
  361. *
  362. * - FP_STATE_FPSIMD, if the state has not been reloaded on this CPU
  363. * since fpsimd_syscall_enter().
  364. *
  365. * - FP_STATE_CURRENT, if the state has been reloaded on this CPU at
  366. * any point.
  367. *
  368. * Reset this to FP_STATE_CURRENT to stop lazy discarding.
  369. */
  370. __this_cpu_write(fpsimd_last_state.to_save, FP_STATE_CURRENT);
  371. }
  372. UNHANDLED(el1t, 64, sync)
  373. UNHANDLED(el1t, 64, irq)
  374. UNHANDLED(el1t, 64, fiq)
  375. UNHANDLED(el1t, 64, error)
  376. static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr)
  377. {
  378. unsigned long far = read_sysreg(far_el1);
  379. enter_from_kernel_mode(regs);
  380. local_daif_inherit(regs);
  381. do_mem_abort(far, esr, regs);
  382. local_daif_mask();
  383. exit_to_kernel_mode(regs);
  384. }
  385. static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr)
  386. {
  387. unsigned long far = read_sysreg(far_el1);
  388. enter_from_kernel_mode(regs);
  389. local_daif_inherit(regs);
  390. do_sp_pc_abort(far, esr, regs);
  391. local_daif_mask();
  392. exit_to_kernel_mode(regs);
  393. }
  394. static void noinstr el1_undef(struct pt_regs *regs, unsigned long esr)
  395. {
  396. enter_from_kernel_mode(regs);
  397. local_daif_inherit(regs);
  398. do_el1_undef(regs, esr);
  399. local_daif_mask();
  400. exit_to_kernel_mode(regs);
  401. }
  402. static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr)
  403. {
  404. enter_from_kernel_mode(regs);
  405. local_daif_inherit(regs);
  406. do_el1_bti(regs, esr);
  407. local_daif_mask();
  408. exit_to_kernel_mode(regs);
  409. }
  410. static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr)
  411. {
  412. unsigned long far = read_sysreg(far_el1);
  413. arm64_enter_el1_dbg(regs);
  414. if (!cortex_a76_erratum_1463225_debug_handler(regs))
  415. do_debug_exception(far, esr, regs);
  416. arm64_exit_el1_dbg(regs);
  417. }
  418. static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr)
  419. {
  420. enter_from_kernel_mode(regs);
  421. local_daif_inherit(regs);
  422. do_el1_fpac(regs, esr);
  423. local_daif_mask();
  424. exit_to_kernel_mode(regs);
  425. }
  426. asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
  427. {
  428. unsigned long esr = read_sysreg(esr_el1);
  429. switch (ESR_ELx_EC(esr)) {
  430. case ESR_ELx_EC_DABT_CUR:
  431. case ESR_ELx_EC_IABT_CUR:
  432. el1_abort(regs, esr);
  433. break;
  434. /*
  435. * We don't handle ESR_ELx_EC_SP_ALIGN, since we will have hit a
  436. * recursive exception when trying to push the initial pt_regs.
  437. */
  438. case ESR_ELx_EC_PC_ALIGN:
  439. el1_pc(regs, esr);
  440. break;
  441. case ESR_ELx_EC_SYS64:
  442. case ESR_ELx_EC_UNKNOWN:
  443. el1_undef(regs, esr);
  444. break;
  445. case ESR_ELx_EC_BTI:
  446. el1_bti(regs, esr);
  447. break;
  448. case ESR_ELx_EC_BREAKPT_CUR:
  449. case ESR_ELx_EC_SOFTSTP_CUR:
  450. case ESR_ELx_EC_WATCHPT_CUR:
  451. case ESR_ELx_EC_BRK64:
  452. el1_dbg(regs, esr);
  453. break;
  454. case ESR_ELx_EC_FPAC:
  455. el1_fpac(regs, esr);
  456. break;
  457. default:
  458. __panic_unhandled(regs, "64-bit el1h sync", esr);
  459. }
  460. }
  461. static __always_inline void __el1_pnmi(struct pt_regs *regs,
  462. void (*handler)(struct pt_regs *))
  463. {
  464. arm64_enter_nmi(regs);
  465. do_interrupt_handler(regs, handler);
  466. arm64_exit_nmi(regs);
  467. }
  468. static __always_inline void __el1_irq(struct pt_regs *regs,
  469. void (*handler)(struct pt_regs *))
  470. {
  471. enter_from_kernel_mode(regs);
  472. irq_enter_rcu();
  473. do_interrupt_handler(regs, handler);
  474. irq_exit_rcu();
  475. arm64_preempt_schedule_irq();
  476. exit_to_kernel_mode(regs);
  477. }
  478. static void noinstr el1_interrupt(struct pt_regs *regs,
  479. void (*handler)(struct pt_regs *))
  480. {
  481. write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
  482. if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs))
  483. __el1_pnmi(regs, handler);
  484. else
  485. __el1_irq(regs, handler);
  486. }
  487. asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)
  488. {
  489. el1_interrupt(regs, handle_arch_irq);
  490. }
  491. asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs)
  492. {
  493. el1_interrupt(regs, handle_arch_fiq);
  494. }
  495. asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)
  496. {
  497. unsigned long esr = read_sysreg(esr_el1);
  498. local_daif_restore(DAIF_ERRCTX);
  499. arm64_enter_nmi(regs);
  500. do_serror(regs, esr);
  501. arm64_exit_nmi(regs);
  502. }
  503. static void noinstr el0_da(struct pt_regs *regs, unsigned long esr)
  504. {
  505. unsigned long far = read_sysreg(far_el1);
  506. enter_from_user_mode(regs);
  507. local_daif_restore(DAIF_PROCCTX);
  508. do_mem_abort(far, esr, regs);
  509. exit_to_user_mode(regs);
  510. }
  511. static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)
  512. {
  513. unsigned long far = read_sysreg(far_el1);
  514. /*
  515. * We've taken an instruction abort from userspace and not yet
  516. * re-enabled IRQs. If the address is a kernel address, apply
  517. * BP hardening prior to enabling IRQs and pre-emption.
  518. */
  519. if (!is_ttbr0_addr(far))
  520. arm64_apply_bp_hardening();
  521. enter_from_user_mode(regs);
  522. local_daif_restore(DAIF_PROCCTX);
  523. do_mem_abort(far, esr, regs);
  524. exit_to_user_mode(regs);
  525. }
  526. static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr)
  527. {
  528. enter_from_user_mode(regs);
  529. local_daif_restore(DAIF_PROCCTX);
  530. do_fpsimd_acc(esr, regs);
  531. exit_to_user_mode(regs);
  532. }
  533. static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr)
  534. {
  535. enter_from_user_mode(regs);
  536. local_daif_restore(DAIF_PROCCTX);
  537. do_sve_acc(esr, regs);
  538. exit_to_user_mode(regs);
  539. }
  540. static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr)
  541. {
  542. enter_from_user_mode(regs);
  543. local_daif_restore(DAIF_PROCCTX);
  544. do_sme_acc(esr, regs);
  545. exit_to_user_mode(regs);
  546. }
  547. static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr)
  548. {
  549. enter_from_user_mode(regs);
  550. local_daif_restore(DAIF_PROCCTX);
  551. do_fpsimd_exc(esr, regs);
  552. exit_to_user_mode(regs);
  553. }
  554. static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr)
  555. {
  556. enter_from_user_mode(regs);
  557. local_daif_restore(DAIF_PROCCTX);
  558. do_el0_sys(esr, regs);
  559. exit_to_user_mode(regs);
  560. }
  561. static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr)
  562. {
  563. unsigned long far = read_sysreg(far_el1);
  564. if (!is_ttbr0_addr(instruction_pointer(regs)))
  565. arm64_apply_bp_hardening();
  566. enter_from_user_mode(regs);
  567. local_daif_restore(DAIF_PROCCTX);
  568. do_sp_pc_abort(far, esr, regs);
  569. exit_to_user_mode(regs);
  570. }
  571. static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr)
  572. {
  573. enter_from_user_mode(regs);
  574. local_daif_restore(DAIF_PROCCTX);
  575. do_sp_pc_abort(regs->sp, esr, regs);
  576. exit_to_user_mode(regs);
  577. }
  578. static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr)
  579. {
  580. enter_from_user_mode(regs);
  581. local_daif_restore(DAIF_PROCCTX);
  582. do_el0_undef(regs, esr);
  583. exit_to_user_mode(regs);
  584. }
  585. static void noinstr el0_bti(struct pt_regs *regs)
  586. {
  587. enter_from_user_mode(regs);
  588. local_daif_restore(DAIF_PROCCTX);
  589. do_el0_bti(regs);
  590. exit_to_user_mode(regs);
  591. }
  592. static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr)
  593. {
  594. enter_from_user_mode(regs);
  595. local_daif_restore(DAIF_PROCCTX);
  596. do_el0_mops(regs, esr);
  597. exit_to_user_mode(regs);
  598. }
  599. static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr)
  600. {
  601. enter_from_user_mode(regs);
  602. local_daif_restore(DAIF_PROCCTX);
  603. bad_el0_sync(regs, 0, esr);
  604. exit_to_user_mode(regs);
  605. }
  606. static void noinstr el0_dbg(struct pt_regs *regs, unsigned long esr)
  607. {
  608. /* Only watchpoints write FAR_EL1, otherwise its UNKNOWN */
  609. unsigned long far = read_sysreg(far_el1);
  610. enter_from_user_mode(regs);
  611. do_debug_exception(far, esr, regs);
  612. local_daif_restore(DAIF_PROCCTX);
  613. exit_to_user_mode(regs);
  614. }
  615. static void noinstr el0_svc(struct pt_regs *regs)
  616. {
  617. enter_from_user_mode(regs);
  618. cortex_a76_erratum_1463225_svc_handler();
  619. fpsimd_syscall_enter();
  620. local_daif_restore(DAIF_PROCCTX);
  621. do_el0_svc(regs);
  622. exit_to_user_mode(regs);
  623. fpsimd_syscall_exit();
  624. }
  625. static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr)
  626. {
  627. enter_from_user_mode(regs);
  628. local_daif_restore(DAIF_PROCCTX);
  629. do_el0_fpac(regs, esr);
  630. exit_to_user_mode(regs);
  631. }
  632. asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
  633. {
  634. unsigned long esr = read_sysreg(esr_el1);
  635. switch (ESR_ELx_EC(esr)) {
  636. case ESR_ELx_EC_SVC64:
  637. el0_svc(regs);
  638. break;
  639. case ESR_ELx_EC_DABT_LOW:
  640. el0_da(regs, esr);
  641. break;
  642. case ESR_ELx_EC_IABT_LOW:
  643. el0_ia(regs, esr);
  644. break;
  645. case ESR_ELx_EC_FP_ASIMD:
  646. el0_fpsimd_acc(regs, esr);
  647. break;
  648. case ESR_ELx_EC_SVE:
  649. el0_sve_acc(regs, esr);
  650. break;
  651. case ESR_ELx_EC_SME:
  652. el0_sme_acc(regs, esr);
  653. break;
  654. case ESR_ELx_EC_FP_EXC64:
  655. el0_fpsimd_exc(regs, esr);
  656. break;
  657. case ESR_ELx_EC_SYS64:
  658. case ESR_ELx_EC_WFx:
  659. el0_sys(regs, esr);
  660. break;
  661. case ESR_ELx_EC_SP_ALIGN:
  662. el0_sp(regs, esr);
  663. break;
  664. case ESR_ELx_EC_PC_ALIGN:
  665. el0_pc(regs, esr);
  666. break;
  667. case ESR_ELx_EC_UNKNOWN:
  668. el0_undef(regs, esr);
  669. break;
  670. case ESR_ELx_EC_BTI:
  671. el0_bti(regs);
  672. break;
  673. case ESR_ELx_EC_MOPS:
  674. el0_mops(regs, esr);
  675. break;
  676. case ESR_ELx_EC_BREAKPT_LOW:
  677. case ESR_ELx_EC_SOFTSTP_LOW:
  678. case ESR_ELx_EC_WATCHPT_LOW:
  679. case ESR_ELx_EC_BRK64:
  680. el0_dbg(regs, esr);
  681. break;
  682. case ESR_ELx_EC_FPAC:
  683. el0_fpac(regs, esr);
  684. break;
  685. default:
  686. el0_inv(regs, esr);
  687. }
  688. }
  689. static void noinstr el0_interrupt(struct pt_regs *regs,
  690. void (*handler)(struct pt_regs *))
  691. {
  692. enter_from_user_mode(regs);
  693. write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
  694. if (regs->pc & BIT(55))
  695. arm64_apply_bp_hardening();
  696. irq_enter_rcu();
  697. do_interrupt_handler(regs, handler);
  698. irq_exit_rcu();
  699. exit_to_user_mode(regs);
  700. }
  701. static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
  702. {
  703. el0_interrupt(regs, handle_arch_irq);
  704. }
  705. asmlinkage void noinstr el0t_64_irq_handler(struct pt_regs *regs)
  706. {
  707. __el0_irq_handler_common(regs);
  708. }
  709. static void noinstr __el0_fiq_handler_common(struct pt_regs *regs)
  710. {
  711. el0_interrupt(regs, handle_arch_fiq);
  712. }
  713. asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs)
  714. {
  715. __el0_fiq_handler_common(regs);
  716. }
  717. static void noinstr __el0_error_handler_common(struct pt_regs *regs)
  718. {
  719. unsigned long esr = read_sysreg(esr_el1);
  720. enter_from_user_mode(regs);
  721. local_daif_restore(DAIF_ERRCTX);
  722. arm64_enter_nmi(regs);
  723. do_serror(regs, esr);
  724. arm64_exit_nmi(regs);
  725. local_daif_restore(DAIF_PROCCTX);
  726. exit_to_user_mode(regs);
  727. }
  728. asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)
  729. {
  730. __el0_error_handler_common(regs);
  731. }
  732. #ifdef CONFIG_COMPAT
  733. static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr)
  734. {
  735. enter_from_user_mode(regs);
  736. local_daif_restore(DAIF_PROCCTX);
  737. do_el0_cp15(esr, regs);
  738. exit_to_user_mode(regs);
  739. }
  740. static void noinstr el0_svc_compat(struct pt_regs *regs)
  741. {
  742. enter_from_user_mode(regs);
  743. cortex_a76_erratum_1463225_svc_handler();
  744. local_daif_restore(DAIF_PROCCTX);
  745. do_el0_svc_compat(regs);
  746. exit_to_user_mode(regs);
  747. }
  748. asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
  749. {
  750. unsigned long esr = read_sysreg(esr_el1);
  751. switch (ESR_ELx_EC(esr)) {
  752. case ESR_ELx_EC_SVC32:
  753. el0_svc_compat(regs);
  754. break;
  755. case ESR_ELx_EC_DABT_LOW:
  756. el0_da(regs, esr);
  757. break;
  758. case ESR_ELx_EC_IABT_LOW:
  759. el0_ia(regs, esr);
  760. break;
  761. case ESR_ELx_EC_FP_ASIMD:
  762. el0_fpsimd_acc(regs, esr);
  763. break;
  764. case ESR_ELx_EC_FP_EXC32:
  765. el0_fpsimd_exc(regs, esr);
  766. break;
  767. case ESR_ELx_EC_PC_ALIGN:
  768. el0_pc(regs, esr);
  769. break;
  770. case ESR_ELx_EC_UNKNOWN:
  771. case ESR_ELx_EC_CP14_MR:
  772. case ESR_ELx_EC_CP14_LS:
  773. case ESR_ELx_EC_CP14_64:
  774. el0_undef(regs, esr);
  775. break;
  776. case ESR_ELx_EC_CP15_32:
  777. case ESR_ELx_EC_CP15_64:
  778. el0_cp15(regs, esr);
  779. break;
  780. case ESR_ELx_EC_BREAKPT_LOW:
  781. case ESR_ELx_EC_SOFTSTP_LOW:
  782. case ESR_ELx_EC_WATCHPT_LOW:
  783. case ESR_ELx_EC_BKPT32:
  784. el0_dbg(regs, esr);
  785. break;
  786. default:
  787. el0_inv(regs, esr);
  788. }
  789. }
  790. asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)
  791. {
  792. __el0_irq_handler_common(regs);
  793. }
  794. asmlinkage void noinstr el0t_32_fiq_handler(struct pt_regs *regs)
  795. {
  796. __el0_fiq_handler_common(regs);
  797. }
  798. asmlinkage void noinstr el0t_32_error_handler(struct pt_regs *regs)
  799. {
  800. __el0_error_handler_common(regs);
  801. }
  802. #else /* CONFIG_COMPAT */
  803. UNHANDLED(el0t, 32, sync)
  804. UNHANDLED(el0t, 32, irq)
  805. UNHANDLED(el0t, 32, fiq)
  806. UNHANDLED(el0t, 32, error)
  807. #endif /* CONFIG_COMPAT */
  808. #ifdef CONFIG_VMAP_STACK
  809. asmlinkage void noinstr __noreturn handle_bad_stack(struct pt_regs *regs)
  810. {
  811. unsigned long esr = read_sysreg(esr_el1);
  812. unsigned long far = read_sysreg(far_el1);
  813. arm64_enter_nmi(regs);
  814. panic_bad_stack(regs, esr, far);
  815. }
  816. #endif /* CONFIG_VMAP_STACK */
  817. #ifdef CONFIG_ARM_SDE_INTERFACE
  818. asmlinkage noinstr unsigned long
  819. __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
  820. {
  821. unsigned long ret;
  822. /*
  823. * We didn't take an exception to get here, so the HW hasn't
  824. * set/cleared bits in PSTATE that we may rely on.
  825. *
  826. * The original SDEI spec (ARM DEN 0054A) can be read ambiguously as to
  827. * whether PSTATE bits are inherited unchanged or generated from
  828. * scratch, and the TF-A implementation always clears PAN and always
  829. * clears UAO. There are no other known implementations.
  830. *
  831. * Subsequent revisions (ARM DEN 0054B) follow the usual rules for how
  832. * PSTATE is modified upon architectural exceptions, and so PAN is
  833. * either inherited or set per SCTLR_ELx.SPAN, and UAO is always
  834. * cleared.
  835. *
  836. * We must explicitly reset PAN to the expected state, including
  837. * clearing it when the host isn't using it, in case a VM had it set.
  838. */
  839. if (system_uses_hw_pan())
  840. set_pstate_pan(1);
  841. else if (cpu_has_pan())
  842. set_pstate_pan(0);
  843. arm64_enter_nmi(regs);
  844. ret = do_sdei_event(regs, arg);
  845. arm64_exit_nmi(regs);
  846. return ret;
  847. }
  848. #endif /* CONFIG_ARM_SDE_INTERFACE */