1a6a4834cSSteven J. Hill /* 2a6a4834cSSteven J. Hill * This file is subject to the terms and conditions of the GNU General Public 3a6a4834cSSteven J. Hill * License. See the file "COPYING" in the main directory of this archive 4a6a4834cSSteven J. Hill * for more details. 5a6a4834cSSteven J. Hill * 6a6a4834cSSteven J. Hill * A small micro-assembler. It is intentionally kept simple, does only 7a6a4834cSSteven J. Hill * support a subset of instructions, and does not try to hide pipeline 8a6a4834cSSteven J. Hill * effects like branch delay slots. 9a6a4834cSSteven J. Hill * 10a6a4834cSSteven J. Hill * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer 11a6a4834cSSteven J. Hill * Copyright (C) 2005, 2007 Maciej W. Rozycki 12a6a4834cSSteven J. Hill * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org) 13a6a4834cSSteven J. Hill * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved. 14a6a4834cSSteven J. Hill */ 15a6a4834cSSteven J. Hill 16a6a4834cSSteven J. Hill #include <linux/kernel.h> 17a6a4834cSSteven J. Hill #include <linux/types.h> 18a6a4834cSSteven J. Hill 19a6a4834cSSteven J. Hill #include <asm/inst.h> 20a6a4834cSSteven J. Hill #include <asm/elf.h> 21a6a4834cSSteven J. Hill #include <asm/bugs.h> 22a6a4834cSSteven J. Hill #define UASM_ISA _UASM_ISA_MICROMIPS 23a6a4834cSSteven J. Hill #include <asm/uasm.h> 24a6a4834cSSteven J. Hill 25a6a4834cSSteven J. Hill #define RS_MASK 0x1f 26a6a4834cSSteven J. Hill #define RS_SH 16 27a6a4834cSSteven J. Hill #define RT_MASK 0x1f 28a6a4834cSSteven J. Hill #define RT_SH 21 29a6a4834cSSteven J. Hill #define SCIMM_MASK 0x3ff 30a6a4834cSSteven J. Hill #define SCIMM_SH 16 31a6a4834cSSteven J. Hill 32a6a4834cSSteven J. Hill /* This macro sets the non-variable bits of an instruction. */ 33a6a4834cSSteven J. Hill #define M(a, b, c, d, e, f) \ 34a6a4834cSSteven J. Hill ((a) << OP_SH \ 35a6a4834cSSteven J. Hill | (b) << RT_SH \ 36a6a4834cSSteven J. Hill | (c) << RS_SH \ 37a6a4834cSSteven J. Hill | (d) << RD_SH \ 38a6a4834cSSteven J. Hill | (e) << RE_SH \ 39a6a4834cSSteven J. Hill | (f) << FUNC_SH) 40a6a4834cSSteven J. Hill 41a6a4834cSSteven J. Hill #include "uasm.c" 42a6a4834cSSteven J. Hill 43ce807d5fSDavid Daney static const struct insn const insn_table_MM[insn_invalid] = { 44ce807d5fSDavid Daney [insn_addu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_addu32_op), RT | RS | RD}, 45ce807d5fSDavid Daney [insn_addiu] = {M(mm_addiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 46ce807d5fSDavid Daney [insn_and] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_and_op), RT | RS | RD}, 47ce807d5fSDavid Daney [insn_andi] = {M(mm_andi32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 48ce807d5fSDavid Daney [insn_beq] = {M(mm_beq32_op, 0, 0, 0, 0, 0), RS | RT | BIMM}, 49ce807d5fSDavid Daney [insn_beql] = {0, 0}, 50ce807d5fSDavid Daney [insn_bgez] = {M(mm_pool32i_op, mm_bgez_op, 0, 0, 0, 0), RS | BIMM}, 51ce807d5fSDavid Daney [insn_bgezl] = {0, 0}, 52ce807d5fSDavid Daney [insn_bltz] = {M(mm_pool32i_op, mm_bltz_op, 0, 0, 0, 0), RS | BIMM}, 53ce807d5fSDavid Daney [insn_bltzl] = {0, 0}, 54ce807d5fSDavid Daney [insn_bne] = {M(mm_bne32_op, 0, 0, 0, 0, 0), RT | RS | BIMM}, 55ce807d5fSDavid Daney [insn_cache] = {M(mm_pool32b_op, 0, 0, mm_cache_func, 0, 0), RT | RS | SIMM}, 56ce807d5fSDavid Daney [insn_cfc1] = {M(mm_pool32f_op, 0, 0, 0, mm_cfc1_op, mm_32f_73_op), RT | RS}, 57ce807d5fSDavid Daney [insn_cfcmsa] = {M(mm_pool32s_op, 0, msa_cfc_op, 0, 0, mm_32s_elm_op), RD | RE}, 58ce807d5fSDavid Daney [insn_ctc1] = {M(mm_pool32f_op, 0, 0, 0, mm_ctc1_op, mm_32f_73_op), RT | RS}, 59ce807d5fSDavid Daney [insn_ctcmsa] = {M(mm_pool32s_op, 0, msa_ctc_op, 0, 0, mm_32s_elm_op), RD | RE}, 60ce807d5fSDavid Daney [insn_daddu] = {0, 0}, 61ce807d5fSDavid Daney [insn_daddiu] = {0, 0}, 62ce807d5fSDavid Daney [insn_di] = {M(mm_pool32a_op, 0, 0, 0, mm_di_op, mm_pool32axf_op), RS}, 63ce807d5fSDavid Daney [insn_divu] = {M(mm_pool32a_op, 0, 0, 0, mm_divu_op, mm_pool32axf_op), RT | RS}, 64ce807d5fSDavid Daney [insn_dmfc0] = {0, 0}, 65ce807d5fSDavid Daney [insn_dmtc0] = {0, 0}, 66ce807d5fSDavid Daney [insn_dsll] = {0, 0}, 67ce807d5fSDavid Daney [insn_dsll32] = {0, 0}, 68ce807d5fSDavid Daney [insn_dsra] = {0, 0}, 69ce807d5fSDavid Daney [insn_dsrl] = {0, 0}, 70ce807d5fSDavid Daney [insn_dsrl32] = {0, 0}, 71ce807d5fSDavid Daney [insn_drotr] = {0, 0}, 72ce807d5fSDavid Daney [insn_drotr32] = {0, 0}, 73ce807d5fSDavid Daney [insn_dsubu] = {0, 0}, 74ce807d5fSDavid Daney [insn_eret] = {M(mm_pool32a_op, 0, 0, 0, mm_eret_op, mm_pool32axf_op), 0}, 75ce807d5fSDavid Daney [insn_ins] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_ins_op), RT | RS | RD | RE}, 76ce807d5fSDavid Daney [insn_ext] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_ext_op), RT | RS | RD | RE}, 77ce807d5fSDavid Daney [insn_j] = {M(mm_j32_op, 0, 0, 0, 0, 0), JIMM}, 78ce807d5fSDavid Daney [insn_jal] = {M(mm_jal32_op, 0, 0, 0, 0, 0), JIMM}, 79ce807d5fSDavid Daney [insn_jalr] = {M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RT | RS}, 80ce807d5fSDavid Daney [insn_jr] = {M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RS}, 81ce807d5fSDavid Daney [insn_lb] = {M(mm_lb32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 82ce807d5fSDavid Daney [insn_ld] = {0, 0}, 83*77238e76SGustavo A. R. Silva [insn_lh] = {M(mm_lh32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 84ce807d5fSDavid Daney [insn_ll] = {M(mm_pool32c_op, 0, 0, (mm_ll_func << 1), 0, 0), RS | RT | SIMM}, 85ce807d5fSDavid Daney [insn_lld] = {0, 0}, 86ce807d5fSDavid Daney [insn_lui] = {M(mm_pool32i_op, mm_lui_op, 0, 0, 0, 0), RS | SIMM}, 87ce807d5fSDavid Daney [insn_lw] = {M(mm_lw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 88ce807d5fSDavid Daney [insn_mfc0] = {M(mm_pool32a_op, 0, 0, 0, mm_mfc0_op, mm_pool32axf_op), RT | RS | RD}, 89ce807d5fSDavid Daney [insn_mfhi] = {M(mm_pool32a_op, 0, 0, 0, mm_mfhi32_op, mm_pool32axf_op), RS}, 90ce807d5fSDavid Daney [insn_mflo] = {M(mm_pool32a_op, 0, 0, 0, mm_mflo32_op, mm_pool32axf_op), RS}, 91ce807d5fSDavid Daney [insn_mtc0] = {M(mm_pool32a_op, 0, 0, 0, mm_mtc0_op, mm_pool32axf_op), RT | RS | RD}, 92ce807d5fSDavid Daney [insn_mthi] = {M(mm_pool32a_op, 0, 0, 0, mm_mthi32_op, mm_pool32axf_op), RS}, 93ce807d5fSDavid Daney [insn_mtlo] = {M(mm_pool32a_op, 0, 0, 0, mm_mtlo32_op, mm_pool32axf_op), RS}, 94ce807d5fSDavid Daney [insn_mul] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_mul_op), RT | RS | RD}, 95ce807d5fSDavid Daney [insn_or] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_or32_op), RT | RS | RD}, 96ce807d5fSDavid Daney [insn_ori] = {M(mm_ori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 97ce807d5fSDavid Daney [insn_pref] = {M(mm_pool32c_op, 0, 0, (mm_pref_func << 1), 0, 0), RT | RS | SIMM}, 98ce807d5fSDavid Daney [insn_rfe] = {0, 0}, 99ce807d5fSDavid Daney [insn_sc] = {M(mm_pool32c_op, 0, 0, (mm_sc_func << 1), 0, 0), RT | RS | SIMM}, 100ce807d5fSDavid Daney [insn_scd] = {0, 0}, 101ce807d5fSDavid Daney [insn_sd] = {0, 0}, 102ce807d5fSDavid Daney [insn_sll] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sll32_op), RT | RS | RD}, 103ce807d5fSDavid Daney [insn_sllv] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sllv32_op), RT | RS | RD}, 104ce807d5fSDavid Daney [insn_slt] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_slt_op), RT | RS | RD}, 105ce807d5fSDavid Daney [insn_sltiu] = {M(mm_sltiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 106ce807d5fSDavid Daney [insn_sltu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sltu_op), RT | RS | RD}, 107ce807d5fSDavid Daney [insn_sra] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sra_op), RT | RS | RD}, 108ce807d5fSDavid Daney [insn_srl] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srl32_op), RT | RS | RD}, 109ce807d5fSDavid Daney [insn_srlv] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srlv32_op), RT | RS | RD}, 110ce807d5fSDavid Daney [insn_rotr] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_rotr_op), RT | RS | RD}, 111ce807d5fSDavid Daney [insn_subu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_subu32_op), RT | RS | RD}, 112ce807d5fSDavid Daney [insn_sw] = {M(mm_sw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 113ce807d5fSDavid Daney [insn_sync] = {M(mm_pool32a_op, 0, 0, 0, mm_sync_op, mm_pool32axf_op), RS}, 114ce807d5fSDavid Daney [insn_tlbp] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbp_op, mm_pool32axf_op), 0}, 115ce807d5fSDavid Daney [insn_tlbr] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbr_op, mm_pool32axf_op), 0}, 116ce807d5fSDavid Daney [insn_tlbwi] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbwi_op, mm_pool32axf_op), 0}, 117ce807d5fSDavid Daney [insn_tlbwr] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbwr_op, mm_pool32axf_op), 0}, 118ce807d5fSDavid Daney [insn_wait] = {M(mm_pool32a_op, 0, 0, 0, mm_wait_op, mm_pool32axf_op), SCIMM}, 119ce807d5fSDavid Daney [insn_wsbh] = {M(mm_pool32a_op, 0, 0, 0, mm_wsbh_op, mm_pool32axf_op), RT | RS}, 120ce807d5fSDavid Daney [insn_xor] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_xor32_op), RT | RS | RD}, 121ce807d5fSDavid Daney [insn_xori] = {M(mm_xori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 122ce807d5fSDavid Daney [insn_dins] = {0, 0}, 123ce807d5fSDavid Daney [insn_dinsm] = {0, 0}, 124ce807d5fSDavid Daney [insn_syscall] = {M(mm_pool32a_op, 0, 0, 0, mm_syscall_op, mm_pool32axf_op), SCIMM}, 125ce807d5fSDavid Daney [insn_bbit0] = {0, 0}, 126ce807d5fSDavid Daney [insn_bbit1] = {0, 0}, 127ce807d5fSDavid Daney [insn_lwx] = {0, 0}, 128ce807d5fSDavid Daney [insn_ldx] = {0, 0}, 129a6a4834cSSteven J. Hill }; 130a6a4834cSSteven J. Hill 131a6a4834cSSteven J. Hill #undef M 132a6a4834cSSteven J. Hill 133078a55fcSPaul Gortmaker static inline u32 build_bimm(s32 arg) 134a6a4834cSSteven J. Hill { 135a6a4834cSSteven J. Hill WARN(arg > 0xffff || arg < -0x10000, 136a6a4834cSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 137a6a4834cSSteven J. Hill 138a6a4834cSSteven J. Hill WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 139a6a4834cSSteven J. Hill 140a6a4834cSSteven J. Hill return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 1) & 0x7fff); 141a6a4834cSSteven J. Hill } 142a6a4834cSSteven J. Hill 143078a55fcSPaul Gortmaker static inline u32 build_jimm(u32 arg) 144a6a4834cSSteven J. Hill { 1458fe4bb98SSteven J. Hill 1468fe4bb98SSteven J. Hill WARN(arg & ~((JIMM_MASK << 2) | 1), 147a6a4834cSSteven J. Hill KERN_WARNING "Micro-assembler field overflow\n"); 148a6a4834cSSteven J. Hill 149a6a4834cSSteven J. Hill return (arg >> 1) & JIMM_MASK; 150a6a4834cSSteven J. Hill } 151a6a4834cSSteven J. Hill 152a6a4834cSSteven J. Hill /* 153a6a4834cSSteven J. Hill * The order of opcode arguments is implicitly left to right, 154a6a4834cSSteven J. Hill * starting with RS and ending with FUNC or IMM. 155a6a4834cSSteven J. Hill */ 156078a55fcSPaul Gortmaker static void build_insn(u32 **buf, enum opcode opc, ...) 157a6a4834cSSteven J. Hill { 158ce807d5fSDavid Daney const struct insn *ip; 159a6a4834cSSteven J. Hill va_list ap; 160a6a4834cSSteven J. Hill u32 op; 161a6a4834cSSteven J. Hill 162ce807d5fSDavid Daney if (opc < 0 || opc >= insn_invalid || 163ce807d5fSDavid Daney (opc == insn_daddiu && r4k_daddiu_bug()) || 164ce807d5fSDavid Daney (insn_table_MM[opc].match == 0 && insn_table_MM[opc].fields == 0)) 165a6a4834cSSteven J. Hill panic("Unsupported Micro-assembler instruction %d", opc); 166a6a4834cSSteven J. Hill 167ce807d5fSDavid Daney ip = &insn_table_MM[opc]; 168ce807d5fSDavid Daney 169a6a4834cSSteven J. Hill op = ip->match; 170a6a4834cSSteven J. Hill va_start(ap, opc); 171a6a4834cSSteven J. Hill if (ip->fields & RS) { 172c29732a1SJames Hogan if (opc == insn_mfc0 || opc == insn_mtc0 || 173c29732a1SJames Hogan opc == insn_cfc1 || opc == insn_ctc1) 174a6a4834cSSteven J. Hill op |= build_rt(va_arg(ap, u32)); 175a6a4834cSSteven J. Hill else 176a6a4834cSSteven J. Hill op |= build_rs(va_arg(ap, u32)); 177a6a4834cSSteven J. Hill } 178a6a4834cSSteven J. Hill if (ip->fields & RT) { 179c29732a1SJames Hogan if (opc == insn_mfc0 || opc == insn_mtc0 || 180c29732a1SJames Hogan opc == insn_cfc1 || opc == insn_ctc1) 181a6a4834cSSteven J. Hill op |= build_rs(va_arg(ap, u32)); 182a6a4834cSSteven J. Hill else 183a6a4834cSSteven J. Hill op |= build_rt(va_arg(ap, u32)); 184a6a4834cSSteven J. Hill } 185a6a4834cSSteven J. Hill if (ip->fields & RD) 186a6a4834cSSteven J. Hill op |= build_rd(va_arg(ap, u32)); 187a6a4834cSSteven J. Hill if (ip->fields & RE) 188a6a4834cSSteven J. Hill op |= build_re(va_arg(ap, u32)); 189a6a4834cSSteven J. Hill if (ip->fields & SIMM) 190a6a4834cSSteven J. Hill op |= build_simm(va_arg(ap, s32)); 191a6a4834cSSteven J. Hill if (ip->fields & UIMM) 192a6a4834cSSteven J. Hill op |= build_uimm(va_arg(ap, u32)); 193a6a4834cSSteven J. Hill if (ip->fields & BIMM) 194a6a4834cSSteven J. Hill op |= build_bimm(va_arg(ap, s32)); 195a6a4834cSSteven J. Hill if (ip->fields & JIMM) 196a6a4834cSSteven J. Hill op |= build_jimm(va_arg(ap, u32)); 197a6a4834cSSteven J. Hill if (ip->fields & FUNC) 198a6a4834cSSteven J. Hill op |= build_func(va_arg(ap, u32)); 199a6a4834cSSteven J. Hill if (ip->fields & SET) 200a6a4834cSSteven J. Hill op |= build_set(va_arg(ap, u32)); 201a6a4834cSSteven J. Hill if (ip->fields & SCIMM) 202a6a4834cSSteven J. Hill op |= build_scimm(va_arg(ap, u32)); 203a6a4834cSSteven J. Hill va_end(ap); 204a6a4834cSSteven J. Hill 205a6a4834cSSteven J. Hill #ifdef CONFIG_CPU_LITTLE_ENDIAN 206a6a4834cSSteven J. Hill **buf = ((op & 0xffff) << 16) | (op >> 16); 207a6a4834cSSteven J. Hill #else 208a6a4834cSSteven J. Hill **buf = op; 209a6a4834cSSteven J. Hill #endif 210a6a4834cSSteven J. Hill (*buf)++; 211a6a4834cSSteven J. Hill } 212a6a4834cSSteven J. Hill 213078a55fcSPaul Gortmaker static inline void 214a6a4834cSSteven J. Hill __resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 215a6a4834cSSteven J. Hill { 216a6a4834cSSteven J. Hill long laddr = (long)lab->addr; 217a6a4834cSSteven J. Hill long raddr = (long)rel->addr; 218a6a4834cSSteven J. Hill 219a6a4834cSSteven J. Hill switch (rel->type) { 220a6a4834cSSteven J. Hill case R_MIPS_PC16: 221a6a4834cSSteven J. Hill #ifdef CONFIG_CPU_LITTLE_ENDIAN 222a6a4834cSSteven J. Hill *rel->addr |= (build_bimm(laddr - (raddr + 4)) << 16); 223a6a4834cSSteven J. Hill #else 224a6a4834cSSteven J. Hill *rel->addr |= build_bimm(laddr - (raddr + 4)); 225a6a4834cSSteven J. Hill #endif 226a6a4834cSSteven J. Hill break; 227a6a4834cSSteven J. Hill 228a6a4834cSSteven J. Hill default: 229a6a4834cSSteven J. Hill panic("Unsupported Micro-assembler relocation %d", 230a6a4834cSSteven J. Hill rel->type); 231a6a4834cSSteven J. Hill } 232a6a4834cSSteven J. Hill } 233