uasm-micromips.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * A small micro-assembler. It is intentionally kept simple, does only
  7. * support a subset of instructions, and does not try to hide pipeline
  8. * effects like branch delay slots.
  9. *
  10. * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
  11. * Copyright (C) 2005, 2007 Maciej W. Rozycki
  12. * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
  13. * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <asm/inst.h>
  18. #include <asm/elf.h>
  19. #include <asm/bugs.h>
  20. #define UASM_ISA _UASM_ISA_MICROMIPS
  21. #include <asm/uasm.h>
  22. #define RS_MASK 0x1f
  23. #define RS_SH 16
  24. #define RT_MASK 0x1f
  25. #define RT_SH 21
  26. #define SCIMM_MASK 0x3ff
  27. #define SCIMM_SH 16
  28. /* This macro sets the non-variable bits of an instruction. */
  29. #define M(a, b, c, d, e, f) \
  30. ((a) << OP_SH \
  31. | (b) << RT_SH \
  32. | (c) << RS_SH \
  33. | (d) << RD_SH \
  34. | (e) << RE_SH \
  35. | (f) << FUNC_SH)
  36. /* Define these when we are not the ISA the kernel is being compiled with. */
  37. #ifndef CONFIG_CPU_MICROMIPS
  38. #define MM_uasm_i_b(buf, off) ISAOPC(_beq)(buf, 0, 0, off)
  39. #define MM_uasm_i_beqz(buf, rs, off) ISAOPC(_beq)(buf, rs, 0, off)
  40. #define MM_uasm_i_beqzl(buf, rs, off) ISAOPC(_beql)(buf, rs, 0, off)
  41. #define MM_uasm_i_bnez(buf, rs, off) ISAOPC(_bne)(buf, rs, 0, off)
  42. #endif
  43. #include "uasm.c"
  44. static struct insn insn_table_MM[] = {
  45. { insn_addu, M(mm_pool32a_op, 0, 0, 0, 0, mm_addu32_op), RT | RS | RD },
  46. { insn_addiu, M(mm_addiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM },
  47. { insn_and, M(mm_pool32a_op, 0, 0, 0, 0, mm_and_op), RT | RS | RD },
  48. { insn_andi, M(mm_andi32_op, 0, 0, 0, 0, 0), RT | RS | UIMM },
  49. { insn_beq, M(mm_beq32_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  50. { insn_beql, 0, 0 },
  51. { insn_bgez, M(mm_pool32i_op, mm_bgez_op, 0, 0, 0, 0), RS | BIMM },
  52. { insn_bgezl, 0, 0 },
  53. { insn_bltz, M(mm_pool32i_op, mm_bltz_op, 0, 0, 0, 0), RS | BIMM },
  54. { insn_bltzl, 0, 0 },
  55. { insn_bne, M(mm_bne32_op, 0, 0, 0, 0, 0), RT | RS | BIMM },
  56. { insn_cache, M(mm_pool32b_op, 0, 0, mm_cache_func, 0, 0), RT | RS | SIMM },
  57. { insn_daddu, 0, 0 },
  58. { insn_daddiu, 0, 0 },
  59. { insn_dmfc0, 0, 0 },
  60. { insn_dmtc0, 0, 0 },
  61. { insn_dsll, 0, 0 },
  62. { insn_dsll32, 0, 0 },
  63. { insn_dsra, 0, 0 },
  64. { insn_dsrl, 0, 0 },
  65. { insn_dsrl32, 0, 0 },
  66. { insn_drotr, 0, 0 },
  67. { insn_drotr32, 0, 0 },
  68. { insn_dsubu, 0, 0 },
  69. { insn_eret, M(mm_pool32a_op, 0, 0, 0, mm_eret_op, mm_pool32axf_op), 0 },
  70. { insn_ins, M(mm_pool32a_op, 0, 0, 0, 0, mm_ins_op), RT | RS | RD | RE },
  71. { insn_ext, M(mm_pool32a_op, 0, 0, 0, 0, mm_ext_op), RT | RS | RD | RE },
  72. { insn_j, M(mm_j32_op, 0, 0, 0, 0, 0), JIMM },
  73. { insn_jal, M(mm_jal32_op, 0, 0, 0, 0, 0), JIMM },
  74. { insn_jr, M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RS },
  75. { insn_ld, 0, 0 },
  76. { insn_ll, M(mm_pool32c_op, 0, 0, (mm_ll_func << 1), 0, 0), RS | RT | SIMM },
  77. { insn_lld, 0, 0 },
  78. { insn_lui, M(mm_pool32i_op, mm_lui_op, 0, 0, 0, 0), RS | SIMM },
  79. { insn_lw, M(mm_lw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM },
  80. { insn_mfc0, M(mm_pool32a_op, 0, 0, 0, mm_mfc0_op, mm_pool32axf_op), RT | RS | RD },
  81. { insn_mtc0, M(mm_pool32a_op, 0, 0, 0, mm_mtc0_op, mm_pool32axf_op), RT | RS | RD },
  82. { insn_or, M(mm_pool32a_op, 0, 0, 0, 0, mm_or32_op), RT | RS | RD },
  83. { insn_ori, M(mm_ori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM },
  84. { insn_pref, M(mm_pool32c_op, 0, 0, (mm_pref_func << 1), 0, 0), RT | RS | SIMM },
  85. { insn_rfe, 0, 0 },
  86. { insn_sc, M(mm_pool32c_op, 0, 0, (mm_sc_func << 1), 0, 0), RT | RS | SIMM },
  87. { insn_scd, 0, 0 },
  88. { insn_sd, 0, 0 },
  89. { insn_sll, M(mm_pool32a_op, 0, 0, 0, 0, mm_sll32_op), RT | RS | RD },
  90. { insn_sra, M(mm_pool32a_op, 0, 0, 0, 0, mm_sra_op), RT | RS | RD },
  91. { insn_srl, M(mm_pool32a_op, 0, 0, 0, 0, mm_srl32_op), RT | RS | RD },
  92. { insn_rotr, M(mm_pool32a_op, 0, 0, 0, 0, mm_rotr_op), RT | RS | RD },
  93. { insn_subu, M(mm_pool32a_op, 0, 0, 0, 0, mm_subu32_op), RT | RS | RD },
  94. { insn_sw, M(mm_sw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM },
  95. { insn_tlbp, M(mm_pool32a_op, 0, 0, 0, mm_tlbp_op, mm_pool32axf_op), 0 },
  96. { insn_tlbr, M(mm_pool32a_op, 0, 0, 0, mm_tlbr_op, mm_pool32axf_op), 0 },
  97. { insn_tlbwi, M(mm_pool32a_op, 0, 0, 0, mm_tlbwi_op, mm_pool32axf_op), 0 },
  98. { insn_tlbwr, M(mm_pool32a_op, 0, 0, 0, mm_tlbwr_op, mm_pool32axf_op), 0 },
  99. { insn_xor, M(mm_pool32a_op, 0, 0, 0, 0, mm_xor32_op), RT | RS | RD },
  100. { insn_xori, M(mm_xori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM },
  101. { insn_dins, 0, 0 },
  102. { insn_dinsm, 0, 0 },
  103. { insn_syscall, M(mm_pool32a_op, 0, 0, 0, mm_syscall_op, mm_pool32axf_op), SCIMM},
  104. { insn_bbit0, 0, 0 },
  105. { insn_bbit1, 0, 0 },
  106. { insn_lwx, 0, 0 },
  107. { insn_ldx, 0, 0 },
  108. { insn_invalid, 0, 0 }
  109. };
  110. #undef M
  111. static inline u32 build_bimm(s32 arg)
  112. {
  113. WARN(arg > 0xffff || arg < -0x10000,
  114. KERN_WARNING "Micro-assembler field overflow\n");
  115. WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n");
  116. return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 1) & 0x7fff);
  117. }
  118. static inline u32 build_jimm(u32 arg)
  119. {
  120. WARN(arg & ~((JIMM_MASK << 2) | 1),
  121. KERN_WARNING "Micro-assembler field overflow\n");
  122. return (arg >> 1) & JIMM_MASK;
  123. }
  124. /*
  125. * The order of opcode arguments is implicitly left to right,
  126. * starting with RS and ending with FUNC or IMM.
  127. */
  128. static void build_insn(u32 **buf, enum opcode opc, ...)
  129. {
  130. struct insn *ip = NULL;
  131. unsigned int i;
  132. va_list ap;
  133. u32 op;
  134. for (i = 0; insn_table_MM[i].opcode != insn_invalid; i++)
  135. if (insn_table_MM[i].opcode == opc) {
  136. ip = &insn_table_MM[i];
  137. break;
  138. }
  139. if (!ip || (opc == insn_daddiu && r4k_daddiu_bug()))
  140. panic("Unsupported Micro-assembler instruction %d", opc);
  141. op = ip->match;
  142. va_start(ap, opc);
  143. if (ip->fields & RS) {
  144. if (opc == insn_mfc0 || opc == insn_mtc0)
  145. op |= build_rt(va_arg(ap, u32));
  146. else
  147. op |= build_rs(va_arg(ap, u32));
  148. }
  149. if (ip->fields & RT) {
  150. if (opc == insn_mfc0 || opc == insn_mtc0)
  151. op |= build_rs(va_arg(ap, u32));
  152. else
  153. op |= build_rt(va_arg(ap, u32));
  154. }
  155. if (ip->fields & RD)
  156. op |= build_rd(va_arg(ap, u32));
  157. if (ip->fields & RE)
  158. op |= build_re(va_arg(ap, u32));
  159. if (ip->fields & SIMM)
  160. op |= build_simm(va_arg(ap, s32));
  161. if (ip->fields & UIMM)
  162. op |= build_uimm(va_arg(ap, u32));
  163. if (ip->fields & BIMM)
  164. op |= build_bimm(va_arg(ap, s32));
  165. if (ip->fields & JIMM)
  166. op |= build_jimm(va_arg(ap, u32));
  167. if (ip->fields & FUNC)
  168. op |= build_func(va_arg(ap, u32));
  169. if (ip->fields & SET)
  170. op |= build_set(va_arg(ap, u32));
  171. if (ip->fields & SCIMM)
  172. op |= build_scimm(va_arg(ap, u32));
  173. va_end(ap);
  174. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  175. **buf = ((op & 0xffff) << 16) | (op >> 16);
  176. #else
  177. **buf = op;
  178. #endif
  179. (*buf)++;
  180. }
  181. static inline void
  182. __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab)
  183. {
  184. long laddr = (long)lab->addr;
  185. long raddr = (long)rel->addr;
  186. switch (rel->type) {
  187. case R_MIPS_PC16:
  188. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  189. *rel->addr |= (build_bimm(laddr - (raddr + 4)) << 16);
  190. #else
  191. *rel->addr |= build_bimm(laddr - (raddr + 4));
  192. #endif
  193. break;
  194. default:
  195. panic("Unsupported Micro-assembler relocation %d",
  196. rel->type);
  197. }
  198. }