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 41cf6d9058SSteven J. Hill /* Define these when we are not the ISA the kernel is being compiled with. */ 42cf6d9058SSteven J. Hill #ifdef CONFIG_CPU_MICROMIPS 43cf6d9058SSteven J. Hill #define CL_uasm_i_b(buf, off) ISAOPC(_beq)(buf, 0, 0, off) 44cf6d9058SSteven J. Hill #define CL_uasm_i_beqz(buf, rs, off) ISAOPC(_beq)(buf, rs, 0, off) 45cf6d9058SSteven J. Hill #define CL_uasm_i_beqzl(buf, rs, off) ISAOPC(_beql)(buf, rs, 0, off) 46cf6d9058SSteven J. Hill #define CL_uasm_i_bnez(buf, rs, off) ISAOPC(_bne)(buf, rs, 0, off) 47cf6d9058SSteven J. Hill #endif 48cf6d9058SSteven J. Hill 49abc597feSSteven J. Hill #include "uasm.c" 50abc597feSSteven J. Hill 51078a55fcSPaul Gortmaker static struct insn insn_table[] = { 52abc597feSSteven J. Hill { insn_addiu, M(addiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 53abc597feSSteven J. Hill { insn_addu, M(spec_op, 0, 0, 0, 0, addu_op), RS | RT | RD }, 54abc597feSSteven J. Hill { insn_andi, M(andi_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 55abc597feSSteven J. Hill { insn_and, M(spec_op, 0, 0, 0, 0, and_op), RS | RT | RD }, 56abc597feSSteven J. Hill { insn_bbit0, M(lwc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 57abc597feSSteven J. Hill { insn_bbit1, M(swc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 58abc597feSSteven J. Hill { insn_beql, M(beql_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 59abc597feSSteven J. Hill { insn_beq, M(beq_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 60abc597feSSteven J. Hill { insn_bgezl, M(bcond_op, 0, bgezl_op, 0, 0, 0), RS | BIMM }, 61abc597feSSteven J. Hill { insn_bgez, M(bcond_op, 0, bgez_op, 0, 0, 0), RS | BIMM }, 62abc597feSSteven J. Hill { insn_bltzl, M(bcond_op, 0, bltzl_op, 0, 0, 0), RS | BIMM }, 63abc597feSSteven J. Hill { insn_bltz, M(bcond_op, 0, bltz_op, 0, 0, 0), RS | BIMM }, 64abc597feSSteven J. Hill { insn_bne, M(bne_op, 0, 0, 0, 0, 0), RS | RT | BIMM }, 65abc597feSSteven J. Hill { insn_cache, M(cache_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 66abc597feSSteven J. Hill { insn_daddiu, M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 67abc597feSSteven J. Hill { insn_daddu, M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD }, 68abc597feSSteven J. Hill { insn_dinsm, M(spec3_op, 0, 0, 0, 0, dinsm_op), RS | RT | RD | RE }, 69abc597feSSteven J. Hill { insn_dins, M(spec3_op, 0, 0, 0, 0, dins_op), RS | RT | RD | RE }, 704c12a854SMarkos Chandras { insn_divu, M(spec_op, 0, 0, 0, 0, divu_op), RS | RT }, 71abc597feSSteven J. Hill { insn_dmfc0, M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET}, 72abc597feSSteven J. Hill { insn_dmtc0, M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET}, 73abc597feSSteven J. Hill { insn_drotr32, M(spec_op, 1, 0, 0, 0, dsrl32_op), RT | RD | RE }, 74abc597feSSteven J. Hill { insn_drotr, M(spec_op, 1, 0, 0, 0, dsrl_op), RT | RD | RE }, 75abc597feSSteven J. Hill { insn_dsll32, M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE }, 76abc597feSSteven J. Hill { insn_dsll, M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE }, 77abc597feSSteven J. Hill { insn_dsra, M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE }, 78abc597feSSteven J. Hill { insn_dsrl32, M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE }, 79abc597feSSteven J. Hill { insn_dsrl, M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE }, 80abc597feSSteven J. Hill { insn_dsubu, M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD }, 81abc597feSSteven J. Hill { insn_eret, M(cop0_op, cop_op, 0, 0, 0, eret_op), 0 }, 82abc597feSSteven J. Hill { insn_ext, M(spec3_op, 0, 0, 0, 0, ext_op), RS | RT | RD | RE }, 83abc597feSSteven J. Hill { insn_ins, M(spec3_op, 0, 0, 0, 0, ins_op), RS | RT | RD | RE }, 84abc597feSSteven J. Hill { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM }, 85abc597feSSteven J. Hill { insn_jal, M(jal_op, 0, 0, 0, 0, 0), JIMM }, 8649e9529bSPaul Burton { insn_jalr, M(spec_op, 0, 0, 0, 0, jalr_op), RS | RD }, 87abc597feSSteven J. Hill { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM }, 88abc597feSSteven J. Hill { insn_jr, M(spec_op, 0, 0, 0, 0, jr_op), RS }, 89abc597feSSteven J. Hill { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 90abc597feSSteven J. Hill { insn_ldx, M(spec3_op, 0, 0, 0, ldx_op, lx_op), RS | RT | RD }, 91abc597feSSteven J. Hill { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 92abc597feSSteven J. Hill { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 93abc597feSSteven J. Hill { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM }, 94abc597feSSteven J. Hill { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 95abc597feSSteven J. Hill { insn_lwx, M(spec3_op, 0, 0, 0, lwx_op, lx_op), RS | RT | RD }, 96abc597feSSteven J. Hill { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET}, 97f3ec7a23SMarkos Chandras { insn_mfhi, M(spec_op, 0, 0, 0, 0, mfhi_op), RD }, 98abc597feSSteven J. Hill { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET}, 99abc597feSSteven J. Hill { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 100abc597feSSteven J. Hill { insn_or, M(spec_op, 0, 0, 0, 0, or_op), RS | RT | RD }, 101abc597feSSteven J. Hill { insn_pref, M(pref_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 102abc597feSSteven J. Hill { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 }, 103abc597feSSteven J. Hill { insn_rotr, M(spec_op, 1, 0, 0, 0, srl_op), RT | RD | RE }, 104abc597feSSteven J. Hill { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 105abc597feSSteven J. Hill { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 106abc597feSSteven J. Hill { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 107abc597feSSteven J. Hill { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE }, 108bef581baSMarkos Chandras { insn_sllv, M(spec_op, 0, 0, 0, 0, sllv_op), RS | RT | RD }, 109abc597feSSteven J. Hill { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE }, 110abc597feSSteven J. Hill { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE }, 111f31318fdSMarkos Chandras { insn_srlv, M(spec_op, 0, 0, 0, 0, srlv_op), RS | RT | RD }, 112abc597feSSteven J. Hill { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD }, 113abc597feSSteven J. Hill { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 114729ff561SPaul Burton { insn_sync, M(spec_op, 0, 0, 0, 0, sync_op), RE }, 115abc597feSSteven J. Hill { insn_syscall, M(spec_op, 0, 0, 0, 0, syscall_op), SCIMM}, 116abc597feSSteven J. Hill { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 }, 117abc597feSSteven J. Hill { insn_tlbr, M(cop0_op, cop_op, 0, 0, 0, tlbr_op), 0 }, 118abc597feSSteven J. Hill { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 }, 119abc597feSSteven J. Hill { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 }, 12053ed1389SPaul Burton { insn_wait, M(cop0_op, cop_op, 0, 0, 0, wait_op), SCIMM }, 121abc597feSSteven J. Hill { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 122abc597feSSteven J. Hill { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD }, 123d674dd14SPaul Burton { insn_yield, M(spec3_op, 0, 0, 0, 0, yield_op), RS | RD }, 124abc597feSSteven J. Hill { insn_invalid, 0, 0 } 125abc597feSSteven J. Hill }; 126abc597feSSteven J. Hill 127abc597feSSteven J. Hill #undef M 128abc597feSSteven J. Hill 129078a55fcSPaul Gortmaker static inline u32 build_bimm(s32 arg) 130abc597feSSteven J. Hill { 131abc597feSSteven J. Hill WARN(arg > 0x1ffff || arg < -0x20000, 132abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 133abc597feSSteven J. Hill 134abc597feSSteven J. Hill WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 135abc597feSSteven J. Hill 136abc597feSSteven J. Hill return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff); 137abc597feSSteven J. Hill } 138abc597feSSteven J. Hill 139078a55fcSPaul Gortmaker static inline u32 build_jimm(u32 arg) 140abc597feSSteven J. Hill { 141abc597feSSteven J. Hill WARN(arg & ~(JIMM_MASK << 2), 142abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 143abc597feSSteven J. Hill 144abc597feSSteven J. Hill return (arg >> 2) & JIMM_MASK; 145abc597feSSteven J. Hill } 146abc597feSSteven J. Hill 147abc597feSSteven J. Hill /* 148abc597feSSteven J. Hill * The order of opcode arguments is implicitly left to right, 149abc597feSSteven J. Hill * starting with RS and ending with FUNC or IMM. 150abc597feSSteven J. Hill */ 151078a55fcSPaul Gortmaker static void build_insn(u32 **buf, enum opcode opc, ...) 152abc597feSSteven J. Hill { 153abc597feSSteven J. Hill struct insn *ip = NULL; 154abc597feSSteven J. Hill unsigned int i; 155abc597feSSteven J. Hill va_list ap; 156abc597feSSteven J. Hill u32 op; 157abc597feSSteven J. Hill 158abc597feSSteven J. Hill for (i = 0; insn_table[i].opcode != insn_invalid; i++) 159abc597feSSteven J. Hill if (insn_table[i].opcode == opc) { 160abc597feSSteven J. Hill ip = &insn_table[i]; 161abc597feSSteven J. Hill break; 162abc597feSSteven J. Hill } 163abc597feSSteven J. Hill 164abc597feSSteven J. Hill if (!ip || (opc == insn_daddiu && r4k_daddiu_bug())) 165abc597feSSteven J. Hill panic("Unsupported Micro-assembler instruction %d", opc); 166abc597feSSteven J. Hill 167abc597feSSteven J. Hill op = ip->match; 168abc597feSSteven J. Hill va_start(ap, opc); 169abc597feSSteven J. Hill if (ip->fields & RS) 170abc597feSSteven J. Hill op |= build_rs(va_arg(ap, u32)); 171abc597feSSteven J. Hill if (ip->fields & RT) 172abc597feSSteven J. Hill op |= build_rt(va_arg(ap, u32)); 173abc597feSSteven J. Hill if (ip->fields & RD) 174abc597feSSteven J. Hill op |= build_rd(va_arg(ap, u32)); 175abc597feSSteven J. Hill if (ip->fields & RE) 176abc597feSSteven J. Hill op |= build_re(va_arg(ap, u32)); 177abc597feSSteven J. Hill if (ip->fields & SIMM) 178abc597feSSteven J. Hill op |= build_simm(va_arg(ap, s32)); 179abc597feSSteven J. Hill if (ip->fields & UIMM) 180abc597feSSteven J. Hill op |= build_uimm(va_arg(ap, u32)); 181abc597feSSteven J. Hill if (ip->fields & BIMM) 182abc597feSSteven J. Hill op |= build_bimm(va_arg(ap, s32)); 183abc597feSSteven J. Hill if (ip->fields & JIMM) 184abc597feSSteven J. Hill op |= build_jimm(va_arg(ap, u32)); 185abc597feSSteven J. Hill if (ip->fields & FUNC) 186abc597feSSteven J. Hill op |= build_func(va_arg(ap, u32)); 187abc597feSSteven J. Hill if (ip->fields & SET) 188abc597feSSteven J. Hill op |= build_set(va_arg(ap, u32)); 189abc597feSSteven J. Hill if (ip->fields & SCIMM) 190abc597feSSteven J. Hill op |= build_scimm(va_arg(ap, u32)); 191abc597feSSteven J. Hill va_end(ap); 192abc597feSSteven J. Hill 193abc597feSSteven J. Hill **buf = op; 194abc597feSSteven J. Hill (*buf)++; 195abc597feSSteven J. Hill } 196abc597feSSteven J. Hill 197078a55fcSPaul Gortmaker static inline void 198abc597feSSteven J. Hill __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 199abc597feSSteven J. Hill { 200abc597feSSteven J. Hill long laddr = (long)lab->addr; 201abc597feSSteven J. Hill long raddr = (long)rel->addr; 202abc597feSSteven J. Hill 203abc597feSSteven J. Hill switch (rel->type) { 204abc597feSSteven J. Hill case R_MIPS_PC16: 205abc597feSSteven J. Hill *rel->addr |= build_bimm(laddr - (raddr + 4)); 206abc597feSSteven J. Hill break; 207abc597feSSteven J. Hill 208abc597feSSteven J. Hill default: 209abc597feSSteven J. Hill panic("Unsupported Micro-assembler relocation %d", 210abc597feSSteven J. Hill rel->type); 211abc597feSSteven J. Hill } 212abc597feSSteven J. Hill } 213