/* Interrupt controller */
        struct vgic_dist        vgic;
+       int max_vcpus;
 };
 
 #define KVM_NR_MEM_OBJS     40
 
        /* Mark the initial VMID generation invalid */
        kvm->arch.vmid_gen = 0;
 
+       /* The maximum number of VCPUs is limited by the host's GIC model */
+       kvm->arch.max_vcpus = kvm_vgic_get_max_vcpus();
+
        return ret;
 out_free_stage2_pgd:
        kvm_free_stage2_pgd(kvm);
                goto out;
        }
 
+       if (id >= kvm->arch.max_vcpus) {
+               err = -EINVAL;
+               goto out;
+       }
+
        vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
        if (!vcpu) {
                err = -ENOMEM;
 
        /* VTTBR value associated with above pgd and vmid */
        u64    vttbr;
 
+       /* The maximum number of vCPUs depends on the used GIC model */
+       int max_vcpus;
+
        /* Interrupt controller */
        struct vgic_dist        vgic;
 
 
 #define VGIC_V2_MAX_LRS                (1 << 6)
 #define VGIC_V3_MAX_LRS                16
 #define VGIC_MAX_IRQS          1024
+#define VGIC_V2_MAX_CPUS       8
 
 /* Sanity checks... */
 #if (KVM_MAX_VCPUS > 8)
        unsigned int    maint_irq;
        /* Virtual control interface base address */
        void __iomem    *vctrl_base;
+       int             max_gic_vcpus;
 };
 
 struct vgic_vm_ops {
 int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
 int kvm_vgic_hyp_init(void);
 int kvm_vgic_map_resources(struct kvm *kvm);
+int kvm_vgic_get_max_vcpus(void);
 int kvm_vgic_create(struct kvm *kvm, u32 type);
 void kvm_vgic_destroy(struct kvm *kvm);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 {
        return true;
 }
+
+static inline int kvm_vgic_get_max_vcpus(void)
+{
+       return KVM_MAX_VCPUS;
+}
 #endif
 
 #endif
 
                 vctrl_res.start, vgic->maint_irq);
 
        vgic->type = VGIC_V2;
+       vgic->max_gic_vcpus = VGIC_V2_MAX_CPUS;
        *ops = &vgic_v2_ops;
        *params = vgic;
        goto out;
 
        vgic->vcpu_base = vcpu_res.start;
        vgic->vctrl_base = NULL;
        vgic->type = VGIC_V3;
+       vgic->max_gic_vcpus = KVM_MAX_VCPUS;
 
        kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
                 vcpu_res.start, vgic->maint_irq);
 
        return 0;
 }
 
+/**
+ * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
+ *
+ * The host's GIC naturally limits the maximum amount of VCPUs a guest
+ * can use.
+ */
+int kvm_vgic_get_max_vcpus(void)
+{
+       return vgic->max_gic_vcpus;
+}
+
 void kvm_vgic_destroy(struct kvm *kvm)
 {
        struct vgic_dist *dist = &kvm->arch.vgic;
        dist->vm_ops.add_sgi_source = vgic_v2_add_sgi_source;
        dist->vm_ops.init_model = vgic_v2_init_model;
        dist->vm_ops.map_resources = vgic_v2_map_resources;
+
+       kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
 }
 
 static int init_vgic_model(struct kvm *kvm, int type)
                return -ENODEV;
        }
 
+       if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
+               return -E2BIG;
+
        return 0;
 }