fpsimd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * FP/SIMD context switching and fault handling
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/cpu.h>
  20. #include <linux/cpu_pm.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/signal.h>
  25. #include <linux/hardirq.h>
  26. #include <asm/fpsimd.h>
  27. #include <asm/cputype.h>
  28. #define FPEXC_IOF (1 << 0)
  29. #define FPEXC_DZF (1 << 1)
  30. #define FPEXC_OFF (1 << 2)
  31. #define FPEXC_UFF (1 << 3)
  32. #define FPEXC_IXF (1 << 4)
  33. #define FPEXC_IDF (1 << 7)
  34. /*
  35. * In order to reduce the number of times the FPSIMD state is needlessly saved
  36. * and restored, we need to keep track of two things:
  37. * (a) for each task, we need to remember which CPU was the last one to have
  38. * the task's FPSIMD state loaded into its FPSIMD registers;
  39. * (b) for each CPU, we need to remember which task's userland FPSIMD state has
  40. * been loaded into its FPSIMD registers most recently, or whether it has
  41. * been used to perform kernel mode NEON in the meantime.
  42. *
  43. * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
  44. * the id of the current CPU every time the state is loaded onto a CPU. For (b),
  45. * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
  46. * address of the userland FPSIMD state of the task that was loaded onto the CPU
  47. * the most recently, or NULL if kernel mode NEON has been performed after that.
  48. *
  49. * With this in place, we no longer have to restore the next FPSIMD state right
  50. * when switching between tasks. Instead, we can defer this check to userland
  51. * resume, at which time we verify whether the CPU's fpsimd_last_state and the
  52. * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
  53. * can omit the FPSIMD restore.
  54. *
  55. * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
  56. * indicate whether or not the userland FPSIMD state of the current task is
  57. * present in the registers. The flag is set unless the FPSIMD registers of this
  58. * CPU currently contain the most recent userland FPSIMD state of the current
  59. * task.
  60. *
  61. * For a certain task, the sequence may look something like this:
  62. * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
  63. * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
  64. * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
  65. * cleared, otherwise it is set;
  66. *
  67. * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
  68. * userland FPSIMD state is copied from memory to the registers, the task's
  69. * fpsimd_state.cpu field is set to the id of the current CPU, the current
  70. * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
  71. * TIF_FOREIGN_FPSTATE flag is cleared;
  72. *
  73. * - the task executes an ordinary syscall; upon return to userland, the
  74. * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
  75. * restored;
  76. *
  77. * - the task executes a syscall which executes some NEON instructions; this is
  78. * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
  79. * register contents to memory, clears the fpsimd_last_state per-cpu variable
  80. * and sets the TIF_FOREIGN_FPSTATE flag;
  81. *
  82. * - the task gets preempted after kernel_neon_end() is called; as we have not
  83. * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
  84. * whatever is in the FPSIMD registers is not saved to memory, but discarded.
  85. */
  86. static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
  87. /*
  88. * Trapped FP/ASIMD access.
  89. */
  90. void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
  91. {
  92. /* TODO: implement lazy context saving/restoring */
  93. WARN_ON(1);
  94. }
  95. /*
  96. * Raise a SIGFPE for the current process.
  97. */
  98. void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
  99. {
  100. siginfo_t info;
  101. unsigned int si_code = 0;
  102. if (esr & FPEXC_IOF)
  103. si_code = FPE_FLTINV;
  104. else if (esr & FPEXC_DZF)
  105. si_code = FPE_FLTDIV;
  106. else if (esr & FPEXC_OFF)
  107. si_code = FPE_FLTOVF;
  108. else if (esr & FPEXC_UFF)
  109. si_code = FPE_FLTUND;
  110. else if (esr & FPEXC_IXF)
  111. si_code = FPE_FLTRES;
  112. memset(&info, 0, sizeof(info));
  113. info.si_signo = SIGFPE;
  114. info.si_code = si_code;
  115. info.si_addr = (void __user *)instruction_pointer(regs);
  116. send_sig_info(SIGFPE, &info, current);
  117. }
  118. void fpsimd_thread_switch(struct task_struct *next)
  119. {
  120. if (!system_supports_fpsimd())
  121. return;
  122. /*
  123. * Save the current FPSIMD state to memory, but only if whatever is in
  124. * the registers is in fact the most recent userland FPSIMD state of
  125. * 'current'.
  126. */
  127. if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
  128. fpsimd_save_state(&current->thread.fpsimd_state);
  129. if (next->mm) {
  130. /*
  131. * If we are switching to a task whose most recent userland
  132. * FPSIMD state is already in the registers of *this* cpu,
  133. * we can skip loading the state from memory. Otherwise, set
  134. * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
  135. * upon the next return to userland.
  136. */
  137. struct fpsimd_state *st = &next->thread.fpsimd_state;
  138. if (__this_cpu_read(fpsimd_last_state) == st
  139. && st->cpu == smp_processor_id())
  140. clear_ti_thread_flag(task_thread_info(next),
  141. TIF_FOREIGN_FPSTATE);
  142. else
  143. set_ti_thread_flag(task_thread_info(next),
  144. TIF_FOREIGN_FPSTATE);
  145. }
  146. }
  147. void fpsimd_flush_thread(void)
  148. {
  149. if (!system_supports_fpsimd())
  150. return;
  151. memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
  152. fpsimd_flush_task_state(current);
  153. set_thread_flag(TIF_FOREIGN_FPSTATE);
  154. }
  155. /*
  156. * Save the userland FPSIMD state of 'current' to memory, but only if the state
  157. * currently held in the registers does in fact belong to 'current'
  158. */
  159. void fpsimd_preserve_current_state(void)
  160. {
  161. if (!system_supports_fpsimd())
  162. return;
  163. preempt_disable();
  164. if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
  165. fpsimd_save_state(&current->thread.fpsimd_state);
  166. preempt_enable();
  167. }
  168. /*
  169. * Load the userland FPSIMD state of 'current' from memory, but only if the
  170. * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
  171. * state of 'current'
  172. */
  173. void fpsimd_restore_current_state(void)
  174. {
  175. if (!system_supports_fpsimd())
  176. return;
  177. preempt_disable();
  178. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  179. struct fpsimd_state *st = &current->thread.fpsimd_state;
  180. fpsimd_load_state(st);
  181. this_cpu_write(fpsimd_last_state, st);
  182. st->cpu = smp_processor_id();
  183. }
  184. preempt_enable();
  185. }
  186. /*
  187. * Load an updated userland FPSIMD state for 'current' from memory and set the
  188. * flag that indicates that the FPSIMD register contents are the most recent
  189. * FPSIMD state of 'current'
  190. */
  191. void fpsimd_update_current_state(struct fpsimd_state *state)
  192. {
  193. if (!system_supports_fpsimd())
  194. return;
  195. preempt_disable();
  196. fpsimd_load_state(state);
  197. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  198. struct fpsimd_state *st = &current->thread.fpsimd_state;
  199. this_cpu_write(fpsimd_last_state, st);
  200. st->cpu = smp_processor_id();
  201. }
  202. preempt_enable();
  203. }
  204. /*
  205. * Invalidate live CPU copies of task t's FPSIMD state
  206. */
  207. void fpsimd_flush_task_state(struct task_struct *t)
  208. {
  209. t->thread.fpsimd_state.cpu = NR_CPUS;
  210. }
  211. #ifdef CONFIG_KERNEL_MODE_NEON
  212. static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
  213. static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
  214. /*
  215. * Kernel-side NEON support functions
  216. */
  217. void kernel_neon_begin_partial(u32 num_regs)
  218. {
  219. if (WARN_ON(!system_supports_fpsimd()))
  220. return;
  221. if (in_interrupt()) {
  222. struct fpsimd_partial_state *s = this_cpu_ptr(
  223. in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
  224. BUG_ON(num_regs > 32);
  225. fpsimd_save_partial_state(s, roundup(num_regs, 2));
  226. } else {
  227. /*
  228. * Save the userland FPSIMD state if we have one and if we
  229. * haven't done so already. Clear fpsimd_last_state to indicate
  230. * that there is no longer userland FPSIMD state in the
  231. * registers.
  232. */
  233. preempt_disable();
  234. if (current->mm &&
  235. !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
  236. fpsimd_save_state(&current->thread.fpsimd_state);
  237. this_cpu_write(fpsimd_last_state, NULL);
  238. }
  239. }
  240. EXPORT_SYMBOL(kernel_neon_begin_partial);
  241. void kernel_neon_end(void)
  242. {
  243. if (!system_supports_fpsimd())
  244. return;
  245. if (in_interrupt()) {
  246. struct fpsimd_partial_state *s = this_cpu_ptr(
  247. in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
  248. fpsimd_load_partial_state(s);
  249. } else {
  250. preempt_enable();
  251. }
  252. }
  253. EXPORT_SYMBOL(kernel_neon_end);
  254. #endif /* CONFIG_KERNEL_MODE_NEON */
  255. #ifdef CONFIG_CPU_PM
  256. static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
  257. unsigned long cmd, void *v)
  258. {
  259. switch (cmd) {
  260. case CPU_PM_ENTER:
  261. if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
  262. fpsimd_save_state(&current->thread.fpsimd_state);
  263. this_cpu_write(fpsimd_last_state, NULL);
  264. break;
  265. case CPU_PM_EXIT:
  266. if (current->mm)
  267. set_thread_flag(TIF_FOREIGN_FPSTATE);
  268. break;
  269. case CPU_PM_ENTER_FAILED:
  270. default:
  271. return NOTIFY_DONE;
  272. }
  273. return NOTIFY_OK;
  274. }
  275. static struct notifier_block fpsimd_cpu_pm_notifier_block = {
  276. .notifier_call = fpsimd_cpu_pm_notifier,
  277. };
  278. static void __init fpsimd_pm_init(void)
  279. {
  280. cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
  281. }
  282. #else
  283. static inline void fpsimd_pm_init(void) { }
  284. #endif /* CONFIG_CPU_PM */
  285. #ifdef CONFIG_HOTPLUG_CPU
  286. static int fpsimd_cpu_dead(unsigned int cpu)
  287. {
  288. per_cpu(fpsimd_last_state, cpu) = NULL;
  289. return 0;
  290. }
  291. static inline void fpsimd_hotplug_init(void)
  292. {
  293. cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
  294. NULL, fpsimd_cpu_dead);
  295. }
  296. #else
  297. static inline void fpsimd_hotplug_init(void) { }
  298. #endif
  299. /*
  300. * FP/SIMD support code initialisation.
  301. */
  302. static int __init fpsimd_init(void)
  303. {
  304. if (elf_hwcap & HWCAP_FP) {
  305. fpsimd_pm_init();
  306. fpsimd_hotplug_init();
  307. } else {
  308. pr_notice("Floating-point is not implemented\n");
  309. }
  310. if (!(elf_hwcap & HWCAP_ASIMD))
  311. pr_notice("Advanced SIMD is not implemented\n");
  312. return 0;
  313. }
  314. late_initcall(fpsimd_init);