Skip to content

Commit d9a0659

Browse files
Marc Zyngieroupton
authored andcommitted
arm64: cpufeatures: Correctly handle signed values
Although we've had signed values for some features such as PMUv3 and FP, the code that handles the comparaison with some limit has a couple of annoying issues: - the min_field_value is always unsigned, meaning that we cannot easily compare it with a negative value - it is not possible to have a range of values, let alone a range of negative values Fix this by: - adding an upper limit to the comparison, defaulting to all bits being set to the maximum positive value - ensuring that the signess of the min and max values are taken into account A ARM64_CPUID_FIELDS_NEG() macro is provided for signed features, but nothing is using it yet. Signed-off-by: Marc Zyngier <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Oliver Upton <[email protected]>
1 parent 53eaeb7 commit d9a0659

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

arch/arm64/include/asm/cpufeature.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ struct arm64_cpu_capabilities {
363363
u8 field_pos;
364364
u8 field_width;
365365
u8 min_field_value;
366+
u8 max_field_value;
366367
u8 hwcap_type;
367368
bool sign;
368369
unsigned long hwcap;

arch/arm64/kernel/cpufeature.c

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,42 @@ void dump_cpu_features(void)
140140
pr_emerg("0x%*pb\n", ARM64_NCAPS, &system_cpucaps);
141141
}
142142

143+
#define __ARM64_MAX_POSITIVE(reg, field) \
144+
((reg##_##field##_SIGNED ? \
145+
BIT(reg##_##field##_WIDTH - 1) : \
146+
BIT(reg##_##field##_WIDTH)) - 1)
147+
148+
#define __ARM64_MIN_NEGATIVE(reg, field) BIT(reg##_##field##_WIDTH - 1)
149+
150+
#define __ARM64_CPUID_FIELDS(reg, field, min_value, max_value) \
151+
.sys_reg = SYS_##reg, \
152+
.field_pos = reg##_##field##_SHIFT, \
153+
.field_width = reg##_##field##_WIDTH, \
154+
.sign = reg##_##field##_SIGNED, \
155+
.min_field_value = min_value, \
156+
.max_field_value = max_value,
157+
158+
/*
159+
* ARM64_CPUID_FIELDS() encodes a field with a range from min_value to
160+
* an implicit maximum that depends on the sign-ess of the field.
161+
*
162+
* An unsigned field will be capped at all ones, while a signed field
163+
* will be limited to the positive half only.
164+
*/
143165
#define ARM64_CPUID_FIELDS(reg, field, min_value) \
144-
.sys_reg = SYS_##reg, \
145-
.field_pos = reg##_##field##_SHIFT, \
146-
.field_width = reg##_##field##_WIDTH, \
147-
.sign = reg##_##field##_SIGNED, \
148-
.min_field_value = reg##_##field##_##min_value,
166+
__ARM64_CPUID_FIELDS(reg, field, \
167+
SYS_FIELD_VALUE(reg, field, min_value), \
168+
__ARM64_MAX_POSITIVE(reg, field))
169+
170+
/*
171+
* ARM64_CPUID_FIELDS_NEG() encodes a field with a range from an
172+
* implicit minimal value to max_value. This should be used when
173+
* matching a non-implemented property.
174+
*/
175+
#define ARM64_CPUID_FIELDS_NEG(reg, field, max_value) \
176+
__ARM64_CPUID_FIELDS(reg, field, \
177+
__ARM64_MIN_NEGATIVE(reg, field), \
178+
SYS_FIELD_VALUE(reg, field, max_value))
149179

150180
#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
151181
{ \
@@ -1451,11 +1481,28 @@ has_always(const struct arm64_cpu_capabilities *entry, int scope)
14511481
static bool
14521482
feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
14531483
{
1454-
int val = cpuid_feature_extract_field_width(reg, entry->field_pos,
1455-
entry->field_width,
1456-
entry->sign);
1484+
int val, min, max;
1485+
u64 tmp;
1486+
1487+
val = cpuid_feature_extract_field_width(reg, entry->field_pos,
1488+
entry->field_width,
1489+
entry->sign);
1490+
1491+
tmp = entry->min_field_value;
1492+
tmp <<= entry->field_pos;
1493+
1494+
min = cpuid_feature_extract_field_width(tmp, entry->field_pos,
1495+
entry->field_width,
1496+
entry->sign);
1497+
1498+
tmp = entry->max_field_value;
1499+
tmp <<= entry->field_pos;
1500+
1501+
max = cpuid_feature_extract_field_width(tmp, entry->field_pos,
1502+
entry->field_width,
1503+
entry->sign);
14571504

1458-
return val >= entry->min_field_value;
1505+
return val >= min && val <= max;
14591506
}
14601507

14611508
static u64

0 commit comments

Comments
 (0)