KVM: arm64: Stash OSLSR_EL1 in the cpu context
authorOliver Upton <oupton@google.com>
Thu, 3 Feb 2022 17:41:55 +0000 (17:41 +0000)
committerMarc Zyngier <maz@kernel.org>
Tue, 8 Feb 2022 14:23:41 +0000 (14:23 +0000)
An upcoming change to KVM will emulate the OS Lock from the PoV of the
guest. Add OSLSR_EL1 to the cpu context and handle reads using the
stored value. Define some mnemonics for for handling the OSLM field and
use them to make the reset value of OSLSR_EL1 more readable.

Wire up a custom handler for writes from userspace and prevent any of
the invariant bits from changing. Note that the OSLK bit is not
invariant and will be made writable by the aforementioned change.

Reviewed-by: Reiji Watanabe <reijiw@google.com>
Signed-off-by: Oliver Upton <oupton@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220203174159.2887882-3-oupton@google.com
arch/arm64/include/asm/kvm_host.h
arch/arm64/include/asm/sysreg.h
arch/arm64/kvm/sys_regs.c

index 5bc01e62c08a03d55f12d1bafa9f6fc9ead49f10..cc1cc40d89f0b327a114663f77a493ae90df97a7 100644 (file)
@@ -171,6 +171,7 @@ enum vcpu_sysreg {
        PAR_EL1,        /* Physical Address Register */
        MDSCR_EL1,      /* Monitor Debug System Control Register */
        MDCCINT_EL1,    /* Monitor Debug Comms Channel Interrupt Enable Reg */
+       OSLSR_EL1,      /* OS Lock Status Register */
        DISR_EL1,       /* Deferred Interrupt Status Register */
 
        /* Performance Monitors Registers */
index 898bee0004aee603d7e51717bbb4643dbd0e6d2b..abc85eaa453d67390af611a1fba46e5266133e6a 100644 (file)
 #define SYS_DBGWCRn_EL1(n)             sys_reg(2, 0, 0, n, 7)
 #define SYS_MDRAR_EL1                  sys_reg(2, 0, 1, 0, 0)
 #define SYS_OSLAR_EL1                  sys_reg(2, 0, 1, 0, 4)
+
 #define SYS_OSLSR_EL1                  sys_reg(2, 0, 1, 1, 4)
+#define SYS_OSLSR_OSLM_MASK            (BIT(3) | BIT(0))
+#define SYS_OSLSR_OSLM_NI              0
+#define SYS_OSLSR_OSLM_IMPLEMENTED     BIT(3)
+
 #define SYS_OSDLR_EL1                  sys_reg(2, 0, 1, 3, 4)
 #define SYS_DBGPRCR_EL1                        sys_reg(2, 0, 1, 4, 4)
 #define SYS_DBGCLAIMSET_EL1            sys_reg(2, 0, 7, 8, 6)
index 85208acd273dcf1e2e8c4efb2c45dc0c344a35bb..b8286c31e01cbaddf937af7f54c4f56de84e1c68 100644 (file)
@@ -291,12 +291,28 @@ static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
                           struct sys_reg_params *p,
                           const struct sys_reg_desc *r)
 {
-       if (p->is_write) {
+       if (p->is_write)
                return write_to_read_only(vcpu, p, r);
-       } else {
-               p->regval = (1 << 3);
-               return true;
-       }
+
+       p->regval = __vcpu_sys_reg(vcpu, r->reg);
+       return true;
+}
+
+static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
+                        const struct kvm_one_reg *reg, void __user *uaddr)
+{
+       u64 id = sys_reg_to_index(rd);
+       u64 val;
+       int err;
+
+       err = reg_from_user(&val, uaddr, id);
+       if (err)
+               return err;
+
+       if (val != rd->val)
+               return -EINVAL;
+
+       return 0;
 }
 
 static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
@@ -1448,7 +1464,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 
        { SYS_DESC(SYS_MDRAR_EL1), trap_raz_wi },
        { SYS_DESC(SYS_OSLAR_EL1), trap_raz_wi },
-       { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1 },
+       { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_val, OSLSR_EL1,
+               SYS_OSLSR_OSLM_IMPLEMENTED, .set_user = set_oslsr_el1, },
        { SYS_DESC(SYS_OSDLR_EL1), trap_raz_wi },
        { SYS_DESC(SYS_DBGPRCR_EL1), trap_raz_wi },
        { SYS_DESC(SYS_DBGCLAIMSET_EL1), trap_raz_wi },
@@ -1923,7 +1940,7 @@ static const struct sys_reg_desc cp14_regs[] = {
        { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi },
        DBGBXVR(1),
        /* DBGOSLSR */
-       { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 },
+       { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1, NULL, OSLSR_EL1 },
        DBGBXVR(2),
        DBGBXVR(3),
        /* DBGOSDLR */