uasm.c 18 KB

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