alternative.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/cpu.h>
  4. #include <linux/smp.h>
  5. #include <asm/text-patching.h>
  6. #include <asm/alternative.h>
  7. #include <asm/facility.h>
  8. #include <asm/nospec-branch.h>
  9. static int __initdata_or_module alt_instr_disabled;
  10. static int __init disable_alternative_instructions(char *str)
  11. {
  12. alt_instr_disabled = 1;
  13. return 0;
  14. }
  15. early_param("noaltinstr", disable_alternative_instructions);
  16. static void __init_or_module __apply_alternatives(struct alt_instr *start,
  17. struct alt_instr *end)
  18. {
  19. struct alt_instr *a;
  20. u8 *instr, *replacement;
  21. /*
  22. * The scan order should be from start to end. A later scanned
  23. * alternative code can overwrite previously scanned alternative code.
  24. */
  25. for (a = start; a < end; a++) {
  26. instr = (u8 *)&a->instr_offset + a->instr_offset;
  27. replacement = (u8 *)&a->repl_offset + a->repl_offset;
  28. if (!__test_facility(a->facility, alt_stfle_fac_list))
  29. continue;
  30. if (unlikely(a->instrlen % 2)) {
  31. WARN_ONCE(1, "cpu alternatives instructions length is "
  32. "odd, skipping patching\n");
  33. continue;
  34. }
  35. s390_kernel_write(instr, replacement, a->instrlen);
  36. }
  37. }
  38. void __init_or_module apply_alternatives(struct alt_instr *start,
  39. struct alt_instr *end)
  40. {
  41. if (!alt_instr_disabled)
  42. __apply_alternatives(start, end);
  43. }
  44. extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
  45. void __init apply_alternative_instructions(void)
  46. {
  47. apply_alternatives(__alt_instructions, __alt_instructions_end);
  48. }
  49. static void do_sync_core(void *info)
  50. {
  51. sync_core();
  52. }
  53. void text_poke_sync(void)
  54. {
  55. on_each_cpu(do_sync_core, NULL, 1);
  56. }
  57. void text_poke_sync_lock(void)
  58. {
  59. cpus_read_lock();
  60. text_poke_sync();
  61. cpus_read_unlock();
  62. }