1ae1fae34SPekka Enberg #include "kvm/kvm.h" 272811558SPekka Enberg #include "kvm/read-write.h" 372811558SPekka Enberg #include "kvm/util.h" 424ed52dbSCyrill Gorcunov #include "kvm/strbuf.h" 54298ddadSSasha Levin #include "kvm/mutex.h" 64298ddadSSasha Levin #include "kvm/kvm-cpu.h" 74b1addaeSSasha Levin #include "kvm/kvm-ipc.h" 8eda03319SPekka Enberg 9*d82350d3SWill Deacon #include <linux/kernel.h> 106c7d8514SPekka Enberg #include <linux/kvm.h> 11*d82350d3SWill Deacon #include <linux/list.h> 12495fbd4eSSasha Levin #include <linux/err.h> 13f5ab5f67SPekka Enberg 144b1addaeSSasha Levin #include <sys/un.h> 15e2e876c2SMatt Evans #include <sys/stat.h> 164b1addaeSSasha Levin #include <sys/types.h> 174b1addaeSSasha Levin #include <sys/socket.h> 18ae1fae34SPekka Enberg #include <sys/ioctl.h> 191f9cff23SPekka Enberg #include <sys/mman.h> 202da26a59SPekka Enberg #include <stdbool.h> 2106e41eeaSPekka Enberg #include <limits.h> 22ce79f1caSPekka Enberg #include <signal.h> 23f5ab5f67SPekka Enberg #include <stdarg.h> 24b8f6afcdSPekka Enberg #include <stdlib.h> 25f5ab5f67SPekka Enberg #include <string.h> 260d1f17ecSPekka Enberg #include <unistd.h> 271f9cff23SPekka Enberg #include <stdio.h> 28b8f6afcdSPekka Enberg #include <fcntl.h> 29ce79f1caSPekka Enberg #include <time.h> 304298ddadSSasha Levin #include <sys/eventfd.h> 31c7828731SSasha Levin #include <asm/unistd.h> 3263bc8503SSasha Levin #include <dirent.h> 33b8f6afcdSPekka Enberg 34ae1fae34SPekka Enberg #define DEFINE_KVM_EXIT_REASON(reason) [reason] = #reason 350d1f17ecSPekka Enberg 36ae1fae34SPekka Enberg const char *kvm_exit_reasons[] = { 37ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_UNKNOWN), 38ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_EXCEPTION), 39ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_IO), 40ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_HYPERCALL), 41ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_DEBUG), 42ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_HLT), 43ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_MMIO), 44ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_IRQ_WINDOW_OPEN), 45ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_SHUTDOWN), 46ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_FAIL_ENTRY), 47ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTR), 48ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_SET_TPR), 49ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_TPR_ACCESS), 50ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_S390_SIEIC), 51ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_S390_RESET), 52ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_DCR), 53ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_NMI), 54ae1fae34SPekka Enberg DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTERNAL_ERROR), 5563e158a0SMatt Evans #ifdef CONFIG_PPC64 5663e158a0SMatt Evans DEFINE_KVM_EXIT_REASON(KVM_EXIT_PAPR_HCALL), 5763e158a0SMatt Evans #endif 589b1fb1c3SPekka Enberg }; 599b1fb1c3SPekka Enberg 604298ddadSSasha Levin static int pause_event; 614298ddadSSasha Levin static DEFINE_MUTEX(pause_lock); 62af7b0868SMatt Evans extern struct kvm_ext kvm_req_ext[]; 634298ddadSSasha Levin 649667701cSPekka Enberg static char kvm_dir[PATH_MAX]; 659667701cSPekka Enberg 66495fbd4eSSasha Levin static int set_dir(const char *fmt, va_list args) 679667701cSPekka Enberg { 68dd188f9fSPekka Enberg char tmp[PATH_MAX]; 69dd188f9fSPekka Enberg 70dd188f9fSPekka Enberg vsnprintf(tmp, sizeof(tmp), fmt, args); 71dd188f9fSPekka Enberg 722bc995fbSPekka Enberg mkdir(tmp, 0777); 732bc995fbSPekka Enberg 74dd188f9fSPekka Enberg if (!realpath(tmp, kvm_dir)) 75495fbd4eSSasha Levin return -errno; 76f76a3285SPekka Enberg 77f76a3285SPekka Enberg strcat(kvm_dir, "/"); 78495fbd4eSSasha Levin 79495fbd4eSSasha Levin return 0; 809667701cSPekka Enberg } 819667701cSPekka Enberg 829667701cSPekka Enberg void kvm__set_dir(const char *fmt, ...) 839667701cSPekka Enberg { 849667701cSPekka Enberg va_list args; 859667701cSPekka Enberg 869667701cSPekka Enberg va_start(args, fmt); 879667701cSPekka Enberg set_dir(fmt, args); 889667701cSPekka Enberg va_end(args); 899667701cSPekka Enberg } 909667701cSPekka Enberg 919667701cSPekka Enberg const char *kvm__get_dir(void) 929667701cSPekka Enberg { 939667701cSPekka Enberg return kvm_dir; 949667701cSPekka Enberg } 959667701cSPekka Enberg 961d6fb3f2SSasha Levin bool kvm__supports_extension(struct kvm *kvm, unsigned int extension) 97b8f6afcdSPekka Enberg { 9828fa19c0SPekka Enberg int ret; 99b8f6afcdSPekka Enberg 10043835ac9SSasha Levin ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, extension); 1014076b041SPekka Enberg if (ret < 0) 1024076b041SPekka Enberg return false; 1034076b041SPekka Enberg 1044076b041SPekka Enberg return ret; 1054076b041SPekka Enberg } 1064076b041SPekka Enberg 10743835ac9SSasha Levin static int kvm__check_extensions(struct kvm *kvm) 10855e19624SCyrill Gorcunov { 109495fbd4eSSasha Levin int i; 11055e19624SCyrill Gorcunov 111af7b0868SMatt Evans for (i = 0; ; i++) { 112af7b0868SMatt Evans if (!kvm_req_ext[i].name) 113af7b0868SMatt Evans break; 11443835ac9SSasha Levin if (!kvm__supports_extension(kvm, kvm_req_ext[i].code)) { 115599ed2a8SCyrill Gorcunov pr_err("Unsuppored KVM extension detected: %s", 11655e19624SCyrill Gorcunov kvm_req_ext[i].name); 117495fbd4eSSasha Levin return -i; 11855e19624SCyrill Gorcunov } 11955e19624SCyrill Gorcunov } 12055e19624SCyrill Gorcunov 12155e19624SCyrill Gorcunov return 0; 12255e19624SCyrill Gorcunov } 12355e19624SCyrill Gorcunov 12447621338SSasha Levin struct kvm *kvm__new(void) 1254076b041SPekka Enberg { 126495fbd4eSSasha Levin struct kvm *kvm = calloc(1, sizeof(*kvm)); 12743835ac9SSasha Levin if (!kvm) 128495fbd4eSSasha Levin return ERR_PTR(-ENOMEM); 1294076b041SPekka Enberg 130d648dbf5SCyrill Gorcunov kvm->sys_fd = -1; 131d648dbf5SCyrill Gorcunov kvm->vm_fd = -1; 132d648dbf5SCyrill Gorcunov 13343835ac9SSasha Levin return kvm; 1344076b041SPekka Enberg } 1354076b041SPekka Enberg 136495fbd4eSSasha Levin int kvm__exit(struct kvm *kvm) 1379ef4c68eSPekka Enberg { 138*d82350d3SWill Deacon struct kvm_mem_bank *bank, *tmp; 139495fbd4eSSasha Levin 140*d82350d3SWill Deacon kvm__arch_delete_ram(kvm); 141*d82350d3SWill Deacon 142*d82350d3SWill Deacon list_for_each_entry_safe(bank, tmp, &kvm->mem_banks, list) { 143*d82350d3SWill Deacon list_del(&bank->list); 144*d82350d3SWill Deacon free(bank); 145*d82350d3SWill Deacon } 146*d82350d3SWill Deacon 147*d82350d3SWill Deacon free(kvm); 148495fbd4eSSasha Levin return 0; 1499ef4c68eSPekka Enberg } 15049a8afd1SSasha Levin core_exit(kvm__exit); 1519ef4c68eSPekka Enberg 15296feb589SPekka Enberg /* 15396feb589SPekka Enberg * Note: KVM_SET_USER_MEMORY_REGION assumes that we don't pass overlapping 15496feb589SPekka Enberg * memory regions to it. Therefore, be careful if you use this function for 15596feb589SPekka Enberg * registering memory regions for emulating hardware. 15696feb589SPekka Enberg */ 157495fbd4eSSasha Levin int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr) 1584076b041SPekka Enberg { 1592b0e3342SPekka Enberg struct kvm_userspace_memory_region mem; 160*d82350d3SWill Deacon struct kvm_mem_bank *bank; 161839051d9SSasha Levin int ret; 162839051d9SSasha Levin 163*d82350d3SWill Deacon bank = malloc(sizeof(*bank)); 164*d82350d3SWill Deacon if (!bank) 165*d82350d3SWill Deacon return -ENOMEM; 166*d82350d3SWill Deacon 167*d82350d3SWill Deacon INIT_LIST_HEAD(&bank->list); 168*d82350d3SWill Deacon bank->guest_phys_addr = guest_phys; 169*d82350d3SWill Deacon bank->host_addr = userspace_addr; 170*d82350d3SWill Deacon bank->size = size; 171*d82350d3SWill Deacon 172839051d9SSasha Levin mem = (struct kvm_userspace_memory_region) { 17396feb589SPekka Enberg .slot = kvm->mem_slots++, 174874467f8SSasha Levin .guest_phys_addr = guest_phys, 175874467f8SSasha Levin .memory_size = size, 176c4acb611SIngo Molnar .userspace_addr = (unsigned long)userspace_addr, 177839051d9SSasha Levin }; 178839051d9SSasha Levin 179874467f8SSasha Levin ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem); 180839051d9SSasha Levin if (ret < 0) 181495fbd4eSSasha Levin return -errno; 182495fbd4eSSasha Levin 183*d82350d3SWill Deacon list_add(&bank->list, &kvm->mem_banks); 184495fbd4eSSasha Levin return 0; 185839051d9SSasha Levin } 186839051d9SSasha Levin 1878259b8ccSSasha Levin int kvm__recommended_cpus(struct kvm *kvm) 188384922b3SPekka Enberg { 189384922b3SPekka Enberg int ret; 190384922b3SPekka Enberg 19143835ac9SSasha Levin ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS); 1928259b8ccSSasha Levin if (ret <= 0) 1933b9b691dSMatt Evans /* 1943b9b691dSMatt Evans * api.txt states that if KVM_CAP_NR_VCPUS does not exist, 1953b9b691dSMatt Evans * assume 4. 1963b9b691dSMatt Evans */ 1973b9b691dSMatt Evans return 4; 198384922b3SPekka Enberg 199384922b3SPekka Enberg return ret; 200384922b3SPekka Enberg } 201384922b3SPekka Enberg 2028259b8ccSSasha Levin /* 2038259b8ccSSasha Levin * The following hack should be removed once 'x86: Raise the hard 2048259b8ccSSasha Levin * VCPU count limit' makes it's way into the mainline. 2058259b8ccSSasha Levin */ 2068259b8ccSSasha Levin #ifndef KVM_CAP_MAX_VCPUS 2078259b8ccSSasha Levin #define KVM_CAP_MAX_VCPUS 66 2088259b8ccSSasha Levin #endif 2098259b8ccSSasha Levin 2108259b8ccSSasha Levin int kvm__max_cpus(struct kvm *kvm) 2118259b8ccSSasha Levin { 2128259b8ccSSasha Levin int ret; 2138259b8ccSSasha Levin 2148259b8ccSSasha Levin ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS); 2158259b8ccSSasha Levin if (ret <= 0) 2168259b8ccSSasha Levin ret = kvm__recommended_cpus(kvm); 2178259b8ccSSasha Levin 2188259b8ccSSasha Levin return ret; 2198259b8ccSSasha Levin } 2208259b8ccSSasha Levin 22147621338SSasha Levin int kvm__init(struct kvm *kvm) 222839051d9SSasha Levin { 2234076b041SPekka Enberg int ret; 2244076b041SPekka Enberg 225495fbd4eSSasha Levin if (!kvm__arch_cpu_supports_vm()) { 226495fbd4eSSasha Levin pr_err("Your CPU does not support hardware virtualization"); 2276fce7105SYang Bai ret = -ENOSYS; 2286fce7105SYang Bai goto err; 229495fbd4eSSasha Levin } 230c78b8713SAsias He 23147621338SSasha Levin kvm->sys_fd = open(kvm->cfg.dev, O_RDWR); 23243835ac9SSasha Levin if (kvm->sys_fd < 0) { 233d648dbf5SCyrill Gorcunov if (errno == ENOENT) 234495fbd4eSSasha Levin pr_err("'%s' not found. Please make sure your kernel has CONFIG_KVM " 23547621338SSasha Levin "enabled and that the KVM modules are loaded.", kvm->cfg.dev); 236d648dbf5SCyrill Gorcunov else if (errno == ENODEV) 237d648dbf5SCyrill Gorcunov pr_err("'%s' KVM driver not available.\n # (If the KVM " 238495fbd4eSSasha Levin "module is loaded then 'dmesg' may offer further clues " 23947621338SSasha Levin "about the failure.)", kvm->cfg.dev); 240d648dbf5SCyrill Gorcunov else 24147621338SSasha Levin pr_err("Could not open %s: ", kvm->cfg.dev); 242d648dbf5SCyrill Gorcunov 243495fbd4eSSasha Levin ret = -errno; 244d648dbf5SCyrill Gorcunov goto err_free; 2456d7c36ceSPekka Enberg } 246b8f6afcdSPekka Enberg 24743835ac9SSasha Levin ret = ioctl(kvm->sys_fd, KVM_GET_API_VERSION, 0); 248495fbd4eSSasha Levin if (ret != KVM_API_VERSION) { 249495fbd4eSSasha Levin pr_err("KVM_API_VERSION ioctl"); 250495fbd4eSSasha Levin ret = -errno; 251d648dbf5SCyrill Gorcunov goto err_sys_fd; 252495fbd4eSSasha Levin } 2536c7d8514SPekka Enberg 25443835ac9SSasha Levin kvm->vm_fd = ioctl(kvm->sys_fd, KVM_CREATE_VM, 0); 255495fbd4eSSasha Levin if (kvm->vm_fd < 0) { 256495fbd4eSSasha Levin ret = kvm->vm_fd; 257d648dbf5SCyrill Gorcunov goto err_sys_fd; 258495fbd4eSSasha Levin } 25928fa19c0SPekka Enberg 260495fbd4eSSasha Levin if (kvm__check_extensions(kvm)) { 261495fbd4eSSasha Levin pr_err("A required KVM extention is not supported by OS"); 262495fbd4eSSasha Levin ret = -ENOSYS; 2636fce7105SYang Bai goto err_vm_fd; 264495fbd4eSSasha Levin } 2659687927dSAsias He 26647621338SSasha Levin kvm__arch_init(kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); 2679687927dSAsias He 268*d82350d3SWill Deacon INIT_LIST_HEAD(&kvm->mem_banks); 269abee258bSSasha Levin kvm__init_ram(kvm); 270abee258bSSasha Levin 271084a1356SSasha Levin if (!kvm->cfg.firmware_filename) { 272084a1356SSasha Levin if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename, 273084a1356SSasha Levin kvm->cfg.initrd_filename, kvm->cfg.real_cmdline, kvm->cfg.vidmode)) 274084a1356SSasha Levin die("unable to load kernel %s", kvm->cfg.kernel_filename); 275084a1356SSasha Levin } 276084a1356SSasha Levin 277084a1356SSasha Levin if (kvm->cfg.firmware_filename) { 278084a1356SSasha Levin if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename)) 279084a1356SSasha Levin die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno)); 280084a1356SSasha Levin } else { 281084a1356SSasha Levin ret = kvm__arch_setup_firmware(kvm); 282084a1356SSasha Levin if (ret < 0) 283084a1356SSasha Levin die("kvm__arch_setup_firmware() failed with error %d\n", ret); 284084a1356SSasha Levin } 285084a1356SSasha Levin 28647621338SSasha Levin return 0; 287d648dbf5SCyrill Gorcunov 2886fce7105SYang Bai err_vm_fd: 289495fbd4eSSasha Levin close(kvm->vm_fd); 290d648dbf5SCyrill Gorcunov err_sys_fd: 291495fbd4eSSasha Levin close(kvm->sys_fd); 292d648dbf5SCyrill Gorcunov err_free: 293495fbd4eSSasha Levin free(kvm); 2946fce7105SYang Bai err: 29547621338SSasha Levin return ret; 2964076b041SPekka Enberg } 29749a8afd1SSasha Levin core_init(kvm__init); 2984076b041SPekka Enberg 29972811558SPekka Enberg /* RFC 1952 */ 30072811558SPekka Enberg #define GZIP_ID1 0x1f 30172811558SPekka Enberg #define GZIP_ID2 0x8b 302663ce1dfSMatt Evans #define CPIO_MAGIC "0707" 303663ce1dfSMatt Evans /* initrd may be gzipped, or a plain cpio */ 30472811558SPekka Enberg static bool initrd_check(int fd) 30572811558SPekka Enberg { 306663ce1dfSMatt Evans unsigned char id[4]; 30772811558SPekka Enberg 30872811558SPekka Enberg if (read_in_full(fd, id, ARRAY_SIZE(id)) < 0) 30972811558SPekka Enberg return false; 31072811558SPekka Enberg 31172811558SPekka Enberg if (lseek(fd, 0, SEEK_SET) < 0) 31272811558SPekka Enberg die_perror("lseek"); 31372811558SPekka Enberg 314663ce1dfSMatt Evans return (id[0] == GZIP_ID1 && id[1] == GZIP_ID2) || 315663ce1dfSMatt Evans !memcmp(id, CPIO_MAGIC, 4); 31672811558SPekka Enberg } 31772811558SPekka Enberg 3186d1f350dSCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, 31953861c74SJohn Floren const char *initrd_filename, const char *kernel_cmdline, u16 vidmode) 320ae1fae34SPekka Enberg { 3217fb218bdSPekka Enberg bool ret; 3222065a6f7SCyrill Gorcunov int fd_kernel = -1, fd_initrd = -1; 323ae1fae34SPekka Enberg 3242065a6f7SCyrill Gorcunov fd_kernel = open(kernel_filename, O_RDONLY); 3252065a6f7SCyrill Gorcunov if (fd_kernel < 0) 3260b62d2bbSPekka Enberg die("Unable to open kernel %s", kernel_filename); 327ae1fae34SPekka Enberg 3282065a6f7SCyrill Gorcunov if (initrd_filename) { 3292065a6f7SCyrill Gorcunov fd_initrd = open(initrd_filename, O_RDONLY); 3302065a6f7SCyrill Gorcunov if (fd_initrd < 0) 3310b62d2bbSPekka Enberg die("Unable to open initrd %s", initrd_filename); 33272811558SPekka Enberg 33372811558SPekka Enberg if (!initrd_check(fd_initrd)) 33472811558SPekka Enberg die("%s is not an initrd", initrd_filename); 3352065a6f7SCyrill Gorcunov } 3362065a6f7SCyrill Gorcunov 33753861c74SJohn Floren ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, vidmode); 33828972750SCyrill Gorcunov 339009b0758SPekka Enberg if (ret) 340009b0758SPekka Enberg goto found_kernel; 341ae1fae34SPekka Enberg 3424542f276SCyrill Gorcunov pr_warning("%s is not a bzImage. Trying to load it as a flat binary...", kernel_filename); 3430b62d2bbSPekka Enberg 344604dbd63SMatt Evans ret = load_flat_binary(kvm, fd_kernel, fd_initrd, kernel_cmdline); 345604dbd63SMatt Evans 346009b0758SPekka Enberg if (ret) 347009b0758SPekka Enberg goto found_kernel; 348009b0758SPekka Enberg 349604dbd63SMatt Evans if (initrd_filename) 350604dbd63SMatt Evans close(fd_initrd); 3515a6ac675SSasha Levin close(fd_kernel); 3525a6ac675SSasha Levin 353009b0758SPekka Enberg die("%s is not a valid bzImage or flat binary", kernel_filename); 354009b0758SPekka Enberg 355009b0758SPekka Enberg found_kernel: 356604dbd63SMatt Evans if (initrd_filename) 357604dbd63SMatt Evans close(fd_initrd); 3585a6ac675SSasha Levin close(fd_kernel); 3595a6ac675SSasha Levin 360ae1fae34SPekka Enberg return ret; 361ae1fae34SPekka Enberg } 362ae1fae34SPekka Enberg 363ce79f1caSPekka Enberg #define TIMER_INTERVAL_NS 1000000 /* 1 msec */ 364ce79f1caSPekka Enberg 365ce79f1caSPekka Enberg /* 366ce79f1caSPekka Enberg * This function sets up a timer that's used to inject interrupts from the 367ce79f1caSPekka Enberg * userspace hypervisor into the guest at periodical intervals. Please note 368ce79f1caSPekka Enberg * that clock interrupt, for example, is not handled here. 369ce79f1caSPekka Enberg */ 370b4532ca9SSasha Levin int kvm_timer__init(struct kvm *kvm) 371ce79f1caSPekka Enberg { 372ce79f1caSPekka Enberg struct itimerspec its; 373ce79f1caSPekka Enberg struct sigevent sev; 374b4532ca9SSasha Levin int r; 375ce79f1caSPekka Enberg 376ce79f1caSPekka Enberg memset(&sev, 0, sizeof(struct sigevent)); 377ce79f1caSPekka Enberg sev.sigev_value.sival_int = 0; 378c7828731SSasha Levin sev.sigev_notify = SIGEV_THREAD_ID; 379ce79f1caSPekka Enberg sev.sigev_signo = SIGALRM; 3805002444cSSasha Levin sev.sigev_value.sival_ptr = kvm; 381c7828731SSasha Levin sev._sigev_un._tid = syscall(__NR_gettid); 382ce79f1caSPekka Enberg 383b4532ca9SSasha Levin r = timer_create(CLOCK_REALTIME, &sev, &kvm->timerid); 384b4532ca9SSasha Levin if (r < 0) 385b4532ca9SSasha Levin return r; 386ce79f1caSPekka Enberg 387ce79f1caSPekka Enberg its.it_value.tv_sec = TIMER_INTERVAL_NS / 1000000000; 388ce79f1caSPekka Enberg its.it_value.tv_nsec = TIMER_INTERVAL_NS % 1000000000; 389ce79f1caSPekka Enberg its.it_interval.tv_sec = its.it_value.tv_sec; 390ce79f1caSPekka Enberg its.it_interval.tv_nsec = its.it_value.tv_nsec; 391ce79f1caSPekka Enberg 392b4532ca9SSasha Levin r = timer_settime(kvm->timerid, 0, &its, NULL); 393b4532ca9SSasha Levin if (r < 0) { 394b4532ca9SSasha Levin timer_delete(kvm->timerid); 395b4532ca9SSasha Levin return r; 396ce79f1caSPekka Enberg } 397ce79f1caSPekka Enberg 398b4532ca9SSasha Levin return 0; 399b4532ca9SSasha Levin } 40049a8afd1SSasha Levin firmware_init(kvm_timer__init); 401b4532ca9SSasha Levin 402b4532ca9SSasha Levin int kvm_timer__exit(struct kvm *kvm) 403fbfe68b7SSasha Levin { 40443835ac9SSasha Levin if (kvm->timerid) 40543835ac9SSasha Levin if (timer_delete(kvm->timerid) < 0) 406fbfe68b7SSasha Levin die("timer_delete()"); 407fbfe68b7SSasha Levin 40843835ac9SSasha Levin kvm->timerid = 0; 409b4532ca9SSasha Levin 410b4532ca9SSasha Levin return 0; 411fbfe68b7SSasha Levin } 41249a8afd1SSasha Levin firmware_exit(kvm_timer__exit); 413fbfe68b7SSasha Levin 41443835ac9SSasha Levin void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size) 415090f898eSCyrill Gorcunov { 416090f898eSCyrill Gorcunov unsigned char *p; 417090f898eSCyrill Gorcunov unsigned long n; 418090f898eSCyrill Gorcunov 419090f898eSCyrill Gorcunov size &= ~7; /* mod 8 */ 420090f898eSCyrill Gorcunov if (!size) 421090f898eSCyrill Gorcunov return; 422090f898eSCyrill Gorcunov 42343835ac9SSasha Levin p = guest_flat_to_host(kvm, addr); 424090f898eSCyrill Gorcunov 42548cf3877SPekka Enberg for (n = 0; n < size; n += 8) { 42643835ac9SSasha Levin if (!host_ptr_in_ram(kvm, p + n)) 42748cf3877SPekka Enberg break; 42848cf3877SPekka Enberg 429090f898eSCyrill Gorcunov printf(" 0x%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n", 430090f898eSCyrill Gorcunov addr + n, p[n + 0], p[n + 1], p[n + 2], p[n + 3], 431090f898eSCyrill Gorcunov p[n + 4], p[n + 5], p[n + 6], p[n + 7]); 432090f898eSCyrill Gorcunov } 43348cf3877SPekka Enberg } 4344298ddadSSasha Levin 4354346fd8fSSasha Levin void kvm__pause(struct kvm *kvm) 4364298ddadSSasha Levin { 4374298ddadSSasha Levin int i, paused_vcpus = 0; 4384298ddadSSasha Levin 4394298ddadSSasha Levin /* Check if the guest is running */ 440df4239fbSSasha Levin if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0) 4414298ddadSSasha Levin return; 4424298ddadSSasha Levin 4434298ddadSSasha Levin mutex_lock(&pause_lock); 4444298ddadSSasha Levin 4454298ddadSSasha Levin pause_event = eventfd(0, 0); 4464298ddadSSasha Levin if (pause_event < 0) 4474298ddadSSasha Levin die("Failed creating pause notification event"); 4484298ddadSSasha Levin for (i = 0; i < kvm->nrcpus; i++) 449df4239fbSSasha Levin pthread_kill(kvm->cpus[i]->thread, SIGKVMPAUSE); 4504298ddadSSasha Levin 4514298ddadSSasha Levin while (paused_vcpus < kvm->nrcpus) { 4524298ddadSSasha Levin u64 cur_read; 4534298ddadSSasha Levin 4544298ddadSSasha Levin if (read(pause_event, &cur_read, sizeof(cur_read)) < 0) 4554298ddadSSasha Levin die("Failed reading pause event"); 4564298ddadSSasha Levin paused_vcpus += cur_read; 4574298ddadSSasha Levin } 4584298ddadSSasha Levin close(pause_event); 4594298ddadSSasha Levin } 4604298ddadSSasha Levin 4614346fd8fSSasha Levin void kvm__continue(struct kvm *kvm) 4624298ddadSSasha Levin { 4634298ddadSSasha Levin /* Check if the guest is running */ 464df4239fbSSasha Levin if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0) 4654298ddadSSasha Levin return; 4664298ddadSSasha Levin 4674298ddadSSasha Levin mutex_unlock(&pause_lock); 4684298ddadSSasha Levin } 4694298ddadSSasha Levin 4704298ddadSSasha Levin void kvm__notify_paused(void) 4714298ddadSSasha Levin { 4724298ddadSSasha Levin u64 p = 1; 4734298ddadSSasha Levin 4744298ddadSSasha Levin if (write(pause_event, &p, sizeof(p)) < 0) 4754298ddadSSasha Levin die("Failed notifying of paused VCPU."); 4764298ddadSSasha Levin 4774298ddadSSasha Levin mutex_lock(&pause_lock); 4784298ddadSSasha Levin mutex_unlock(&pause_lock); 4794298ddadSSasha Levin } 480