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