2 #include "kvm/read-write.h"
4 #include "kvm/strbuf.h"
6 #include "kvm/kvm-cpu.h"
7 #include "kvm/kvm-ipc.h"
10 #include <linux/err.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
28 #include <sys/eventfd.h>
29 #include <asm/unistd.h>
32 #define DEFINE_KVM_EXIT_REASON(reason) [reason] = #reason
34 const char *kvm_exit_reasons[] = {
35 DEFINE_KVM_EXIT_REASON(KVM_EXIT_UNKNOWN),
36 DEFINE_KVM_EXIT_REASON(KVM_EXIT_EXCEPTION),
37 DEFINE_KVM_EXIT_REASON(KVM_EXIT_IO),
38 DEFINE_KVM_EXIT_REASON(KVM_EXIT_HYPERCALL),
39 DEFINE_KVM_EXIT_REASON(KVM_EXIT_DEBUG),
40 DEFINE_KVM_EXIT_REASON(KVM_EXIT_HLT),
41 DEFINE_KVM_EXIT_REASON(KVM_EXIT_MMIO),
42 DEFINE_KVM_EXIT_REASON(KVM_EXIT_IRQ_WINDOW_OPEN),
43 DEFINE_KVM_EXIT_REASON(KVM_EXIT_SHUTDOWN),
44 DEFINE_KVM_EXIT_REASON(KVM_EXIT_FAIL_ENTRY),
45 DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTR),
46 DEFINE_KVM_EXIT_REASON(KVM_EXIT_SET_TPR),
47 DEFINE_KVM_EXIT_REASON(KVM_EXIT_TPR_ACCESS),
48 DEFINE_KVM_EXIT_REASON(KVM_EXIT_S390_SIEIC),
49 DEFINE_KVM_EXIT_REASON(KVM_EXIT_S390_RESET),
50 DEFINE_KVM_EXIT_REASON(KVM_EXIT_DCR),
51 DEFINE_KVM_EXIT_REASON(KVM_EXIT_NMI),
52 DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTERNAL_ERROR),
54 DEFINE_KVM_EXIT_REASON(KVM_EXIT_PAPR_HCALL),
58 static int pause_event;
59 static DEFINE_MUTEX(pause_lock);
60 extern struct kvm_ext kvm_req_ext[];
62 static char kvm_dir[PATH_MAX];
64 static int set_dir(const char *fmt, va_list args)
68 vsnprintf(tmp, sizeof(tmp), fmt, args);
72 if (!realpath(tmp, kvm_dir))
80 void kvm__set_dir(const char *fmt, ...)
89 const char *kvm__get_dir(void)
94 bool kvm__supports_extension(struct kvm *kvm, unsigned int extension)
98 ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, extension);
105 static int kvm__check_extensions(struct kvm *kvm)
110 if (!kvm_req_ext[i].name)
112 if (!kvm__supports_extension(kvm, kvm_req_ext[i].code)) {
113 pr_err("Unsuppored KVM extension detected: %s",
114 kvm_req_ext[i].name);
122 struct kvm *kvm__new(void)
124 struct kvm *kvm = calloc(1, sizeof(*kvm));
126 return ERR_PTR(-ENOMEM);
134 int kvm__exit(struct kvm *kvm)
136 kvm__arch_delete_ram(kvm);
141 core_exit(kvm__exit);
144 * Note: KVM_SET_USER_MEMORY_REGION assumes that we don't pass overlapping
145 * memory regions to it. Therefore, be careful if you use this function for
146 * registering memory regions for emulating hardware.
148 int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr)
150 struct kvm_userspace_memory_region mem;
153 mem = (struct kvm_userspace_memory_region) {
154 .slot = kvm->mem_slots++,
155 .guest_phys_addr = guest_phys,
157 .userspace_addr = (unsigned long)userspace_addr,
160 ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
167 int kvm__recommended_cpus(struct kvm *kvm)
171 ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
174 * api.txt states that if KVM_CAP_NR_VCPUS does not exist,
183 * The following hack should be removed once 'x86: Raise the hard
184 * VCPU count limit' makes it's way into the mainline.
186 #ifndef KVM_CAP_MAX_VCPUS
187 #define KVM_CAP_MAX_VCPUS 66
190 int kvm__max_cpus(struct kvm *kvm)
194 ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS);
196 ret = kvm__recommended_cpus(kvm);
201 int kvm__init(struct kvm *kvm)
205 if (!kvm__arch_cpu_supports_vm()) {
206 pr_err("Your CPU does not support hardware virtualization");
211 kvm->sys_fd = open(kvm->cfg.dev, O_RDWR);
212 if (kvm->sys_fd < 0) {
214 pr_err("'%s' not found. Please make sure your kernel has CONFIG_KVM "
215 "enabled and that the KVM modules are loaded.", kvm->cfg.dev);
216 else if (errno == ENODEV)
217 pr_err("'%s' KVM driver not available.\n # (If the KVM "
218 "module is loaded then 'dmesg' may offer further clues "
219 "about the failure.)", kvm->cfg.dev);
221 pr_err("Could not open %s: ", kvm->cfg.dev);
227 ret = ioctl(kvm->sys_fd, KVM_GET_API_VERSION, 0);
228 if (ret != KVM_API_VERSION) {
229 pr_err("KVM_API_VERSION ioctl");
234 kvm->vm_fd = ioctl(kvm->sys_fd, KVM_CREATE_VM, 0);
235 if (kvm->vm_fd < 0) {
240 if (kvm__check_extensions(kvm)) {
241 pr_err("A required KVM extention is not supported by OS");
246 kvm__arch_init(kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size);
250 if (!kvm->cfg.firmware_filename) {
251 if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename,
252 kvm->cfg.initrd_filename, kvm->cfg.real_cmdline, kvm->cfg.vidmode))
253 die("unable to load kernel %s", kvm->cfg.kernel_filename);
256 if (kvm->cfg.firmware_filename) {
257 if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename))
258 die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno));
260 ret = kvm__arch_setup_firmware(kvm);
262 die("kvm__arch_setup_firmware() failed with error %d\n", ret);
276 core_init(kvm__init);
279 #define GZIP_ID1 0x1f
280 #define GZIP_ID2 0x8b
281 #define CPIO_MAGIC "0707"
282 /* initrd may be gzipped, or a plain cpio */
283 static bool initrd_check(int fd)
287 if (read_in_full(fd, id, ARRAY_SIZE(id)) < 0)
290 if (lseek(fd, 0, SEEK_SET) < 0)
293 return (id[0] == GZIP_ID1 && id[1] == GZIP_ID2) ||
294 !memcmp(id, CPIO_MAGIC, 4);
297 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
298 const char *initrd_filename, const char *kernel_cmdline, u16 vidmode)
301 int fd_kernel = -1, fd_initrd = -1;
303 fd_kernel = open(kernel_filename, O_RDONLY);
305 die("Unable to open kernel %s", kernel_filename);
307 if (initrd_filename) {
308 fd_initrd = open(initrd_filename, O_RDONLY);
310 die("Unable to open initrd %s", initrd_filename);
312 if (!initrd_check(fd_initrd))
313 die("%s is not an initrd", initrd_filename);
316 ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, vidmode);
321 pr_warning("%s is not a bzImage. Trying to load it as a flat binary...", kernel_filename);
323 ret = load_flat_binary(kvm, fd_kernel, fd_initrd, kernel_cmdline);
332 die("%s is not a valid bzImage or flat binary", kernel_filename);
342 #define TIMER_INTERVAL_NS 1000000 /* 1 msec */
345 * This function sets up a timer that's used to inject interrupts from the
346 * userspace hypervisor into the guest at periodical intervals. Please note
347 * that clock interrupt, for example, is not handled here.
349 int kvm_timer__init(struct kvm *kvm)
351 struct itimerspec its;
355 memset(&sev, 0, sizeof(struct sigevent));
356 sev.sigev_value.sival_int = 0;
357 sev.sigev_notify = SIGEV_THREAD_ID;
358 sev.sigev_signo = SIGALRM;
359 sev.sigev_value.sival_ptr = kvm;
360 sev._sigev_un._tid = syscall(__NR_gettid);
362 r = timer_create(CLOCK_REALTIME, &sev, &kvm->timerid);
366 its.it_value.tv_sec = TIMER_INTERVAL_NS / 1000000000;
367 its.it_value.tv_nsec = TIMER_INTERVAL_NS % 1000000000;
368 its.it_interval.tv_sec = its.it_value.tv_sec;
369 its.it_interval.tv_nsec = its.it_value.tv_nsec;
371 r = timer_settime(kvm->timerid, 0, &its, NULL);
373 timer_delete(kvm->timerid);
379 firmware_init(kvm_timer__init);
381 int kvm_timer__exit(struct kvm *kvm)
384 if (timer_delete(kvm->timerid) < 0)
385 die("timer_delete()");
391 firmware_exit(kvm_timer__exit);
393 void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size)
398 size &= ~7; /* mod 8 */
402 p = guest_flat_to_host(kvm, addr);
404 for (n = 0; n < size; n += 8) {
405 if (!host_ptr_in_ram(kvm, p + n))
408 printf(" 0x%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n",
409 addr + n, p[n + 0], p[n + 1], p[n + 2], p[n + 3],
410 p[n + 4], p[n + 5], p[n + 6], p[n + 7]);
414 void kvm__pause(struct kvm *kvm)
416 int i, paused_vcpus = 0;
418 /* Check if the guest is running */
419 if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0)
422 mutex_lock(&pause_lock);
424 pause_event = eventfd(0, 0);
426 die("Failed creating pause notification event");
427 for (i = 0; i < kvm->nrcpus; i++)
428 pthread_kill(kvm->cpus[i]->thread, SIGKVMPAUSE);
430 while (paused_vcpus < kvm->nrcpus) {
433 if (read(pause_event, &cur_read, sizeof(cur_read)) < 0)
434 die("Failed reading pause event");
435 paused_vcpus += cur_read;
440 void kvm__continue(struct kvm *kvm)
442 /* Check if the guest is running */
443 if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0)
446 mutex_unlock(&pause_lock);
449 void kvm__notify_paused(void)
453 if (write(pause_event, &p, sizeof(p)) < 0)
454 die("Failed notifying of paused VCPU.");
456 mutex_lock(&pause_lock);
457 mutex_unlock(&pause_lock);