From patchwork Thu Jan 7 16:24:27 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gleb Natapov X-Patchwork-Id: 42447 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9FA041007D2 for ; Fri, 8 Jan 2010 04:26:33 +1100 (EST) Received: from localhost ([127.0.0.1]:58406 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NSw7y-0001er-Dq for incoming@patchwork.ozlabs.org; Thu, 07 Jan 2010 12:26:30 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NSvtd-0008Ji-O3 for qemu-devel@nongnu.org; Thu, 07 Jan 2010 12:11:41 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NSvtZ-0008Ey-Ei for qemu-devel@nongnu.org; Thu, 07 Jan 2010 12:11:41 -0500 Received: from [199.232.76.173] (port=58003 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NSvtZ-0008Eg-2h for qemu-devel@nongnu.org; Thu, 07 Jan 2010 12:11:37 -0500 Received: from mx1.redhat.com ([209.132.183.28]:21420) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NSvtY-0007Uj-Fg for qemu-devel@nongnu.org; Thu, 07 Jan 2010 12:11:36 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o07HBVT7019915 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 7 Jan 2010 12:11:35 -0500 Received: from dhcp-1-237.tlv.redhat.com (dhcp-1-237.tlv.redhat.com [10.35.1.237]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o07GORRh014182 for ; Thu, 7 Jan 2010 11:25:30 -0500 Received: by dhcp-1-237.tlv.redhat.com (Postfix, from userid 13519) id 1CE031336D0; Thu, 7 Jan 2010 18:24:27 +0200 (IST) Date: Thu, 7 Jan 2010 18:24:27 +0200 From: Gleb Natapov To: qemu-devel@nongnu.org Message-ID: <20100107162427.GF4905@redhat.com> References: <20100107124449.GY4905@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20100107124449.GY4905@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [Qemu-devel] [PATCHv2] Add KVM paravirt cpuid leaf X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Initialize KVM paravirt cpuid leaf and allow user to control guest visible PV features through -cpu flag. Signed-off-by: Gleb Natapov --- v1->v2 fix indentation remove unneeded ifdefs -- Gleb. diff --git a/target-i386/cpu.h b/target-i386/cpu.h index f3834b3..fbe11b8 100644 --- a/target-i386/cpu.h +++ b/target-i386/cpu.h @@ -701,6 +701,7 @@ typedef struct CPUX86State { uint8_t nmi_pending; uint8_t has_error_code; uint32_t sipi_vector; + uint32_t cpuid_kvm_features; /* in order to simplify APIC support, we leave this pointer to the user */ diff --git a/target-i386/helper.c b/target-i386/helper.c index c39a993..20ec4b6 100644 --- a/target-i386/helper.c +++ b/target-i386/helper.c @@ -58,10 +58,18 @@ static const char *ext3_feature_name[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }; +static const char *kvm_feature_name[] = { + "kvmclock", "kvm_nopiodelay", "kvm_mmu", NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, +}; + static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features, uint32_t *ext_features, uint32_t *ext2_features, - uint32_t *ext3_features) + uint32_t *ext3_features, + uint32_t *kvm_features) { int i; int found = 0; @@ -86,6 +94,12 @@ static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features, *ext3_features |= 1 << i; found = 1; } + for ( i = 0 ; i < 32 ; i++ ) + if (kvm_feature_name[i] && !strcmp (flagname, kvm_feature_name[i])) { + *kvm_features |= 1 << i; + found = 1; + } + if (!found) { fprintf(stderr, "CPU feature %s not found\n", flagname); } @@ -98,7 +112,7 @@ typedef struct x86_def_t { int family; int model; int stepping; - uint32_t features, ext_features, ext2_features, ext3_features; + uint32_t features, ext_features, ext2_features, ext3_features, kvm_features; uint32_t xlevel; char model_id[48]; int vendor_override; @@ -375,8 +389,8 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) char *s = strdup(cpu_model); char *featurestr, *name = strtok(s, ","); - uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0; - uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0; + uint32_t plus_features = 0, plus_ext_features = 0, plus_ext2_features = 0, plus_ext3_features = 0, plus_kvm_features = 0; + uint32_t minus_features = 0, minus_ext_features = 0, minus_ext2_features = 0, minus_ext3_features = 0, minus_kvm_features = 0; uint32_t numvalue; def = NULL; @@ -394,17 +408,20 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) memcpy(x86_cpu_def, def, sizeof(*def)); } + plus_kvm_features = ~0; /* not supported bits will be filtered out later */ + add_flagname_to_bitmaps("hypervisor", &plus_features, - &plus_ext_features, &plus_ext2_features, &plus_ext3_features); + &plus_ext_features, &plus_ext2_features, &plus_ext3_features, + &plus_kvm_features); featurestr = strtok(NULL, ","); while (featurestr) { char *val; if (featurestr[0] == '+') { - add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features); + add_flagname_to_bitmaps(featurestr + 1, &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features, &plus_kvm_features); } else if (featurestr[0] == '-') { - add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features); + add_flagname_to_bitmaps(featurestr + 1, &minus_features, &minus_ext_features, &minus_ext2_features, &minus_ext3_features, &minus_kvm_features); } else if ((val = strchr(featurestr, '='))) { *val = 0; val++; if (!strcmp(featurestr, "family")) { @@ -481,10 +498,12 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) x86_cpu_def->ext_features |= plus_ext_features; x86_cpu_def->ext2_features |= plus_ext2_features; x86_cpu_def->ext3_features |= plus_ext3_features; + x86_cpu_def->kvm_features |= plus_kvm_features; x86_cpu_def->features &= ~minus_features; x86_cpu_def->ext_features &= ~minus_ext_features; x86_cpu_def->ext2_features &= ~minus_ext2_features; x86_cpu_def->ext3_features &= ~minus_ext3_features; + x86_cpu_def->kvm_features &= ~minus_kvm_features; free(s); return 0; @@ -529,7 +548,7 @@ static int cpu_x86_register (CPUX86State *env, const char *cpu_model) env->cpuid_ext_features = def->ext_features; env->cpuid_ext2_features = def->ext2_features; env->cpuid_xlevel = def->xlevel; - env->cpuid_ext3_features = def->ext3_features; + env->cpuid_kvm_features = def->kvm_features; { const char *model_id = def->model_id; int c, len, i; diff --git a/target-i386/kvm.c b/target-i386/kvm.c index 4084503..6a841de 100644 --- a/target-i386/kvm.c +++ b/target-i386/kvm.c @@ -17,6 +17,7 @@ #include #include +#include #include "qemu-common.h" #include "sysemu.h" @@ -134,6 +135,40 @@ static void kvm_trim_features(uint32_t *features, uint32_t supported) } } +struct kvm_para_features { + int cap; + int feature; +} para_features[] = { +#ifdef KVM_CAP_CLOCKSOURCE + { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE }, +#endif +#ifdef KVM_CAP_NOP_IO_DELAY + { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY }, +#endif +#ifdef KVM_CAP_PV_MMU + { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP }, +#endif +#ifdef KVM_CAP_CR3_CACHE + { KVM_CAP_CR3_CACHE, KVM_FEATURE_CR3_CACHE }, +#endif +#ifdef KVM_CAP_HYPERV + { KVM_CAP_HYPERV, KVM_FEATURE_HYPERV }, +#endif + { -1, -1 } +}; + +static int get_para_features(CPUState *env) +{ + int i, features = 0; + + for (i = 0; i < ARRAY_SIZE(para_features) - 1; i++) { + if (kvm_check_extension(env->kvm_state, para_features[i].cap)) + features |= (1 << para_features[i].feature); + } + + return features; +} + int kvm_arch_init_vcpu(CPUState *env) { struct { @@ -142,6 +177,8 @@ int kvm_arch_init_vcpu(CPUState *env) } __attribute__((packed)) cpuid_data; uint32_t limit, i, j, cpuid_i; uint32_t unused; + struct kvm_cpuid_entry2 *c; + uint32_t signature[3]; env->mp_state = KVM_MP_STATE_RUNNABLE; @@ -160,10 +197,25 @@ int kvm_arch_init_vcpu(CPUState *env) cpuid_i = 0; + /* Paravirtualization CPUIDs */ + memcpy(signature, "KVMKVMKVM\0\0\0", 12); + c = &cpuid_data.entries[cpuid_i++]; + memset(c, 0, sizeof(*c)); + c->function = KVM_CPUID_SIGNATURE; + c->eax = 0; + c->ebx = signature[0]; + c->ecx = signature[1]; + c->edx = signature[2]; + + c = &cpuid_data.entries[cpuid_i++]; + memset(c, 0, sizeof(*c)); + c->function = KVM_CPUID_FEATURES; + c->eax = env->cpuid_kvm_features & get_para_features(env); + cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused); for (i = 0; i <= limit; i++) { - struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++]; + c = &cpuid_data.entries[cpuid_i++]; switch (i) { case 2: { @@ -213,7 +265,7 @@ int kvm_arch_init_vcpu(CPUState *env) cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused); for (i = 0x80000000; i <= limit; i++) { - struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++]; + c = &cpuid_data.entries[cpuid_i++]; c->function = i; c->flags = 0;