diff mbox series

[kvm-unit-tests] kvm-unit-test: x86: Add RDPRU test

Message ID 20190919230225.37796-1-jmattson@google.com (mailing list archive)
State New, archived
Headers show
Series [kvm-unit-tests] kvm-unit-test: x86: Add RDPRU test | expand

Commit Message

Jim Mattson Sept. 19, 2019, 11:02 p.m. UTC
Ensure that support for RDPRU is not enumerated in the guest's CPUID
and that the RDPRU instruction raises #UD.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 lib/x86/processor.h |  1 +
 x86/Makefile.x86_64 |  1 +
 x86/rdpru.c         | 23 +++++++++++++++++++++++
 x86/unittests.cfg   |  5 +++++
 4 files changed, 30 insertions(+)
 create mode 100644 x86/rdpru.c

Comments

Krish Sadhukhan Sept. 20, 2019, 7:36 p.m. UTC | #1
On 9/19/19 4:02 PM, Jim Mattson wrote:
> Ensure that support for RDPRU is not enumerated in the guest's CPUID
> and that the RDPRU instruction raises #UD.


The AMD spec says,

         "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will 
generate a #UD fault."

So we don't need to check the CR4.TSD value here ?


>
> Signed-off-by: Jim Mattson <jmattson@google.com>
> Reviewed-by: Peter Shier <pshier@google.com>
> ---
>   lib/x86/processor.h |  1 +
>   x86/Makefile.x86_64 |  1 +
>   x86/rdpru.c         | 23 +++++++++++++++++++++++
>   x86/unittests.cfg   |  5 +++++
>   4 files changed, 30 insertions(+)
>   create mode 100644 x86/rdpru.c
>
> diff --git a/lib/x86/processor.h b/lib/x86/processor.h
> index b1c579b..121f19c 100644
> --- a/lib/x86/processor.h
> +++ b/lib/x86/processor.h
> @@ -150,6 +150,7 @@ static inline u8 cpuid_maxphyaddr(void)
>   #define	X86_FEATURE_RDPID		(CPUID(0x7, 0, ECX, 22))
>   #define	X86_FEATURE_SPEC_CTRL		(CPUID(0x7, 0, EDX, 26))
>   #define	X86_FEATURE_NX			(CPUID(0x80000001, 0, EDX, 20))
> +#define	X86_FEATURE_RDPRU		(CPUID(0x80000008, 0, EBX, 4))
>   
>   /*
>    * AMD CPUID features
> diff --git a/x86/Makefile.x86_64 b/x86/Makefile.x86_64
> index 51f9b80..010102b 100644
> --- a/x86/Makefile.x86_64
> +++ b/x86/Makefile.x86_64
> @@ -19,6 +19,7 @@ tests += $(TEST_DIR)/vmx.flat
>   tests += $(TEST_DIR)/tscdeadline_latency.flat
>   tests += $(TEST_DIR)/intel-iommu.flat
>   tests += $(TEST_DIR)/vmware_backdoors.flat
> +tests += $(TEST_DIR)/rdpru.flat
>   
>   include $(SRCDIR)/$(TEST_DIR)/Makefile.common
>   
> diff --git a/x86/rdpru.c b/x86/rdpru.c
> new file mode 100644
> index 0000000..a298960
> --- /dev/null
> +++ b/x86/rdpru.c
> @@ -0,0 +1,23 @@
> +/* RDPRU test */
> +
> +#include "libcflat.h"
> +#include "processor.h"
> +#include "desc.h"
> +
> +static int rdpru_checking(void)
> +{
> +	asm volatile (ASM_TRY("1f")
> +		      ".byte 0x0f,0x01,0xfd \n\t" /* rdpru */
> +		      "1:" : : "c" (0) : "eax", "edx");
> +	return exception_vector();
> +}
> +
> +int main(int ac, char **av)
> +{
> +	setup_idt();
> +
> +	report("RDPRU not supported", !this_cpu_has(X86_FEATURE_RDPRU));
> +	report("RDPRU raises #UD", rdpru_checking() == UD_VECTOR);
> +
> +	return report_summary();
> +}
> diff --git a/x86/unittests.cfg b/x86/unittests.cfg
> index 694ee3d..9764e18 100644
> --- a/x86/unittests.cfg
> +++ b/x86/unittests.cfg
> @@ -221,6 +221,11 @@ file = pcid.flat
>   extra_params = -cpu qemu64,+pcid
>   arch = x86_64
>   
> +[rdpru]
> +file = rdpru.flat
> +extra_params = -cpu host
> +arch = x86_64
> +
>   [umip]
>   file = umip.flat
>   extra_params = -cpu qemu64,+umip
Jim Mattson Sept. 20, 2019, 7:44 p.m. UTC | #2
On Fri, Sep 20, 2019 at 12:36 PM Krish Sadhukhan
<krish.sadhukhan@oracle.com> wrote:
>
>
> On 9/19/19 4:02 PM, Jim Mattson wrote:
> > Ensure that support for RDPRU is not enumerated in the guest's CPUID
> > and that the RDPRU instruction raises #UD.
>
>
> The AMD spec says,
>
>          "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will
> generate a #UD fault."
>
> So we don't need to check the CR4.TSD value here ?

KVM should set CPUID Fn8000_0008_EBX[RDPRU] to 0.

However, I should modify the test so it passes (or skips) on hardware. :-)

>
> >
> > Signed-off-by: Jim Mattson <jmattson@google.com>
> > Reviewed-by: Peter Shier <pshier@google.com>
> > ---
> >   lib/x86/processor.h |  1 +
> >   x86/Makefile.x86_64 |  1 +
> >   x86/rdpru.c         | 23 +++++++++++++++++++++++
> >   x86/unittests.cfg   |  5 +++++
> >   4 files changed, 30 insertions(+)
> >   create mode 100644 x86/rdpru.c
> >
> > diff --git a/lib/x86/processor.h b/lib/x86/processor.h
> > index b1c579b..121f19c 100644
> > --- a/lib/x86/processor.h
> > +++ b/lib/x86/processor.h
> > @@ -150,6 +150,7 @@ static inline u8 cpuid_maxphyaddr(void)
> >   #define     X86_FEATURE_RDPID               (CPUID(0x7, 0, ECX, 22))
> >   #define     X86_FEATURE_SPEC_CTRL           (CPUID(0x7, 0, EDX, 26))
> >   #define     X86_FEATURE_NX                  (CPUID(0x80000001, 0, EDX, 20))
> > +#define      X86_FEATURE_RDPRU               (CPUID(0x80000008, 0, EBX, 4))
> >
> >   /*
> >    * AMD CPUID features
> > diff --git a/x86/Makefile.x86_64 b/x86/Makefile.x86_64
> > index 51f9b80..010102b 100644
> > --- a/x86/Makefile.x86_64
> > +++ b/x86/Makefile.x86_64
> > @@ -19,6 +19,7 @@ tests += $(TEST_DIR)/vmx.flat
> >   tests += $(TEST_DIR)/tscdeadline_latency.flat
> >   tests += $(TEST_DIR)/intel-iommu.flat
> >   tests += $(TEST_DIR)/vmware_backdoors.flat
> > +tests += $(TEST_DIR)/rdpru.flat
> >
> >   include $(SRCDIR)/$(TEST_DIR)/Makefile.common
> >
> > diff --git a/x86/rdpru.c b/x86/rdpru.c
> > new file mode 100644
> > index 0000000..a298960
> > --- /dev/null
> > +++ b/x86/rdpru.c
> > @@ -0,0 +1,23 @@
> > +/* RDPRU test */
> > +
> > +#include "libcflat.h"
> > +#include "processor.h"
> > +#include "desc.h"
> > +
> > +static int rdpru_checking(void)
> > +{
> > +     asm volatile (ASM_TRY("1f")
> > +                   ".byte 0x0f,0x01,0xfd \n\t" /* rdpru */
> > +                   "1:" : : "c" (0) : "eax", "edx");
> > +     return exception_vector();
> > +}
> > +
> > +int main(int ac, char **av)
> > +{
> > +     setup_idt();
> > +
> > +     report("RDPRU not supported", !this_cpu_has(X86_FEATURE_RDPRU));
> > +     report("RDPRU raises #UD", rdpru_checking() == UD_VECTOR);
> > +
> > +     return report_summary();
> > +}
> > diff --git a/x86/unittests.cfg b/x86/unittests.cfg
> > index 694ee3d..9764e18 100644
> > --- a/x86/unittests.cfg
> > +++ b/x86/unittests.cfg
> > @@ -221,6 +221,11 @@ file = pcid.flat
> >   extra_params = -cpu qemu64,+pcid
> >   arch = x86_64
> >
> > +[rdpru]
> > +file = rdpru.flat
> > +extra_params = -cpu host
> > +arch = x86_64
> > +
> >   [umip]
> >   file = umip.flat
> >   extra_params = -cpu qemu64,+umip
Nadav Amit Sept. 24, 2019, 5:29 p.m. UTC | #3
> On Sep 20, 2019, at 12:44 PM, Jim Mattson <jmattson@google.com> wrote:
> 
> On Fri, Sep 20, 2019 at 12:36 PM Krish Sadhukhan
> <krish.sadhukhan@oracle.com> wrote:
>> On 9/19/19 4:02 PM, Jim Mattson wrote:
>>> Ensure that support for RDPRU is not enumerated in the guest's CPUID
>>> and that the RDPRU instruction raises #UD.
>> 
>> 
>> The AMD spec says,
>> 
>>         "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will
>> generate a #UD fault."
>> 
>> So we don't need to check the CR4.TSD value here ?
> 
> KVM should set CPUID Fn8000_0008_EBX[RDPRU] to 0.
> 
> However, I should modify the test so it passes (or skips) on hardware. :-)

Thanks for making this exception. Just wondering: have you or anyone else
used this functionality - of running tests on bare-metal?

I ask because it would also save me the trouble of checking (occasionally)
that nothing broke.
Jim Mattson Sept. 24, 2019, 6:09 p.m. UTC | #4
On Tue, Sep 24, 2019 at 10:29 AM Nadav Amit <nadav.amit@gmail.com> wrote:
>
> > On Sep 20, 2019, at 12:44 PM, Jim Mattson <jmattson@google.com> wrote:
> >
> > On Fri, Sep 20, 2019 at 12:36 PM Krish Sadhukhan
> > <krish.sadhukhan@oracle.com> wrote:
> >> On 9/19/19 4:02 PM, Jim Mattson wrote:
> >>> Ensure that support for RDPRU is not enumerated in the guest's CPUID
> >>> and that the RDPRU instruction raises #UD.
> >>
> >>
> >> The AMD spec says,
> >>
> >>         "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will
> >> generate a #UD fault."
> >>
> >> So we don't need to check the CR4.TSD value here ?
> >
> > KVM should set CPUID Fn8000_0008_EBX[RDPRU] to 0.
> >
> > However, I should modify the test so it passes (or skips) on hardware. :-)
>
> Thanks for making this exception. Just wondering: have you or anyone else
> used this functionality - of running tests on bare-metal?

I have not. However, if there is a simple way to add this testing to
our workflow, I would be happy to ask the team to do so before sending
submissions upstream.

> I ask because it would also save me the trouble of checking (occasionally)
> that nothing broke.
>
Nadav Amit Sept. 24, 2019, 6:14 p.m. UTC | #5
> On Sep 24, 2019, at 11:09 AM, Jim Mattson <jmattson@google.com> wrote:
> 
> On Tue, Sep 24, 2019 at 10:29 AM Nadav Amit <nadav.amit@gmail.com> wrote:
>>> On Sep 20, 2019, at 12:44 PM, Jim Mattson <jmattson@google.com> wrote:
>>> 
>>> On Fri, Sep 20, 2019 at 12:36 PM Krish Sadhukhan
>>> <krish.sadhukhan@oracle.com> wrote:
>>>> On 9/19/19 4:02 PM, Jim Mattson wrote:
>>>>> Ensure that support for RDPRU is not enumerated in the guest's CPUID
>>>>> and that the RDPRU instruction raises #UD.
>>>> 
>>>> 
>>>> The AMD spec says,
>>>> 
>>>>        "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will
>>>> generate a #UD fault."
>>>> 
>>>> So we don't need to check the CR4.TSD value here ?
>>> 
>>> KVM should set CPUID Fn8000_0008_EBX[RDPRU] to 0.
>>> 
>>> However, I should modify the test so it passes (or skips) on hardware. :-)
>> 
>> Thanks for making this exception. Just wondering: have you or anyone else
>> used this functionality - of running tests on bare-metal?
> 
> I have not. However, if there is a simple way to add this testing to
> our workflow, I would be happy to ask the team to do so before sending
> submissions upstream.

I guess I should build some script that uses idrac to automate this process.
Jim Mattson Sept. 24, 2019, 7:29 p.m. UTC | #6
On Tue, Sep 24, 2019 at 11:14 AM Nadav Amit <nadav.amit@gmail.com> wrote:
>
> > On Sep 24, 2019, at 11:09 AM, Jim Mattson <jmattson@google.com> wrote:
> >
> > On Tue, Sep 24, 2019 at 10:29 AM Nadav Amit <nadav.amit@gmail.com> wrote:
> >>> On Sep 20, 2019, at 12:44 PM, Jim Mattson <jmattson@google.com> wrote:
> >>>
> >>> On Fri, Sep 20, 2019 at 12:36 PM Krish Sadhukhan
> >>> <krish.sadhukhan@oracle.com> wrote:
> >>>> On 9/19/19 4:02 PM, Jim Mattson wrote:
> >>>>> Ensure that support for RDPRU is not enumerated in the guest's CPUID
> >>>>> and that the RDPRU instruction raises #UD.
> >>>>
> >>>>
> >>>> The AMD spec says,
> >>>>
> >>>>        "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will
> >>>> generate a #UD fault."
> >>>>
> >>>> So we don't need to check the CR4.TSD value here ?
> >>>
> >>> KVM should set CPUID Fn8000_0008_EBX[RDPRU] to 0.
> >>>
> >>> However, I should modify the test so it passes (or skips) on hardware. :-)
> >>
> >> Thanks for making this exception. Just wondering: have you or anyone else
> >> used this functionality - of running tests on bare-metal?
> >
> > I have not. However, if there is a simple way to add this testing to
> > our workflow, I would be happy to ask the team to do so before sending
> > submissions upstream.
>
> I guess I should build some script that uses idrac to automate this process.

I'm not familiar with idrac. What sort of functionality do you need
from the test infrastructure to automate this process?
Nadav Amit Sept. 24, 2019, 7:31 p.m. UTC | #7
> On Sep 24, 2019, at 12:29 PM, Jim Mattson <jmattson@google.com> wrote:
> 
> On Tue, Sep 24, 2019 at 11:14 AM Nadav Amit <nadav.amit@gmail.com> wrote:
>>> On Sep 24, 2019, at 11:09 AM, Jim Mattson <jmattson@google.com> wrote:
>>> 
>>> On Tue, Sep 24, 2019 at 10:29 AM Nadav Amit <nadav.amit@gmail.com> wrote:
>>>>> On Sep 20, 2019, at 12:44 PM, Jim Mattson <jmattson@google.com> wrote:
>>>>> 
>>>>> On Fri, Sep 20, 2019 at 12:36 PM Krish Sadhukhan
>>>>> <krish.sadhukhan@oracle.com> wrote:
>>>>>> On 9/19/19 4:02 PM, Jim Mattson wrote:
>>>>>>> Ensure that support for RDPRU is not enumerated in the guest's CPUID
>>>>>>> and that the RDPRU instruction raises #UD.
>>>>>> 
>>>>>> 
>>>>>> The AMD spec says,
>>>>>> 
>>>>>>       "When the CPL>0 with CR4.TSD=1, the RDPRUinstruction will
>>>>>> generate a #UD fault."
>>>>>> 
>>>>>> So we don't need to check the CR4.TSD value here ?
>>>>> 
>>>>> KVM should set CPUID Fn8000_0008_EBX[RDPRU] to 0.
>>>>> 
>>>>> However, I should modify the test so it passes (or skips) on hardware. :-)
>>>> 
>>>> Thanks for making this exception. Just wondering: have you or anyone else
>>>> used this functionality - of running tests on bare-metal?
>>> 
>>> I have not. However, if there is a simple way to add this testing to
>>> our workflow, I would be happy to ask the team to do so before sending
>>> submissions upstream.
>> 
>> I guess I should build some script that uses idrac to automate this process.
> 
> I'm not familiar with idrac. What sort of functionality do you need
> from the test infrastructure to automate this process?

Redirecting the serial port to the console and rebooting the machine
remotely.
Paolo Bonzini Sept. 25, 2019, 1:43 p.m. UTC | #8
On 20/09/19 01:02, Jim Mattson wrote:
> +int main(int ac, char **av)
> +{
> +	setup_idt();
> +
> +	report("RDPRU not supported", !this_cpu_has(X86_FEATURE_RDPRU));
> +	report("RDPRU raises #UD", rdpru_checking() == UD_VECTOR);
> +
> +	return report_summary();
> +}
> diff --git a/x86/unittests.cfg b/x86/unittests.cfg
> index 694ee3d..9764e18 100644
> --- a/x86/unittests.cfg
> +++ b/x86/unittests.cfg
> @@ -221,6 +221,11 @@ file = pcid.flat
>  extra_params = -cpu qemu64,+pcid
>  arch = x86_64
>  
> +[rdpru]
> +file = rdpru.flat
> +extra_params = -cpu host
> +arch = x86_64
> +
>  [umip]
>  file = umip.flat
>  extra_params = -cpu qemu64,+umip
> 

Queued, thanks.

Paolo
Paolo Bonzini Sept. 25, 2019, 1:47 p.m. UTC | #9
On 20/09/19 21:44, Jim Mattson wrote:
> However, I should modify the test so it passes (or skips) on hardware. :-)

This should do.

diff --git a/x86/rdpru.c b/x86/rdpru.c
index a298960..3cdb2d6 100644
--- a/x86/rdpru.c
+++ b/x86/rdpru.c
@@ -16,8 +16,10 @@ int main(int ac, char **av)
 {
 	setup_idt();
 
-	report("RDPRU not supported", !this_cpu_has(X86_FEATURE_RDPRU));
-	report("RDPRU raises #UD", rdpru_checking() == UD_VECTOR);
+	if (this_cpu_has(X86_FEATURE_RDPRU))
+		report_skip("RDPRU raises #UD");
+	else
+		report("RDPRU raises #UD", rdpru_checking() == UD_VECTOR);
 
 	return report_summary();
 }

Paolo
diff mbox series

Patch

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index b1c579b..121f19c 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -150,6 +150,7 @@  static inline u8 cpuid_maxphyaddr(void)
 #define	X86_FEATURE_RDPID		(CPUID(0x7, 0, ECX, 22))
 #define	X86_FEATURE_SPEC_CTRL		(CPUID(0x7, 0, EDX, 26))
 #define	X86_FEATURE_NX			(CPUID(0x80000001, 0, EDX, 20))
+#define	X86_FEATURE_RDPRU		(CPUID(0x80000008, 0, EBX, 4))
 
 /*
  * AMD CPUID features
diff --git a/x86/Makefile.x86_64 b/x86/Makefile.x86_64
index 51f9b80..010102b 100644
--- a/x86/Makefile.x86_64
+++ b/x86/Makefile.x86_64
@@ -19,6 +19,7 @@  tests += $(TEST_DIR)/vmx.flat
 tests += $(TEST_DIR)/tscdeadline_latency.flat
 tests += $(TEST_DIR)/intel-iommu.flat
 tests += $(TEST_DIR)/vmware_backdoors.flat
+tests += $(TEST_DIR)/rdpru.flat
 
 include $(SRCDIR)/$(TEST_DIR)/Makefile.common
 
diff --git a/x86/rdpru.c b/x86/rdpru.c
new file mode 100644
index 0000000..a298960
--- /dev/null
+++ b/x86/rdpru.c
@@ -0,0 +1,23 @@ 
+/* RDPRU test */
+
+#include "libcflat.h"
+#include "processor.h"
+#include "desc.h"
+
+static int rdpru_checking(void)
+{
+	asm volatile (ASM_TRY("1f")
+		      ".byte 0x0f,0x01,0xfd \n\t" /* rdpru */
+		      "1:" : : "c" (0) : "eax", "edx");
+	return exception_vector();
+}
+
+int main(int ac, char **av)
+{
+	setup_idt();
+
+	report("RDPRU not supported", !this_cpu_has(X86_FEATURE_RDPRU));
+	report("RDPRU raises #UD", rdpru_checking() == UD_VECTOR);
+
+	return report_summary();
+}
diff --git a/x86/unittests.cfg b/x86/unittests.cfg
index 694ee3d..9764e18 100644
--- a/x86/unittests.cfg
+++ b/x86/unittests.cfg
@@ -221,6 +221,11 @@  file = pcid.flat
 extra_params = -cpu qemu64,+pcid
 arch = x86_64
 
+[rdpru]
+file = rdpru.flat
+extra_params = -cpu host
+arch = x86_64
+
 [umip]
 file = umip.flat
 extra_params = -cpu qemu64,+umip