diff mbox series

[kvm-unit-tests,v4,1/9] s390x: Add test_bit to library

Message ID 20210112132054.49756-2-frankja@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series s390x: Add SIE library and simple tests | expand

Commit Message

Janosch Frank Jan. 12, 2021, 1:20 p.m. UTC
Query/feature bits are commonly tested via MSB bit numbers on
s390. Let's add test bit functions, so we don't need to copy code to
test query bits.

The test_bit code has been taken from the kernel since most s390x KVM unit
test developers are used to them.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
---
 lib/s390x/asm/bitops.h   | 26 ++++++++++++++++++++++++++
 lib/s390x/asm/facility.h |  3 ++-
 s390x/uv-guest.c         |  6 +++---
 3 files changed, 31 insertions(+), 4 deletions(-)

Comments

Claudio Imbrenda Jan. 14, 2021, 1:55 p.m. UTC | #1
On Tue, 12 Jan 2021 08:20:46 -0500
Janosch Frank <frankja@linux.ibm.com> wrote:

> Query/feature bits are commonly tested via MSB bit numbers on
> s390. Let's add test bit functions, so we don't need to copy code to
> test query bits.
> 
> The test_bit code has been taken from the kernel since most s390x KVM
> unit test developers are used to them.
> 
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> Reviewed-by: Cornelia Huck <cohuck@redhat.com>
> Reviewed-by: Thomas Huth <thuth@redhat.com>
> ---
>  lib/s390x/asm/bitops.h   | 26 ++++++++++++++++++++++++++
>  lib/s390x/asm/facility.h |  3 ++-
>  s390x/uv-guest.c         |  6 +++---
>  3 files changed, 31 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/s390x/asm/bitops.h b/lib/s390x/asm/bitops.h
> index e7cdda9..792881e 100644
> --- a/lib/s390x/asm/bitops.h
> +++ b/lib/s390x/asm/bitops.h
> @@ -1,3 +1,13 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + *    Bitops taken from the kernel as most developers are already
> used
> + *    to them.
> + *
> + *    Copyright IBM Corp. 1999,2013
> + *
> + *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
> + *
> + */
>  #ifndef _ASMS390X_BITOPS_H_
>  #define _ASMS390X_BITOPS_H_
>  
> @@ -7,4 +17,20 @@
>  
>  #define BITS_PER_LONG	64
>  
> +static inline bool test_bit(unsigned long nr,
> +			    const volatile unsigned long *ptr)
> +{
> +	const volatile unsigned char *addr;
> +
> +	addr = ((const volatile unsigned char *)ptr);
> +	addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
> +	return (*addr >> (nr & 7)) & 1;
> +}
> +
> +static inline bool test_bit_inv(unsigned long nr,
> +				const volatile unsigned long *ptr)
> +{
> +	return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
> +}
> +
>  #endif
> diff --git a/lib/s390x/asm/facility.h b/lib/s390x/asm/facility.h
> index 7828cf8..95d4a15 100644
> --- a/lib/s390x/asm/facility.h
> +++ b/lib/s390x/asm/facility.h
> @@ -11,13 +11,14 @@
>  #include <libcflat.h>
>  #include <asm/facility.h>
>  #include <asm/arch_def.h>
> +#include <bitops.h>
>  
>  #define NB_STFL_DOUBLEWORDS 32
>  extern uint64_t stfl_doublewords[];
>  
>  static inline bool test_facility(int nr)
>  {
> -	return stfl_doublewords[nr / 64] & (0x8000000000000000UL >>
> (nr % 64));
> +	return test_bit_inv(nr, stfl_doublewords);
>  }
>  
>  static inline void stfl(void)
> diff --git a/s390x/uv-guest.c b/s390x/uv-guest.c
> index bc947ab..e51b85e 100644
> --- a/s390x/uv-guest.c
> +++ b/s390x/uv-guest.c
> @@ -75,11 +75,11 @@ static void test_query(void)
>  	 * Ultravisor version and are expected to always be available
>  	 * because they are basic building blocks.
>  	 */
> -	report(uvcb.inst_calls_list[0] & (1UL << (63 -
> BIT_UVC_CMD_QUI)),
> +	report(test_bit_inv(BIT_UVC_CMD_QUI,
> &uvcb.inst_calls_list[0]), "query indicated");
> -	report(uvcb.inst_calls_list[0] & (1UL << (63 -
> BIT_UVC_CMD_SET_SHARED_ACCESS)),
> +	report(test_bit_inv(BIT_UVC_CMD_SET_SHARED_ACCESS,
> &uvcb.inst_calls_list[0]), "share indicated");
> -	report(uvcb.inst_calls_list[0] & (1UL << (63 -
> BIT_UVC_CMD_REMOVE_SHARED_ACCESS)),
> +	report(test_bit_inv(BIT_UVC_CMD_REMOVE_SHARED_ACCESS,
> &uvcb.inst_calls_list[0]), "unshare indicated");
>  	report_prefix_pop();
>  }

Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
diff mbox series

Patch

diff --git a/lib/s390x/asm/bitops.h b/lib/s390x/asm/bitops.h
index e7cdda9..792881e 100644
--- a/lib/s390x/asm/bitops.h
+++ b/lib/s390x/asm/bitops.h
@@ -1,3 +1,13 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *    Bitops taken from the kernel as most developers are already used
+ *    to them.
+ *
+ *    Copyright IBM Corp. 1999,2013
+ *
+ *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
+ *
+ */
 #ifndef _ASMS390X_BITOPS_H_
 #define _ASMS390X_BITOPS_H_
 
@@ -7,4 +17,20 @@ 
 
 #define BITS_PER_LONG	64
 
+static inline bool test_bit(unsigned long nr,
+			    const volatile unsigned long *ptr)
+{
+	const volatile unsigned char *addr;
+
+	addr = ((const volatile unsigned char *)ptr);
+	addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
+	return (*addr >> (nr & 7)) & 1;
+}
+
+static inline bool test_bit_inv(unsigned long nr,
+				const volatile unsigned long *ptr)
+{
+	return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
+}
+
 #endif
diff --git a/lib/s390x/asm/facility.h b/lib/s390x/asm/facility.h
index 7828cf8..95d4a15 100644
--- a/lib/s390x/asm/facility.h
+++ b/lib/s390x/asm/facility.h
@@ -11,13 +11,14 @@ 
 #include <libcflat.h>
 #include <asm/facility.h>
 #include <asm/arch_def.h>
+#include <bitops.h>
 
 #define NB_STFL_DOUBLEWORDS 32
 extern uint64_t stfl_doublewords[];
 
 static inline bool test_facility(int nr)
 {
-	return stfl_doublewords[nr / 64] & (0x8000000000000000UL >> (nr % 64));
+	return test_bit_inv(nr, stfl_doublewords);
 }
 
 static inline void stfl(void)
diff --git a/s390x/uv-guest.c b/s390x/uv-guest.c
index bc947ab..e51b85e 100644
--- a/s390x/uv-guest.c
+++ b/s390x/uv-guest.c
@@ -75,11 +75,11 @@  static void test_query(void)
 	 * Ultravisor version and are expected to always be available
 	 * because they are basic building blocks.
 	 */
-	report(uvcb.inst_calls_list[0] & (1UL << (63 - BIT_UVC_CMD_QUI)),
+	report(test_bit_inv(BIT_UVC_CMD_QUI, &uvcb.inst_calls_list[0]),
 	       "query indicated");
-	report(uvcb.inst_calls_list[0] & (1UL << (63 - BIT_UVC_CMD_SET_SHARED_ACCESS)),
+	report(test_bit_inv(BIT_UVC_CMD_SET_SHARED_ACCESS, &uvcb.inst_calls_list[0]),
 	       "share indicated");
-	report(uvcb.inst_calls_list[0] & (1UL << (63 - BIT_UVC_CMD_REMOVE_SHARED_ACCESS)),
+	report(test_bit_inv(BIT_UVC_CMD_REMOVE_SHARED_ACCESS, &uvcb.inst_calls_list[0]),
 	       "unshare indicated");
 	report_prefix_pop();
 }