Skip to contentSkip to conversation

KVM: arm64: Treat 32bit ID registers as RAZ/WI on 64bit-only system

PATCH Superseded Oliver Upton 3 participants 16 messages Aug 17, 2022 – Sep 1, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover KVM: arm64: Treat 32bit ID registers as RAZ/WI on 64bit-only system

For reasons unknown, the Arm architecture defines the 64-bit views of the 32-bit ID registers as UNKNOWN [1]. This combines poorly with the fact that KVM unconditionally exposes these registers to userspace, which could throw a wrench in migration between 64-bit only systems.

This series reworks KVM's definition of these registers to RAZ/WI with the goal of providing consistent register values across 64-bit machines.

Patches 1-2 clean up some of the ID register accessors, taking advantage of the fact that the generic ones already know how to handle RAZ registers.

Patches 3-4 wire in a new visibility bit to indicate a register ignores writes from userspace.

Patch 5 moves all exposed 32-bit ID registers to have RAZ/WI behavior on 64-bit only systems. Note that hidden 32-bit registers continue to have RAZ behavior and carry the additional requirement of invariance.

Lastly, patch 6 tests that userspace and guest indeed see the registers as RAZ/WI.

Applies to 6.0-rc1 + the mismatched system fixes [2] picked up earlier today. Tested on the fast model, both with mismatched AArch32 support and no AArch32 support whatoever.

[1]: DDI0487H.a Table D12-2 'Instruction encodings for non-Debug System Register accesses' [2]: https://lore.kernel.org/kvmarm/[email protected]/

Oliver Upton (6):
KVM: arm64: Use visibility hook to treat ID regs as RAZ
KVM: arm64: Remove internal accessor helpers for id regs
KVM: arm64: Spin off helper for calling visibility hook
KVM: arm64: Add a visibility bit to ignore user writes
KVM: arm64: Treat 32bit ID registers as RAZ/WI on 64bit-only system
KVM: selftests: Add test for RAZ/WI AArch32 ID registers

 arch/arm64/kvm/sys_regs.c                     | 137 +++++++++---------
 arch/arm64/kvm/sys_regs.h                     |  24 ++-
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/aarch64_only_id_regs.c        | 135 +++++++++++++++++
 5 files changed, 222 insertions(+), 76 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/aarch64_only_id_regs.c
Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#1 lore raw

PATCH 1/6 KVM: arm64: Use visibility hook to treat ID regs as RAZ

The generic id reg accessors already handle RAZ registers by way of the visibility hook. Add a visibility hook that returns REG_RAZ unconditionally and throw out the RAZ specific accessors.

---
 arch/arm64/kvm/sys_regs.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
arch/arm64/kvm/sys_regs.c +14 -19
index 3234f50b8c4b..e18efb9211f0 100644
@@ -1145,6 +1145,12 @@ static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 	return 0;
 }
 
+static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
+				   const struct sys_reg_desc *r)
+{
+	return REG_RAZ;
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool __access_id_reg(struct kvm_vcpu *vcpu,
@@ -1168,13 +1174,6 @@ static bool access_id_reg(struct kvm_vcpu *vcpu,
 	return __access_id_reg(vcpu, p, r, raz);
 }
 
-static bool access_raz_id_reg(struct kvm_vcpu *vcpu,
-			      struct sys_reg_params *p,
-			      const struct sys_reg_desc *r)
-{
-	return __access_id_reg(vcpu, p, r, true);
-}
-
 /* Visibility overrides for SVE-specific control registers */
 static unsigned int sve_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *rd)
@@ -1262,12 +1261,6 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 	return __set_id_reg(vcpu, rd, val, raz);
 }
 
-static int set_raz_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
-			  u64 val)
-{
-	return __set_id_reg(vcpu, rd, val, true);
-}
-
 static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		       u64 *val)
 {
@@ -1374,9 +1367,10 @@ static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
  */
 #define ID_UNALLOCATED(crm, op2) {			\
 	Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
-	.access = access_raz_id_reg,			\
-	.get_user = get_raz_reg,			\
-	.set_user = set_raz_id_reg,			\
+	.access = access_id_reg,			\
+	.get_user = get_id_reg,				\
+	.set_user = set_id_reg,				\
+	.visibility = raz_visibility			\
 }
 
 /*
@@ -1386,9 +1380,10 @@ static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
  */
 #define ID_HIDDEN(name) {			\
 	SYS_DESC(SYS_##name),			\
-	.access = access_raz_id_reg,		\
-	.get_user = get_raz_reg,		\
-	.set_user = set_raz_id_reg,		\
+	.access = access_id_reg,		\
+	.get_user = get_id_reg,			\
+	.set_user = set_id_reg,			\
+	.visibility = raz_visibility,		\
 }
 
 /*
Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] KVM: arm64: Use visibility hook to treat ID regs as RAZ

Quoted 6 lines · Oliver Upton

The generic id reg accessors already handle RAZ registers by way of the visibility hook. Add a visibility hook that returns REG_RAZ unconditionally and throw out the RAZ specific accessors.

#3 lore raw

PATCH 2/6 KVM: arm64: Remove internal accessor helpers for id regs

In reply to Oliver Upton

The internal accessors are only ever called once. Dump out their contents in the caller.

No functional change intended.

---
 arch/arm64/kvm/sys_regs.c | 46 ++++++++++-----------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
arch/arm64/kvm/sys_regs.c +12 -34
index e18efb9211f0..26210f3a0b27 100644
@@ -1153,25 +1153,17 @@ static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
 
 /* cpufeature ID register access trap handlers */
 
-static bool __access_id_reg(struct kvm_vcpu *vcpu,
-			    struct sys_reg_params *p,
-			    const struct sys_reg_desc *r,
-			    bool raz)
-{
-	if (p->is_write)
-		return write_to_read_only(vcpu, p, r);
-
-	p->regval = read_id_reg(vcpu, r, raz);
-	return true;
-}
-
 static bool access_id_reg(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
 {
 	bool raz = sysreg_visible_as_raz(vcpu, r);
 
-	return __access_id_reg(vcpu, p, r, raz);
+	if (p->is_write)
+		return write_to_read_only(vcpu, p, r);
+
+	p->regval = read_id_reg(vcpu, r, raz);
+	return true;
 }
 
 /* Visibility overrides for SVE-specific control registers */
@@ -1226,31 +1218,13 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
  * are stored, and for set_id_reg() we don't allow the effective value
  * to be changed.
  */
-static int __get_id_reg(const struct kvm_vcpu *vcpu,
-			const struct sys_reg_desc *rd, u64 *val,
-			bool raz)
-{
-	*val = read_id_reg(vcpu, rd, raz);
-	return 0;
-}
-
-static int __set_id_reg(const struct kvm_vcpu *vcpu,
-			const struct sys_reg_desc *rd, u64 val,
-			bool raz)
-{
-	/* This is what we mean by invariant: you can't change it. */
-	if (val != read_id_reg(vcpu, rd, raz))
-		return -EINVAL;
-
-	return 0;
-}
-
 static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		      u64 *val)
 {
 	bool raz = sysreg_visible_as_raz(vcpu, rd);
 
-	return __get_id_reg(vcpu, rd, val, raz);
+	*val = read_id_reg(vcpu, rd, raz);
+	return 0;
 }
 
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
@@ -1258,7 +1232,11 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
 	bool raz = sysreg_visible_as_raz(vcpu, rd);
 
-	return __set_id_reg(vcpu, rd, val, raz);
+	/* This is what we mean by invariant: you can't change it. */
+	if (val != read_id_reg(vcpu, rd, raz))
+		return -EINVAL;
+
+	return 0;
 }
 
 static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#4 lore raw

Re: [PATCH 2/6] KVM: arm64: Remove internal accessor helpers for id regs

Hi Oliver,

Quoted 81 lines · Oliver Upton

The internal accessors are only ever called once. Dump out their contents in the caller.

No functional change intended.

---
 arch/arm64/kvm/sys_regs.c | 46 ++++++++++-----------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
arch/arm64/kvm/sys_regs.c +7 -33
index e18efb9211f0..26210f3a0b27 100644
@@ -1153,25 +1153,17 @@ static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
 
 /* cpufeature ID register access trap handlers */
 
-static bool __access_id_reg(struct kvm_vcpu *vcpu,
-                           struct sys_reg_params *p,
-                           const struct sys_reg_desc *r,
-                           bool raz)
-{
-       if (p->is_write)
-               return write_to_read_only(vcpu, p, r);
-
-       p->regval = read_id_reg(vcpu, r, raz);
-       return true;
-}
-
 static bool access_id_reg(struct kvm_vcpu *vcpu,
                          struct sys_reg_params *p,
                          const struct sys_reg_desc *r)
 {
        bool raz = sysreg_visible_as_raz(vcpu, r);
 
-       return __access_id_reg(vcpu, p, r, raz);
+       if (p->is_write)
+               return write_to_read_only(vcpu, p, r);
+
+       p->regval = read_id_reg(vcpu, r, raz);
+       return true;
 }
 
 /* Visibility overrides for SVE-specific control registers */
@@ -1226,31 +1218,13 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
  * are stored, and for set_id_reg() we don't allow the effective value
  * to be changed.
  */
-static int __get_id_reg(const struct kvm_vcpu *vcpu,
-                       const struct sys_reg_desc *rd, u64 *val,
-                       bool raz)
-{
-       *val = read_id_reg(vcpu, rd, raz);
-       return 0;
-}
-
-static int __set_id_reg(const struct kvm_vcpu *vcpu,
-                       const struct sys_reg_desc *rd, u64 val,
-                       bool raz)
-{
-       /* This is what we mean by invariant: you can't change it. */
-       if (val != read_id_reg(vcpu, rd, raz))
-               return -EINVAL;
-
-       return 0;
-}
-
 static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
                      u64 *val)
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
-       return __get_id_reg(vcpu, rd, val, raz);
+       *val = read_id_reg(vcpu, rd, raz);
+       return 0;
 }
 
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,

arch/arm64/kvm/sys_regs.c static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, · line 1240

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +5 -1
@@ -1258,7 +1232,11 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
-       return __set_id_reg(vcpu, rd, val, raz);
+       /* This is what we mean by invariant: you can't change it. */
+       if (val != read_id_reg(vcpu, rd, raz))
+               return -EINVAL;
+
+       return 0;
 }

I see no reason for read_id_reg() to take raz as an argument. Perhaps having read_id_reg() call sysreg_visible_as_raz() instead might make those functions even simpler?

Thank you,
Reiji
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#5 lore raw

Re: [PATCH 2/6] KVM: arm64: Remove internal accessor helpers for id regs

Hi Reiji,

Quoted 87 lines · Reiji Watanabe

Hi Oliver,

Quoted 84 lines · Oliver Upton

The internal accessors are only ever called once. Dump out their contents in the caller.

No functional change intended.

---
 arch/arm64/kvm/sys_regs.c | 46 ++++++++++-----------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
arch/arm64/kvm/sys_regs.c +7 -33
index e18efb9211f0..26210f3a0b27 100644
@@ -1153,25 +1153,17 @@ static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
 
 /* cpufeature ID register access trap handlers */
 
-static bool __access_id_reg(struct kvm_vcpu *vcpu,
-                           struct sys_reg_params *p,
-                           const struct sys_reg_desc *r,
-                           bool raz)
-{
-       if (p->is_write)
-               return write_to_read_only(vcpu, p, r);
-
-       p->regval = read_id_reg(vcpu, r, raz);
-       return true;
-}
-
 static bool access_id_reg(struct kvm_vcpu *vcpu,
                          struct sys_reg_params *p,
                          const struct sys_reg_desc *r)
 {
        bool raz = sysreg_visible_as_raz(vcpu, r);
 
-       return __access_id_reg(vcpu, p, r, raz);
+       if (p->is_write)
+               return write_to_read_only(vcpu, p, r);
+
+       p->regval = read_id_reg(vcpu, r, raz);
+       return true;
 }
 
 /* Visibility overrides for SVE-specific control registers */
@@ -1226,31 +1218,13 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
  * are stored, and for set_id_reg() we don't allow the effective value
  * to be changed.
  */
-static int __get_id_reg(const struct kvm_vcpu *vcpu,
-                       const struct sys_reg_desc *rd, u64 *val,
-                       bool raz)
-{
-       *val = read_id_reg(vcpu, rd, raz);
-       return 0;
-}
-
-static int __set_id_reg(const struct kvm_vcpu *vcpu,
-                       const struct sys_reg_desc *rd, u64 val,
-                       bool raz)
-{
-       /* This is what we mean by invariant: you can't change it. */
-       if (val != read_id_reg(vcpu, rd, raz))
-               return -EINVAL;
-
-       return 0;
-}
-
 static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
                      u64 *val)
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
-       return __get_id_reg(vcpu, rd, val, raz);
+       *val = read_id_reg(vcpu, rd, raz);
+       return 0;
 }
 
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
@@ -1258,7 +1232,11 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
Quoted 11 lines

arch/arm64/kvm/sys_regs.c static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, · line 1240

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +5 -1
-       return __set_id_reg(vcpu, rd, val, raz);
+       /* This is what we mean by invariant: you can't change it. */
+       if (val != read_id_reg(vcpu, rd, raz))
+               return -EINVAL;
+
+       return 0;
 }

I see no reason for read_id_reg() to take raz as an argument. Perhaps having read_id_reg() call sysreg_visible_as_raz() instead might make those functions even simpler?

Good point, as this patch has done away with caller-specified RAZ. I'll incorporate that into v2.

--
Best,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#6 lore raw

PATCH 3/6 KVM: arm64: Spin off helper for calling visibility hook

In reply to Oliver Upton

No functional change intended.

---
 arch/arm64/kvm/sys_regs.h | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
arch/arm64/kvm/sys_regs.h +11 -8
index a8c4cc32eb9a..e78b51059622 100644
@@ -136,22 +136,25 @@ static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r
 	__vcpu_sys_reg(vcpu, r->reg) = r->val;
 }
 
-static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
-				 const struct sys_reg_desc *r)
+static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
+					     const struct sys_reg_desc *r)
 {
 	if (likely(!r->visibility))
-		return false;
+		return 0;
 
-	return r->visibility(vcpu, r) & REG_HIDDEN;
+	return r->visibility(vcpu, r);
+}
+
+static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
+				 const struct sys_reg_desc *r)
+{
+	return sysreg_visibility(vcpu, r) & REG_HIDDEN;
 }
 
 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
 					 const struct sys_reg_desc *r)
 {
-	if (likely(!r->visibility))
-		return false;
-
-	return r->visibility(vcpu, r) & REG_RAZ;
+	return sysreg_visibility(vcpu, r) & REG_RAZ;
 }
 
 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Reviewed-by on v1 3/6
#7 lore raw

Re: [PATCH 3/6] KVM: arm64: Spin off helper for calling visibility hook

Quoted 36 lines · Oliver Upton

No functional change intended.

---
 arch/arm64/kvm/sys_regs.h | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
arch/arm64/kvm/sys_regs.h +10 -4
index a8c4cc32eb9a..e78b51059622 100644
@@ -136,22 +136,25 @@ static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r
        __vcpu_sys_reg(vcpu, r->reg) = r->val;
 }
 
-static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
-                                const struct sys_reg_desc *r)
+static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
+                                            const struct sys_reg_desc *r)
 {
        if (likely(!r->visibility))
-               return false;
+               return 0;
 
-       return r->visibility(vcpu, r) & REG_HIDDEN;
+       return r->visibility(vcpu, r);
+}
+
+static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
+                                const struct sys_reg_desc *r)
+{
+       return sysreg_visibility(vcpu, r) & REG_HIDDEN;
 }
 
 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,

arch/arm64/kvm/sys_regs.h static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r · line 160

Quoted 10 lines
arch/arm64/kvm/sys_regs.h +1 -4
                                         const struct sys_reg_desc *r)
 {
-       if (likely(!r->visibility))
-               return false;
-
-       return r->visibility(vcpu, r) & REG_RAZ;
+       return sysreg_visibility(vcpu, r) & REG_RAZ;
 }
 
 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,

Thank you,
Reiji

#8 lore raw

PATCH 4/6 KVM: arm64: Add a visibility bit to ignore user writes

In reply to Oliver Upton

We're about to ignore writes to AArch32 ID registers on AArch64-only systems. Add a bit to indicate a register is handled as write ignore when accessed from userspace.

---
 arch/arm64/kvm/sys_regs.c | 3 +++
 arch/arm64/kvm/sys_regs.h | 7 +++++++
 2 files changed, 10 insertions(+)
arch/arm64/kvm/sys_regs.c +3 -0
index 26210f3a0b27..9f06c85f26b8 100644
@@ -1232,6 +1232,9 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
 	bool raz = sysreg_visible_as_raz(vcpu, rd);
 
+	if (sysreg_user_write_ignore(vcpu, rd))
+		return 0;
+
 	/* This is what we mean by invariant: you can't change it. */
 	if (val != read_id_reg(vcpu, rd, raz))
 		return -EINVAL;
arch/arm64/kvm/sys_regs.h +7 -0
index e78b51059622..e4ebb3a379fd 100644
@@ -86,6 +86,7 @@ struct sys_reg_desc {
 
 #define REG_HIDDEN		(1 << 0) /* hidden from userspace and guest */
 #define REG_RAZ			(1 << 1) /* RAZ from userspace and guest */
+#define REG_USER_WI		(1 << 2) /* WI from userspace only */
 
 static __printf(2, 3)
 inline void print_sys_reg_msg(const struct sys_reg_params *p,
@@ -157,6 +158,12 @@ static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
 	return sysreg_visibility(vcpu, r) & REG_RAZ;
 }
 
+static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
+					    const struct sys_reg_desc *r)
+{
+	return sysreg_visibility(vcpu, r) & REG_USER_WI;
+}
+
 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
 			      const struct sys_reg_desc *i2)
 {
Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#9 lore raw

Re: [PATCH 4/6] KVM: arm64: Add a visibility bit to ignore user writes

Hi Oliver,

Quoted 16 lines · Oliver Upton

We're about to ignore writes to AArch32 ID registers on AArch64-only systems. Add a bit to indicate a register is handled as write ignore when accessed from userspace.

---
 arch/arm64/kvm/sys_regs.c | 3 +++
 arch/arm64/kvm/sys_regs.h | 7 +++++++
 2 files changed, 10 insertions(+)
arch/arm64/kvm/sys_regs.c +0 -0
index 26210f3a0b27..9f06c85f26b8 100644

arch/arm64/kvm/sys_regs.c static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, · line 1236

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -1232,6 +1232,9 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
+       if (sysreg_user_write_ignore(vcpu, rd))
+               return 0;

Since the visibility flags are not ID register specific, have you considered checking REG_USER_WI from kvm_sys_reg_set_user() rather than the ID register specific function ?

This patch made me reconsider my comment for the patch-2. Perhaps it might be more appropriate to check RAZ visibility from kvm_sys_reg_get_user() rather than the ID register specific function ?

REG_HIDDEN is already checked from kvm_sys_reg_{s,g}et_user() (indirectly).

Thank you,
Reiji

Quoted 32 lines
arch/arm64/kvm/sys_regs.c +1 -0
+
        /* This is what we mean by invariant: you can't change it. */
        if (val != read_id_reg(vcpu, rd, raz))
                return -EINVAL;
arch/arm64/kvm/sys_regs.h +7 -1
index e78b51059622..e4ebb3a379fd 100644
@@ -86,6 +86,7 @@ struct sys_reg_desc {
 
 #define REG_HIDDEN             (1 << 0) /* hidden from userspace and guest */
 #define REG_RAZ                        (1 << 1) /* RAZ from userspace and guest */
+#define REG_USER_WI            (1 << 2) /* WI from userspace only */
 
 static __printf(2, 3)
 inline void print_sys_reg_msg(const struct sys_reg_params *p,
@@ -157,6 +158,12 @@ static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
        return sysreg_visibility(vcpu, r) & REG_RAZ;
 }
 
+static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
+                                           const struct sys_reg_desc *r)
+{
+       return sysreg_visibility(vcpu, r) & REG_USER_WI;
+}
+
 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
                              const struct sys_reg_desc *i2)
 {
--

2.37.1.595.g718a3a8f04-goog

Quoted 4 lines

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#10 lore raw

Re: [PATCH 4/6] KVM: arm64: Add a visibility bit to ignore user writes

Quoted 22 lines · Reiji Watanabe

Hi Oliver,

Quoted 19 lines · Oliver Upton

We're about to ignore writes to AArch32 ID registers on AArch64-only systems. Add a bit to indicate a register is handled as write ignore when accessed from userspace.

---
 arch/arm64/kvm/sys_regs.c | 3 +++
 arch/arm64/kvm/sys_regs.h | 7 +++++++
 2 files changed, 10 insertions(+)
arch/arm64/kvm/sys_regs.c +0 -0
index 26210f3a0b27..9f06c85f26b8 100644
@@ -1232,6 +1232,9 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
Quoted 6 lines

arch/arm64/kvm/sys_regs.c static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, · line 1236

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+       if (sysreg_user_write_ignore(vcpu, rd))
+               return 0;

Since the visibility flags are not ID register specific, have you considered checking REG_USER_WI from kvm_sys_reg_set_user() rather than the ID register specific function ?

Yeah, that's definitely a better place to wire it in.

Quoted 3 lines

This patch made me reconsider my comment for the patch-2. Perhaps it might be more appropriate to check RAZ visibility from kvm_sys_reg_get_user() rather than the ID register specific function ?

REG_RAZ hides the register value from the guest as well as userspace, so it might be better to leave it in place. REG_RAZ also has implications for writing a register from userspace, as we still apply the expectation of invariance to ID registers that set this flag.

It all 'just works' right now with the check buried in the ID register accessors. Going the other way around would require sprinkling the check in several locations.

--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#11 lore raw

Re: [PATCH 4/6] KVM: arm64: Add a visibility bit to ignore user writes

Quoted 37 lines · Oliver Upton
Quoted 22 lines · Reiji Watanabe

Hi Oliver,

Quoted 19 lines · Oliver Upton

We're about to ignore writes to AArch32 ID registers on AArch64-only systems. Add a bit to indicate a register is handled as write ignore when accessed from userspace.

---
 arch/arm64/kvm/sys_regs.c | 3 +++
 arch/arm64/kvm/sys_regs.h | 7 +++++++
 2 files changed, 10 insertions(+)
arch/arm64/kvm/sys_regs.c +0 -0
index 26210f3a0b27..9f06c85f26b8 100644
@@ -1232,6 +1232,9 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 {
        bool raz = sysreg_visible_as_raz(vcpu, rd);
 
Quoted 6 lines

arch/arm64/kvm/sys_regs.c static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, · line 1236

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+       if (sysreg_user_write_ignore(vcpu, rd))
+               return 0;

Since the visibility flags are not ID register specific, have you considered checking REG_USER_WI from kvm_sys_reg_set_user() rather than the ID register specific function ?

Yeah, that's definitely a better place to wire it in.

Quoted 3 lines

This patch made me reconsider my comment for the patch-2. Perhaps it might be more appropriate to check RAZ visibility from kvm_sys_reg_get_user() rather than the ID register specific function ?

Quoted 8 lines

REG_RAZ hides the register value from the guest as well as userspace, so it might be better to leave it in place. REG_RAZ also has implications for writing a register from userspace, as we still apply the expectation of invariance to ID registers that set this flag.

It all 'just works' right now with the check buried in the ID register accessors. Going the other way around would require sprinkling the check in several locations.

Ah, I see the handling of REG_RAZ is a bit tricky...
I kind of suspect that REG_RAZ won't probably be used for any registers other than ID registers even in the future...

Anyway, yes, it might be better to leave it in place at least for now.

Thank you,
Reiji
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#12 lore raw

PATCH 5/6 KVM: arm64: Treat 32bit ID registers as RAZ/WI on 64bit-only system

In reply to Oliver Upton

One of the oddities of the architecture is that the AArch64 views of the AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any EL. Nonetheless, KVM exposes these registers to userspace for the sake of save/restore. It is possible that the UNKNOWN value could differ between systems, leading to a rejected write from userspace.

Avoid the issue altogether by handling the AArch32 ID registers as RAZ/WI when on an AArch64-only system.

---
 arch/arm64/kvm/sys_regs.c | 63 ++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 20 deletions(-)
arch/arm64/kvm/sys_regs.c +43 -20
index 9f06c85f26b8..5f6a633182c8 100644
@@ -1145,6 +1145,20 @@ static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 	return 0;
 }
 
+static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
+				       const struct sys_reg_desc *r)
+{
+	/*
+	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
+	 * EL. Promote to RAZ/WI in order to guarantee consistency between
+	 * systems.
+	 */
+	if (!kvm_supports_32bit_el0())
+		return REG_RAZ | REG_USER_WI;
+
+	return id_visibility(vcpu, r);
+}
+
 static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *r)
 {
@@ -1341,6 +1355,15 @@ static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
 	.visibility = id_visibility,		\
 }
 
+/* sys_reg_desc initialiser for known cpufeature ID registers */
+#define AA32_ID_SANITISED(name) {		\
+	SYS_DESC(SYS_##name),			\
+	.access	= access_id_reg,		\
+	.get_user = get_id_reg,			\
+	.set_user = set_id_reg,			\
+	.visibility = aa32_id_visibility,	\
+}
+
 /*
  * sys_reg_desc initialiser for architecturally unallocated cpufeature ID
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
@@ -1428,33 +1451,33 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 
 	/* AArch64 mappings of the AArch32 ID registers */
 	/* CRm=1 */
-	ID_SANITISED(ID_PFR0_EL1),
-	ID_SANITISED(ID_PFR1_EL1),
-	ID_SANITISED(ID_DFR0_EL1),
+	AA32_ID_SANITISED(ID_PFR0_EL1),
+	AA32_ID_SANITISED(ID_PFR1_EL1),
+	AA32_ID_SANITISED(ID_DFR0_EL1),
 	ID_HIDDEN(ID_AFR0_EL1),
-	ID_SANITISED(ID_MMFR0_EL1),
-	ID_SANITISED(ID_MMFR1_EL1),
-	ID_SANITISED(ID_MMFR2_EL1),
-	ID_SANITISED(ID_MMFR3_EL1),
+	AA32_ID_SANITISED(ID_MMFR0_EL1),
+	AA32_ID_SANITISED(ID_MMFR1_EL1),
+	AA32_ID_SANITISED(ID_MMFR2_EL1),
+	AA32_ID_SANITISED(ID_MMFR3_EL1),
 
 	/* CRm=2 */
-	ID_SANITISED(ID_ISAR0_EL1),
-	ID_SANITISED(ID_ISAR1_EL1),
-	ID_SANITISED(ID_ISAR2_EL1),
-	ID_SANITISED(ID_ISAR3_EL1),
-	ID_SANITISED(ID_ISAR4_EL1),
-	ID_SANITISED(ID_ISAR5_EL1),
-	ID_SANITISED(ID_MMFR4_EL1),
-	ID_SANITISED(ID_ISAR6_EL1),
+	AA32_ID_SANITISED(ID_ISAR0_EL1),
+	AA32_ID_SANITISED(ID_ISAR1_EL1),
+	AA32_ID_SANITISED(ID_ISAR2_EL1),
+	AA32_ID_SANITISED(ID_ISAR3_EL1),
+	AA32_ID_SANITISED(ID_ISAR4_EL1),
+	AA32_ID_SANITISED(ID_ISAR5_EL1),
+	AA32_ID_SANITISED(ID_MMFR4_EL1),
+	AA32_ID_SANITISED(ID_ISAR6_EL1),
 
 	/* CRm=3 */
-	ID_SANITISED(MVFR0_EL1),
-	ID_SANITISED(MVFR1_EL1),
-	ID_SANITISED(MVFR2_EL1),
+	AA32_ID_SANITISED(MVFR0_EL1),
+	AA32_ID_SANITISED(MVFR1_EL1),
+	AA32_ID_SANITISED(MVFR2_EL1),
 	ID_UNALLOCATED(3,3),
-	ID_SANITISED(ID_PFR2_EL1),
+	AA32_ID_SANITISED(ID_PFR2_EL1),
 	ID_HIDDEN(ID_DFR1_EL1),
-	ID_SANITISED(ID_MMFR5_EL1),
+	AA32_ID_SANITISED(ID_MMFR5_EL1),
 	ID_UNALLOCATED(3,7),
 
 	/* AArch64 ID registers */
Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
maintainer
#13 lore raw
Quoted 23 lines · Oliver Upton

One of the oddities of the architecture is that the AArch64 views of the AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any EL. Nonetheless, KVM exposes these registers to userspace for the sake of save/restore. It is possible that the UNKNOWN value could differ between systems, leading to a rejected write from userspace.

Avoid the issue altogether by handling the AArch32 ID registers as RAZ/WI when on an AArch64-only system.

---
 arch/arm64/kvm/sys_regs.c | 63 ++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 20 deletions(-)
arch/arm64/kvm/sys_regs.c +0 -0
index 9f06c85f26b8..5f6a633182c8 100644
@@ -1145,6 +1145,20 @@ static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 	return 0;
 }
 

arch/arm64/kvm/sys_regs.c static unsigned int id_visibility(const struct kvm_vcpu *vcpu, · line 1157

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
+				       const struct sys_reg_desc *r)
+{
+	/*
+	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
+	 * EL. Promote to RAZ/WI in order to guarantee consistency between
+	 * systems.
+	 */
+	if (!kvm_supports_32bit_el0())
+		return REG_RAZ | REG_USER_WI;

This is probably only a nit, but why does one visibility has a _USER_ tag while the other doesn't? In other word, what sysregs are WI from userspace that aren't so from the guest?

Also, do we have any cases where RAZ and WI would be used independently? My gut feeling is that RAZ implies WI in most (all?) cases. If this assumption holds, shouldn't we simply rename REG_RAZ to REG_RAZ_WI and be done with it?

Thanks,

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#14 lore raw

Hey Marc,

Thanks for the review!

Quoted 40 lines · Marc Zyngier
Quoted 23 lines · Oliver Upton

One of the oddities of the architecture is that the AArch64 views of the AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any EL. Nonetheless, KVM exposes these registers to userspace for the sake of save/restore. It is possible that the UNKNOWN value could differ between systems, leading to a rejected write from userspace.

Avoid the issue altogether by handling the AArch32 ID registers as RAZ/WI when on an AArch64-only system.

---
 arch/arm64/kvm/sys_regs.c | 63 ++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 20 deletions(-)
arch/arm64/kvm/sys_regs.c +0 -0
index 9f06c85f26b8..5f6a633182c8 100644
@@ -1145,6 +1145,20 @@ static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 	return 0;
 }
 

arch/arm64/kvm/sys_regs.c static unsigned int id_visibility(const struct kvm_vcpu *vcpu, · line 1157

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
+				       const struct sys_reg_desc *r)
+{
+	/*
+	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
+	 * EL. Promote to RAZ/WI in order to guarantee consistency between
+	 * systems.
+	 */
+	if (!kvm_supports_32bit_el0())
+		return REG_RAZ | REG_USER_WI;

This is probably only a nit, but why does one visibility has a _USER_ tag while the other doesn't? In other word, what sysregs are WI from userspace that aren't so from the guest?

Quoted 4 lines

Also, do we have any cases where RAZ and WI would be used independently? My gut feeling is that RAZ implies WI in most (all?) cases. If this assumption holds, shouldn't we simply rename REG_RAZ to REG_RAZ_WI and be done with it?

Yeah, this reads a bit strange, but there is some reason around it (I think!)

As it applies to ID registers, REG_RAZ already implies RAZ w/ immutable writes (-EINVAL if something different is written). As such I didn't want to change the meaning of the other ID registers to WI and only ignore writes for the registers that could have an UNKNOWN value. Furthermore, I added the _USER_ tag to make it clear that we aren't magically allowing writes from the guest to these registers.

I think we will need an additional visibility bit (or special accessor, which I tried to avoid) to precisely apply WI to the 32bit registers, but if the _USER_ tag is distracting I can get rid of it. After all, hardware should politely UNDEF the guest when writing to such a register.

Thoughts?

--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#15 lore raw

PATCH 6/6 KVM: selftests: Add test for RAZ/WI AArch32 ID registers

In reply to Oliver Upton

Add a test to assert that KVM handles the AArch64 views of the AArch32 ID registers as RAZ/WI (writable only from userspace).

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/aarch64_only_id_regs.c        | 135 ++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/aarch64_only_id_regs.c
tools/testing/selftests/kvm/.gitignore +1 -0
index d625a3f83780..4331af62a982 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
+/aarch64/aarch64_only_id_regs
 /aarch64/arch_timer
 /aarch64/debug-exceptions
 /aarch64/get-reg-list
tools/testing/selftests/kvm/Makefile +1 -0
index 4c122f1b1737..efe155259095 100644
@@ -144,6 +144,7 @@ TEST_GEN_PROGS_x86_64 += system_counter_offset_test
 # Compiled outputs used by test targets
 TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
 
+TEST_GEN_PROGS_aarch64 += aarch64/aarch64_only_id_regs
 TEST_GEN_PROGS_aarch64 += aarch64/arch_timer
 TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
 TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
tools/testing/selftests/kvm/aarch64/aarch64_only_id_regs.c new file +135 -0

135 lines not on this page · raw message

Signature · 6 lines
2.37.1.595.g718a3a8f04-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm