1 #include "kvm/kvm-cpu.h" 2 #include "kvm/kvm.h" 3 #include "kvm/virtio.h" 4 #include "kvm/sbi.h" 5 #include "kvm/term.h" 6 7 #include <asm/ptrace.h> 8 9 static int debug_fd; 10 11 void kvm_cpu__set_debug_fd(int fd) 12 { 13 debug_fd = fd; 14 } 15 16 int kvm_cpu__get_debug_fd(void) 17 { 18 return debug_fd; 19 } 20 21 struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) 22 { 23 struct kvm_cpu *vcpu; 24 u64 timebase = 0; 25 unsigned long isa = 0, id = 0; 26 int coalesced_offset, mmap_size; 27 struct kvm_one_reg reg; 28 29 vcpu = calloc(1, sizeof(struct kvm_cpu)); 30 if (!vcpu) 31 return NULL; 32 33 vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id); 34 if (vcpu->vcpu_fd < 0) 35 die_perror("KVM_CREATE_VCPU ioctl"); 36 37 reg.id = RISCV_CONFIG_REG(isa); 38 reg.addr = (unsigned long)&isa; 39 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 40 die("KVM_GET_ONE_REG failed (config.isa)"); 41 42 reg.id = RISCV_TIMER_REG(frequency); 43 reg.addr = (unsigned long)&timebase; 44 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 45 die("KVM_GET_ONE_REG failed (timer.frequency)"); 46 47 mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0); 48 if (mmap_size < 0) 49 die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl"); 50 51 vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, 52 vcpu->vcpu_fd, 0); 53 if (vcpu->kvm_run == MAP_FAILED) 54 die("unable to mmap vcpu fd"); 55 56 coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, 57 KVM_CAP_COALESCED_MMIO); 58 if (coalesced_offset) 59 vcpu->ring = (void *)vcpu->kvm_run + 60 (coalesced_offset * PAGE_SIZE); 61 62 reg.id = RISCV_CONFIG_REG(isa); 63 reg.addr = (unsigned long)&isa; 64 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 65 die("KVM_SET_ONE_REG failed (config.isa)"); 66 67 if (kvm->cfg.arch.custom_mvendorid) { 68 id = kvm->cfg.arch.custom_mvendorid; 69 reg.id = RISCV_CONFIG_REG(mvendorid); 70 reg.addr = (unsigned long)&id; 71 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 72 die("KVM_SET_ONE_REG failed (config.mvendorid)"); 73 } 74 75 if (kvm->cfg.arch.custom_marchid) { 76 id = kvm->cfg.arch.custom_marchid; 77 reg.id = RISCV_CONFIG_REG(marchid); 78 reg.addr = (unsigned long)&id; 79 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 80 die("KVM_SET_ONE_REG failed (config.marchid)"); 81 } 82 83 if (kvm->cfg.arch.custom_mimpid) { 84 id = kvm->cfg.arch.custom_mimpid; 85 reg.id = RISCV_CONFIG_REG(mimpid); 86 reg.addr = (unsigned long)&id; 87 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 88 die("KVM_SET_ONE_REG failed (config.mimpid)"); 89 } 90 91 /* Populate the vcpu structure. */ 92 vcpu->kvm = kvm; 93 vcpu->cpu_id = cpu_id; 94 vcpu->riscv_isa = isa; 95 vcpu->riscv_xlen = __riscv_xlen; 96 vcpu->riscv_timebase = timebase; 97 vcpu->is_running = true; 98 99 return vcpu; 100 } 101 102 void kvm_cpu__arch_nmi(struct kvm_cpu *cpu) 103 { 104 } 105 106 void kvm_cpu__delete(struct kvm_cpu *vcpu) 107 { 108 free(vcpu); 109 } 110 111 static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu) 112 { 113 char ch; 114 bool ret = true; 115 int dfd = kvm_cpu__get_debug_fd(); 116 117 switch (vcpu->kvm_run->riscv_sbi.extension_id) { 118 case SBI_EXT_0_1_CONSOLE_PUTCHAR: 119 ch = vcpu->kvm_run->riscv_sbi.args[0]; 120 term_putc(&ch, 1, 0); 121 vcpu->kvm_run->riscv_sbi.ret[0] = 0; 122 break; 123 case SBI_EXT_0_1_CONSOLE_GETCHAR: 124 if (term_readable(0)) 125 vcpu->kvm_run->riscv_sbi.ret[0] = 126 term_getc(vcpu->kvm, 0); 127 else 128 vcpu->kvm_run->riscv_sbi.ret[0] = SBI_ERR_FAILURE; 129 break; 130 default: 131 dprintf(dfd, "Unhandled SBI call\n"); 132 dprintf(dfd, "extension_id=0x%lx function_id=0x%lx\n", 133 vcpu->kvm_run->riscv_sbi.extension_id, 134 vcpu->kvm_run->riscv_sbi.function_id); 135 dprintf(dfd, "args[0]=0x%lx args[1]=0x%lx\n", 136 vcpu->kvm_run->riscv_sbi.args[0], 137 vcpu->kvm_run->riscv_sbi.args[1]); 138 dprintf(dfd, "args[2]=0x%lx args[3]=0x%lx\n", 139 vcpu->kvm_run->riscv_sbi.args[2], 140 vcpu->kvm_run->riscv_sbi.args[3]); 141 dprintf(dfd, "args[4]=0x%lx args[5]=0x%lx\n", 142 vcpu->kvm_run->riscv_sbi.args[4], 143 vcpu->kvm_run->riscv_sbi.args[5]); 144 ret = false; 145 break; 146 }; 147 148 return ret; 149 } 150 151 bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) 152 { 153 switch (vcpu->kvm_run->exit_reason) { 154 case KVM_EXIT_RISCV_SBI: 155 return kvm_cpu_riscv_sbi(vcpu); 156 default: 157 break; 158 }; 159 160 return false; 161 } 162 163 void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu) 164 { 165 } 166 167 void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu) 168 { 169 struct kvm *kvm = vcpu->kvm; 170 struct kvm_mp_state mp_state; 171 struct kvm_one_reg reg; 172 unsigned long data; 173 174 if (ioctl(vcpu->vcpu_fd, KVM_GET_MP_STATE, &mp_state) < 0) 175 die_perror("KVM_GET_MP_STATE failed"); 176 177 /* 178 * If MP state is stopped then it means Linux KVM RISC-V emulates 179 * SBI v0.2 (or higher) with HART power managment and give VCPU 180 * will power-up at boot-time by boot VCPU. For such VCPU, we 181 * don't update PC, A0 and A1 here. 182 */ 183 if (mp_state.mp_state == KVM_MP_STATE_STOPPED) 184 return; 185 186 reg.addr = (unsigned long)&data; 187 188 data = kvm->arch.kern_guest_start; 189 reg.id = RISCV_CORE_REG(regs.pc); 190 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 191 die_perror("KVM_SET_ONE_REG failed (pc)"); 192 193 data = vcpu->cpu_id; 194 reg.id = RISCV_CORE_REG(regs.a0); 195 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 196 die_perror("KVM_SET_ONE_REG failed (a0)"); 197 198 data = kvm->arch.dtb_guest_start; 199 reg.id = RISCV_CORE_REG(regs.a1); 200 if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 201 die_perror("KVM_SET_ONE_REG failed (a1)"); 202 } 203 204 int kvm_cpu__get_endianness(struct kvm_cpu *vcpu) 205 { 206 return VIRTIO_ENDIAN_LE; 207 } 208 209 void kvm_cpu__show_code(struct kvm_cpu *vcpu) 210 { 211 struct kvm_one_reg reg; 212 unsigned long data; 213 int debug_fd = kvm_cpu__get_debug_fd(); 214 215 reg.addr = (unsigned long)&data; 216 217 dprintf(debug_fd, "\n*PC:\n"); 218 reg.id = RISCV_CORE_REG(regs.pc); 219 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 220 die("KVM_GET_ONE_REG failed (show_code @ PC)"); 221 222 kvm__dump_mem(vcpu->kvm, data, 32, debug_fd); 223 224 dprintf(debug_fd, "\n*RA:\n"); 225 reg.id = RISCV_CORE_REG(regs.ra); 226 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 227 die("KVM_GET_ONE_REG failed (show_code @ RA)"); 228 229 kvm__dump_mem(vcpu->kvm, data, 32, debug_fd); 230 } 231 232 static void kvm_cpu__show_csrs(struct kvm_cpu *vcpu) 233 { 234 struct kvm_one_reg reg; 235 struct kvm_riscv_csr csr; 236 unsigned long data; 237 int debug_fd = kvm_cpu__get_debug_fd(); 238 239 reg.addr = (unsigned long)&data; 240 dprintf(debug_fd, "\n Control Status Registers:\n"); 241 dprintf(debug_fd, " ------------------------\n"); 242 243 reg.id = RISCV_CSR_REG(sstatus); 244 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 245 die("KVM_GET_ONE_REG failed (sstatus)"); 246 csr.sstatus = data; 247 248 reg.id = RISCV_CSR_REG(sie); 249 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 250 die("KVM_GET_ONE_REG failed (sie)"); 251 csr.sie = data; 252 253 reg.id = RISCV_CSR_REG(stvec); 254 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 255 die("KVM_GET_ONE_REG failed (stvec)"); 256 csr.stvec = data; 257 258 reg.id = RISCV_CSR_REG(sip); 259 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 260 die("KVM_GET_ONE_REG failed (sip)"); 261 csr.sip = data; 262 263 reg.id = RISCV_CSR_REG(satp); 264 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 265 die("KVM_GET_ONE_REG failed (satp)"); 266 csr.satp = data; 267 268 reg.id = RISCV_CSR_REG(stval); 269 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 270 die("KVM_GET_ONE_REG failed (stval)"); 271 csr.stval = data; 272 273 reg.id = RISCV_CSR_REG(scause); 274 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 275 die("KVM_GET_ONE_REG failed (SCAUSE)"); 276 csr.scause = data; 277 278 reg.id = RISCV_CSR_REG(sscratch); 279 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 280 die("KVM_GET_ONE_REG failed (sscartch)"); 281 csr.sscratch = data; 282 dprintf(debug_fd, " SSTATUS: 0x%016lx\n", csr.sstatus); 283 dprintf(debug_fd, " SIE: 0x%016lx\n", csr.sie); 284 dprintf(debug_fd, " STVEC: 0x%016lx\n", csr.stvec); 285 dprintf(debug_fd, " SIP: 0x%016lx\n", csr.sip); 286 dprintf(debug_fd, " SATP: 0x%016lx\n", csr.satp); 287 dprintf(debug_fd, " STVAL: 0x%016lx\n", csr.stval); 288 dprintf(debug_fd, " SCAUSE: 0x%016lx\n", csr.scause); 289 dprintf(debug_fd, " SSCRATCH: 0x%016lx\n", csr.sscratch); 290 } 291 292 void kvm_cpu__show_registers(struct kvm_cpu *vcpu) 293 { 294 struct kvm_one_reg reg; 295 unsigned long data; 296 int debug_fd = kvm_cpu__get_debug_fd(); 297 struct kvm_riscv_core core; 298 299 reg.addr = (unsigned long)&data; 300 301 reg.id = RISCV_CORE_REG(mode); 302 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 303 die("KVM_GET_ONE_REG failed (mode)"); 304 core.mode = data; 305 306 reg.id = RISCV_CORE_REG(regs.pc); 307 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 308 die("KVM_GET_ONE_REG failed (pc)"); 309 core.regs.pc = data; 310 311 reg.id = RISCV_CORE_REG(regs.ra); 312 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 313 die("KVM_GET_ONE_REG failed (ra)"); 314 core.regs.ra = data; 315 316 reg.id = RISCV_CORE_REG(regs.sp); 317 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 318 die("KVM_GET_ONE_REG failed (sp)"); 319 core.regs.sp = data; 320 321 reg.id = RISCV_CORE_REG(regs.gp); 322 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 323 die("KVM_GET_ONE_REG failed (gp)"); 324 core.regs.gp = data; 325 326 reg.id = RISCV_CORE_REG(regs.tp); 327 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 328 die("KVM_GET_ONE_REG failed (tp)"); 329 core.regs.tp = data; 330 331 reg.id = RISCV_CORE_REG(regs.t0); 332 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 333 die("KVM_GET_ONE_REG failed (t0)"); 334 core.regs.t0 = data; 335 336 reg.id = RISCV_CORE_REG(regs.t1); 337 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 338 die("KVM_GET_ONE_REG failed (t1)"); 339 core.regs.t1 = data; 340 341 reg.id = RISCV_CORE_REG(regs.t2); 342 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 343 die("KVM_GET_ONE_REG failed (t2)"); 344 core.regs.t2 = data; 345 346 reg.id = RISCV_CORE_REG(regs.s0); 347 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 348 die("KVM_GET_ONE_REG failed (s0)"); 349 core.regs.s0 = data; 350 351 reg.id = RISCV_CORE_REG(regs.s1); 352 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 353 die("KVM_GET_ONE_REG failed (s1)"); 354 core.regs.s1 = data; 355 356 reg.id = RISCV_CORE_REG(regs.a0); 357 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 358 die("KVM_GET_ONE_REG failed (a0)"); 359 core.regs.a0 = data; 360 361 reg.id = RISCV_CORE_REG(regs.a1); 362 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 363 die("KVM_GET_ONE_REG failed (a1)"); 364 core.regs.a1 = data; 365 366 reg.id = RISCV_CORE_REG(regs.a2); 367 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 368 die("KVM_GET_ONE_REG failed (a2)"); 369 core.regs.a2 = data; 370 371 reg.id = RISCV_CORE_REG(regs.a3); 372 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 373 die("KVM_GET_ONE_REG failed (a3)"); 374 core.regs.a3 = data; 375 376 reg.id = RISCV_CORE_REG(regs.a4); 377 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 378 die("KVM_GET_ONE_REG failed (a4)"); 379 core.regs.a4 = data; 380 381 reg.id = RISCV_CORE_REG(regs.a5); 382 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 383 die("KVM_GET_ONE_REG failed (a5)"); 384 core.regs.a5 = data; 385 386 reg.id = RISCV_CORE_REG(regs.a6); 387 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 388 die("KVM_GET_ONE_REG failed (a6)"); 389 core.regs.a6 = data; 390 391 reg.id = RISCV_CORE_REG(regs.a7); 392 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 393 die("KVM_GET_ONE_REG failed (a7)"); 394 core.regs.a7 = data; 395 396 reg.id = RISCV_CORE_REG(regs.s2); 397 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 398 die("KVM_GET_ONE_REG failed (s2)"); 399 core.regs.s2 = data; 400 401 reg.id = RISCV_CORE_REG(regs.s3); 402 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 403 die("KVM_GET_ONE_REG failed (s3)"); 404 core.regs.s3 = data; 405 406 reg.id = RISCV_CORE_REG(regs.s4); 407 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 408 die("KVM_GET_ONE_REG failed (s4)"); 409 core.regs.s4 = data; 410 411 reg.id = RISCV_CORE_REG(regs.s5); 412 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 413 die("KVM_GET_ONE_REG failed (s5)"); 414 core.regs.s5 = data; 415 416 reg.id = RISCV_CORE_REG(regs.s6); 417 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 418 die("KVM_GET_ONE_REG failed (s6)"); 419 core.regs.s6 = data; 420 421 reg.id = RISCV_CORE_REG(regs.s7); 422 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 423 die("KVM_GET_ONE_REG failed (s7)"); 424 core.regs.s7 = data; 425 426 reg.id = RISCV_CORE_REG(regs.s8); 427 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 428 die("KVM_GET_ONE_REG failed (s8)"); 429 core.regs.s8 = data; 430 431 reg.id = RISCV_CORE_REG(regs.s9); 432 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 433 die("KVM_GET_ONE_REG failed (s9)"); 434 core.regs.s9 = data; 435 436 reg.id = RISCV_CORE_REG(regs.s10); 437 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 438 die("KVM_GET_ONE_REG failed (s10)"); 439 core.regs.s10 = data; 440 441 reg.id = RISCV_CORE_REG(regs.s11); 442 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 443 die("KVM_GET_ONE_REG failed (s11)"); 444 core.regs.s11 = data; 445 446 reg.id = RISCV_CORE_REG(regs.t3); 447 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 448 die("KVM_GET_ONE_REG failed (t3)"); 449 core.regs.t3 = data; 450 451 reg.id = RISCV_CORE_REG(regs.t4); 452 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 453 die("KVM_GET_ONE_REG failed (t4)"); 454 core.regs.t4 = data; 455 456 reg.id = RISCV_CORE_REG(regs.t5); 457 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 458 die("KVM_GET_ONE_REG failed (t5)"); 459 core.regs.t5 = data; 460 461 reg.id = RISCV_CORE_REG(regs.t6); 462 if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 463 die("KVM_GET_ONE_REG failed (t6)"); 464 core.regs.t6 = data; 465 466 dprintf(debug_fd, "\n General Purpose Registers:\n"); 467 dprintf(debug_fd, " -------------------------\n"); 468 dprintf(debug_fd, " MODE: 0x%lx\n", data); 469 dprintf(debug_fd, " PC: 0x%016lx RA: 0x%016lx SP: 0x%016lx GP: 0x%016lx\n", 470 core.regs.pc, core.regs.ra, core.regs.sp, core.regs.gp); 471 dprintf(debug_fd, " TP: 0x%016lx T0: 0x%016lx T1: 0x%016lx T2: 0x%016lx\n", 472 core.regs.tp, core.regs.t0, core.regs.t1, core.regs.t2); 473 dprintf(debug_fd, " S0: 0x%016lx S1: 0x%016lx A0: 0x%016lx A1: 0x%016lx\n", 474 core.regs.s0, core.regs.s1, core.regs.a0, core.regs.a1); 475 dprintf(debug_fd, " A2: 0x%016lx A3: 0x%016lx A4: 0x%016lx A5: 0x%016lx\n", 476 core.regs.a2, core.regs.a3, core.regs.a4, core.regs.a5); 477 dprintf(debug_fd, " A6: 0x%016lx A7: 0x%016lx S2: 0x%016lx S3: 0x%016lx\n", 478 core.regs.a6, core.regs.a7, core.regs.s2, core.regs.s3); 479 dprintf(debug_fd, " S4: 0x%016lx S5: 0x%016lx S6: 0x%016lx S7: 0x%016lx\n", 480 core.regs.s4, core.regs.s5, core.regs.s6, core.regs.s7); 481 dprintf(debug_fd, " S8: 0x%016lx S9: 0x%016lx S10: 0x%016lx S11: 0x%016lx\n", 482 core.regs.s8, core.regs.s9, core.regs.s10, core.regs.s11); 483 dprintf(debug_fd, " T3: 0x%016lx T4: 0x%016lx T5: 0x%016lx T6: 0x%016lx\n", 484 core.regs.t3, core.regs.t4, core.regs.t5, core.regs.t6); 485 486 kvm_cpu__show_csrs(vcpu); 487 } 488