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