nbd: Switch to byte-based block access
[qemu/ar7.git] / target-i386 / kvm.c
blob799fdfa682a2a6aba20d6760b36dbbeea2752464
1 /*
2 * QEMU KVM support
4 * Copyright (C) 2006-2008 Qumranet Technologies
5 * Copyright IBM, Corp. 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <sys/utsname.h>
21 #include <linux/kvm.h>
22 #include <linux/kvm_para.h>
24 #include "qemu-common.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/kvm_int.h"
27 #include "kvm_i386.h"
28 #include "cpu.h"
29 #include "hyperv.h"
31 #include "exec/gdbstub.h"
32 #include "qemu/host-utils.h"
33 #include "qemu/config-file.h"
34 #include "qemu/error-report.h"
35 #include "hw/i386/pc.h"
36 #include "hw/i386/apic.h"
37 #include "hw/i386/apic_internal.h"
38 #include "hw/i386/apic-msidef.h"
40 #include "exec/ioport.h"
41 #include "standard-headers/asm-x86/hyperv.h"
42 #include "hw/pci/pci.h"
43 #include "hw/pci/msi.h"
44 #include "migration/migration.h"
45 #include "exec/memattrs.h"
47 //#define DEBUG_KVM
49 #ifdef DEBUG_KVM
50 #define DPRINTF(fmt, ...) \
51 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
52 #else
53 #define DPRINTF(fmt, ...) \
54 do { } while (0)
55 #endif
57 #define MSR_KVM_WALL_CLOCK 0x11
58 #define MSR_KVM_SYSTEM_TIME 0x12
60 #ifndef BUS_MCEERR_AR
61 #define BUS_MCEERR_AR 4
62 #endif
63 #ifndef BUS_MCEERR_AO
64 #define BUS_MCEERR_AO 5
65 #endif
67 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
68 KVM_CAP_INFO(SET_TSS_ADDR),
69 KVM_CAP_INFO(EXT_CPUID),
70 KVM_CAP_INFO(MP_STATE),
71 KVM_CAP_LAST_INFO
74 static bool has_msr_star;
75 static bool has_msr_hsave_pa;
76 static bool has_msr_tsc_aux;
77 static bool has_msr_tsc_adjust;
78 static bool has_msr_tsc_deadline;
79 static bool has_msr_feature_control;
80 static bool has_msr_async_pf_en;
81 static bool has_msr_pv_eoi_en;
82 static bool has_msr_misc_enable;
83 static bool has_msr_smbase;
84 static bool has_msr_bndcfgs;
85 static bool has_msr_kvm_steal_time;
86 static int lm_capable_kernel;
87 static bool has_msr_hv_hypercall;
88 static bool has_msr_hv_vapic;
89 static bool has_msr_hv_tsc;
90 static bool has_msr_hv_crash;
91 static bool has_msr_hv_reset;
92 static bool has_msr_hv_vpindex;
93 static bool has_msr_hv_runtime;
94 static bool has_msr_hv_synic;
95 static bool has_msr_hv_stimer;
96 static bool has_msr_mtrr;
97 static bool has_msr_xss;
99 static bool has_msr_architectural_pmu;
100 static uint32_t num_architectural_pmu_counters;
102 static int has_xsave;
103 static int has_xcrs;
104 static int has_pit_state2;
106 int kvm_has_pit_state2(void)
108 return has_pit_state2;
111 bool kvm_has_smm(void)
113 return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
116 bool kvm_allows_irq0_override(void)
118 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
121 static int kvm_get_tsc(CPUState *cs)
123 X86CPU *cpu = X86_CPU(cs);
124 CPUX86State *env = &cpu->env;
125 struct {
126 struct kvm_msrs info;
127 struct kvm_msr_entry entries[1];
128 } msr_data;
129 int ret;
131 if (env->tsc_valid) {
132 return 0;
135 msr_data.info.nmsrs = 1;
136 msr_data.entries[0].index = MSR_IA32_TSC;
137 env->tsc_valid = !runstate_is_running();
139 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
140 if (ret < 0) {
141 return ret;
144 assert(ret == 1);
145 env->tsc = msr_data.entries[0].data;
146 return 0;
149 static inline void do_kvm_synchronize_tsc(void *arg)
151 CPUState *cpu = arg;
153 kvm_get_tsc(cpu);
156 void kvm_synchronize_all_tsc(void)
158 CPUState *cpu;
160 if (kvm_enabled()) {
161 CPU_FOREACH(cpu) {
162 run_on_cpu(cpu, do_kvm_synchronize_tsc, cpu);
167 static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
169 struct kvm_cpuid2 *cpuid;
170 int r, size;
172 size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
173 cpuid = g_malloc0(size);
174 cpuid->nent = max;
175 r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid);
176 if (r == 0 && cpuid->nent >= max) {
177 r = -E2BIG;
179 if (r < 0) {
180 if (r == -E2BIG) {
181 g_free(cpuid);
182 return NULL;
183 } else {
184 fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
185 strerror(-r));
186 exit(1);
189 return cpuid;
192 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
193 * for all entries.
195 static struct kvm_cpuid2 *get_supported_cpuid(KVMState *s)
197 struct kvm_cpuid2 *cpuid;
198 int max = 1;
199 while ((cpuid = try_get_cpuid(s, max)) == NULL) {
200 max *= 2;
202 return cpuid;
205 static const struct kvm_para_features {
206 int cap;
207 int feature;
208 } para_features[] = {
209 { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
210 { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
211 { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
212 { KVM_CAP_ASYNC_PF, KVM_FEATURE_ASYNC_PF },
215 static int get_para_features(KVMState *s)
217 int i, features = 0;
219 for (i = 0; i < ARRAY_SIZE(para_features); i++) {
220 if (kvm_check_extension(s, para_features[i].cap)) {
221 features |= (1 << para_features[i].feature);
225 return features;
229 /* Returns the value for a specific register on the cpuid entry
231 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg)
233 uint32_t ret = 0;
234 switch (reg) {
235 case R_EAX:
236 ret = entry->eax;
237 break;
238 case R_EBX:
239 ret = entry->ebx;
240 break;
241 case R_ECX:
242 ret = entry->ecx;
243 break;
244 case R_EDX:
245 ret = entry->edx;
246 break;
248 return ret;
251 /* Find matching entry for function/index on kvm_cpuid2 struct
253 static struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid,
254 uint32_t function,
255 uint32_t index)
257 int i;
258 for (i = 0; i < cpuid->nent; ++i) {
259 if (cpuid->entries[i].function == function &&
260 cpuid->entries[i].index == index) {
261 return &cpuid->entries[i];
264 /* not found: */
265 return NULL;
268 uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
269 uint32_t index, int reg)
271 struct kvm_cpuid2 *cpuid;
272 uint32_t ret = 0;
273 uint32_t cpuid_1_edx;
274 bool found = false;
276 cpuid = get_supported_cpuid(s);
278 struct kvm_cpuid_entry2 *entry = cpuid_find_entry(cpuid, function, index);
279 if (entry) {
280 found = true;
281 ret = cpuid_entry_get_reg(entry, reg);
284 /* Fixups for the data returned by KVM, below */
286 if (function == 1 && reg == R_EDX) {
287 /* KVM before 2.6.30 misreports the following features */
288 ret |= CPUID_MTRR | CPUID_PAT | CPUID_MCE | CPUID_MCA;
289 } else if (function == 1 && reg == R_ECX) {
290 /* We can set the hypervisor flag, even if KVM does not return it on
291 * GET_SUPPORTED_CPUID
293 ret |= CPUID_EXT_HYPERVISOR;
294 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
295 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
296 * and the irqchip is in the kernel.
298 if (kvm_irqchip_in_kernel() &&
299 kvm_check_extension(s, KVM_CAP_TSC_DEADLINE_TIMER)) {
300 ret |= CPUID_EXT_TSC_DEADLINE_TIMER;
303 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
304 * without the in-kernel irqchip
306 if (!kvm_irqchip_in_kernel()) {
307 ret &= ~CPUID_EXT_X2APIC;
309 } else if (function == 6 && reg == R_EAX) {
310 ret |= CPUID_6_EAX_ARAT; /* safe to allow because of emulated APIC */
311 } else if (function == 0x80000001 && reg == R_EDX) {
312 /* On Intel, kvm returns cpuid according to the Intel spec,
313 * so add missing bits according to the AMD spec:
315 cpuid_1_edx = kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
316 ret |= cpuid_1_edx & CPUID_EXT2_AMD_ALIASES;
319 g_free(cpuid);
321 /* fallback for older kernels */
322 if ((function == KVM_CPUID_FEATURES) && !found) {
323 ret = get_para_features(s);
326 return ret;
329 typedef struct HWPoisonPage {
330 ram_addr_t ram_addr;
331 QLIST_ENTRY(HWPoisonPage) list;
332 } HWPoisonPage;
334 static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
335 QLIST_HEAD_INITIALIZER(hwpoison_page_list);
337 static void kvm_unpoison_all(void *param)
339 HWPoisonPage *page, *next_page;
341 QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
342 QLIST_REMOVE(page, list);
343 qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
344 g_free(page);
348 static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
350 HWPoisonPage *page;
352 QLIST_FOREACH(page, &hwpoison_page_list, list) {
353 if (page->ram_addr == ram_addr) {
354 return;
357 page = g_new(HWPoisonPage, 1);
358 page->ram_addr = ram_addr;
359 QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
362 static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap,
363 int *max_banks)
365 int r;
367 r = kvm_check_extension(s, KVM_CAP_MCE);
368 if (r > 0) {
369 *max_banks = r;
370 return kvm_ioctl(s, KVM_X86_GET_MCE_CAP_SUPPORTED, mce_cap);
372 return -ENOSYS;
375 static void kvm_mce_inject(X86CPU *cpu, hwaddr paddr, int code)
377 CPUX86State *env = &cpu->env;
378 uint64_t status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN |
379 MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S;
380 uint64_t mcg_status = MCG_STATUS_MCIP;
382 if (code == BUS_MCEERR_AR) {
383 status |= MCI_STATUS_AR | 0x134;
384 mcg_status |= MCG_STATUS_EIPV;
385 } else {
386 status |= 0xc0;
387 mcg_status |= MCG_STATUS_RIPV;
389 cpu_x86_inject_mce(NULL, cpu, 9, status, mcg_status, paddr,
390 (MCM_ADDR_PHYS << 6) | 0xc,
391 cpu_x86_support_mca_broadcast(env) ?
392 MCE_INJECT_BROADCAST : 0);
395 static void hardware_memory_error(void)
397 fprintf(stderr, "Hardware memory error!\n");
398 exit(1);
401 int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
403 X86CPU *cpu = X86_CPU(c);
404 CPUX86State *env = &cpu->env;
405 ram_addr_t ram_addr;
406 hwaddr paddr;
408 if ((env->mcg_cap & MCG_SER_P) && addr
409 && (code == BUS_MCEERR_AR || code == BUS_MCEERR_AO)) {
410 if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL ||
411 !kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
412 fprintf(stderr, "Hardware memory error for memory used by "
413 "QEMU itself instead of guest system!\n");
414 /* Hope we are lucky for AO MCE */
415 if (code == BUS_MCEERR_AO) {
416 return 0;
417 } else {
418 hardware_memory_error();
421 kvm_hwpoison_page_add(ram_addr);
422 kvm_mce_inject(cpu, paddr, code);
423 } else {
424 if (code == BUS_MCEERR_AO) {
425 return 0;
426 } else if (code == BUS_MCEERR_AR) {
427 hardware_memory_error();
428 } else {
429 return 1;
432 return 0;
435 int kvm_arch_on_sigbus(int code, void *addr)
437 X86CPU *cpu = X86_CPU(first_cpu);
439 if ((cpu->env.mcg_cap & MCG_SER_P) && addr && code == BUS_MCEERR_AO) {
440 ram_addr_t ram_addr;
441 hwaddr paddr;
443 /* Hope we are lucky for AO MCE */
444 if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL ||
445 !kvm_physical_memory_addr_from_host(first_cpu->kvm_state,
446 addr, &paddr)) {
447 fprintf(stderr, "Hardware memory error for memory used by "
448 "QEMU itself instead of guest system!: %p\n", addr);
449 return 0;
451 kvm_hwpoison_page_add(ram_addr);
452 kvm_mce_inject(X86_CPU(first_cpu), paddr, code);
453 } else {
454 if (code == BUS_MCEERR_AO) {
455 return 0;
456 } else if (code == BUS_MCEERR_AR) {
457 hardware_memory_error();
458 } else {
459 return 1;
462 return 0;
465 static int kvm_inject_mce_oldstyle(X86CPU *cpu)
467 CPUX86State *env = &cpu->env;
469 if (!kvm_has_vcpu_events() && env->exception_injected == EXCP12_MCHK) {
470 unsigned int bank, bank_num = env->mcg_cap & 0xff;
471 struct kvm_x86_mce mce;
473 env->exception_injected = -1;
476 * There must be at least one bank in use if an MCE is pending.
477 * Find it and use its values for the event injection.
479 for (bank = 0; bank < bank_num; bank++) {
480 if (env->mce_banks[bank * 4 + 1] & MCI_STATUS_VAL) {
481 break;
484 assert(bank < bank_num);
486 mce.bank = bank;
487 mce.status = env->mce_banks[bank * 4 + 1];
488 mce.mcg_status = env->mcg_status;
489 mce.addr = env->mce_banks[bank * 4 + 2];
490 mce.misc = env->mce_banks[bank * 4 + 3];
492 return kvm_vcpu_ioctl(CPU(cpu), KVM_X86_SET_MCE, &mce);
494 return 0;
497 static void cpu_update_state(void *opaque, int running, RunState state)
499 CPUX86State *env = opaque;
501 if (running) {
502 env->tsc_valid = false;
506 unsigned long kvm_arch_vcpu_id(CPUState *cs)
508 X86CPU *cpu = X86_CPU(cs);
509 return cpu->apic_id;
512 #ifndef KVM_CPUID_SIGNATURE_NEXT
513 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
514 #endif
516 static bool hyperv_hypercall_available(X86CPU *cpu)
518 return cpu->hyperv_vapic ||
519 (cpu->hyperv_spinlock_attempts != HYPERV_SPINLOCK_NEVER_RETRY);
522 static bool hyperv_enabled(X86CPU *cpu)
524 CPUState *cs = CPU(cpu);
525 return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 &&
526 (hyperv_hypercall_available(cpu) ||
527 cpu->hyperv_time ||
528 cpu->hyperv_relaxed_timing ||
529 cpu->hyperv_crash ||
530 cpu->hyperv_reset ||
531 cpu->hyperv_vpindex ||
532 cpu->hyperv_runtime ||
533 cpu->hyperv_synic ||
534 cpu->hyperv_stimer);
537 static int kvm_arch_set_tsc_khz(CPUState *cs)
539 X86CPU *cpu = X86_CPU(cs);
540 CPUX86State *env = &cpu->env;
541 int r;
543 if (!env->tsc_khz) {
544 return 0;
547 r = kvm_check_extension(cs->kvm_state, KVM_CAP_TSC_CONTROL) ?
548 kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz) :
549 -ENOTSUP;
550 if (r < 0) {
551 /* When KVM_SET_TSC_KHZ fails, it's an error only if the current
552 * TSC frequency doesn't match the one we want.
554 int cur_freq = kvm_check_extension(cs->kvm_state, KVM_CAP_GET_TSC_KHZ) ?
555 kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ) :
556 -ENOTSUP;
557 if (cur_freq <= 0 || cur_freq != env->tsc_khz) {
558 error_report("warning: TSC frequency mismatch between "
559 "VM and host, and TSC scaling unavailable");
560 return r;
564 return 0;
567 static Error *invtsc_mig_blocker;
569 #define KVM_MAX_CPUID_ENTRIES 100
571 int kvm_arch_init_vcpu(CPUState *cs)
573 struct {
574 struct kvm_cpuid2 cpuid;
575 struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES];
576 } QEMU_PACKED cpuid_data;
577 X86CPU *cpu = X86_CPU(cs);
578 CPUX86State *env = &cpu->env;
579 uint32_t limit, i, j, cpuid_i;
580 uint32_t unused;
581 struct kvm_cpuid_entry2 *c;
582 uint32_t signature[3];
583 int kvm_base = KVM_CPUID_SIGNATURE;
584 int r;
586 memset(&cpuid_data, 0, sizeof(cpuid_data));
588 cpuid_i = 0;
590 /* Paravirtualization CPUIDs */
591 if (hyperv_enabled(cpu)) {
592 c = &cpuid_data.entries[cpuid_i++];
593 c->function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
594 if (!cpu->hyperv_vendor_id) {
595 memcpy(signature, "Microsoft Hv", 12);
596 } else {
597 size_t len = strlen(cpu->hyperv_vendor_id);
599 if (len > 12) {
600 error_report("hv-vendor-id truncated to 12 characters");
601 len = 12;
603 memset(signature, 0, 12);
604 memcpy(signature, cpu->hyperv_vendor_id, len);
606 c->eax = HYPERV_CPUID_MIN;
607 c->ebx = signature[0];
608 c->ecx = signature[1];
609 c->edx = signature[2];
611 c = &cpuid_data.entries[cpuid_i++];
612 c->function = HYPERV_CPUID_INTERFACE;
613 memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
614 c->eax = signature[0];
615 c->ebx = 0;
616 c->ecx = 0;
617 c->edx = 0;
619 c = &cpuid_data.entries[cpuid_i++];
620 c->function = HYPERV_CPUID_VERSION;
621 c->eax = 0x00001bbc;
622 c->ebx = 0x00060001;
624 c = &cpuid_data.entries[cpuid_i++];
625 c->function = HYPERV_CPUID_FEATURES;
626 if (cpu->hyperv_relaxed_timing) {
627 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
629 if (cpu->hyperv_vapic) {
630 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
631 c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
632 has_msr_hv_vapic = true;
634 if (cpu->hyperv_time &&
635 kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
636 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
637 c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
638 c->eax |= 0x200;
639 has_msr_hv_tsc = true;
641 if (cpu->hyperv_crash && has_msr_hv_crash) {
642 c->edx |= HV_X64_GUEST_CRASH_MSR_AVAILABLE;
644 c->edx |= HV_X64_CPU_DYNAMIC_PARTITIONING_AVAILABLE;
645 if (cpu->hyperv_reset && has_msr_hv_reset) {
646 c->eax |= HV_X64_MSR_RESET_AVAILABLE;
648 if (cpu->hyperv_vpindex && has_msr_hv_vpindex) {
649 c->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE;
651 if (cpu->hyperv_runtime && has_msr_hv_runtime) {
652 c->eax |= HV_X64_MSR_VP_RUNTIME_AVAILABLE;
654 if (cpu->hyperv_synic) {
655 int sint;
657 if (!has_msr_hv_synic ||
658 kvm_vcpu_enable_cap(cs, KVM_CAP_HYPERV_SYNIC, 0)) {
659 fprintf(stderr, "Hyper-V SynIC is not supported by kernel\n");
660 return -ENOSYS;
663 c->eax |= HV_X64_MSR_SYNIC_AVAILABLE;
664 env->msr_hv_synic_version = HV_SYNIC_VERSION_1;
665 for (sint = 0; sint < ARRAY_SIZE(env->msr_hv_synic_sint); sint++) {
666 env->msr_hv_synic_sint[sint] = HV_SYNIC_SINT_MASKED;
669 if (cpu->hyperv_stimer) {
670 if (!has_msr_hv_stimer) {
671 fprintf(stderr, "Hyper-V timers aren't supported by kernel\n");
672 return -ENOSYS;
674 c->eax |= HV_X64_MSR_SYNTIMER_AVAILABLE;
676 c = &cpuid_data.entries[cpuid_i++];
677 c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
678 if (cpu->hyperv_relaxed_timing) {
679 c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
681 if (has_msr_hv_vapic) {
682 c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
684 c->ebx = cpu->hyperv_spinlock_attempts;
686 c = &cpuid_data.entries[cpuid_i++];
687 c->function = HYPERV_CPUID_IMPLEMENT_LIMITS;
688 c->eax = 0x40;
689 c->ebx = 0x40;
691 kvm_base = KVM_CPUID_SIGNATURE_NEXT;
692 has_msr_hv_hypercall = true;
695 if (cpu->expose_kvm) {
696 memcpy(signature, "KVMKVMKVM\0\0\0", 12);
697 c = &cpuid_data.entries[cpuid_i++];
698 c->function = KVM_CPUID_SIGNATURE | kvm_base;
699 c->eax = KVM_CPUID_FEATURES | kvm_base;
700 c->ebx = signature[0];
701 c->ecx = signature[1];
702 c->edx = signature[2];
704 c = &cpuid_data.entries[cpuid_i++];
705 c->function = KVM_CPUID_FEATURES | kvm_base;
706 c->eax = env->features[FEAT_KVM];
708 has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF);
710 has_msr_pv_eoi_en = c->eax & (1 << KVM_FEATURE_PV_EOI);
712 has_msr_kvm_steal_time = c->eax & (1 << KVM_FEATURE_STEAL_TIME);
715 cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
717 for (i = 0; i <= limit; i++) {
718 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
719 fprintf(stderr, "unsupported level value: 0x%x\n", limit);
720 abort();
722 c = &cpuid_data.entries[cpuid_i++];
724 switch (i) {
725 case 2: {
726 /* Keep reading function 2 till all the input is received */
727 int times;
729 c->function = i;
730 c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC |
731 KVM_CPUID_FLAG_STATE_READ_NEXT;
732 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
733 times = c->eax & 0xff;
735 for (j = 1; j < times; ++j) {
736 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
737 fprintf(stderr, "cpuid_data is full, no space for "
738 "cpuid(eax:2):eax & 0xf = 0x%x\n", times);
739 abort();
741 c = &cpuid_data.entries[cpuid_i++];
742 c->function = i;
743 c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC;
744 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
746 break;
748 case 4:
749 case 0xb:
750 case 0xd:
751 for (j = 0; ; j++) {
752 if (i == 0xd && j == 64) {
753 break;
755 c->function = i;
756 c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
757 c->index = j;
758 cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx);
760 if (i == 4 && c->eax == 0) {
761 break;
763 if (i == 0xb && !(c->ecx & 0xff00)) {
764 break;
766 if (i == 0xd && c->eax == 0) {
767 continue;
769 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
770 fprintf(stderr, "cpuid_data is full, no space for "
771 "cpuid(eax:0x%x,ecx:0x%x)\n", i, j);
772 abort();
774 c = &cpuid_data.entries[cpuid_i++];
776 break;
777 default:
778 c->function = i;
779 c->flags = 0;
780 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
781 break;
785 if (limit >= 0x0a) {
786 uint32_t ver;
788 cpu_x86_cpuid(env, 0x0a, 0, &ver, &unused, &unused, &unused);
789 if ((ver & 0xff) > 0) {
790 has_msr_architectural_pmu = true;
791 num_architectural_pmu_counters = (ver & 0xff00) >> 8;
793 /* Shouldn't be more than 32, since that's the number of bits
794 * available in EBX to tell us _which_ counters are available.
795 * Play it safe.
797 if (num_architectural_pmu_counters > MAX_GP_COUNTERS) {
798 num_architectural_pmu_counters = MAX_GP_COUNTERS;
803 cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused);
805 for (i = 0x80000000; i <= limit; i++) {
806 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
807 fprintf(stderr, "unsupported xlevel value: 0x%x\n", limit);
808 abort();
810 c = &cpuid_data.entries[cpuid_i++];
812 c->function = i;
813 c->flags = 0;
814 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
817 /* Call Centaur's CPUID instructions they are supported. */
818 if (env->cpuid_xlevel2 > 0) {
819 cpu_x86_cpuid(env, 0xC0000000, 0, &limit, &unused, &unused, &unused);
821 for (i = 0xC0000000; i <= limit; i++) {
822 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
823 fprintf(stderr, "unsupported xlevel2 value: 0x%x\n", limit);
824 abort();
826 c = &cpuid_data.entries[cpuid_i++];
828 c->function = i;
829 c->flags = 0;
830 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
834 cpuid_data.cpuid.nent = cpuid_i;
836 if (((env->cpuid_version >> 8)&0xF) >= 6
837 && (env->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
838 (CPUID_MCE | CPUID_MCA)
839 && kvm_check_extension(cs->kvm_state, KVM_CAP_MCE) > 0) {
840 uint64_t mcg_cap, unsupported_caps;
841 int banks;
842 int ret;
844 ret = kvm_get_mce_cap_supported(cs->kvm_state, &mcg_cap, &banks);
845 if (ret < 0) {
846 fprintf(stderr, "kvm_get_mce_cap_supported: %s", strerror(-ret));
847 return ret;
850 if (banks < (env->mcg_cap & MCG_CAP_BANKS_MASK)) {
851 error_report("kvm: Unsupported MCE bank count (QEMU = %d, KVM = %d)",
852 (int)(env->mcg_cap & MCG_CAP_BANKS_MASK), banks);
853 return -ENOTSUP;
856 unsupported_caps = env->mcg_cap & ~(mcg_cap | MCG_CAP_BANKS_MASK);
857 if (unsupported_caps) {
858 error_report("warning: Unsupported MCG_CAP bits: 0x%" PRIx64,
859 unsupported_caps);
862 env->mcg_cap &= mcg_cap | MCG_CAP_BANKS_MASK;
863 ret = kvm_vcpu_ioctl(cs, KVM_X86_SETUP_MCE, &env->mcg_cap);
864 if (ret < 0) {
865 fprintf(stderr, "KVM_X86_SETUP_MCE: %s", strerror(-ret));
866 return ret;
870 qemu_add_vm_change_state_handler(cpu_update_state, env);
872 c = cpuid_find_entry(&cpuid_data.cpuid, 1, 0);
873 if (c) {
874 has_msr_feature_control = !!(c->ecx & CPUID_EXT_VMX) ||
875 !!(c->ecx & CPUID_EXT_SMX);
878 c = cpuid_find_entry(&cpuid_data.cpuid, 0x80000007, 0);
879 if (c && (c->edx & 1<<8) && invtsc_mig_blocker == NULL) {
880 /* for migration */
881 error_setg(&invtsc_mig_blocker,
882 "State blocked by non-migratable CPU device"
883 " (invtsc flag)");
884 migrate_add_blocker(invtsc_mig_blocker);
885 /* for savevm */
886 vmstate_x86_cpu.unmigratable = 1;
889 cpuid_data.cpuid.padding = 0;
890 r = kvm_vcpu_ioctl(cs, KVM_SET_CPUID2, &cpuid_data);
891 if (r) {
892 return r;
895 r = kvm_arch_set_tsc_khz(cs);
896 if (r < 0) {
897 return r;
900 /* vcpu's TSC frequency is either specified by user, or following
901 * the value used by KVM if the former is not present. In the
902 * latter case, we query it from KVM and record in env->tsc_khz,
903 * so that vcpu's TSC frequency can be migrated later via this field.
905 if (!env->tsc_khz) {
906 r = kvm_check_extension(cs->kvm_state, KVM_CAP_GET_TSC_KHZ) ?
907 kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ) :
908 -ENOTSUP;
909 if (r > 0) {
910 env->tsc_khz = r;
914 if (has_xsave) {
915 env->kvm_xsave_buf = qemu_memalign(4096, sizeof(struct kvm_xsave));
918 if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
919 has_msr_mtrr = true;
921 if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_RDTSCP)) {
922 has_msr_tsc_aux = false;
925 return 0;
928 void kvm_arch_reset_vcpu(X86CPU *cpu)
930 CPUX86State *env = &cpu->env;
932 env->exception_injected = -1;
933 env->interrupt_injected = -1;
934 env->xcr0 = 1;
935 if (kvm_irqchip_in_kernel()) {
936 env->mp_state = cpu_is_bsp(cpu) ? KVM_MP_STATE_RUNNABLE :
937 KVM_MP_STATE_UNINITIALIZED;
938 } else {
939 env->mp_state = KVM_MP_STATE_RUNNABLE;
943 void kvm_arch_do_init_vcpu(X86CPU *cpu)
945 CPUX86State *env = &cpu->env;
947 /* APs get directly into wait-for-SIPI state. */
948 if (env->mp_state == KVM_MP_STATE_UNINITIALIZED) {
949 env->mp_state = KVM_MP_STATE_INIT_RECEIVED;
953 static int kvm_get_supported_msrs(KVMState *s)
955 static int kvm_supported_msrs;
956 int ret = 0;
958 /* first time */
959 if (kvm_supported_msrs == 0) {
960 struct kvm_msr_list msr_list, *kvm_msr_list;
962 kvm_supported_msrs = -1;
964 /* Obtain MSR list from KVM. These are the MSRs that we must
965 * save/restore */
966 msr_list.nmsrs = 0;
967 ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, &msr_list);
968 if (ret < 0 && ret != -E2BIG) {
969 return ret;
971 /* Old kernel modules had a bug and could write beyond the provided
972 memory. Allocate at least a safe amount of 1K. */
973 kvm_msr_list = g_malloc0(MAX(1024, sizeof(msr_list) +
974 msr_list.nmsrs *
975 sizeof(msr_list.indices[0])));
977 kvm_msr_list->nmsrs = msr_list.nmsrs;
978 ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, kvm_msr_list);
979 if (ret >= 0) {
980 int i;
982 for (i = 0; i < kvm_msr_list->nmsrs; i++) {
983 if (kvm_msr_list->indices[i] == MSR_STAR) {
984 has_msr_star = true;
985 continue;
987 if (kvm_msr_list->indices[i] == MSR_VM_HSAVE_PA) {
988 has_msr_hsave_pa = true;
989 continue;
991 if (kvm_msr_list->indices[i] == MSR_TSC_AUX) {
992 has_msr_tsc_aux = true;
993 continue;
995 if (kvm_msr_list->indices[i] == MSR_TSC_ADJUST) {
996 has_msr_tsc_adjust = true;
997 continue;
999 if (kvm_msr_list->indices[i] == MSR_IA32_TSCDEADLINE) {
1000 has_msr_tsc_deadline = true;
1001 continue;
1003 if (kvm_msr_list->indices[i] == MSR_IA32_SMBASE) {
1004 has_msr_smbase = true;
1005 continue;
1007 if (kvm_msr_list->indices[i] == MSR_IA32_MISC_ENABLE) {
1008 has_msr_misc_enable = true;
1009 continue;
1011 if (kvm_msr_list->indices[i] == MSR_IA32_BNDCFGS) {
1012 has_msr_bndcfgs = true;
1013 continue;
1015 if (kvm_msr_list->indices[i] == MSR_IA32_XSS) {
1016 has_msr_xss = true;
1017 continue;
1019 if (kvm_msr_list->indices[i] == HV_X64_MSR_CRASH_CTL) {
1020 has_msr_hv_crash = true;
1021 continue;
1023 if (kvm_msr_list->indices[i] == HV_X64_MSR_RESET) {
1024 has_msr_hv_reset = true;
1025 continue;
1027 if (kvm_msr_list->indices[i] == HV_X64_MSR_VP_INDEX) {
1028 has_msr_hv_vpindex = true;
1029 continue;
1031 if (kvm_msr_list->indices[i] == HV_X64_MSR_VP_RUNTIME) {
1032 has_msr_hv_runtime = true;
1033 continue;
1035 if (kvm_msr_list->indices[i] == HV_X64_MSR_SCONTROL) {
1036 has_msr_hv_synic = true;
1037 continue;
1039 if (kvm_msr_list->indices[i] == HV_X64_MSR_STIMER0_CONFIG) {
1040 has_msr_hv_stimer = true;
1041 continue;
1046 g_free(kvm_msr_list);
1049 return ret;
1052 static Notifier smram_machine_done;
1053 static KVMMemoryListener smram_listener;
1054 static AddressSpace smram_address_space;
1055 static MemoryRegion smram_as_root;
1056 static MemoryRegion smram_as_mem;
1058 static void register_smram_listener(Notifier *n, void *unused)
1060 MemoryRegion *smram =
1061 (MemoryRegion *) object_resolve_path("/machine/smram", NULL);
1063 /* Outer container... */
1064 memory_region_init(&smram_as_root, OBJECT(kvm_state), "mem-container-smram", ~0ull);
1065 memory_region_set_enabled(&smram_as_root, true);
1067 /* ... with two regions inside: normal system memory with low
1068 * priority, and...
1070 memory_region_init_alias(&smram_as_mem, OBJECT(kvm_state), "mem-smram",
1071 get_system_memory(), 0, ~0ull);
1072 memory_region_add_subregion_overlap(&smram_as_root, 0, &smram_as_mem, 0);
1073 memory_region_set_enabled(&smram_as_mem, true);
1075 if (smram) {
1076 /* ... SMRAM with higher priority */
1077 memory_region_add_subregion_overlap(&smram_as_root, 0, smram, 10);
1078 memory_region_set_enabled(smram, true);
1081 address_space_init(&smram_address_space, &smram_as_root, "KVM-SMRAM");
1082 kvm_memory_listener_register(kvm_state, &smram_listener,
1083 &smram_address_space, 1);
1086 int kvm_arch_init(MachineState *ms, KVMState *s)
1088 uint64_t identity_base = 0xfffbc000;
1089 uint64_t shadow_mem;
1090 int ret;
1091 struct utsname utsname;
1093 #ifdef KVM_CAP_XSAVE
1094 has_xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1095 #endif
1097 #ifdef KVM_CAP_XCRS
1098 has_xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1099 #endif
1101 #ifdef KVM_CAP_PIT_STATE2
1102 has_pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1103 #endif
1105 ret = kvm_get_supported_msrs(s);
1106 if (ret < 0) {
1107 return ret;
1110 uname(&utsname);
1111 lm_capable_kernel = strcmp(utsname.machine, "x86_64") == 0;
1114 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
1115 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
1116 * Since these must be part of guest physical memory, we need to allocate
1117 * them, both by setting their start addresses in the kernel and by
1118 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
1120 * Older KVM versions may not support setting the identity map base. In
1121 * that case we need to stick with the default, i.e. a 256K maximum BIOS
1122 * size.
1124 if (kvm_check_extension(s, KVM_CAP_SET_IDENTITY_MAP_ADDR)) {
1125 /* Allows up to 16M BIOSes. */
1126 identity_base = 0xfeffc000;
1128 ret = kvm_vm_ioctl(s, KVM_SET_IDENTITY_MAP_ADDR, &identity_base);
1129 if (ret < 0) {
1130 return ret;
1134 /* Set TSS base one page after EPT identity map. */
1135 ret = kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, identity_base + 0x1000);
1136 if (ret < 0) {
1137 return ret;
1140 /* Tell fw_cfg to notify the BIOS to reserve the range. */
1141 ret = e820_add_entry(identity_base, 0x4000, E820_RESERVED);
1142 if (ret < 0) {
1143 fprintf(stderr, "e820_add_entry() table is full\n");
1144 return ret;
1146 qemu_register_reset(kvm_unpoison_all, NULL);
1148 shadow_mem = machine_kvm_shadow_mem(ms);
1149 if (shadow_mem != -1) {
1150 shadow_mem /= 4096;
1151 ret = kvm_vm_ioctl(s, KVM_SET_NR_MMU_PAGES, shadow_mem);
1152 if (ret < 0) {
1153 return ret;
1157 if (kvm_check_extension(s, KVM_CAP_X86_SMM)) {
1158 smram_machine_done.notify = register_smram_listener;
1159 qemu_add_machine_init_done_notifier(&smram_machine_done);
1161 return 0;
1164 static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
1166 lhs->selector = rhs->selector;
1167 lhs->base = rhs->base;
1168 lhs->limit = rhs->limit;
1169 lhs->type = 3;
1170 lhs->present = 1;
1171 lhs->dpl = 3;
1172 lhs->db = 0;
1173 lhs->s = 1;
1174 lhs->l = 0;
1175 lhs->g = 0;
1176 lhs->avl = 0;
1177 lhs->unusable = 0;
1180 static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
1182 unsigned flags = rhs->flags;
1183 lhs->selector = rhs->selector;
1184 lhs->base = rhs->base;
1185 lhs->limit = rhs->limit;
1186 lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
1187 lhs->present = (flags & DESC_P_MASK) != 0;
1188 lhs->dpl = (flags >> DESC_DPL_SHIFT) & 3;
1189 lhs->db = (flags >> DESC_B_SHIFT) & 1;
1190 lhs->s = (flags & DESC_S_MASK) != 0;
1191 lhs->l = (flags >> DESC_L_SHIFT) & 1;
1192 lhs->g = (flags & DESC_G_MASK) != 0;
1193 lhs->avl = (flags & DESC_AVL_MASK) != 0;
1194 lhs->unusable = !lhs->present;
1195 lhs->padding = 0;
1198 static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
1200 lhs->selector = rhs->selector;
1201 lhs->base = rhs->base;
1202 lhs->limit = rhs->limit;
1203 if (rhs->unusable) {
1204 lhs->flags = 0;
1205 } else {
1206 lhs->flags = (rhs->type << DESC_TYPE_SHIFT) |
1207 (rhs->present * DESC_P_MASK) |
1208 (rhs->dpl << DESC_DPL_SHIFT) |
1209 (rhs->db << DESC_B_SHIFT) |
1210 (rhs->s * DESC_S_MASK) |
1211 (rhs->l << DESC_L_SHIFT) |
1212 (rhs->g * DESC_G_MASK) |
1213 (rhs->avl * DESC_AVL_MASK);
1217 static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
1219 if (set) {
1220 *kvm_reg = *qemu_reg;
1221 } else {
1222 *qemu_reg = *kvm_reg;
1226 static int kvm_getput_regs(X86CPU *cpu, int set)
1228 CPUX86State *env = &cpu->env;
1229 struct kvm_regs regs;
1230 int ret = 0;
1232 if (!set) {
1233 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_REGS, &regs);
1234 if (ret < 0) {
1235 return ret;
1239 kvm_getput_reg(&regs.rax, &env->regs[R_EAX], set);
1240 kvm_getput_reg(&regs.rbx, &env->regs[R_EBX], set);
1241 kvm_getput_reg(&regs.rcx, &env->regs[R_ECX], set);
1242 kvm_getput_reg(&regs.rdx, &env->regs[R_EDX], set);
1243 kvm_getput_reg(&regs.rsi, &env->regs[R_ESI], set);
1244 kvm_getput_reg(&regs.rdi, &env->regs[R_EDI], set);
1245 kvm_getput_reg(&regs.rsp, &env->regs[R_ESP], set);
1246 kvm_getput_reg(&regs.rbp, &env->regs[R_EBP], set);
1247 #ifdef TARGET_X86_64
1248 kvm_getput_reg(&regs.r8, &env->regs[8], set);
1249 kvm_getput_reg(&regs.r9, &env->regs[9], set);
1250 kvm_getput_reg(&regs.r10, &env->regs[10], set);
1251 kvm_getput_reg(&regs.r11, &env->regs[11], set);
1252 kvm_getput_reg(&regs.r12, &env->regs[12], set);
1253 kvm_getput_reg(&regs.r13, &env->regs[13], set);
1254 kvm_getput_reg(&regs.r14, &env->regs[14], set);
1255 kvm_getput_reg(&regs.r15, &env->regs[15], set);
1256 #endif
1258 kvm_getput_reg(&regs.rflags, &env->eflags, set);
1259 kvm_getput_reg(&regs.rip, &env->eip, set);
1261 if (set) {
1262 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_REGS, &regs);
1265 return ret;
1268 static int kvm_put_fpu(X86CPU *cpu)
1270 CPUX86State *env = &cpu->env;
1271 struct kvm_fpu fpu;
1272 int i;
1274 memset(&fpu, 0, sizeof fpu);
1275 fpu.fsw = env->fpus & ~(7 << 11);
1276 fpu.fsw |= (env->fpstt & 7) << 11;
1277 fpu.fcw = env->fpuc;
1278 fpu.last_opcode = env->fpop;
1279 fpu.last_ip = env->fpip;
1280 fpu.last_dp = env->fpdp;
1281 for (i = 0; i < 8; ++i) {
1282 fpu.ftwx |= (!env->fptags[i]) << i;
1284 memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
1285 for (i = 0; i < CPU_NB_REGS; i++) {
1286 stq_p(&fpu.xmm[i][0], env->xmm_regs[i].ZMM_Q(0));
1287 stq_p(&fpu.xmm[i][8], env->xmm_regs[i].ZMM_Q(1));
1289 fpu.mxcsr = env->mxcsr;
1291 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_FPU, &fpu);
1294 #define XSAVE_FCW_FSW 0
1295 #define XSAVE_FTW_FOP 1
1296 #define XSAVE_CWD_RIP 2
1297 #define XSAVE_CWD_RDP 4
1298 #define XSAVE_MXCSR 6
1299 #define XSAVE_ST_SPACE 8
1300 #define XSAVE_XMM_SPACE 40
1301 #define XSAVE_XSTATE_BV 128
1302 #define XSAVE_YMMH_SPACE 144
1303 #define XSAVE_BNDREGS 240
1304 #define XSAVE_BNDCSR 256
1305 #define XSAVE_OPMASK 272
1306 #define XSAVE_ZMM_Hi256 288
1307 #define XSAVE_Hi16_ZMM 416
1308 #define XSAVE_PKRU 672
1310 static int kvm_put_xsave(X86CPU *cpu)
1312 CPUX86State *env = &cpu->env;
1313 struct kvm_xsave* xsave = env->kvm_xsave_buf;
1314 uint16_t cwd, swd, twd;
1315 uint8_t *xmm, *ymmh, *zmmh;
1316 int i, r;
1318 if (!has_xsave) {
1319 return kvm_put_fpu(cpu);
1322 memset(xsave, 0, sizeof(struct kvm_xsave));
1323 twd = 0;
1324 swd = env->fpus & ~(7 << 11);
1325 swd |= (env->fpstt & 7) << 11;
1326 cwd = env->fpuc;
1327 for (i = 0; i < 8; ++i) {
1328 twd |= (!env->fptags[i]) << i;
1330 xsave->region[XSAVE_FCW_FSW] = (uint32_t)(swd << 16) + cwd;
1331 xsave->region[XSAVE_FTW_FOP] = (uint32_t)(env->fpop << 16) + twd;
1332 memcpy(&xsave->region[XSAVE_CWD_RIP], &env->fpip, sizeof(env->fpip));
1333 memcpy(&xsave->region[XSAVE_CWD_RDP], &env->fpdp, sizeof(env->fpdp));
1334 memcpy(&xsave->region[XSAVE_ST_SPACE], env->fpregs,
1335 sizeof env->fpregs);
1336 xsave->region[XSAVE_MXCSR] = env->mxcsr;
1337 *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv;
1338 memcpy(&xsave->region[XSAVE_BNDREGS], env->bnd_regs,
1339 sizeof env->bnd_regs);
1340 memcpy(&xsave->region[XSAVE_BNDCSR], &env->bndcs_regs,
1341 sizeof(env->bndcs_regs));
1342 memcpy(&xsave->region[XSAVE_OPMASK], env->opmask_regs,
1343 sizeof env->opmask_regs);
1345 xmm = (uint8_t *)&xsave->region[XSAVE_XMM_SPACE];
1346 ymmh = (uint8_t *)&xsave->region[XSAVE_YMMH_SPACE];
1347 zmmh = (uint8_t *)&xsave->region[XSAVE_ZMM_Hi256];
1348 for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) {
1349 stq_p(xmm, env->xmm_regs[i].ZMM_Q(0));
1350 stq_p(xmm+8, env->xmm_regs[i].ZMM_Q(1));
1351 stq_p(ymmh, env->xmm_regs[i].ZMM_Q(2));
1352 stq_p(ymmh+8, env->xmm_regs[i].ZMM_Q(3));
1353 stq_p(zmmh, env->xmm_regs[i].ZMM_Q(4));
1354 stq_p(zmmh+8, env->xmm_regs[i].ZMM_Q(5));
1355 stq_p(zmmh+16, env->xmm_regs[i].ZMM_Q(6));
1356 stq_p(zmmh+24, env->xmm_regs[i].ZMM_Q(7));
1359 #ifdef TARGET_X86_64
1360 memcpy(&xsave->region[XSAVE_Hi16_ZMM], &env->xmm_regs[16],
1361 16 * sizeof env->xmm_regs[16]);
1362 memcpy(&xsave->region[XSAVE_PKRU], &env->pkru, sizeof env->pkru);
1363 #endif
1364 r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave);
1365 return r;
1368 static int kvm_put_xcrs(X86CPU *cpu)
1370 CPUX86State *env = &cpu->env;
1371 struct kvm_xcrs xcrs = {};
1373 if (!has_xcrs) {
1374 return 0;
1377 xcrs.nr_xcrs = 1;
1378 xcrs.flags = 0;
1379 xcrs.xcrs[0].xcr = 0;
1380 xcrs.xcrs[0].value = env->xcr0;
1381 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XCRS, &xcrs);
1384 static int kvm_put_sregs(X86CPU *cpu)
1386 CPUX86State *env = &cpu->env;
1387 struct kvm_sregs sregs;
1389 memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));
1390 if (env->interrupt_injected >= 0) {
1391 sregs.interrupt_bitmap[env->interrupt_injected / 64] |=
1392 (uint64_t)1 << (env->interrupt_injected % 64);
1395 if ((env->eflags & VM_MASK)) {
1396 set_v8086_seg(&sregs.cs, &env->segs[R_CS]);
1397 set_v8086_seg(&sregs.ds, &env->segs[R_DS]);
1398 set_v8086_seg(&sregs.es, &env->segs[R_ES]);
1399 set_v8086_seg(&sregs.fs, &env->segs[R_FS]);
1400 set_v8086_seg(&sregs.gs, &env->segs[R_GS]);
1401 set_v8086_seg(&sregs.ss, &env->segs[R_SS]);
1402 } else {
1403 set_seg(&sregs.cs, &env->segs[R_CS]);
1404 set_seg(&sregs.ds, &env->segs[R_DS]);
1405 set_seg(&sregs.es, &env->segs[R_ES]);
1406 set_seg(&sregs.fs, &env->segs[R_FS]);
1407 set_seg(&sregs.gs, &env->segs[R_GS]);
1408 set_seg(&sregs.ss, &env->segs[R_SS]);
1411 set_seg(&sregs.tr, &env->tr);
1412 set_seg(&sregs.ldt, &env->ldt);
1414 sregs.idt.limit = env->idt.limit;
1415 sregs.idt.base = env->idt.base;
1416 memset(sregs.idt.padding, 0, sizeof sregs.idt.padding);
1417 sregs.gdt.limit = env->gdt.limit;
1418 sregs.gdt.base = env->gdt.base;
1419 memset(sregs.gdt.padding, 0, sizeof sregs.gdt.padding);
1421 sregs.cr0 = env->cr[0];
1422 sregs.cr2 = env->cr[2];
1423 sregs.cr3 = env->cr[3];
1424 sregs.cr4 = env->cr[4];
1426 sregs.cr8 = cpu_get_apic_tpr(cpu->apic_state);
1427 sregs.apic_base = cpu_get_apic_base(cpu->apic_state);
1429 sregs.efer = env->efer;
1431 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
1434 static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
1435 uint32_t index, uint64_t value)
1437 entry->index = index;
1438 entry->reserved = 0;
1439 entry->data = value;
1442 static int kvm_put_tscdeadline_msr(X86CPU *cpu)
1444 CPUX86State *env = &cpu->env;
1445 struct {
1446 struct kvm_msrs info;
1447 struct kvm_msr_entry entries[1];
1448 } msr_data;
1449 struct kvm_msr_entry *msrs = msr_data.entries;
1450 int ret;
1452 if (!has_msr_tsc_deadline) {
1453 return 0;
1456 kvm_msr_entry_set(&msrs[0], MSR_IA32_TSCDEADLINE, env->tsc_deadline);
1458 msr_data.info = (struct kvm_msrs) {
1459 .nmsrs = 1,
1462 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1463 if (ret < 0) {
1464 return ret;
1467 assert(ret == 1);
1468 return 0;
1472 * Provide a separate write service for the feature control MSR in order to
1473 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1474 * before writing any other state because forcibly leaving nested mode
1475 * invalidates the VCPU state.
1477 static int kvm_put_msr_feature_control(X86CPU *cpu)
1479 struct {
1480 struct kvm_msrs info;
1481 struct kvm_msr_entry entry;
1482 } msr_data;
1483 int ret;
1485 if (!has_msr_feature_control) {
1486 return 0;
1489 kvm_msr_entry_set(&msr_data.entry, MSR_IA32_FEATURE_CONTROL,
1490 cpu->env.msr_ia32_feature_control);
1492 msr_data.info = (struct kvm_msrs) {
1493 .nmsrs = 1,
1496 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1497 if (ret < 0) {
1498 return ret;
1501 assert(ret == 1);
1502 return 0;
1505 static int kvm_put_msrs(X86CPU *cpu, int level)
1507 CPUX86State *env = &cpu->env;
1508 struct {
1509 struct kvm_msrs info;
1510 struct kvm_msr_entry entries[150];
1511 } msr_data;
1512 struct kvm_msr_entry *msrs = msr_data.entries;
1513 int n = 0, i;
1514 int ret;
1516 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
1517 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
1518 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
1519 kvm_msr_entry_set(&msrs[n++], MSR_PAT, env->pat);
1520 if (has_msr_star) {
1521 kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
1523 if (has_msr_hsave_pa) {
1524 kvm_msr_entry_set(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave);
1526 if (has_msr_tsc_aux) {
1527 kvm_msr_entry_set(&msrs[n++], MSR_TSC_AUX, env->tsc_aux);
1529 if (has_msr_tsc_adjust) {
1530 kvm_msr_entry_set(&msrs[n++], MSR_TSC_ADJUST, env->tsc_adjust);
1532 if (has_msr_misc_enable) {
1533 kvm_msr_entry_set(&msrs[n++], MSR_IA32_MISC_ENABLE,
1534 env->msr_ia32_misc_enable);
1536 if (has_msr_smbase) {
1537 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SMBASE, env->smbase);
1539 if (has_msr_bndcfgs) {
1540 kvm_msr_entry_set(&msrs[n++], MSR_IA32_BNDCFGS, env->msr_bndcfgs);
1542 if (has_msr_xss) {
1543 kvm_msr_entry_set(&msrs[n++], MSR_IA32_XSS, env->xss);
1545 #ifdef TARGET_X86_64
1546 if (lm_capable_kernel) {
1547 kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
1548 kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
1549 kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
1550 kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
1552 #endif
1554 * The following MSRs have side effects on the guest or are too heavy
1555 * for normal writeback. Limit them to reset or full state updates.
1557 if (level >= KVM_PUT_RESET_STATE) {
1558 kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
1559 kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME,
1560 env->system_time_msr);
1561 kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
1562 if (has_msr_async_pf_en) {
1563 kvm_msr_entry_set(&msrs[n++], MSR_KVM_ASYNC_PF_EN,
1564 env->async_pf_en_msr);
1566 if (has_msr_pv_eoi_en) {
1567 kvm_msr_entry_set(&msrs[n++], MSR_KVM_PV_EOI_EN,
1568 env->pv_eoi_en_msr);
1570 if (has_msr_kvm_steal_time) {
1571 kvm_msr_entry_set(&msrs[n++], MSR_KVM_STEAL_TIME,
1572 env->steal_time_msr);
1574 if (has_msr_architectural_pmu) {
1575 /* Stop the counter. */
1576 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
1577 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL, 0);
1579 /* Set the counter values. */
1580 for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
1581 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR0 + i,
1582 env->msr_fixed_counters[i]);
1584 for (i = 0; i < num_architectural_pmu_counters; i++) {
1585 kvm_msr_entry_set(&msrs[n++], MSR_P6_PERFCTR0 + i,
1586 env->msr_gp_counters[i]);
1587 kvm_msr_entry_set(&msrs[n++], MSR_P6_EVNTSEL0 + i,
1588 env->msr_gp_evtsel[i]);
1590 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_STATUS,
1591 env->msr_global_status);
1592 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1593 env->msr_global_ovf_ctrl);
1595 /* Now start the PMU. */
1596 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL,
1597 env->msr_fixed_ctr_ctrl);
1598 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL,
1599 env->msr_global_ctrl);
1601 if (has_msr_hv_hypercall) {
1602 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID,
1603 env->msr_hv_guest_os_id);
1604 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL,
1605 env->msr_hv_hypercall);
1607 if (has_msr_hv_vapic) {
1608 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
1609 env->msr_hv_vapic);
1611 if (has_msr_hv_tsc) {
1612 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
1613 env->msr_hv_tsc);
1615 if (has_msr_hv_crash) {
1616 int j;
1618 for (j = 0; j < HV_X64_MSR_CRASH_PARAMS; j++)
1619 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_CRASH_P0 + j,
1620 env->msr_hv_crash_params[j]);
1622 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_CRASH_CTL,
1623 HV_X64_MSR_CRASH_CTL_NOTIFY);
1625 if (has_msr_hv_runtime) {
1626 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_VP_RUNTIME,
1627 env->msr_hv_runtime);
1629 if (cpu->hyperv_synic) {
1630 int j;
1632 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SCONTROL,
1633 env->msr_hv_synic_control);
1634 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SVERSION,
1635 env->msr_hv_synic_version);
1636 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SIEFP,
1637 env->msr_hv_synic_evt_page);
1638 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SIMP,
1639 env->msr_hv_synic_msg_page);
1641 for (j = 0; j < ARRAY_SIZE(env->msr_hv_synic_sint); j++) {
1642 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SINT0 + j,
1643 env->msr_hv_synic_sint[j]);
1646 if (has_msr_hv_stimer) {
1647 int j;
1649 for (j = 0; j < ARRAY_SIZE(env->msr_hv_stimer_config); j++) {
1650 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_STIMER0_CONFIG + j*2,
1651 env->msr_hv_stimer_config[j]);
1654 for (j = 0; j < ARRAY_SIZE(env->msr_hv_stimer_count); j++) {
1655 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_STIMER0_COUNT + j*2,
1656 env->msr_hv_stimer_count[j]);
1659 if (has_msr_mtrr) {
1660 kvm_msr_entry_set(&msrs[n++], MSR_MTRRdefType, env->mtrr_deftype);
1661 kvm_msr_entry_set(&msrs[n++],
1662 MSR_MTRRfix64K_00000, env->mtrr_fixed[0]);
1663 kvm_msr_entry_set(&msrs[n++],
1664 MSR_MTRRfix16K_80000, env->mtrr_fixed[1]);
1665 kvm_msr_entry_set(&msrs[n++],
1666 MSR_MTRRfix16K_A0000, env->mtrr_fixed[2]);
1667 kvm_msr_entry_set(&msrs[n++],
1668 MSR_MTRRfix4K_C0000, env->mtrr_fixed[3]);
1669 kvm_msr_entry_set(&msrs[n++],
1670 MSR_MTRRfix4K_C8000, env->mtrr_fixed[4]);
1671 kvm_msr_entry_set(&msrs[n++],
1672 MSR_MTRRfix4K_D0000, env->mtrr_fixed[5]);
1673 kvm_msr_entry_set(&msrs[n++],
1674 MSR_MTRRfix4K_D8000, env->mtrr_fixed[6]);
1675 kvm_msr_entry_set(&msrs[n++],
1676 MSR_MTRRfix4K_E0000, env->mtrr_fixed[7]);
1677 kvm_msr_entry_set(&msrs[n++],
1678 MSR_MTRRfix4K_E8000, env->mtrr_fixed[8]);
1679 kvm_msr_entry_set(&msrs[n++],
1680 MSR_MTRRfix4K_F0000, env->mtrr_fixed[9]);
1681 kvm_msr_entry_set(&msrs[n++],
1682 MSR_MTRRfix4K_F8000, env->mtrr_fixed[10]);
1683 for (i = 0; i < MSR_MTRRcap_VCNT; i++) {
1684 kvm_msr_entry_set(&msrs[n++],
1685 MSR_MTRRphysBase(i), env->mtrr_var[i].base);
1686 kvm_msr_entry_set(&msrs[n++],
1687 MSR_MTRRphysMask(i), env->mtrr_var[i].mask);
1691 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1692 * kvm_put_msr_feature_control. */
1694 if (env->mcg_cap) {
1695 int i;
1697 kvm_msr_entry_set(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
1698 kvm_msr_entry_set(&msrs[n++], MSR_MCG_CTL, env->mcg_ctl);
1699 for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) {
1700 kvm_msr_entry_set(&msrs[n++], MSR_MC0_CTL + i, env->mce_banks[i]);
1704 msr_data.info = (struct kvm_msrs) {
1705 .nmsrs = n,
1708 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1709 if (ret < 0) {
1710 return ret;
1713 assert(ret == n);
1714 return 0;
1718 static int kvm_get_fpu(X86CPU *cpu)
1720 CPUX86State *env = &cpu->env;
1721 struct kvm_fpu fpu;
1722 int i, ret;
1724 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_FPU, &fpu);
1725 if (ret < 0) {
1726 return ret;
1729 env->fpstt = (fpu.fsw >> 11) & 7;
1730 env->fpus = fpu.fsw;
1731 env->fpuc = fpu.fcw;
1732 env->fpop = fpu.last_opcode;
1733 env->fpip = fpu.last_ip;
1734 env->fpdp = fpu.last_dp;
1735 for (i = 0; i < 8; ++i) {
1736 env->fptags[i] = !((fpu.ftwx >> i) & 1);
1738 memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
1739 for (i = 0; i < CPU_NB_REGS; i++) {
1740 env->xmm_regs[i].ZMM_Q(0) = ldq_p(&fpu.xmm[i][0]);
1741 env->xmm_regs[i].ZMM_Q(1) = ldq_p(&fpu.xmm[i][8]);
1743 env->mxcsr = fpu.mxcsr;
1745 return 0;
1748 static int kvm_get_xsave(X86CPU *cpu)
1750 CPUX86State *env = &cpu->env;
1751 struct kvm_xsave* xsave = env->kvm_xsave_buf;
1752 int ret, i;
1753 const uint8_t *xmm, *ymmh, *zmmh;
1754 uint16_t cwd, swd, twd;
1756 if (!has_xsave) {
1757 return kvm_get_fpu(cpu);
1760 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XSAVE, xsave);
1761 if (ret < 0) {
1762 return ret;
1765 cwd = (uint16_t)xsave->region[XSAVE_FCW_FSW];
1766 swd = (uint16_t)(xsave->region[XSAVE_FCW_FSW] >> 16);
1767 twd = (uint16_t)xsave->region[XSAVE_FTW_FOP];
1768 env->fpop = (uint16_t)(xsave->region[XSAVE_FTW_FOP] >> 16);
1769 env->fpstt = (swd >> 11) & 7;
1770 env->fpus = swd;
1771 env->fpuc = cwd;
1772 for (i = 0; i < 8; ++i) {
1773 env->fptags[i] = !((twd >> i) & 1);
1775 memcpy(&env->fpip, &xsave->region[XSAVE_CWD_RIP], sizeof(env->fpip));
1776 memcpy(&env->fpdp, &xsave->region[XSAVE_CWD_RDP], sizeof(env->fpdp));
1777 env->mxcsr = xsave->region[XSAVE_MXCSR];
1778 memcpy(env->fpregs, &xsave->region[XSAVE_ST_SPACE],
1779 sizeof env->fpregs);
1780 env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV];
1781 memcpy(env->bnd_regs, &xsave->region[XSAVE_BNDREGS],
1782 sizeof env->bnd_regs);
1783 memcpy(&env->bndcs_regs, &xsave->region[XSAVE_BNDCSR],
1784 sizeof(env->bndcs_regs));
1785 memcpy(env->opmask_regs, &xsave->region[XSAVE_OPMASK],
1786 sizeof env->opmask_regs);
1788 xmm = (const uint8_t *)&xsave->region[XSAVE_XMM_SPACE];
1789 ymmh = (const uint8_t *)&xsave->region[XSAVE_YMMH_SPACE];
1790 zmmh = (const uint8_t *)&xsave->region[XSAVE_ZMM_Hi256];
1791 for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) {
1792 env->xmm_regs[i].ZMM_Q(0) = ldq_p(xmm);
1793 env->xmm_regs[i].ZMM_Q(1) = ldq_p(xmm+8);
1794 env->xmm_regs[i].ZMM_Q(2) = ldq_p(ymmh);
1795 env->xmm_regs[i].ZMM_Q(3) = ldq_p(ymmh+8);
1796 env->xmm_regs[i].ZMM_Q(4) = ldq_p(zmmh);
1797 env->xmm_regs[i].ZMM_Q(5) = ldq_p(zmmh+8);
1798 env->xmm_regs[i].ZMM_Q(6) = ldq_p(zmmh+16);
1799 env->xmm_regs[i].ZMM_Q(7) = ldq_p(zmmh+24);
1802 #ifdef TARGET_X86_64
1803 memcpy(&env->xmm_regs[16], &xsave->region[XSAVE_Hi16_ZMM],
1804 16 * sizeof env->xmm_regs[16]);
1805 memcpy(&env->pkru, &xsave->region[XSAVE_PKRU], sizeof env->pkru);
1806 #endif
1807 return 0;
1810 static int kvm_get_xcrs(X86CPU *cpu)
1812 CPUX86State *env = &cpu->env;
1813 int i, ret;
1814 struct kvm_xcrs xcrs;
1816 if (!has_xcrs) {
1817 return 0;
1820 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XCRS, &xcrs);
1821 if (ret < 0) {
1822 return ret;
1825 for (i = 0; i < xcrs.nr_xcrs; i++) {
1826 /* Only support xcr0 now */
1827 if (xcrs.xcrs[i].xcr == 0) {
1828 env->xcr0 = xcrs.xcrs[i].value;
1829 break;
1832 return 0;
1835 static int kvm_get_sregs(X86CPU *cpu)
1837 CPUX86State *env = &cpu->env;
1838 struct kvm_sregs sregs;
1839 uint32_t hflags;
1840 int bit, i, ret;
1842 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1843 if (ret < 0) {
1844 return ret;
1847 /* There can only be one pending IRQ set in the bitmap at a time, so try
1848 to find it and save its number instead (-1 for none). */
1849 env->interrupt_injected = -1;
1850 for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) {
1851 if (sregs.interrupt_bitmap[i]) {
1852 bit = ctz64(sregs.interrupt_bitmap[i]);
1853 env->interrupt_injected = i * 64 + bit;
1854 break;
1858 get_seg(&env->segs[R_CS], &sregs.cs);
1859 get_seg(&env->segs[R_DS], &sregs.ds);
1860 get_seg(&env->segs[R_ES], &sregs.es);
1861 get_seg(&env->segs[R_FS], &sregs.fs);
1862 get_seg(&env->segs[R_GS], &sregs.gs);
1863 get_seg(&env->segs[R_SS], &sregs.ss);
1865 get_seg(&env->tr, &sregs.tr);
1866 get_seg(&env->ldt, &sregs.ldt);
1868 env->idt.limit = sregs.idt.limit;
1869 env->idt.base = sregs.idt.base;
1870 env->gdt.limit = sregs.gdt.limit;
1871 env->gdt.base = sregs.gdt.base;
1873 env->cr[0] = sregs.cr0;
1874 env->cr[2] = sregs.cr2;
1875 env->cr[3] = sregs.cr3;
1876 env->cr[4] = sregs.cr4;
1878 env->efer = sregs.efer;
1880 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1882 #define HFLAG_COPY_MASK \
1883 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1884 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1885 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1886 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1888 hflags = env->hflags & HFLAG_COPY_MASK;
1889 hflags |= (env->segs[R_SS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;
1890 hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
1891 hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
1892 (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK);
1893 hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK));
1895 if (env->cr[4] & CR4_OSFXSR_MASK) {
1896 hflags |= HF_OSFXSR_MASK;
1899 if (env->efer & MSR_EFER_LMA) {
1900 hflags |= HF_LMA_MASK;
1903 if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
1904 hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
1905 } else {
1906 hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >>
1907 (DESC_B_SHIFT - HF_CS32_SHIFT);
1908 hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >>
1909 (DESC_B_SHIFT - HF_SS32_SHIFT);
1910 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK) ||
1911 !(hflags & HF_CS32_MASK)) {
1912 hflags |= HF_ADDSEG_MASK;
1913 } else {
1914 hflags |= ((env->segs[R_DS].base | env->segs[R_ES].base |
1915 env->segs[R_SS].base) != 0) << HF_ADDSEG_SHIFT;
1918 env->hflags = hflags;
1920 return 0;
1923 static int kvm_get_msrs(X86CPU *cpu)
1925 CPUX86State *env = &cpu->env;
1926 struct {
1927 struct kvm_msrs info;
1928 struct kvm_msr_entry entries[150];
1929 } msr_data;
1930 struct kvm_msr_entry *msrs = msr_data.entries;
1931 int ret, i, n;
1933 n = 0;
1934 msrs[n++].index = MSR_IA32_SYSENTER_CS;
1935 msrs[n++].index = MSR_IA32_SYSENTER_ESP;
1936 msrs[n++].index = MSR_IA32_SYSENTER_EIP;
1937 msrs[n++].index = MSR_PAT;
1938 if (has_msr_star) {
1939 msrs[n++].index = MSR_STAR;
1941 if (has_msr_hsave_pa) {
1942 msrs[n++].index = MSR_VM_HSAVE_PA;
1944 if (has_msr_tsc_aux) {
1945 msrs[n++].index = MSR_TSC_AUX;
1947 if (has_msr_tsc_adjust) {
1948 msrs[n++].index = MSR_TSC_ADJUST;
1950 if (has_msr_tsc_deadline) {
1951 msrs[n++].index = MSR_IA32_TSCDEADLINE;
1953 if (has_msr_misc_enable) {
1954 msrs[n++].index = MSR_IA32_MISC_ENABLE;
1956 if (has_msr_smbase) {
1957 msrs[n++].index = MSR_IA32_SMBASE;
1959 if (has_msr_feature_control) {
1960 msrs[n++].index = MSR_IA32_FEATURE_CONTROL;
1962 if (has_msr_bndcfgs) {
1963 msrs[n++].index = MSR_IA32_BNDCFGS;
1965 if (has_msr_xss) {
1966 msrs[n++].index = MSR_IA32_XSS;
1970 if (!env->tsc_valid) {
1971 msrs[n++].index = MSR_IA32_TSC;
1972 env->tsc_valid = !runstate_is_running();
1975 #ifdef TARGET_X86_64
1976 if (lm_capable_kernel) {
1977 msrs[n++].index = MSR_CSTAR;
1978 msrs[n++].index = MSR_KERNELGSBASE;
1979 msrs[n++].index = MSR_FMASK;
1980 msrs[n++].index = MSR_LSTAR;
1982 #endif
1983 msrs[n++].index = MSR_KVM_SYSTEM_TIME;
1984 msrs[n++].index = MSR_KVM_WALL_CLOCK;
1985 if (has_msr_async_pf_en) {
1986 msrs[n++].index = MSR_KVM_ASYNC_PF_EN;
1988 if (has_msr_pv_eoi_en) {
1989 msrs[n++].index = MSR_KVM_PV_EOI_EN;
1991 if (has_msr_kvm_steal_time) {
1992 msrs[n++].index = MSR_KVM_STEAL_TIME;
1994 if (has_msr_architectural_pmu) {
1995 msrs[n++].index = MSR_CORE_PERF_FIXED_CTR_CTRL;
1996 msrs[n++].index = MSR_CORE_PERF_GLOBAL_CTRL;
1997 msrs[n++].index = MSR_CORE_PERF_GLOBAL_STATUS;
1998 msrs[n++].index = MSR_CORE_PERF_GLOBAL_OVF_CTRL;
1999 for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
2000 msrs[n++].index = MSR_CORE_PERF_FIXED_CTR0 + i;
2002 for (i = 0; i < num_architectural_pmu_counters; i++) {
2003 msrs[n++].index = MSR_P6_PERFCTR0 + i;
2004 msrs[n++].index = MSR_P6_EVNTSEL0 + i;
2008 if (env->mcg_cap) {
2009 msrs[n++].index = MSR_MCG_STATUS;
2010 msrs[n++].index = MSR_MCG_CTL;
2011 for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) {
2012 msrs[n++].index = MSR_MC0_CTL + i;
2016 if (has_msr_hv_hypercall) {
2017 msrs[n++].index = HV_X64_MSR_HYPERCALL;
2018 msrs[n++].index = HV_X64_MSR_GUEST_OS_ID;
2020 if (has_msr_hv_vapic) {
2021 msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
2023 if (has_msr_hv_tsc) {
2024 msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
2026 if (has_msr_hv_crash) {
2027 int j;
2029 for (j = 0; j < HV_X64_MSR_CRASH_PARAMS; j++) {
2030 msrs[n++].index = HV_X64_MSR_CRASH_P0 + j;
2033 if (has_msr_hv_runtime) {
2034 msrs[n++].index = HV_X64_MSR_VP_RUNTIME;
2036 if (cpu->hyperv_synic) {
2037 uint32_t msr;
2039 msrs[n++].index = HV_X64_MSR_SCONTROL;
2040 msrs[n++].index = HV_X64_MSR_SVERSION;
2041 msrs[n++].index = HV_X64_MSR_SIEFP;
2042 msrs[n++].index = HV_X64_MSR_SIMP;
2043 for (msr = HV_X64_MSR_SINT0; msr <= HV_X64_MSR_SINT15; msr++) {
2044 msrs[n++].index = msr;
2047 if (has_msr_hv_stimer) {
2048 uint32_t msr;
2050 for (msr = HV_X64_MSR_STIMER0_CONFIG; msr <= HV_X64_MSR_STIMER3_COUNT;
2051 msr++) {
2052 msrs[n++].index = msr;
2055 if (has_msr_mtrr) {
2056 msrs[n++].index = MSR_MTRRdefType;
2057 msrs[n++].index = MSR_MTRRfix64K_00000;
2058 msrs[n++].index = MSR_MTRRfix16K_80000;
2059 msrs[n++].index = MSR_MTRRfix16K_A0000;
2060 msrs[n++].index = MSR_MTRRfix4K_C0000;
2061 msrs[n++].index = MSR_MTRRfix4K_C8000;
2062 msrs[n++].index = MSR_MTRRfix4K_D0000;
2063 msrs[n++].index = MSR_MTRRfix4K_D8000;
2064 msrs[n++].index = MSR_MTRRfix4K_E0000;
2065 msrs[n++].index = MSR_MTRRfix4K_E8000;
2066 msrs[n++].index = MSR_MTRRfix4K_F0000;
2067 msrs[n++].index = MSR_MTRRfix4K_F8000;
2068 for (i = 0; i < MSR_MTRRcap_VCNT; i++) {
2069 msrs[n++].index = MSR_MTRRphysBase(i);
2070 msrs[n++].index = MSR_MTRRphysMask(i);
2074 msr_data.info = (struct kvm_msrs) {
2075 .nmsrs = n,
2078 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
2079 if (ret < 0) {
2080 return ret;
2083 assert(ret == n);
2084 for (i = 0; i < ret; i++) {
2085 uint32_t index = msrs[i].index;
2086 switch (index) {
2087 case MSR_IA32_SYSENTER_CS:
2088 env->sysenter_cs = msrs[i].data;
2089 break;
2090 case MSR_IA32_SYSENTER_ESP:
2091 env->sysenter_esp = msrs[i].data;
2092 break;
2093 case MSR_IA32_SYSENTER_EIP:
2094 env->sysenter_eip = msrs[i].data;
2095 break;
2096 case MSR_PAT:
2097 env->pat = msrs[i].data;
2098 break;
2099 case MSR_STAR:
2100 env->star = msrs[i].data;
2101 break;
2102 #ifdef TARGET_X86_64
2103 case MSR_CSTAR:
2104 env->cstar = msrs[i].data;
2105 break;
2106 case MSR_KERNELGSBASE:
2107 env->kernelgsbase = msrs[i].data;
2108 break;
2109 case MSR_FMASK:
2110 env->fmask = msrs[i].data;
2111 break;
2112 case MSR_LSTAR:
2113 env->lstar = msrs[i].data;
2114 break;
2115 #endif
2116 case MSR_IA32_TSC:
2117 env->tsc = msrs[i].data;
2118 break;
2119 case MSR_TSC_AUX:
2120 env->tsc_aux = msrs[i].data;
2121 break;
2122 case MSR_TSC_ADJUST:
2123 env->tsc_adjust = msrs[i].data;
2124 break;
2125 case MSR_IA32_TSCDEADLINE:
2126 env->tsc_deadline = msrs[i].data;
2127 break;
2128 case MSR_VM_HSAVE_PA:
2129 env->vm_hsave = msrs[i].data;
2130 break;
2131 case MSR_KVM_SYSTEM_TIME:
2132 env->system_time_msr = msrs[i].data;
2133 break;
2134 case MSR_KVM_WALL_CLOCK:
2135 env->wall_clock_msr = msrs[i].data;
2136 break;
2137 case MSR_MCG_STATUS:
2138 env->mcg_status = msrs[i].data;
2139 break;
2140 case MSR_MCG_CTL:
2141 env->mcg_ctl = msrs[i].data;
2142 break;
2143 case MSR_IA32_MISC_ENABLE:
2144 env->msr_ia32_misc_enable = msrs[i].data;
2145 break;
2146 case MSR_IA32_SMBASE:
2147 env->smbase = msrs[i].data;
2148 break;
2149 case MSR_IA32_FEATURE_CONTROL:
2150 env->msr_ia32_feature_control = msrs[i].data;
2151 break;
2152 case MSR_IA32_BNDCFGS:
2153 env->msr_bndcfgs = msrs[i].data;
2154 break;
2155 case MSR_IA32_XSS:
2156 env->xss = msrs[i].data;
2157 break;
2158 default:
2159 if (msrs[i].index >= MSR_MC0_CTL &&
2160 msrs[i].index < MSR_MC0_CTL + (env->mcg_cap & 0xff) * 4) {
2161 env->mce_banks[msrs[i].index - MSR_MC0_CTL] = msrs[i].data;
2163 break;
2164 case MSR_KVM_ASYNC_PF_EN:
2165 env->async_pf_en_msr = msrs[i].data;
2166 break;
2167 case MSR_KVM_PV_EOI_EN:
2168 env->pv_eoi_en_msr = msrs[i].data;
2169 break;
2170 case MSR_KVM_STEAL_TIME:
2171 env->steal_time_msr = msrs[i].data;
2172 break;
2173 case MSR_CORE_PERF_FIXED_CTR_CTRL:
2174 env->msr_fixed_ctr_ctrl = msrs[i].data;
2175 break;
2176 case MSR_CORE_PERF_GLOBAL_CTRL:
2177 env->msr_global_ctrl = msrs[i].data;
2178 break;
2179 case MSR_CORE_PERF_GLOBAL_STATUS:
2180 env->msr_global_status = msrs[i].data;
2181 break;
2182 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
2183 env->msr_global_ovf_ctrl = msrs[i].data;
2184 break;
2185 case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR0 + MAX_FIXED_COUNTERS - 1:
2186 env->msr_fixed_counters[index - MSR_CORE_PERF_FIXED_CTR0] = msrs[i].data;
2187 break;
2188 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR0 + MAX_GP_COUNTERS - 1:
2189 env->msr_gp_counters[index - MSR_P6_PERFCTR0] = msrs[i].data;
2190 break;
2191 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL0 + MAX_GP_COUNTERS - 1:
2192 env->msr_gp_evtsel[index - MSR_P6_EVNTSEL0] = msrs[i].data;
2193 break;
2194 case HV_X64_MSR_HYPERCALL:
2195 env->msr_hv_hypercall = msrs[i].data;
2196 break;
2197 case HV_X64_MSR_GUEST_OS_ID:
2198 env->msr_hv_guest_os_id = msrs[i].data;
2199 break;
2200 case HV_X64_MSR_APIC_ASSIST_PAGE:
2201 env->msr_hv_vapic = msrs[i].data;
2202 break;
2203 case HV_X64_MSR_REFERENCE_TSC:
2204 env->msr_hv_tsc = msrs[i].data;
2205 break;
2206 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2207 env->msr_hv_crash_params[index - HV_X64_MSR_CRASH_P0] = msrs[i].data;
2208 break;
2209 case HV_X64_MSR_VP_RUNTIME:
2210 env->msr_hv_runtime = msrs[i].data;
2211 break;
2212 case HV_X64_MSR_SCONTROL:
2213 env->msr_hv_synic_control = msrs[i].data;
2214 break;
2215 case HV_X64_MSR_SVERSION:
2216 env->msr_hv_synic_version = msrs[i].data;
2217 break;
2218 case HV_X64_MSR_SIEFP:
2219 env->msr_hv_synic_evt_page = msrs[i].data;
2220 break;
2221 case HV_X64_MSR_SIMP:
2222 env->msr_hv_synic_msg_page = msrs[i].data;
2223 break;
2224 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2225 env->msr_hv_synic_sint[index - HV_X64_MSR_SINT0] = msrs[i].data;
2226 break;
2227 case HV_X64_MSR_STIMER0_CONFIG:
2228 case HV_X64_MSR_STIMER1_CONFIG:
2229 case HV_X64_MSR_STIMER2_CONFIG:
2230 case HV_X64_MSR_STIMER3_CONFIG:
2231 env->msr_hv_stimer_config[(index - HV_X64_MSR_STIMER0_CONFIG)/2] =
2232 msrs[i].data;
2233 break;
2234 case HV_X64_MSR_STIMER0_COUNT:
2235 case HV_X64_MSR_STIMER1_COUNT:
2236 case HV_X64_MSR_STIMER2_COUNT:
2237 case HV_X64_MSR_STIMER3_COUNT:
2238 env->msr_hv_stimer_count[(index - HV_X64_MSR_STIMER0_COUNT)/2] =
2239 msrs[i].data;
2240 break;
2241 case MSR_MTRRdefType:
2242 env->mtrr_deftype = msrs[i].data;
2243 break;
2244 case MSR_MTRRfix64K_00000:
2245 env->mtrr_fixed[0] = msrs[i].data;
2246 break;
2247 case MSR_MTRRfix16K_80000:
2248 env->mtrr_fixed[1] = msrs[i].data;
2249 break;
2250 case MSR_MTRRfix16K_A0000:
2251 env->mtrr_fixed[2] = msrs[i].data;
2252 break;
2253 case MSR_MTRRfix4K_C0000:
2254 env->mtrr_fixed[3] = msrs[i].data;
2255 break;
2256 case MSR_MTRRfix4K_C8000:
2257 env->mtrr_fixed[4] = msrs[i].data;
2258 break;
2259 case MSR_MTRRfix4K_D0000:
2260 env->mtrr_fixed[5] = msrs[i].data;
2261 break;
2262 case MSR_MTRRfix4K_D8000:
2263 env->mtrr_fixed[6] = msrs[i].data;
2264 break;
2265 case MSR_MTRRfix4K_E0000:
2266 env->mtrr_fixed[7] = msrs[i].data;
2267 break;
2268 case MSR_MTRRfix4K_E8000:
2269 env->mtrr_fixed[8] = msrs[i].data;
2270 break;
2271 case MSR_MTRRfix4K_F0000:
2272 env->mtrr_fixed[9] = msrs[i].data;
2273 break;
2274 case MSR_MTRRfix4K_F8000:
2275 env->mtrr_fixed[10] = msrs[i].data;
2276 break;
2277 case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT - 1):
2278 if (index & 1) {
2279 env->mtrr_var[MSR_MTRRphysIndex(index)].mask = msrs[i].data;
2280 } else {
2281 env->mtrr_var[MSR_MTRRphysIndex(index)].base = msrs[i].data;
2283 break;
2287 return 0;
2290 static int kvm_put_mp_state(X86CPU *cpu)
2292 struct kvm_mp_state mp_state = { .mp_state = cpu->env.mp_state };
2294 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2297 static int kvm_get_mp_state(X86CPU *cpu)
2299 CPUState *cs = CPU(cpu);
2300 CPUX86State *env = &cpu->env;
2301 struct kvm_mp_state mp_state;
2302 int ret;
2304 ret = kvm_vcpu_ioctl(cs, KVM_GET_MP_STATE, &mp_state);
2305 if (ret < 0) {
2306 return ret;
2308 env->mp_state = mp_state.mp_state;
2309 if (kvm_irqchip_in_kernel()) {
2310 cs->halted = (mp_state.mp_state == KVM_MP_STATE_HALTED);
2312 return 0;
2315 static int kvm_get_apic(X86CPU *cpu)
2317 DeviceState *apic = cpu->apic_state;
2318 struct kvm_lapic_state kapic;
2319 int ret;
2321 if (apic && kvm_irqchip_in_kernel()) {
2322 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_LAPIC, &kapic);
2323 if (ret < 0) {
2324 return ret;
2327 kvm_get_apic_state(apic, &kapic);
2329 return 0;
2332 static int kvm_put_apic(X86CPU *cpu)
2334 DeviceState *apic = cpu->apic_state;
2335 struct kvm_lapic_state kapic;
2337 if (apic && kvm_irqchip_in_kernel()) {
2338 kvm_put_apic_state(apic, &kapic);
2340 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_LAPIC, &kapic);
2342 return 0;
2345 static int kvm_put_vcpu_events(X86CPU *cpu, int level)
2347 CPUState *cs = CPU(cpu);
2348 CPUX86State *env = &cpu->env;
2349 struct kvm_vcpu_events events = {};
2351 if (!kvm_has_vcpu_events()) {
2352 return 0;
2355 events.exception.injected = (env->exception_injected >= 0);
2356 events.exception.nr = env->exception_injected;
2357 events.exception.has_error_code = env->has_error_code;
2358 events.exception.error_code = env->error_code;
2359 events.exception.pad = 0;
2361 events.interrupt.injected = (env->interrupt_injected >= 0);
2362 events.interrupt.nr = env->interrupt_injected;
2363 events.interrupt.soft = env->soft_interrupt;
2365 events.nmi.injected = env->nmi_injected;
2366 events.nmi.pending = env->nmi_pending;
2367 events.nmi.masked = !!(env->hflags2 & HF2_NMI_MASK);
2368 events.nmi.pad = 0;
2370 events.sipi_vector = env->sipi_vector;
2372 if (has_msr_smbase) {
2373 events.smi.smm = !!(env->hflags & HF_SMM_MASK);
2374 events.smi.smm_inside_nmi = !!(env->hflags2 & HF2_SMM_INSIDE_NMI_MASK);
2375 if (kvm_irqchip_in_kernel()) {
2376 /* As soon as these are moved to the kernel, remove them
2377 * from cs->interrupt_request.
2379 events.smi.pending = cs->interrupt_request & CPU_INTERRUPT_SMI;
2380 events.smi.latched_init = cs->interrupt_request & CPU_INTERRUPT_INIT;
2381 cs->interrupt_request &= ~(CPU_INTERRUPT_INIT | CPU_INTERRUPT_SMI);
2382 } else {
2383 /* Keep these in cs->interrupt_request. */
2384 events.smi.pending = 0;
2385 events.smi.latched_init = 0;
2387 events.flags |= KVM_VCPUEVENT_VALID_SMM;
2390 events.flags = 0;
2391 if (level >= KVM_PUT_RESET_STATE) {
2392 events.flags |=
2393 KVM_VCPUEVENT_VALID_NMI_PENDING | KVM_VCPUEVENT_VALID_SIPI_VECTOR;
2396 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);
2399 static int kvm_get_vcpu_events(X86CPU *cpu)
2401 CPUX86State *env = &cpu->env;
2402 struct kvm_vcpu_events events;
2403 int ret;
2405 if (!kvm_has_vcpu_events()) {
2406 return 0;
2409 memset(&events, 0, sizeof(events));
2410 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events);
2411 if (ret < 0) {
2412 return ret;
2414 env->exception_injected =
2415 events.exception.injected ? events.exception.nr : -1;
2416 env->has_error_code = events.exception.has_error_code;
2417 env->error_code = events.exception.error_code;
2419 env->interrupt_injected =
2420 events.interrupt.injected ? events.interrupt.nr : -1;
2421 env->soft_interrupt = events.interrupt.soft;
2423 env->nmi_injected = events.nmi.injected;
2424 env->nmi_pending = events.nmi.pending;
2425 if (events.nmi.masked) {
2426 env->hflags2 |= HF2_NMI_MASK;
2427 } else {
2428 env->hflags2 &= ~HF2_NMI_MASK;
2431 if (events.flags & KVM_VCPUEVENT_VALID_SMM) {
2432 if (events.smi.smm) {
2433 env->hflags |= HF_SMM_MASK;
2434 } else {
2435 env->hflags &= ~HF_SMM_MASK;
2437 if (events.smi.pending) {
2438 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
2439 } else {
2440 cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
2442 if (events.smi.smm_inside_nmi) {
2443 env->hflags2 |= HF2_SMM_INSIDE_NMI_MASK;
2444 } else {
2445 env->hflags2 &= ~HF2_SMM_INSIDE_NMI_MASK;
2447 if (events.smi.latched_init) {
2448 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_INIT);
2449 } else {
2450 cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_INIT);
2454 env->sipi_vector = events.sipi_vector;
2456 return 0;
2459 static int kvm_guest_debug_workarounds(X86CPU *cpu)
2461 CPUState *cs = CPU(cpu);
2462 CPUX86State *env = &cpu->env;
2463 int ret = 0;
2464 unsigned long reinject_trap = 0;
2466 if (!kvm_has_vcpu_events()) {
2467 if (env->exception_injected == 1) {
2468 reinject_trap = KVM_GUESTDBG_INJECT_DB;
2469 } else if (env->exception_injected == 3) {
2470 reinject_trap = KVM_GUESTDBG_INJECT_BP;
2472 env->exception_injected = -1;
2476 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
2477 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
2478 * by updating the debug state once again if single-stepping is on.
2479 * Another reason to call kvm_update_guest_debug here is a pending debug
2480 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
2481 * reinject them via SET_GUEST_DEBUG.
2483 if (reinject_trap ||
2484 (!kvm_has_robust_singlestep() && cs->singlestep_enabled)) {
2485 ret = kvm_update_guest_debug(cs, reinject_trap);
2487 return ret;
2490 static int kvm_put_debugregs(X86CPU *cpu)
2492 CPUX86State *env = &cpu->env;
2493 struct kvm_debugregs dbgregs;
2494 int i;
2496 if (!kvm_has_debugregs()) {
2497 return 0;
2500 for (i = 0; i < 4; i++) {
2501 dbgregs.db[i] = env->dr[i];
2503 dbgregs.dr6 = env->dr[6];
2504 dbgregs.dr7 = env->dr[7];
2505 dbgregs.flags = 0;
2507 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_DEBUGREGS, &dbgregs);
2510 static int kvm_get_debugregs(X86CPU *cpu)
2512 CPUX86State *env = &cpu->env;
2513 struct kvm_debugregs dbgregs;
2514 int i, ret;
2516 if (!kvm_has_debugregs()) {
2517 return 0;
2520 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_DEBUGREGS, &dbgregs);
2521 if (ret < 0) {
2522 return ret;
2524 for (i = 0; i < 4; i++) {
2525 env->dr[i] = dbgregs.db[i];
2527 env->dr[4] = env->dr[6] = dbgregs.dr6;
2528 env->dr[5] = env->dr[7] = dbgregs.dr7;
2530 return 0;
2533 int kvm_arch_put_registers(CPUState *cpu, int level)
2535 X86CPU *x86_cpu = X86_CPU(cpu);
2536 int ret;
2538 assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu));
2540 if (level >= KVM_PUT_RESET_STATE) {
2541 ret = kvm_put_msr_feature_control(x86_cpu);
2542 if (ret < 0) {
2543 return ret;
2547 if (level == KVM_PUT_FULL_STATE) {
2548 /* We don't check for kvm_arch_set_tsc_khz() errors here,
2549 * because TSC frequency mismatch shouldn't abort migration,
2550 * unless the user explicitly asked for a more strict TSC
2551 * setting (e.g. using an explicit "tsc-freq" option).
2553 kvm_arch_set_tsc_khz(cpu);
2556 ret = kvm_getput_regs(x86_cpu, 1);
2557 if (ret < 0) {
2558 return ret;
2560 ret = kvm_put_xsave(x86_cpu);
2561 if (ret < 0) {
2562 return ret;
2564 ret = kvm_put_xcrs(x86_cpu);
2565 if (ret < 0) {
2566 return ret;
2568 ret = kvm_put_sregs(x86_cpu);
2569 if (ret < 0) {
2570 return ret;
2572 /* must be before kvm_put_msrs */
2573 ret = kvm_inject_mce_oldstyle(x86_cpu);
2574 if (ret < 0) {
2575 return ret;
2577 ret = kvm_put_msrs(x86_cpu, level);
2578 if (ret < 0) {
2579 return ret;
2581 if (level >= KVM_PUT_RESET_STATE) {
2582 ret = kvm_put_mp_state(x86_cpu);
2583 if (ret < 0) {
2584 return ret;
2586 ret = kvm_put_apic(x86_cpu);
2587 if (ret < 0) {
2588 return ret;
2592 ret = kvm_put_tscdeadline_msr(x86_cpu);
2593 if (ret < 0) {
2594 return ret;
2597 ret = kvm_put_vcpu_events(x86_cpu, level);
2598 if (ret < 0) {
2599 return ret;
2601 ret = kvm_put_debugregs(x86_cpu);
2602 if (ret < 0) {
2603 return ret;
2605 /* must be last */
2606 ret = kvm_guest_debug_workarounds(x86_cpu);
2607 if (ret < 0) {
2608 return ret;
2610 return 0;
2613 int kvm_arch_get_registers(CPUState *cs)
2615 X86CPU *cpu = X86_CPU(cs);
2616 int ret;
2618 assert(cpu_is_stopped(cs) || qemu_cpu_is_self(cs));
2620 ret = kvm_getput_regs(cpu, 0);
2621 if (ret < 0) {
2622 goto out;
2624 ret = kvm_get_xsave(cpu);
2625 if (ret < 0) {
2626 goto out;
2628 ret = kvm_get_xcrs(cpu);
2629 if (ret < 0) {
2630 goto out;
2632 ret = kvm_get_sregs(cpu);
2633 if (ret < 0) {
2634 goto out;
2636 ret = kvm_get_msrs(cpu);
2637 if (ret < 0) {
2638 goto out;
2640 ret = kvm_get_mp_state(cpu);
2641 if (ret < 0) {
2642 goto out;
2644 ret = kvm_get_apic(cpu);
2645 if (ret < 0) {
2646 goto out;
2648 ret = kvm_get_vcpu_events(cpu);
2649 if (ret < 0) {
2650 goto out;
2652 ret = kvm_get_debugregs(cpu);
2653 if (ret < 0) {
2654 goto out;
2656 ret = 0;
2657 out:
2658 cpu_sync_bndcs_hflags(&cpu->env);
2659 return ret;
2662 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
2664 X86CPU *x86_cpu = X86_CPU(cpu);
2665 CPUX86State *env = &x86_cpu->env;
2666 int ret;
2668 /* Inject NMI */
2669 if (cpu->interrupt_request & (CPU_INTERRUPT_NMI | CPU_INTERRUPT_SMI)) {
2670 if (cpu->interrupt_request & CPU_INTERRUPT_NMI) {
2671 qemu_mutex_lock_iothread();
2672 cpu->interrupt_request &= ~CPU_INTERRUPT_NMI;
2673 qemu_mutex_unlock_iothread();
2674 DPRINTF("injected NMI\n");
2675 ret = kvm_vcpu_ioctl(cpu, KVM_NMI);
2676 if (ret < 0) {
2677 fprintf(stderr, "KVM: injection failed, NMI lost (%s)\n",
2678 strerror(-ret));
2681 if (cpu->interrupt_request & CPU_INTERRUPT_SMI) {
2682 qemu_mutex_lock_iothread();
2683 cpu->interrupt_request &= ~CPU_INTERRUPT_SMI;
2684 qemu_mutex_unlock_iothread();
2685 DPRINTF("injected SMI\n");
2686 ret = kvm_vcpu_ioctl(cpu, KVM_SMI);
2687 if (ret < 0) {
2688 fprintf(stderr, "KVM: injection failed, SMI lost (%s)\n",
2689 strerror(-ret));
2694 if (!kvm_pic_in_kernel()) {
2695 qemu_mutex_lock_iothread();
2698 /* Force the VCPU out of its inner loop to process any INIT requests
2699 * or (for userspace APIC, but it is cheap to combine the checks here)
2700 * pending TPR access reports.
2702 if (cpu->interrupt_request & (CPU_INTERRUPT_INIT | CPU_INTERRUPT_TPR)) {
2703 if ((cpu->interrupt_request & CPU_INTERRUPT_INIT) &&
2704 !(env->hflags & HF_SMM_MASK)) {
2705 cpu->exit_request = 1;
2707 if (cpu->interrupt_request & CPU_INTERRUPT_TPR) {
2708 cpu->exit_request = 1;
2712 if (!kvm_pic_in_kernel()) {
2713 /* Try to inject an interrupt if the guest can accept it */
2714 if (run->ready_for_interrupt_injection &&
2715 (cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
2716 (env->eflags & IF_MASK)) {
2717 int irq;
2719 cpu->interrupt_request &= ~CPU_INTERRUPT_HARD;
2720 irq = cpu_get_pic_interrupt(env);
2721 if (irq >= 0) {
2722 struct kvm_interrupt intr;
2724 intr.irq = irq;
2725 DPRINTF("injected interrupt %d\n", irq);
2726 ret = kvm_vcpu_ioctl(cpu, KVM_INTERRUPT, &intr);
2727 if (ret < 0) {
2728 fprintf(stderr,
2729 "KVM: injection failed, interrupt lost (%s)\n",
2730 strerror(-ret));
2735 /* If we have an interrupt but the guest is not ready to receive an
2736 * interrupt, request an interrupt window exit. This will
2737 * cause a return to userspace as soon as the guest is ready to
2738 * receive interrupts. */
2739 if ((cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
2740 run->request_interrupt_window = 1;
2741 } else {
2742 run->request_interrupt_window = 0;
2745 DPRINTF("setting tpr\n");
2746 run->cr8 = cpu_get_apic_tpr(x86_cpu->apic_state);
2748 qemu_mutex_unlock_iothread();
2752 MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
2754 X86CPU *x86_cpu = X86_CPU(cpu);
2755 CPUX86State *env = &x86_cpu->env;
2757 if (run->flags & KVM_RUN_X86_SMM) {
2758 env->hflags |= HF_SMM_MASK;
2759 } else {
2760 env->hflags &= HF_SMM_MASK;
2762 if (run->if_flag) {
2763 env->eflags |= IF_MASK;
2764 } else {
2765 env->eflags &= ~IF_MASK;
2768 /* We need to protect the apic state against concurrent accesses from
2769 * different threads in case the userspace irqchip is used. */
2770 if (!kvm_irqchip_in_kernel()) {
2771 qemu_mutex_lock_iothread();
2773 cpu_set_apic_tpr(x86_cpu->apic_state, run->cr8);
2774 cpu_set_apic_base(x86_cpu->apic_state, run->apic_base);
2775 if (!kvm_irqchip_in_kernel()) {
2776 qemu_mutex_unlock_iothread();
2778 return cpu_get_mem_attrs(env);
2781 int kvm_arch_process_async_events(CPUState *cs)
2783 X86CPU *cpu = X86_CPU(cs);
2784 CPUX86State *env = &cpu->env;
2786 if (cs->interrupt_request & CPU_INTERRUPT_MCE) {
2787 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2788 assert(env->mcg_cap);
2790 cs->interrupt_request &= ~CPU_INTERRUPT_MCE;
2792 kvm_cpu_synchronize_state(cs);
2794 if (env->exception_injected == EXCP08_DBLE) {
2795 /* this means triple fault */
2796 qemu_system_reset_request();
2797 cs->exit_request = 1;
2798 return 0;
2800 env->exception_injected = EXCP12_MCHK;
2801 env->has_error_code = 0;
2803 cs->halted = 0;
2804 if (kvm_irqchip_in_kernel() && env->mp_state == KVM_MP_STATE_HALTED) {
2805 env->mp_state = KVM_MP_STATE_RUNNABLE;
2809 if ((cs->interrupt_request & CPU_INTERRUPT_INIT) &&
2810 !(env->hflags & HF_SMM_MASK)) {
2811 kvm_cpu_synchronize_state(cs);
2812 do_cpu_init(cpu);
2815 if (kvm_irqchip_in_kernel()) {
2816 return 0;
2819 if (cs->interrupt_request & CPU_INTERRUPT_POLL) {
2820 cs->interrupt_request &= ~CPU_INTERRUPT_POLL;
2821 apic_poll_irq(cpu->apic_state);
2823 if (((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
2824 (env->eflags & IF_MASK)) ||
2825 (cs->interrupt_request & CPU_INTERRUPT_NMI)) {
2826 cs->halted = 0;
2828 if (cs->interrupt_request & CPU_INTERRUPT_SIPI) {
2829 kvm_cpu_synchronize_state(cs);
2830 do_cpu_sipi(cpu);
2832 if (cs->interrupt_request & CPU_INTERRUPT_TPR) {
2833 cs->interrupt_request &= ~CPU_INTERRUPT_TPR;
2834 kvm_cpu_synchronize_state(cs);
2835 apic_handle_tpr_access_report(cpu->apic_state, env->eip,
2836 env->tpr_access_type);
2839 return cs->halted;
2842 static int kvm_handle_halt(X86CPU *cpu)
2844 CPUState *cs = CPU(cpu);
2845 CPUX86State *env = &cpu->env;
2847 if (!((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
2848 (env->eflags & IF_MASK)) &&
2849 !(cs->interrupt_request & CPU_INTERRUPT_NMI)) {
2850 cs->halted = 1;
2851 return EXCP_HLT;
2854 return 0;
2857 static int kvm_handle_tpr_access(X86CPU *cpu)
2859 CPUState *cs = CPU(cpu);
2860 struct kvm_run *run = cs->kvm_run;
2862 apic_handle_tpr_access_report(cpu->apic_state, run->tpr_access.rip,
2863 run->tpr_access.is_write ? TPR_ACCESS_WRITE
2864 : TPR_ACCESS_READ);
2865 return 1;
2868 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
2870 static const uint8_t int3 = 0xcc;
2872 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) ||
2873 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&int3, 1, 1)) {
2874 return -EINVAL;
2876 return 0;
2879 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
2881 uint8_t int3;
2883 if (cpu_memory_rw_debug(cs, bp->pc, &int3, 1, 0) || int3 != 0xcc ||
2884 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
2885 return -EINVAL;
2887 return 0;
2890 static struct {
2891 target_ulong addr;
2892 int len;
2893 int type;
2894 } hw_breakpoint[4];
2896 static int nb_hw_breakpoint;
2898 static int find_hw_breakpoint(target_ulong addr, int len, int type)
2900 int n;
2902 for (n = 0; n < nb_hw_breakpoint; n++) {
2903 if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
2904 (hw_breakpoint[n].len == len || len == -1)) {
2905 return n;
2908 return -1;
2911 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
2912 target_ulong len, int type)
2914 switch (type) {
2915 case GDB_BREAKPOINT_HW:
2916 len = 1;
2917 break;
2918 case GDB_WATCHPOINT_WRITE:
2919 case GDB_WATCHPOINT_ACCESS:
2920 switch (len) {
2921 case 1:
2922 break;
2923 case 2:
2924 case 4:
2925 case 8:
2926 if (addr & (len - 1)) {
2927 return -EINVAL;
2929 break;
2930 default:
2931 return -EINVAL;
2933 break;
2934 default:
2935 return -ENOSYS;
2938 if (nb_hw_breakpoint == 4) {
2939 return -ENOBUFS;
2941 if (find_hw_breakpoint(addr, len, type) >= 0) {
2942 return -EEXIST;
2944 hw_breakpoint[nb_hw_breakpoint].addr = addr;
2945 hw_breakpoint[nb_hw_breakpoint].len = len;
2946 hw_breakpoint[nb_hw_breakpoint].type = type;
2947 nb_hw_breakpoint++;
2949 return 0;
2952 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
2953 target_ulong len, int type)
2955 int n;
2957 n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
2958 if (n < 0) {
2959 return -ENOENT;
2961 nb_hw_breakpoint--;
2962 hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint];
2964 return 0;
2967 void kvm_arch_remove_all_hw_breakpoints(void)
2969 nb_hw_breakpoint = 0;
2972 static CPUWatchpoint hw_watchpoint;
2974 static int kvm_handle_debug(X86CPU *cpu,
2975 struct kvm_debug_exit_arch *arch_info)
2977 CPUState *cs = CPU(cpu);
2978 CPUX86State *env = &cpu->env;
2979 int ret = 0;
2980 int n;
2982 if (arch_info->exception == 1) {
2983 if (arch_info->dr6 & (1 << 14)) {
2984 if (cs->singlestep_enabled) {
2985 ret = EXCP_DEBUG;
2987 } else {
2988 for (n = 0; n < 4; n++) {
2989 if (arch_info->dr6 & (1 << n)) {
2990 switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
2991 case 0x0:
2992 ret = EXCP_DEBUG;
2993 break;
2994 case 0x1:
2995 ret = EXCP_DEBUG;
2996 cs->watchpoint_hit = &hw_watchpoint;
2997 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
2998 hw_watchpoint.flags = BP_MEM_WRITE;
2999 break;
3000 case 0x3:
3001 ret = EXCP_DEBUG;
3002 cs->watchpoint_hit = &hw_watchpoint;
3003 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
3004 hw_watchpoint.flags = BP_MEM_ACCESS;
3005 break;
3010 } else if (kvm_find_sw_breakpoint(cs, arch_info->pc)) {
3011 ret = EXCP_DEBUG;
3013 if (ret == 0) {
3014 cpu_synchronize_state(cs);
3015 assert(env->exception_injected == -1);
3017 /* pass to guest */
3018 env->exception_injected = arch_info->exception;
3019 env->has_error_code = 0;
3022 return ret;
3025 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
3027 const uint8_t type_code[] = {
3028 [GDB_BREAKPOINT_HW] = 0x0,
3029 [GDB_WATCHPOINT_WRITE] = 0x1,
3030 [GDB_WATCHPOINT_ACCESS] = 0x3
3032 const uint8_t len_code[] = {
3033 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
3035 int n;
3037 if (kvm_sw_breakpoints_active(cpu)) {
3038 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
3040 if (nb_hw_breakpoint > 0) {
3041 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
3042 dbg->arch.debugreg[7] = 0x0600;
3043 for (n = 0; n < nb_hw_breakpoint; n++) {
3044 dbg->arch.debugreg[n] = hw_breakpoint[n].addr;
3045 dbg->arch.debugreg[7] |= (2 << (n * 2)) |
3046 (type_code[hw_breakpoint[n].type] << (16 + n*4)) |
3047 ((uint32_t)len_code[hw_breakpoint[n].len] << (18 + n*4));
3052 static bool host_supports_vmx(void)
3054 uint32_t ecx, unused;
3056 host_cpuid(1, 0, &unused, &unused, &ecx, &unused);
3057 return ecx & CPUID_EXT_VMX;
3060 #define VMX_INVALID_GUEST_STATE 0x80000021
3062 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
3064 X86CPU *cpu = X86_CPU(cs);
3065 uint64_t code;
3066 int ret;
3068 switch (run->exit_reason) {
3069 case KVM_EXIT_HLT:
3070 DPRINTF("handle_hlt\n");
3071 qemu_mutex_lock_iothread();
3072 ret = kvm_handle_halt(cpu);
3073 qemu_mutex_unlock_iothread();
3074 break;
3075 case KVM_EXIT_SET_TPR:
3076 ret = 0;
3077 break;
3078 case KVM_EXIT_TPR_ACCESS:
3079 qemu_mutex_lock_iothread();
3080 ret = kvm_handle_tpr_access(cpu);
3081 qemu_mutex_unlock_iothread();
3082 break;
3083 case KVM_EXIT_FAIL_ENTRY:
3084 code = run->fail_entry.hardware_entry_failure_reason;
3085 fprintf(stderr, "KVM: entry failed, hardware error 0x%" PRIx64 "\n",
3086 code);
3087 if (host_supports_vmx() && code == VMX_INVALID_GUEST_STATE) {
3088 fprintf(stderr,
3089 "\nIf you're running a guest on an Intel machine without "
3090 "unrestricted mode\n"
3091 "support, the failure can be most likely due to the guest "
3092 "entering an invalid\n"
3093 "state for Intel VT. For example, the guest maybe running "
3094 "in big real mode\n"
3095 "which is not supported on less recent Intel processors."
3096 "\n\n");
3098 ret = -1;
3099 break;
3100 case KVM_EXIT_EXCEPTION:
3101 fprintf(stderr, "KVM: exception %d exit (error code 0x%x)\n",
3102 run->ex.exception, run->ex.error_code);
3103 ret = -1;
3104 break;
3105 case KVM_EXIT_DEBUG:
3106 DPRINTF("kvm_exit_debug\n");
3107 qemu_mutex_lock_iothread();
3108 ret = kvm_handle_debug(cpu, &run->debug.arch);
3109 qemu_mutex_unlock_iothread();
3110 break;
3111 case KVM_EXIT_HYPERV:
3112 ret = kvm_hv_handle_exit(cpu, &run->hyperv);
3113 break;
3114 case KVM_EXIT_IOAPIC_EOI:
3115 ioapic_eoi_broadcast(run->eoi.vector);
3116 ret = 0;
3117 break;
3118 default:
3119 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
3120 ret = -1;
3121 break;
3124 return ret;
3127 bool kvm_arch_stop_on_emulation_error(CPUState *cs)
3129 X86CPU *cpu = X86_CPU(cs);
3130 CPUX86State *env = &cpu->env;
3132 kvm_cpu_synchronize_state(cs);
3133 return !(env->cr[0] & CR0_PE_MASK) ||
3134 ((env->segs[R_CS].selector & 3) != 3);
3137 void kvm_arch_init_irq_routing(KVMState *s)
3139 if (!kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
3140 /* If kernel can't do irq routing, interrupt source
3141 * override 0->2 cannot be set up as required by HPET.
3142 * So we have to disable it.
3144 no_hpet = 1;
3146 /* We know at this point that we're using the in-kernel
3147 * irqchip, so we can use irqfds, and on x86 we know
3148 * we can use msi via irqfd and GSI routing.
3150 kvm_msi_via_irqfd_allowed = true;
3151 kvm_gsi_routing_allowed = true;
3153 if (kvm_irqchip_is_split()) {
3154 int i;
3156 /* If the ioapic is in QEMU and the lapics are in KVM, reserve
3157 MSI routes for signaling interrupts to the local apics. */
3158 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
3159 struct MSIMessage msg = { 0x0, 0x0 };
3160 if (kvm_irqchip_add_msi_route(s, msg, NULL) < 0) {
3161 error_report("Could not enable split IRQ mode.");
3162 exit(1);
3168 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
3170 int ret;
3171 if (machine_kernel_irqchip_split(ms)) {
3172 ret = kvm_vm_enable_cap(s, KVM_CAP_SPLIT_IRQCHIP, 0, 24);
3173 if (ret) {
3174 error_report("Could not enable split irqchip mode: %s\n",
3175 strerror(-ret));
3176 exit(1);
3177 } else {
3178 DPRINTF("Enabled KVM_CAP_SPLIT_IRQCHIP\n");
3179 kvm_split_irqchip = true;
3180 return 1;
3182 } else {
3183 return 0;
3187 /* Classic KVM device assignment interface. Will remain x86 only. */
3188 int kvm_device_pci_assign(KVMState *s, PCIHostDeviceAddress *dev_addr,
3189 uint32_t flags, uint32_t *dev_id)
3191 struct kvm_assigned_pci_dev dev_data = {
3192 .segnr = dev_addr->domain,
3193 .busnr = dev_addr->bus,
3194 .devfn = PCI_DEVFN(dev_addr->slot, dev_addr->function),
3195 .flags = flags,
3197 int ret;
3199 dev_data.assigned_dev_id =
3200 (dev_addr->domain << 16) | (dev_addr->bus << 8) | dev_data.devfn;
3202 ret = kvm_vm_ioctl(s, KVM_ASSIGN_PCI_DEVICE, &dev_data);
3203 if (ret < 0) {
3204 return ret;
3207 *dev_id = dev_data.assigned_dev_id;
3209 return 0;
3212 int kvm_device_pci_deassign(KVMState *s, uint32_t dev_id)
3214 struct kvm_assigned_pci_dev dev_data = {
3215 .assigned_dev_id = dev_id,
3218 return kvm_vm_ioctl(s, KVM_DEASSIGN_PCI_DEVICE, &dev_data);
3221 static int kvm_assign_irq_internal(KVMState *s, uint32_t dev_id,
3222 uint32_t irq_type, uint32_t guest_irq)
3224 struct kvm_assigned_irq assigned_irq = {
3225 .assigned_dev_id = dev_id,
3226 .guest_irq = guest_irq,
3227 .flags = irq_type,
3230 if (kvm_check_extension(s, KVM_CAP_ASSIGN_DEV_IRQ)) {
3231 return kvm_vm_ioctl(s, KVM_ASSIGN_DEV_IRQ, &assigned_irq);
3232 } else {
3233 return kvm_vm_ioctl(s, KVM_ASSIGN_IRQ, &assigned_irq);
3237 int kvm_device_intx_assign(KVMState *s, uint32_t dev_id, bool use_host_msi,
3238 uint32_t guest_irq)
3240 uint32_t irq_type = KVM_DEV_IRQ_GUEST_INTX |
3241 (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX);
3243 return kvm_assign_irq_internal(s, dev_id, irq_type, guest_irq);
3246 int kvm_device_intx_set_mask(KVMState *s, uint32_t dev_id, bool masked)
3248 struct kvm_assigned_pci_dev dev_data = {
3249 .assigned_dev_id = dev_id,
3250 .flags = masked ? KVM_DEV_ASSIGN_MASK_INTX : 0,
3253 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_INTX_MASK, &dev_data);
3256 static int kvm_deassign_irq_internal(KVMState *s, uint32_t dev_id,
3257 uint32_t type)
3259 struct kvm_assigned_irq assigned_irq = {
3260 .assigned_dev_id = dev_id,
3261 .flags = type,
3264 return kvm_vm_ioctl(s, KVM_DEASSIGN_DEV_IRQ, &assigned_irq);
3267 int kvm_device_intx_deassign(KVMState *s, uint32_t dev_id, bool use_host_msi)
3269 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_INTX |
3270 (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX));
3273 int kvm_device_msi_assign(KVMState *s, uint32_t dev_id, int virq)
3275 return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSI |
3276 KVM_DEV_IRQ_GUEST_MSI, virq);
3279 int kvm_device_msi_deassign(KVMState *s, uint32_t dev_id)
3281 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSI |
3282 KVM_DEV_IRQ_HOST_MSI);
3285 bool kvm_device_msix_supported(KVMState *s)
3287 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
3288 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
3289 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, NULL) == -EFAULT;
3292 int kvm_device_msix_init_vectors(KVMState *s, uint32_t dev_id,
3293 uint32_t nr_vectors)
3295 struct kvm_assigned_msix_nr msix_nr = {
3296 .assigned_dev_id = dev_id,
3297 .entry_nr = nr_vectors,
3300 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, &msix_nr);
3303 int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector,
3304 int virq)
3306 struct kvm_assigned_msix_entry msix_entry = {
3307 .assigned_dev_id = dev_id,
3308 .gsi = virq,
3309 .entry = vector,
3312 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_ENTRY, &msix_entry);
3315 int kvm_device_msix_assign(KVMState *s, uint32_t dev_id)
3317 return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSIX |
3318 KVM_DEV_IRQ_GUEST_MSIX, 0);
3321 int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)
3323 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSIX |
3324 KVM_DEV_IRQ_HOST_MSIX);
3327 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
3328 uint64_t address, uint32_t data, PCIDevice *dev)
3330 return 0;
3333 int kvm_arch_msi_data_to_gsi(uint32_t data)
3335 abort();