aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGleb Natapov <gleb@redhat.com>2009-06-09 08:56:26 -0400
committerAvi Kivity <avi@redhat.com>2009-09-10 01:32:51 -0400
commitc5af89b68abb26eea5e745f33228f4d672f115e5 (patch)
treeaad12571ebedea7b869adbeec27e614c498715f4
parentd555c333aa544b222fe077adcd5dfea024b2c913 (diff)
KVM: Introduce kvm_vcpu_is_bsp() function.
Use it instead of open code "vcpu_id zero is BSP" assumption. Signed-off-by: Gleb Natapov <gleb@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
-rw-r--r--arch/ia64/kvm/kvm-ia64.c2
-rw-r--r--arch/ia64/kvm/vcpu.c2
-rw-r--r--arch/x86/kvm/i8254.c4
-rw-r--r--arch/x86/kvm/i8259.c6
-rw-r--r--arch/x86/kvm/lapic.c7
-rw-r--r--arch/x86/kvm/svm.c4
-rw-r--r--arch/x86/kvm/vmx.c6
-rw-r--r--arch/x86/kvm/x86.c4
-rw-r--r--include/linux/kvm_host.h5
-rw-r--r--virt/kvm/ioapic.c4
-rw-r--r--virt/kvm/kvm_main.c2
11 files changed, 28 insertions, 18 deletions
diff --git a/arch/ia64/kvm/kvm-ia64.c b/arch/ia64/kvm/kvm-ia64.c
index 8dde36953af3..4082665ace0a 100644
--- a/arch/ia64/kvm/kvm-ia64.c
+++ b/arch/ia64/kvm/kvm-ia64.c
@@ -1216,7 +1216,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1216 if (IS_ERR(vmm_vcpu)) 1216 if (IS_ERR(vmm_vcpu))
1217 return PTR_ERR(vmm_vcpu); 1217 return PTR_ERR(vmm_vcpu);
1218 1218
1219 if (vcpu->vcpu_id == 0) { 1219 if (kvm_vcpu_is_bsp(vcpu)) {
1220 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; 1220 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1221 1221
1222 /*Set entry address for first run.*/ 1222 /*Set entry address for first run.*/
diff --git a/arch/ia64/kvm/vcpu.c b/arch/ia64/kvm/vcpu.c
index cc406d064a09..61a3320b62c1 100644
--- a/arch/ia64/kvm/vcpu.c
+++ b/arch/ia64/kvm/vcpu.c
@@ -830,7 +830,7 @@ static void vcpu_set_itc(struct kvm_vcpu *vcpu, u64 val)
830 830
831 kvm = (struct kvm *)KVM_VM_BASE; 831 kvm = (struct kvm *)KVM_VM_BASE;
832 832
833 if (vcpu->vcpu_id == 0) { 833 if (kvm_vcpu_is_bsp(vcpu)) {
834 for (i = 0; i < kvm->arch.online_vcpus; i++) { 834 for (i = 0; i < kvm->arch.online_vcpus; i++) {
835 v = (struct kvm_vcpu *)((char *)vcpu + 835 v = (struct kvm_vcpu *)((char *)vcpu +
836 sizeof(struct kvm_vcpu_data) * i); 836 sizeof(struct kvm_vcpu_data) * i);
diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
index 3837db65d33e..008a83185067 100644
--- a/arch/x86/kvm/i8254.c
+++ b/arch/x86/kvm/i8254.c
@@ -231,7 +231,7 @@ int pit_has_pending_timer(struct kvm_vcpu *vcpu)
231{ 231{
232 struct kvm_pit *pit = vcpu->kvm->arch.vpit; 232 struct kvm_pit *pit = vcpu->kvm->arch.vpit;
233 233
234 if (pit && vcpu->vcpu_id == 0 && pit->pit_state.irq_ack) 234 if (pit && kvm_vcpu_is_bsp(vcpu) && pit->pit_state.irq_ack)
235 return atomic_read(&pit->pit_state.pit_timer.pending); 235 return atomic_read(&pit->pit_state.pit_timer.pending);
236 return 0; 236 return 0;
237} 237}
@@ -252,7 +252,7 @@ void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
252 struct kvm_pit *pit = vcpu->kvm->arch.vpit; 252 struct kvm_pit *pit = vcpu->kvm->arch.vpit;
253 struct hrtimer *timer; 253 struct hrtimer *timer;
254 254
255 if (vcpu->vcpu_id != 0 || !pit) 255 if (!kvm_vcpu_is_bsp(vcpu) || !pit)
256 return; 256 return;
257 257
258 timer = &pit->pit_state.pit_timer.timer; 258 timer = &pit->pit_state.pit_timer.timer;
diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c
index bf94a45f4f86..148c52a608d6 100644
--- a/arch/x86/kvm/i8259.c
+++ b/arch/x86/kvm/i8259.c
@@ -57,7 +57,7 @@ static void pic_unlock(struct kvm_pic *s)
57 } 57 }
58 58
59 if (wakeup) { 59 if (wakeup) {
60 vcpu = s->kvm->vcpus[0]; 60 vcpu = s->kvm->bsp_vcpu;
61 if (vcpu) 61 if (vcpu)
62 kvm_vcpu_kick(vcpu); 62 kvm_vcpu_kick(vcpu);
63 } 63 }
@@ -254,7 +254,7 @@ void kvm_pic_reset(struct kvm_kpic_state *s)
254{ 254{
255 int irq, irqbase, n; 255 int irq, irqbase, n;
256 struct kvm *kvm = s->pics_state->irq_request_opaque; 256 struct kvm *kvm = s->pics_state->irq_request_opaque;
257 struct kvm_vcpu *vcpu0 = kvm->vcpus[0]; 257 struct kvm_vcpu *vcpu0 = kvm->bsp_vcpu;
258 258
259 if (s == &s->pics_state->pics[0]) 259 if (s == &s->pics_state->pics[0])
260 irqbase = 0; 260 irqbase = 0;
@@ -512,7 +512,7 @@ static void picdev_read(struct kvm_io_device *this,
512static void pic_irq_request(void *opaque, int level) 512static void pic_irq_request(void *opaque, int level)
513{ 513{
514 struct kvm *kvm = opaque; 514 struct kvm *kvm = opaque;
515 struct kvm_vcpu *vcpu = kvm->vcpus[0]; 515 struct kvm_vcpu *vcpu = kvm->bsp_vcpu;
516 struct kvm_pic *s = pic_irqchip(kvm); 516 struct kvm_pic *s = pic_irqchip(kvm);
517 int irq = pic_get_irq(&s->pics[0]); 517 int irq = pic_get_irq(&s->pics[0]);
518 518
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 44f20cdb5709..b0661300eb28 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -793,7 +793,8 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
793 vcpu->arch.apic_base = value; 793 vcpu->arch.apic_base = value;
794 return; 794 return;
795 } 795 }
796 if (apic->vcpu->vcpu_id) 796
797 if (!kvm_vcpu_is_bsp(apic->vcpu))
797 value &= ~MSR_IA32_APICBASE_BSP; 798 value &= ~MSR_IA32_APICBASE_BSP;
798 799
799 vcpu->arch.apic_base = value; 800 vcpu->arch.apic_base = value;
@@ -844,7 +845,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu)
844 } 845 }
845 update_divide_count(apic); 846 update_divide_count(apic);
846 atomic_set(&apic->lapic_timer.pending, 0); 847 atomic_set(&apic->lapic_timer.pending, 0);
847 if (vcpu->vcpu_id == 0) 848 if (kvm_vcpu_is_bsp(vcpu))
848 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; 849 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
849 apic_update_ppr(apic); 850 apic_update_ppr(apic);
850 851
@@ -985,7 +986,7 @@ int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
985 u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0); 986 u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
986 int r = 0; 987 int r = 0;
987 988
988 if (vcpu->vcpu_id == 0) { 989 if (kvm_vcpu_is_bsp(vcpu)) {
989 if (!apic_hw_enabled(vcpu->arch.apic)) 990 if (!apic_hw_enabled(vcpu->arch.apic))
990 r = 1; 991 r = 1;
991 if ((lvt0 & APIC_LVT_MASKED) == 0 && 992 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index 7749b0692cb2..28b981409a8f 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -642,7 +642,7 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
642 642
643 init_vmcb(svm); 643 init_vmcb(svm);
644 644
645 if (vcpu->vcpu_id != 0) { 645 if (!kvm_vcpu_is_bsp(vcpu)) {
646 kvm_rip_write(vcpu, 0); 646 kvm_rip_write(vcpu, 0);
647 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; 647 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
648 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; 648 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
@@ -706,7 +706,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
706 fx_init(&svm->vcpu); 706 fx_init(&svm->vcpu);
707 svm->vcpu.fpu_active = 1; 707 svm->vcpu.fpu_active = 1;
708 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; 708 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
709 if (svm->vcpu.vcpu_id == 0) 709 if (kvm_vcpu_is_bsp(&svm->vcpu))
710 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; 710 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
711 711
712 return &svm->vcpu; 712 return &svm->vcpu;
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index ae682929a642..c08bb4cf372e 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -2411,7 +2411,7 @@ static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2411 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); 2411 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
2412 kvm_set_cr8(&vmx->vcpu, 0); 2412 kvm_set_cr8(&vmx->vcpu, 0);
2413 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; 2413 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2414 if (vmx->vcpu.vcpu_id == 0) 2414 if (kvm_vcpu_is_bsp(&vmx->vcpu))
2415 msr |= MSR_IA32_APICBASE_BSP; 2415 msr |= MSR_IA32_APICBASE_BSP;
2416 kvm_set_apic_base(&vmx->vcpu, msr); 2416 kvm_set_apic_base(&vmx->vcpu, msr);
2417 2417
@@ -2422,7 +2422,7 @@ static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2422 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode 2422 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2423 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. 2423 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2424 */ 2424 */
2425 if (vmx->vcpu.vcpu_id == 0) { 2425 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
2426 vmcs_write16(GUEST_CS_SELECTOR, 0xf000); 2426 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2427 vmcs_writel(GUEST_CS_BASE, 0x000f0000); 2427 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2428 } else { 2428 } else {
@@ -2451,7 +2451,7 @@ static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2451 vmcs_writel(GUEST_SYSENTER_EIP, 0); 2451 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2452 2452
2453 vmcs_writel(GUEST_RFLAGS, 0x02); 2453 vmcs_writel(GUEST_RFLAGS, 0x02);
2454 if (vmx->vcpu.vcpu_id == 0) 2454 if (kvm_vcpu_is_bsp(&vmx->vcpu))
2455 kvm_rip_write(vcpu, 0xfff0); 2455 kvm_rip_write(vcpu, 0xfff0);
2456 else 2456 else
2457 kvm_rip_write(vcpu, 0); 2457 kvm_rip_write(vcpu, 0);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e877efa37620..d8adc1da76dd 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4330,7 +4330,7 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4330 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); 4330 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4331 4331
4332 /* Older userspace won't unhalt the vcpu on reset. */ 4332 /* Older userspace won't unhalt the vcpu on reset. */
4333 if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 && 4333 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
4334 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && 4334 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4335 !(vcpu->arch.cr0 & X86_CR0_PE)) 4335 !(vcpu->arch.cr0 & X86_CR0_PE))
4336 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; 4336 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
@@ -4601,7 +4601,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4601 kvm = vcpu->kvm; 4601 kvm = vcpu->kvm;
4602 4602
4603 vcpu->arch.mmu.root_hpa = INVALID_PAGE; 4603 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
4604 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0) 4604 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
4605 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; 4605 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4606 else 4606 else
4607 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; 4607 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index a29ea030dd8e..a5bd429e9bd3 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -131,6 +131,7 @@ struct kvm {
131 int nmemslots; 131 int nmemslots;
132 struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS + 132 struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
133 KVM_PRIVATE_MEM_SLOTS]; 133 KVM_PRIVATE_MEM_SLOTS];
134 struct kvm_vcpu *bsp_vcpu;
134 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; 135 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
135 struct list_head vm_list; 136 struct list_head vm_list;
136 struct mutex lock; 137 struct mutex lock;
@@ -549,4 +550,8 @@ static inline void kvm_irqfd_release(struct kvm *kvm) {}
549 550
550#endif /* CONFIG_HAVE_KVM_EVENTFD */ 551#endif /* CONFIG_HAVE_KVM_EVENTFD */
551 552
553static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
554{
555 return vcpu->kvm->bsp_vcpu == vcpu;
556}
552#endif 557#endif
diff --git a/virt/kvm/ioapic.c b/virt/kvm/ioapic.c
index 2a5667173995..0532fa68f5d1 100644
--- a/virt/kvm/ioapic.c
+++ b/virt/kvm/ioapic.c
@@ -164,7 +164,9 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
164 /* Always delivery PIT interrupt to vcpu 0 */ 164 /* Always delivery PIT interrupt to vcpu 0 */
165 if (irq == 0) { 165 if (irq == 0) {
166 irqe.dest_mode = 0; /* Physical mode. */ 166 irqe.dest_mode = 0; /* Physical mode. */
167 irqe.dest_id = ioapic->kvm->vcpus[0]->vcpu_id; 167 /* need to read apic_id from apic regiest since
168 * it can be rewritten */
169 irqe.dest_id = ioapic->kvm->bsp_vcpu->vcpu_id;
168 } 170 }
169#endif 171#endif
170 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe); 172 return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 0d481b282448..0d54edecbc70 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1747,6 +1747,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1747 goto vcpu_destroy; 1747 goto vcpu_destroy;
1748 } 1748 }
1749 kvm->vcpus[n] = vcpu; 1749 kvm->vcpus[n] = vcpu;
1750 if (n == 0)
1751 kvm->bsp_vcpu = vcpu;
1750 mutex_unlock(&kvm->lock); 1752 mutex_unlock(&kvm->lock);
1751 1753
1752 /* Now it's all set up, let userspace reach it */ 1754 /* Now it's all set up, let userspace reach it */