xref: /kvmtool/riscv/kvm-cpu.c (revision 42bfe448c1c38c877cc81ebd403d5fb85bb927a7)
12e996783SAnup Patel #include "kvm/kvm-cpu.h"
22e996783SAnup Patel #include "kvm/kvm.h"
32e996783SAnup Patel #include "kvm/virtio.h"
42e996783SAnup Patel #include "kvm/term.h"
52e996783SAnup Patel 
62e996783SAnup Patel #include <asm/ptrace.h>
72e996783SAnup Patel 
82e996783SAnup Patel static int debug_fd;
92e996783SAnup Patel 
102e996783SAnup Patel void kvm_cpu__set_debug_fd(int fd)
112e996783SAnup Patel {
122e996783SAnup Patel 	debug_fd = fd;
132e996783SAnup Patel }
142e996783SAnup Patel 
152e996783SAnup Patel int kvm_cpu__get_debug_fd(void)
162e996783SAnup Patel {
172e996783SAnup Patel 	return debug_fd;
182e996783SAnup Patel }
192e996783SAnup Patel 
20*42bfe448SAnup Patel static __u64 __kvm_reg_id(__u64 type, __u64 idx, __u64  size)
21*42bfe448SAnup Patel {
22*42bfe448SAnup Patel 	return KVM_REG_RISCV | type | idx | size;
23*42bfe448SAnup Patel }
24*42bfe448SAnup Patel 
25*42bfe448SAnup Patel #if __riscv_xlen == 64
26*42bfe448SAnup Patel #define KVM_REG_SIZE_ULONG	KVM_REG_SIZE_U64
27*42bfe448SAnup Patel #else
28*42bfe448SAnup Patel #define KVM_REG_SIZE_ULONG	KVM_REG_SIZE_U32
29*42bfe448SAnup Patel #endif
30*42bfe448SAnup Patel 
31*42bfe448SAnup Patel #define RISCV_CONFIG_REG(name)	__kvm_reg_id(KVM_REG_RISCV_CONFIG, \
32*42bfe448SAnup Patel 					     KVM_REG_RISCV_CONFIG_REG(name), \
33*42bfe448SAnup Patel 					     KVM_REG_SIZE_ULONG)
34*42bfe448SAnup Patel 
35*42bfe448SAnup Patel #define RISCV_CORE_REG(name)	__kvm_reg_id(KVM_REG_RISCV_CORE, \
36*42bfe448SAnup Patel 					     KVM_REG_RISCV_CORE_REG(name), \
37*42bfe448SAnup Patel 					     KVM_REG_SIZE_ULONG)
38*42bfe448SAnup Patel 
39*42bfe448SAnup Patel #define RISCV_CSR_REG(name)	__kvm_reg_id(KVM_REG_RISCV_CSR, \
40*42bfe448SAnup Patel 					     KVM_REG_RISCV_CSR_REG(name), \
41*42bfe448SAnup Patel 					     KVM_REG_SIZE_ULONG)
42*42bfe448SAnup Patel 
43*42bfe448SAnup Patel #define RISCV_TIMER_REG(name)	__kvm_reg_id(KVM_REG_RISCV_TIMER, \
44*42bfe448SAnup Patel 					     KVM_REG_RISCV_TIMER_REG(name), \
45*42bfe448SAnup Patel 					     KVM_REG_SIZE_U64)
46*42bfe448SAnup Patel 
472e996783SAnup Patel struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
482e996783SAnup Patel {
49*42bfe448SAnup Patel 	struct kvm_cpu *vcpu;
50*42bfe448SAnup Patel 	u64 timebase = 0;
51*42bfe448SAnup Patel 	unsigned long isa = 0;
52*42bfe448SAnup Patel 	int coalesced_offset, mmap_size;
53*42bfe448SAnup Patel 	struct kvm_one_reg reg;
54*42bfe448SAnup Patel 
55*42bfe448SAnup Patel 	vcpu = calloc(1, sizeof(struct kvm_cpu));
56*42bfe448SAnup Patel 	if (!vcpu)
572e996783SAnup Patel 		return NULL;
58*42bfe448SAnup Patel 
59*42bfe448SAnup Patel 	vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
60*42bfe448SAnup Patel 	if (vcpu->vcpu_fd < 0)
61*42bfe448SAnup Patel 		die_perror("KVM_CREATE_VCPU ioctl");
62*42bfe448SAnup Patel 
63*42bfe448SAnup Patel 	reg.id = RISCV_CONFIG_REG(isa);
64*42bfe448SAnup Patel 	reg.addr = (unsigned long)&isa;
65*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
66*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (config.isa)");
67*42bfe448SAnup Patel 
68*42bfe448SAnup Patel 	reg.id = RISCV_TIMER_REG(frequency);
69*42bfe448SAnup Patel 	reg.addr = (unsigned long)&timebase;
70*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
71*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (timer.frequency)");
72*42bfe448SAnup Patel 
73*42bfe448SAnup Patel 	mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
74*42bfe448SAnup Patel 	if (mmap_size < 0)
75*42bfe448SAnup Patel 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
76*42bfe448SAnup Patel 
77*42bfe448SAnup Patel 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED,
78*42bfe448SAnup Patel 			     vcpu->vcpu_fd, 0);
79*42bfe448SAnup Patel 	if (vcpu->kvm_run == MAP_FAILED)
80*42bfe448SAnup Patel 		die("unable to mmap vcpu fd");
81*42bfe448SAnup Patel 
82*42bfe448SAnup Patel 	coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION,
83*42bfe448SAnup Patel 				 KVM_CAP_COALESCED_MMIO);
84*42bfe448SAnup Patel 	if (coalesced_offset)
85*42bfe448SAnup Patel 		vcpu->ring = (void *)vcpu->kvm_run +
86*42bfe448SAnup Patel 			     (coalesced_offset * PAGE_SIZE);
87*42bfe448SAnup Patel 
88*42bfe448SAnup Patel 	reg.id = RISCV_CONFIG_REG(isa);
89*42bfe448SAnup Patel 	reg.addr = (unsigned long)&isa;
90*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
91*42bfe448SAnup Patel 		die("KVM_SET_ONE_REG failed (config.isa)");
92*42bfe448SAnup Patel 
93*42bfe448SAnup Patel 	/* Populate the vcpu structure. */
94*42bfe448SAnup Patel 	vcpu->kvm		= kvm;
95*42bfe448SAnup Patel 	vcpu->cpu_id		= cpu_id;
96*42bfe448SAnup Patel 	vcpu->riscv_isa		= isa;
97*42bfe448SAnup Patel 	vcpu->riscv_xlen	= __riscv_xlen;
98*42bfe448SAnup Patel 	vcpu->riscv_timebase	= timebase;
99*42bfe448SAnup Patel 	vcpu->is_running	= true;
100*42bfe448SAnup Patel 
101*42bfe448SAnup Patel 	return vcpu;
1022e996783SAnup Patel }
1032e996783SAnup Patel 
1042e996783SAnup Patel void kvm_cpu__arch_nmi(struct kvm_cpu *cpu)
1052e996783SAnup Patel {
1062e996783SAnup Patel }
1072e996783SAnup Patel 
1082e996783SAnup Patel void kvm_cpu__delete(struct kvm_cpu *vcpu)
1092e996783SAnup Patel {
110*42bfe448SAnup Patel 	free(vcpu);
1112e996783SAnup Patel }
1122e996783SAnup Patel 
1132e996783SAnup Patel bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
1142e996783SAnup Patel {
1152e996783SAnup Patel 	/* TODO: */
1162e996783SAnup Patel 	return false;
1172e996783SAnup Patel }
1182e996783SAnup Patel 
1192e996783SAnup Patel void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
1202e996783SAnup Patel {
1212e996783SAnup Patel }
1222e996783SAnup Patel 
1232e996783SAnup Patel void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
1242e996783SAnup Patel {
125*42bfe448SAnup Patel 	struct kvm *kvm = vcpu->kvm;
126*42bfe448SAnup Patel 	struct kvm_mp_state mp_state;
127*42bfe448SAnup Patel 	struct kvm_one_reg reg;
128*42bfe448SAnup Patel 	unsigned long data;
129*42bfe448SAnup Patel 
130*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_MP_STATE, &mp_state) < 0)
131*42bfe448SAnup Patel 		die_perror("KVM_GET_MP_STATE failed");
132*42bfe448SAnup Patel 
133*42bfe448SAnup Patel 	/*
134*42bfe448SAnup Patel 	 * If MP state is stopped then it means Linux KVM RISC-V emulates
135*42bfe448SAnup Patel 	 * SBI v0.2 (or higher) with HART power managment and give VCPU
136*42bfe448SAnup Patel 	 * will power-up at boot-time by boot VCPU. For such VCPU, we
137*42bfe448SAnup Patel 	 * don't update PC, A0 and A1 here.
138*42bfe448SAnup Patel 	 */
139*42bfe448SAnup Patel 	if (mp_state.mp_state == KVM_MP_STATE_STOPPED)
140*42bfe448SAnup Patel 		return;
141*42bfe448SAnup Patel 
142*42bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
143*42bfe448SAnup Patel 
144*42bfe448SAnup Patel 	data	= kvm->arch.kern_guest_start;
145*42bfe448SAnup Patel 	reg.id	= RISCV_CORE_REG(regs.pc);
146*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
147*42bfe448SAnup Patel 		die_perror("KVM_SET_ONE_REG failed (pc)");
148*42bfe448SAnup Patel 
149*42bfe448SAnup Patel 	data	= vcpu->cpu_id;
150*42bfe448SAnup Patel 	reg.id	= RISCV_CORE_REG(regs.a0);
151*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
152*42bfe448SAnup Patel 		die_perror("KVM_SET_ONE_REG failed (a0)");
153*42bfe448SAnup Patel 
154*42bfe448SAnup Patel 	data	= kvm->arch.dtb_guest_start;
155*42bfe448SAnup Patel 	reg.id	= RISCV_CORE_REG(regs.a1);
156*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
157*42bfe448SAnup Patel 		die_perror("KVM_SET_ONE_REG failed (a1)");
1582e996783SAnup Patel }
1592e996783SAnup Patel 
1602e996783SAnup Patel int kvm_cpu__get_endianness(struct kvm_cpu *vcpu)
1612e996783SAnup Patel {
1622e996783SAnup Patel 	return VIRTIO_ENDIAN_LE;
1632e996783SAnup Patel }
1642e996783SAnup Patel 
1652e996783SAnup Patel void kvm_cpu__show_code(struct kvm_cpu *vcpu)
1662e996783SAnup Patel {
167*42bfe448SAnup Patel 	struct kvm_one_reg reg;
168*42bfe448SAnup Patel 	unsigned long data;
169*42bfe448SAnup Patel 	int debug_fd = kvm_cpu__get_debug_fd();
170*42bfe448SAnup Patel 
171*42bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
172*42bfe448SAnup Patel 
173*42bfe448SAnup Patel 	dprintf(debug_fd, "\n*PC:\n");
174*42bfe448SAnup Patel 	reg.id = RISCV_CORE_REG(regs.pc);
175*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
176*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (show_code @ PC)");
177*42bfe448SAnup Patel 
178*42bfe448SAnup Patel 	kvm__dump_mem(vcpu->kvm, data, 32, debug_fd);
179*42bfe448SAnup Patel 
180*42bfe448SAnup Patel 	dprintf(debug_fd, "\n*RA:\n");
181*42bfe448SAnup Patel 	reg.id = RISCV_CORE_REG(regs.ra);
182*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
183*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (show_code @ RA)");
184*42bfe448SAnup Patel 
185*42bfe448SAnup Patel 	kvm__dump_mem(vcpu->kvm, data, 32, debug_fd);
186*42bfe448SAnup Patel }
187*42bfe448SAnup Patel 
188*42bfe448SAnup Patel static void kvm_cpu__show_csrs(struct kvm_cpu *vcpu)
189*42bfe448SAnup Patel {
190*42bfe448SAnup Patel 	struct kvm_one_reg reg;
191*42bfe448SAnup Patel 	struct kvm_riscv_csr csr;
192*42bfe448SAnup Patel 	unsigned long data;
193*42bfe448SAnup Patel 	int debug_fd = kvm_cpu__get_debug_fd();
194*42bfe448SAnup Patel 
195*42bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
196*42bfe448SAnup Patel 	dprintf(debug_fd, "\n Control Status Registers:\n");
197*42bfe448SAnup Patel 	dprintf(debug_fd,   " ------------------------\n");
198*42bfe448SAnup Patel 
199*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sstatus);
200*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
201*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sstatus)");
202*42bfe448SAnup Patel 	csr.sstatus = data;
203*42bfe448SAnup Patel 
204*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sie);
205*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
206*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sie)");
207*42bfe448SAnup Patel 	csr.sie = data;
208*42bfe448SAnup Patel 
209*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(stvec);
210*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
211*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (stvec)");
212*42bfe448SAnup Patel 	csr.stvec = data;
213*42bfe448SAnup Patel 
214*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sip);
215*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
216*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sip)");
217*42bfe448SAnup Patel 	csr.sip = data;
218*42bfe448SAnup Patel 
219*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(satp);
220*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
221*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (satp)");
222*42bfe448SAnup Patel 	csr.satp = data;
223*42bfe448SAnup Patel 
224*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(stval);
225*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
226*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (stval)");
227*42bfe448SAnup Patel 	csr.stval = data;
228*42bfe448SAnup Patel 
229*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(scause);
230*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
231*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (SCAUSE)");
232*42bfe448SAnup Patel 	csr.scause = data;
233*42bfe448SAnup Patel 
234*42bfe448SAnup Patel 	reg.id		= RISCV_CSR_REG(sscratch);
235*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
236*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sscartch)");
237*42bfe448SAnup Patel 	csr.sscratch = data;
238*42bfe448SAnup Patel 	dprintf(debug_fd, " SSTATUS:  0x%016lx\n", csr.sstatus);
239*42bfe448SAnup Patel 	dprintf(debug_fd, " SIE:      0x%016lx\n", csr.sie);
240*42bfe448SAnup Patel 	dprintf(debug_fd, " STVEC:    0x%016lx\n", csr.stvec);
241*42bfe448SAnup Patel 	dprintf(debug_fd, " SIP:      0x%016lx\n", csr.sip);
242*42bfe448SAnup Patel 	dprintf(debug_fd, " SATP:     0x%016lx\n", csr.satp);
243*42bfe448SAnup Patel 	dprintf(debug_fd, " STVAL:    0x%016lx\n", csr.stval);
244*42bfe448SAnup Patel 	dprintf(debug_fd, " SCAUSE:   0x%016lx\n", csr.scause);
245*42bfe448SAnup Patel 	dprintf(debug_fd, " SSCRATCH: 0x%016lx\n", csr.sscratch);
2462e996783SAnup Patel }
2472e996783SAnup Patel 
2482e996783SAnup Patel void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
2492e996783SAnup Patel {
250*42bfe448SAnup Patel 	struct kvm_one_reg reg;
251*42bfe448SAnup Patel 	unsigned long data;
252*42bfe448SAnup Patel 	int debug_fd = kvm_cpu__get_debug_fd();
253*42bfe448SAnup Patel 	struct kvm_riscv_core core;
254*42bfe448SAnup Patel 
255*42bfe448SAnup Patel 	reg.addr = (unsigned long)&data;
256*42bfe448SAnup Patel 
257*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(mode);
258*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
259*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (mode)");
260*42bfe448SAnup Patel 	core.mode = data;
261*42bfe448SAnup Patel 
262*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.pc);
263*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
264*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (pc)");
265*42bfe448SAnup Patel 	core.regs.pc = data;
266*42bfe448SAnup Patel 
267*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.ra);
268*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
269*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (ra)");
270*42bfe448SAnup Patel 	core.regs.ra = data;
271*42bfe448SAnup Patel 
272*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.sp);
273*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
274*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (sp)");
275*42bfe448SAnup Patel 	core.regs.sp = data;
276*42bfe448SAnup Patel 
277*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.gp);
278*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
279*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (gp)");
280*42bfe448SAnup Patel 	core.regs.gp = data;
281*42bfe448SAnup Patel 
282*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.tp);
283*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
284*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (tp)");
285*42bfe448SAnup Patel 	core.regs.tp = data;
286*42bfe448SAnup Patel 
287*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t0);
288*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
289*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t0)");
290*42bfe448SAnup Patel 	core.regs.t0 = data;
291*42bfe448SAnup Patel 
292*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t1);
293*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
294*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t1)");
295*42bfe448SAnup Patel 	core.regs.t1 = data;
296*42bfe448SAnup Patel 
297*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t2);
298*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
299*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t2)");
300*42bfe448SAnup Patel 	core.regs.t2 = data;
301*42bfe448SAnup Patel 
302*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s0);
303*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
304*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s0)");
305*42bfe448SAnup Patel 	core.regs.s0 = data;
306*42bfe448SAnup Patel 
307*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s1);
308*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
309*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s1)");
310*42bfe448SAnup Patel 	core.regs.s1 = data;
311*42bfe448SAnup Patel 
312*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a0);
313*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
314*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a0)");
315*42bfe448SAnup Patel 	core.regs.a0 = data;
316*42bfe448SAnup Patel 
317*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a1);
318*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
319*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a1)");
320*42bfe448SAnup Patel 	core.regs.a1 = data;
321*42bfe448SAnup Patel 
322*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a2);
323*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
324*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a2)");
325*42bfe448SAnup Patel 	core.regs.a2 = data;
326*42bfe448SAnup Patel 
327*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a3);
328*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
329*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a3)");
330*42bfe448SAnup Patel 	core.regs.a3 = data;
331*42bfe448SAnup Patel 
332*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a4);
333*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
334*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a4)");
335*42bfe448SAnup Patel 	core.regs.a4 = data;
336*42bfe448SAnup Patel 
337*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a5);
338*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
339*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a5)");
340*42bfe448SAnup Patel 	core.regs.a5 = data;
341*42bfe448SAnup Patel 
342*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a6);
343*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
344*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a6)");
345*42bfe448SAnup Patel 	core.regs.a6 = data;
346*42bfe448SAnup Patel 
347*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.a7);
348*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
349*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (a7)");
350*42bfe448SAnup Patel 	core.regs.a7 = data;
351*42bfe448SAnup Patel 
352*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s2);
353*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
354*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s2)");
355*42bfe448SAnup Patel 	core.regs.s2 = data;
356*42bfe448SAnup Patel 
357*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s3);
358*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
359*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s3)");
360*42bfe448SAnup Patel 	core.regs.s3 = data;
361*42bfe448SAnup Patel 
362*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s4);
363*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
364*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s4)");
365*42bfe448SAnup Patel 	core.regs.s4 = data;
366*42bfe448SAnup Patel 
367*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s5);
368*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
369*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s5)");
370*42bfe448SAnup Patel 	core.regs.s5 = data;
371*42bfe448SAnup Patel 
372*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s6);
373*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
374*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s6)");
375*42bfe448SAnup Patel 	core.regs.s6 = data;
376*42bfe448SAnup Patel 
377*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s7);
378*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
379*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s7)");
380*42bfe448SAnup Patel 	core.regs.s7 = data;
381*42bfe448SAnup Patel 
382*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s8);
383*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
384*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s8)");
385*42bfe448SAnup Patel 	core.regs.s8 = data;
386*42bfe448SAnup Patel 
387*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s9);
388*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
389*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s9)");
390*42bfe448SAnup Patel 	core.regs.s9 = data;
391*42bfe448SAnup Patel 
392*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s10);
393*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
394*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s10)");
395*42bfe448SAnup Patel 	core.regs.s10 = data;
396*42bfe448SAnup Patel 
397*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.s11);
398*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
399*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (s11)");
400*42bfe448SAnup Patel 	core.regs.s11 = data;
401*42bfe448SAnup Patel 
402*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t3);
403*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
404*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t3)");
405*42bfe448SAnup Patel 	core.regs.t3 = data;
406*42bfe448SAnup Patel 
407*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t4);
408*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
409*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t4)");
410*42bfe448SAnup Patel 	core.regs.t4 = data;
411*42bfe448SAnup Patel 
412*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t5);
413*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
414*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t5)");
415*42bfe448SAnup Patel 	core.regs.t5 = data;
416*42bfe448SAnup Patel 
417*42bfe448SAnup Patel 	reg.id		= RISCV_CORE_REG(regs.t6);
418*42bfe448SAnup Patel 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
419*42bfe448SAnup Patel 		die("KVM_GET_ONE_REG failed (t6)");
420*42bfe448SAnup Patel 	core.regs.t6 = data;
421*42bfe448SAnup Patel 
422*42bfe448SAnup Patel 	dprintf(debug_fd, "\n General Purpose Registers:\n");
423*42bfe448SAnup Patel 	dprintf(debug_fd,   " -------------------------\n");
424*42bfe448SAnup Patel 	dprintf(debug_fd, " MODE:  0x%lx\n", data);
425*42bfe448SAnup Patel 	dprintf(debug_fd, " PC: 0x%016lx   RA: 0x%016lx SP: 0x%016lx GP: 0x%016lx\n",
426*42bfe448SAnup Patel 		core.regs.pc, core.regs.ra, core.regs.sp, core.regs.gp);
427*42bfe448SAnup Patel 	dprintf(debug_fd, " TP: 0x%016lx   T0: 0x%016lx T1: 0x%016lx T2: 0x%016lx\n",
428*42bfe448SAnup Patel 		core.regs.tp, core.regs.t0, core.regs.t1, core.regs.t2);
429*42bfe448SAnup Patel 	dprintf(debug_fd, " S0: 0x%016lx   S1: 0x%016lx A0: 0x%016lx A1: 0x%016lx\n",
430*42bfe448SAnup Patel 		core.regs.s0, core.regs.s1, core.regs.a0, core.regs.a1);
431*42bfe448SAnup Patel 	dprintf(debug_fd, " A2: 0x%016lx   A3: 0x%016lx A4: 0x%016lx A5: 0x%016lx\n",
432*42bfe448SAnup Patel 		core.regs.a2, core.regs.a3, core.regs.a4, core.regs.a5);
433*42bfe448SAnup Patel 	dprintf(debug_fd, " A6: 0x%016lx   A7: 0x%016lx S2: 0x%016lx S3: 0x%016lx\n",
434*42bfe448SAnup Patel 		core.regs.a6, core.regs.a7, core.regs.s2, core.regs.s3);
435*42bfe448SAnup Patel 	dprintf(debug_fd, " S4: 0x%016lx   S5: 0x%016lx S6: 0x%016lx S7: 0x%016lx\n",
436*42bfe448SAnup Patel 		core.regs.s4, core.regs.s5, core.regs.s6, core.regs.s7);
437*42bfe448SAnup Patel 	dprintf(debug_fd, " S8: 0x%016lx   S9: 0x%016lx S10: 0x%016lx S11: 0x%016lx\n",
438*42bfe448SAnup Patel 		core.regs.s8, core.regs.s9, core.regs.s10, core.regs.s11);
439*42bfe448SAnup Patel 	dprintf(debug_fd, " T3: 0x%016lx   T4: 0x%016lx T5: 0x%016lx T6: 0x%016lx\n",
440*42bfe448SAnup Patel 		core.regs.t3, core.regs.t4, core.regs.t5, core.regs.t6);
441*42bfe448SAnup Patel 
442*42bfe448SAnup Patel 	kvm_cpu__show_csrs(vcpu);
4432e996783SAnup Patel }
444