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 }, 70abc597feSSteven J. Hill { insn_dmfc0, M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET}, 71abc597feSSteven J. Hill { insn_dmtc0, M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET}, 72abc597feSSteven J. Hill { insn_drotr32, M(spec_op, 1, 0, 0, 0, dsrl32_op), RT | RD | RE }, 73abc597feSSteven J. Hill { insn_drotr, M(spec_op, 1, 0, 0, 0, dsrl_op), RT | RD | RE }, 74abc597feSSteven J. Hill { insn_dsll32, M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE }, 75abc597feSSteven J. Hill { insn_dsll, M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE }, 76abc597feSSteven J. Hill { insn_dsra, M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE }, 77abc597feSSteven J. Hill { insn_dsrl32, M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE }, 78abc597feSSteven J. Hill { insn_dsrl, M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE }, 79abc597feSSteven J. Hill { insn_dsubu, M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD }, 80abc597feSSteven J. Hill { insn_eret, M(cop0_op, cop_op, 0, 0, 0, eret_op), 0 }, 81abc597feSSteven J. Hill { insn_ext, M(spec3_op, 0, 0, 0, 0, ext_op), RS | RT | RD | RE }, 82abc597feSSteven J. Hill { insn_ins, M(spec3_op, 0, 0, 0, 0, ins_op), RS | RT | RD | RE }, 83abc597feSSteven J. Hill { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM }, 84abc597feSSteven J. Hill { insn_jal, M(jal_op, 0, 0, 0, 0, 0), JIMM }, 8549e9529bSPaul Burton { insn_jalr, M(spec_op, 0, 0, 0, 0, jalr_op), RS | RD }, 86abc597feSSteven J. Hill { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM }, 87abc597feSSteven J. Hill { insn_jr, M(spec_op, 0, 0, 0, 0, jr_op), RS }, 88abc597feSSteven J. Hill { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 89abc597feSSteven J. Hill { insn_ldx, M(spec3_op, 0, 0, 0, ldx_op, lx_op), RS | RT | RD }, 90abc597feSSteven J. Hill { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 91abc597feSSteven J. Hill { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 92abc597feSSteven J. Hill { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM }, 93abc597feSSteven J. Hill { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 94abc597feSSteven J. Hill { insn_lwx, M(spec3_op, 0, 0, 0, lwx_op, lx_op), RS | RT | RD }, 95abc597feSSteven J. Hill { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET}, 96abc597feSSteven J. Hill { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET}, 97abc597feSSteven J. Hill { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 98abc597feSSteven J. Hill { insn_or, M(spec_op, 0, 0, 0, 0, or_op), RS | RT | RD }, 99abc597feSSteven J. Hill { insn_pref, M(pref_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 100abc597feSSteven J. Hill { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 }, 101abc597feSSteven J. Hill { insn_rotr, M(spec_op, 1, 0, 0, 0, srl_op), RT | RD | RE }, 102abc597feSSteven J. Hill { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 103abc597feSSteven J. Hill { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 104abc597feSSteven J. Hill { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 105abc597feSSteven J. Hill { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE }, 106abc597feSSteven J. Hill { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE }, 107abc597feSSteven J. Hill { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE }, 108abc597feSSteven J. Hill { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD }, 109abc597feSSteven J. Hill { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM }, 110729ff561SPaul Burton { insn_sync, M(spec_op, 0, 0, 0, 0, sync_op), RE }, 111abc597feSSteven J. Hill { insn_syscall, M(spec_op, 0, 0, 0, 0, syscall_op), SCIMM}, 112abc597feSSteven J. Hill { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 }, 113abc597feSSteven J. Hill { insn_tlbr, M(cop0_op, cop_op, 0, 0, 0, tlbr_op), 0 }, 114abc597feSSteven J. Hill { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 }, 115abc597feSSteven J. Hill { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 }, 116*53ed1389SPaul Burton { insn_wait, M(cop0_op, cop_op, 0, 0, 0, wait_op), SCIMM }, 117abc597feSSteven J. Hill { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM }, 118abc597feSSteven J. Hill { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD }, 119abc597feSSteven J. Hill { insn_invalid, 0, 0 } 120abc597feSSteven J. Hill }; 121abc597feSSteven J. Hill 122abc597feSSteven J. Hill #undef M 123abc597feSSteven J. Hill 124078a55fcSPaul Gortmaker static inline u32 build_bimm(s32 arg) 125abc597feSSteven J. Hill { 126abc597feSSteven J. Hill WARN(arg > 0x1ffff || arg < -0x20000, 127abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 128abc597feSSteven J. Hill 129abc597feSSteven J. Hill WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 130abc597feSSteven J. Hill 131abc597feSSteven J. Hill return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff); 132abc597feSSteven J. Hill } 133abc597feSSteven J. Hill 134078a55fcSPaul Gortmaker static inline u32 build_jimm(u32 arg) 135abc597feSSteven J. Hill { 136abc597feSSteven J. Hill WARN(arg & ~(JIMM_MASK << 2), 137abc597feSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 138abc597feSSteven J. Hill 139abc597feSSteven J. Hill return (arg >> 2) & JIMM_MASK; 140abc597feSSteven J. Hill } 141abc597feSSteven J. Hill 142abc597feSSteven J. Hill /* 143abc597feSSteven J. Hill * The order of opcode arguments is implicitly left to right, 144abc597feSSteven J. Hill * starting with RS and ending with FUNC or IMM. 145abc597feSSteven J. Hill */ 146078a55fcSPaul Gortmaker static void build_insn(u32 **buf, enum opcode opc, ...) 147abc597feSSteven J. Hill { 148abc597feSSteven J. Hill struct insn *ip = NULL; 149abc597feSSteven J. Hill unsigned int i; 150abc597feSSteven J. Hill va_list ap; 151abc597feSSteven J. Hill u32 op; 152abc597feSSteven J. Hill 153abc597feSSteven J. Hill for (i = 0; insn_table[i].opcode != insn_invalid; i++) 154abc597feSSteven J. Hill if (insn_table[i].opcode == opc) { 155abc597feSSteven J. Hill ip = &insn_table[i]; 156abc597feSSteven J. Hill break; 157abc597feSSteven J. Hill } 158abc597feSSteven J. Hill 159abc597feSSteven J. Hill if (!ip || (opc == insn_daddiu && r4k_daddiu_bug())) 160abc597feSSteven J. Hill panic("Unsupported Micro-assembler instruction %d", opc); 161abc597feSSteven J. Hill 162abc597feSSteven J. Hill op = ip->match; 163abc597feSSteven J. Hill va_start(ap, opc); 164abc597feSSteven J. Hill if (ip->fields & RS) 165abc597feSSteven J. Hill op |= build_rs(va_arg(ap, u32)); 166abc597feSSteven J. Hill if (ip->fields & RT) 167abc597feSSteven J. Hill op |= build_rt(va_arg(ap, u32)); 168abc597feSSteven J. Hill if (ip->fields & RD) 169abc597feSSteven J. Hill op |= build_rd(va_arg(ap, u32)); 170abc597feSSteven J. Hill if (ip->fields & RE) 171abc597feSSteven J. Hill op |= build_re(va_arg(ap, u32)); 172abc597feSSteven J. Hill if (ip->fields & SIMM) 173abc597feSSteven J. Hill op |= build_simm(va_arg(ap, s32)); 174abc597feSSteven J. Hill if (ip->fields & UIMM) 175abc597feSSteven J. Hill op |= build_uimm(va_arg(ap, u32)); 176abc597feSSteven J. Hill if (ip->fields & BIMM) 177abc597feSSteven J. Hill op |= build_bimm(va_arg(ap, s32)); 178abc597feSSteven J. Hill if (ip->fields & JIMM) 179abc597feSSteven J. Hill op |= build_jimm(va_arg(ap, u32)); 180abc597feSSteven J. Hill if (ip->fields & FUNC) 181abc597feSSteven J. Hill op |= build_func(va_arg(ap, u32)); 182abc597feSSteven J. Hill if (ip->fields & SET) 183abc597feSSteven J. Hill op |= build_set(va_arg(ap, u32)); 184abc597feSSteven J. Hill if (ip->fields & SCIMM) 185abc597feSSteven J. Hill op |= build_scimm(va_arg(ap, u32)); 186abc597feSSteven J. Hill va_end(ap); 187abc597feSSteven J. Hill 188abc597feSSteven J. Hill **buf = op; 189abc597feSSteven J. Hill (*buf)++; 190abc597feSSteven J. Hill } 191abc597feSSteven J. Hill 192078a55fcSPaul Gortmaker static inline void 193abc597feSSteven J. Hill __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 194abc597feSSteven J. Hill { 195abc597feSSteven J. Hill long laddr = (long)lab->addr; 196abc597feSSteven J. Hill long raddr = (long)rel->addr; 197abc597feSSteven J. Hill 198abc597feSSteven J. Hill switch (rel->type) { 199abc597feSSteven J. Hill case R_MIPS_PC16: 200abc597feSSteven J. Hill *rel->addr |= build_bimm(laddr - (raddr + 4)); 201abc597feSSteven J. Hill break; 202abc597feSSteven J. Hill 203abc597feSSteven J. Hill default: 204abc597feSSteven J. Hill panic("Unsupported Micro-assembler relocation %d", 205abc597feSSteven J. Hill rel->type); 206abc597feSSteven J. Hill } 207abc597feSSteven J. Hill } 208