12e996783SAnup Patel #include "kvm/kvm-cpu.h" 22e996783SAnup Patel #include "kvm/kvm.h" 32e996783SAnup Patel #include "kvm/virtio.h" 4*721da166SAnup Patel #include "kvm/sbi.h" 52e996783SAnup Patel #include "kvm/term.h" 62e996783SAnup Patel 72e996783SAnup Patel #include <asm/ptrace.h> 82e996783SAnup Patel 92e996783SAnup Patel static int debug_fd; 102e996783SAnup Patel 112e996783SAnup Patel void kvm_cpu__set_debug_fd(int fd) 122e996783SAnup Patel { 132e996783SAnup Patel debug_fd = fd; 142e996783SAnup Patel } 152e996783SAnup Patel 162e996783SAnup Patel int kvm_cpu__get_debug_fd(void) 172e996783SAnup Patel { 182e996783SAnup Patel return debug_fd; 192e996783SAnup Patel } 202e996783SAnup Patel 2142bfe448SAnup Patel static __u64 __kvm_reg_id(__u64 type, __u64 idx, __u64 size) 2242bfe448SAnup Patel { 2342bfe448SAnup Patel return KVM_REG_RISCV | type | idx | size; 2442bfe448SAnup Patel } 2542bfe448SAnup Patel 2642bfe448SAnup Patel #if __riscv_xlen == 64 2742bfe448SAnup Patel #define KVM_REG_SIZE_ULONG KVM_REG_SIZE_U64 2842bfe448SAnup Patel #else 2942bfe448SAnup Patel #define KVM_REG_SIZE_ULONG KVM_REG_SIZE_U32 3042bfe448SAnup Patel #endif 3142bfe448SAnup Patel 3242bfe448SAnup Patel #define RISCV_CONFIG_REG(name) __kvm_reg_id(KVM_REG_RISCV_CONFIG, \ 3342bfe448SAnup Patel KVM_REG_RISCV_CONFIG_REG(name), \ 3442bfe448SAnup Patel KVM_REG_SIZE_ULONG) 3542bfe448SAnup Patel 3642bfe448SAnup Patel #define RISCV_CORE_REG(name) __kvm_reg_id(KVM_REG_RISCV_CORE, \ 3742bfe448SAnup Patel KVM_REG_RISCV_CORE_REG(name), \ 3842bfe448SAnup Patel KVM_REG_SIZE_ULONG) 3942bfe448SAnup Patel 4042bfe448SAnup Patel #define RISCV_CSR_REG(name) __kvm_reg_id(KVM_REG_RISCV_CSR, \ 4142bfe448SAnup Patel KVM_REG_RISCV_CSR_REG(name), \ 4242bfe448SAnup Patel KVM_REG_SIZE_ULONG) 4342bfe448SAnup Patel 4442bfe448SAnup Patel #define RISCV_TIMER_REG(name) __kvm_reg_id(KVM_REG_RISCV_TIMER, \ 4542bfe448SAnup Patel KVM_REG_RISCV_TIMER_REG(name), \ 4642bfe448SAnup Patel KVM_REG_SIZE_U64) 4742bfe448SAnup Patel 482e996783SAnup Patel struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) 492e996783SAnup Patel { 5042bfe448SAnup Patel struct kvm_cpu *vcpu; 5142bfe448SAnup Patel u64 timebase = 0; 5242bfe448SAnup Patel unsigned long isa = 0; 5342bfe448SAnup Patel int coalesced_offset, mmap_size; 5442bfe448SAnup Patel struct kvm_one_reg reg; 5542bfe448SAnup Patel 5642bfe448SAnup Patel vcpu = calloc(1, sizeof(struct kvm_cpu)); 5742bfe448SAnup Patel if (!vcpu) 582e996783SAnup Patel return NULL; 5942bfe448SAnup Patel 6042bfe448SAnup Patel vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id); 6142bfe448SAnup Patel if (vcpu->vcpu_fd < 0) 6242bfe448SAnup Patel die_perror("KVM_CREATE_VCPU ioctl"); 6342bfe448SAnup Patel 6442bfe448SAnup Patel reg.id = RISCV_CONFIG_REG(isa); 6542bfe448SAnup Patel reg.addr = (unsigned long)&isa; 6642bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 6742bfe448SAnup Patel die("KVM_GET_ONE_REG failed (config.isa)"); 6842bfe448SAnup Patel 6942bfe448SAnup Patel reg.id = RISCV_TIMER_REG(frequency); 7042bfe448SAnup Patel reg.addr = (unsigned long)&timebase; 7142bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 7242bfe448SAnup Patel die("KVM_GET_ONE_REG failed (timer.frequency)"); 7342bfe448SAnup Patel 7442bfe448SAnup Patel mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0); 7542bfe448SAnup Patel if (mmap_size < 0) 7642bfe448SAnup Patel die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl"); 7742bfe448SAnup Patel 7842bfe448SAnup Patel vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, 7942bfe448SAnup Patel vcpu->vcpu_fd, 0); 8042bfe448SAnup Patel if (vcpu->kvm_run == MAP_FAILED) 8142bfe448SAnup Patel die("unable to mmap vcpu fd"); 8242bfe448SAnup Patel 8342bfe448SAnup Patel coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, 8442bfe448SAnup Patel KVM_CAP_COALESCED_MMIO); 8542bfe448SAnup Patel if (coalesced_offset) 8642bfe448SAnup Patel vcpu->ring = (void *)vcpu->kvm_run + 8742bfe448SAnup Patel (coalesced_offset * PAGE_SIZE); 8842bfe448SAnup Patel 8942bfe448SAnup Patel reg.id = RISCV_CONFIG_REG(isa); 9042bfe448SAnup Patel reg.addr = (unsigned long)&isa; 9142bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 9242bfe448SAnup Patel die("KVM_SET_ONE_REG failed (config.isa)"); 9342bfe448SAnup Patel 9442bfe448SAnup Patel /* Populate the vcpu structure. */ 9542bfe448SAnup Patel vcpu->kvm = kvm; 9642bfe448SAnup Patel vcpu->cpu_id = cpu_id; 9742bfe448SAnup Patel vcpu->riscv_isa = isa; 9842bfe448SAnup Patel vcpu->riscv_xlen = __riscv_xlen; 9942bfe448SAnup Patel vcpu->riscv_timebase = timebase; 10042bfe448SAnup Patel vcpu->is_running = true; 10142bfe448SAnup Patel 10242bfe448SAnup Patel return vcpu; 1032e996783SAnup Patel } 1042e996783SAnup Patel 1052e996783SAnup Patel void kvm_cpu__arch_nmi(struct kvm_cpu *cpu) 1062e996783SAnup Patel { 1072e996783SAnup Patel } 1082e996783SAnup Patel 1092e996783SAnup Patel void kvm_cpu__delete(struct kvm_cpu *vcpu) 1102e996783SAnup Patel { 11142bfe448SAnup Patel free(vcpu); 1122e996783SAnup Patel } 1132e996783SAnup Patel 114*721da166SAnup Patel static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu) 115*721da166SAnup Patel { 116*721da166SAnup Patel char ch; 117*721da166SAnup Patel bool ret = true; 118*721da166SAnup Patel int dfd = kvm_cpu__get_debug_fd(); 119*721da166SAnup Patel 120*721da166SAnup Patel switch (vcpu->kvm_run->riscv_sbi.extension_id) { 121*721da166SAnup Patel case SBI_EXT_0_1_CONSOLE_PUTCHAR: 122*721da166SAnup Patel ch = vcpu->kvm_run->riscv_sbi.args[0]; 123*721da166SAnup Patel term_putc(&ch, 1, 0); 124*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.ret[0] = 0; 125*721da166SAnup Patel break; 126*721da166SAnup Patel case SBI_EXT_0_1_CONSOLE_GETCHAR: 127*721da166SAnup Patel if (term_readable(0)) 128*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.ret[0] = 129*721da166SAnup Patel term_getc(vcpu->kvm, 0); 130*721da166SAnup Patel else 131*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.ret[0] = SBI_ERR_FAILURE; 132*721da166SAnup Patel break; 133*721da166SAnup Patel default: 134*721da166SAnup Patel dprintf(dfd, "Unhandled SBI call\n"); 135*721da166SAnup Patel dprintf(dfd, "extension_id=0x%lx function_id=0x%lx\n", 136*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.extension_id, 137*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.function_id); 138*721da166SAnup Patel dprintf(dfd, "args[0]=0x%lx args[1]=0x%lx\n", 139*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.args[0], 140*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.args[1]); 141*721da166SAnup Patel dprintf(dfd, "args[2]=0x%lx args[3]=0x%lx\n", 142*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.args[2], 143*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.args[3]); 144*721da166SAnup Patel dprintf(dfd, "args[4]=0x%lx args[5]=0x%lx\n", 145*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.args[4], 146*721da166SAnup Patel vcpu->kvm_run->riscv_sbi.args[5]); 147*721da166SAnup Patel ret = false; 148*721da166SAnup Patel break; 149*721da166SAnup Patel }; 150*721da166SAnup Patel 151*721da166SAnup Patel return ret; 152*721da166SAnup Patel } 153*721da166SAnup Patel 1542e996783SAnup Patel bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) 1552e996783SAnup Patel { 156*721da166SAnup Patel switch (vcpu->kvm_run->exit_reason) { 157*721da166SAnup Patel case KVM_EXIT_RISCV_SBI: 158*721da166SAnup Patel return kvm_cpu_riscv_sbi(vcpu); 159*721da166SAnup Patel default: 160*721da166SAnup Patel break; 161*721da166SAnup Patel }; 162*721da166SAnup Patel 1632e996783SAnup Patel return false; 1642e996783SAnup Patel } 1652e996783SAnup Patel 1662e996783SAnup Patel void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu) 1672e996783SAnup Patel { 1682e996783SAnup Patel } 1692e996783SAnup Patel 1702e996783SAnup Patel void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu) 1712e996783SAnup Patel { 17242bfe448SAnup Patel struct kvm *kvm = vcpu->kvm; 17342bfe448SAnup Patel struct kvm_mp_state mp_state; 17442bfe448SAnup Patel struct kvm_one_reg reg; 17542bfe448SAnup Patel unsigned long data; 17642bfe448SAnup Patel 17742bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_MP_STATE, &mp_state) < 0) 17842bfe448SAnup Patel die_perror("KVM_GET_MP_STATE failed"); 17942bfe448SAnup Patel 18042bfe448SAnup Patel /* 18142bfe448SAnup Patel * If MP state is stopped then it means Linux KVM RISC-V emulates 18242bfe448SAnup Patel * SBI v0.2 (or higher) with HART power managment and give VCPU 18342bfe448SAnup Patel * will power-up at boot-time by boot VCPU. For such VCPU, we 18442bfe448SAnup Patel * don't update PC, A0 and A1 here. 18542bfe448SAnup Patel */ 18642bfe448SAnup Patel if (mp_state.mp_state == KVM_MP_STATE_STOPPED) 18742bfe448SAnup Patel return; 18842bfe448SAnup Patel 18942bfe448SAnup Patel reg.addr = (unsigned long)&data; 19042bfe448SAnup Patel 19142bfe448SAnup Patel data = kvm->arch.kern_guest_start; 19242bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.pc); 19342bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 19442bfe448SAnup Patel die_perror("KVM_SET_ONE_REG failed (pc)"); 19542bfe448SAnup Patel 19642bfe448SAnup Patel data = vcpu->cpu_id; 19742bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a0); 19842bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 19942bfe448SAnup Patel die_perror("KVM_SET_ONE_REG failed (a0)"); 20042bfe448SAnup Patel 20142bfe448SAnup Patel data = kvm->arch.dtb_guest_start; 20242bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a1); 20342bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 20442bfe448SAnup Patel die_perror("KVM_SET_ONE_REG failed (a1)"); 2052e996783SAnup Patel } 2062e996783SAnup Patel 2072e996783SAnup Patel int kvm_cpu__get_endianness(struct kvm_cpu *vcpu) 2082e996783SAnup Patel { 2092e996783SAnup Patel return VIRTIO_ENDIAN_LE; 2102e996783SAnup Patel } 2112e996783SAnup Patel 2122e996783SAnup Patel void kvm_cpu__show_code(struct kvm_cpu *vcpu) 2132e996783SAnup Patel { 21442bfe448SAnup Patel struct kvm_one_reg reg; 21542bfe448SAnup Patel unsigned long data; 21642bfe448SAnup Patel int debug_fd = kvm_cpu__get_debug_fd(); 21742bfe448SAnup Patel 21842bfe448SAnup Patel reg.addr = (unsigned long)&data; 21942bfe448SAnup Patel 22042bfe448SAnup Patel dprintf(debug_fd, "\n*PC:\n"); 22142bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.pc); 22242bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 22342bfe448SAnup Patel die("KVM_GET_ONE_REG failed (show_code @ PC)"); 22442bfe448SAnup Patel 22542bfe448SAnup Patel kvm__dump_mem(vcpu->kvm, data, 32, debug_fd); 22642bfe448SAnup Patel 22742bfe448SAnup Patel dprintf(debug_fd, "\n*RA:\n"); 22842bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.ra); 22942bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 23042bfe448SAnup Patel die("KVM_GET_ONE_REG failed (show_code @ RA)"); 23142bfe448SAnup Patel 23242bfe448SAnup Patel kvm__dump_mem(vcpu->kvm, data, 32, debug_fd); 23342bfe448SAnup Patel } 23442bfe448SAnup Patel 23542bfe448SAnup Patel static void kvm_cpu__show_csrs(struct kvm_cpu *vcpu) 23642bfe448SAnup Patel { 23742bfe448SAnup Patel struct kvm_one_reg reg; 23842bfe448SAnup Patel struct kvm_riscv_csr csr; 23942bfe448SAnup Patel unsigned long data; 24042bfe448SAnup Patel int debug_fd = kvm_cpu__get_debug_fd(); 24142bfe448SAnup Patel 24242bfe448SAnup Patel reg.addr = (unsigned long)&data; 24342bfe448SAnup Patel dprintf(debug_fd, "\n Control Status Registers:\n"); 24442bfe448SAnup Patel dprintf(debug_fd, " ------------------------\n"); 24542bfe448SAnup Patel 24642bfe448SAnup Patel reg.id = RISCV_CSR_REG(sstatus); 24742bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 24842bfe448SAnup Patel die("KVM_GET_ONE_REG failed (sstatus)"); 24942bfe448SAnup Patel csr.sstatus = data; 25042bfe448SAnup Patel 25142bfe448SAnup Patel reg.id = RISCV_CSR_REG(sie); 25242bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 25342bfe448SAnup Patel die("KVM_GET_ONE_REG failed (sie)"); 25442bfe448SAnup Patel csr.sie = data; 25542bfe448SAnup Patel 25642bfe448SAnup Patel reg.id = RISCV_CSR_REG(stvec); 25742bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 25842bfe448SAnup Patel die("KVM_GET_ONE_REG failed (stvec)"); 25942bfe448SAnup Patel csr.stvec = data; 26042bfe448SAnup Patel 26142bfe448SAnup Patel reg.id = RISCV_CSR_REG(sip); 26242bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 26342bfe448SAnup Patel die("KVM_GET_ONE_REG failed (sip)"); 26442bfe448SAnup Patel csr.sip = data; 26542bfe448SAnup Patel 26642bfe448SAnup Patel reg.id = RISCV_CSR_REG(satp); 26742bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 26842bfe448SAnup Patel die("KVM_GET_ONE_REG failed (satp)"); 26942bfe448SAnup Patel csr.satp = data; 27042bfe448SAnup Patel 27142bfe448SAnup Patel reg.id = RISCV_CSR_REG(stval); 27242bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 27342bfe448SAnup Patel die("KVM_GET_ONE_REG failed (stval)"); 27442bfe448SAnup Patel csr.stval = data; 27542bfe448SAnup Patel 27642bfe448SAnup Patel reg.id = RISCV_CSR_REG(scause); 27742bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 27842bfe448SAnup Patel die("KVM_GET_ONE_REG failed (SCAUSE)"); 27942bfe448SAnup Patel csr.scause = data; 28042bfe448SAnup Patel 28142bfe448SAnup Patel reg.id = RISCV_CSR_REG(sscratch); 28242bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 28342bfe448SAnup Patel die("KVM_GET_ONE_REG failed (sscartch)"); 28442bfe448SAnup Patel csr.sscratch = data; 28542bfe448SAnup Patel dprintf(debug_fd, " SSTATUS: 0x%016lx\n", csr.sstatus); 28642bfe448SAnup Patel dprintf(debug_fd, " SIE: 0x%016lx\n", csr.sie); 28742bfe448SAnup Patel dprintf(debug_fd, " STVEC: 0x%016lx\n", csr.stvec); 28842bfe448SAnup Patel dprintf(debug_fd, " SIP: 0x%016lx\n", csr.sip); 28942bfe448SAnup Patel dprintf(debug_fd, " SATP: 0x%016lx\n", csr.satp); 29042bfe448SAnup Patel dprintf(debug_fd, " STVAL: 0x%016lx\n", csr.stval); 29142bfe448SAnup Patel dprintf(debug_fd, " SCAUSE: 0x%016lx\n", csr.scause); 29242bfe448SAnup Patel dprintf(debug_fd, " SSCRATCH: 0x%016lx\n", csr.sscratch); 2932e996783SAnup Patel } 2942e996783SAnup Patel 2952e996783SAnup Patel void kvm_cpu__show_registers(struct kvm_cpu *vcpu) 2962e996783SAnup Patel { 29742bfe448SAnup Patel struct kvm_one_reg reg; 29842bfe448SAnup Patel unsigned long data; 29942bfe448SAnup Patel int debug_fd = kvm_cpu__get_debug_fd(); 30042bfe448SAnup Patel struct kvm_riscv_core core; 30142bfe448SAnup Patel 30242bfe448SAnup Patel reg.addr = (unsigned long)&data; 30342bfe448SAnup Patel 30442bfe448SAnup Patel reg.id = RISCV_CORE_REG(mode); 30542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 30642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (mode)"); 30742bfe448SAnup Patel core.mode = data; 30842bfe448SAnup Patel 30942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.pc); 31042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 31142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (pc)"); 31242bfe448SAnup Patel core.regs.pc = data; 31342bfe448SAnup Patel 31442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.ra); 31542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 31642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (ra)"); 31742bfe448SAnup Patel core.regs.ra = data; 31842bfe448SAnup Patel 31942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.sp); 32042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 32142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (sp)"); 32242bfe448SAnup Patel core.regs.sp = data; 32342bfe448SAnup Patel 32442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.gp); 32542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 32642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (gp)"); 32742bfe448SAnup Patel core.regs.gp = data; 32842bfe448SAnup Patel 32942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.tp); 33042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 33142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (tp)"); 33242bfe448SAnup Patel core.regs.tp = data; 33342bfe448SAnup Patel 33442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t0); 33542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 33642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t0)"); 33742bfe448SAnup Patel core.regs.t0 = data; 33842bfe448SAnup Patel 33942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t1); 34042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 34142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t1)"); 34242bfe448SAnup Patel core.regs.t1 = data; 34342bfe448SAnup Patel 34442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t2); 34542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 34642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t2)"); 34742bfe448SAnup Patel core.regs.t2 = data; 34842bfe448SAnup Patel 34942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s0); 35042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 35142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s0)"); 35242bfe448SAnup Patel core.regs.s0 = data; 35342bfe448SAnup Patel 35442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s1); 35542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 35642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s1)"); 35742bfe448SAnup Patel core.regs.s1 = data; 35842bfe448SAnup Patel 35942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a0); 36042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 36142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a0)"); 36242bfe448SAnup Patel core.regs.a0 = data; 36342bfe448SAnup Patel 36442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a1); 36542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 36642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a1)"); 36742bfe448SAnup Patel core.regs.a1 = data; 36842bfe448SAnup Patel 36942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a2); 37042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 37142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a2)"); 37242bfe448SAnup Patel core.regs.a2 = data; 37342bfe448SAnup Patel 37442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a3); 37542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 37642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a3)"); 37742bfe448SAnup Patel core.regs.a3 = data; 37842bfe448SAnup Patel 37942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a4); 38042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 38142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a4)"); 38242bfe448SAnup Patel core.regs.a4 = data; 38342bfe448SAnup Patel 38442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a5); 38542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 38642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a5)"); 38742bfe448SAnup Patel core.regs.a5 = data; 38842bfe448SAnup Patel 38942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a6); 39042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 39142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a6)"); 39242bfe448SAnup Patel core.regs.a6 = data; 39342bfe448SAnup Patel 39442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.a7); 39542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 39642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (a7)"); 39742bfe448SAnup Patel core.regs.a7 = data; 39842bfe448SAnup Patel 39942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s2); 40042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 40142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s2)"); 40242bfe448SAnup Patel core.regs.s2 = data; 40342bfe448SAnup Patel 40442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s3); 40542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 40642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s3)"); 40742bfe448SAnup Patel core.regs.s3 = data; 40842bfe448SAnup Patel 40942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s4); 41042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 41142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s4)"); 41242bfe448SAnup Patel core.regs.s4 = data; 41342bfe448SAnup Patel 41442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s5); 41542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 41642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s5)"); 41742bfe448SAnup Patel core.regs.s5 = data; 41842bfe448SAnup Patel 41942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s6); 42042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 42142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s6)"); 42242bfe448SAnup Patel core.regs.s6 = data; 42342bfe448SAnup Patel 42442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s7); 42542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 42642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s7)"); 42742bfe448SAnup Patel core.regs.s7 = data; 42842bfe448SAnup Patel 42942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s8); 43042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 43142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s8)"); 43242bfe448SAnup Patel core.regs.s8 = data; 43342bfe448SAnup Patel 43442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s9); 43542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 43642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s9)"); 43742bfe448SAnup Patel core.regs.s9 = data; 43842bfe448SAnup Patel 43942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s10); 44042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 44142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s10)"); 44242bfe448SAnup Patel core.regs.s10 = data; 44342bfe448SAnup Patel 44442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.s11); 44542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 44642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (s11)"); 44742bfe448SAnup Patel core.regs.s11 = data; 44842bfe448SAnup Patel 44942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t3); 45042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 45142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t3)"); 45242bfe448SAnup Patel core.regs.t3 = data; 45342bfe448SAnup Patel 45442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t4); 45542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 45642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t4)"); 45742bfe448SAnup Patel core.regs.t4 = data; 45842bfe448SAnup Patel 45942bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t5); 46042bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 46142bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t5)"); 46242bfe448SAnup Patel core.regs.t5 = data; 46342bfe448SAnup Patel 46442bfe448SAnup Patel reg.id = RISCV_CORE_REG(regs.t6); 46542bfe448SAnup Patel if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 46642bfe448SAnup Patel die("KVM_GET_ONE_REG failed (t6)"); 46742bfe448SAnup Patel core.regs.t6 = data; 46842bfe448SAnup Patel 46942bfe448SAnup Patel dprintf(debug_fd, "\n General Purpose Registers:\n"); 47042bfe448SAnup Patel dprintf(debug_fd, " -------------------------\n"); 47142bfe448SAnup Patel dprintf(debug_fd, " MODE: 0x%lx\n", data); 47242bfe448SAnup Patel dprintf(debug_fd, " PC: 0x%016lx RA: 0x%016lx SP: 0x%016lx GP: 0x%016lx\n", 47342bfe448SAnup Patel core.regs.pc, core.regs.ra, core.regs.sp, core.regs.gp); 47442bfe448SAnup Patel dprintf(debug_fd, " TP: 0x%016lx T0: 0x%016lx T1: 0x%016lx T2: 0x%016lx\n", 47542bfe448SAnup Patel core.regs.tp, core.regs.t0, core.regs.t1, core.regs.t2); 47642bfe448SAnup Patel dprintf(debug_fd, " S0: 0x%016lx S1: 0x%016lx A0: 0x%016lx A1: 0x%016lx\n", 47742bfe448SAnup Patel core.regs.s0, core.regs.s1, core.regs.a0, core.regs.a1); 47842bfe448SAnup Patel dprintf(debug_fd, " A2: 0x%016lx A3: 0x%016lx A4: 0x%016lx A5: 0x%016lx\n", 47942bfe448SAnup Patel core.regs.a2, core.regs.a3, core.regs.a4, core.regs.a5); 48042bfe448SAnup Patel dprintf(debug_fd, " A6: 0x%016lx A7: 0x%016lx S2: 0x%016lx S3: 0x%016lx\n", 48142bfe448SAnup Patel core.regs.a6, core.regs.a7, core.regs.s2, core.regs.s3); 48242bfe448SAnup Patel dprintf(debug_fd, " S4: 0x%016lx S5: 0x%016lx S6: 0x%016lx S7: 0x%016lx\n", 48342bfe448SAnup Patel core.regs.s4, core.regs.s5, core.regs.s6, core.regs.s7); 48442bfe448SAnup Patel dprintf(debug_fd, " S8: 0x%016lx S9: 0x%016lx S10: 0x%016lx S11: 0x%016lx\n", 48542bfe448SAnup Patel core.regs.s8, core.regs.s9, core.regs.s10, core.regs.s11); 48642bfe448SAnup Patel dprintf(debug_fd, " T3: 0x%016lx T4: 0x%016lx T5: 0x%016lx T6: 0x%016lx\n", 48742bfe448SAnup Patel core.regs.t3, core.regs.t4, core.regs.t5, core.regs.t6); 48842bfe448SAnup Patel 48942bfe448SAnup Patel kvm_cpu__show_csrs(vcpu); 4902e996783SAnup Patel } 491