uasm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. */
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/init.h>
  17. #include <asm/inst.h>
  18. #include <asm/elf.h>
  19. #include <asm/bugs.h>
  20. #include <asm/uasm.h>
  21. enum fields {
  22. RS = 0x001,
  23. RT = 0x002,
  24. RD = 0x004,
  25. RE = 0x008,
  26. SIMM = 0x010,
  27. UIMM = 0x020,
  28. BIMM = 0x040,
  29. JIMM = 0x080,
  30. FUNC = 0x100,
  31. SET = 0x200,
  32. SCIMM = 0x400
  33. };
  34. #define OP_MASK 0x3f
  35. #define OP_SH 26
  36. #define RS_MASK 0x1f
  37. #define RS_SH 21
  38. #define RT_MASK 0x1f
  39. #define RT_SH 16
  40. #define RD_MASK 0x1f
  41. #define RD_SH 11
  42. #define RE_MASK 0x1f
  43. #define RE_SH 6
  44. #define IMM_MASK 0xffff
  45. #define IMM_SH 0
  46. #define JIMM_MASK 0x3ffffff
  47. #define JIMM_SH 0
  48. #define FUNC_MASK 0x3f
  49. #define FUNC_SH 0
  50. #define SET_MASK 0x7
  51. #define SET_SH 0
  52. #define SCIMM_MASK 0xfffff
  53. #define SCIMM_SH 6
  54. enum opcode {
  55. insn_invalid,
  56. insn_addu, insn_addiu, insn_and, insn_andi, insn_beq,
  57. insn_beql, insn_bgez, insn_bgezl, insn_bltz, insn_bltzl,
  58. insn_bne, insn_cache, insn_daddu, insn_daddiu, insn_dmfc0,
  59. insn_dmtc0, insn_dsll, insn_dsll32, insn_dsra, insn_dsrl,
  60. insn_dsrl32, insn_drotr, insn_drotr32, insn_dsubu, insn_eret,
  61. insn_j, insn_jal, insn_jr, insn_ld, insn_ll, insn_lld,
  62. insn_lui, insn_lw, insn_mfc0, insn_mtc0, insn_or, insn_ori,
  63. insn_pref, insn_rfe, insn_sc, insn_scd, insn_sd, insn_sll,
  64. insn_sra, insn_srl, insn_rotr, insn_subu, insn_sw, insn_tlbp,
  65. insn_tlbr, insn_tlbwi, insn_tlbwr, insn_xor, insn_xori,
  66. insn_dins, insn_syscall, insn_bbit0, insn_bbit1
  67. };
  68. struct insn {
  69. enum opcode opcode;
  70. u32 match;
  71. enum fields fields;
  72. };
  73. /* This macro sets the non-variable bits of an instruction. */
  74. #define M(a, b, c, d, e, f) \
  75. ((a) << OP_SH \
  76. | (b) << RS_SH \
  77. | (c) << RT_SH \
  78. | (d) << RD_SH \
  79. | (e) << RE_SH \
  80. | (f) << FUNC_SH)
  81. static struct insn insn_table[] __uasminitdata = {
  82. { insn_addiu, M(addiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  83. { insn_addu, M(spec_op, 0, 0, 0, 0, addu_op), RS | RT | RD },
  84. { insn_and, M(spec_op, 0, 0, 0, 0, and_op), RS | RT | RD },
  85. { insn_andi, M(andi_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
  86. { insn_beq, M(beq_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  87. { insn_beql, M(beql_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  88. { insn_bgez, M(bcond_op, 0, bgez_op, 0, 0, 0), RS | BIMM },
  89. { insn_bgezl, M(bcond_op, 0, bgezl_op, 0, 0, 0), RS | BIMM },
  90. { insn_bltz, M(bcond_op, 0, bltz_op, 0, 0, 0), RS | BIMM },
  91. { insn_bltzl, M(bcond_op, 0, bltzl_op, 0, 0, 0), RS | BIMM },
  92. { insn_bne, M(bne_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  93. { insn_cache, M(cache_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  94. { insn_daddiu, M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  95. { insn_daddu, M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD },
  96. { insn_dmfc0, M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET},
  97. { insn_dmtc0, M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET},
  98. { insn_dsll, M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE },
  99. { insn_dsll32, M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE },
  100. { insn_dsra, M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE },
  101. { insn_dsrl, M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE },
  102. { insn_dsrl32, M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE },
  103. { insn_drotr, M(spec_op, 1, 0, 0, 0, dsrl_op), RT | RD | RE },
  104. { insn_drotr32, M(spec_op, 1, 0, 0, 0, dsrl32_op), RT | RD | RE },
  105. { insn_dsubu, M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD },
  106. { insn_eret, M(cop0_op, cop_op, 0, 0, 0, eret_op), 0 },
  107. { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM },
  108. { insn_jal, M(jal_op, 0, 0, 0, 0, 0), JIMM },
  109. { insn_jr, M(spec_op, 0, 0, 0, 0, jr_op), RS },
  110. { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  111. { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  112. { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  113. { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM },
  114. { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  115. { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET},
  116. { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET},
  117. { insn_or, M(spec_op, 0, 0, 0, 0, or_op), RS | RT | RD },
  118. { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
  119. { insn_pref, M(pref_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  120. { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 },
  121. { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  122. { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  123. { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  124. { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE },
  125. { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE },
  126. { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE },
  127. { insn_rotr, M(spec_op, 1, 0, 0, 0, srl_op), RT | RD | RE },
  128. { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD },
  129. { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  130. { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 },
  131. { insn_tlbr, M(cop0_op, cop_op, 0, 0, 0, tlbr_op), 0 },
  132. { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 },
  133. { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 },
  134. { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD },
  135. { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
  136. { insn_dins, M(spec3_op, 0, 0, 0, 0, dins_op), RS | RT | RD | RE },
  137. { insn_syscall, M(spec_op, 0, 0, 0, 0, syscall_op), SCIMM},
  138. { insn_bbit0, M(lwc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  139. { insn_bbit1, M(swc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  140. { insn_invalid, 0, 0 }
  141. };
  142. #undef M
  143. static inline __uasminit u32 build_rs(u32 arg)
  144. {
  145. if (arg & ~RS_MASK)
  146. printk(KERN_WARNING "Micro-assembler field overflow\n");
  147. return (arg & RS_MASK) << RS_SH;
  148. }
  149. static inline __uasminit u32 build_rt(u32 arg)
  150. {
  151. if (arg & ~RT_MASK)
  152. printk(KERN_WARNING "Micro-assembler field overflow\n");
  153. return (arg & RT_MASK) << RT_SH;
  154. }
  155. static inline __uasminit u32 build_rd(u32 arg)
  156. {
  157. if (arg & ~RD_MASK)
  158. printk(KERN_WARNING "Micro-assembler field overflow\n");
  159. return (arg & RD_MASK) << RD_SH;
  160. }
  161. static inline __uasminit u32 build_re(u32 arg)
  162. {
  163. if (arg & ~RE_MASK)
  164. printk(KERN_WARNING "Micro-assembler field overflow\n");
  165. return (arg & RE_MASK) << RE_SH;
  166. }
  167. static inline __uasminit u32 build_simm(s32 arg)
  168. {
  169. if (arg > 0x7fff || arg < -0x8000)
  170. printk(KERN_WARNING "Micro-assembler field overflow\n");
  171. return arg & 0xffff;
  172. }
  173. static inline __uasminit u32 build_uimm(u32 arg)
  174. {
  175. if (arg & ~IMM_MASK)
  176. printk(KERN_WARNING "Micro-assembler field overflow\n");
  177. return arg & IMM_MASK;
  178. }
  179. static inline __uasminit u32 build_bimm(s32 arg)
  180. {
  181. if (arg > 0x1ffff || arg < -0x20000)
  182. printk(KERN_WARNING "Micro-assembler field overflow\n");
  183. if (arg & 0x3)
  184. printk(KERN_WARNING "Invalid micro-assembler branch target\n");
  185. return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff);
  186. }
  187. static inline __uasminit u32 build_jimm(u32 arg)
  188. {
  189. if (arg & ~((JIMM_MASK) << 2))
  190. printk(KERN_WARNING "Micro-assembler field overflow\n");
  191. return (arg >> 2) & JIMM_MASK;
  192. }
  193. static inline __uasminit u32 build_scimm(u32 arg)
  194. {
  195. if (arg & ~SCIMM_MASK)
  196. printk(KERN_WARNING "Micro-assembler field overflow\n");
  197. return (arg & SCIMM_MASK) << SCIMM_SH;
  198. }
  199. static inline __uasminit u32 build_func(u32 arg)
  200. {
  201. if (arg & ~FUNC_MASK)
  202. printk(KERN_WARNING "Micro-assembler field overflow\n");
  203. return arg & FUNC_MASK;
  204. }
  205. static inline __uasminit u32 build_set(u32 arg)
  206. {
  207. if (arg & ~SET_MASK)
  208. printk(KERN_WARNING "Micro-assembler field overflow\n");
  209. return arg & SET_MASK;
  210. }
  211. /*
  212. * The order of opcode arguments is implicitly left to right,
  213. * starting with RS and ending with FUNC or IMM.
  214. */
  215. static void __uasminit build_insn(u32 **buf, enum opcode opc, ...)
  216. {
  217. struct insn *ip = NULL;
  218. unsigned int i;
  219. va_list ap;
  220. u32 op;
  221. for (i = 0; insn_table[i].opcode != insn_invalid; i++)
  222. if (insn_table[i].opcode == opc) {
  223. ip = &insn_table[i];
  224. break;
  225. }
  226. if (!ip || (opc == insn_daddiu && r4k_daddiu_bug()))
  227. panic("Unsupported Micro-assembler instruction %d", opc);
  228. op = ip->match;
  229. va_start(ap, opc);
  230. if (ip->fields & RS)
  231. op |= build_rs(va_arg(ap, u32));
  232. if (ip->fields & RT)
  233. op |= build_rt(va_arg(ap, u32));
  234. if (ip->fields & RD)
  235. op |= build_rd(va_arg(ap, u32));
  236. if (ip->fields & RE)
  237. op |= build_re(va_arg(ap, u32));
  238. if (ip->fields & SIMM)
  239. op |= build_simm(va_arg(ap, s32));
  240. if (ip->fields & UIMM)
  241. op |= build_uimm(va_arg(ap, u32));
  242. if (ip->fields & BIMM)
  243. op |= build_bimm(va_arg(ap, s32));
  244. if (ip->fields & JIMM)
  245. op |= build_jimm(va_arg(ap, u32));
  246. if (ip->fields & FUNC)
  247. op |= build_func(va_arg(ap, u32));
  248. if (ip->fields & SET)
  249. op |= build_set(va_arg(ap, u32));
  250. if (ip->fields & SCIMM)
  251. op |= build_scimm(va_arg(ap, u32));
  252. va_end(ap);
  253. **buf = op;
  254. (*buf)++;
  255. }
  256. #define I_u1u2u3(op) \
  257. Ip_u1u2u3(op) \
  258. { \
  259. build_insn(buf, insn##op, a, b, c); \
  260. } \
  261. UASM_EXPORT_SYMBOL(uasm_i##op);
  262. #define I_u2u1u3(op) \
  263. Ip_u2u1u3(op) \
  264. { \
  265. build_insn(buf, insn##op, b, a, c); \
  266. } \
  267. UASM_EXPORT_SYMBOL(uasm_i##op);
  268. #define I_u3u1u2(op) \
  269. Ip_u3u1u2(op) \
  270. { \
  271. build_insn(buf, insn##op, b, c, a); \
  272. } \
  273. UASM_EXPORT_SYMBOL(uasm_i##op);
  274. #define I_u1u2s3(op) \
  275. Ip_u1u2s3(op) \
  276. { \
  277. build_insn(buf, insn##op, a, b, c); \
  278. } \
  279. UASM_EXPORT_SYMBOL(uasm_i##op);
  280. #define I_u2s3u1(op) \
  281. Ip_u2s3u1(op) \
  282. { \
  283. build_insn(buf, insn##op, c, a, b); \
  284. } \
  285. UASM_EXPORT_SYMBOL(uasm_i##op);
  286. #define I_u2u1s3(op) \
  287. Ip_u2u1s3(op) \
  288. { \
  289. build_insn(buf, insn##op, b, a, c); \
  290. } \
  291. UASM_EXPORT_SYMBOL(uasm_i##op);
  292. #define I_u2u1msbu3(op) \
  293. Ip_u2u1msbu3(op) \
  294. { \
  295. build_insn(buf, insn##op, b, a, c+d-1, c); \
  296. } \
  297. UASM_EXPORT_SYMBOL(uasm_i##op);
  298. #define I_u1u2(op) \
  299. Ip_u1u2(op) \
  300. { \
  301. build_insn(buf, insn##op, a, b); \
  302. } \
  303. UASM_EXPORT_SYMBOL(uasm_i##op);
  304. #define I_u1s2(op) \
  305. Ip_u1s2(op) \
  306. { \
  307. build_insn(buf, insn##op, a, b); \
  308. } \
  309. UASM_EXPORT_SYMBOL(uasm_i##op);
  310. #define I_u1(op) \
  311. Ip_u1(op) \
  312. { \
  313. build_insn(buf, insn##op, a); \
  314. } \
  315. UASM_EXPORT_SYMBOL(uasm_i##op);
  316. #define I_0(op) \
  317. Ip_0(op) \
  318. { \
  319. build_insn(buf, insn##op); \
  320. } \
  321. UASM_EXPORT_SYMBOL(uasm_i##op);
  322. I_u2u1s3(_addiu)
  323. I_u3u1u2(_addu)
  324. I_u2u1u3(_andi)
  325. I_u3u1u2(_and)
  326. I_u1u2s3(_beq)
  327. I_u1u2s3(_beql)
  328. I_u1s2(_bgez)
  329. I_u1s2(_bgezl)
  330. I_u1s2(_bltz)
  331. I_u1s2(_bltzl)
  332. I_u1u2s3(_bne)
  333. I_u2s3u1(_cache)
  334. I_u1u2u3(_dmfc0)
  335. I_u1u2u3(_dmtc0)
  336. I_u2u1s3(_daddiu)
  337. I_u3u1u2(_daddu)
  338. I_u2u1u3(_dsll)
  339. I_u2u1u3(_dsll32)
  340. I_u2u1u3(_dsra)
  341. I_u2u1u3(_dsrl)
  342. I_u2u1u3(_dsrl32)
  343. I_u2u1u3(_drotr)
  344. I_u2u1u3(_drotr32)
  345. I_u3u1u2(_dsubu)
  346. I_0(_eret)
  347. I_u1(_j)
  348. I_u1(_jal)
  349. I_u1(_jr)
  350. I_u2s3u1(_ld)
  351. I_u2s3u1(_ll)
  352. I_u2s3u1(_lld)
  353. I_u1s2(_lui)
  354. I_u2s3u1(_lw)
  355. I_u1u2u3(_mfc0)
  356. I_u1u2u3(_mtc0)
  357. I_u2u1u3(_ori)
  358. I_u3u1u2(_or)
  359. I_u2s3u1(_pref)
  360. I_0(_rfe)
  361. I_u2s3u1(_sc)
  362. I_u2s3u1(_scd)
  363. I_u2s3u1(_sd)
  364. I_u2u1u3(_sll)
  365. I_u2u1u3(_sra)
  366. I_u2u1u3(_srl)
  367. I_u2u1u3(_rotr)
  368. I_u3u1u2(_subu)
  369. I_u2s3u1(_sw)
  370. I_0(_tlbp)
  371. I_0(_tlbr)
  372. I_0(_tlbwi)
  373. I_0(_tlbwr)
  374. I_u3u1u2(_xor)
  375. I_u2u1u3(_xori)
  376. I_u2u1msbu3(_dins);
  377. I_u1(_syscall);
  378. I_u1u2s3(_bbit0);
  379. I_u1u2s3(_bbit1);
  380. /* Handle labels. */
  381. void __uasminit uasm_build_label(struct uasm_label **lab, u32 *addr, int lid)
  382. {
  383. (*lab)->addr = addr;
  384. (*lab)->lab = lid;
  385. (*lab)++;
  386. }
  387. UASM_EXPORT_SYMBOL(uasm_build_label);
  388. int __uasminit uasm_in_compat_space_p(long addr)
  389. {
  390. /* Is this address in 32bit compat space? */
  391. #ifdef CONFIG_64BIT
  392. return (((addr) & 0xffffffff00000000L) == 0xffffffff00000000L);
  393. #else
  394. return 1;
  395. #endif
  396. }
  397. UASM_EXPORT_SYMBOL(uasm_in_compat_space_p);
  398. static int __uasminit uasm_rel_highest(long val)
  399. {
  400. #ifdef CONFIG_64BIT
  401. return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
  402. #else
  403. return 0;
  404. #endif
  405. }
  406. static int __uasminit uasm_rel_higher(long val)
  407. {
  408. #ifdef CONFIG_64BIT
  409. return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
  410. #else
  411. return 0;
  412. #endif
  413. }
  414. int __uasminit uasm_rel_hi(long val)
  415. {
  416. return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
  417. }
  418. UASM_EXPORT_SYMBOL(uasm_rel_hi);
  419. int __uasminit uasm_rel_lo(long val)
  420. {
  421. return ((val & 0xffff) ^ 0x8000) - 0x8000;
  422. }
  423. UASM_EXPORT_SYMBOL(uasm_rel_lo);
  424. void __uasminit UASM_i_LA_mostly(u32 **buf, unsigned int rs, long addr)
  425. {
  426. if (!uasm_in_compat_space_p(addr)) {
  427. uasm_i_lui(buf, rs, uasm_rel_highest(addr));
  428. if (uasm_rel_higher(addr))
  429. uasm_i_daddiu(buf, rs, rs, uasm_rel_higher(addr));
  430. if (uasm_rel_hi(addr)) {
  431. uasm_i_dsll(buf, rs, rs, 16);
  432. uasm_i_daddiu(buf, rs, rs, uasm_rel_hi(addr));
  433. uasm_i_dsll(buf, rs, rs, 16);
  434. } else
  435. uasm_i_dsll32(buf, rs, rs, 0);
  436. } else
  437. uasm_i_lui(buf, rs, uasm_rel_hi(addr));
  438. }
  439. UASM_EXPORT_SYMBOL(UASM_i_LA_mostly);
  440. void __uasminit UASM_i_LA(u32 **buf, unsigned int rs, long addr)
  441. {
  442. UASM_i_LA_mostly(buf, rs, addr);
  443. if (uasm_rel_lo(addr)) {
  444. if (!uasm_in_compat_space_p(addr))
  445. uasm_i_daddiu(buf, rs, rs, uasm_rel_lo(addr));
  446. else
  447. uasm_i_addiu(buf, rs, rs, uasm_rel_lo(addr));
  448. }
  449. }
  450. UASM_EXPORT_SYMBOL(UASM_i_LA);
  451. /* Handle relocations. */
  452. void __uasminit
  453. uasm_r_mips_pc16(struct uasm_reloc **rel, u32 *addr, int lid)
  454. {
  455. (*rel)->addr = addr;
  456. (*rel)->type = R_MIPS_PC16;
  457. (*rel)->lab = lid;
  458. (*rel)++;
  459. }
  460. UASM_EXPORT_SYMBOL(uasm_r_mips_pc16);
  461. static inline void __uasminit
  462. __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab)
  463. {
  464. long laddr = (long)lab->addr;
  465. long raddr = (long)rel->addr;
  466. switch (rel->type) {
  467. case R_MIPS_PC16:
  468. *rel->addr |= build_bimm(laddr - (raddr + 4));
  469. break;
  470. default:
  471. panic("Unsupported Micro-assembler relocation %d",
  472. rel->type);
  473. }
  474. }
  475. void __uasminit
  476. uasm_resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab)
  477. {
  478. struct uasm_label *l;
  479. for (; rel->lab != UASM_LABEL_INVALID; rel++)
  480. for (l = lab; l->lab != UASM_LABEL_INVALID; l++)
  481. if (rel->lab == l->lab)
  482. __resolve_relocs(rel, l);
  483. }
  484. UASM_EXPORT_SYMBOL(uasm_resolve_relocs);
  485. void __uasminit
  486. uasm_move_relocs(struct uasm_reloc *rel, u32 *first, u32 *end, long off)
  487. {
  488. for (; rel->lab != UASM_LABEL_INVALID; rel++)
  489. if (rel->addr >= first && rel->addr < end)
  490. rel->addr += off;
  491. }
  492. UASM_EXPORT_SYMBOL(uasm_move_relocs);
  493. void __uasminit
  494. uasm_move_labels(struct uasm_label *lab, u32 *first, u32 *end, long off)
  495. {
  496. for (; lab->lab != UASM_LABEL_INVALID; lab++)
  497. if (lab->addr >= first && lab->addr < end)
  498. lab->addr += off;
  499. }
  500. UASM_EXPORT_SYMBOL(uasm_move_labels);
  501. void __uasminit
  502. uasm_copy_handler(struct uasm_reloc *rel, struct uasm_label *lab, u32 *first,
  503. u32 *end, u32 *target)
  504. {
  505. long off = (long)(target - first);
  506. memcpy(target, first, (end - first) * sizeof(u32));
  507. uasm_move_relocs(rel, first, end, off);
  508. uasm_move_labels(lab, first, end, off);
  509. }
  510. UASM_EXPORT_SYMBOL(uasm_copy_handler);
  511. int __uasminit uasm_insn_has_bdelay(struct uasm_reloc *rel, u32 *addr)
  512. {
  513. for (; rel->lab != UASM_LABEL_INVALID; rel++) {
  514. if (rel->addr == addr
  515. && (rel->type == R_MIPS_PC16
  516. || rel->type == R_MIPS_26))
  517. return 1;
  518. }
  519. return 0;
  520. }
  521. UASM_EXPORT_SYMBOL(uasm_insn_has_bdelay);
  522. /* Convenience functions for labeled branches. */
  523. void __uasminit
  524. uasm_il_bltz(u32 **p, struct uasm_reloc **r, unsigned int reg, int lid)
  525. {
  526. uasm_r_mips_pc16(r, *p, lid);
  527. uasm_i_bltz(p, reg, 0);
  528. }
  529. UASM_EXPORT_SYMBOL(uasm_il_bltz);
  530. void __uasminit
  531. uasm_il_b(u32 **p, struct uasm_reloc **r, int lid)
  532. {
  533. uasm_r_mips_pc16(r, *p, lid);
  534. uasm_i_b(p, 0);
  535. }
  536. UASM_EXPORT_SYMBOL(uasm_il_b);
  537. void __uasminit
  538. uasm_il_beqz(u32 **p, struct uasm_reloc **r, unsigned int reg, int lid)
  539. {
  540. uasm_r_mips_pc16(r, *p, lid);
  541. uasm_i_beqz(p, reg, 0);
  542. }
  543. UASM_EXPORT_SYMBOL(uasm_il_beqz);
  544. void __uasminit
  545. uasm_il_beqzl(u32 **p, struct uasm_reloc **r, unsigned int reg, int lid)
  546. {
  547. uasm_r_mips_pc16(r, *p, lid);
  548. uasm_i_beqzl(p, reg, 0);
  549. }
  550. UASM_EXPORT_SYMBOL(uasm_il_beqzl);
  551. void __uasminit
  552. uasm_il_bne(u32 **p, struct uasm_reloc **r, unsigned int reg1,
  553. unsigned int reg2, int lid)
  554. {
  555. uasm_r_mips_pc16(r, *p, lid);
  556. uasm_i_bne(p, reg1, reg2, 0);
  557. }
  558. UASM_EXPORT_SYMBOL(uasm_il_bne);
  559. void __uasminit
  560. uasm_il_bnez(u32 **p, struct uasm_reloc **r, unsigned int reg, int lid)
  561. {
  562. uasm_r_mips_pc16(r, *p, lid);
  563. uasm_i_bnez(p, reg, 0);
  564. }
  565. UASM_EXPORT_SYMBOL(uasm_il_bnez);
  566. void __uasminit
  567. uasm_il_bgezl(u32 **p, struct uasm_reloc **r, unsigned int reg, int lid)
  568. {
  569. uasm_r_mips_pc16(r, *p, lid);
  570. uasm_i_bgezl(p, reg, 0);
  571. }
  572. UASM_EXPORT_SYMBOL(uasm_il_bgezl);
  573. void __uasminit
  574. uasm_il_bgez(u32 **p, struct uasm_reloc **r, unsigned int reg, int lid)
  575. {
  576. uasm_r_mips_pc16(r, *p, lid);
  577. uasm_i_bgez(p, reg, 0);
  578. }
  579. UASM_EXPORT_SYMBOL(uasm_il_bgez);
  580. void __uasminit
  581. uasm_il_bbit0(u32 **p, struct uasm_reloc **r, unsigned int reg,
  582. unsigned int bit, int lid)
  583. {
  584. uasm_r_mips_pc16(r, *p, lid);
  585. uasm_i_bbit0(p, reg, bit, 0);
  586. }
  587. UASM_EXPORT_SYMBOL(uasm_il_bbit0);
  588. void __uasminit
  589. uasm_il_bbit1(u32 **p, struct uasm_reloc **r, unsigned int reg,
  590. unsigned int bit, int lid)
  591. {
  592. uasm_r_mips_pc16(r, *p, lid);
  593. uasm_i_bbit1(p, reg, bit, 0);
  594. }
  595. UASM_EXPORT_SYMBOL(uasm_il_bbit1);