diff options
author | Gleb Natapov <gleb@redhat.com> | 2009-06-09 08:56:28 -0400 |
---|---|---|
committer | Avi Kivity <avi@redhat.com> | 2009-09-10 01:32:52 -0400 |
commit | 73880c80aa9c8dc353cd0ad26579023213cd5314 (patch) | |
tree | d5bee5c87c3468feb941cc898e342f52c0adce19 /virt/kvm | |
parent | 1ed0ce000a6c20c36ec649e32fc24393ef418ed8 (diff) |
KVM: Break dependency between vcpu index in vcpus array and vcpu_id.
Archs are free to use vcpu_id as they see fit. For x86 it is used as
vcpu's apic id. New ioctl is added to configure boot vcpu id that was
assumed to be 0 till now.
Signed-off-by: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'virt/kvm')
-rw-r--r-- | virt/kvm/Kconfig | 3 | ||||
-rw-r--r-- | virt/kvm/kvm_main.c | 64 |
2 files changed, 42 insertions, 25 deletions
diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 56c6848d2df8..daece36c0a57 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig | |||
@@ -9,3 +9,6 @@ config HAVE_KVM_IRQCHIP | |||
9 | config HAVE_KVM_EVENTFD | 9 | config HAVE_KVM_EVENTFD |
10 | bool | 10 | bool |
11 | select EVENTFD | 11 | select EVENTFD |
12 | |||
13 | config KVM_APIC_ARCHITECTURE | ||
14 | bool | ||
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0d54edecbc70..25e1f9c97b1a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c | |||
@@ -689,11 +689,6 @@ out: | |||
689 | } | 689 | } |
690 | #endif | 690 | #endif |
691 | 691 | ||
692 | static inline int valid_vcpu(int n) | ||
693 | { | ||
694 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | ||
695 | } | ||
696 | |||
697 | inline int kvm_is_mmio_pfn(pfn_t pfn) | 692 | inline int kvm_is_mmio_pfn(pfn_t pfn) |
698 | { | 693 | { |
699 | if (pfn_valid(pfn)) { | 694 | if (pfn_valid(pfn)) { |
@@ -1714,24 +1709,18 @@ static struct file_operations kvm_vcpu_fops = { | |||
1714 | */ | 1709 | */ |
1715 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | 1710 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
1716 | { | 1711 | { |
1717 | int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); | 1712 | return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); |
1718 | if (fd < 0) | ||
1719 | kvm_put_kvm(vcpu->kvm); | ||
1720 | return fd; | ||
1721 | } | 1713 | } |
1722 | 1714 | ||
1723 | /* | 1715 | /* |
1724 | * Creates some virtual cpus. Good luck creating more than one. | 1716 | * Creates some virtual cpus. Good luck creating more than one. |
1725 | */ | 1717 | */ |
1726 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | 1718 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) |
1727 | { | 1719 | { |
1728 | int r; | 1720 | int r; |
1729 | struct kvm_vcpu *vcpu; | 1721 | struct kvm_vcpu *vcpu; |
1730 | 1722 | ||
1731 | if (!valid_vcpu(n)) | 1723 | vcpu = kvm_arch_vcpu_create(kvm, id); |
1732 | return -EINVAL; | ||
1733 | |||
1734 | vcpu = kvm_arch_vcpu_create(kvm, n); | ||
1735 | if (IS_ERR(vcpu)) | 1724 | if (IS_ERR(vcpu)) |
1736 | return PTR_ERR(vcpu); | 1725 | return PTR_ERR(vcpu); |
1737 | 1726 | ||
@@ -1742,25 +1731,38 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |||
1742 | return r; | 1731 | return r; |
1743 | 1732 | ||
1744 | mutex_lock(&kvm->lock); | 1733 | mutex_lock(&kvm->lock); |
1745 | if (kvm->vcpus[n]) { | 1734 | if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) { |
1746 | r = -EEXIST; | 1735 | r = -EINVAL; |
1747 | goto vcpu_destroy; | 1736 | goto vcpu_destroy; |
1748 | } | 1737 | } |
1749 | kvm->vcpus[n] = vcpu; | 1738 | |
1750 | if (n == 0) | 1739 | for (r = 0; r < atomic_read(&kvm->online_vcpus); r++) |
1751 | kvm->bsp_vcpu = vcpu; | 1740 | if (kvm->vcpus[r]->vcpu_id == id) { |
1752 | mutex_unlock(&kvm->lock); | 1741 | r = -EEXIST; |
1742 | goto vcpu_destroy; | ||
1743 | } | ||
1744 | |||
1745 | BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); | ||
1753 | 1746 | ||
1754 | /* Now it's all set up, let userspace reach it */ | 1747 | /* Now it's all set up, let userspace reach it */ |
1755 | kvm_get_kvm(kvm); | 1748 | kvm_get_kvm(kvm); |
1756 | r = create_vcpu_fd(vcpu); | 1749 | r = create_vcpu_fd(vcpu); |
1757 | if (r < 0) | 1750 | if (r < 0) { |
1758 | goto unlink; | 1751 | kvm_put_kvm(kvm); |
1752 | goto vcpu_destroy; | ||
1753 | } | ||
1754 | |||
1755 | kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; | ||
1756 | smp_wmb(); | ||
1757 | atomic_inc(&kvm->online_vcpus); | ||
1758 | |||
1759 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE | ||
1760 | if (kvm->bsp_vcpu_id == id) | ||
1761 | kvm->bsp_vcpu = vcpu; | ||
1762 | #endif | ||
1763 | mutex_unlock(&kvm->lock); | ||
1759 | return r; | 1764 | return r; |
1760 | 1765 | ||
1761 | unlink: | ||
1762 | mutex_lock(&kvm->lock); | ||
1763 | kvm->vcpus[n] = NULL; | ||
1764 | vcpu_destroy: | 1766 | vcpu_destroy: |
1765 | mutex_unlock(&kvm->lock); | 1767 | mutex_unlock(&kvm->lock); |
1766 | kvm_arch_vcpu_destroy(vcpu); | 1768 | kvm_arch_vcpu_destroy(vcpu); |
@@ -2233,6 +2235,15 @@ static long kvm_vm_ioctl(struct file *filp, | |||
2233 | r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags); | 2235 | r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags); |
2234 | break; | 2236 | break; |
2235 | } | 2237 | } |
2238 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE | ||
2239 | case KVM_SET_BOOT_CPU_ID: | ||
2240 | r = 0; | ||
2241 | if (atomic_read(&kvm->online_vcpus) != 0) | ||
2242 | r = -EBUSY; | ||
2243 | else | ||
2244 | kvm->bsp_vcpu_id = arg; | ||
2245 | break; | ||
2246 | #endif | ||
2236 | default: | 2247 | default: |
2237 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); | 2248 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
2238 | } | 2249 | } |
@@ -2299,6 +2310,9 @@ static long kvm_dev_ioctl_check_extension_generic(long arg) | |||
2299 | case KVM_CAP_USER_MEMORY: | 2310 | case KVM_CAP_USER_MEMORY: |
2300 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: | 2311 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
2301 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: | 2312 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: |
2313 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE | ||
2314 | case KVM_CAP_SET_BOOT_CPU_ID: | ||
2315 | #endif | ||
2302 | return 1; | 2316 | return 1; |
2303 | #ifdef CONFIG_HAVE_KVM_IRQCHIP | 2317 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
2304 | case KVM_CAP_IRQ_ROUTING: | 2318 | case KVM_CAP_IRQ_ROUTING: |