nospec-branch.c 4.6 KB

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