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