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 }, 8982488818SMarkos Chandras { insn_lb, M(lb_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 90abc597feSSteven J. Hill { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 91abc597feSSteven J. Hill { insn_ldx, M(spec3_op, 0, 0, 0, ldx_op, lx_op), RS | RT | RD }, 9284c68cbcSMarkos Chandras { insn_lh, M(lh_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 93abc597feSSteven J. Hill { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 94abc597feSSteven J. Hill { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 95abc597feSSteven J. Hill { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM }, 96abc597feSSteven J. Hill { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 97abc597feSSteven J. Hill { insn_lwx, M(spec3_op, 0, 0, 0, lwx_op, lx_op), RS | RT | RD }, 98abc597feSSteven J. Hill { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET}, 99e2965cd0SSteven J. Hill { insn_mfhc0, M(cop0_op, mfhc0_op, 0, 0, 0, 0), RT | RD | SET}, 100f3ec7a23SMarkos Chandras { insn_mfhi, M(spec_op, 0, 0, 0, 0, mfhi_op), RD }, 10116d21a81SMarkos Chandras { insn_mflo, M(spec_op, 0, 0, 0, 0, mflo_op), RD }, 102abc597feSSteven J. Hill { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET}, 103e2965cd0SSteven J. Hill { insn_mthc0, M(cop0_op, mthc0_op, 0, 0, 0, 0), RT | RD | SET}, 104a8e897adSMarkos Chandras { insn_mul, M(spec2_op, 0, 0, 0, 0, mul_op), RS | RT | RD}, 105abc597feSSteven J. Hill { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 106abc597feSSteven J. Hill { insn_or, M(spec_op, 0, 0, 0, 0, or_op), RS | RT | RD }, 107abc597feSSteven J. Hill { insn_pref, M(pref_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 108abc597feSSteven J. Hill { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 }, 109abc597feSSteven J. Hill { insn_rotr, M(spec_op, 1, 0, 0, 0, srl_op), RT | RD | RE }, 110abc597feSSteven J. Hill { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 111abc597feSSteven J. Hill { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 112abc597feSSteven J. Hill { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 113abc597feSSteven J. Hill { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE }, 114bef581baSMarkos Chandras { insn_sllv, M(spec_op, 0, 0, 0, 0, sllv_op), RS | RT | RD }, 1157682f9e8SMarkos Chandras { insn_slt, M(spec_op, 0, 0, 0, 0, slt_op), RS | RT | RD }, 116390363edSMarkos Chandras { insn_sltiu, M(sltiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 117e8ef868bSMarkos Chandras { insn_sltu, M(spec_op, 0, 0, 0, 0, sltu_op), RS | RT | RD }, 118abc597feSSteven J. Hill { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE }, 119abc597feSSteven J. Hill { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE }, 120f31318fdSMarkos Chandras { insn_srlv, M(spec_op, 0, 0, 0, 0, srlv_op), RS | RT | RD }, 121abc597feSSteven J. Hill { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD }, 122abc597feSSteven J. Hill { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 123729ff561SPaul Burton { insn_sync, M(spec_op, 0, 0, 0, 0, sync_op), RE }, 124abc597feSSteven J. Hill { insn_syscall, M(spec_op, 0, 0, 0, 0, syscall_op), SCIMM}, 125abc597feSSteven J. Hill { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 }, 126abc597feSSteven J. Hill { insn_tlbr, M(cop0_op, cop_op, 0, 0, 0, tlbr_op), 0 }, 127abc597feSSteven J. Hill { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 }, 128abc597feSSteven J. Hill { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 }, 12953ed1389SPaul Burton { insn_wait, M(cop0_op, cop_op, 0, 0, 0, wait_op), SCIMM }, 130ab9e4fa0SMarkos Chandras { insn_wsbh, M(spec3_op, 0, 0, 0, wsbh_op, bshfl_op), RT | RD }, 131abc597feSSteven J. Hill { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 132abc597feSSteven J. Hill { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD }, 133d674dd14SPaul Burton { insn_yield, M(spec3_op, 0, 0, 0, 0, yield_op), RS | RD }, 134abc597feSSteven J. Hill { insn_invalid, 0, 0 } 135abc597feSSteven J. Hill }; 136abc597feSSteven J. Hill 137abc597feSSteven J. Hill #undef M 138abc597feSSteven J. Hill 139078a55fcSPaul Gortmaker static inline u32 build_bimm(s32 arg) 140abc597feSSteven J. Hill { 141abc597feSSteven J. Hill WARN(arg > 0x1ffff || arg < -0x20000, 142abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 143abc597feSSteven J. Hill 144abc597feSSteven J. Hill WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 145abc597feSSteven J. Hill 146abc597feSSteven J. Hill return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff); 147abc597feSSteven J. Hill } 148abc597feSSteven J. Hill 149078a55fcSPaul Gortmaker static inline u32 build_jimm(u32 arg) 150abc597feSSteven J. Hill { 151abc597feSSteven J. Hill WARN(arg & ~(JIMM_MASK << 2), 152abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 153abc597feSSteven J. Hill 154abc597feSSteven J. Hill return (arg >> 2) & JIMM_MASK; 155abc597feSSteven J. Hill } 156abc597feSSteven J. Hill 157abc597feSSteven J. Hill /* 158abc597feSSteven J. Hill * The order of opcode arguments is implicitly left to right, 159abc597feSSteven J. Hill * starting with RS and ending with FUNC or IMM. 160abc597feSSteven J. Hill */ 161078a55fcSPaul Gortmaker static void build_insn(u32 **buf, enum opcode opc, ...) 162abc597feSSteven J. Hill { 163abc597feSSteven J. Hill struct insn *ip = NULL; 164abc597feSSteven J. Hill unsigned int i; 165abc597feSSteven J. Hill va_list ap; 166abc597feSSteven J. Hill u32 op; 167abc597feSSteven J. Hill 168abc597feSSteven J. Hill for (i = 0; insn_table[i].opcode != insn_invalid; i++) 169abc597feSSteven J. Hill if (insn_table[i].opcode == opc) { 170abc597feSSteven J. Hill ip = &insn_table[i]; 171abc597feSSteven J. Hill break; 172abc597feSSteven J. Hill } 173abc597feSSteven J. Hill 174abc597feSSteven J. Hill if (!ip || (opc == insn_daddiu && r4k_daddiu_bug())) 175abc597feSSteven J. Hill panic("Unsupported Micro-assembler instruction %d", opc); 176abc597feSSteven J. Hill 177abc597feSSteven J. Hill op = ip->match; 178abc597feSSteven J. Hill va_start(ap, opc); 179abc597feSSteven J. Hill if (ip->fields & RS) 180abc597feSSteven J. Hill op |= build_rs(va_arg(ap, u32)); 181abc597feSSteven J. Hill if (ip->fields & RT) 182abc597feSSteven J. Hill op |= build_rt(va_arg(ap, u32)); 183abc597feSSteven J. Hill if (ip->fields & RD) 184abc597feSSteven J. Hill op |= build_rd(va_arg(ap, u32)); 185abc597feSSteven J. Hill if (ip->fields & RE) 186abc597feSSteven J. Hill op |= build_re(va_arg(ap, u32)); 187abc597feSSteven J. Hill if (ip->fields & SIMM) 188abc597feSSteven J. Hill op |= build_simm(va_arg(ap, s32)); 189abc597feSSteven J. Hill if (ip->fields & UIMM) 190abc597feSSteven J. Hill op |= build_uimm(va_arg(ap, u32)); 191abc597feSSteven J. Hill if (ip->fields & BIMM) 192abc597feSSteven J. Hill op |= build_bimm(va_arg(ap, s32)); 193abc597feSSteven J. Hill if (ip->fields & JIMM) 194abc597feSSteven J. Hill op |= build_jimm(va_arg(ap, u32)); 195abc597feSSteven J. Hill if (ip->fields & FUNC) 196abc597feSSteven J. Hill op |= build_func(va_arg(ap, u32)); 197abc597feSSteven J. Hill if (ip->fields & SET) 198abc597feSSteven J. Hill op |= build_set(va_arg(ap, u32)); 199abc597feSSteven J. Hill if (ip->fields & SCIMM) 200abc597feSSteven J. Hill op |= build_scimm(va_arg(ap, u32)); 201abc597feSSteven J. Hill va_end(ap); 202abc597feSSteven J. Hill 203abc597feSSteven J. Hill **buf = op; 204abc597feSSteven J. Hill (*buf)++; 205abc597feSSteven J. Hill } 206abc597feSSteven J. Hill 207078a55fcSPaul Gortmaker static inline void 208abc597feSSteven J. Hill __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 209abc597feSSteven J. Hill { 210abc597feSSteven J. Hill long laddr = (long)lab->addr; 211abc597feSSteven J. Hill long raddr = (long)rel->addr; 212abc597feSSteven J. Hill 213abc597feSSteven J. Hill switch (rel->type) { 214abc597feSSteven J. Hill case R_MIPS_PC16: 215abc597feSSteven J. Hill *rel->addr |= build_bimm(laddr - (raddr + 4)); 216abc597feSSteven J. Hill break; 217abc597feSSteven J. Hill 218abc597feSSteven J. Hill default: 219abc597feSSteven J. Hill panic("Unsupported Micro-assembler relocation %d", 220abc597feSSteven J. Hill rel->type); 221abc597feSSteven J. Hill } 222abc597feSSteven J. Hill } 223