ui/cocoa.m: Add Machine menu with pause and resume menu items
[qemu/ar7.git] / target-i386 / kvm.c
blob5a236e3103acd2e45e4bb452a4c552c1c1daa23f
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 <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/mman.h>
18 #include <sys/utsname.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 #include "qemu-common.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/kvm.h"
26 #include "kvm_i386.h"
27 #include "cpu.h"
28 #include "exec/gdbstub.h"
29 #include "qemu/host-utils.h"
30 #include "qemu/config-file.h"
31 #include "hw/i386/pc.h"
32 #include "hw/i386/apic.h"
33 #include "hw/i386/apic_internal.h"
34 #include "hw/i386/apic-msidef.h"
35 #include "exec/ioport.h"
36 #include <asm/hyperv.h>
37 #include "hw/pci/pci.h"
38 #include "migration/migration.h"
39 #include "qapi/qmp/qerror.h"
40 #include "exec/memattrs.h"
42 //#define DEBUG_KVM
44 #ifdef DEBUG_KVM
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define DPRINTF(fmt, ...) \
49 do { } while (0)
50 #endif
52 #define MSR_KVM_WALL_CLOCK 0x11
53 #define MSR_KVM_SYSTEM_TIME 0x12
55 #ifndef BUS_MCEERR_AR
56 #define BUS_MCEERR_AR 4
57 #endif
58 #ifndef BUS_MCEERR_AO
59 #define BUS_MCEERR_AO 5
60 #endif
62 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
63 KVM_CAP_INFO(SET_TSS_ADDR),
64 KVM_CAP_INFO(EXT_CPUID),
65 KVM_CAP_INFO(MP_STATE),
66 KVM_CAP_LAST_INFO
69 static bool has_msr_star;
70 static bool has_msr_hsave_pa;
71 static bool has_msr_tsc_adjust;
72 static bool has_msr_tsc_deadline;
73 static bool has_msr_feature_control;
74 static bool has_msr_async_pf_en;
75 static bool has_msr_pv_eoi_en;
76 static bool has_msr_misc_enable;
77 static bool has_msr_bndcfgs;
78 static bool has_msr_kvm_steal_time;
79 static int lm_capable_kernel;
80 static bool has_msr_hv_hypercall;
81 static bool has_msr_hv_vapic;
82 static bool has_msr_hv_tsc;
83 static bool has_msr_mtrr;
84 static bool has_msr_xss;
86 static bool has_msr_architectural_pmu;
87 static uint32_t num_architectural_pmu_counters;
89 bool kvm_allows_irq0_override(void)
91 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
94 static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
96 struct kvm_cpuid2 *cpuid;
97 int r, size;
99 size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
100 cpuid = g_malloc0(size);
101 cpuid->nent = max;
102 r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid);
103 if (r == 0 && cpuid->nent >= max) {
104 r = -E2BIG;
106 if (r < 0) {
107 if (r == -E2BIG) {
108 g_free(cpuid);
109 return NULL;
110 } else {
111 fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
112 strerror(-r));
113 exit(1);
116 return cpuid;
119 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
120 * for all entries.
122 static struct kvm_cpuid2 *get_supported_cpuid(KVMState *s)
124 struct kvm_cpuid2 *cpuid;
125 int max = 1;
126 while ((cpuid = try_get_cpuid(s, max)) == NULL) {
127 max *= 2;
129 return cpuid;
132 static const struct kvm_para_features {
133 int cap;
134 int feature;
135 } para_features[] = {
136 { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
137 { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
138 { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
139 { KVM_CAP_ASYNC_PF, KVM_FEATURE_ASYNC_PF },
142 static int get_para_features(KVMState *s)
144 int i, features = 0;
146 for (i = 0; i < ARRAY_SIZE(para_features); i++) {
147 if (kvm_check_extension(s, para_features[i].cap)) {
148 features |= (1 << para_features[i].feature);
152 return features;
156 /* Returns the value for a specific register on the cpuid entry
158 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg)
160 uint32_t ret = 0;
161 switch (reg) {
162 case R_EAX:
163 ret = entry->eax;
164 break;
165 case R_EBX:
166 ret = entry->ebx;
167 break;
168 case R_ECX:
169 ret = entry->ecx;
170 break;
171 case R_EDX:
172 ret = entry->edx;
173 break;
175 return ret;
178 /* Find matching entry for function/index on kvm_cpuid2 struct
180 static struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid,
181 uint32_t function,
182 uint32_t index)
184 int i;
185 for (i = 0; i < cpuid->nent; ++i) {
186 if (cpuid->entries[i].function == function &&
187 cpuid->entries[i].index == index) {
188 return &cpuid->entries[i];
191 /* not found: */
192 return NULL;
195 uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
196 uint32_t index, int reg)
198 struct kvm_cpuid2 *cpuid;
199 uint32_t ret = 0;
200 uint32_t cpuid_1_edx;
201 bool found = false;
203 cpuid = get_supported_cpuid(s);
205 struct kvm_cpuid_entry2 *entry = cpuid_find_entry(cpuid, function, index);
206 if (entry) {
207 found = true;
208 ret = cpuid_entry_get_reg(entry, reg);
211 /* Fixups for the data returned by KVM, below */
213 if (function == 1 && reg == R_EDX) {
214 /* KVM before 2.6.30 misreports the following features */
215 ret |= CPUID_MTRR | CPUID_PAT | CPUID_MCE | CPUID_MCA;
216 } else if (function == 1 && reg == R_ECX) {
217 /* We can set the hypervisor flag, even if KVM does not return it on
218 * GET_SUPPORTED_CPUID
220 ret |= CPUID_EXT_HYPERVISOR;
221 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
222 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
223 * and the irqchip is in the kernel.
225 if (kvm_irqchip_in_kernel() &&
226 kvm_check_extension(s, KVM_CAP_TSC_DEADLINE_TIMER)) {
227 ret |= CPUID_EXT_TSC_DEADLINE_TIMER;
230 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
231 * without the in-kernel irqchip
233 if (!kvm_irqchip_in_kernel()) {
234 ret &= ~CPUID_EXT_X2APIC;
236 } else if (function == 0x80000001 && reg == R_EDX) {
237 /* On Intel, kvm returns cpuid according to the Intel spec,
238 * so add missing bits according to the AMD spec:
240 cpuid_1_edx = kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
241 ret |= cpuid_1_edx & CPUID_EXT2_AMD_ALIASES;
244 g_free(cpuid);
246 /* fallback for older kernels */
247 if ((function == KVM_CPUID_FEATURES) && !found) {
248 ret = get_para_features(s);
251 return ret;
254 typedef struct HWPoisonPage {
255 ram_addr_t ram_addr;
256 QLIST_ENTRY(HWPoisonPage) list;
257 } HWPoisonPage;
259 static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
260 QLIST_HEAD_INITIALIZER(hwpoison_page_list);
262 static void kvm_unpoison_all(void *param)
264 HWPoisonPage *page, *next_page;
266 QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
267 QLIST_REMOVE(page, list);
268 qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
269 g_free(page);
273 static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
275 HWPoisonPage *page;
277 QLIST_FOREACH(page, &hwpoison_page_list, list) {
278 if (page->ram_addr == ram_addr) {
279 return;
282 page = g_new(HWPoisonPage, 1);
283 page->ram_addr = ram_addr;
284 QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
287 static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap,
288 int *max_banks)
290 int r;
292 r = kvm_check_extension(s, KVM_CAP_MCE);
293 if (r > 0) {
294 *max_banks = r;
295 return kvm_ioctl(s, KVM_X86_GET_MCE_CAP_SUPPORTED, mce_cap);
297 return -ENOSYS;
300 static void kvm_mce_inject(X86CPU *cpu, hwaddr paddr, int code)
302 CPUX86State *env = &cpu->env;
303 uint64_t status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN |
304 MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S;
305 uint64_t mcg_status = MCG_STATUS_MCIP;
307 if (code == BUS_MCEERR_AR) {
308 status |= MCI_STATUS_AR | 0x134;
309 mcg_status |= MCG_STATUS_EIPV;
310 } else {
311 status |= 0xc0;
312 mcg_status |= MCG_STATUS_RIPV;
314 cpu_x86_inject_mce(NULL, cpu, 9, status, mcg_status, paddr,
315 (MCM_ADDR_PHYS << 6) | 0xc,
316 cpu_x86_support_mca_broadcast(env) ?
317 MCE_INJECT_BROADCAST : 0);
320 static void hardware_memory_error(void)
322 fprintf(stderr, "Hardware memory error!\n");
323 exit(1);
326 int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
328 X86CPU *cpu = X86_CPU(c);
329 CPUX86State *env = &cpu->env;
330 ram_addr_t ram_addr;
331 hwaddr paddr;
333 if ((env->mcg_cap & MCG_SER_P) && addr
334 && (code == BUS_MCEERR_AR || code == BUS_MCEERR_AO)) {
335 if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL ||
336 !kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
337 fprintf(stderr, "Hardware memory error for memory used by "
338 "QEMU itself instead of guest system!\n");
339 /* Hope we are lucky for AO MCE */
340 if (code == BUS_MCEERR_AO) {
341 return 0;
342 } else {
343 hardware_memory_error();
346 kvm_hwpoison_page_add(ram_addr);
347 kvm_mce_inject(cpu, paddr, code);
348 } else {
349 if (code == BUS_MCEERR_AO) {
350 return 0;
351 } else if (code == BUS_MCEERR_AR) {
352 hardware_memory_error();
353 } else {
354 return 1;
357 return 0;
360 int kvm_arch_on_sigbus(int code, void *addr)
362 X86CPU *cpu = X86_CPU(first_cpu);
364 if ((cpu->env.mcg_cap & MCG_SER_P) && addr && code == BUS_MCEERR_AO) {
365 ram_addr_t ram_addr;
366 hwaddr paddr;
368 /* Hope we are lucky for AO MCE */
369 if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL ||
370 !kvm_physical_memory_addr_from_host(first_cpu->kvm_state,
371 addr, &paddr)) {
372 fprintf(stderr, "Hardware memory error for memory used by "
373 "QEMU itself instead of guest system!: %p\n", addr);
374 return 0;
376 kvm_hwpoison_page_add(ram_addr);
377 kvm_mce_inject(X86_CPU(first_cpu), paddr, code);
378 } else {
379 if (code == BUS_MCEERR_AO) {
380 return 0;
381 } else if (code == BUS_MCEERR_AR) {
382 hardware_memory_error();
383 } else {
384 return 1;
387 return 0;
390 static int kvm_inject_mce_oldstyle(X86CPU *cpu)
392 CPUX86State *env = &cpu->env;
394 if (!kvm_has_vcpu_events() && env->exception_injected == EXCP12_MCHK) {
395 unsigned int bank, bank_num = env->mcg_cap & 0xff;
396 struct kvm_x86_mce mce;
398 env->exception_injected = -1;
401 * There must be at least one bank in use if an MCE is pending.
402 * Find it and use its values for the event injection.
404 for (bank = 0; bank < bank_num; bank++) {
405 if (env->mce_banks[bank * 4 + 1] & MCI_STATUS_VAL) {
406 break;
409 assert(bank < bank_num);
411 mce.bank = bank;
412 mce.status = env->mce_banks[bank * 4 + 1];
413 mce.mcg_status = env->mcg_status;
414 mce.addr = env->mce_banks[bank * 4 + 2];
415 mce.misc = env->mce_banks[bank * 4 + 3];
417 return kvm_vcpu_ioctl(CPU(cpu), KVM_X86_SET_MCE, &mce);
419 return 0;
422 static void cpu_update_state(void *opaque, int running, RunState state)
424 CPUX86State *env = opaque;
426 if (running) {
427 env->tsc_valid = false;
431 unsigned long kvm_arch_vcpu_id(CPUState *cs)
433 X86CPU *cpu = X86_CPU(cs);
434 return cpu->apic_id;
437 #ifndef KVM_CPUID_SIGNATURE_NEXT
438 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
439 #endif
441 static bool hyperv_hypercall_available(X86CPU *cpu)
443 return cpu->hyperv_vapic ||
444 (cpu->hyperv_spinlock_attempts != HYPERV_SPINLOCK_NEVER_RETRY);
447 static bool hyperv_enabled(X86CPU *cpu)
449 CPUState *cs = CPU(cpu);
450 return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 &&
451 (hyperv_hypercall_available(cpu) ||
452 cpu->hyperv_time ||
453 cpu->hyperv_relaxed_timing);
456 static Error *invtsc_mig_blocker;
458 #define KVM_MAX_CPUID_ENTRIES 100
460 int kvm_arch_init_vcpu(CPUState *cs)
462 struct {
463 struct kvm_cpuid2 cpuid;
464 struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES];
465 } QEMU_PACKED cpuid_data;
466 X86CPU *cpu = X86_CPU(cs);
467 CPUX86State *env = &cpu->env;
468 uint32_t limit, i, j, cpuid_i;
469 uint32_t unused;
470 struct kvm_cpuid_entry2 *c;
471 uint32_t signature[3];
472 int kvm_base = KVM_CPUID_SIGNATURE;
473 int r;
475 memset(&cpuid_data, 0, sizeof(cpuid_data));
477 cpuid_i = 0;
479 /* Paravirtualization CPUIDs */
480 if (hyperv_enabled(cpu)) {
481 c = &cpuid_data.entries[cpuid_i++];
482 c->function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
483 memcpy(signature, "Microsoft Hv", 12);
484 c->eax = HYPERV_CPUID_MIN;
485 c->ebx = signature[0];
486 c->ecx = signature[1];
487 c->edx = signature[2];
489 c = &cpuid_data.entries[cpuid_i++];
490 c->function = HYPERV_CPUID_INTERFACE;
491 memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
492 c->eax = signature[0];
493 c->ebx = 0;
494 c->ecx = 0;
495 c->edx = 0;
497 c = &cpuid_data.entries[cpuid_i++];
498 c->function = HYPERV_CPUID_VERSION;
499 c->eax = 0x00001bbc;
500 c->ebx = 0x00060001;
502 c = &cpuid_data.entries[cpuid_i++];
503 c->function = HYPERV_CPUID_FEATURES;
504 if (cpu->hyperv_relaxed_timing) {
505 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
507 if (cpu->hyperv_vapic) {
508 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
509 c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
510 has_msr_hv_vapic = true;
512 if (cpu->hyperv_time &&
513 kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
514 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
515 c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
516 c->eax |= 0x200;
517 has_msr_hv_tsc = true;
519 c = &cpuid_data.entries[cpuid_i++];
520 c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
521 if (cpu->hyperv_relaxed_timing) {
522 c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
524 if (has_msr_hv_vapic) {
525 c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
527 c->ebx = cpu->hyperv_spinlock_attempts;
529 c = &cpuid_data.entries[cpuid_i++];
530 c->function = HYPERV_CPUID_IMPLEMENT_LIMITS;
531 c->eax = 0x40;
532 c->ebx = 0x40;
534 kvm_base = KVM_CPUID_SIGNATURE_NEXT;
535 has_msr_hv_hypercall = true;
538 if (cpu->expose_kvm) {
539 memcpy(signature, "KVMKVMKVM\0\0\0", 12);
540 c = &cpuid_data.entries[cpuid_i++];
541 c->function = KVM_CPUID_SIGNATURE | kvm_base;
542 c->eax = KVM_CPUID_FEATURES | kvm_base;
543 c->ebx = signature[0];
544 c->ecx = signature[1];
545 c->edx = signature[2];
547 c = &cpuid_data.entries[cpuid_i++];
548 c->function = KVM_CPUID_FEATURES | kvm_base;
549 c->eax = env->features[FEAT_KVM];
551 has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF);
553 has_msr_pv_eoi_en = c->eax & (1 << KVM_FEATURE_PV_EOI);
555 has_msr_kvm_steal_time = c->eax & (1 << KVM_FEATURE_STEAL_TIME);
558 cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
560 for (i = 0; i <= limit; i++) {
561 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
562 fprintf(stderr, "unsupported level value: 0x%x\n", limit);
563 abort();
565 c = &cpuid_data.entries[cpuid_i++];
567 switch (i) {
568 case 2: {
569 /* Keep reading function 2 till all the input is received */
570 int times;
572 c->function = i;
573 c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC |
574 KVM_CPUID_FLAG_STATE_READ_NEXT;
575 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
576 times = c->eax & 0xff;
578 for (j = 1; j < times; ++j) {
579 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
580 fprintf(stderr, "cpuid_data is full, no space for "
581 "cpuid(eax:2):eax & 0xf = 0x%x\n", times);
582 abort();
584 c = &cpuid_data.entries[cpuid_i++];
585 c->function = i;
586 c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC;
587 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
589 break;
591 case 4:
592 case 0xb:
593 case 0xd:
594 for (j = 0; ; j++) {
595 if (i == 0xd && j == 64) {
596 break;
598 c->function = i;
599 c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
600 c->index = j;
601 cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx);
603 if (i == 4 && c->eax == 0) {
604 break;
606 if (i == 0xb && !(c->ecx & 0xff00)) {
607 break;
609 if (i == 0xd && c->eax == 0) {
610 continue;
612 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
613 fprintf(stderr, "cpuid_data is full, no space for "
614 "cpuid(eax:0x%x,ecx:0x%x)\n", i, j);
615 abort();
617 c = &cpuid_data.entries[cpuid_i++];
619 break;
620 default:
621 c->function = i;
622 c->flags = 0;
623 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
624 break;
628 if (limit >= 0x0a) {
629 uint32_t ver;
631 cpu_x86_cpuid(env, 0x0a, 0, &ver, &unused, &unused, &unused);
632 if ((ver & 0xff) > 0) {
633 has_msr_architectural_pmu = true;
634 num_architectural_pmu_counters = (ver & 0xff00) >> 8;
636 /* Shouldn't be more than 32, since that's the number of bits
637 * available in EBX to tell us _which_ counters are available.
638 * Play it safe.
640 if (num_architectural_pmu_counters > MAX_GP_COUNTERS) {
641 num_architectural_pmu_counters = MAX_GP_COUNTERS;
646 cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused);
648 for (i = 0x80000000; i <= limit; i++) {
649 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
650 fprintf(stderr, "unsupported xlevel value: 0x%x\n", limit);
651 abort();
653 c = &cpuid_data.entries[cpuid_i++];
655 c->function = i;
656 c->flags = 0;
657 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
660 /* Call Centaur's CPUID instructions they are supported. */
661 if (env->cpuid_xlevel2 > 0) {
662 cpu_x86_cpuid(env, 0xC0000000, 0, &limit, &unused, &unused, &unused);
664 for (i = 0xC0000000; i <= limit; i++) {
665 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
666 fprintf(stderr, "unsupported xlevel2 value: 0x%x\n", limit);
667 abort();
669 c = &cpuid_data.entries[cpuid_i++];
671 c->function = i;
672 c->flags = 0;
673 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
677 cpuid_data.cpuid.nent = cpuid_i;
679 if (((env->cpuid_version >> 8)&0xF) >= 6
680 && (env->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
681 (CPUID_MCE | CPUID_MCA)
682 && kvm_check_extension(cs->kvm_state, KVM_CAP_MCE) > 0) {
683 uint64_t mcg_cap;
684 int banks;
685 int ret;
687 ret = kvm_get_mce_cap_supported(cs->kvm_state, &mcg_cap, &banks);
688 if (ret < 0) {
689 fprintf(stderr, "kvm_get_mce_cap_supported: %s", strerror(-ret));
690 return ret;
693 if (banks > MCE_BANKS_DEF) {
694 banks = MCE_BANKS_DEF;
696 mcg_cap &= MCE_CAP_DEF;
697 mcg_cap |= banks;
698 ret = kvm_vcpu_ioctl(cs, KVM_X86_SETUP_MCE, &mcg_cap);
699 if (ret < 0) {
700 fprintf(stderr, "KVM_X86_SETUP_MCE: %s", strerror(-ret));
701 return ret;
704 env->mcg_cap = mcg_cap;
707 qemu_add_vm_change_state_handler(cpu_update_state, env);
709 c = cpuid_find_entry(&cpuid_data.cpuid, 1, 0);
710 if (c) {
711 has_msr_feature_control = !!(c->ecx & CPUID_EXT_VMX) ||
712 !!(c->ecx & CPUID_EXT_SMX);
715 c = cpuid_find_entry(&cpuid_data.cpuid, 0x80000007, 0);
716 if (c && (c->edx & 1<<8) && invtsc_mig_blocker == NULL) {
717 /* for migration */
718 error_setg(&invtsc_mig_blocker,
719 "State blocked by non-migratable CPU device"
720 " (invtsc flag)");
721 migrate_add_blocker(invtsc_mig_blocker);
722 /* for savevm */
723 vmstate_x86_cpu.unmigratable = 1;
726 cpuid_data.cpuid.padding = 0;
727 r = kvm_vcpu_ioctl(cs, KVM_SET_CPUID2, &cpuid_data);
728 if (r) {
729 return r;
732 r = kvm_check_extension(cs->kvm_state, KVM_CAP_TSC_CONTROL);
733 if (r && env->tsc_khz) {
734 r = kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz);
735 if (r < 0) {
736 fprintf(stderr, "KVM_SET_TSC_KHZ failed\n");
737 return r;
741 if (kvm_has_xsave()) {
742 env->kvm_xsave_buf = qemu_memalign(4096, sizeof(struct kvm_xsave));
745 if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
746 has_msr_mtrr = true;
749 return 0;
752 void kvm_arch_reset_vcpu(X86CPU *cpu)
754 CPUX86State *env = &cpu->env;
756 env->exception_injected = -1;
757 env->interrupt_injected = -1;
758 env->xcr0 = 1;
759 if (kvm_irqchip_in_kernel()) {
760 env->mp_state = cpu_is_bsp(cpu) ? KVM_MP_STATE_RUNNABLE :
761 KVM_MP_STATE_UNINITIALIZED;
762 } else {
763 env->mp_state = KVM_MP_STATE_RUNNABLE;
767 void kvm_arch_do_init_vcpu(X86CPU *cpu)
769 CPUX86State *env = &cpu->env;
771 /* APs get directly into wait-for-SIPI state. */
772 if (env->mp_state == KVM_MP_STATE_UNINITIALIZED) {
773 env->mp_state = KVM_MP_STATE_INIT_RECEIVED;
777 static int kvm_get_supported_msrs(KVMState *s)
779 static int kvm_supported_msrs;
780 int ret = 0;
782 /* first time */
783 if (kvm_supported_msrs == 0) {
784 struct kvm_msr_list msr_list, *kvm_msr_list;
786 kvm_supported_msrs = -1;
788 /* Obtain MSR list from KVM. These are the MSRs that we must
789 * save/restore */
790 msr_list.nmsrs = 0;
791 ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, &msr_list);
792 if (ret < 0 && ret != -E2BIG) {
793 return ret;
795 /* Old kernel modules had a bug and could write beyond the provided
796 memory. Allocate at least a safe amount of 1K. */
797 kvm_msr_list = g_malloc0(MAX(1024, sizeof(msr_list) +
798 msr_list.nmsrs *
799 sizeof(msr_list.indices[0])));
801 kvm_msr_list->nmsrs = msr_list.nmsrs;
802 ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, kvm_msr_list);
803 if (ret >= 0) {
804 int i;
806 for (i = 0; i < kvm_msr_list->nmsrs; i++) {
807 if (kvm_msr_list->indices[i] == MSR_STAR) {
808 has_msr_star = true;
809 continue;
811 if (kvm_msr_list->indices[i] == MSR_VM_HSAVE_PA) {
812 has_msr_hsave_pa = true;
813 continue;
815 if (kvm_msr_list->indices[i] == MSR_TSC_ADJUST) {
816 has_msr_tsc_adjust = true;
817 continue;
819 if (kvm_msr_list->indices[i] == MSR_IA32_TSCDEADLINE) {
820 has_msr_tsc_deadline = true;
821 continue;
823 if (kvm_msr_list->indices[i] == MSR_IA32_MISC_ENABLE) {
824 has_msr_misc_enable = true;
825 continue;
827 if (kvm_msr_list->indices[i] == MSR_IA32_BNDCFGS) {
828 has_msr_bndcfgs = true;
829 continue;
831 if (kvm_msr_list->indices[i] == MSR_IA32_XSS) {
832 has_msr_xss = true;
833 continue;
838 g_free(kvm_msr_list);
841 return ret;
844 int kvm_arch_init(MachineState *ms, KVMState *s)
846 uint64_t identity_base = 0xfffbc000;
847 uint64_t shadow_mem;
848 int ret;
849 struct utsname utsname;
851 ret = kvm_get_supported_msrs(s);
852 if (ret < 0) {
853 return ret;
856 uname(&utsname);
857 lm_capable_kernel = strcmp(utsname.machine, "x86_64") == 0;
860 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
861 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
862 * Since these must be part of guest physical memory, we need to allocate
863 * them, both by setting their start addresses in the kernel and by
864 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
866 * Older KVM versions may not support setting the identity map base. In
867 * that case we need to stick with the default, i.e. a 256K maximum BIOS
868 * size.
870 if (kvm_check_extension(s, KVM_CAP_SET_IDENTITY_MAP_ADDR)) {
871 /* Allows up to 16M BIOSes. */
872 identity_base = 0xfeffc000;
874 ret = kvm_vm_ioctl(s, KVM_SET_IDENTITY_MAP_ADDR, &identity_base);
875 if (ret < 0) {
876 return ret;
880 /* Set TSS base one page after EPT identity map. */
881 ret = kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, identity_base + 0x1000);
882 if (ret < 0) {
883 return ret;
886 /* Tell fw_cfg to notify the BIOS to reserve the range. */
887 ret = e820_add_entry(identity_base, 0x4000, E820_RESERVED);
888 if (ret < 0) {
889 fprintf(stderr, "e820_add_entry() table is full\n");
890 return ret;
892 qemu_register_reset(kvm_unpoison_all, NULL);
894 shadow_mem = machine_kvm_shadow_mem(ms);
895 if (shadow_mem != -1) {
896 shadow_mem /= 4096;
897 ret = kvm_vm_ioctl(s, KVM_SET_NR_MMU_PAGES, shadow_mem);
898 if (ret < 0) {
899 return ret;
902 return 0;
905 static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
907 lhs->selector = rhs->selector;
908 lhs->base = rhs->base;
909 lhs->limit = rhs->limit;
910 lhs->type = 3;
911 lhs->present = 1;
912 lhs->dpl = 3;
913 lhs->db = 0;
914 lhs->s = 1;
915 lhs->l = 0;
916 lhs->g = 0;
917 lhs->avl = 0;
918 lhs->unusable = 0;
921 static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
923 unsigned flags = rhs->flags;
924 lhs->selector = rhs->selector;
925 lhs->base = rhs->base;
926 lhs->limit = rhs->limit;
927 lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
928 lhs->present = (flags & DESC_P_MASK) != 0;
929 lhs->dpl = (flags >> DESC_DPL_SHIFT) & 3;
930 lhs->db = (flags >> DESC_B_SHIFT) & 1;
931 lhs->s = (flags & DESC_S_MASK) != 0;
932 lhs->l = (flags >> DESC_L_SHIFT) & 1;
933 lhs->g = (flags & DESC_G_MASK) != 0;
934 lhs->avl = (flags & DESC_AVL_MASK) != 0;
935 lhs->unusable = 0;
936 lhs->padding = 0;
939 static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
941 lhs->selector = rhs->selector;
942 lhs->base = rhs->base;
943 lhs->limit = rhs->limit;
944 lhs->flags = (rhs->type << DESC_TYPE_SHIFT) |
945 (rhs->present * DESC_P_MASK) |
946 (rhs->dpl << DESC_DPL_SHIFT) |
947 (rhs->db << DESC_B_SHIFT) |
948 (rhs->s * DESC_S_MASK) |
949 (rhs->l << DESC_L_SHIFT) |
950 (rhs->g * DESC_G_MASK) |
951 (rhs->avl * DESC_AVL_MASK);
954 static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
956 if (set) {
957 *kvm_reg = *qemu_reg;
958 } else {
959 *qemu_reg = *kvm_reg;
963 static int kvm_getput_regs(X86CPU *cpu, int set)
965 CPUX86State *env = &cpu->env;
966 struct kvm_regs regs;
967 int ret = 0;
969 if (!set) {
970 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_REGS, &regs);
971 if (ret < 0) {
972 return ret;
976 kvm_getput_reg(&regs.rax, &env->regs[R_EAX], set);
977 kvm_getput_reg(&regs.rbx, &env->regs[R_EBX], set);
978 kvm_getput_reg(&regs.rcx, &env->regs[R_ECX], set);
979 kvm_getput_reg(&regs.rdx, &env->regs[R_EDX], set);
980 kvm_getput_reg(&regs.rsi, &env->regs[R_ESI], set);
981 kvm_getput_reg(&regs.rdi, &env->regs[R_EDI], set);
982 kvm_getput_reg(&regs.rsp, &env->regs[R_ESP], set);
983 kvm_getput_reg(&regs.rbp, &env->regs[R_EBP], set);
984 #ifdef TARGET_X86_64
985 kvm_getput_reg(&regs.r8, &env->regs[8], set);
986 kvm_getput_reg(&regs.r9, &env->regs[9], set);
987 kvm_getput_reg(&regs.r10, &env->regs[10], set);
988 kvm_getput_reg(&regs.r11, &env->regs[11], set);
989 kvm_getput_reg(&regs.r12, &env->regs[12], set);
990 kvm_getput_reg(&regs.r13, &env->regs[13], set);
991 kvm_getput_reg(&regs.r14, &env->regs[14], set);
992 kvm_getput_reg(&regs.r15, &env->regs[15], set);
993 #endif
995 kvm_getput_reg(&regs.rflags, &env->eflags, set);
996 kvm_getput_reg(&regs.rip, &env->eip, set);
998 if (set) {
999 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_REGS, &regs);
1002 return ret;
1005 static int kvm_put_fpu(X86CPU *cpu)
1007 CPUX86State *env = &cpu->env;
1008 struct kvm_fpu fpu;
1009 int i;
1011 memset(&fpu, 0, sizeof fpu);
1012 fpu.fsw = env->fpus & ~(7 << 11);
1013 fpu.fsw |= (env->fpstt & 7) << 11;
1014 fpu.fcw = env->fpuc;
1015 fpu.last_opcode = env->fpop;
1016 fpu.last_ip = env->fpip;
1017 fpu.last_dp = env->fpdp;
1018 for (i = 0; i < 8; ++i) {
1019 fpu.ftwx |= (!env->fptags[i]) << i;
1021 memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
1022 for (i = 0; i < CPU_NB_REGS; i++) {
1023 stq_p(&fpu.xmm[i][0], env->xmm_regs[i].XMM_Q(0));
1024 stq_p(&fpu.xmm[i][8], env->xmm_regs[i].XMM_Q(1));
1026 fpu.mxcsr = env->mxcsr;
1028 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_FPU, &fpu);
1031 #define XSAVE_FCW_FSW 0
1032 #define XSAVE_FTW_FOP 1
1033 #define XSAVE_CWD_RIP 2
1034 #define XSAVE_CWD_RDP 4
1035 #define XSAVE_MXCSR 6
1036 #define XSAVE_ST_SPACE 8
1037 #define XSAVE_XMM_SPACE 40
1038 #define XSAVE_XSTATE_BV 128
1039 #define XSAVE_YMMH_SPACE 144
1040 #define XSAVE_BNDREGS 240
1041 #define XSAVE_BNDCSR 256
1042 #define XSAVE_OPMASK 272
1043 #define XSAVE_ZMM_Hi256 288
1044 #define XSAVE_Hi16_ZMM 416
1046 static int kvm_put_xsave(X86CPU *cpu)
1048 CPUX86State *env = &cpu->env;
1049 struct kvm_xsave* xsave = env->kvm_xsave_buf;
1050 uint16_t cwd, swd, twd;
1051 uint8_t *xmm, *ymmh, *zmmh;
1052 int i, r;
1054 if (!kvm_has_xsave()) {
1055 return kvm_put_fpu(cpu);
1058 memset(xsave, 0, sizeof(struct kvm_xsave));
1059 twd = 0;
1060 swd = env->fpus & ~(7 << 11);
1061 swd |= (env->fpstt & 7) << 11;
1062 cwd = env->fpuc;
1063 for (i = 0; i < 8; ++i) {
1064 twd |= (!env->fptags[i]) << i;
1066 xsave->region[XSAVE_FCW_FSW] = (uint32_t)(swd << 16) + cwd;
1067 xsave->region[XSAVE_FTW_FOP] = (uint32_t)(env->fpop << 16) + twd;
1068 memcpy(&xsave->region[XSAVE_CWD_RIP], &env->fpip, sizeof(env->fpip));
1069 memcpy(&xsave->region[XSAVE_CWD_RDP], &env->fpdp, sizeof(env->fpdp));
1070 memcpy(&xsave->region[XSAVE_ST_SPACE], env->fpregs,
1071 sizeof env->fpregs);
1072 xsave->region[XSAVE_MXCSR] = env->mxcsr;
1073 *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv;
1074 memcpy(&xsave->region[XSAVE_BNDREGS], env->bnd_regs,
1075 sizeof env->bnd_regs);
1076 memcpy(&xsave->region[XSAVE_BNDCSR], &env->bndcs_regs,
1077 sizeof(env->bndcs_regs));
1078 memcpy(&xsave->region[XSAVE_OPMASK], env->opmask_regs,
1079 sizeof env->opmask_regs);
1081 xmm = (uint8_t *)&xsave->region[XSAVE_XMM_SPACE];
1082 ymmh = (uint8_t *)&xsave->region[XSAVE_YMMH_SPACE];
1083 zmmh = (uint8_t *)&xsave->region[XSAVE_ZMM_Hi256];
1084 for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) {
1085 stq_p(xmm, env->xmm_regs[i].XMM_Q(0));
1086 stq_p(xmm+8, env->xmm_regs[i].XMM_Q(1));
1087 stq_p(ymmh, env->xmm_regs[i].XMM_Q(2));
1088 stq_p(ymmh+8, env->xmm_regs[i].XMM_Q(3));
1089 stq_p(zmmh, env->xmm_regs[i].XMM_Q(4));
1090 stq_p(zmmh+8, env->xmm_regs[i].XMM_Q(5));
1091 stq_p(zmmh+16, env->xmm_regs[i].XMM_Q(6));
1092 stq_p(zmmh+24, env->xmm_regs[i].XMM_Q(7));
1095 #ifdef TARGET_X86_64
1096 memcpy(&xsave->region[XSAVE_Hi16_ZMM], &env->xmm_regs[16],
1097 16 * sizeof env->xmm_regs[16]);
1098 #endif
1099 r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave);
1100 return r;
1103 static int kvm_put_xcrs(X86CPU *cpu)
1105 CPUX86State *env = &cpu->env;
1106 struct kvm_xcrs xcrs = {};
1108 if (!kvm_has_xcrs()) {
1109 return 0;
1112 xcrs.nr_xcrs = 1;
1113 xcrs.flags = 0;
1114 xcrs.xcrs[0].xcr = 0;
1115 xcrs.xcrs[0].value = env->xcr0;
1116 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XCRS, &xcrs);
1119 static int kvm_put_sregs(X86CPU *cpu)
1121 CPUX86State *env = &cpu->env;
1122 struct kvm_sregs sregs;
1124 memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));
1125 if (env->interrupt_injected >= 0) {
1126 sregs.interrupt_bitmap[env->interrupt_injected / 64] |=
1127 (uint64_t)1 << (env->interrupt_injected % 64);
1130 if ((env->eflags & VM_MASK)) {
1131 set_v8086_seg(&sregs.cs, &env->segs[R_CS]);
1132 set_v8086_seg(&sregs.ds, &env->segs[R_DS]);
1133 set_v8086_seg(&sregs.es, &env->segs[R_ES]);
1134 set_v8086_seg(&sregs.fs, &env->segs[R_FS]);
1135 set_v8086_seg(&sregs.gs, &env->segs[R_GS]);
1136 set_v8086_seg(&sregs.ss, &env->segs[R_SS]);
1137 } else {
1138 set_seg(&sregs.cs, &env->segs[R_CS]);
1139 set_seg(&sregs.ds, &env->segs[R_DS]);
1140 set_seg(&sregs.es, &env->segs[R_ES]);
1141 set_seg(&sregs.fs, &env->segs[R_FS]);
1142 set_seg(&sregs.gs, &env->segs[R_GS]);
1143 set_seg(&sregs.ss, &env->segs[R_SS]);
1146 set_seg(&sregs.tr, &env->tr);
1147 set_seg(&sregs.ldt, &env->ldt);
1149 sregs.idt.limit = env->idt.limit;
1150 sregs.idt.base = env->idt.base;
1151 memset(sregs.idt.padding, 0, sizeof sregs.idt.padding);
1152 sregs.gdt.limit = env->gdt.limit;
1153 sregs.gdt.base = env->gdt.base;
1154 memset(sregs.gdt.padding, 0, sizeof sregs.gdt.padding);
1156 sregs.cr0 = env->cr[0];
1157 sregs.cr2 = env->cr[2];
1158 sregs.cr3 = env->cr[3];
1159 sregs.cr4 = env->cr[4];
1161 sregs.cr8 = cpu_get_apic_tpr(cpu->apic_state);
1162 sregs.apic_base = cpu_get_apic_base(cpu->apic_state);
1164 sregs.efer = env->efer;
1166 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
1169 static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
1170 uint32_t index, uint64_t value)
1172 entry->index = index;
1173 entry->reserved = 0;
1174 entry->data = value;
1177 static int kvm_put_tscdeadline_msr(X86CPU *cpu)
1179 CPUX86State *env = &cpu->env;
1180 struct {
1181 struct kvm_msrs info;
1182 struct kvm_msr_entry entries[1];
1183 } msr_data;
1184 struct kvm_msr_entry *msrs = msr_data.entries;
1186 if (!has_msr_tsc_deadline) {
1187 return 0;
1190 kvm_msr_entry_set(&msrs[0], MSR_IA32_TSCDEADLINE, env->tsc_deadline);
1192 msr_data.info = (struct kvm_msrs) {
1193 .nmsrs = 1,
1196 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1200 * Provide a separate write service for the feature control MSR in order to
1201 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1202 * before writing any other state because forcibly leaving nested mode
1203 * invalidates the VCPU state.
1205 static int kvm_put_msr_feature_control(X86CPU *cpu)
1207 struct {
1208 struct kvm_msrs info;
1209 struct kvm_msr_entry entry;
1210 } msr_data;
1212 kvm_msr_entry_set(&msr_data.entry, MSR_IA32_FEATURE_CONTROL,
1213 cpu->env.msr_ia32_feature_control);
1215 msr_data.info = (struct kvm_msrs) {
1216 .nmsrs = 1,
1219 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1222 static int kvm_put_msrs(X86CPU *cpu, int level)
1224 CPUX86State *env = &cpu->env;
1225 struct {
1226 struct kvm_msrs info;
1227 struct kvm_msr_entry entries[150];
1228 } msr_data;
1229 struct kvm_msr_entry *msrs = msr_data.entries;
1230 int n = 0, i;
1232 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
1233 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
1234 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
1235 kvm_msr_entry_set(&msrs[n++], MSR_PAT, env->pat);
1236 if (has_msr_star) {
1237 kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
1239 if (has_msr_hsave_pa) {
1240 kvm_msr_entry_set(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave);
1242 if (has_msr_tsc_adjust) {
1243 kvm_msr_entry_set(&msrs[n++], MSR_TSC_ADJUST, env->tsc_adjust);
1245 if (has_msr_misc_enable) {
1246 kvm_msr_entry_set(&msrs[n++], MSR_IA32_MISC_ENABLE,
1247 env->msr_ia32_misc_enable);
1249 if (has_msr_bndcfgs) {
1250 kvm_msr_entry_set(&msrs[n++], MSR_IA32_BNDCFGS, env->msr_bndcfgs);
1252 if (has_msr_xss) {
1253 kvm_msr_entry_set(&msrs[n++], MSR_IA32_XSS, env->xss);
1255 #ifdef TARGET_X86_64
1256 if (lm_capable_kernel) {
1257 kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
1258 kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
1259 kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
1260 kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
1262 #endif
1264 * The following MSRs have side effects on the guest or are too heavy
1265 * for normal writeback. Limit them to reset or full state updates.
1267 if (level >= KVM_PUT_RESET_STATE) {
1268 kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
1269 kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME,
1270 env->system_time_msr);
1271 kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
1272 if (has_msr_async_pf_en) {
1273 kvm_msr_entry_set(&msrs[n++], MSR_KVM_ASYNC_PF_EN,
1274 env->async_pf_en_msr);
1276 if (has_msr_pv_eoi_en) {
1277 kvm_msr_entry_set(&msrs[n++], MSR_KVM_PV_EOI_EN,
1278 env->pv_eoi_en_msr);
1280 if (has_msr_kvm_steal_time) {
1281 kvm_msr_entry_set(&msrs[n++], MSR_KVM_STEAL_TIME,
1282 env->steal_time_msr);
1284 if (has_msr_architectural_pmu) {
1285 /* Stop the counter. */
1286 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
1287 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL, 0);
1289 /* Set the counter values. */
1290 for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
1291 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR0 + i,
1292 env->msr_fixed_counters[i]);
1294 for (i = 0; i < num_architectural_pmu_counters; i++) {
1295 kvm_msr_entry_set(&msrs[n++], MSR_P6_PERFCTR0 + i,
1296 env->msr_gp_counters[i]);
1297 kvm_msr_entry_set(&msrs[n++], MSR_P6_EVNTSEL0 + i,
1298 env->msr_gp_evtsel[i]);
1300 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_STATUS,
1301 env->msr_global_status);
1302 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1303 env->msr_global_ovf_ctrl);
1305 /* Now start the PMU. */
1306 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL,
1307 env->msr_fixed_ctr_ctrl);
1308 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL,
1309 env->msr_global_ctrl);
1311 if (has_msr_hv_hypercall) {
1312 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID,
1313 env->msr_hv_guest_os_id);
1314 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL,
1315 env->msr_hv_hypercall);
1317 if (has_msr_hv_vapic) {
1318 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
1319 env->msr_hv_vapic);
1321 if (has_msr_hv_tsc) {
1322 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
1323 env->msr_hv_tsc);
1325 if (has_msr_mtrr) {
1326 kvm_msr_entry_set(&msrs[n++], MSR_MTRRdefType, env->mtrr_deftype);
1327 kvm_msr_entry_set(&msrs[n++],
1328 MSR_MTRRfix64K_00000, env->mtrr_fixed[0]);
1329 kvm_msr_entry_set(&msrs[n++],
1330 MSR_MTRRfix16K_80000, env->mtrr_fixed[1]);
1331 kvm_msr_entry_set(&msrs[n++],
1332 MSR_MTRRfix16K_A0000, env->mtrr_fixed[2]);
1333 kvm_msr_entry_set(&msrs[n++],
1334 MSR_MTRRfix4K_C0000, env->mtrr_fixed[3]);
1335 kvm_msr_entry_set(&msrs[n++],
1336 MSR_MTRRfix4K_C8000, env->mtrr_fixed[4]);
1337 kvm_msr_entry_set(&msrs[n++],
1338 MSR_MTRRfix4K_D0000, env->mtrr_fixed[5]);
1339 kvm_msr_entry_set(&msrs[n++],
1340 MSR_MTRRfix4K_D8000, env->mtrr_fixed[6]);
1341 kvm_msr_entry_set(&msrs[n++],
1342 MSR_MTRRfix4K_E0000, env->mtrr_fixed[7]);
1343 kvm_msr_entry_set(&msrs[n++],
1344 MSR_MTRRfix4K_E8000, env->mtrr_fixed[8]);
1345 kvm_msr_entry_set(&msrs[n++],
1346 MSR_MTRRfix4K_F0000, env->mtrr_fixed[9]);
1347 kvm_msr_entry_set(&msrs[n++],
1348 MSR_MTRRfix4K_F8000, env->mtrr_fixed[10]);
1349 for (i = 0; i < MSR_MTRRcap_VCNT; i++) {
1350 kvm_msr_entry_set(&msrs[n++],
1351 MSR_MTRRphysBase(i), env->mtrr_var[i].base);
1352 kvm_msr_entry_set(&msrs[n++],
1353 MSR_MTRRphysMask(i), env->mtrr_var[i].mask);
1357 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1358 * kvm_put_msr_feature_control. */
1360 if (env->mcg_cap) {
1361 int i;
1363 kvm_msr_entry_set(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
1364 kvm_msr_entry_set(&msrs[n++], MSR_MCG_CTL, env->mcg_ctl);
1365 for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) {
1366 kvm_msr_entry_set(&msrs[n++], MSR_MC0_CTL + i, env->mce_banks[i]);
1370 msr_data.info = (struct kvm_msrs) {
1371 .nmsrs = n,
1374 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1379 static int kvm_get_fpu(X86CPU *cpu)
1381 CPUX86State *env = &cpu->env;
1382 struct kvm_fpu fpu;
1383 int i, ret;
1385 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_FPU, &fpu);
1386 if (ret < 0) {
1387 return ret;
1390 env->fpstt = (fpu.fsw >> 11) & 7;
1391 env->fpus = fpu.fsw;
1392 env->fpuc = fpu.fcw;
1393 env->fpop = fpu.last_opcode;
1394 env->fpip = fpu.last_ip;
1395 env->fpdp = fpu.last_dp;
1396 for (i = 0; i < 8; ++i) {
1397 env->fptags[i] = !((fpu.ftwx >> i) & 1);
1399 memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
1400 for (i = 0; i < CPU_NB_REGS; i++) {
1401 env->xmm_regs[i].XMM_Q(0) = ldq_p(&fpu.xmm[i][0]);
1402 env->xmm_regs[i].XMM_Q(1) = ldq_p(&fpu.xmm[i][8]);
1404 env->mxcsr = fpu.mxcsr;
1406 return 0;
1409 static int kvm_get_xsave(X86CPU *cpu)
1411 CPUX86State *env = &cpu->env;
1412 struct kvm_xsave* xsave = env->kvm_xsave_buf;
1413 int ret, i;
1414 const uint8_t *xmm, *ymmh, *zmmh;
1415 uint16_t cwd, swd, twd;
1417 if (!kvm_has_xsave()) {
1418 return kvm_get_fpu(cpu);
1421 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XSAVE, xsave);
1422 if (ret < 0) {
1423 return ret;
1426 cwd = (uint16_t)xsave->region[XSAVE_FCW_FSW];
1427 swd = (uint16_t)(xsave->region[XSAVE_FCW_FSW] >> 16);
1428 twd = (uint16_t)xsave->region[XSAVE_FTW_FOP];
1429 env->fpop = (uint16_t)(xsave->region[XSAVE_FTW_FOP] >> 16);
1430 env->fpstt = (swd >> 11) & 7;
1431 env->fpus = swd;
1432 env->fpuc = cwd;
1433 for (i = 0; i < 8; ++i) {
1434 env->fptags[i] = !((twd >> i) & 1);
1436 memcpy(&env->fpip, &xsave->region[XSAVE_CWD_RIP], sizeof(env->fpip));
1437 memcpy(&env->fpdp, &xsave->region[XSAVE_CWD_RDP], sizeof(env->fpdp));
1438 env->mxcsr = xsave->region[XSAVE_MXCSR];
1439 memcpy(env->fpregs, &xsave->region[XSAVE_ST_SPACE],
1440 sizeof env->fpregs);
1441 env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV];
1442 memcpy(env->bnd_regs, &xsave->region[XSAVE_BNDREGS],
1443 sizeof env->bnd_regs);
1444 memcpy(&env->bndcs_regs, &xsave->region[XSAVE_BNDCSR],
1445 sizeof(env->bndcs_regs));
1446 memcpy(env->opmask_regs, &xsave->region[XSAVE_OPMASK],
1447 sizeof env->opmask_regs);
1449 xmm = (const uint8_t *)&xsave->region[XSAVE_XMM_SPACE];
1450 ymmh = (const uint8_t *)&xsave->region[XSAVE_YMMH_SPACE];
1451 zmmh = (const uint8_t *)&xsave->region[XSAVE_ZMM_Hi256];
1452 for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) {
1453 env->xmm_regs[i].XMM_Q(0) = ldq_p(xmm);
1454 env->xmm_regs[i].XMM_Q(1) = ldq_p(xmm+8);
1455 env->xmm_regs[i].XMM_Q(2) = ldq_p(ymmh);
1456 env->xmm_regs[i].XMM_Q(3) = ldq_p(ymmh+8);
1457 env->xmm_regs[i].XMM_Q(4) = ldq_p(zmmh);
1458 env->xmm_regs[i].XMM_Q(5) = ldq_p(zmmh+8);
1459 env->xmm_regs[i].XMM_Q(6) = ldq_p(zmmh+16);
1460 env->xmm_regs[i].XMM_Q(7) = ldq_p(zmmh+24);
1463 #ifdef TARGET_X86_64
1464 memcpy(&env->xmm_regs[16], &xsave->region[XSAVE_Hi16_ZMM],
1465 16 * sizeof env->xmm_regs[16]);
1466 #endif
1467 return 0;
1470 static int kvm_get_xcrs(X86CPU *cpu)
1472 CPUX86State *env = &cpu->env;
1473 int i, ret;
1474 struct kvm_xcrs xcrs;
1476 if (!kvm_has_xcrs()) {
1477 return 0;
1480 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XCRS, &xcrs);
1481 if (ret < 0) {
1482 return ret;
1485 for (i = 0; i < xcrs.nr_xcrs; i++) {
1486 /* Only support xcr0 now */
1487 if (xcrs.xcrs[i].xcr == 0) {
1488 env->xcr0 = xcrs.xcrs[i].value;
1489 break;
1492 return 0;
1495 static int kvm_get_sregs(X86CPU *cpu)
1497 CPUX86State *env = &cpu->env;
1498 struct kvm_sregs sregs;
1499 uint32_t hflags;
1500 int bit, i, ret;
1502 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1503 if (ret < 0) {
1504 return ret;
1507 /* There can only be one pending IRQ set in the bitmap at a time, so try
1508 to find it and save its number instead (-1 for none). */
1509 env->interrupt_injected = -1;
1510 for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) {
1511 if (sregs.interrupt_bitmap[i]) {
1512 bit = ctz64(sregs.interrupt_bitmap[i]);
1513 env->interrupt_injected = i * 64 + bit;
1514 break;
1518 get_seg(&env->segs[R_CS], &sregs.cs);
1519 get_seg(&env->segs[R_DS], &sregs.ds);
1520 get_seg(&env->segs[R_ES], &sregs.es);
1521 get_seg(&env->segs[R_FS], &sregs.fs);
1522 get_seg(&env->segs[R_GS], &sregs.gs);
1523 get_seg(&env->segs[R_SS], &sregs.ss);
1525 get_seg(&env->tr, &sregs.tr);
1526 get_seg(&env->ldt, &sregs.ldt);
1528 env->idt.limit = sregs.idt.limit;
1529 env->idt.base = sregs.idt.base;
1530 env->gdt.limit = sregs.gdt.limit;
1531 env->gdt.base = sregs.gdt.base;
1533 env->cr[0] = sregs.cr0;
1534 env->cr[2] = sregs.cr2;
1535 env->cr[3] = sregs.cr3;
1536 env->cr[4] = sregs.cr4;
1538 env->efer = sregs.efer;
1540 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1542 #define HFLAG_COPY_MASK \
1543 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1544 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1545 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1546 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1548 hflags = (env->segs[R_SS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;
1549 hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
1550 hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
1551 (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK);
1552 hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK));
1553 hflags |= (env->cr[4] & CR4_OSFXSR_MASK) <<
1554 (HF_OSFXSR_SHIFT - CR4_OSFXSR_SHIFT);
1556 if (env->efer & MSR_EFER_LMA) {
1557 hflags |= HF_LMA_MASK;
1560 if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
1561 hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
1562 } else {
1563 hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >>
1564 (DESC_B_SHIFT - HF_CS32_SHIFT);
1565 hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >>
1566 (DESC_B_SHIFT - HF_SS32_SHIFT);
1567 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK) ||
1568 !(hflags & HF_CS32_MASK)) {
1569 hflags |= HF_ADDSEG_MASK;
1570 } else {
1571 hflags |= ((env->segs[R_DS].base | env->segs[R_ES].base |
1572 env->segs[R_SS].base) != 0) << HF_ADDSEG_SHIFT;
1575 env->hflags = (env->hflags & HFLAG_COPY_MASK) | hflags;
1577 return 0;
1580 static int kvm_get_msrs(X86CPU *cpu)
1582 CPUX86State *env = &cpu->env;
1583 struct {
1584 struct kvm_msrs info;
1585 struct kvm_msr_entry entries[150];
1586 } msr_data;
1587 struct kvm_msr_entry *msrs = msr_data.entries;
1588 int ret, i, n;
1590 n = 0;
1591 msrs[n++].index = MSR_IA32_SYSENTER_CS;
1592 msrs[n++].index = MSR_IA32_SYSENTER_ESP;
1593 msrs[n++].index = MSR_IA32_SYSENTER_EIP;
1594 msrs[n++].index = MSR_PAT;
1595 if (has_msr_star) {
1596 msrs[n++].index = MSR_STAR;
1598 if (has_msr_hsave_pa) {
1599 msrs[n++].index = MSR_VM_HSAVE_PA;
1601 if (has_msr_tsc_adjust) {
1602 msrs[n++].index = MSR_TSC_ADJUST;
1604 if (has_msr_tsc_deadline) {
1605 msrs[n++].index = MSR_IA32_TSCDEADLINE;
1607 if (has_msr_misc_enable) {
1608 msrs[n++].index = MSR_IA32_MISC_ENABLE;
1610 if (has_msr_feature_control) {
1611 msrs[n++].index = MSR_IA32_FEATURE_CONTROL;
1613 if (has_msr_bndcfgs) {
1614 msrs[n++].index = MSR_IA32_BNDCFGS;
1616 if (has_msr_xss) {
1617 msrs[n++].index = MSR_IA32_XSS;
1621 if (!env->tsc_valid) {
1622 msrs[n++].index = MSR_IA32_TSC;
1623 env->tsc_valid = !runstate_is_running();
1626 #ifdef TARGET_X86_64
1627 if (lm_capable_kernel) {
1628 msrs[n++].index = MSR_CSTAR;
1629 msrs[n++].index = MSR_KERNELGSBASE;
1630 msrs[n++].index = MSR_FMASK;
1631 msrs[n++].index = MSR_LSTAR;
1633 #endif
1634 msrs[n++].index = MSR_KVM_SYSTEM_TIME;
1635 msrs[n++].index = MSR_KVM_WALL_CLOCK;
1636 if (has_msr_async_pf_en) {
1637 msrs[n++].index = MSR_KVM_ASYNC_PF_EN;
1639 if (has_msr_pv_eoi_en) {
1640 msrs[n++].index = MSR_KVM_PV_EOI_EN;
1642 if (has_msr_kvm_steal_time) {
1643 msrs[n++].index = MSR_KVM_STEAL_TIME;
1645 if (has_msr_architectural_pmu) {
1646 msrs[n++].index = MSR_CORE_PERF_FIXED_CTR_CTRL;
1647 msrs[n++].index = MSR_CORE_PERF_GLOBAL_CTRL;
1648 msrs[n++].index = MSR_CORE_PERF_GLOBAL_STATUS;
1649 msrs[n++].index = MSR_CORE_PERF_GLOBAL_OVF_CTRL;
1650 for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
1651 msrs[n++].index = MSR_CORE_PERF_FIXED_CTR0 + i;
1653 for (i = 0; i < num_architectural_pmu_counters; i++) {
1654 msrs[n++].index = MSR_P6_PERFCTR0 + i;
1655 msrs[n++].index = MSR_P6_EVNTSEL0 + i;
1659 if (env->mcg_cap) {
1660 msrs[n++].index = MSR_MCG_STATUS;
1661 msrs[n++].index = MSR_MCG_CTL;
1662 for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) {
1663 msrs[n++].index = MSR_MC0_CTL + i;
1667 if (has_msr_hv_hypercall) {
1668 msrs[n++].index = HV_X64_MSR_HYPERCALL;
1669 msrs[n++].index = HV_X64_MSR_GUEST_OS_ID;
1671 if (has_msr_hv_vapic) {
1672 msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
1674 if (has_msr_hv_tsc) {
1675 msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
1677 if (has_msr_mtrr) {
1678 msrs[n++].index = MSR_MTRRdefType;
1679 msrs[n++].index = MSR_MTRRfix64K_00000;
1680 msrs[n++].index = MSR_MTRRfix16K_80000;
1681 msrs[n++].index = MSR_MTRRfix16K_A0000;
1682 msrs[n++].index = MSR_MTRRfix4K_C0000;
1683 msrs[n++].index = MSR_MTRRfix4K_C8000;
1684 msrs[n++].index = MSR_MTRRfix4K_D0000;
1685 msrs[n++].index = MSR_MTRRfix4K_D8000;
1686 msrs[n++].index = MSR_MTRRfix4K_E0000;
1687 msrs[n++].index = MSR_MTRRfix4K_E8000;
1688 msrs[n++].index = MSR_MTRRfix4K_F0000;
1689 msrs[n++].index = MSR_MTRRfix4K_F8000;
1690 for (i = 0; i < MSR_MTRRcap_VCNT; i++) {
1691 msrs[n++].index = MSR_MTRRphysBase(i);
1692 msrs[n++].index = MSR_MTRRphysMask(i);
1696 msr_data.info = (struct kvm_msrs) {
1697 .nmsrs = n,
1700 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
1701 if (ret < 0) {
1702 return ret;
1705 for (i = 0; i < ret; i++) {
1706 uint32_t index = msrs[i].index;
1707 switch (index) {
1708 case MSR_IA32_SYSENTER_CS:
1709 env->sysenter_cs = msrs[i].data;
1710 break;
1711 case MSR_IA32_SYSENTER_ESP:
1712 env->sysenter_esp = msrs[i].data;
1713 break;
1714 case MSR_IA32_SYSENTER_EIP:
1715 env->sysenter_eip = msrs[i].data;
1716 break;
1717 case MSR_PAT:
1718 env->pat = msrs[i].data;
1719 break;
1720 case MSR_STAR:
1721 env->star = msrs[i].data;
1722 break;
1723 #ifdef TARGET_X86_64
1724 case MSR_CSTAR:
1725 env->cstar = msrs[i].data;
1726 break;
1727 case MSR_KERNELGSBASE:
1728 env->kernelgsbase = msrs[i].data;
1729 break;
1730 case MSR_FMASK:
1731 env->fmask = msrs[i].data;
1732 break;
1733 case MSR_LSTAR:
1734 env->lstar = msrs[i].data;
1735 break;
1736 #endif
1737 case MSR_IA32_TSC:
1738 env->tsc = msrs[i].data;
1739 break;
1740 case MSR_TSC_ADJUST:
1741 env->tsc_adjust = msrs[i].data;
1742 break;
1743 case MSR_IA32_TSCDEADLINE:
1744 env->tsc_deadline = msrs[i].data;
1745 break;
1746 case MSR_VM_HSAVE_PA:
1747 env->vm_hsave = msrs[i].data;
1748 break;
1749 case MSR_KVM_SYSTEM_TIME:
1750 env->system_time_msr = msrs[i].data;
1751 break;
1752 case MSR_KVM_WALL_CLOCK:
1753 env->wall_clock_msr = msrs[i].data;
1754 break;
1755 case MSR_MCG_STATUS:
1756 env->mcg_status = msrs[i].data;
1757 break;
1758 case MSR_MCG_CTL:
1759 env->mcg_ctl = msrs[i].data;
1760 break;
1761 case MSR_IA32_MISC_ENABLE:
1762 env->msr_ia32_misc_enable = msrs[i].data;
1763 break;
1764 case MSR_IA32_FEATURE_CONTROL:
1765 env->msr_ia32_feature_control = msrs[i].data;
1766 break;
1767 case MSR_IA32_BNDCFGS:
1768 env->msr_bndcfgs = msrs[i].data;
1769 break;
1770 case MSR_IA32_XSS:
1771 env->xss = msrs[i].data;
1772 break;
1773 default:
1774 if (msrs[i].index >= MSR_MC0_CTL &&
1775 msrs[i].index < MSR_MC0_CTL + (env->mcg_cap & 0xff) * 4) {
1776 env->mce_banks[msrs[i].index - MSR_MC0_CTL] = msrs[i].data;
1778 break;
1779 case MSR_KVM_ASYNC_PF_EN:
1780 env->async_pf_en_msr = msrs[i].data;
1781 break;
1782 case MSR_KVM_PV_EOI_EN:
1783 env->pv_eoi_en_msr = msrs[i].data;
1784 break;
1785 case MSR_KVM_STEAL_TIME:
1786 env->steal_time_msr = msrs[i].data;
1787 break;
1788 case MSR_CORE_PERF_FIXED_CTR_CTRL:
1789 env->msr_fixed_ctr_ctrl = msrs[i].data;
1790 break;
1791 case MSR_CORE_PERF_GLOBAL_CTRL:
1792 env->msr_global_ctrl = msrs[i].data;
1793 break;
1794 case MSR_CORE_PERF_GLOBAL_STATUS:
1795 env->msr_global_status = msrs[i].data;
1796 break;
1797 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
1798 env->msr_global_ovf_ctrl = msrs[i].data;
1799 break;
1800 case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR0 + MAX_FIXED_COUNTERS - 1:
1801 env->msr_fixed_counters[index - MSR_CORE_PERF_FIXED_CTR0] = msrs[i].data;
1802 break;
1803 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR0 + MAX_GP_COUNTERS - 1:
1804 env->msr_gp_counters[index - MSR_P6_PERFCTR0] = msrs[i].data;
1805 break;
1806 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL0 + MAX_GP_COUNTERS - 1:
1807 env->msr_gp_evtsel[index - MSR_P6_EVNTSEL0] = msrs[i].data;
1808 break;
1809 case HV_X64_MSR_HYPERCALL:
1810 env->msr_hv_hypercall = msrs[i].data;
1811 break;
1812 case HV_X64_MSR_GUEST_OS_ID:
1813 env->msr_hv_guest_os_id = msrs[i].data;
1814 break;
1815 case HV_X64_MSR_APIC_ASSIST_PAGE:
1816 env->msr_hv_vapic = msrs[i].data;
1817 break;
1818 case HV_X64_MSR_REFERENCE_TSC:
1819 env->msr_hv_tsc = msrs[i].data;
1820 break;
1821 case MSR_MTRRdefType:
1822 env->mtrr_deftype = msrs[i].data;
1823 break;
1824 case MSR_MTRRfix64K_00000:
1825 env->mtrr_fixed[0] = msrs[i].data;
1826 break;
1827 case MSR_MTRRfix16K_80000:
1828 env->mtrr_fixed[1] = msrs[i].data;
1829 break;
1830 case MSR_MTRRfix16K_A0000:
1831 env->mtrr_fixed[2] = msrs[i].data;
1832 break;
1833 case MSR_MTRRfix4K_C0000:
1834 env->mtrr_fixed[3] = msrs[i].data;
1835 break;
1836 case MSR_MTRRfix4K_C8000:
1837 env->mtrr_fixed[4] = msrs[i].data;
1838 break;
1839 case MSR_MTRRfix4K_D0000:
1840 env->mtrr_fixed[5] = msrs[i].data;
1841 break;
1842 case MSR_MTRRfix4K_D8000:
1843 env->mtrr_fixed[6] = msrs[i].data;
1844 break;
1845 case MSR_MTRRfix4K_E0000:
1846 env->mtrr_fixed[7] = msrs[i].data;
1847 break;
1848 case MSR_MTRRfix4K_E8000:
1849 env->mtrr_fixed[8] = msrs[i].data;
1850 break;
1851 case MSR_MTRRfix4K_F0000:
1852 env->mtrr_fixed[9] = msrs[i].data;
1853 break;
1854 case MSR_MTRRfix4K_F8000:
1855 env->mtrr_fixed[10] = msrs[i].data;
1856 break;
1857 case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT - 1):
1858 if (index & 1) {
1859 env->mtrr_var[MSR_MTRRphysIndex(index)].mask = msrs[i].data;
1860 } else {
1861 env->mtrr_var[MSR_MTRRphysIndex(index)].base = msrs[i].data;
1863 break;
1867 return 0;
1870 static int kvm_put_mp_state(X86CPU *cpu)
1872 struct kvm_mp_state mp_state = { .mp_state = cpu->env.mp_state };
1874 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1877 static int kvm_get_mp_state(X86CPU *cpu)
1879 CPUState *cs = CPU(cpu);
1880 CPUX86State *env = &cpu->env;
1881 struct kvm_mp_state mp_state;
1882 int ret;
1884 ret = kvm_vcpu_ioctl(cs, KVM_GET_MP_STATE, &mp_state);
1885 if (ret < 0) {
1886 return ret;
1888 env->mp_state = mp_state.mp_state;
1889 if (kvm_irqchip_in_kernel()) {
1890 cs->halted = (mp_state.mp_state == KVM_MP_STATE_HALTED);
1892 return 0;
1895 static int kvm_get_apic(X86CPU *cpu)
1897 DeviceState *apic = cpu->apic_state;
1898 struct kvm_lapic_state kapic;
1899 int ret;
1901 if (apic && kvm_irqchip_in_kernel()) {
1902 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_LAPIC, &kapic);
1903 if (ret < 0) {
1904 return ret;
1907 kvm_get_apic_state(apic, &kapic);
1909 return 0;
1912 static int kvm_put_apic(X86CPU *cpu)
1914 DeviceState *apic = cpu->apic_state;
1915 struct kvm_lapic_state kapic;
1917 if (apic && kvm_irqchip_in_kernel()) {
1918 kvm_put_apic_state(apic, &kapic);
1920 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_LAPIC, &kapic);
1922 return 0;
1925 static int kvm_put_vcpu_events(X86CPU *cpu, int level)
1927 CPUX86State *env = &cpu->env;
1928 struct kvm_vcpu_events events = {};
1930 if (!kvm_has_vcpu_events()) {
1931 return 0;
1934 events.exception.injected = (env->exception_injected >= 0);
1935 events.exception.nr = env->exception_injected;
1936 events.exception.has_error_code = env->has_error_code;
1937 events.exception.error_code = env->error_code;
1938 events.exception.pad = 0;
1940 events.interrupt.injected = (env->interrupt_injected >= 0);
1941 events.interrupt.nr = env->interrupt_injected;
1942 events.interrupt.soft = env->soft_interrupt;
1944 events.nmi.injected = env->nmi_injected;
1945 events.nmi.pending = env->nmi_pending;
1946 events.nmi.masked = !!(env->hflags2 & HF2_NMI_MASK);
1947 events.nmi.pad = 0;
1949 events.sipi_vector = env->sipi_vector;
1951 events.flags = 0;
1952 if (level >= KVM_PUT_RESET_STATE) {
1953 events.flags |=
1954 KVM_VCPUEVENT_VALID_NMI_PENDING | KVM_VCPUEVENT_VALID_SIPI_VECTOR;
1957 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);
1960 static int kvm_get_vcpu_events(X86CPU *cpu)
1962 CPUX86State *env = &cpu->env;
1963 struct kvm_vcpu_events events;
1964 int ret;
1966 if (!kvm_has_vcpu_events()) {
1967 return 0;
1970 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events);
1971 if (ret < 0) {
1972 return ret;
1974 env->exception_injected =
1975 events.exception.injected ? events.exception.nr : -1;
1976 env->has_error_code = events.exception.has_error_code;
1977 env->error_code = events.exception.error_code;
1979 env->interrupt_injected =
1980 events.interrupt.injected ? events.interrupt.nr : -1;
1981 env->soft_interrupt = events.interrupt.soft;
1983 env->nmi_injected = events.nmi.injected;
1984 env->nmi_pending = events.nmi.pending;
1985 if (events.nmi.masked) {
1986 env->hflags2 |= HF2_NMI_MASK;
1987 } else {
1988 env->hflags2 &= ~HF2_NMI_MASK;
1991 env->sipi_vector = events.sipi_vector;
1993 return 0;
1996 static int kvm_guest_debug_workarounds(X86CPU *cpu)
1998 CPUState *cs = CPU(cpu);
1999 CPUX86State *env = &cpu->env;
2000 int ret = 0;
2001 unsigned long reinject_trap = 0;
2003 if (!kvm_has_vcpu_events()) {
2004 if (env->exception_injected == 1) {
2005 reinject_trap = KVM_GUESTDBG_INJECT_DB;
2006 } else if (env->exception_injected == 3) {
2007 reinject_trap = KVM_GUESTDBG_INJECT_BP;
2009 env->exception_injected = -1;
2013 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
2014 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
2015 * by updating the debug state once again if single-stepping is on.
2016 * Another reason to call kvm_update_guest_debug here is a pending debug
2017 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
2018 * reinject them via SET_GUEST_DEBUG.
2020 if (reinject_trap ||
2021 (!kvm_has_robust_singlestep() && cs->singlestep_enabled)) {
2022 ret = kvm_update_guest_debug(cs, reinject_trap);
2024 return ret;
2027 static int kvm_put_debugregs(X86CPU *cpu)
2029 CPUX86State *env = &cpu->env;
2030 struct kvm_debugregs dbgregs;
2031 int i;
2033 if (!kvm_has_debugregs()) {
2034 return 0;
2037 for (i = 0; i < 4; i++) {
2038 dbgregs.db[i] = env->dr[i];
2040 dbgregs.dr6 = env->dr[6];
2041 dbgregs.dr7 = env->dr[7];
2042 dbgregs.flags = 0;
2044 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_DEBUGREGS, &dbgregs);
2047 static int kvm_get_debugregs(X86CPU *cpu)
2049 CPUX86State *env = &cpu->env;
2050 struct kvm_debugregs dbgregs;
2051 int i, ret;
2053 if (!kvm_has_debugregs()) {
2054 return 0;
2057 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_DEBUGREGS, &dbgregs);
2058 if (ret < 0) {
2059 return ret;
2061 for (i = 0; i < 4; i++) {
2062 env->dr[i] = dbgregs.db[i];
2064 env->dr[4] = env->dr[6] = dbgregs.dr6;
2065 env->dr[5] = env->dr[7] = dbgregs.dr7;
2067 return 0;
2070 int kvm_arch_put_registers(CPUState *cpu, int level)
2072 X86CPU *x86_cpu = X86_CPU(cpu);
2073 int ret;
2075 assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu));
2077 if (level >= KVM_PUT_RESET_STATE && has_msr_feature_control) {
2078 ret = kvm_put_msr_feature_control(x86_cpu);
2079 if (ret < 0) {
2080 return ret;
2084 ret = kvm_getput_regs(x86_cpu, 1);
2085 if (ret < 0) {
2086 return ret;
2088 ret = kvm_put_xsave(x86_cpu);
2089 if (ret < 0) {
2090 return ret;
2092 ret = kvm_put_xcrs(x86_cpu);
2093 if (ret < 0) {
2094 return ret;
2096 ret = kvm_put_sregs(x86_cpu);
2097 if (ret < 0) {
2098 return ret;
2100 /* must be before kvm_put_msrs */
2101 ret = kvm_inject_mce_oldstyle(x86_cpu);
2102 if (ret < 0) {
2103 return ret;
2105 ret = kvm_put_msrs(x86_cpu, level);
2106 if (ret < 0) {
2107 return ret;
2109 if (level >= KVM_PUT_RESET_STATE) {
2110 ret = kvm_put_mp_state(x86_cpu);
2111 if (ret < 0) {
2112 return ret;
2114 ret = kvm_put_apic(x86_cpu);
2115 if (ret < 0) {
2116 return ret;
2120 ret = kvm_put_tscdeadline_msr(x86_cpu);
2121 if (ret < 0) {
2122 return ret;
2125 ret = kvm_put_vcpu_events(x86_cpu, level);
2126 if (ret < 0) {
2127 return ret;
2129 ret = kvm_put_debugregs(x86_cpu);
2130 if (ret < 0) {
2131 return ret;
2133 /* must be last */
2134 ret = kvm_guest_debug_workarounds(x86_cpu);
2135 if (ret < 0) {
2136 return ret;
2138 return 0;
2141 int kvm_arch_get_registers(CPUState *cs)
2143 X86CPU *cpu = X86_CPU(cs);
2144 int ret;
2146 assert(cpu_is_stopped(cs) || qemu_cpu_is_self(cs));
2148 ret = kvm_getput_regs(cpu, 0);
2149 if (ret < 0) {
2150 return ret;
2152 ret = kvm_get_xsave(cpu);
2153 if (ret < 0) {
2154 return ret;
2156 ret = kvm_get_xcrs(cpu);
2157 if (ret < 0) {
2158 return ret;
2160 ret = kvm_get_sregs(cpu);
2161 if (ret < 0) {
2162 return ret;
2164 ret = kvm_get_msrs(cpu);
2165 if (ret < 0) {
2166 return ret;
2168 ret = kvm_get_mp_state(cpu);
2169 if (ret < 0) {
2170 return ret;
2172 ret = kvm_get_apic(cpu);
2173 if (ret < 0) {
2174 return ret;
2176 ret = kvm_get_vcpu_events(cpu);
2177 if (ret < 0) {
2178 return ret;
2180 ret = kvm_get_debugregs(cpu);
2181 if (ret < 0) {
2182 return ret;
2184 return 0;
2187 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
2189 X86CPU *x86_cpu = X86_CPU(cpu);
2190 CPUX86State *env = &x86_cpu->env;
2191 int ret;
2193 /* Inject NMI */
2194 if (cpu->interrupt_request & CPU_INTERRUPT_NMI) {
2195 cpu->interrupt_request &= ~CPU_INTERRUPT_NMI;
2196 DPRINTF("injected NMI\n");
2197 ret = kvm_vcpu_ioctl(cpu, KVM_NMI);
2198 if (ret < 0) {
2199 fprintf(stderr, "KVM: injection failed, NMI lost (%s)\n",
2200 strerror(-ret));
2204 /* Force the VCPU out of its inner loop to process any INIT requests
2205 * or (for userspace APIC, but it is cheap to combine the checks here)
2206 * pending TPR access reports.
2208 if (cpu->interrupt_request & (CPU_INTERRUPT_INIT | CPU_INTERRUPT_TPR)) {
2209 cpu->exit_request = 1;
2212 if (!kvm_irqchip_in_kernel()) {
2213 /* Try to inject an interrupt if the guest can accept it */
2214 if (run->ready_for_interrupt_injection &&
2215 (cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
2216 (env->eflags & IF_MASK)) {
2217 int irq;
2219 cpu->interrupt_request &= ~CPU_INTERRUPT_HARD;
2220 irq = cpu_get_pic_interrupt(env);
2221 if (irq >= 0) {
2222 struct kvm_interrupt intr;
2224 intr.irq = irq;
2225 DPRINTF("injected interrupt %d\n", irq);
2226 ret = kvm_vcpu_ioctl(cpu, KVM_INTERRUPT, &intr);
2227 if (ret < 0) {
2228 fprintf(stderr,
2229 "KVM: injection failed, interrupt lost (%s)\n",
2230 strerror(-ret));
2235 /* If we have an interrupt but the guest is not ready to receive an
2236 * interrupt, request an interrupt window exit. This will
2237 * cause a return to userspace as soon as the guest is ready to
2238 * receive interrupts. */
2239 if ((cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
2240 run->request_interrupt_window = 1;
2241 } else {
2242 run->request_interrupt_window = 0;
2245 DPRINTF("setting tpr\n");
2246 run->cr8 = cpu_get_apic_tpr(x86_cpu->apic_state);
2250 MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
2252 X86CPU *x86_cpu = X86_CPU(cpu);
2253 CPUX86State *env = &x86_cpu->env;
2255 if (run->if_flag) {
2256 env->eflags |= IF_MASK;
2257 } else {
2258 env->eflags &= ~IF_MASK;
2260 cpu_set_apic_tpr(x86_cpu->apic_state, run->cr8);
2261 cpu_set_apic_base(x86_cpu->apic_state, run->apic_base);
2262 return cpu_get_mem_attrs(env);
2265 int kvm_arch_process_async_events(CPUState *cs)
2267 X86CPU *cpu = X86_CPU(cs);
2268 CPUX86State *env = &cpu->env;
2270 if (cs->interrupt_request & CPU_INTERRUPT_MCE) {
2271 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2272 assert(env->mcg_cap);
2274 cs->interrupt_request &= ~CPU_INTERRUPT_MCE;
2276 kvm_cpu_synchronize_state(cs);
2278 if (env->exception_injected == EXCP08_DBLE) {
2279 /* this means triple fault */
2280 qemu_system_reset_request();
2281 cs->exit_request = 1;
2282 return 0;
2284 env->exception_injected = EXCP12_MCHK;
2285 env->has_error_code = 0;
2287 cs->halted = 0;
2288 if (kvm_irqchip_in_kernel() && env->mp_state == KVM_MP_STATE_HALTED) {
2289 env->mp_state = KVM_MP_STATE_RUNNABLE;
2293 if (cs->interrupt_request & CPU_INTERRUPT_INIT) {
2294 kvm_cpu_synchronize_state(cs);
2295 do_cpu_init(cpu);
2298 if (kvm_irqchip_in_kernel()) {
2299 return 0;
2302 if (cs->interrupt_request & CPU_INTERRUPT_POLL) {
2303 cs->interrupt_request &= ~CPU_INTERRUPT_POLL;
2304 apic_poll_irq(cpu->apic_state);
2306 if (((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
2307 (env->eflags & IF_MASK)) ||
2308 (cs->interrupt_request & CPU_INTERRUPT_NMI)) {
2309 cs->halted = 0;
2311 if (cs->interrupt_request & CPU_INTERRUPT_SIPI) {
2312 kvm_cpu_synchronize_state(cs);
2313 do_cpu_sipi(cpu);
2315 if (cs->interrupt_request & CPU_INTERRUPT_TPR) {
2316 cs->interrupt_request &= ~CPU_INTERRUPT_TPR;
2317 kvm_cpu_synchronize_state(cs);
2318 apic_handle_tpr_access_report(cpu->apic_state, env->eip,
2319 env->tpr_access_type);
2322 return cs->halted;
2325 static int kvm_handle_halt(X86CPU *cpu)
2327 CPUState *cs = CPU(cpu);
2328 CPUX86State *env = &cpu->env;
2330 if (!((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
2331 (env->eflags & IF_MASK)) &&
2332 !(cs->interrupt_request & CPU_INTERRUPT_NMI)) {
2333 cs->halted = 1;
2334 return EXCP_HLT;
2337 return 0;
2340 static int kvm_handle_tpr_access(X86CPU *cpu)
2342 CPUState *cs = CPU(cpu);
2343 struct kvm_run *run = cs->kvm_run;
2345 apic_handle_tpr_access_report(cpu->apic_state, run->tpr_access.rip,
2346 run->tpr_access.is_write ? TPR_ACCESS_WRITE
2347 : TPR_ACCESS_READ);
2348 return 1;
2351 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
2353 static const uint8_t int3 = 0xcc;
2355 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) ||
2356 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&int3, 1, 1)) {
2357 return -EINVAL;
2359 return 0;
2362 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
2364 uint8_t int3;
2366 if (cpu_memory_rw_debug(cs, bp->pc, &int3, 1, 0) || int3 != 0xcc ||
2367 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
2368 return -EINVAL;
2370 return 0;
2373 static struct {
2374 target_ulong addr;
2375 int len;
2376 int type;
2377 } hw_breakpoint[4];
2379 static int nb_hw_breakpoint;
2381 static int find_hw_breakpoint(target_ulong addr, int len, int type)
2383 int n;
2385 for (n = 0; n < nb_hw_breakpoint; n++) {
2386 if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
2387 (hw_breakpoint[n].len == len || len == -1)) {
2388 return n;
2391 return -1;
2394 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
2395 target_ulong len, int type)
2397 switch (type) {
2398 case GDB_BREAKPOINT_HW:
2399 len = 1;
2400 break;
2401 case GDB_WATCHPOINT_WRITE:
2402 case GDB_WATCHPOINT_ACCESS:
2403 switch (len) {
2404 case 1:
2405 break;
2406 case 2:
2407 case 4:
2408 case 8:
2409 if (addr & (len - 1)) {
2410 return -EINVAL;
2412 break;
2413 default:
2414 return -EINVAL;
2416 break;
2417 default:
2418 return -ENOSYS;
2421 if (nb_hw_breakpoint == 4) {
2422 return -ENOBUFS;
2424 if (find_hw_breakpoint(addr, len, type) >= 0) {
2425 return -EEXIST;
2427 hw_breakpoint[nb_hw_breakpoint].addr = addr;
2428 hw_breakpoint[nb_hw_breakpoint].len = len;
2429 hw_breakpoint[nb_hw_breakpoint].type = type;
2430 nb_hw_breakpoint++;
2432 return 0;
2435 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
2436 target_ulong len, int type)
2438 int n;
2440 n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
2441 if (n < 0) {
2442 return -ENOENT;
2444 nb_hw_breakpoint--;
2445 hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint];
2447 return 0;
2450 void kvm_arch_remove_all_hw_breakpoints(void)
2452 nb_hw_breakpoint = 0;
2455 static CPUWatchpoint hw_watchpoint;
2457 static int kvm_handle_debug(X86CPU *cpu,
2458 struct kvm_debug_exit_arch *arch_info)
2460 CPUState *cs = CPU(cpu);
2461 CPUX86State *env = &cpu->env;
2462 int ret = 0;
2463 int n;
2465 if (arch_info->exception == 1) {
2466 if (arch_info->dr6 & (1 << 14)) {
2467 if (cs->singlestep_enabled) {
2468 ret = EXCP_DEBUG;
2470 } else {
2471 for (n = 0; n < 4; n++) {
2472 if (arch_info->dr6 & (1 << n)) {
2473 switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
2474 case 0x0:
2475 ret = EXCP_DEBUG;
2476 break;
2477 case 0x1:
2478 ret = EXCP_DEBUG;
2479 cs->watchpoint_hit = &hw_watchpoint;
2480 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
2481 hw_watchpoint.flags = BP_MEM_WRITE;
2482 break;
2483 case 0x3:
2484 ret = EXCP_DEBUG;
2485 cs->watchpoint_hit = &hw_watchpoint;
2486 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
2487 hw_watchpoint.flags = BP_MEM_ACCESS;
2488 break;
2493 } else if (kvm_find_sw_breakpoint(cs, arch_info->pc)) {
2494 ret = EXCP_DEBUG;
2496 if (ret == 0) {
2497 cpu_synchronize_state(cs);
2498 assert(env->exception_injected == -1);
2500 /* pass to guest */
2501 env->exception_injected = arch_info->exception;
2502 env->has_error_code = 0;
2505 return ret;
2508 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
2510 const uint8_t type_code[] = {
2511 [GDB_BREAKPOINT_HW] = 0x0,
2512 [GDB_WATCHPOINT_WRITE] = 0x1,
2513 [GDB_WATCHPOINT_ACCESS] = 0x3
2515 const uint8_t len_code[] = {
2516 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2518 int n;
2520 if (kvm_sw_breakpoints_active(cpu)) {
2521 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
2523 if (nb_hw_breakpoint > 0) {
2524 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
2525 dbg->arch.debugreg[7] = 0x0600;
2526 for (n = 0; n < nb_hw_breakpoint; n++) {
2527 dbg->arch.debugreg[n] = hw_breakpoint[n].addr;
2528 dbg->arch.debugreg[7] |= (2 << (n * 2)) |
2529 (type_code[hw_breakpoint[n].type] << (16 + n*4)) |
2530 ((uint32_t)len_code[hw_breakpoint[n].len] << (18 + n*4));
2535 static bool host_supports_vmx(void)
2537 uint32_t ecx, unused;
2539 host_cpuid(1, 0, &unused, &unused, &ecx, &unused);
2540 return ecx & CPUID_EXT_VMX;
2543 #define VMX_INVALID_GUEST_STATE 0x80000021
2545 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2547 X86CPU *cpu = X86_CPU(cs);
2548 uint64_t code;
2549 int ret;
2551 switch (run->exit_reason) {
2552 case KVM_EXIT_HLT:
2553 DPRINTF("handle_hlt\n");
2554 ret = kvm_handle_halt(cpu);
2555 break;
2556 case KVM_EXIT_SET_TPR:
2557 ret = 0;
2558 break;
2559 case KVM_EXIT_TPR_ACCESS:
2560 ret = kvm_handle_tpr_access(cpu);
2561 break;
2562 case KVM_EXIT_FAIL_ENTRY:
2563 code = run->fail_entry.hardware_entry_failure_reason;
2564 fprintf(stderr, "KVM: entry failed, hardware error 0x%" PRIx64 "\n",
2565 code);
2566 if (host_supports_vmx() && code == VMX_INVALID_GUEST_STATE) {
2567 fprintf(stderr,
2568 "\nIf you're running a guest on an Intel machine without "
2569 "unrestricted mode\n"
2570 "support, the failure can be most likely due to the guest "
2571 "entering an invalid\n"
2572 "state for Intel VT. For example, the guest maybe running "
2573 "in big real mode\n"
2574 "which is not supported on less recent Intel processors."
2575 "\n\n");
2577 ret = -1;
2578 break;
2579 case KVM_EXIT_EXCEPTION:
2580 fprintf(stderr, "KVM: exception %d exit (error code 0x%x)\n",
2581 run->ex.exception, run->ex.error_code);
2582 ret = -1;
2583 break;
2584 case KVM_EXIT_DEBUG:
2585 DPRINTF("kvm_exit_debug\n");
2586 ret = kvm_handle_debug(cpu, &run->debug.arch);
2587 break;
2588 default:
2589 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
2590 ret = -1;
2591 break;
2594 return ret;
2597 bool kvm_arch_stop_on_emulation_error(CPUState *cs)
2599 X86CPU *cpu = X86_CPU(cs);
2600 CPUX86State *env = &cpu->env;
2602 kvm_cpu_synchronize_state(cs);
2603 return !(env->cr[0] & CR0_PE_MASK) ||
2604 ((env->segs[R_CS].selector & 3) != 3);
2607 void kvm_arch_init_irq_routing(KVMState *s)
2609 if (!kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2610 /* If kernel can't do irq routing, interrupt source
2611 * override 0->2 cannot be set up as required by HPET.
2612 * So we have to disable it.
2614 no_hpet = 1;
2616 /* We know at this point that we're using the in-kernel
2617 * irqchip, so we can use irqfds, and on x86 we know
2618 * we can use msi via irqfd and GSI routing.
2620 kvm_msi_via_irqfd_allowed = true;
2621 kvm_gsi_routing_allowed = true;
2624 /* Classic KVM device assignment interface. Will remain x86 only. */
2625 int kvm_device_pci_assign(KVMState *s, PCIHostDeviceAddress *dev_addr,
2626 uint32_t flags, uint32_t *dev_id)
2628 struct kvm_assigned_pci_dev dev_data = {
2629 .segnr = dev_addr->domain,
2630 .busnr = dev_addr->bus,
2631 .devfn = PCI_DEVFN(dev_addr->slot, dev_addr->function),
2632 .flags = flags,
2634 int ret;
2636 dev_data.assigned_dev_id =
2637 (dev_addr->domain << 16) | (dev_addr->bus << 8) | dev_data.devfn;
2639 ret = kvm_vm_ioctl(s, KVM_ASSIGN_PCI_DEVICE, &dev_data);
2640 if (ret < 0) {
2641 return ret;
2644 *dev_id = dev_data.assigned_dev_id;
2646 return 0;
2649 int kvm_device_pci_deassign(KVMState *s, uint32_t dev_id)
2651 struct kvm_assigned_pci_dev dev_data = {
2652 .assigned_dev_id = dev_id,
2655 return kvm_vm_ioctl(s, KVM_DEASSIGN_PCI_DEVICE, &dev_data);
2658 static int kvm_assign_irq_internal(KVMState *s, uint32_t dev_id,
2659 uint32_t irq_type, uint32_t guest_irq)
2661 struct kvm_assigned_irq assigned_irq = {
2662 .assigned_dev_id = dev_id,
2663 .guest_irq = guest_irq,
2664 .flags = irq_type,
2667 if (kvm_check_extension(s, KVM_CAP_ASSIGN_DEV_IRQ)) {
2668 return kvm_vm_ioctl(s, KVM_ASSIGN_DEV_IRQ, &assigned_irq);
2669 } else {
2670 return kvm_vm_ioctl(s, KVM_ASSIGN_IRQ, &assigned_irq);
2674 int kvm_device_intx_assign(KVMState *s, uint32_t dev_id, bool use_host_msi,
2675 uint32_t guest_irq)
2677 uint32_t irq_type = KVM_DEV_IRQ_GUEST_INTX |
2678 (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX);
2680 return kvm_assign_irq_internal(s, dev_id, irq_type, guest_irq);
2683 int kvm_device_intx_set_mask(KVMState *s, uint32_t dev_id, bool masked)
2685 struct kvm_assigned_pci_dev dev_data = {
2686 .assigned_dev_id = dev_id,
2687 .flags = masked ? KVM_DEV_ASSIGN_MASK_INTX : 0,
2690 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_INTX_MASK, &dev_data);
2693 static int kvm_deassign_irq_internal(KVMState *s, uint32_t dev_id,
2694 uint32_t type)
2696 struct kvm_assigned_irq assigned_irq = {
2697 .assigned_dev_id = dev_id,
2698 .flags = type,
2701 return kvm_vm_ioctl(s, KVM_DEASSIGN_DEV_IRQ, &assigned_irq);
2704 int kvm_device_intx_deassign(KVMState *s, uint32_t dev_id, bool use_host_msi)
2706 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_INTX |
2707 (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX));
2710 int kvm_device_msi_assign(KVMState *s, uint32_t dev_id, int virq)
2712 return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSI |
2713 KVM_DEV_IRQ_GUEST_MSI, virq);
2716 int kvm_device_msi_deassign(KVMState *s, uint32_t dev_id)
2718 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSI |
2719 KVM_DEV_IRQ_HOST_MSI);
2722 bool kvm_device_msix_supported(KVMState *s)
2724 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2725 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2726 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, NULL) == -EFAULT;
2729 int kvm_device_msix_init_vectors(KVMState *s, uint32_t dev_id,
2730 uint32_t nr_vectors)
2732 struct kvm_assigned_msix_nr msix_nr = {
2733 .assigned_dev_id = dev_id,
2734 .entry_nr = nr_vectors,
2737 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, &msix_nr);
2740 int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector,
2741 int virq)
2743 struct kvm_assigned_msix_entry msix_entry = {
2744 .assigned_dev_id = dev_id,
2745 .gsi = virq,
2746 .entry = vector,
2749 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_ENTRY, &msix_entry);
2752 int kvm_device_msix_assign(KVMState *s, uint32_t dev_id)
2754 return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSIX |
2755 KVM_DEV_IRQ_GUEST_MSIX, 0);
2758 int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)
2760 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSIX |
2761 KVM_DEV_IRQ_HOST_MSIX);
2764 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2765 uint64_t address, uint32_t data)
2767 return 0;
2770 int kvm_arch_msi_data_to_gsi(uint32_t data)
2772 abort();