jump_label.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2010 Cavium Networks, Inc.
  7. */
  8. #include <linux/jump_label.h>
  9. #include <linux/kernel.h>
  10. #include <linux/memory.h>
  11. #include <linux/mutex.h>
  12. #include <linux/types.h>
  13. #include <linux/cpu.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/inst.h>
  16. /*
  17. * Define parameters for the standard MIPS and the microMIPS jump
  18. * instruction encoding respectively:
  19. *
  20. * - the ISA bit of the target, either 0 or 1 respectively,
  21. *
  22. * - the amount the jump target address is shifted right to fit in the
  23. * immediate field of the machine instruction, either 2 or 1,
  24. *
  25. * - the mask determining the size of the jump region relative to the
  26. * delay-slot instruction, either 256MB or 128MB,
  27. *
  28. * - the jump target alignment, either 4 or 2 bytes.
  29. */
  30. #define J_ISA_BIT IS_ENABLED(CONFIG_CPU_MICROMIPS)
  31. #define J_RANGE_SHIFT (2 - J_ISA_BIT)
  32. #define J_RANGE_MASK ((1ul << (26 + J_RANGE_SHIFT)) - 1)
  33. #define J_ALIGN_MASK ((1ul << J_RANGE_SHIFT) - 1)
  34. void arch_jump_label_transform(struct jump_entry *e,
  35. enum jump_label_type type)
  36. {
  37. union mips_instruction *insn_p;
  38. union mips_instruction insn;
  39. long offset;
  40. insn_p = (union mips_instruction *)msk_isa16_mode(e->code);
  41. /* Target must have the right alignment and ISA must be preserved. */
  42. BUG_ON((e->target & J_ALIGN_MASK) != J_ISA_BIT);
  43. if (type == JUMP_LABEL_JMP) {
  44. if (!IS_ENABLED(CONFIG_CPU_MICROMIPS) && MIPS_ISA_REV >= 6) {
  45. offset = e->target - ((unsigned long)insn_p + 4);
  46. offset >>= 2;
  47. /*
  48. * The branch offset must fit in the instruction's 26
  49. * bit field.
  50. */
  51. WARN_ON((offset >= (long)BIT(25)) ||
  52. (offset < -(long)BIT(25)));
  53. insn.j_format.opcode = bc6_op;
  54. insn.j_format.target = offset;
  55. } else {
  56. /*
  57. * Jump only works within an aligned region its delay
  58. * slot is in.
  59. */
  60. WARN_ON((e->target & ~J_RANGE_MASK) !=
  61. ((e->code + 4) & ~J_RANGE_MASK));
  62. insn.j_format.opcode = J_ISA_BIT ? mm_j32_op : j_op;
  63. insn.j_format.target = e->target >> J_RANGE_SHIFT;
  64. }
  65. } else {
  66. insn.word = 0; /* nop */
  67. }
  68. mutex_lock(&text_mutex);
  69. if (IS_ENABLED(CONFIG_CPU_MICROMIPS)) {
  70. insn_p->halfword[0] = insn.word >> 16;
  71. insn_p->halfword[1] = insn.word;
  72. } else
  73. *insn_p = insn;
  74. flush_icache_range((unsigned long)insn_p,
  75. (unsigned long)insn_p + sizeof(*insn_p));
  76. mutex_unlock(&text_mutex);
  77. }
  78. #ifdef CONFIG_MODULES
  79. void jump_label_apply_nops(struct module *mod)
  80. {
  81. struct jump_entry *iter_start = mod->jump_entries;
  82. struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
  83. struct jump_entry *iter;
  84. /* if the module doesn't have jump label entries, just return */
  85. if (iter_start == iter_stop)
  86. return;
  87. for (iter = iter_start; iter < iter_stop; iter++) {
  88. /* Only write NOPs for arch_branch_static(). */
  89. if (jump_label_init_type(iter) == JUMP_LABEL_NOP)
  90. arch_jump_label_transform(iter, JUMP_LABEL_NOP);
  91. }
  92. }
  93. #endif