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 /* loose check condition for fcsr in vector extension */ 42 if ((csrno == CSR_FCSR) && (env->misa & RVV)) { 43 return RISCV_EXCP_NONE; 44 } 45 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 46 return RISCV_EXCP_ILLEGAL_INST; 47 } 48 #endif 49 return RISCV_EXCP_NONE; 50 } 51 52 static RISCVException vs(CPURISCVState *env, int csrno) 53 { 54 if (env->misa & RVV) { 55 return RISCV_EXCP_NONE; 56 } 57 return RISCV_EXCP_ILLEGAL_INST; 58 } 59 60 static RISCVException ctr(CPURISCVState *env, int csrno) 61 { 62 #if !defined(CONFIG_USER_ONLY) 63 CPUState *cs = env_cpu(env); 64 RISCVCPU *cpu = RISCV_CPU(cs); 65 66 if (!cpu->cfg.ext_counters) { 67 /* The Counters extensions is not enabled */ 68 return RISCV_EXCP_ILLEGAL_INST; 69 } 70 71 if (riscv_cpu_virt_enabled(env)) { 72 switch (csrno) { 73 case CSR_CYCLE: 74 if (!get_field(env->hcounteren, HCOUNTEREN_CY) && 75 get_field(env->mcounteren, HCOUNTEREN_CY)) { 76 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 77 } 78 break; 79 case CSR_TIME: 80 if (!get_field(env->hcounteren, HCOUNTEREN_TM) && 81 get_field(env->mcounteren, HCOUNTEREN_TM)) { 82 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 83 } 84 break; 85 case CSR_INSTRET: 86 if (!get_field(env->hcounteren, HCOUNTEREN_IR) && 87 get_field(env->mcounteren, HCOUNTEREN_IR)) { 88 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 89 } 90 break; 91 case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: 92 if (!get_field(env->hcounteren, 1 << (csrno - CSR_HPMCOUNTER3)) && 93 get_field(env->mcounteren, 1 << (csrno - CSR_HPMCOUNTER3))) { 94 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 95 } 96 break; 97 } 98 if (riscv_cpu_is_32bit(env)) { 99 switch (csrno) { 100 case CSR_CYCLEH: 101 if (!get_field(env->hcounteren, HCOUNTEREN_CY) && 102 get_field(env->mcounteren, HCOUNTEREN_CY)) { 103 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 104 } 105 break; 106 case CSR_TIMEH: 107 if (!get_field(env->hcounteren, HCOUNTEREN_TM) && 108 get_field(env->mcounteren, HCOUNTEREN_TM)) { 109 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 110 } 111 break; 112 case CSR_INSTRETH: 113 if (!get_field(env->hcounteren, HCOUNTEREN_IR) && 114 get_field(env->mcounteren, HCOUNTEREN_IR)) { 115 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 116 } 117 break; 118 case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: 119 if (!get_field(env->hcounteren, 1 << (csrno - CSR_HPMCOUNTER3H)) && 120 get_field(env->mcounteren, 1 << (csrno - CSR_HPMCOUNTER3H))) { 121 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 122 } 123 break; 124 } 125 } 126 } 127 #endif 128 return RISCV_EXCP_NONE; 129 } 130 131 static RISCVException ctr32(CPURISCVState *env, int csrno) 132 { 133 if (!riscv_cpu_is_32bit(env)) { 134 return RISCV_EXCP_ILLEGAL_INST; 135 } 136 137 return ctr(env, csrno); 138 } 139 140 #if !defined(CONFIG_USER_ONLY) 141 static RISCVException any(CPURISCVState *env, int csrno) 142 { 143 return RISCV_EXCP_NONE; 144 } 145 146 static RISCVException any32(CPURISCVState *env, int csrno) 147 { 148 if (!riscv_cpu_is_32bit(env)) { 149 return RISCV_EXCP_ILLEGAL_INST; 150 } 151 152 return any(env, csrno); 153 154 } 155 156 static RISCVException smode(CPURISCVState *env, int csrno) 157 { 158 if (riscv_has_ext(env, RVS)) { 159 return RISCV_EXCP_NONE; 160 } 161 162 return RISCV_EXCP_ILLEGAL_INST; 163 } 164 165 static RISCVException hmode(CPURISCVState *env, int csrno) 166 { 167 if (riscv_has_ext(env, RVS) && 168 riscv_has_ext(env, RVH)) { 169 /* Hypervisor extension is supported */ 170 if ((env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) || 171 env->priv == PRV_M) { 172 return RISCV_EXCP_NONE; 173 } else { 174 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 175 } 176 } 177 178 return RISCV_EXCP_ILLEGAL_INST; 179 } 180 181 static RISCVException hmode32(CPURISCVState *env, int csrno) 182 { 183 if (!riscv_cpu_is_32bit(env)) { 184 if (riscv_cpu_virt_enabled(env)) { 185 return RISCV_EXCP_ILLEGAL_INST; 186 } else { 187 return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; 188 } 189 } 190 191 return hmode(env, csrno); 192 193 } 194 195 static RISCVException pmp(CPURISCVState *env, int csrno) 196 { 197 if (riscv_feature(env, RISCV_FEATURE_PMP)) { 198 return RISCV_EXCP_NONE; 199 } 200 201 return RISCV_EXCP_ILLEGAL_INST; 202 } 203 #endif 204 205 /* User Floating-Point CSRs */ 206 static int read_fflags(CPURISCVState *env, int csrno, target_ulong *val) 207 { 208 #if !defined(CONFIG_USER_ONLY) 209 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 210 return -RISCV_EXCP_ILLEGAL_INST; 211 } 212 #endif 213 *val = riscv_cpu_get_fflags(env); 214 return 0; 215 } 216 217 static int write_fflags(CPURISCVState *env, int csrno, target_ulong val) 218 { 219 #if !defined(CONFIG_USER_ONLY) 220 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 221 return -RISCV_EXCP_ILLEGAL_INST; 222 } 223 env->mstatus |= MSTATUS_FS; 224 #endif 225 riscv_cpu_set_fflags(env, val & (FSR_AEXC >> FSR_AEXC_SHIFT)); 226 return 0; 227 } 228 229 static int read_frm(CPURISCVState *env, int csrno, target_ulong *val) 230 { 231 #if !defined(CONFIG_USER_ONLY) 232 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 233 return -RISCV_EXCP_ILLEGAL_INST; 234 } 235 #endif 236 *val = env->frm; 237 return 0; 238 } 239 240 static int write_frm(CPURISCVState *env, int csrno, target_ulong val) 241 { 242 #if !defined(CONFIG_USER_ONLY) 243 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 244 return -RISCV_EXCP_ILLEGAL_INST; 245 } 246 env->mstatus |= MSTATUS_FS; 247 #endif 248 env->frm = val & (FSR_RD >> FSR_RD_SHIFT); 249 return 0; 250 } 251 252 static int read_fcsr(CPURISCVState *env, int csrno, target_ulong *val) 253 { 254 #if !defined(CONFIG_USER_ONLY) 255 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 256 return -RISCV_EXCP_ILLEGAL_INST; 257 } 258 #endif 259 *val = (riscv_cpu_get_fflags(env) << FSR_AEXC_SHIFT) 260 | (env->frm << FSR_RD_SHIFT); 261 if (vs(env, csrno) >= 0) { 262 *val |= (env->vxrm << FSR_VXRM_SHIFT) 263 | (env->vxsat << FSR_VXSAT_SHIFT); 264 } 265 return 0; 266 } 267 268 static int write_fcsr(CPURISCVState *env, int csrno, target_ulong val) 269 { 270 #if !defined(CONFIG_USER_ONLY) 271 if (!env->debugger && !riscv_cpu_fp_enabled(env)) { 272 return -RISCV_EXCP_ILLEGAL_INST; 273 } 274 env->mstatus |= MSTATUS_FS; 275 #endif 276 env->frm = (val & FSR_RD) >> FSR_RD_SHIFT; 277 if (vs(env, csrno) >= 0) { 278 env->vxrm = (val & FSR_VXRM) >> FSR_VXRM_SHIFT; 279 env->vxsat = (val & FSR_VXSAT) >> FSR_VXSAT_SHIFT; 280 } 281 riscv_cpu_set_fflags(env, (val & FSR_AEXC) >> FSR_AEXC_SHIFT); 282 return 0; 283 } 284 285 static int read_vtype(CPURISCVState *env, int csrno, target_ulong *val) 286 { 287 *val = env->vtype; 288 return 0; 289 } 290 291 static int read_vl(CPURISCVState *env, int csrno, target_ulong *val) 292 { 293 *val = env->vl; 294 return 0; 295 } 296 297 static int read_vxrm(CPURISCVState *env, int csrno, target_ulong *val) 298 { 299 *val = env->vxrm; 300 return 0; 301 } 302 303 static int write_vxrm(CPURISCVState *env, int csrno, target_ulong val) 304 { 305 env->vxrm = val; 306 return 0; 307 } 308 309 static int read_vxsat(CPURISCVState *env, int csrno, target_ulong *val) 310 { 311 *val = env->vxsat; 312 return 0; 313 } 314 315 static int write_vxsat(CPURISCVState *env, int csrno, target_ulong val) 316 { 317 env->vxsat = val; 318 return 0; 319 } 320 321 static int read_vstart(CPURISCVState *env, int csrno, target_ulong *val) 322 { 323 *val = env->vstart; 324 return 0; 325 } 326 327 static int write_vstart(CPURISCVState *env, int csrno, target_ulong val) 328 { 329 env->vstart = val; 330 return 0; 331 } 332 333 /* User Timers and Counters */ 334 static int read_instret(CPURISCVState *env, int csrno, target_ulong *val) 335 { 336 #if !defined(CONFIG_USER_ONLY) 337 if (icount_enabled()) { 338 *val = icount_get(); 339 } else { 340 *val = cpu_get_host_ticks(); 341 } 342 #else 343 *val = cpu_get_host_ticks(); 344 #endif 345 return 0; 346 } 347 348 static int read_instreth(CPURISCVState *env, int csrno, target_ulong *val) 349 { 350 #if !defined(CONFIG_USER_ONLY) 351 if (icount_enabled()) { 352 *val = icount_get() >> 32; 353 } else { 354 *val = cpu_get_host_ticks() >> 32; 355 } 356 #else 357 *val = cpu_get_host_ticks() >> 32; 358 #endif 359 return 0; 360 } 361 362 #if defined(CONFIG_USER_ONLY) 363 static int read_time(CPURISCVState *env, int csrno, target_ulong *val) 364 { 365 *val = cpu_get_host_ticks(); 366 return 0; 367 } 368 369 static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val) 370 { 371 *val = cpu_get_host_ticks() >> 32; 372 return 0; 373 } 374 375 #else /* CONFIG_USER_ONLY */ 376 377 static int read_time(CPURISCVState *env, int csrno, target_ulong *val) 378 { 379 uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0; 380 381 if (!env->rdtime_fn) { 382 return -RISCV_EXCP_ILLEGAL_INST; 383 } 384 385 *val = env->rdtime_fn(env->rdtime_fn_arg) + delta; 386 return 0; 387 } 388 389 static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val) 390 { 391 uint64_t delta = riscv_cpu_virt_enabled(env) ? env->htimedelta : 0; 392 393 if (!env->rdtime_fn) { 394 return -RISCV_EXCP_ILLEGAL_INST; 395 } 396 397 *val = (env->rdtime_fn(env->rdtime_fn_arg) + delta) >> 32; 398 return 0; 399 } 400 401 /* Machine constants */ 402 403 #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP) 404 #define S_MODE_INTERRUPTS (MIP_SSIP | MIP_STIP | MIP_SEIP) 405 #define VS_MODE_INTERRUPTS (MIP_VSSIP | MIP_VSTIP | MIP_VSEIP) 406 407 static const target_ulong delegable_ints = S_MODE_INTERRUPTS | 408 VS_MODE_INTERRUPTS; 409 static const target_ulong all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS | 410 VS_MODE_INTERRUPTS; 411 static const target_ulong delegable_excps = 412 (1ULL << (RISCV_EXCP_INST_ADDR_MIS)) | 413 (1ULL << (RISCV_EXCP_INST_ACCESS_FAULT)) | 414 (1ULL << (RISCV_EXCP_ILLEGAL_INST)) | 415 (1ULL << (RISCV_EXCP_BREAKPOINT)) | 416 (1ULL << (RISCV_EXCP_LOAD_ADDR_MIS)) | 417 (1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT)) | 418 (1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS)) | 419 (1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT)) | 420 (1ULL << (RISCV_EXCP_U_ECALL)) | 421 (1ULL << (RISCV_EXCP_S_ECALL)) | 422 (1ULL << (RISCV_EXCP_VS_ECALL)) | 423 (1ULL << (RISCV_EXCP_M_ECALL)) | 424 (1ULL << (RISCV_EXCP_INST_PAGE_FAULT)) | 425 (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | 426 (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | 427 (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | 428 (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | 429 (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | 430 (1ULL << (RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT)); 431 static const target_ulong sstatus_v1_10_mask = SSTATUS_SIE | SSTATUS_SPIE | 432 SSTATUS_UIE | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS | 433 SSTATUS_SUM | SSTATUS_MXR | SSTATUS_SD; 434 static const target_ulong sip_writable_mask = SIP_SSIP | MIP_USIP | MIP_UEIP; 435 static const target_ulong hip_writable_mask = MIP_VSSIP; 436 static const target_ulong hvip_writable_mask = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP; 437 static const target_ulong vsip_writable_mask = MIP_VSSIP; 438 439 static const char valid_vm_1_10_32[16] = { 440 [VM_1_10_MBARE] = 1, 441 [VM_1_10_SV32] = 1 442 }; 443 444 static const char valid_vm_1_10_64[16] = { 445 [VM_1_10_MBARE] = 1, 446 [VM_1_10_SV39] = 1, 447 [VM_1_10_SV48] = 1, 448 [VM_1_10_SV57] = 1 449 }; 450 451 /* Machine Information Registers */ 452 static int read_zero(CPURISCVState *env, int csrno, target_ulong *val) 453 { 454 return *val = 0; 455 } 456 457 static int read_mhartid(CPURISCVState *env, int csrno, target_ulong *val) 458 { 459 *val = env->mhartid; 460 return 0; 461 } 462 463 /* Machine Trap Setup */ 464 static int read_mstatus(CPURISCVState *env, int csrno, target_ulong *val) 465 { 466 *val = env->mstatus; 467 return 0; 468 } 469 470 static int validate_vm(CPURISCVState *env, target_ulong vm) 471 { 472 if (riscv_cpu_is_32bit(env)) { 473 return valid_vm_1_10_32[vm & 0xf]; 474 } else { 475 return valid_vm_1_10_64[vm & 0xf]; 476 } 477 } 478 479 static int write_mstatus(CPURISCVState *env, int csrno, target_ulong val) 480 { 481 uint64_t mstatus = env->mstatus; 482 uint64_t mask = 0; 483 int dirty; 484 485 /* flush tlb on mstatus fields that affect VM */ 486 if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV | 487 MSTATUS_MPRV | MSTATUS_SUM)) { 488 tlb_flush(env_cpu(env)); 489 } 490 mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | 491 MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM | 492 MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR | 493 MSTATUS_TW; 494 495 if (!riscv_cpu_is_32bit(env)) { 496 /* 497 * RV32: MPV and GVA are not in mstatus. The current plan is to 498 * add them to mstatush. For now, we just don't support it. 499 */ 500 mask |= MSTATUS_MPV | MSTATUS_GVA; 501 } 502 503 mstatus = (mstatus & ~mask) | (val & mask); 504 505 dirty = ((mstatus & MSTATUS_FS) == MSTATUS_FS) | 506 ((mstatus & MSTATUS_XS) == MSTATUS_XS); 507 mstatus = set_field(mstatus, MSTATUS_SD, dirty); 508 env->mstatus = mstatus; 509 510 return 0; 511 } 512 513 static int read_mstatush(CPURISCVState *env, int csrno, target_ulong *val) 514 { 515 *val = env->mstatus >> 32; 516 return 0; 517 } 518 519 static int write_mstatush(CPURISCVState *env, int csrno, target_ulong val) 520 { 521 uint64_t valh = (uint64_t)val << 32; 522 uint64_t mask = MSTATUS_MPV | MSTATUS_GVA; 523 524 if ((valh ^ env->mstatus) & (MSTATUS_MPV)) { 525 tlb_flush(env_cpu(env)); 526 } 527 528 env->mstatus = (env->mstatus & ~mask) | (valh & mask); 529 530 return 0; 531 } 532 533 static int read_misa(CPURISCVState *env, int csrno, target_ulong *val) 534 { 535 *val = env->misa; 536 return 0; 537 } 538 539 static int write_misa(CPURISCVState *env, int csrno, target_ulong val) 540 { 541 if (!riscv_feature(env, RISCV_FEATURE_MISA)) { 542 /* drop write to misa */ 543 return 0; 544 } 545 546 /* 'I' or 'E' must be present */ 547 if (!(val & (RVI | RVE))) { 548 /* It is not, drop write to misa */ 549 return 0; 550 } 551 552 /* 'E' excludes all other extensions */ 553 if (val & RVE) { 554 /* when we support 'E' we can do "val = RVE;" however 555 * for now we just drop writes if 'E' is present. 556 */ 557 return 0; 558 } 559 560 /* Mask extensions that are not supported by this hart */ 561 val &= env->misa_mask; 562 563 /* Mask extensions that are not supported by QEMU */ 564 val &= (RVI | RVE | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 565 566 /* 'D' depends on 'F', so clear 'D' if 'F' is not present */ 567 if ((val & RVD) && !(val & RVF)) { 568 val &= ~RVD; 569 } 570 571 /* Suppress 'C' if next instruction is not aligned 572 * TODO: this should check next_pc 573 */ 574 if ((val & RVC) && (GETPC() & ~3) != 0) { 575 val &= ~RVC; 576 } 577 578 /* misa.MXL writes are not supported by QEMU */ 579 val = (env->misa & MISA_MXL) | (val & ~MISA_MXL); 580 581 /* flush translation cache */ 582 if (val != env->misa) { 583 tb_flush(env_cpu(env)); 584 } 585 586 env->misa = val; 587 588 return 0; 589 } 590 591 static int read_medeleg(CPURISCVState *env, int csrno, target_ulong *val) 592 { 593 *val = env->medeleg; 594 return 0; 595 } 596 597 static int write_medeleg(CPURISCVState *env, int csrno, target_ulong val) 598 { 599 env->medeleg = (env->medeleg & ~delegable_excps) | (val & delegable_excps); 600 return 0; 601 } 602 603 static int read_mideleg(CPURISCVState *env, int csrno, target_ulong *val) 604 { 605 *val = env->mideleg; 606 return 0; 607 } 608 609 static int write_mideleg(CPURISCVState *env, int csrno, target_ulong val) 610 { 611 env->mideleg = (env->mideleg & ~delegable_ints) | (val & delegable_ints); 612 if (riscv_has_ext(env, RVH)) { 613 env->mideleg |= VS_MODE_INTERRUPTS; 614 } 615 return 0; 616 } 617 618 static int read_mie(CPURISCVState *env, int csrno, target_ulong *val) 619 { 620 *val = env->mie; 621 return 0; 622 } 623 624 static int write_mie(CPURISCVState *env, int csrno, target_ulong val) 625 { 626 env->mie = (env->mie & ~all_ints) | (val & all_ints); 627 return 0; 628 } 629 630 static int read_mtvec(CPURISCVState *env, int csrno, target_ulong *val) 631 { 632 *val = env->mtvec; 633 return 0; 634 } 635 636 static int write_mtvec(CPURISCVState *env, int csrno, target_ulong val) 637 { 638 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */ 639 if ((val & 3) < 2) { 640 env->mtvec = val; 641 } else { 642 qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: reserved mode not supported\n"); 643 } 644 return 0; 645 } 646 647 static int read_mcounteren(CPURISCVState *env, int csrno, target_ulong *val) 648 { 649 *val = env->mcounteren; 650 return 0; 651 } 652 653 static int write_mcounteren(CPURISCVState *env, int csrno, target_ulong val) 654 { 655 env->mcounteren = val; 656 return 0; 657 } 658 659 /* Machine Trap Handling */ 660 static int read_mscratch(CPURISCVState *env, int csrno, target_ulong *val) 661 { 662 *val = env->mscratch; 663 return 0; 664 } 665 666 static int write_mscratch(CPURISCVState *env, int csrno, target_ulong val) 667 { 668 env->mscratch = val; 669 return 0; 670 } 671 672 static int read_mepc(CPURISCVState *env, int csrno, target_ulong *val) 673 { 674 *val = env->mepc; 675 return 0; 676 } 677 678 static int write_mepc(CPURISCVState *env, int csrno, target_ulong val) 679 { 680 env->mepc = val; 681 return 0; 682 } 683 684 static int read_mcause(CPURISCVState *env, int csrno, target_ulong *val) 685 { 686 *val = env->mcause; 687 return 0; 688 } 689 690 static int write_mcause(CPURISCVState *env, int csrno, target_ulong val) 691 { 692 env->mcause = val; 693 return 0; 694 } 695 696 static int read_mtval(CPURISCVState *env, int csrno, target_ulong *val) 697 { 698 *val = env->mtval; 699 return 0; 700 } 701 702 static int write_mtval(CPURISCVState *env, int csrno, target_ulong val) 703 { 704 env->mtval = val; 705 return 0; 706 } 707 708 static int rmw_mip(CPURISCVState *env, int csrno, target_ulong *ret_value, 709 target_ulong new_value, target_ulong write_mask) 710 { 711 RISCVCPU *cpu = env_archcpu(env); 712 /* Allow software control of delegable interrupts not claimed by hardware */ 713 target_ulong mask = write_mask & delegable_ints & ~env->miclaim; 714 uint32_t old_mip; 715 716 if (mask) { 717 old_mip = riscv_cpu_update_mip(cpu, mask, (new_value & mask)); 718 } else { 719 old_mip = env->mip; 720 } 721 722 if (ret_value) { 723 *ret_value = old_mip; 724 } 725 726 return 0; 727 } 728 729 /* Supervisor Trap Setup */ 730 static int read_sstatus(CPURISCVState *env, int csrno, target_ulong *val) 731 { 732 target_ulong mask = (sstatus_v1_10_mask); 733 *val = env->mstatus & mask; 734 return 0; 735 } 736 737 static int write_sstatus(CPURISCVState *env, int csrno, target_ulong val) 738 { 739 target_ulong mask = (sstatus_v1_10_mask); 740 target_ulong newval = (env->mstatus & ~mask) | (val & mask); 741 return write_mstatus(env, CSR_MSTATUS, newval); 742 } 743 744 static int read_vsie(CPURISCVState *env, int csrno, target_ulong *val) 745 { 746 /* Shift the VS bits to their S bit location in vsie */ 747 *val = (env->mie & env->hideleg & VS_MODE_INTERRUPTS) >> 1; 748 return 0; 749 } 750 751 static int read_sie(CPURISCVState *env, int csrno, target_ulong *val) 752 { 753 if (riscv_cpu_virt_enabled(env)) { 754 read_vsie(env, CSR_VSIE, val); 755 } else { 756 *val = env->mie & env->mideleg; 757 } 758 return 0; 759 } 760 761 static int write_vsie(CPURISCVState *env, int csrno, target_ulong val) 762 { 763 /* Shift the S bits to their VS bit location in mie */ 764 target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS) | 765 ((val << 1) & env->hideleg & VS_MODE_INTERRUPTS); 766 return write_mie(env, CSR_MIE, newval); 767 } 768 769 static int write_sie(CPURISCVState *env, int csrno, target_ulong val) 770 { 771 if (riscv_cpu_virt_enabled(env)) { 772 write_vsie(env, CSR_VSIE, val); 773 } else { 774 target_ulong newval = (env->mie & ~S_MODE_INTERRUPTS) | 775 (val & S_MODE_INTERRUPTS); 776 write_mie(env, CSR_MIE, newval); 777 } 778 779 return 0; 780 } 781 782 static int read_stvec(CPURISCVState *env, int csrno, target_ulong *val) 783 { 784 *val = env->stvec; 785 return 0; 786 } 787 788 static int write_stvec(CPURISCVState *env, int csrno, target_ulong val) 789 { 790 /* bits [1:0] encode mode; 0 = direct, 1 = vectored, 2 >= reserved */ 791 if ((val & 3) < 2) { 792 env->stvec = val; 793 } else { 794 qemu_log_mask(LOG_UNIMP, "CSR_STVEC: reserved mode not supported\n"); 795 } 796 return 0; 797 } 798 799 static int read_scounteren(CPURISCVState *env, int csrno, target_ulong *val) 800 { 801 *val = env->scounteren; 802 return 0; 803 } 804 805 static int write_scounteren(CPURISCVState *env, int csrno, target_ulong val) 806 { 807 env->scounteren = val; 808 return 0; 809 } 810 811 /* Supervisor Trap Handling */ 812 static int read_sscratch(CPURISCVState *env, int csrno, target_ulong *val) 813 { 814 *val = env->sscratch; 815 return 0; 816 } 817 818 static int write_sscratch(CPURISCVState *env, int csrno, target_ulong val) 819 { 820 env->sscratch = val; 821 return 0; 822 } 823 824 static int read_sepc(CPURISCVState *env, int csrno, target_ulong *val) 825 { 826 *val = env->sepc; 827 return 0; 828 } 829 830 static int write_sepc(CPURISCVState *env, int csrno, target_ulong val) 831 { 832 env->sepc = val; 833 return 0; 834 } 835 836 static int read_scause(CPURISCVState *env, int csrno, target_ulong *val) 837 { 838 *val = env->scause; 839 return 0; 840 } 841 842 static int write_scause(CPURISCVState *env, int csrno, target_ulong val) 843 { 844 env->scause = val; 845 return 0; 846 } 847 848 static int read_stval(CPURISCVState *env, int csrno, target_ulong *val) 849 { 850 *val = env->stval; 851 return 0; 852 } 853 854 static int write_stval(CPURISCVState *env, int csrno, target_ulong val) 855 { 856 env->stval = val; 857 return 0; 858 } 859 860 static int rmw_vsip(CPURISCVState *env, int csrno, target_ulong *ret_value, 861 target_ulong new_value, target_ulong write_mask) 862 { 863 /* Shift the S bits to their VS bit location in mip */ 864 int ret = rmw_mip(env, 0, ret_value, new_value << 1, 865 (write_mask << 1) & vsip_writable_mask & env->hideleg); 866 *ret_value &= VS_MODE_INTERRUPTS; 867 /* Shift the VS bits to their S bit location in vsip */ 868 *ret_value >>= 1; 869 return ret; 870 } 871 872 static int rmw_sip(CPURISCVState *env, int csrno, target_ulong *ret_value, 873 target_ulong new_value, target_ulong write_mask) 874 { 875 int ret; 876 877 if (riscv_cpu_virt_enabled(env)) { 878 ret = rmw_vsip(env, CSR_VSIP, ret_value, new_value, write_mask); 879 } else { 880 ret = rmw_mip(env, CSR_MSTATUS, ret_value, new_value, 881 write_mask & env->mideleg & sip_writable_mask); 882 } 883 884 *ret_value &= env->mideleg; 885 return ret; 886 } 887 888 /* Supervisor Protection and Translation */ 889 static int read_satp(CPURISCVState *env, int csrno, target_ulong *val) 890 { 891 if (!riscv_feature(env, RISCV_FEATURE_MMU)) { 892 *val = 0; 893 return 0; 894 } 895 896 if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) { 897 return -RISCV_EXCP_ILLEGAL_INST; 898 } else { 899 *val = env->satp; 900 } 901 902 return 0; 903 } 904 905 static int write_satp(CPURISCVState *env, int csrno, target_ulong val) 906 { 907 if (!riscv_feature(env, RISCV_FEATURE_MMU)) { 908 return 0; 909 } 910 if (validate_vm(env, get_field(val, SATP_MODE)) && 911 ((val ^ env->satp) & (SATP_MODE | SATP_ASID | SATP_PPN))) 912 { 913 if (env->priv == PRV_S && get_field(env->mstatus, MSTATUS_TVM)) { 914 return -RISCV_EXCP_ILLEGAL_INST; 915 } else { 916 if ((val ^ env->satp) & SATP_ASID) { 917 tlb_flush(env_cpu(env)); 918 } 919 env->satp = val; 920 } 921 } 922 return 0; 923 } 924 925 /* Hypervisor Extensions */ 926 static int read_hstatus(CPURISCVState *env, int csrno, target_ulong *val) 927 { 928 *val = env->hstatus; 929 if (!riscv_cpu_is_32bit(env)) { 930 /* We only support 64-bit VSXL */ 931 *val = set_field(*val, HSTATUS_VSXL, 2); 932 } 933 /* We only support little endian */ 934 *val = set_field(*val, HSTATUS_VSBE, 0); 935 return 0; 936 } 937 938 static int write_hstatus(CPURISCVState *env, int csrno, target_ulong val) 939 { 940 env->hstatus = val; 941 if (!riscv_cpu_is_32bit(env) && get_field(val, HSTATUS_VSXL) != 2) { 942 qemu_log_mask(LOG_UNIMP, "QEMU does not support mixed HSXLEN options."); 943 } 944 if (get_field(val, HSTATUS_VSBE) != 0) { 945 qemu_log_mask(LOG_UNIMP, "QEMU does not support big endian guests."); 946 } 947 return 0; 948 } 949 950 static int read_hedeleg(CPURISCVState *env, int csrno, target_ulong *val) 951 { 952 *val = env->hedeleg; 953 return 0; 954 } 955 956 static int write_hedeleg(CPURISCVState *env, int csrno, target_ulong val) 957 { 958 env->hedeleg = val; 959 return 0; 960 } 961 962 static int read_hideleg(CPURISCVState *env, int csrno, target_ulong *val) 963 { 964 *val = env->hideleg; 965 return 0; 966 } 967 968 static int write_hideleg(CPURISCVState *env, int csrno, target_ulong val) 969 { 970 env->hideleg = val; 971 return 0; 972 } 973 974 static int rmw_hvip(CPURISCVState *env, int csrno, target_ulong *ret_value, 975 target_ulong new_value, target_ulong write_mask) 976 { 977 int ret = rmw_mip(env, 0, ret_value, new_value, 978 write_mask & hvip_writable_mask); 979 980 *ret_value &= hvip_writable_mask; 981 982 return ret; 983 } 984 985 static int rmw_hip(CPURISCVState *env, int csrno, target_ulong *ret_value, 986 target_ulong new_value, target_ulong write_mask) 987 { 988 int ret = rmw_mip(env, 0, ret_value, new_value, 989 write_mask & hip_writable_mask); 990 991 *ret_value &= hip_writable_mask; 992 993 return ret; 994 } 995 996 static int read_hie(CPURISCVState *env, int csrno, target_ulong *val) 997 { 998 *val = env->mie & VS_MODE_INTERRUPTS; 999 return 0; 1000 } 1001 1002 static int write_hie(CPURISCVState *env, int csrno, target_ulong val) 1003 { 1004 target_ulong newval = (env->mie & ~VS_MODE_INTERRUPTS) | (val & VS_MODE_INTERRUPTS); 1005 return write_mie(env, CSR_MIE, newval); 1006 } 1007 1008 static int read_hcounteren(CPURISCVState *env, int csrno, target_ulong *val) 1009 { 1010 *val = env->hcounteren; 1011 return 0; 1012 } 1013 1014 static int write_hcounteren(CPURISCVState *env, int csrno, target_ulong val) 1015 { 1016 env->hcounteren = val; 1017 return 0; 1018 } 1019 1020 static int read_hgeie(CPURISCVState *env, int csrno, target_ulong *val) 1021 { 1022 qemu_log_mask(LOG_UNIMP, "No support for a non-zero GEILEN."); 1023 return 0; 1024 } 1025 1026 static int write_hgeie(CPURISCVState *env, int csrno, target_ulong val) 1027 { 1028 qemu_log_mask(LOG_UNIMP, "No support for a non-zero GEILEN."); 1029 return 0; 1030 } 1031 1032 static int read_htval(CPURISCVState *env, int csrno, target_ulong *val) 1033 { 1034 *val = env->htval; 1035 return 0; 1036 } 1037 1038 static int write_htval(CPURISCVState *env, int csrno, target_ulong val) 1039 { 1040 env->htval = val; 1041 return 0; 1042 } 1043 1044 static int read_htinst(CPURISCVState *env, int csrno, target_ulong *val) 1045 { 1046 *val = env->htinst; 1047 return 0; 1048 } 1049 1050 static int write_htinst(CPURISCVState *env, int csrno, target_ulong val) 1051 { 1052 return 0; 1053 } 1054 1055 static int read_hgeip(CPURISCVState *env, int csrno, target_ulong *val) 1056 { 1057 qemu_log_mask(LOG_UNIMP, "No support for a non-zero GEILEN."); 1058 return 0; 1059 } 1060 1061 static int write_hgeip(CPURISCVState *env, int csrno, target_ulong val) 1062 { 1063 qemu_log_mask(LOG_UNIMP, "No support for a non-zero GEILEN."); 1064 return 0; 1065 } 1066 1067 static int read_hgatp(CPURISCVState *env, int csrno, target_ulong *val) 1068 { 1069 *val = env->hgatp; 1070 return 0; 1071 } 1072 1073 static int write_hgatp(CPURISCVState *env, int csrno, target_ulong val) 1074 { 1075 env->hgatp = val; 1076 return 0; 1077 } 1078 1079 static int read_htimedelta(CPURISCVState *env, int csrno, target_ulong *val) 1080 { 1081 if (!env->rdtime_fn) { 1082 return -RISCV_EXCP_ILLEGAL_INST; 1083 } 1084 1085 *val = env->htimedelta; 1086 return 0; 1087 } 1088 1089 static int write_htimedelta(CPURISCVState *env, int csrno, target_ulong val) 1090 { 1091 if (!env->rdtime_fn) { 1092 return -RISCV_EXCP_ILLEGAL_INST; 1093 } 1094 1095 if (riscv_cpu_is_32bit(env)) { 1096 env->htimedelta = deposit64(env->htimedelta, 0, 32, (uint64_t)val); 1097 } else { 1098 env->htimedelta = val; 1099 } 1100 return 0; 1101 } 1102 1103 static int read_htimedeltah(CPURISCVState *env, int csrno, target_ulong *val) 1104 { 1105 if (!env->rdtime_fn) { 1106 return -RISCV_EXCP_ILLEGAL_INST; 1107 } 1108 1109 *val = env->htimedelta >> 32; 1110 return 0; 1111 } 1112 1113 static int write_htimedeltah(CPURISCVState *env, int csrno, target_ulong val) 1114 { 1115 if (!env->rdtime_fn) { 1116 return -RISCV_EXCP_ILLEGAL_INST; 1117 } 1118 1119 env->htimedelta = deposit64(env->htimedelta, 32, 32, (uint64_t)val); 1120 return 0; 1121 } 1122 1123 /* Virtual CSR Registers */ 1124 static int read_vsstatus(CPURISCVState *env, int csrno, target_ulong *val) 1125 { 1126 *val = env->vsstatus; 1127 return 0; 1128 } 1129 1130 static int write_vsstatus(CPURISCVState *env, int csrno, target_ulong val) 1131 { 1132 uint64_t mask = (target_ulong)-1; 1133 env->vsstatus = (env->vsstatus & ~mask) | (uint64_t)val; 1134 return 0; 1135 } 1136 1137 static int read_vstvec(CPURISCVState *env, int csrno, target_ulong *val) 1138 { 1139 *val = env->vstvec; 1140 return 0; 1141 } 1142 1143 static int write_vstvec(CPURISCVState *env, int csrno, target_ulong val) 1144 { 1145 env->vstvec = val; 1146 return 0; 1147 } 1148 1149 static int read_vsscratch(CPURISCVState *env, int csrno, target_ulong *val) 1150 { 1151 *val = env->vsscratch; 1152 return 0; 1153 } 1154 1155 static int write_vsscratch(CPURISCVState *env, int csrno, target_ulong val) 1156 { 1157 env->vsscratch = val; 1158 return 0; 1159 } 1160 1161 static int read_vsepc(CPURISCVState *env, int csrno, target_ulong *val) 1162 { 1163 *val = env->vsepc; 1164 return 0; 1165 } 1166 1167 static int write_vsepc(CPURISCVState *env, int csrno, target_ulong val) 1168 { 1169 env->vsepc = val; 1170 return 0; 1171 } 1172 1173 static int read_vscause(CPURISCVState *env, int csrno, target_ulong *val) 1174 { 1175 *val = env->vscause; 1176 return 0; 1177 } 1178 1179 static int write_vscause(CPURISCVState *env, int csrno, target_ulong val) 1180 { 1181 env->vscause = val; 1182 return 0; 1183 } 1184 1185 static int read_vstval(CPURISCVState *env, int csrno, target_ulong *val) 1186 { 1187 *val = env->vstval; 1188 return 0; 1189 } 1190 1191 static int write_vstval(CPURISCVState *env, int csrno, target_ulong val) 1192 { 1193 env->vstval = val; 1194 return 0; 1195 } 1196 1197 static int read_vsatp(CPURISCVState *env, int csrno, target_ulong *val) 1198 { 1199 *val = env->vsatp; 1200 return 0; 1201 } 1202 1203 static int write_vsatp(CPURISCVState *env, int csrno, target_ulong val) 1204 { 1205 env->vsatp = val; 1206 return 0; 1207 } 1208 1209 static int read_mtval2(CPURISCVState *env, int csrno, target_ulong *val) 1210 { 1211 *val = env->mtval2; 1212 return 0; 1213 } 1214 1215 static int write_mtval2(CPURISCVState *env, int csrno, target_ulong val) 1216 { 1217 env->mtval2 = val; 1218 return 0; 1219 } 1220 1221 static int read_mtinst(CPURISCVState *env, int csrno, target_ulong *val) 1222 { 1223 *val = env->mtinst; 1224 return 0; 1225 } 1226 1227 static int write_mtinst(CPURISCVState *env, int csrno, target_ulong val) 1228 { 1229 env->mtinst = val; 1230 return 0; 1231 } 1232 1233 /* Physical Memory Protection */ 1234 static int read_pmpcfg(CPURISCVState *env, int csrno, target_ulong *val) 1235 { 1236 *val = pmpcfg_csr_read(env, csrno - CSR_PMPCFG0); 1237 return 0; 1238 } 1239 1240 static int write_pmpcfg(CPURISCVState *env, int csrno, target_ulong val) 1241 { 1242 pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val); 1243 return 0; 1244 } 1245 1246 static int read_pmpaddr(CPURISCVState *env, int csrno, target_ulong *val) 1247 { 1248 *val = pmpaddr_csr_read(env, csrno - CSR_PMPADDR0); 1249 return 0; 1250 } 1251 1252 static int write_pmpaddr(CPURISCVState *env, int csrno, target_ulong val) 1253 { 1254 pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val); 1255 return 0; 1256 } 1257 1258 #endif 1259 1260 /* 1261 * riscv_csrrw - read and/or update control and status register 1262 * 1263 * csrr <-> riscv_csrrw(env, csrno, ret_value, 0, 0); 1264 * csrrw <-> riscv_csrrw(env, csrno, ret_value, value, -1); 1265 * csrrs <-> riscv_csrrw(env, csrno, ret_value, -1, value); 1266 * csrrc <-> riscv_csrrw(env, csrno, ret_value, 0, value); 1267 */ 1268 1269 int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, 1270 target_ulong new_value, target_ulong write_mask) 1271 { 1272 int ret; 1273 target_ulong old_value; 1274 RISCVCPU *cpu = env_archcpu(env); 1275 1276 /* check privileges and return -1 if check fails */ 1277 #if !defined(CONFIG_USER_ONLY) 1278 int effective_priv = env->priv; 1279 int read_only = get_field(csrno, 0xC00) == 3; 1280 1281 if (riscv_has_ext(env, RVH) && 1282 env->priv == PRV_S && 1283 !riscv_cpu_virt_enabled(env)) { 1284 /* 1285 * We are in S mode without virtualisation, therefore we are in HS Mode. 1286 * Add 1 to the effective privledge level to allow us to access the 1287 * Hypervisor CSRs. 1288 */ 1289 effective_priv++; 1290 } 1291 1292 if ((write_mask && read_only) || 1293 (!env->debugger && (effective_priv < get_field(csrno, 0x300)))) { 1294 return -RISCV_EXCP_ILLEGAL_INST; 1295 } 1296 #endif 1297 1298 /* ensure the CSR extension is enabled. */ 1299 if (!cpu->cfg.ext_icsr) { 1300 return -RISCV_EXCP_ILLEGAL_INST; 1301 } 1302 1303 /* check predicate */ 1304 if (!csr_ops[csrno].predicate) { 1305 return -RISCV_EXCP_ILLEGAL_INST; 1306 } 1307 ret = csr_ops[csrno].predicate(env, csrno); 1308 if (ret != RISCV_EXCP_NONE) { 1309 return -ret; 1310 } 1311 1312 /* execute combined read/write operation if it exists */ 1313 if (csr_ops[csrno].op) { 1314 return csr_ops[csrno].op(env, csrno, ret_value, new_value, write_mask); 1315 } 1316 1317 /* if no accessor exists then return failure */ 1318 if (!csr_ops[csrno].read) { 1319 return -RISCV_EXCP_ILLEGAL_INST; 1320 } 1321 1322 /* read old value */ 1323 ret = csr_ops[csrno].read(env, csrno, &old_value); 1324 if (ret < 0) { 1325 return ret; 1326 } 1327 1328 /* write value if writable and write mask set, otherwise drop writes */ 1329 if (write_mask) { 1330 new_value = (old_value & ~write_mask) | (new_value & write_mask); 1331 if (csr_ops[csrno].write) { 1332 ret = csr_ops[csrno].write(env, csrno, new_value); 1333 if (ret < 0) { 1334 return ret; 1335 } 1336 } 1337 } 1338 1339 /* return old value */ 1340 if (ret_value) { 1341 *ret_value = old_value; 1342 } 1343 1344 return 0; 1345 } 1346 1347 /* 1348 * Debugger support. If not in user mode, set env->debugger before the 1349 * riscv_csrrw call and clear it after the call. 1350 */ 1351 int riscv_csrrw_debug(CPURISCVState *env, int csrno, target_ulong *ret_value, 1352 target_ulong new_value, target_ulong write_mask) 1353 { 1354 int ret; 1355 #if !defined(CONFIG_USER_ONLY) 1356 env->debugger = true; 1357 #endif 1358 ret = riscv_csrrw(env, csrno, ret_value, new_value, write_mask); 1359 #if !defined(CONFIG_USER_ONLY) 1360 env->debugger = false; 1361 #endif 1362 return ret; 1363 } 1364 1365 /* Control and Status Register function table */ 1366 riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { 1367 /* User Floating-Point CSRs */ 1368 [CSR_FFLAGS] = { "fflags", fs, read_fflags, write_fflags }, 1369 [CSR_FRM] = { "frm", fs, read_frm, write_frm }, 1370 [CSR_FCSR] = { "fcsr", fs, read_fcsr, write_fcsr }, 1371 /* Vector CSRs */ 1372 [CSR_VSTART] = { "vstart", vs, read_vstart, write_vstart }, 1373 [CSR_VXSAT] = { "vxsat", vs, read_vxsat, write_vxsat }, 1374 [CSR_VXRM] = { "vxrm", vs, read_vxrm, write_vxrm }, 1375 [CSR_VL] = { "vl", vs, read_vl }, 1376 [CSR_VTYPE] = { "vtype", vs, read_vtype }, 1377 /* User Timers and Counters */ 1378 [CSR_CYCLE] = { "cycle", ctr, read_instret }, 1379 [CSR_INSTRET] = { "instret", ctr, read_instret }, 1380 [CSR_CYCLEH] = { "cycleh", ctr32, read_instreth }, 1381 [CSR_INSTRETH] = { "instreth", ctr32, read_instreth }, 1382 1383 /* 1384 * In privileged mode, the monitor will have to emulate TIME CSRs only if 1385 * rdtime callback is not provided by machine/platform emulation. 1386 */ 1387 [CSR_TIME] = { "time", ctr, read_time }, 1388 [CSR_TIMEH] = { "timeh", ctr32, read_timeh }, 1389 1390 #if !defined(CONFIG_USER_ONLY) 1391 /* Machine Timers and Counters */ 1392 [CSR_MCYCLE] = { "mcycle", any, read_instret }, 1393 [CSR_MINSTRET] = { "minstret", any, read_instret }, 1394 [CSR_MCYCLEH] = { "mcycleh", any32, read_instreth }, 1395 [CSR_MINSTRETH] = { "minstreth", any32, read_instreth }, 1396 1397 /* Machine Information Registers */ 1398 [CSR_MVENDORID] = { "mvendorid", any, read_zero }, 1399 [CSR_MARCHID] = { "marchid", any, read_zero }, 1400 [CSR_MIMPID] = { "mimpid", any, read_zero }, 1401 [CSR_MHARTID] = { "mhartid", any, read_mhartid }, 1402 1403 /* Machine Trap Setup */ 1404 [CSR_MSTATUS] = { "mstatus", any, read_mstatus, write_mstatus }, 1405 [CSR_MISA] = { "misa", any, read_misa, write_misa }, 1406 [CSR_MIDELEG] = { "mideleg", any, read_mideleg, write_mideleg }, 1407 [CSR_MEDELEG] = { "medeleg", any, read_medeleg, write_medeleg }, 1408 [CSR_MIE] = { "mie", any, read_mie, write_mie }, 1409 [CSR_MTVEC] = { "mtvec", any, read_mtvec, write_mtvec }, 1410 [CSR_MCOUNTEREN] = { "mcounteren", any, read_mcounteren, write_mcounteren }, 1411 1412 [CSR_MSTATUSH] = { "mstatush", any32, read_mstatush, write_mstatush }, 1413 1414 /* Machine Trap Handling */ 1415 [CSR_MSCRATCH] = { "mscratch", any, read_mscratch, write_mscratch }, 1416 [CSR_MEPC] = { "mepc", any, read_mepc, write_mepc }, 1417 [CSR_MCAUSE] = { "mcause", any, read_mcause, write_mcause }, 1418 [CSR_MTVAL] = { "mtval", any, read_mtval, write_mtval }, 1419 [CSR_MIP] = { "mip", any, NULL, NULL, rmw_mip }, 1420 1421 /* Supervisor Trap Setup */ 1422 [CSR_SSTATUS] = { "sstatus", smode, read_sstatus, write_sstatus }, 1423 [CSR_SIE] = { "sie", smode, read_sie, write_sie }, 1424 [CSR_STVEC] = { "stvec", smode, read_stvec, write_stvec }, 1425 [CSR_SCOUNTEREN] = { "scounteren", smode, read_scounteren, write_scounteren }, 1426 1427 /* Supervisor Trap Handling */ 1428 [CSR_SSCRATCH] = { "sscratch", smode, read_sscratch, write_sscratch }, 1429 [CSR_SEPC] = { "sepc", smode, read_sepc, write_sepc }, 1430 [CSR_SCAUSE] = { "scause", smode, read_scause, write_scause }, 1431 [CSR_STVAL] = { "stval", smode, read_stval, write_stval }, 1432 [CSR_SIP] = { "sip", smode, NULL, NULL, rmw_sip }, 1433 1434 /* Supervisor Protection and Translation */ 1435 [CSR_SATP] = { "satp", smode, read_satp, write_satp }, 1436 1437 [CSR_HSTATUS] = { "hstatus", hmode, read_hstatus, write_hstatus }, 1438 [CSR_HEDELEG] = { "hedeleg", hmode, read_hedeleg, write_hedeleg }, 1439 [CSR_HIDELEG] = { "hideleg", hmode, read_hideleg, write_hideleg }, 1440 [CSR_HVIP] = { "hvip", hmode, NULL, NULL, rmw_hvip }, 1441 [CSR_HIP] = { "hip", hmode, NULL, NULL, rmw_hip }, 1442 [CSR_HIE] = { "hie", hmode, read_hie, write_hie }, 1443 [CSR_HCOUNTEREN] = { "hcounteren", hmode, read_hcounteren, write_hcounteren }, 1444 [CSR_HGEIE] = { "hgeie", hmode, read_hgeie, write_hgeie }, 1445 [CSR_HTVAL] = { "htval", hmode, read_htval, write_htval }, 1446 [CSR_HTINST] = { "htinst", hmode, read_htinst, write_htinst }, 1447 [CSR_HGEIP] = { "hgeip", hmode, read_hgeip, write_hgeip }, 1448 [CSR_HGATP] = { "hgatp", hmode, read_hgatp, write_hgatp }, 1449 [CSR_HTIMEDELTA] = { "htimedelta", hmode, read_htimedelta, write_htimedelta }, 1450 [CSR_HTIMEDELTAH] = { "htimedeltah", hmode32, read_htimedeltah, write_htimedeltah }, 1451 1452 [CSR_VSSTATUS] = { "vsstatus", hmode, read_vsstatus, write_vsstatus }, 1453 [CSR_VSIP] = { "vsip", hmode, NULL, NULL, rmw_vsip }, 1454 [CSR_VSIE] = { "vsie", hmode, read_vsie, write_vsie }, 1455 [CSR_VSTVEC] = { "vstvec", hmode, read_vstvec, write_vstvec }, 1456 [CSR_VSSCRATCH] = { "vsscratch", hmode, read_vsscratch, write_vsscratch }, 1457 [CSR_VSEPC] = { "vsepc", hmode, read_vsepc, write_vsepc }, 1458 [CSR_VSCAUSE] = { "vscause", hmode, read_vscause, write_vscause }, 1459 [CSR_VSTVAL] = { "vstval", hmode, read_vstval, write_vstval }, 1460 [CSR_VSATP] = { "vsatp", hmode, read_vsatp, write_vsatp }, 1461 1462 [CSR_MTVAL2] = { "mtval2", hmode, read_mtval2, write_mtval2 }, 1463 [CSR_MTINST] = { "mtinst", hmode, read_mtinst, write_mtinst }, 1464 1465 /* Physical Memory Protection */ 1466 [CSR_PMPCFG0] = { "pmpcfg0", pmp, read_pmpcfg, write_pmpcfg }, 1467 [CSR_PMPCFG1] = { "pmpcfg1", pmp, read_pmpcfg, write_pmpcfg }, 1468 [CSR_PMPCFG2] = { "pmpcfg2", pmp, read_pmpcfg, write_pmpcfg }, 1469 [CSR_PMPCFG3] = { "pmpcfg3", pmp, read_pmpcfg, write_pmpcfg }, 1470 [CSR_PMPADDR0] = { "pmpaddr0", pmp, read_pmpaddr, write_pmpaddr }, 1471 [CSR_PMPADDR1] = { "pmpaddr1", pmp, read_pmpaddr, write_pmpaddr }, 1472 [CSR_PMPADDR2] = { "pmpaddr2", pmp, read_pmpaddr, write_pmpaddr }, 1473 [CSR_PMPADDR3] = { "pmpaddr3", pmp, read_pmpaddr, write_pmpaddr }, 1474 [CSR_PMPADDR4] = { "pmpaddr4", pmp, read_pmpaddr, write_pmpaddr }, 1475 [CSR_PMPADDR5] = { "pmpaddr5", pmp, read_pmpaddr, write_pmpaddr }, 1476 [CSR_PMPADDR6] = { "pmpaddr6", pmp, read_pmpaddr, write_pmpaddr }, 1477 [CSR_PMPADDR7] = { "pmpaddr7", pmp, read_pmpaddr, write_pmpaddr }, 1478 [CSR_PMPADDR8] = { "pmpaddr8", pmp, read_pmpaddr, write_pmpaddr }, 1479 [CSR_PMPADDR9] = { "pmpaddr9", pmp, read_pmpaddr, write_pmpaddr }, 1480 [CSR_PMPADDR10] = { "pmpaddr10", pmp, read_pmpaddr, write_pmpaddr }, 1481 [CSR_PMPADDR11] = { "pmpaddr11", pmp, read_pmpaddr, write_pmpaddr }, 1482 [CSR_PMPADDR12] = { "pmpaddr12", pmp, read_pmpaddr, write_pmpaddr }, 1483 [CSR_PMPADDR13] = { "pmpaddr13", pmp, read_pmpaddr, write_pmpaddr }, 1484 [CSR_PMPADDR14] = { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr }, 1485 [CSR_PMPADDR15] = { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr }, 1486 1487 /* Performance Counters */ 1488 [CSR_HPMCOUNTER3] = { "hpmcounter3", ctr, read_zero }, 1489 [CSR_HPMCOUNTER4] = { "hpmcounter4", ctr, read_zero }, 1490 [CSR_HPMCOUNTER5] = { "hpmcounter5", ctr, read_zero }, 1491 [CSR_HPMCOUNTER6] = { "hpmcounter6", ctr, read_zero }, 1492 [CSR_HPMCOUNTER7] = { "hpmcounter7", ctr, read_zero }, 1493 [CSR_HPMCOUNTER8] = { "hpmcounter8", ctr, read_zero }, 1494 [CSR_HPMCOUNTER9] = { "hpmcounter9", ctr, read_zero }, 1495 [CSR_HPMCOUNTER10] = { "hpmcounter10", ctr, read_zero }, 1496 [CSR_HPMCOUNTER11] = { "hpmcounter11", ctr, read_zero }, 1497 [CSR_HPMCOUNTER12] = { "hpmcounter12", ctr, read_zero }, 1498 [CSR_HPMCOUNTER13] = { "hpmcounter13", ctr, read_zero }, 1499 [CSR_HPMCOUNTER14] = { "hpmcounter14", ctr, read_zero }, 1500 [CSR_HPMCOUNTER15] = { "hpmcounter15", ctr, read_zero }, 1501 [CSR_HPMCOUNTER16] = { "hpmcounter16", ctr, read_zero }, 1502 [CSR_HPMCOUNTER17] = { "hpmcounter17", ctr, read_zero }, 1503 [CSR_HPMCOUNTER18] = { "hpmcounter18", ctr, read_zero }, 1504 [CSR_HPMCOUNTER19] = { "hpmcounter19", ctr, read_zero }, 1505 [CSR_HPMCOUNTER20] = { "hpmcounter20", ctr, read_zero }, 1506 [CSR_HPMCOUNTER21] = { "hpmcounter21", ctr, read_zero }, 1507 [CSR_HPMCOUNTER22] = { "hpmcounter22", ctr, read_zero }, 1508 [CSR_HPMCOUNTER23] = { "hpmcounter23", ctr, read_zero }, 1509 [CSR_HPMCOUNTER24] = { "hpmcounter24", ctr, read_zero }, 1510 [CSR_HPMCOUNTER25] = { "hpmcounter25", ctr, read_zero }, 1511 [CSR_HPMCOUNTER26] = { "hpmcounter26", ctr, read_zero }, 1512 [CSR_HPMCOUNTER27] = { "hpmcounter27", ctr, read_zero }, 1513 [CSR_HPMCOUNTER28] = { "hpmcounter28", ctr, read_zero }, 1514 [CSR_HPMCOUNTER29] = { "hpmcounter29", ctr, read_zero }, 1515 [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_zero }, 1516 [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_zero }, 1517 1518 [CSR_MHPMCOUNTER3] = { "mhpmcounter3", any, read_zero }, 1519 [CSR_MHPMCOUNTER4] = { "mhpmcounter4", any, read_zero }, 1520 [CSR_MHPMCOUNTER5] = { "mhpmcounter5", any, read_zero }, 1521 [CSR_MHPMCOUNTER6] = { "mhpmcounter6", any, read_zero }, 1522 [CSR_MHPMCOUNTER7] = { "mhpmcounter7", any, read_zero }, 1523 [CSR_MHPMCOUNTER8] = { "mhpmcounter8", any, read_zero }, 1524 [CSR_MHPMCOUNTER9] = { "mhpmcounter9", any, read_zero }, 1525 [CSR_MHPMCOUNTER10] = { "mhpmcounter10", any, read_zero }, 1526 [CSR_MHPMCOUNTER11] = { "mhpmcounter11", any, read_zero }, 1527 [CSR_MHPMCOUNTER12] = { "mhpmcounter12", any, read_zero }, 1528 [CSR_MHPMCOUNTER13] = { "mhpmcounter13", any, read_zero }, 1529 [CSR_MHPMCOUNTER14] = { "mhpmcounter14", any, read_zero }, 1530 [CSR_MHPMCOUNTER15] = { "mhpmcounter15", any, read_zero }, 1531 [CSR_MHPMCOUNTER16] = { "mhpmcounter16", any, read_zero }, 1532 [CSR_MHPMCOUNTER17] = { "mhpmcounter17", any, read_zero }, 1533 [CSR_MHPMCOUNTER18] = { "mhpmcounter18", any, read_zero }, 1534 [CSR_MHPMCOUNTER19] = { "mhpmcounter19", any, read_zero }, 1535 [CSR_MHPMCOUNTER20] = { "mhpmcounter20", any, read_zero }, 1536 [CSR_MHPMCOUNTER21] = { "mhpmcounter21", any, read_zero }, 1537 [CSR_MHPMCOUNTER22] = { "mhpmcounter22", any, read_zero }, 1538 [CSR_MHPMCOUNTER23] = { "mhpmcounter23", any, read_zero }, 1539 [CSR_MHPMCOUNTER24] = { "mhpmcounter24", any, read_zero }, 1540 [CSR_MHPMCOUNTER25] = { "mhpmcounter25", any, read_zero }, 1541 [CSR_MHPMCOUNTER26] = { "mhpmcounter26", any, read_zero }, 1542 [CSR_MHPMCOUNTER27] = { "mhpmcounter27", any, read_zero }, 1543 [CSR_MHPMCOUNTER28] = { "mhpmcounter28", any, read_zero }, 1544 [CSR_MHPMCOUNTER29] = { "mhpmcounter29", any, read_zero }, 1545 [CSR_MHPMCOUNTER30] = { "mhpmcounter30", any, read_zero }, 1546 [CSR_MHPMCOUNTER31] = { "mhpmcounter31", any, read_zero }, 1547 1548 [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, 1549 [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero }, 1550 [CSR_MHPMEVENT5] = { "mhpmevent5", any, read_zero }, 1551 [CSR_MHPMEVENT6] = { "mhpmevent6", any, read_zero }, 1552 [CSR_MHPMEVENT7] = { "mhpmevent7", any, read_zero }, 1553 [CSR_MHPMEVENT8] = { "mhpmevent8", any, read_zero }, 1554 [CSR_MHPMEVENT9] = { "mhpmevent9", any, read_zero }, 1555 [CSR_MHPMEVENT10] = { "mhpmevent10", any, read_zero }, 1556 [CSR_MHPMEVENT11] = { "mhpmevent11", any, read_zero }, 1557 [CSR_MHPMEVENT12] = { "mhpmevent12", any, read_zero }, 1558 [CSR_MHPMEVENT13] = { "mhpmevent13", any, read_zero }, 1559 [CSR_MHPMEVENT14] = { "mhpmevent14", any, read_zero }, 1560 [CSR_MHPMEVENT15] = { "mhpmevent15", any, read_zero }, 1561 [CSR_MHPMEVENT16] = { "mhpmevent16", any, read_zero }, 1562 [CSR_MHPMEVENT17] = { "mhpmevent17", any, read_zero }, 1563 [CSR_MHPMEVENT18] = { "mhpmevent18", any, read_zero }, 1564 [CSR_MHPMEVENT19] = { "mhpmevent19", any, read_zero }, 1565 [CSR_MHPMEVENT20] = { "mhpmevent20", any, read_zero }, 1566 [CSR_MHPMEVENT21] = { "mhpmevent21", any, read_zero }, 1567 [CSR_MHPMEVENT22] = { "mhpmevent22", any, read_zero }, 1568 [CSR_MHPMEVENT23] = { "mhpmevent23", any, read_zero }, 1569 [CSR_MHPMEVENT24] = { "mhpmevent24", any, read_zero }, 1570 [CSR_MHPMEVENT25] = { "mhpmevent25", any, read_zero }, 1571 [CSR_MHPMEVENT26] = { "mhpmevent26", any, read_zero }, 1572 [CSR_MHPMEVENT27] = { "mhpmevent27", any, read_zero }, 1573 [CSR_MHPMEVENT28] = { "mhpmevent28", any, read_zero }, 1574 [CSR_MHPMEVENT29] = { "mhpmevent29", any, read_zero }, 1575 [CSR_MHPMEVENT30] = { "mhpmevent30", any, read_zero }, 1576 [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_zero }, 1577 1578 [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_zero }, 1579 [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_zero }, 1580 [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_zero }, 1581 [CSR_HPMCOUNTER6H] = { "hpmcounter6h", ctr32, read_zero }, 1582 [CSR_HPMCOUNTER7H] = { "hpmcounter7h", ctr32, read_zero }, 1583 [CSR_HPMCOUNTER8H] = { "hpmcounter8h", ctr32, read_zero }, 1584 [CSR_HPMCOUNTER9H] = { "hpmcounter9h", ctr32, read_zero }, 1585 [CSR_HPMCOUNTER10H] = { "hpmcounter10h", ctr32, read_zero }, 1586 [CSR_HPMCOUNTER11H] = { "hpmcounter11h", ctr32, read_zero }, 1587 [CSR_HPMCOUNTER12H] = { "hpmcounter12h", ctr32, read_zero }, 1588 [CSR_HPMCOUNTER13H] = { "hpmcounter13h", ctr32, read_zero }, 1589 [CSR_HPMCOUNTER14H] = { "hpmcounter14h", ctr32, read_zero }, 1590 [CSR_HPMCOUNTER15H] = { "hpmcounter15h", ctr32, read_zero }, 1591 [CSR_HPMCOUNTER16H] = { "hpmcounter16h", ctr32, read_zero }, 1592 [CSR_HPMCOUNTER17H] = { "hpmcounter17h", ctr32, read_zero }, 1593 [CSR_HPMCOUNTER18H] = { "hpmcounter18h", ctr32, read_zero }, 1594 [CSR_HPMCOUNTER19H] = { "hpmcounter19h", ctr32, read_zero }, 1595 [CSR_HPMCOUNTER20H] = { "hpmcounter20h", ctr32, read_zero }, 1596 [CSR_HPMCOUNTER21H] = { "hpmcounter21h", ctr32, read_zero }, 1597 [CSR_HPMCOUNTER22H] = { "hpmcounter22h", ctr32, read_zero }, 1598 [CSR_HPMCOUNTER23H] = { "hpmcounter23h", ctr32, read_zero }, 1599 [CSR_HPMCOUNTER24H] = { "hpmcounter24h", ctr32, read_zero }, 1600 [CSR_HPMCOUNTER25H] = { "hpmcounter25h", ctr32, read_zero }, 1601 [CSR_HPMCOUNTER26H] = { "hpmcounter26h", ctr32, read_zero }, 1602 [CSR_HPMCOUNTER27H] = { "hpmcounter27h", ctr32, read_zero }, 1603 [CSR_HPMCOUNTER28H] = { "hpmcounter28h", ctr32, read_zero }, 1604 [CSR_HPMCOUNTER29H] = { "hpmcounter29h", ctr32, read_zero }, 1605 [CSR_HPMCOUNTER30H] = { "hpmcounter30h", ctr32, read_zero }, 1606 [CSR_HPMCOUNTER31H] = { "hpmcounter31h", ctr32, read_zero }, 1607 1608 [CSR_MHPMCOUNTER3H] = { "mhpmcounter3h", any32, read_zero }, 1609 [CSR_MHPMCOUNTER4H] = { "mhpmcounter4h", any32, read_zero }, 1610 [CSR_MHPMCOUNTER5H] = { "mhpmcounter5h", any32, read_zero }, 1611 [CSR_MHPMCOUNTER6H] = { "mhpmcounter6h", any32, read_zero }, 1612 [CSR_MHPMCOUNTER7H] = { "mhpmcounter7h", any32, read_zero }, 1613 [CSR_MHPMCOUNTER8H] = { "mhpmcounter8h", any32, read_zero }, 1614 [CSR_MHPMCOUNTER9H] = { "mhpmcounter9h", any32, read_zero }, 1615 [CSR_MHPMCOUNTER10H] = { "mhpmcounter10h", any32, read_zero }, 1616 [CSR_MHPMCOUNTER11H] = { "mhpmcounter11h", any32, read_zero }, 1617 [CSR_MHPMCOUNTER12H] = { "mhpmcounter12h", any32, read_zero }, 1618 [CSR_MHPMCOUNTER13H] = { "mhpmcounter13h", any32, read_zero }, 1619 [CSR_MHPMCOUNTER14H] = { "mhpmcounter14h", any32, read_zero }, 1620 [CSR_MHPMCOUNTER15H] = { "mhpmcounter15h", any32, read_zero }, 1621 [CSR_MHPMCOUNTER16H] = { "mhpmcounter16h", any32, read_zero }, 1622 [CSR_MHPMCOUNTER17H] = { "mhpmcounter17h", any32, read_zero }, 1623 [CSR_MHPMCOUNTER18H] = { "mhpmcounter18h", any32, read_zero }, 1624 [CSR_MHPMCOUNTER19H] = { "mhpmcounter19h", any32, read_zero }, 1625 [CSR_MHPMCOUNTER20H] = { "mhpmcounter20h", any32, read_zero }, 1626 [CSR_MHPMCOUNTER21H] = { "mhpmcounter21h", any32, read_zero }, 1627 [CSR_MHPMCOUNTER22H] = { "mhpmcounter22h", any32, read_zero }, 1628 [CSR_MHPMCOUNTER23H] = { "mhpmcounter23h", any32, read_zero }, 1629 [CSR_MHPMCOUNTER24H] = { "mhpmcounter24h", any32, read_zero }, 1630 [CSR_MHPMCOUNTER25H] = { "mhpmcounter25h", any32, read_zero }, 1631 [CSR_MHPMCOUNTER26H] = { "mhpmcounter26h", any32, read_zero }, 1632 [CSR_MHPMCOUNTER27H] = { "mhpmcounter27h", any32, read_zero }, 1633 [CSR_MHPMCOUNTER28H] = { "mhpmcounter28h", any32, read_zero }, 1634 [CSR_MHPMCOUNTER29H] = { "mhpmcounter29h", any32, read_zero }, 1635 [CSR_MHPMCOUNTER30H] = { "mhpmcounter30h", any32, read_zero }, 1636 [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", any32, read_zero }, 1637 #endif /* !CONFIG_USER_ONLY */ 1638 }; 1639