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 9d82350d3SWill Deacon #include <linux/kernel.h> 106c7d8514SPekka Enberg #include <linux/kvm.h> 11d82350d3SWill 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 { 138d82350d3SWill Deacon struct kvm_mem_bank *bank, *tmp; 139495fbd4eSSasha Levin 140d82350d3SWill Deacon kvm__arch_delete_ram(kvm); 141d82350d3SWill Deacon 142d82350d3SWill Deacon list_for_each_entry_safe(bank, tmp, &kvm->mem_banks, list) { 143d82350d3SWill Deacon list_del(&bank->list); 144d82350d3SWill Deacon free(bank); 145d82350d3SWill Deacon } 146d82350d3SWill Deacon 147d82350d3SWill 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; 160d82350d3SWill Deacon struct kvm_mem_bank *bank; 161839051d9SSasha Levin int ret; 162839051d9SSasha Levin 163d82350d3SWill Deacon bank = malloc(sizeof(*bank)); 164d82350d3SWill Deacon if (!bank) 165d82350d3SWill Deacon return -ENOMEM; 166d82350d3SWill Deacon 167d82350d3SWill Deacon INIT_LIST_HEAD(&bank->list); 168d82350d3SWill Deacon bank->guest_phys_addr = guest_phys; 169d82350d3SWill Deacon bank->host_addr = userspace_addr; 170d82350d3SWill Deacon bank->size = size; 171d82350d3SWill 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 183d82350d3SWill Deacon list_add(&bank->list, &kvm->mem_banks); 184495fbd4eSSasha Levin return 0; 185839051d9SSasha Levin } 186839051d9SSasha Levin 187*f412251fSWill Deacon void *guest_flat_to_host(struct kvm *kvm, u64 offset) 188*f412251fSWill Deacon { 189*f412251fSWill Deacon struct kvm_mem_bank *bank; 190*f412251fSWill Deacon 191*f412251fSWill Deacon list_for_each_entry(bank, &kvm->mem_banks, list) { 192*f412251fSWill Deacon u64 bank_start = bank->guest_phys_addr; 193*f412251fSWill Deacon u64 bank_end = bank_start + bank->size; 194*f412251fSWill Deacon 195*f412251fSWill Deacon if (offset >= bank_start && offset < bank_end) 196*f412251fSWill Deacon return bank->host_addr + (offset - bank_start); 197*f412251fSWill Deacon } 198*f412251fSWill Deacon 199*f412251fSWill Deacon pr_warning("unable to translate guest address 0x%llx to host", 200*f412251fSWill Deacon (unsigned long long)offset); 201*f412251fSWill Deacon return NULL; 202*f412251fSWill Deacon } 203*f412251fSWill Deacon 2048259b8ccSSasha Levin int kvm__recommended_cpus(struct kvm *kvm) 205384922b3SPekka Enberg { 206384922b3SPekka Enberg int ret; 207384922b3SPekka Enberg 20843835ac9SSasha Levin ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS); 2098259b8ccSSasha Levin if (ret <= 0) 2103b9b691dSMatt Evans /* 2113b9b691dSMatt Evans * api.txt states that if KVM_CAP_NR_VCPUS does not exist, 2123b9b691dSMatt Evans * assume 4. 2133b9b691dSMatt Evans */ 2143b9b691dSMatt Evans return 4; 215384922b3SPekka Enberg 216384922b3SPekka Enberg return ret; 217384922b3SPekka Enberg } 218384922b3SPekka Enberg 2198259b8ccSSasha Levin /* 2208259b8ccSSasha Levin * The following hack should be removed once 'x86: Raise the hard 2218259b8ccSSasha Levin * VCPU count limit' makes it's way into the mainline. 2228259b8ccSSasha Levin */ 2238259b8ccSSasha Levin #ifndef KVM_CAP_MAX_VCPUS 2248259b8ccSSasha Levin #define KVM_CAP_MAX_VCPUS 66 2258259b8ccSSasha Levin #endif 2268259b8ccSSasha Levin 2278259b8ccSSasha Levin int kvm__max_cpus(struct kvm *kvm) 2288259b8ccSSasha Levin { 2298259b8ccSSasha Levin int ret; 2308259b8ccSSasha Levin 2318259b8ccSSasha Levin ret = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS); 2328259b8ccSSasha Levin if (ret <= 0) 2338259b8ccSSasha Levin ret = kvm__recommended_cpus(kvm); 2348259b8ccSSasha Levin 2358259b8ccSSasha Levin return ret; 2368259b8ccSSasha Levin } 2378259b8ccSSasha Levin 23847621338SSasha Levin int kvm__init(struct kvm *kvm) 239839051d9SSasha Levin { 2404076b041SPekka Enberg int ret; 2414076b041SPekka Enberg 242495fbd4eSSasha Levin if (!kvm__arch_cpu_supports_vm()) { 243495fbd4eSSasha Levin pr_err("Your CPU does not support hardware virtualization"); 2446fce7105SYang Bai ret = -ENOSYS; 2456fce7105SYang Bai goto err; 246495fbd4eSSasha Levin } 247c78b8713SAsias He 24847621338SSasha Levin kvm->sys_fd = open(kvm->cfg.dev, O_RDWR); 24943835ac9SSasha Levin if (kvm->sys_fd < 0) { 250d648dbf5SCyrill Gorcunov if (errno == ENOENT) 251495fbd4eSSasha Levin pr_err("'%s' not found. Please make sure your kernel has CONFIG_KVM " 25247621338SSasha Levin "enabled and that the KVM modules are loaded.", kvm->cfg.dev); 253d648dbf5SCyrill Gorcunov else if (errno == ENODEV) 254d648dbf5SCyrill Gorcunov pr_err("'%s' KVM driver not available.\n # (If the KVM " 255495fbd4eSSasha Levin "module is loaded then 'dmesg' may offer further clues " 25647621338SSasha Levin "about the failure.)", kvm->cfg.dev); 257d648dbf5SCyrill Gorcunov else 25847621338SSasha Levin pr_err("Could not open %s: ", kvm->cfg.dev); 259d648dbf5SCyrill Gorcunov 260495fbd4eSSasha Levin ret = -errno; 261d648dbf5SCyrill Gorcunov goto err_free; 2626d7c36ceSPekka Enberg } 263b8f6afcdSPekka Enberg 26443835ac9SSasha Levin ret = ioctl(kvm->sys_fd, KVM_GET_API_VERSION, 0); 265495fbd4eSSasha Levin if (ret != KVM_API_VERSION) { 266495fbd4eSSasha Levin pr_err("KVM_API_VERSION ioctl"); 267495fbd4eSSasha Levin ret = -errno; 268d648dbf5SCyrill Gorcunov goto err_sys_fd; 269495fbd4eSSasha Levin } 2706c7d8514SPekka Enberg 27143835ac9SSasha Levin kvm->vm_fd = ioctl(kvm->sys_fd, KVM_CREATE_VM, 0); 272495fbd4eSSasha Levin if (kvm->vm_fd < 0) { 273495fbd4eSSasha Levin ret = kvm->vm_fd; 274d648dbf5SCyrill Gorcunov goto err_sys_fd; 275495fbd4eSSasha Levin } 27628fa19c0SPekka Enberg 277495fbd4eSSasha Levin if (kvm__check_extensions(kvm)) { 278495fbd4eSSasha Levin pr_err("A required KVM extention is not supported by OS"); 279495fbd4eSSasha Levin ret = -ENOSYS; 2806fce7105SYang Bai goto err_vm_fd; 281495fbd4eSSasha Levin } 2829687927dSAsias He 28347621338SSasha Levin kvm__arch_init(kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); 2849687927dSAsias He 285d82350d3SWill Deacon INIT_LIST_HEAD(&kvm->mem_banks); 286abee258bSSasha Levin kvm__init_ram(kvm); 287abee258bSSasha Levin 288084a1356SSasha Levin if (!kvm->cfg.firmware_filename) { 289084a1356SSasha Levin if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename, 290084a1356SSasha Levin kvm->cfg.initrd_filename, kvm->cfg.real_cmdline, kvm->cfg.vidmode)) 291084a1356SSasha Levin die("unable to load kernel %s", kvm->cfg.kernel_filename); 292084a1356SSasha Levin } 293084a1356SSasha Levin 294084a1356SSasha Levin if (kvm->cfg.firmware_filename) { 295084a1356SSasha Levin if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename)) 296084a1356SSasha Levin die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno)); 297084a1356SSasha Levin } else { 298084a1356SSasha Levin ret = kvm__arch_setup_firmware(kvm); 299084a1356SSasha Levin if (ret < 0) 300084a1356SSasha Levin die("kvm__arch_setup_firmware() failed with error %d\n", ret); 301084a1356SSasha Levin } 302084a1356SSasha Levin 30347621338SSasha Levin return 0; 304d648dbf5SCyrill Gorcunov 3056fce7105SYang Bai err_vm_fd: 306495fbd4eSSasha Levin close(kvm->vm_fd); 307d648dbf5SCyrill Gorcunov err_sys_fd: 308495fbd4eSSasha Levin close(kvm->sys_fd); 309d648dbf5SCyrill Gorcunov err_free: 310495fbd4eSSasha Levin free(kvm); 3116fce7105SYang Bai err: 31247621338SSasha Levin return ret; 3134076b041SPekka Enberg } 31449a8afd1SSasha Levin core_init(kvm__init); 3154076b041SPekka Enberg 31672811558SPekka Enberg /* RFC 1952 */ 31772811558SPekka Enberg #define GZIP_ID1 0x1f 31872811558SPekka Enberg #define GZIP_ID2 0x8b 319663ce1dfSMatt Evans #define CPIO_MAGIC "0707" 320663ce1dfSMatt Evans /* initrd may be gzipped, or a plain cpio */ 32172811558SPekka Enberg static bool initrd_check(int fd) 32272811558SPekka Enberg { 323663ce1dfSMatt Evans unsigned char id[4]; 32472811558SPekka Enberg 32572811558SPekka Enberg if (read_in_full(fd, id, ARRAY_SIZE(id)) < 0) 32672811558SPekka Enberg return false; 32772811558SPekka Enberg 32872811558SPekka Enberg if (lseek(fd, 0, SEEK_SET) < 0) 32972811558SPekka Enberg die_perror("lseek"); 33072811558SPekka Enberg 331663ce1dfSMatt Evans return (id[0] == GZIP_ID1 && id[1] == GZIP_ID2) || 332663ce1dfSMatt Evans !memcmp(id, CPIO_MAGIC, 4); 33372811558SPekka Enberg } 33472811558SPekka Enberg 3356d1f350dSCyrill Gorcunov bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, 33653861c74SJohn Floren const char *initrd_filename, const char *kernel_cmdline, u16 vidmode) 337ae1fae34SPekka Enberg { 3387fb218bdSPekka Enberg bool ret; 3392065a6f7SCyrill Gorcunov int fd_kernel = -1, fd_initrd = -1; 340ae1fae34SPekka Enberg 3412065a6f7SCyrill Gorcunov fd_kernel = open(kernel_filename, O_RDONLY); 3422065a6f7SCyrill Gorcunov if (fd_kernel < 0) 3430b62d2bbSPekka Enberg die("Unable to open kernel %s", kernel_filename); 344ae1fae34SPekka Enberg 3452065a6f7SCyrill Gorcunov if (initrd_filename) { 3462065a6f7SCyrill Gorcunov fd_initrd = open(initrd_filename, O_RDONLY); 3472065a6f7SCyrill Gorcunov if (fd_initrd < 0) 3480b62d2bbSPekka Enberg die("Unable to open initrd %s", initrd_filename); 34972811558SPekka Enberg 35072811558SPekka Enberg if (!initrd_check(fd_initrd)) 35172811558SPekka Enberg die("%s is not an initrd", initrd_filename); 3522065a6f7SCyrill Gorcunov } 3532065a6f7SCyrill Gorcunov 35453861c74SJohn Floren ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, vidmode); 35528972750SCyrill Gorcunov 356009b0758SPekka Enberg if (ret) 357009b0758SPekka Enberg goto found_kernel; 358ae1fae34SPekka Enberg 3594542f276SCyrill Gorcunov pr_warning("%s is not a bzImage. Trying to load it as a flat binary...", kernel_filename); 3600b62d2bbSPekka Enberg 361604dbd63SMatt Evans ret = load_flat_binary(kvm, fd_kernel, fd_initrd, kernel_cmdline); 362604dbd63SMatt Evans 363009b0758SPekka Enberg if (ret) 364009b0758SPekka Enberg goto found_kernel; 365009b0758SPekka Enberg 366604dbd63SMatt Evans if (initrd_filename) 367604dbd63SMatt Evans close(fd_initrd); 3685a6ac675SSasha Levin close(fd_kernel); 3695a6ac675SSasha Levin 370009b0758SPekka Enberg die("%s is not a valid bzImage or flat binary", kernel_filename); 371009b0758SPekka Enberg 372009b0758SPekka Enberg found_kernel: 373604dbd63SMatt Evans if (initrd_filename) 374604dbd63SMatt Evans close(fd_initrd); 3755a6ac675SSasha Levin close(fd_kernel); 3765a6ac675SSasha Levin 377ae1fae34SPekka Enberg return ret; 378ae1fae34SPekka Enberg } 379ae1fae34SPekka Enberg 380ce79f1caSPekka Enberg #define TIMER_INTERVAL_NS 1000000 /* 1 msec */ 381ce79f1caSPekka Enberg 382ce79f1caSPekka Enberg /* 383ce79f1caSPekka Enberg * This function sets up a timer that's used to inject interrupts from the 384ce79f1caSPekka Enberg * userspace hypervisor into the guest at periodical intervals. Please note 385ce79f1caSPekka Enberg * that clock interrupt, for example, is not handled here. 386ce79f1caSPekka Enberg */ 387b4532ca9SSasha Levin int kvm_timer__init(struct kvm *kvm) 388ce79f1caSPekka Enberg { 389ce79f1caSPekka Enberg struct itimerspec its; 390ce79f1caSPekka Enberg struct sigevent sev; 391b4532ca9SSasha Levin int r; 392ce79f1caSPekka Enberg 393ce79f1caSPekka Enberg memset(&sev, 0, sizeof(struct sigevent)); 394ce79f1caSPekka Enberg sev.sigev_value.sival_int = 0; 395c7828731SSasha Levin sev.sigev_notify = SIGEV_THREAD_ID; 396ce79f1caSPekka Enberg sev.sigev_signo = SIGALRM; 3975002444cSSasha Levin sev.sigev_value.sival_ptr = kvm; 398c7828731SSasha Levin sev._sigev_un._tid = syscall(__NR_gettid); 399ce79f1caSPekka Enberg 400b4532ca9SSasha Levin r = timer_create(CLOCK_REALTIME, &sev, &kvm->timerid); 401b4532ca9SSasha Levin if (r < 0) 402b4532ca9SSasha Levin return r; 403ce79f1caSPekka Enberg 404ce79f1caSPekka Enberg its.it_value.tv_sec = TIMER_INTERVAL_NS / 1000000000; 405ce79f1caSPekka Enberg its.it_value.tv_nsec = TIMER_INTERVAL_NS % 1000000000; 406ce79f1caSPekka Enberg its.it_interval.tv_sec = its.it_value.tv_sec; 407ce79f1caSPekka Enberg its.it_interval.tv_nsec = its.it_value.tv_nsec; 408ce79f1caSPekka Enberg 409b4532ca9SSasha Levin r = timer_settime(kvm->timerid, 0, &its, NULL); 410b4532ca9SSasha Levin if (r < 0) { 411b4532ca9SSasha Levin timer_delete(kvm->timerid); 412b4532ca9SSasha Levin return r; 413ce79f1caSPekka Enberg } 414ce79f1caSPekka Enberg 415b4532ca9SSasha Levin return 0; 416b4532ca9SSasha Levin } 41749a8afd1SSasha Levin firmware_init(kvm_timer__init); 418b4532ca9SSasha Levin 419b4532ca9SSasha Levin int kvm_timer__exit(struct kvm *kvm) 420fbfe68b7SSasha Levin { 42143835ac9SSasha Levin if (kvm->timerid) 42243835ac9SSasha Levin if (timer_delete(kvm->timerid) < 0) 423fbfe68b7SSasha Levin die("timer_delete()"); 424fbfe68b7SSasha Levin 42543835ac9SSasha Levin kvm->timerid = 0; 426b4532ca9SSasha Levin 427b4532ca9SSasha Levin return 0; 428fbfe68b7SSasha Levin } 42949a8afd1SSasha Levin firmware_exit(kvm_timer__exit); 430fbfe68b7SSasha Levin 43143835ac9SSasha Levin void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size) 432090f898eSCyrill Gorcunov { 433090f898eSCyrill Gorcunov unsigned char *p; 434090f898eSCyrill Gorcunov unsigned long n; 435090f898eSCyrill Gorcunov 436090f898eSCyrill Gorcunov size &= ~7; /* mod 8 */ 437090f898eSCyrill Gorcunov if (!size) 438090f898eSCyrill Gorcunov return; 439090f898eSCyrill Gorcunov 44043835ac9SSasha Levin p = guest_flat_to_host(kvm, addr); 441090f898eSCyrill Gorcunov 44248cf3877SPekka Enberg for (n = 0; n < size; n += 8) { 44343835ac9SSasha Levin if (!host_ptr_in_ram(kvm, p + n)) 44448cf3877SPekka Enberg break; 44548cf3877SPekka Enberg 446090f898eSCyrill Gorcunov printf(" 0x%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n", 447090f898eSCyrill Gorcunov addr + n, p[n + 0], p[n + 1], p[n + 2], p[n + 3], 448090f898eSCyrill Gorcunov p[n + 4], p[n + 5], p[n + 6], p[n + 7]); 449090f898eSCyrill Gorcunov } 45048cf3877SPekka Enberg } 4514298ddadSSasha Levin 4524346fd8fSSasha Levin void kvm__pause(struct kvm *kvm) 4534298ddadSSasha Levin { 4544298ddadSSasha Levin int i, paused_vcpus = 0; 4554298ddadSSasha Levin 4564298ddadSSasha Levin /* Check if the guest is running */ 457df4239fbSSasha Levin if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0) 4584298ddadSSasha Levin return; 4594298ddadSSasha Levin 4604298ddadSSasha Levin mutex_lock(&pause_lock); 4614298ddadSSasha Levin 4624298ddadSSasha Levin pause_event = eventfd(0, 0); 4634298ddadSSasha Levin if (pause_event < 0) 4644298ddadSSasha Levin die("Failed creating pause notification event"); 4654298ddadSSasha Levin for (i = 0; i < kvm->nrcpus; i++) 466df4239fbSSasha Levin pthread_kill(kvm->cpus[i]->thread, SIGKVMPAUSE); 4674298ddadSSasha Levin 4684298ddadSSasha Levin while (paused_vcpus < kvm->nrcpus) { 4694298ddadSSasha Levin u64 cur_read; 4704298ddadSSasha Levin 4714298ddadSSasha Levin if (read(pause_event, &cur_read, sizeof(cur_read)) < 0) 4724298ddadSSasha Levin die("Failed reading pause event"); 4734298ddadSSasha Levin paused_vcpus += cur_read; 4744298ddadSSasha Levin } 4754298ddadSSasha Levin close(pause_event); 4764298ddadSSasha Levin } 4774298ddadSSasha Levin 4784346fd8fSSasha Levin void kvm__continue(struct kvm *kvm) 4794298ddadSSasha Levin { 4804298ddadSSasha Levin /* Check if the guest is running */ 481df4239fbSSasha Levin if (!kvm->cpus[0] || kvm->cpus[0]->thread == 0) 4824298ddadSSasha Levin return; 4834298ddadSSasha Levin 4844298ddadSSasha Levin mutex_unlock(&pause_lock); 4854298ddadSSasha Levin } 4864298ddadSSasha Levin 4874298ddadSSasha Levin void kvm__notify_paused(void) 4884298ddadSSasha Levin { 4894298ddadSSasha Levin u64 p = 1; 4904298ddadSSasha Levin 4914298ddadSSasha Levin if (write(pause_event, &p, sizeof(p)) < 0) 4924298ddadSSasha Levin die("Failed notifying of paused VCPU."); 4934298ddadSSasha Levin 4944298ddadSSasha Levin mutex_lock(&pause_lock); 4954298ddadSSasha Levin mutex_unlock(&pause_lock); 4964298ddadSSasha Levin } 497