1 /* 2 * RISC-V CPU helpers for qemu. 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 "exec/exec-all.h" 24 #include "tcg-op.h" 25 #include "trace.h" 26 27 int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch) 28 { 29 #ifdef CONFIG_USER_ONLY 30 return 0; 31 #else 32 return env->priv; 33 #endif 34 } 35 36 #ifndef CONFIG_USER_ONLY 37 static int riscv_cpu_local_irq_pending(CPURISCVState *env) 38 { 39 target_ulong mstatus_mie = get_field(env->mstatus, MSTATUS_MIE); 40 target_ulong mstatus_sie = get_field(env->mstatus, MSTATUS_SIE); 41 target_ulong pending = atomic_read(&env->mip) & env->mie; 42 target_ulong mie = env->priv < PRV_M || (env->priv == PRV_M && mstatus_mie); 43 target_ulong sie = env->priv < PRV_S || (env->priv == PRV_S && mstatus_sie); 44 target_ulong irqs = (pending & ~env->mideleg & -mie) | 45 (pending & env->mideleg & -sie); 46 47 if (irqs) { 48 return ctz64(irqs); /* since non-zero */ 49 } else { 50 return EXCP_NONE; /* indicates no pending interrupt */ 51 } 52 } 53 #endif 54 55 bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 56 { 57 #if !defined(CONFIG_USER_ONLY) 58 if (interrupt_request & CPU_INTERRUPT_HARD) { 59 RISCVCPU *cpu = RISCV_CPU(cs); 60 CPURISCVState *env = &cpu->env; 61 int interruptno = riscv_cpu_local_irq_pending(env); 62 if (interruptno >= 0) { 63 cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno; 64 riscv_cpu_do_interrupt(cs); 65 return true; 66 } 67 } 68 #endif 69 return false; 70 } 71 72 #if !defined(CONFIG_USER_ONLY) 73 74 int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts) 75 { 76 CPURISCVState *env = &cpu->env; 77 if (env->miclaim & interrupts) { 78 return -1; 79 } else { 80 env->miclaim |= interrupts; 81 return 0; 82 } 83 } 84 85 struct CpuAsyncInfo { 86 uint32_t new_mip; 87 }; 88 89 static void riscv_cpu_update_mip_irqs_async(CPUState *target_cpu_state, 90 run_on_cpu_data data) 91 { 92 struct CpuAsyncInfo *info = (struct CpuAsyncInfo *) data.host_ptr; 93 94 if (info->new_mip) { 95 cpu_interrupt(target_cpu_state, CPU_INTERRUPT_HARD); 96 } else { 97 cpu_reset_interrupt(target_cpu_state, CPU_INTERRUPT_HARD); 98 } 99 100 g_free(info); 101 } 102 103 uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value) 104 { 105 CPURISCVState *env = &cpu->env; 106 CPUState *cs = CPU(cpu); 107 struct CpuAsyncInfo *info; 108 uint32_t old, new, cmp = atomic_read(&env->mip); 109 110 do { 111 old = cmp; 112 new = (old & ~mask) | (value & mask); 113 cmp = atomic_cmpxchg(&env->mip, old, new); 114 } while (old != cmp); 115 116 info = g_new(struct CpuAsyncInfo, 1); 117 info->new_mip = new; 118 119 async_run_on_cpu(cs, riscv_cpu_update_mip_irqs_async, 120 RUN_ON_CPU_HOST_PTR(info)); 121 122 return old; 123 } 124 125 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) 126 { 127 if (newpriv > PRV_M) { 128 g_assert_not_reached(); 129 } 130 if (newpriv == PRV_H) { 131 newpriv = PRV_U; 132 } 133 /* tlb_flush is unnecessary as mode is contained in mmu_idx */ 134 env->priv = newpriv; 135 } 136 137 /* get_physical_address - get the physical address for this virtual address 138 * 139 * Do a page table walk to obtain the physical address corresponding to a 140 * virtual address. Returns 0 if the translation was successful 141 * 142 * Adapted from Spike's mmu_t::translate and mmu_t::walk 143 * 144 */ 145 static int get_physical_address(CPURISCVState *env, hwaddr *physical, 146 int *prot, target_ulong addr, 147 int access_type, int mmu_idx) 148 { 149 /* NOTE: the env->pc value visible here will not be 150 * correct, but the value visible to the exception handler 151 * (riscv_cpu_do_interrupt) is correct */ 152 153 int mode = mmu_idx; 154 155 if (mode == PRV_M && access_type != MMU_INST_FETCH) { 156 if (get_field(env->mstatus, MSTATUS_MPRV)) { 157 mode = get_field(env->mstatus, MSTATUS_MPP); 158 } 159 } 160 161 if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) { 162 *physical = addr; 163 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 164 return TRANSLATE_SUCCESS; 165 } 166 167 *prot = 0; 168 169 target_ulong base; 170 int levels, ptidxbits, ptesize, vm, sum; 171 int mxr = get_field(env->mstatus, MSTATUS_MXR); 172 173 if (env->priv_ver >= PRIV_VERSION_1_10_0) { 174 base = get_field(env->satp, SATP_PPN) << PGSHIFT; 175 sum = get_field(env->mstatus, MSTATUS_SUM); 176 vm = get_field(env->satp, SATP_MODE); 177 switch (vm) { 178 case VM_1_10_SV32: 179 levels = 2; ptidxbits = 10; ptesize = 4; break; 180 case VM_1_10_SV39: 181 levels = 3; ptidxbits = 9; ptesize = 8; break; 182 case VM_1_10_SV48: 183 levels = 4; ptidxbits = 9; ptesize = 8; break; 184 case VM_1_10_SV57: 185 levels = 5; ptidxbits = 9; ptesize = 8; break; 186 case VM_1_10_MBARE: 187 *physical = addr; 188 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 189 return TRANSLATE_SUCCESS; 190 default: 191 g_assert_not_reached(); 192 } 193 } else { 194 base = env->sptbr << PGSHIFT; 195 sum = !get_field(env->mstatus, MSTATUS_PUM); 196 vm = get_field(env->mstatus, MSTATUS_VM); 197 switch (vm) { 198 case VM_1_09_SV32: 199 levels = 2; ptidxbits = 10; ptesize = 4; break; 200 case VM_1_09_SV39: 201 levels = 3; ptidxbits = 9; ptesize = 8; break; 202 case VM_1_09_SV48: 203 levels = 4; ptidxbits = 9; ptesize = 8; break; 204 case VM_1_09_MBARE: 205 *physical = addr; 206 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 207 return TRANSLATE_SUCCESS; 208 default: 209 g_assert_not_reached(); 210 } 211 } 212 213 CPUState *cs = env_cpu(env); 214 int va_bits = PGSHIFT + levels * ptidxbits; 215 target_ulong mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1; 216 target_ulong masked_msbs = (addr >> (va_bits - 1)) & mask; 217 if (masked_msbs != 0 && masked_msbs != mask) { 218 return TRANSLATE_FAIL; 219 } 220 221 int ptshift = (levels - 1) * ptidxbits; 222 int i; 223 224 #if !TCG_OVERSIZED_GUEST 225 restart: 226 #endif 227 for (i = 0; i < levels; i++, ptshift -= ptidxbits) { 228 target_ulong idx = (addr >> (PGSHIFT + ptshift)) & 229 ((1 << ptidxbits) - 1); 230 231 /* check that physical address of PTE is legal */ 232 target_ulong pte_addr = base + idx * ptesize; 233 234 if (riscv_feature(env, RISCV_FEATURE_PMP) && 235 !pmp_hart_has_privs(env, pte_addr, sizeof(target_ulong), 236 1 << MMU_DATA_LOAD, PRV_S)) { 237 return TRANSLATE_PMP_FAIL; 238 } 239 #if defined(TARGET_RISCV32) 240 target_ulong pte = ldl_phys(cs->as, pte_addr); 241 #elif defined(TARGET_RISCV64) 242 target_ulong pte = ldq_phys(cs->as, pte_addr); 243 #endif 244 target_ulong ppn = pte >> PTE_PPN_SHIFT; 245 246 if (!(pte & PTE_V)) { 247 /* Invalid PTE */ 248 return TRANSLATE_FAIL; 249 } else if (!(pte & (PTE_R | PTE_W | PTE_X))) { 250 /* Inner PTE, continue walking */ 251 base = ppn << PGSHIFT; 252 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) { 253 /* Reserved leaf PTE flags: PTE_W */ 254 return TRANSLATE_FAIL; 255 } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) { 256 /* Reserved leaf PTE flags: PTE_W + PTE_X */ 257 return TRANSLATE_FAIL; 258 } else if ((pte & PTE_U) && ((mode != PRV_U) && 259 (!sum || access_type == MMU_INST_FETCH))) { 260 /* User PTE flags when not U mode and mstatus.SUM is not set, 261 or the access type is an instruction fetch */ 262 return TRANSLATE_FAIL; 263 } else if (!(pte & PTE_U) && (mode != PRV_S)) { 264 /* Supervisor PTE flags when not S mode */ 265 return TRANSLATE_FAIL; 266 } else if (ppn & ((1ULL << ptshift) - 1)) { 267 /* Misaligned PPN */ 268 return TRANSLATE_FAIL; 269 } else if (access_type == MMU_DATA_LOAD && !((pte & PTE_R) || 270 ((pte & PTE_X) && mxr))) { 271 /* Read access check failed */ 272 return TRANSLATE_FAIL; 273 } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) { 274 /* Write access check failed */ 275 return TRANSLATE_FAIL; 276 } else if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) { 277 /* Fetch access check failed */ 278 return TRANSLATE_FAIL; 279 } else { 280 /* if necessary, set accessed and dirty bits. */ 281 target_ulong updated_pte = pte | PTE_A | 282 (access_type == MMU_DATA_STORE ? PTE_D : 0); 283 284 /* Page table updates need to be atomic with MTTCG enabled */ 285 if (updated_pte != pte) { 286 /* 287 * - if accessed or dirty bits need updating, and the PTE is 288 * in RAM, then we do so atomically with a compare and swap. 289 * - if the PTE is in IO space or ROM, then it can't be updated 290 * and we return TRANSLATE_FAIL. 291 * - if the PTE changed by the time we went to update it, then 292 * it is no longer valid and we must re-walk the page table. 293 */ 294 MemoryRegion *mr; 295 hwaddr l = sizeof(target_ulong), addr1; 296 mr = address_space_translate(cs->as, pte_addr, 297 &addr1, &l, false, MEMTXATTRS_UNSPECIFIED); 298 if (memory_region_is_ram(mr)) { 299 target_ulong *pte_pa = 300 qemu_map_ram_ptr(mr->ram_block, addr1); 301 #if TCG_OVERSIZED_GUEST 302 /* MTTCG is not enabled on oversized TCG guests so 303 * page table updates do not need to be atomic */ 304 *pte_pa = pte = updated_pte; 305 #else 306 target_ulong old_pte = 307 atomic_cmpxchg(pte_pa, pte, updated_pte); 308 if (old_pte != pte) { 309 goto restart; 310 } else { 311 pte = updated_pte; 312 } 313 #endif 314 } else { 315 /* misconfigured PTE in ROM (AD bits are not preset) or 316 * PTE is in IO space and can't be updated atomically */ 317 return TRANSLATE_FAIL; 318 } 319 } 320 321 /* for superpage mappings, make a fake leaf PTE for the TLB's 322 benefit. */ 323 target_ulong vpn = addr >> PGSHIFT; 324 *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; 325 326 /* set permissions on the TLB entry */ 327 if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) { 328 *prot |= PAGE_READ; 329 } 330 if ((pte & PTE_X)) { 331 *prot |= PAGE_EXEC; 332 } 333 /* add write permission on stores or if the page is already dirty, 334 so that we TLB miss on later writes to update the dirty bit */ 335 if ((pte & PTE_W) && 336 (access_type == MMU_DATA_STORE || (pte & PTE_D))) { 337 *prot |= PAGE_WRITE; 338 } 339 return TRANSLATE_SUCCESS; 340 } 341 } 342 return TRANSLATE_FAIL; 343 } 344 345 static void raise_mmu_exception(CPURISCVState *env, target_ulong address, 346 MMUAccessType access_type, bool pmp_violation) 347 { 348 CPUState *cs = env_cpu(env); 349 int page_fault_exceptions = 350 (env->priv_ver >= PRIV_VERSION_1_10_0) && 351 get_field(env->satp, SATP_MODE) != VM_1_10_MBARE && 352 !pmp_violation; 353 switch (access_type) { 354 case MMU_INST_FETCH: 355 cs->exception_index = page_fault_exceptions ? 356 RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT; 357 break; 358 case MMU_DATA_LOAD: 359 cs->exception_index = page_fault_exceptions ? 360 RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT; 361 break; 362 case MMU_DATA_STORE: 363 cs->exception_index = page_fault_exceptions ? 364 RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT; 365 break; 366 default: 367 g_assert_not_reached(); 368 } 369 env->badaddr = address; 370 } 371 372 hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) 373 { 374 RISCVCPU *cpu = RISCV_CPU(cs); 375 hwaddr phys_addr; 376 int prot; 377 int mmu_idx = cpu_mmu_index(&cpu->env, false); 378 379 if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0, mmu_idx)) { 380 return -1; 381 } 382 return phys_addr; 383 } 384 385 void riscv_cpu_unassigned_access(CPUState *cs, hwaddr addr, bool is_write, 386 bool is_exec, int unused, unsigned size) 387 { 388 RISCVCPU *cpu = RISCV_CPU(cs); 389 CPURISCVState *env = &cpu->env; 390 391 if (is_write) { 392 cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT; 393 } else { 394 cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT; 395 } 396 397 env->badaddr = addr; 398 riscv_raise_exception(&cpu->env, cs->exception_index, GETPC()); 399 } 400 401 void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, 402 MMUAccessType access_type, int mmu_idx, 403 uintptr_t retaddr) 404 { 405 RISCVCPU *cpu = RISCV_CPU(cs); 406 CPURISCVState *env = &cpu->env; 407 switch (access_type) { 408 case MMU_INST_FETCH: 409 cs->exception_index = RISCV_EXCP_INST_ADDR_MIS; 410 break; 411 case MMU_DATA_LOAD: 412 cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS; 413 break; 414 case MMU_DATA_STORE: 415 cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS; 416 break; 417 default: 418 g_assert_not_reached(); 419 } 420 env->badaddr = addr; 421 riscv_raise_exception(env, cs->exception_index, retaddr); 422 } 423 #endif 424 425 bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, 426 MMUAccessType access_type, int mmu_idx, 427 bool probe, uintptr_t retaddr) 428 { 429 #ifndef CONFIG_USER_ONLY 430 RISCVCPU *cpu = RISCV_CPU(cs); 431 CPURISCVState *env = &cpu->env; 432 hwaddr pa = 0; 433 int prot; 434 bool pmp_violation = false; 435 int ret = TRANSLATE_FAIL; 436 int mode = mmu_idx; 437 438 qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", 439 __func__, address, access_type, mmu_idx); 440 441 ret = get_physical_address(env, &pa, &prot, address, access_type, mmu_idx); 442 443 if (mode == PRV_M && access_type != MMU_INST_FETCH) { 444 if (get_field(env->mstatus, MSTATUS_MPRV)) { 445 mode = get_field(env->mstatus, MSTATUS_MPP); 446 } 447 } 448 449 qemu_log_mask(CPU_LOG_MMU, 450 "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx 451 " prot %d\n", __func__, address, ret, pa, prot); 452 453 if (riscv_feature(env, RISCV_FEATURE_PMP) && 454 (ret == TRANSLATE_SUCCESS) && 455 !pmp_hart_has_privs(env, pa, TARGET_PAGE_SIZE, 1 << access_type, 456 mode)) { 457 ret = TRANSLATE_PMP_FAIL; 458 } 459 if (ret == TRANSLATE_PMP_FAIL) { 460 pmp_violation = true; 461 } 462 if (ret == TRANSLATE_SUCCESS) { 463 tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK, 464 prot, mmu_idx, TARGET_PAGE_SIZE); 465 return true; 466 } else if (probe) { 467 return false; 468 } else { 469 raise_mmu_exception(env, address, access_type, pmp_violation); 470 riscv_raise_exception(env, cs->exception_index, retaddr); 471 } 472 #else 473 switch (access_type) { 474 case MMU_INST_FETCH: 475 cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT; 476 break; 477 case MMU_DATA_LOAD: 478 cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT; 479 break; 480 case MMU_DATA_STORE: 481 cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT; 482 break; 483 } 484 cpu_loop_exit_restore(cs, retaddr); 485 #endif 486 } 487 488 /* 489 * Handle Traps 490 * 491 * Adapted from Spike's processor_t::take_trap. 492 * 493 */ 494 void riscv_cpu_do_interrupt(CPUState *cs) 495 { 496 #if !defined(CONFIG_USER_ONLY) 497 498 RISCVCPU *cpu = RISCV_CPU(cs); 499 CPURISCVState *env = &cpu->env; 500 501 /* cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide 502 * so we mask off the MSB and separate into trap type and cause. 503 */ 504 bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG); 505 target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK; 506 target_ulong deleg = async ? env->mideleg : env->medeleg; 507 target_ulong tval = 0; 508 509 static const int ecall_cause_map[] = { 510 [PRV_U] = RISCV_EXCP_U_ECALL, 511 [PRV_S] = RISCV_EXCP_S_ECALL, 512 [PRV_H] = RISCV_EXCP_H_ECALL, 513 [PRV_M] = RISCV_EXCP_M_ECALL 514 }; 515 516 if (!async) { 517 /* set tval to badaddr for traps with address information */ 518 switch (cause) { 519 case RISCV_EXCP_INST_ADDR_MIS: 520 case RISCV_EXCP_INST_ACCESS_FAULT: 521 case RISCV_EXCP_LOAD_ADDR_MIS: 522 case RISCV_EXCP_STORE_AMO_ADDR_MIS: 523 case RISCV_EXCP_LOAD_ACCESS_FAULT: 524 case RISCV_EXCP_STORE_AMO_ACCESS_FAULT: 525 case RISCV_EXCP_INST_PAGE_FAULT: 526 case RISCV_EXCP_LOAD_PAGE_FAULT: 527 case RISCV_EXCP_STORE_PAGE_FAULT: 528 tval = env->badaddr; 529 break; 530 default: 531 break; 532 } 533 /* ecall is dispatched as one cause so translate based on mode */ 534 if (cause == RISCV_EXCP_U_ECALL) { 535 assert(env->priv <= 3); 536 cause = ecall_cause_map[env->priv]; 537 } 538 } 539 540 trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, cause < 16 ? 541 (async ? riscv_intr_names : riscv_excp_names)[cause] : "(unknown)"); 542 543 if (env->priv <= PRV_S && 544 cause < TARGET_LONG_BITS && ((deleg >> cause) & 1)) { 545 /* handle the trap in S-mode */ 546 target_ulong s = env->mstatus; 547 s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? 548 get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv)); 549 s = set_field(s, MSTATUS_SPP, env->priv); 550 s = set_field(s, MSTATUS_SIE, 0); 551 env->mstatus = s; 552 env->scause = cause | ((target_ulong)async << (TARGET_LONG_BITS - 1)); 553 env->sepc = env->pc; 554 env->sbadaddr = tval; 555 env->pc = (env->stvec >> 2 << 2) + 556 ((async && (env->stvec & 3) == 1) ? cause * 4 : 0); 557 riscv_cpu_set_mode(env, PRV_S); 558 } else { 559 /* handle the trap in M-mode */ 560 target_ulong s = env->mstatus; 561 s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? 562 get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv)); 563 s = set_field(s, MSTATUS_MPP, env->priv); 564 s = set_field(s, MSTATUS_MIE, 0); 565 env->mstatus = s; 566 env->mcause = cause | ~(((target_ulong)-1) >> async); 567 env->mepc = env->pc; 568 env->mbadaddr = tval; 569 env->pc = (env->mtvec >> 2 << 2) + 570 ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); 571 riscv_cpu_set_mode(env, PRV_M); 572 } 573 574 /* NOTE: it is not necessary to yield load reservations here. It is only 575 * necessary for an SC from "another hart" to cause a load reservation 576 * to be yielded. Refer to the memory consistency model section of the 577 * RISC-V ISA Specification. 578 */ 579 580 #endif 581 cs->exception_index = EXCP_NONE; /* mark handled to qemu */ 582 } 583