1abc597feSSteven J. Hill /* 2abc597feSSteven J. Hill * This file is subject to the terms and conditions of the GNU General Public 3abc597feSSteven J. Hill * License. See the file "COPYING" in the main directory of this archive 4abc597feSSteven J. Hill * for more details. 5abc597feSSteven J. Hill * 6abc597feSSteven J. Hill * A small micro-assembler. It is intentionally kept simple, does only 7abc597feSSteven J. Hill * support a subset of instructions, and does not try to hide pipeline 8abc597feSSteven J. Hill * effects like branch delay slots. 9abc597feSSteven J. Hill * 10abc597feSSteven J. Hill * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer 11abc597feSSteven J. Hill * Copyright (C) 2005, 2007 Maciej W. Rozycki 12abc597feSSteven J. Hill * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org) 13abc597feSSteven J. Hill * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved. 14abc597feSSteven J. Hill */ 15abc597feSSteven J. Hill 16abc597feSSteven J. Hill #include <linux/kernel.h> 17abc597feSSteven J. Hill #include <linux/types.h> 18abc597feSSteven J. Hill 19abc597feSSteven J. Hill #include <asm/inst.h> 20abc597feSSteven J. Hill #include <asm/elf.h> 21abc597feSSteven J. Hill #include <asm/bugs.h> 22cf6d9058SSteven J. Hill #define UASM_ISA _UASM_ISA_CLASSIC 23abc597feSSteven J. Hill #include <asm/uasm.h> 24abc597feSSteven J. Hill 25abc597feSSteven J. Hill #define RS_MASK 0x1f 26abc597feSSteven J. Hill #define RS_SH 21 27abc597feSSteven J. Hill #define RT_MASK 0x1f 28abc597feSSteven J. Hill #define RT_SH 16 29abc597feSSteven J. Hill #define SCIMM_MASK 0xfffff 30abc597feSSteven J. Hill #define SCIMM_SH 6 31abc597feSSteven J. Hill 32abc597feSSteven J. Hill /* This macro sets the non-variable bits of an instruction. */ 33abc597feSSteven J. Hill #define M(a, b, c, d, e, f) \ 34abc597feSSteven J. Hill ((a) << OP_SH \ 35abc597feSSteven J. Hill | (b) << RS_SH \ 36abc597feSSteven J. Hill | (c) << RT_SH \ 37abc597feSSteven J. Hill | (d) << RD_SH \ 38abc597feSSteven J. Hill | (e) << RE_SH \ 39abc597feSSteven J. Hill | (f) << FUNC_SH) 40abc597feSSteven J. Hill 41*a168b8f1SLeonid Yegoshin /* This macro sets the non-variable bits of an R6 instruction. */ 42*a168b8f1SLeonid Yegoshin #define M6(a, b, c, d, e) \ 43*a168b8f1SLeonid Yegoshin ((a) << OP_SH \ 44*a168b8f1SLeonid Yegoshin | (b) << RS_SH \ 45*a168b8f1SLeonid Yegoshin | (c) << RT_SH \ 46*a168b8f1SLeonid Yegoshin | (d) << SIMM9_SH \ 47*a168b8f1SLeonid Yegoshin | (e) << FUNC_SH) 48*a168b8f1SLeonid Yegoshin 49cf6d9058SSteven J. Hill /* Define these when we are not the ISA the kernel is being compiled with. */ 50cf6d9058SSteven J. Hill #ifdef CONFIG_CPU_MICROMIPS 51cf6d9058SSteven J. Hill #define CL_uasm_i_b(buf, off) ISAOPC(_beq)(buf, 0, 0, off) 52cf6d9058SSteven J. Hill #define CL_uasm_i_beqz(buf, rs, off) ISAOPC(_beq)(buf, rs, 0, off) 53cf6d9058SSteven J. Hill #define CL_uasm_i_beqzl(buf, rs, off) ISAOPC(_beql)(buf, rs, 0, off) 54cf6d9058SSteven J. Hill #define CL_uasm_i_bnez(buf, rs, off) ISAOPC(_bne)(buf, rs, 0, off) 55cf6d9058SSteven J. Hill #endif 56cf6d9058SSteven J. Hill 57abc597feSSteven J. Hill #include "uasm.c" 58abc597feSSteven J. Hill 59078a55fcSPaul Gortmaker static struct insn insn_table[] = { 60abc597feSSteven J. Hill { insn_addiu, M(addiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 61abc597feSSteven J. Hill { insn_addu, M(spec_op, 0, 0, 0, 0, addu_op), RS | RT | RD }, 62abc597feSSteven J. Hill { insn_andi, M(andi_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 63abc597feSSteven J. Hill { insn_and, M(spec_op, 0, 0, 0, 0, and_op), RS | RT | RD }, 64abc597feSSteven J. Hill { insn_bbit0, M(lwc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 65abc597feSSteven J. Hill { insn_bbit1, M(swc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 66abc597feSSteven J. Hill { insn_beql, M(beql_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 67abc597feSSteven J. Hill { insn_beq, M(beq_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 68abc597feSSteven J. Hill { insn_bgezl, M(bcond_op, 0, bgezl_op, 0, 0, 0), RS | BIMM }, 69abc597feSSteven J. Hill { insn_bgez, M(bcond_op, 0, bgez_op, 0, 0, 0), RS | BIMM }, 70abc597feSSteven J. Hill { insn_bltzl, M(bcond_op, 0, bltzl_op, 0, 0, 0), RS | BIMM }, 71abc597feSSteven J. Hill { insn_bltz, M(bcond_op, 0, bltz_op, 0, 0, 0), RS | BIMM }, 72abc597feSSteven J. Hill { insn_bne, M(bne_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 73*a168b8f1SLeonid Yegoshin #ifndef CONFIG_CPU_MIPSR6 74abc597feSSteven J. Hill { insn_cache, M(cache_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 75*a168b8f1SLeonid Yegoshin #else 76*a168b8f1SLeonid Yegoshin { insn_cache, M6(cache_op, 0, 0, 0, cache6_op), RS | RT | SIMM9 }, 77*a168b8f1SLeonid Yegoshin #endif 78abc597feSSteven J. Hill { insn_daddiu, M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 79abc597feSSteven J. Hill { insn_daddu, M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD }, 80abc597feSSteven J. Hill { insn_dinsm, M(spec3_op, 0, 0, 0, 0, dinsm_op), RS | RT | RD | RE }, 81abc597feSSteven J. Hill { insn_dins, M(spec3_op, 0, 0, 0, 0, dins_op), RS | RT | RD | RE }, 824c12a854SMarkos Chandras { insn_divu, M(spec_op, 0, 0, 0, 0, divu_op), RS | RT }, 83abc597feSSteven J. Hill { insn_dmfc0, M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET}, 84abc597feSSteven J. Hill { insn_dmtc0, M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET}, 85abc597feSSteven J. Hill { insn_drotr32, M(spec_op, 1, 0, 0, 0, dsrl32_op), RT | RD | RE }, 86abc597feSSteven J. Hill { insn_drotr, M(spec_op, 1, 0, 0, 0, dsrl_op), RT | RD | RE }, 87abc597feSSteven J. Hill { insn_dsll32, M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE }, 88abc597feSSteven J. Hill { insn_dsll, M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE }, 89abc597feSSteven J. Hill { insn_dsra, M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE }, 90abc597feSSteven J. Hill { insn_dsrl32, M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE }, 91abc597feSSteven J. Hill { insn_dsrl, M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE }, 92abc597feSSteven J. Hill { insn_dsubu, M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD }, 93abc597feSSteven J. Hill { insn_eret, M(cop0_op, cop_op, 0, 0, 0, eret_op), 0 }, 94abc597feSSteven J. Hill { insn_ext, M(spec3_op, 0, 0, 0, 0, ext_op), RS | RT | RD | RE }, 95abc597feSSteven J. Hill { insn_ins, M(spec3_op, 0, 0, 0, 0, ins_op), RS | RT | RD | RE }, 96abc597feSSteven J. Hill { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM }, 97abc597feSSteven J. Hill { insn_jal, M(jal_op, 0, 0, 0, 0, 0), JIMM }, 9849e9529bSPaul Burton { insn_jalr, M(spec_op, 0, 0, 0, 0, jalr_op), RS | RD }, 99abc597feSSteven J. Hill { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM }, 100*a168b8f1SLeonid Yegoshin #ifndef CONFIG_CPU_MIPSR6 101abc597feSSteven J. Hill { insn_jr, M(spec_op, 0, 0, 0, 0, jr_op), RS }, 102*a168b8f1SLeonid Yegoshin #else 103*a168b8f1SLeonid Yegoshin { insn_jr, M(spec_op, 0, 0, 0, 0, jalr_op), RS }, 104*a168b8f1SLeonid Yegoshin #endif 10582488818SMarkos Chandras { insn_lb, M(lb_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 106abc597feSSteven J. Hill { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 107abc597feSSteven J. Hill { insn_ldx, M(spec3_op, 0, 0, 0, ldx_op, lx_op), RS | RT | RD }, 10884c68cbcSMarkos Chandras { insn_lh, M(lh_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 109*a168b8f1SLeonid Yegoshin #ifndef CONFIG_CPU_MIPSR6 110abc597feSSteven J. Hill { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 111abc597feSSteven J. Hill { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 112*a168b8f1SLeonid Yegoshin #else 113*a168b8f1SLeonid Yegoshin { insn_lld, M6(spec3_op, 0, 0, 0, lld6_op), RS | RT | SIMM9 }, 114*a168b8f1SLeonid Yegoshin { insn_ll, M6(spec3_op, 0, 0, 0, ll6_op), RS | RT | SIMM9 }, 115*a168b8f1SLeonid Yegoshin #endif 116abc597feSSteven J. Hill { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM }, 117abc597feSSteven J. Hill { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 118abc597feSSteven J. Hill { insn_lwx, M(spec3_op, 0, 0, 0, lwx_op, lx_op), RS | RT | RD }, 119abc597feSSteven J. Hill { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET}, 120e2965cd0SSteven J. Hill { insn_mfhc0, M(cop0_op, mfhc0_op, 0, 0, 0, 0), RT | RD | SET}, 121f3ec7a23SMarkos Chandras { insn_mfhi, M(spec_op, 0, 0, 0, 0, mfhi_op), RD }, 12216d21a81SMarkos Chandras { insn_mflo, M(spec_op, 0, 0, 0, 0, mflo_op), RD }, 123abc597feSSteven J. Hill { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET}, 124e2965cd0SSteven J. Hill { insn_mthc0, M(cop0_op, mthc0_op, 0, 0, 0, 0), RT | RD | SET}, 125a8e897adSMarkos Chandras { insn_mul, M(spec2_op, 0, 0, 0, 0, mul_op), RS | RT | RD}, 126abc597feSSteven J. Hill { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 127abc597feSSteven J. Hill { insn_or, M(spec_op, 0, 0, 0, 0, or_op), RS | RT | RD }, 128*a168b8f1SLeonid Yegoshin #ifndef CONFIG_CPU_MIPSR6 129abc597feSSteven J. Hill { insn_pref, M(pref_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 130*a168b8f1SLeonid Yegoshin #else 131*a168b8f1SLeonid Yegoshin { insn_pref, M6(spec3_op, 0, 0, 0, pref6_op), RS | RT | SIMM9 }, 132*a168b8f1SLeonid Yegoshin #endif 133abc597feSSteven J. Hill { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 }, 134abc597feSSteven J. Hill { insn_rotr, M(spec_op, 1, 0, 0, 0, srl_op), RT | RD | RE }, 135*a168b8f1SLeonid Yegoshin #ifndef CONFIG_CPU_MIPSR6 136abc597feSSteven J. Hill { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 137abc597feSSteven J. Hill { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 138*a168b8f1SLeonid Yegoshin #else 139*a168b8f1SLeonid Yegoshin { insn_scd, M6(spec3_op, 0, 0, 0, scd6_op), RS | RT | SIMM9 }, 140*a168b8f1SLeonid Yegoshin { insn_sc, M6(spec3_op, 0, 0, 0, sc6_op), RS | RT | SIMM9 }, 141*a168b8f1SLeonid Yegoshin #endif 142abc597feSSteven J. Hill { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 143abc597feSSteven J. Hill { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE }, 144bef581baSMarkos Chandras { insn_sllv, M(spec_op, 0, 0, 0, 0, sllv_op), RS | RT | RD }, 1457682f9e8SMarkos Chandras { insn_slt, M(spec_op, 0, 0, 0, 0, slt_op), RS | RT | RD }, 146390363edSMarkos Chandras { insn_sltiu, M(sltiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 147e8ef868bSMarkos Chandras { insn_sltu, M(spec_op, 0, 0, 0, 0, sltu_op), RS | RT | RD }, 148abc597feSSteven J. Hill { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE }, 149abc597feSSteven J. Hill { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE }, 150f31318fdSMarkos Chandras { insn_srlv, M(spec_op, 0, 0, 0, 0, srlv_op), RS | RT | RD }, 151abc597feSSteven J. Hill { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD }, 152abc597feSSteven J. Hill { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 153729ff561SPaul Burton { insn_sync, M(spec_op, 0, 0, 0, 0, sync_op), RE }, 154abc597feSSteven J. Hill { insn_syscall, M(spec_op, 0, 0, 0, 0, syscall_op), SCIMM}, 155abc597feSSteven J. Hill { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 }, 156abc597feSSteven J. Hill { insn_tlbr, M(cop0_op, cop_op, 0, 0, 0, tlbr_op), 0 }, 157abc597feSSteven J. Hill { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 }, 158abc597feSSteven J. Hill { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 }, 15953ed1389SPaul Burton { insn_wait, M(cop0_op, cop_op, 0, 0, 0, wait_op), SCIMM }, 160ab9e4fa0SMarkos Chandras { insn_wsbh, M(spec3_op, 0, 0, 0, wsbh_op, bshfl_op), RT | RD }, 161abc597feSSteven J. Hill { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 162abc597feSSteven J. Hill { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD }, 163d674dd14SPaul Burton { insn_yield, M(spec3_op, 0, 0, 0, 0, yield_op), RS | RD }, 164abc597feSSteven J. Hill { insn_invalid, 0, 0 } 165abc597feSSteven J. Hill }; 166abc597feSSteven J. Hill 167abc597feSSteven J. Hill #undef M 168abc597feSSteven J. Hill 169078a55fcSPaul Gortmaker static inline u32 build_bimm(s32 arg) 170abc597feSSteven J. Hill { 171abc597feSSteven J. Hill WARN(arg > 0x1ffff || arg < -0x20000, 172abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 173abc597feSSteven J. Hill 174abc597feSSteven J. Hill WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 175abc597feSSteven J. Hill 176abc597feSSteven J. Hill return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff); 177abc597feSSteven J. Hill } 178abc597feSSteven J. Hill 179078a55fcSPaul Gortmaker static inline u32 build_jimm(u32 arg) 180abc597feSSteven J. Hill { 181abc597feSSteven J. Hill WARN(arg & ~(JIMM_MASK << 2), 182abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 183abc597feSSteven J. Hill 184abc597feSSteven J. Hill return (arg >> 2) & JIMM_MASK; 185abc597feSSteven J. Hill } 186abc597feSSteven J. Hill 187abc597feSSteven J. Hill /* 188abc597feSSteven J. Hill * The order of opcode arguments is implicitly left to right, 189abc597feSSteven J. Hill * starting with RS and ending with FUNC or IMM. 190abc597feSSteven J. Hill */ 191078a55fcSPaul Gortmaker static void build_insn(u32 **buf, enum opcode opc, ...) 192abc597feSSteven J. Hill { 193abc597feSSteven J. Hill struct insn *ip = NULL; 194abc597feSSteven J. Hill unsigned int i; 195abc597feSSteven J. Hill va_list ap; 196abc597feSSteven J. Hill u32 op; 197abc597feSSteven J. Hill 198abc597feSSteven J. Hill for (i = 0; insn_table[i].opcode != insn_invalid; i++) 199abc597feSSteven J. Hill if (insn_table[i].opcode == opc) { 200abc597feSSteven J. Hill ip = &insn_table[i]; 201abc597feSSteven J. Hill break; 202abc597feSSteven J. Hill } 203abc597feSSteven J. Hill 204abc597feSSteven J. Hill if (!ip || (opc == insn_daddiu && r4k_daddiu_bug())) 205abc597feSSteven J. Hill panic("Unsupported Micro-assembler instruction %d", opc); 206abc597feSSteven J. Hill 207abc597feSSteven J. Hill op = ip->match; 208abc597feSSteven J. Hill va_start(ap, opc); 209abc597feSSteven J. Hill if (ip->fields & RS) 210abc597feSSteven J. Hill op |= build_rs(va_arg(ap, u32)); 211abc597feSSteven J. Hill if (ip->fields & RT) 212abc597feSSteven J. Hill op |= build_rt(va_arg(ap, u32)); 213abc597feSSteven J. Hill if (ip->fields & RD) 214abc597feSSteven J. Hill op |= build_rd(va_arg(ap, u32)); 215abc597feSSteven J. Hill if (ip->fields & RE) 216abc597feSSteven J. Hill op |= build_re(va_arg(ap, u32)); 217abc597feSSteven J. Hill if (ip->fields & SIMM) 218abc597feSSteven J. Hill op |= build_simm(va_arg(ap, s32)); 219abc597feSSteven J. Hill if (ip->fields & UIMM) 220abc597feSSteven J. Hill op |= build_uimm(va_arg(ap, u32)); 221abc597feSSteven J. Hill if (ip->fields & BIMM) 222abc597feSSteven J. Hill op |= build_bimm(va_arg(ap, s32)); 223abc597feSSteven J. Hill if (ip->fields & JIMM) 224abc597feSSteven J. Hill op |= build_jimm(va_arg(ap, u32)); 225abc597feSSteven J. Hill if (ip->fields & FUNC) 226abc597feSSteven J. Hill op |= build_func(va_arg(ap, u32)); 227abc597feSSteven J. Hill if (ip->fields & SET) 228abc597feSSteven J. Hill op |= build_set(va_arg(ap, u32)); 229abc597feSSteven J. Hill if (ip->fields & SCIMM) 230abc597feSSteven J. Hill op |= build_scimm(va_arg(ap, u32)); 231*a168b8f1SLeonid Yegoshin if (ip->fields & SIMM9) 232*a168b8f1SLeonid Yegoshin op |= build_scimm9(va_arg(ap, u32)); 233abc597feSSteven J. Hill va_end(ap); 234abc597feSSteven J. Hill 235abc597feSSteven J. Hill **buf = op; 236abc597feSSteven J. Hill (*buf)++; 237abc597feSSteven J. Hill } 238abc597feSSteven J. Hill 239078a55fcSPaul Gortmaker static inline void 240abc597feSSteven J. Hill __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 241abc597feSSteven J. Hill { 242abc597feSSteven J. Hill long laddr = (long)lab->addr; 243abc597feSSteven J. Hill long raddr = (long)rel->addr; 244abc597feSSteven J. Hill 245abc597feSSteven J. Hill switch (rel->type) { 246abc597feSSteven J. Hill case R_MIPS_PC16: 247abc597feSSteven J. Hill *rel->addr |= build_bimm(laddr - (raddr + 4)); 248abc597feSSteven J. Hill break; 249abc597feSSteven J. Hill 250abc597feSSteven J. Hill default: 251abc597feSSteven J. Hill panic("Unsupported Micro-assembler relocation %d", 252abc597feSSteven J. Hill rel->type); 253abc597feSSteven J. Hill } 254abc597feSSteven J. Hill } 255