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