xref: /kvmtool/riscv/kvm-cpu.c (revision a416fdc2e664b8b8faf653fec636f93c187d2529)
12e996783SAnup Patel #include "kvm/kvm-cpu.h"
22e996783SAnup Patel #include "kvm/kvm.h"
32e996783SAnup Patel #include "kvm/virtio.h"
4721da166SAnup 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 
212e996783SAnup Patel struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
222e996783SAnup Patel {
2342bfe448SAnup Patel 	struct kvm_cpu *vcpu;
2442bfe448SAnup Patel 	u64 timebase = 0;
259e9cfde5SAnup Patel 	unsigned long isa = 0, id = 0;
26*a416fdc2SAnup Patel 	unsigned long masks[KVM_REG_RISCV_SBI_MULTI_REG_LAST + 1] = { 0 };
27*a416fdc2SAnup Patel 	int i, coalesced_offset, mmap_size;
2842bfe448SAnup Patel 	struct kvm_one_reg reg;
2942bfe448SAnup Patel 
3042bfe448SAnup Patel 	vcpu = calloc(1, sizeof(struct kvm_cpu));
3142bfe448SAnup Patel 	if (!vcpu)
322e996783SAnup Patel 		return NULL;
3342bfe448SAnup Patel 
3442bfe448SAnup Patel 	vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
3542bfe448SAnup Patel 	if (vcpu->vcpu_fd < 0)
3642bfe448SAnup Patel 		die_perror("KVM_CREATE_VCPU ioctl");
3742bfe448SAnup Patel 
3842bfe448SAnup Patel 	reg.id = RISCV_CONFIG_REG(isa);
3942bfe448SAnup Patel 	reg.addr = (unsigned long)&isa;
4042bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
4142bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (config.isa)");
4242bfe448SAnup Patel 
4342bfe448SAnup Patel 	reg.id = RISCV_TIMER_REG(frequency);
4442bfe448SAnup Patel 	reg.addr = (unsigned long)&timebase;
4542bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
4642bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (timer.frequency)");
4742bfe448SAnup Patel 
4842bfe448SAnup Patel 	mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
4942bfe448SAnup Patel 	if (mmap_size < 0)
5042bfe448SAnup Patel 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
5142bfe448SAnup Patel 
5242bfe448SAnup Patel 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED,
5342bfe448SAnup Patel 			     vcpu->vcpu_fd, 0);
5442bfe448SAnup Patel 	if (vcpu->kvm_run == MAP_FAILED)
5542bfe448SAnup Patel 		die("unable to mmap vcpu fd");
5642bfe448SAnup Patel 
5742bfe448SAnup Patel 	coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION,
5842bfe448SAnup Patel 				 KVM_CAP_COALESCED_MMIO);
5942bfe448SAnup Patel 	if (coalesced_offset)
6042bfe448SAnup Patel 		vcpu->ring = (void *)vcpu->kvm_run +
6142bfe448SAnup Patel 			     (coalesced_offset * PAGE_SIZE);
6242bfe448SAnup Patel 
6342bfe448SAnup Patel 	reg.id = RISCV_CONFIG_REG(isa);
6442bfe448SAnup Patel 	reg.addr = (unsigned long)&isa;
6542bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
6642bfe448SAnup Patel 		die("KVM_SET_ONE_REG failed (config.isa)");
6742bfe448SAnup Patel 
689e9cfde5SAnup Patel 	if (kvm->cfg.arch.custom_mvendorid) {
699e9cfde5SAnup Patel 		id = kvm->cfg.arch.custom_mvendorid;
709e9cfde5SAnup Patel 		reg.id = RISCV_CONFIG_REG(mvendorid);
719e9cfde5SAnup Patel 		reg.addr = (unsigned long)&id;
729e9cfde5SAnup Patel 		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
739e9cfde5SAnup Patel 			die("KVM_SET_ONE_REG failed (config.mvendorid)");
749e9cfde5SAnup Patel 	}
759e9cfde5SAnup Patel 
769e9cfde5SAnup Patel 	if (kvm->cfg.arch.custom_marchid) {
779e9cfde5SAnup Patel 		id = kvm->cfg.arch.custom_marchid;
789e9cfde5SAnup Patel 		reg.id = RISCV_CONFIG_REG(marchid);
799e9cfde5SAnup Patel 		reg.addr = (unsigned long)&id;
809e9cfde5SAnup Patel 		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
819e9cfde5SAnup Patel 			die("KVM_SET_ONE_REG failed (config.marchid)");
829e9cfde5SAnup Patel 	}
839e9cfde5SAnup Patel 
849e9cfde5SAnup Patel 	if (kvm->cfg.arch.custom_mimpid) {
859e9cfde5SAnup Patel 		id = kvm->cfg.arch.custom_mimpid;
869e9cfde5SAnup Patel 		reg.id = RISCV_CONFIG_REG(mimpid);
879e9cfde5SAnup Patel 		reg.addr = (unsigned long)&id;
889e9cfde5SAnup Patel 		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
899e9cfde5SAnup Patel 			die("KVM_SET_ONE_REG failed (config.mimpid)");
909e9cfde5SAnup Patel 	}
919e9cfde5SAnup Patel 
92*a416fdc2SAnup Patel 	for (i = 0; i < KVM_RISCV_SBI_EXT_MAX; i++) {
93*a416fdc2SAnup Patel 		if (!kvm->cfg.arch.sbi_ext_disabled[i])
94*a416fdc2SAnup Patel 			continue;
95*a416fdc2SAnup Patel 		masks[KVM_REG_RISCV_SBI_MULTI_REG(i)] |=
96*a416fdc2SAnup Patel 					KVM_REG_RISCV_SBI_MULTI_MASK(i);
97*a416fdc2SAnup Patel 	}
98*a416fdc2SAnup Patel 	for (i = 0; i <= KVM_REG_RISCV_SBI_MULTI_REG_LAST; i++) {
99*a416fdc2SAnup Patel 		if (!masks[i])
100*a416fdc2SAnup Patel 			continue;
101*a416fdc2SAnup Patel 
102*a416fdc2SAnup Patel 		reg.id = RISCV_SBI_EXT_REG(KVM_REG_RISCV_SBI_MULTI_DIS, i);
103*a416fdc2SAnup Patel 		reg.addr = (unsigned long)&masks[i];
104*a416fdc2SAnup Patel 		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
105*a416fdc2SAnup Patel 			die("KVM_SET_ONE_REG failed (sbi_ext %d)", i);
106*a416fdc2SAnup Patel 	}
107*a416fdc2SAnup Patel 
10842bfe448SAnup Patel 	/* Populate the vcpu structure. */
10942bfe448SAnup Patel 	vcpu->kvm		= kvm;
11042bfe448SAnup Patel 	vcpu->cpu_id		= cpu_id;
11142bfe448SAnup Patel 	vcpu->riscv_isa		= isa;
11242bfe448SAnup Patel 	vcpu->riscv_xlen	= __riscv_xlen;
11342bfe448SAnup Patel 	vcpu->riscv_timebase	= timebase;
11442bfe448SAnup Patel 	vcpu->is_running	= true;
11542bfe448SAnup Patel 
11642bfe448SAnup Patel 	return vcpu;
1172e996783SAnup Patel }
1182e996783SAnup Patel 
1192e996783SAnup Patel void kvm_cpu__arch_nmi(struct kvm_cpu *cpu)
1202e996783SAnup Patel {
1212e996783SAnup Patel }
1222e996783SAnup Patel 
1232e996783SAnup Patel void kvm_cpu__delete(struct kvm_cpu *vcpu)
1242e996783SAnup Patel {
12542bfe448SAnup Patel 	free(vcpu);
1262e996783SAnup Patel }
1272e996783SAnup Patel 
128721da166SAnup Patel static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu)
129721da166SAnup Patel {
130721da166SAnup Patel 	char ch;
131721da166SAnup Patel 	bool ret = true;
132721da166SAnup Patel 	int dfd = kvm_cpu__get_debug_fd();
133721da166SAnup Patel 
134721da166SAnup Patel 	switch (vcpu->kvm_run->riscv_sbi.extension_id) {
135721da166SAnup Patel 	case SBI_EXT_0_1_CONSOLE_PUTCHAR:
136721da166SAnup Patel 		ch = vcpu->kvm_run->riscv_sbi.args[0];
137721da166SAnup Patel 		term_putc(&ch, 1, 0);
138721da166SAnup Patel 		vcpu->kvm_run->riscv_sbi.ret[0] = 0;
139721da166SAnup Patel 		break;
140721da166SAnup Patel 	case SBI_EXT_0_1_CONSOLE_GETCHAR:
141721da166SAnup Patel 		if (term_readable(0))
142721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.ret[0] =
143721da166SAnup Patel 					term_getc(vcpu->kvm, 0);
144721da166SAnup Patel 		else
145721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.ret[0] = SBI_ERR_FAILURE;
146721da166SAnup Patel 		break;
147721da166SAnup Patel 	default:
148721da166SAnup Patel 		dprintf(dfd, "Unhandled SBI call\n");
149721da166SAnup Patel 		dprintf(dfd, "extension_id=0x%lx function_id=0x%lx\n",
150721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.extension_id,
151721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.function_id);
152721da166SAnup Patel 		dprintf(dfd, "args[0]=0x%lx args[1]=0x%lx\n",
153721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.args[0],
154721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.args[1]);
155721da166SAnup Patel 		dprintf(dfd, "args[2]=0x%lx args[3]=0x%lx\n",
156721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.args[2],
157721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.args[3]);
158721da166SAnup Patel 		dprintf(dfd, "args[4]=0x%lx args[5]=0x%lx\n",
159721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.args[4],
160721da166SAnup Patel 			vcpu->kvm_run->riscv_sbi.args[5]);
161721da166SAnup Patel 		ret = false;
162721da166SAnup Patel 		break;
163721da166SAnup Patel 	};
164721da166SAnup Patel 
165721da166SAnup Patel 	return ret;
166721da166SAnup Patel }
167721da166SAnup Patel 
1682e996783SAnup Patel bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
1692e996783SAnup Patel {
170721da166SAnup Patel 	switch (vcpu->kvm_run->exit_reason) {
171721da166SAnup Patel 	case KVM_EXIT_RISCV_SBI:
172721da166SAnup Patel 		return kvm_cpu_riscv_sbi(vcpu);
173721da166SAnup Patel 	default:
174721da166SAnup Patel 		break;
175721da166SAnup Patel 	};
176721da166SAnup Patel 
1772e996783SAnup Patel 	return false;
1782e996783SAnup Patel }
1792e996783SAnup Patel 
1802e996783SAnup Patel void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
1812e996783SAnup Patel {
1822e996783SAnup Patel }
1832e996783SAnup Patel 
1842e996783SAnup Patel void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
1852e996783SAnup Patel {
18642bfe448SAnup Patel 	struct kvm *kvm = vcpu->kvm;
18742bfe448SAnup Patel 	struct kvm_mp_state mp_state;
18842bfe448SAnup Patel 	struct kvm_one_reg reg;
18942bfe448SAnup Patel 	unsigned long data;
19042bfe448SAnup Patel 
19142bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_MP_STATE, &mp_state) < 0)
19242bfe448SAnup Patel 		die_perror("KVM_GET_MP_STATE failed");
19342bfe448SAnup Patel 
19442bfe448SAnup Patel 	/*
19542bfe448SAnup Patel 	 * If MP state is stopped then it means Linux KVM RISC-V emulates
19642bfe448SAnup Patel 	 * SBI v0.2 (or higher) with HART power managment and give VCPU
19742bfe448SAnup Patel 	 * will power-up at boot-time by boot VCPU. For such VCPU, we
19842bfe448SAnup Patel 	 * don't update PC, A0 and A1 here.
19942bfe448SAnup Patel 	 */
20042bfe448SAnup Patel 	if (mp_state.mp_state == KVM_MP_STATE_STOPPED)
20142bfe448SAnup Patel 		return;
20242bfe448SAnup Patel 
20342bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
20442bfe448SAnup Patel 
20542bfe448SAnup Patel 	data	= kvm->arch.kern_guest_start;
20642bfe448SAnup Patel 	reg.id	= RISCV_CORE_REG(regs.pc);
20742bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
20842bfe448SAnup Patel 		die_perror("KVM_SET_ONE_REG failed (pc)");
20942bfe448SAnup Patel 
21042bfe448SAnup Patel 	data	= vcpu->cpu_id;
21142bfe448SAnup Patel 	reg.id	= RISCV_CORE_REG(regs.a0);
21242bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
21342bfe448SAnup Patel 		die_perror("KVM_SET_ONE_REG failed (a0)");
21442bfe448SAnup Patel 
21542bfe448SAnup Patel 	data	= kvm->arch.dtb_guest_start;
21642bfe448SAnup Patel 	reg.id	= RISCV_CORE_REG(regs.a1);
21742bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
21842bfe448SAnup Patel 		die_perror("KVM_SET_ONE_REG failed (a1)");
2192e996783SAnup Patel }
2202e996783SAnup Patel 
2212e996783SAnup Patel int kvm_cpu__get_endianness(struct kvm_cpu *vcpu)
2222e996783SAnup Patel {
2232e996783SAnup Patel 	return VIRTIO_ENDIAN_LE;
2242e996783SAnup Patel }
2252e996783SAnup Patel 
2262e996783SAnup Patel void kvm_cpu__show_code(struct kvm_cpu *vcpu)
2272e996783SAnup Patel {
22842bfe448SAnup Patel 	struct kvm_one_reg reg;
22942bfe448SAnup Patel 	unsigned long data;
23042bfe448SAnup Patel 	int debug_fd = kvm_cpu__get_debug_fd();
23142bfe448SAnup Patel 
23242bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
23342bfe448SAnup Patel 
23442bfe448SAnup Patel 	dprintf(debug_fd, "\n*PC:\n");
23542bfe448SAnup Patel 	reg.id = RISCV_CORE_REG(regs.pc);
23642bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
23742bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (show_code @ PC)");
23842bfe448SAnup Patel 
23942bfe448SAnup Patel 	kvm__dump_mem(vcpu->kvm, data, 32, debug_fd);
24042bfe448SAnup Patel 
24142bfe448SAnup Patel 	dprintf(debug_fd, "\n*RA:\n");
24242bfe448SAnup Patel 	reg.id = RISCV_CORE_REG(regs.ra);
24342bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
24442bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (show_code @ RA)");
24542bfe448SAnup Patel 
24642bfe448SAnup Patel 	kvm__dump_mem(vcpu->kvm, data, 32, debug_fd);
24742bfe448SAnup Patel }
24842bfe448SAnup Patel 
24942bfe448SAnup Patel static void kvm_cpu__show_csrs(struct kvm_cpu *vcpu)
25042bfe448SAnup Patel {
25142bfe448SAnup Patel 	struct kvm_one_reg reg;
25242bfe448SAnup Patel 	struct kvm_riscv_csr csr;
25342bfe448SAnup Patel 	unsigned long data;
25442bfe448SAnup Patel 	int debug_fd = kvm_cpu__get_debug_fd();
25542bfe448SAnup Patel 
25642bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
25742bfe448SAnup Patel 	dprintf(debug_fd, "\n Control Status Registers:\n");
25842bfe448SAnup Patel 	dprintf(debug_fd,   " ------------------------\n");
25942bfe448SAnup Patel 
26042bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sstatus);
26142bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
26242bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sstatus)");
26342bfe448SAnup Patel 	csr.sstatus = data;
26442bfe448SAnup Patel 
26542bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sie);
26642bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
26742bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sie)");
26842bfe448SAnup Patel 	csr.sie = data;
26942bfe448SAnup Patel 
27042bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(stvec);
27142bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
27242bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (stvec)");
27342bfe448SAnup Patel 	csr.stvec = data;
27442bfe448SAnup Patel 
27542bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sip);
27642bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
27742bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sip)");
27842bfe448SAnup Patel 	csr.sip = data;
27942bfe448SAnup Patel 
28042bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(satp);
28142bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
28242bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (satp)");
28342bfe448SAnup Patel 	csr.satp = data;
28442bfe448SAnup Patel 
28542bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(stval);
28642bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
28742bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (stval)");
28842bfe448SAnup Patel 	csr.stval = data;
28942bfe448SAnup Patel 
29042bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(scause);
29142bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
29242bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (SCAUSE)");
29342bfe448SAnup Patel 	csr.scause = data;
29442bfe448SAnup Patel 
29542bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sscratch);
29642bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
29742bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sscartch)");
29842bfe448SAnup Patel 	csr.sscratch = data;
29942bfe448SAnup Patel 	dprintf(debug_fd, " SSTATUS:  0x%016lx\n", csr.sstatus);
30042bfe448SAnup Patel 	dprintf(debug_fd, " SIE:      0x%016lx\n", csr.sie);
30142bfe448SAnup Patel 	dprintf(debug_fd, " STVEC:    0x%016lx\n", csr.stvec);
30242bfe448SAnup Patel 	dprintf(debug_fd, " SIP:      0x%016lx\n", csr.sip);
30342bfe448SAnup Patel 	dprintf(debug_fd, " SATP:     0x%016lx\n", csr.satp);
30442bfe448SAnup Patel 	dprintf(debug_fd, " STVAL:    0x%016lx\n", csr.stval);
30542bfe448SAnup Patel 	dprintf(debug_fd, " SCAUSE:   0x%016lx\n", csr.scause);
30642bfe448SAnup Patel 	dprintf(debug_fd, " SSCRATCH: 0x%016lx\n", csr.sscratch);
3072e996783SAnup Patel }
3082e996783SAnup Patel 
3092e996783SAnup Patel void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
3102e996783SAnup Patel {
31142bfe448SAnup Patel 	struct kvm_one_reg reg;
31242bfe448SAnup Patel 	unsigned long data;
31342bfe448SAnup Patel 	int debug_fd = kvm_cpu__get_debug_fd();
31442bfe448SAnup Patel 	struct kvm_riscv_core core;
31542bfe448SAnup Patel 
31642bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
31742bfe448SAnup Patel 
31842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(mode);
31942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
32042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (mode)");
32142bfe448SAnup Patel 	core.mode = data;
32242bfe448SAnup Patel 
32342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.pc);
32442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
32542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (pc)");
32642bfe448SAnup Patel 	core.regs.pc = data;
32742bfe448SAnup Patel 
32842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.ra);
32942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
33042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (ra)");
33142bfe448SAnup Patel 	core.regs.ra = data;
33242bfe448SAnup Patel 
33342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.sp);
33442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
33542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sp)");
33642bfe448SAnup Patel 	core.regs.sp = data;
33742bfe448SAnup Patel 
33842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.gp);
33942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
34042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (gp)");
34142bfe448SAnup Patel 	core.regs.gp = data;
34242bfe448SAnup Patel 
34342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.tp);
34442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
34542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (tp)");
34642bfe448SAnup Patel 	core.regs.tp = data;
34742bfe448SAnup Patel 
34842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t0);
34942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
35042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t0)");
35142bfe448SAnup Patel 	core.regs.t0 = data;
35242bfe448SAnup Patel 
35342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t1);
35442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
35542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t1)");
35642bfe448SAnup Patel 	core.regs.t1 = data;
35742bfe448SAnup Patel 
35842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t2);
35942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
36042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t2)");
36142bfe448SAnup Patel 	core.regs.t2 = data;
36242bfe448SAnup Patel 
36342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s0);
36442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
36542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s0)");
36642bfe448SAnup Patel 	core.regs.s0 = data;
36742bfe448SAnup Patel 
36842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s1);
36942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
37042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s1)");
37142bfe448SAnup Patel 	core.regs.s1 = data;
37242bfe448SAnup Patel 
37342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a0);
37442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
37542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a0)");
37642bfe448SAnup Patel 	core.regs.a0 = data;
37742bfe448SAnup Patel 
37842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a1);
37942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
38042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a1)");
38142bfe448SAnup Patel 	core.regs.a1 = data;
38242bfe448SAnup Patel 
38342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a2);
38442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
38542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a2)");
38642bfe448SAnup Patel 	core.regs.a2 = data;
38742bfe448SAnup Patel 
38842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a3);
38942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
39042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a3)");
39142bfe448SAnup Patel 	core.regs.a3 = data;
39242bfe448SAnup Patel 
39342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a4);
39442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
39542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a4)");
39642bfe448SAnup Patel 	core.regs.a4 = data;
39742bfe448SAnup Patel 
39842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a5);
39942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
40042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a5)");
40142bfe448SAnup Patel 	core.regs.a5 = data;
40242bfe448SAnup Patel 
40342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a6);
40442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
40542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a6)");
40642bfe448SAnup Patel 	core.regs.a6 = data;
40742bfe448SAnup Patel 
40842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a7);
40942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
41042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a7)");
41142bfe448SAnup Patel 	core.regs.a7 = data;
41242bfe448SAnup Patel 
41342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s2);
41442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
41542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s2)");
41642bfe448SAnup Patel 	core.regs.s2 = data;
41742bfe448SAnup Patel 
41842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s3);
41942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
42042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s3)");
42142bfe448SAnup Patel 	core.regs.s3 = data;
42242bfe448SAnup Patel 
42342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s4);
42442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
42542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s4)");
42642bfe448SAnup Patel 	core.regs.s4 = data;
42742bfe448SAnup Patel 
42842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s5);
42942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
43042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s5)");
43142bfe448SAnup Patel 	core.regs.s5 = data;
43242bfe448SAnup Patel 
43342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s6);
43442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
43542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s6)");
43642bfe448SAnup Patel 	core.regs.s6 = data;
43742bfe448SAnup Patel 
43842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s7);
43942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
44042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s7)");
44142bfe448SAnup Patel 	core.regs.s7 = data;
44242bfe448SAnup Patel 
44342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s8);
44442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
44542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s8)");
44642bfe448SAnup Patel 	core.regs.s8 = data;
44742bfe448SAnup Patel 
44842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s9);
44942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
45042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s9)");
45142bfe448SAnup Patel 	core.regs.s9 = data;
45242bfe448SAnup Patel 
45342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s10);
45442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
45542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s10)");
45642bfe448SAnup Patel 	core.regs.s10 = data;
45742bfe448SAnup Patel 
45842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s11);
45942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
46042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s11)");
46142bfe448SAnup Patel 	core.regs.s11 = data;
46242bfe448SAnup Patel 
46342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t3);
46442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
46542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t3)");
46642bfe448SAnup Patel 	core.regs.t3 = data;
46742bfe448SAnup Patel 
46842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t4);
46942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
47042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t4)");
47142bfe448SAnup Patel 	core.regs.t4 = data;
47242bfe448SAnup Patel 
47342bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t5);
47442bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
47542bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t5)");
47642bfe448SAnup Patel 	core.regs.t5 = data;
47742bfe448SAnup Patel 
47842bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t6);
47942bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
48042bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t6)");
48142bfe448SAnup Patel 	core.regs.t6 = data;
48242bfe448SAnup Patel 
48342bfe448SAnup Patel 	dprintf(debug_fd, "\n General Purpose Registers:\n");
48442bfe448SAnup Patel 	dprintf(debug_fd,   " -------------------------\n");
48542bfe448SAnup Patel 	dprintf(debug_fd, " MODE:  0x%lx\n", data);
48642bfe448SAnup Patel 	dprintf(debug_fd, " PC: 0x%016lx   RA: 0x%016lx SP: 0x%016lx GP: 0x%016lx\n",
48742bfe448SAnup Patel 		core.regs.pc, core.regs.ra, core.regs.sp, core.regs.gp);
48842bfe448SAnup Patel 	dprintf(debug_fd, " TP: 0x%016lx   T0: 0x%016lx T1: 0x%016lx T2: 0x%016lx\n",
48942bfe448SAnup Patel 		core.regs.tp, core.regs.t0, core.regs.t1, core.regs.t2);
49042bfe448SAnup Patel 	dprintf(debug_fd, " S0: 0x%016lx   S1: 0x%016lx A0: 0x%016lx A1: 0x%016lx\n",
49142bfe448SAnup Patel 		core.regs.s0, core.regs.s1, core.regs.a0, core.regs.a1);
49242bfe448SAnup Patel 	dprintf(debug_fd, " A2: 0x%016lx   A3: 0x%016lx A4: 0x%016lx A5: 0x%016lx\n",
49342bfe448SAnup Patel 		core.regs.a2, core.regs.a3, core.regs.a4, core.regs.a5);
49442bfe448SAnup Patel 	dprintf(debug_fd, " A6: 0x%016lx   A7: 0x%016lx S2: 0x%016lx S3: 0x%016lx\n",
49542bfe448SAnup Patel 		core.regs.a6, core.regs.a7, core.regs.s2, core.regs.s3);
49642bfe448SAnup Patel 	dprintf(debug_fd, " S4: 0x%016lx   S5: 0x%016lx S6: 0x%016lx S7: 0x%016lx\n",
49742bfe448SAnup Patel 		core.regs.s4, core.regs.s5, core.regs.s6, core.regs.s7);
49842bfe448SAnup Patel 	dprintf(debug_fd, " S8: 0x%016lx   S9: 0x%016lx S10: 0x%016lx S11: 0x%016lx\n",
49942bfe448SAnup Patel 		core.regs.s8, core.regs.s9, core.regs.s10, core.regs.s11);
50042bfe448SAnup Patel 	dprintf(debug_fd, " T3: 0x%016lx   T4: 0x%016lx T5: 0x%016lx T6: 0x%016lx\n",
50142bfe448SAnup Patel 		core.regs.t3, core.regs.t4, core.regs.t5, core.regs.t6);
50242bfe448SAnup Patel 
50342bfe448SAnup Patel 	kvm_cpu__show_csrs(vcpu);
5042e996783SAnup Patel }
505