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