sdei.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2017 Arm Ltd.
  3. #define pr_fmt(fmt) "sdei: " fmt
  4. #include <linux/arm_sdei.h>
  5. #include <linux/hardirq.h>
  6. #include <linux/irqflags.h>
  7. #include <linux/sched/task_stack.h>
  8. #include <linux/uaccess.h>
  9. #include <asm/alternative.h>
  10. #include <asm/kprobes.h>
  11. #include <asm/mmu.h>
  12. #include <asm/ptrace.h>
  13. #include <asm/sections.h>
  14. #include <asm/sysreg.h>
  15. #include <asm/vmap_stack.h>
  16. unsigned long sdei_exit_mode;
  17. /*
  18. * VMAP'd stacks checking for stack overflow on exception using sp as a scratch
  19. * register, meaning SDEI has to switch to its own stack. We need two stacks as
  20. * a critical event may interrupt a normal event that has just taken a
  21. * synchronous exception, and is using sp as scratch register. For a critical
  22. * event interrupting a normal event, we can't reliably tell if we were on the
  23. * sdei stack.
  24. * For now, we allocate stacks when the driver is probed.
  25. */
  26. DECLARE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
  27. DECLARE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
  28. #ifdef CONFIG_VMAP_STACK
  29. DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
  30. DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
  31. #endif
  32. static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
  33. {
  34. unsigned long *p;
  35. p = per_cpu(*ptr, cpu);
  36. if (p) {
  37. per_cpu(*ptr, cpu) = NULL;
  38. vfree(p);
  39. }
  40. }
  41. static void free_sdei_stacks(void)
  42. {
  43. int cpu;
  44. for_each_possible_cpu(cpu) {
  45. _free_sdei_stack(&sdei_stack_normal_ptr, cpu);
  46. _free_sdei_stack(&sdei_stack_critical_ptr, cpu);
  47. }
  48. }
  49. static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
  50. {
  51. unsigned long *p;
  52. p = arch_alloc_vmap_stack(SDEI_STACK_SIZE, cpu_to_node(cpu));
  53. if (!p)
  54. return -ENOMEM;
  55. per_cpu(*ptr, cpu) = p;
  56. return 0;
  57. }
  58. static int init_sdei_stacks(void)
  59. {
  60. int cpu;
  61. int err = 0;
  62. for_each_possible_cpu(cpu) {
  63. err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
  64. if (err)
  65. break;
  66. err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
  67. if (err)
  68. break;
  69. }
  70. if (err)
  71. free_sdei_stacks();
  72. return err;
  73. }
  74. bool _on_sdei_stack(unsigned long sp)
  75. {
  76. unsigned long low, high;
  77. if (!IS_ENABLED(CONFIG_VMAP_STACK))
  78. return false;
  79. low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
  80. high = low + SDEI_STACK_SIZE;
  81. if (low <= sp && sp < high)
  82. return true;
  83. low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
  84. high = low + SDEI_STACK_SIZE;
  85. return (low <= sp && sp < high);
  86. }
  87. unsigned long sdei_arch_get_entry_point(int conduit)
  88. {
  89. /*
  90. * SDEI works between adjacent exception levels. If we booted at EL1 we
  91. * assume a hypervisor is marshalling events. If we booted at EL2 and
  92. * dropped to EL1 because we don't support VHE, then we can't support
  93. * SDEI.
  94. */
  95. if (is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
  96. pr_err("Not supported on this hardware/boot configuration\n");
  97. return 0;
  98. }
  99. if (IS_ENABLED(CONFIG_VMAP_STACK)) {
  100. if (init_sdei_stacks())
  101. return 0;
  102. }
  103. sdei_exit_mode = (conduit == CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
  104. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  105. if (arm64_kernel_unmapped_at_el0()) {
  106. unsigned long offset;
  107. offset = (unsigned long)__sdei_asm_entry_trampoline -
  108. (unsigned long)__entry_tramp_text_start;
  109. return TRAMP_VALIAS + offset;
  110. } else
  111. #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
  112. return (unsigned long)__sdei_asm_handler;
  113. }
  114. /*
  115. * __sdei_handler() returns one of:
  116. * SDEI_EV_HANDLED - success, return to the interrupted context.
  117. * SDEI_EV_FAILED - failure, return this error code to firmare.
  118. * virtual-address - success, return to this address.
  119. */
  120. static __kprobes unsigned long _sdei_handler(struct pt_regs *regs,
  121. struct sdei_registered_event *arg)
  122. {
  123. u32 mode;
  124. int i, err = 0;
  125. int clobbered_registers = 4;
  126. u64 elr = read_sysreg(elr_el1);
  127. u32 kernel_mode = read_sysreg(CurrentEL) | 1; /* +SPSel */
  128. unsigned long vbar = read_sysreg(vbar_el1);
  129. if (arm64_kernel_unmapped_at_el0())
  130. clobbered_registers++;
  131. /* Retrieve the missing registers values */
  132. for (i = 0; i < clobbered_registers; i++) {
  133. /* from within the handler, this call always succeeds */
  134. sdei_api_event_context(i, &regs->regs[i]);
  135. }
  136. /*
  137. * We didn't take an exception to get here, set PAN. UAO will be cleared
  138. * by sdei_event_handler()s set_fs(USER_DS) call.
  139. */
  140. __uaccess_enable_hw_pan();
  141. err = sdei_event_handler(regs, arg);
  142. if (err)
  143. return SDEI_EV_FAILED;
  144. if (elr != read_sysreg(elr_el1)) {
  145. /*
  146. * We took a synchronous exception from the SDEI handler.
  147. * This could deadlock, and if you interrupt KVM it will
  148. * hyp-panic instead.
  149. */
  150. pr_warn("unsafe: exception during handler\n");
  151. }
  152. mode = regs->pstate & (PSR_MODE32_BIT | PSR_MODE_MASK);
  153. /*
  154. * If we interrupted the kernel with interrupts masked, we always go
  155. * back to wherever we came from.
  156. */
  157. if (mode == kernel_mode && !interrupts_enabled(regs))
  158. return SDEI_EV_HANDLED;
  159. /*
  160. * Otherwise, we pretend this was an IRQ. This lets user space tasks
  161. * receive signals before we return to them, and KVM to invoke it's
  162. * world switch to do the same.
  163. *
  164. * See DDI0487B.a Table D1-7 'Vector offsets from vector table base
  165. * address'.
  166. */
  167. if (mode == kernel_mode)
  168. return vbar + 0x280;
  169. else if (mode & PSR_MODE32_BIT)
  170. return vbar + 0x680;
  171. return vbar + 0x480;
  172. }
  173. asmlinkage __kprobes notrace unsigned long
  174. __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
  175. {
  176. unsigned long ret;
  177. bool do_nmi_exit = false;
  178. /*
  179. * nmi_enter() deals with printk() re-entrance and use of RCU when
  180. * RCU believed this CPU was idle. Because critical events can
  181. * interrupt normal events, we may already be in_nmi().
  182. */
  183. if (!in_nmi()) {
  184. nmi_enter();
  185. do_nmi_exit = true;
  186. }
  187. ret = _sdei_handler(regs, arg);
  188. if (do_nmi_exit)
  189. nmi_exit();
  190. return ret;
  191. }