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