aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/kvm/kvm_main.c
diff options
context:
space:
mode:
authorHollis Blanchard <hollisb@us.ibm.com>2007-11-01 15:16:10 -0400
committerAvi Kivity <avi@qumranet.com>2008-01-30 10:53:00 -0500
commitb6c7a5dccf9471f4891df722dbd0700ce56eb2e2 (patch)
treecf7c15cb74b5949940f69df9fe9bc50161d880f5 /drivers/kvm/kvm_main.c
parentd075206073286dca84768137af0a0bf3d11f0663 (diff)
KVM: Portability: Move x86 vcpu ioctl handlers to x86.c
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com> Signed-off-by: Avi Kivity <avi@qumranet.com>
Diffstat (limited to 'drivers/kvm/kvm_main.c')
-rw-r--r--drivers/kvm/kvm_main.c442
1 files changed, 6 insertions, 436 deletions
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 7230f48ba08e..8665531d9287 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -779,422 +779,6 @@ void kvm_resched(struct kvm_vcpu *vcpu)
779EXPORT_SYMBOL_GPL(kvm_resched); 779EXPORT_SYMBOL_GPL(kvm_resched);
780 780
781/* 781/*
782 * Check if userspace requested an interrupt window, and that the
783 * interrupt window is open.
784 *
785 * No need to exit to userspace if we already have an interrupt queued.
786 */
787static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
788 struct kvm_run *kvm_run)
789{
790 return (!vcpu->irq_summary &&
791 kvm_run->request_interrupt_window &&
792 vcpu->interrupt_window_open &&
793 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
794}
795
796static void post_kvm_run_save(struct kvm_vcpu *vcpu,
797 struct kvm_run *kvm_run)
798{
799 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
800 kvm_run->cr8 = get_cr8(vcpu);
801 kvm_run->apic_base = kvm_get_apic_base(vcpu);
802 if (irqchip_in_kernel(vcpu->kvm))
803 kvm_run->ready_for_interrupt_injection = 1;
804 else
805 kvm_run->ready_for_interrupt_injection =
806 (vcpu->interrupt_window_open &&
807 vcpu->irq_summary == 0);
808}
809
810static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
811{
812 int r;
813
814 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
815 pr_debug("vcpu %d received sipi with vector # %x\n",
816 vcpu->vcpu_id, vcpu->sipi_vector);
817 kvm_lapic_reset(vcpu);
818 r = kvm_x86_ops->vcpu_reset(vcpu);
819 if (r)
820 return r;
821 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
822 }
823
824preempted:
825 if (vcpu->guest_debug.enabled)
826 kvm_x86_ops->guest_debug_pre(vcpu);
827
828again:
829 r = kvm_mmu_reload(vcpu);
830 if (unlikely(r))
831 goto out;
832
833 kvm_inject_pending_timer_irqs(vcpu);
834
835 preempt_disable();
836
837 kvm_x86_ops->prepare_guest_switch(vcpu);
838 kvm_load_guest_fpu(vcpu);
839
840 local_irq_disable();
841
842 if (signal_pending(current)) {
843 local_irq_enable();
844 preempt_enable();
845 r = -EINTR;
846 kvm_run->exit_reason = KVM_EXIT_INTR;
847 ++vcpu->stat.signal_exits;
848 goto out;
849 }
850
851 if (irqchip_in_kernel(vcpu->kvm))
852 kvm_x86_ops->inject_pending_irq(vcpu);
853 else if (!vcpu->mmio_read_completed)
854 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
855
856 vcpu->guest_mode = 1;
857 kvm_guest_enter();
858
859 if (vcpu->requests)
860 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
861 kvm_x86_ops->tlb_flush(vcpu);
862
863 kvm_x86_ops->run(vcpu, kvm_run);
864
865 vcpu->guest_mode = 0;
866 local_irq_enable();
867
868 ++vcpu->stat.exits;
869
870 /*
871 * We must have an instruction between local_irq_enable() and
872 * kvm_guest_exit(), so the timer interrupt isn't delayed by
873 * the interrupt shadow. The stat.exits increment will do nicely.
874 * But we need to prevent reordering, hence this barrier():
875 */
876 barrier();
877
878 kvm_guest_exit();
879
880 preempt_enable();
881
882 /*
883 * Profile KVM exit RIPs:
884 */
885 if (unlikely(prof_on == KVM_PROFILING)) {
886 kvm_x86_ops->cache_regs(vcpu);
887 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
888 }
889
890 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
891
892 if (r > 0) {
893 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
894 r = -EINTR;
895 kvm_run->exit_reason = KVM_EXIT_INTR;
896 ++vcpu->stat.request_irq_exits;
897 goto out;
898 }
899 if (!need_resched()) {
900 ++vcpu->stat.light_exits;
901 goto again;
902 }
903 }
904
905out:
906 if (r > 0) {
907 kvm_resched(vcpu);
908 goto preempted;
909 }
910
911 post_kvm_run_save(vcpu, kvm_run);
912
913 return r;
914}
915
916
917static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
918{
919 int r;
920 sigset_t sigsaved;
921
922 vcpu_load(vcpu);
923
924 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
925 kvm_vcpu_block(vcpu);
926 vcpu_put(vcpu);
927 return -EAGAIN;
928 }
929
930 if (vcpu->sigset_active)
931 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
932
933 /* re-sync apic's tpr */
934 if (!irqchip_in_kernel(vcpu->kvm))
935 set_cr8(vcpu, kvm_run->cr8);
936
937 if (vcpu->pio.cur_count) {
938 r = complete_pio(vcpu);
939 if (r)
940 goto out;
941 }
942#if CONFIG_HAS_IOMEM
943 if (vcpu->mmio_needed) {
944 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
945 vcpu->mmio_read_completed = 1;
946 vcpu->mmio_needed = 0;
947 r = emulate_instruction(vcpu, kvm_run,
948 vcpu->mmio_fault_cr2, 0, 1);
949 if (r == EMULATE_DO_MMIO) {
950 /*
951 * Read-modify-write. Back to userspace.
952 */
953 r = 0;
954 goto out;
955 }
956 }
957#endif
958 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
959 kvm_x86_ops->cache_regs(vcpu);
960 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
961 kvm_x86_ops->decache_regs(vcpu);
962 }
963
964 r = __vcpu_run(vcpu, kvm_run);
965
966out:
967 if (vcpu->sigset_active)
968 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
969
970 vcpu_put(vcpu);
971 return r;
972}
973
974static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
975 struct kvm_regs *regs)
976{
977 vcpu_load(vcpu);
978
979 kvm_x86_ops->cache_regs(vcpu);
980
981 regs->rax = vcpu->regs[VCPU_REGS_RAX];
982 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
983 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
984 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
985 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
986 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
987 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
988 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
989#ifdef CONFIG_X86_64
990 regs->r8 = vcpu->regs[VCPU_REGS_R8];
991 regs->r9 = vcpu->regs[VCPU_REGS_R9];
992 regs->r10 = vcpu->regs[VCPU_REGS_R10];
993 regs->r11 = vcpu->regs[VCPU_REGS_R11];
994 regs->r12 = vcpu->regs[VCPU_REGS_R12];
995 regs->r13 = vcpu->regs[VCPU_REGS_R13];
996 regs->r14 = vcpu->regs[VCPU_REGS_R14];
997 regs->r15 = vcpu->regs[VCPU_REGS_R15];
998#endif
999
1000 regs->rip = vcpu->rip;
1001 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
1002
1003 /*
1004 * Don't leak debug flags in case they were set for guest debugging
1005 */
1006 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1007 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1008
1009 vcpu_put(vcpu);
1010
1011 return 0;
1012}
1013
1014static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1015 struct kvm_regs *regs)
1016{
1017 vcpu_load(vcpu);
1018
1019 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1020 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1021 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1022 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1023 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1024 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1025 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1026 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1027#ifdef CONFIG_X86_64
1028 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1029 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1030 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1031 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1032 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1033 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1034 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1035 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1036#endif
1037
1038 vcpu->rip = regs->rip;
1039 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
1040
1041 kvm_x86_ops->decache_regs(vcpu);
1042
1043 vcpu_put(vcpu);
1044
1045 return 0;
1046}
1047
1048static void get_segment(struct kvm_vcpu *vcpu,
1049 struct kvm_segment *var, int seg)
1050{
1051 return kvm_x86_ops->get_segment(vcpu, var, seg);
1052}
1053
1054static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1055 struct kvm_sregs *sregs)
1056{
1057 struct descriptor_table dt;
1058 int pending_vec;
1059
1060 vcpu_load(vcpu);
1061
1062 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1063 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1064 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1065 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1066 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1067 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1068
1069 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1070 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1071
1072 kvm_x86_ops->get_idt(vcpu, &dt);
1073 sregs->idt.limit = dt.limit;
1074 sregs->idt.base = dt.base;
1075 kvm_x86_ops->get_gdt(vcpu, &dt);
1076 sregs->gdt.limit = dt.limit;
1077 sregs->gdt.base = dt.base;
1078
1079 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
1080 sregs->cr0 = vcpu->cr0;
1081 sregs->cr2 = vcpu->cr2;
1082 sregs->cr3 = vcpu->cr3;
1083 sregs->cr4 = vcpu->cr4;
1084 sregs->cr8 = get_cr8(vcpu);
1085 sregs->efer = vcpu->shadow_efer;
1086 sregs->apic_base = kvm_get_apic_base(vcpu);
1087
1088 if (irqchip_in_kernel(vcpu->kvm)) {
1089 memset(sregs->interrupt_bitmap, 0,
1090 sizeof sregs->interrupt_bitmap);
1091 pending_vec = kvm_x86_ops->get_irq(vcpu);
1092 if (pending_vec >= 0)
1093 set_bit(pending_vec,
1094 (unsigned long *)sregs->interrupt_bitmap);
1095 } else
1096 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1097 sizeof sregs->interrupt_bitmap);
1098
1099 vcpu_put(vcpu);
1100
1101 return 0;
1102}
1103
1104static void set_segment(struct kvm_vcpu *vcpu,
1105 struct kvm_segment *var, int seg)
1106{
1107 return kvm_x86_ops->set_segment(vcpu, var, seg);
1108}
1109
1110static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1111 struct kvm_sregs *sregs)
1112{
1113 int mmu_reset_needed = 0;
1114 int i, pending_vec, max_bits;
1115 struct descriptor_table dt;
1116
1117 vcpu_load(vcpu);
1118
1119 dt.limit = sregs->idt.limit;
1120 dt.base = sregs->idt.base;
1121 kvm_x86_ops->set_idt(vcpu, &dt);
1122 dt.limit = sregs->gdt.limit;
1123 dt.base = sregs->gdt.base;
1124 kvm_x86_ops->set_gdt(vcpu, &dt);
1125
1126 vcpu->cr2 = sregs->cr2;
1127 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1128 vcpu->cr3 = sregs->cr3;
1129
1130 set_cr8(vcpu, sregs->cr8);
1131
1132 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
1133#ifdef CONFIG_X86_64
1134 kvm_x86_ops->set_efer(vcpu, sregs->efer);
1135#endif
1136 kvm_set_apic_base(vcpu, sregs->apic_base);
1137
1138 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
1139
1140 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1141 vcpu->cr0 = sregs->cr0;
1142 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
1143
1144 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1145 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
1146 if (!is_long_mode(vcpu) && is_pae(vcpu))
1147 load_pdptrs(vcpu, vcpu->cr3);
1148
1149 if (mmu_reset_needed)
1150 kvm_mmu_reset_context(vcpu);
1151
1152 if (!irqchip_in_kernel(vcpu->kvm)) {
1153 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1154 sizeof vcpu->irq_pending);
1155 vcpu->irq_summary = 0;
1156 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
1157 if (vcpu->irq_pending[i])
1158 __set_bit(i, &vcpu->irq_summary);
1159 } else {
1160 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
1161 pending_vec = find_first_bit(
1162 (const unsigned long *)sregs->interrupt_bitmap,
1163 max_bits);
1164 /* Only pending external irq is handled here */
1165 if (pending_vec < max_bits) {
1166 kvm_x86_ops->set_irq(vcpu, pending_vec);
1167 pr_debug("Set back pending irq %d\n",
1168 pending_vec);
1169 }
1170 }
1171
1172 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1173 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1174 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1175 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1176 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1177 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1178
1179 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1180 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1181
1182 vcpu_put(vcpu);
1183
1184 return 0;
1185}
1186
1187void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1188{
1189 struct kvm_segment cs;
1190
1191 get_segment(vcpu, &cs, VCPU_SREG_CS);
1192 *db = cs.db;
1193 *l = cs.l;
1194}
1195EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
1196
1197/*
1198 * Translate a guest virtual address to a guest physical address. 782 * Translate a guest virtual address to a guest physical address.
1199 */ 783 */
1200static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 784static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
@@ -1233,20 +817,6 @@ static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1233 return 0; 817 return 0;
1234} 818}
1235 819
1236static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1237 struct kvm_debug_guest *dbg)
1238{
1239 int r;
1240
1241 vcpu_load(vcpu);
1242
1243 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
1244
1245 vcpu_put(vcpu);
1246
1247 return r;
1248}
1249
1250static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, 820static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
1251 unsigned long address, 821 unsigned long address,
1252 int *type) 822 int *type)
@@ -1392,13 +962,13 @@ static long kvm_vcpu_ioctl(struct file *filp,
1392 r = -EINVAL; 962 r = -EINVAL;
1393 if (arg) 963 if (arg)
1394 goto out; 964 goto out;
1395 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); 965 r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
1396 break; 966 break;
1397 case KVM_GET_REGS: { 967 case KVM_GET_REGS: {
1398 struct kvm_regs kvm_regs; 968 struct kvm_regs kvm_regs;
1399 969
1400 memset(&kvm_regs, 0, sizeof kvm_regs); 970 memset(&kvm_regs, 0, sizeof kvm_regs);
1401 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); 971 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
1402 if (r) 972 if (r)
1403 goto out; 973 goto out;
1404 r = -EFAULT; 974 r = -EFAULT;
@@ -1413,7 +983,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
1413 r = -EFAULT; 983 r = -EFAULT;
1414 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) 984 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
1415 goto out; 985 goto out;
1416 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); 986 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
1417 if (r) 987 if (r)
1418 goto out; 988 goto out;
1419 r = 0; 989 r = 0;
@@ -1423,7 +993,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
1423 struct kvm_sregs kvm_sregs; 993 struct kvm_sregs kvm_sregs;
1424 994
1425 memset(&kvm_sregs, 0, sizeof kvm_sregs); 995 memset(&kvm_sregs, 0, sizeof kvm_sregs);
1426 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); 996 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
1427 if (r) 997 if (r)
1428 goto out; 998 goto out;
1429 r = -EFAULT; 999 r = -EFAULT;
@@ -1438,7 +1008,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
1438 r = -EFAULT; 1008 r = -EFAULT;
1439 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) 1009 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
1440 goto out; 1010 goto out;
1441 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); 1011 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
1442 if (r) 1012 if (r)
1443 goto out; 1013 goto out;
1444 r = 0; 1014 r = 0;
@@ -1477,7 +1047,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
1477 r = -EFAULT; 1047 r = -EFAULT;
1478 if (copy_from_user(&dbg, argp, sizeof dbg)) 1048 if (copy_from_user(&dbg, argp, sizeof dbg))
1479 goto out; 1049 goto out;
1480 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); 1050 r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
1481 if (r) 1051 if (r)
1482 goto out; 1052 goto out;
1483 r = 0; 1053 r = 0;