1 /* 2 * RISC-V Emulation Helpers for QEMU. 3 * 4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 * Copyright (c) 2017-2018 SiFive, Inc. 6 * Copyright (c) 2022 VRULL GmbH 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 21 #include "qemu/osdep.h" 22 #include "cpu.h" 23 #include "internals.h" 24 #include "exec/exec-all.h" 25 #include "exec/cpu_ldst.h" 26 #include "exec/helper-proto.h" 27 #include "trace.h" 28 29 /* Exceptions processing helpers */ 30 G_NORETURN void riscv_raise_exception(CPURISCVState *env, 31 RISCVException exception, 32 uintptr_t pc) 33 { 34 CPUState *cs = env_cpu(env); 35 36 trace_riscv_exception(exception, 37 riscv_cpu_get_trap_name(exception, false), 38 env->pc); 39 40 cs->exception_index = exception; 41 cpu_loop_exit_restore(cs, pc); 42 } 43 44 void helper_raise_exception(CPURISCVState *env, uint32_t exception) 45 { 46 riscv_raise_exception(env, exception, 0); 47 } 48 49 target_ulong helper_csrr(CPURISCVState *env, int csr) 50 { 51 /* 52 * The seed CSR must be accessed with a read-write instruction. A 53 * read-only instruction such as CSRRS/CSRRC with rs1=x0 or CSRRSI/ 54 * CSRRCI with uimm=0 will raise an illegal instruction exception. 55 */ 56 if (csr == CSR_SEED) { 57 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 58 } 59 60 target_ulong val = 0; 61 RISCVException ret = riscv_csrr(env, csr, &val); 62 63 if (ret != RISCV_EXCP_NONE) { 64 riscv_raise_exception(env, ret, GETPC()); 65 } 66 return val; 67 } 68 69 void helper_csrw(CPURISCVState *env, int csr, target_ulong src) 70 { 71 target_ulong mask = env->xl == MXL_RV32 ? UINT32_MAX : (target_ulong)-1; 72 RISCVException ret = riscv_csrrw(env, csr, NULL, src, mask); 73 74 if (ret != RISCV_EXCP_NONE) { 75 riscv_raise_exception(env, ret, GETPC()); 76 } 77 } 78 79 target_ulong helper_csrrw(CPURISCVState *env, int csr, 80 target_ulong src, target_ulong write_mask) 81 { 82 target_ulong val = 0; 83 RISCVException ret = riscv_csrrw(env, csr, &val, src, write_mask); 84 85 if (ret != RISCV_EXCP_NONE) { 86 riscv_raise_exception(env, ret, GETPC()); 87 } 88 return val; 89 } 90 91 target_ulong helper_csrr_i128(CPURISCVState *env, int csr) 92 { 93 Int128 rv = int128_zero(); 94 RISCVException ret = riscv_csrr_i128(env, csr, &rv); 95 96 if (ret != RISCV_EXCP_NONE) { 97 riscv_raise_exception(env, ret, GETPC()); 98 } 99 100 env->retxh = int128_gethi(rv); 101 return int128_getlo(rv); 102 } 103 104 void helper_csrw_i128(CPURISCVState *env, int csr, 105 target_ulong srcl, target_ulong srch) 106 { 107 RISCVException ret = riscv_csrrw_i128(env, csr, NULL, 108 int128_make128(srcl, srch), 109 UINT128_MAX); 110 111 if (ret != RISCV_EXCP_NONE) { 112 riscv_raise_exception(env, ret, GETPC()); 113 } 114 } 115 116 target_ulong helper_csrrw_i128(CPURISCVState *env, int csr, 117 target_ulong srcl, target_ulong srch, 118 target_ulong maskl, target_ulong maskh) 119 { 120 Int128 rv = int128_zero(); 121 RISCVException ret = riscv_csrrw_i128(env, csr, &rv, 122 int128_make128(srcl, srch), 123 int128_make128(maskl, maskh)); 124 125 if (ret != RISCV_EXCP_NONE) { 126 riscv_raise_exception(env, ret, GETPC()); 127 } 128 129 env->retxh = int128_gethi(rv); 130 return int128_getlo(rv); 131 } 132 133 134 /* 135 * check_zicbo_envcfg 136 * 137 * Raise virtual exceptions and illegal instruction exceptions for 138 * Zicbo[mz] instructions based on the settings of [mhs]envcfg as 139 * specified in section 2.5.1 of the CMO specification. 140 */ 141 static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits, 142 uintptr_t ra) 143 { 144 #ifndef CONFIG_USER_ONLY 145 if ((env->priv < PRV_M) && !get_field(env->menvcfg, envbits)) { 146 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); 147 } 148 149 if (env->virt_enabled && 150 (((env->priv <= PRV_S) && !get_field(env->henvcfg, envbits)) || 151 ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)))) { 152 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra); 153 } 154 155 if ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)) { 156 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); 157 } 158 #endif 159 } 160 161 void helper_cbo_zero(CPURISCVState *env, target_ulong address) 162 { 163 RISCVCPU *cpu = env_archcpu(env); 164 uint16_t cbozlen = cpu->cfg.cboz_blocksize; 165 int mmu_idx = riscv_env_mmu_index(env, false); 166 uintptr_t ra = GETPC(); 167 void *mem; 168 169 check_zicbo_envcfg(env, MENVCFG_CBZE, ra); 170 171 /* Mask off low-bits to align-down to the cache-block. */ 172 address &= ~(cbozlen - 1); 173 174 /* 175 * cbo.zero requires MMU_DATA_STORE access. Do a probe_write() 176 * to raise any exceptions, including PMP. 177 */ 178 mem = probe_write(env, address, cbozlen, mmu_idx, ra); 179 180 if (likely(mem)) { 181 memset(mem, 0, cbozlen); 182 } else { 183 /* 184 * This means that we're dealing with an I/O page. Section 4.2 185 * of cmobase v1.0.1 says: 186 * 187 * "Cache-block zero instructions store zeros independently 188 * of whether data from the underlying memory locations are 189 * cacheable." 190 * 191 * Write zeros in address + cbozlen regardless of not being 192 * a RAM page. 193 */ 194 for (int i = 0; i < cbozlen; i++) { 195 cpu_stb_mmuidx_ra(env, address + i, 0, mmu_idx, ra); 196 } 197 } 198 } 199 200 /* 201 * check_zicbom_access 202 * 203 * Check access permissions (LOAD, STORE or FETCH as specified in 204 * section 2.5.2 of the CMO specification) for Zicbom, raising 205 * either store page-fault (non-virtualized) or store guest-page 206 * fault (virtualized). 207 */ 208 static void check_zicbom_access(CPURISCVState *env, 209 target_ulong address, 210 uintptr_t ra) 211 { 212 RISCVCPU *cpu = env_archcpu(env); 213 int mmu_idx = riscv_env_mmu_index(env, false); 214 uint16_t cbomlen = cpu->cfg.cbom_blocksize; 215 void *phost; 216 int ret; 217 218 /* Mask off low-bits to align-down to the cache-block. */ 219 address &= ~(cbomlen - 1); 220 221 /* 222 * Section 2.5.2 of cmobase v1.0.1: 223 * 224 * "A cache-block management instruction is permitted to 225 * access the specified cache block whenever a load instruction 226 * or store instruction is permitted to access the corresponding 227 * physical addresses. If neither a load instruction nor store 228 * instruction is permitted to access the physical addresses, 229 * but an instruction fetch is permitted to access the physical 230 * addresses, whether a cache-block management instruction is 231 * permitted to access the cache block is UNSPECIFIED." 232 */ 233 ret = probe_access_flags(env, address, cbomlen, MMU_DATA_LOAD, 234 mmu_idx, true, &phost, ra); 235 if (ret != TLB_INVALID_MASK) { 236 /* Success: readable */ 237 return; 238 } 239 240 /* 241 * Since not readable, must be writable. On failure, store 242 * fault/store guest amo fault will be raised by 243 * riscv_cpu_tlb_fill(). PMP exceptions will be caught 244 * there as well. 245 */ 246 probe_write(env, address, cbomlen, mmu_idx, ra); 247 } 248 249 void helper_cbo_clean_flush(CPURISCVState *env, target_ulong address) 250 { 251 uintptr_t ra = GETPC(); 252 check_zicbo_envcfg(env, MENVCFG_CBCFE, ra); 253 check_zicbom_access(env, address, ra); 254 255 /* We don't emulate the cache-hierarchy, so we're done. */ 256 } 257 258 void helper_cbo_inval(CPURISCVState *env, target_ulong address) 259 { 260 uintptr_t ra = GETPC(); 261 check_zicbo_envcfg(env, MENVCFG_CBIE, ra); 262 check_zicbom_access(env, address, ra); 263 264 /* We don't emulate the cache-hierarchy, so we're done. */ 265 } 266 267 #ifndef CONFIG_USER_ONLY 268 269 target_ulong helper_sret(CPURISCVState *env) 270 { 271 uint64_t mstatus; 272 target_ulong prev_priv, prev_virt = env->virt_enabled; 273 274 if (!(env->priv >= PRV_S)) { 275 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 276 } 277 278 target_ulong retpc = env->sepc; 279 if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { 280 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); 281 } 282 283 if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) { 284 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 285 } 286 287 if (env->virt_enabled && get_field(env->hstatus, HSTATUS_VTSR)) { 288 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 289 } 290 291 mstatus = env->mstatus; 292 prev_priv = get_field(mstatus, MSTATUS_SPP); 293 mstatus = set_field(mstatus, MSTATUS_SIE, 294 get_field(mstatus, MSTATUS_SPIE)); 295 mstatus = set_field(mstatus, MSTATUS_SPIE, 1); 296 mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U); 297 if (env->priv_ver >= PRIV_VERSION_1_12_0) { 298 mstatus = set_field(mstatus, MSTATUS_MPRV, 0); 299 } 300 env->mstatus = mstatus; 301 302 if (riscv_has_ext(env, RVH) && !env->virt_enabled) { 303 /* We support Hypervisor extensions and virtulisation is disabled */ 304 target_ulong hstatus = env->hstatus; 305 306 prev_virt = get_field(hstatus, HSTATUS_SPV); 307 308 hstatus = set_field(hstatus, HSTATUS_SPV, 0); 309 310 env->hstatus = hstatus; 311 312 if (prev_virt) { 313 riscv_cpu_swap_hypervisor_regs(env); 314 } 315 } 316 317 riscv_cpu_set_mode(env, prev_priv, prev_virt); 318 319 /* 320 * If forward cfi enabled for new priv, restore elp status 321 * and clear spelp in mstatus 322 */ 323 if (cpu_get_fcfien(env)) { 324 env->elp = get_field(env->mstatus, MSTATUS_SPELP); 325 } 326 env->mstatus = set_field(env->mstatus, MSTATUS_SPELP, 0); 327 328 return retpc; 329 } 330 331 target_ulong helper_mret(CPURISCVState *env) 332 { 333 if (!(env->priv >= PRV_M)) { 334 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 335 } 336 337 target_ulong retpc = env->mepc; 338 if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { 339 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); 340 } 341 342 uint64_t mstatus = env->mstatus; 343 target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); 344 345 if (riscv_cpu_cfg(env)->pmp && 346 !pmp_get_num_rules(env) && (prev_priv != PRV_M)) { 347 riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); 348 } 349 350 target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) && 351 (prev_priv != PRV_M); 352 mstatus = set_field(mstatus, MSTATUS_MIE, 353 get_field(mstatus, MSTATUS_MPIE)); 354 mstatus = set_field(mstatus, MSTATUS_MPIE, 1); 355 mstatus = set_field(mstatus, MSTATUS_MPP, 356 riscv_has_ext(env, RVU) ? PRV_U : PRV_M); 357 mstatus = set_field(mstatus, MSTATUS_MPV, 0); 358 if ((env->priv_ver >= PRIV_VERSION_1_12_0) && (prev_priv != PRV_M)) { 359 mstatus = set_field(mstatus, MSTATUS_MPRV, 0); 360 } 361 env->mstatus = mstatus; 362 363 if (riscv_has_ext(env, RVH) && prev_virt) { 364 riscv_cpu_swap_hypervisor_regs(env); 365 } 366 367 riscv_cpu_set_mode(env, prev_priv, prev_virt); 368 /* 369 * If forward cfi enabled for new priv, restore elp status 370 * and clear mpelp in mstatus 371 */ 372 if (cpu_get_fcfien(env)) { 373 env->elp = get_field(env->mstatus, MSTATUS_MPELP); 374 } 375 env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, 0); 376 377 return retpc; 378 } 379 380 void helper_wfi(CPURISCVState *env) 381 { 382 CPUState *cs = env_cpu(env); 383 bool rvs = riscv_has_ext(env, RVS); 384 bool prv_u = env->priv == PRV_U; 385 bool prv_s = env->priv == PRV_S; 386 387 if (((prv_s || (!rvs && prv_u)) && get_field(env->mstatus, MSTATUS_TW)) || 388 (rvs && prv_u && !env->virt_enabled)) { 389 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 390 } else if (env->virt_enabled && 391 (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) { 392 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 393 } else { 394 cs->halted = 1; 395 cs->exception_index = EXCP_HLT; 396 cpu_loop_exit(cs); 397 } 398 } 399 400 void helper_wrs_nto(CPURISCVState *env) 401 { 402 if (env->virt_enabled && (env->priv == PRV_S || env->priv == PRV_U) && 403 get_field(env->hstatus, HSTATUS_VTW) && 404 !get_field(env->mstatus, MSTATUS_TW)) { 405 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 406 } else if (env->priv != PRV_M && get_field(env->mstatus, MSTATUS_TW)) { 407 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 408 } 409 } 410 411 void helper_tlb_flush(CPURISCVState *env) 412 { 413 CPUState *cs = env_cpu(env); 414 if (!env->virt_enabled && 415 (env->priv == PRV_U || 416 (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)))) { 417 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 418 } else if (env->virt_enabled && 419 (env->priv == PRV_U || get_field(env->hstatus, HSTATUS_VTVM))) { 420 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 421 } else { 422 tlb_flush(cs); 423 } 424 } 425 426 void helper_tlb_flush_all(CPURISCVState *env) 427 { 428 CPUState *cs = env_cpu(env); 429 tlb_flush_all_cpus_synced(cs); 430 } 431 432 void helper_hyp_tlb_flush(CPURISCVState *env) 433 { 434 CPUState *cs = env_cpu(env); 435 436 if (env->virt_enabled) { 437 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 438 } 439 440 if (env->priv == PRV_M || 441 (env->priv == PRV_S && !env->virt_enabled)) { 442 tlb_flush(cs); 443 return; 444 } 445 446 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 447 } 448 449 void helper_hyp_gvma_tlb_flush(CPURISCVState *env) 450 { 451 if (env->priv == PRV_S && !env->virt_enabled && 452 get_field(env->mstatus, MSTATUS_TVM)) { 453 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 454 } 455 456 helper_hyp_tlb_flush(env); 457 } 458 459 static int check_access_hlsv(CPURISCVState *env, bool x, uintptr_t ra) 460 { 461 if (env->priv == PRV_M) { 462 /* always allowed */ 463 } else if (env->virt_enabled) { 464 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra); 465 } else if (env->priv == PRV_U && !get_field(env->hstatus, HSTATUS_HU)) { 466 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); 467 } 468 469 int mode = get_field(env->hstatus, HSTATUS_SPVP); 470 if (!x && mode == PRV_S && get_field(env->vsstatus, MSTATUS_SUM)) { 471 mode = MMUIdx_S_SUM; 472 } 473 return mode | MMU_2STAGE_BIT; 474 } 475 476 target_ulong helper_hyp_hlv_bu(CPURISCVState *env, target_ulong addr) 477 { 478 uintptr_t ra = GETPC(); 479 int mmu_idx = check_access_hlsv(env, false, ra); 480 MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 481 482 return cpu_ldb_mmu(env, adjust_addr_virt(env, addr), oi, ra); 483 } 484 485 target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr) 486 { 487 uintptr_t ra = GETPC(); 488 int mmu_idx = check_access_hlsv(env, false, ra); 489 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx); 490 491 return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra); 492 } 493 494 target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr) 495 { 496 uintptr_t ra = GETPC(); 497 int mmu_idx = check_access_hlsv(env, false, ra); 498 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx); 499 500 return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra); 501 } 502 503 target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr) 504 { 505 uintptr_t ra = GETPC(); 506 int mmu_idx = check_access_hlsv(env, false, ra); 507 MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx); 508 509 return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra); 510 } 511 512 void helper_hyp_hsv_b(CPURISCVState *env, target_ulong addr, target_ulong val) 513 { 514 uintptr_t ra = GETPC(); 515 int mmu_idx = check_access_hlsv(env, false, ra); 516 MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 517 518 cpu_stb_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 519 } 520 521 void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val) 522 { 523 uintptr_t ra = GETPC(); 524 int mmu_idx = check_access_hlsv(env, false, ra); 525 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx); 526 527 cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 528 } 529 530 void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val) 531 { 532 uintptr_t ra = GETPC(); 533 int mmu_idx = check_access_hlsv(env, false, ra); 534 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx); 535 536 cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 537 } 538 539 void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val) 540 { 541 uintptr_t ra = GETPC(); 542 int mmu_idx = check_access_hlsv(env, false, ra); 543 MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx); 544 545 cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 546 } 547 548 /* 549 * TODO: These implementations are not quite correct. They perform the 550 * access using execute permission just fine, but the final PMP check 551 * is supposed to have read permission as well. Without replicating 552 * a fair fraction of cputlb.c, fixing this requires adding new mmu_idx 553 * which would imply that exact check in tlb_fill. 554 */ 555 target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr) 556 { 557 uintptr_t ra = GETPC(); 558 int mmu_idx = check_access_hlsv(env, true, ra); 559 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx); 560 561 return cpu_ldw_code_mmu(env, addr, oi, GETPC()); 562 } 563 564 target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr) 565 { 566 uintptr_t ra = GETPC(); 567 int mmu_idx = check_access_hlsv(env, true, ra); 568 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx); 569 570 return cpu_ldl_code_mmu(env, addr, oi, ra); 571 } 572 573 #endif /* !CONFIG_USER_ONLY */ 574