diff mbox

[1/4] x86: introduce hypervisor_cpuid_base()

Message ID 1374572465-15278-1-git-send-email-jasowang@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jason Wang July 23, 2013, 9:41 a.m. UTC
This patch introduce hypervisor_cpuid_base() which loop test the hypervisor
existence function until the signature match and check the number of leaves if
required. This could be used by Xen/KVM guest to detect the existence of
hypervisor.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: "Paolo Bonzini" <pbonzini@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Cc: x86@kernel.org
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 arch/x86/include/asm/processor.h |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

Comments

H. Peter Anvin July 23, 2013, 10:04 a.m. UTC | #1
On 07/23/2013 02:41 AM, Jason Wang wrote:
>  
> +static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
> +{
> +	uint32_t base, eax, ebx, ecx, edx;
> +	char signature[13];
> +
> +	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
> +		cpuid(base, &eax, &ebx, &ecx, &edx);
> +		*(uint32_t *)(signature + 0) = ebx;
> +		*(uint32_t *)(signature + 4) = ecx;
> +		*(uint32_t *)(signature + 8) = edx;
> +		signature[12] = 0;
> +
> +		if (!strcmp(sig, signature) &&
> +		    (leaves == 0 || ((eax - base) >= leaves)))
> +			return base;
> +	}
> +
> +	return 0;
> +}
> +

Hmm... how about:

uint32_t sign[3];

	cpuid(base, &eax, &sign[0], &sign[1], &sign[2]);

	if (!memcmp(sig, sign, 12) && ...);

	-hpa

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Paolo Bonzini July 23, 2013, 11:16 a.m. UTC | #2
Il 23/07/2013 12:04, H. Peter Anvin ha scritto:
> On 07/23/2013 02:41 AM, Jason Wang wrote:
>>  
>> +static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
>> +{
>> +	uint32_t base, eax, ebx, ecx, edx;
>> +	char signature[13];
>> +
>> +	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
>> +		cpuid(base, &eax, &ebx, &ecx, &edx);
>> +		*(uint32_t *)(signature + 0) = ebx;
>> +		*(uint32_t *)(signature + 4) = ecx;
>> +		*(uint32_t *)(signature + 8) = edx;
>> +		signature[12] = 0;
>> +
>> +		if (!strcmp(sig, signature) &&
>> +		    (leaves == 0 || ((eax - base) >= leaves)))
>> +			return base;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
> 
> Hmm... how about:
> 
> uint32_t sign[3];
> 
> 	cpuid(base, &eax, &sign[0], &sign[1], &sign[2]);
> 
> 	if (!memcmp(sig, sign, 12) && ...);

That's nicer, though strcmp is what the replaced code used to do in
patches 2 and 3.

Note that memcmp requires the caller to use "KVMKVMKVM\0\0" as the
signature (or alternatively hypervisor_cpuid_base can copy the argument
into another 12-byte local variable).

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Gleb Natapov July 23, 2013, 1:48 p.m. UTC | #3
On Tue, Jul 23, 2013 at 05:41:02PM +0800, Jason Wang wrote:
> This patch introduce hypervisor_cpuid_base() which loop test the hypervisor
> existence function until the signature match and check the number of leaves if
> required. This could be used by Xen/KVM guest to detect the existence of
> hypervisor.
> 
Looks good to me.

Since this touches common code, kvm and xen I expect this to be taken
via the tip tree, correct?

> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Cc: "Paolo Bonzini" <pbonzini@redhat.com>
> Cc: Gleb Natapov <gleb@redhat.com>
> Cc: x86@kernel.org
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  arch/x86/include/asm/processor.h |   20 ++++++++++++++++++++
>  1 files changed, 20 insertions(+), 0 deletions(-)
> 
> diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
> index 24cf5ae..a8136d0 100644
> --- a/arch/x86/include/asm/processor.h
> +++ b/arch/x86/include/asm/processor.h
> @@ -971,6 +971,26 @@ unsigned long calc_aperfmperf_ratio(struct aperfmperf *old,
>  	return ratio;
>  }
>  
> +static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
> +{
> +	uint32_t base, eax, ebx, ecx, edx;
> +	char signature[13];
> +
> +	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
> +		cpuid(base, &eax, &ebx, &ecx, &edx);
> +		*(uint32_t *)(signature + 0) = ebx;
> +		*(uint32_t *)(signature + 4) = ecx;
> +		*(uint32_t *)(signature + 8) = edx;
> +		signature[12] = 0;
> +
> +		if (!strcmp(sig, signature) &&
> +		    (leaves == 0 || ((eax - base) >= leaves)))
> +			return base;
> +	}
> +
> +	return 0;
> +}
> +
>  extern unsigned long arch_align_stack(unsigned long sp);
>  extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
>  
> -- 
> 1.7.1

--
			Gleb.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
H. Peter Anvin July 23, 2013, 4:03 p.m. UTC | #4
On 07/23/2013 04:16 AM, Paolo Bonzini wrote:
> 
> That's nicer, though strcmp is what the replaced code used to do in
> patches 2 and 3.
> 
> Note that memcmp requires the caller to use "KVMKVMKVM\0\0" as the
> signature (or alternatively hypervisor_cpuid_base can copy the argument
> into another 12-byte local variable).
> 

Which is the actual signature, though...

	-hpa


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jason Wang July 24, 2013, 4:34 a.m. UTC | #5
On 07/23/2013 09:48 PM, Gleb Natapov wrote:
> On Tue, Jul 23, 2013 at 05:41:02PM +0800, Jason Wang wrote:
>> > This patch introduce hypervisor_cpuid_base() which loop test the hypervisor
>> > existence function until the signature match and check the number of leaves if
>> > required. This could be used by Xen/KVM guest to detect the existence of
>> > hypervisor.
>> > 
> Looks good to me.
>
> Since this touches common code, kvm and xen I expect this to be taken
> via the tip tree, correct?
>
Yes, I think so.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jason Wang July 24, 2013, 4:44 a.m. UTC | #6
On 07/24/2013 12:03 AM, H. Peter Anvin wrote:
> On 07/23/2013 04:16 AM, Paolo Bonzini wrote:
>> That's nicer, though strcmp is what the replaced code used to do in
>> patches 2 and 3.
>>
>> Note that memcmp requires the caller to use "KVMKVMKVM\0\0" as the
>> signature (or alternatively hypervisor_cpuid_base can copy the argument
>> into another 12-byte local variable).
>>
> Which is the actual signature, though...
>
> 	-hpa
>
>

Since it's just a minor optimization. How about just keep using the
strcmp()?
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
H. Peter Anvin July 24, 2013, 4:47 a.m. UTC | #7
On 07/23/2013 09:44 PM, Jason Wang wrote:
> 
> Since it's just a minor optimization. How about just keep using the
> strcmp()?
> 

It's more that it enables the rest of the cleanup, making the code
easier to read.

	-hpa

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index 24cf5ae..a8136d0 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -971,6 +971,26 @@  unsigned long calc_aperfmperf_ratio(struct aperfmperf *old,
 	return ratio;
 }
 
+static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
+{
+	uint32_t base, eax, ebx, ecx, edx;
+	char signature[13];
+
+	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
+		cpuid(base, &eax, &ebx, &ecx, &edx);
+		*(uint32_t *)(signature + 0) = ebx;
+		*(uint32_t *)(signature + 4) = ecx;
+		*(uint32_t *)(signature + 8) = edx;
+		signature[12] = 0;
+
+		if (!strcmp(sig, signature) &&
+		    (leaves == 0 || ((eax - base) >= leaves)))
+			return base;
+	}
+
+	return 0;
+}
+
 extern unsigned long arch_align_stack(unsigned long sp);
 extern void free_init_pages(char *what, unsigned long begin, unsigned long end);