1 /* 2 * RISC-V Control and Status Registers. 3 * 4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 * Copyright (c) 2017-2018 SiFive, Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2 or later, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * You should have received a copy of the GNU General Public License along with 17 * this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu/log.h" 22 #include "qemu/timer.h" 23 #include "cpu.h" 24 #include "tcg/tcg-cpu.h" 25 #include "pmu.h" 26 #include "time_helper.h" 27 #include "exec/cputlb.h" 28 #include "exec/icount.h" 29 #include "accel/tcg/getpc.h" 30 #include "qemu/guest-random.h" 31 #include "qapi/error.h" 32 #include "tcg/insn-start-words.h" 33 #include "internals.h" 34 #include <stdbool.h> 35 36 /* CSR function table public API */ 37 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops) 38 { 39 *ops = csr_ops[csrno & (CSR_TABLE_SIZE - 1)]; 40 } 41 42 void riscv_set_csr_ops(int csrno, const riscv_csr_operations *ops) 43 { 44 csr_ops[csrno & (CSR_TABLE_SIZE - 1)] = *ops; 45 } 46 47 /* Predicates */ 48 #if !defined(CONFIG_USER_ONLY) 49 RISCVException smstateen_acc_ok(CPURISCVState *env, int index, uint64_t bit) 50 { 51 bool virt = env->virt_enabled; 52 53 if (env->priv == PRV_M || !riscv_cpu_cfg(env)->ext_smstateen) { 54 return RISCV_EXCP_NONE; 55 } 56 57 if (!(env->mstateen[index] & bit)) { 58 return RISCV_EXCP_ILLEGAL_INST; 59 } 60 61 if (virt) { 62 if (!(env->hstateen[index] & bit)) { 63 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 64 } 65 66 if (env->priv == PRV_U && !(env->sstateen[index] & bit)) { 67 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 68 } 69 } 70 71 if (env->priv == PRV_U && riscv_has_ext(env, RVS)) { 72 if (!(env->sstateen[index] & bit)) { 73 return RISCV_EXCP_ILLEGAL_INST; 74 } 75 } 76 77 return RISCV_EXCP_NONE; 78 } 79 #endif 80 81 static RISCVException fs(CPURISCVState *env, int csrno) 82 { 83 #if !defined(CONFIG_USER_ONLY) 84 if (!env->debugger && !riscv_cpu_fp_enabled(env) && 85 !riscv_cpu_cfg(env)->ext_zfinx) { 86 return RISCV_EXCP_ILLEGAL_INST; 87 } 88 89 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 90 return smstateen_acc_ok(env, 0, SMSTATEEN0_FCSR); 91 } 92 #endif 93 return RISCV_EXCP_NONE; 94 } 95 96 static RISCVException vs(CPURISCVState *env, int csrno) 97 { 98 if (riscv_cpu_cfg(env)->ext_zve32x) { 99 #if !defined(CONFIG_USER_ONLY) 100 if (!env->debugger && !riscv_cpu_vector_enabled(env)) { 101 return RISCV_EXCP_ILLEGAL_INST; 102 } 103 #endif 104 return RISCV_EXCP_NONE; 105 } 106 return RISCV_EXCP_ILLEGAL_INST; 107 } 108 109 static RISCVException ctr(CPURISCVState *env, int csrno) 110 { 111 #if !defined(CONFIG_USER_ONLY) 112 RISCVCPU *cpu = env_archcpu(env); 113 int ctr_index; 114 target_ulong ctr_mask; 115 int base_csrno = CSR_CYCLE; 116 bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false; 117 118 if (rv32 && csrno >= CSR_CYCLEH) { 119 /* Offset for RV32 hpmcounternh counters */ 120 base_csrno += 0x80; 121 } 122 ctr_index = csrno - base_csrno; 123 ctr_mask = BIT(ctr_index); 124 125 if ((csrno >= CSR_CYCLE && csrno <= CSR_INSTRET) || 126 (csrno >= CSR_CYCLEH && csrno <= CSR_INSTRETH)) { 127 if (!riscv_cpu_cfg(env)->ext_zicntr) { 128 return RISCV_EXCP_ILLEGAL_INST; 129 } 130 131 goto skip_ext_pmu_check; 132 } 133 134 if (!(cpu->pmu_avail_ctrs & ctr_mask)) { 135 /* No counter is enabled in PMU or the counter is out of range */ 136 return RISCV_EXCP_ILLEGAL_INST; 137 } 138 139 skip_ext_pmu_check: 140 141 if (env->debugger) { 142 return RISCV_EXCP_NONE; 143 } 144 145 if (env->priv < PRV_M && !get_field(env->mcounteren, ctr_mask)) { 146 return RISCV_EXCP_ILLEGAL_INST; 147 } 148 149 if (env->virt_enabled) { 150 if (!get_field(env->hcounteren, ctr_mask) || 151 (env->priv == PRV_U && !get_field(env->scounteren, ctr_mask))) { 152 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 153 } 154 } 155 156 if (riscv_has_ext(env, RVS) && env->priv == PRV_U && 157 !get_field(env->scounteren, ctr_mask)) { 158 return RISCV_EXCP_ILLEGAL_INST; 159 } 160 161 #endif 162 return RISCV_EXCP_NONE; 163 } 164 165 static RISCVException ctr32(CPURISCVState *env, int csrno) 166 { 167 if (riscv_cpu_mxl(env) != MXL_RV32) { 168 return RISCV_EXCP_ILLEGAL_INST; 169 } 170 171 return ctr(env, csrno); 172 } 173 174 static RISCVException zcmt(CPURISCVState *env, int csrno) 175 { 176 if (!riscv_cpu_cfg(env)->ext_zcmt) { 177 return RISCV_EXCP_ILLEGAL_INST; 178 } 179 180 #if !defined(CONFIG_USER_ONLY) 181 RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_JVT); 182 if (ret != RISCV_EXCP_NONE) { 183 return ret; 184 } 185 #endif 186 187 return RISCV_EXCP_NONE; 188 } 189 190 static RISCVException cfi_ss(CPURISCVState *env, int csrno) 191 { 192 if (!env_archcpu(env)->cfg.ext_zicfiss) { 193 return RISCV_EXCP_ILLEGAL_INST; 194 } 195 196 /* If ext implemented, M-mode always have access to SSP CSR */ 197 if (env->priv == PRV_M) { 198 return RISCV_EXCP_NONE; 199 } 200 201 /* if bcfi not active for current env, access to csr is illegal */ 202 if (!cpu_get_bcfien(env)) { 203 #if !defined(CONFIG_USER_ONLY) 204 if (env->debugger) { 205 return RISCV_EXCP_NONE; 206 } else if (env->virt_enabled) { 207 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 208 } 209 #endif 210 return RISCV_EXCP_ILLEGAL_INST; 211 } 212 213 return RISCV_EXCP_NONE; 214 } 215 216 #if !defined(CONFIG_USER_ONLY) 217 static RISCVException mctr(CPURISCVState *env, int csrno) 218 { 219 RISCVCPU *cpu = env_archcpu(env); 220 uint32_t pmu_avail_ctrs = cpu->pmu_avail_ctrs; 221 int ctr_index; 222 int base_csrno = CSR_MHPMCOUNTER3; 223 224 if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) { 225 /* Offset for RV32 mhpmcounternh counters */ 226 csrno -= 0x80; 227 } 228 229 g_assert(csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31); 230 231 ctr_index = csrno - base_csrno; 232 if ((BIT(ctr_index) & pmu_avail_ctrs >> 3) == 0) { 233 /* The PMU is not enabled or counter is out of range */ 234 return RISCV_EXCP_ILLEGAL_INST; 235 } 236 237 return RISCV_EXCP_NONE; 238 } 239 240 static RISCVException mctr32(CPURISCVState *env, int csrno) 241 { 242 if (riscv_cpu_mxl(env) != MXL_RV32) { 243 return RISCV_EXCP_ILLEGAL_INST; 244 } 245 246 return mctr(env, csrno); 247 } 248 249 static RISCVException sscofpmf(CPURISCVState *env, int csrno) 250 { 251 if (!riscv_cpu_cfg(env)->ext_sscofpmf) { 252 return RISCV_EXCP_ILLEGAL_INST; 253 } 254 255 return RISCV_EXCP_NONE; 256 } 257 258 static RISCVException sscofpmf_32(CPURISCVState *env, int csrno) 259 { 260 if (riscv_cpu_mxl(env) != MXL_RV32) { 261 return RISCV_EXCP_ILLEGAL_INST; 262 } 263 264 return sscofpmf(env, csrno); 265 } 266 267 static RISCVException smcntrpmf(CPURISCVState *env, int csrno) 268 { 269 if (!riscv_cpu_cfg(env)->ext_smcntrpmf) { 270 return RISCV_EXCP_ILLEGAL_INST; 271 } 272 273 return RISCV_EXCP_NONE; 274 } 275 276 static RISCVException smcntrpmf_32(CPURISCVState *env, int csrno) 277 { 278 if (riscv_cpu_mxl(env) != MXL_RV32) { 279 return RISCV_EXCP_ILLEGAL_INST; 280 } 281 282 return smcntrpmf(env, csrno); 283 } 284 285 static RISCVException any(CPURISCVState *env, int csrno) 286 { 287 return RISCV_EXCP_NONE; 288 } 289 290 static RISCVException any32(CPURISCVState *env, int csrno) 291 { 292 if (riscv_cpu_mxl(env) != MXL_RV32) { 293 return RISCV_EXCP_ILLEGAL_INST; 294 } 295 296 return any(env, csrno); 297 298 } 299 300 static RISCVException aia_any(CPURISCVState *env, int csrno) 301 { 302 if (!riscv_cpu_cfg(env)->ext_smaia) { 303 return RISCV_EXCP_ILLEGAL_INST; 304 } 305 306 return any(env, csrno); 307 } 308 309 static RISCVException aia_any32(CPURISCVState *env, int csrno) 310 { 311 if (!riscv_cpu_cfg(env)->ext_smaia) { 312 return RISCV_EXCP_ILLEGAL_INST; 313 } 314 315 return any32(env, csrno); 316 } 317 318 static RISCVException csrind_any(CPURISCVState *env, int csrno) 319 { 320 if (!riscv_cpu_cfg(env)->ext_smcsrind) { 321 return RISCV_EXCP_ILLEGAL_INST; 322 } 323 324 return RISCV_EXCP_NONE; 325 } 326 327 static RISCVException csrind_or_aia_any(CPURISCVState *env, int csrno) 328 { 329 if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrind) { 330 return RISCV_EXCP_ILLEGAL_INST; 331 } 332 333 return any(env, csrno); 334 } 335 336 static RISCVException smode(CPURISCVState *env, int csrno) 337 { 338 if (riscv_has_ext(env, RVS)) { 339 return RISCV_EXCP_NONE; 340 } 341 342 return RISCV_EXCP_ILLEGAL_INST; 343 } 344 345 static RISCVException smode32(CPURISCVState *env, int csrno) 346 { 347 if (riscv_cpu_mxl(env) != MXL_RV32) { 348 return RISCV_EXCP_ILLEGAL_INST; 349 } 350 351 return smode(env, csrno); 352 } 353 354 static RISCVException aia_smode(CPURISCVState *env, int csrno) 355 { 356 int ret; 357 358 if (!riscv_cpu_cfg(env)->ext_ssaia) { 359 return RISCV_EXCP_ILLEGAL_INST; 360 } 361 362 if (csrno == CSR_STOPEI) { 363 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_IMSIC); 364 } else { 365 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); 366 } 367 368 if (ret != RISCV_EXCP_NONE) { 369 return ret; 370 } 371 372 return smode(env, csrno); 373 } 374 375 static RISCVException aia_smode32(CPURISCVState *env, int csrno) 376 { 377 int ret; 378 int csr_priv = get_field(csrno, 0x300); 379 380 if (csr_priv == PRV_M && !riscv_cpu_cfg(env)->ext_smaia) { 381 return RISCV_EXCP_ILLEGAL_INST; 382 } else if (!riscv_cpu_cfg(env)->ext_ssaia) { 383 return RISCV_EXCP_ILLEGAL_INST; 384 } 385 386 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); 387 if (ret != RISCV_EXCP_NONE) { 388 return ret; 389 } 390 391 return smode32(env, csrno); 392 } 393 394 static RISCVException scountinhibit_pred(CPURISCVState *env, int csrno) 395 { 396 RISCVCPU *cpu = env_archcpu(env); 397 398 if (!cpu->cfg.ext_ssccfg || !cpu->cfg.ext_smcdeleg) { 399 return RISCV_EXCP_ILLEGAL_INST; 400 } 401 402 if (env->virt_enabled) { 403 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 404 } 405 406 return smode(env, csrno); 407 } 408 409 static bool csrind_extensions_present(CPURISCVState *env) 410 { 411 return riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind; 412 } 413 414 static bool aia_extensions_present(CPURISCVState *env) 415 { 416 return riscv_cpu_cfg(env)->ext_smaia || riscv_cpu_cfg(env)->ext_ssaia; 417 } 418 419 static bool csrind_or_aia_extensions_present(CPURISCVState *env) 420 { 421 return csrind_extensions_present(env) || aia_extensions_present(env); 422 } 423 424 static RISCVException csrind_smode(CPURISCVState *env, int csrno) 425 { 426 if (!csrind_extensions_present(env)) { 427 return RISCV_EXCP_ILLEGAL_INST; 428 } 429 430 return smode(env, csrno); 431 } 432 433 static RISCVException csrind_or_aia_smode(CPURISCVState *env, int csrno) 434 { 435 if (!csrind_or_aia_extensions_present(env)) { 436 return RISCV_EXCP_ILLEGAL_INST; 437 } 438 439 return smode(env, csrno); 440 } 441 442 static RISCVException hmode(CPURISCVState *env, int csrno) 443 { 444 if (riscv_has_ext(env, RVH)) { 445 return RISCV_EXCP_NONE; 446 } 447 448 return RISCV_EXCP_ILLEGAL_INST; 449 } 450 451 static RISCVException hmode32(CPURISCVState *env, int csrno) 452 { 453 if (riscv_cpu_mxl(env) != MXL_RV32) { 454 return RISCV_EXCP_ILLEGAL_INST; 455 } 456 457 return hmode(env, csrno); 458 459 } 460 461 static RISCVException csrind_hmode(CPURISCVState *env, int csrno) 462 { 463 if (!csrind_extensions_present(env)) { 464 return RISCV_EXCP_ILLEGAL_INST; 465 } 466 467 return hmode(env, csrno); 468 } 469 470 static RISCVException csrind_or_aia_hmode(CPURISCVState *env, int csrno) 471 { 472 if (!csrind_or_aia_extensions_present(env)) { 473 return RISCV_EXCP_ILLEGAL_INST; 474 } 475 476 return hmode(env, csrno); 477 } 478 479 static RISCVException umode(CPURISCVState *env, int csrno) 480 { 481 if (riscv_has_ext(env, RVU)) { 482 return RISCV_EXCP_NONE; 483 } 484 485 return RISCV_EXCP_ILLEGAL_INST; 486 } 487 488 static RISCVException umode32(CPURISCVState *env, int csrno) 489 { 490 if (riscv_cpu_mxl(env) != MXL_RV32) { 491 return RISCV_EXCP_ILLEGAL_INST; 492 } 493 494 return umode(env, csrno); 495 } 496 497 static RISCVException mstateen(CPURISCVState *env, int csrno) 498 { 499 if (!riscv_cpu_cfg(env)->ext_smstateen) { 500 return RISCV_EXCP_ILLEGAL_INST; 501 } 502 503 return any(env, csrno); 504 } 505 506 static RISCVException hstateen_pred(CPURISCVState *env, int csrno, int base) 507 { 508 if (!riscv_cpu_cfg(env)->ext_smstateen) { 509 return RISCV_EXCP_ILLEGAL_INST; 510 } 511 512 RISCVException ret = hmode(env, csrno); 513 if (ret != RISCV_EXCP_NONE) { 514 return ret; 515 } 516 517 if (env->debugger) { 518 return RISCV_EXCP_NONE; 519 } 520 521 if (env->priv < PRV_M) { 522 if (!(env->mstateen[csrno - base] & SMSTATEEN_STATEEN)) { 523 return RISCV_EXCP_ILLEGAL_INST; 524 } 525 } 526 527 return RISCV_EXCP_NONE; 528 } 529 530 static RISCVException hstateen(CPURISCVState *env, int csrno) 531 { 532 return hstateen_pred(env, csrno, CSR_HSTATEEN0); 533 } 534 535 static RISCVException hstateenh(CPURISCVState *env, int csrno) 536 { 537 return hstateen_pred(env, csrno, CSR_HSTATEEN0H); 538 } 539 540 static RISCVException sstateen(CPURISCVState *env, int csrno) 541 { 542 bool virt = env->virt_enabled; 543 int index = csrno - CSR_SSTATEEN0; 544 545 if (!riscv_cpu_cfg(env)->ext_smstateen) { 546 return RISCV_EXCP_ILLEGAL_INST; 547 } 548 549 RISCVException ret = smode(env, csrno); 550 if (ret != RISCV_EXCP_NONE) { 551 return ret; 552 } 553 554 if (env->debugger) { 555 return RISCV_EXCP_NONE; 556 } 557 558 if (env->priv < PRV_M) { 559 if (!(env->mstateen[index] & SMSTATEEN_STATEEN)) { 560 return RISCV_EXCP_ILLEGAL_INST; 561 } 562 563 if (virt) { 564 if (!(env->hstateen[index] & SMSTATEEN_STATEEN)) { 565 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 566 } 567 } 568 } 569 570 return RISCV_EXCP_NONE; 571 } 572 573 static RISCVException sstc(CPURISCVState *env, int csrno) 574 { 575 bool hmode_check = false; 576 577 if (!riscv_cpu_cfg(env)->ext_sstc || !env->rdtime_fn) { 578 return RISCV_EXCP_ILLEGAL_INST; 579 } 580 581 if ((csrno == CSR_VSTIMECMP) || (csrno == CSR_VSTIMECMPH)) { 582 hmode_check = true; 583 } 584 585 RISCVException ret = hmode_check ? hmode(env, csrno) : smode(env, csrno); 586 if (ret != RISCV_EXCP_NONE) { 587 return ret; 588 } 589 590 if (env->debugger) { 591 return RISCV_EXCP_NONE; 592 } 593 594 if (env->priv == PRV_M) { 595 return RISCV_EXCP_NONE; 596 } 597 598 /* 599 * No need of separate function for rv32 as menvcfg stores both menvcfg 600 * menvcfgh for RV32. 601 */ 602 if (!(get_field(env->mcounteren, COUNTEREN_TM) && 603 get_field(env->menvcfg, MENVCFG_STCE))) { 604 return RISCV_EXCP_ILLEGAL_INST; 605 } 606 607 if (env->virt_enabled) { 608 if (!(get_field(env->hcounteren, COUNTEREN_TM) && 609 get_field(env->henvcfg, HENVCFG_STCE))) { 610 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 611 } 612 } 613 614 return RISCV_EXCP_NONE; 615 } 616 617 static RISCVException sstc_32(CPURISCVState *env, int csrno) 618 { 619 if (riscv_cpu_mxl(env) != MXL_RV32) { 620 return RISCV_EXCP_ILLEGAL_INST; 621 } 622 623 return sstc(env, csrno); 624 } 625 626 static RISCVException satp(CPURISCVState *env, int csrno) 627 { 628 if (env->priv == PRV_S && !env->virt_enabled && 629 get_field(env->mstatus, MSTATUS_TVM)) { 630 return RISCV_EXCP_ILLEGAL_INST; 631 } 632 if (env->priv == PRV_S && env->virt_enabled && 633 get_field(env->hstatus, HSTATUS_VTVM)) { 634 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 635 } 636 637 return smode(env, csrno); 638 } 639 640 static RISCVException hgatp(CPURISCVState *env, int csrno) 641 { 642 if (env->priv == PRV_S && !env->virt_enabled && 643 get_field(env->mstatus, MSTATUS_TVM)) { 644 return RISCV_EXCP_ILLEGAL_INST; 645 } 646 647 return hmode(env, csrno); 648 } 649 650 /* 651 * M-mode: 652 * Without ext_smctr raise illegal inst excep. 653 * Otherwise everything is accessible to m-mode. 654 * 655 * S-mode: 656 * Without ext_ssctr or mstateen.ctr raise illegal inst excep. 657 * Otherwise everything other than mctrctl is accessible. 658 * 659 * VS-mode: 660 * Without ext_ssctr or mstateen.ctr raise illegal inst excep. 661 * Without hstateen.ctr raise virtual illegal inst excep. 662 * Otherwise allow sctrctl (vsctrctl), sctrstatus, 0x200-0x2ff entry range. 663 * Always raise illegal instruction exception for sctrdepth. 664 */ 665 static RISCVException ctr_mmode(CPURISCVState *env, int csrno) 666 { 667 /* Check if smctr-ext is present */ 668 if (riscv_cpu_cfg(env)->ext_smctr) { 669 return RISCV_EXCP_NONE; 670 } 671 672 return RISCV_EXCP_ILLEGAL_INST; 673 } 674 675 static RISCVException ctr_smode(CPURISCVState *env, int csrno) 676 { 677 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); 678 679 if (!cfg->ext_smctr && !cfg->ext_ssctr) { 680 return RISCV_EXCP_ILLEGAL_INST; 681 } 682 683 RISCVException ret = smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); 684 if (ret == RISCV_EXCP_NONE && csrno == CSR_SCTRDEPTH && 685 env->virt_enabled) { 686 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 687 } 688 689 return ret; 690 } 691 692 static RISCVException aia_hmode(CPURISCVState *env, int csrno) 693 { 694 int ret; 695 696 if (!riscv_cpu_cfg(env)->ext_ssaia) { 697 return RISCV_EXCP_ILLEGAL_INST; 698 } 699 700 if (csrno == CSR_VSTOPEI) { 701 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_IMSIC); 702 } else { 703 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); 704 } 705 706 if (ret != RISCV_EXCP_NONE) { 707 return ret; 708 } 709 710 return hmode(env, csrno); 711 } 712 713 static RISCVException aia_hmode32(CPURISCVState *env, int csrno) 714 { 715 int ret; 716 717 if (!riscv_cpu_cfg(env)->ext_ssaia) { 718 return RISCV_EXCP_ILLEGAL_INST; 719 } 720 721 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); 722 if (ret != RISCV_EXCP_NONE) { 723 return ret; 724 } 725 726 if (!riscv_cpu_cfg(env)->ext_ssaia) { 727 return RISCV_EXCP_ILLEGAL_INST; 728 } 729 730 return hmode32(env, csrno); 731 } 732 733 static RISCVException dbltrp_hmode(CPURISCVState *env, int csrno) 734 { 735 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 736 return RISCV_EXCP_NONE; 737 } 738 739 return hmode(env, csrno); 740 } 741 742 static RISCVException pmp(CPURISCVState *env, int csrno) 743 { 744 if (riscv_cpu_cfg(env)->pmp) { 745 int max_pmpcfg = (env->priv_ver >= PRIV_VERSION_1_12_0) ? 746 + CSR_PMPCFG15 : CSR_PMPCFG3; 747 748 if (csrno <= max_pmpcfg) { 749 uint32_t reg_index = csrno - CSR_PMPCFG0; 750 751 /* TODO: RV128 restriction check */ 752 if ((reg_index & 1) && (riscv_cpu_mxl(env) == MXL_RV64)) { 753 return RISCV_EXCP_ILLEGAL_INST; 754 } 755 } 756 757 return RISCV_EXCP_NONE; 758 } 759 760 return RISCV_EXCP_ILLEGAL_INST; 761 } 762 763 static RISCVException have_mseccfg(CPURISCVState *env, int csrno) 764 { 765 if (riscv_cpu_cfg(env)->ext_smepmp) { 766 return RISCV_EXCP_NONE; 767 } 768 if (riscv_cpu_cfg(env)->ext_zkr) { 769 return RISCV_EXCP_NONE; 770 } 771 if (riscv_cpu_cfg(env)->ext_smmpm) { 772 return RISCV_EXCP_NONE; 773 } 774 775 return RISCV_EXCP_ILLEGAL_INST; 776 } 777 778 static RISCVException debug(CPURISCVState *env, int csrno) 779 { 780 if (riscv_cpu_cfg(env)->debug) { 781 return RISCV_EXCP_NONE; 782 } 783 784 return RISCV_EXCP_ILLEGAL_INST; 785 } 786 787 static RISCVException rnmi(CPURISCVState *env, int csrno) 788 { 789 RISCVCPU *cpu = env_archcpu(env); 790 791 if (cpu->cfg.ext_smrnmi) { 792 return RISCV_EXCP_NONE; 793 } 794 795 return RISCV_EXCP_ILLEGAL_INST; 796 } 797 #endif 798 799 static RISCVException seed(CPURISCVState *env, int csrno) 800 { 801 if (!riscv_cpu_cfg(env)->ext_zkr) { 802 return RISCV_EXCP_ILLEGAL_INST; 803 } 804 805 #if !defined(CONFIG_USER_ONLY) 806 if (env->debugger) { 807 return RISCV_EXCP_NONE; 808 } 809 810 /* 811 * With a CSR read-write instruction: 812 * 1) The seed CSR is always available in machine mode as normal. 813 * 2) Attempted access to seed from virtual modes VS and VU always raises 814 * an exception(virtual instruction exception only if mseccfg.sseed=1). 815 * 3) Without the corresponding access control bit set to 1, any attempted 816 * access to seed from U, S or HS modes will raise an illegal instruction 817 * exception. 818 */ 819 if (env->priv == PRV_M) { 820 return RISCV_EXCP_NONE; 821 } else if (env->virt_enabled) { 822 if (env->mseccfg & MSECCFG_SSEED) { 823 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 824 } else { 825 return RISCV_EXCP_ILLEGAL_INST; 826 } 827 } else { 828 if (env->priv == PRV_S && (env->mseccfg & MSECCFG_SSEED)) { 829 return RISCV_EXCP_NONE; 830 } else if (env->priv == PRV_U && (env->mseccfg & MSECCFG_USEED)) { 831 return RISCV_EXCP_NONE; 832 } else { 833 return RISCV_EXCP_ILLEGAL_INST; 834 } 835 } 836 #else 837 return RISCV_EXCP_NONE; 838 #endif 839 } 840 841 /* zicfiss CSR_SSP read and write */ 842 static RISCVException read_ssp(CPURISCVState *env, int csrno, 843 target_ulong *val) 844 { 845 *val = env->ssp; 846 return RISCV_EXCP_NONE; 847 } 848 849 static RISCVException write_ssp(CPURISCVState *env, int csrno, 850 target_ulong val, uintptr_t ra) 851 { 852 env->ssp = val; 853 return RISCV_EXCP_NONE; 854 } 855 856 /* User Floating-Point CSRs */ 857 static RISCVException read_fflags(CPURISCVState *env, int csrno, 858 target_ulong *val) 859 { 860 *val = riscv_cpu_get_fflags(env); 861 return RISCV_EXCP_NONE; 862 } 863 864 static RISCVException write_fflags(CPURISCVState *env, int csrno, 865 target_ulong val, uintptr_t ra) 866 { 867 #if !defined(CONFIG_USER_ONLY) 868 if (riscv_has_ext(env, RVF)) { 869 env->mstatus |= MSTATUS_FS; 870 } 871 #endif 872 riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT)); 873 return RISCV_EXCP_NONE; 874 } 875 876 static RISCVException read_frm(CPURISCVState *env, int csrno, 877 target_ulong *val) 878 { 879 *val = env->frm; 880 return RISCV_EXCP_NONE; 881 } 882 883 static RISCVException write_frm(CPURISCVState *env, int csrno, 884 target_ulong val, uintptr_t ra) 885 { 886 #if !defined(CONFIG_USER_ONLY) 887 if (riscv_has_ext(env, RVF)) { 888 env->mstatus |= MSTATUS_FS; 889 } 890 #endif 891 env->frm = val & (FSR_RD >> FSR_RD_SHIFT); 892 return RISCV_EXCP_NONE; 893 } 894 895 static RISCVException read_fcsr(CPURISCVState *env, int csrno, 896 target_ulong *val) 897 { 898 *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT) 899 | (env->frm << FSR_RD_SHIFT); 900 return RISCV_EXCP_NONE; 901 } 902 903 static RISCVException write_fcsr(CPURISCVState *env, int csrno, 904 target_ulong val, uintptr_t ra) 905 { 906 #if !defined(CONFIG_USER_ONLY) 907 if (riscv_has_ext(env, RVF)) { 908 env->mstatus |= MSTATUS_FS; 909 } 910 #endif 911 env->frm = (val & FSR_RD) >> FSR_RD_SHIFT; 912 riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT); 913 return RISCV_EXCP_NONE; 914 } 915 916 static RISCVException read_vtype(CPURISCVState *env, int csrno, 917 target_ulong *val) 918 { 919 uint64_t vill; 920 switch (env->xl) { 921 case MXL_RV32: 922 vill = (uint32_t)env->vill << 31; 923 break; 924 case MXL_RV64: 925 vill = (uint64_t)env->vill << 63; 926 break; 927 default: 928 g_assert_not_reached(); 929 } 930 *val = (target_ulong)vill | env->vtype; 931 return RISCV_EXCP_NONE; 932 } 933 934 static RISCVException read_vl(CPURISCVState *env, int csrno, 935 target_ulong *val) 936 { 937 *val = env->vl; 938 return RISCV_EXCP_NONE; 939 } 940 941 static RISCVException read_vlenb(CPURISCVState *env, int csrno, 942 target_ulong *val) 943 { 944 *val = riscv_cpu_cfg(env)->vlenb; 945 return RISCV_EXCP_NONE; 946 } 947 948 static RISCVException read_vxrm(CPURISCVState *env, int csrno, 949 target_ulong *val) 950 { 951 *val = env->vxrm; 952 return RISCV_EXCP_NONE; 953 } 954 955 static RISCVException write_vxrm(CPURISCVState *env, int csrno, 956 target_ulong val, uintptr_t ra) 957 { 958 #if !defined(CONFIG_USER_ONLY) 959 env->mstatus |= MSTATUS_VS; 960 #endif 961 env->vxrm = val; 962 return RISCV_EXCP_NONE; 963 } 964 965 static RISCVException read_vxsat(CPURISCVState *env, int csrno, 966 target_ulong *val) 967 { 968 *val = env->vxsat & BIT(0); 969 return RISCV_EXCP_NONE; 970 } 971 972 static RISCVException write_vxsat(CPURISCVState *env, int csrno, 973 target_ulong val, uintptr_t ra) 974 { 975 #if !defined(CONFIG_USER_ONLY) 976 env->mstatus |= MSTATUS_VS; 977 #endif 978 env->vxsat = val & BIT(0); 979 return RISCV_EXCP_NONE; 980 } 981 982 static RISCVException read_vstart(CPURISCVState *env, int csrno, 983 target_ulong *val) 984 { 985 *val = env->vstart; 986 return RISCV_EXCP_NONE; 987 } 988 989 static RISCVException write_vstart(CPURISCVState *env, int csrno, 990 target_ulong val, uintptr_t ra) 991 { 992 #if !defined(CONFIG_USER_ONLY) 993 env->mstatus |= MSTATUS_VS; 994 #endif 995 /* 996 * The vstart CSR is defined to have only enough writable bits 997 * to hold the largest element index, i.e. lg2(VLEN) bits. 998 */ 999 env->vstart = val & ~(~0ULL << ctzl(riscv_cpu_cfg(env)->vlenb << 3)); 1000 return RISCV_EXCP_NONE; 1001 } 1002 1003 static RISCVException read_vcsr(CPURISCVState *env, int csrno, 1004 target_ulong *val) 1005 { 1006 *val = (env->vxrm << VCSR_VXRM_SHIFT) | (env->vxsat << VCSR_VXSAT_SHIFT); 1007 return RISCV_EXCP_NONE; 1008 } 1009 1010 static RISCVException write_vcsr(CPURISCVState *env, int csrno, 1011 target_ulong val, uintptr_t ra) 1012 { 1013 #if !defined(CONFIG_USER_ONLY) 1014 env->mstatus |= MSTATUS_VS; 1015 #endif 1016 env->vxrm = (val & VCSR_VXRM) >> VCSR_VXRM_SHIFT; 1017 env->vxsat = (val & VCSR_VXSAT) >> VCSR_VXSAT_SHIFT; 1018 return RISCV_EXCP_NONE; 1019 } 1020 1021 #if defined(CONFIG_USER_ONLY) 1022 /* User Timers and Counters */ 1023 static target_ulong get_ticks(bool shift) 1024 { 1025 int64_t val = cpu_get_host_ticks(); 1026 target_ulong result = shift ? val >> 32 : val; 1027 1028 return result; 1029 } 1030 1031 static RISCVException read_time(CPURISCVState *env, int csrno, 1032 target_ulong *val) 1033 { 1034 *val = cpu_get_host_ticks(); 1035 return RISCV_EXCP_NONE; 1036 } 1037 1038 static RISCVException read_timeh(CPURISCVState *env, int csrno, 1039 target_ulong *val) 1040 { 1041 *val = cpu_get_host_ticks() >> 32; 1042 return RISCV_EXCP_NONE; 1043 } 1044 1045 static RISCVException read_hpmcounter(CPURISCVState *env, int csrno, 1046 target_ulong *val) 1047 { 1048 *val = get_ticks(false); 1049 return RISCV_EXCP_NONE; 1050 } 1051 1052 static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno, 1053 target_ulong *val) 1054 { 1055 *val = get_ticks(true); 1056 return RISCV_EXCP_NONE; 1057 } 1058 1059 #else /* CONFIG_USER_ONLY */ 1060 1061 static RISCVException read_mcyclecfg(CPURISCVState *env, int csrno, 1062 target_ulong *val) 1063 { 1064 *val = env->mcyclecfg; 1065 return RISCV_EXCP_NONE; 1066 } 1067 1068 static RISCVException write_mcyclecfg(CPURISCVState *env, int csrno, 1069 target_ulong val, uintptr_t ra) 1070 { 1071 uint64_t inh_avail_mask; 1072 1073 if (riscv_cpu_mxl(env) == MXL_RV32) { 1074 env->mcyclecfg = val; 1075 } else { 1076 /* Set xINH fields if priv mode supported */ 1077 inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MCYCLECFG_BIT_MINH; 1078 inh_avail_mask |= riscv_has_ext(env, RVU) ? MCYCLECFG_BIT_UINH : 0; 1079 inh_avail_mask |= riscv_has_ext(env, RVS) ? MCYCLECFG_BIT_SINH : 0; 1080 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1081 riscv_has_ext(env, RVU)) ? MCYCLECFG_BIT_VUINH : 0; 1082 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1083 riscv_has_ext(env, RVS)) ? MCYCLECFG_BIT_VSINH : 0; 1084 env->mcyclecfg = val & inh_avail_mask; 1085 } 1086 1087 return RISCV_EXCP_NONE; 1088 } 1089 1090 static RISCVException read_mcyclecfgh(CPURISCVState *env, int csrno, 1091 target_ulong *val) 1092 { 1093 *val = env->mcyclecfgh; 1094 return RISCV_EXCP_NONE; 1095 } 1096 1097 static RISCVException write_mcyclecfgh(CPURISCVState *env, int csrno, 1098 target_ulong val, uintptr_t ra) 1099 { 1100 target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK | 1101 MCYCLECFGH_BIT_MINH); 1102 1103 /* Set xINH fields if priv mode supported */ 1104 inh_avail_mask |= riscv_has_ext(env, RVU) ? MCYCLECFGH_BIT_UINH : 0; 1105 inh_avail_mask |= riscv_has_ext(env, RVS) ? MCYCLECFGH_BIT_SINH : 0; 1106 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1107 riscv_has_ext(env, RVU)) ? MCYCLECFGH_BIT_VUINH : 0; 1108 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1109 riscv_has_ext(env, RVS)) ? MCYCLECFGH_BIT_VSINH : 0; 1110 1111 env->mcyclecfgh = val & inh_avail_mask; 1112 return RISCV_EXCP_NONE; 1113 } 1114 1115 static RISCVException read_minstretcfg(CPURISCVState *env, int csrno, 1116 target_ulong *val) 1117 { 1118 *val = env->minstretcfg; 1119 return RISCV_EXCP_NONE; 1120 } 1121 1122 static RISCVException write_minstretcfg(CPURISCVState *env, int csrno, 1123 target_ulong val, uintptr_t ra) 1124 { 1125 uint64_t inh_avail_mask; 1126 1127 if (riscv_cpu_mxl(env) == MXL_RV32) { 1128 env->minstretcfg = val; 1129 } else { 1130 inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MINSTRETCFG_BIT_MINH; 1131 inh_avail_mask |= riscv_has_ext(env, RVU) ? MINSTRETCFG_BIT_UINH : 0; 1132 inh_avail_mask |= riscv_has_ext(env, RVS) ? MINSTRETCFG_BIT_SINH : 0; 1133 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1134 riscv_has_ext(env, RVU)) ? MINSTRETCFG_BIT_VUINH : 0; 1135 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1136 riscv_has_ext(env, RVS)) ? MINSTRETCFG_BIT_VSINH : 0; 1137 env->minstretcfg = val & inh_avail_mask; 1138 } 1139 return RISCV_EXCP_NONE; 1140 } 1141 1142 static RISCVException read_minstretcfgh(CPURISCVState *env, int csrno, 1143 target_ulong *val) 1144 { 1145 *val = env->minstretcfgh; 1146 return RISCV_EXCP_NONE; 1147 } 1148 1149 static RISCVException write_minstretcfgh(CPURISCVState *env, int csrno, 1150 target_ulong val, uintptr_t ra) 1151 { 1152 target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK | 1153 MINSTRETCFGH_BIT_MINH); 1154 1155 inh_avail_mask |= riscv_has_ext(env, RVU) ? MINSTRETCFGH_BIT_UINH : 0; 1156 inh_avail_mask |= riscv_has_ext(env, RVS) ? MINSTRETCFGH_BIT_SINH : 0; 1157 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1158 riscv_has_ext(env, RVU)) ? MINSTRETCFGH_BIT_VUINH : 0; 1159 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1160 riscv_has_ext(env, RVS)) ? MINSTRETCFGH_BIT_VSINH : 0; 1161 1162 env->minstretcfgh = val & inh_avail_mask; 1163 return RISCV_EXCP_NONE; 1164 } 1165 1166 static RISCVException read_mhpmevent(CPURISCVState *env, int csrno, 1167 target_ulong *val) 1168 { 1169 int evt_index = csrno - CSR_MCOUNTINHIBIT; 1170 1171 *val = env->mhpmevent_val[evt_index]; 1172 1173 return RISCV_EXCP_NONE; 1174 } 1175 1176 static RISCVException write_mhpmevent(CPURISCVState *env, int csrno, 1177 target_ulong val, uintptr_t ra) 1178 { 1179 int evt_index = csrno - CSR_MCOUNTINHIBIT; 1180 uint64_t mhpmevt_val = val; 1181 uint64_t inh_avail_mask; 1182 1183 if (riscv_cpu_mxl(env) == MXL_RV32) { 1184 env->mhpmevent_val[evt_index] = val; 1185 mhpmevt_val = mhpmevt_val | 1186 ((uint64_t)env->mhpmeventh_val[evt_index] << 32); 1187 } else { 1188 inh_avail_mask = ~MHPMEVENT_FILTER_MASK | MHPMEVENT_BIT_MINH; 1189 inh_avail_mask |= riscv_has_ext(env, RVU) ? MHPMEVENT_BIT_UINH : 0; 1190 inh_avail_mask |= riscv_has_ext(env, RVS) ? MHPMEVENT_BIT_SINH : 0; 1191 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1192 riscv_has_ext(env, RVU)) ? MHPMEVENT_BIT_VUINH : 0; 1193 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1194 riscv_has_ext(env, RVS)) ? MHPMEVENT_BIT_VSINH : 0; 1195 mhpmevt_val = val & inh_avail_mask; 1196 env->mhpmevent_val[evt_index] = mhpmevt_val; 1197 } 1198 1199 riscv_pmu_update_event_map(env, mhpmevt_val, evt_index); 1200 1201 return RISCV_EXCP_NONE; 1202 } 1203 1204 static RISCVException read_mhpmeventh(CPURISCVState *env, int csrno, 1205 target_ulong *val) 1206 { 1207 int evt_index = csrno - CSR_MHPMEVENT3H + 3; 1208 1209 *val = env->mhpmeventh_val[evt_index]; 1210 1211 return RISCV_EXCP_NONE; 1212 } 1213 1214 static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno, 1215 target_ulong val, uintptr_t ra) 1216 { 1217 int evt_index = csrno - CSR_MHPMEVENT3H + 3; 1218 uint64_t mhpmevth_val; 1219 uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; 1220 target_ulong inh_avail_mask = (target_ulong)(~MHPMEVENTH_FILTER_MASK | 1221 MHPMEVENTH_BIT_MINH); 1222 1223 inh_avail_mask |= riscv_has_ext(env, RVU) ? MHPMEVENTH_BIT_UINH : 0; 1224 inh_avail_mask |= riscv_has_ext(env, RVS) ? MHPMEVENTH_BIT_SINH : 0; 1225 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1226 riscv_has_ext(env, RVU)) ? MHPMEVENTH_BIT_VUINH : 0; 1227 inh_avail_mask |= (riscv_has_ext(env, RVH) && 1228 riscv_has_ext(env, RVS)) ? MHPMEVENTH_BIT_VSINH : 0; 1229 1230 mhpmevth_val = val & inh_avail_mask; 1231 mhpmevt_val = mhpmevt_val | (mhpmevth_val << 32); 1232 env->mhpmeventh_val[evt_index] = mhpmevth_val; 1233 1234 riscv_pmu_update_event_map(env, mhpmevt_val, evt_index); 1235 1236 return RISCV_EXCP_NONE; 1237 } 1238 1239 static target_ulong riscv_pmu_ctr_get_fixed_counters_val(CPURISCVState *env, 1240 int counter_idx, 1241 bool upper_half) 1242 { 1243 int inst = riscv_pmu_ctr_monitor_instructions(env, counter_idx); 1244 uint64_t *counter_arr_virt = env->pmu_fixed_ctrs[inst].counter_virt; 1245 uint64_t *counter_arr = env->pmu_fixed_ctrs[inst].counter; 1246 target_ulong result = 0; 1247 uint64_t curr_val = 0; 1248 uint64_t cfg_val = 0; 1249 1250 if (counter_idx == 0) { 1251 cfg_val = upper_half ? ((uint64_t)env->mcyclecfgh << 32) : 1252 env->mcyclecfg; 1253 } else if (counter_idx == 2) { 1254 cfg_val = upper_half ? ((uint64_t)env->minstretcfgh << 32) : 1255 env->minstretcfg; 1256 } else { 1257 cfg_val = upper_half ? 1258 ((uint64_t)env->mhpmeventh_val[counter_idx] << 32) : 1259 env->mhpmevent_val[counter_idx]; 1260 cfg_val &= MHPMEVENT_FILTER_MASK; 1261 } 1262 1263 if (!cfg_val) { 1264 if (icount_enabled()) { 1265 curr_val = inst ? icount_get_raw() : icount_get(); 1266 } else { 1267 curr_val = cpu_get_host_ticks(); 1268 } 1269 1270 goto done; 1271 } 1272 1273 /* Update counter before reading. */ 1274 riscv_pmu_update_fixed_ctrs(env, env->priv, env->virt_enabled); 1275 1276 if (!(cfg_val & MCYCLECFG_BIT_MINH)) { 1277 curr_val += counter_arr[PRV_M]; 1278 } 1279 1280 if (!(cfg_val & MCYCLECFG_BIT_SINH)) { 1281 curr_val += counter_arr[PRV_S]; 1282 } 1283 1284 if (!(cfg_val & MCYCLECFG_BIT_UINH)) { 1285 curr_val += counter_arr[PRV_U]; 1286 } 1287 1288 if (!(cfg_val & MCYCLECFG_BIT_VSINH)) { 1289 curr_val += counter_arr_virt[PRV_S]; 1290 } 1291 1292 if (!(cfg_val & MCYCLECFG_BIT_VUINH)) { 1293 curr_val += counter_arr_virt[PRV_U]; 1294 } 1295 1296 done: 1297 if (riscv_cpu_mxl(env) == MXL_RV32) { 1298 result = upper_half ? curr_val >> 32 : curr_val; 1299 } else { 1300 result = curr_val; 1301 } 1302 1303 return result; 1304 } 1305 1306 static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val, 1307 uint32_t ctr_idx) 1308 { 1309 PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; 1310 uint64_t mhpmctr_val = val; 1311 1312 counter->mhpmcounter_val = val; 1313 if (!get_field(env->mcountinhibit, BIT(ctr_idx)) && 1314 (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || 1315 riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) { 1316 counter->mhpmcounter_prev = riscv_pmu_ctr_get_fixed_counters_val(env, 1317 ctr_idx, false); 1318 if (ctr_idx > 2) { 1319 if (riscv_cpu_mxl(env) == MXL_RV32) { 1320 mhpmctr_val = mhpmctr_val | 1321 ((uint64_t)counter->mhpmcounterh_val << 32); 1322 } 1323 riscv_pmu_setup_timer(env, mhpmctr_val, ctr_idx); 1324 } 1325 } else { 1326 /* Other counters can keep incrementing from the given value */ 1327 counter->mhpmcounter_prev = val; 1328 } 1329 1330 return RISCV_EXCP_NONE; 1331 } 1332 1333 static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val, 1334 uint32_t ctr_idx) 1335 { 1336 PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; 1337 uint64_t mhpmctr_val = counter->mhpmcounter_val; 1338 uint64_t mhpmctrh_val = val; 1339 1340 counter->mhpmcounterh_val = val; 1341 mhpmctr_val = mhpmctr_val | (mhpmctrh_val << 32); 1342 if (!get_field(env->mcountinhibit, BIT(ctr_idx)) && 1343 (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || 1344 riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) { 1345 counter->mhpmcounterh_prev = riscv_pmu_ctr_get_fixed_counters_val(env, 1346 ctr_idx, true); 1347 if (ctr_idx > 2) { 1348 riscv_pmu_setup_timer(env, mhpmctr_val, ctr_idx); 1349 } 1350 } else { 1351 counter->mhpmcounterh_prev = val; 1352 } 1353 1354 return RISCV_EXCP_NONE; 1355 } 1356 1357 static RISCVException write_mhpmcounter(CPURISCVState *env, int csrno, 1358 target_ulong val, uintptr_t ra) 1359 { 1360 int ctr_idx = csrno - CSR_MCYCLE; 1361 1362 return riscv_pmu_write_ctr(env, val, ctr_idx); 1363 } 1364 1365 static RISCVException write_mhpmcounterh(CPURISCVState *env, int csrno, 1366 target_ulong val, uintptr_t ra) 1367 { 1368 int ctr_idx = csrno - CSR_MCYCLEH; 1369 1370 return riscv_pmu_write_ctrh(env, val, ctr_idx); 1371 } 1372 1373 RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val, 1374 bool upper_half, uint32_t ctr_idx) 1375 { 1376 PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; 1377 target_ulong ctr_prev = upper_half ? counter->mhpmcounterh_prev : 1378 counter->mhpmcounter_prev; 1379 target_ulong ctr_val = upper_half ? counter->mhpmcounterh_val : 1380 counter->mhpmcounter_val; 1381 1382 if (get_field(env->mcountinhibit, BIT(ctr_idx))) { 1383 /* 1384 * Counter should not increment if inhibit bit is set. Just return the 1385 * current counter value. 1386 */ 1387 *val = ctr_val; 1388 return RISCV_EXCP_NONE; 1389 } 1390 1391 /* 1392 * The kernel computes the perf delta by subtracting the current value from 1393 * the value it initialized previously (ctr_val). 1394 */ 1395 if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || 1396 riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { 1397 *val = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx, upper_half) - 1398 ctr_prev + ctr_val; 1399 } else { 1400 *val = ctr_val; 1401 } 1402 1403 return RISCV_EXCP_NONE; 1404 } 1405 1406 static RISCVException read_hpmcounter(CPURISCVState *env, int csrno, 1407 target_ulong *val) 1408 { 1409 uint16_t ctr_index; 1410 1411 if (csrno >= CSR_MCYCLE && csrno <= CSR_MHPMCOUNTER31) { 1412 ctr_index = csrno - CSR_MCYCLE; 1413 } else if (csrno >= CSR_CYCLE && csrno <= CSR_HPMCOUNTER31) { 1414 ctr_index = csrno - CSR_CYCLE; 1415 } else { 1416 return RISCV_EXCP_ILLEGAL_INST; 1417 } 1418 1419 return riscv_pmu_read_ctr(env, val, false, ctr_index); 1420 } 1421 1422 static RISCVException read_hpmcounterh(CPURISCVState *env, int csrno, 1423 target_ulong *val) 1424 { 1425 uint16_t ctr_index; 1426 1427 if (csrno >= CSR_MCYCLEH && csrno <= CSR_MHPMCOUNTER31H) { 1428 ctr_index = csrno - CSR_MCYCLEH; 1429 } else if (csrno >= CSR_CYCLEH && csrno <= CSR_HPMCOUNTER31H) { 1430 ctr_index = csrno - CSR_CYCLEH; 1431 } else { 1432 return RISCV_EXCP_ILLEGAL_INST; 1433 } 1434 1435 return riscv_pmu_read_ctr(env, val, true, ctr_index); 1436 } 1437 1438 static int rmw_cd_mhpmcounter(CPURISCVState *env, int ctr_idx, 1439 target_ulong *val, target_ulong new_val, 1440 target_ulong wr_mask) 1441 { 1442 if (wr_mask != 0 && wr_mask != -1) { 1443 return -EINVAL; 1444 } 1445 1446 if (!wr_mask && val) { 1447 riscv_pmu_read_ctr(env, val, false, ctr_idx); 1448 } else if (wr_mask) { 1449 riscv_pmu_write_ctr(env, new_val, ctr_idx); 1450 } else { 1451 return -EINVAL; 1452 } 1453 1454 return 0; 1455 } 1456 1457 static int rmw_cd_mhpmcounterh(CPURISCVState *env, int ctr_idx, 1458 target_ulong *val, target_ulong new_val, 1459 target_ulong wr_mask) 1460 { 1461 if (wr_mask != 0 && wr_mask != -1) { 1462 return -EINVAL; 1463 } 1464 1465 if (!wr_mask && val) { 1466 riscv_pmu_read_ctr(env, val, true, ctr_idx); 1467 } else if (wr_mask) { 1468 riscv_pmu_write_ctrh(env, new_val, ctr_idx); 1469 } else { 1470 return -EINVAL; 1471 } 1472 1473 return 0; 1474 } 1475 1476 static int rmw_cd_mhpmevent(CPURISCVState *env, int evt_index, 1477 target_ulong *val, target_ulong new_val, 1478 target_ulong wr_mask) 1479 { 1480 uint64_t mhpmevt_val = new_val; 1481 1482 if (wr_mask != 0 && wr_mask != -1) { 1483 return -EINVAL; 1484 } 1485 1486 if (!wr_mask && val) { 1487 *val = env->mhpmevent_val[evt_index]; 1488 if (riscv_cpu_cfg(env)->ext_sscofpmf) { 1489 *val &= ~MHPMEVENT_BIT_MINH; 1490 } 1491 } else if (wr_mask) { 1492 wr_mask &= ~MHPMEVENT_BIT_MINH; 1493 mhpmevt_val = (new_val & wr_mask) | 1494 (env->mhpmevent_val[evt_index] & ~wr_mask); 1495 if (riscv_cpu_mxl(env) == MXL_RV32) { 1496 mhpmevt_val = mhpmevt_val | 1497 ((uint64_t)env->mhpmeventh_val[evt_index] << 32); 1498 } 1499 env->mhpmevent_val[evt_index] = mhpmevt_val; 1500 riscv_pmu_update_event_map(env, mhpmevt_val, evt_index); 1501 } else { 1502 return -EINVAL; 1503 } 1504 1505 return 0; 1506 } 1507 1508 static int rmw_cd_mhpmeventh(CPURISCVState *env, int evt_index, 1509 target_ulong *val, target_ulong new_val, 1510 target_ulong wr_mask) 1511 { 1512 uint64_t mhpmevth_val; 1513 uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; 1514 1515 if (wr_mask != 0 && wr_mask != -1) { 1516 return -EINVAL; 1517 } 1518 1519 if (!wr_mask && val) { 1520 *val = env->mhpmeventh_val[evt_index]; 1521 if (riscv_cpu_cfg(env)->ext_sscofpmf) { 1522 *val &= ~MHPMEVENTH_BIT_MINH; 1523 } 1524 } else if (wr_mask) { 1525 wr_mask &= ~MHPMEVENTH_BIT_MINH; 1526 env->mhpmeventh_val[evt_index] = 1527 (new_val & wr_mask) | (env->mhpmeventh_val[evt_index] & ~wr_mask); 1528 mhpmevth_val = env->mhpmeventh_val[evt_index]; 1529 mhpmevt_val = mhpmevt_val | (mhpmevth_val << 32); 1530 riscv_pmu_update_event_map(env, mhpmevt_val, evt_index); 1531 } else { 1532 return -EINVAL; 1533 } 1534 1535 return 0; 1536 } 1537 1538 static int rmw_cd_ctr_cfg(CPURISCVState *env, int cfg_index, target_ulong *val, 1539 target_ulong new_val, target_ulong wr_mask) 1540 { 1541 switch (cfg_index) { 1542 case 0: /* CYCLECFG */ 1543 if (wr_mask) { 1544 wr_mask &= ~MCYCLECFG_BIT_MINH; 1545 env->mcyclecfg = (new_val & wr_mask) | (env->mcyclecfg & ~wr_mask); 1546 } else { 1547 *val = env->mcyclecfg &= ~MHPMEVENTH_BIT_MINH; 1548 } 1549 break; 1550 case 2: /* INSTRETCFG */ 1551 if (wr_mask) { 1552 wr_mask &= ~MINSTRETCFG_BIT_MINH; 1553 env->minstretcfg = (new_val & wr_mask) | 1554 (env->minstretcfg & ~wr_mask); 1555 } else { 1556 *val = env->minstretcfg &= ~MHPMEVENTH_BIT_MINH; 1557 } 1558 break; 1559 default: 1560 return -EINVAL; 1561 } 1562 return 0; 1563 } 1564 1565 static int rmw_cd_ctr_cfgh(CPURISCVState *env, int cfg_index, target_ulong *val, 1566 target_ulong new_val, target_ulong wr_mask) 1567 { 1568 1569 if (riscv_cpu_mxl(env) != MXL_RV32) { 1570 return RISCV_EXCP_ILLEGAL_INST; 1571 } 1572 1573 switch (cfg_index) { 1574 case 0: /* CYCLECFGH */ 1575 if (wr_mask) { 1576 wr_mask &= ~MCYCLECFGH_BIT_MINH; 1577 env->mcyclecfgh = (new_val & wr_mask) | 1578 (env->mcyclecfgh & ~wr_mask); 1579 } else { 1580 *val = env->mcyclecfgh; 1581 } 1582 break; 1583 case 2: /* INSTRETCFGH */ 1584 if (wr_mask) { 1585 wr_mask &= ~MINSTRETCFGH_BIT_MINH; 1586 env->minstretcfgh = (new_val & wr_mask) | 1587 (env->minstretcfgh & ~wr_mask); 1588 } else { 1589 *val = env->minstretcfgh; 1590 } 1591 break; 1592 default: 1593 return -EINVAL; 1594 } 1595 return 0; 1596 } 1597 1598 1599 static RISCVException read_scountovf(CPURISCVState *env, int csrno, 1600 target_ulong *val) 1601 { 1602 int mhpmevt_start = CSR_MHPMEVENT3 - CSR_MCOUNTINHIBIT; 1603 int i; 1604 *val = 0; 1605 target_ulong *mhpm_evt_val; 1606 uint64_t of_bit_mask; 1607 1608 /* Virtualize scountovf for counter delegation */ 1609 if (riscv_cpu_cfg(env)->ext_sscofpmf && 1610 riscv_cpu_cfg(env)->ext_ssccfg && 1611 get_field(env->menvcfg, MENVCFG_CDE) && 1612 env->virt_enabled) { 1613 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 1614 } 1615 1616 if (riscv_cpu_mxl(env) == MXL_RV32) { 1617 mhpm_evt_val = env->mhpmeventh_val; 1618 of_bit_mask = MHPMEVENTH_BIT_OF; 1619 } else { 1620 mhpm_evt_val = env->mhpmevent_val; 1621 of_bit_mask = MHPMEVENT_BIT_OF; 1622 } 1623 1624 for (i = mhpmevt_start; i < RV_MAX_MHPMEVENTS; i++) { 1625 if ((get_field(env->mcounteren, BIT(i))) && 1626 (mhpm_evt_val[i] & of_bit_mask)) { 1627 *val |= BIT(i); 1628 } 1629 } 1630 1631 return RISCV_EXCP_NONE; 1632 } 1633 1634 static RISCVException read_time(CPURISCVState *env, int csrno, 1635 target_ulong *val) 1636 { 1637 uint64_t delta = env->virt_enabled ? env->htimedelta : 0; 1638 1639 if (!env->rdtime_fn) { 1640 return RISCV_EXCP_ILLEGAL_INST; 1641 } 1642 1643 *val = env->rdtime_fn(env->rdtime_fn_arg) + delta; 1644 return RISCV_EXCP_NONE; 1645 } 1646 1647 static RISCVException read_timeh(CPURISCVState *env, int csrno, 1648 target_ulong *val) 1649 { 1650 uint64_t delta = env->virt_enabled ? env->htimedelta : 0; 1651 1652 if (!env->rdtime_fn) { 1653 return RISCV_EXCP_ILLEGAL_INST; 1654 } 1655 1656 *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32; 1657 return RISCV_EXCP_NONE; 1658 } 1659 1660 static RISCVException read_vstimecmp(CPURISCVState *env, int csrno, 1661 target_ulong *val) 1662 { 1663 *val = env->vstimecmp; 1664 1665 return RISCV_EXCP_NONE; 1666 } 1667 1668 static RISCVException read_vstimecmph(CPURISCVState *env, int csrno, 1669 target_ulong *val) 1670 { 1671 *val = env->vstimecmp >> 32; 1672 1673 return RISCV_EXCP_NONE; 1674 } 1675 1676 static RISCVException write_vstimecmp(CPURISCVState *env, int csrno, 1677 target_ulong val, uintptr_t ra) 1678 { 1679 if (riscv_cpu_mxl(env) == MXL_RV32) { 1680 env->vstimecmp = deposit64(env->vstimecmp, 0, 32, (uint64_t)val); 1681 } else { 1682 env->vstimecmp = val; 1683 } 1684 1685 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp, 1686 env->htimedelta, MIP_VSTIP); 1687 1688 return RISCV_EXCP_NONE; 1689 } 1690 1691 static RISCVException write_vstimecmph(CPURISCVState *env, int csrno, 1692 target_ulong val, uintptr_t ra) 1693 { 1694 env->vstimecmp = deposit64(env->vstimecmp, 32, 32, (uint64_t)val); 1695 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp, 1696 env->htimedelta, MIP_VSTIP); 1697 1698 return RISCV_EXCP_NONE; 1699 } 1700 1701 static RISCVException read_stimecmp(CPURISCVState *env, int csrno, 1702 target_ulong *val) 1703 { 1704 if (env->virt_enabled) { 1705 *val = env->vstimecmp; 1706 } else { 1707 *val = env->stimecmp; 1708 } 1709 1710 return RISCV_EXCP_NONE; 1711 } 1712 1713 static RISCVException read_stimecmph(CPURISCVState *env, int csrno, 1714 target_ulong *val) 1715 { 1716 if (env->virt_enabled) { 1717 *val = env->vstimecmp >> 32; 1718 } else { 1719 *val = env->stimecmp >> 32; 1720 } 1721 1722 return RISCV_EXCP_NONE; 1723 } 1724 1725 static RISCVException write_stimecmp(CPURISCVState *env, int csrno, 1726 target_ulong val, uintptr_t ra) 1727 { 1728 if (env->virt_enabled) { 1729 if (env->hvictl & HVICTL_VTI) { 1730 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 1731 } 1732 return write_vstimecmp(env, csrno, val, ra); 1733 } 1734 1735 if (riscv_cpu_mxl(env) == MXL_RV32) { 1736 env->stimecmp = deposit64(env->stimecmp, 0, 32, (uint64_t)val); 1737 } else { 1738 env->stimecmp = val; 1739 } 1740 1741 riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, MIP_STIP); 1742 1743 return RISCV_EXCP_NONE; 1744 } 1745 1746 static RISCVException write_stimecmph(CPURISCVState *env, int csrno, 1747 target_ulong val, uintptr_t ra) 1748 { 1749 if (env->virt_enabled) { 1750 if (env->hvictl & HVICTL_VTI) { 1751 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 1752 } 1753 return write_vstimecmph(env, csrno, val, ra); 1754 } 1755 1756 env->stimecmp = deposit64(env->stimecmp, 32, 32, (uint64_t)val); 1757 riscv_timer_write_timecmp(env, env->stimer, env->stimecmp, 0, MIP_STIP); 1758 1759 return RISCV_EXCP_NONE; 1760 } 1761 1762 #define VSTOPI_NUM_SRCS 5 1763 1764 /* 1765 * All core local interrupts except the fixed ones 0:12. This macro is for 1766 * virtual interrupts logic so please don't change this to avoid messing up 1767 * the whole support, For reference see AIA spec: `5.3 Interrupt filtering and 1768 * virtual interrupts for supervisor level` and `6.3.2 Virtual interrupts for 1769 * VS level`. 1770 */ 1771 #define LOCAL_INTERRUPTS (~0x1FFFULL) 1772 1773 static const uint64_t delegable_ints = 1774 S_MODE_INTERRUPTS | VS_MODE_INTERRUPTS | MIP_LCOFIP; 1775 static const uint64_t vs_delegable_ints = 1776 (VS_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & ~MIP_LCOFIP; 1777 static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS | 1778 HS_MODE_INTERRUPTS | LOCAL_INTERRUPTS; 1779 #define DELEGABLE_EXCPS ((1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | \ 1780 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | \ 1781 (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | \ 1782 (1ULL << (RISCV_EXCP_BREAKPOINT)) | \ 1783 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | \ 1784 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | \ 1785 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | \ 1786 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | \ 1787 (1ULL << (RISCV_EXCP_U_ECALL)) | \ 1788 (1ULL << (RISCV_EXCP_S_ECALL)) | \ 1789 (1ULL << (RISCV_EXCP_VS_ECALL)) | \ 1790 (1ULL << (RISCV_EXCP_M_ECALL)) | \ 1791 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | \ 1792 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \ 1793 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \ 1794 (1ULL << (RISCV_EXCP_SW_CHECK)) | \ 1795 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \ 1796 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \ 1797 (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \ 1798 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT))) 1799 static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS & 1800 ~((1ULL << (RISCV_EXCP_S_ECALL)) | 1801 (1ULL << (RISCV_EXCP_VS_ECALL)) | 1802 (1ULL << (RISCV_EXCP_M_ECALL)) | 1803 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | 1804 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | 1805 (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | 1806 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT))); 1807 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE | 1808 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS | 1809 SSTATUS_SUM | SSTATUS_MXR | SSTATUS_VS; 1810 1811 /* 1812 * Spec allows for bits 13:63 to be either read-only or writable. 1813 * So far we have interrupt LCOFIP in that region which is writable. 1814 * 1815 * Also, spec allows to inject virtual interrupts in this region even 1816 * without any hardware interrupts for that interrupt number. 1817 * 1818 * For now interrupt in 13:63 region are all kept writable. 13 being 1819 * LCOFIP and 14:63 being virtual only. Change this in future if we 1820 * introduce more interrupts that are not writable. 1821 */ 1822 1823 /* Bit STIP can be an alias of mip.STIP that's why it's writable in mvip. */ 1824 static const uint64_t mvip_writable_mask = MIP_SSIP | MIP_STIP | MIP_SEIP | 1825 LOCAL_INTERRUPTS; 1826 static const uint64_t mvien_writable_mask = MIP_SSIP | MIP_SEIP | 1827 LOCAL_INTERRUPTS; 1828 1829 static const uint64_t sip_writable_mask = SIP_SSIP | LOCAL_INTERRUPTS; 1830 static const uint64_t hip_writable_mask = MIP_VSSIP; 1831 static const uint64_t hvip_writable_mask = MIP_VSSIP | MIP_VSTIP | 1832 MIP_VSEIP | LOCAL_INTERRUPTS; 1833 static const uint64_t hvien_writable_mask = LOCAL_INTERRUPTS; 1834 1835 static const uint64_t vsip_writable_mask = MIP_VSSIP | LOCAL_INTERRUPTS; 1836 1837 const bool valid_vm_1_10_32[16] = { 1838 [VM_1_10_MBARE] = true, 1839 [VM_1_10_SV32] = true 1840 }; 1841 1842 const bool valid_vm_1_10_64[16] = { 1843 [VM_1_10_MBARE] = true, 1844 [VM_1_10_SV39] = true, 1845 [VM_1_10_SV48] = true, 1846 [VM_1_10_SV57] = true 1847 }; 1848 1849 /* Machine Information Registers */ 1850 static RISCVException read_zero(CPURISCVState *env, int csrno, 1851 target_ulong *val) 1852 { 1853 *val = 0; 1854 return RISCV_EXCP_NONE; 1855 } 1856 1857 static RISCVException write_ignore(CPURISCVState *env, int csrno, 1858 target_ulong val, uintptr_t ra) 1859 { 1860 return RISCV_EXCP_NONE; 1861 } 1862 1863 static RISCVException read_mvendorid(CPURISCVState *env, int csrno, 1864 target_ulong *val) 1865 { 1866 *val = riscv_cpu_cfg(env)->mvendorid; 1867 return RISCV_EXCP_NONE; 1868 } 1869 1870 static RISCVException read_marchid(CPURISCVState *env, int csrno, 1871 target_ulong *val) 1872 { 1873 *val = riscv_cpu_cfg(env)->marchid; 1874 return RISCV_EXCP_NONE; 1875 } 1876 1877 static RISCVException read_mimpid(CPURISCVState *env, int csrno, 1878 target_ulong *val) 1879 { 1880 *val = riscv_cpu_cfg(env)->mimpid; 1881 return RISCV_EXCP_NONE; 1882 } 1883 1884 static RISCVException read_mhartid(CPURISCVState *env, int csrno, 1885 target_ulong *val) 1886 { 1887 *val = env->mhartid; 1888 return RISCV_EXCP_NONE; 1889 } 1890 1891 /* Machine Trap Setup */ 1892 1893 /* We do not store SD explicitly, only compute it on demand. */ 1894 static uint64_t add_status_sd(RISCVMXL xl, uint64_t status) 1895 { 1896 if ((status & MSTATUS_FS) == MSTATUS_FS || 1897 (status & MSTATUS_VS) == MSTATUS_VS || 1898 (status & MSTATUS_XS) == MSTATUS_XS) { 1899 switch (xl) { 1900 case MXL_RV32: 1901 return status | MSTATUS32_SD; 1902 case MXL_RV64: 1903 return status | MSTATUS64_SD; 1904 case MXL_RV128: 1905 return MSTATUSH128_SD; 1906 default: 1907 g_assert_not_reached(); 1908 } 1909 } 1910 return status; 1911 } 1912 1913 static RISCVException read_mstatus(CPURISCVState *env, int csrno, 1914 target_ulong *val) 1915 { 1916 *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus); 1917 return RISCV_EXCP_NONE; 1918 } 1919 1920 static bool validate_vm(CPURISCVState *env, target_ulong vm) 1921 { 1922 bool rv32 = riscv_cpu_mxl(env) == MXL_RV32; 1923 RISCVCPU *cpu = env_archcpu(env); 1924 int satp_mode_supported_max = cpu->cfg.max_satp_mode; 1925 const bool *valid_vm = rv32 ? valid_vm_1_10_32 : valid_vm_1_10_64; 1926 1927 assert(satp_mode_supported_max >= 0); 1928 return vm <= satp_mode_supported_max && valid_vm[vm]; 1929 } 1930 1931 static target_ulong legalize_xatp(CPURISCVState *env, target_ulong old_xatp, 1932 target_ulong val) 1933 { 1934 target_ulong mask; 1935 bool vm; 1936 if (riscv_cpu_mxl(env) == MXL_RV32) { 1937 vm = validate_vm(env, get_field(val, SATP32_MODE)); 1938 mask = (val ^ old_xatp) & (SATP32_MODE | SATP32_ASID | SATP32_PPN); 1939 } else { 1940 vm = validate_vm(env, get_field(val, SATP64_MODE)); 1941 mask = (val ^ old_xatp) & (SATP64_MODE | SATP64_ASID | SATP64_PPN); 1942 } 1943 1944 if (vm && mask) { 1945 /* 1946 * The ISA defines SATP.MODE=Bare as "no translation", but we still 1947 * pass these through QEMU's TLB emulation as it improves 1948 * performance. Flushing the TLB on SATP writes with paging 1949 * enabled avoids leaking those invalid cached mappings. 1950 */ 1951 tlb_flush(env_cpu(env)); 1952 return val; 1953 } 1954 return old_xatp; 1955 } 1956 1957 static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp, 1958 target_ulong val) 1959 { 1960 bool valid = false; 1961 target_ulong new_mpp = get_field(val, MSTATUS_MPP); 1962 1963 switch (new_mpp) { 1964 case PRV_M: 1965 valid = true; 1966 break; 1967 case PRV_S: 1968 valid = riscv_has_ext(env, RVS); 1969 break; 1970 case PRV_U: 1971 valid = riscv_has_ext(env, RVU); 1972 break; 1973 } 1974 1975 /* Remain field unchanged if new_mpp value is invalid */ 1976 if (!valid) { 1977 val = set_field(val, MSTATUS_MPP, old_mpp); 1978 } 1979 1980 return val; 1981 } 1982 1983 static RISCVException write_mstatus(CPURISCVState *env, int csrno, 1984 target_ulong val, uintptr_t ra) 1985 { 1986 uint64_t mstatus = env->mstatus; 1987 uint64_t mask = 0; 1988 RISCVMXL xl = riscv_cpu_mxl(env); 1989 1990 /* 1991 * MPP field have been made WARL since priv version 1.11. However, 1992 * legalization for it will not break any software running on 1.10. 1993 */ 1994 val = legalize_mpp(env, get_field(mstatus, MSTATUS_MPP), val); 1995 1996 /* flush tlb on mstatus fields that affect VM */ 1997 if ((val ^ mstatus) & MSTATUS_MXR) { 1998 tlb_flush(env_cpu(env)); 1999 } 2000 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | 2001 MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM | 2002 MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR | 2003 MSTATUS_TW; 2004 2005 if (riscv_has_ext(env, RVF)) { 2006 mask |= MSTATUS_FS; 2007 } 2008 2009 if (riscv_cpu_cfg(env)->ext_zve32x) { 2010 mask |= MSTATUS_VS; 2011 } 2012 2013 if (riscv_env_smode_dbltrp_enabled(env, env->virt_enabled)) { 2014 mask |= MSTATUS_SDT; 2015 if ((val & MSTATUS_SDT) != 0) { 2016 val &= ~MSTATUS_SIE; 2017 } 2018 } 2019 2020 if (riscv_cpu_cfg(env)->ext_smdbltrp) { 2021 mask |= MSTATUS_MDT; 2022 if ((val & MSTATUS_MDT) != 0) { 2023 val &= ~MSTATUS_MIE; 2024 } 2025 } 2026 2027 if (xl != MXL_RV32 || env->debugger) { 2028 if (riscv_has_ext(env, RVH)) { 2029 mask |= MSTATUS_MPV | MSTATUS_GVA; 2030 } 2031 if ((val & MSTATUS64_UXL) != 0) { 2032 mask |= MSTATUS64_UXL; 2033 } 2034 } 2035 2036 /* If cfi lp extension is available, then apply cfi lp mask */ 2037 if (env_archcpu(env)->cfg.ext_zicfilp) { 2038 mask |= (MSTATUS_MPELP | MSTATUS_SPELP); 2039 } 2040 2041 mstatus = (mstatus & ~mask) | (val & mask); 2042 2043 env->mstatus = mstatus; 2044 2045 /* 2046 * Except in debug mode, UXL/SXL can only be modified by higher 2047 * privilege mode. So xl will not be changed in normal mode. 2048 */ 2049 if (env->debugger) { 2050 env->xl = cpu_recompute_xl(env); 2051 } 2052 2053 return RISCV_EXCP_NONE; 2054 } 2055 2056 static RISCVException read_mstatush(CPURISCVState *env, int csrno, 2057 target_ulong *val) 2058 { 2059 *val = env->mstatus >> 32; 2060 return RISCV_EXCP_NONE; 2061 } 2062 2063 static RISCVException write_mstatush(CPURISCVState *env, int csrno, 2064 target_ulong val, uintptr_t ra) 2065 { 2066 uint64_t valh = (uint64_t)val << 32; 2067 uint64_t mask = riscv_has_ext(env, RVH) ? MSTATUS_MPV | MSTATUS_GVA : 0; 2068 2069 if (riscv_cpu_cfg(env)->ext_smdbltrp) { 2070 mask |= MSTATUS_MDT; 2071 if ((valh & MSTATUS_MDT) != 0) { 2072 mask |= MSTATUS_MIE; 2073 } 2074 } 2075 env->mstatus = (env->mstatus & ~mask) | (valh & mask); 2076 2077 return RISCV_EXCP_NONE; 2078 } 2079 2080 static RISCVException read_mstatus_i128(CPURISCVState *env, int csrno, 2081 Int128 *val) 2082 { 2083 *val = int128_make128(env->mstatus, add_status_sd(MXL_RV128, 2084 env->mstatus)); 2085 return RISCV_EXCP_NONE; 2086 } 2087 2088 static RISCVException read_misa_i128(CPURISCVState *env, int csrno, 2089 Int128 *val) 2090 { 2091 *val = int128_make128(env->misa_ext, (uint64_t)MXL_RV128 << 62); 2092 return RISCV_EXCP_NONE; 2093 } 2094 2095 static RISCVException read_misa(CPURISCVState *env, int csrno, 2096 target_ulong *val) 2097 { 2098 target_ulong misa; 2099 2100 switch (env->misa_mxl) { 2101 case MXL_RV32: 2102 misa = (target_ulong)MXL_RV32 << 30; 2103 break; 2104 #ifdef TARGET_RISCV64 2105 case MXL_RV64: 2106 misa = (target_ulong)MXL_RV64 << 62; 2107 break; 2108 #endif 2109 default: 2110 g_assert_not_reached(); 2111 } 2112 2113 *val = misa | env->misa_ext; 2114 return RISCV_EXCP_NONE; 2115 } 2116 2117 static target_ulong get_next_pc(CPURISCVState *env, uintptr_t ra) 2118 { 2119 uint64_t data[INSN_START_WORDS]; 2120 2121 /* Outside of a running cpu, env contains the next pc. */ 2122 if (ra == 0 || !cpu_unwind_state_data(env_cpu(env), ra, data)) { 2123 return env->pc; 2124 } 2125 2126 /* Within unwind data, [0] is pc and [1] is the opcode. */ 2127 return data[0] + insn_len(data[1]); 2128 } 2129 2130 static RISCVException write_misa(CPURISCVState *env, int csrno, 2131 target_ulong val, uintptr_t ra) 2132 { 2133 RISCVCPU *cpu = env_archcpu(env); 2134 uint32_t orig_misa_ext = env->misa_ext; 2135 Error *local_err = NULL; 2136 2137 if (!riscv_cpu_cfg(env)->misa_w) { 2138 /* drop write to misa */ 2139 return RISCV_EXCP_NONE; 2140 } 2141 2142 /* Mask extensions that are not supported by this hart */ 2143 val &= env->misa_ext_mask; 2144 2145 /* Suppress 'C' if next instruction is not aligned. */ 2146 if ((val & RVC) && (get_next_pc(env, ra) & 3) != 0) { 2147 val &= ~RVC; 2148 } 2149 2150 /* Disable RVG if any of its dependencies are disabled */ 2151 if (!(val & RVI && val & RVM && val & RVA && 2152 val & RVF && val & RVD)) { 2153 val &= ~RVG; 2154 } 2155 2156 /* If nothing changed, do nothing. */ 2157 if (val == env->misa_ext) { 2158 return RISCV_EXCP_NONE; 2159 } 2160 2161 env->misa_ext = val; 2162 riscv_cpu_validate_set_extensions(cpu, &local_err); 2163 if (local_err != NULL) { 2164 /* Rollback on validation error */ 2165 qemu_log_mask(LOG_GUEST_ERROR, "Unable to write MISA ext value " 2166 "0x%x, keeping existing MISA ext 0x%x\n", 2167 env->misa_ext, orig_misa_ext); 2168 2169 env->misa_ext = orig_misa_ext; 2170 2171 return RISCV_EXCP_NONE; 2172 } 2173 2174 if (!(env->misa_ext & RVF)) { 2175 env->mstatus &= ~MSTATUS_FS; 2176 } 2177 2178 env->xl = riscv_cpu_mxl(env); 2179 return RISCV_EXCP_NONE; 2180 } 2181 2182 static RISCVException read_medeleg(CPURISCVState *env, int csrno, 2183 target_ulong *val) 2184 { 2185 *val = env->medeleg; 2186 return RISCV_EXCP_NONE; 2187 } 2188 2189 static RISCVException write_medeleg(CPURISCVState *env, int csrno, 2190 target_ulong val, uintptr_t ra) 2191 { 2192 env->medeleg = (env->medeleg & ~DELEGABLE_EXCPS) | (val & DELEGABLE_EXCPS); 2193 return RISCV_EXCP_NONE; 2194 } 2195 2196 static RISCVException rmw_mideleg64(CPURISCVState *env, int csrno, 2197 uint64_t *ret_val, 2198 uint64_t new_val, uint64_t wr_mask) 2199 { 2200 uint64_t mask = wr_mask & delegable_ints; 2201 2202 if (ret_val) { 2203 *ret_val = env->mideleg; 2204 } 2205 2206 env->mideleg = (env->mideleg & ~mask) | (new_val & mask); 2207 2208 if (riscv_has_ext(env, RVH)) { 2209 env->mideleg |= HS_MODE_INTERRUPTS; 2210 } 2211 2212 return RISCV_EXCP_NONE; 2213 } 2214 2215 static RISCVException rmw_mideleg(CPURISCVState *env, int csrno, 2216 target_ulong *ret_val, 2217 target_ulong new_val, target_ulong wr_mask) 2218 { 2219 uint64_t rval; 2220 RISCVException ret; 2221 2222 ret = rmw_mideleg64(env, csrno, &rval, new_val, wr_mask); 2223 if (ret_val) { 2224 *ret_val = rval; 2225 } 2226 2227 return ret; 2228 } 2229 2230 static RISCVException rmw_midelegh(CPURISCVState *env, int csrno, 2231 target_ulong *ret_val, 2232 target_ulong new_val, 2233 target_ulong wr_mask) 2234 { 2235 uint64_t rval; 2236 RISCVException ret; 2237 2238 ret = rmw_mideleg64(env, csrno, &rval, 2239 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 2240 if (ret_val) { 2241 *ret_val = rval >> 32; 2242 } 2243 2244 return ret; 2245 } 2246 2247 static RISCVException rmw_mie64(CPURISCVState *env, int csrno, 2248 uint64_t *ret_val, 2249 uint64_t new_val, uint64_t wr_mask) 2250 { 2251 uint64_t mask = wr_mask & all_ints; 2252 2253 if (ret_val) { 2254 *ret_val = env->mie; 2255 } 2256 2257 env->mie = (env->mie & ~mask) | (new_val & mask); 2258 2259 if (!riscv_has_ext(env, RVH)) { 2260 env->mie &= ~((uint64_t)HS_MODE_INTERRUPTS); 2261 } 2262 2263 return RISCV_EXCP_NONE; 2264 } 2265 2266 static RISCVException rmw_mie(CPURISCVState *env, int csrno, 2267 target_ulong *ret_val, 2268 target_ulong new_val, target_ulong wr_mask) 2269 { 2270 uint64_t rval; 2271 RISCVException ret; 2272 2273 ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask); 2274 if (ret_val) { 2275 *ret_val = rval; 2276 } 2277 2278 return ret; 2279 } 2280 2281 static RISCVException rmw_mieh(CPURISCVState *env, int csrno, 2282 target_ulong *ret_val, 2283 target_ulong new_val, target_ulong wr_mask) 2284 { 2285 uint64_t rval; 2286 RISCVException ret; 2287 2288 ret = rmw_mie64(env, csrno, &rval, 2289 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 2290 if (ret_val) { 2291 *ret_val = rval >> 32; 2292 } 2293 2294 return ret; 2295 } 2296 2297 static RISCVException rmw_mvien64(CPURISCVState *env, int csrno, 2298 uint64_t *ret_val, 2299 uint64_t new_val, uint64_t wr_mask) 2300 { 2301 uint64_t mask = wr_mask & mvien_writable_mask; 2302 2303 if (ret_val) { 2304 *ret_val = env->mvien; 2305 } 2306 2307 env->mvien = (env->mvien & ~mask) | (new_val & mask); 2308 2309 return RISCV_EXCP_NONE; 2310 } 2311 2312 static RISCVException rmw_mvien(CPURISCVState *env, int csrno, 2313 target_ulong *ret_val, 2314 target_ulong new_val, target_ulong wr_mask) 2315 { 2316 uint64_t rval; 2317 RISCVException ret; 2318 2319 ret = rmw_mvien64(env, csrno, &rval, new_val, wr_mask); 2320 if (ret_val) { 2321 *ret_val = rval; 2322 } 2323 2324 return ret; 2325 } 2326 2327 static RISCVException rmw_mvienh(CPURISCVState *env, int csrno, 2328 target_ulong *ret_val, 2329 target_ulong new_val, target_ulong wr_mask) 2330 { 2331 uint64_t rval; 2332 RISCVException ret; 2333 2334 ret = rmw_mvien64(env, csrno, &rval, 2335 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 2336 if (ret_val) { 2337 *ret_val = rval >> 32; 2338 } 2339 2340 return ret; 2341 } 2342 2343 static RISCVException read_mtopi(CPURISCVState *env, int csrno, 2344 target_ulong *val) 2345 { 2346 int irq; 2347 uint8_t iprio; 2348 2349 irq = riscv_cpu_mirq_pending(env); 2350 if (irq <= 0 || irq > 63) { 2351 *val = 0; 2352 } else { 2353 iprio = env->miprio[irq]; 2354 if (!iprio) { 2355 if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_M) { 2356 iprio = IPRIO_MMAXIPRIO; 2357 } 2358 } 2359 *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT; 2360 *val |= iprio; 2361 } 2362 2363 return RISCV_EXCP_NONE; 2364 } 2365 2366 static int aia_xlate_vs_csrno(CPURISCVState *env, int csrno) 2367 { 2368 if (!env->virt_enabled) { 2369 return csrno; 2370 } 2371 2372 switch (csrno) { 2373 case CSR_SISELECT: 2374 return CSR_VSISELECT; 2375 case CSR_SIREG: 2376 return CSR_VSIREG; 2377 case CSR_STOPEI: 2378 return CSR_VSTOPEI; 2379 default: 2380 return csrno; 2381 }; 2382 } 2383 2384 static int csrind_xlate_vs_csrno(CPURISCVState *env, int csrno) 2385 { 2386 if (!env->virt_enabled) { 2387 return csrno; 2388 } 2389 2390 switch (csrno) { 2391 case CSR_SISELECT: 2392 return CSR_VSISELECT; 2393 case CSR_SIREG: 2394 case CSR_SIREG2: 2395 case CSR_SIREG3: 2396 case CSR_SIREG4: 2397 case CSR_SIREG5: 2398 case CSR_SIREG6: 2399 return CSR_VSIREG + (csrno - CSR_SIREG); 2400 default: 2401 return csrno; 2402 }; 2403 } 2404 2405 static RISCVException rmw_xiselect(CPURISCVState *env, int csrno, 2406 target_ulong *val, target_ulong new_val, 2407 target_ulong wr_mask) 2408 { 2409 target_ulong *iselect; 2410 int ret; 2411 2412 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); 2413 if (ret != RISCV_EXCP_NONE) { 2414 return ret; 2415 } 2416 2417 /* Translate CSR number for VS-mode */ 2418 csrno = csrind_xlate_vs_csrno(env, csrno); 2419 2420 /* Find the iselect CSR based on CSR number */ 2421 switch (csrno) { 2422 case CSR_MISELECT: 2423 iselect = &env->miselect; 2424 break; 2425 case CSR_SISELECT: 2426 iselect = &env->siselect; 2427 break; 2428 case CSR_VSISELECT: 2429 iselect = &env->vsiselect; 2430 break; 2431 default: 2432 return RISCV_EXCP_ILLEGAL_INST; 2433 }; 2434 2435 if (val) { 2436 *val = *iselect; 2437 } 2438 2439 if (riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind) { 2440 wr_mask &= ISELECT_MASK_SXCSRIND; 2441 } else { 2442 wr_mask &= ISELECT_MASK_AIA; 2443 } 2444 2445 if (wr_mask) { 2446 *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask); 2447 } 2448 2449 return RISCV_EXCP_NONE; 2450 } 2451 2452 static bool xiselect_aia_range(target_ulong isel) 2453 { 2454 return (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) || 2455 (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST); 2456 } 2457 2458 static bool xiselect_cd_range(target_ulong isel) 2459 { 2460 return (ISELECT_CD_FIRST <= isel && isel <= ISELECT_CD_LAST); 2461 } 2462 2463 static bool xiselect_ctr_range(int csrno, target_ulong isel) 2464 { 2465 /* MIREG-MIREG6 for the range 0x200-0x2ff are not used by CTR. */ 2466 return CTR_ENTRIES_FIRST <= isel && isel <= CTR_ENTRIES_LAST && 2467 csrno < CSR_MIREG; 2468 } 2469 2470 static int rmw_iprio(target_ulong xlen, 2471 target_ulong iselect, uint8_t *iprio, 2472 target_ulong *val, target_ulong new_val, 2473 target_ulong wr_mask, int ext_irq_no) 2474 { 2475 int i, firq, nirqs; 2476 target_ulong old_val; 2477 2478 if (iselect < ISELECT_IPRIO0 || ISELECT_IPRIO15 < iselect) { 2479 return -EINVAL; 2480 } 2481 if (xlen != 32 && iselect & 0x1) { 2482 return -EINVAL; 2483 } 2484 2485 nirqs = 4 * (xlen / 32); 2486 firq = ((iselect - ISELECT_IPRIO0) / (xlen / 32)) * (nirqs); 2487 2488 old_val = 0; 2489 for (i = 0; i < nirqs; i++) { 2490 old_val |= ((target_ulong)iprio[firq + i]) << (IPRIO_IRQ_BITS * i); 2491 } 2492 2493 if (val) { 2494 *val = old_val; 2495 } 2496 2497 if (wr_mask) { 2498 new_val = (old_val & ~wr_mask) | (new_val & wr_mask); 2499 for (i = 0; i < nirqs; i++) { 2500 /* 2501 * M-level and S-level external IRQ priority always read-only 2502 * zero. This means default priority order is always preferred 2503 * for M-level and S-level external IRQs. 2504 */ 2505 if ((firq + i) == ext_irq_no) { 2506 continue; 2507 } 2508 iprio[firq + i] = (new_val >> (IPRIO_IRQ_BITS * i)) & 0xff; 2509 } 2510 } 2511 2512 return 0; 2513 } 2514 2515 static int rmw_ctrsource(CPURISCVState *env, int isel, target_ulong *val, 2516 target_ulong new_val, target_ulong wr_mask) 2517 { 2518 /* 2519 * CTR arrays are treated as circular buffers and TOS always points to next 2520 * empty slot, keeping TOS - 1 always pointing to latest entry. Given entry 2521 * 0 is always the latest one, traversal is a bit different here. See the 2522 * below example. 2523 * 2524 * Depth = 16. 2525 * 2526 * idx [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] 2527 * TOS H 2528 * entry 6 5 4 3 2 1 0 F E D C B A 9 8 7 2529 */ 2530 const uint64_t entry = isel - CTR_ENTRIES_FIRST; 2531 const uint64_t depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); 2532 uint64_t idx; 2533 2534 /* Entry greater than depth-1 is read-only zero */ 2535 if (entry >= depth) { 2536 if (val) { 2537 *val = 0; 2538 } 2539 return 0; 2540 } 2541 2542 idx = get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); 2543 idx = (idx - entry - 1) & (depth - 1); 2544 2545 if (val) { 2546 *val = env->ctr_src[idx]; 2547 } 2548 2549 env->ctr_src[idx] = (env->ctr_src[idx] & ~wr_mask) | (new_val & wr_mask); 2550 2551 return 0; 2552 } 2553 2554 static int rmw_ctrtarget(CPURISCVState *env, int isel, target_ulong *val, 2555 target_ulong new_val, target_ulong wr_mask) 2556 { 2557 /* 2558 * CTR arrays are treated as circular buffers and TOS always points to next 2559 * empty slot, keeping TOS - 1 always pointing to latest entry. Given entry 2560 * 0 is always the latest one, traversal is a bit different here. See the 2561 * below example. 2562 * 2563 * Depth = 16. 2564 * 2565 * idx [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] 2566 * head H 2567 * entry 6 5 4 3 2 1 0 F E D C B A 9 8 7 2568 */ 2569 const uint64_t entry = isel - CTR_ENTRIES_FIRST; 2570 const uint64_t depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); 2571 uint64_t idx; 2572 2573 /* Entry greater than depth-1 is read-only zero */ 2574 if (entry >= depth) { 2575 if (val) { 2576 *val = 0; 2577 } 2578 return 0; 2579 } 2580 2581 idx = get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); 2582 idx = (idx - entry - 1) & (depth - 1); 2583 2584 if (val) { 2585 *val = env->ctr_dst[idx]; 2586 } 2587 2588 env->ctr_dst[idx] = (env->ctr_dst[idx] & ~wr_mask) | (new_val & wr_mask); 2589 2590 return 0; 2591 } 2592 2593 static int rmw_ctrdata(CPURISCVState *env, int isel, target_ulong *val, 2594 target_ulong new_val, target_ulong wr_mask) 2595 { 2596 /* 2597 * CTR arrays are treated as circular buffers and TOS always points to next 2598 * empty slot, keeping TOS - 1 always pointing to latest entry. Given entry 2599 * 0 is always the latest one, traversal is a bit different here. See the 2600 * below example. 2601 * 2602 * Depth = 16. 2603 * 2604 * idx [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] 2605 * head H 2606 * entry 6 5 4 3 2 1 0 F E D C B A 9 8 7 2607 */ 2608 const uint64_t entry = isel - CTR_ENTRIES_FIRST; 2609 const uint64_t mask = wr_mask & CTRDATA_MASK; 2610 const uint64_t depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); 2611 uint64_t idx; 2612 2613 /* Entry greater than depth-1 is read-only zero */ 2614 if (entry >= depth) { 2615 if (val) { 2616 *val = 0; 2617 } 2618 return 0; 2619 } 2620 2621 idx = get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); 2622 idx = (idx - entry - 1) & (depth - 1); 2623 2624 if (val) { 2625 *val = env->ctr_data[idx]; 2626 } 2627 2628 env->ctr_data[idx] = (env->ctr_data[idx] & ~mask) | (new_val & mask); 2629 2630 return 0; 2631 } 2632 2633 static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno, 2634 target_ulong isel, target_ulong *val, 2635 target_ulong new_val, target_ulong wr_mask) 2636 { 2637 bool virt = false, isel_reserved = false; 2638 int ret = -EINVAL; 2639 uint8_t *iprio; 2640 target_ulong priv, vgein; 2641 2642 /* VS-mode CSR number passed in has already been translated */ 2643 switch (csrno) { 2644 case CSR_MIREG: 2645 if (!riscv_cpu_cfg(env)->ext_smaia) { 2646 goto done; 2647 } 2648 iprio = env->miprio; 2649 priv = PRV_M; 2650 break; 2651 case CSR_SIREG: 2652 if (!riscv_cpu_cfg(env)->ext_ssaia || 2653 (env->priv == PRV_S && env->mvien & MIP_SEIP && 2654 env->siselect >= ISELECT_IMSIC_EIDELIVERY && 2655 env->siselect <= ISELECT_IMSIC_EIE63)) { 2656 goto done; 2657 } 2658 iprio = env->siprio; 2659 priv = PRV_S; 2660 break; 2661 case CSR_VSIREG: 2662 if (!riscv_cpu_cfg(env)->ext_ssaia) { 2663 goto done; 2664 } 2665 iprio = env->hviprio; 2666 priv = PRV_S; 2667 virt = true; 2668 break; 2669 default: 2670 goto done; 2671 }; 2672 2673 /* Find the selected guest interrupt file */ 2674 vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0; 2675 2676 if (ISELECT_IPRIO0 <= isel && isel <= ISELECT_IPRIO15) { 2677 /* Local interrupt priority registers not available for VS-mode */ 2678 if (!virt) { 2679 ret = rmw_iprio(riscv_cpu_mxl_bits(env), 2680 isel, iprio, val, new_val, wr_mask, 2681 (priv == PRV_M) ? IRQ_M_EXT : IRQ_S_EXT); 2682 } 2683 } else if (ISELECT_IMSIC_FIRST <= isel && isel <= ISELECT_IMSIC_LAST) { 2684 /* IMSIC registers only available when machine implements it. */ 2685 if (env->aia_ireg_rmw_fn[priv]) { 2686 /* Selected guest interrupt file should not be zero */ 2687 if (virt && (!vgein || env->geilen < vgein)) { 2688 goto done; 2689 } 2690 /* Call machine specific IMSIC register emulation */ 2691 ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], 2692 AIA_MAKE_IREG(isel, priv, virt, vgein, 2693 riscv_cpu_mxl_bits(env)), 2694 val, new_val, wr_mask); 2695 } 2696 } else { 2697 isel_reserved = true; 2698 } 2699 2700 done: 2701 /* 2702 * If AIA is not enabled, illegal instruction exception is always 2703 * returned regardless of whether we are in VS-mode or not 2704 */ 2705 if (ret) { 2706 return (env->virt_enabled && virt && !isel_reserved) ? 2707 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; 2708 } 2709 2710 return RISCV_EXCP_NONE; 2711 } 2712 2713 static int rmw_xireg_cd(CPURISCVState *env, int csrno, 2714 target_ulong isel, target_ulong *val, 2715 target_ulong new_val, target_ulong wr_mask) 2716 { 2717 int ret = -EINVAL; 2718 int ctr_index = isel - ISELECT_CD_FIRST; 2719 int isel_hpm_start = ISELECT_CD_FIRST + 3; 2720 2721 if (!riscv_cpu_cfg(env)->ext_smcdeleg || !riscv_cpu_cfg(env)->ext_ssccfg) { 2722 ret = RISCV_EXCP_ILLEGAL_INST; 2723 goto done; 2724 } 2725 2726 /* Invalid siselect value for reserved */ 2727 if (ctr_index == 1) { 2728 goto done; 2729 } 2730 2731 /* sireg4 and sireg5 provides access RV32 only CSRs */ 2732 if (((csrno == CSR_SIREG5) || (csrno == CSR_SIREG4)) && 2733 (riscv_cpu_mxl(env) != MXL_RV32)) { 2734 ret = RISCV_EXCP_ILLEGAL_INST; 2735 goto done; 2736 } 2737 2738 /* Check Sscofpmf dependancy */ 2739 if (!riscv_cpu_cfg(env)->ext_sscofpmf && csrno == CSR_SIREG5 && 2740 (isel_hpm_start <= isel && isel <= ISELECT_CD_LAST)) { 2741 goto done; 2742 } 2743 2744 /* Check smcntrpmf dependancy */ 2745 if (!riscv_cpu_cfg(env)->ext_smcntrpmf && 2746 (csrno == CSR_SIREG2 || csrno == CSR_SIREG5) && 2747 (ISELECT_CD_FIRST <= isel && isel < isel_hpm_start)) { 2748 goto done; 2749 } 2750 2751 if (!get_field(env->mcounteren, BIT(ctr_index)) || 2752 !get_field(env->menvcfg, MENVCFG_CDE)) { 2753 goto done; 2754 } 2755 2756 switch (csrno) { 2757 case CSR_SIREG: 2758 ret = rmw_cd_mhpmcounter(env, ctr_index, val, new_val, wr_mask); 2759 break; 2760 case CSR_SIREG4: 2761 ret = rmw_cd_mhpmcounterh(env, ctr_index, val, new_val, wr_mask); 2762 break; 2763 case CSR_SIREG2: 2764 if (ctr_index <= 2) { 2765 ret = rmw_cd_ctr_cfg(env, ctr_index, val, new_val, wr_mask); 2766 } else { 2767 ret = rmw_cd_mhpmevent(env, ctr_index, val, new_val, wr_mask); 2768 } 2769 break; 2770 case CSR_SIREG5: 2771 if (ctr_index <= 2) { 2772 ret = rmw_cd_ctr_cfgh(env, ctr_index, val, new_val, wr_mask); 2773 } else { 2774 ret = rmw_cd_mhpmeventh(env, ctr_index, val, new_val, wr_mask); 2775 } 2776 break; 2777 default: 2778 goto done; 2779 } 2780 2781 done: 2782 return ret; 2783 } 2784 2785 static int rmw_xireg_ctr(CPURISCVState *env, int csrno, 2786 target_ulong isel, target_ulong *val, 2787 target_ulong new_val, target_ulong wr_mask) 2788 { 2789 if (!riscv_cpu_cfg(env)->ext_smctr && !riscv_cpu_cfg(env)->ext_ssctr) { 2790 return -EINVAL; 2791 } 2792 2793 if (csrno == CSR_SIREG || csrno == CSR_VSIREG) { 2794 return rmw_ctrsource(env, isel, val, new_val, wr_mask); 2795 } else if (csrno == CSR_SIREG2 || csrno == CSR_VSIREG2) { 2796 return rmw_ctrtarget(env, isel, val, new_val, wr_mask); 2797 } else if (csrno == CSR_SIREG3 || csrno == CSR_VSIREG3) { 2798 return rmw_ctrdata(env, isel, val, new_val, wr_mask); 2799 } else if (val) { 2800 *val = 0; 2801 } 2802 2803 return 0; 2804 } 2805 2806 /* 2807 * rmw_xireg_csrind: Perform indirect access to xireg and xireg2-xireg6 2808 * 2809 * Perform indirect access to xireg and xireg2-xireg6. 2810 * This is a generic interface for all xireg CSRs. Apart from AIA, all other 2811 * extension using csrind should be implemented here. 2812 */ 2813 static int rmw_xireg_csrind(CPURISCVState *env, int csrno, 2814 target_ulong isel, target_ulong *val, 2815 target_ulong new_val, target_ulong wr_mask) 2816 { 2817 bool virt = csrno == CSR_VSIREG ? true : false; 2818 int ret = -EINVAL; 2819 2820 if (xiselect_cd_range(isel)) { 2821 ret = rmw_xireg_cd(env, csrno, isel, val, new_val, wr_mask); 2822 } else if (xiselect_ctr_range(csrno, isel)) { 2823 ret = rmw_xireg_ctr(env, csrno, isel, val, new_val, wr_mask); 2824 } else { 2825 /* 2826 * As per the specification, access to unimplented region is undefined 2827 * but recommendation is to raise illegal instruction exception. 2828 */ 2829 return RISCV_EXCP_ILLEGAL_INST; 2830 } 2831 2832 if (ret) { 2833 return (env->virt_enabled && virt) ? 2834 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; 2835 } 2836 2837 return RISCV_EXCP_NONE; 2838 } 2839 2840 static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, 2841 target_ulong new_val, target_ulong wr_mask) 2842 { 2843 int ret = -EINVAL; 2844 target_ulong isel; 2845 2846 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); 2847 if (ret != RISCV_EXCP_NONE) { 2848 return ret; 2849 } 2850 2851 /* Translate CSR number for VS-mode */ 2852 csrno = csrind_xlate_vs_csrno(env, csrno); 2853 2854 if (CSR_MIREG <= csrno && csrno <= CSR_MIREG6 && 2855 csrno != CSR_MIREG4 - 1) { 2856 isel = env->miselect; 2857 } else if (CSR_SIREG <= csrno && csrno <= CSR_SIREG6 && 2858 csrno != CSR_SIREG4 - 1) { 2859 isel = env->siselect; 2860 } else if (CSR_VSIREG <= csrno && csrno <= CSR_VSIREG6 && 2861 csrno != CSR_VSIREG4 - 1) { 2862 isel = env->vsiselect; 2863 } else { 2864 return RISCV_EXCP_ILLEGAL_INST; 2865 } 2866 2867 return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); 2868 } 2869 2870 static RISCVException rmw_xireg(CPURISCVState *env, int csrno, 2871 target_ulong *val, target_ulong new_val, 2872 target_ulong wr_mask) 2873 { 2874 int ret = -EINVAL; 2875 target_ulong isel; 2876 2877 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); 2878 if (ret != RISCV_EXCP_NONE) { 2879 return ret; 2880 } 2881 2882 /* Translate CSR number for VS-mode */ 2883 csrno = csrind_xlate_vs_csrno(env, csrno); 2884 2885 /* Decode register details from CSR number */ 2886 switch (csrno) { 2887 case CSR_MIREG: 2888 isel = env->miselect; 2889 break; 2890 case CSR_SIREG: 2891 isel = env->siselect; 2892 break; 2893 case CSR_VSIREG: 2894 isel = env->vsiselect; 2895 break; 2896 default: 2897 goto done; 2898 }; 2899 2900 /* 2901 * Use the xiselect range to determine actual op on xireg. 2902 * 2903 * Since we only checked the existence of AIA or Indirect Access in the 2904 * predicate, we should check the existence of the exact extension when 2905 * we get to a specific range and return illegal instruction exception even 2906 * in VS-mode. 2907 */ 2908 if (xiselect_aia_range(isel)) { 2909 return rmw_xireg_aia(env, csrno, isel, val, new_val, wr_mask); 2910 } else if (riscv_cpu_cfg(env)->ext_smcsrind || 2911 riscv_cpu_cfg(env)->ext_sscsrind) { 2912 return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask); 2913 } 2914 2915 done: 2916 return RISCV_EXCP_ILLEGAL_INST; 2917 } 2918 2919 static RISCVException rmw_xtopei(CPURISCVState *env, int csrno, 2920 target_ulong *val, target_ulong new_val, 2921 target_ulong wr_mask) 2922 { 2923 bool virt; 2924 int ret = -EINVAL; 2925 target_ulong priv, vgein; 2926 2927 /* Translate CSR number for VS-mode */ 2928 csrno = aia_xlate_vs_csrno(env, csrno); 2929 2930 /* Decode register details from CSR number */ 2931 virt = false; 2932 switch (csrno) { 2933 case CSR_MTOPEI: 2934 priv = PRV_M; 2935 break; 2936 case CSR_STOPEI: 2937 if (env->mvien & MIP_SEIP && env->priv == PRV_S) { 2938 goto done; 2939 } 2940 priv = PRV_S; 2941 break; 2942 case CSR_VSTOPEI: 2943 priv = PRV_S; 2944 virt = true; 2945 break; 2946 default: 2947 goto done; 2948 }; 2949 2950 /* IMSIC CSRs only available when machine implements IMSIC. */ 2951 if (!env->aia_ireg_rmw_fn[priv]) { 2952 goto done; 2953 } 2954 2955 /* Find the selected guest interrupt file */ 2956 vgein = (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0; 2957 2958 /* Selected guest interrupt file should be valid */ 2959 if (virt && (!vgein || env->geilen < vgein)) { 2960 goto done; 2961 } 2962 2963 /* Call machine specific IMSIC register emulation for TOPEI */ 2964 ret = env->aia_ireg_rmw_fn[priv](env->aia_ireg_rmw_fn_arg[priv], 2965 AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein, 2966 riscv_cpu_mxl_bits(env)), 2967 val, new_val, wr_mask); 2968 2969 done: 2970 if (ret) { 2971 return (env->virt_enabled && virt) ? 2972 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; 2973 } 2974 return RISCV_EXCP_NONE; 2975 } 2976 2977 static RISCVException read_mtvec(CPURISCVState *env, int csrno, 2978 target_ulong *val) 2979 { 2980 *val = env->mtvec; 2981 return RISCV_EXCP_NONE; 2982 } 2983 2984 static RISCVException write_mtvec(CPURISCVState *env, int csrno, 2985 target_ulong val, uintptr_t ra) 2986 { 2987 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */ 2988 if ((val & 3) < 2) { 2989 env->mtvec = val; 2990 } else { 2991 qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n"); 2992 } 2993 return RISCV_EXCP_NONE; 2994 } 2995 2996 static RISCVException read_mcountinhibit(CPURISCVState *env, int csrno, 2997 target_ulong *val) 2998 { 2999 *val = env->mcountinhibit; 3000 return RISCV_EXCP_NONE; 3001 } 3002 3003 static RISCVException write_mcountinhibit(CPURISCVState *env, int csrno, 3004 target_ulong val, uintptr_t ra) 3005 { 3006 int cidx; 3007 PMUCTRState *counter; 3008 RISCVCPU *cpu = env_archcpu(env); 3009 uint32_t present_ctrs = cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_IR; 3010 target_ulong updated_ctrs = (env->mcountinhibit ^ val) & present_ctrs; 3011 uint64_t mhpmctr_val, prev_count, curr_count; 3012 3013 /* WARL register - disable unavailable counters; TM bit is always 0 */ 3014 env->mcountinhibit = val & present_ctrs; 3015 3016 /* Check if any other counter is also monitoring cycles/instructions */ 3017 for (cidx = 0; cidx < RV_MAX_MHPMCOUNTERS; cidx++) { 3018 if (!(updated_ctrs & BIT(cidx)) || 3019 (!riscv_pmu_ctr_monitor_cycles(env, cidx) && 3020 !riscv_pmu_ctr_monitor_instructions(env, cidx))) { 3021 continue; 3022 } 3023 3024 counter = &env->pmu_ctrs[cidx]; 3025 3026 if (!get_field(env->mcountinhibit, BIT(cidx))) { 3027 counter->mhpmcounter_prev = 3028 riscv_pmu_ctr_get_fixed_counters_val(env, cidx, false); 3029 if (riscv_cpu_mxl(env) == MXL_RV32) { 3030 counter->mhpmcounterh_prev = 3031 riscv_pmu_ctr_get_fixed_counters_val(env, cidx, true); 3032 } 3033 3034 if (cidx > 2) { 3035 mhpmctr_val = counter->mhpmcounter_val; 3036 if (riscv_cpu_mxl(env) == MXL_RV32) { 3037 mhpmctr_val = mhpmctr_val | 3038 ((uint64_t)counter->mhpmcounterh_val << 32); 3039 } 3040 riscv_pmu_setup_timer(env, mhpmctr_val, cidx); 3041 } 3042 } else { 3043 curr_count = riscv_pmu_ctr_get_fixed_counters_val(env, cidx, false); 3044 3045 mhpmctr_val = counter->mhpmcounter_val; 3046 prev_count = counter->mhpmcounter_prev; 3047 if (riscv_cpu_mxl(env) == MXL_RV32) { 3048 uint64_t tmp = 3049 riscv_pmu_ctr_get_fixed_counters_val(env, cidx, true); 3050 3051 curr_count = curr_count | (tmp << 32); 3052 mhpmctr_val = mhpmctr_val | 3053 ((uint64_t)counter->mhpmcounterh_val << 32); 3054 prev_count = prev_count | 3055 ((uint64_t)counter->mhpmcounterh_prev << 32); 3056 } 3057 3058 /* Adjust the counter for later reads. */ 3059 mhpmctr_val = curr_count - prev_count + mhpmctr_val; 3060 counter->mhpmcounter_val = mhpmctr_val; 3061 if (riscv_cpu_mxl(env) == MXL_RV32) { 3062 counter->mhpmcounterh_val = mhpmctr_val >> 32; 3063 } 3064 } 3065 } 3066 3067 return RISCV_EXCP_NONE; 3068 } 3069 3070 static RISCVException read_scountinhibit(CPURISCVState *env, int csrno, 3071 target_ulong *val) 3072 { 3073 /* S-mode can only access the bits delegated by M-mode */ 3074 *val = env->mcountinhibit & env->mcounteren; 3075 return RISCV_EXCP_NONE; 3076 } 3077 3078 static RISCVException write_scountinhibit(CPURISCVState *env, int csrno, 3079 target_ulong val, uintptr_t ra) 3080 { 3081 return write_mcountinhibit(env, csrno, val & env->mcounteren, ra); 3082 } 3083 3084 static RISCVException read_mcounteren(CPURISCVState *env, int csrno, 3085 target_ulong *val) 3086 { 3087 *val = env->mcounteren; 3088 return RISCV_EXCP_NONE; 3089 } 3090 3091 static RISCVException write_mcounteren(CPURISCVState *env, int csrno, 3092 target_ulong val, uintptr_t ra) 3093 { 3094 RISCVCPU *cpu = env_archcpu(env); 3095 3096 /* WARL register - disable unavailable counters */ 3097 env->mcounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM | 3098 COUNTEREN_IR); 3099 return RISCV_EXCP_NONE; 3100 } 3101 3102 /* Machine Trap Handling */ 3103 static RISCVException read_mscratch_i128(CPURISCVState *env, int csrno, 3104 Int128 *val) 3105 { 3106 *val = int128_make128(env->mscratch, env->mscratchh); 3107 return RISCV_EXCP_NONE; 3108 } 3109 3110 static RISCVException write_mscratch_i128(CPURISCVState *env, int csrno, 3111 Int128 val) 3112 { 3113 env->mscratch = int128_getlo(val); 3114 env->mscratchh = int128_gethi(val); 3115 return RISCV_EXCP_NONE; 3116 } 3117 3118 static RISCVException read_mscratch(CPURISCVState *env, int csrno, 3119 target_ulong *val) 3120 { 3121 *val = env->mscratch; 3122 return RISCV_EXCP_NONE; 3123 } 3124 3125 static RISCVException write_mscratch(CPURISCVState *env, int csrno, 3126 target_ulong val, uintptr_t ra) 3127 { 3128 env->mscratch = val; 3129 return RISCV_EXCP_NONE; 3130 } 3131 3132 static RISCVException read_mepc(CPURISCVState *env, int csrno, 3133 target_ulong *val) 3134 { 3135 *val = env->mepc & get_xepc_mask(env); 3136 return RISCV_EXCP_NONE; 3137 } 3138 3139 static RISCVException write_mepc(CPURISCVState *env, int csrno, 3140 target_ulong val, uintptr_t ra) 3141 { 3142 env->mepc = val & get_xepc_mask(env); 3143 return RISCV_EXCP_NONE; 3144 } 3145 3146 static RISCVException read_mcause(CPURISCVState *env, int csrno, 3147 target_ulong *val) 3148 { 3149 *val = env->mcause; 3150 return RISCV_EXCP_NONE; 3151 } 3152 3153 static RISCVException write_mcause(CPURISCVState *env, int csrno, 3154 target_ulong val, uintptr_t ra) 3155 { 3156 env->mcause = val; 3157 return RISCV_EXCP_NONE; 3158 } 3159 3160 static RISCVException read_mtval(CPURISCVState *env, int csrno, 3161 target_ulong *val) 3162 { 3163 *val = env->mtval; 3164 return RISCV_EXCP_NONE; 3165 } 3166 3167 static RISCVException write_mtval(CPURISCVState *env, int csrno, 3168 target_ulong val, uintptr_t ra) 3169 { 3170 env->mtval = val; 3171 return RISCV_EXCP_NONE; 3172 } 3173 3174 /* Execution environment configuration setup */ 3175 static RISCVException read_menvcfg(CPURISCVState *env, int csrno, 3176 target_ulong *val) 3177 { 3178 *val = env->menvcfg; 3179 return RISCV_EXCP_NONE; 3180 } 3181 3182 static RISCVException write_henvcfg(CPURISCVState *env, int csrno, 3183 target_ulong val, uintptr_t ra); 3184 static RISCVException write_menvcfg(CPURISCVState *env, int csrno, 3185 target_ulong val, uintptr_t ra) 3186 { 3187 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); 3188 uint64_t mask = MENVCFG_FIOM | MENVCFG_CBIE | MENVCFG_CBCFE | 3189 MENVCFG_CBZE | MENVCFG_CDE; 3190 bool stce_changed = false; 3191 3192 if (riscv_cpu_mxl(env) == MXL_RV64) { 3193 mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) | 3194 (cfg->ext_sstc ? MENVCFG_STCE : 0) | 3195 (cfg->ext_smcdeleg ? MENVCFG_CDE : 0) | 3196 (cfg->ext_svadu ? MENVCFG_ADUE : 0) | 3197 (cfg->ext_ssdbltrp ? MENVCFG_DTE : 0); 3198 3199 if (env_archcpu(env)->cfg.ext_zicfilp) { 3200 mask |= MENVCFG_LPE; 3201 } 3202 3203 if (env_archcpu(env)->cfg.ext_zicfiss) { 3204 mask |= MENVCFG_SSE; 3205 } 3206 3207 /* Update PMM field only if the value is valid according to Zjpm v1.0 */ 3208 if (env_archcpu(env)->cfg.ext_smnpm && 3209 get_field(val, MENVCFG_PMM) != PMM_FIELD_RESERVED) { 3210 mask |= MENVCFG_PMM; 3211 } 3212 3213 if ((val & MENVCFG_DTE) == 0) { 3214 env->mstatus &= ~MSTATUS_SDT; 3215 } 3216 3217 if (cfg->ext_sstc && 3218 ((env->menvcfg & MENVCFG_STCE) != (val & MENVCFG_STCE))) { 3219 stce_changed = true; 3220 } 3221 } 3222 env->menvcfg = (env->menvcfg & ~mask) | (val & mask); 3223 3224 if (stce_changed) { 3225 riscv_timer_stce_changed(env, true, !!(val & MENVCFG_STCE)); 3226 } 3227 3228 return write_henvcfg(env, CSR_HENVCFG, env->henvcfg, ra); 3229 } 3230 3231 static RISCVException read_menvcfgh(CPURISCVState *env, int csrno, 3232 target_ulong *val) 3233 { 3234 *val = env->menvcfg >> 32; 3235 return RISCV_EXCP_NONE; 3236 } 3237 3238 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, 3239 target_ulong val, uintptr_t ra); 3240 static RISCVException write_menvcfgh(CPURISCVState *env, int csrno, 3241 target_ulong val, uintptr_t ra) 3242 { 3243 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); 3244 uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) | 3245 (cfg->ext_sstc ? MENVCFG_STCE : 0) | 3246 (cfg->ext_svadu ? MENVCFG_ADUE : 0) | 3247 (cfg->ext_smcdeleg ? MENVCFG_CDE : 0) | 3248 (cfg->ext_ssdbltrp ? MENVCFG_DTE : 0); 3249 uint64_t valh = (uint64_t)val << 32; 3250 bool stce_changed = false; 3251 3252 if (cfg->ext_sstc && 3253 ((env->menvcfg & MENVCFG_STCE) != (valh & MENVCFG_STCE))) { 3254 stce_changed = true; 3255 } 3256 3257 if ((valh & MENVCFG_DTE) == 0) { 3258 env->mstatus &= ~MSTATUS_SDT; 3259 } 3260 3261 env->menvcfg = (env->menvcfg & ~mask) | (valh & mask); 3262 3263 if (stce_changed) { 3264 riscv_timer_stce_changed(env, true, !!(valh & MENVCFG_STCE)); 3265 } 3266 3267 return write_henvcfgh(env, CSR_HENVCFGH, env->henvcfg >> 32, ra); 3268 } 3269 3270 static RISCVException read_senvcfg(CPURISCVState *env, int csrno, 3271 target_ulong *val) 3272 { 3273 RISCVException ret; 3274 3275 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); 3276 if (ret != RISCV_EXCP_NONE) { 3277 return ret; 3278 } 3279 3280 *val = env->senvcfg; 3281 return RISCV_EXCP_NONE; 3282 } 3283 3284 static RISCVException write_senvcfg(CPURISCVState *env, int csrno, 3285 target_ulong val, uintptr_t ra) 3286 { 3287 uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE; 3288 RISCVException ret; 3289 /* Update PMM field only if the value is valid according to Zjpm v1.0 */ 3290 if (env_archcpu(env)->cfg.ext_ssnpm && 3291 riscv_cpu_mxl(env) == MXL_RV64 && 3292 get_field(val, SENVCFG_PMM) != PMM_FIELD_RESERVED) { 3293 mask |= SENVCFG_PMM; 3294 } 3295 3296 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); 3297 if (ret != RISCV_EXCP_NONE) { 3298 return ret; 3299 } 3300 3301 if (env_archcpu(env)->cfg.ext_zicfilp) { 3302 mask |= SENVCFG_LPE; 3303 } 3304 3305 /* Higher mode SSE must be ON for next-less mode SSE to be ON */ 3306 if (env_archcpu(env)->cfg.ext_zicfiss && 3307 get_field(env->menvcfg, MENVCFG_SSE) && 3308 (env->virt_enabled ? get_field(env->henvcfg, HENVCFG_SSE) : true)) { 3309 mask |= SENVCFG_SSE; 3310 } 3311 3312 if (env_archcpu(env)->cfg.ext_svukte) { 3313 mask |= SENVCFG_UKTE; 3314 } 3315 3316 env->senvcfg = (env->senvcfg & ~mask) | (val & mask); 3317 return RISCV_EXCP_NONE; 3318 } 3319 3320 static RISCVException read_henvcfg(CPURISCVState *env, int csrno, 3321 target_ulong *val) 3322 { 3323 RISCVException ret; 3324 3325 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); 3326 if (ret != RISCV_EXCP_NONE) { 3327 return ret; 3328 } 3329 3330 /* 3331 * henvcfg.pbmte is read_only 0 when menvcfg.pbmte = 0 3332 * henvcfg.stce is read_only 0 when menvcfg.stce = 0 3333 * henvcfg.adue is read_only 0 when menvcfg.adue = 0 3334 * henvcfg.dte is read_only 0 when menvcfg.dte = 0 3335 */ 3336 *val = env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE | 3337 HENVCFG_DTE) | env->menvcfg); 3338 return RISCV_EXCP_NONE; 3339 } 3340 3341 static RISCVException write_henvcfg(CPURISCVState *env, int csrno, 3342 target_ulong val, uintptr_t ra) 3343 { 3344 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); 3345 uint64_t mask = HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCFG_CBZE; 3346 RISCVException ret; 3347 bool stce_changed = false; 3348 3349 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); 3350 if (ret != RISCV_EXCP_NONE) { 3351 return ret; 3352 } 3353 3354 if (riscv_cpu_mxl(env) == MXL_RV64) { 3355 mask |= env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE | 3356 HENVCFG_DTE); 3357 3358 if (env_archcpu(env)->cfg.ext_zicfilp) { 3359 mask |= HENVCFG_LPE; 3360 } 3361 3362 /* H can light up SSE for VS only if HS had it from menvcfg */ 3363 if (env_archcpu(env)->cfg.ext_zicfiss && 3364 get_field(env->menvcfg, MENVCFG_SSE)) { 3365 mask |= HENVCFG_SSE; 3366 } 3367 3368 /* Update PMM field only if the value is valid according to Zjpm v1.0 */ 3369 if (env_archcpu(env)->cfg.ext_ssnpm && 3370 get_field(val, HENVCFG_PMM) != PMM_FIELD_RESERVED) { 3371 mask |= HENVCFG_PMM; 3372 } 3373 3374 if (cfg->ext_sstc && 3375 ((env->henvcfg & HENVCFG_STCE) != (val & HENVCFG_STCE))) { 3376 stce_changed = true; 3377 } 3378 } 3379 3380 env->henvcfg = val & mask; 3381 if ((env->henvcfg & HENVCFG_DTE) == 0) { 3382 env->vsstatus &= ~MSTATUS_SDT; 3383 } 3384 3385 if (stce_changed) { 3386 riscv_timer_stce_changed(env, false, !!(val & HENVCFG_STCE)); 3387 } 3388 3389 return RISCV_EXCP_NONE; 3390 } 3391 3392 static RISCVException read_henvcfgh(CPURISCVState *env, int csrno, 3393 target_ulong *val) 3394 { 3395 RISCVException ret; 3396 3397 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); 3398 if (ret != RISCV_EXCP_NONE) { 3399 return ret; 3400 } 3401 3402 *val = (env->henvcfg & (~(HENVCFG_PBMTE | HENVCFG_STCE | HENVCFG_ADUE | 3403 HENVCFG_DTE) | env->menvcfg)) >> 32; 3404 return RISCV_EXCP_NONE; 3405 } 3406 3407 static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, 3408 target_ulong val, uintptr_t ra) 3409 { 3410 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env); 3411 uint64_t mask = env->menvcfg & (HENVCFG_PBMTE | HENVCFG_STCE | 3412 HENVCFG_ADUE | HENVCFG_DTE); 3413 uint64_t valh = (uint64_t)val << 32; 3414 RISCVException ret; 3415 bool stce_changed = false; 3416 3417 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); 3418 if (ret != RISCV_EXCP_NONE) { 3419 return ret; 3420 } 3421 3422 if (cfg->ext_sstc && 3423 ((env->henvcfg & HENVCFG_STCE) != (valh & HENVCFG_STCE))) { 3424 stce_changed = true; 3425 } 3426 3427 env->henvcfg = (env->henvcfg & 0xFFFFFFFF) | (valh & mask); 3428 if ((env->henvcfg & HENVCFG_DTE) == 0) { 3429 env->vsstatus &= ~MSTATUS_SDT; 3430 } 3431 3432 if (stce_changed) { 3433 riscv_timer_stce_changed(env, false, !!(val & HENVCFG_STCE)); 3434 } 3435 3436 return RISCV_EXCP_NONE; 3437 } 3438 3439 static RISCVException read_mstateen(CPURISCVState *env, int csrno, 3440 target_ulong *val) 3441 { 3442 *val = env->mstateen[csrno - CSR_MSTATEEN0]; 3443 3444 return RISCV_EXCP_NONE; 3445 } 3446 3447 static RISCVException write_mstateen(CPURISCVState *env, int csrno, 3448 uint64_t wr_mask, target_ulong new_val) 3449 { 3450 uint64_t *reg; 3451 3452 reg = &env->mstateen[csrno - CSR_MSTATEEN0]; 3453 *reg = (*reg & ~wr_mask) | (new_val & wr_mask); 3454 3455 return RISCV_EXCP_NONE; 3456 } 3457 3458 static RISCVException write_mstateen0(CPURISCVState *env, int csrno, 3459 target_ulong new_val, uintptr_t ra) 3460 { 3461 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG; 3462 if (!riscv_has_ext(env, RVF)) { 3463 wr_mask |= SMSTATEEN0_FCSR; 3464 } 3465 3466 if (env->priv_ver >= PRIV_VERSION_1_13_0) { 3467 wr_mask |= SMSTATEEN0_P1P13; 3468 } 3469 3470 if (riscv_cpu_cfg(env)->ext_smaia || riscv_cpu_cfg(env)->ext_smcsrind) { 3471 wr_mask |= SMSTATEEN0_SVSLCT; 3472 } 3473 3474 /* 3475 * As per the AIA specification, SMSTATEEN0_IMSIC is valid only if IMSIC is 3476 * implemented. However, that information is with MachineState and we can't 3477 * figure that out in csr.c. Just enable if Smaia is available. 3478 */ 3479 if (riscv_cpu_cfg(env)->ext_smaia) { 3480 wr_mask |= (SMSTATEEN0_AIA | SMSTATEEN0_IMSIC); 3481 } 3482 3483 if (riscv_cpu_cfg(env)->ext_ssctr) { 3484 wr_mask |= SMSTATEEN0_CTR; 3485 } 3486 3487 return write_mstateen(env, csrno, wr_mask, new_val); 3488 } 3489 3490 static RISCVException write_mstateen_1_3(CPURISCVState *env, int csrno, 3491 target_ulong new_val, uintptr_t ra) 3492 { 3493 return write_mstateen(env, csrno, SMSTATEEN_STATEEN, new_val); 3494 } 3495 3496 static RISCVException read_mstateenh(CPURISCVState *env, int csrno, 3497 target_ulong *val) 3498 { 3499 *val = env->mstateen[csrno - CSR_MSTATEEN0H] >> 32; 3500 3501 return RISCV_EXCP_NONE; 3502 } 3503 3504 static RISCVException write_mstateenh(CPURISCVState *env, int csrno, 3505 uint64_t wr_mask, target_ulong new_val) 3506 { 3507 uint64_t *reg, val; 3508 3509 reg = &env->mstateen[csrno - CSR_MSTATEEN0H]; 3510 val = (uint64_t)new_val << 32; 3511 val |= *reg & 0xFFFFFFFF; 3512 *reg = (*reg & ~wr_mask) | (val & wr_mask); 3513 3514 return RISCV_EXCP_NONE; 3515 } 3516 3517 static RISCVException write_mstateen0h(CPURISCVState *env, int csrno, 3518 target_ulong new_val, uintptr_t ra) 3519 { 3520 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG; 3521 3522 if (env->priv_ver >= PRIV_VERSION_1_13_0) { 3523 wr_mask |= SMSTATEEN0_P1P13; 3524 } 3525 3526 if (riscv_cpu_cfg(env)->ext_ssctr) { 3527 wr_mask |= SMSTATEEN0_CTR; 3528 } 3529 3530 return write_mstateenh(env, csrno, wr_mask, new_val); 3531 } 3532 3533 static RISCVException write_mstateenh_1_3(CPURISCVState *env, int csrno, 3534 target_ulong new_val, uintptr_t ra) 3535 { 3536 return write_mstateenh(env, csrno, SMSTATEEN_STATEEN, new_val); 3537 } 3538 3539 static RISCVException read_hstateen(CPURISCVState *env, int csrno, 3540 target_ulong *val) 3541 { 3542 int index = csrno - CSR_HSTATEEN0; 3543 3544 *val = env->hstateen[index] & env->mstateen[index]; 3545 3546 return RISCV_EXCP_NONE; 3547 } 3548 3549 static RISCVException write_hstateen(CPURISCVState *env, int csrno, 3550 uint64_t mask, target_ulong new_val) 3551 { 3552 int index = csrno - CSR_HSTATEEN0; 3553 uint64_t *reg, wr_mask; 3554 3555 reg = &env->hstateen[index]; 3556 wr_mask = env->mstateen[index] & mask; 3557 *reg = (*reg & ~wr_mask) | (new_val & wr_mask); 3558 3559 return RISCV_EXCP_NONE; 3560 } 3561 3562 static RISCVException write_hstateen0(CPURISCVState *env, int csrno, 3563 target_ulong new_val, uintptr_t ra) 3564 { 3565 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG; 3566 3567 if (!riscv_has_ext(env, RVF)) { 3568 wr_mask |= SMSTATEEN0_FCSR; 3569 } 3570 3571 if (riscv_cpu_cfg(env)->ext_ssaia || riscv_cpu_cfg(env)->ext_sscsrind) { 3572 wr_mask |= SMSTATEEN0_SVSLCT; 3573 } 3574 3575 /* 3576 * As per the AIA specification, SMSTATEEN0_IMSIC is valid only if IMSIC is 3577 * implemented. However, that information is with MachineState and we can't 3578 * figure that out in csr.c. Just enable if Ssaia is available. 3579 */ 3580 if (riscv_cpu_cfg(env)->ext_ssaia) { 3581 wr_mask |= (SMSTATEEN0_AIA | SMSTATEEN0_IMSIC); 3582 } 3583 3584 if (riscv_cpu_cfg(env)->ext_ssctr) { 3585 wr_mask |= SMSTATEEN0_CTR; 3586 } 3587 3588 return write_hstateen(env, csrno, wr_mask, new_val); 3589 } 3590 3591 static RISCVException write_hstateen_1_3(CPURISCVState *env, int csrno, 3592 target_ulong new_val, uintptr_t ra) 3593 { 3594 return write_hstateen(env, csrno, SMSTATEEN_STATEEN, new_val); 3595 } 3596 3597 static RISCVException read_hstateenh(CPURISCVState *env, int csrno, 3598 target_ulong *val) 3599 { 3600 int index = csrno - CSR_HSTATEEN0H; 3601 3602 *val = (env->hstateen[index] >> 32) & (env->mstateen[index] >> 32); 3603 3604 return RISCV_EXCP_NONE; 3605 } 3606 3607 static RISCVException write_hstateenh(CPURISCVState *env, int csrno, 3608 uint64_t mask, target_ulong new_val) 3609 { 3610 int index = csrno - CSR_HSTATEEN0H; 3611 uint64_t *reg, wr_mask, val; 3612 3613 reg = &env->hstateen[index]; 3614 val = (uint64_t)new_val << 32; 3615 val |= *reg & 0xFFFFFFFF; 3616 wr_mask = env->mstateen[index] & mask; 3617 *reg = (*reg & ~wr_mask) | (val & wr_mask); 3618 3619 return RISCV_EXCP_NONE; 3620 } 3621 3622 static RISCVException write_hstateen0h(CPURISCVState *env, int csrno, 3623 target_ulong new_val, uintptr_t ra) 3624 { 3625 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG; 3626 3627 if (riscv_cpu_cfg(env)->ext_ssctr) { 3628 wr_mask |= SMSTATEEN0_CTR; 3629 } 3630 3631 return write_hstateenh(env, csrno, wr_mask, new_val); 3632 } 3633 3634 static RISCVException write_hstateenh_1_3(CPURISCVState *env, int csrno, 3635 target_ulong new_val, uintptr_t ra) 3636 { 3637 return write_hstateenh(env, csrno, SMSTATEEN_STATEEN, new_val); 3638 } 3639 3640 static RISCVException read_sstateen(CPURISCVState *env, int csrno, 3641 target_ulong *val) 3642 { 3643 bool virt = env->virt_enabled; 3644 int index = csrno - CSR_SSTATEEN0; 3645 3646 *val = env->sstateen[index] & env->mstateen[index]; 3647 if (virt) { 3648 *val &= env->hstateen[index]; 3649 } 3650 3651 return RISCV_EXCP_NONE; 3652 } 3653 3654 static RISCVException write_sstateen(CPURISCVState *env, int csrno, 3655 uint64_t mask, target_ulong new_val) 3656 { 3657 bool virt = env->virt_enabled; 3658 int index = csrno - CSR_SSTATEEN0; 3659 uint64_t wr_mask; 3660 uint64_t *reg; 3661 3662 wr_mask = env->mstateen[index] & mask; 3663 if (virt) { 3664 wr_mask &= env->hstateen[index]; 3665 } 3666 3667 reg = &env->sstateen[index]; 3668 *reg = (*reg & ~wr_mask) | (new_val & wr_mask); 3669 3670 return RISCV_EXCP_NONE; 3671 } 3672 3673 static RISCVException write_sstateen0(CPURISCVState *env, int csrno, 3674 target_ulong new_val, uintptr_t ra) 3675 { 3676 uint64_t wr_mask = SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG; 3677 3678 if (!riscv_has_ext(env, RVF)) { 3679 wr_mask |= SMSTATEEN0_FCSR; 3680 } 3681 3682 return write_sstateen(env, csrno, wr_mask, new_val); 3683 } 3684 3685 static RISCVException write_sstateen_1_3(CPURISCVState *env, int csrno, 3686 target_ulong new_val, uintptr_t ra) 3687 { 3688 return write_sstateen(env, csrno, SMSTATEEN_STATEEN, new_val); 3689 } 3690 3691 static RISCVException rmw_mip64(CPURISCVState *env, int csrno, 3692 uint64_t *ret_val, 3693 uint64_t new_val, uint64_t wr_mask) 3694 { 3695 uint64_t old_mip, mask = wr_mask & delegable_ints; 3696 uint32_t gin; 3697 3698 if (mask & MIP_SEIP) { 3699 env->software_seip = new_val & MIP_SEIP; 3700 new_val |= env->external_seip * MIP_SEIP; 3701 } 3702 3703 if (riscv_cpu_cfg(env)->ext_sstc && (env->priv == PRV_M) && 3704 get_field(env->menvcfg, MENVCFG_STCE)) { 3705 /* sstc extension forbids STIP & VSTIP to be writeable in mip */ 3706 3707 /* STIP is not writable when menvcfg.STCE is enabled. */ 3708 mask = mask & ~MIP_STIP; 3709 3710 /* VSTIP is not writable when both [mh]envcfg.STCE are enabled. */ 3711 if (get_field(env->henvcfg, HENVCFG_STCE)) { 3712 mask = mask & ~MIP_VSTIP; 3713 } 3714 } 3715 3716 if (mask) { 3717 old_mip = riscv_cpu_update_mip(env, mask, (new_val & mask)); 3718 } else { 3719 old_mip = env->mip; 3720 } 3721 3722 if (csrno != CSR_HVIP) { 3723 gin = get_field(env->hstatus, HSTATUS_VGEIN); 3724 old_mip |= (env->hgeip & ((target_ulong)1 << gin)) ? MIP_VSEIP : 0; 3725 old_mip |= env->vstime_irq ? MIP_VSTIP : 0; 3726 } 3727 3728 if (ret_val) { 3729 *ret_val = old_mip; 3730 } 3731 3732 return RISCV_EXCP_NONE; 3733 } 3734 3735 static RISCVException rmw_mip(CPURISCVState *env, int csrno, 3736 target_ulong *ret_val, 3737 target_ulong new_val, target_ulong wr_mask) 3738 { 3739 uint64_t rval; 3740 RISCVException ret; 3741 3742 ret = rmw_mip64(env, csrno, &rval, new_val, wr_mask); 3743 if (ret_val) { 3744 *ret_val = rval; 3745 } 3746 3747 return ret; 3748 } 3749 3750 static RISCVException rmw_miph(CPURISCVState *env, int csrno, 3751 target_ulong *ret_val, 3752 target_ulong new_val, target_ulong wr_mask) 3753 { 3754 uint64_t rval; 3755 RISCVException ret; 3756 3757 ret = rmw_mip64(env, csrno, &rval, 3758 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 3759 if (ret_val) { 3760 *ret_val = rval >> 32; 3761 } 3762 3763 return ret; 3764 } 3765 3766 /* 3767 * The function is written for two use-cases: 3768 * 1- To access mvip csr as is for m-mode access. 3769 * 2- To access sip as a combination of mip and mvip for s-mode. 3770 * 3771 * Both report bits 1, 5, 9 and 13:63 but with the exception of 3772 * STIP being read-only zero in case of mvip when sstc extension 3773 * is present. 3774 * Also, sip needs to be read-only zero when both mideleg[i] and 3775 * mvien[i] are zero but mvip needs to be an alias of mip. 3776 */ 3777 static RISCVException rmw_mvip64(CPURISCVState *env, int csrno, 3778 uint64_t *ret_val, 3779 uint64_t new_val, uint64_t wr_mask) 3780 { 3781 RISCVCPU *cpu = env_archcpu(env); 3782 target_ulong ret_mip = 0; 3783 RISCVException ret; 3784 uint64_t old_mvip; 3785 3786 /* 3787 * mideleg[i] mvien[i] 3788 * 0 0 No delegation. mvip[i] is alias of mip[i]. 3789 * 0 1 mvip[i] becomes source of interrupt, mip bypassed. 3790 * 1 X mip[i] is source of interrupt and mvip[i] aliases 3791 * mip[i]. 3792 * 3793 * So alias condition would be for bits: 3794 * ((S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & (mideleg | ~mvien)) | 3795 * (!sstc & MIP_STIP) 3796 * 3797 * Non-alias condition will be for bits: 3798 * (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & (~mideleg & mvien) 3799 * 3800 * alias_mask denotes the bits that come from mip nalias_mask denotes bits 3801 * that come from hvip. 3802 */ 3803 uint64_t alias_mask = ((S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & 3804 (env->mideleg | ~env->mvien)) | MIP_STIP; 3805 uint64_t nalias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & 3806 (~env->mideleg & env->mvien); 3807 uint64_t wr_mask_mvip; 3808 uint64_t wr_mask_mip; 3809 3810 /* 3811 * mideleg[i] mvien[i] 3812 * 0 0 sip[i] read-only zero. 3813 * 0 1 sip[i] alias of mvip[i]. 3814 * 1 X sip[i] alias of mip[i]. 3815 * 3816 * Both alias and non-alias mask remain same for sip except for bits 3817 * which are zero in both mideleg and mvien. 3818 */ 3819 if (csrno == CSR_SIP) { 3820 /* Remove bits that are zero in both mideleg and mvien. */ 3821 alias_mask &= (env->mideleg | env->mvien); 3822 nalias_mask &= (env->mideleg | env->mvien); 3823 } 3824 3825 /* 3826 * If sstc is present, mvip.STIP is not an alias of mip.STIP so clear 3827 * that our in mip returned value. 3828 */ 3829 if (cpu->cfg.ext_sstc && (env->priv == PRV_M) && 3830 get_field(env->menvcfg, MENVCFG_STCE)) { 3831 alias_mask &= ~MIP_STIP; 3832 } 3833 3834 wr_mask_mip = wr_mask & alias_mask & mvip_writable_mask; 3835 wr_mask_mvip = wr_mask & nalias_mask & mvip_writable_mask; 3836 3837 /* 3838 * For bits set in alias_mask, mvip needs to be alias of mip, so forward 3839 * this to rmw_mip. 3840 */ 3841 ret = rmw_mip(env, CSR_MIP, &ret_mip, new_val, wr_mask_mip); 3842 if (ret != RISCV_EXCP_NONE) { 3843 return ret; 3844 } 3845 3846 old_mvip = env->mvip; 3847 3848 /* 3849 * Write to mvip. Update only non-alias bits. Alias bits were updated 3850 * in mip in rmw_mip above. 3851 */ 3852 if (wr_mask_mvip) { 3853 env->mvip = (env->mvip & ~wr_mask_mvip) | (new_val & wr_mask_mvip); 3854 3855 /* 3856 * Given mvip is separate source from mip, we need to trigger interrupt 3857 * from here separately. Normally this happen from riscv_cpu_update_mip. 3858 */ 3859 riscv_cpu_interrupt(env); 3860 } 3861 3862 if (ret_val) { 3863 ret_mip &= alias_mask; 3864 old_mvip &= nalias_mask; 3865 3866 *ret_val = old_mvip | ret_mip; 3867 } 3868 3869 return RISCV_EXCP_NONE; 3870 } 3871 3872 static RISCVException rmw_mvip(CPURISCVState *env, int csrno, 3873 target_ulong *ret_val, 3874 target_ulong new_val, target_ulong wr_mask) 3875 { 3876 uint64_t rval; 3877 RISCVException ret; 3878 3879 ret = rmw_mvip64(env, csrno, &rval, new_val, wr_mask); 3880 if (ret_val) { 3881 *ret_val = rval; 3882 } 3883 3884 return ret; 3885 } 3886 3887 static RISCVException rmw_mviph(CPURISCVState *env, int csrno, 3888 target_ulong *ret_val, 3889 target_ulong new_val, target_ulong wr_mask) 3890 { 3891 uint64_t rval; 3892 RISCVException ret; 3893 3894 ret = rmw_mvip64(env, csrno, &rval, 3895 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 3896 if (ret_val) { 3897 *ret_val = rval >> 32; 3898 } 3899 3900 return ret; 3901 } 3902 3903 /* Supervisor Trap Setup */ 3904 static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno, 3905 Int128 *val) 3906 { 3907 uint64_t mask = sstatus_v1_10_mask; 3908 uint64_t sstatus = env->mstatus & mask; 3909 if (env->xl != MXL_RV32 || env->debugger) { 3910 mask |= SSTATUS64_UXL; 3911 } 3912 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 3913 mask |= SSTATUS_SDT; 3914 } 3915 3916 if (env_archcpu(env)->cfg.ext_zicfilp) { 3917 mask |= SSTATUS_SPELP; 3918 } 3919 3920 *val = int128_make128(sstatus, add_status_sd(MXL_RV128, sstatus)); 3921 return RISCV_EXCP_NONE; 3922 } 3923 3924 static RISCVException read_sstatus(CPURISCVState *env, int csrno, 3925 target_ulong *val) 3926 { 3927 target_ulong mask = (sstatus_v1_10_mask); 3928 if (env->xl != MXL_RV32 || env->debugger) { 3929 mask |= SSTATUS64_UXL; 3930 } 3931 3932 if (env_archcpu(env)->cfg.ext_zicfilp) { 3933 mask |= SSTATUS_SPELP; 3934 } 3935 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 3936 mask |= SSTATUS_SDT; 3937 } 3938 /* TODO: Use SXL not MXL. */ 3939 *val = add_status_sd(riscv_cpu_mxl(env), env->mstatus & mask); 3940 return RISCV_EXCP_NONE; 3941 } 3942 3943 static RISCVException write_sstatus(CPURISCVState *env, int csrno, 3944 target_ulong val, uintptr_t ra) 3945 { 3946 target_ulong mask = (sstatus_v1_10_mask); 3947 3948 if (env->xl != MXL_RV32 || env->debugger) { 3949 if ((val & SSTATUS64_UXL) != 0) { 3950 mask |= SSTATUS64_UXL; 3951 } 3952 } 3953 3954 if (env_archcpu(env)->cfg.ext_zicfilp) { 3955 mask |= SSTATUS_SPELP; 3956 } 3957 if (riscv_cpu_cfg(env)->ext_ssdbltrp) { 3958 mask |= SSTATUS_SDT; 3959 } 3960 target_ulong newval = (env->mstatus & ~mask) | (val & mask); 3961 return write_mstatus(env, CSR_MSTATUS, newval, ra); 3962 } 3963 3964 static RISCVException rmw_vsie64(CPURISCVState *env, int csrno, 3965 uint64_t *ret_val, 3966 uint64_t new_val, uint64_t wr_mask) 3967 { 3968 uint64_t alias_mask = (LOCAL_INTERRUPTS | VS_MODE_INTERRUPTS) & 3969 env->hideleg; 3970 uint64_t nalias_mask = LOCAL_INTERRUPTS & (~env->hideleg & env->hvien); 3971 uint64_t rval, rval_vs, vsbits; 3972 uint64_t wr_mask_vsie; 3973 uint64_t wr_mask_mie; 3974 RISCVException ret; 3975 3976 /* Bring VS-level bits to correct position */ 3977 vsbits = new_val & (VS_MODE_INTERRUPTS >> 1); 3978 new_val &= ~(VS_MODE_INTERRUPTS >> 1); 3979 new_val |= vsbits << 1; 3980 3981 vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1); 3982 wr_mask &= ~(VS_MODE_INTERRUPTS >> 1); 3983 wr_mask |= vsbits << 1; 3984 3985 wr_mask_mie = wr_mask & alias_mask; 3986 wr_mask_vsie = wr_mask & nalias_mask; 3987 3988 ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask_mie); 3989 3990 rval_vs = env->vsie & nalias_mask; 3991 env->vsie = (env->vsie & ~wr_mask_vsie) | (new_val & wr_mask_vsie); 3992 3993 if (ret_val) { 3994 rval &= alias_mask; 3995 vsbits = rval & VS_MODE_INTERRUPTS; 3996 rval &= ~VS_MODE_INTERRUPTS; 3997 *ret_val = rval | (vsbits >> 1) | rval_vs; 3998 } 3999 4000 return ret; 4001 } 4002 4003 static RISCVException rmw_vsie(CPURISCVState *env, int csrno, 4004 target_ulong *ret_val, 4005 target_ulong new_val, target_ulong wr_mask) 4006 { 4007 uint64_t rval; 4008 RISCVException ret; 4009 4010 ret = rmw_vsie64(env, csrno, &rval, new_val, wr_mask); 4011 if (ret_val) { 4012 *ret_val = rval; 4013 } 4014 4015 return ret; 4016 } 4017 4018 static RISCVException rmw_vsieh(CPURISCVState *env, int csrno, 4019 target_ulong *ret_val, 4020 target_ulong new_val, target_ulong wr_mask) 4021 { 4022 uint64_t rval; 4023 RISCVException ret; 4024 4025 ret = rmw_vsie64(env, csrno, &rval, 4026 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4027 if (ret_val) { 4028 *ret_val = rval >> 32; 4029 } 4030 4031 return ret; 4032 } 4033 4034 static RISCVException rmw_sie64(CPURISCVState *env, int csrno, 4035 uint64_t *ret_val, 4036 uint64_t new_val, uint64_t wr_mask) 4037 { 4038 uint64_t nalias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & 4039 (~env->mideleg & env->mvien); 4040 uint64_t alias_mask = (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS) & env->mideleg; 4041 uint64_t sie_mask = wr_mask & nalias_mask; 4042 RISCVException ret; 4043 4044 /* 4045 * mideleg[i] mvien[i] 4046 * 0 0 sie[i] read-only zero. 4047 * 0 1 sie[i] is a separate writable bit. 4048 * 1 X sie[i] alias of mie[i]. 4049 * 4050 * Both alias and non-alias mask remain same for sip except for bits 4051 * which are zero in both mideleg and mvien. 4052 */ 4053 if (env->virt_enabled) { 4054 if (env->hvictl & HVICTL_VTI) { 4055 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 4056 } 4057 ret = rmw_vsie64(env, CSR_VSIE, ret_val, new_val, wr_mask); 4058 if (ret_val) { 4059 *ret_val &= alias_mask; 4060 } 4061 } else { 4062 ret = rmw_mie64(env, csrno, ret_val, new_val, wr_mask & alias_mask); 4063 if (ret_val) { 4064 *ret_val &= alias_mask; 4065 *ret_val |= env->sie & nalias_mask; 4066 } 4067 4068 env->sie = (env->sie & ~sie_mask) | (new_val & sie_mask); 4069 } 4070 4071 return ret; 4072 } 4073 4074 static RISCVException rmw_sie(CPURISCVState *env, int csrno, 4075 target_ulong *ret_val, 4076 target_ulong new_val, target_ulong wr_mask) 4077 { 4078 uint64_t rval; 4079 RISCVException ret; 4080 4081 ret = rmw_sie64(env, csrno, &rval, new_val, wr_mask); 4082 if (ret == RISCV_EXCP_NONE && ret_val) { 4083 *ret_val = rval; 4084 } 4085 4086 return ret; 4087 } 4088 4089 static RISCVException rmw_sieh(CPURISCVState *env, int csrno, 4090 target_ulong *ret_val, 4091 target_ulong new_val, target_ulong wr_mask) 4092 { 4093 uint64_t rval; 4094 RISCVException ret; 4095 4096 ret = rmw_sie64(env, csrno, &rval, 4097 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4098 if (ret_val) { 4099 *ret_val = rval >> 32; 4100 } 4101 4102 return ret; 4103 } 4104 4105 static RISCVException read_stvec(CPURISCVState *env, int csrno, 4106 target_ulong *val) 4107 { 4108 *val = env->stvec; 4109 return RISCV_EXCP_NONE; 4110 } 4111 4112 static RISCVException write_stvec(CPURISCVState *env, int csrno, 4113 target_ulong val, uintptr_t ra) 4114 { 4115 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */ 4116 if ((val & 3) < 2) { 4117 env->stvec = val; 4118 } else { 4119 qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n"); 4120 } 4121 return RISCV_EXCP_NONE; 4122 } 4123 4124 static RISCVException read_scounteren(CPURISCVState *env, int csrno, 4125 target_ulong *val) 4126 { 4127 *val = env->scounteren; 4128 return RISCV_EXCP_NONE; 4129 } 4130 4131 static RISCVException write_scounteren(CPURISCVState *env, int csrno, 4132 target_ulong val, uintptr_t ra) 4133 { 4134 RISCVCPU *cpu = env_archcpu(env); 4135 4136 /* WARL register - disable unavailable counters */ 4137 env->scounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM | 4138 COUNTEREN_IR); 4139 return RISCV_EXCP_NONE; 4140 } 4141 4142 /* Supervisor Trap Handling */ 4143 static RISCVException read_sscratch_i128(CPURISCVState *env, int csrno, 4144 Int128 *val) 4145 { 4146 *val = int128_make128(env->sscratch, env->sscratchh); 4147 return RISCV_EXCP_NONE; 4148 } 4149 4150 static RISCVException write_sscratch_i128(CPURISCVState *env, int csrno, 4151 Int128 val) 4152 { 4153 env->sscratch = int128_getlo(val); 4154 env->sscratchh = int128_gethi(val); 4155 return RISCV_EXCP_NONE; 4156 } 4157 4158 static RISCVException read_sscratch(CPURISCVState *env, int csrno, 4159 target_ulong *val) 4160 { 4161 *val = env->sscratch; 4162 return RISCV_EXCP_NONE; 4163 } 4164 4165 static RISCVException write_sscratch(CPURISCVState *env, int csrno, 4166 target_ulong val, uintptr_t ra) 4167 { 4168 env->sscratch = val; 4169 return RISCV_EXCP_NONE; 4170 } 4171 4172 static RISCVException read_sepc(CPURISCVState *env, int csrno, 4173 target_ulong *val) 4174 { 4175 *val = env->sepc & get_xepc_mask(env); 4176 return RISCV_EXCP_NONE; 4177 } 4178 4179 static RISCVException write_sepc(CPURISCVState *env, int csrno, 4180 target_ulong val, uintptr_t ra) 4181 { 4182 env->sepc = val & get_xepc_mask(env); 4183 return RISCV_EXCP_NONE; 4184 } 4185 4186 static RISCVException read_scause(CPURISCVState *env, int csrno, 4187 target_ulong *val) 4188 { 4189 *val = env->scause; 4190 return RISCV_EXCP_NONE; 4191 } 4192 4193 static RISCVException write_scause(CPURISCVState *env, int csrno, 4194 target_ulong val, uintptr_t ra) 4195 { 4196 env->scause = val; 4197 return RISCV_EXCP_NONE; 4198 } 4199 4200 static RISCVException read_stval(CPURISCVState *env, int csrno, 4201 target_ulong *val) 4202 { 4203 *val = env->stval; 4204 return RISCV_EXCP_NONE; 4205 } 4206 4207 static RISCVException write_stval(CPURISCVState *env, int csrno, 4208 target_ulong val, uintptr_t ra) 4209 { 4210 env->stval = val; 4211 return RISCV_EXCP_NONE; 4212 } 4213 4214 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno, 4215 uint64_t *ret_val, 4216 uint64_t new_val, uint64_t wr_mask); 4217 4218 static RISCVException rmw_vsip64(CPURISCVState *env, int csrno, 4219 uint64_t *ret_val, 4220 uint64_t new_val, uint64_t wr_mask) 4221 { 4222 RISCVException ret; 4223 uint64_t rval, mask = env->hideleg & VS_MODE_INTERRUPTS; 4224 uint64_t vsbits; 4225 4226 /* Add virtualized bits into vsip mask. */ 4227 mask |= env->hvien & ~env->hideleg; 4228 4229 /* Bring VS-level bits to correct position */ 4230 vsbits = new_val & (VS_MODE_INTERRUPTS >> 1); 4231 new_val &= ~(VS_MODE_INTERRUPTS >> 1); 4232 new_val |= vsbits << 1; 4233 vsbits = wr_mask & (VS_MODE_INTERRUPTS >> 1); 4234 wr_mask &= ~(VS_MODE_INTERRUPTS >> 1); 4235 wr_mask |= vsbits << 1; 4236 4237 ret = rmw_hvip64(env, csrno, &rval, new_val, 4238 wr_mask & mask & vsip_writable_mask); 4239 if (ret_val) { 4240 rval &= mask; 4241 vsbits = rval & VS_MODE_INTERRUPTS; 4242 rval &= ~VS_MODE_INTERRUPTS; 4243 *ret_val = rval | (vsbits >> 1); 4244 } 4245 4246 return ret; 4247 } 4248 4249 static RISCVException rmw_vsip(CPURISCVState *env, int csrno, 4250 target_ulong *ret_val, 4251 target_ulong new_val, target_ulong wr_mask) 4252 { 4253 uint64_t rval; 4254 RISCVException ret; 4255 4256 ret = rmw_vsip64(env, csrno, &rval, new_val, wr_mask); 4257 if (ret_val) { 4258 *ret_val = rval; 4259 } 4260 4261 return ret; 4262 } 4263 4264 static RISCVException rmw_vsiph(CPURISCVState *env, int csrno, 4265 target_ulong *ret_val, 4266 target_ulong new_val, target_ulong wr_mask) 4267 { 4268 uint64_t rval; 4269 RISCVException ret; 4270 4271 ret = rmw_vsip64(env, csrno, &rval, 4272 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4273 if (ret_val) { 4274 *ret_val = rval >> 32; 4275 } 4276 4277 return ret; 4278 } 4279 4280 static RISCVException rmw_sip64(CPURISCVState *env, int csrno, 4281 uint64_t *ret_val, 4282 uint64_t new_val, uint64_t wr_mask) 4283 { 4284 RISCVException ret; 4285 uint64_t mask = (env->mideleg | env->mvien) & sip_writable_mask; 4286 4287 if (env->virt_enabled) { 4288 if (env->hvictl & HVICTL_VTI) { 4289 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 4290 } 4291 ret = rmw_vsip64(env, CSR_VSIP, ret_val, new_val, wr_mask); 4292 } else { 4293 ret = rmw_mvip64(env, csrno, ret_val, new_val, wr_mask & mask); 4294 } 4295 4296 if (ret_val) { 4297 *ret_val &= (env->mideleg | env->mvien) & 4298 (S_MODE_INTERRUPTS | LOCAL_INTERRUPTS); 4299 } 4300 4301 return ret; 4302 } 4303 4304 static RISCVException rmw_sip(CPURISCVState *env, int csrno, 4305 target_ulong *ret_val, 4306 target_ulong new_val, target_ulong wr_mask) 4307 { 4308 uint64_t rval; 4309 RISCVException ret; 4310 4311 ret = rmw_sip64(env, csrno, &rval, new_val, wr_mask); 4312 if (ret_val) { 4313 *ret_val = rval; 4314 } 4315 4316 return ret; 4317 } 4318 4319 static RISCVException rmw_siph(CPURISCVState *env, int csrno, 4320 target_ulong *ret_val, 4321 target_ulong new_val, target_ulong wr_mask) 4322 { 4323 uint64_t rval; 4324 RISCVException ret; 4325 4326 ret = rmw_sip64(env, csrno, &rval, 4327 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4328 if (ret_val) { 4329 *ret_val = rval >> 32; 4330 } 4331 4332 return ret; 4333 } 4334 4335 /* Supervisor Protection and Translation */ 4336 static RISCVException read_satp(CPURISCVState *env, int csrno, 4337 target_ulong *val) 4338 { 4339 if (!riscv_cpu_cfg(env)->mmu) { 4340 *val = 0; 4341 return RISCV_EXCP_NONE; 4342 } 4343 *val = env->satp; 4344 return RISCV_EXCP_NONE; 4345 } 4346 4347 static RISCVException write_satp(CPURISCVState *env, int csrno, 4348 target_ulong val, uintptr_t ra) 4349 { 4350 if (!riscv_cpu_cfg(env)->mmu) { 4351 return RISCV_EXCP_NONE; 4352 } 4353 4354 env->satp = legalize_xatp(env, env->satp, val); 4355 return RISCV_EXCP_NONE; 4356 } 4357 4358 static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, 4359 target_ulong *ret_val, 4360 target_ulong new_val, target_ulong wr_mask) 4361 { 4362 uint64_t mask = wr_mask & SCTRDEPTH_MASK; 4363 4364 if (ret_val) { 4365 *ret_val = env->sctrdepth; 4366 } 4367 4368 env->sctrdepth = (env->sctrdepth & ~mask) | (new_val & mask); 4369 4370 /* Correct depth. */ 4371 if (mask) { 4372 uint64_t depth = get_field(env->sctrdepth, SCTRDEPTH_MASK); 4373 4374 if (depth > SCTRDEPTH_MAX) { 4375 depth = SCTRDEPTH_MAX; 4376 env->sctrdepth = set_field(env->sctrdepth, SCTRDEPTH_MASK, depth); 4377 } 4378 4379 /* Update sctrstatus.WRPTR with a legal value */ 4380 depth = 16ULL << depth; 4381 env->sctrstatus = 4382 env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1)); 4383 } 4384 4385 return RISCV_EXCP_NONE; 4386 } 4387 4388 static RISCVException rmw_sctrstatus(CPURISCVState *env, int csrno, 4389 target_ulong *ret_val, 4390 target_ulong new_val, target_ulong wr_mask) 4391 { 4392 uint32_t depth = 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); 4393 uint32_t mask = wr_mask & SCTRSTATUS_MASK; 4394 4395 if (ret_val) { 4396 *ret_val = env->sctrstatus; 4397 } 4398 4399 env->sctrstatus = (env->sctrstatus & ~mask) | (new_val & mask); 4400 4401 /* Update sctrstatus.WRPTR with a legal value */ 4402 env->sctrstatus = env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1)); 4403 4404 return RISCV_EXCP_NONE; 4405 } 4406 4407 static RISCVException rmw_xctrctl(CPURISCVState *env, int csrno, 4408 target_ulong *ret_val, 4409 target_ulong new_val, target_ulong wr_mask) 4410 { 4411 uint64_t csr_mask, mask = wr_mask; 4412 uint64_t *ctl_ptr = &env->mctrctl; 4413 4414 if (csrno == CSR_MCTRCTL) { 4415 csr_mask = MCTRCTL_MASK; 4416 } else if (csrno == CSR_SCTRCTL && !env->virt_enabled) { 4417 csr_mask = SCTRCTL_MASK; 4418 } else { 4419 /* 4420 * This is for csrno == CSR_SCTRCTL and env->virt_enabled == true 4421 * or csrno == CSR_VSCTRCTL. 4422 */ 4423 csr_mask = VSCTRCTL_MASK; 4424 ctl_ptr = &env->vsctrctl; 4425 } 4426 4427 mask &= csr_mask; 4428 4429 if (ret_val) { 4430 *ret_val = *ctl_ptr & csr_mask; 4431 } 4432 4433 *ctl_ptr = (*ctl_ptr & ~mask) | (new_val & mask); 4434 4435 return RISCV_EXCP_NONE; 4436 } 4437 4438 static RISCVException read_vstopi(CPURISCVState *env, int csrno, 4439 target_ulong *val) 4440 { 4441 int irq, ret; 4442 target_ulong topei; 4443 uint64_t vseip, vsgein; 4444 uint32_t iid, iprio, hviid, hviprio, gein; 4445 uint32_t s, scount = 0, siid[VSTOPI_NUM_SRCS], siprio[VSTOPI_NUM_SRCS]; 4446 4447 gein = get_field(env->hstatus, HSTATUS_VGEIN); 4448 hviid = get_field(env->hvictl, HVICTL_IID); 4449 hviprio = get_field(env->hvictl, HVICTL_IPRIO); 4450 4451 if (gein) { 4452 vsgein = (env->hgeip & (1ULL << gein)) ? MIP_VSEIP : 0; 4453 vseip = env->mie & (env->mip | vsgein) & MIP_VSEIP; 4454 if (gein <= env->geilen && vseip) { 4455 siid[scount] = IRQ_S_EXT; 4456 siprio[scount] = IPRIO_MMAXIPRIO + 1; 4457 if (env->aia_ireg_rmw_fn[PRV_S]) { 4458 /* 4459 * Call machine specific IMSIC register emulation for 4460 * reading TOPEI. 4461 */ 4462 ret = env->aia_ireg_rmw_fn[PRV_S]( 4463 env->aia_ireg_rmw_fn_arg[PRV_S], 4464 AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, PRV_S, true, gein, 4465 riscv_cpu_mxl_bits(env)), 4466 &topei, 0, 0); 4467 if (!ret && topei) { 4468 siprio[scount] = topei & IMSIC_TOPEI_IPRIO_MASK; 4469 } 4470 } 4471 scount++; 4472 } 4473 } else { 4474 if (hviid == IRQ_S_EXT && hviprio) { 4475 siid[scount] = IRQ_S_EXT; 4476 siprio[scount] = hviprio; 4477 scount++; 4478 } 4479 } 4480 4481 if (env->hvictl & HVICTL_VTI) { 4482 if (hviid != IRQ_S_EXT) { 4483 siid[scount] = hviid; 4484 siprio[scount] = hviprio; 4485 scount++; 4486 } 4487 } else { 4488 irq = riscv_cpu_vsirq_pending(env); 4489 if (irq != IRQ_S_EXT && 0 < irq && irq <= 63) { 4490 siid[scount] = irq; 4491 siprio[scount] = env->hviprio[irq]; 4492 scount++; 4493 } 4494 } 4495 4496 iid = 0; 4497 iprio = UINT_MAX; 4498 for (s = 0; s < scount; s++) { 4499 if (siprio[s] < iprio) { 4500 iid = siid[s]; 4501 iprio = siprio[s]; 4502 } 4503 } 4504 4505 if (iid) { 4506 if (env->hvictl & HVICTL_IPRIOM) { 4507 if (iprio > IPRIO_MMAXIPRIO) { 4508 iprio = IPRIO_MMAXIPRIO; 4509 } 4510 if (!iprio) { 4511 if (riscv_cpu_default_priority(iid) > IPRIO_DEFAULT_S) { 4512 iprio = IPRIO_MMAXIPRIO; 4513 } 4514 } 4515 } else { 4516 iprio = 1; 4517 } 4518 } else { 4519 iprio = 0; 4520 } 4521 4522 *val = (iid & TOPI_IID_MASK) << TOPI_IID_SHIFT; 4523 *val |= iprio; 4524 4525 return RISCV_EXCP_NONE; 4526 } 4527 4528 static RISCVException read_stopi(CPURISCVState *env, int csrno, 4529 target_ulong *val) 4530 { 4531 int irq; 4532 uint8_t iprio; 4533 4534 if (env->virt_enabled) { 4535 return read_vstopi(env, CSR_VSTOPI, val); 4536 } 4537 4538 irq = riscv_cpu_sirq_pending(env); 4539 if (irq <= 0 || irq > 63) { 4540 *val = 0; 4541 } else { 4542 iprio = env->siprio[irq]; 4543 if (!iprio) { 4544 if (riscv_cpu_default_priority(irq) > IPRIO_DEFAULT_S) { 4545 iprio = IPRIO_MMAXIPRIO; 4546 } 4547 } 4548 *val = (irq & TOPI_IID_MASK) << TOPI_IID_SHIFT; 4549 *val |= iprio; 4550 } 4551 4552 return RISCV_EXCP_NONE; 4553 } 4554 4555 /* Hypervisor Extensions */ 4556 static RISCVException read_hstatus(CPURISCVState *env, int csrno, 4557 target_ulong *val) 4558 { 4559 *val = env->hstatus; 4560 if (riscv_cpu_mxl(env) != MXL_RV32) { 4561 /* We only support 64-bit VSXL */ 4562 *val = set_field(*val, HSTATUS_VSXL, 2); 4563 } 4564 /* We only support little endian */ 4565 *val = set_field(*val, HSTATUS_VSBE, 0); 4566 return RISCV_EXCP_NONE; 4567 } 4568 4569 static RISCVException write_hstatus(CPURISCVState *env, int csrno, 4570 target_ulong val, uintptr_t ra) 4571 { 4572 uint64_t mask = (target_ulong)-1; 4573 if (!env_archcpu(env)->cfg.ext_svukte) { 4574 mask &= ~HSTATUS_HUKTE; 4575 } 4576 /* Update PMM field only if the value is valid according to Zjpm v1.0 */ 4577 if (!env_archcpu(env)->cfg.ext_ssnpm || 4578 riscv_cpu_mxl(env) != MXL_RV64 || 4579 get_field(val, HSTATUS_HUPMM) == PMM_FIELD_RESERVED) { 4580 mask &= ~HSTATUS_HUPMM; 4581 } 4582 env->hstatus = (env->hstatus & ~mask) | (val & mask); 4583 4584 if (riscv_cpu_mxl(env) != MXL_RV32 && get_field(val, HSTATUS_VSXL) != 2) { 4585 qemu_log_mask(LOG_UNIMP, 4586 "QEMU does not support mixed HSXLEN options."); 4587 } 4588 if (get_field(val, HSTATUS_VSBE) != 0) { 4589 qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests."); 4590 } 4591 return RISCV_EXCP_NONE; 4592 } 4593 4594 static RISCVException read_hedeleg(CPURISCVState *env, int csrno, 4595 target_ulong *val) 4596 { 4597 *val = env->hedeleg; 4598 return RISCV_EXCP_NONE; 4599 } 4600 4601 static RISCVException write_hedeleg(CPURISCVState *env, int csrno, 4602 target_ulong val, uintptr_t ra) 4603 { 4604 env->hedeleg = val & vs_delegable_excps; 4605 return RISCV_EXCP_NONE; 4606 } 4607 4608 static RISCVException read_hedelegh(CPURISCVState *env, int csrno, 4609 target_ulong *val) 4610 { 4611 RISCVException ret; 4612 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_P1P13); 4613 if (ret != RISCV_EXCP_NONE) { 4614 return ret; 4615 } 4616 4617 /* Reserved, now read zero */ 4618 *val = 0; 4619 return RISCV_EXCP_NONE; 4620 } 4621 4622 static RISCVException write_hedelegh(CPURISCVState *env, int csrno, 4623 target_ulong val, uintptr_t ra) 4624 { 4625 RISCVException ret; 4626 ret = smstateen_acc_ok(env, 0, SMSTATEEN0_P1P13); 4627 if (ret != RISCV_EXCP_NONE) { 4628 return ret; 4629 } 4630 4631 /* Reserved, now write ignore */ 4632 return RISCV_EXCP_NONE; 4633 } 4634 4635 static RISCVException rmw_hvien64(CPURISCVState *env, int csrno, 4636 uint64_t *ret_val, 4637 uint64_t new_val, uint64_t wr_mask) 4638 { 4639 uint64_t mask = wr_mask & hvien_writable_mask; 4640 4641 if (ret_val) { 4642 *ret_val = env->hvien; 4643 } 4644 4645 env->hvien = (env->hvien & ~mask) | (new_val & mask); 4646 4647 return RISCV_EXCP_NONE; 4648 } 4649 4650 static RISCVException rmw_hvien(CPURISCVState *env, int csrno, 4651 target_ulong *ret_val, 4652 target_ulong new_val, target_ulong wr_mask) 4653 { 4654 uint64_t rval; 4655 RISCVException ret; 4656 4657 ret = rmw_hvien64(env, csrno, &rval, new_val, wr_mask); 4658 if (ret_val) { 4659 *ret_val = rval; 4660 } 4661 4662 return ret; 4663 } 4664 4665 static RISCVException rmw_hvienh(CPURISCVState *env, int csrno, 4666 target_ulong *ret_val, 4667 target_ulong new_val, target_ulong wr_mask) 4668 { 4669 uint64_t rval; 4670 RISCVException ret; 4671 4672 ret = rmw_hvien64(env, csrno, &rval, 4673 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4674 if (ret_val) { 4675 *ret_val = rval >> 32; 4676 } 4677 4678 return ret; 4679 } 4680 4681 static RISCVException rmw_hideleg64(CPURISCVState *env, int csrno, 4682 uint64_t *ret_val, 4683 uint64_t new_val, uint64_t wr_mask) 4684 { 4685 uint64_t mask = wr_mask & vs_delegable_ints; 4686 4687 if (ret_val) { 4688 *ret_val = env->hideleg & vs_delegable_ints; 4689 } 4690 4691 env->hideleg = (env->hideleg & ~mask) | (new_val & mask); 4692 return RISCV_EXCP_NONE; 4693 } 4694 4695 static RISCVException rmw_hideleg(CPURISCVState *env, int csrno, 4696 target_ulong *ret_val, 4697 target_ulong new_val, target_ulong wr_mask) 4698 { 4699 uint64_t rval; 4700 RISCVException ret; 4701 4702 ret = rmw_hideleg64(env, csrno, &rval, new_val, wr_mask); 4703 if (ret_val) { 4704 *ret_val = rval; 4705 } 4706 4707 return ret; 4708 } 4709 4710 static RISCVException rmw_hidelegh(CPURISCVState *env, int csrno, 4711 target_ulong *ret_val, 4712 target_ulong new_val, target_ulong wr_mask) 4713 { 4714 uint64_t rval; 4715 RISCVException ret; 4716 4717 ret = rmw_hideleg64(env, csrno, &rval, 4718 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4719 if (ret_val) { 4720 *ret_val = rval >> 32; 4721 } 4722 4723 return ret; 4724 } 4725 4726 /* 4727 * The function is written for two use-cases: 4728 * 1- To access hvip csr as is for HS-mode access. 4729 * 2- To access vsip as a combination of hvip, and mip for vs-mode. 4730 * 4731 * Both report bits 2, 6, 10 and 13:63. 4732 * vsip needs to be read-only zero when both hideleg[i] and 4733 * hvien[i] are zero. 4734 */ 4735 static RISCVException rmw_hvip64(CPURISCVState *env, int csrno, 4736 uint64_t *ret_val, 4737 uint64_t new_val, uint64_t wr_mask) 4738 { 4739 RISCVException ret; 4740 uint64_t old_hvip; 4741 uint64_t ret_mip; 4742 4743 /* 4744 * For bits 10, 6 and 2, vsip[i] is an alias of hip[i]. These bits are 4745 * present in hip, hvip and mip. Where mip[i] is alias of hip[i] and hvip[i] 4746 * is OR'ed in hip[i] to inject virtual interrupts from hypervisor. These 4747 * bits are actually being maintained in mip so we read them from there. 4748 * This way we have a single source of truth and allows for easier 4749 * implementation. 4750 * 4751 * For bits 13:63 we have: 4752 * 4753 * hideleg[i] hvien[i] 4754 * 0 0 No delegation. vsip[i] readonly zero. 4755 * 0 1 vsip[i] is alias of hvip[i], sip bypassed. 4756 * 1 X vsip[i] is alias of sip[i], hvip bypassed. 4757 * 4758 * alias_mask denotes the bits that come from sip (mip here given we 4759 * maintain all bits there). nalias_mask denotes bits that come from 4760 * hvip. 4761 */ 4762 uint64_t alias_mask = (env->hideleg | ~env->hvien) | VS_MODE_INTERRUPTS; 4763 uint64_t nalias_mask = (~env->hideleg & env->hvien); 4764 uint64_t wr_mask_hvip; 4765 uint64_t wr_mask_mip; 4766 4767 /* 4768 * Both alias and non-alias mask remain same for vsip except: 4769 * 1- For VS* bits if they are zero in hideleg. 4770 * 2- For 13:63 bits if they are zero in both hideleg and hvien. 4771 */ 4772 if (csrno == CSR_VSIP) { 4773 /* zero-out VS* bits that are not delegated to VS mode. */ 4774 alias_mask &= (env->hideleg | ~VS_MODE_INTERRUPTS); 4775 4776 /* 4777 * zero-out 13:63 bits that are zero in both hideleg and hvien. 4778 * nalias_mask mask can not contain any VS* bits so only second 4779 * condition applies on it. 4780 */ 4781 nalias_mask &= (env->hideleg | env->hvien); 4782 alias_mask &= (env->hideleg | env->hvien); 4783 } 4784 4785 wr_mask_hvip = wr_mask & nalias_mask & hvip_writable_mask; 4786 wr_mask_mip = wr_mask & alias_mask & hvip_writable_mask; 4787 4788 /* Aliased bits, bits 10, 6, 2 need to come from mip. */ 4789 ret = rmw_mip64(env, csrno, &ret_mip, new_val, wr_mask_mip); 4790 if (ret != RISCV_EXCP_NONE) { 4791 return ret; 4792 } 4793 4794 old_hvip = env->hvip; 4795 4796 if (wr_mask_hvip) { 4797 env->hvip = (env->hvip & ~wr_mask_hvip) | (new_val & wr_mask_hvip); 4798 4799 /* 4800 * Given hvip is separate source from mip, we need to trigger interrupt 4801 * from here separately. Normally this happen from riscv_cpu_update_mip. 4802 */ 4803 riscv_cpu_interrupt(env); 4804 } 4805 4806 if (ret_val) { 4807 /* Only take VS* bits from mip. */ 4808 ret_mip &= alias_mask; 4809 4810 /* Take in non-delegated 13:63 bits from hvip. */ 4811 old_hvip &= nalias_mask; 4812 4813 *ret_val = ret_mip | old_hvip; 4814 } 4815 4816 return ret; 4817 } 4818 4819 static RISCVException rmw_hvip(CPURISCVState *env, int csrno, 4820 target_ulong *ret_val, 4821 target_ulong new_val, target_ulong wr_mask) 4822 { 4823 uint64_t rval; 4824 RISCVException ret; 4825 4826 ret = rmw_hvip64(env, csrno, &rval, new_val, wr_mask); 4827 if (ret_val) { 4828 *ret_val = rval; 4829 } 4830 4831 return ret; 4832 } 4833 4834 static RISCVException rmw_hviph(CPURISCVState *env, int csrno, 4835 target_ulong *ret_val, 4836 target_ulong new_val, target_ulong wr_mask) 4837 { 4838 uint64_t rval; 4839 RISCVException ret; 4840 4841 ret = rmw_hvip64(env, csrno, &rval, 4842 ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); 4843 if (ret_val) { 4844 *ret_val = rval >> 32; 4845 } 4846 4847 return ret; 4848 } 4849 4850 static RISCVException rmw_hip(CPURISCVState *env, int csrno, 4851 target_ulong *ret_value, 4852 target_ulong new_value, target_ulong write_mask) 4853 { 4854 int ret = rmw_mip(env, csrno, ret_value, new_value, 4855 write_mask & hip_writable_mask); 4856 4857 if (ret_value) { 4858 *ret_value &= HS_MODE_INTERRUPTS; 4859 } 4860 return ret; 4861 } 4862 4863 static RISCVException rmw_hie(CPURISCVState *env, int csrno, 4864 target_ulong *ret_val, 4865 target_ulong new_val, target_ulong wr_mask) 4866 { 4867 uint64_t rval; 4868 RISCVException ret; 4869 4870 ret = rmw_mie64(env, csrno, &rval, new_val, wr_mask & HS_MODE_INTERRUPTS); 4871 if (ret_val) { 4872 *ret_val = rval & HS_MODE_INTERRUPTS; 4873 } 4874 4875 return ret; 4876 } 4877 4878 static RISCVException read_hcounteren(CPURISCVState *env, int csrno, 4879 target_ulong *val) 4880 { 4881 *val = env->hcounteren; 4882 return RISCV_EXCP_NONE; 4883 } 4884 4885 static RISCVException write_hcounteren(CPURISCVState *env, int csrno, 4886 target_ulong val, uintptr_t ra) 4887 { 4888 RISCVCPU *cpu = env_archcpu(env); 4889 4890 /* WARL register - disable unavailable counters */ 4891 env->hcounteren = val & (cpu->pmu_avail_ctrs | COUNTEREN_CY | COUNTEREN_TM | 4892 COUNTEREN_IR); 4893 return RISCV_EXCP_NONE; 4894 } 4895 4896 static RISCVException read_hgeie(CPURISCVState *env, int csrno, 4897 target_ulong *val) 4898 { 4899 if (val) { 4900 *val = env->hgeie; 4901 } 4902 return RISCV_EXCP_NONE; 4903 } 4904 4905 static RISCVException write_hgeie(CPURISCVState *env, int csrno, 4906 target_ulong val, uintptr_t ra) 4907 { 4908 /* Only GEILEN:1 bits implemented and BIT0 is never implemented */ 4909 val &= ((((target_ulong)1) << env->geilen) - 1) << 1; 4910 env->hgeie = val; 4911 /* Update mip.SGEIP bit */ 4912 riscv_cpu_update_mip(env, MIP_SGEIP, 4913 BOOL_TO_MASK(!!(env->hgeie & env->hgeip))); 4914 return RISCV_EXCP_NONE; 4915 } 4916 4917 static RISCVException read_htval(CPURISCVState *env, int csrno, 4918 target_ulong *val) 4919 { 4920 *val = env->htval; 4921 return RISCV_EXCP_NONE; 4922 } 4923 4924 static RISCVException write_htval(CPURISCVState *env, int csrno, 4925 target_ulong val, uintptr_t ra) 4926 { 4927 env->htval = val; 4928 return RISCV_EXCP_NONE; 4929 } 4930 4931 static RISCVException read_htinst(CPURISCVState *env, int csrno, 4932 target_ulong *val) 4933 { 4934 *val = env->htinst; 4935 return RISCV_EXCP_NONE; 4936 } 4937 4938 static RISCVException write_htinst(CPURISCVState *env, int csrno, 4939 target_ulong val, uintptr_t ra) 4940 { 4941 return RISCV_EXCP_NONE; 4942 } 4943 4944 static RISCVException read_hgeip(CPURISCVState *env, int csrno, 4945 target_ulong *val) 4946 { 4947 if (val) { 4948 *val = env->hgeip; 4949 } 4950 return RISCV_EXCP_NONE; 4951 } 4952 4953 static RISCVException read_hgatp(CPURISCVState *env, int csrno, 4954 target_ulong *val) 4955 { 4956 *val = env->hgatp; 4957 return RISCV_EXCP_NONE; 4958 } 4959 4960 static RISCVException write_hgatp(CPURISCVState *env, int csrno, 4961 target_ulong val, uintptr_t ra) 4962 { 4963 env->hgatp = legalize_xatp(env, env->hgatp, val); 4964 return RISCV_EXCP_NONE; 4965 } 4966 4967 static RISCVException read_htimedelta(CPURISCVState *env, int csrno, 4968 target_ulong *val) 4969 { 4970 if (!env->rdtime_fn) { 4971 return RISCV_EXCP_ILLEGAL_INST; 4972 } 4973 4974 *val = env->htimedelta; 4975 return RISCV_EXCP_NONE; 4976 } 4977 4978 static RISCVException write_htimedelta(CPURISCVState *env, int csrno, 4979 target_ulong val, uintptr_t ra) 4980 { 4981 if (!env->rdtime_fn) { 4982 return RISCV_EXCP_ILLEGAL_INST; 4983 } 4984 4985 if (riscv_cpu_mxl(env) == MXL_RV32) { 4986 env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val); 4987 } else { 4988 env->htimedelta = val; 4989 } 4990 4991 if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) { 4992 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp, 4993 env->htimedelta, MIP_VSTIP); 4994 } 4995 4996 return RISCV_EXCP_NONE; 4997 } 4998 4999 static RISCVException read_htimedeltah(CPURISCVState *env, int csrno, 5000 target_ulong *val) 5001 { 5002 if (!env->rdtime_fn) { 5003 return RISCV_EXCP_ILLEGAL_INST; 5004 } 5005 5006 *val = env->htimedelta >> 32; 5007 return RISCV_EXCP_NONE; 5008 } 5009 5010 static RISCVException write_htimedeltah(CPURISCVState *env, int csrno, 5011 target_ulong val, uintptr_t ra) 5012 { 5013 if (!env->rdtime_fn) { 5014 return RISCV_EXCP_ILLEGAL_INST; 5015 } 5016 5017 env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val); 5018 5019 if (riscv_cpu_cfg(env)->ext_sstc && env->rdtime_fn) { 5020 riscv_timer_write_timecmp(env, env->vstimer, env->vstimecmp, 5021 env->htimedelta, MIP_VSTIP); 5022 } 5023 5024 return RISCV_EXCP_NONE; 5025 } 5026 5027 static RISCVException read_hvictl(CPURISCVState *env, int csrno, 5028 target_ulong *val) 5029 { 5030 *val = env->hvictl; 5031 return RISCV_EXCP_NONE; 5032 } 5033 5034 static RISCVException write_hvictl(CPURISCVState *env, int csrno, 5035 target_ulong val, uintptr_t ra) 5036 { 5037 env->hvictl = val & HVICTL_VALID_MASK; 5038 return RISCV_EXCP_NONE; 5039 } 5040 5041 static RISCVException read_hvipriox(CPURISCVState *env, int first_index, 5042 uint8_t *iprio, target_ulong *val) 5043 { 5044 int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32); 5045 5046 /* First index has to be a multiple of number of irqs per register */ 5047 if (first_index % num_irqs) { 5048 return (env->virt_enabled) ? 5049 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; 5050 } 5051 5052 /* Fill-up return value */ 5053 *val = 0; 5054 for (i = 0; i < num_irqs; i++) { 5055 if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) { 5056 continue; 5057 } 5058 if (rdzero) { 5059 continue; 5060 } 5061 *val |= ((target_ulong)iprio[irq]) << (i * 8); 5062 } 5063 5064 return RISCV_EXCP_NONE; 5065 } 5066 5067 static RISCVException write_hvipriox(CPURISCVState *env, int first_index, 5068 uint8_t *iprio, target_ulong val) 5069 { 5070 int i, irq, rdzero, num_irqs = 4 * (riscv_cpu_mxl_bits(env) / 32); 5071 5072 /* First index has to be a multiple of number of irqs per register */ 5073 if (first_index % num_irqs) { 5074 return (env->virt_enabled) ? 5075 RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; 5076 } 5077 5078 /* Fill-up priority array */ 5079 for (i = 0; i < num_irqs; i++) { 5080 if (riscv_cpu_hviprio_index2irq(first_index + i, &irq, &rdzero)) { 5081 continue; 5082 } 5083 if (rdzero) { 5084 iprio[irq] = 0; 5085 } else { 5086 iprio[irq] = (val >> (i * 8)) & 0xff; 5087 } 5088 } 5089 5090 return RISCV_EXCP_NONE; 5091 } 5092 5093 static RISCVException read_hviprio1(CPURISCVState *env, int csrno, 5094 target_ulong *val) 5095 { 5096 return read_hvipriox(env, 0, env->hviprio, val); 5097 } 5098 5099 static RISCVException write_hviprio1(CPURISCVState *env, int csrno, 5100 target_ulong val, uintptr_t ra) 5101 { 5102 return write_hvipriox(env, 0, env->hviprio, val); 5103 } 5104 5105 static RISCVException read_hviprio1h(CPURISCVState *env, int csrno, 5106 target_ulong *val) 5107 { 5108 return read_hvipriox(env, 4, env->hviprio, val); 5109 } 5110 5111 static RISCVException write_hviprio1h(CPURISCVState *env, int csrno, 5112 target_ulong val, uintptr_t ra) 5113 { 5114 return write_hvipriox(env, 4, env->hviprio, val); 5115 } 5116 5117 static RISCVException read_hviprio2(CPURISCVState *env, int csrno, 5118 target_ulong *val) 5119 { 5120 return read_hvipriox(env, 8, env->hviprio, val); 5121 } 5122 5123 static RISCVException write_hviprio2(CPURISCVState *env, int csrno, 5124 target_ulong val, uintptr_t ra) 5125 { 5126 return write_hvipriox(env, 8, env->hviprio, val); 5127 } 5128 5129 static RISCVException read_hviprio2h(CPURISCVState *env, int csrno, 5130 target_ulong *val) 5131 { 5132 return read_hvipriox(env, 12, env->hviprio, val); 5133 } 5134 5135 static RISCVException write_hviprio2h(CPURISCVState *env, int csrno, 5136 target_ulong val, uintptr_t ra) 5137 { 5138 return write_hvipriox(env, 12, env->hviprio, val); 5139 } 5140 5141 /* Virtual CSR Registers */ 5142 static RISCVException read_vsstatus(CPURISCVState *env, int csrno, 5143 target_ulong *val) 5144 { 5145 *val = env->vsstatus; 5146 return RISCV_EXCP_NONE; 5147 } 5148 5149 static RISCVException write_vsstatus(CPURISCVState *env, int csrno, 5150 target_ulong val, uintptr_t ra) 5151 { 5152 uint64_t mask = (target_ulong)-1; 5153 if ((val & VSSTATUS64_UXL) == 0) { 5154 mask &= ~VSSTATUS64_UXL; 5155 } 5156 if ((env->henvcfg & HENVCFG_DTE)) { 5157 if ((val & SSTATUS_SDT) != 0) { 5158 val &= ~SSTATUS_SIE; 5159 } 5160 } else { 5161 val &= ~SSTATUS_SDT; 5162 } 5163 env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val; 5164 return RISCV_EXCP_NONE; 5165 } 5166 5167 static RISCVException read_vstvec(CPURISCVState *env, int csrno, 5168 target_ulong *val) 5169 { 5170 *val = env->vstvec; 5171 return RISCV_EXCP_NONE; 5172 } 5173 5174 static RISCVException write_vstvec(CPURISCVState *env, int csrno, 5175 target_ulong val, uintptr_t ra) 5176 { 5177 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */ 5178 if ((val & 3) < 2) { 5179 env->vstvec = val; 5180 } else { 5181 qemu_log_mask(LOG_UNIMP, "CSR_VSTVEC: reserved mode not supported\n"); 5182 } 5183 return RISCV_EXCP_NONE; 5184 } 5185 5186 static RISCVException read_vsscratch(CPURISCVState *env, int csrno, 5187 target_ulong *val) 5188 { 5189 *val = env->vsscratch; 5190 return RISCV_EXCP_NONE; 5191 } 5192 5193 static RISCVException write_vsscratch(CPURISCVState *env, int csrno, 5194 target_ulong val, uintptr_t ra) 5195 { 5196 env->vsscratch = val; 5197 return RISCV_EXCP_NONE; 5198 } 5199 5200 static RISCVException read_vsepc(CPURISCVState *env, int csrno, 5201 target_ulong *val) 5202 { 5203 *val = env->vsepc; 5204 return RISCV_EXCP_NONE; 5205 } 5206 5207 static RISCVException write_vsepc(CPURISCVState *env, int csrno, 5208 target_ulong val, uintptr_t ra) 5209 { 5210 env->vsepc = val; 5211 return RISCV_EXCP_NONE; 5212 } 5213 5214 static RISCVException read_vscause(CPURISCVState *env, int csrno, 5215 target_ulong *val) 5216 { 5217 *val = env->vscause; 5218 return RISCV_EXCP_NONE; 5219 } 5220 5221 static RISCVException write_vscause(CPURISCVState *env, int csrno, 5222 target_ulong val, uintptr_t ra) 5223 { 5224 env->vscause = val; 5225 return RISCV_EXCP_NONE; 5226 } 5227 5228 static RISCVException read_vstval(CPURISCVState *env, int csrno, 5229 target_ulong *val) 5230 { 5231 *val = env->vstval; 5232 return RISCV_EXCP_NONE; 5233 } 5234 5235 static RISCVException write_vstval(CPURISCVState *env, int csrno, 5236 target_ulong val, uintptr_t ra) 5237 { 5238 env->vstval = val; 5239 return RISCV_EXCP_NONE; 5240 } 5241 5242 static RISCVException read_vsatp(CPURISCVState *env, int csrno, 5243 target_ulong *val) 5244 { 5245 *val = env->vsatp; 5246 return RISCV_EXCP_NONE; 5247 } 5248 5249 static RISCVException write_vsatp(CPURISCVState *env, int csrno, 5250 target_ulong val, uintptr_t ra) 5251 { 5252 env->vsatp = legalize_xatp(env, env->vsatp, val); 5253 return RISCV_EXCP_NONE; 5254 } 5255 5256 static RISCVException read_mtval2(CPURISCVState *env, int csrno, 5257 target_ulong *val) 5258 { 5259 *val = env->mtval2; 5260 return RISCV_EXCP_NONE; 5261 } 5262 5263 static RISCVException write_mtval2(CPURISCVState *env, int csrno, 5264 target_ulong val, uintptr_t ra) 5265 { 5266 env->mtval2 = val; 5267 return RISCV_EXCP_NONE; 5268 } 5269 5270 static RISCVException read_mtinst(CPURISCVState *env, int csrno, 5271 target_ulong *val) 5272 { 5273 *val = env->mtinst; 5274 return RISCV_EXCP_NONE; 5275 } 5276 5277 static RISCVException write_mtinst(CPURISCVState *env, int csrno, 5278 target_ulong val, uintptr_t ra) 5279 { 5280 env->mtinst = val; 5281 return RISCV_EXCP_NONE; 5282 } 5283 5284 /* Physical Memory Protection */ 5285 static RISCVException read_mseccfg(CPURISCVState *env, int csrno, 5286 target_ulong *val) 5287 { 5288 *val = mseccfg_csr_read(env); 5289 return RISCV_EXCP_NONE; 5290 } 5291 5292 static RISCVException write_mseccfg(CPURISCVState *env, int csrno, 5293 target_ulong val, uintptr_t ra) 5294 { 5295 mseccfg_csr_write(env, val); 5296 return RISCV_EXCP_NONE; 5297 } 5298 5299 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno, 5300 target_ulong *val) 5301 { 5302 uint32_t reg_index = csrno - CSR_PMPCFG0; 5303 5304 *val = pmpcfg_csr_read(env, reg_index); 5305 return RISCV_EXCP_NONE; 5306 } 5307 5308 static RISCVException write_pmpcfg(CPURISCVState *env, int csrno, 5309 target_ulong val, uintptr_t ra) 5310 { 5311 uint32_t reg_index = csrno - CSR_PMPCFG0; 5312 5313 pmpcfg_csr_write(env, reg_index, val); 5314 return RISCV_EXCP_NONE; 5315 } 5316 5317 static RISCVException read_pmpaddr(CPURISCVState *env, int csrno, 5318 target_ulong *val) 5319 { 5320 *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0); 5321 return RISCV_EXCP_NONE; 5322 } 5323 5324 static RISCVException write_pmpaddr(CPURISCVState *env, int csrno, 5325 target_ulong val, uintptr_t ra) 5326 { 5327 pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val); 5328 return RISCV_EXCP_NONE; 5329 } 5330 5331 static RISCVException read_tselect(CPURISCVState *env, int csrno, 5332 target_ulong *val) 5333 { 5334 *val = tselect_csr_read(env); 5335 return RISCV_EXCP_NONE; 5336 } 5337 5338 static RISCVException write_tselect(CPURISCVState *env, int csrno, 5339 target_ulong val, uintptr_t ra) 5340 { 5341 tselect_csr_write(env, val); 5342 return RISCV_EXCP_NONE; 5343 } 5344 5345 static RISCVException read_tdata(CPURISCVState *env, int csrno, 5346 target_ulong *val) 5347 { 5348 /* return 0 in tdata1 to end the trigger enumeration */ 5349 if (env->trigger_cur >= RV_MAX_TRIGGERS && csrno == CSR_TDATA1) { 5350 *val = 0; 5351 return RISCV_EXCP_NONE; 5352 } 5353 5354 if (!tdata_available(env, csrno - CSR_TDATA1)) { 5355 return RISCV_EXCP_ILLEGAL_INST; 5356 } 5357 5358 *val = tdata_csr_read(env, csrno - CSR_TDATA1); 5359 return RISCV_EXCP_NONE; 5360 } 5361 5362 static RISCVException write_tdata(CPURISCVState *env, int csrno, 5363 target_ulong val, uintptr_t ra) 5364 { 5365 if (!tdata_available(env, csrno - CSR_TDATA1)) { 5366 return RISCV_EXCP_ILLEGAL_INST; 5367 } 5368 5369 tdata_csr_write(env, csrno - CSR_TDATA1, val); 5370 return RISCV_EXCP_NONE; 5371 } 5372 5373 static RISCVException read_tinfo(CPURISCVState *env, int csrno, 5374 target_ulong *val) 5375 { 5376 *val = tinfo_csr_read(env); 5377 return RISCV_EXCP_NONE; 5378 } 5379 5380 static RISCVException read_mcontext(CPURISCVState *env, int csrno, 5381 target_ulong *val) 5382 { 5383 *val = env->mcontext; 5384 return RISCV_EXCP_NONE; 5385 } 5386 5387 static RISCVException write_mcontext(CPURISCVState *env, int csrno, 5388 target_ulong val, uintptr_t ra) 5389 { 5390 bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false; 5391 int32_t mask; 5392 5393 if (riscv_has_ext(env, RVH)) { 5394 /* Spec suggest 7-bit for RV32 and 14-bit for RV64 w/ H extension */ 5395 mask = rv32 ? MCONTEXT32_HCONTEXT : MCONTEXT64_HCONTEXT; 5396 } else { 5397 /* Spec suggest 6-bit for RV32 and 13-bit for RV64 w/o H extension */ 5398 mask = rv32 ? MCONTEXT32 : MCONTEXT64; 5399 } 5400 5401 env->mcontext = val & mask; 5402 return RISCV_EXCP_NONE; 5403 } 5404 5405 static RISCVException read_mnscratch(CPURISCVState *env, int csrno, 5406 target_ulong *val) 5407 { 5408 *val = env->mnscratch; 5409 return RISCV_EXCP_NONE; 5410 } 5411 5412 static RISCVException write_mnscratch(CPURISCVState *env, int csrno, 5413 target_ulong val, uintptr_t ra) 5414 { 5415 env->mnscratch = val; 5416 return RISCV_EXCP_NONE; 5417 } 5418 5419 static RISCVException read_mnepc(CPURISCVState *env, int csrno, 5420 target_ulong *val) 5421 { 5422 *val = env->mnepc; 5423 return RISCV_EXCP_NONE; 5424 } 5425 5426 static RISCVException write_mnepc(CPURISCVState *env, int csrno, 5427 target_ulong val, uintptr_t ra) 5428 { 5429 env->mnepc = val; 5430 return RISCV_EXCP_NONE; 5431 } 5432 5433 static RISCVException read_mncause(CPURISCVState *env, int csrno, 5434 target_ulong *val) 5435 { 5436 *val = env->mncause; 5437 return RISCV_EXCP_NONE; 5438 } 5439 5440 static RISCVException write_mncause(CPURISCVState *env, int csrno, 5441 target_ulong val, uintptr_t ra) 5442 { 5443 env->mncause = val; 5444 return RISCV_EXCP_NONE; 5445 } 5446 5447 static RISCVException read_mnstatus(CPURISCVState *env, int csrno, 5448 target_ulong *val) 5449 { 5450 *val = env->mnstatus; 5451 return RISCV_EXCP_NONE; 5452 } 5453 5454 static RISCVException write_mnstatus(CPURISCVState *env, int csrno, 5455 target_ulong val, uintptr_t ra) 5456 { 5457 target_ulong mask = (MNSTATUS_NMIE | MNSTATUS_MNPP); 5458 5459 if (riscv_has_ext(env, RVH)) { 5460 /* Flush tlb on mnstatus fields that affect VM. */ 5461 if ((val ^ env->mnstatus) & MNSTATUS_MNPV) { 5462 tlb_flush(env_cpu(env)); 5463 } 5464 5465 mask |= MNSTATUS_MNPV; 5466 } 5467 5468 /* mnstatus.mnie can only be cleared by hardware. */ 5469 env->mnstatus = (env->mnstatus & MNSTATUS_NMIE) | (val & mask); 5470 return RISCV_EXCP_NONE; 5471 } 5472 5473 #endif 5474 5475 /* Crypto Extension */ 5476 target_ulong riscv_new_csr_seed(target_ulong new_value, 5477 target_ulong write_mask) 5478 { 5479 uint16_t random_v; 5480 Error *random_e = NULL; 5481 int random_r; 5482 target_ulong rval; 5483 5484 random_r = qemu_guest_getrandom(&random_v, 2, &random_e); 5485 if (unlikely(random_r < 0)) { 5486 /* 5487 * Failed, for unknown reasons in the crypto subsystem. 5488 * The best we can do is log the reason and return a 5489 * failure indication to the guest. There is no reason 5490 * we know to expect the failure to be transitory, so 5491 * indicate DEAD to avoid having the guest spin on WAIT. 5492 */ 5493 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s", 5494 __func__, error_get_pretty(random_e)); 5495 error_free(random_e); 5496 rval = SEED_OPST_DEAD; 5497 } else { 5498 rval = random_v | SEED_OPST_ES16; 5499 } 5500 5501 return rval; 5502 } 5503 5504 static RISCVException rmw_seed(CPURISCVState *env, int csrno, 5505 target_ulong *ret_value, 5506 target_ulong new_value, 5507 target_ulong write_mask) 5508 { 5509 target_ulong rval; 5510 5511 rval = riscv_new_csr_seed(new_value, write_mask); 5512 5513 if (ret_value) { 5514 *ret_value = rval; 5515 } 5516 5517 return RISCV_EXCP_NONE; 5518 } 5519 5520 /* 5521 * riscv_csrrw - read and/or update control and status register 5522 * 5523 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0); 5524 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1); 5525 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value); 5526 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value); 5527 */ 5528 5529 static inline RISCVException riscv_csrrw_check(CPURISCVState *env, 5530 int csrno, 5531 bool write) 5532 { 5533 /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails */ 5534 bool read_only = get_field(csrno, 0xC00) == 3; 5535 int csr_min_priv = csr_ops[csrno].min_priv_ver; 5536 5537 /* ensure the CSR extension is enabled */ 5538 if (!riscv_cpu_cfg(env)->ext_zicsr) { 5539 return RISCV_EXCP_ILLEGAL_INST; 5540 } 5541 5542 /* ensure CSR is implemented by checking predicate */ 5543 if (!csr_ops[csrno].predicate) { 5544 return RISCV_EXCP_ILLEGAL_INST; 5545 } 5546 5547 /* privileged spec version check */ 5548 if (env->priv_ver < csr_min_priv) { 5549 return RISCV_EXCP_ILLEGAL_INST; 5550 } 5551 5552 /* read / write check */ 5553 if (write && read_only) { 5554 return RISCV_EXCP_ILLEGAL_INST; 5555 } 5556 5557 /* 5558 * The predicate() not only does existence check but also does some 5559 * access control check which triggers for example virtual instruction 5560 * exception in some cases. When writing read-only CSRs in those cases 5561 * illegal instruction exception should be triggered instead of virtual 5562 * instruction exception. Hence this comes after the read / write check. 5563 */ 5564 RISCVException ret = csr_ops[csrno].predicate(env, csrno); 5565 if (ret != RISCV_EXCP_NONE) { 5566 return ret; 5567 } 5568 5569 #if !defined(CONFIG_USER_ONLY) 5570 int csr_priv, effective_priv = env->priv; 5571 5572 if (riscv_has_ext(env, RVH) && env->priv == PRV_S && 5573 !env->virt_enabled) { 5574 /* 5575 * We are in HS mode. Add 1 to the effective privilege level to 5576 * allow us to access the Hypervisor CSRs. 5577 */ 5578 effective_priv++; 5579 } 5580 5581 csr_priv = get_field(csrno, 0x300); 5582 if (!env->debugger && (effective_priv < csr_priv)) { 5583 if (csr_priv <= (PRV_S + 1) && env->virt_enabled) { 5584 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 5585 } 5586 return RISCV_EXCP_ILLEGAL_INST; 5587 } 5588 #endif 5589 return RISCV_EXCP_NONE; 5590 } 5591 5592 static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno, 5593 target_ulong *ret_value, 5594 target_ulong new_value, 5595 target_ulong write_mask, 5596 uintptr_t ra) 5597 { 5598 RISCVException ret; 5599 target_ulong old_value = 0; 5600 5601 /* execute combined read/write operation if it exists */ 5602 if (csr_ops[csrno].op) { 5603 return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask); 5604 } 5605 5606 /* 5607 * ret_value == NULL means that rd=x0 and we're coming from helper_csrw() 5608 * and we can't throw side effects caused by CSR reads. 5609 */ 5610 if (ret_value) { 5611 /* if no accessor exists then return failure */ 5612 if (!csr_ops[csrno].read) { 5613 return RISCV_EXCP_ILLEGAL_INST; 5614 } 5615 /* read old value */ 5616 ret = csr_ops[csrno].read(env, csrno, &old_value); 5617 if (ret != RISCV_EXCP_NONE) { 5618 return ret; 5619 } 5620 } 5621 5622 /* write value if writable and write mask set, otherwise drop writes */ 5623 if (write_mask) { 5624 new_value = (old_value & ~write_mask) | (new_value & write_mask); 5625 if (csr_ops[csrno].write) { 5626 ret = csr_ops[csrno].write(env, csrno, new_value, ra); 5627 if (ret != RISCV_EXCP_NONE) { 5628 return ret; 5629 } 5630 } 5631 } 5632 5633 /* return old value */ 5634 if (ret_value) { 5635 *ret_value = old_value; 5636 } 5637 5638 return RISCV_EXCP_NONE; 5639 } 5640 5641 RISCVException riscv_csrr(CPURISCVState *env, int csrno, 5642 target_ulong *ret_value) 5643 { 5644 RISCVException ret = riscv_csrrw_check(env, csrno, false); 5645 if (ret != RISCV_EXCP_NONE) { 5646 return ret; 5647 } 5648 5649 return riscv_csrrw_do64(env, csrno, ret_value, 0, 0, 0); 5650 } 5651 5652 RISCVException riscv_csrrw(CPURISCVState *env, int csrno, 5653 target_ulong *ret_value, target_ulong new_value, 5654 target_ulong write_mask, uintptr_t ra) 5655 { 5656 RISCVException ret = riscv_csrrw_check(env, csrno, true); 5657 if (ret != RISCV_EXCP_NONE) { 5658 return ret; 5659 } 5660 5661 return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask, ra); 5662 } 5663 5664 static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno, 5665 Int128 *ret_value, 5666 Int128 new_value, 5667 Int128 write_mask, uintptr_t ra) 5668 { 5669 RISCVException ret; 5670 Int128 old_value; 5671 5672 /* read old value */ 5673 ret = csr_ops[csrno].read128(env, csrno, &old_value); 5674 if (ret != RISCV_EXCP_NONE) { 5675 return ret; 5676 } 5677 5678 /* write value if writable and write mask set, otherwise drop writes */ 5679 if (int128_nz(write_mask)) { 5680 new_value = int128_or(int128_and(old_value, int128_not(write_mask)), 5681 int128_and(new_value, write_mask)); 5682 if (csr_ops[csrno].write128) { 5683 ret = csr_ops[csrno].write128(env, csrno, new_value); 5684 if (ret != RISCV_EXCP_NONE) { 5685 return ret; 5686 } 5687 } else if (csr_ops[csrno].write) { 5688 /* avoids having to write wrappers for all registers */ 5689 ret = csr_ops[csrno].write(env, csrno, int128_getlo(new_value), ra); 5690 if (ret != RISCV_EXCP_NONE) { 5691 return ret; 5692 } 5693 } 5694 } 5695 5696 /* return old value */ 5697 if (ret_value) { 5698 *ret_value = old_value; 5699 } 5700 5701 return RISCV_EXCP_NONE; 5702 } 5703 5704 RISCVException riscv_csrr_i128(CPURISCVState *env, int csrno, 5705 Int128 *ret_value) 5706 { 5707 RISCVException ret; 5708 5709 ret = riscv_csrrw_check(env, csrno, false); 5710 if (ret != RISCV_EXCP_NONE) { 5711 return ret; 5712 } 5713 5714 if (csr_ops[csrno].read128) { 5715 return riscv_csrrw_do128(env, csrno, ret_value, 5716 int128_zero(), int128_zero(), 0); 5717 } 5718 5719 /* 5720 * Fall back to 64-bit version for now, if the 128-bit alternative isn't 5721 * at all defined. 5722 * Note, some CSRs don't need to extend to MXLEN (64 upper bits non 5723 * significant), for those, this fallback is correctly handling the 5724 * accesses 5725 */ 5726 target_ulong old_value; 5727 ret = riscv_csrrw_do64(env, csrno, &old_value, 0, 0, 0); 5728 if (ret == RISCV_EXCP_NONE && ret_value) { 5729 *ret_value = int128_make64(old_value); 5730 } 5731 return ret; 5732 } 5733 5734 RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno, 5735 Int128 *ret_value, Int128 new_value, 5736 Int128 write_mask, uintptr_t ra) 5737 { 5738 RISCVException ret; 5739 5740 ret = riscv_csrrw_check(env, csrno, true); 5741 if (ret != RISCV_EXCP_NONE) { 5742 return ret; 5743 } 5744 5745 if (csr_ops[csrno].read128) { 5746 return riscv_csrrw_do128(env, csrno, ret_value, 5747 new_value, write_mask, ra); 5748 } 5749 5750 /* 5751 * Fall back to 64-bit version for now, if the 128-bit alternative isn't 5752 * at all defined. 5753 * Note, some CSRs don't need to extend to MXLEN (64 upper bits non 5754 * significant), for those, this fallback is correctly handling the 5755 * accesses 5756 */ 5757 target_ulong old_value; 5758 ret = riscv_csrrw_do64(env, csrno, &old_value, 5759 int128_getlo(new_value), 5760 int128_getlo(write_mask), ra); 5761 if (ret == RISCV_EXCP_NONE && ret_value) { 5762 *ret_value = int128_make64(old_value); 5763 } 5764 return ret; 5765 } 5766 5767 /* 5768 * Debugger support. If not in user mode, set env->debugger before the 5769 * riscv_csrrw call and clear it after the call. 5770 */ 5771 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno, 5772 target_ulong *ret_value, 5773 target_ulong new_value, 5774 target_ulong write_mask) 5775 { 5776 RISCVException ret; 5777 #if !defined(CONFIG_USER_ONLY) 5778 env->debugger = true; 5779 #endif 5780 if (!write_mask) { 5781 ret = riscv_csrr(env, csrno, ret_value); 5782 } else { 5783 ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask, 0); 5784 } 5785 #if !defined(CONFIG_USER_ONLY) 5786 env->debugger = false; 5787 #endif 5788 return ret; 5789 } 5790 5791 static RISCVException read_jvt(CPURISCVState *env, int csrno, 5792 target_ulong *val) 5793 { 5794 *val = env->jvt; 5795 return RISCV_EXCP_NONE; 5796 } 5797 5798 static RISCVException write_jvt(CPURISCVState *env, int csrno, 5799 target_ulong val, uintptr_t ra) 5800 { 5801 env->jvt = val; 5802 return RISCV_EXCP_NONE; 5803 } 5804 5805 /* 5806 * Control and Status Register function table 5807 * riscv_csr_operations::predicate() must be provided for an implemented CSR 5808 */ 5809 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { 5810 /* User Floating-Point CSRs */ 5811 [CSR_FFLAGS] = { "fflags", fs, read_fflags, write_fflags }, 5812 [CSR_FRM] = { "frm", fs, read_frm, write_frm }, 5813 [CSR_FCSR] = { "fcsr", fs, read_fcsr, write_fcsr }, 5814 /* Vector CSRs */ 5815 [CSR_VSTART] = { "vstart", vs, read_vstart, write_vstart }, 5816 [CSR_VXSAT] = { "vxsat", vs, read_vxsat, write_vxsat }, 5817 [CSR_VXRM] = { "vxrm", vs, read_vxrm, write_vxrm }, 5818 [CSR_VCSR] = { "vcsr", vs, read_vcsr, write_vcsr }, 5819 [CSR_VL] = { "vl", vs, read_vl }, 5820 [CSR_VTYPE] = { "vtype", vs, read_vtype }, 5821 [CSR_VLENB] = { "vlenb", vs, read_vlenb }, 5822 /* User Timers and Counters */ 5823 [CSR_CYCLE] = { "cycle", ctr, read_hpmcounter }, 5824 [CSR_INSTRET] = { "instret", ctr, read_hpmcounter }, 5825 [CSR_CYCLEH] = { "cycleh", ctr32, read_hpmcounterh }, 5826 [CSR_INSTRETH] = { "instreth", ctr32, read_hpmcounterh }, 5827 5828 /* 5829 * In privileged mode, the monitor will have to emulate TIME CSRs only if 5830 * rdtime callback is not provided by machine/platform emulation. 5831 */ 5832 [CSR_TIME] = { "time", ctr, read_time }, 5833 [CSR_TIMEH] = { "timeh", ctr32, read_timeh }, 5834 5835 /* Crypto Extension */ 5836 [CSR_SEED] = { "seed", seed, NULL, NULL, rmw_seed }, 5837 5838 /* Zcmt Extension */ 5839 [CSR_JVT] = {"jvt", zcmt, read_jvt, write_jvt}, 5840 5841 /* zicfiss Extension, shadow stack register */ 5842 [CSR_SSP] = { "ssp", cfi_ss, read_ssp, write_ssp }, 5843 5844 #if !defined(CONFIG_USER_ONLY) 5845 /* Machine Timers and Counters */ 5846 [CSR_MCYCLE] = { "mcycle", any, read_hpmcounter, 5847 write_mhpmcounter }, 5848 [CSR_MINSTRET] = { "minstret", any, read_hpmcounter, 5849 write_mhpmcounter }, 5850 [CSR_MCYCLEH] = { "mcycleh", any32, read_hpmcounterh, 5851 write_mhpmcounterh }, 5852 [CSR_MINSTRETH] = { "minstreth", any32, read_hpmcounterh, 5853 write_mhpmcounterh }, 5854 5855 /* Machine Information Registers */ 5856 [CSR_MVENDORID] = { "mvendorid", any, read_mvendorid }, 5857 [CSR_MARCHID] = { "marchid", any, read_marchid }, 5858 [CSR_MIMPID] = { "mimpid", any, read_mimpid }, 5859 [CSR_MHARTID] = { "mhartid", any, read_mhartid }, 5860 5861 [CSR_MCONFIGPTR] = { "mconfigptr", any, read_zero, 5862 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5863 /* Machine Trap Setup */ 5864 [CSR_MSTATUS] = { "mstatus", any, read_mstatus, write_mstatus, 5865 NULL, read_mstatus_i128 }, 5866 [CSR_MISA] = { "misa", any, read_misa, write_misa, 5867 NULL, read_misa_i128 }, 5868 [CSR_MIDELEG] = { "mideleg", smode, NULL, NULL, rmw_mideleg }, 5869 [CSR_MEDELEG] = { "medeleg", smode, read_medeleg, write_medeleg }, 5870 [CSR_MIE] = { "mie", any, NULL, NULL, rmw_mie }, 5871 [CSR_MTVEC] = { "mtvec", any, read_mtvec, write_mtvec }, 5872 [CSR_MCOUNTEREN] = { "mcounteren", umode, read_mcounteren, 5873 write_mcounteren }, 5874 5875 [CSR_MSTATUSH] = { "mstatush", any32, read_mstatush, 5876 write_mstatush }, 5877 [CSR_MEDELEGH] = { "medelegh", smode32, read_zero, write_ignore, 5878 .min_priv_ver = PRIV_VERSION_1_13_0 }, 5879 [CSR_HEDELEGH] = { "hedelegh", hmode32, read_hedelegh, write_hedelegh, 5880 .min_priv_ver = PRIV_VERSION_1_13_0 }, 5881 5882 /* Machine Trap Handling */ 5883 [CSR_MSCRATCH] = { "mscratch", any, read_mscratch, write_mscratch, 5884 NULL, read_mscratch_i128, write_mscratch_i128 }, 5885 [CSR_MEPC] = { "mepc", any, read_mepc, write_mepc }, 5886 [CSR_MCAUSE] = { "mcause", any, read_mcause, write_mcause }, 5887 [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, 5888 [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, 5889 5890 /* Machine-Level Window to Indirectly Accessed Registers (AIA) */ 5891 [CSR_MISELECT] = { "miselect", csrind_or_aia_any, NULL, NULL, 5892 rmw_xiselect }, 5893 [CSR_MIREG] = { "mireg", csrind_or_aia_any, NULL, NULL, 5894 rmw_xireg }, 5895 5896 /* Machine Indirect Register Alias */ 5897 [CSR_MIREG2] = { "mireg2", csrind_any, NULL, NULL, rmw_xiregi, 5898 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5899 [CSR_MIREG3] = { "mireg3", csrind_any, NULL, NULL, rmw_xiregi, 5900 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5901 [CSR_MIREG4] = { "mireg4", csrind_any, NULL, NULL, rmw_xiregi, 5902 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5903 [CSR_MIREG5] = { "mireg5", csrind_any, NULL, NULL, rmw_xiregi, 5904 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5905 [CSR_MIREG6] = { "mireg6", csrind_any, NULL, NULL, rmw_xiregi, 5906 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5907 5908 /* Machine-Level Interrupts (AIA) */ 5909 [CSR_MTOPEI] = { "mtopei", aia_any, NULL, NULL, rmw_xtopei }, 5910 [CSR_MTOPI] = { "mtopi", aia_any, read_mtopi }, 5911 5912 /* Virtual Interrupts for Supervisor Level (AIA) */ 5913 [CSR_MVIEN] = { "mvien", aia_any, NULL, NULL, rmw_mvien }, 5914 [CSR_MVIP] = { "mvip", aia_any, NULL, NULL, rmw_mvip }, 5915 5916 /* Machine-Level High-Half CSRs (AIA) */ 5917 [CSR_MIDELEGH] = { "midelegh", aia_smode32, NULL, NULL, rmw_midelegh }, 5918 [CSR_MIEH] = { "mieh", aia_any32, NULL, NULL, rmw_mieh }, 5919 [CSR_MVIENH] = { "mvienh", aia_any32, NULL, NULL, rmw_mvienh }, 5920 [CSR_MVIPH] = { "mviph", aia_any32, NULL, NULL, rmw_mviph }, 5921 [CSR_MIPH] = { "miph", aia_any32, NULL, NULL, rmw_miph }, 5922 5923 /* Execution environment configuration */ 5924 [CSR_MENVCFG] = { "menvcfg", umode, read_menvcfg, write_menvcfg, 5925 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5926 [CSR_MENVCFGH] = { "menvcfgh", umode32, read_menvcfgh, write_menvcfgh, 5927 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5928 [CSR_SENVCFG] = { "senvcfg", smode, read_senvcfg, write_senvcfg, 5929 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5930 [CSR_HENVCFG] = { "henvcfg", hmode, read_henvcfg, write_henvcfg, 5931 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5932 [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh, write_henvcfgh, 5933 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5934 5935 /* Smstateen extension CSRs */ 5936 [CSR_MSTATEEN0] = { "mstateen0", mstateen, read_mstateen, write_mstateen0, 5937 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5938 [CSR_MSTATEEN0H] = { "mstateen0h", mstateen, read_mstateenh, 5939 write_mstateen0h, 5940 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5941 [CSR_MSTATEEN1] = { "mstateen1", mstateen, read_mstateen, 5942 write_mstateen_1_3, 5943 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5944 [CSR_MSTATEEN1H] = { "mstateen1h", mstateen, read_mstateenh, 5945 write_mstateenh_1_3, 5946 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5947 [CSR_MSTATEEN2] = { "mstateen2", mstateen, read_mstateen, 5948 write_mstateen_1_3, 5949 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5950 [CSR_MSTATEEN2H] = { "mstateen2h", mstateen, read_mstateenh, 5951 write_mstateenh_1_3, 5952 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5953 [CSR_MSTATEEN3] = { "mstateen3", mstateen, read_mstateen, 5954 write_mstateen_1_3, 5955 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5956 [CSR_MSTATEEN3H] = { "mstateen3h", mstateen, read_mstateenh, 5957 write_mstateenh_1_3, 5958 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5959 [CSR_HSTATEEN0] = { "hstateen0", hstateen, read_hstateen, write_hstateen0, 5960 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5961 [CSR_HSTATEEN0H] = { "hstateen0h", hstateenh, read_hstateenh, 5962 write_hstateen0h, 5963 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5964 [CSR_HSTATEEN1] = { "hstateen1", hstateen, read_hstateen, 5965 write_hstateen_1_3, 5966 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5967 [CSR_HSTATEEN1H] = { "hstateen1h", hstateenh, read_hstateenh, 5968 write_hstateenh_1_3, 5969 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5970 [CSR_HSTATEEN2] = { "hstateen2", hstateen, read_hstateen, 5971 write_hstateen_1_3, 5972 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5973 [CSR_HSTATEEN2H] = { "hstateen2h", hstateenh, read_hstateenh, 5974 write_hstateenh_1_3, 5975 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5976 [CSR_HSTATEEN3] = { "hstateen3", hstateen, read_hstateen, 5977 write_hstateen_1_3, 5978 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5979 [CSR_HSTATEEN3H] = { "hstateen3h", hstateenh, read_hstateenh, 5980 write_hstateenh_1_3, 5981 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5982 [CSR_SSTATEEN0] = { "sstateen0", sstateen, read_sstateen, write_sstateen0, 5983 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5984 [CSR_SSTATEEN1] = { "sstateen1", sstateen, read_sstateen, 5985 write_sstateen_1_3, 5986 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5987 [CSR_SSTATEEN2] = { "sstateen2", sstateen, read_sstateen, 5988 write_sstateen_1_3, 5989 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5990 [CSR_SSTATEEN3] = { "sstateen3", sstateen, read_sstateen, 5991 write_sstateen_1_3, 5992 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5993 5994 /* RNMI */ 5995 [CSR_MNSCRATCH] = { "mnscratch", rnmi, read_mnscratch, write_mnscratch, 5996 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5997 [CSR_MNEPC] = { "mnepc", rnmi, read_mnepc, write_mnepc, 5998 .min_priv_ver = PRIV_VERSION_1_12_0 }, 5999 [CSR_MNCAUSE] = { "mncause", rnmi, read_mncause, write_mncause, 6000 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6001 [CSR_MNSTATUS] = { "mnstatus", rnmi, read_mnstatus, write_mnstatus, 6002 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6003 6004 /* Supervisor Counter Delegation */ 6005 [CSR_SCOUNTINHIBIT] = {"scountinhibit", scountinhibit_pred, 6006 read_scountinhibit, write_scountinhibit, 6007 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6008 6009 /* Supervisor Trap Setup */ 6010 [CSR_SSTATUS] = { "sstatus", smode, read_sstatus, write_sstatus, 6011 NULL, read_sstatus_i128 }, 6012 [CSR_SIE] = { "sie", smode, NULL, NULL, rmw_sie }, 6013 [CSR_STVEC] = { "stvec", smode, read_stvec, write_stvec }, 6014 [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren, 6015 write_scounteren }, 6016 6017 /* Supervisor Trap Handling */ 6018 [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch, 6019 NULL, read_sscratch_i128, write_sscratch_i128 }, 6020 [CSR_SEPC] = { "sepc", smode, read_sepc, write_sepc }, 6021 [CSR_SCAUSE] = { "scause", smode, read_scause, write_scause }, 6022 [CSR_STVAL] = { "stval", smode, read_stval, write_stval }, 6023 [CSR_SIP] = { "sip", smode, NULL, NULL, rmw_sip }, 6024 [CSR_STIMECMP] = { "stimecmp", sstc, read_stimecmp, write_stimecmp, 6025 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6026 [CSR_STIMECMPH] = { "stimecmph", sstc_32, read_stimecmph, write_stimecmph, 6027 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6028 [CSR_VSTIMECMP] = { "vstimecmp", sstc, read_vstimecmp, 6029 write_vstimecmp, 6030 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6031 [CSR_VSTIMECMPH] = { "vstimecmph", sstc_32, read_vstimecmph, 6032 write_vstimecmph, 6033 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6034 6035 /* Supervisor Protection and Translation */ 6036 [CSR_SATP] = { "satp", satp, read_satp, write_satp }, 6037 6038 /* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */ 6039 [CSR_SISELECT] = { "siselect", csrind_or_aia_smode, NULL, NULL, 6040 rmw_xiselect }, 6041 [CSR_SIREG] = { "sireg", csrind_or_aia_smode, NULL, NULL, 6042 rmw_xireg }, 6043 6044 /* Supervisor Indirect Register Alias */ 6045 [CSR_SIREG2] = { "sireg2", csrind_smode, NULL, NULL, rmw_xiregi, 6046 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6047 [CSR_SIREG3] = { "sireg3", csrind_smode, NULL, NULL, rmw_xiregi, 6048 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6049 [CSR_SIREG4] = { "sireg4", csrind_smode, NULL, NULL, rmw_xiregi, 6050 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6051 [CSR_SIREG5] = { "sireg5", csrind_smode, NULL, NULL, rmw_xiregi, 6052 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6053 [CSR_SIREG6] = { "sireg6", csrind_smode, NULL, NULL, rmw_xiregi, 6054 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6055 6056 /* Supervisor-Level Interrupts (AIA) */ 6057 [CSR_STOPEI] = { "stopei", aia_smode, NULL, NULL, rmw_xtopei }, 6058 [CSR_STOPI] = { "stopi", aia_smode, read_stopi }, 6059 6060 /* Supervisor-Level High-Half CSRs (AIA) */ 6061 [CSR_SIEH] = { "sieh", aia_smode32, NULL, NULL, rmw_sieh }, 6062 [CSR_SIPH] = { "siph", aia_smode32, NULL, NULL, rmw_siph }, 6063 6064 [CSR_HSTATUS] = { "hstatus", hmode, read_hstatus, write_hstatus, 6065 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6066 [CSR_HEDELEG] = { "hedeleg", hmode, read_hedeleg, write_hedeleg, 6067 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6068 [CSR_HIDELEG] = { "hideleg", hmode, NULL, NULL, rmw_hideleg, 6069 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6070 [CSR_HVIP] = { "hvip", hmode, NULL, NULL, rmw_hvip, 6071 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6072 [CSR_HIP] = { "hip", hmode, NULL, NULL, rmw_hip, 6073 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6074 [CSR_HIE] = { "hie", hmode, NULL, NULL, rmw_hie, 6075 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6076 [CSR_HCOUNTEREN] = { "hcounteren", hmode, read_hcounteren, 6077 write_hcounteren, 6078 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6079 [CSR_HGEIE] = { "hgeie", hmode, read_hgeie, write_hgeie, 6080 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6081 [CSR_HTVAL] = { "htval", hmode, read_htval, write_htval, 6082 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6083 [CSR_HTINST] = { "htinst", hmode, read_htinst, write_htinst, 6084 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6085 [CSR_HGEIP] = { "hgeip", hmode, read_hgeip, 6086 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6087 [CSR_HGATP] = { "hgatp", hgatp, read_hgatp, write_hgatp, 6088 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6089 [CSR_HTIMEDELTA] = { "htimedelta", hmode, read_htimedelta, 6090 write_htimedelta, 6091 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6092 [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah, 6093 write_htimedeltah, 6094 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6095 6096 [CSR_VSSTATUS] = { "vsstatus", hmode, read_vsstatus, 6097 write_vsstatus, 6098 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6099 [CSR_VSIP] = { "vsip", hmode, NULL, NULL, rmw_vsip, 6100 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6101 [CSR_VSIE] = { "vsie", hmode, NULL, NULL, rmw_vsie , 6102 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6103 [CSR_VSTVEC] = { "vstvec", hmode, read_vstvec, write_vstvec, 6104 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6105 [CSR_VSSCRATCH] = { "vsscratch", hmode, read_vsscratch, 6106 write_vsscratch, 6107 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6108 [CSR_VSEPC] = { "vsepc", hmode, read_vsepc, write_vsepc, 6109 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6110 [CSR_VSCAUSE] = { "vscause", hmode, read_vscause, write_vscause, 6111 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6112 [CSR_VSTVAL] = { "vstval", hmode, read_vstval, write_vstval, 6113 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6114 [CSR_VSATP] = { "vsatp", hmode, read_vsatp, write_vsatp, 6115 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6116 6117 [CSR_MTVAL2] = { "mtval2", dbltrp_hmode, read_mtval2, write_mtval2, 6118 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6119 [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst, 6120 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6121 6122 /* Virtual Interrupts and Interrupt Priorities (H-extension with AIA) */ 6123 [CSR_HVIEN] = { "hvien", aia_hmode, NULL, NULL, rmw_hvien }, 6124 [CSR_HVICTL] = { "hvictl", aia_hmode, read_hvictl, 6125 write_hvictl }, 6126 [CSR_HVIPRIO1] = { "hviprio1", aia_hmode, read_hviprio1, 6127 write_hviprio1 }, 6128 [CSR_HVIPRIO2] = { "hviprio2", aia_hmode, read_hviprio2, 6129 write_hviprio2 }, 6130 /* 6131 * VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) 6132 */ 6133 [CSR_VSISELECT] = { "vsiselect", csrind_or_aia_hmode, NULL, NULL, 6134 rmw_xiselect }, 6135 [CSR_VSIREG] = { "vsireg", csrind_or_aia_hmode, NULL, NULL, 6136 rmw_xireg }, 6137 6138 /* Virtual Supervisor Indirect Alias */ 6139 [CSR_VSIREG2] = { "vsireg2", csrind_hmode, NULL, NULL, rmw_xiregi, 6140 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6141 [CSR_VSIREG3] = { "vsireg3", csrind_hmode, NULL, NULL, rmw_xiregi, 6142 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6143 [CSR_VSIREG4] = { "vsireg4", csrind_hmode, NULL, NULL, rmw_xiregi, 6144 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6145 [CSR_VSIREG5] = { "vsireg5", csrind_hmode, NULL, NULL, rmw_xiregi, 6146 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6147 [CSR_VSIREG6] = { "vsireg6", csrind_hmode, NULL, NULL, rmw_xiregi, 6148 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6149 6150 /* VS-Level Interrupts (H-extension with AIA) */ 6151 [CSR_VSTOPEI] = { "vstopei", aia_hmode, NULL, NULL, rmw_xtopei }, 6152 [CSR_VSTOPI] = { "vstopi", aia_hmode, read_vstopi }, 6153 6154 /* Hypervisor and VS-Level High-Half CSRs (H-extension with AIA) */ 6155 [CSR_HIDELEGH] = { "hidelegh", aia_hmode32, NULL, NULL, 6156 rmw_hidelegh }, 6157 [CSR_HVIENH] = { "hvienh", aia_hmode32, NULL, NULL, rmw_hvienh }, 6158 [CSR_HVIPH] = { "hviph", aia_hmode32, NULL, NULL, rmw_hviph }, 6159 [CSR_HVIPRIO1H] = { "hviprio1h", aia_hmode32, read_hviprio1h, 6160 write_hviprio1h }, 6161 [CSR_HVIPRIO2H] = { "hviprio2h", aia_hmode32, read_hviprio2h, 6162 write_hviprio2h }, 6163 [CSR_VSIEH] = { "vsieh", aia_hmode32, NULL, NULL, rmw_vsieh }, 6164 [CSR_VSIPH] = { "vsiph", aia_hmode32, NULL, NULL, rmw_vsiph }, 6165 6166 /* Physical Memory Protection */ 6167 [CSR_MSECCFG] = { "mseccfg", have_mseccfg, read_mseccfg, write_mseccfg, 6168 .min_priv_ver = PRIV_VERSION_1_11_0 }, 6169 [CSR_PMPCFG0] = { "pmpcfg0", pmp, read_pmpcfg, write_pmpcfg }, 6170 [CSR_PMPCFG1] = { "pmpcfg1", pmp, read_pmpcfg, write_pmpcfg }, 6171 [CSR_PMPCFG2] = { "pmpcfg2", pmp, read_pmpcfg, write_pmpcfg }, 6172 [CSR_PMPCFG3] = { "pmpcfg3", pmp, read_pmpcfg, write_pmpcfg }, 6173 [CSR_PMPCFG4] = { "pmpcfg4", pmp, read_pmpcfg, write_pmpcfg, 6174 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6175 [CSR_PMPCFG5] = { "pmpcfg5", pmp, read_pmpcfg, write_pmpcfg, 6176 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6177 [CSR_PMPCFG6] = { "pmpcfg6", pmp, read_pmpcfg, write_pmpcfg, 6178 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6179 [CSR_PMPCFG7] = { "pmpcfg7", pmp, read_pmpcfg, write_pmpcfg, 6180 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6181 [CSR_PMPCFG8] = { "pmpcfg8", pmp, read_pmpcfg, write_pmpcfg, 6182 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6183 [CSR_PMPCFG9] = { "pmpcfg9", pmp, read_pmpcfg, write_pmpcfg, 6184 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6185 [CSR_PMPCFG10] = { "pmpcfg10", pmp, read_pmpcfg, write_pmpcfg, 6186 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6187 [CSR_PMPCFG11] = { "pmpcfg11", pmp, read_pmpcfg, write_pmpcfg, 6188 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6189 [CSR_PMPCFG12] = { "pmpcfg12", pmp, read_pmpcfg, write_pmpcfg, 6190 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6191 [CSR_PMPCFG13] = { "pmpcfg13", pmp, read_pmpcfg, write_pmpcfg, 6192 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6193 [CSR_PMPCFG14] = { "pmpcfg14", pmp, read_pmpcfg, write_pmpcfg, 6194 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6195 [CSR_PMPCFG15] = { "pmpcfg15", pmp, read_pmpcfg, write_pmpcfg, 6196 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6197 [CSR_PMPADDR0] = { "pmpaddr0", pmp, read_pmpaddr, write_pmpaddr }, 6198 [CSR_PMPADDR1] = { "pmpaddr1", pmp, read_pmpaddr, write_pmpaddr }, 6199 [CSR_PMPADDR2] = { "pmpaddr2", pmp, read_pmpaddr, write_pmpaddr }, 6200 [CSR_PMPADDR3] = { "pmpaddr3", pmp, read_pmpaddr, write_pmpaddr }, 6201 [CSR_PMPADDR4] = { "pmpaddr4", pmp, read_pmpaddr, write_pmpaddr }, 6202 [CSR_PMPADDR5] = { "pmpaddr5", pmp, read_pmpaddr, write_pmpaddr }, 6203 [CSR_PMPADDR6] = { "pmpaddr6", pmp, read_pmpaddr, write_pmpaddr }, 6204 [CSR_PMPADDR7] = { "pmpaddr7", pmp, read_pmpaddr, write_pmpaddr }, 6205 [CSR_PMPADDR8] = { "pmpaddr8", pmp, read_pmpaddr, write_pmpaddr }, 6206 [CSR_PMPADDR9] = { "pmpaddr9", pmp, read_pmpaddr, write_pmpaddr }, 6207 [CSR_PMPADDR10] = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr }, 6208 [CSR_PMPADDR11] = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr }, 6209 [CSR_PMPADDR12] = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr }, 6210 [CSR_PMPADDR13] = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr }, 6211 [CSR_PMPADDR14] = { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr }, 6212 [CSR_PMPADDR15] = { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr }, 6213 [CSR_PMPADDR16] = { "pmpaddr16", pmp, read_pmpaddr, write_pmpaddr, 6214 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6215 [CSR_PMPADDR17] = { "pmpaddr17", pmp, read_pmpaddr, write_pmpaddr, 6216 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6217 [CSR_PMPADDR18] = { "pmpaddr18", pmp, read_pmpaddr, write_pmpaddr, 6218 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6219 [CSR_PMPADDR19] = { "pmpaddr19", pmp, read_pmpaddr, write_pmpaddr, 6220 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6221 [CSR_PMPADDR20] = { "pmpaddr20", pmp, read_pmpaddr, write_pmpaddr, 6222 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6223 [CSR_PMPADDR21] = { "pmpaddr21", pmp, read_pmpaddr, write_pmpaddr, 6224 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6225 [CSR_PMPADDR22] = { "pmpaddr22", pmp, read_pmpaddr, write_pmpaddr, 6226 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6227 [CSR_PMPADDR23] = { "pmpaddr23", pmp, read_pmpaddr, write_pmpaddr, 6228 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6229 [CSR_PMPADDR24] = { "pmpaddr24", pmp, read_pmpaddr, write_pmpaddr, 6230 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6231 [CSR_PMPADDR25] = { "pmpaddr25", pmp, read_pmpaddr, write_pmpaddr, 6232 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6233 [CSR_PMPADDR26] = { "pmpaddr26", pmp, read_pmpaddr, write_pmpaddr, 6234 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6235 [CSR_PMPADDR27] = { "pmpaddr27", pmp, read_pmpaddr, write_pmpaddr, 6236 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6237 [CSR_PMPADDR28] = { "pmpaddr28", pmp, read_pmpaddr, write_pmpaddr, 6238 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6239 [CSR_PMPADDR29] = { "pmpaddr29", pmp, read_pmpaddr, write_pmpaddr, 6240 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6241 [CSR_PMPADDR30] = { "pmpaddr30", pmp, read_pmpaddr, write_pmpaddr, 6242 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6243 [CSR_PMPADDR31] = { "pmpaddr31", pmp, read_pmpaddr, write_pmpaddr, 6244 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6245 [CSR_PMPADDR32] = { "pmpaddr32", pmp, read_pmpaddr, write_pmpaddr, 6246 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6247 [CSR_PMPADDR33] = { "pmpaddr33", pmp, read_pmpaddr, write_pmpaddr, 6248 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6249 [CSR_PMPADDR34] = { "pmpaddr34", pmp, read_pmpaddr, write_pmpaddr, 6250 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6251 [CSR_PMPADDR35] = { "pmpaddr35", pmp, read_pmpaddr, write_pmpaddr, 6252 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6253 [CSR_PMPADDR36] = { "pmpaddr36", pmp, read_pmpaddr, write_pmpaddr, 6254 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6255 [CSR_PMPADDR37] = { "pmpaddr37", pmp, read_pmpaddr, write_pmpaddr, 6256 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6257 [CSR_PMPADDR38] = { "pmpaddr38", pmp, read_pmpaddr, write_pmpaddr, 6258 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6259 [CSR_PMPADDR39] = { "pmpaddr39", pmp, read_pmpaddr, write_pmpaddr, 6260 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6261 [CSR_PMPADDR40] = { "pmpaddr40", pmp, read_pmpaddr, write_pmpaddr, 6262 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6263 [CSR_PMPADDR41] = { "pmpaddr41", pmp, read_pmpaddr, write_pmpaddr, 6264 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6265 [CSR_PMPADDR42] = { "pmpaddr42", pmp, read_pmpaddr, write_pmpaddr, 6266 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6267 [CSR_PMPADDR43] = { "pmpaddr43", pmp, read_pmpaddr, write_pmpaddr, 6268 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6269 [CSR_PMPADDR44] = { "pmpaddr44", pmp, read_pmpaddr, write_pmpaddr, 6270 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6271 [CSR_PMPADDR45] = { "pmpaddr45", pmp, read_pmpaddr, write_pmpaddr, 6272 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6273 [CSR_PMPADDR46] = { "pmpaddr46", pmp, read_pmpaddr, write_pmpaddr, 6274 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6275 [CSR_PMPADDR47] = { "pmpaddr47", pmp, read_pmpaddr, write_pmpaddr, 6276 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6277 [CSR_PMPADDR48] = { "pmpaddr48", pmp, read_pmpaddr, write_pmpaddr, 6278 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6279 [CSR_PMPADDR49] = { "pmpaddr49", pmp, read_pmpaddr, write_pmpaddr, 6280 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6281 [CSR_PMPADDR50] = { "pmpaddr50", pmp, read_pmpaddr, write_pmpaddr, 6282 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6283 [CSR_PMPADDR51] = { "pmpaddr51", pmp, read_pmpaddr, write_pmpaddr, 6284 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6285 [CSR_PMPADDR52] = { "pmpaddr52", pmp, read_pmpaddr, write_pmpaddr, 6286 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6287 [CSR_PMPADDR53] = { "pmpaddr53", pmp, read_pmpaddr, write_pmpaddr, 6288 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6289 [CSR_PMPADDR54] = { "pmpaddr54", pmp, read_pmpaddr, write_pmpaddr, 6290 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6291 [CSR_PMPADDR55] = { "pmpaddr55", pmp, read_pmpaddr, write_pmpaddr, 6292 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6293 [CSR_PMPADDR56] = { "pmpaddr56", pmp, read_pmpaddr, write_pmpaddr, 6294 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6295 [CSR_PMPADDR57] = { "pmpaddr57", pmp, read_pmpaddr, write_pmpaddr, 6296 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6297 [CSR_PMPADDR58] = { "pmpaddr58", pmp, read_pmpaddr, write_pmpaddr, 6298 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6299 [CSR_PMPADDR59] = { "pmpaddr59", pmp, read_pmpaddr, write_pmpaddr, 6300 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6301 [CSR_PMPADDR60] = { "pmpaddr60", pmp, read_pmpaddr, write_pmpaddr, 6302 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6303 [CSR_PMPADDR61] = { "pmpaddr61", pmp, read_pmpaddr, write_pmpaddr, 6304 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6305 [CSR_PMPADDR62] = { "pmpaddr62", pmp, read_pmpaddr, write_pmpaddr, 6306 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6307 [CSR_PMPADDR63] = { "pmpaddr63", pmp, read_pmpaddr, write_pmpaddr, 6308 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6309 6310 /* Debug CSRs */ 6311 [CSR_TSELECT] = { "tselect", debug, read_tselect, write_tselect }, 6312 [CSR_TDATA1] = { "tdata1", debug, read_tdata, write_tdata }, 6313 [CSR_TDATA2] = { "tdata2", debug, read_tdata, write_tdata }, 6314 [CSR_TDATA3] = { "tdata3", debug, read_tdata, write_tdata }, 6315 [CSR_TINFO] = { "tinfo", debug, read_tinfo, write_ignore }, 6316 [CSR_MCONTEXT] = { "mcontext", debug, read_mcontext, write_mcontext }, 6317 6318 [CSR_MCTRCTL] = { "mctrctl", ctr_mmode, NULL, NULL, rmw_xctrctl }, 6319 [CSR_SCTRCTL] = { "sctrctl", ctr_smode, NULL, NULL, rmw_xctrctl }, 6320 [CSR_VSCTRCTL] = { "vsctrctl", ctr_smode, NULL, NULL, rmw_xctrctl }, 6321 [CSR_SCTRDEPTH] = { "sctrdepth", ctr_smode, NULL, NULL, rmw_sctrdepth }, 6322 [CSR_SCTRSTATUS] = { "sctrstatus", ctr_smode, NULL, NULL, rmw_sctrstatus }, 6323 6324 /* Performance Counters */ 6325 [CSR_HPMCOUNTER3] = { "hpmcounter3", ctr, read_hpmcounter }, 6326 [CSR_HPMCOUNTER4] = { "hpmcounter4", ctr, read_hpmcounter }, 6327 [CSR_HPMCOUNTER5] = { "hpmcounter5", ctr, read_hpmcounter }, 6328 [CSR_HPMCOUNTER6] = { "hpmcounter6", ctr, read_hpmcounter }, 6329 [CSR_HPMCOUNTER7] = { "hpmcounter7", ctr, read_hpmcounter }, 6330 [CSR_HPMCOUNTER8] = { "hpmcounter8", ctr, read_hpmcounter }, 6331 [CSR_HPMCOUNTER9] = { "hpmcounter9", ctr, read_hpmcounter }, 6332 [CSR_HPMCOUNTER10] = { "hpmcounter10", ctr, read_hpmcounter }, 6333 [CSR_HPMCOUNTER11] = { "hpmcounter11", ctr, read_hpmcounter }, 6334 [CSR_HPMCOUNTER12] = { "hpmcounter12", ctr, read_hpmcounter }, 6335 [CSR_HPMCOUNTER13] = { "hpmcounter13", ctr, read_hpmcounter }, 6336 [CSR_HPMCOUNTER14] = { "hpmcounter14", ctr, read_hpmcounter }, 6337 [CSR_HPMCOUNTER15] = { "hpmcounter15", ctr, read_hpmcounter }, 6338 [CSR_HPMCOUNTER16] = { "hpmcounter16", ctr, read_hpmcounter }, 6339 [CSR_HPMCOUNTER17] = { "hpmcounter17", ctr, read_hpmcounter }, 6340 [CSR_HPMCOUNTER18] = { "hpmcounter18", ctr, read_hpmcounter }, 6341 [CSR_HPMCOUNTER19] = { "hpmcounter19", ctr, read_hpmcounter }, 6342 [CSR_HPMCOUNTER20] = { "hpmcounter20", ctr, read_hpmcounter }, 6343 [CSR_HPMCOUNTER21] = { "hpmcounter21", ctr, read_hpmcounter }, 6344 [CSR_HPMCOUNTER22] = { "hpmcounter22", ctr, read_hpmcounter }, 6345 [CSR_HPMCOUNTER23] = { "hpmcounter23", ctr, read_hpmcounter }, 6346 [CSR_HPMCOUNTER24] = { "hpmcounter24", ctr, read_hpmcounter }, 6347 [CSR_HPMCOUNTER25] = { "hpmcounter25", ctr, read_hpmcounter }, 6348 [CSR_HPMCOUNTER26] = { "hpmcounter26", ctr, read_hpmcounter }, 6349 [CSR_HPMCOUNTER27] = { "hpmcounter27", ctr, read_hpmcounter }, 6350 [CSR_HPMCOUNTER28] = { "hpmcounter28", ctr, read_hpmcounter }, 6351 [CSR_HPMCOUNTER29] = { "hpmcounter29", ctr, read_hpmcounter }, 6352 [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_hpmcounter }, 6353 [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_hpmcounter }, 6354 6355 [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_hpmcounter, 6356 write_mhpmcounter }, 6357 [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_hpmcounter, 6358 write_mhpmcounter }, 6359 [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_hpmcounter, 6360 write_mhpmcounter }, 6361 [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_hpmcounter, 6362 write_mhpmcounter }, 6363 [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_hpmcounter, 6364 write_mhpmcounter }, 6365 [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_hpmcounter, 6366 write_mhpmcounter }, 6367 [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_hpmcounter, 6368 write_mhpmcounter }, 6369 [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_hpmcounter, 6370 write_mhpmcounter }, 6371 [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_hpmcounter, 6372 write_mhpmcounter }, 6373 [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_hpmcounter, 6374 write_mhpmcounter }, 6375 [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_hpmcounter, 6376 write_mhpmcounter }, 6377 [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_hpmcounter, 6378 write_mhpmcounter }, 6379 [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_hpmcounter, 6380 write_mhpmcounter }, 6381 [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_hpmcounter, 6382 write_mhpmcounter }, 6383 [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_hpmcounter, 6384 write_mhpmcounter }, 6385 [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_hpmcounter, 6386 write_mhpmcounter }, 6387 [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_hpmcounter, 6388 write_mhpmcounter }, 6389 [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_hpmcounter, 6390 write_mhpmcounter }, 6391 [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_hpmcounter, 6392 write_mhpmcounter }, 6393 [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_hpmcounter, 6394 write_mhpmcounter }, 6395 [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_hpmcounter, 6396 write_mhpmcounter }, 6397 [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_hpmcounter, 6398 write_mhpmcounter }, 6399 [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_hpmcounter, 6400 write_mhpmcounter }, 6401 [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_hpmcounter, 6402 write_mhpmcounter }, 6403 [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_hpmcounter, 6404 write_mhpmcounter }, 6405 [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_hpmcounter, 6406 write_mhpmcounter }, 6407 [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_hpmcounter, 6408 write_mhpmcounter }, 6409 [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_hpmcounter, 6410 write_mhpmcounter }, 6411 [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_hpmcounter, 6412 write_mhpmcounter }, 6413 6414 [CSR_MCOUNTINHIBIT] = { "mcountinhibit", any, read_mcountinhibit, 6415 write_mcountinhibit, 6416 .min_priv_ver = PRIV_VERSION_1_11_0 }, 6417 6418 [CSR_MCYCLECFG] = { "mcyclecfg", smcntrpmf, read_mcyclecfg, 6419 write_mcyclecfg, 6420 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6421 [CSR_MINSTRETCFG] = { "minstretcfg", smcntrpmf, read_minstretcfg, 6422 write_minstretcfg, 6423 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6424 6425 [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_mhpmevent, 6426 write_mhpmevent }, 6427 [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_mhpmevent, 6428 write_mhpmevent }, 6429 [CSR_MHPMEVENT5] = { "mhpmevent5", any, read_mhpmevent, 6430 write_mhpmevent }, 6431 [CSR_MHPMEVENT6] = { "mhpmevent6", any, read_mhpmevent, 6432 write_mhpmevent }, 6433 [CSR_MHPMEVENT7] = { "mhpmevent7", any, read_mhpmevent, 6434 write_mhpmevent }, 6435 [CSR_MHPMEVENT8] = { "mhpmevent8", any, read_mhpmevent, 6436 write_mhpmevent }, 6437 [CSR_MHPMEVENT9] = { "mhpmevent9", any, read_mhpmevent, 6438 write_mhpmevent }, 6439 [CSR_MHPMEVENT10] = { "mhpmevent10", any, read_mhpmevent, 6440 write_mhpmevent }, 6441 [CSR_MHPMEVENT11] = { "mhpmevent11", any, read_mhpmevent, 6442 write_mhpmevent }, 6443 [CSR_MHPMEVENT12] = { "mhpmevent12", any, read_mhpmevent, 6444 write_mhpmevent }, 6445 [CSR_MHPMEVENT13] = { "mhpmevent13", any, read_mhpmevent, 6446 write_mhpmevent }, 6447 [CSR_MHPMEVENT14] = { "mhpmevent14", any, read_mhpmevent, 6448 write_mhpmevent }, 6449 [CSR_MHPMEVENT15] = { "mhpmevent15", any, read_mhpmevent, 6450 write_mhpmevent }, 6451 [CSR_MHPMEVENT16] = { "mhpmevent16", any, read_mhpmevent, 6452 write_mhpmevent }, 6453 [CSR_MHPMEVENT17] = { "mhpmevent17", any, read_mhpmevent, 6454 write_mhpmevent }, 6455 [CSR_MHPMEVENT18] = { "mhpmevent18", any, read_mhpmevent, 6456 write_mhpmevent }, 6457 [CSR_MHPMEVENT19] = { "mhpmevent19", any, read_mhpmevent, 6458 write_mhpmevent }, 6459 [CSR_MHPMEVENT20] = { "mhpmevent20", any, read_mhpmevent, 6460 write_mhpmevent }, 6461 [CSR_MHPMEVENT21] = { "mhpmevent21", any, read_mhpmevent, 6462 write_mhpmevent }, 6463 [CSR_MHPMEVENT22] = { "mhpmevent22", any, read_mhpmevent, 6464 write_mhpmevent }, 6465 [CSR_MHPMEVENT23] = { "mhpmevent23", any, read_mhpmevent, 6466 write_mhpmevent }, 6467 [CSR_MHPMEVENT24] = { "mhpmevent24", any, read_mhpmevent, 6468 write_mhpmevent }, 6469 [CSR_MHPMEVENT25] = { "mhpmevent25", any, read_mhpmevent, 6470 write_mhpmevent }, 6471 [CSR_MHPMEVENT26] = { "mhpmevent26", any, read_mhpmevent, 6472 write_mhpmevent }, 6473 [CSR_MHPMEVENT27] = { "mhpmevent27", any, read_mhpmevent, 6474 write_mhpmevent }, 6475 [CSR_MHPMEVENT28] = { "mhpmevent28", any, read_mhpmevent, 6476 write_mhpmevent }, 6477 [CSR_MHPMEVENT29] = { "mhpmevent29", any, read_mhpmevent, 6478 write_mhpmevent }, 6479 [CSR_MHPMEVENT30] = { "mhpmevent30", any, read_mhpmevent, 6480 write_mhpmevent }, 6481 [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_mhpmevent, 6482 write_mhpmevent }, 6483 6484 [CSR_MCYCLECFGH] = { "mcyclecfgh", smcntrpmf_32, read_mcyclecfgh, 6485 write_mcyclecfgh, 6486 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6487 [CSR_MINSTRETCFGH] = { "minstretcfgh", smcntrpmf_32, read_minstretcfgh, 6488 write_minstretcfgh, 6489 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6490 6491 [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf_32, read_mhpmeventh, 6492 write_mhpmeventh, 6493 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6494 [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf_32, read_mhpmeventh, 6495 write_mhpmeventh, 6496 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6497 [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf_32, read_mhpmeventh, 6498 write_mhpmeventh, 6499 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6500 [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf_32, read_mhpmeventh, 6501 write_mhpmeventh, 6502 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6503 [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf_32, read_mhpmeventh, 6504 write_mhpmeventh, 6505 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6506 [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf_32, read_mhpmeventh, 6507 write_mhpmeventh, 6508 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6509 [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf_32, read_mhpmeventh, 6510 write_mhpmeventh, 6511 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6512 [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf_32, read_mhpmeventh, 6513 write_mhpmeventh, 6514 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6515 [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf_32, read_mhpmeventh, 6516 write_mhpmeventh, 6517 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6518 [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf_32, read_mhpmeventh, 6519 write_mhpmeventh, 6520 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6521 [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf_32, read_mhpmeventh, 6522 write_mhpmeventh, 6523 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6524 [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf_32, read_mhpmeventh, 6525 write_mhpmeventh, 6526 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6527 [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf_32, read_mhpmeventh, 6528 write_mhpmeventh, 6529 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6530 [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf_32, read_mhpmeventh, 6531 write_mhpmeventh, 6532 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6533 [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf_32, read_mhpmeventh, 6534 write_mhpmeventh, 6535 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6536 [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf_32, read_mhpmeventh, 6537 write_mhpmeventh, 6538 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6539 [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf_32, read_mhpmeventh, 6540 write_mhpmeventh, 6541 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6542 [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf_32, read_mhpmeventh, 6543 write_mhpmeventh, 6544 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6545 [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf_32, read_mhpmeventh, 6546 write_mhpmeventh, 6547 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6548 [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf_32, read_mhpmeventh, 6549 write_mhpmeventh, 6550 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6551 [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf_32, read_mhpmeventh, 6552 write_mhpmeventh, 6553 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6554 [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf_32, read_mhpmeventh, 6555 write_mhpmeventh, 6556 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6557 [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf_32, read_mhpmeventh, 6558 write_mhpmeventh, 6559 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6560 [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf_32, read_mhpmeventh, 6561 write_mhpmeventh, 6562 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6563 [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf_32, read_mhpmeventh, 6564 write_mhpmeventh, 6565 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6566 [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf_32, read_mhpmeventh, 6567 write_mhpmeventh, 6568 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6569 [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf_32, read_mhpmeventh, 6570 write_mhpmeventh, 6571 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6572 [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf_32, read_mhpmeventh, 6573 write_mhpmeventh, 6574 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6575 [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf_32, read_mhpmeventh, 6576 write_mhpmeventh, 6577 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6578 6579 [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, 6580 [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh }, 6581 [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_hpmcounterh }, 6582 [CSR_HPMCOUNTER6H] = { "hpmcounter6h", ctr32, read_hpmcounterh }, 6583 [CSR_HPMCOUNTER7H] = { "hpmcounter7h", ctr32, read_hpmcounterh }, 6584 [CSR_HPMCOUNTER8H] = { "hpmcounter8h", ctr32, read_hpmcounterh }, 6585 [CSR_HPMCOUNTER9H] = { "hpmcounter9h", ctr32, read_hpmcounterh }, 6586 [CSR_HPMCOUNTER10H] = { "hpmcounter10h", ctr32, read_hpmcounterh }, 6587 [CSR_HPMCOUNTER11H] = { "hpmcounter11h", ctr32, read_hpmcounterh }, 6588 [CSR_HPMCOUNTER12H] = { "hpmcounter12h", ctr32, read_hpmcounterh }, 6589 [CSR_HPMCOUNTER13H] = { "hpmcounter13h", ctr32, read_hpmcounterh }, 6590 [CSR_HPMCOUNTER14H] = { "hpmcounter14h", ctr32, read_hpmcounterh }, 6591 [CSR_HPMCOUNTER15H] = { "hpmcounter15h", ctr32, read_hpmcounterh }, 6592 [CSR_HPMCOUNTER16H] = { "hpmcounter16h", ctr32, read_hpmcounterh }, 6593 [CSR_HPMCOUNTER17H] = { "hpmcounter17h", ctr32, read_hpmcounterh }, 6594 [CSR_HPMCOUNTER18H] = { "hpmcounter18h", ctr32, read_hpmcounterh }, 6595 [CSR_HPMCOUNTER19H] = { "hpmcounter19h", ctr32, read_hpmcounterh }, 6596 [CSR_HPMCOUNTER20H] = { "hpmcounter20h", ctr32, read_hpmcounterh }, 6597 [CSR_HPMCOUNTER21H] = { "hpmcounter21h", ctr32, read_hpmcounterh }, 6598 [CSR_HPMCOUNTER22H] = { "hpmcounter22h", ctr32, read_hpmcounterh }, 6599 [CSR_HPMCOUNTER23H] = { "hpmcounter23h", ctr32, read_hpmcounterh }, 6600 [CSR_HPMCOUNTER24H] = { "hpmcounter24h", ctr32, read_hpmcounterh }, 6601 [CSR_HPMCOUNTER25H] = { "hpmcounter25h", ctr32, read_hpmcounterh }, 6602 [CSR_HPMCOUNTER26H] = { "hpmcounter26h", ctr32, read_hpmcounterh }, 6603 [CSR_HPMCOUNTER27H] = { "hpmcounter27h", ctr32, read_hpmcounterh }, 6604 [CSR_HPMCOUNTER28H] = { "hpmcounter28h", ctr32, read_hpmcounterh }, 6605 [CSR_HPMCOUNTER29H] = { "hpmcounter29h", ctr32, read_hpmcounterh }, 6606 [CSR_HPMCOUNTER30H] = { "hpmcounter30h", ctr32, read_hpmcounterh }, 6607 [CSR_HPMCOUNTER31H] = { "hpmcounter31h", ctr32, read_hpmcounterh }, 6608 6609 [CSR_MHPMCOUNTER3H] = { "mhpmcounter3h", mctr32, read_hpmcounterh, 6610 write_mhpmcounterh }, 6611 [CSR_MHPMCOUNTER4H] = { "mhpmcounter4h", mctr32, read_hpmcounterh, 6612 write_mhpmcounterh }, 6613 [CSR_MHPMCOUNTER5H] = { "mhpmcounter5h", mctr32, read_hpmcounterh, 6614 write_mhpmcounterh }, 6615 [CSR_MHPMCOUNTER6H] = { "mhpmcounter6h", mctr32, read_hpmcounterh, 6616 write_mhpmcounterh }, 6617 [CSR_MHPMCOUNTER7H] = { "mhpmcounter7h", mctr32, read_hpmcounterh, 6618 write_mhpmcounterh }, 6619 [CSR_MHPMCOUNTER8H] = { "mhpmcounter8h", mctr32, read_hpmcounterh, 6620 write_mhpmcounterh }, 6621 [CSR_MHPMCOUNTER9H] = { "mhpmcounter9h", mctr32, read_hpmcounterh, 6622 write_mhpmcounterh }, 6623 [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", mctr32, read_hpmcounterh, 6624 write_mhpmcounterh }, 6625 [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", mctr32, read_hpmcounterh, 6626 write_mhpmcounterh }, 6627 [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", mctr32, read_hpmcounterh, 6628 write_mhpmcounterh }, 6629 [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", mctr32, read_hpmcounterh, 6630 write_mhpmcounterh }, 6631 [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", mctr32, read_hpmcounterh, 6632 write_mhpmcounterh }, 6633 [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", mctr32, read_hpmcounterh, 6634 write_mhpmcounterh }, 6635 [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", mctr32, read_hpmcounterh, 6636 write_mhpmcounterh }, 6637 [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", mctr32, read_hpmcounterh, 6638 write_mhpmcounterh }, 6639 [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", mctr32, read_hpmcounterh, 6640 write_mhpmcounterh }, 6641 [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", mctr32, read_hpmcounterh, 6642 write_mhpmcounterh }, 6643 [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", mctr32, read_hpmcounterh, 6644 write_mhpmcounterh }, 6645 [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", mctr32, read_hpmcounterh, 6646 write_mhpmcounterh }, 6647 [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", mctr32, read_hpmcounterh, 6648 write_mhpmcounterh }, 6649 [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", mctr32, read_hpmcounterh, 6650 write_mhpmcounterh }, 6651 [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", mctr32, read_hpmcounterh, 6652 write_mhpmcounterh }, 6653 [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", mctr32, read_hpmcounterh, 6654 write_mhpmcounterh }, 6655 [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", mctr32, read_hpmcounterh, 6656 write_mhpmcounterh }, 6657 [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", mctr32, read_hpmcounterh, 6658 write_mhpmcounterh }, 6659 [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", mctr32, read_hpmcounterh, 6660 write_mhpmcounterh }, 6661 [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", mctr32, read_hpmcounterh, 6662 write_mhpmcounterh }, 6663 [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", mctr32, read_hpmcounterh, 6664 write_mhpmcounterh }, 6665 [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, 6666 write_mhpmcounterh }, 6667 [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf, 6668 .min_priv_ver = PRIV_VERSION_1_12_0 }, 6669 6670 #endif /* !CONFIG_USER_ONLY */ 6671 }; 6672