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