nospec-branch.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, S390_lowcore.alt_stfle_fac_list);
  19. if (IS_ENABLED(CONFIG_EXPOLINE))
  20. nospec_disable = 1;
  21. } else {
  22. __clear_facility(82, S390_lowcore.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, S390_lowcore.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 (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable)
  38. pr_info("Spectre V2 mitigation: execute trampolines\n");
  39. if (__test_facility(82, S390_lowcore.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_ENABLED(CC_USING_EXPOLINE))
  60. nospec_disable = 1;
  61. __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
  62. } else if (IS_ENABLED(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, S390_lowcore.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, S390_lowcore.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. u8 *instr, *thunk, *br;
  93. u8 insnbuf[6];
  94. s32 *epo;
  95. /* Second part of the instruction replace is always a nop */
  96. for (epo = start; epo < end; epo++) {
  97. instr = (u8 *) epo + *epo;
  98. if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
  99. type = BRCL_EXPOLINE; /* brcl instruction */
  100. else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
  101. type = BRASL_EXPOLINE; /* brasl instruction */
  102. else
  103. continue;
  104. thunk = instr + (*(int *)(instr + 2)) * 2;
  105. if (thunk[0] == 0xc6 && thunk[1] == 0x00)
  106. /* exrl %r0,<target-br> */
  107. br = thunk + (*(int *)(thunk + 2)) * 2;
  108. else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 &&
  109. thunk[6] == 0x44 && thunk[7] == 0x00 &&
  110. (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 &&
  111. (thunk[1] & 0xf0) == (thunk[8] & 0xf0))
  112. /* larl %rx,<target br> + ex %r0,0(%rx) */
  113. br = thunk + (*(int *)(thunk + 2)) * 2;
  114. else
  115. continue;
  116. /* Check for unconditional branch 0x07f? or 0x47f???? */
  117. if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
  118. continue;
  119. memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x07, 0x00 }, 4);
  120. switch (type) {
  121. case BRCL_EXPOLINE:
  122. insnbuf[0] = br[0];
  123. insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
  124. if (br[0] == 0x47) {
  125. /* brcl to b, replace with bc + nopr */
  126. insnbuf[2] = br[2];
  127. insnbuf[3] = br[3];
  128. } else {
  129. /* brcl to br, replace with bcr + nop */
  130. }
  131. break;
  132. case BRASL_EXPOLINE:
  133. insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
  134. if (br[0] == 0x47) {
  135. /* brasl to b, replace with bas + nopr */
  136. insnbuf[0] = 0x4d;
  137. insnbuf[2] = br[2];
  138. insnbuf[3] = br[3];
  139. } else {
  140. /* brasl to br, replace with basr + nop */
  141. insnbuf[0] = 0x0d;
  142. }
  143. break;
  144. }
  145. s390_kernel_write(instr, insnbuf, 6);
  146. }
  147. }
  148. void __init_or_module nospec_revert(s32 *start, s32 *end)
  149. {
  150. if (nospec_disable)
  151. __nospec_revert(start, end);
  152. }
  153. extern s32 __nospec_call_start[], __nospec_call_end[];
  154. extern s32 __nospec_return_start[], __nospec_return_end[];
  155. void __init nospec_init_branches(void)
  156. {
  157. nospec_revert(__nospec_call_start, __nospec_call_end);
  158. nospec_revert(__nospec_return_start, __nospec_return_end);
  159. }
  160. #endif /* CONFIG_EXPOLINE */