jump_label.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Jump label s390 support
  4. *
  5. * Copyright IBM Corp. 2011
  6. * Author(s): Jan Glauber <[email protected]>
  7. */
  8. #include <linux/uaccess.h>
  9. #include <linux/jump_label.h>
  10. #include <linux/module.h>
  11. #include <asm/text-patching.h>
  12. #include <asm/ipl.h>
  13. struct insn {
  14. u16 opcode;
  15. s32 offset;
  16. } __packed;
  17. static void jump_label_make_nop(struct jump_entry *entry, struct insn *insn)
  18. {
  19. /* brcl 0,offset */
  20. insn->opcode = 0xc004;
  21. insn->offset = (jump_entry_target(entry) - jump_entry_code(entry)) >> 1;
  22. }
  23. static void jump_label_make_branch(struct jump_entry *entry, struct insn *insn)
  24. {
  25. /* brcl 15,offset */
  26. insn->opcode = 0xc0f4;
  27. insn->offset = (jump_entry_target(entry) - jump_entry_code(entry)) >> 1;
  28. }
  29. static void jump_label_bug(struct jump_entry *entry, struct insn *expected,
  30. struct insn *new)
  31. {
  32. unsigned char *ipc = (unsigned char *)jump_entry_code(entry);
  33. unsigned char *ipe = (unsigned char *)expected;
  34. unsigned char *ipn = (unsigned char *)new;
  35. pr_emerg("Jump label code mismatch at %pS [%px]\n", ipc, ipc);
  36. pr_emerg("Found: %6ph\n", ipc);
  37. pr_emerg("Expected: %6ph\n", ipe);
  38. pr_emerg("New: %6ph\n", ipn);
  39. panic("Corrupted kernel text");
  40. }
  41. static void jump_label_transform(struct jump_entry *entry,
  42. enum jump_label_type type)
  43. {
  44. void *code = (void *)jump_entry_code(entry);
  45. struct insn old, new;
  46. if (type == JUMP_LABEL_JMP) {
  47. jump_label_make_nop(entry, &old);
  48. jump_label_make_branch(entry, &new);
  49. } else {
  50. jump_label_make_branch(entry, &old);
  51. jump_label_make_nop(entry, &new);
  52. }
  53. if (memcmp(code, &old, sizeof(old)))
  54. jump_label_bug(entry, &old, &new);
  55. s390_kernel_write(code, &new, sizeof(new));
  56. }
  57. void arch_jump_label_transform(struct jump_entry *entry,
  58. enum jump_label_type type)
  59. {
  60. jump_label_transform(entry, type);
  61. text_poke_sync();
  62. }
  63. bool arch_jump_label_transform_queue(struct jump_entry *entry,
  64. enum jump_label_type type)
  65. {
  66. jump_label_transform(entry, type);
  67. return true;
  68. }
  69. void arch_jump_label_transform_apply(void)
  70. {
  71. text_poke_sync();
  72. }