nospec-branch.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/cpu.h>
  5. #include <asm/nospec-branch.h>
  6. static int __init nobp_setup_early(char *str)
  7. {
  8. bool enabled;
  9. int rc;
  10. rc = kstrtobool(str, &enabled);
  11. if (rc)
  12. return rc;
  13. if (enabled && test_facility(82)) {
  14. /*
  15. * The user explicitely requested nobp=1, enable it and
  16. * disable the expoline support.
  17. */
  18. __set_facility(82, alt_stfle_fac_list);
  19. if (IS_ENABLED(CONFIG_EXPOLINE))
  20. nospec_disable = 1;
  21. } else {
  22. __clear_facility(82, alt_stfle_fac_list);
  23. }
  24. return 0;
  25. }
  26. early_param("nobp", nobp_setup_early);
  27. static int __init nospec_setup_early(char *str)
  28. {
  29. __clear_facility(82, alt_stfle_fac_list);
  30. return 0;
  31. }
  32. early_param("nospec", nospec_setup_early);
  33. static int __init nospec_report(void)
  34. {
  35. if (test_facility(156))
  36. pr_info("Spectre V2 mitigation: etokens\n");
  37. if (nospec_uses_trampoline())
  38. pr_info("Spectre V2 mitigation: execute trampolines\n");
  39. if (__test_facility(82, alt_stfle_fac_list))
  40. pr_info("Spectre V2 mitigation: limited branch prediction\n");
  41. return 0;
  42. }
  43. arch_initcall(nospec_report);
  44. #ifdef CONFIG_EXPOLINE
  45. int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
  46. static int __init nospectre_v2_setup_early(char *str)
  47. {
  48. nospec_disable = 1;
  49. return 0;
  50. }
  51. early_param("nospectre_v2", nospectre_v2_setup_early);
  52. void __init nospec_auto_detect(void)
  53. {
  54. if (test_facility(156) || cpu_mitigations_off()) {
  55. /*
  56. * The machine supports etokens.
  57. * Disable expolines and disable nobp.
  58. */
  59. if (__is_defined(CC_USING_EXPOLINE))
  60. nospec_disable = 1;
  61. __clear_facility(82, alt_stfle_fac_list);
  62. } else if (__is_defined(CC_USING_EXPOLINE)) {
  63. /*
  64. * The kernel has been compiled with expolines.
  65. * Keep expolines enabled and disable nobp.
  66. */
  67. nospec_disable = 0;
  68. __clear_facility(82, alt_stfle_fac_list);
  69. }
  70. /*
  71. * If the kernel has not been compiled with expolines the
  72. * nobp setting decides what is done, this depends on the
  73. * CONFIG_KERNEL_NP option and the nobp/nospec parameters.
  74. */
  75. }
  76. static int __init spectre_v2_setup_early(char *str)
  77. {
  78. if (str && !strncmp(str, "on", 2)) {
  79. nospec_disable = 0;
  80. __clear_facility(82, alt_stfle_fac_list);
  81. }
  82. if (str && !strncmp(str, "off", 3))
  83. nospec_disable = 1;
  84. if (str && !strncmp(str, "auto", 4))
  85. nospec_auto_detect();
  86. return 0;
  87. }
  88. early_param("spectre_v2", spectre_v2_setup_early);
  89. static void __init_or_module __nospec_revert(s32 *start, s32 *end)
  90. {
  91. enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
  92. static const u8 branch[] = { 0x47, 0x00, 0x07, 0x00 };
  93. u8 *instr, *thunk, *br;
  94. u8 insnbuf[6];
  95. s32 *epo;
  96. /* Second part of the instruction replace is always a nop */
  97. memcpy(insnbuf + 2, branch, sizeof(branch));
  98. for (epo = start; epo < end; epo++) {
  99. instr = (u8 *) epo + *epo;
  100. if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
  101. type = BRCL_EXPOLINE; /* brcl instruction */
  102. else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
  103. type = BRASL_EXPOLINE; /* brasl instruction */
  104. else
  105. continue;
  106. thunk = instr + (*(int *)(instr + 2)) * 2;
  107. if (thunk[0] == 0xc6 && thunk[1] == 0x00)
  108. /* exrl %r0,<target-br> */
  109. br = thunk + (*(int *)(thunk + 2)) * 2;
  110. else
  111. continue;
  112. if (br[0] != 0x07 || (br[1] & 0xf0) != 0xf0)
  113. continue;
  114. switch (type) {
  115. case BRCL_EXPOLINE:
  116. /* brcl to thunk, replace with br + nop */
  117. insnbuf[0] = br[0];
  118. insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
  119. break;
  120. case BRASL_EXPOLINE:
  121. /* brasl to thunk, replace with basr + nop */
  122. insnbuf[0] = 0x0d;
  123. insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
  124. break;
  125. }
  126. s390_kernel_write(instr, insnbuf, 6);
  127. }
  128. }
  129. void __init_or_module nospec_revert(s32 *start, s32 *end)
  130. {
  131. if (nospec_disable)
  132. __nospec_revert(start, end);
  133. }
  134. extern s32 __nospec_call_start[], __nospec_call_end[];
  135. extern s32 __nospec_return_start[], __nospec_return_end[];
  136. void __init nospec_init_branches(void)
  137. {
  138. nospec_revert(__nospec_call_start, __nospec_call_end);
  139. nospec_revert(__nospec_return_start, __nospec_return_end);
  140. }
  141. #endif /* CONFIG_EXPOLINE */