nospec-branch.c 3.8 KB

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