1/* 2 * RISC-V translation routines for the RVXI Base Integer Instruction Set. 3 * 4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 * Copyright (c) 2018 Peer Adelt, peer.adelt@hni.uni-paderborn.de 6 * Bastian Koppelmann, kbastian@mail.uni-paderborn.de 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms and conditions of the GNU General Public License, 10 * version 2 or later, as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21static bool trans_illegal(DisasContext *ctx, arg_empty *a) 22{ 23 gen_exception_illegal(ctx); 24 return true; 25} 26 27static bool trans_c64_illegal(DisasContext *ctx, arg_empty *a) 28{ 29 REQUIRE_64BIT(ctx); 30 return trans_illegal(ctx, a); 31} 32 33static bool trans_lui(DisasContext *ctx, arg_lui *a) 34{ 35 if (a->rd != 0) { 36 tcg_gen_movi_tl(cpu_gpr[a->rd], a->imm); 37 } 38 return true; 39} 40 41static bool trans_auipc(DisasContext *ctx, arg_auipc *a) 42{ 43 if (a->rd != 0) { 44 tcg_gen_movi_tl(cpu_gpr[a->rd], a->imm + ctx->base.pc_next); 45 } 46 return true; 47} 48 49static bool trans_jal(DisasContext *ctx, arg_jal *a) 50{ 51 gen_jal(ctx, a->rd, a->imm); 52 return true; 53} 54 55static bool trans_jalr(DisasContext *ctx, arg_jalr *a) 56{ 57 TCGLabel *misaligned = NULL; 58 59 tcg_gen_addi_tl(cpu_pc, get_gpr(ctx, a->rs1, EXT_NONE), a->imm); 60 tcg_gen_andi_tl(cpu_pc, cpu_pc, (target_ulong)-2); 61 62 if (!has_ext(ctx, RVC)) { 63 TCGv t0 = tcg_temp_new(); 64 65 misaligned = gen_new_label(); 66 tcg_gen_andi_tl(t0, cpu_pc, 0x2); 67 tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned); 68 tcg_temp_free(t0); 69 } 70 71 if (a->rd != 0) { 72 tcg_gen_movi_tl(cpu_gpr[a->rd], ctx->pc_succ_insn); 73 } 74 75 /* No chaining with JALR. */ 76 lookup_and_goto_ptr(ctx); 77 78 if (misaligned) { 79 gen_set_label(misaligned); 80 gen_exception_inst_addr_mis(ctx); 81 } 82 ctx->base.is_jmp = DISAS_NORETURN; 83 84 return true; 85} 86 87static bool gen_branch(DisasContext *ctx, arg_b *a, TCGCond cond) 88{ 89 TCGLabel *l = gen_new_label(); 90 TCGv src1 = get_gpr(ctx, a->rs1, EXT_SIGN); 91 TCGv src2 = get_gpr(ctx, a->rs2, EXT_SIGN); 92 93 tcg_gen_brcond_tl(cond, src1, src2, l); 94 gen_goto_tb(ctx, 1, ctx->pc_succ_insn); 95 96 gen_set_label(l); /* branch taken */ 97 98 if (!has_ext(ctx, RVC) && ((ctx->base.pc_next + a->imm) & 0x3)) { 99 /* misaligned */ 100 gen_exception_inst_addr_mis(ctx); 101 } else { 102 gen_goto_tb(ctx, 0, ctx->base.pc_next + a->imm); 103 } 104 ctx->base.is_jmp = DISAS_NORETURN; 105 106 return true; 107} 108 109static bool trans_beq(DisasContext *ctx, arg_beq *a) 110{ 111 return gen_branch(ctx, a, TCG_COND_EQ); 112} 113 114static bool trans_bne(DisasContext *ctx, arg_bne *a) 115{ 116 return gen_branch(ctx, a, TCG_COND_NE); 117} 118 119static bool trans_blt(DisasContext *ctx, arg_blt *a) 120{ 121 return gen_branch(ctx, a, TCG_COND_LT); 122} 123 124static bool trans_bge(DisasContext *ctx, arg_bge *a) 125{ 126 return gen_branch(ctx, a, TCG_COND_GE); 127} 128 129static bool trans_bltu(DisasContext *ctx, arg_bltu *a) 130{ 131 return gen_branch(ctx, a, TCG_COND_LTU); 132} 133 134static bool trans_bgeu(DisasContext *ctx, arg_bgeu *a) 135{ 136 return gen_branch(ctx, a, TCG_COND_GEU); 137} 138 139static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) 140{ 141 TCGv dest = dest_gpr(ctx, a->rd); 142 TCGv addr = get_gpr(ctx, a->rs1, EXT_NONE); 143 144 if (a->imm) { 145 TCGv temp = temp_new(ctx); 146 tcg_gen_addi_tl(temp, addr, a->imm); 147 addr = temp; 148 } 149 150 tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop); 151 gen_set_gpr(ctx, a->rd, dest); 152 return true; 153} 154 155static bool trans_lb(DisasContext *ctx, arg_lb *a) 156{ 157 return gen_load(ctx, a, MO_SB); 158} 159 160static bool trans_lh(DisasContext *ctx, arg_lh *a) 161{ 162 return gen_load(ctx, a, MO_TESW); 163} 164 165static bool trans_lw(DisasContext *ctx, arg_lw *a) 166{ 167 return gen_load(ctx, a, MO_TESL); 168} 169 170static bool trans_lbu(DisasContext *ctx, arg_lbu *a) 171{ 172 return gen_load(ctx, a, MO_UB); 173} 174 175static bool trans_lhu(DisasContext *ctx, arg_lhu *a) 176{ 177 return gen_load(ctx, a, MO_TEUW); 178} 179 180static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) 181{ 182 TCGv addr = get_gpr(ctx, a->rs1, EXT_NONE); 183 TCGv data = get_gpr(ctx, a->rs2, EXT_NONE); 184 185 if (a->imm) { 186 TCGv temp = temp_new(ctx); 187 tcg_gen_addi_tl(temp, addr, a->imm); 188 addr = temp; 189 } 190 191 tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop); 192 return true; 193} 194 195static bool trans_sb(DisasContext *ctx, arg_sb *a) 196{ 197 return gen_store(ctx, a, MO_SB); 198} 199 200static bool trans_sh(DisasContext *ctx, arg_sh *a) 201{ 202 return gen_store(ctx, a, MO_TESW); 203} 204 205static bool trans_sw(DisasContext *ctx, arg_sw *a) 206{ 207 return gen_store(ctx, a, MO_TESL); 208} 209 210static bool trans_lwu(DisasContext *ctx, arg_lwu *a) 211{ 212 REQUIRE_64BIT(ctx); 213 return gen_load(ctx, a, MO_TEUL); 214} 215 216static bool trans_ld(DisasContext *ctx, arg_ld *a) 217{ 218 REQUIRE_64BIT(ctx); 219 return gen_load(ctx, a, MO_TEQ); 220} 221 222static bool trans_sd(DisasContext *ctx, arg_sd *a) 223{ 224 REQUIRE_64BIT(ctx); 225 return gen_store(ctx, a, MO_TEQ); 226} 227 228static bool trans_addi(DisasContext *ctx, arg_addi *a) 229{ 230 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl); 231} 232 233static void gen_slt(TCGv ret, TCGv s1, TCGv s2) 234{ 235 tcg_gen_setcond_tl(TCG_COND_LT, ret, s1, s2); 236} 237 238static void gen_sltu(TCGv ret, TCGv s1, TCGv s2) 239{ 240 tcg_gen_setcond_tl(TCG_COND_LTU, ret, s1, s2); 241} 242 243static bool trans_slti(DisasContext *ctx, arg_slti *a) 244{ 245 return gen_arith_imm_tl(ctx, a, EXT_SIGN, gen_slt); 246} 247 248static bool trans_sltiu(DisasContext *ctx, arg_sltiu *a) 249{ 250 return gen_arith_imm_tl(ctx, a, EXT_SIGN, gen_sltu); 251} 252 253static bool trans_xori(DisasContext *ctx, arg_xori *a) 254{ 255 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_xori_tl); 256} 257 258static bool trans_ori(DisasContext *ctx, arg_ori *a) 259{ 260 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_ori_tl); 261} 262 263static bool trans_andi(DisasContext *ctx, arg_andi *a) 264{ 265 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_andi_tl); 266} 267 268static bool trans_slli(DisasContext *ctx, arg_slli *a) 269{ 270 return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl); 271} 272 273static bool trans_srli(DisasContext *ctx, arg_srli *a) 274{ 275 return gen_shift_imm_fn(ctx, a, EXT_ZERO, tcg_gen_shri_tl); 276} 277 278static bool trans_srai(DisasContext *ctx, arg_srai *a) 279{ 280 return gen_shift_imm_fn(ctx, a, EXT_SIGN, tcg_gen_sari_tl); 281} 282 283static bool trans_add(DisasContext *ctx, arg_add *a) 284{ 285 return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl); 286} 287 288static bool trans_sub(DisasContext *ctx, arg_sub *a) 289{ 290 return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl); 291} 292 293static bool trans_sll(DisasContext *ctx, arg_sll *a) 294{ 295 return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl); 296} 297 298static bool trans_slt(DisasContext *ctx, arg_slt *a) 299{ 300 return gen_arith(ctx, a, EXT_SIGN, gen_slt); 301} 302 303static bool trans_sltu(DisasContext *ctx, arg_sltu *a) 304{ 305 return gen_arith(ctx, a, EXT_SIGN, gen_sltu); 306} 307 308static bool trans_xor(DisasContext *ctx, arg_xor *a) 309{ 310 return gen_arith(ctx, a, EXT_NONE, tcg_gen_xor_tl); 311} 312 313static bool trans_srl(DisasContext *ctx, arg_srl *a) 314{ 315 return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl); 316} 317 318static bool trans_sra(DisasContext *ctx, arg_sra *a) 319{ 320 return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl); 321} 322 323static bool trans_or(DisasContext *ctx, arg_or *a) 324{ 325 return gen_arith(ctx, a, EXT_NONE, tcg_gen_or_tl); 326} 327 328static bool trans_and(DisasContext *ctx, arg_and *a) 329{ 330 return gen_arith(ctx, a, EXT_NONE, tcg_gen_and_tl); 331} 332 333static bool trans_addiw(DisasContext *ctx, arg_addiw *a) 334{ 335 REQUIRE_64BIT(ctx); 336 ctx->w = true; 337 return gen_arith_imm_fn(ctx, a, EXT_NONE, tcg_gen_addi_tl); 338} 339 340static bool trans_slliw(DisasContext *ctx, arg_slliw *a) 341{ 342 REQUIRE_64BIT(ctx); 343 ctx->w = true; 344 return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_shli_tl); 345} 346 347static void gen_srliw(TCGv dst, TCGv src, target_long shamt) 348{ 349 tcg_gen_extract_tl(dst, src, shamt, 32 - shamt); 350} 351 352static bool trans_srliw(DisasContext *ctx, arg_srliw *a) 353{ 354 REQUIRE_64BIT(ctx); 355 ctx->w = true; 356 return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_srliw); 357} 358 359static void gen_sraiw(TCGv dst, TCGv src, target_long shamt) 360{ 361 tcg_gen_sextract_tl(dst, src, shamt, 32 - shamt); 362} 363 364static bool trans_sraiw(DisasContext *ctx, arg_sraiw *a) 365{ 366 REQUIRE_64BIT(ctx); 367 ctx->w = true; 368 return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_sraiw); 369} 370 371static bool trans_addw(DisasContext *ctx, arg_addw *a) 372{ 373 REQUIRE_64BIT(ctx); 374 ctx->w = true; 375 return gen_arith(ctx, a, EXT_NONE, tcg_gen_add_tl); 376} 377 378static bool trans_subw(DisasContext *ctx, arg_subw *a) 379{ 380 REQUIRE_64BIT(ctx); 381 ctx->w = true; 382 return gen_arith(ctx, a, EXT_NONE, tcg_gen_sub_tl); 383} 384 385static bool trans_sllw(DisasContext *ctx, arg_sllw *a) 386{ 387 REQUIRE_64BIT(ctx); 388 ctx->w = true; 389 return gen_shift(ctx, a, EXT_NONE, tcg_gen_shl_tl); 390} 391 392static bool trans_srlw(DisasContext *ctx, arg_srlw *a) 393{ 394 REQUIRE_64BIT(ctx); 395 ctx->w = true; 396 return gen_shift(ctx, a, EXT_ZERO, tcg_gen_shr_tl); 397} 398 399static bool trans_sraw(DisasContext *ctx, arg_sraw *a) 400{ 401 REQUIRE_64BIT(ctx); 402 ctx->w = true; 403 return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl); 404} 405 406static bool trans_fence(DisasContext *ctx, arg_fence *a) 407{ 408 /* FENCE is a full memory barrier. */ 409 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC); 410 return true; 411} 412 413static bool trans_fence_i(DisasContext *ctx, arg_fence_i *a) 414{ 415 if (!ctx->ext_ifencei) { 416 return false; 417 } 418 419 /* 420 * FENCE_I is a no-op in QEMU, 421 * however we need to end the translation block 422 */ 423 tcg_gen_movi_tl(cpu_pc, ctx->pc_succ_insn); 424 exit_tb(ctx); 425 ctx->base.is_jmp = DISAS_NORETURN; 426 return true; 427} 428 429#define RISCV_OP_CSR_PRE do {\ 430 source1 = tcg_temp_new(); \ 431 csr_store = tcg_temp_new(); \ 432 dest = tcg_temp_new(); \ 433 rs1_pass = tcg_temp_new(); \ 434 gen_get_gpr(ctx, source1, a->rs1); \ 435 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next); \ 436 tcg_gen_movi_tl(rs1_pass, a->rs1); \ 437 tcg_gen_movi_tl(csr_store, a->csr); \ 438 gen_io_start();\ 439} while (0) 440 441#define RISCV_OP_CSR_POST do {\ 442 gen_set_gpr(ctx, a->rd, dest); \ 443 tcg_gen_movi_tl(cpu_pc, ctx->pc_succ_insn); \ 444 exit_tb(ctx); \ 445 ctx->base.is_jmp = DISAS_NORETURN; \ 446 tcg_temp_free(source1); \ 447 tcg_temp_free(csr_store); \ 448 tcg_temp_free(dest); \ 449 tcg_temp_free(rs1_pass); \ 450} while (0) 451 452 453static bool trans_csrrw(DisasContext *ctx, arg_csrrw *a) 454{ 455 TCGv source1, csr_store, dest, rs1_pass; 456 RISCV_OP_CSR_PRE; 457 gen_helper_csrrw(dest, cpu_env, source1, csr_store); 458 RISCV_OP_CSR_POST; 459 return true; 460} 461 462static bool trans_csrrs(DisasContext *ctx, arg_csrrs *a) 463{ 464 TCGv source1, csr_store, dest, rs1_pass; 465 RISCV_OP_CSR_PRE; 466 gen_helper_csrrs(dest, cpu_env, source1, csr_store, rs1_pass); 467 RISCV_OP_CSR_POST; 468 return true; 469} 470 471static bool trans_csrrc(DisasContext *ctx, arg_csrrc *a) 472{ 473 TCGv source1, csr_store, dest, rs1_pass; 474 RISCV_OP_CSR_PRE; 475 gen_helper_csrrc(dest, cpu_env, source1, csr_store, rs1_pass); 476 RISCV_OP_CSR_POST; 477 return true; 478} 479 480static bool trans_csrrwi(DisasContext *ctx, arg_csrrwi *a) 481{ 482 TCGv source1, csr_store, dest, rs1_pass; 483 RISCV_OP_CSR_PRE; 484 gen_helper_csrrw(dest, cpu_env, rs1_pass, csr_store); 485 RISCV_OP_CSR_POST; 486 return true; 487} 488 489static bool trans_csrrsi(DisasContext *ctx, arg_csrrsi *a) 490{ 491 TCGv source1, csr_store, dest, rs1_pass; 492 RISCV_OP_CSR_PRE; 493 gen_helper_csrrs(dest, cpu_env, rs1_pass, csr_store, rs1_pass); 494 RISCV_OP_CSR_POST; 495 return true; 496} 497 498static bool trans_csrrci(DisasContext *ctx, arg_csrrci *a) 499{ 500 TCGv source1, csr_store, dest, rs1_pass; 501 RISCV_OP_CSR_PRE; 502 gen_helper_csrrc(dest, cpu_env, rs1_pass, csr_store, rs1_pass); 503 RISCV_OP_CSR_POST; 504 return true; 505} 506