5 #include <asm/bootparam.h>
22 * Compatibility code. Remove this when we move to tools/kvm.
24 #ifndef KVM_EXIT_INTERNAL_ERROR
25 # define KVM_EXIT_INTERNAL_ERROR 17
28 #define DEFINE_KVM_EXIT_REASON(reason) [reason] = #reason
30 const char *kvm_exit_reasons[] = {
31 DEFINE_KVM_EXIT_REASON(KVM_EXIT_UNKNOWN),
32 DEFINE_KVM_EXIT_REASON(KVM_EXIT_EXCEPTION),
33 DEFINE_KVM_EXIT_REASON(KVM_EXIT_IO),
34 DEFINE_KVM_EXIT_REASON(KVM_EXIT_HYPERCALL),
35 DEFINE_KVM_EXIT_REASON(KVM_EXIT_DEBUG),
36 DEFINE_KVM_EXIT_REASON(KVM_EXIT_HLT),
37 DEFINE_KVM_EXIT_REASON(KVM_EXIT_MMIO),
38 DEFINE_KVM_EXIT_REASON(KVM_EXIT_IRQ_WINDOW_OPEN),
39 DEFINE_KVM_EXIT_REASON(KVM_EXIT_SHUTDOWN),
40 DEFINE_KVM_EXIT_REASON(KVM_EXIT_FAIL_ENTRY),
41 DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTR),
42 DEFINE_KVM_EXIT_REASON(KVM_EXIT_SET_TPR),
43 DEFINE_KVM_EXIT_REASON(KVM_EXIT_TPR_ACCESS),
44 DEFINE_KVM_EXIT_REASON(KVM_EXIT_S390_SIEIC),
45 DEFINE_KVM_EXIT_REASON(KVM_EXIT_S390_RESET),
46 DEFINE_KVM_EXIT_REASON(KVM_EXIT_DCR),
47 DEFINE_KVM_EXIT_REASON(KVM_EXIT_NMI),
48 DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTERNAL_ERROR),
51 static inline void *guest_addr_to_host(struct kvm *self, unsigned long offset)
53 return self->ram_start + offset;
56 static bool kvm__supports_extension(struct kvm *self, unsigned int extension)
60 ret = ioctl(self->sys_fd, KVM_CHECK_EXTENSION, extension);
67 static struct kvm *kvm__new(void)
69 struct kvm *self = calloc(1, sizeof *self);
77 struct kvm *kvm__init(void)
79 struct kvm_userspace_memory_region mem;
87 self->sys_fd = open("/dev/kvm", O_RDWR);
91 ret = ioctl(self->sys_fd, KVM_GET_API_VERSION, 0);
92 if (ret != KVM_API_VERSION)
93 die_perror("KVM_API_VERSION ioctl");
95 self->vm_fd = ioctl(self->sys_fd, KVM_CREATE_VM, 0);
97 die_perror("KVM_CREATE_VM ioctl");
99 if (!kvm__supports_extension(self, KVM_CAP_USER_MEMORY))
100 die("KVM_CAP_USER_MEMORY is not supported");
102 self->ram_size = 64UL * 1024UL * 1024UL;
104 page_size = sysconf(_SC_PAGESIZE);
105 if (posix_memalign(&self->ram_start, page_size, self->ram_size) != 0)
106 die("out of memory");
108 mem = (struct kvm_userspace_memory_region) {
110 .guest_phys_addr = 0x0UL,
111 .memory_size = self->ram_size,
112 .userspace_addr = (unsigned long) self->ram_start,
115 ret = ioctl(self->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem, 1);
117 die_perror("KVM_SET_USER_MEMORY_REGION ioctl");
119 if (!kvm__supports_extension(self, KVM_CAP_SET_TSS_ADDR))
120 die("KVM_CAP_SET_TSS_ADDR is not supported");
122 ret = ioctl(self->vm_fd, KVM_SET_TSS_ADDR, 0xfffbd000);
124 die_perror("KVM_SET_TSS_ADDR ioctl");
126 self->vcpu_fd = ioctl(self->vm_fd, KVM_CREATE_VCPU, 0);
127 if (self->vcpu_fd < 0)
128 die_perror("KVM_CREATE_VCPU ioctl");
130 mmap_size = ioctl(self->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
132 die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
134 self->kvm_run = mmap(NULL, mmap_size, PROT_READ|PROT_WRITE, MAP_SHARED, self->vcpu_fd, 0);
135 if (self->kvm_run == MAP_FAILED)
136 die("unable to mmap vcpu fd");
141 void kvm__enable_singlestep(struct kvm *self)
143 struct kvm_guest_debug debug = {
144 .control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP,
147 if (ioctl(self->vcpu_fd, KVM_SET_GUEST_DEBUG, &debug) < 0)
148 warning("KVM_SET_GUEST_DEBUG failed");
151 static inline uint32_t segment_to_flat(uint16_t selector, uint16_t offset)
153 return ((uint32_t)selector << 4) + (uint32_t) offset;
157 * HACK ALERT! KVM seems to be unable to run 16-bit real mode if 'cs' selector
158 * does not equal to 0xf000 at the beginning. To work around that, we need a
159 * 'reset vector' at 0xf000:0xfff0 that has an hard-coded jump to 0x000:0x7c000
160 * where we also load the Linux kernel boot sector and setup code.
163 #define RESET_VECTOR_CS 0xf000
164 #define RESET_VECTOR_IP 0xfff0
166 static unsigned char reset_vector_code[] = { 0xea, 0x00, 0x7c, 0x00, 0x00 };
168 static void load_reset_vector(struct kvm *self)
172 p = guest_addr_to_host(self, segment_to_flat(RESET_VECTOR_CS, RESET_VECTOR_IP));
174 memcpy(p, reset_vector_code, ARRAY_SIZE(reset_vector_code));
177 #define BOOT_LOADER_CS 0x0000
178 #define BOOT_LOADER_IP 0x7c00
180 static int load_flat_binary(struct kvm *kvm, int fd)
185 if (lseek(fd, 0, SEEK_SET) < 0)
188 p = guest_addr_to_host(kvm, segment_to_flat(BOOT_LOADER_CS, BOOT_LOADER_IP));
190 while ((nr = read(fd, p, 65536)) > 0)
197 * The protected mode kernel part of a modern bzImage is loaded at 1 MB by
200 #define BZ_KERNEL_START 0x100000UL
202 static const char *BZIMAGE_MAGIC = "HdrS";
204 #define BZ_DEFAULT_SETUP_SECTS 4
206 static bool load_bzimage(struct kvm *kvm, int fd)
208 unsigned long setup_sects;
209 struct boot_params boot;
215 * See Documentation/x86/boot.txt for details no bzImage on-disk and
219 if (lseek(fd, 0, SEEK_SET) < 0)
222 read(fd, &boot, sizeof(boot));
224 if (memcmp(&boot.hdr.header, BZIMAGE_MAGIC, strlen(BZIMAGE_MAGIC)) != 0)
227 if (lseek(fd, 0, SEEK_SET) < 0)
230 setup_sects = boot.hdr.setup_sects + 1;
231 if (setup_sects == 0)
232 setup_sects = BZ_DEFAULT_SETUP_SECTS;
234 setup_size = setup_sects << 4;
235 p = guest_addr_to_host(kvm, segment_to_flat(BOOT_LOADER_CS, BOOT_LOADER_IP));
237 if (read(fd, p, setup_size) != setup_size)
240 p = guest_addr_to_host(kvm, BZ_KERNEL_START);
242 while ((nr = read(fd, p, 65536)) > 0)
248 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename)
253 fd = open(kernel_filename, O_RDONLY);
255 die("unable to open kernel");
257 ret = load_bzimage(kvm, fd);
261 ret = load_flat_binary(kvm, fd);
265 die("%s is not a valid bzImage or flat binary", kernel_filename);
271 static inline uint64_t ip_flat_to_real(struct kvm *self, uint64_t ip)
273 uint64_t cs = self->sregs.cs.selector;
275 return ip - (cs << 4);
278 static inline uint64_t ip_real_to_flat(struct kvm *self, uint64_t ip)
280 uint64_t cs = self->sregs.cs.selector;
282 return ip + (cs << 4);
285 void kvm__reset_vcpu(struct kvm *self)
287 load_reset_vector(self);
289 self->sregs = (struct kvm_sregs) {
290 .cr0 = 0x60000010ULL,
291 .cs = (struct kvm_segment) {
292 .selector = RESET_VECTOR_CS,
293 .base = 0xffff0000UL,
300 .ss = (struct kvm_segment) {
307 .ds = (struct kvm_segment) {
314 .es = (struct kvm_segment) {
321 .fs = (struct kvm_segment) {
328 .gs = (struct kvm_segment) {
335 .tr = (struct kvm_segment) {
340 .ldt = (struct kvm_segment) {
345 .gdt = (struct kvm_dtable) {
348 .idt = (struct kvm_dtable) {
353 if (ioctl(self->vcpu_fd, KVM_SET_SREGS, &self->sregs) < 0)
354 die_perror("KVM_SET_SREGS failed");
356 self->regs = (struct kvm_regs) {
357 .rip = RESET_VECTOR_IP,
358 /* We start the guest in 16-bit real mode */
359 .rflags = 0x0000000000000002ULL,
362 if (self->regs.rip > USHRT_MAX)
363 die("ip 0x%" PRIx64 " is too high for real mode", (uint64_t) self->regs.rip);
365 if (ioctl(self->vcpu_fd, KVM_SET_REGS, &self->regs) < 0)
366 die_perror("KVM_SET_REGS failed");
370 void kvm__run(struct kvm *self)
372 if (ioctl(self->vcpu_fd, KVM_RUN, 0) < 0)
373 die_perror("KVM_RUN failed");
376 static void kvm__emulate_io_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
378 fprintf(stderr, "%s port=%x, size=%d, count=%" PRIu32 "\n", __func__, port, size, count);
381 static void kvm__emulate_io_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
383 fprintf(stderr, "%s port=%x, size=%d, count=%" PRIu32 "\n", __func__, port, size, count);
386 void kvm__emulate_io(struct kvm *self, uint16_t port, void *data, int direction, int size, uint32_t count)
388 if (direction == KVM_EXIT_IO_IN)
389 kvm__emulate_io_in(self, port, data, size, count);
391 kvm__emulate_io_out(self, port, data, size, count);
394 static void print_segment(const char *name, struct kvm_segment *seg)
396 printf(" %s %04" PRIx16 " %016" PRIx64 " %08" PRIx32 " %02" PRIx8 " %x %x %x %x %x %x %x\n",
397 name, (uint16_t) seg->selector, (uint64_t) seg->base, (uint32_t) seg->limit,
398 (uint8_t) seg->type, seg->present, seg->dpl, seg->db, seg->s, seg->l, seg->g, seg->avl);
401 void kvm__show_registers(struct kvm *self)
403 unsigned long cr0, cr2, cr3;
404 unsigned long cr4, cr8;
405 unsigned long rax, rbx, rcx;
406 unsigned long rdx, rsi, rdi;
407 unsigned long rbp, r8, r9;
408 unsigned long r10, r11, r12;
409 unsigned long r13, r14, r15;
410 unsigned long rip, rsp;
411 struct kvm_sregs sregs;
412 unsigned long rflags;
413 struct kvm_regs regs;
416 if (ioctl(self->vcpu_fd, KVM_GET_REGS, ®s) < 0)
417 die("KVM_GET_REGS failed");
419 rflags = regs.rflags;
421 rip = regs.rip; rsp = regs.rsp;
422 rax = regs.rax; rbx = regs.rbx; rcx = regs.rcx;
423 rdx = regs.rdx; rsi = regs.rsi; rdi = regs.rdi;
424 rbp = regs.rbp; r8 = regs.r8; r9 = regs.r9;
425 r10 = regs.r10; r11 = regs.r11; r12 = regs.r12;
426 r13 = regs.r13; r14 = regs.r14; r15 = regs.r15;
428 printf("Registers:\n");
429 printf(" rip: %016lx rsp: %016lx flags: %016lx\n", rip, rsp, rflags);
430 printf(" rax: %016lx ebx: %016lx ecx: %016lx\n", rax, rbx, rcx);
431 printf(" rdx: %016lx rsi: %016lx rdi: %016lx\n", rdx, rsi, rdi);
432 printf(" rbp: %016lx r8: %016lx r9: %016lx\n", rbp, r8, r9);
433 printf(" r10: %016lx r11: %016lx r12: %016lx\n", r10, r11, r12);
434 printf(" r13: %016lx r14: %016lx r15: %016lx\n", r13, r14, r15);
436 if (ioctl(self->vcpu_fd, KVM_GET_SREGS, &sregs) < 0)
437 die("KVM_GET_REGS failed");
439 cr0 = sregs.cr0; cr2 = sregs.cr2; cr3 = sregs.cr3;
440 cr4 = sregs.cr4; cr8 = sregs.cr8;
442 printf(" cr0: %016lx cr2: %016lx cr3: %016lx\n", cr0, cr2, cr3);
443 printf(" cr4: %016lx cr8: %016lx\n", cr4, cr8);
444 printf("Segment registers:\n");
445 printf(" register selector base limit type p dpl db s l g avl\n");
446 print_segment("cs ", &sregs.cs);
447 print_segment("ss ", &sregs.ss);
448 print_segment("ds ", &sregs.ds);
449 print_segment("es ", &sregs.es);
450 print_segment("fs ", &sregs.fs);
451 print_segment("gs ", &sregs.gs);
452 print_segment("tr ", &sregs.tr);
453 print_segment("ldt", &sregs.ldt);
454 printf(" [ efer: %016lx apic base: %016lx ]\n", (uint64_t) sregs.efer, (uint64_t) sregs.apic_base);
455 printf("Interrupt bitmap:\n");
457 for (i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++)
458 printf("%016lx ", (uint64_t) sregs.interrupt_bitmap[i]);
462 void kvm__show_code(struct kvm *self)
464 unsigned int code_bytes = 64;
465 unsigned int code_prologue = code_bytes * 43 / 64;
466 unsigned int code_len = code_bytes;
471 if (ioctl(self->vcpu_fd, KVM_GET_REGS, &self->regs) < 0)
472 die("KVM_GET_REGS failed");
474 if (ioctl(self->vcpu_fd, KVM_GET_SREGS, &self->sregs) < 0)
475 die("KVM_GET_SREGS failed");
477 ip = guest_addr_to_host(self, ip_real_to_flat(self, self->regs.rip) - code_prologue);
481 for (i = 0; i < code_len; i++, ip++) {
484 if (ip == guest_addr_to_host(self, ip_real_to_flat(self, self->regs.rip)))
485 printf("<%02x> ", c);