entry-common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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/sched.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/thread_info.h>
  15. #include <asm/cpufeature.h>
  16. #include <asm/daifflags.h>
  17. #include <asm/esr.h>
  18. #include <asm/exception.h>
  19. #include <asm/irq_regs.h>
  20. #include <asm/kprobes.h>
  21. #include <asm/mmu.h>
  22. #include <asm/processor.h>
  23. #include <asm/sdei.h>
  24. #include <asm/stacktrace.h>
  25. #include <asm/sysreg.h>
  26. #include <asm/system_misc.h>
  27. #include <trace/hooks/traps.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. * intrumentable 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. * intrumentable 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. * intrumentable 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() != CONTEXT_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. * intrumentable 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 __always_inline void prepare_exit_to_user_mode(struct pt_regs *regs)
  116. {
  117. unsigned long flags;
  118. local_daif_mask();
  119. flags = read_thread_flags();
  120. if (unlikely(flags & _TIF_WORK_MASK))
  121. do_notify_resume(regs, flags);
  122. }
  123. static __always_inline void exit_to_user_mode(struct pt_regs *regs)
  124. {
  125. prepare_exit_to_user_mode(regs);
  126. mte_check_tfsr_exit();
  127. __exit_to_user_mode();
  128. }
  129. asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs)
  130. {
  131. exit_to_user_mode(regs);
  132. }
  133. /*
  134. * Handle IRQ/context state management when entering an NMI from user/kernel
  135. * mode. Before this function is called it is not safe to call regular kernel
  136. * code, intrumentable code, or any code which may trigger an exception.
  137. */
  138. static void noinstr arm64_enter_nmi(struct pt_regs *regs)
  139. {
  140. regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
  141. __nmi_enter();
  142. lockdep_hardirqs_off(CALLER_ADDR0);
  143. lockdep_hardirq_enter();
  144. ct_nmi_enter();
  145. trace_hardirqs_off_finish();
  146. ftrace_nmi_enter();
  147. }
  148. /*
  149. * Handle IRQ/context state management when exiting an NMI from user/kernel
  150. * mode. After this function returns it is not safe to call regular kernel
  151. * code, intrumentable code, or any code which may trigger an exception.
  152. */
  153. static void noinstr arm64_exit_nmi(struct pt_regs *regs)
  154. {
  155. bool restore = regs->lockdep_hardirqs;
  156. ftrace_nmi_exit();
  157. if (restore) {
  158. trace_hardirqs_on_prepare();
  159. lockdep_hardirqs_on_prepare();
  160. }
  161. ct_nmi_exit();
  162. lockdep_hardirq_exit();
  163. if (restore)
  164. lockdep_hardirqs_on(CALLER_ADDR0);
  165. __nmi_exit();
  166. }
  167. /*
  168. * Handle IRQ/context state management when entering a debug exception from
  169. * kernel mode. Before this function is called it is not safe to call regular
  170. * kernel code, intrumentable code, or any code which may trigger an exception.
  171. */
  172. static void noinstr arm64_enter_el1_dbg(struct pt_regs *regs)
  173. {
  174. regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
  175. lockdep_hardirqs_off(CALLER_ADDR0);
  176. ct_nmi_enter();
  177. trace_hardirqs_off_finish();
  178. }
  179. /*
  180. * Handle IRQ/context state management when exiting a debug exception from
  181. * kernel mode. After this function returns it is not safe to call regular
  182. * kernel code, intrumentable code, or any code which may trigger an exception.
  183. */
  184. static void noinstr arm64_exit_el1_dbg(struct pt_regs *regs)
  185. {
  186. bool restore = regs->lockdep_hardirqs;
  187. if (restore) {
  188. trace_hardirqs_on_prepare();
  189. lockdep_hardirqs_on_prepare();
  190. }
  191. ct_nmi_exit();
  192. if (restore)
  193. lockdep_hardirqs_on(CALLER_ADDR0);
  194. }
  195. #ifdef CONFIG_PREEMPT_DYNAMIC
  196. DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
  197. #define need_irq_preemption() \
  198. (static_branch_unlikely(&sk_dynamic_irqentry_exit_cond_resched))
  199. #else
  200. #define need_irq_preemption() (IS_ENABLED(CONFIG_PREEMPTION))
  201. #endif
  202. static void __sched arm64_preempt_schedule_irq(void)
  203. {
  204. if (!need_irq_preemption())
  205. return;
  206. /*
  207. * Note: thread_info::preempt_count includes both thread_info::count
  208. * and thread_info::need_resched, and is not equivalent to
  209. * preempt_count().
  210. */
  211. if (READ_ONCE(current_thread_info()->preempt_count) != 0)
  212. return;
  213. /*
  214. * DAIF.DA are cleared at the start of IRQ/FIQ handling, and when GIC
  215. * priority masking is used the GIC irqchip driver will clear DAIF.IF
  216. * using gic_arch_enable_irqs() for normal IRQs. If anything is set in
  217. * DAIF we must have handled an NMI, so skip preemption.
  218. */
  219. if (system_uses_irq_prio_masking() && read_sysreg(daif))
  220. return;
  221. /*
  222. * Preempting a task from an IRQ means we leave copies of PSTATE
  223. * on the stack. cpufeature's enable calls may modify PSTATE, but
  224. * resuming one of these preempted tasks would undo those changes.
  225. *
  226. * Only allow a task to be preempted once cpufeatures have been
  227. * enabled.
  228. */
  229. if (system_capabilities_finalized())
  230. preempt_schedule_irq();
  231. }
  232. static void do_interrupt_handler(struct pt_regs *regs,
  233. void (*handler)(struct pt_regs *))
  234. {
  235. struct pt_regs *old_regs = set_irq_regs(regs);
  236. if (on_thread_stack())
  237. call_on_irq_stack(regs, handler);
  238. else
  239. handler(regs);
  240. set_irq_regs(old_regs);
  241. }
  242. extern void (*handle_arch_irq)(struct pt_regs *);
  243. extern void (*handle_arch_fiq)(struct pt_regs *);
  244. static void noinstr __panic_unhandled(struct pt_regs *regs, const char *vector,
  245. unsigned long esr)
  246. {
  247. arm64_enter_nmi(regs);
  248. console_verbose();
  249. pr_crit("Unhandled %s exception on CPU%d, ESR 0x%016lx -- %s\n",
  250. vector, smp_processor_id(), esr,
  251. esr_get_class_string(esr));
  252. trace_android_rvh_panic_unhandled(regs, vector, esr);
  253. __show_regs(regs);
  254. panic("Unhandled exception");
  255. }
  256. #define UNHANDLED(el, regsize, vector) \
  257. asmlinkage void noinstr el##_##regsize##_##vector##_handler(struct pt_regs *regs) \
  258. { \
  259. const char *desc = #regsize "-bit " #el " " #vector; \
  260. __panic_unhandled(regs, desc, read_sysreg(esr_el1)); \
  261. }
  262. #ifdef CONFIG_ARM64_ERRATUM_1463225
  263. static DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
  264. static void cortex_a76_erratum_1463225_svc_handler(void)
  265. {
  266. u32 reg, val;
  267. if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
  268. return;
  269. if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
  270. return;
  271. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
  272. reg = read_sysreg(mdscr_el1);
  273. val = reg | DBG_MDSCR_SS | DBG_MDSCR_KDE;
  274. write_sysreg(val, mdscr_el1);
  275. asm volatile("msr daifclr, #8");
  276. isb();
  277. /* We will have taken a single-step exception by this point */
  278. write_sysreg(reg, mdscr_el1);
  279. __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
  280. }
  281. static __always_inline bool
  282. cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
  283. {
  284. if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
  285. return false;
  286. /*
  287. * We've taken a dummy step exception from the kernel to ensure
  288. * that interrupts are re-enabled on the syscall path. Return back
  289. * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
  290. * masked so that we can safely restore the mdscr and get on with
  291. * handling the syscall.
  292. */
  293. regs->pstate |= PSR_D_BIT;
  294. return true;
  295. }
  296. #else /* CONFIG_ARM64_ERRATUM_1463225 */
  297. static void cortex_a76_erratum_1463225_svc_handler(void) { }
  298. static bool cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
  299. {
  300. return false;
  301. }
  302. #endif /* CONFIG_ARM64_ERRATUM_1463225 */
  303. UNHANDLED(el1t, 64, sync)
  304. UNHANDLED(el1t, 64, irq)
  305. UNHANDLED(el1t, 64, fiq)
  306. UNHANDLED(el1t, 64, error)
  307. static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr)
  308. {
  309. unsigned long far = read_sysreg(far_el1);
  310. enter_from_kernel_mode(regs);
  311. local_daif_inherit(regs);
  312. do_mem_abort(far, esr, regs);
  313. local_daif_mask();
  314. exit_to_kernel_mode(regs);
  315. }
  316. static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr)
  317. {
  318. unsigned long far = read_sysreg(far_el1);
  319. enter_from_kernel_mode(regs);
  320. local_daif_inherit(regs);
  321. do_sp_pc_abort(far, esr, regs);
  322. local_daif_mask();
  323. exit_to_kernel_mode(regs);
  324. }
  325. static void noinstr el1_undef(struct pt_regs *regs, unsigned long esr)
  326. {
  327. enter_from_kernel_mode(regs);
  328. local_daif_inherit(regs);
  329. do_undefinstr(regs, esr);
  330. local_daif_mask();
  331. exit_to_kernel_mode(regs);
  332. }
  333. static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr)
  334. {
  335. enter_from_kernel_mode(regs);
  336. local_daif_inherit(regs);
  337. do_el1_bti(regs, esr);
  338. local_daif_mask();
  339. exit_to_kernel_mode(regs);
  340. }
  341. static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr)
  342. {
  343. unsigned long far = read_sysreg(far_el1);
  344. arm64_enter_el1_dbg(regs);
  345. if (!cortex_a76_erratum_1463225_debug_handler(regs))
  346. do_debug_exception(far, esr, regs);
  347. arm64_exit_el1_dbg(regs);
  348. }
  349. static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr)
  350. {
  351. enter_from_kernel_mode(regs);
  352. local_daif_inherit(regs);
  353. do_el1_fpac(regs, esr);
  354. local_daif_mask();
  355. exit_to_kernel_mode(regs);
  356. }
  357. asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
  358. {
  359. unsigned long esr = read_sysreg(esr_el1);
  360. switch (ESR_ELx_EC(esr)) {
  361. case ESR_ELx_EC_DABT_CUR:
  362. case ESR_ELx_EC_IABT_CUR:
  363. el1_abort(regs, esr);
  364. break;
  365. /*
  366. * We don't handle ESR_ELx_EC_SP_ALIGN, since we will have hit a
  367. * recursive exception when trying to push the initial pt_regs.
  368. */
  369. case ESR_ELx_EC_PC_ALIGN:
  370. el1_pc(regs, esr);
  371. break;
  372. case ESR_ELx_EC_SYS64:
  373. case ESR_ELx_EC_UNKNOWN:
  374. el1_undef(regs, esr);
  375. break;
  376. case ESR_ELx_EC_BTI:
  377. el1_bti(regs, esr);
  378. break;
  379. case ESR_ELx_EC_BREAKPT_CUR:
  380. case ESR_ELx_EC_SOFTSTP_CUR:
  381. case ESR_ELx_EC_WATCHPT_CUR:
  382. case ESR_ELx_EC_BRK64:
  383. el1_dbg(regs, esr);
  384. break;
  385. case ESR_ELx_EC_FPAC:
  386. el1_fpac(regs, esr);
  387. break;
  388. default:
  389. __panic_unhandled(regs, "64-bit el1h sync", esr);
  390. }
  391. }
  392. static __always_inline void __el1_pnmi(struct pt_regs *regs,
  393. void (*handler)(struct pt_regs *))
  394. {
  395. arm64_enter_nmi(regs);
  396. do_interrupt_handler(regs, handler);
  397. arm64_exit_nmi(regs);
  398. }
  399. static __always_inline void __el1_irq(struct pt_regs *regs,
  400. void (*handler)(struct pt_regs *))
  401. {
  402. enter_from_kernel_mode(regs);
  403. irq_enter_rcu();
  404. do_interrupt_handler(regs, handler);
  405. irq_exit_rcu();
  406. arm64_preempt_schedule_irq();
  407. exit_to_kernel_mode(regs);
  408. }
  409. static void noinstr el1_interrupt(struct pt_regs *regs,
  410. void (*handler)(struct pt_regs *))
  411. {
  412. write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
  413. if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs))
  414. __el1_pnmi(regs, handler);
  415. else
  416. __el1_irq(regs, handler);
  417. }
  418. asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)
  419. {
  420. el1_interrupt(regs, handle_arch_irq);
  421. }
  422. asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs)
  423. {
  424. el1_interrupt(regs, handle_arch_fiq);
  425. }
  426. asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)
  427. {
  428. unsigned long esr = read_sysreg(esr_el1);
  429. local_daif_restore(DAIF_ERRCTX);
  430. arm64_enter_nmi(regs);
  431. do_serror(regs, esr);
  432. arm64_exit_nmi(regs);
  433. }
  434. static void noinstr el0_da(struct pt_regs *regs, unsigned long esr)
  435. {
  436. unsigned long far = read_sysreg(far_el1);
  437. enter_from_user_mode(regs);
  438. local_daif_restore(DAIF_PROCCTX);
  439. do_mem_abort(far, esr, regs);
  440. exit_to_user_mode(regs);
  441. }
  442. static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)
  443. {
  444. unsigned long far = read_sysreg(far_el1);
  445. /*
  446. * We've taken an instruction abort from userspace and not yet
  447. * re-enabled IRQs. If the address is a kernel address, apply
  448. * BP hardening prior to enabling IRQs and pre-emption.
  449. */
  450. if (!is_ttbr0_addr(far))
  451. arm64_apply_bp_hardening();
  452. enter_from_user_mode(regs);
  453. local_daif_restore(DAIF_PROCCTX);
  454. do_mem_abort(far, esr, regs);
  455. exit_to_user_mode(regs);
  456. }
  457. static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr)
  458. {
  459. enter_from_user_mode(regs);
  460. local_daif_restore(DAIF_PROCCTX);
  461. do_fpsimd_acc(esr, regs);
  462. exit_to_user_mode(regs);
  463. }
  464. static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr)
  465. {
  466. enter_from_user_mode(regs);
  467. local_daif_restore(DAIF_PROCCTX);
  468. do_sve_acc(esr, regs);
  469. exit_to_user_mode(regs);
  470. }
  471. static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr)
  472. {
  473. enter_from_user_mode(regs);
  474. local_daif_restore(DAIF_PROCCTX);
  475. do_sme_acc(esr, regs);
  476. exit_to_user_mode(regs);
  477. }
  478. static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr)
  479. {
  480. enter_from_user_mode(regs);
  481. local_daif_restore(DAIF_PROCCTX);
  482. do_fpsimd_exc(esr, regs);
  483. exit_to_user_mode(regs);
  484. }
  485. static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr)
  486. {
  487. enter_from_user_mode(regs);
  488. local_daif_restore(DAIF_PROCCTX);
  489. do_sysinstr(esr, regs);
  490. exit_to_user_mode(regs);
  491. }
  492. static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr)
  493. {
  494. unsigned long far = read_sysreg(far_el1);
  495. if (!is_ttbr0_addr(instruction_pointer(regs)))
  496. arm64_apply_bp_hardening();
  497. enter_from_user_mode(regs);
  498. local_daif_restore(DAIF_PROCCTX);
  499. do_sp_pc_abort(far, esr, regs);
  500. exit_to_user_mode(regs);
  501. }
  502. static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr)
  503. {
  504. enter_from_user_mode(regs);
  505. local_daif_restore(DAIF_PROCCTX);
  506. do_sp_pc_abort(regs->sp, esr, regs);
  507. exit_to_user_mode(regs);
  508. }
  509. static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr)
  510. {
  511. enter_from_user_mode(regs);
  512. local_daif_restore(DAIF_PROCCTX);
  513. do_undefinstr(regs, esr);
  514. exit_to_user_mode(regs);
  515. }
  516. static void noinstr el0_bti(struct pt_regs *regs)
  517. {
  518. enter_from_user_mode(regs);
  519. local_daif_restore(DAIF_PROCCTX);
  520. do_el0_bti(regs);
  521. exit_to_user_mode(regs);
  522. }
  523. static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr)
  524. {
  525. enter_from_user_mode(regs);
  526. local_daif_restore(DAIF_PROCCTX);
  527. bad_el0_sync(regs, 0, esr);
  528. exit_to_user_mode(regs);
  529. }
  530. static void noinstr el0_dbg(struct pt_regs *regs, unsigned long esr)
  531. {
  532. /* Only watchpoints write FAR_EL1, otherwise its UNKNOWN */
  533. unsigned long far = read_sysreg(far_el1);
  534. enter_from_user_mode(regs);
  535. do_debug_exception(far, esr, regs);
  536. local_daif_restore(DAIF_PROCCTX);
  537. exit_to_user_mode(regs);
  538. }
  539. static void noinstr el0_svc(struct pt_regs *regs)
  540. {
  541. enter_from_user_mode(regs);
  542. cortex_a76_erratum_1463225_svc_handler();
  543. do_el0_svc(regs);
  544. exit_to_user_mode(regs);
  545. }
  546. static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr)
  547. {
  548. enter_from_user_mode(regs);
  549. local_daif_restore(DAIF_PROCCTX);
  550. do_el0_fpac(regs, esr);
  551. exit_to_user_mode(regs);
  552. }
  553. asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
  554. {
  555. unsigned long esr = read_sysreg(esr_el1);
  556. switch (ESR_ELx_EC(esr)) {
  557. case ESR_ELx_EC_SVC64:
  558. el0_svc(regs);
  559. break;
  560. case ESR_ELx_EC_DABT_LOW:
  561. el0_da(regs, esr);
  562. break;
  563. case ESR_ELx_EC_IABT_LOW:
  564. el0_ia(regs, esr);
  565. break;
  566. case ESR_ELx_EC_FP_ASIMD:
  567. el0_fpsimd_acc(regs, esr);
  568. break;
  569. case ESR_ELx_EC_SVE:
  570. el0_sve_acc(regs, esr);
  571. break;
  572. case ESR_ELx_EC_SME:
  573. el0_sme_acc(regs, esr);
  574. break;
  575. case ESR_ELx_EC_FP_EXC64:
  576. el0_fpsimd_exc(regs, esr);
  577. break;
  578. case ESR_ELx_EC_SYS64:
  579. case ESR_ELx_EC_WFx:
  580. el0_sys(regs, esr);
  581. break;
  582. case ESR_ELx_EC_SP_ALIGN:
  583. el0_sp(regs, esr);
  584. break;
  585. case ESR_ELx_EC_PC_ALIGN:
  586. el0_pc(regs, esr);
  587. break;
  588. case ESR_ELx_EC_UNKNOWN:
  589. el0_undef(regs, esr);
  590. break;
  591. case ESR_ELx_EC_BTI:
  592. el0_bti(regs);
  593. break;
  594. case ESR_ELx_EC_BREAKPT_LOW:
  595. case ESR_ELx_EC_SOFTSTP_LOW:
  596. case ESR_ELx_EC_WATCHPT_LOW:
  597. case ESR_ELx_EC_BRK64:
  598. el0_dbg(regs, esr);
  599. break;
  600. case ESR_ELx_EC_FPAC:
  601. el0_fpac(regs, esr);
  602. break;
  603. default:
  604. el0_inv(regs, esr);
  605. }
  606. }
  607. static void noinstr el0_interrupt(struct pt_regs *regs,
  608. void (*handler)(struct pt_regs *))
  609. {
  610. enter_from_user_mode(regs);
  611. write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
  612. if (regs->pc & BIT(55))
  613. arm64_apply_bp_hardening();
  614. irq_enter_rcu();
  615. do_interrupt_handler(regs, handler);
  616. irq_exit_rcu();
  617. exit_to_user_mode(regs);
  618. }
  619. static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
  620. {
  621. el0_interrupt(regs, handle_arch_irq);
  622. }
  623. asmlinkage void noinstr el0t_64_irq_handler(struct pt_regs *regs)
  624. {
  625. __el0_irq_handler_common(regs);
  626. }
  627. static void noinstr __el0_fiq_handler_common(struct pt_regs *regs)
  628. {
  629. el0_interrupt(regs, handle_arch_fiq);
  630. }
  631. asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs)
  632. {
  633. __el0_fiq_handler_common(regs);
  634. }
  635. static void noinstr __el0_error_handler_common(struct pt_regs *regs)
  636. {
  637. unsigned long esr = read_sysreg(esr_el1);
  638. enter_from_user_mode(regs);
  639. local_daif_restore(DAIF_ERRCTX);
  640. arm64_enter_nmi(regs);
  641. do_serror(regs, esr);
  642. arm64_exit_nmi(regs);
  643. local_daif_restore(DAIF_PROCCTX);
  644. exit_to_user_mode(regs);
  645. }
  646. asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)
  647. {
  648. __el0_error_handler_common(regs);
  649. }
  650. #ifdef CONFIG_COMPAT
  651. static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr)
  652. {
  653. enter_from_user_mode(regs);
  654. local_daif_restore(DAIF_PROCCTX);
  655. do_cp15instr(esr, regs);
  656. exit_to_user_mode(regs);
  657. }
  658. static void noinstr el0_svc_compat(struct pt_regs *regs)
  659. {
  660. enter_from_user_mode(regs);
  661. cortex_a76_erratum_1463225_svc_handler();
  662. do_el0_svc_compat(regs);
  663. exit_to_user_mode(regs);
  664. }
  665. asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
  666. {
  667. unsigned long esr = read_sysreg(esr_el1);
  668. switch (ESR_ELx_EC(esr)) {
  669. case ESR_ELx_EC_SVC32:
  670. el0_svc_compat(regs);
  671. break;
  672. case ESR_ELx_EC_DABT_LOW:
  673. el0_da(regs, esr);
  674. break;
  675. case ESR_ELx_EC_IABT_LOW:
  676. el0_ia(regs, esr);
  677. break;
  678. case ESR_ELx_EC_FP_ASIMD:
  679. el0_fpsimd_acc(regs, esr);
  680. break;
  681. case ESR_ELx_EC_FP_EXC32:
  682. el0_fpsimd_exc(regs, esr);
  683. break;
  684. case ESR_ELx_EC_PC_ALIGN:
  685. el0_pc(regs, esr);
  686. break;
  687. case ESR_ELx_EC_UNKNOWN:
  688. case ESR_ELx_EC_CP14_MR:
  689. case ESR_ELx_EC_CP14_LS:
  690. case ESR_ELx_EC_CP14_64:
  691. el0_undef(regs, esr);
  692. break;
  693. case ESR_ELx_EC_CP15_32:
  694. case ESR_ELx_EC_CP15_64:
  695. el0_cp15(regs, esr);
  696. break;
  697. case ESR_ELx_EC_BREAKPT_LOW:
  698. case ESR_ELx_EC_SOFTSTP_LOW:
  699. case ESR_ELx_EC_WATCHPT_LOW:
  700. case ESR_ELx_EC_BKPT32:
  701. el0_dbg(regs, esr);
  702. break;
  703. default:
  704. el0_inv(regs, esr);
  705. }
  706. }
  707. asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)
  708. {
  709. __el0_irq_handler_common(regs);
  710. }
  711. asmlinkage void noinstr el0t_32_fiq_handler(struct pt_regs *regs)
  712. {
  713. __el0_fiq_handler_common(regs);
  714. }
  715. asmlinkage void noinstr el0t_32_error_handler(struct pt_regs *regs)
  716. {
  717. __el0_error_handler_common(regs);
  718. }
  719. #else /* CONFIG_COMPAT */
  720. UNHANDLED(el0t, 32, sync)
  721. UNHANDLED(el0t, 32, irq)
  722. UNHANDLED(el0t, 32, fiq)
  723. UNHANDLED(el0t, 32, error)
  724. #endif /* CONFIG_COMPAT */
  725. #ifdef CONFIG_VMAP_STACK
  726. asmlinkage void noinstr handle_bad_stack(struct pt_regs *regs)
  727. {
  728. unsigned long esr = read_sysreg(esr_el1);
  729. unsigned long far = read_sysreg(far_el1);
  730. arm64_enter_nmi(regs);
  731. panic_bad_stack(regs, esr, far);
  732. }
  733. #endif /* CONFIG_VMAP_STACK */
  734. #ifdef CONFIG_ARM_SDE_INTERFACE
  735. asmlinkage noinstr unsigned long
  736. __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
  737. {
  738. unsigned long ret;
  739. /*
  740. * We didn't take an exception to get here, so the HW hasn't
  741. * set/cleared bits in PSTATE that we may rely on.
  742. *
  743. * The original SDEI spec (ARM DEN 0054A) can be read ambiguously as to
  744. * whether PSTATE bits are inherited unchanged or generated from
  745. * scratch, and the TF-A implementation always clears PAN and always
  746. * clears UAO. There are no other known implementations.
  747. *
  748. * Subsequent revisions (ARM DEN 0054B) follow the usual rules for how
  749. * PSTATE is modified upon architectural exceptions, and so PAN is
  750. * either inherited or set per SCTLR_ELx.SPAN, and UAO is always
  751. * cleared.
  752. *
  753. * We must explicitly reset PAN to the expected state, including
  754. * clearing it when the host isn't using it, in case a VM had it set.
  755. */
  756. if (system_uses_hw_pan())
  757. set_pstate_pan(1);
  758. else if (cpu_has_pan())
  759. set_pstate_pan(0);
  760. arm64_enter_nmi(regs);
  761. ret = do_sdei_event(regs, arg);
  762. arm64_exit_nmi(regs);
  763. return ret;
  764. }
  765. #endif /* CONFIG_ARM_SDE_INTERFACE */