jump_label.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * jump label x86 support
  3. *
  4. * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
  5. *
  6. */
  7. #include <linux/jump_label.h>
  8. #include <linux/memory.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/jhash.h>
  13. #include <linux/cpu.h>
  14. #include <asm/kprobes.h>
  15. #include <asm/alternative.h>
  16. #ifdef HAVE_JUMP_LABEL
  17. union jump_code_union {
  18. char code[JUMP_LABEL_NOP_SIZE];
  19. struct {
  20. char jump;
  21. int offset;
  22. } __attribute__((packed));
  23. };
  24. static void __jump_label_transform(struct jump_entry *entry,
  25. enum jump_label_type type,
  26. void *(*poker)(void *, const void *, size_t))
  27. {
  28. union jump_code_union code;
  29. if (type == JUMP_LABEL_ENABLE) {
  30. code.jump = 0xe9;
  31. code.offset = entry->target -
  32. (entry->code + JUMP_LABEL_NOP_SIZE);
  33. } else
  34. memcpy(&code, ideal_nops[NOP_ATOMIC5], JUMP_LABEL_NOP_SIZE);
  35. (*poker)((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE);
  36. }
  37. void arch_jump_label_transform(struct jump_entry *entry,
  38. enum jump_label_type type)
  39. {
  40. get_online_cpus();
  41. mutex_lock(&text_mutex);
  42. __jump_label_transform(entry, type, text_poke_smp);
  43. mutex_unlock(&text_mutex);
  44. put_online_cpus();
  45. }
  46. static enum {
  47. JL_STATE_START,
  48. JL_STATE_NO_UPDATE,
  49. JL_STATE_UPDATE,
  50. } jlstate __initdata_or_module = JL_STATE_START;
  51. __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
  52. enum jump_label_type type)
  53. {
  54. /*
  55. * This function is called at boot up and when modules are
  56. * first loaded. Check if the default nop, the one that is
  57. * inserted at compile time, is the ideal nop. If it is, then
  58. * we do not need to update the nop, and we can leave it as is.
  59. * If it is not, then we need to update the nop to the ideal nop.
  60. */
  61. if (jlstate == JL_STATE_START) {
  62. const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
  63. const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
  64. if (memcmp(ideal_nop, default_nop, 5) != 0)
  65. jlstate = JL_STATE_UPDATE;
  66. else
  67. jlstate = JL_STATE_NO_UPDATE;
  68. }
  69. if (jlstate == JL_STATE_UPDATE)
  70. __jump_label_transform(entry, type, text_poke_early);
  71. }
  72. #endif