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