alternative.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <asm/alternative.h>
  4. #include <asm/facility.h>
  5. #include <asm/nospec-branch.h>
  6. #define MAX_PATCH_LEN (255 - 1)
  7. static int __initdata_or_module alt_instr_disabled;
  8. static int __init disable_alternative_instructions(char *str)
  9. {
  10. alt_instr_disabled = 1;
  11. return 0;
  12. }
  13. early_param("noaltinstr", disable_alternative_instructions);
  14. struct brcl_insn {
  15. u16 opc;
  16. s32 disp;
  17. } __packed;
  18. static u16 __initdata_or_module nop16 = 0x0700;
  19. static u32 __initdata_or_module nop32 = 0x47000000;
  20. static struct brcl_insn __initdata_or_module nop48 = {
  21. 0xc004, 0
  22. };
  23. static const void *nops[] __initdata_or_module = {
  24. &nop16,
  25. &nop32,
  26. &nop48
  27. };
  28. static void __init_or_module add_jump_padding(void *insns, unsigned int len)
  29. {
  30. struct brcl_insn brcl = {
  31. 0xc0f4,
  32. len / 2
  33. };
  34. memcpy(insns, &brcl, sizeof(brcl));
  35. insns += sizeof(brcl);
  36. len -= sizeof(brcl);
  37. while (len > 0) {
  38. memcpy(insns, &nop16, 2);
  39. insns += 2;
  40. len -= 2;
  41. }
  42. }
  43. static void __init_or_module add_padding(void *insns, unsigned int len)
  44. {
  45. if (len > 6)
  46. add_jump_padding(insns, len);
  47. else if (len >= 2)
  48. memcpy(insns, nops[len / 2 - 1], len);
  49. }
  50. static void __init_or_module __apply_alternatives(struct alt_instr *start,
  51. struct alt_instr *end)
  52. {
  53. struct alt_instr *a;
  54. u8 *instr, *replacement;
  55. u8 insnbuf[MAX_PATCH_LEN];
  56. /*
  57. * The scan order should be from start to end. A later scanned
  58. * alternative code can overwrite previously scanned alternative code.
  59. */
  60. for (a = start; a < end; a++) {
  61. int insnbuf_sz = 0;
  62. instr = (u8 *)&a->instr_offset + a->instr_offset;
  63. replacement = (u8 *)&a->repl_offset + a->repl_offset;
  64. if (!__test_facility(a->facility,
  65. S390_lowcore.alt_stfle_fac_list))
  66. continue;
  67. if (unlikely(a->instrlen % 2 || a->replacementlen % 2)) {
  68. WARN_ONCE(1, "cpu alternatives instructions length is "
  69. "odd, skipping patching\n");
  70. continue;
  71. }
  72. memcpy(insnbuf, replacement, a->replacementlen);
  73. insnbuf_sz = a->replacementlen;
  74. if (a->instrlen > a->replacementlen) {
  75. add_padding(insnbuf + a->replacementlen,
  76. a->instrlen - a->replacementlen);
  77. insnbuf_sz += a->instrlen - a->replacementlen;
  78. }
  79. s390_kernel_write(instr, insnbuf, insnbuf_sz);
  80. }
  81. }
  82. void __init_or_module apply_alternatives(struct alt_instr *start,
  83. struct alt_instr *end)
  84. {
  85. if (!alt_instr_disabled)
  86. __apply_alternatives(start, end);
  87. }
  88. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  89. void __init apply_alternative_instructions(void)
  90. {
  91. apply_alternatives(__alt_instructions, __alt_instructions_end);
  92. }