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