18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public 38c2ecf20Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive 48c2ecf20Sopenharmony_ci * for more details. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * A small micro-assembler. It is intentionally kept simple, does only 78c2ecf20Sopenharmony_ci * support a subset of instructions, and does not try to hide pipeline 88c2ecf20Sopenharmony_ci * effects like branch delay slots. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer 118c2ecf20Sopenharmony_ci * Copyright (C) 2005, 2007 Maciej W. Rozycki 128c2ecf20Sopenharmony_ci * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org) 138c2ecf20Sopenharmony_ci * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved. 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <linux/kernel.h> 178c2ecf20Sopenharmony_ci#include <linux/types.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <asm/inst.h> 208c2ecf20Sopenharmony_ci#include <asm/elf.h> 218c2ecf20Sopenharmony_ci#include <asm/bugs.h> 228c2ecf20Sopenharmony_ci#include <asm/uasm.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define RS_MASK 0x1f 258c2ecf20Sopenharmony_ci#define RS_SH 16 268c2ecf20Sopenharmony_ci#define RT_MASK 0x1f 278c2ecf20Sopenharmony_ci#define RT_SH 21 288c2ecf20Sopenharmony_ci#define SCIMM_MASK 0x3ff 298c2ecf20Sopenharmony_ci#define SCIMM_SH 16 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/* This macro sets the non-variable bits of an instruction. */ 328c2ecf20Sopenharmony_ci#define M(a, b, c, d, e, f) \ 338c2ecf20Sopenharmony_ci ((a) << OP_SH \ 348c2ecf20Sopenharmony_ci | (b) << RT_SH \ 358c2ecf20Sopenharmony_ci | (c) << RS_SH \ 368c2ecf20Sopenharmony_ci | (d) << RD_SH \ 378c2ecf20Sopenharmony_ci | (e) << RE_SH \ 388c2ecf20Sopenharmony_ci | (f) << FUNC_SH) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#include "uasm.c" 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic const struct insn insn_table_MM[insn_invalid] = { 438c2ecf20Sopenharmony_ci [insn_addu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_addu32_op), RT | RS | RD}, 448c2ecf20Sopenharmony_ci [insn_addiu] = {M(mm_addiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 458c2ecf20Sopenharmony_ci [insn_and] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_and_op), RT | RS | RD}, 468c2ecf20Sopenharmony_ci [insn_andi] = {M(mm_andi32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 478c2ecf20Sopenharmony_ci [insn_beq] = {M(mm_beq32_op, 0, 0, 0, 0, 0), RS | RT | BIMM}, 488c2ecf20Sopenharmony_ci [insn_beql] = {0, 0}, 498c2ecf20Sopenharmony_ci [insn_bgez] = {M(mm_pool32i_op, mm_bgez_op, 0, 0, 0, 0), RS | BIMM}, 508c2ecf20Sopenharmony_ci [insn_bgezl] = {0, 0}, 518c2ecf20Sopenharmony_ci [insn_bltz] = {M(mm_pool32i_op, mm_bltz_op, 0, 0, 0, 0), RS | BIMM}, 528c2ecf20Sopenharmony_ci [insn_bltzl] = {0, 0}, 538c2ecf20Sopenharmony_ci [insn_bne] = {M(mm_bne32_op, 0, 0, 0, 0, 0), RT | RS | BIMM}, 548c2ecf20Sopenharmony_ci [insn_cache] = {M(mm_pool32b_op, 0, 0, mm_cache_func, 0, 0), RT | RS | SIMM}, 558c2ecf20Sopenharmony_ci [insn_cfc1] = {M(mm_pool32f_op, 0, 0, 0, mm_cfc1_op, mm_32f_73_op), RT | RS}, 568c2ecf20Sopenharmony_ci [insn_cfcmsa] = {M(mm_pool32s_op, 0, msa_cfc_op, 0, 0, mm_32s_elm_op), RD | RE}, 578c2ecf20Sopenharmony_ci [insn_ctc1] = {M(mm_pool32f_op, 0, 0, 0, mm_ctc1_op, mm_32f_73_op), RT | RS}, 588c2ecf20Sopenharmony_ci [insn_ctcmsa] = {M(mm_pool32s_op, 0, msa_ctc_op, 0, 0, mm_32s_elm_op), RD | RE}, 598c2ecf20Sopenharmony_ci [insn_daddu] = {0, 0}, 608c2ecf20Sopenharmony_ci [insn_daddiu] = {0, 0}, 618c2ecf20Sopenharmony_ci [insn_di] = {M(mm_pool32a_op, 0, 0, 0, mm_di_op, mm_pool32axf_op), RS}, 628c2ecf20Sopenharmony_ci [insn_divu] = {M(mm_pool32a_op, 0, 0, 0, mm_divu_op, mm_pool32axf_op), RT | RS}, 638c2ecf20Sopenharmony_ci [insn_dmfc0] = {0, 0}, 648c2ecf20Sopenharmony_ci [insn_dmtc0] = {0, 0}, 658c2ecf20Sopenharmony_ci [insn_dsll] = {0, 0}, 668c2ecf20Sopenharmony_ci [insn_dsll32] = {0, 0}, 678c2ecf20Sopenharmony_ci [insn_dsra] = {0, 0}, 688c2ecf20Sopenharmony_ci [insn_dsrl] = {0, 0}, 698c2ecf20Sopenharmony_ci [insn_dsrl32] = {0, 0}, 708c2ecf20Sopenharmony_ci [insn_drotr] = {0, 0}, 718c2ecf20Sopenharmony_ci [insn_drotr32] = {0, 0}, 728c2ecf20Sopenharmony_ci [insn_dsubu] = {0, 0}, 738c2ecf20Sopenharmony_ci [insn_eret] = {M(mm_pool32a_op, 0, 0, 0, mm_eret_op, mm_pool32axf_op), 0}, 748c2ecf20Sopenharmony_ci [insn_ins] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_ins_op), RT | RS | RD | RE}, 758c2ecf20Sopenharmony_ci [insn_ext] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_ext_op), RT | RS | RD | RE}, 768c2ecf20Sopenharmony_ci [insn_j] = {M(mm_j32_op, 0, 0, 0, 0, 0), JIMM}, 778c2ecf20Sopenharmony_ci [insn_jal] = {M(mm_jal32_op, 0, 0, 0, 0, 0), JIMM}, 788c2ecf20Sopenharmony_ci [insn_jalr] = {M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RT | RS}, 798c2ecf20Sopenharmony_ci [insn_jr] = {M(mm_pool32a_op, 0, 0, 0, mm_jalr_op, mm_pool32axf_op), RS}, 808c2ecf20Sopenharmony_ci [insn_lb] = {M(mm_lb32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 818c2ecf20Sopenharmony_ci [insn_ld] = {0, 0}, 828c2ecf20Sopenharmony_ci [insn_lh] = {M(mm_lh32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 838c2ecf20Sopenharmony_ci [insn_ll] = {M(mm_pool32c_op, 0, 0, (mm_ll_func << 1), 0, 0), RS | RT | SIMM}, 848c2ecf20Sopenharmony_ci [insn_lld] = {0, 0}, 858c2ecf20Sopenharmony_ci [insn_lui] = {M(mm_pool32i_op, mm_lui_op, 0, 0, 0, 0), RS | SIMM}, 868c2ecf20Sopenharmony_ci [insn_lw] = {M(mm_lw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 878c2ecf20Sopenharmony_ci [insn_mfc0] = {M(mm_pool32a_op, 0, 0, 0, mm_mfc0_op, mm_pool32axf_op), RT | RS | RD}, 888c2ecf20Sopenharmony_ci [insn_mfhi] = {M(mm_pool32a_op, 0, 0, 0, mm_mfhi32_op, mm_pool32axf_op), RS}, 898c2ecf20Sopenharmony_ci [insn_mflo] = {M(mm_pool32a_op, 0, 0, 0, mm_mflo32_op, mm_pool32axf_op), RS}, 908c2ecf20Sopenharmony_ci [insn_mtc0] = {M(mm_pool32a_op, 0, 0, 0, mm_mtc0_op, mm_pool32axf_op), RT | RS | RD}, 918c2ecf20Sopenharmony_ci [insn_mthi] = {M(mm_pool32a_op, 0, 0, 0, mm_mthi32_op, mm_pool32axf_op), RS}, 928c2ecf20Sopenharmony_ci [insn_mtlo] = {M(mm_pool32a_op, 0, 0, 0, mm_mtlo32_op, mm_pool32axf_op), RS}, 938c2ecf20Sopenharmony_ci [insn_mul] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_mul_op), RT | RS | RD}, 948c2ecf20Sopenharmony_ci [insn_or] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_or32_op), RT | RS | RD}, 958c2ecf20Sopenharmony_ci [insn_ori] = {M(mm_ori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 968c2ecf20Sopenharmony_ci [insn_pref] = {M(mm_pool32c_op, 0, 0, (mm_pref_func << 1), 0, 0), RT | RS | SIMM}, 978c2ecf20Sopenharmony_ci [insn_rfe] = {0, 0}, 988c2ecf20Sopenharmony_ci [insn_sc] = {M(mm_pool32c_op, 0, 0, (mm_sc_func << 1), 0, 0), RT | RS | SIMM}, 998c2ecf20Sopenharmony_ci [insn_scd] = {0, 0}, 1008c2ecf20Sopenharmony_ci [insn_sd] = {0, 0}, 1018c2ecf20Sopenharmony_ci [insn_sll] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sll32_op), RT | RS | RD}, 1028c2ecf20Sopenharmony_ci [insn_sllv] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sllv32_op), RT | RS | RD}, 1038c2ecf20Sopenharmony_ci [insn_slt] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_slt_op), RT | RS | RD}, 1048c2ecf20Sopenharmony_ci [insn_sltiu] = {M(mm_sltiu32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 1058c2ecf20Sopenharmony_ci [insn_sltu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sltu_op), RT | RS | RD}, 1068c2ecf20Sopenharmony_ci [insn_sra] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_sra_op), RT | RS | RD}, 1078c2ecf20Sopenharmony_ci [insn_srav] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srav_op), RT | RS | RD}, 1088c2ecf20Sopenharmony_ci [insn_srl] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srl32_op), RT | RS | RD}, 1098c2ecf20Sopenharmony_ci [insn_srlv] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_srlv32_op), RT | RS | RD}, 1108c2ecf20Sopenharmony_ci [insn_rotr] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_rotr_op), RT | RS | RD}, 1118c2ecf20Sopenharmony_ci [insn_subu] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_subu32_op), RT | RS | RD}, 1128c2ecf20Sopenharmony_ci [insn_sw] = {M(mm_sw32_op, 0, 0, 0, 0, 0), RT | RS | SIMM}, 1138c2ecf20Sopenharmony_ci [insn_sync] = {M(mm_pool32a_op, 0, 0, 0, mm_sync_op, mm_pool32axf_op), RS}, 1148c2ecf20Sopenharmony_ci [insn_tlbp] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbp_op, mm_pool32axf_op), 0}, 1158c2ecf20Sopenharmony_ci [insn_tlbr] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbr_op, mm_pool32axf_op), 0}, 1168c2ecf20Sopenharmony_ci [insn_tlbwi] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbwi_op, mm_pool32axf_op), 0}, 1178c2ecf20Sopenharmony_ci [insn_tlbwr] = {M(mm_pool32a_op, 0, 0, 0, mm_tlbwr_op, mm_pool32axf_op), 0}, 1188c2ecf20Sopenharmony_ci [insn_wait] = {M(mm_pool32a_op, 0, 0, 0, mm_wait_op, mm_pool32axf_op), SCIMM}, 1198c2ecf20Sopenharmony_ci [insn_wsbh] = {M(mm_pool32a_op, 0, 0, 0, mm_wsbh_op, mm_pool32axf_op), RT | RS}, 1208c2ecf20Sopenharmony_ci [insn_xor] = {M(mm_pool32a_op, 0, 0, 0, 0, mm_xor32_op), RT | RS | RD}, 1218c2ecf20Sopenharmony_ci [insn_xori] = {M(mm_xori32_op, 0, 0, 0, 0, 0), RT | RS | UIMM}, 1228c2ecf20Sopenharmony_ci [insn_dins] = {0, 0}, 1238c2ecf20Sopenharmony_ci [insn_dinsm] = {0, 0}, 1248c2ecf20Sopenharmony_ci [insn_syscall] = {M(mm_pool32a_op, 0, 0, 0, mm_syscall_op, mm_pool32axf_op), SCIMM}, 1258c2ecf20Sopenharmony_ci [insn_bbit0] = {0, 0}, 1268c2ecf20Sopenharmony_ci [insn_bbit1] = {0, 0}, 1278c2ecf20Sopenharmony_ci [insn_lwx] = {0, 0}, 1288c2ecf20Sopenharmony_ci [insn_ldx] = {0, 0}, 1298c2ecf20Sopenharmony_ci}; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci#undef M 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic inline u32 build_bimm(s32 arg) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci WARN(arg > 0xffff || arg < -0x10000, 1368c2ecf20Sopenharmony_ci KERN_WARNING "Micro-assembler field overflow\n"); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n"); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 1) & 0x7fff); 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic inline u32 build_jimm(u32 arg) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci WARN(arg & ~((JIMM_MASK << 2) | 1), 1478c2ecf20Sopenharmony_ci KERN_WARNING "Micro-assembler field overflow\n"); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return (arg >> 1) & JIMM_MASK; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci/* 1538c2ecf20Sopenharmony_ci * The order of opcode arguments is implicitly left to right, 1548c2ecf20Sopenharmony_ci * starting with RS and ending with FUNC or IMM. 1558c2ecf20Sopenharmony_ci */ 1568c2ecf20Sopenharmony_cistatic void build_insn(u32 **buf, enum opcode opc, ...) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci const struct insn *ip; 1598c2ecf20Sopenharmony_ci va_list ap; 1608c2ecf20Sopenharmony_ci u32 op; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci if (opc < 0 || opc >= insn_invalid || 1638c2ecf20Sopenharmony_ci (opc == insn_daddiu && r4k_daddiu_bug()) || 1648c2ecf20Sopenharmony_ci (insn_table_MM[opc].match == 0 && insn_table_MM[opc].fields == 0)) 1658c2ecf20Sopenharmony_ci panic("Unsupported Micro-assembler instruction %d", opc); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci ip = &insn_table_MM[opc]; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci op = ip->match; 1708c2ecf20Sopenharmony_ci va_start(ap, opc); 1718c2ecf20Sopenharmony_ci if (ip->fields & RS) { 1728c2ecf20Sopenharmony_ci if (opc == insn_mfc0 || opc == insn_mtc0 || 1738c2ecf20Sopenharmony_ci opc == insn_cfc1 || opc == insn_ctc1) 1748c2ecf20Sopenharmony_ci op |= build_rt(va_arg(ap, u32)); 1758c2ecf20Sopenharmony_ci else 1768c2ecf20Sopenharmony_ci op |= build_rs(va_arg(ap, u32)); 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci if (ip->fields & RT) { 1798c2ecf20Sopenharmony_ci if (opc == insn_mfc0 || opc == insn_mtc0 || 1808c2ecf20Sopenharmony_ci opc == insn_cfc1 || opc == insn_ctc1) 1818c2ecf20Sopenharmony_ci op |= build_rs(va_arg(ap, u32)); 1828c2ecf20Sopenharmony_ci else 1838c2ecf20Sopenharmony_ci op |= build_rt(va_arg(ap, u32)); 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci if (ip->fields & RD) 1868c2ecf20Sopenharmony_ci op |= build_rd(va_arg(ap, u32)); 1878c2ecf20Sopenharmony_ci if (ip->fields & RE) 1888c2ecf20Sopenharmony_ci op |= build_re(va_arg(ap, u32)); 1898c2ecf20Sopenharmony_ci if (ip->fields & SIMM) 1908c2ecf20Sopenharmony_ci op |= build_simm(va_arg(ap, s32)); 1918c2ecf20Sopenharmony_ci if (ip->fields & UIMM) 1928c2ecf20Sopenharmony_ci op |= build_uimm(va_arg(ap, u32)); 1938c2ecf20Sopenharmony_ci if (ip->fields & BIMM) 1948c2ecf20Sopenharmony_ci op |= build_bimm(va_arg(ap, s32)); 1958c2ecf20Sopenharmony_ci if (ip->fields & JIMM) 1968c2ecf20Sopenharmony_ci op |= build_jimm(va_arg(ap, u32)); 1978c2ecf20Sopenharmony_ci if (ip->fields & FUNC) 1988c2ecf20Sopenharmony_ci op |= build_func(va_arg(ap, u32)); 1998c2ecf20Sopenharmony_ci if (ip->fields & SET) 2008c2ecf20Sopenharmony_ci op |= build_set(va_arg(ap, u32)); 2018c2ecf20Sopenharmony_ci if (ip->fields & SCIMM) 2028c2ecf20Sopenharmony_ci op |= build_scimm(va_arg(ap, u32)); 2038c2ecf20Sopenharmony_ci va_end(ap); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_LITTLE_ENDIAN 2068c2ecf20Sopenharmony_ci **buf = ((op & 0xffff) << 16) | (op >> 16); 2078c2ecf20Sopenharmony_ci#else 2088c2ecf20Sopenharmony_ci **buf = op; 2098c2ecf20Sopenharmony_ci#endif 2108c2ecf20Sopenharmony_ci (*buf)++; 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic inline void 2148c2ecf20Sopenharmony_ci__resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci long laddr = (long)lab->addr; 2178c2ecf20Sopenharmony_ci long raddr = (long)rel->addr; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci switch (rel->type) { 2208c2ecf20Sopenharmony_ci case R_MIPS_PC16: 2218c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_LITTLE_ENDIAN 2228c2ecf20Sopenharmony_ci *rel->addr |= (build_bimm(laddr - (raddr + 4)) << 16); 2238c2ecf20Sopenharmony_ci#else 2248c2ecf20Sopenharmony_ci *rel->addr |= build_bimm(laddr - (raddr + 4)); 2258c2ecf20Sopenharmony_ci#endif 2268c2ecf20Sopenharmony_ci break; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci default: 2298c2ecf20Sopenharmony_ci panic("Unsupported Micro-assembler relocation %d", 2308c2ecf20Sopenharmony_ci rel->type); 2318c2ecf20Sopenharmony_ci } 2328c2ecf20Sopenharmony_ci} 233