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 /* no chaining with JALR */ 58 TCGLabel *misaligned = NULL; 59 TCGv t0 = tcg_temp_new(); 60 61 62 gen_get_gpr(cpu_pc, a->rs1); 63 tcg_gen_addi_tl(cpu_pc, cpu_pc, a->imm); 64 tcg_gen_andi_tl(cpu_pc, cpu_pc, (target_ulong)-2); 65 66 if (!has_ext(ctx, RVC)) { 67 misaligned = gen_new_label(); 68 tcg_gen_andi_tl(t0, cpu_pc, 0x2); 69 tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned); 70 } 71 72 if (a->rd != 0) { 73 tcg_gen_movi_tl(cpu_gpr[a->rd], ctx->pc_succ_insn); 74 } 75 lookup_and_goto_ptr(ctx); 76 77 if (misaligned) { 78 gen_set_label(misaligned); 79 gen_exception_inst_addr_mis(ctx); 80 } 81 ctx->base.is_jmp = DISAS_NORETURN; 82 83 tcg_temp_free(t0); 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 source1, source2; 91 source1 = tcg_temp_new(); 92 source2 = tcg_temp_new(); 93 gen_get_gpr(source1, a->rs1); 94 gen_get_gpr(source2, a->rs2); 95 96 tcg_gen_brcond_tl(cond, source1, source2, l); 97 gen_goto_tb(ctx, 1, ctx->pc_succ_insn); 98 gen_set_label(l); /* branch taken */ 99 100 if (!has_ext(ctx, RVC) && ((ctx->base.pc_next + a->imm) & 0x3)) { 101 /* misaligned */ 102 gen_exception_inst_addr_mis(ctx); 103 } else { 104 gen_goto_tb(ctx, 0, ctx->base.pc_next + a->imm); 105 } 106 ctx->base.is_jmp = DISAS_NORETURN; 107 108 tcg_temp_free(source1); 109 tcg_temp_free(source2); 110 111 return true; 112} 113 114static bool trans_beq(DisasContext *ctx, arg_beq *a) 115{ 116 return gen_branch(ctx, a, TCG_COND_EQ); 117} 118 119static bool trans_bne(DisasContext *ctx, arg_bne *a) 120{ 121 return gen_branch(ctx, a, TCG_COND_NE); 122} 123 124static bool trans_blt(DisasContext *ctx, arg_blt *a) 125{ 126 return gen_branch(ctx, a, TCG_COND_LT); 127} 128 129static bool trans_bge(DisasContext *ctx, arg_bge *a) 130{ 131 return gen_branch(ctx, a, TCG_COND_GE); 132} 133 134static bool trans_bltu(DisasContext *ctx, arg_bltu *a) 135{ 136 return gen_branch(ctx, a, TCG_COND_LTU); 137} 138 139static bool trans_bgeu(DisasContext *ctx, arg_bgeu *a) 140{ 141 return gen_branch(ctx, a, TCG_COND_GEU); 142} 143 144static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) 145{ 146 TCGv t0 = tcg_temp_new(); 147 TCGv t1 = tcg_temp_new(); 148 gen_get_gpr(t0, a->rs1); 149 tcg_gen_addi_tl(t0, t0, a->imm); 150 151 tcg_gen_qemu_ld_tl(t1, t0, ctx->mem_idx, memop); 152 gen_set_gpr(a->rd, t1); 153 tcg_temp_free(t0); 154 tcg_temp_free(t1); 155 return true; 156} 157 158static bool trans_lb(DisasContext *ctx, arg_lb *a) 159{ 160 return gen_load(ctx, a, MO_SB); 161} 162 163static bool trans_lh(DisasContext *ctx, arg_lh *a) 164{ 165 return gen_load(ctx, a, MO_TESW); 166} 167 168static bool trans_lw(DisasContext *ctx, arg_lw *a) 169{ 170 return gen_load(ctx, a, MO_TESL); 171} 172 173static bool trans_lbu(DisasContext *ctx, arg_lbu *a) 174{ 175 return gen_load(ctx, a, MO_UB); 176} 177 178static bool trans_lhu(DisasContext *ctx, arg_lhu *a) 179{ 180 return gen_load(ctx, a, MO_TEUW); 181} 182 183static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) 184{ 185 TCGv t0 = tcg_temp_new(); 186 TCGv dat = tcg_temp_new(); 187 gen_get_gpr(t0, a->rs1); 188 tcg_gen_addi_tl(t0, t0, a->imm); 189 gen_get_gpr(dat, a->rs2); 190 191 tcg_gen_qemu_st_tl(dat, t0, ctx->mem_idx, memop); 192 tcg_temp_free(t0); 193 tcg_temp_free(dat); 194 return true; 195} 196 197 198static bool trans_sb(DisasContext *ctx, arg_sb *a) 199{ 200 return gen_store(ctx, a, MO_SB); 201} 202 203static bool trans_sh(DisasContext *ctx, arg_sh *a) 204{ 205 return gen_store(ctx, a, MO_TESW); 206} 207 208static bool trans_sw(DisasContext *ctx, arg_sw *a) 209{ 210 return gen_store(ctx, a, MO_TESL); 211} 212 213static bool trans_lwu(DisasContext *ctx, arg_lwu *a) 214{ 215 REQUIRE_64BIT(ctx); 216 return gen_load(ctx, a, MO_TEUL); 217} 218 219static bool trans_ld(DisasContext *ctx, arg_ld *a) 220{ 221 REQUIRE_64BIT(ctx); 222 return gen_load(ctx, a, MO_TEQ); 223} 224 225static bool trans_sd(DisasContext *ctx, arg_sd *a) 226{ 227 REQUIRE_64BIT(ctx); 228 return gen_store(ctx, a, MO_TEQ); 229} 230 231static bool trans_addi(DisasContext *ctx, arg_addi *a) 232{ 233 return gen_arith_imm_fn(ctx, a, &tcg_gen_addi_tl); 234} 235 236static void gen_slt(TCGv ret, TCGv s1, TCGv s2) 237{ 238 tcg_gen_setcond_tl(TCG_COND_LT, ret, s1, s2); 239} 240 241static void gen_sltu(TCGv ret, TCGv s1, TCGv s2) 242{ 243 tcg_gen_setcond_tl(TCG_COND_LTU, ret, s1, s2); 244} 245 246 247static bool trans_slti(DisasContext *ctx, arg_slti *a) 248{ 249 return gen_arith_imm_tl(ctx, a, &gen_slt); 250} 251 252static bool trans_sltiu(DisasContext *ctx, arg_sltiu *a) 253{ 254 return gen_arith_imm_tl(ctx, a, &gen_sltu); 255} 256 257static bool trans_xori(DisasContext *ctx, arg_xori *a) 258{ 259 return gen_arith_imm_fn(ctx, a, &tcg_gen_xori_tl); 260} 261static bool trans_ori(DisasContext *ctx, arg_ori *a) 262{ 263 return gen_arith_imm_fn(ctx, a, &tcg_gen_ori_tl); 264} 265static bool trans_andi(DisasContext *ctx, arg_andi *a) 266{ 267 return gen_arith_imm_fn(ctx, a, &tcg_gen_andi_tl); 268} 269static bool trans_slli(DisasContext *ctx, arg_slli *a) 270{ 271 if (a->shamt >= TARGET_LONG_BITS) { 272 return false; 273 } 274 275 if (a->rd != 0) { 276 TCGv t = tcg_temp_new(); 277 gen_get_gpr(t, a->rs1); 278 279 tcg_gen_shli_tl(t, t, a->shamt); 280 281 gen_set_gpr(a->rd, t); 282 tcg_temp_free(t); 283 } /* NOP otherwise */ 284 return true; 285} 286 287static bool trans_srli(DisasContext *ctx, arg_srli *a) 288{ 289 if (a->shamt >= TARGET_LONG_BITS) { 290 return false; 291 } 292 293 if (a->rd != 0) { 294 TCGv t = tcg_temp_new(); 295 gen_get_gpr(t, a->rs1); 296 297 tcg_gen_shri_tl(t, t, a->shamt); 298 gen_set_gpr(a->rd, t); 299 tcg_temp_free(t); 300 } /* NOP otherwise */ 301 return true; 302} 303 304static bool trans_srai(DisasContext *ctx, arg_srai *a) 305{ 306 if (a->shamt >= TARGET_LONG_BITS) { 307 return false; 308 } 309 310 if (a->rd != 0) { 311 TCGv t = tcg_temp_new(); 312 gen_get_gpr(t, a->rs1); 313 314 tcg_gen_sari_tl(t, t, a->shamt); 315 gen_set_gpr(a->rd, t); 316 tcg_temp_free(t); 317 } /* NOP otherwise */ 318 return true; 319} 320 321static bool trans_add(DisasContext *ctx, arg_add *a) 322{ 323 return gen_arith(ctx, a, &tcg_gen_add_tl); 324} 325 326static bool trans_sub(DisasContext *ctx, arg_sub *a) 327{ 328 return gen_arith(ctx, a, &tcg_gen_sub_tl); 329} 330 331static bool trans_sll(DisasContext *ctx, arg_sll *a) 332{ 333 return gen_shift(ctx, a, &tcg_gen_shl_tl); 334} 335 336static bool trans_slt(DisasContext *ctx, arg_slt *a) 337{ 338 return gen_arith(ctx, a, &gen_slt); 339} 340 341static bool trans_sltu(DisasContext *ctx, arg_sltu *a) 342{ 343 return gen_arith(ctx, a, &gen_sltu); 344} 345 346static bool trans_xor(DisasContext *ctx, arg_xor *a) 347{ 348 return gen_arith(ctx, a, &tcg_gen_xor_tl); 349} 350 351static bool trans_srl(DisasContext *ctx, arg_srl *a) 352{ 353 return gen_shift(ctx, a, &tcg_gen_shr_tl); 354} 355 356static bool trans_sra(DisasContext *ctx, arg_sra *a) 357{ 358 return gen_shift(ctx, a, &tcg_gen_sar_tl); 359} 360 361static bool trans_or(DisasContext *ctx, arg_or *a) 362{ 363 return gen_arith(ctx, a, &tcg_gen_or_tl); 364} 365 366static bool trans_and(DisasContext *ctx, arg_and *a) 367{ 368 return gen_arith(ctx, a, &tcg_gen_and_tl); 369} 370 371static bool trans_addiw(DisasContext *ctx, arg_addiw *a) 372{ 373 REQUIRE_64BIT(ctx); 374 return gen_arith_imm_tl(ctx, a, &gen_addw); 375} 376 377static bool trans_slliw(DisasContext *ctx, arg_slliw *a) 378{ 379 REQUIRE_64BIT(ctx); 380 TCGv source1; 381 source1 = tcg_temp_new(); 382 gen_get_gpr(source1, a->rs1); 383 384 tcg_gen_shli_tl(source1, source1, a->shamt); 385 tcg_gen_ext32s_tl(source1, source1); 386 gen_set_gpr(a->rd, source1); 387 388 tcg_temp_free(source1); 389 return true; 390} 391 392static bool trans_srliw(DisasContext *ctx, arg_srliw *a) 393{ 394 REQUIRE_64BIT(ctx); 395 TCGv t = tcg_temp_new(); 396 gen_get_gpr(t, a->rs1); 397 tcg_gen_extract_tl(t, t, a->shamt, 32 - a->shamt); 398 /* sign-extend for W instructions */ 399 tcg_gen_ext32s_tl(t, t); 400 gen_set_gpr(a->rd, t); 401 tcg_temp_free(t); 402 return true; 403} 404 405static bool trans_sraiw(DisasContext *ctx, arg_sraiw *a) 406{ 407 REQUIRE_64BIT(ctx); 408 TCGv t = tcg_temp_new(); 409 gen_get_gpr(t, a->rs1); 410 tcg_gen_sextract_tl(t, t, a->shamt, 32 - a->shamt); 411 gen_set_gpr(a->rd, t); 412 tcg_temp_free(t); 413 return true; 414} 415 416static bool trans_addw(DisasContext *ctx, arg_addw *a) 417{ 418 REQUIRE_64BIT(ctx); 419 return gen_arith(ctx, a, &gen_addw); 420} 421 422static bool trans_subw(DisasContext *ctx, arg_subw *a) 423{ 424 REQUIRE_64BIT(ctx); 425 return gen_arith(ctx, a, &gen_subw); 426} 427 428static bool trans_sllw(DisasContext *ctx, arg_sllw *a) 429{ 430 REQUIRE_64BIT(ctx); 431 TCGv source1 = tcg_temp_new(); 432 TCGv source2 = tcg_temp_new(); 433 434 gen_get_gpr(source1, a->rs1); 435 gen_get_gpr(source2, a->rs2); 436 437 tcg_gen_andi_tl(source2, source2, 0x1F); 438 tcg_gen_shl_tl(source1, source1, source2); 439 440 tcg_gen_ext32s_tl(source1, source1); 441 gen_set_gpr(a->rd, source1); 442 tcg_temp_free(source1); 443 tcg_temp_free(source2); 444 return true; 445} 446 447static bool trans_srlw(DisasContext *ctx, arg_srlw *a) 448{ 449 REQUIRE_64BIT(ctx); 450 TCGv source1 = tcg_temp_new(); 451 TCGv source2 = tcg_temp_new(); 452 453 gen_get_gpr(source1, a->rs1); 454 gen_get_gpr(source2, a->rs2); 455 456 /* clear upper 32 */ 457 tcg_gen_ext32u_tl(source1, source1); 458 tcg_gen_andi_tl(source2, source2, 0x1F); 459 tcg_gen_shr_tl(source1, source1, source2); 460 461 tcg_gen_ext32s_tl(source1, source1); 462 gen_set_gpr(a->rd, source1); 463 tcg_temp_free(source1); 464 tcg_temp_free(source2); 465 return true; 466} 467 468static bool trans_sraw(DisasContext *ctx, arg_sraw *a) 469{ 470 REQUIRE_64BIT(ctx); 471 TCGv source1 = tcg_temp_new(); 472 TCGv source2 = tcg_temp_new(); 473 474 gen_get_gpr(source1, a->rs1); 475 gen_get_gpr(source2, a->rs2); 476 477 /* 478 * first, trick to get it to act like working on 32 bits (get rid of 479 * upper 32, sign extend to fill space) 480 */ 481 tcg_gen_ext32s_tl(source1, source1); 482 tcg_gen_andi_tl(source2, source2, 0x1F); 483 tcg_gen_sar_tl(source1, source1, source2); 484 485 gen_set_gpr(a->rd, source1); 486 tcg_temp_free(source1); 487 tcg_temp_free(source2); 488 489 return true; 490} 491 492static bool trans_fence(DisasContext *ctx, arg_fence *a) 493{ 494 /* FENCE is a full memory barrier. */ 495 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC); 496 return true; 497} 498 499static bool trans_fence_i(DisasContext *ctx, arg_fence_i *a) 500{ 501 if (!ctx->ext_ifencei) { 502 return false; 503 } 504 505 /* 506 * FENCE_I is a no-op in QEMU, 507 * however we need to end the translation block 508 */ 509 tcg_gen_movi_tl(cpu_pc, ctx->pc_succ_insn); 510 exit_tb(ctx); 511 ctx->base.is_jmp = DISAS_NORETURN; 512 return true; 513} 514 515#define RISCV_OP_CSR_PRE do {\ 516 source1 = tcg_temp_new(); \ 517 csr_store = tcg_temp_new(); \ 518 dest = tcg_temp_new(); \ 519 rs1_pass = tcg_temp_new(); \ 520 gen_get_gpr(source1, a->rs1); \ 521 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next); \ 522 tcg_gen_movi_tl(rs1_pass, a->rs1); \ 523 tcg_gen_movi_tl(csr_store, a->csr); \ 524 gen_io_start();\ 525} while (0) 526 527#define RISCV_OP_CSR_POST do {\ 528 gen_set_gpr(a->rd, dest); \ 529 tcg_gen_movi_tl(cpu_pc, ctx->pc_succ_insn); \ 530 exit_tb(ctx); \ 531 ctx->base.is_jmp = DISAS_NORETURN; \ 532 tcg_temp_free(source1); \ 533 tcg_temp_free(csr_store); \ 534 tcg_temp_free(dest); \ 535 tcg_temp_free(rs1_pass); \ 536} while (0) 537 538 539static bool trans_csrrw(DisasContext *ctx, arg_csrrw *a) 540{ 541 TCGv source1, csr_store, dest, rs1_pass; 542 RISCV_OP_CSR_PRE; 543 gen_helper_csrrw(dest, cpu_env, source1, csr_store); 544 RISCV_OP_CSR_POST; 545 return true; 546} 547 548static bool trans_csrrs(DisasContext *ctx, arg_csrrs *a) 549{ 550 TCGv source1, csr_store, dest, rs1_pass; 551 RISCV_OP_CSR_PRE; 552 gen_helper_csrrs(dest, cpu_env, source1, csr_store, rs1_pass); 553 RISCV_OP_CSR_POST; 554 return true; 555} 556 557static bool trans_csrrc(DisasContext *ctx, arg_csrrc *a) 558{ 559 TCGv source1, csr_store, dest, rs1_pass; 560 RISCV_OP_CSR_PRE; 561 gen_helper_csrrc(dest, cpu_env, source1, csr_store, rs1_pass); 562 RISCV_OP_CSR_POST; 563 return true; 564} 565 566static bool trans_csrrwi(DisasContext *ctx, arg_csrrwi *a) 567{ 568 TCGv source1, csr_store, dest, rs1_pass; 569 RISCV_OP_CSR_PRE; 570 gen_helper_csrrw(dest, cpu_env, rs1_pass, csr_store); 571 RISCV_OP_CSR_POST; 572 return true; 573} 574 575static bool trans_csrrsi(DisasContext *ctx, arg_csrrsi *a) 576{ 577 TCGv source1, csr_store, dest, rs1_pass; 578 RISCV_OP_CSR_PRE; 579 gen_helper_csrrs(dest, cpu_env, rs1_pass, csr_store, rs1_pass); 580 RISCV_OP_CSR_POST; 581 return true; 582} 583 584static bool trans_csrrci(DisasContext *ctx, arg_csrrci *a) 585{ 586 TCGv source1, csr_store, dest, rs1_pass; 587 RISCV_OP_CSR_PRE; 588 gen_helper_csrrc(dest, cpu_env, rs1_pass, csr_store, rs1_pass); 589 RISCV_OP_CSR_POST; 590 return true; 591} 592