xref: /kvmtool/x86/kvm-cpu.c (revision af7b08685968ff8f6ffdf5cac1ef067688bce8c6)
1*af7b0868SMatt Evans #include "kvm/kvm-cpu.h"
2*af7b0868SMatt Evans 
3*af7b0868SMatt Evans #include "kvm/symbol.h"
4*af7b0868SMatt Evans #include "kvm/util.h"
5*af7b0868SMatt Evans #include "kvm/kvm.h"
6*af7b0868SMatt Evans 
7*af7b0868SMatt Evans #include <asm/msr-index.h>
8*af7b0868SMatt Evans 
9*af7b0868SMatt Evans #include <sys/ioctl.h>
10*af7b0868SMatt Evans #include <sys/mman.h>
11*af7b0868SMatt Evans #include <signal.h>
12*af7b0868SMatt Evans #include <stdlib.h>
13*af7b0868SMatt Evans #include <string.h>
14*af7b0868SMatt Evans #include <errno.h>
15*af7b0868SMatt Evans #include <stdio.h>
16*af7b0868SMatt Evans 
17*af7b0868SMatt Evans static int debug_fd;
18*af7b0868SMatt Evans 
19*af7b0868SMatt Evans void kvm_cpu__set_debug_fd(int fd)
20*af7b0868SMatt Evans {
21*af7b0868SMatt Evans 	debug_fd = fd;
22*af7b0868SMatt Evans }
23*af7b0868SMatt Evans 
24*af7b0868SMatt Evans int kvm_cpu__get_debug_fd(void)
25*af7b0868SMatt Evans {
26*af7b0868SMatt Evans 	return debug_fd;
27*af7b0868SMatt Evans }
28*af7b0868SMatt Evans 
29*af7b0868SMatt Evans static inline bool is_in_protected_mode(struct kvm_cpu *vcpu)
30*af7b0868SMatt Evans {
31*af7b0868SMatt Evans 	return vcpu->sregs.cr0 & 0x01;
32*af7b0868SMatt Evans }
33*af7b0868SMatt Evans 
34*af7b0868SMatt Evans static inline u64 ip_to_flat(struct kvm_cpu *vcpu, u64 ip)
35*af7b0868SMatt Evans {
36*af7b0868SMatt Evans 	u64 cs;
37*af7b0868SMatt Evans 
38*af7b0868SMatt Evans 	/*
39*af7b0868SMatt Evans 	 * NOTE! We should take code segment base address into account here.
40*af7b0868SMatt Evans 	 * Luckily it's usually zero because Linux uses flat memory model.
41*af7b0868SMatt Evans 	 */
42*af7b0868SMatt Evans 	if (is_in_protected_mode(vcpu))
43*af7b0868SMatt Evans 		return ip;
44*af7b0868SMatt Evans 
45*af7b0868SMatt Evans 	cs = vcpu->sregs.cs.selector;
46*af7b0868SMatt Evans 
47*af7b0868SMatt Evans 	return ip + (cs << 4);
48*af7b0868SMatt Evans }
49*af7b0868SMatt Evans 
50*af7b0868SMatt Evans static inline u32 selector_to_base(u16 selector)
51*af7b0868SMatt Evans {
52*af7b0868SMatt Evans 	/*
53*af7b0868SMatt Evans 	 * KVM on Intel requires 'base' to be 'selector * 16' in real mode.
54*af7b0868SMatt Evans 	 */
55*af7b0868SMatt Evans 	return (u32)selector * 16;
56*af7b0868SMatt Evans }
57*af7b0868SMatt Evans 
58*af7b0868SMatt Evans static struct kvm_cpu *kvm_cpu__new(struct kvm *kvm)
59*af7b0868SMatt Evans {
60*af7b0868SMatt Evans 	struct kvm_cpu *vcpu;
61*af7b0868SMatt Evans 
62*af7b0868SMatt Evans 	vcpu		= calloc(1, sizeof *vcpu);
63*af7b0868SMatt Evans 	if (!vcpu)
64*af7b0868SMatt Evans 		return NULL;
65*af7b0868SMatt Evans 
66*af7b0868SMatt Evans 	vcpu->kvm	= kvm;
67*af7b0868SMatt Evans 
68*af7b0868SMatt Evans 	return vcpu;
69*af7b0868SMatt Evans }
70*af7b0868SMatt Evans 
71*af7b0868SMatt Evans void kvm_cpu__delete(struct kvm_cpu *vcpu)
72*af7b0868SMatt Evans {
73*af7b0868SMatt Evans 	if (vcpu->msrs)
74*af7b0868SMatt Evans 		free(vcpu->msrs);
75*af7b0868SMatt Evans 
76*af7b0868SMatt Evans 	free(vcpu);
77*af7b0868SMatt Evans }
78*af7b0868SMatt Evans 
79*af7b0868SMatt Evans struct kvm_cpu *kvm_cpu__init(struct kvm *kvm, unsigned long cpu_id)
80*af7b0868SMatt Evans {
81*af7b0868SMatt Evans 	struct kvm_cpu *vcpu;
82*af7b0868SMatt Evans 	int mmap_size;
83*af7b0868SMatt Evans 	int coalesced_offset;
84*af7b0868SMatt Evans 
85*af7b0868SMatt Evans 	vcpu		= kvm_cpu__new(kvm);
86*af7b0868SMatt Evans 	if (!vcpu)
87*af7b0868SMatt Evans 		return NULL;
88*af7b0868SMatt Evans 
89*af7b0868SMatt Evans 	vcpu->cpu_id	= cpu_id;
90*af7b0868SMatt Evans 
91*af7b0868SMatt Evans 	vcpu->vcpu_fd = ioctl(vcpu->kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
92*af7b0868SMatt Evans 	if (vcpu->vcpu_fd < 0)
93*af7b0868SMatt Evans 		die_perror("KVM_CREATE_VCPU ioctl");
94*af7b0868SMatt Evans 
95*af7b0868SMatt Evans 	mmap_size = ioctl(vcpu->kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
96*af7b0868SMatt Evans 	if (mmap_size < 0)
97*af7b0868SMatt Evans 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
98*af7b0868SMatt Evans 
99*af7b0868SMatt Evans 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, vcpu->vcpu_fd, 0);
100*af7b0868SMatt Evans 	if (vcpu->kvm_run == MAP_FAILED)
101*af7b0868SMatt Evans 		die("unable to mmap vcpu fd");
102*af7b0868SMatt Evans 
103*af7b0868SMatt Evans 	coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_COALESCED_MMIO);
104*af7b0868SMatt Evans 	if (coalesced_offset)
105*af7b0868SMatt Evans 		vcpu->ring = (void *)vcpu->kvm_run + (coalesced_offset * PAGE_SIZE);
106*af7b0868SMatt Evans 
107*af7b0868SMatt Evans 	vcpu->is_running = true;
108*af7b0868SMatt Evans 
109*af7b0868SMatt Evans 	return vcpu;
110*af7b0868SMatt Evans }
111*af7b0868SMatt Evans 
112*af7b0868SMatt Evans static struct kvm_msrs *kvm_msrs__new(size_t nmsrs)
113*af7b0868SMatt Evans {
114*af7b0868SMatt Evans 	struct kvm_msrs *vcpu = calloc(1, sizeof(*vcpu) + (sizeof(struct kvm_msr_entry) * nmsrs));
115*af7b0868SMatt Evans 
116*af7b0868SMatt Evans 	if (!vcpu)
117*af7b0868SMatt Evans 		die("out of memory");
118*af7b0868SMatt Evans 
119*af7b0868SMatt Evans 	return vcpu;
120*af7b0868SMatt Evans }
121*af7b0868SMatt Evans 
122*af7b0868SMatt Evans #define KVM_MSR_ENTRY(_index, _data)	\
123*af7b0868SMatt Evans 	(struct kvm_msr_entry) { .index = _index, .data = _data }
124*af7b0868SMatt Evans 
125*af7b0868SMatt Evans static void kvm_cpu__setup_msrs(struct kvm_cpu *vcpu)
126*af7b0868SMatt Evans {
127*af7b0868SMatt Evans 	unsigned long ndx = 0;
128*af7b0868SMatt Evans 
129*af7b0868SMatt Evans 	vcpu->msrs = kvm_msrs__new(100);
130*af7b0868SMatt Evans 
131*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_CS,	0x0);
132*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_ESP,	0x0);
133*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_EIP,	0x0);
134*af7b0868SMatt Evans #ifdef CONFIG_X86_64
135*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_STAR,			0x0);
136*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_CSTAR,			0x0);
137*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_KERNEL_GS_BASE,		0x0);
138*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_SYSCALL_MASK,		0x0);
139*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_LSTAR,			0x0);
140*af7b0868SMatt Evans #endif
141*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_TSC,		0x0);
142*af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_MISC_ENABLE,
143*af7b0868SMatt Evans 						MSR_IA32_MISC_ENABLE_FAST_STRING);
144*af7b0868SMatt Evans 
145*af7b0868SMatt Evans 	vcpu->msrs->nmsrs	= ndx;
146*af7b0868SMatt Evans 
147*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_MSRS, vcpu->msrs) < 0)
148*af7b0868SMatt Evans 		die_perror("KVM_SET_MSRS failed");
149*af7b0868SMatt Evans }
150*af7b0868SMatt Evans 
151*af7b0868SMatt Evans static void kvm_cpu__setup_fpu(struct kvm_cpu *vcpu)
152*af7b0868SMatt Evans {
153*af7b0868SMatt Evans 	vcpu->fpu = (struct kvm_fpu) {
154*af7b0868SMatt Evans 		.fcw		= 0x37f,
155*af7b0868SMatt Evans 		.mxcsr		= 0x1f80,
156*af7b0868SMatt Evans 	};
157*af7b0868SMatt Evans 
158*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_FPU, &vcpu->fpu) < 0)
159*af7b0868SMatt Evans 		die_perror("KVM_SET_FPU failed");
160*af7b0868SMatt Evans }
161*af7b0868SMatt Evans 
162*af7b0868SMatt Evans static void kvm_cpu__setup_regs(struct kvm_cpu *vcpu)
163*af7b0868SMatt Evans {
164*af7b0868SMatt Evans 	vcpu->regs = (struct kvm_regs) {
165*af7b0868SMatt Evans 		/* We start the guest in 16-bit real mode  */
166*af7b0868SMatt Evans 		.rflags		= 0x0000000000000002ULL,
167*af7b0868SMatt Evans 
168*af7b0868SMatt Evans 		.rip		= vcpu->kvm->boot_ip,
169*af7b0868SMatt Evans 		.rsp		= vcpu->kvm->boot_sp,
170*af7b0868SMatt Evans 		.rbp		= vcpu->kvm->boot_sp,
171*af7b0868SMatt Evans 	};
172*af7b0868SMatt Evans 
173*af7b0868SMatt Evans 	if (vcpu->regs.rip > USHRT_MAX)
174*af7b0868SMatt Evans 		die("ip 0x%llx is too high for real mode", (u64) vcpu->regs.rip);
175*af7b0868SMatt Evans 
176*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_REGS, &vcpu->regs) < 0)
177*af7b0868SMatt Evans 		die_perror("KVM_SET_REGS failed");
178*af7b0868SMatt Evans }
179*af7b0868SMatt Evans 
180*af7b0868SMatt Evans static void kvm_cpu__setup_sregs(struct kvm_cpu *vcpu)
181*af7b0868SMatt Evans {
182*af7b0868SMatt Evans 
183*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &vcpu->sregs) < 0)
184*af7b0868SMatt Evans 		die_perror("KVM_GET_SREGS failed");
185*af7b0868SMatt Evans 
186*af7b0868SMatt Evans 	vcpu->sregs.cs.selector	= vcpu->kvm->boot_selector;
187*af7b0868SMatt Evans 	vcpu->sregs.cs.base	= selector_to_base(vcpu->kvm->boot_selector);
188*af7b0868SMatt Evans 	vcpu->sregs.ss.selector	= vcpu->kvm->boot_selector;
189*af7b0868SMatt Evans 	vcpu->sregs.ss.base	= selector_to_base(vcpu->kvm->boot_selector);
190*af7b0868SMatt Evans 	vcpu->sregs.ds.selector	= vcpu->kvm->boot_selector;
191*af7b0868SMatt Evans 	vcpu->sregs.ds.base	= selector_to_base(vcpu->kvm->boot_selector);
192*af7b0868SMatt Evans 	vcpu->sregs.es.selector	= vcpu->kvm->boot_selector;
193*af7b0868SMatt Evans 	vcpu->sregs.es.base	= selector_to_base(vcpu->kvm->boot_selector);
194*af7b0868SMatt Evans 	vcpu->sregs.fs.selector	= vcpu->kvm->boot_selector;
195*af7b0868SMatt Evans 	vcpu->sregs.fs.base	= selector_to_base(vcpu->kvm->boot_selector);
196*af7b0868SMatt Evans 	vcpu->sregs.gs.selector	= vcpu->kvm->boot_selector;
197*af7b0868SMatt Evans 	vcpu->sregs.gs.base	= selector_to_base(vcpu->kvm->boot_selector);
198*af7b0868SMatt Evans 
199*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_SREGS, &vcpu->sregs) < 0)
200*af7b0868SMatt Evans 		die_perror("KVM_SET_SREGS failed");
201*af7b0868SMatt Evans }
202*af7b0868SMatt Evans 
203*af7b0868SMatt Evans /**
204*af7b0868SMatt Evans  * kvm_cpu__reset_vcpu - reset virtual CPU to a known state
205*af7b0868SMatt Evans  */
206*af7b0868SMatt Evans void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
207*af7b0868SMatt Evans {
208*af7b0868SMatt Evans 	kvm_cpu__setup_cpuid(vcpu);
209*af7b0868SMatt Evans 	kvm_cpu__setup_sregs(vcpu);
210*af7b0868SMatt Evans 	kvm_cpu__setup_regs(vcpu);
211*af7b0868SMatt Evans 	kvm_cpu__setup_fpu(vcpu);
212*af7b0868SMatt Evans 	kvm_cpu__setup_msrs(vcpu);
213*af7b0868SMatt Evans }
214*af7b0868SMatt Evans 
215*af7b0868SMatt Evans static void print_dtable(const char *name, struct kvm_dtable *dtable)
216*af7b0868SMatt Evans {
217*af7b0868SMatt Evans 	dprintf(debug_fd, " %s                 %016llx  %08hx\n",
218*af7b0868SMatt Evans 		name, (u64) dtable->base, (u16) dtable->limit);
219*af7b0868SMatt Evans }
220*af7b0868SMatt Evans 
221*af7b0868SMatt Evans static void print_segment(const char *name, struct kvm_segment *seg)
222*af7b0868SMatt Evans {
223*af7b0868SMatt Evans 	dprintf(debug_fd, " %s       %04hx      %016llx  %08x  %02hhx    %x %x   %x  %x %x %x %x\n",
224*af7b0868SMatt Evans 		name, (u16) seg->selector, (u64) seg->base, (u32) seg->limit,
225*af7b0868SMatt Evans 		(u8) seg->type, seg->present, seg->dpl, seg->db, seg->s, seg->l, seg->g, seg->avl);
226*af7b0868SMatt Evans }
227*af7b0868SMatt Evans 
228*af7b0868SMatt Evans void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
229*af7b0868SMatt Evans {
230*af7b0868SMatt Evans 	unsigned long cr0, cr2, cr3;
231*af7b0868SMatt Evans 	unsigned long cr4, cr8;
232*af7b0868SMatt Evans 	unsigned long rax, rbx, rcx;
233*af7b0868SMatt Evans 	unsigned long rdx, rsi, rdi;
234*af7b0868SMatt Evans 	unsigned long rbp,  r8,  r9;
235*af7b0868SMatt Evans 	unsigned long r10, r11, r12;
236*af7b0868SMatt Evans 	unsigned long r13, r14, r15;
237*af7b0868SMatt Evans 	unsigned long rip, rsp;
238*af7b0868SMatt Evans 	struct kvm_sregs sregs;
239*af7b0868SMatt Evans 	unsigned long rflags;
240*af7b0868SMatt Evans 	struct kvm_regs regs;
241*af7b0868SMatt Evans 	int i;
242*af7b0868SMatt Evans 
243*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_REGS, &regs) < 0)
244*af7b0868SMatt Evans 		die("KVM_GET_REGS failed");
245*af7b0868SMatt Evans 
246*af7b0868SMatt Evans 	rflags = regs.rflags;
247*af7b0868SMatt Evans 
248*af7b0868SMatt Evans 	rip = regs.rip; rsp = regs.rsp;
249*af7b0868SMatt Evans 	rax = regs.rax; rbx = regs.rbx; rcx = regs.rcx;
250*af7b0868SMatt Evans 	rdx = regs.rdx; rsi = regs.rsi; rdi = regs.rdi;
251*af7b0868SMatt Evans 	rbp = regs.rbp; r8  = regs.r8;  r9  = regs.r9;
252*af7b0868SMatt Evans 	r10 = regs.r10; r11 = regs.r11; r12 = regs.r12;
253*af7b0868SMatt Evans 	r13 = regs.r13; r14 = regs.r14; r15 = regs.r15;
254*af7b0868SMatt Evans 
255*af7b0868SMatt Evans 	dprintf(debug_fd, "\n Registers:\n");
256*af7b0868SMatt Evans 	dprintf(debug_fd,   " ----------\n");
257*af7b0868SMatt Evans 	dprintf(debug_fd, " rip: %016lx   rsp: %016lx flags: %016lx\n", rip, rsp, rflags);
258*af7b0868SMatt Evans 	dprintf(debug_fd, " rax: %016lx   rbx: %016lx   rcx: %016lx\n", rax, rbx, rcx);
259*af7b0868SMatt Evans 	dprintf(debug_fd, " rdx: %016lx   rsi: %016lx   rdi: %016lx\n", rdx, rsi, rdi);
260*af7b0868SMatt Evans 	dprintf(debug_fd, " rbp: %016lx    r8: %016lx    r9: %016lx\n", rbp, r8,  r9);
261*af7b0868SMatt Evans 	dprintf(debug_fd, " r10: %016lx   r11: %016lx   r12: %016lx\n", r10, r11, r12);
262*af7b0868SMatt Evans 	dprintf(debug_fd, " r13: %016lx   r14: %016lx   r15: %016lx\n", r13, r14, r15);
263*af7b0868SMatt Evans 
264*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &sregs) < 0)
265*af7b0868SMatt Evans 		die("KVM_GET_REGS failed");
266*af7b0868SMatt Evans 
267*af7b0868SMatt Evans 	cr0 = sregs.cr0; cr2 = sregs.cr2; cr3 = sregs.cr3;
268*af7b0868SMatt Evans 	cr4 = sregs.cr4; cr8 = sregs.cr8;
269*af7b0868SMatt Evans 
270*af7b0868SMatt Evans 	dprintf(debug_fd, " cr0: %016lx   cr2: %016lx   cr3: %016lx\n", cr0, cr2, cr3);
271*af7b0868SMatt Evans 	dprintf(debug_fd, " cr4: %016lx   cr8: %016lx\n", cr4, cr8);
272*af7b0868SMatt Evans 	dprintf(debug_fd, "\n Segment registers:\n");
273*af7b0868SMatt Evans 	dprintf(debug_fd,   " ------------------\n");
274*af7b0868SMatt Evans 	dprintf(debug_fd, " register  selector  base              limit     type  p dpl db s l g avl\n");
275*af7b0868SMatt Evans 	print_segment("cs ", &sregs.cs);
276*af7b0868SMatt Evans 	print_segment("ss ", &sregs.ss);
277*af7b0868SMatt Evans 	print_segment("ds ", &sregs.ds);
278*af7b0868SMatt Evans 	print_segment("es ", &sregs.es);
279*af7b0868SMatt Evans 	print_segment("fs ", &sregs.fs);
280*af7b0868SMatt Evans 	print_segment("gs ", &sregs.gs);
281*af7b0868SMatt Evans 	print_segment("tr ", &sregs.tr);
282*af7b0868SMatt Evans 	print_segment("ldt", &sregs.ldt);
283*af7b0868SMatt Evans 	print_dtable("gdt", &sregs.gdt);
284*af7b0868SMatt Evans 	print_dtable("idt", &sregs.idt);
285*af7b0868SMatt Evans 
286*af7b0868SMatt Evans 	dprintf(debug_fd, "\n APIC:\n");
287*af7b0868SMatt Evans 	dprintf(debug_fd,   " -----\n");
288*af7b0868SMatt Evans 	dprintf(debug_fd, " efer: %016llx  apic base: %016llx  nmi: %s\n",
289*af7b0868SMatt Evans 		(u64) sregs.efer, (u64) sregs.apic_base,
290*af7b0868SMatt Evans 		(vcpu->kvm->nmi_disabled ? "disabled" : "enabled"));
291*af7b0868SMatt Evans 
292*af7b0868SMatt Evans 	dprintf(debug_fd, "\n Interrupt bitmap:\n");
293*af7b0868SMatt Evans 	dprintf(debug_fd,   " -----------------\n");
294*af7b0868SMatt Evans 	for (i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++)
295*af7b0868SMatt Evans 		dprintf(debug_fd, " %016llx", (u64) sregs.interrupt_bitmap[i]);
296*af7b0868SMatt Evans 	dprintf(debug_fd, "\n");
297*af7b0868SMatt Evans }
298*af7b0868SMatt Evans 
299*af7b0868SMatt Evans #define MAX_SYM_LEN		128
300*af7b0868SMatt Evans 
301*af7b0868SMatt Evans void kvm_cpu__show_code(struct kvm_cpu *vcpu)
302*af7b0868SMatt Evans {
303*af7b0868SMatt Evans 	unsigned int code_bytes = 64;
304*af7b0868SMatt Evans 	unsigned int code_prologue = code_bytes * 43 / 64;
305*af7b0868SMatt Evans 	unsigned int code_len = code_bytes;
306*af7b0868SMatt Evans 	char sym[MAX_SYM_LEN];
307*af7b0868SMatt Evans 	unsigned char c;
308*af7b0868SMatt Evans 	unsigned int i;
309*af7b0868SMatt Evans 	u8 *ip;
310*af7b0868SMatt Evans 
311*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_REGS, &vcpu->regs) < 0)
312*af7b0868SMatt Evans 		die("KVM_GET_REGS failed");
313*af7b0868SMatt Evans 
314*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &vcpu->sregs) < 0)
315*af7b0868SMatt Evans 		die("KVM_GET_SREGS failed");
316*af7b0868SMatt Evans 
317*af7b0868SMatt Evans 	ip = guest_flat_to_host(vcpu->kvm, ip_to_flat(vcpu, vcpu->regs.rip) - code_prologue);
318*af7b0868SMatt Evans 
319*af7b0868SMatt Evans 	dprintf(debug_fd, "\n Code:\n");
320*af7b0868SMatt Evans 	dprintf(debug_fd,   " -----\n");
321*af7b0868SMatt Evans 
322*af7b0868SMatt Evans 	symbol__lookup(vcpu->kvm, vcpu->regs.rip, sym, MAX_SYM_LEN);
323*af7b0868SMatt Evans 
324*af7b0868SMatt Evans 	dprintf(debug_fd, " rip: [<%016lx>] %s\n\n", (unsigned long) vcpu->regs.rip, sym);
325*af7b0868SMatt Evans 
326*af7b0868SMatt Evans 	for (i = 0; i < code_len; i++, ip++) {
327*af7b0868SMatt Evans 		if (!host_ptr_in_ram(vcpu->kvm, ip))
328*af7b0868SMatt Evans 			break;
329*af7b0868SMatt Evans 
330*af7b0868SMatt Evans 		c = *ip;
331*af7b0868SMatt Evans 
332*af7b0868SMatt Evans 		if (ip == guest_flat_to_host(vcpu->kvm, ip_to_flat(vcpu, vcpu->regs.rip)))
333*af7b0868SMatt Evans 			dprintf(debug_fd, " <%02x>", c);
334*af7b0868SMatt Evans 		else
335*af7b0868SMatt Evans 			dprintf(debug_fd, " %02x", c);
336*af7b0868SMatt Evans 	}
337*af7b0868SMatt Evans 
338*af7b0868SMatt Evans 	dprintf(debug_fd, "\n");
339*af7b0868SMatt Evans 
340*af7b0868SMatt Evans 	dprintf(debug_fd, "\n Stack:\n");
341*af7b0868SMatt Evans 	dprintf(debug_fd,   " ------\n");
342*af7b0868SMatt Evans 	kvm__dump_mem(vcpu->kvm, vcpu->regs.rsp, 32);
343*af7b0868SMatt Evans }
344*af7b0868SMatt Evans 
345*af7b0868SMatt Evans void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
346*af7b0868SMatt Evans {
347*af7b0868SMatt Evans 	u64 *pte1;
348*af7b0868SMatt Evans 	u64 *pte2;
349*af7b0868SMatt Evans 	u64 *pte3;
350*af7b0868SMatt Evans 	u64 *pte4;
351*af7b0868SMatt Evans 
352*af7b0868SMatt Evans 	if (!is_in_protected_mode(vcpu))
353*af7b0868SMatt Evans 		return;
354*af7b0868SMatt Evans 
355*af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &vcpu->sregs) < 0)
356*af7b0868SMatt Evans 		die("KVM_GET_SREGS failed");
357*af7b0868SMatt Evans 
358*af7b0868SMatt Evans 	pte4	= guest_flat_to_host(vcpu->kvm, vcpu->sregs.cr3);
359*af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte4))
360*af7b0868SMatt Evans 		return;
361*af7b0868SMatt Evans 
362*af7b0868SMatt Evans 	pte3	= guest_flat_to_host(vcpu->kvm, (*pte4 & ~0xfff));
363*af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte3))
364*af7b0868SMatt Evans 		return;
365*af7b0868SMatt Evans 
366*af7b0868SMatt Evans 	pte2	= guest_flat_to_host(vcpu->kvm, (*pte3 & ~0xfff));
367*af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte2))
368*af7b0868SMatt Evans 		return;
369*af7b0868SMatt Evans 
370*af7b0868SMatt Evans 	pte1	= guest_flat_to_host(vcpu->kvm, (*pte2 & ~0xfff));
371*af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte1))
372*af7b0868SMatt Evans 		return;
373*af7b0868SMatt Evans 
374*af7b0868SMatt Evans 	dprintf(debug_fd, "Page Tables:\n");
375*af7b0868SMatt Evans 	if (*pte2 & (1 << 7))
376*af7b0868SMatt Evans 		dprintf(debug_fd, " pte4: %016llx   pte3: %016llx"
377*af7b0868SMatt Evans 			"   pte2: %016llx\n",
378*af7b0868SMatt Evans 			*pte4, *pte3, *pte2);
379*af7b0868SMatt Evans 	else
380*af7b0868SMatt Evans 		dprintf(debug_fd, " pte4: %016llx  pte3: %016llx   pte2: %016"
381*af7b0868SMatt Evans 			"llx   pte1: %016llx\n",
382*af7b0868SMatt Evans 			*pte4, *pte3, *pte2, *pte1);
383*af7b0868SMatt Evans }
384