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