diff options
Diffstat (limited to 'arch/x86/kvm/x86.c')
-rw-r--r-- | arch/x86/kvm/x86.c | 432 |
1 files changed, 318 insertions, 114 deletions
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 758b7a155ae9..8ca100a9ecac 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c | |||
@@ -36,6 +36,7 @@ | |||
36 | #include <linux/highmem.h> | 36 | #include <linux/highmem.h> |
37 | #include <linux/iommu.h> | 37 | #include <linux/iommu.h> |
38 | #include <linux/intel-iommu.h> | 38 | #include <linux/intel-iommu.h> |
39 | #include <linux/cpufreq.h> | ||
39 | 40 | ||
40 | #include <asm/uaccess.h> | 41 | #include <asm/uaccess.h> |
41 | #include <asm/msr.h> | 42 | #include <asm/msr.h> |
@@ -69,6 +70,8 @@ static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; | |||
69 | 70 | ||
70 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, | 71 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
71 | struct kvm_cpuid_entry2 __user *entries); | 72 | struct kvm_cpuid_entry2 __user *entries); |
73 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, | ||
74 | u32 function, u32 index); | ||
72 | 75 | ||
73 | struct kvm_x86_ops *kvm_x86_ops; | 76 | struct kvm_x86_ops *kvm_x86_ops; |
74 | EXPORT_SYMBOL_GPL(kvm_x86_ops); | 77 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
@@ -173,6 +176,7 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, | |||
173 | u32 error_code) | 176 | u32 error_code) |
174 | { | 177 | { |
175 | ++vcpu->stat.pf_guest; | 178 | ++vcpu->stat.pf_guest; |
179 | |||
176 | if (vcpu->arch.exception.pending) { | 180 | if (vcpu->arch.exception.pending) { |
177 | if (vcpu->arch.exception.nr == PF_VECTOR) { | 181 | if (vcpu->arch.exception.nr == PF_VECTOR) { |
178 | printk(KERN_DEBUG "kvm: inject_page_fault:" | 182 | printk(KERN_DEBUG "kvm: inject_page_fault:" |
@@ -361,6 +365,7 @@ void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |||
361 | } | 365 | } |
362 | kvm_x86_ops->set_cr4(vcpu, cr4); | 366 | kvm_x86_ops->set_cr4(vcpu, cr4); |
363 | vcpu->arch.cr4 = cr4; | 367 | vcpu->arch.cr4 = cr4; |
368 | vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled; | ||
364 | kvm_mmu_sync_global(vcpu); | 369 | kvm_mmu_sync_global(vcpu); |
365 | kvm_mmu_reset_context(vcpu); | 370 | kvm_mmu_reset_context(vcpu); |
366 | } | 371 | } |
@@ -442,6 +447,11 @@ unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) | |||
442 | } | 447 | } |
443 | EXPORT_SYMBOL_GPL(kvm_get_cr8); | 448 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
444 | 449 | ||
450 | static inline u32 bit(int bitno) | ||
451 | { | ||
452 | return 1 << (bitno & 31); | ||
453 | } | ||
454 | |||
445 | /* | 455 | /* |
446 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | 456 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
447 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | 457 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
@@ -456,7 +466,7 @@ static u32 msrs_to_save[] = { | |||
456 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | 466 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
457 | #endif | 467 | #endif |
458 | MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, | 468 | MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
459 | MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT | 469 | MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA |
460 | }; | 470 | }; |
461 | 471 | ||
462 | static unsigned num_msrs_to_save; | 472 | static unsigned num_msrs_to_save; |
@@ -481,6 +491,28 @@ static void set_efer(struct kvm_vcpu *vcpu, u64 efer) | |||
481 | return; | 491 | return; |
482 | } | 492 | } |
483 | 493 | ||
494 | if (efer & EFER_FFXSR) { | ||
495 | struct kvm_cpuid_entry2 *feat; | ||
496 | |||
497 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); | ||
498 | if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) { | ||
499 | printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n"); | ||
500 | kvm_inject_gp(vcpu, 0); | ||
501 | return; | ||
502 | } | ||
503 | } | ||
504 | |||
505 | if (efer & EFER_SVME) { | ||
506 | struct kvm_cpuid_entry2 *feat; | ||
507 | |||
508 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); | ||
509 | if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) { | ||
510 | printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n"); | ||
511 | kvm_inject_gp(vcpu, 0); | ||
512 | return; | ||
513 | } | ||
514 | } | ||
515 | |||
484 | kvm_x86_ops->set_efer(vcpu, efer); | 516 | kvm_x86_ops->set_efer(vcpu, efer); |
485 | 517 | ||
486 | efer &= ~EFER_LMA; | 518 | efer &= ~EFER_LMA; |
@@ -586,6 +618,8 @@ static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info * | |||
586 | hv_clock->tsc_to_system_mul); | 618 | hv_clock->tsc_to_system_mul); |
587 | } | 619 | } |
588 | 620 | ||
621 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); | ||
622 | |||
589 | static void kvm_write_guest_time(struct kvm_vcpu *v) | 623 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
590 | { | 624 | { |
591 | struct timespec ts; | 625 | struct timespec ts; |
@@ -596,9 +630,9 @@ static void kvm_write_guest_time(struct kvm_vcpu *v) | |||
596 | if ((!vcpu->time_page)) | 630 | if ((!vcpu->time_page)) |
597 | return; | 631 | return; |
598 | 632 | ||
599 | if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) { | 633 | if (unlikely(vcpu->hv_clock_tsc_khz != __get_cpu_var(cpu_tsc_khz))) { |
600 | kvm_set_time_scale(tsc_khz, &vcpu->hv_clock); | 634 | kvm_set_time_scale(__get_cpu_var(cpu_tsc_khz), &vcpu->hv_clock); |
601 | vcpu->hv_clock_tsc_khz = tsc_khz; | 635 | vcpu->hv_clock_tsc_khz = __get_cpu_var(cpu_tsc_khz); |
602 | } | 636 | } |
603 | 637 | ||
604 | /* Keep irq disabled to prevent changes to the clock */ | 638 | /* Keep irq disabled to prevent changes to the clock */ |
@@ -629,6 +663,16 @@ static void kvm_write_guest_time(struct kvm_vcpu *v) | |||
629 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); | 663 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
630 | } | 664 | } |
631 | 665 | ||
666 | static int kvm_request_guest_time_update(struct kvm_vcpu *v) | ||
667 | { | ||
668 | struct kvm_vcpu_arch *vcpu = &v->arch; | ||
669 | |||
670 | if (!vcpu->time_page) | ||
671 | return 0; | ||
672 | set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests); | ||
673 | return 1; | ||
674 | } | ||
675 | |||
632 | static bool msr_mtrr_valid(unsigned msr) | 676 | static bool msr_mtrr_valid(unsigned msr) |
633 | { | 677 | { |
634 | switch (msr) { | 678 | switch (msr) { |
@@ -722,6 +766,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |||
722 | break; | 766 | break; |
723 | case MSR_IA32_UCODE_REV: | 767 | case MSR_IA32_UCODE_REV: |
724 | case MSR_IA32_UCODE_WRITE: | 768 | case MSR_IA32_UCODE_WRITE: |
769 | case MSR_VM_HSAVE_PA: | ||
725 | break; | 770 | break; |
726 | case 0x200 ... 0x2ff: | 771 | case 0x200 ... 0x2ff: |
727 | return set_msr_mtrr(vcpu, msr, data); | 772 | return set_msr_mtrr(vcpu, msr, data); |
@@ -758,7 +803,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |||
758 | vcpu->arch.time_page = NULL; | 803 | vcpu->arch.time_page = NULL; |
759 | } | 804 | } |
760 | 805 | ||
761 | kvm_write_guest_time(vcpu); | 806 | kvm_request_guest_time_update(vcpu); |
762 | break; | 807 | break; |
763 | } | 808 | } |
764 | default: | 809 | default: |
@@ -843,6 +888,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) | |||
843 | case MSR_IA32_LASTBRANCHTOIP: | 888 | case MSR_IA32_LASTBRANCHTOIP: |
844 | case MSR_IA32_LASTINTFROMIP: | 889 | case MSR_IA32_LASTINTFROMIP: |
845 | case MSR_IA32_LASTINTTOIP: | 890 | case MSR_IA32_LASTINTTOIP: |
891 | case MSR_VM_HSAVE_PA: | ||
846 | data = 0; | 892 | data = 0; |
847 | break; | 893 | break; |
848 | case MSR_MTRRcap: | 894 | case MSR_MTRRcap: |
@@ -967,10 +1013,13 @@ int kvm_dev_ioctl_check_extension(long ext) | |||
967 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | 1013 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
968 | case KVM_CAP_SET_TSS_ADDR: | 1014 | case KVM_CAP_SET_TSS_ADDR: |
969 | case KVM_CAP_EXT_CPUID: | 1015 | case KVM_CAP_EXT_CPUID: |
1016 | case KVM_CAP_CLOCKSOURCE: | ||
970 | case KVM_CAP_PIT: | 1017 | case KVM_CAP_PIT: |
971 | case KVM_CAP_NOP_IO_DELAY: | 1018 | case KVM_CAP_NOP_IO_DELAY: |
972 | case KVM_CAP_MP_STATE: | 1019 | case KVM_CAP_MP_STATE: |
973 | case KVM_CAP_SYNC_MMU: | 1020 | case KVM_CAP_SYNC_MMU: |
1021 | case KVM_CAP_REINJECT_CONTROL: | ||
1022 | case KVM_CAP_IRQ_INJECT_STATUS: | ||
974 | r = 1; | 1023 | r = 1; |
975 | break; | 1024 | break; |
976 | case KVM_CAP_COALESCED_MMIO: | 1025 | case KVM_CAP_COALESCED_MMIO: |
@@ -991,9 +1040,6 @@ int kvm_dev_ioctl_check_extension(long ext) | |||
991 | case KVM_CAP_IOMMU: | 1040 | case KVM_CAP_IOMMU: |
992 | r = iommu_found(); | 1041 | r = iommu_found(); |
993 | break; | 1042 | break; |
994 | case KVM_CAP_CLOCKSOURCE: | ||
995 | r = boot_cpu_has(X86_FEATURE_CONSTANT_TSC); | ||
996 | break; | ||
997 | default: | 1043 | default: |
998 | r = 0; | 1044 | r = 0; |
999 | break; | 1045 | break; |
@@ -1044,7 +1090,7 @@ long kvm_arch_dev_ioctl(struct file *filp, | |||
1044 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | 1090 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
1045 | goto out; | 1091 | goto out; |
1046 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, | 1092 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
1047 | cpuid_arg->entries); | 1093 | cpuid_arg->entries); |
1048 | if (r) | 1094 | if (r) |
1049 | goto out; | 1095 | goto out; |
1050 | 1096 | ||
@@ -1064,7 +1110,7 @@ out: | |||
1064 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) | 1110 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
1065 | { | 1111 | { |
1066 | kvm_x86_ops->vcpu_load(vcpu, cpu); | 1112 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
1067 | kvm_write_guest_time(vcpu); | 1113 | kvm_request_guest_time_update(vcpu); |
1068 | } | 1114 | } |
1069 | 1115 | ||
1070 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | 1116 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
@@ -1142,8 +1188,8 @@ out: | |||
1142 | } | 1188 | } |
1143 | 1189 | ||
1144 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, | 1190 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
1145 | struct kvm_cpuid2 *cpuid, | 1191 | struct kvm_cpuid2 *cpuid, |
1146 | struct kvm_cpuid_entry2 __user *entries) | 1192 | struct kvm_cpuid_entry2 __user *entries) |
1147 | { | 1193 | { |
1148 | int r; | 1194 | int r; |
1149 | 1195 | ||
@@ -1162,8 +1208,8 @@ out: | |||
1162 | } | 1208 | } |
1163 | 1209 | ||
1164 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, | 1210 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
1165 | struct kvm_cpuid2 *cpuid, | 1211 | struct kvm_cpuid2 *cpuid, |
1166 | struct kvm_cpuid_entry2 __user *entries) | 1212 | struct kvm_cpuid_entry2 __user *entries) |
1167 | { | 1213 | { |
1168 | int r; | 1214 | int r; |
1169 | 1215 | ||
@@ -1172,7 +1218,7 @@ static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, | |||
1172 | goto out; | 1218 | goto out; |
1173 | r = -EFAULT; | 1219 | r = -EFAULT; |
1174 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, | 1220 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
1175 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) | 1221 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
1176 | goto out; | 1222 | goto out; |
1177 | return 0; | 1223 | return 0; |
1178 | 1224 | ||
@@ -1181,18 +1227,13 @@ out: | |||
1181 | return r; | 1227 | return r; |
1182 | } | 1228 | } |
1183 | 1229 | ||
1184 | static inline u32 bit(int bitno) | ||
1185 | { | ||
1186 | return 1 << (bitno & 31); | ||
1187 | } | ||
1188 | |||
1189 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, | 1230 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
1190 | u32 index) | 1231 | u32 index) |
1191 | { | 1232 | { |
1192 | entry->function = function; | 1233 | entry->function = function; |
1193 | entry->index = index; | 1234 | entry->index = index; |
1194 | cpuid_count(entry->function, entry->index, | 1235 | cpuid_count(entry->function, entry->index, |
1195 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); | 1236 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
1196 | entry->flags = 0; | 1237 | entry->flags = 0; |
1197 | } | 1238 | } |
1198 | 1239 | ||
@@ -1222,15 +1263,17 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |||
1222 | #ifdef CONFIG_X86_64 | 1263 | #ifdef CONFIG_X86_64 |
1223 | bit(X86_FEATURE_LM) | | 1264 | bit(X86_FEATURE_LM) | |
1224 | #endif | 1265 | #endif |
1266 | bit(X86_FEATURE_FXSR_OPT) | | ||
1225 | bit(X86_FEATURE_MMXEXT) | | 1267 | bit(X86_FEATURE_MMXEXT) | |
1226 | bit(X86_FEATURE_3DNOWEXT) | | 1268 | bit(X86_FEATURE_3DNOWEXT) | |
1227 | bit(X86_FEATURE_3DNOW); | 1269 | bit(X86_FEATURE_3DNOW); |
1228 | const u32 kvm_supported_word3_x86_features = | 1270 | const u32 kvm_supported_word3_x86_features = |
1229 | bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16); | 1271 | bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16); |
1230 | const u32 kvm_supported_word6_x86_features = | 1272 | const u32 kvm_supported_word6_x86_features = |
1231 | bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY); | 1273 | bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY) | |
1274 | bit(X86_FEATURE_SVM); | ||
1232 | 1275 | ||
1233 | /* all func 2 cpuid_count() should be called on the same cpu */ | 1276 | /* all calls to cpuid_count() should be made on the same cpu */ |
1234 | get_cpu(); | 1277 | get_cpu(); |
1235 | do_cpuid_1_ent(entry, function, index); | 1278 | do_cpuid_1_ent(entry, function, index); |
1236 | ++*nent; | 1279 | ++*nent; |
@@ -1304,7 +1347,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |||
1304 | } | 1347 | } |
1305 | 1348 | ||
1306 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, | 1349 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
1307 | struct kvm_cpuid_entry2 __user *entries) | 1350 | struct kvm_cpuid_entry2 __user *entries) |
1308 | { | 1351 | { |
1309 | struct kvm_cpuid_entry2 *cpuid_entries; | 1352 | struct kvm_cpuid_entry2 *cpuid_entries; |
1310 | int limit, nent = 0, r = -E2BIG; | 1353 | int limit, nent = 0, r = -E2BIG; |
@@ -1321,7 +1364,7 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, | |||
1321 | limit = cpuid_entries[0].eax; | 1364 | limit = cpuid_entries[0].eax; |
1322 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) | 1365 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
1323 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | 1366 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
1324 | &nent, cpuid->nent); | 1367 | &nent, cpuid->nent); |
1325 | r = -E2BIG; | 1368 | r = -E2BIG; |
1326 | if (nent >= cpuid->nent) | 1369 | if (nent >= cpuid->nent) |
1327 | goto out_free; | 1370 | goto out_free; |
@@ -1330,10 +1373,10 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, | |||
1330 | limit = cpuid_entries[nent - 1].eax; | 1373 | limit = cpuid_entries[nent - 1].eax; |
1331 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) | 1374 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
1332 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | 1375 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
1333 | &nent, cpuid->nent); | 1376 | &nent, cpuid->nent); |
1334 | r = -EFAULT; | 1377 | r = -EFAULT; |
1335 | if (copy_to_user(entries, cpuid_entries, | 1378 | if (copy_to_user(entries, cpuid_entries, |
1336 | nent * sizeof(struct kvm_cpuid_entry2))) | 1379 | nent * sizeof(struct kvm_cpuid_entry2))) |
1337 | goto out_free; | 1380 | goto out_free; |
1338 | cpuid->nent = nent; | 1381 | cpuid->nent = nent; |
1339 | r = 0; | 1382 | r = 0; |
@@ -1477,7 +1520,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, | |||
1477 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | 1520 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
1478 | goto out; | 1521 | goto out; |
1479 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | 1522 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
1480 | cpuid_arg->entries); | 1523 | cpuid_arg->entries); |
1481 | if (r) | 1524 | if (r) |
1482 | goto out; | 1525 | goto out; |
1483 | break; | 1526 | break; |
@@ -1490,7 +1533,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, | |||
1490 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | 1533 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
1491 | goto out; | 1534 | goto out; |
1492 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | 1535 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
1493 | cpuid_arg->entries); | 1536 | cpuid_arg->entries); |
1494 | if (r) | 1537 | if (r) |
1495 | goto out; | 1538 | goto out; |
1496 | r = -EFAULT; | 1539 | r = -EFAULT; |
@@ -1710,6 +1753,15 @@ static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |||
1710 | return r; | 1753 | return r; |
1711 | } | 1754 | } |
1712 | 1755 | ||
1756 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, | ||
1757 | struct kvm_reinject_control *control) | ||
1758 | { | ||
1759 | if (!kvm->arch.vpit) | ||
1760 | return -ENXIO; | ||
1761 | kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; | ||
1762 | return 0; | ||
1763 | } | ||
1764 | |||
1713 | /* | 1765 | /* |
1714 | * Get (and clear) the dirty memory log for a memory slot. | 1766 | * Get (and clear) the dirty memory log for a memory slot. |
1715 | */ | 1767 | */ |
@@ -1807,13 +1859,26 @@ long kvm_arch_vm_ioctl(struct file *filp, | |||
1807 | } | 1859 | } |
1808 | } else | 1860 | } else |
1809 | goto out; | 1861 | goto out; |
1862 | r = kvm_setup_default_irq_routing(kvm); | ||
1863 | if (r) { | ||
1864 | kfree(kvm->arch.vpic); | ||
1865 | kfree(kvm->arch.vioapic); | ||
1866 | goto out; | ||
1867 | } | ||
1810 | break; | 1868 | break; |
1811 | case KVM_CREATE_PIT: | 1869 | case KVM_CREATE_PIT: |
1870 | mutex_lock(&kvm->lock); | ||
1871 | r = -EEXIST; | ||
1872 | if (kvm->arch.vpit) | ||
1873 | goto create_pit_unlock; | ||
1812 | r = -ENOMEM; | 1874 | r = -ENOMEM; |
1813 | kvm->arch.vpit = kvm_create_pit(kvm); | 1875 | kvm->arch.vpit = kvm_create_pit(kvm); |
1814 | if (kvm->arch.vpit) | 1876 | if (kvm->arch.vpit) |
1815 | r = 0; | 1877 | r = 0; |
1878 | create_pit_unlock: | ||
1879 | mutex_unlock(&kvm->lock); | ||
1816 | break; | 1880 | break; |
1881 | case KVM_IRQ_LINE_STATUS: | ||
1817 | case KVM_IRQ_LINE: { | 1882 | case KVM_IRQ_LINE: { |
1818 | struct kvm_irq_level irq_event; | 1883 | struct kvm_irq_level irq_event; |
1819 | 1884 | ||
@@ -1821,10 +1886,17 @@ long kvm_arch_vm_ioctl(struct file *filp, | |||
1821 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) | 1886 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
1822 | goto out; | 1887 | goto out; |
1823 | if (irqchip_in_kernel(kvm)) { | 1888 | if (irqchip_in_kernel(kvm)) { |
1889 | __s32 status; | ||
1824 | mutex_lock(&kvm->lock); | 1890 | mutex_lock(&kvm->lock); |
1825 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, | 1891 | status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
1826 | irq_event.irq, irq_event.level); | 1892 | irq_event.irq, irq_event.level); |
1827 | mutex_unlock(&kvm->lock); | 1893 | mutex_unlock(&kvm->lock); |
1894 | if (ioctl == KVM_IRQ_LINE_STATUS) { | ||
1895 | irq_event.status = status; | ||
1896 | if (copy_to_user(argp, &irq_event, | ||
1897 | sizeof irq_event)) | ||
1898 | goto out; | ||
1899 | } | ||
1828 | r = 0; | 1900 | r = 0; |
1829 | } | 1901 | } |
1830 | break; | 1902 | break; |
@@ -1907,6 +1979,17 @@ long kvm_arch_vm_ioctl(struct file *filp, | |||
1907 | r = 0; | 1979 | r = 0; |
1908 | break; | 1980 | break; |
1909 | } | 1981 | } |
1982 | case KVM_REINJECT_CONTROL: { | ||
1983 | struct kvm_reinject_control control; | ||
1984 | r = -EFAULT; | ||
1985 | if (copy_from_user(&control, argp, sizeof(control))) | ||
1986 | goto out; | ||
1987 | r = kvm_vm_ioctl_reinject(kvm, &control); | ||
1988 | if (r) | ||
1989 | goto out; | ||
1990 | r = 0; | ||
1991 | break; | ||
1992 | } | ||
1910 | default: | 1993 | default: |
1911 | ; | 1994 | ; |
1912 | } | 1995 | } |
@@ -1960,10 +2043,38 @@ static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, | |||
1960 | return dev; | 2043 | return dev; |
1961 | } | 2044 | } |
1962 | 2045 | ||
1963 | int emulator_read_std(unsigned long addr, | 2046 | static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, |
1964 | void *val, | 2047 | struct kvm_vcpu *vcpu) |
1965 | unsigned int bytes, | 2048 | { |
1966 | struct kvm_vcpu *vcpu) | 2049 | void *data = val; |
2050 | int r = X86EMUL_CONTINUE; | ||
2051 | |||
2052 | while (bytes) { | ||
2053 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); | ||
2054 | unsigned offset = addr & (PAGE_SIZE-1); | ||
2055 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); | ||
2056 | int ret; | ||
2057 | |||
2058 | if (gpa == UNMAPPED_GVA) { | ||
2059 | r = X86EMUL_PROPAGATE_FAULT; | ||
2060 | goto out; | ||
2061 | } | ||
2062 | ret = kvm_read_guest(vcpu->kvm, gpa, data, toread); | ||
2063 | if (ret < 0) { | ||
2064 | r = X86EMUL_UNHANDLEABLE; | ||
2065 | goto out; | ||
2066 | } | ||
2067 | |||
2068 | bytes -= toread; | ||
2069 | data += toread; | ||
2070 | addr += toread; | ||
2071 | } | ||
2072 | out: | ||
2073 | return r; | ||
2074 | } | ||
2075 | |||
2076 | static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes, | ||
2077 | struct kvm_vcpu *vcpu) | ||
1967 | { | 2078 | { |
1968 | void *data = val; | 2079 | void *data = val; |
1969 | int r = X86EMUL_CONTINUE; | 2080 | int r = X86EMUL_CONTINUE; |
@@ -1971,27 +2082,27 @@ int emulator_read_std(unsigned long addr, | |||
1971 | while (bytes) { | 2082 | while (bytes) { |
1972 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); | 2083 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
1973 | unsigned offset = addr & (PAGE_SIZE-1); | 2084 | unsigned offset = addr & (PAGE_SIZE-1); |
1974 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | 2085 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
1975 | int ret; | 2086 | int ret; |
1976 | 2087 | ||
1977 | if (gpa == UNMAPPED_GVA) { | 2088 | if (gpa == UNMAPPED_GVA) { |
1978 | r = X86EMUL_PROPAGATE_FAULT; | 2089 | r = X86EMUL_PROPAGATE_FAULT; |
1979 | goto out; | 2090 | goto out; |
1980 | } | 2091 | } |
1981 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); | 2092 | ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite); |
1982 | if (ret < 0) { | 2093 | if (ret < 0) { |
1983 | r = X86EMUL_UNHANDLEABLE; | 2094 | r = X86EMUL_UNHANDLEABLE; |
1984 | goto out; | 2095 | goto out; |
1985 | } | 2096 | } |
1986 | 2097 | ||
1987 | bytes -= tocopy; | 2098 | bytes -= towrite; |
1988 | data += tocopy; | 2099 | data += towrite; |
1989 | addr += tocopy; | 2100 | addr += towrite; |
1990 | } | 2101 | } |
1991 | out: | 2102 | out: |
1992 | return r; | 2103 | return r; |
1993 | } | 2104 | } |
1994 | EXPORT_SYMBOL_GPL(emulator_read_std); | 2105 | |
1995 | 2106 | ||
1996 | static int emulator_read_emulated(unsigned long addr, | 2107 | static int emulator_read_emulated(unsigned long addr, |
1997 | void *val, | 2108 | void *val, |
@@ -2013,8 +2124,8 @@ static int emulator_read_emulated(unsigned long addr, | |||
2013 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | 2124 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
2014 | goto mmio; | 2125 | goto mmio; |
2015 | 2126 | ||
2016 | if (emulator_read_std(addr, val, bytes, vcpu) | 2127 | if (kvm_read_guest_virt(addr, val, bytes, vcpu) |
2017 | == X86EMUL_CONTINUE) | 2128 | == X86EMUL_CONTINUE) |
2018 | return X86EMUL_CONTINUE; | 2129 | return X86EMUL_CONTINUE; |
2019 | if (gpa == UNMAPPED_GVA) | 2130 | if (gpa == UNMAPPED_GVA) |
2020 | return X86EMUL_PROPAGATE_FAULT; | 2131 | return X86EMUL_PROPAGATE_FAULT; |
@@ -2217,7 +2328,7 @@ void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) | |||
2217 | 2328 | ||
2218 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); | 2329 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
2219 | 2330 | ||
2220 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); | 2331 | kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu); |
2221 | 2332 | ||
2222 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", | 2333 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
2223 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | 2334 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
@@ -2225,7 +2336,7 @@ void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) | |||
2225 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); | 2336 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
2226 | 2337 | ||
2227 | static struct x86_emulate_ops emulate_ops = { | 2338 | static struct x86_emulate_ops emulate_ops = { |
2228 | .read_std = emulator_read_std, | 2339 | .read_std = kvm_read_guest_virt, |
2229 | .read_emulated = emulator_read_emulated, | 2340 | .read_emulated = emulator_read_emulated, |
2230 | .write_emulated = emulator_write_emulated, | 2341 | .write_emulated = emulator_write_emulated, |
2231 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | 2342 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
@@ -2327,40 +2438,19 @@ int emulate_instruction(struct kvm_vcpu *vcpu, | |||
2327 | } | 2438 | } |
2328 | EXPORT_SYMBOL_GPL(emulate_instruction); | 2439 | EXPORT_SYMBOL_GPL(emulate_instruction); |
2329 | 2440 | ||
2330 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) | ||
2331 | { | ||
2332 | int i; | ||
2333 | |||
2334 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i) | ||
2335 | if (vcpu->arch.pio.guest_pages[i]) { | ||
2336 | kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]); | ||
2337 | vcpu->arch.pio.guest_pages[i] = NULL; | ||
2338 | } | ||
2339 | } | ||
2340 | |||
2341 | static int pio_copy_data(struct kvm_vcpu *vcpu) | 2441 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
2342 | { | 2442 | { |
2343 | void *p = vcpu->arch.pio_data; | 2443 | void *p = vcpu->arch.pio_data; |
2344 | void *q; | 2444 | gva_t q = vcpu->arch.pio.guest_gva; |
2345 | unsigned bytes; | 2445 | unsigned bytes; |
2346 | int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1; | 2446 | int ret; |
2347 | 2447 | ||
2348 | q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE, | ||
2349 | PAGE_KERNEL); | ||
2350 | if (!q) { | ||
2351 | free_pio_guest_pages(vcpu); | ||
2352 | return -ENOMEM; | ||
2353 | } | ||
2354 | q += vcpu->arch.pio.guest_page_offset; | ||
2355 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; | 2448 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; |
2356 | if (vcpu->arch.pio.in) | 2449 | if (vcpu->arch.pio.in) |
2357 | memcpy(q, p, bytes); | 2450 | ret = kvm_write_guest_virt(q, p, bytes, vcpu); |
2358 | else | 2451 | else |
2359 | memcpy(p, q, bytes); | 2452 | ret = kvm_read_guest_virt(q, p, bytes, vcpu); |
2360 | q -= vcpu->arch.pio.guest_page_offset; | 2453 | return ret; |
2361 | vunmap(q); | ||
2362 | free_pio_guest_pages(vcpu); | ||
2363 | return 0; | ||
2364 | } | 2454 | } |
2365 | 2455 | ||
2366 | int complete_pio(struct kvm_vcpu *vcpu) | 2456 | int complete_pio(struct kvm_vcpu *vcpu) |
@@ -2471,7 +2561,6 @@ int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2471 | vcpu->arch.pio.in = in; | 2561 | vcpu->arch.pio.in = in; |
2472 | vcpu->arch.pio.string = 0; | 2562 | vcpu->arch.pio.string = 0; |
2473 | vcpu->arch.pio.down = 0; | 2563 | vcpu->arch.pio.down = 0; |
2474 | vcpu->arch.pio.guest_page_offset = 0; | ||
2475 | vcpu->arch.pio.rep = 0; | 2564 | vcpu->arch.pio.rep = 0; |
2476 | 2565 | ||
2477 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) | 2566 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
@@ -2499,9 +2588,7 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2499 | gva_t address, int rep, unsigned port) | 2588 | gva_t address, int rep, unsigned port) |
2500 | { | 2589 | { |
2501 | unsigned now, in_page; | 2590 | unsigned now, in_page; |
2502 | int i, ret = 0; | 2591 | int ret = 0; |
2503 | int nr_pages = 1; | ||
2504 | struct page *page; | ||
2505 | struct kvm_io_device *pio_dev; | 2592 | struct kvm_io_device *pio_dev; |
2506 | 2593 | ||
2507 | vcpu->run->exit_reason = KVM_EXIT_IO; | 2594 | vcpu->run->exit_reason = KVM_EXIT_IO; |
@@ -2513,7 +2600,6 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2513 | vcpu->arch.pio.in = in; | 2600 | vcpu->arch.pio.in = in; |
2514 | vcpu->arch.pio.string = 1; | 2601 | vcpu->arch.pio.string = 1; |
2515 | vcpu->arch.pio.down = down; | 2602 | vcpu->arch.pio.down = down; |
2516 | vcpu->arch.pio.guest_page_offset = offset_in_page(address); | ||
2517 | vcpu->arch.pio.rep = rep; | 2603 | vcpu->arch.pio.rep = rep; |
2518 | 2604 | ||
2519 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) | 2605 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
@@ -2533,15 +2619,8 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2533 | else | 2619 | else |
2534 | in_page = offset_in_page(address) + size; | 2620 | in_page = offset_in_page(address) + size; |
2535 | now = min(count, (unsigned long)in_page / size); | 2621 | now = min(count, (unsigned long)in_page / size); |
2536 | if (!now) { | 2622 | if (!now) |
2537 | /* | ||
2538 | * String I/O straddles page boundary. Pin two guest pages | ||
2539 | * so that we satisfy atomicity constraints. Do just one | ||
2540 | * transaction to avoid complexity. | ||
2541 | */ | ||
2542 | nr_pages = 2; | ||
2543 | now = 1; | 2623 | now = 1; |
2544 | } | ||
2545 | if (down) { | 2624 | if (down) { |
2546 | /* | 2625 | /* |
2547 | * String I/O in reverse. Yuck. Kill the guest, fix later. | 2626 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
@@ -2556,15 +2635,7 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2556 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) | 2635 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
2557 | kvm_x86_ops->skip_emulated_instruction(vcpu); | 2636 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
2558 | 2637 | ||
2559 | for (i = 0; i < nr_pages; ++i) { | 2638 | vcpu->arch.pio.guest_gva = address; |
2560 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); | ||
2561 | vcpu->arch.pio.guest_pages[i] = page; | ||
2562 | if (!page) { | ||
2563 | kvm_inject_gp(vcpu, 0); | ||
2564 | free_pio_guest_pages(vcpu); | ||
2565 | return 1; | ||
2566 | } | ||
2567 | } | ||
2568 | 2639 | ||
2569 | pio_dev = vcpu_find_pio_dev(vcpu, port, | 2640 | pio_dev = vcpu_find_pio_dev(vcpu, port, |
2570 | vcpu->arch.pio.cur_count, | 2641 | vcpu->arch.pio.cur_count, |
@@ -2572,7 +2643,11 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2572 | if (!vcpu->arch.pio.in) { | 2643 | if (!vcpu->arch.pio.in) { |
2573 | /* string PIO write */ | 2644 | /* string PIO write */ |
2574 | ret = pio_copy_data(vcpu); | 2645 | ret = pio_copy_data(vcpu); |
2575 | if (ret >= 0 && pio_dev) { | 2646 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2647 | kvm_inject_gp(vcpu, 0); | ||
2648 | return 1; | ||
2649 | } | ||
2650 | if (ret == 0 && pio_dev) { | ||
2576 | pio_string_write(pio_dev, vcpu); | 2651 | pio_string_write(pio_dev, vcpu); |
2577 | complete_pio(vcpu); | 2652 | complete_pio(vcpu); |
2578 | if (vcpu->arch.pio.count == 0) | 2653 | if (vcpu->arch.pio.count == 0) |
@@ -2587,9 +2662,72 @@ int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |||
2587 | } | 2662 | } |
2588 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); | 2663 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
2589 | 2664 | ||
2665 | static void bounce_off(void *info) | ||
2666 | { | ||
2667 | /* nothing */ | ||
2668 | } | ||
2669 | |||
2670 | static unsigned int ref_freq; | ||
2671 | static unsigned long tsc_khz_ref; | ||
2672 | |||
2673 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | ||
2674 | void *data) | ||
2675 | { | ||
2676 | struct cpufreq_freqs *freq = data; | ||
2677 | struct kvm *kvm; | ||
2678 | struct kvm_vcpu *vcpu; | ||
2679 | int i, send_ipi = 0; | ||
2680 | |||
2681 | if (!ref_freq) | ||
2682 | ref_freq = freq->old; | ||
2683 | |||
2684 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) | ||
2685 | return 0; | ||
2686 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) | ||
2687 | return 0; | ||
2688 | per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new); | ||
2689 | |||
2690 | spin_lock(&kvm_lock); | ||
2691 | list_for_each_entry(kvm, &vm_list, vm_list) { | ||
2692 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | ||
2693 | vcpu = kvm->vcpus[i]; | ||
2694 | if (!vcpu) | ||
2695 | continue; | ||
2696 | if (vcpu->cpu != freq->cpu) | ||
2697 | continue; | ||
2698 | if (!kvm_request_guest_time_update(vcpu)) | ||
2699 | continue; | ||
2700 | if (vcpu->cpu != smp_processor_id()) | ||
2701 | send_ipi++; | ||
2702 | } | ||
2703 | } | ||
2704 | spin_unlock(&kvm_lock); | ||
2705 | |||
2706 | if (freq->old < freq->new && send_ipi) { | ||
2707 | /* | ||
2708 | * We upscale the frequency. Must make the guest | ||
2709 | * doesn't see old kvmclock values while running with | ||
2710 | * the new frequency, otherwise we risk the guest sees | ||
2711 | * time go backwards. | ||
2712 | * | ||
2713 | * In case we update the frequency for another cpu | ||
2714 | * (which might be in guest context) send an interrupt | ||
2715 | * to kick the cpu out of guest context. Next time | ||
2716 | * guest context is entered kvmclock will be updated, | ||
2717 | * so the guest will not see stale values. | ||
2718 | */ | ||
2719 | smp_call_function_single(freq->cpu, bounce_off, NULL, 1); | ||
2720 | } | ||
2721 | return 0; | ||
2722 | } | ||
2723 | |||
2724 | static struct notifier_block kvmclock_cpufreq_notifier_block = { | ||
2725 | .notifier_call = kvmclock_cpufreq_notifier | ||
2726 | }; | ||
2727 | |||
2590 | int kvm_arch_init(void *opaque) | 2728 | int kvm_arch_init(void *opaque) |
2591 | { | 2729 | { |
2592 | int r; | 2730 | int r, cpu; |
2593 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; | 2731 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
2594 | 2732 | ||
2595 | if (kvm_x86_ops) { | 2733 | if (kvm_x86_ops) { |
@@ -2620,6 +2758,15 @@ int kvm_arch_init(void *opaque) | |||
2620 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); | 2758 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
2621 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, | 2759 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
2622 | PT_DIRTY_MASK, PT64_NX_MASK, 0, 0); | 2760 | PT_DIRTY_MASK, PT64_NX_MASK, 0, 0); |
2761 | |||
2762 | for_each_possible_cpu(cpu) | ||
2763 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; | ||
2764 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { | ||
2765 | tsc_khz_ref = tsc_khz; | ||
2766 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, | ||
2767 | CPUFREQ_TRANSITION_NOTIFIER); | ||
2768 | } | ||
2769 | |||
2623 | return 0; | 2770 | return 0; |
2624 | 2771 | ||
2625 | out: | 2772 | out: |
@@ -2827,25 +2974,20 @@ static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, | |||
2827 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) | 2974 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
2828 | return 0; | 2975 | return 0; |
2829 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && | 2976 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
2830 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) | 2977 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
2831 | return 0; | 2978 | return 0; |
2832 | return 1; | 2979 | return 1; |
2833 | } | 2980 | } |
2834 | 2981 | ||
2835 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) | 2982 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
2983 | u32 function, u32 index) | ||
2836 | { | 2984 | { |
2837 | int i; | 2985 | int i; |
2838 | u32 function, index; | 2986 | struct kvm_cpuid_entry2 *best = NULL; |
2839 | struct kvm_cpuid_entry2 *e, *best; | ||
2840 | 2987 | ||
2841 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); | ||
2842 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); | ||
2843 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); | ||
2844 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); | ||
2845 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); | ||
2846 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); | ||
2847 | best = NULL; | ||
2848 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { | 2988 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
2989 | struct kvm_cpuid_entry2 *e; | ||
2990 | |||
2849 | e = &vcpu->arch.cpuid_entries[i]; | 2991 | e = &vcpu->arch.cpuid_entries[i]; |
2850 | if (is_matching_cpuid_entry(e, function, index)) { | 2992 | if (is_matching_cpuid_entry(e, function, index)) { |
2851 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) | 2993 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
@@ -2860,6 +3002,21 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) | |||
2860 | if (!best || e->function > best->function) | 3002 | if (!best || e->function > best->function) |
2861 | best = e; | 3003 | best = e; |
2862 | } | 3004 | } |
3005 | return best; | ||
3006 | } | ||
3007 | |||
3008 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) | ||
3009 | { | ||
3010 | u32 function, index; | ||
3011 | struct kvm_cpuid_entry2 *best; | ||
3012 | |||
3013 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); | ||
3014 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); | ||
3015 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); | ||
3016 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); | ||
3017 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); | ||
3018 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); | ||
3019 | best = kvm_find_cpuid_entry(vcpu, function, index); | ||
2863 | if (best) { | 3020 | if (best) { |
2864 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); | 3021 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
2865 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); | 3022 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
@@ -2945,6 +3102,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |||
2945 | if (vcpu->requests) { | 3102 | if (vcpu->requests) { |
2946 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) | 3103 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) |
2947 | __kvm_migrate_timers(vcpu); | 3104 | __kvm_migrate_timers(vcpu); |
3105 | if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests)) | ||
3106 | kvm_write_guest_time(vcpu); | ||
2948 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) | 3107 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) |
2949 | kvm_mmu_sync_roots(vcpu); | 3108 | kvm_mmu_sync_roots(vcpu); |
2950 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) | 3109 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
@@ -2979,9 +3138,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |||
2979 | goto out; | 3138 | goto out; |
2980 | } | 3139 | } |
2981 | 3140 | ||
2982 | if (vcpu->guest_debug.enabled) | ||
2983 | kvm_x86_ops->guest_debug_pre(vcpu); | ||
2984 | |||
2985 | vcpu->guest_mode = 1; | 3141 | vcpu->guest_mode = 1; |
2986 | /* | 3142 | /* |
2987 | * Make sure that guest_mode assignment won't happen after | 3143 | * Make sure that guest_mode assignment won't happen after |
@@ -3002,10 +3158,34 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |||
3002 | 3158 | ||
3003 | kvm_guest_enter(); | 3159 | kvm_guest_enter(); |
3004 | 3160 | ||
3161 | get_debugreg(vcpu->arch.host_dr6, 6); | ||
3162 | get_debugreg(vcpu->arch.host_dr7, 7); | ||
3163 | if (unlikely(vcpu->arch.switch_db_regs)) { | ||
3164 | get_debugreg(vcpu->arch.host_db[0], 0); | ||
3165 | get_debugreg(vcpu->arch.host_db[1], 1); | ||
3166 | get_debugreg(vcpu->arch.host_db[2], 2); | ||
3167 | get_debugreg(vcpu->arch.host_db[3], 3); | ||
3168 | |||
3169 | set_debugreg(0, 7); | ||
3170 | set_debugreg(vcpu->arch.eff_db[0], 0); | ||
3171 | set_debugreg(vcpu->arch.eff_db[1], 1); | ||
3172 | set_debugreg(vcpu->arch.eff_db[2], 2); | ||
3173 | set_debugreg(vcpu->arch.eff_db[3], 3); | ||
3174 | } | ||
3005 | 3175 | ||
3006 | KVMTRACE_0D(VMENTRY, vcpu, entryexit); | 3176 | KVMTRACE_0D(VMENTRY, vcpu, entryexit); |
3007 | kvm_x86_ops->run(vcpu, kvm_run); | 3177 | kvm_x86_ops->run(vcpu, kvm_run); |
3008 | 3178 | ||
3179 | if (unlikely(vcpu->arch.switch_db_regs)) { | ||
3180 | set_debugreg(0, 7); | ||
3181 | set_debugreg(vcpu->arch.host_db[0], 0); | ||
3182 | set_debugreg(vcpu->arch.host_db[1], 1); | ||
3183 | set_debugreg(vcpu->arch.host_db[2], 2); | ||
3184 | set_debugreg(vcpu->arch.host_db[3], 3); | ||
3185 | } | ||
3186 | set_debugreg(vcpu->arch.host_dr6, 6); | ||
3187 | set_debugreg(vcpu->arch.host_dr7, 7); | ||
3188 | |||
3009 | vcpu->guest_mode = 0; | 3189 | vcpu->guest_mode = 0; |
3010 | local_irq_enable(); | 3190 | local_irq_enable(); |
3011 | 3191 | ||
@@ -3192,7 +3372,7 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |||
3192 | /* | 3372 | /* |
3193 | * Don't leak debug flags in case they were set for guest debugging | 3373 | * Don't leak debug flags in case they were set for guest debugging |
3194 | */ | 3374 | */ |
3195 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | 3375 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
3196 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | 3376 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
3197 | 3377 | ||
3198 | vcpu_put(vcpu); | 3378 | vcpu_put(vcpu); |
@@ -3811,15 +3991,32 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |||
3811 | return 0; | 3991 | return 0; |
3812 | } | 3992 | } |
3813 | 3993 | ||
3814 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, | 3994 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
3815 | struct kvm_debug_guest *dbg) | 3995 | struct kvm_guest_debug *dbg) |
3816 | { | 3996 | { |
3817 | int r; | 3997 | int i, r; |
3818 | 3998 | ||
3819 | vcpu_load(vcpu); | 3999 | vcpu_load(vcpu); |
3820 | 4000 | ||
4001 | if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) == | ||
4002 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) { | ||
4003 | for (i = 0; i < KVM_NR_DB_REGS; ++i) | ||
4004 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; | ||
4005 | vcpu->arch.switch_db_regs = | ||
4006 | (dbg->arch.debugreg[7] & DR7_BP_EN_MASK); | ||
4007 | } else { | ||
4008 | for (i = 0; i < KVM_NR_DB_REGS; i++) | ||
4009 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | ||
4010 | vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK); | ||
4011 | } | ||
4012 | |||
3821 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); | 4013 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
3822 | 4014 | ||
4015 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) | ||
4016 | kvm_queue_exception(vcpu, DB_VECTOR); | ||
4017 | else if (dbg->control & KVM_GUESTDBG_INJECT_BP) | ||
4018 | kvm_queue_exception(vcpu, BP_VECTOR); | ||
4019 | |||
3823 | vcpu_put(vcpu); | 4020 | vcpu_put(vcpu); |
3824 | 4021 | ||
3825 | return r; | 4022 | return r; |
@@ -4007,6 +4204,11 @@ int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) | |||
4007 | vcpu->arch.nmi_pending = false; | 4204 | vcpu->arch.nmi_pending = false; |
4008 | vcpu->arch.nmi_injected = false; | 4205 | vcpu->arch.nmi_injected = false; |
4009 | 4206 | ||
4207 | vcpu->arch.switch_db_regs = 0; | ||
4208 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); | ||
4209 | vcpu->arch.dr6 = DR6_FIXED_1; | ||
4210 | vcpu->arch.dr7 = DR7_FIXED_1; | ||
4211 | |||
4010 | return kvm_x86_ops->vcpu_reset(vcpu); | 4212 | return kvm_x86_ops->vcpu_reset(vcpu); |
4011 | } | 4213 | } |
4012 | 4214 | ||
@@ -4100,6 +4302,8 @@ struct kvm *kvm_arch_create_vm(void) | |||
4100 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ | 4302 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
4101 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); | 4303 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
4102 | 4304 | ||
4305 | rdtscll(kvm->arch.vm_init_tsc); | ||
4306 | |||
4103 | return kvm; | 4307 | return kvm; |
4104 | } | 4308 | } |
4105 | 4309 | ||