vmx-helper.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) IBM Corporation, 2011
  5. *
  6. * Authors: Sukadev Bhattiprolu <[email protected]>
  7. * Anton Blanchard <[email protected]>
  8. */
  9. #include <linux/uaccess.h>
  10. #include <linux/hardirq.h>
  11. #include <asm/switch_to.h>
  12. int enter_vmx_usercopy(void)
  13. {
  14. if (in_interrupt())
  15. return 0;
  16. preempt_disable();
  17. /*
  18. * We need to disable page faults as they can call schedule and
  19. * thus make us lose the VMX context. So on page faults, we just
  20. * fail which will cause a fallback to the normal non-vmx copy.
  21. */
  22. pagefault_disable();
  23. enable_kernel_altivec();
  24. return 1;
  25. }
  26. /*
  27. * This function must return 0 because we tail call optimise when calling
  28. * from __copy_tofrom_user_power7 which returns 0 on success.
  29. */
  30. int exit_vmx_usercopy(void)
  31. {
  32. disable_kernel_altivec();
  33. pagefault_enable();
  34. preempt_enable_no_resched();
  35. /*
  36. * Must never explicitly call schedule (including preempt_enable())
  37. * while in a kuap-unlocked user copy, because the AMR register will
  38. * not be saved and restored across context switch. However preempt
  39. * kernels need to be preempted as soon as possible if need_resched is
  40. * set and we are preemptible. The hack here is to schedule a
  41. * decrementer to fire here and reschedule for us if necessary.
  42. */
  43. if (IS_ENABLED(CONFIG_PREEMPT) && need_resched())
  44. set_dec(1);
  45. return 0;
  46. }
  47. int enter_vmx_ops(void)
  48. {
  49. if (in_interrupt())
  50. return 0;
  51. preempt_disable();
  52. enable_kernel_altivec();
  53. return 1;
  54. }
  55. /*
  56. * All calls to this function will be optimised into tail calls. We are
  57. * passed a pointer to the destination which we return as required by a
  58. * memcpy implementation.
  59. */
  60. void *exit_vmx_ops(void *dest)
  61. {
  62. disable_kernel_altivec();
  63. preempt_enable();
  64. return dest;
  65. }