xref: /kvmtool/x86/kvm-cpu.c (revision 1cc05b24bfe0211bb408f3264af8e0c42dcdde9c)
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 
74b1c6f6eSSasha Levin #include <asm/apicdef.h>
84932d174SSasha Levin #include <linux/err.h>
9af7b0868SMatt Evans #include <sys/ioctl.h>
10af7b0868SMatt Evans #include <sys/mman.h>
11af7b0868SMatt Evans #include <signal.h>
12af7b0868SMatt Evans #include <stdlib.h>
13af7b0868SMatt Evans #include <string.h>
14af7b0868SMatt Evans #include <errno.h>
15af7b0868SMatt Evans #include <stdio.h>
16af7b0868SMatt Evans 
17af7b0868SMatt Evans static int debug_fd;
18af7b0868SMatt Evans 
kvm_cpu__set_debug_fd(int fd)19af7b0868SMatt Evans void kvm_cpu__set_debug_fd(int fd)
20af7b0868SMatt Evans {
21af7b0868SMatt Evans 	debug_fd = fd;
22af7b0868SMatt Evans }
23af7b0868SMatt Evans 
kvm_cpu__get_debug_fd(void)24af7b0868SMatt Evans int kvm_cpu__get_debug_fd(void)
25af7b0868SMatt Evans {
26af7b0868SMatt Evans 	return debug_fd;
27af7b0868SMatt Evans }
28af7b0868SMatt Evans 
is_in_protected_mode(struct kvm_cpu * vcpu)29af7b0868SMatt Evans static inline bool is_in_protected_mode(struct kvm_cpu *vcpu)
30af7b0868SMatt Evans {
31af7b0868SMatt Evans 	return vcpu->sregs.cr0 & 0x01;
32af7b0868SMatt Evans }
33af7b0868SMatt Evans 
ip_to_flat(struct kvm_cpu * vcpu,u64 ip)34af7b0868SMatt Evans static inline u64 ip_to_flat(struct kvm_cpu *vcpu, u64 ip)
35af7b0868SMatt Evans {
36af7b0868SMatt Evans 	u64 cs;
37af7b0868SMatt Evans 
38af7b0868SMatt Evans 	/*
39af7b0868SMatt Evans 	 * NOTE! We should take code segment base address into account here.
40af7b0868SMatt Evans 	 * Luckily it's usually zero because Linux uses flat memory model.
41af7b0868SMatt Evans 	 */
42af7b0868SMatt Evans 	if (is_in_protected_mode(vcpu))
43af7b0868SMatt Evans 		return ip;
44af7b0868SMatt Evans 
45af7b0868SMatt Evans 	cs = vcpu->sregs.cs.selector;
46af7b0868SMatt Evans 
47af7b0868SMatt Evans 	return ip + (cs << 4);
48af7b0868SMatt Evans }
49af7b0868SMatt Evans 
selector_to_base(u16 selector)50af7b0868SMatt Evans static inline u32 selector_to_base(u16 selector)
51af7b0868SMatt Evans {
52af7b0868SMatt Evans 	/*
53af7b0868SMatt Evans 	 * KVM on Intel requires 'base' to be 'selector * 16' in real mode.
54af7b0868SMatt Evans 	 */
553a60be06SSasha Levin 	return (u32)selector << 4;
56af7b0868SMatt Evans }
57af7b0868SMatt Evans 
kvm_cpu__new(struct kvm * kvm)58af7b0868SMatt Evans static struct kvm_cpu *kvm_cpu__new(struct kvm *kvm)
59af7b0868SMatt Evans {
60af7b0868SMatt Evans 	struct kvm_cpu *vcpu;
61af7b0868SMatt Evans 
623a60be06SSasha Levin 	vcpu = calloc(1, sizeof(*vcpu));
63af7b0868SMatt Evans 	if (!vcpu)
64af7b0868SMatt Evans 		return NULL;
65af7b0868SMatt Evans 
66af7b0868SMatt Evans 	vcpu->kvm = kvm;
67af7b0868SMatt Evans 
68af7b0868SMatt Evans 	return vcpu;
69af7b0868SMatt Evans }
70af7b0868SMatt Evans 
kvm_cpu__delete(struct kvm_cpu * vcpu)71af7b0868SMatt Evans void kvm_cpu__delete(struct kvm_cpu *vcpu)
72af7b0868SMatt Evans {
73af7b0868SMatt Evans 	if (vcpu->msrs)
74af7b0868SMatt Evans 		free(vcpu->msrs);
75af7b0868SMatt Evans 
76af7b0868SMatt Evans 	free(vcpu);
77af7b0868SMatt Evans }
78af7b0868SMatt Evans 
kvm_cpu__set_lint(struct kvm_cpu * vcpu)794b1c6f6eSSasha Levin static int kvm_cpu__set_lint(struct kvm_cpu *vcpu)
804b1c6f6eSSasha Levin {
81e49921b8SMatt Evans 	struct local_apic lapic;
824b1c6f6eSSasha Levin 
83e49921b8SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_LAPIC, &lapic))
844b1c6f6eSSasha Levin 		return -1;
854b1c6f6eSSasha Levin 
86e49921b8SMatt Evans 	lapic.lvt_lint0.delivery_mode = APIC_MODE_EXTINT;
87e49921b8SMatt Evans 	lapic.lvt_lint1.delivery_mode = APIC_MODE_NMI;
884b1c6f6eSSasha Levin 
89e49921b8SMatt Evans 	return ioctl(vcpu->vcpu_fd, KVM_SET_LAPIC, &lapic);
904b1c6f6eSSasha Levin }
914b1c6f6eSSasha Levin 
kvm_cpu__arch_init(struct kvm * kvm,unsigned long cpu_id)92df4239fbSSasha Levin struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
93af7b0868SMatt Evans {
94af7b0868SMatt Evans 	struct kvm_cpu *vcpu;
95af7b0868SMatt Evans 	int mmap_size;
96af7b0868SMatt Evans 	int coalesced_offset;
97af7b0868SMatt Evans 
98af7b0868SMatt Evans 	vcpu = kvm_cpu__new(kvm);
99af7b0868SMatt Evans 	if (!vcpu)
100af7b0868SMatt Evans 		return NULL;
101af7b0868SMatt Evans 
102af7b0868SMatt Evans 	vcpu->cpu_id = cpu_id;
103af7b0868SMatt Evans 
104af7b0868SMatt Evans 	vcpu->vcpu_fd = ioctl(vcpu->kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
105af7b0868SMatt Evans 	if (vcpu->vcpu_fd < 0)
106af7b0868SMatt Evans 		die_perror("KVM_CREATE_VCPU ioctl");
107af7b0868SMatt Evans 
108af7b0868SMatt Evans 	mmap_size = ioctl(vcpu->kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
109af7b0868SMatt Evans 	if (mmap_size < 0)
110af7b0868SMatt Evans 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
111af7b0868SMatt Evans 
112af7b0868SMatt Evans 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, vcpu->vcpu_fd, 0);
113af7b0868SMatt Evans 	if (vcpu->kvm_run == MAP_FAILED)
114af7b0868SMatt Evans 		die("unable to mmap vcpu fd");
115af7b0868SMatt Evans 
116af7b0868SMatt Evans 	coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_COALESCED_MMIO);
117af7b0868SMatt Evans 	if (coalesced_offset)
118af7b0868SMatt Evans 		vcpu->ring = (void *)vcpu->kvm_run + (coalesced_offset * PAGE_SIZE);
119af7b0868SMatt Evans 
1204b1c6f6eSSasha Levin 	if (kvm_cpu__set_lint(vcpu))
1214b1c6f6eSSasha Levin 		die_perror("KVM_SET_LAPIC failed");
1224b1c6f6eSSasha Levin 
123af7b0868SMatt Evans 	vcpu->is_running = true;
124af7b0868SMatt Evans 
125af7b0868SMatt Evans 	return vcpu;
126af7b0868SMatt Evans }
127af7b0868SMatt Evans 
kvm_msrs__new(size_t nmsrs)128af7b0868SMatt Evans static struct kvm_msrs *kvm_msrs__new(size_t nmsrs)
129af7b0868SMatt Evans {
130af7b0868SMatt Evans 	struct kvm_msrs *vcpu = calloc(1, sizeof(*vcpu) + (sizeof(struct kvm_msr_entry) * nmsrs));
131af7b0868SMatt Evans 
132af7b0868SMatt Evans 	if (!vcpu)
133af7b0868SMatt Evans 		die("out of memory");
134af7b0868SMatt Evans 
135af7b0868SMatt Evans 	return vcpu;
136af7b0868SMatt Evans }
137af7b0868SMatt Evans 
138*1cc05b24SThomas Petazzoni #define MSR_IA32_SYSENTER_CS            0x00000174
139*1cc05b24SThomas Petazzoni #define MSR_IA32_SYSENTER_ESP           0x00000175
140*1cc05b24SThomas Petazzoni #define MSR_IA32_SYSENTER_EIP           0x00000176
141*1cc05b24SThomas Petazzoni 
142*1cc05b24SThomas Petazzoni #define MSR_STAR                0xc0000081 /* legacy mode SYSCALL target */
143*1cc05b24SThomas Petazzoni #define MSR_LSTAR               0xc0000082 /* long mode SYSCALL target */
144*1cc05b24SThomas Petazzoni #define MSR_CSTAR               0xc0000083 /* compat mode SYSCALL target */
145*1cc05b24SThomas Petazzoni #define MSR_SYSCALL_MASK        0xc0000084 /* EFLAGS mask for syscall */
146*1cc05b24SThomas Petazzoni #define MSR_KERNEL_GS_BASE      0xc0000102 /* SwapGS GS shadow */
147*1cc05b24SThomas Petazzoni 
148*1cc05b24SThomas Petazzoni #define MSR_IA32_TSC                    0x00000010
149*1cc05b24SThomas Petazzoni #define MSR_IA32_MISC_ENABLE            0x000001a0
150*1cc05b24SThomas Petazzoni 
151*1cc05b24SThomas Petazzoni #define MSR_IA32_MISC_ENABLE_FAST_STRING_BIT            0
152*1cc05b24SThomas Petazzoni #define MSR_IA32_MISC_ENABLE_FAST_STRING                (1ULL << MSR_IA32_MISC_ENABLE_FAST_STRING_BIT)
153*1cc05b24SThomas Petazzoni 
154af7b0868SMatt Evans #define KVM_MSR_ENTRY(_index, _data)	\
155af7b0868SMatt Evans 	(struct kvm_msr_entry) { .index = _index, .data = _data }
156af7b0868SMatt Evans 
kvm_cpu__setup_msrs(struct kvm_cpu * vcpu)157af7b0868SMatt Evans static void kvm_cpu__setup_msrs(struct kvm_cpu *vcpu)
158af7b0868SMatt Evans {
159af7b0868SMatt Evans 	unsigned long ndx = 0;
160af7b0868SMatt Evans 
161af7b0868SMatt Evans 	vcpu->msrs = kvm_msrs__new(100);
162af7b0868SMatt Evans 
163af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_CS,	0x0);
164af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_ESP,	0x0);
165af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_SYSENTER_EIP,	0x0);
166af7b0868SMatt Evans #ifdef CONFIG_X86_64
167af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_STAR,			0x0);
168af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_CSTAR,			0x0);
169af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_KERNEL_GS_BASE,		0x0);
170af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_SYSCALL_MASK,		0x0);
171af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_LSTAR,			0x0);
172af7b0868SMatt Evans #endif
173af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_TSC,		0x0);
174af7b0868SMatt Evans 	vcpu->msrs->entries[ndx++] = KVM_MSR_ENTRY(MSR_IA32_MISC_ENABLE,
175af7b0868SMatt Evans 						MSR_IA32_MISC_ENABLE_FAST_STRING);
176af7b0868SMatt Evans 
177af7b0868SMatt Evans 	vcpu->msrs->nmsrs = ndx;
178af7b0868SMatt Evans 
179af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_MSRS, vcpu->msrs) < 0)
180af7b0868SMatt Evans 		die_perror("KVM_SET_MSRS failed");
181af7b0868SMatt Evans }
182af7b0868SMatt Evans 
kvm_cpu__setup_fpu(struct kvm_cpu * vcpu)183af7b0868SMatt Evans static void kvm_cpu__setup_fpu(struct kvm_cpu *vcpu)
184af7b0868SMatt Evans {
185af7b0868SMatt Evans 	vcpu->fpu = (struct kvm_fpu) {
186af7b0868SMatt Evans 		.fcw	= 0x37f,
187af7b0868SMatt Evans 		.mxcsr	= 0x1f80,
188af7b0868SMatt Evans 	};
189af7b0868SMatt Evans 
190af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_FPU, &vcpu->fpu) < 0)
191af7b0868SMatt Evans 		die_perror("KVM_SET_FPU failed");
192af7b0868SMatt Evans }
193af7b0868SMatt Evans 
kvm_cpu__setup_regs(struct kvm_cpu * vcpu)194af7b0868SMatt Evans static void kvm_cpu__setup_regs(struct kvm_cpu *vcpu)
195af7b0868SMatt Evans {
196af7b0868SMatt Evans 	vcpu->regs = (struct kvm_regs) {
197af7b0868SMatt Evans 		/* We start the guest in 16-bit real mode  */
198af7b0868SMatt Evans 		.rflags	= 0x0000000000000002ULL,
199af7b0868SMatt Evans 
20042ac24f9SSasha Levin 		.rip	= vcpu->kvm->arch.boot_ip,
20142ac24f9SSasha Levin 		.rsp	= vcpu->kvm->arch.boot_sp,
20242ac24f9SSasha Levin 		.rbp	= vcpu->kvm->arch.boot_sp,
203af7b0868SMatt Evans 	};
204af7b0868SMatt Evans 
205af7b0868SMatt Evans 	if (vcpu->regs.rip > USHRT_MAX)
206af7b0868SMatt Evans 		die("ip 0x%llx is too high for real mode", (u64)vcpu->regs.rip);
207af7b0868SMatt Evans 
208af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_REGS, &vcpu->regs) < 0)
209af7b0868SMatt Evans 		die_perror("KVM_SET_REGS failed");
210af7b0868SMatt Evans }
211af7b0868SMatt Evans 
kvm_cpu__setup_sregs(struct kvm_cpu * vcpu)212af7b0868SMatt Evans static void kvm_cpu__setup_sregs(struct kvm_cpu *vcpu)
213af7b0868SMatt Evans {
214af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &vcpu->sregs) < 0)
215af7b0868SMatt Evans 		die_perror("KVM_GET_SREGS failed");
216af7b0868SMatt Evans 
21742ac24f9SSasha Levin 	vcpu->sregs.cs.selector	= vcpu->kvm->arch.boot_selector;
21842ac24f9SSasha Levin 	vcpu->sregs.cs.base	= selector_to_base(vcpu->kvm->arch.boot_selector);
21942ac24f9SSasha Levin 	vcpu->sregs.ss.selector	= vcpu->kvm->arch.boot_selector;
22042ac24f9SSasha Levin 	vcpu->sregs.ss.base	= selector_to_base(vcpu->kvm->arch.boot_selector);
22142ac24f9SSasha Levin 	vcpu->sregs.ds.selector	= vcpu->kvm->arch.boot_selector;
22242ac24f9SSasha Levin 	vcpu->sregs.ds.base	= selector_to_base(vcpu->kvm->arch.boot_selector);
22342ac24f9SSasha Levin 	vcpu->sregs.es.selector	= vcpu->kvm->arch.boot_selector;
22442ac24f9SSasha Levin 	vcpu->sregs.es.base	= selector_to_base(vcpu->kvm->arch.boot_selector);
22542ac24f9SSasha Levin 	vcpu->sregs.fs.selector	= vcpu->kvm->arch.boot_selector;
22642ac24f9SSasha Levin 	vcpu->sregs.fs.base	= selector_to_base(vcpu->kvm->arch.boot_selector);
22742ac24f9SSasha Levin 	vcpu->sregs.gs.selector	= vcpu->kvm->arch.boot_selector;
22842ac24f9SSasha Levin 	vcpu->sregs.gs.base	= selector_to_base(vcpu->kvm->arch.boot_selector);
229af7b0868SMatt Evans 
230af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_SET_SREGS, &vcpu->sregs) < 0)
231af7b0868SMatt Evans 		die_perror("KVM_SET_SREGS failed");
232af7b0868SMatt Evans }
233af7b0868SMatt Evans 
234af7b0868SMatt Evans /**
235af7b0868SMatt Evans  * kvm_cpu__reset_vcpu - reset virtual CPU to a known state
236af7b0868SMatt Evans  */
kvm_cpu__reset_vcpu(struct kvm_cpu * vcpu)237af7b0868SMatt Evans void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
238af7b0868SMatt Evans {
239af7b0868SMatt Evans 	kvm_cpu__setup_cpuid(vcpu);
240af7b0868SMatt Evans 	kvm_cpu__setup_sregs(vcpu);
241af7b0868SMatt Evans 	kvm_cpu__setup_regs(vcpu);
242af7b0868SMatt Evans 	kvm_cpu__setup_fpu(vcpu);
243af7b0868SMatt Evans 	kvm_cpu__setup_msrs(vcpu);
244af7b0868SMatt Evans }
245af7b0868SMatt Evans 
kvm_cpu__handle_exit(struct kvm_cpu * vcpu)246341ee0d4SMatt Evans bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
247341ee0d4SMatt Evans {
248341ee0d4SMatt Evans 	return false;
249341ee0d4SMatt Evans }
250341ee0d4SMatt Evans 
print_dtable(const char * name,struct kvm_dtable * dtable)251af7b0868SMatt Evans static void print_dtable(const char *name, struct kvm_dtable *dtable)
252af7b0868SMatt Evans {
253af7b0868SMatt Evans 	dprintf(debug_fd, " %s                 %016llx  %08hx\n",
254af7b0868SMatt Evans 		name, (u64) dtable->base, (u16) dtable->limit);
255af7b0868SMatt Evans }
256af7b0868SMatt Evans 
print_segment(const char * name,struct kvm_segment * seg)257af7b0868SMatt Evans static void print_segment(const char *name, struct kvm_segment *seg)
258af7b0868SMatt Evans {
259af7b0868SMatt Evans 	dprintf(debug_fd, " %s       %04hx      %016llx  %08x  %02hhx    %x %x   %x  %x %x %x %x\n",
260af7b0868SMatt Evans 		name, (u16) seg->selector, (u64) seg->base, (u32) seg->limit,
261af7b0868SMatt Evans 		(u8) seg->type, seg->present, seg->dpl, seg->db, seg->s, seg->l, seg->g, seg->avl);
262af7b0868SMatt Evans }
263af7b0868SMatt Evans 
kvm_cpu__show_registers(struct kvm_cpu * vcpu)264af7b0868SMatt Evans void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
265af7b0868SMatt Evans {
266af7b0868SMatt Evans 	unsigned long cr0, cr2, cr3;
267af7b0868SMatt Evans 	unsigned long cr4, cr8;
268af7b0868SMatt Evans 	unsigned long rax, rbx, rcx;
269af7b0868SMatt Evans 	unsigned long rdx, rsi, rdi;
270af7b0868SMatt Evans 	unsigned long rbp,  r8,  r9;
271af7b0868SMatt Evans 	unsigned long r10, r11, r12;
272af7b0868SMatt Evans 	unsigned long r13, r14, r15;
273af7b0868SMatt Evans 	unsigned long rip, rsp;
274af7b0868SMatt Evans 	struct kvm_sregs sregs;
275af7b0868SMatt Evans 	unsigned long rflags;
276af7b0868SMatt Evans 	struct kvm_regs regs;
277af7b0868SMatt Evans 	int i;
278af7b0868SMatt Evans 
279af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_REGS, &regs) < 0)
280af7b0868SMatt Evans 		die("KVM_GET_REGS failed");
281af7b0868SMatt Evans 
282af7b0868SMatt Evans 	rflags = regs.rflags;
283af7b0868SMatt Evans 
284af7b0868SMatt Evans 	rip = regs.rip; rsp = regs.rsp;
285af7b0868SMatt Evans 	rax = regs.rax; rbx = regs.rbx; rcx = regs.rcx;
286af7b0868SMatt Evans 	rdx = regs.rdx; rsi = regs.rsi; rdi = regs.rdi;
287af7b0868SMatt Evans 	rbp = regs.rbp; r8  = regs.r8;  r9  = regs.r9;
288af7b0868SMatt Evans 	r10 = regs.r10; r11 = regs.r11; r12 = regs.r12;
289af7b0868SMatt Evans 	r13 = regs.r13; r14 = regs.r14; r15 = regs.r15;
290af7b0868SMatt Evans 
291af7b0868SMatt Evans 	dprintf(debug_fd, "\n Registers:\n");
292af7b0868SMatt Evans 	dprintf(debug_fd,   " ----------\n");
293af7b0868SMatt Evans 	dprintf(debug_fd, " rip: %016lx   rsp: %016lx flags: %016lx\n", rip, rsp, rflags);
294af7b0868SMatt Evans 	dprintf(debug_fd, " rax: %016lx   rbx: %016lx   rcx: %016lx\n", rax, rbx, rcx);
295af7b0868SMatt Evans 	dprintf(debug_fd, " rdx: %016lx   rsi: %016lx   rdi: %016lx\n", rdx, rsi, rdi);
296af7b0868SMatt Evans 	dprintf(debug_fd, " rbp: %016lx    r8: %016lx    r9: %016lx\n", rbp, r8,  r9);
297af7b0868SMatt Evans 	dprintf(debug_fd, " r10: %016lx   r11: %016lx   r12: %016lx\n", r10, r11, r12);
298af7b0868SMatt Evans 	dprintf(debug_fd, " r13: %016lx   r14: %016lx   r15: %016lx\n", r13, r14, r15);
299af7b0868SMatt Evans 
300af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &sregs) < 0)
301af7b0868SMatt Evans 		die("KVM_GET_REGS failed");
302af7b0868SMatt Evans 
303af7b0868SMatt Evans 	cr0 = sregs.cr0; cr2 = sregs.cr2; cr3 = sregs.cr3;
304af7b0868SMatt Evans 	cr4 = sregs.cr4; cr8 = sregs.cr8;
305af7b0868SMatt Evans 
306af7b0868SMatt Evans 	dprintf(debug_fd, " cr0: %016lx   cr2: %016lx   cr3: %016lx\n", cr0, cr2, cr3);
307af7b0868SMatt Evans 	dprintf(debug_fd, " cr4: %016lx   cr8: %016lx\n", cr4, cr8);
308af7b0868SMatt Evans 	dprintf(debug_fd, "\n Segment registers:\n");
309af7b0868SMatt Evans 	dprintf(debug_fd,   " ------------------\n");
310af7b0868SMatt Evans 	dprintf(debug_fd, " register  selector  base              limit     type  p dpl db s l g avl\n");
311af7b0868SMatt Evans 	print_segment("cs ", &sregs.cs);
312af7b0868SMatt Evans 	print_segment("ss ", &sregs.ss);
313af7b0868SMatt Evans 	print_segment("ds ", &sregs.ds);
314af7b0868SMatt Evans 	print_segment("es ", &sregs.es);
315af7b0868SMatt Evans 	print_segment("fs ", &sregs.fs);
316af7b0868SMatt Evans 	print_segment("gs ", &sregs.gs);
317af7b0868SMatt Evans 	print_segment("tr ", &sregs.tr);
318af7b0868SMatt Evans 	print_segment("ldt", &sregs.ldt);
319af7b0868SMatt Evans 	print_dtable("gdt", &sregs.gdt);
320af7b0868SMatt Evans 	print_dtable("idt", &sregs.idt);
321af7b0868SMatt Evans 
322af7b0868SMatt Evans 	dprintf(debug_fd, "\n APIC:\n");
323af7b0868SMatt Evans 	dprintf(debug_fd,   " -----\n");
324af7b0868SMatt Evans 	dprintf(debug_fd, " efer: %016llx  apic base: %016llx  nmi: %s\n",
325af7b0868SMatt Evans 		(u64) sregs.efer, (u64) sregs.apic_base,
326af7b0868SMatt Evans 		(vcpu->kvm->nmi_disabled ? "disabled" : "enabled"));
327af7b0868SMatt Evans 
328af7b0868SMatt Evans 	dprintf(debug_fd, "\n Interrupt bitmap:\n");
329af7b0868SMatt Evans 	dprintf(debug_fd,   " -----------------\n");
330af7b0868SMatt Evans 	for (i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++)
331af7b0868SMatt Evans 		dprintf(debug_fd, " %016llx", (u64) sregs.interrupt_bitmap[i]);
332af7b0868SMatt Evans 	dprintf(debug_fd, "\n");
333af7b0868SMatt Evans }
334af7b0868SMatt Evans 
335af7b0868SMatt Evans #define MAX_SYM_LEN 128
336af7b0868SMatt Evans 
kvm_cpu__show_code(struct kvm_cpu * vcpu)337af7b0868SMatt Evans void kvm_cpu__show_code(struct kvm_cpu *vcpu)
338af7b0868SMatt Evans {
339af7b0868SMatt Evans 	unsigned int code_bytes = 64;
3403a60be06SSasha Levin 	unsigned int code_prologue = 43;
341af7b0868SMatt Evans 	unsigned int code_len = code_bytes;
3424932d174SSasha Levin 	char sym[MAX_SYM_LEN] = SYMBOL_DEFAULT_UNKNOWN, *psym;
343af7b0868SMatt Evans 	unsigned char c;
344af7b0868SMatt Evans 	unsigned int i;
345af7b0868SMatt Evans 	u8 *ip;
346af7b0868SMatt Evans 
347af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_REGS, &vcpu->regs) < 0)
348af7b0868SMatt Evans 		die("KVM_GET_REGS failed");
349af7b0868SMatt Evans 
350af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &vcpu->sregs) < 0)
351af7b0868SMatt Evans 		die("KVM_GET_SREGS failed");
352af7b0868SMatt Evans 
353af7b0868SMatt Evans 	ip = guest_flat_to_host(vcpu->kvm, ip_to_flat(vcpu, vcpu->regs.rip) - code_prologue);
354af7b0868SMatt Evans 
355af7b0868SMatt Evans 	dprintf(debug_fd, "\n Code:\n");
356af7b0868SMatt Evans 	dprintf(debug_fd,   " -----\n");
357af7b0868SMatt Evans 
358807b77b9SCyrill Gorcunov 	psym = symbol_lookup(vcpu->kvm, vcpu->regs.rip, sym, MAX_SYM_LEN);
3594932d174SSasha Levin 	if (IS_ERR(psym))
3604932d174SSasha Levin 		dprintf(debug_fd,
361807b77b9SCyrill Gorcunov 			"Warning: symbol_lookup() failed to find symbol "
3624932d174SSasha Levin 			"with error: %ld\n", PTR_ERR(psym));
363af7b0868SMatt Evans 
364af7b0868SMatt Evans 	dprintf(debug_fd, " rip: [<%016lx>] %s\n\n", (unsigned long) vcpu->regs.rip, sym);
365af7b0868SMatt Evans 
366af7b0868SMatt Evans 	for (i = 0; i < code_len; i++, ip++) {
367af7b0868SMatt Evans 		if (!host_ptr_in_ram(vcpu->kvm, ip))
368af7b0868SMatt Evans 			break;
369af7b0868SMatt Evans 
370af7b0868SMatt Evans 		c = *ip;
371af7b0868SMatt Evans 
372af7b0868SMatt Evans 		if (ip == guest_flat_to_host(vcpu->kvm, ip_to_flat(vcpu, vcpu->regs.rip)))
373af7b0868SMatt Evans 			dprintf(debug_fd, " <%02x>", c);
374af7b0868SMatt Evans 		else
375af7b0868SMatt Evans 			dprintf(debug_fd, " %02x", c);
376af7b0868SMatt Evans 	}
377af7b0868SMatt Evans 
378af7b0868SMatt Evans 	dprintf(debug_fd, "\n");
379af7b0868SMatt Evans 
380af7b0868SMatt Evans 	dprintf(debug_fd, "\n Stack:\n");
381af7b0868SMatt Evans 	dprintf(debug_fd,   " ------\n");
382b2cf1e9fSAsias He 	dprintf(debug_fd, " rsp: [<%016lx>] \n", (unsigned long) vcpu->regs.rsp);
383b2cf1e9fSAsias He 	kvm__dump_mem(vcpu->kvm, vcpu->regs.rsp, 32, debug_fd);
384af7b0868SMatt Evans }
385af7b0868SMatt Evans 
kvm_cpu__show_page_tables(struct kvm_cpu * vcpu)386af7b0868SMatt Evans void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
387af7b0868SMatt Evans {
388af7b0868SMatt Evans 	u64 *pte1;
389af7b0868SMatt Evans 	u64 *pte2;
390af7b0868SMatt Evans 	u64 *pte3;
391af7b0868SMatt Evans 	u64 *pte4;
392af7b0868SMatt Evans 
393b2cf1e9fSAsias He 	if (!is_in_protected_mode(vcpu)) {
394b2cf1e9fSAsias He 		dprintf(debug_fd, "\n Page Tables:\n");
395b2cf1e9fSAsias He 		dprintf(debug_fd, " ------\n");
396b2cf1e9fSAsias He 		dprintf(debug_fd, " Not in protected mode\n");
397af7b0868SMatt Evans 		return;
398b2cf1e9fSAsias He 	}
399af7b0868SMatt Evans 
400af7b0868SMatt Evans 	if (ioctl(vcpu->vcpu_fd, KVM_GET_SREGS, &vcpu->sregs) < 0)
401af7b0868SMatt Evans 		die("KVM_GET_SREGS failed");
402af7b0868SMatt Evans 
403af7b0868SMatt Evans 	pte4 = guest_flat_to_host(vcpu->kvm, vcpu->sregs.cr3);
404af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte4))
405af7b0868SMatt Evans 		return;
406af7b0868SMatt Evans 
407af7b0868SMatt Evans 	pte3 = guest_flat_to_host(vcpu->kvm, (*pte4 & ~0xfff));
408af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte3))
409af7b0868SMatt Evans 		return;
410af7b0868SMatt Evans 
411af7b0868SMatt Evans 	pte2 = guest_flat_to_host(vcpu->kvm, (*pte3 & ~0xfff));
412af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte2))
413af7b0868SMatt Evans 		return;
414af7b0868SMatt Evans 
415af7b0868SMatt Evans 	pte1 = guest_flat_to_host(vcpu->kvm, (*pte2 & ~0xfff));
416af7b0868SMatt Evans 	if (!host_ptr_in_ram(vcpu->kvm, pte1))
417af7b0868SMatt Evans 		return;
418af7b0868SMatt Evans 
419b2cf1e9fSAsias He 	dprintf(debug_fd, "\n Page Tables:\n");
420b2cf1e9fSAsias He 	dprintf(debug_fd, " ------\n");
421af7b0868SMatt Evans 	if (*pte2 & (1 << 7))
422af7b0868SMatt Evans 		dprintf(debug_fd, " pte4: %016llx   pte3: %016llx"
423af7b0868SMatt Evans 			"   pte2: %016llx\n",
424af7b0868SMatt Evans 			*pte4, *pte3, *pte2);
425af7b0868SMatt Evans 	else
426af7b0868SMatt Evans 		dprintf(debug_fd, " pte4: %016llx  pte3: %016llx   pte2: %016"
427af7b0868SMatt Evans 			"llx   pte1: %016llx\n",
428af7b0868SMatt Evans 			*pte4, *pte3, *pte2, *pte1);
429af7b0868SMatt Evans }
4304b1c6f6eSSasha Levin 
kvm_cpu__arch_nmi(struct kvm_cpu * cpu)4314b1c6f6eSSasha Levin void kvm_cpu__arch_nmi(struct kvm_cpu *cpu)
4324b1c6f6eSSasha Levin {
4334b1c6f6eSSasha Levin 	struct kvm_lapic_state klapic;
4344b1c6f6eSSasha Levin 	struct local_apic *lapic = (void *)&klapic;
4354b1c6f6eSSasha Levin 
4364b1c6f6eSSasha Levin 	if (ioctl(cpu->vcpu_fd, KVM_GET_LAPIC, &klapic) != 0)
4374b1c6f6eSSasha Levin 		return;
4384b1c6f6eSSasha Levin 
4394b1c6f6eSSasha Levin 	if (lapic->lvt_lint1.mask)
4404b1c6f6eSSasha Levin 		return;
4414b1c6f6eSSasha Levin 
4424b1c6f6eSSasha Levin 	if (lapic->lvt_lint1.delivery_mode != APIC_MODE_NMI)
4434b1c6f6eSSasha Levin 		return;
4444b1c6f6eSSasha Levin 
4454b1c6f6eSSasha Levin 	ioctl(cpu->vcpu_fd, KVM_NMI);
4464b1c6f6eSSasha Levin }
447