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/cputlb.h" 26 #include "exec/cpu_ldst.h" 27 #include "exec/helper-proto.h" 28 #include "exec/tlb-flags.h" 29 #include "trace.h" 30 31 /* Exceptions processing helpers */ 32 G_NORETURN void riscv_raise_exception(CPURISCVState *env, 33 RISCVException exception, 34 uintptr_t pc) 35 { 36 CPUState *cs = env_cpu(env); 37 38 trace_riscv_exception(exception, 39 riscv_cpu_get_trap_name(exception, false), 40 env->pc); 41 42 cs->exception_index = exception; 43 cpu_loop_exit_restore(cs, pc); 44 } 45 46 void helper_raise_exception(CPURISCVState *env, uint32_t exception) 47 { 48 riscv_raise_exception(env, exception, 0); 49 } 50 51 target_ulong helper_csrr(CPURISCVState *env, int csr) 52 { 53 /* 54 * The seed CSR must be accessed with a read-write instruction. A 55 * read-only instruction such as CSRRS/CSRRC with rs1=x0 or CSRRSI/ 56 * CSRRCI with uimm=0 will raise an illegal instruction exception. 57 */ 58 if (csr == CSR_SEED) { 59 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 60 } 61 62 target_ulong val = 0; 63 RISCVException ret = riscv_csrr(env, csr, &val); 64 65 if (ret != RISCV_EXCP_NONE) { 66 riscv_raise_exception(env, ret, GETPC()); 67 } 68 return val; 69 } 70 71 void helper_csrw(CPURISCVState *env, int csr, target_ulong src) 72 { 73 target_ulong mask = env->xl == MXL_RV32 ? UINT32_MAX : (target_ulong)-1; 74 RISCVException ret = riscv_csrrw(env, csr, NULL, src, mask); 75 76 if (ret != RISCV_EXCP_NONE) { 77 riscv_raise_exception(env, ret, GETPC()); 78 } 79 } 80 81 target_ulong helper_csrrw(CPURISCVState *env, int csr, 82 target_ulong src, target_ulong write_mask) 83 { 84 target_ulong val = 0; 85 RISCVException ret = riscv_csrrw(env, csr, &val, src, write_mask); 86 87 if (ret != RISCV_EXCP_NONE) { 88 riscv_raise_exception(env, ret, GETPC()); 89 } 90 return val; 91 } 92 93 target_ulong helper_csrr_i128(CPURISCVState *env, int csr) 94 { 95 Int128 rv = int128_zero(); 96 RISCVException ret = riscv_csrr_i128(env, csr, &rv); 97 98 if (ret != RISCV_EXCP_NONE) { 99 riscv_raise_exception(env, ret, GETPC()); 100 } 101 102 env->retxh = int128_gethi(rv); 103 return int128_getlo(rv); 104 } 105 106 void helper_csrw_i128(CPURISCVState *env, int csr, 107 target_ulong srcl, target_ulong srch) 108 { 109 RISCVException ret = riscv_csrrw_i128(env, csr, NULL, 110 int128_make128(srcl, srch), 111 UINT128_MAX); 112 113 if (ret != RISCV_EXCP_NONE) { 114 riscv_raise_exception(env, ret, GETPC()); 115 } 116 } 117 118 target_ulong helper_csrrw_i128(CPURISCVState *env, int csr, 119 target_ulong srcl, target_ulong srch, 120 target_ulong maskl, target_ulong maskh) 121 { 122 Int128 rv = int128_zero(); 123 RISCVException ret = riscv_csrrw_i128(env, csr, &rv, 124 int128_make128(srcl, srch), 125 int128_make128(maskl, maskh)); 126 127 if (ret != RISCV_EXCP_NONE) { 128 riscv_raise_exception(env, ret, GETPC()); 129 } 130 131 env->retxh = int128_gethi(rv); 132 return int128_getlo(rv); 133 } 134 135 136 /* 137 * check_zicbo_envcfg 138 * 139 * Raise virtual exceptions and illegal instruction exceptions for 140 * Zicbo[mz] instructions based on the settings of [mhs]envcfg as 141 * specified in section 2.5.1 of the CMO specification. 142 */ 143 static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits, 144 uintptr_t ra) 145 { 146 #ifndef CONFIG_USER_ONLY 147 if ((env->priv < PRV_M) && !get_field(env->menvcfg, envbits)) { 148 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); 149 } 150 151 if (env->virt_enabled && 152 (((env->priv <= PRV_S) && !get_field(env->henvcfg, envbits)) || 153 ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)))) { 154 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra); 155 } 156 157 if ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)) { 158 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); 159 } 160 #endif 161 } 162 163 void helper_cbo_zero(CPURISCVState *env, target_ulong address) 164 { 165 RISCVCPU *cpu = env_archcpu(env); 166 uint16_t cbozlen = cpu->cfg.cboz_blocksize; 167 int mmu_idx = riscv_env_mmu_index(env, false); 168 uintptr_t ra = GETPC(); 169 void *mem; 170 171 check_zicbo_envcfg(env, MENVCFG_CBZE, ra); 172 173 /* Mask off low-bits to align-down to the cache-block. */ 174 address &= ~(cbozlen - 1); 175 176 /* 177 * cbo.zero requires MMU_DATA_STORE access. Do a probe_write() 178 * to raise any exceptions, including PMP. 179 */ 180 mem = probe_write(env, address, cbozlen, mmu_idx, ra); 181 182 if (likely(mem)) { 183 memset(mem, 0, cbozlen); 184 } else { 185 /* 186 * This means that we're dealing with an I/O page. Section 4.2 187 * of cmobase v1.0.1 says: 188 * 189 * "Cache-block zero instructions store zeros independently 190 * of whether data from the underlying memory locations are 191 * cacheable." 192 * 193 * Write zeros in address + cbozlen regardless of not being 194 * a RAM page. 195 */ 196 for (int i = 0; i < cbozlen; i++) { 197 cpu_stb_mmuidx_ra(env, address + i, 0, mmu_idx, ra); 198 } 199 } 200 } 201 202 /* 203 * check_zicbom_access 204 * 205 * Check access permissions (LOAD, STORE or FETCH as specified in 206 * section 2.5.2 of the CMO specification) for Zicbom, raising 207 * either store page-fault (non-virtualized) or store guest-page 208 * fault (virtualized). 209 */ 210 static void check_zicbom_access(CPURISCVState *env, 211 target_ulong address, 212 uintptr_t ra) 213 { 214 RISCVCPU *cpu = env_archcpu(env); 215 int mmu_idx = riscv_env_mmu_index(env, false); 216 uint16_t cbomlen = cpu->cfg.cbom_blocksize; 217 void *phost; 218 int ret; 219 220 /* Mask off low-bits to align-down to the cache-block. */ 221 address &= ~(cbomlen - 1); 222 223 /* 224 * Section 2.5.2 of cmobase v1.0.1: 225 * 226 * "A cache-block management instruction is permitted to 227 * access the specified cache block whenever a load instruction 228 * or store instruction is permitted to access the corresponding 229 * physical addresses. If neither a load instruction nor store 230 * instruction is permitted to access the physical addresses, 231 * but an instruction fetch is permitted to access the physical 232 * addresses, whether a cache-block management instruction is 233 * permitted to access the cache block is UNSPECIFIED." 234 */ 235 ret = probe_access_flags(env, address, cbomlen, MMU_DATA_LOAD, 236 mmu_idx, true, &phost, ra); 237 if (ret != TLB_INVALID_MASK) { 238 /* Success: readable */ 239 return; 240 } 241 242 /* 243 * Since not readable, must be writable. On failure, store 244 * fault/store guest amo fault will be raised by 245 * riscv_cpu_tlb_fill(). PMP exceptions will be caught 246 * there as well. 247 */ 248 probe_write(env, address, cbomlen, mmu_idx, ra); 249 } 250 251 void helper_cbo_clean_flush(CPURISCVState *env, target_ulong address) 252 { 253 uintptr_t ra = GETPC(); 254 check_zicbo_envcfg(env, MENVCFG_CBCFE, ra); 255 check_zicbom_access(env, address, ra); 256 257 /* We don't emulate the cache-hierarchy, so we're done. */ 258 } 259 260 void helper_cbo_inval(CPURISCVState *env, target_ulong address) 261 { 262 uintptr_t ra = GETPC(); 263 check_zicbo_envcfg(env, MENVCFG_CBIE, ra); 264 check_zicbom_access(env, address, ra); 265 266 /* We don't emulate the cache-hierarchy, so we're done. */ 267 } 268 269 #ifndef CONFIG_USER_ONLY 270 271 target_ulong helper_sret(CPURISCVState *env) 272 { 273 uint64_t mstatus; 274 target_ulong prev_priv, prev_virt = env->virt_enabled; 275 const target_ulong src_priv = env->priv; 276 const bool src_virt = env->virt_enabled; 277 278 if (!(env->priv >= PRV_S)) { 279 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 280 } 281 282 target_ulong retpc = env->sepc; 283 if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg, 284 env->priv_ver, 285 env->misa_ext) && (retpc & 0x3)) { 286 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); 287 } 288 289 if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) { 290 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 291 } 292 293 if (env->virt_enabled && get_field(env->hstatus, HSTATUS_VTSR)) { 294 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 295 } 296 297 mstatus = env->mstatus; 298 prev_priv = get_field(mstatus, MSTATUS_SPP); 299 mstatus = set_field(mstatus, MSTATUS_SIE, 300 get_field(mstatus, MSTATUS_SPIE)); 301 mstatus = set_field(mstatus, MSTATUS_SPIE, 1); 302 mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U); 303 304 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 305 if (riscv_has_ext(env, RVH)) { 306 target_ulong prev_vu = get_field(env->hstatus, HSTATUS_SPV) && 307 prev_priv == PRV_U; 308 /* Returning to VU from HS, vsstatus.sdt = 0 */ 309 if (!env->virt_enabled && prev_vu) { 310 env->vsstatus = set_field(env->vsstatus, MSTATUS_SDT, 0); 311 } 312 } 313 mstatus = set_field(mstatus, MSTATUS_SDT, 0); 314 } 315 if (riscv_cpu_cfg(env)->ext_smdbltrp && env->priv >= PRV_M) { 316 mstatus = set_field(mstatus, MSTATUS_MDT, 0); 317 } 318 if (env->priv_ver >= PRIV_VERSION_1_12_0) { 319 mstatus = set_field(mstatus, MSTATUS_MPRV, 0); 320 } 321 env->mstatus = mstatus; 322 323 if (riscv_has_ext(env, RVH) && !env->virt_enabled) { 324 /* We support Hypervisor extensions and virtulisation is disabled */ 325 target_ulong hstatus = env->hstatus; 326 327 prev_virt = get_field(hstatus, HSTATUS_SPV); 328 hstatus = set_field(hstatus, HSTATUS_SPV, 0); 329 330 env->hstatus = hstatus; 331 332 if (prev_virt) { 333 riscv_cpu_swap_hypervisor_regs(env); 334 } 335 } 336 337 riscv_cpu_set_mode(env, prev_priv, prev_virt); 338 339 /* 340 * If forward cfi enabled for new priv, restore elp status 341 * and clear spelp in mstatus 342 */ 343 if (cpu_get_fcfien(env)) { 344 env->elp = get_field(env->mstatus, MSTATUS_SPELP); 345 } 346 env->mstatus = set_field(env->mstatus, MSTATUS_SPELP, 0); 347 348 if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) { 349 riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET, 350 src_priv, src_virt); 351 } 352 353 return retpc; 354 } 355 356 static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc, 357 target_ulong prev_priv) 358 { 359 if (!(env->priv >= PRV_M)) { 360 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 361 } 362 363 if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg, 364 env->priv_ver, 365 env->misa_ext) && (retpc & 0x3)) { 366 riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); 367 } 368 369 if (riscv_cpu_cfg(env)->pmp && 370 !pmp_get_num_rules(env) && (prev_priv != PRV_M)) { 371 riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); 372 } 373 } 374 static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus, 375 target_ulong prev_priv, 376 target_ulong prev_virt) 377 { 378 /* If returning to U, VS or VU, sstatus.sdt = 0 */ 379 if (prev_priv == PRV_U || (prev_virt && 380 (prev_priv == PRV_S || prev_priv == PRV_U))) { 381 mstatus = set_field(mstatus, MSTATUS_SDT, 0); 382 /* If returning to VU, vsstatus.sdt = 0 */ 383 if (prev_virt && prev_priv == PRV_U) { 384 env->vsstatus = set_field(env->vsstatus, MSTATUS_SDT, 0); 385 } 386 } 387 388 return mstatus; 389 } 390 391 target_ulong helper_mret(CPURISCVState *env) 392 { 393 target_ulong retpc = env->mepc; 394 uint64_t mstatus = env->mstatus; 395 target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); 396 397 check_ret_from_m_mode(env, retpc, prev_priv); 398 399 target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) && 400 (prev_priv != PRV_M); 401 mstatus = set_field(mstatus, MSTATUS_MIE, 402 get_field(mstatus, MSTATUS_MPIE)); 403 mstatus = set_field(mstatus, MSTATUS_MPIE, 1); 404 mstatus = set_field(mstatus, MSTATUS_MPP, 405 riscv_has_ext(env, RVU) ? PRV_U : PRV_M); 406 mstatus = set_field(mstatus, MSTATUS_MPV, 0); 407 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 408 mstatus = ssdbltrp_mxret(env, mstatus, prev_priv, prev_virt); 409 } 410 if (riscv_cpu_cfg(env)->ext_smdbltrp) { 411 mstatus = set_field(mstatus, MSTATUS_MDT, 0); 412 } 413 if ((env->priv_ver >= PRIV_VERSION_1_12_0) && (prev_priv != PRV_M)) { 414 mstatus = set_field(mstatus, MSTATUS_MPRV, 0); 415 } 416 env->mstatus = mstatus; 417 418 if (riscv_has_ext(env, RVH) && prev_virt) { 419 riscv_cpu_swap_hypervisor_regs(env); 420 } 421 422 riscv_cpu_set_mode(env, prev_priv, prev_virt); 423 /* 424 * If forward cfi enabled for new priv, restore elp status 425 * and clear mpelp in mstatus 426 */ 427 if (cpu_get_fcfien(env)) { 428 env->elp = get_field(env->mstatus, MSTATUS_MPELP); 429 } 430 env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, 0); 431 432 if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) { 433 riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET, 434 PRV_M, false); 435 } 436 437 return retpc; 438 } 439 440 target_ulong helper_mnret(CPURISCVState *env) 441 { 442 target_ulong retpc = env->mnepc; 443 target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP); 444 target_ulong prev_virt; 445 446 check_ret_from_m_mode(env, retpc, prev_priv); 447 448 prev_virt = get_field(env->mnstatus, MNSTATUS_MNPV) && 449 (prev_priv != PRV_M); 450 env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, true); 451 452 /* 453 * If MNRET changes the privilege mode to a mode 454 * less privileged than M, it also sets mstatus.MPRV to 0. 455 */ 456 if (prev_priv < PRV_M) { 457 env->mstatus = set_field(env->mstatus, MSTATUS_MPRV, false); 458 } 459 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 460 env->mstatus = ssdbltrp_mxret(env, env->mstatus, prev_priv, prev_virt); 461 } 462 463 if (riscv_cpu_cfg(env)->ext_smdbltrp) { 464 if (prev_priv < PRV_M) { 465 env->mstatus = set_field(env->mstatus, MSTATUS_MDT, 0); 466 } 467 } 468 469 if (riscv_has_ext(env, RVH) && prev_virt) { 470 riscv_cpu_swap_hypervisor_regs(env); 471 } 472 473 riscv_cpu_set_mode(env, prev_priv, prev_virt); 474 475 /* 476 * If forward cfi enabled for new priv, restore elp status 477 * and clear mnpelp in mnstatus 478 */ 479 if (cpu_get_fcfien(env)) { 480 env->elp = get_field(env->mnstatus, MNSTATUS_MNPELP); 481 } 482 env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, 0); 483 484 return retpc; 485 } 486 487 void helper_ctr_add_entry(CPURISCVState *env, target_ulong src, 488 target_ulong dest, target_ulong type) 489 { 490 riscv_ctr_add_entry(env, src, dest, (enum CTRType)type, 491 env->priv, env->virt_enabled); 492 } 493 494 void helper_ctr_clear(CPURISCVState *env) 495 { 496 /* 497 * It's safe to call smstateen_acc_ok() for umode access regardless of the 498 * state of bit 54 (CTR bit in case of m/hstateen) of sstateen. If the bit 499 * is zero, smstateen_acc_ok() will return the correct exception code and 500 * if it's one, smstateen_acc_ok() will return RISCV_EXCP_NONE. In that 501 * scenario the U-mode check below will handle that case. 502 */ 503 RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); 504 if (ret != RISCV_EXCP_NONE) { 505 riscv_raise_exception(env, ret, GETPC()); 506 } 507 508 if (env->priv == PRV_U) { 509 /* 510 * One corner case is when sctrclr is executed from VU-mode and 511 * mstateen.CTR = 0, in which case we are supposed to raise 512 * RISCV_EXCP_ILLEGAL_INST. This case is already handled in 513 * smstateen_acc_ok(). 514 */ 515 uint32_t excep = env->virt_enabled ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT : 516 RISCV_EXCP_ILLEGAL_INST; 517 riscv_raise_exception(env, excep, GETPC()); 518 } 519 520 riscv_ctr_clear(env); 521 } 522 523 void helper_wfi(CPURISCVState *env) 524 { 525 CPUState *cs = env_cpu(env); 526 bool rvs = riscv_has_ext(env, RVS); 527 bool prv_u = env->priv == PRV_U; 528 bool prv_s = env->priv == PRV_S; 529 530 if (((prv_s || (!rvs && prv_u)) && get_field(env->mstatus, MSTATUS_TW)) || 531 (rvs && prv_u && !env->virt_enabled)) { 532 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 533 } else if (env->virt_enabled && 534 (prv_u || (prv_s && get_field(env->hstatus, HSTATUS_VTW)))) { 535 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 536 } else { 537 cs->halted = 1; 538 cs->exception_index = EXCP_HLT; 539 cpu_loop_exit(cs); 540 } 541 } 542 543 void helper_wrs_nto(CPURISCVState *env) 544 { 545 if (env->virt_enabled && (env->priv == PRV_S || env->priv == PRV_U) && 546 get_field(env->hstatus, HSTATUS_VTW) && 547 !get_field(env->mstatus, MSTATUS_TW)) { 548 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 549 } else if (env->priv != PRV_M && get_field(env->mstatus, MSTATUS_TW)) { 550 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 551 } 552 } 553 554 void helper_tlb_flush(CPURISCVState *env) 555 { 556 CPUState *cs = env_cpu(env); 557 if (!env->virt_enabled && 558 (env->priv == PRV_U || 559 (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)))) { 560 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 561 } else if (env->virt_enabled && 562 (env->priv == PRV_U || get_field(env->hstatus, HSTATUS_VTVM))) { 563 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 564 } else { 565 tlb_flush(cs); 566 } 567 } 568 569 void helper_tlb_flush_all(CPURISCVState *env) 570 { 571 CPUState *cs = env_cpu(env); 572 tlb_flush_all_cpus_synced(cs); 573 } 574 575 void helper_hyp_tlb_flush(CPURISCVState *env) 576 { 577 CPUState *cs = env_cpu(env); 578 579 if (env->virt_enabled) { 580 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); 581 } 582 583 if (env->priv == PRV_M || 584 (env->priv == PRV_S && !env->virt_enabled)) { 585 tlb_flush(cs); 586 return; 587 } 588 589 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 590 } 591 592 void helper_hyp_gvma_tlb_flush(CPURISCVState *env) 593 { 594 if (env->priv == PRV_S && !env->virt_enabled && 595 get_field(env->mstatus, MSTATUS_TVM)) { 596 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 597 } 598 599 helper_hyp_tlb_flush(env); 600 } 601 602 static int check_access_hlsv(CPURISCVState *env, bool x, uintptr_t ra) 603 { 604 if (env->priv == PRV_M) { 605 /* always allowed */ 606 } else if (env->virt_enabled) { 607 riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra); 608 } else if (env->priv == PRV_U && !get_field(env->hstatus, HSTATUS_HU)) { 609 riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); 610 } 611 612 int mode = get_field(env->hstatus, HSTATUS_SPVP); 613 if (!x && mode == PRV_S && get_field(env->vsstatus, MSTATUS_SUM)) { 614 mode = MMUIdx_S_SUM; 615 } 616 return mode | MMU_2STAGE_BIT; 617 } 618 619 target_ulong helper_hyp_hlv_bu(CPURISCVState *env, target_ulong addr) 620 { 621 uintptr_t ra = GETPC(); 622 int mmu_idx = check_access_hlsv(env, false, ra); 623 MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 624 625 return cpu_ldb_mmu(env, adjust_addr_virt(env, addr), oi, ra); 626 } 627 628 target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr) 629 { 630 uintptr_t ra = GETPC(); 631 int mmu_idx = check_access_hlsv(env, false, ra); 632 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx); 633 634 return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra); 635 } 636 637 target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr) 638 { 639 uintptr_t ra = GETPC(); 640 int mmu_idx = check_access_hlsv(env, false, ra); 641 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx); 642 643 return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra); 644 } 645 646 target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr) 647 { 648 uintptr_t ra = GETPC(); 649 int mmu_idx = check_access_hlsv(env, false, ra); 650 MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx); 651 652 return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra); 653 } 654 655 void helper_hyp_hsv_b(CPURISCVState *env, target_ulong addr, target_ulong val) 656 { 657 uintptr_t ra = GETPC(); 658 int mmu_idx = check_access_hlsv(env, false, ra); 659 MemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 660 661 cpu_stb_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 662 } 663 664 void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val) 665 { 666 uintptr_t ra = GETPC(); 667 int mmu_idx = check_access_hlsv(env, false, ra); 668 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx); 669 670 cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 671 } 672 673 void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val) 674 { 675 uintptr_t ra = GETPC(); 676 int mmu_idx = check_access_hlsv(env, false, ra); 677 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx); 678 679 cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 680 } 681 682 void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val) 683 { 684 uintptr_t ra = GETPC(); 685 int mmu_idx = check_access_hlsv(env, false, ra); 686 MemOpIdx oi = make_memop_idx(MO_TEUQ, mmu_idx); 687 688 cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra); 689 } 690 691 /* 692 * TODO: These implementations are not quite correct. They perform the 693 * access using execute permission just fine, but the final PMP check 694 * is supposed to have read permission as well. Without replicating 695 * a fair fraction of cputlb.c, fixing this requires adding new mmu_idx 696 * which would imply that exact check in tlb_fill. 697 */ 698 target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr) 699 { 700 uintptr_t ra = GETPC(); 701 int mmu_idx = check_access_hlsv(env, true, ra); 702 MemOpIdx oi = make_memop_idx(MO_TEUW, mmu_idx); 703 704 return cpu_ldw_code_mmu(env, addr, oi, GETPC()); 705 } 706 707 target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr) 708 { 709 uintptr_t ra = GETPC(); 710 int mmu_idx = check_access_hlsv(env, true, ra); 711 MemOpIdx oi = make_memop_idx(MO_TEUL, mmu_idx); 712 713 return cpu_ldl_code_mmu(env, addr, oi, ra); 714 } 715 716 #endif /* !CONFIG_USER_ONLY */ 717