public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly
@ 2024-01-15 22:02 Jing Zhang
  2024-01-15 22:02 ` [PATCH v2] " Jing Zhang
  0 siblings, 1 reply; 4+ messages in thread
From: Jing Zhang @ 2024-01-15 22:02 UTC (permalink / raw)
  To: KVM, KVMARM, ARMLinux, Marc Zyngier, Oliver Upton
  Cc: Paolo Bonzini, James Morse, Suzuki K Poulose, Zenghui Yu,
	Itaru Kitayama, Jing Zhang

There are some feature fields with nonzero minimum valid value. Make
sure get_safe_value() won't return invalid field values for them.
Also fix a bug that wrongly uses the feature bits type as the feature
bits sign causing all fields as signed in the get_safe_value() and
get_invalid_value().

Fixes: 54a9ea73527d ("KVM: arm64: selftests: Test for setting ID register from usersapce")
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Reported-by: Itaru Kitayama <itaru.kitayama@linux.dev>
Signed-off-by: Jing Zhang <jingzhangos@google.com>
---
 .../selftests/kvm/aarch64/set_id_regs.c       | 20 +++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/aarch64/set_id_regs.c b/tools/testing/selftests/kvm/aarch64/set_id_regs.c
index bac05210b539..f17454dc6d9e 100644
--- a/tools/testing/selftests/kvm/aarch64/set_id_regs.c
+++ b/tools/testing/selftests/kvm/aarch64/set_id_regs.c
@@ -224,13 +224,20 @@ uint64_t get_safe_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
 {
 	uint64_t ftr_max = GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 0);
 
-	if (ftr_bits->type == FTR_UNSIGNED) {
+	if (ftr_bits->sign == FTR_UNSIGNED) {
 		switch (ftr_bits->type) {
 		case FTR_EXACT:
 			ftr = ftr_bits->safe_val;
 			break;
 		case FTR_LOWER_SAFE:
-			if (ftr > 0)
+			uint64_t min_safe = 0;
+
+			if (!strcmp(ftr_bits->name, "ID_AA64DFR0_EL1_DebugVer"))
+				min_safe = ID_AA64DFR0_EL1_DebugVer_IMP;
+			else if (!strcmp(ftr_bits->name, "ID_DFR0_EL1_CopDbg"))
+				min_safe = ID_DFR0_EL1_CopDbg_Armv8;
+
+			if (ftr > min_safe)
 				ftr--;
 			break;
 		case FTR_HIGHER_SAFE:
@@ -252,7 +259,12 @@ uint64_t get_safe_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
 			ftr = ftr_bits->safe_val;
 			break;
 		case FTR_LOWER_SAFE:
-			if (ftr > 0)
+			uint64_t min_safe = 0;
+
+			if (!strcmp(ftr_bits->name, "ID_DFR0_EL1_PerfMon"))
+				min_safe = ID_DFR0_EL1_PerfMon_PMUv3;
+
+			if (ftr > min_safe)
 				ftr--;
 			break;
 		case FTR_HIGHER_SAFE:
@@ -276,7 +288,7 @@ uint64_t get_invalid_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
 {
 	uint64_t ftr_max = GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 0);
 
-	if (ftr_bits->type == FTR_UNSIGNED) {
+	if (ftr_bits->sign == FTR_UNSIGNED) {
 		switch (ftr_bits->type) {
 		case FTR_EXACT:
 			ftr = max((uint64_t)ftr_bits->safe_val + 1, ftr + 1);

base-commit: 0dd3ee31125508cd67f7e7172247f05b7fd1753a
-- 
2.43.0.472.g3155946c3a-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly
  2024-01-15 22:02 [PATCH v1] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly Jing Zhang
@ 2024-01-15 22:02 ` Jing Zhang
  2024-01-15 23:07   ` Itaru Kitayama
  2024-01-24 20:59   ` Oliver Upton
  0 siblings, 2 replies; 4+ messages in thread
From: Jing Zhang @ 2024-01-15 22:02 UTC (permalink / raw)
  To: KVM, KVMARM, ARMLinux, Marc Zyngier, Oliver Upton
  Cc: Paolo Bonzini, James Morse, Suzuki K Poulose, Zenghui Yu,
	Itaru Kitayama, Jing Zhang, Itaru Kitayama

There are some feature fields with nonzero minimum valid value. Make
sure get_safe_value() won't return invalid field values for them.
Also fix a bug that wrongly uses the feature bits type as the feature
bits sign causing all fields as signed in the get_safe_value() and
get_invalid_value().

Fixes: 54a9ea73527d ("KVM: arm64: selftests: Test for setting ID register from usersapce")
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Reported-by: Itaru Kitayama <itaru.kitayama@linux.dev>
Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com>
Signed-off-by: Jing Zhang <jingzhangos@google.com>

---
* v1 -> v2:
  - Use ftr_bits->safe_val for minimal safe value for type FTR_LOWER_SAFE.
  - Fix build error reported by Zenghui with gcc-10.3.1.
---
 .../selftests/kvm/aarch64/set_id_regs.c        | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/kvm/aarch64/set_id_regs.c b/tools/testing/selftests/kvm/aarch64/set_id_regs.c
index bac05210b539..16e2338686c1 100644
--- a/tools/testing/selftests/kvm/aarch64/set_id_regs.c
+++ b/tools/testing/selftests/kvm/aarch64/set_id_regs.c
@@ -32,6 +32,10 @@ struct reg_ftr_bits {
 	enum ftr_type type;
 	uint8_t shift;
 	uint64_t mask;
+	/*
+	 * For FTR_EXACT, safe_val is used as the exact safe value.
+	 * For FTR_LOWER_SAFE, safe_val is used as the minimal safe value.
+	 */
 	int64_t safe_val;
 };
 
@@ -65,13 +69,13 @@ struct test_feature_reg {
 
 static const struct reg_ftr_bits ftr_id_aa64dfr0_el1[] = {
 	S_REG_FTR_BITS(FTR_LOWER_SAFE, ID_AA64DFR0_EL1, PMUVer, 0),
-	REG_FTR_BITS(FTR_LOWER_SAFE, ID_AA64DFR0_EL1, DebugVer, 0),
+	REG_FTR_BITS(FTR_LOWER_SAFE, ID_AA64DFR0_EL1, DebugVer, ID_AA64DFR0_EL1_DebugVer_IMP),
 	REG_FTR_END,
 };
 
 static const struct reg_ftr_bits ftr_id_dfr0_el1[] = {
-	S_REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, PerfMon, 0),
-	REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, CopDbg, 0),
+	S_REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, PerfMon, ID_DFR0_EL1_PerfMon_PMUv3),
+	REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, CopDbg, ID_DFR0_EL1_CopDbg_Armv8),
 	REG_FTR_END,
 };
 
@@ -224,13 +228,13 @@ uint64_t get_safe_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
 {
 	uint64_t ftr_max = GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 0);
 
-	if (ftr_bits->type == FTR_UNSIGNED) {
+	if (ftr_bits->sign == FTR_UNSIGNED) {
 		switch (ftr_bits->type) {
 		case FTR_EXACT:
 			ftr = ftr_bits->safe_val;
 			break;
 		case FTR_LOWER_SAFE:
-			if (ftr > 0)
+			if (ftr > ftr_bits->safe_val)
 				ftr--;
 			break;
 		case FTR_HIGHER_SAFE:
@@ -252,7 +256,7 @@ uint64_t get_safe_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
 			ftr = ftr_bits->safe_val;
 			break;
 		case FTR_LOWER_SAFE:
-			if (ftr > 0)
+			if (ftr > ftr_bits->safe_val)
 				ftr--;
 			break;
 		case FTR_HIGHER_SAFE:
@@ -276,7 +280,7 @@ uint64_t get_invalid_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
 {
 	uint64_t ftr_max = GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 0);
 
-	if (ftr_bits->type == FTR_UNSIGNED) {
+	if (ftr_bits->sign == FTR_UNSIGNED) {
 		switch (ftr_bits->type) {
 		case FTR_EXACT:
 			ftr = max((uint64_t)ftr_bits->safe_val + 1, ftr + 1);

base-commit: 0dd3ee31125508cd67f7e7172247f05b7fd1753a
-- 
2.43.0.381.gb435a96ce8-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly
  2024-01-15 22:02 ` [PATCH v2] " Jing Zhang
@ 2024-01-15 23:07   ` Itaru Kitayama
  2024-01-24 20:59   ` Oliver Upton
  1 sibling, 0 replies; 4+ messages in thread
From: Itaru Kitayama @ 2024-01-15 23:07 UTC (permalink / raw)
  To: Jing Zhang
  Cc: KVM, KVMARM, ARMLinux, Marc Zyngier, Oliver Upton, Paolo Bonzini,
	James Morse, Suzuki K Poulose, Zenghui Yu, Itaru Kitayama

On Mon, Jan 15, 2024 at 02:02:09PM -0800, Jing Zhang wrote:
> There are some feature fields with nonzero minimum valid value. Make
> sure get_safe_value() won't return invalid field values for them.
> Also fix a bug that wrongly uses the feature bits type as the feature
> bits sign causing all fields as signed in the get_safe_value() and
> get_invalid_value().
> 
> Fixes: 54a9ea73527d ("KVM: arm64: selftests: Test for setting ID register from usersapce")
> Reported-by: Zenghui Yu <yuzenghui@huawei.com>
> Reported-by: Itaru Kitayama <itaru.kitayama@linux.dev>
> Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com>
> Signed-off-by: Jing Zhang <jingzhangos@google.com>

v2 works on FVP.
Tested-by: Itaru Kitayama <itaru.kitayama@fujitsu.com>

Thanks,
Itaru.

> 
> ---
> * v1 -> v2:
>   - Use ftr_bits->safe_val for minimal safe value for type FTR_LOWER_SAFE.
>   - Fix build error reported by Zenghui with gcc-10.3.1.
> ---
>  .../selftests/kvm/aarch64/set_id_regs.c        | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/aarch64/set_id_regs.c b/tools/testing/selftests/kvm/aarch64/set_id_regs.c
> index bac05210b539..16e2338686c1 100644
> --- a/tools/testing/selftests/kvm/aarch64/set_id_regs.c
> +++ b/tools/testing/selftests/kvm/aarch64/set_id_regs.c
> @@ -32,6 +32,10 @@ struct reg_ftr_bits {
>  	enum ftr_type type;
>  	uint8_t shift;
>  	uint64_t mask;
> +	/*
> +	 * For FTR_EXACT, safe_val is used as the exact safe value.
> +	 * For FTR_LOWER_SAFE, safe_val is used as the minimal safe value.
> +	 */
>  	int64_t safe_val;
>  };
>  
> @@ -65,13 +69,13 @@ struct test_feature_reg {
>  
>  static const struct reg_ftr_bits ftr_id_aa64dfr0_el1[] = {
>  	S_REG_FTR_BITS(FTR_LOWER_SAFE, ID_AA64DFR0_EL1, PMUVer, 0),
> -	REG_FTR_BITS(FTR_LOWER_SAFE, ID_AA64DFR0_EL1, DebugVer, 0),
> +	REG_FTR_BITS(FTR_LOWER_SAFE, ID_AA64DFR0_EL1, DebugVer, ID_AA64DFR0_EL1_DebugVer_IMP),
>  	REG_FTR_END,
>  };
>  
>  static const struct reg_ftr_bits ftr_id_dfr0_el1[] = {
> -	S_REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, PerfMon, 0),
> -	REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, CopDbg, 0),
> +	S_REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, PerfMon, ID_DFR0_EL1_PerfMon_PMUv3),
> +	REG_FTR_BITS(FTR_LOWER_SAFE, ID_DFR0_EL1, CopDbg, ID_DFR0_EL1_CopDbg_Armv8),
>  	REG_FTR_END,
>  };
>  
> @@ -224,13 +228,13 @@ uint64_t get_safe_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
>  {
>  	uint64_t ftr_max = GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 0);
>  
> -	if (ftr_bits->type == FTR_UNSIGNED) {
> +	if (ftr_bits->sign == FTR_UNSIGNED) {
>  		switch (ftr_bits->type) {
>  		case FTR_EXACT:
>  			ftr = ftr_bits->safe_val;
>  			break;
>  		case FTR_LOWER_SAFE:
> -			if (ftr > 0)
> +			if (ftr > ftr_bits->safe_val)
>  				ftr--;
>  			break;
>  		case FTR_HIGHER_SAFE:
> @@ -252,7 +256,7 @@ uint64_t get_safe_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
>  			ftr = ftr_bits->safe_val;
>  			break;
>  		case FTR_LOWER_SAFE:
> -			if (ftr > 0)
> +			if (ftr > ftr_bits->safe_val)
>  				ftr--;
>  			break;
>  		case FTR_HIGHER_SAFE:
> @@ -276,7 +280,7 @@ uint64_t get_invalid_value(const struct reg_ftr_bits *ftr_bits, uint64_t ftr)
>  {
>  	uint64_t ftr_max = GENMASK_ULL(ARM64_FEATURE_FIELD_BITS - 1, 0);
>  
> -	if (ftr_bits->type == FTR_UNSIGNED) {
> +	if (ftr_bits->sign == FTR_UNSIGNED) {
>  		switch (ftr_bits->type) {
>  		case FTR_EXACT:
>  			ftr = max((uint64_t)ftr_bits->safe_val + 1, ftr + 1);
> 
> base-commit: 0dd3ee31125508cd67f7e7172247f05b7fd1753a
> -- 
> 2.43.0.381.gb435a96ce8-goog
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly
  2024-01-15 22:02 ` [PATCH v2] " Jing Zhang
  2024-01-15 23:07   ` Itaru Kitayama
@ 2024-01-24 20:59   ` Oliver Upton
  1 sibling, 0 replies; 4+ messages in thread
From: Oliver Upton @ 2024-01-24 20:59 UTC (permalink / raw)
  To: ARMLinux, KVMARM, Jing Zhang, Marc Zyngier, KVM
  Cc: Oliver Upton, James Morse, Suzuki K Poulose, Itaru Kitayama,
	Paolo Bonzini, Itaru Kitayama, Zenghui Yu

On Mon, 15 Jan 2024 14:02:09 -0800, Jing Zhang wrote:
> There are some feature fields with nonzero minimum valid value. Make
> sure get_safe_value() won't return invalid field values for them.
> Also fix a bug that wrongly uses the feature bits type as the feature
> bits sign causing all fields as signed in the get_safe_value() and
> get_invalid_value().
> 
> 
> [...]

Applied to kvmarm/next, thanks!

[1/1] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly
      https://git.kernel.org/kvmarm/kvmarm/c/1cd2b08f7cc4

--
Best,
Oliver

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-24 21:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-15 22:02 [PATCH v1] KVM: arm64: selftests: Handle feature fields with nonzero minimum value correctly Jing Zhang
2024-01-15 22:02 ` [PATCH v2] " Jing Zhang
2024-01-15 23:07   ` Itaru Kitayama
2024-01-24 20:59   ` Oliver Upton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox