162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public 362306a36Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive 462306a36Sopenharmony_ci * for more details. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * A small micro-assembler. It is intentionally kept simple, does only 762306a36Sopenharmony_ci * support a subset of instructions, and does not try to hide pipeline 862306a36Sopenharmony_ci * effects like branch delay slots. 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer 1162306a36Sopenharmony_ci * Copyright (C) 2005, 2007 Maciej W. Rozycki 1262306a36Sopenharmony_ci * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org) 1362306a36Sopenharmony_ci * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved. 1462306a36Sopenharmony_ci */ 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include <linux/kernel.h> 1762306a36Sopenharmony_ci#include <linux/types.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include <asm/inst.h> 2062306a36Sopenharmony_ci#include <asm/elf.h> 2162306a36Sopenharmony_ci#include <asm/bugs.h> 2262306a36Sopenharmony_ci#include <asm/uasm.h> 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#define RS_MASK 0x1f 2562306a36Sopenharmony_ci#define RS_SH 16 2662306a36Sopenharmony_ci#define RT_MASK 0x1f 2762306a36Sopenharmony_ci#define RT_SH 21 2862306a36Sopenharmony_ci#define SCIMM_MASK 0x3ff 2962306a36Sopenharmony_ci#define SCIMM_SH 16 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* This macro sets the non-variable bits of an instruction. */ 3262306a36Sopenharmony_ci#define M(a, b, c, d, e, f) \ 3362306a36Sopenharmony_ci ((a) << OP_SH \ 3462306a36Sopenharmony_ci | (b) << RT_SH \ 3562306a36Sopenharmony_ci | (c) << RS_SH \ 3662306a36Sopenharmony_ci | (d) << RD_SH \ 3762306a36Sopenharmony_ci | (e) << RE_SH \ 3862306a36Sopenharmony_ci | (f) << FUNC_SH) 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci#include "uasm.c" 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cistatic const struct insn insn_table_MM[insn_invalid] = { 4362306a36Sopenharmony_ci [insn_addu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_addu32_op), RT | RS | RD}, 4462306a36Sopenharmony_ci [insn_addiu] = {M(mm_addiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 4562306a36Sopenharmony_ci [insn_and] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_and_op), RT | RS | RD}, 4662306a36Sopenharmony_ci [insn_andi] = {M(mm_andi32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 4762306a36Sopenharmony_ci [insn_beq] = {M(mm_beq32_op, 0, 0, 0, 0, 0), RS | RT | BIMM}, 4862306a36Sopenharmony_ci [insn_beql] = {0, 0}, 4962306a36Sopenharmony_ci [insn_bgez] = {M(mm_pool32i_op, mm_bgez_op, 0, 0, 0, 0), RS | BIMM}, 5062306a36Sopenharmony_ci [insn_bgezl] = {0, 0}, 5162306a36Sopenharmony_ci [insn_bltz] = {M(mm_pool32i_op, mm_bltz_op, 0, 0, 0, 0), RS | BIMM}, 5262306a36Sopenharmony_ci [insn_bltzl] = {0, 0}, 5362306a36Sopenharmony_ci [insn_bne] = {M(mm_bne32_op, 0, 0, 0, 0, 0), RT | RS | BIMM}, 5462306a36Sopenharmony_ci [insn_cache] = {M(mm_pool32b_op, 0, 0, mm_cache_func, 0, 0), RT | RS | SIMM}, 5562306a36Sopenharmony_ci [insn_cfc1] = {M(mm_pool32f_op, 0, 0, 0, mm_cfc1_op, mm_32f_73_op), RT | RS}, 5662306a36Sopenharmony_ci [insn_cfcmsa] = {M(mm_pool32s_op, 0, msa_cfc_op, 0, 0, mm_32s_elm_op), RD | RE}, 5762306a36Sopenharmony_ci [insn_ctc1] = {M(mm_pool32f_op, 0, 0, 0, mm_ctc1_op, mm_32f_73_op), RT | RS}, 5862306a36Sopenharmony_ci [insn_ctcmsa] = {M(mm_pool32s_op, 0, msa_ctc_op, 0, 0, mm_32s_elm_op), RD | RE}, 5962306a36Sopenharmony_ci [insn_daddu] = {0, 0}, 6062306a36Sopenharmony_ci [insn_daddiu] = {0, 0}, 6162306a36Sopenharmony_ci [insn_di] = {M(mm_pool32a_op, 0, 0, 0, mm_di_op, mm_pool32axf_op), RS}, 6262306a36Sopenharmony_ci [insn_divu] = {M(mm_pool32a_op, 0, 0, 0, mm_divu_op, mm_pool32axf_op), RT | RS}, 6362306a36Sopenharmony_ci [insn_dmfc0] = {0, 0}, 6462306a36Sopenharmony_ci [insn_dmtc0] = {0, 0}, 6562306a36Sopenharmony_ci [insn_dsll] = {0, 0}, 6662306a36Sopenharmony_ci [insn_dsll32] = {0, 0}, 6762306a36Sopenharmony_ci [insn_dsra] = {0, 0}, 6862306a36Sopenharmony_ci [insn_dsrl] = {0, 0}, 6962306a36Sopenharmony_ci [insn_dsrl32] = {0, 0}, 7062306a36Sopenharmony_ci [insn_drotr] = {0, 0}, 7162306a36Sopenharmony_ci [insn_drotr32] = {0, 0}, 7262306a36Sopenharmony_ci [insn_dsubu] = {0, 0}, 7362306a36Sopenharmony_ci [insn_eret] = {M(mm_pool32a_op, 0, 0, 0, mm_eret_op, mm_pool32axf_op), 0}, 7462306a36Sopenharmony_ci [insn_ins] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_ins_op), RT | RS | RD | RE}, 7562306a36Sopenharmony_ci [insn_ext] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_ext_op), RT | RS | RD | RE}, 7662306a36Sopenharmony_ci [insn_j] = {M(mm_j32_op, 0, 0, 0, 0, 0), JIMM}, 7762306a36Sopenharmony_ci [insn_jal] = {M(mm_jal32_op, 0, 0, 0, 0, 0), JIMM}, 7862306a36Sopenharmony_ci [insn_jalr] = {M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RT | RS}, 7962306a36Sopenharmony_ci [insn_jr] = {M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RS}, 8062306a36Sopenharmony_ci [insn_lb] = {M(mm_lb32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 8162306a36Sopenharmony_ci [insn_ld] = {0, 0}, 8262306a36Sopenharmony_ci [insn_lh] = {M(mm_lh32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 8362306a36Sopenharmony_ci [insn_ll] = {M(mm_pool32c_op, 0, 0, (mm_ll_func << 1), 0, 0), RS | RT | SIMM}, 8462306a36Sopenharmony_ci [insn_lld] = {0, 0}, 8562306a36Sopenharmony_ci [insn_lui] = {M(mm_pool32i_op, mm_lui_op, 0, 0, 0, 0), RS | SIMM}, 8662306a36Sopenharmony_ci [insn_lw] = {M(mm_lw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 8762306a36Sopenharmony_ci [insn_mfc0] = {M(mm_pool32a_op, 0, 0, 0, mm_mfc0_op, mm_pool32axf_op), RT | RS | RD}, 8862306a36Sopenharmony_ci [insn_mfhi] = {M(mm_pool32a_op, 0, 0, 0, mm_mfhi32_op, mm_pool32axf_op), RS}, 8962306a36Sopenharmony_ci [insn_mflo] = {M(mm_pool32a_op, 0, 0, 0, mm_mflo32_op, mm_pool32axf_op), RS}, 9062306a36Sopenharmony_ci [insn_mtc0] = {M(mm_pool32a_op, 0, 0, 0, mm_mtc0_op, mm_pool32axf_op), RT | RS | RD}, 9162306a36Sopenharmony_ci [insn_mthi] = {M(mm_pool32a_op, 0, 0, 0, mm_mthi32_op, mm_pool32axf_op), RS}, 9262306a36Sopenharmony_ci [insn_mtlo] = {M(mm_pool32a_op, 0, 0, 0, mm_mtlo32_op, mm_pool32axf_op), RS}, 9362306a36Sopenharmony_ci [insn_mul] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_mul_op), RT | RS | RD}, 9462306a36Sopenharmony_ci [insn_or] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_or32_op), RT | RS | RD}, 9562306a36Sopenharmony_ci [insn_ori] = {M(mm_ori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 9662306a36Sopenharmony_ci [insn_pref] = {M(mm_pool32c_op, 0, 0, (mm_pref_func << 1), 0, 0), RT | RS | SIMM}, 9762306a36Sopenharmony_ci [insn_rfe] = {0, 0}, 9862306a36Sopenharmony_ci [insn_sc] = {M(mm_pool32c_op, 0, 0, (mm_sc_func << 1), 0, 0), RT | RS | SIMM}, 9962306a36Sopenharmony_ci [insn_scd] = {0, 0}, 10062306a36Sopenharmony_ci [insn_sd] = {0, 0}, 10162306a36Sopenharmony_ci [insn_sll] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sll32_op), RT | RS | RD}, 10262306a36Sopenharmony_ci [insn_sllv] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sllv32_op), RT | RS | RD}, 10362306a36Sopenharmony_ci [insn_slt] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_slt_op), RT | RS | RD}, 10462306a36Sopenharmony_ci [insn_sltiu] = {M(mm_sltiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 10562306a36Sopenharmony_ci [insn_sltu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sltu_op), RT | RS | RD}, 10662306a36Sopenharmony_ci [insn_sra] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sra_op), RT | RS | RD}, 10762306a36Sopenharmony_ci [insn_srav] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srav_op), RT | RS | RD}, 10862306a36Sopenharmony_ci [insn_srl] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srl32_op), RT | RS | RD}, 10962306a36Sopenharmony_ci [insn_srlv] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srlv32_op), RT | RS | RD}, 11062306a36Sopenharmony_ci [insn_rotr] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_rotr_op), RT | RS | RD}, 11162306a36Sopenharmony_ci [insn_subu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_subu32_op), RT | RS | RD}, 11262306a36Sopenharmony_ci [insn_sw] = {M(mm_sw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 11362306a36Sopenharmony_ci [insn_sync] = {M(mm_pool32a_op, 0, 0, 0, mm_sync_op, mm_pool32axf_op), RS}, 11462306a36Sopenharmony_ci [insn_tlbp] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbp_op, mm_pool32axf_op), 0}, 11562306a36Sopenharmony_ci [insn_tlbr] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbr_op, mm_pool32axf_op), 0}, 11662306a36Sopenharmony_ci [insn_tlbwi] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbwi_op, mm_pool32axf_op), 0}, 11762306a36Sopenharmony_ci [insn_tlbwr] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbwr_op, mm_pool32axf_op), 0}, 11862306a36Sopenharmony_ci [insn_wait] = {M(mm_pool32a_op, 0, 0, 0, mm_wait_op, mm_pool32axf_op), SCIMM}, 11962306a36Sopenharmony_ci [insn_wsbh] = {M(mm_pool32a_op, 0, 0, 0, mm_wsbh_op, mm_pool32axf_op), RT | RS}, 12062306a36Sopenharmony_ci [insn_xor] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_xor32_op), RT | RS | RD}, 12162306a36Sopenharmony_ci [insn_xori] = {M(mm_xori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 12262306a36Sopenharmony_ci [insn_dins] = {0, 0}, 12362306a36Sopenharmony_ci [insn_dinsm] = {0, 0}, 12462306a36Sopenharmony_ci [insn_syscall] = {M(mm_pool32a_op, 0, 0, 0, mm_syscall_op, mm_pool32axf_op), SCIMM}, 12562306a36Sopenharmony_ci [insn_bbit0] = {0, 0}, 12662306a36Sopenharmony_ci [insn_bbit1] = {0, 0}, 12762306a36Sopenharmony_ci [insn_lwx] = {0, 0}, 12862306a36Sopenharmony_ci [insn_ldx] = {0, 0}, 12962306a36Sopenharmony_ci}; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci#undef M 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cistatic inline u32 build_bimm(s32 arg) 13462306a36Sopenharmony_ci{ 13562306a36Sopenharmony_ci WARN(arg > 0xffff || arg < -0x10000, 13662306a36Sopenharmony_ci KERN_WARNING "Micro-assembler field overflow\n"); 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 1) & 0x7fff); 14162306a36Sopenharmony_ci} 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_cistatic inline u32 build_jimm(u32 arg) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci WARN(arg & ~((JIMM_MASK << 2) | 1), 14762306a36Sopenharmony_ci KERN_WARNING "Micro-assembler field overflow\n"); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci return (arg >> 1) & JIMM_MASK; 15062306a36Sopenharmony_ci} 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci/* 15362306a36Sopenharmony_ci * The order of opcode arguments is implicitly left to right, 15462306a36Sopenharmony_ci * starting with RS and ending with FUNC or IMM. 15562306a36Sopenharmony_ci */ 15662306a36Sopenharmony_cistatic void build_insn(u32 **buf, enum opcode opc, ...) 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci const struct insn *ip; 15962306a36Sopenharmony_ci va_list ap; 16062306a36Sopenharmony_ci u32 op; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci if (opc < 0 || opc >= insn_invalid || 16362306a36Sopenharmony_ci (opc == insn_daddiu && r4k_daddiu_bug()) || 16462306a36Sopenharmony_ci (insn_table_MM[opc].match == 0 && insn_table_MM[opc].fields == 0)) 16562306a36Sopenharmony_ci panic("Unsupported Micro-assembler instruction %d", opc); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci ip = &insn_table_MM[opc]; 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci op = ip->match; 17062306a36Sopenharmony_ci va_start(ap, opc); 17162306a36Sopenharmony_ci if (ip->fields & RS) { 17262306a36Sopenharmony_ci if (opc == insn_mfc0 || opc == insn_mtc0 || 17362306a36Sopenharmony_ci opc == insn_cfc1 || opc == insn_ctc1) 17462306a36Sopenharmony_ci op |= build_rt(va_arg(ap, u32)); 17562306a36Sopenharmony_ci else 17662306a36Sopenharmony_ci op |= build_rs(va_arg(ap, u32)); 17762306a36Sopenharmony_ci } 17862306a36Sopenharmony_ci if (ip->fields & RT) { 17962306a36Sopenharmony_ci if (opc == insn_mfc0 || opc == insn_mtc0 || 18062306a36Sopenharmony_ci opc == insn_cfc1 || opc == insn_ctc1) 18162306a36Sopenharmony_ci op |= build_rs(va_arg(ap, u32)); 18262306a36Sopenharmony_ci else 18362306a36Sopenharmony_ci op |= build_rt(va_arg(ap, u32)); 18462306a36Sopenharmony_ci } 18562306a36Sopenharmony_ci if (ip->fields & RD) 18662306a36Sopenharmony_ci op |= build_rd(va_arg(ap, u32)); 18762306a36Sopenharmony_ci if (ip->fields & RE) 18862306a36Sopenharmony_ci op |= build_re(va_arg(ap, u32)); 18962306a36Sopenharmony_ci if (ip->fields & SIMM) 19062306a36Sopenharmony_ci op |= build_simm(va_arg(ap, s32)); 19162306a36Sopenharmony_ci if (ip->fields & UIMM) 19262306a36Sopenharmony_ci op |= build_uimm(va_arg(ap, u32)); 19362306a36Sopenharmony_ci if (ip->fields & BIMM) 19462306a36Sopenharmony_ci op |= build_bimm(va_arg(ap, s32)); 19562306a36Sopenharmony_ci if (ip->fields & JIMM) 19662306a36Sopenharmony_ci op |= build_jimm(va_arg(ap, u32)); 19762306a36Sopenharmony_ci if (ip->fields & FUNC) 19862306a36Sopenharmony_ci op |= build_func(va_arg(ap, u32)); 19962306a36Sopenharmony_ci if (ip->fields & SET) 20062306a36Sopenharmony_ci op |= build_set(va_arg(ap, u32)); 20162306a36Sopenharmony_ci if (ip->fields & SCIMM) 20262306a36Sopenharmony_ci op |= build_scimm(va_arg(ap, u32)); 20362306a36Sopenharmony_ci va_end(ap); 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci#ifdef CONFIG_CPU_LITTLE_ENDIAN 20662306a36Sopenharmony_ci **buf = ((op & 0xffff) << 16) | (op >> 16); 20762306a36Sopenharmony_ci#else 20862306a36Sopenharmony_ci **buf = op; 20962306a36Sopenharmony_ci#endif 21062306a36Sopenharmony_ci (*buf)++; 21162306a36Sopenharmony_ci} 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_cistatic inline void 21462306a36Sopenharmony_ci__resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 21562306a36Sopenharmony_ci{ 21662306a36Sopenharmony_ci long laddr = (long)lab->addr; 21762306a36Sopenharmony_ci long raddr = (long)rel->addr; 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci switch (rel->type) { 22062306a36Sopenharmony_ci case R_MIPS_PC16: 22162306a36Sopenharmony_ci#ifdef CONFIG_CPU_LITTLE_ENDIAN 22262306a36Sopenharmony_ci *rel->addr |= (build_bimm(laddr - (raddr + 4)) << 16); 22362306a36Sopenharmony_ci#else 22462306a36Sopenharmony_ci *rel->addr |= build_bimm(laddr - (raddr + 4)); 22562306a36Sopenharmony_ci#endif 22662306a36Sopenharmony_ci break; 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci default: 22962306a36Sopenharmony_ci panic("Unsupported Micro-assembler relocation %d", 23062306a36Sopenharmony_ci rel->type); 23162306a36Sopenharmony_ci } 23262306a36Sopenharmony_ci} 233