jump_label.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Jump label s390 support
  4. *
  5. * Copyright IBM Corp. 2011
  6. * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
  7. */
  8. #include <linux/uaccess.h>
  9. #include <linux/stop_machine.h>
  10. #include <linux/jump_label.h>
  11. #include <asm/ipl.h>
  12. struct insn {
  13. u16 opcode;
  14. s32 offset;
  15. } __packed;
  16. struct insn_args {
  17. struct jump_entry *entry;
  18. enum jump_label_type type;
  19. };
  20. static void jump_label_make_nop(struct jump_entry *entry, struct insn *insn)
  21. {
  22. /* brcl 0,0 */
  23. insn->opcode = 0xc004;
  24. insn->offset = 0;
  25. }
  26. static void jump_label_make_branch(struct jump_entry *entry, struct insn *insn)
  27. {
  28. /* brcl 15,offset */
  29. insn->opcode = 0xc0f4;
  30. insn->offset = (entry->target - entry->code) >> 1;
  31. }
  32. static void jump_label_bug(struct jump_entry *entry, struct insn *expected,
  33. struct insn *new)
  34. {
  35. unsigned char *ipc = (unsigned char *)entry->code;
  36. unsigned char *ipe = (unsigned char *)expected;
  37. unsigned char *ipn = (unsigned char *)new;
  38. pr_emerg("Jump label code mismatch at %pS [%p]\n", ipc, ipc);
  39. pr_emerg("Found: %6ph\n", ipc);
  40. pr_emerg("Expected: %6ph\n", ipe);
  41. pr_emerg("New: %6ph\n", ipn);
  42. panic("Corrupted kernel text");
  43. }
  44. static struct insn orignop = {
  45. .opcode = 0xc004,
  46. .offset = JUMP_LABEL_NOP_OFFSET >> 1,
  47. };
  48. static void __jump_label_transform(struct jump_entry *entry,
  49. enum jump_label_type type,
  50. int init)
  51. {
  52. struct insn old, new;
  53. if (type == JUMP_LABEL_JMP) {
  54. jump_label_make_nop(entry, &old);
  55. jump_label_make_branch(entry, &new);
  56. } else {
  57. jump_label_make_branch(entry, &old);
  58. jump_label_make_nop(entry, &new);
  59. }
  60. if (init) {
  61. if (memcmp((void *)entry->code, &orignop, sizeof(orignop)))
  62. jump_label_bug(entry, &orignop, &new);
  63. } else {
  64. if (memcmp((void *)entry->code, &old, sizeof(old)))
  65. jump_label_bug(entry, &old, &new);
  66. }
  67. s390_kernel_write((void *)entry->code, &new, sizeof(new));
  68. }
  69. static int __sm_arch_jump_label_transform(void *data)
  70. {
  71. struct insn_args *args = data;
  72. __jump_label_transform(args->entry, args->type, 0);
  73. return 0;
  74. }
  75. void arch_jump_label_transform(struct jump_entry *entry,
  76. enum jump_label_type type)
  77. {
  78. struct insn_args args;
  79. args.entry = entry;
  80. args.type = type;
  81. stop_machine_cpuslocked(__sm_arch_jump_label_transform, &args, NULL);
  82. }
  83. void arch_jump_label_transform_static(struct jump_entry *entry,
  84. enum jump_label_type type)
  85. {
  86. __jump_label_transform(entry, type, 1);
  87. }