ssbd.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 ARM Ltd, All Rights Reserved.
  4. */
  5. #include <linux/compat.h>
  6. #include <linux/errno.h>
  7. #include <linux/prctl.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/thread_info.h>
  11. #include <asm/cpufeature.h>
  12. static void ssbd_ssbs_enable(struct task_struct *task)
  13. {
  14. u64 val = is_compat_thread(task_thread_info(task)) ?
  15. PSR_AA32_SSBS_BIT : PSR_SSBS_BIT;
  16. task_pt_regs(task)->pstate |= val;
  17. }
  18. static void ssbd_ssbs_disable(struct task_struct *task)
  19. {
  20. u64 val = is_compat_thread(task_thread_info(task)) ?
  21. PSR_AA32_SSBS_BIT : PSR_SSBS_BIT;
  22. task_pt_regs(task)->pstate &= ~val;
  23. }
  24. /*
  25. * prctl interface for SSBD
  26. * FIXME: Drop the below ifdefery once merged in 4.18.
  27. */
  28. #ifdef PR_SPEC_STORE_BYPASS
  29. static int ssbd_prctl_set(struct task_struct *task, unsigned long ctrl)
  30. {
  31. int state = arm64_get_ssbd_state();
  32. /* Unsupported */
  33. if (state == ARM64_SSBD_UNKNOWN)
  34. return -EINVAL;
  35. /* Treat the unaffected/mitigated state separately */
  36. if (state == ARM64_SSBD_MITIGATED) {
  37. switch (ctrl) {
  38. case PR_SPEC_ENABLE:
  39. return -EPERM;
  40. case PR_SPEC_DISABLE:
  41. case PR_SPEC_FORCE_DISABLE:
  42. return 0;
  43. }
  44. }
  45. /*
  46. * Things are a bit backward here: the arm64 internal API
  47. * *enables the mitigation* when the userspace API *disables
  48. * speculation*. So much fun.
  49. */
  50. switch (ctrl) {
  51. case PR_SPEC_ENABLE:
  52. /* If speculation is force disabled, enable is not allowed */
  53. if (state == ARM64_SSBD_FORCE_ENABLE ||
  54. task_spec_ssb_force_disable(task))
  55. return -EPERM;
  56. task_clear_spec_ssb_disable(task);
  57. clear_tsk_thread_flag(task, TIF_SSBD);
  58. ssbd_ssbs_enable(task);
  59. break;
  60. case PR_SPEC_DISABLE:
  61. if (state == ARM64_SSBD_FORCE_DISABLE)
  62. return -EPERM;
  63. task_set_spec_ssb_disable(task);
  64. set_tsk_thread_flag(task, TIF_SSBD);
  65. ssbd_ssbs_disable(task);
  66. break;
  67. case PR_SPEC_FORCE_DISABLE:
  68. if (state == ARM64_SSBD_FORCE_DISABLE)
  69. return -EPERM;
  70. task_set_spec_ssb_disable(task);
  71. task_set_spec_ssb_force_disable(task);
  72. set_tsk_thread_flag(task, TIF_SSBD);
  73. ssbd_ssbs_disable(task);
  74. break;
  75. default:
  76. return -ERANGE;
  77. }
  78. return 0;
  79. }
  80. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  81. unsigned long ctrl)
  82. {
  83. switch (which) {
  84. case PR_SPEC_STORE_BYPASS:
  85. return ssbd_prctl_set(task, ctrl);
  86. default:
  87. return -ENODEV;
  88. }
  89. }
  90. static int ssbd_prctl_get(struct task_struct *task)
  91. {
  92. switch (arm64_get_ssbd_state()) {
  93. case ARM64_SSBD_UNKNOWN:
  94. return -EINVAL;
  95. case ARM64_SSBD_FORCE_ENABLE:
  96. return PR_SPEC_DISABLE;
  97. case ARM64_SSBD_KERNEL:
  98. if (task_spec_ssb_force_disable(task))
  99. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  100. if (task_spec_ssb_disable(task))
  101. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  102. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  103. case ARM64_SSBD_FORCE_DISABLE:
  104. return PR_SPEC_ENABLE;
  105. default:
  106. return PR_SPEC_NOT_AFFECTED;
  107. }
  108. }
  109. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
  110. {
  111. switch (which) {
  112. case PR_SPEC_STORE_BYPASS:
  113. return ssbd_prctl_get(task);
  114. default:
  115. return -ENODEV;
  116. }
  117. }
  118. #endif /* PR_SPEC_STORE_BYPASS */