Merge commit '5610c3aaf4196cf714fb9cb57118187963550d1c' into upstream-merge
[qemu-kvm/markmc.git] / qemu-kvm-x86.c
blob475151d4b8571ec892f2435cca6d6dc91d24fc0d
1 /*
2 * qemu/kvm integration, x86 specific code
4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
7 */
9 #include "config.h"
10 #include "config-host.h"
12 #include <string.h>
13 #include "hw/hw.h"
14 #include "gdbstub.h"
15 #include <sys/io.h>
17 #include "qemu-kvm.h"
18 #include "libkvm.h"
19 #include <pthread.h>
20 #include <sys/utsname.h>
21 #include <linux/kvm_para.h>
22 #include <sys/ioctl.h>
24 #include "kvm.h"
25 #include "hw/pc.h"
27 #define MSR_IA32_TSC 0x10
29 static struct kvm_msr_list *kvm_msr_list;
30 extern unsigned int kvm_shadow_memory;
31 static int kvm_has_msr_star;
32 static int kvm_has_vm_hsave_pa;
34 static int lm_capable_kernel;
36 int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr)
38 #ifdef KVM_CAP_SET_TSS_ADDR
39 int r;
41 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
42 if (r > 0) {
43 r = kvm_vm_ioctl(kvm_state, KVM_SET_TSS_ADDR, addr);
44 if (r < 0) {
45 fprintf(stderr, "kvm_set_tss_addr: %m\n");
46 return r;
48 return 0;
50 #endif
51 return -ENOSYS;
54 static int kvm_init_tss(kvm_context_t kvm)
56 #ifdef KVM_CAP_SET_TSS_ADDR
57 int r;
59 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
60 if (r > 0) {
62 * this address is 3 pages before the bios, and the bios should present
63 * as unavaible memory
65 r = kvm_set_tss_addr(kvm, 0xfeffd000);
66 if (r < 0) {
67 fprintf(stderr, "kvm_init_tss: unable to set tss addr\n");
68 return r;
72 #endif
73 return 0;
76 static int kvm_set_identity_map_addr(kvm_context_t kvm, uint64_t addr)
78 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
79 int r;
81 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_SET_IDENTITY_MAP_ADDR);
82 if (r > 0) {
83 r = kvm_vm_ioctl(kvm_state, KVM_SET_IDENTITY_MAP_ADDR, &addr);
84 if (r == -1) {
85 fprintf(stderr, "kvm_set_identity_map_addr: %m\n");
86 return -errno;
88 return 0;
90 #endif
91 return -ENOSYS;
94 static int kvm_init_identity_map_page(kvm_context_t kvm)
96 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
97 int r;
99 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_SET_IDENTITY_MAP_ADDR);
100 if (r > 0) {
102 * this address is 4 pages before the bios, and the bios should present
103 * as unavaible memory
105 r = kvm_set_identity_map_addr(kvm, 0xfeffc000);
106 if (r < 0) {
107 fprintf(stderr, "kvm_init_identity_map_page: "
108 "unable to set identity mapping addr\n");
109 return r;
113 #endif
114 return 0;
117 static int kvm_create_pit(kvm_context_t kvm)
119 #ifdef KVM_CAP_PIT
120 int r;
122 kvm->pit_in_kernel = 0;
123 if (!kvm->no_pit_creation) {
124 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_PIT);
125 if (r > 0) {
126 r = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
127 if (r >= 0)
128 kvm->pit_in_kernel = 1;
129 else {
130 fprintf(stderr, "Create kernel PIC irqchip failed\n");
131 return r;
135 #endif
136 return 0;
139 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
140 void **vm_mem)
142 int r = 0;
144 r = kvm_init_tss(kvm);
145 if (r < 0)
146 return r;
148 r = kvm_init_identity_map_page(kvm);
149 if (r < 0)
150 return r;
152 r = kvm_create_pit(kvm);
153 if (r < 0)
154 return r;
156 r = kvm_init_coalesced_mmio(kvm);
157 if (r < 0)
158 return r;
160 return 0;
163 #ifdef KVM_EXIT_TPR_ACCESS
165 static int kvm_handle_tpr_access(CPUState *env)
167 struct kvm_run *run = env->kvm_run;
168 kvm_tpr_access_report(env,
169 run->tpr_access.rip,
170 run->tpr_access.is_write);
171 return 0;
175 int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic)
177 int r;
178 struct kvm_vapic_addr va = {
179 .vapic_addr = vapic,
182 r = ioctl(vcpu->fd, KVM_SET_VAPIC_ADDR, &va);
183 if (r == -1) {
184 r = -errno;
185 perror("kvm_enable_vapic");
186 return r;
188 return 0;
191 #endif
193 int kvm_arch_run(CPUState *env)
195 int r = 0;
196 struct kvm_run *run = env->kvm_run;
199 switch (run->exit_reason) {
200 #ifdef KVM_EXIT_SET_TPR
201 case KVM_EXIT_SET_TPR:
202 break;
203 #endif
204 #ifdef KVM_EXIT_TPR_ACCESS
205 case KVM_EXIT_TPR_ACCESS:
206 r = kvm_handle_tpr_access(env);
207 break;
208 #endif
209 default:
210 r = 1;
211 break;
214 return r;
217 #define MAX_ALIAS_SLOTS 4
218 static struct {
219 uint64_t start;
220 uint64_t len;
221 } kvm_aliases[MAX_ALIAS_SLOTS];
223 static int get_alias_slot(uint64_t start)
225 int i;
227 for (i=0; i<MAX_ALIAS_SLOTS; i++)
228 if (kvm_aliases[i].start == start)
229 return i;
230 return -1;
232 static int get_free_alias_slot(void)
234 int i;
236 for (i=0; i<MAX_ALIAS_SLOTS; i++)
237 if (kvm_aliases[i].len == 0)
238 return i;
239 return -1;
242 static void register_alias(int slot, uint64_t start, uint64_t len)
244 kvm_aliases[slot].start = start;
245 kvm_aliases[slot].len = len;
248 int kvm_create_memory_alias(kvm_context_t kvm,
249 uint64_t phys_start,
250 uint64_t len,
251 uint64_t target_phys)
253 struct kvm_memory_alias alias = {
254 .flags = 0,
255 .guest_phys_addr = phys_start,
256 .memory_size = len,
257 .target_phys_addr = target_phys,
259 int r;
260 int slot;
262 slot = get_alias_slot(phys_start);
263 if (slot < 0)
264 slot = get_free_alias_slot();
265 if (slot < 0)
266 return -EBUSY;
267 alias.slot = slot;
269 r = kvm_vm_ioctl(kvm_state, KVM_SET_MEMORY_ALIAS, &alias);
270 if (r == -1)
271 return -errno;
273 register_alias(slot, phys_start, len);
274 return 0;
277 int kvm_destroy_memory_alias(kvm_context_t kvm, uint64_t phys_start)
279 return kvm_create_memory_alias(kvm, phys_start, 0, 0);
282 #ifdef KVM_CAP_IRQCHIP
284 int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s)
286 int r;
287 if (!kvm_irqchip_in_kernel())
288 return 0;
289 r = ioctl(vcpu->fd, KVM_GET_LAPIC, s);
290 if (r == -1) {
291 r = -errno;
292 perror("kvm_get_lapic");
294 return r;
297 int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s)
299 int r;
300 if (!kvm_irqchip_in_kernel())
301 return 0;
302 r = ioctl(vcpu->fd, KVM_SET_LAPIC, s);
303 if (r == -1) {
304 r = -errno;
305 perror("kvm_set_lapic");
307 return r;
310 #endif
312 #ifdef KVM_CAP_PIT
314 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s)
316 if (!kvm->pit_in_kernel)
317 return 0;
318 return kvm_vm_ioctl(kvm_state, KVM_GET_PIT, s);
321 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s)
323 if (!kvm->pit_in_kernel)
324 return 0;
325 return kvm_vm_ioctl(kvm_state, KVM_SET_PIT, s);
328 #ifdef KVM_CAP_PIT_STATE2
329 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2)
331 if (!kvm->pit_in_kernel)
332 return 0;
333 return kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, ps2);
336 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2)
338 if (!kvm->pit_in_kernel)
339 return 0;
340 return kvm_vm_ioctl(kvm_state, KVM_SET_PIT2, ps2);
343 #endif
344 #endif
346 int kvm_has_pit_state2(kvm_context_t kvm)
348 int r = 0;
350 #ifdef KVM_CAP_PIT_STATE2
351 r = kvm_check_extension(kvm_state, KVM_CAP_PIT_STATE2);
352 #endif
353 return r;
356 void kvm_show_code(kvm_vcpu_context_t vcpu)
358 #define SHOW_CODE_LEN 50
359 int fd = vcpu->fd;
360 struct kvm_regs regs;
361 struct kvm_sregs sregs;
362 int r, n;
363 int back_offset;
364 unsigned char code;
365 char code_str[SHOW_CODE_LEN * 3 + 1];
366 unsigned long rip;
368 r = ioctl(fd, KVM_GET_SREGS, &sregs);
369 if (r == -1) {
370 perror("KVM_GET_SREGS");
371 return;
373 r = ioctl(fd, KVM_GET_REGS, &regs);
374 if (r == -1) {
375 perror("KVM_GET_REGS");
376 return;
378 rip = sregs.cs.base + regs.rip;
379 back_offset = regs.rip;
380 if (back_offset > 20)
381 back_offset = 20;
382 *code_str = 0;
383 for (n = -back_offset; n < SHOW_CODE_LEN-back_offset; ++n) {
384 if (n == 0)
385 strcat(code_str, " -->");
386 cpu_physical_memory_rw(rip + n, &code, 1, 1);
387 sprintf(code_str + strlen(code_str), " %02x", code);
389 fprintf(stderr, "code:%s\n", code_str);
394 * Returns available msr list. User must free.
396 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
398 struct kvm_msr_list sizer, *msrs;
399 int r;
401 sizer.nmsrs = 0;
402 r = kvm_ioctl(kvm_state, KVM_GET_MSR_INDEX_LIST, &sizer);
403 if (r < 0 && r != -E2BIG)
404 return NULL;
405 /* Old kernel modules had a bug and could write beyond the provided
406 memory. Allocate at least a safe amount of 1K. */
407 msrs = qemu_malloc(MAX(1024, sizeof(*msrs) +
408 sizer.nmsrs * sizeof(*msrs->indices)));
410 msrs->nmsrs = sizer.nmsrs;
411 r = kvm_ioctl(kvm_state, KVM_GET_MSR_INDEX_LIST, msrs);
412 if (r < 0) {
413 free(msrs);
414 errno = r;
415 return NULL;
417 return msrs;
420 int kvm_get_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
422 struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
423 int r, e;
425 kmsrs->nmsrs = n;
426 memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
427 r = ioctl(vcpu->fd, KVM_GET_MSRS, kmsrs);
428 e = errno;
429 memcpy(msrs, kmsrs->entries, n * sizeof *msrs);
430 free(kmsrs);
431 errno = e;
432 return r;
435 int kvm_set_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
437 struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
438 int r, e;
440 kmsrs->nmsrs = n;
441 memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
442 r = ioctl(vcpu->fd, KVM_SET_MSRS, kmsrs);
443 e = errno;
444 free(kmsrs);
445 errno = e;
446 return r;
449 int kvm_get_mce_cap_supported(kvm_context_t kvm, uint64_t *mce_cap,
450 int *max_banks)
452 #ifdef KVM_CAP_MCE
453 int r;
455 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_MCE);
456 if (r > 0) {
457 *max_banks = r;
458 return kvm_ioctl(kvm_state, KVM_X86_GET_MCE_CAP_SUPPORTED, mce_cap);
460 #endif
461 return -ENOSYS;
464 int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap)
466 #ifdef KVM_CAP_MCE
467 return ioctl(vcpu->fd, KVM_X86_SETUP_MCE, mcg_cap);
468 #else
469 return -ENOSYS;
470 #endif
473 int kvm_set_mce(kvm_vcpu_context_t vcpu, struct kvm_x86_mce *m)
475 #ifdef KVM_CAP_MCE
476 return ioctl(vcpu->fd, KVM_X86_SET_MCE, m);
477 #else
478 return -ENOSYS;
479 #endif
482 static void print_seg(FILE *file, const char *name, struct kvm_segment *seg)
484 fprintf(stderr,
485 "%s %04x (%08llx/%08x p %d dpl %d db %d s %d type %x l %d"
486 " g %d avl %d)\n",
487 name, seg->selector, seg->base, seg->limit, seg->present,
488 seg->dpl, seg->db, seg->s, seg->type, seg->l, seg->g,
489 seg->avl);
492 static void print_dt(FILE *file, const char *name, struct kvm_dtable *dt)
494 fprintf(stderr, "%s %llx/%x\n", name, dt->base, dt->limit);
497 void kvm_show_regs(kvm_vcpu_context_t vcpu)
499 int fd = vcpu->fd;
500 struct kvm_regs regs;
501 struct kvm_sregs sregs;
502 int r;
504 r = ioctl(fd, KVM_GET_REGS, &regs);
505 if (r == -1) {
506 perror("KVM_GET_REGS");
507 return;
509 fprintf(stderr,
510 "rax %016llx rbx %016llx rcx %016llx rdx %016llx\n"
511 "rsi %016llx rdi %016llx rsp %016llx rbp %016llx\n"
512 "r8 %016llx r9 %016llx r10 %016llx r11 %016llx\n"
513 "r12 %016llx r13 %016llx r14 %016llx r15 %016llx\n"
514 "rip %016llx rflags %08llx\n",
515 regs.rax, regs.rbx, regs.rcx, regs.rdx,
516 regs.rsi, regs.rdi, regs.rsp, regs.rbp,
517 regs.r8, regs.r9, regs.r10, regs.r11,
518 regs.r12, regs.r13, regs.r14, regs.r15,
519 regs.rip, regs.rflags);
520 r = ioctl(fd, KVM_GET_SREGS, &sregs);
521 if (r == -1) {
522 perror("KVM_GET_SREGS");
523 return;
525 print_seg(stderr, "cs", &sregs.cs);
526 print_seg(stderr, "ds", &sregs.ds);
527 print_seg(stderr, "es", &sregs.es);
528 print_seg(stderr, "ss", &sregs.ss);
529 print_seg(stderr, "fs", &sregs.fs);
530 print_seg(stderr, "gs", &sregs.gs);
531 print_seg(stderr, "tr", &sregs.tr);
532 print_seg(stderr, "ldt", &sregs.ldt);
533 print_dt(stderr, "gdt", &sregs.gdt);
534 print_dt(stderr, "idt", &sregs.idt);
535 fprintf(stderr, "cr0 %llx cr2 %llx cr3 %llx cr4 %llx cr8 %llx"
536 " efer %llx\n",
537 sregs.cr0, sregs.cr2, sregs.cr3, sregs.cr4, sregs.cr8,
538 sregs.efer);
541 static uint64_t kvm_get_apic_base(CPUState *env)
543 return env->kvm_run->apic_base;
546 static void kvm_set_cr8(CPUState *env, uint64_t cr8)
548 env->kvm_run->cr8 = cr8;
551 static __u64 kvm_get_cr8(CPUState *env)
553 return env->kvm_run->cr8;
556 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
557 struct kvm_cpuid_entry *entries)
559 struct kvm_cpuid *cpuid;
560 int r;
562 cpuid = qemu_malloc(sizeof(*cpuid) + nent * sizeof(*entries));
564 cpuid->nent = nent;
565 memcpy(cpuid->entries, entries, nent * sizeof(*entries));
566 r = ioctl(vcpu->fd, KVM_SET_CPUID, cpuid);
568 free(cpuid);
569 return r;
572 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
573 struct kvm_cpuid_entry2 *entries)
575 struct kvm_cpuid2 *cpuid;
576 int r;
578 cpuid = qemu_malloc(sizeof(*cpuid) + nent * sizeof(*entries));
580 cpuid->nent = nent;
581 memcpy(cpuid->entries, entries, nent * sizeof(*entries));
582 r = ioctl(vcpu->fd, KVM_SET_CPUID2, cpuid);
583 if (r == -1) {
584 fprintf(stderr, "kvm_setup_cpuid2: %m\n");
585 r = -errno;
587 free(cpuid);
588 return r;
591 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages)
593 #ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
594 int r;
596 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION,
597 KVM_CAP_MMU_SHADOW_CACHE_CONTROL);
598 if (r > 0) {
599 r = kvm_vm_ioctl(kvm_state, KVM_SET_NR_MMU_PAGES, nrshadow_pages);
600 if (r < 0) {
601 fprintf(stderr, "kvm_set_shadow_pages: %m\n");
602 return r;
604 return 0;
606 #endif
607 return -1;
610 int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages)
612 #ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
613 int r;
615 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION,
616 KVM_CAP_MMU_SHADOW_CACHE_CONTROL);
617 if (r > 0) {
618 *nrshadow_pages = kvm_vm_ioctl(kvm_state, KVM_GET_NR_MMU_PAGES);
619 return 0;
621 #endif
622 return -1;
625 #ifdef KVM_CAP_VAPIC
627 static int tpr_access_reporting(kvm_vcpu_context_t vcpu, int enabled)
629 int r;
630 struct kvm_tpr_access_ctl tac = {
631 .enabled = enabled,
634 r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_VAPIC);
635 if (r <= 0)
636 return -ENOSYS;
637 r = ioctl(vcpu->fd, KVM_TPR_ACCESS_REPORTING, &tac);
638 if (r == -1) {
639 r = -errno;
640 perror("KVM_TPR_ACCESS_REPORTING");
641 return r;
643 return 0;
646 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu)
648 return tpr_access_reporting(vcpu, 1);
651 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu)
653 return tpr_access_reporting(vcpu, 0);
656 #endif
658 #ifdef KVM_CAP_EXT_CPUID
660 static struct kvm_cpuid2 *try_get_cpuid(kvm_context_t kvm, int max)
662 struct kvm_cpuid2 *cpuid;
663 int r, size;
665 size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
666 cpuid = qemu_malloc(size);
667 cpuid->nent = max;
668 r = kvm_ioctl(kvm_state, KVM_GET_SUPPORTED_CPUID, cpuid);
669 if (r == 0 && cpuid->nent >= max)
670 r = -E2BIG;
671 if (r < 0) {
672 if (r == -E2BIG) {
673 free(cpuid);
674 return NULL;
675 } else {
676 fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
677 strerror(-r));
678 exit(1);
681 return cpuid;
684 #define R_EAX 0
685 #define R_ECX 1
686 #define R_EDX 2
687 #define R_EBX 3
688 #define R_ESP 4
689 #define R_EBP 5
690 #define R_ESI 6
691 #define R_EDI 7
693 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg)
695 struct kvm_cpuid2 *cpuid;
696 int i, max;
697 uint32_t ret = 0;
698 uint32_t cpuid_1_edx;
700 if (!kvm_check_extension(kvm_state, KVM_CAP_EXT_CPUID)) {
701 return -1U;
704 max = 1;
705 while ((cpuid = try_get_cpuid(kvm, max)) == NULL) {
706 max *= 2;
709 for (i = 0; i < cpuid->nent; ++i) {
710 if (cpuid->entries[i].function == function) {
711 switch (reg) {
712 case R_EAX:
713 ret = cpuid->entries[i].eax;
714 break;
715 case R_EBX:
716 ret = cpuid->entries[i].ebx;
717 break;
718 case R_ECX:
719 ret = cpuid->entries[i].ecx;
720 break;
721 case R_EDX:
722 ret = cpuid->entries[i].edx;
723 if (function == 1) {
724 /* kvm misreports the following features
726 ret |= 1 << 12; /* MTRR */
727 ret |= 1 << 16; /* PAT */
728 ret |= 1 << 7; /* MCE */
729 ret |= 1 << 14; /* MCA */
732 /* On Intel, kvm returns cpuid according to
733 * the Intel spec, so add missing bits
734 * according to the AMD spec:
736 if (function == 0x80000001) {
737 cpuid_1_edx = kvm_get_supported_cpuid(kvm, 1, R_EDX);
738 ret |= cpuid_1_edx & 0xdfeff7ff;
740 break;
745 free(cpuid);
747 return ret;
750 #else
752 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg)
754 return -1U;
757 #endif
758 int kvm_qemu_create_memory_alias(uint64_t phys_start,
759 uint64_t len,
760 uint64_t target_phys)
762 return kvm_create_memory_alias(kvm_context, phys_start, len, target_phys);
765 int kvm_qemu_destroy_memory_alias(uint64_t phys_start)
767 return kvm_destroy_memory_alias(kvm_context, phys_start);
770 int kvm_arch_qemu_create_context(void)
772 int i;
773 struct utsname utsname;
775 uname(&utsname);
776 lm_capable_kernel = strcmp(utsname.machine, "x86_64") == 0;
778 if (kvm_shadow_memory)
779 kvm_set_shadow_pages(kvm_context, kvm_shadow_memory);
781 kvm_msr_list = kvm_get_msr_list(kvm_context);
782 if (!kvm_msr_list)
783 return -1;
784 for (i = 0; i < kvm_msr_list->nmsrs; ++i) {
785 if (kvm_msr_list->indices[i] == MSR_STAR)
786 kvm_has_msr_star = 1;
787 if (kvm_msr_list->indices[i] == MSR_VM_HSAVE_PA)
788 kvm_has_vm_hsave_pa = 1;
791 return 0;
794 static void set_msr_entry(struct kvm_msr_entry *entry, uint32_t index,
795 uint64_t data)
797 entry->index = index;
798 entry->data = data;
801 /* returns 0 on success, non-0 on failure */
802 static int get_msr_entry(struct kvm_msr_entry *entry, CPUState *env)
804 switch (entry->index) {
805 case MSR_IA32_SYSENTER_CS:
806 env->sysenter_cs = entry->data;
807 break;
808 case MSR_IA32_SYSENTER_ESP:
809 env->sysenter_esp = entry->data;
810 break;
811 case MSR_IA32_SYSENTER_EIP:
812 env->sysenter_eip = entry->data;
813 break;
814 case MSR_STAR:
815 env->star = entry->data;
816 break;
817 #ifdef TARGET_X86_64
818 case MSR_CSTAR:
819 env->cstar = entry->data;
820 break;
821 case MSR_KERNELGSBASE:
822 env->kernelgsbase = entry->data;
823 break;
824 case MSR_FMASK:
825 env->fmask = entry->data;
826 break;
827 case MSR_LSTAR:
828 env->lstar = entry->data;
829 break;
830 #endif
831 case MSR_IA32_TSC:
832 env->tsc = entry->data;
833 break;
834 case MSR_VM_HSAVE_PA:
835 env->vm_hsave = entry->data;
836 break;
837 default:
838 printf("Warning unknown msr index 0x%x\n", entry->index);
839 return 1;
841 return 0;
844 #ifdef TARGET_X86_64
845 #define MSR_COUNT 10
846 #else
847 #define MSR_COUNT 6
848 #endif
850 static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
852 lhs->selector = rhs->selector;
853 lhs->base = rhs->base;
854 lhs->limit = rhs->limit;
855 lhs->type = 3;
856 lhs->present = 1;
857 lhs->dpl = 3;
858 lhs->db = 0;
859 lhs->s = 1;
860 lhs->l = 0;
861 lhs->g = 0;
862 lhs->avl = 0;
863 lhs->unusable = 0;
866 static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
868 unsigned flags = rhs->flags;
869 lhs->selector = rhs->selector;
870 lhs->base = rhs->base;
871 lhs->limit = rhs->limit;
872 lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
873 lhs->present = (flags & DESC_P_MASK) != 0;
874 lhs->dpl = rhs->selector & 3;
875 lhs->db = (flags >> DESC_B_SHIFT) & 1;
876 lhs->s = (flags & DESC_S_MASK) != 0;
877 lhs->l = (flags >> DESC_L_SHIFT) & 1;
878 lhs->g = (flags & DESC_G_MASK) != 0;
879 lhs->avl = (flags & DESC_AVL_MASK) != 0;
880 lhs->unusable = 0;
883 static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
885 lhs->selector = rhs->selector;
886 lhs->base = rhs->base;
887 lhs->limit = rhs->limit;
888 lhs->flags =
889 (rhs->type << DESC_TYPE_SHIFT)
890 | (rhs->present * DESC_P_MASK)
891 | (rhs->dpl << DESC_DPL_SHIFT)
892 | (rhs->db << DESC_B_SHIFT)
893 | (rhs->s * DESC_S_MASK)
894 | (rhs->l << DESC_L_SHIFT)
895 | (rhs->g * DESC_G_MASK)
896 | (rhs->avl * DESC_AVL_MASK);
899 void kvm_arch_load_regs(CPUState *env)
901 struct kvm_regs regs;
902 struct kvm_fpu fpu;
903 struct kvm_sregs sregs;
904 struct kvm_msr_entry msrs[MSR_COUNT];
905 int rc, n, i;
907 regs.rax = env->regs[R_EAX];
908 regs.rbx = env->regs[R_EBX];
909 regs.rcx = env->regs[R_ECX];
910 regs.rdx = env->regs[R_EDX];
911 regs.rsi = env->regs[R_ESI];
912 regs.rdi = env->regs[R_EDI];
913 regs.rsp = env->regs[R_ESP];
914 regs.rbp = env->regs[R_EBP];
915 #ifdef TARGET_X86_64
916 regs.r8 = env->regs[8];
917 regs.r9 = env->regs[9];
918 regs.r10 = env->regs[10];
919 regs.r11 = env->regs[11];
920 regs.r12 = env->regs[12];
921 regs.r13 = env->regs[13];
922 regs.r14 = env->regs[14];
923 regs.r15 = env->regs[15];
924 #endif
926 regs.rflags = env->eflags;
927 regs.rip = env->eip;
929 kvm_set_regs(env->kvm_cpu_state.vcpu_ctx, &regs);
931 memset(&fpu, 0, sizeof fpu);
932 fpu.fsw = env->fpus & ~(7 << 11);
933 fpu.fsw |= (env->fpstt & 7) << 11;
934 fpu.fcw = env->fpuc;
935 for (i = 0; i < 8; ++i)
936 fpu.ftwx |= (!env->fptags[i]) << i;
937 memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
938 memcpy(fpu.xmm, env->xmm_regs, sizeof env->xmm_regs);
939 fpu.mxcsr = env->mxcsr;
940 kvm_set_fpu(env->kvm_cpu_state.vcpu_ctx, &fpu);
942 memcpy(sregs.interrupt_bitmap, env->interrupt_bitmap, sizeof(sregs.interrupt_bitmap));
944 if ((env->eflags & VM_MASK)) {
945 set_v8086_seg(&sregs.cs, &env->segs[R_CS]);
946 set_v8086_seg(&sregs.ds, &env->segs[R_DS]);
947 set_v8086_seg(&sregs.es, &env->segs[R_ES]);
948 set_v8086_seg(&sregs.fs, &env->segs[R_FS]);
949 set_v8086_seg(&sregs.gs, &env->segs[R_GS]);
950 set_v8086_seg(&sregs.ss, &env->segs[R_SS]);
951 } else {
952 set_seg(&sregs.cs, &env->segs[R_CS]);
953 set_seg(&sregs.ds, &env->segs[R_DS]);
954 set_seg(&sregs.es, &env->segs[R_ES]);
955 set_seg(&sregs.fs, &env->segs[R_FS]);
956 set_seg(&sregs.gs, &env->segs[R_GS]);
957 set_seg(&sregs.ss, &env->segs[R_SS]);
959 if (env->cr[0] & CR0_PE_MASK) {
960 /* force ss cpl to cs cpl */
961 sregs.ss.selector = (sregs.ss.selector & ~3) |
962 (sregs.cs.selector & 3);
963 sregs.ss.dpl = sregs.ss.selector & 3;
967 set_seg(&sregs.tr, &env->tr);
968 set_seg(&sregs.ldt, &env->ldt);
970 sregs.idt.limit = env->idt.limit;
971 sregs.idt.base = env->idt.base;
972 sregs.gdt.limit = env->gdt.limit;
973 sregs.gdt.base = env->gdt.base;
975 sregs.cr0 = env->cr[0];
976 sregs.cr2 = env->cr[2];
977 sregs.cr3 = env->cr[3];
978 sregs.cr4 = env->cr[4];
980 sregs.cr8 = cpu_get_apic_tpr(env);
981 sregs.apic_base = cpu_get_apic_base(env);
983 sregs.efer = env->efer;
985 kvm_set_sregs(env->kvm_cpu_state.vcpu_ctx, &sregs);
987 /* msrs */
988 n = 0;
989 /* Remember to increase MSR_COUNT if you add new registers below */
990 set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
991 set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
992 set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
993 if (kvm_has_msr_star)
994 set_msr_entry(&msrs[n++], MSR_STAR, env->star);
995 if (kvm_has_vm_hsave_pa)
996 set_msr_entry(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave);
997 #ifdef TARGET_X86_64
998 if (lm_capable_kernel) {
999 set_msr_entry(&msrs[n++], MSR_CSTAR, env->cstar);
1000 set_msr_entry(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
1001 set_msr_entry(&msrs[n++], MSR_FMASK, env->fmask);
1002 set_msr_entry(&msrs[n++], MSR_LSTAR , env->lstar);
1004 #endif
1006 rc = kvm_set_msrs(env->kvm_cpu_state.vcpu_ctx, msrs, n);
1007 if (rc == -1)
1008 perror("kvm_set_msrs FAILED");
1011 void kvm_load_tsc(CPUState *env)
1013 int rc;
1014 struct kvm_msr_entry msr;
1016 set_msr_entry(&msr, MSR_IA32_TSC, env->tsc);
1018 rc = kvm_set_msrs(env->kvm_cpu_state.vcpu_ctx, &msr, 1);
1019 if (rc == -1)
1020 perror("kvm_set_tsc FAILED.\n");
1023 void kvm_arch_save_mpstate(CPUState *env)
1025 #ifdef KVM_CAP_MP_STATE
1026 int r;
1027 struct kvm_mp_state mp_state;
1029 r = kvm_get_mpstate(env->kvm_cpu_state.vcpu_ctx, &mp_state);
1030 if (r < 0)
1031 env->mp_state = -1;
1032 else
1033 env->mp_state = mp_state.mp_state;
1034 #else
1035 env->mp_state = -1;
1036 #endif
1039 void kvm_arch_load_mpstate(CPUState *env)
1041 #ifdef KVM_CAP_MP_STATE
1042 struct kvm_mp_state mp_state = { .mp_state = env->mp_state };
1045 * -1 indicates that the host did not support GET_MP_STATE ioctl,
1046 * so don't touch it.
1048 if (env->mp_state != -1)
1049 kvm_set_mpstate(env->kvm_cpu_state.vcpu_ctx, &mp_state);
1050 #endif
1053 void kvm_arch_save_regs(CPUState *env)
1055 struct kvm_regs regs;
1056 struct kvm_fpu fpu;
1057 struct kvm_sregs sregs;
1058 struct kvm_msr_entry msrs[MSR_COUNT];
1059 uint32_t hflags;
1060 uint32_t i, n, rc;
1062 kvm_get_regs(env->kvm_cpu_state.vcpu_ctx, &regs);
1064 env->regs[R_EAX] = regs.rax;
1065 env->regs[R_EBX] = regs.rbx;
1066 env->regs[R_ECX] = regs.rcx;
1067 env->regs[R_EDX] = regs.rdx;
1068 env->regs[R_ESI] = regs.rsi;
1069 env->regs[R_EDI] = regs.rdi;
1070 env->regs[R_ESP] = regs.rsp;
1071 env->regs[R_EBP] = regs.rbp;
1072 #ifdef TARGET_X86_64
1073 env->regs[8] = regs.r8;
1074 env->regs[9] = regs.r9;
1075 env->regs[10] = regs.r10;
1076 env->regs[11] = regs.r11;
1077 env->regs[12] = regs.r12;
1078 env->regs[13] = regs.r13;
1079 env->regs[14] = regs.r14;
1080 env->regs[15] = regs.r15;
1081 #endif
1083 env->eflags = regs.rflags;
1084 env->eip = regs.rip;
1086 kvm_get_fpu(env->kvm_cpu_state.vcpu_ctx, &fpu);
1087 env->fpstt = (fpu.fsw >> 11) & 7;
1088 env->fpus = fpu.fsw;
1089 env->fpuc = fpu.fcw;
1090 for (i = 0; i < 8; ++i)
1091 env->fptags[i] = !((fpu.ftwx >> i) & 1);
1092 memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
1093 memcpy(env->xmm_regs, fpu.xmm, sizeof env->xmm_regs);
1094 env->mxcsr = fpu.mxcsr;
1096 kvm_get_sregs(env->kvm_cpu_state.vcpu_ctx, &sregs);
1098 memcpy(env->interrupt_bitmap, sregs.interrupt_bitmap, sizeof(env->interrupt_bitmap));
1100 get_seg(&env->segs[R_CS], &sregs.cs);
1101 get_seg(&env->segs[R_DS], &sregs.ds);
1102 get_seg(&env->segs[R_ES], &sregs.es);
1103 get_seg(&env->segs[R_FS], &sregs.fs);
1104 get_seg(&env->segs[R_GS], &sregs.gs);
1105 get_seg(&env->segs[R_SS], &sregs.ss);
1107 get_seg(&env->tr, &sregs.tr);
1108 get_seg(&env->ldt, &sregs.ldt);
1110 env->idt.limit = sregs.idt.limit;
1111 env->idt.base = sregs.idt.base;
1112 env->gdt.limit = sregs.gdt.limit;
1113 env->gdt.base = sregs.gdt.base;
1115 env->cr[0] = sregs.cr0;
1116 env->cr[2] = sregs.cr2;
1117 env->cr[3] = sregs.cr3;
1118 env->cr[4] = sregs.cr4;
1120 cpu_set_apic_base(env, sregs.apic_base);
1122 env->efer = sregs.efer;
1123 //cpu_set_apic_tpr(env, sregs.cr8);
1125 #define HFLAG_COPY_MASK ~( \
1126 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1127 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1128 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1129 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1133 hflags = (env->segs[R_CS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;
1134 hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
1135 hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
1136 (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK);
1137 hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK));
1138 hflags |= (env->cr[4] & CR4_OSFXSR_MASK) <<
1139 (HF_OSFXSR_SHIFT - CR4_OSFXSR_SHIFT);
1141 if (env->efer & MSR_EFER_LMA) {
1142 hflags |= HF_LMA_MASK;
1145 if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
1146 hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
1147 } else {
1148 hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >>
1149 (DESC_B_SHIFT - HF_CS32_SHIFT);
1150 hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >>
1151 (DESC_B_SHIFT - HF_SS32_SHIFT);
1152 if (!(env->cr[0] & CR0_PE_MASK) ||
1153 (env->eflags & VM_MASK) ||
1154 !(hflags & HF_CS32_MASK)) {
1155 hflags |= HF_ADDSEG_MASK;
1156 } else {
1157 hflags |= ((env->segs[R_DS].base |
1158 env->segs[R_ES].base |
1159 env->segs[R_SS].base) != 0) <<
1160 HF_ADDSEG_SHIFT;
1163 env->hflags = (env->hflags & HFLAG_COPY_MASK) | hflags;
1165 /* msrs */
1166 n = 0;
1167 /* Remember to increase MSR_COUNT if you add new registers below */
1168 msrs[n++].index = MSR_IA32_SYSENTER_CS;
1169 msrs[n++].index = MSR_IA32_SYSENTER_ESP;
1170 msrs[n++].index = MSR_IA32_SYSENTER_EIP;
1171 if (kvm_has_msr_star)
1172 msrs[n++].index = MSR_STAR;
1173 msrs[n++].index = MSR_IA32_TSC;
1174 if (kvm_has_vm_hsave_pa)
1175 msrs[n++].index = MSR_VM_HSAVE_PA;
1176 #ifdef TARGET_X86_64
1177 if (lm_capable_kernel) {
1178 msrs[n++].index = MSR_CSTAR;
1179 msrs[n++].index = MSR_KERNELGSBASE;
1180 msrs[n++].index = MSR_FMASK;
1181 msrs[n++].index = MSR_LSTAR;
1183 #endif
1184 rc = kvm_get_msrs(env->kvm_cpu_state.vcpu_ctx, msrs, n);
1185 if (rc == -1) {
1186 perror("kvm_get_msrs FAILED");
1188 else {
1189 n = rc; /* actual number of MSRs */
1190 for (i=0 ; i<n; i++) {
1191 if (get_msr_entry(&msrs[i], env))
1192 return;
1197 static void do_cpuid_ent(struct kvm_cpuid_entry2 *e, uint32_t function,
1198 uint32_t count, CPUState *env)
1200 env->regs[R_EAX] = function;
1201 env->regs[R_ECX] = count;
1202 qemu_kvm_cpuid_on_env(env);
1203 e->function = function;
1204 e->flags = 0;
1205 e->index = 0;
1206 e->eax = env->regs[R_EAX];
1207 e->ebx = env->regs[R_EBX];
1208 e->ecx = env->regs[R_ECX];
1209 e->edx = env->regs[R_EDX];
1212 struct kvm_para_features {
1213 int cap;
1214 int feature;
1215 } para_features[] = {
1216 #ifdef KVM_CAP_CLOCKSOURCE
1217 { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
1218 #endif
1219 #ifdef KVM_CAP_NOP_IO_DELAY
1220 { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
1221 #endif
1222 #ifdef KVM_CAP_PV_MMU
1223 { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
1224 #endif
1225 #ifdef KVM_CAP_CR3_CACHE
1226 { KVM_CAP_CR3_CACHE, KVM_FEATURE_CR3_CACHE },
1227 #endif
1228 { -1, -1 }
1231 static int get_para_features(kvm_context_t kvm_context)
1233 int i, features = 0;
1235 for (i = 0; i < ARRAY_SIZE(para_features)-1; i++) {
1236 if (kvm_check_extension(kvm_state, para_features[i].cap))
1237 features |= (1 << para_features[i].feature);
1240 return features;
1243 static void kvm_trim_features(uint32_t *features, uint32_t supported)
1245 int i;
1246 uint32_t mask;
1248 for (i = 0; i < 32; ++i) {
1249 mask = 1U << i;
1250 if ((*features & mask) && !(supported & mask)) {
1251 *features &= ~mask;
1256 int kvm_arch_init_vcpu(CPUState *cenv)
1258 struct kvm_cpuid_entry2 cpuid_ent[100];
1259 #ifdef KVM_CPUID_SIGNATURE
1260 struct kvm_cpuid_entry2 *pv_ent;
1261 uint32_t signature[3];
1262 #endif
1263 int cpuid_nent = 0;
1264 CPUState copy;
1265 uint32_t i, j, limit;
1267 qemu_kvm_load_lapic(cenv);
1270 #ifdef KVM_CPUID_SIGNATURE
1271 /* Paravirtualization CPUIDs */
1272 memcpy(signature, "KVMKVMKVM\0\0\0", 12);
1273 pv_ent = &cpuid_ent[cpuid_nent++];
1274 memset(pv_ent, 0, sizeof(*pv_ent));
1275 pv_ent->function = KVM_CPUID_SIGNATURE;
1276 pv_ent->eax = 0;
1277 pv_ent->ebx = signature[0];
1278 pv_ent->ecx = signature[1];
1279 pv_ent->edx = signature[2];
1281 pv_ent = &cpuid_ent[cpuid_nent++];
1282 memset(pv_ent, 0, sizeof(*pv_ent));
1283 pv_ent->function = KVM_CPUID_FEATURES;
1284 pv_ent->eax = get_para_features(kvm_context);
1285 #endif
1287 kvm_trim_features(&cenv->cpuid_features,
1288 kvm_arch_get_supported_cpuid(cenv, 1, R_EDX));
1290 /* prevent the hypervisor bit from being cleared by the kernel */
1291 i = cenv->cpuid_ext_features & CPUID_EXT_HYPERVISOR;
1292 kvm_trim_features(&cenv->cpuid_ext_features,
1293 kvm_arch_get_supported_cpuid(cenv, 1, R_ECX));
1294 cenv->cpuid_ext_features |= i;
1296 kvm_trim_features(&cenv->cpuid_ext2_features,
1297 kvm_arch_get_supported_cpuid(cenv, 0x80000001, R_EDX));
1298 kvm_trim_features(&cenv->cpuid_ext3_features,
1299 kvm_arch_get_supported_cpuid(cenv, 0x80000001, R_ECX));
1301 copy = *cenv;
1303 copy.regs[R_EAX] = 0;
1304 qemu_kvm_cpuid_on_env(&copy);
1305 limit = copy.regs[R_EAX];
1307 for (i = 0; i <= limit; ++i) {
1308 if (i == 4 || i == 0xb || i == 0xd) {
1309 for (j = 0; ; ++j) {
1310 do_cpuid_ent(&cpuid_ent[cpuid_nent], i, j, &copy);
1312 cpuid_ent[cpuid_nent].flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1313 cpuid_ent[cpuid_nent].index = j;
1315 cpuid_nent++;
1317 if (i == 4 && copy.regs[R_EAX] == 0)
1318 break;
1319 if (i == 0xb && !(copy.regs[R_ECX] & 0xff00))
1320 break;
1321 if (i == 0xd && copy.regs[R_EAX] == 0)
1322 break;
1324 } else
1325 do_cpuid_ent(&cpuid_ent[cpuid_nent++], i, 0, &copy);
1328 copy.regs[R_EAX] = 0x80000000;
1329 qemu_kvm_cpuid_on_env(&copy);
1330 limit = copy.regs[R_EAX];
1332 for (i = 0x80000000; i <= limit; ++i)
1333 do_cpuid_ent(&cpuid_ent[cpuid_nent++], i, 0, &copy);
1335 kvm_setup_cpuid2(cenv->kvm_cpu_state.vcpu_ctx, cpuid_nent, cpuid_ent);
1337 #ifdef KVM_CAP_MCE
1338 if (((cenv->cpuid_version >> 8)&0xF) >= 6
1339 && (cenv->cpuid_features&(CPUID_MCE|CPUID_MCA)) == (CPUID_MCE|CPUID_MCA)
1340 && kvm_check_extension(kvm_state, KVM_CAP_MCE) > 0) {
1341 uint64_t mcg_cap;
1342 int banks;
1344 if (kvm_get_mce_cap_supported(kvm_context, &mcg_cap, &banks))
1345 perror("kvm_get_mce_cap_supported FAILED");
1346 else {
1347 if (banks > MCE_BANKS_DEF)
1348 banks = MCE_BANKS_DEF;
1349 mcg_cap &= MCE_CAP_DEF;
1350 mcg_cap |= banks;
1351 if (kvm_setup_mce(cenv->kvm_cpu_state.vcpu_ctx, &mcg_cap))
1352 perror("kvm_setup_mce FAILED");
1353 else
1354 cenv->mcg_cap = mcg_cap;
1357 #endif
1359 return 0;
1362 int kvm_arch_halt(kvm_vcpu_context_t vcpu)
1364 CPUState *env = cpu_single_env;
1366 if (!((env->interrupt_request & CPU_INTERRUPT_HARD) &&
1367 (env->eflags & IF_MASK)) &&
1368 !(env->interrupt_request & CPU_INTERRUPT_NMI)) {
1369 env->halted = 1;
1371 return 1;
1374 void kvm_arch_pre_kvm_run(void *opaque, CPUState *env)
1376 if (!kvm_irqchip_in_kernel())
1377 kvm_set_cr8(env, cpu_get_apic_tpr(env));
1380 void kvm_arch_post_kvm_run(void *opaque, CPUState *env)
1382 cpu_single_env = env;
1384 env->eflags = kvm_get_interrupt_flag(env)
1385 ? env->eflags | IF_MASK : env->eflags & ~IF_MASK;
1387 cpu_set_apic_tpr(env, kvm_get_cr8(env));
1388 cpu_set_apic_base(env, kvm_get_apic_base(env));
1391 int kvm_arch_has_work(CPUState *env)
1393 if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
1394 (env->eflags & IF_MASK)) ||
1395 (env->interrupt_request & CPU_INTERRUPT_NMI))
1396 return 1;
1397 return 0;
1400 int kvm_arch_try_push_interrupts(void *opaque)
1402 CPUState *env = cpu_single_env;
1403 int r, irq;
1405 if (kvm_is_ready_for_interrupt_injection(env) &&
1406 (env->interrupt_request & CPU_INTERRUPT_HARD) &&
1407 (env->eflags & IF_MASK)) {
1408 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
1409 irq = cpu_get_pic_interrupt(env);
1410 if (irq >= 0) {
1411 r = kvm_inject_irq(env->kvm_cpu_state.vcpu_ctx, irq);
1412 if (r < 0)
1413 printf("cpu %d fail inject %x\n", env->cpu_index, irq);
1417 return (env->interrupt_request & CPU_INTERRUPT_HARD) != 0;
1420 #ifdef KVM_CAP_USER_NMI
1421 void kvm_arch_push_nmi(void *opaque)
1423 CPUState *env = cpu_single_env;
1424 int r;
1426 if (likely(!(env->interrupt_request & CPU_INTERRUPT_NMI)))
1427 return;
1429 env->interrupt_request &= ~CPU_INTERRUPT_NMI;
1430 r = kvm_inject_nmi(env->kvm_cpu_state.vcpu_ctx);
1431 if (r < 0)
1432 printf("cpu %d fail inject NMI\n", env->cpu_index);
1434 #endif /* KVM_CAP_USER_NMI */
1436 void kvm_arch_cpu_reset(CPUState *env)
1438 kvm_arch_load_regs(env);
1439 if (!cpu_is_bsp(env)) {
1440 if (kvm_irqchip_in_kernel()) {
1441 #ifdef KVM_CAP_MP_STATE
1442 kvm_reset_mpstate(env->kvm_cpu_state.vcpu_ctx);
1443 #endif
1444 } else {
1445 env->interrupt_request &= ~CPU_INTERRUPT_HARD;
1446 env->halted = 1;
1451 int kvm_arch_insert_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
1453 uint8_t int3 = 0xcc;
1455 if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) ||
1456 cpu_memory_rw_debug(env, bp->pc, &int3, 1, 1))
1457 return -EINVAL;
1458 return 0;
1461 int kvm_arch_remove_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
1463 uint8_t int3;
1465 if (cpu_memory_rw_debug(env, bp->pc, &int3, 1, 0) || int3 != 0xcc ||
1466 cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1))
1467 return -EINVAL;
1468 return 0;
1471 #ifdef KVM_CAP_SET_GUEST_DEBUG
1472 static struct {
1473 target_ulong addr;
1474 int len;
1475 int type;
1476 } hw_breakpoint[4];
1478 static int nb_hw_breakpoint;
1480 static int find_hw_breakpoint(target_ulong addr, int len, int type)
1482 int n;
1484 for (n = 0; n < nb_hw_breakpoint; n++)
1485 if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
1486 (hw_breakpoint[n].len == len || len == -1))
1487 return n;
1488 return -1;
1491 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1492 target_ulong len, int type)
1494 switch (type) {
1495 case GDB_BREAKPOINT_HW:
1496 len = 1;
1497 break;
1498 case GDB_WATCHPOINT_WRITE:
1499 case GDB_WATCHPOINT_ACCESS:
1500 switch (len) {
1501 case 1:
1502 break;
1503 case 2:
1504 case 4:
1505 case 8:
1506 if (addr & (len - 1))
1507 return -EINVAL;
1508 break;
1509 default:
1510 return -EINVAL;
1512 break;
1513 default:
1514 return -ENOSYS;
1517 if (nb_hw_breakpoint == 4)
1518 return -ENOBUFS;
1520 if (find_hw_breakpoint(addr, len, type) >= 0)
1521 return -EEXIST;
1523 hw_breakpoint[nb_hw_breakpoint].addr = addr;
1524 hw_breakpoint[nb_hw_breakpoint].len = len;
1525 hw_breakpoint[nb_hw_breakpoint].type = type;
1526 nb_hw_breakpoint++;
1528 return 0;
1531 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1532 target_ulong len, int type)
1534 int n;
1536 n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
1537 if (n < 0)
1538 return -ENOENT;
1540 nb_hw_breakpoint--;
1541 hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint];
1543 return 0;
1546 void kvm_arch_remove_all_hw_breakpoints(void)
1548 nb_hw_breakpoint = 0;
1551 static CPUWatchpoint hw_watchpoint;
1553 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
1555 int handle = 0;
1556 int n;
1558 if (arch_info->exception == 1) {
1559 if (arch_info->dr6 & (1 << 14)) {
1560 if (cpu_single_env->singlestep_enabled)
1561 handle = 1;
1562 } else {
1563 for (n = 0; n < 4; n++)
1564 if (arch_info->dr6 & (1 << n))
1565 switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
1566 case 0x0:
1567 handle = 1;
1568 break;
1569 case 0x1:
1570 handle = 1;
1571 cpu_single_env->watchpoint_hit = &hw_watchpoint;
1572 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
1573 hw_watchpoint.flags = BP_MEM_WRITE;
1574 break;
1575 case 0x3:
1576 handle = 1;
1577 cpu_single_env->watchpoint_hit = &hw_watchpoint;
1578 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
1579 hw_watchpoint.flags = BP_MEM_ACCESS;
1580 break;
1583 } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc))
1584 handle = 1;
1586 if (!handle)
1587 kvm_update_guest_debug(cpu_single_env,
1588 (arch_info->exception == 1) ?
1589 KVM_GUESTDBG_INJECT_DB : KVM_GUESTDBG_INJECT_BP);
1591 return handle;
1594 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
1596 const uint8_t type_code[] = {
1597 [GDB_BREAKPOINT_HW] = 0x0,
1598 [GDB_WATCHPOINT_WRITE] = 0x1,
1599 [GDB_WATCHPOINT_ACCESS] = 0x3
1601 const uint8_t len_code[] = {
1602 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1604 int n;
1606 if (kvm_sw_breakpoints_active(env))
1607 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1609 if (nb_hw_breakpoint > 0) {
1610 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1611 dbg->arch.debugreg[7] = 0x0600;
1612 for (n = 0; n < nb_hw_breakpoint; n++) {
1613 dbg->arch.debugreg[n] = hw_breakpoint[n].addr;
1614 dbg->arch.debugreg[7] |= (2 << (n * 2)) |
1615 (type_code[hw_breakpoint[n].type] << (16 + n*4)) |
1616 (len_code[hw_breakpoint[n].len] << (18 + n*4));
1620 #endif
1622 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1623 void kvm_arch_do_ioperm(void *_data)
1625 struct ioperm_data *data = _data;
1626 ioperm(data->start_port, data->num, data->turn_on);
1628 #endif
1631 * Setup x86 specific IRQ routing
1633 int kvm_arch_init_irq_routing(void)
1635 int i, r;
1637 if (kvm_irqchip && kvm_has_gsi_routing(kvm_context)) {
1638 kvm_clear_gsi_routes(kvm_context);
1639 for (i = 0; i < 8; ++i) {
1640 if (i == 2)
1641 continue;
1642 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_MASTER, i);
1643 if (r < 0)
1644 return r;
1646 for (i = 8; i < 16; ++i) {
1647 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
1648 if (r < 0)
1649 return r;
1651 for (i = 0; i < 24; ++i) {
1652 if (i == 0) {
1653 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_IOAPIC, 2);
1654 } else if (i != 2) {
1655 r = kvm_add_irq_route(kvm_context, i, KVM_IRQCHIP_IOAPIC, i);
1657 if (r < 0)
1658 return r;
1660 kvm_commit_irq_routes(kvm_context);
1662 return 0;
1665 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1666 int reg)
1668 return kvm_get_supported_cpuid(kvm_context, function, reg);
1671 void kvm_arch_process_irqchip_events(CPUState *env)
1673 if (env->interrupt_request & CPU_INTERRUPT_INIT) {
1674 kvm_cpu_synchronize_state(env);
1675 do_cpu_init(env);
1677 if (env->interrupt_request & CPU_INTERRUPT_SIPI) {
1678 kvm_cpu_synchronize_state(env);
1679 do_cpu_sipi(env);