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