3 #include "kvm/cpufeature.h"
4 #include "kvm/interrupt.h"
5 #include "kvm/boot-protocol.h"
10 #include <asm/bootparam.h>
12 #include <sys/ioctl.h>
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 #define DEFINE_KVM_EXT(ext) \
59 { DEFINE_KVM_EXT(KVM_CAP_COALESCED_MMIO) },
60 { DEFINE_KVM_EXT(KVM_CAP_SET_TSS_ADDR) },
61 { DEFINE_KVM_EXT(KVM_CAP_PIT2) },
62 { DEFINE_KVM_EXT(KVM_CAP_USER_MEMORY) },
63 { DEFINE_KVM_EXT(KVM_CAP_IRQ_ROUTING) },
64 { DEFINE_KVM_EXT(KVM_CAP_IRQCHIP) },
65 { DEFINE_KVM_EXT(KVM_CAP_HLT) },
66 { DEFINE_KVM_EXT(KVM_CAP_IRQ_INJECT_STATUS) },
67 { DEFINE_KVM_EXT(KVM_CAP_EXT_CPUID) },
70 static bool kvm__supports_extension(struct kvm *self, unsigned int extension)
74 ret = ioctl(self->sys_fd, KVM_CHECK_EXTENSION, extension);
81 static int kvm__check_extensions(struct kvm *self)
85 for (i = 0; i < ARRAY_SIZE(kvm_req_ext); i++) {
86 if (!kvm__supports_extension(self, kvm_req_ext[i].code)) {
87 error("Unsuppored KVM extension detected: %s",
96 static struct kvm *kvm__new(void)
98 struct kvm *self = calloc(1, sizeof *self);
101 die("out of memory");
106 void kvm__delete(struct kvm *self)
108 kvm__stop_timer(self);
110 free(self->ram_start);
114 static bool kvm__cpu_supports_vm(void)
116 struct cpuid_regs regs;
120 regs = (struct cpuid_regs) {
126 case CPUID_VENDOR_INTEL_1:
128 feature = KVM__X86_FEATURE_VMX;
131 case CPUID_VENDOR_AMD_1:
132 eax_base = 0x80000000;
133 feature = KVM__X86_FEATURE_SVM;
140 regs = (struct cpuid_regs) {
145 if (regs.eax < eax_base + 0x01)
148 regs = (struct cpuid_regs) {
149 .eax = eax_base + 0x01
153 return regs.ecx & (1 << feature);
156 struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size)
158 struct kvm_userspace_memory_region mem;
159 struct kvm_pit_config pit_config = { .flags = 0, };
164 if (!kvm__cpu_supports_vm())
165 die("Your CPU does not support hardware virtualization");
169 self->sys_fd = open(kvm_dev, O_RDWR);
170 if (self->sys_fd < 0) {
172 die("'%s' not found. Please make sure your kernel has CONFIG_KVM enabled and that the KVM modules are loaded.", kvm_dev);
174 die("'%s' KVM driver not available.\n # (If the KVM module is loaded then 'dmesg' may offer further clues about the failure.)", kvm_dev);
176 fprintf(stderr, " Fatal, could not open %s: ", kvm_dev);
181 ret = ioctl(self->sys_fd, KVM_GET_API_VERSION, 0);
182 if (ret != KVM_API_VERSION)
183 die_perror("KVM_API_VERSION ioctl");
185 self->vm_fd = ioctl(self->sys_fd, KVM_CREATE_VM, 0);
187 die_perror("KVM_CREATE_VM ioctl");
189 if (kvm__check_extensions(self))
190 die("A required KVM extention is not supported by OS");
192 ret = ioctl(self->vm_fd, KVM_SET_TSS_ADDR, 0xfffbd000);
194 die_perror("KVM_SET_TSS_ADDR ioctl");
196 ret = ioctl(self->vm_fd, KVM_CREATE_PIT2, &pit_config);
198 die_perror("KVM_CREATE_PIT2 ioctl");
200 self->ram_size = ram_size;
202 page_size = sysconf(_SC_PAGESIZE);
203 if (posix_memalign(&self->ram_start, page_size, self->ram_size) != 0)
204 die("out of memory");
206 mem = (struct kvm_userspace_memory_region) {
208 .guest_phys_addr = 0x0UL,
209 .memory_size = self->ram_size,
210 .userspace_addr = (unsigned long) self->ram_start,
213 ret = ioctl(self->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
215 die_perror("KVM_SET_USER_MEMORY_REGION ioctl");
217 ret = ioctl(self->vm_fd, KVM_CREATE_IRQCHIP);
219 die_perror("KVM_CREATE_IRQCHIP ioctl");
224 #define BOOT_LOADER_SELECTOR 0x1000
225 #define BOOT_LOADER_IP 0x0000
226 #define BOOT_LOADER_SP 0x8000
227 #define BOOT_CMDLINE_OFFSET 0x20000
229 #define BOOT_PROTOCOL_REQUIRED 0x206
230 #define LOAD_HIGH 0x01
232 static int load_flat_binary(struct kvm *self, int fd)
237 if (lseek(fd, 0, SEEK_SET) < 0)
240 p = guest_real_to_host(self, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
242 while ((nr = read(fd, p, 65536)) > 0)
245 self->boot_selector = BOOT_LOADER_SELECTOR;
246 self->boot_ip = BOOT_LOADER_IP;
247 self->boot_sp = BOOT_LOADER_SP;
252 static const char *BZIMAGE_MAGIC = "HdrS";
254 static bool load_bzimage(struct kvm *self, int fd_kernel,
255 int fd_initrd, const char *kernel_cmdline)
257 struct boot_params *kern_boot;
258 unsigned long setup_sects;
259 struct boot_params boot;
266 * See Documentation/x86/boot.txt for details no bzImage on-disk and
270 if (lseek(fd_kernel, 0, SEEK_SET) < 0)
273 if (read(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
276 if (memcmp(&boot.hdr.header, BZIMAGE_MAGIC, strlen(BZIMAGE_MAGIC)))
279 if (boot.hdr.version < BOOT_PROTOCOL_REQUIRED)
280 die("Too old kernel");
282 if (lseek(fd_kernel, 0, SEEK_SET) < 0)
285 if (!boot.hdr.setup_sects)
286 boot.hdr.setup_sects = BZ_DEFAULT_SETUP_SECTS;
287 setup_sects = boot.hdr.setup_sects + 1;
289 setup_size = setup_sects << 9;
290 p = guest_real_to_host(self, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
292 /* copy setup.bin to mem*/
293 if (read(fd_kernel, p, setup_size) != setup_size)
296 /* copy vmlinux.bin to BZ_KERNEL_START*/
297 p = guest_flat_to_host(self, BZ_KERNEL_START);
299 while ((nr = read(fd_kernel, p, 65536)) > 0)
302 p = guest_flat_to_host(self, BOOT_CMDLINE_OFFSET);
303 if (kernel_cmdline) {
304 cmdline_size = strlen(kernel_cmdline) + 1;
305 if (cmdline_size > boot.hdr.cmdline_size)
306 cmdline_size = boot.hdr.cmdline_size;
308 memset(p, 0, boot.hdr.cmdline_size);
309 memcpy(p, kernel_cmdline, cmdline_size - 1);
312 kern_boot = guest_real_to_host(self, BOOT_LOADER_SELECTOR, 0x00);
314 kern_boot->hdr.cmd_line_ptr = BOOT_CMDLINE_OFFSET;
315 kern_boot->hdr.type_of_loader = 0xff;
316 kern_boot->hdr.heap_end_ptr = 0xfe00;
317 kern_boot->hdr.loadflags |= CAN_USE_HEAP;
320 * Read initrd image into guest memory
322 if (fd_initrd >= 0) {
323 struct stat initrd_stat;
326 if (fstat(fd_initrd, &initrd_stat))
329 addr = boot.hdr.initrd_addr_max & ~0xfffff;
331 if (addr < BZ_KERNEL_START)
332 die("Not enough memory for initrd");
333 else if (addr < (self->ram_size - initrd_stat.st_size))
338 p = guest_flat_to_host(self, addr);
339 nr = read(fd_initrd, p, initrd_stat.st_size);
340 if (nr != initrd_stat.st_size)
341 die("Failed to read initrd");
343 kern_boot->hdr.ramdisk_image = addr;
344 kern_boot->hdr.ramdisk_size = initrd_stat.st_size;
347 self->boot_selector = BOOT_LOADER_SELECTOR;
349 * The real-mode setup code starts at offset 0x200 of a bzImage. See
350 * Documentation/x86/boot.txt for details.
352 self->boot_ip = BOOT_LOADER_IP + 0x200;
353 self->boot_sp = BOOT_LOADER_SP;
358 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
359 const char *initrd_filename, const char *kernel_cmdline)
362 int fd_kernel = -1, fd_initrd = -1;
364 fd_kernel = open(kernel_filename, O_RDONLY);
366 die("Unable to open kernel %s", kernel_filename);
368 if (initrd_filename) {
369 fd_initrd = open(initrd_filename, O_RDONLY);
371 die("Unable to open initrd %s", initrd_filename);
374 ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline);
382 warning("%s is not a bzImage. Trying to load it as a flat binary...", kernel_filename);
384 ret = load_flat_binary(kvm, fd_kernel);
388 die("%s is not a valid bzImage or flat binary", kernel_filename);
395 * kvm__setup_bios - inject BIOS into guest system memory
396 * @self - guest system descriptor
398 * This function is a main routine where we poke guest memory
399 * and install BIOS there.
401 void kvm__setup_bios(struct kvm *self)
403 /* standart minimal configuration */
406 /* FIXME: SMP, ACPI and friends here */
409 #define TIMER_INTERVAL_NS 1000000 /* 1 msec */
411 static void alarm_handler(int sig)
416 * This function sets up a timer that's used to inject interrupts from the
417 * userspace hypervisor into the guest at periodical intervals. Please note
418 * that clock interrupt, for example, is not handled here.
420 void kvm__start_timer(struct kvm *self)
422 struct itimerspec its;
426 sigfillset(&sa.sa_mask);
428 sa.sa_handler = alarm_handler;
430 sigaction(SIGALRM, &sa, NULL);
432 memset(&sev, 0, sizeof(struct sigevent));
433 sev.sigev_value.sival_int = 0;
434 sev.sigev_notify = SIGEV_SIGNAL;
435 sev.sigev_signo = SIGALRM;
437 if (timer_create(CLOCK_REALTIME, &sev, &self->timerid) < 0)
438 die("timer_create()");
440 its.it_value.tv_sec = TIMER_INTERVAL_NS / 1000000000;
441 its.it_value.tv_nsec = TIMER_INTERVAL_NS % 1000000000;
442 its.it_interval.tv_sec = its.it_value.tv_sec;
443 its.it_interval.tv_nsec = its.it_value.tv_nsec;
445 if (timer_settime(self->timerid, 0, &its, NULL) < 0)
446 die("timer_settime()");
449 void kvm__stop_timer(struct kvm *self)
452 if (timer_delete(self->timerid) < 0)
453 die("timer_delete()");
458 void kvm__irq_line(struct kvm *self, int irq, int level)
460 struct kvm_irq_level irq_level;
462 irq_level = (struct kvm_irq_level) {
469 if (ioctl(self->vm_fd, KVM_IRQ_LINE, &irq_level) < 0)
470 die_perror("KVM_IRQ_LINE failed");
473 void kvm__dump_mem(struct kvm *self, unsigned long addr, unsigned long size)
478 size &= ~7; /* mod 8 */
482 p = guest_flat_to_host(self, addr);
484 for (n = 0; n < size; n += 8) {
485 if (!host_ptr_in_ram(self, p + n))
488 printf(" 0x%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n",
489 addr + n, p[n + 0], p[n + 1], p[n + 2], p[n + 3],
490 p[n + 4], p[n + 5], p[n + 6], p[n + 7]);