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