nospec-branch.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <asm/facility.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 (IS_ENABLED(CC_USING_EXPOLINE) && !nospec_disable)
  36. pr_info("Spectre V2 mitigation: execute trampolines.\n");
  37. if (__test_facility(82, S390_lowcore.alt_stfle_fac_list))
  38. pr_info("Spectre V2 mitigation: limited branch prediction.\n");
  39. return 0;
  40. }
  41. arch_initcall(nospec_report);
  42. #ifdef CONFIG_EXPOLINE
  43. int nospec_disable = IS_ENABLED(CONFIG_EXPOLINE_OFF);
  44. static int __init nospectre_v2_setup_early(char *str)
  45. {
  46. nospec_disable = 1;
  47. return 0;
  48. }
  49. early_param("nospectre_v2", nospectre_v2_setup_early);
  50. void __init nospec_auto_detect(void)
  51. {
  52. if (IS_ENABLED(CC_USING_EXPOLINE)) {
  53. /*
  54. * The kernel has been compiled with expolines.
  55. * Keep expolines enabled and disable nobp.
  56. */
  57. nospec_disable = 0;
  58. __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
  59. }
  60. /*
  61. * If the kernel has not been compiled with expolines the
  62. * nobp setting decides what is done, this depends on the
  63. * CONFIG_KERNEL_NP option and the nobp/nospec parameters.
  64. */
  65. }
  66. static int __init spectre_v2_setup_early(char *str)
  67. {
  68. if (str && !strncmp(str, "on", 2)) {
  69. nospec_disable = 0;
  70. __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
  71. }
  72. if (str && !strncmp(str, "off", 3))
  73. nospec_disable = 1;
  74. if (str && !strncmp(str, "auto", 4))
  75. nospec_auto_detect();
  76. return 0;
  77. }
  78. early_param("spectre_v2", spectre_v2_setup_early);
  79. static void __init_or_module __nospec_revert(s32 *start, s32 *end)
  80. {
  81. enum { BRCL_EXPOLINE, BRASL_EXPOLINE } type;
  82. u8 *instr, *thunk, *br;
  83. u8 insnbuf[6];
  84. s32 *epo;
  85. /* Second part of the instruction replace is always a nop */
  86. for (epo = start; epo < end; epo++) {
  87. instr = (u8 *) epo + *epo;
  88. if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
  89. type = BRCL_EXPOLINE; /* brcl instruction */
  90. else if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x05)
  91. type = BRASL_EXPOLINE; /* brasl instruction */
  92. else
  93. continue;
  94. thunk = instr + (*(int *)(instr + 2)) * 2;
  95. if (thunk[0] == 0xc6 && thunk[1] == 0x00)
  96. /* exrl %r0,<target-br> */
  97. br = thunk + (*(int *)(thunk + 2)) * 2;
  98. else if (thunk[0] == 0xc0 && (thunk[1] & 0x0f) == 0x00 &&
  99. thunk[6] == 0x44 && thunk[7] == 0x00 &&
  100. (thunk[8] & 0x0f) == 0x00 && thunk[9] == 0x00 &&
  101. (thunk[1] & 0xf0) == (thunk[8] & 0xf0))
  102. /* larl %rx,<target br> + ex %r0,0(%rx) */
  103. br = thunk + (*(int *)(thunk + 2)) * 2;
  104. else
  105. continue;
  106. /* Check for unconditional branch 0x07f? or 0x47f???? */
  107. if ((br[0] & 0xbf) != 0x07 || (br[1] & 0xf0) != 0xf0)
  108. continue;
  109. memcpy(insnbuf + 2, (char[]) { 0x47, 0x00, 0x07, 0x00 }, 4);
  110. switch (type) {
  111. case BRCL_EXPOLINE:
  112. insnbuf[0] = br[0];
  113. insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
  114. if (br[0] == 0x47) {
  115. /* brcl to b, replace with bc + nopr */
  116. insnbuf[2] = br[2];
  117. insnbuf[3] = br[3];
  118. } else {
  119. /* brcl to br, replace with bcr + nop */
  120. }
  121. break;
  122. case BRASL_EXPOLINE:
  123. insnbuf[1] = (instr[1] & 0xf0) | (br[1] & 0x0f);
  124. if (br[0] == 0x47) {
  125. /* brasl to b, replace with bas + nopr */
  126. insnbuf[0] = 0x4d;
  127. insnbuf[2] = br[2];
  128. insnbuf[3] = br[3];
  129. } else {
  130. /* brasl to br, replace with basr + nop */
  131. insnbuf[0] = 0x0d;
  132. }
  133. break;
  134. }
  135. s390_kernel_write(instr, insnbuf, 6);
  136. }
  137. }
  138. void __init_or_module nospec_revert(s32 *start, s32 *end)
  139. {
  140. if (nospec_disable)
  141. __nospec_revert(start, end);
  142. }
  143. extern s32 __nospec_call_start[], __nospec_call_end[];
  144. extern s32 __nospec_return_start[], __nospec_return_end[];
  145. void __init nospec_init_branches(void)
  146. {
  147. nospec_revert(__nospec_call_start, __nospec_call_end);
  148. nospec_revert(__nospec_return_start, __nospec_return_end);
  149. }
  150. #endif /* CONFIG_EXPOLINE */