Skip to contentSkip to conversation

Support writable CPU ID registers from userspace

PATCH Superseded Jing Zhang 3 participants 11 messages Apr 24, 2023 – Apr 26, 2023

Subsystems (3)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

v7 cover Support writable CPU ID registers from userspace

This patchset refactors/adds code to support writable per guest CPU ID feature registers. Part of the code/ideas are from
https://lore.kernel.org/all/[email protected] . No functional change is intended in this patchset. With the new CPU ID feature registers infrastructure, only writtings of ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon are allowed as KVM does before.

Writable (Configurable) per guest CPU ID feature registers are useful for creating/migrating guest on ARM CPUs with different kinds of features.

This patchset uses kvm->arch.config_lock from Oliver's lock inversion fixes at
https://lore.kernel.org/linux-arm-kernel/[email protected]/

---

* v6 -> v7
- Rebased to v6.3-rc7.
- Add helpers for idregs read/write.
- Guard all idregs reads/writes.
- Add code to fix features' safe value type which is different for KVM than for the host.

* v5 -> v6
- Rebased to v6.3-rc5.
- Reuse struct sys_reg_desc's reset() callback and field val for KVM. sanitisation function and writable mask instead of creating a new data structure for idregs.
- Use get_arm64_ftr_reg() instead of exposing idregs ftr_bits array.

* v4 -> v5
- Rebased to 2fad20ae05cb (kvmarm/next)
Merge branch kvm-arm64/selftest/misc-6,4 into kvmarm-master/next
- Use kvm->arch.config_lock to guard update to multiple VM scope idregs to avoid lock inversion
- Add back IDREG() macro for idregs access
- Refactor struct id_reg_desc by using existing infrastructure.
- Addressed many other comments from Marc.

* v3 -> v4
- Remove IDREG() macro for ID reg access, use simple array access instead
- Rename kvm_arm_read_id_reg_with_encoding() to kvm_arm_read_id_reg()
- Save perfmon value in ID_DFR0_EL1 instead of pmuver
- Update perfmon in ID_DFR0_EL1 and pmuver in ID_AA64DFR0_EL1 atomically
- Remove kvm_vcpu_has_pmu() in macro kvm_pmu_is_3p5()
- Improve ID register sanity checking in kvm_arm_check_idreg_table()

* v2 -> v3
- Rebased to 96a4627dbbd4 (kvmarm/next)
Merge tag ' https://github.com/oupton/linux tags/kvmarm-6.3' from into kvmarm-master/next
- Add id registere emulation entry point function emulate_id_reg
- Fix consistency for ID_AA64DFR0_EL1.PMUVer and ID_DFR0_EL1.PerfMon
- Improve the checking for id register table by ensuring that every entry has the correct id register encoding.
- Addressed other comments from Reiji and Marc.

* v1 -> v2
- Rebase to 7121a2e1d107 (kvmarm/next) Merge branch kvm-arm64/nv-prefix into kvmarm/next
- Address writing issue for PMUVer

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
[3] https://lore.kernel.org/all/[email protected]
[4] https://lore.kernel.org/all/[email protected]
[5] https://lore.kernel.org/all/[email protected]
[6] https://lore.kernel.org/all/[email protected]

---

Jing Zhang (6):
KVM: arm64: Move CPU ID feature registers emulation into a separate
file
KVM: arm64: Save ID registers' sanitized value per guest
KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]
KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer
KVM: arm64: Reuse fields of sys_reg_desc for idreg
KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

 arch/arm64/include/asm/cpufeature.h |   1 +
 arch/arm64/include/asm/kvm_host.h   |  60 ++-
 arch/arm64/kernel/cpufeature.c      |   2 +-
 arch/arm64/kvm/Makefile             |   2 +-
 arch/arm64/kvm/arm.c                |  24 +-
 arch/arm64/kvm/id_regs.c            | 692 ++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c           | 525 ++++-----------------
 arch/arm64/kvm/sys_regs.h           |  28 +-
 include/kvm/arm_pmu.h               |   5 +-
 9 files changed, 857 insertions(+), 482 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c

base-commit: 6a8f57ae2eb07ab39a6f0ccad60c760743051026

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
author
#1 lore raw

PATCH v7 1/6 KVM: arm64: Move CPU ID feature registers emulation into a separate file

Create a new file id_regs.c for CPU ID feature registers emulation code, which are moved from sys_regs.c and tweak sys_regs code accordingly.

No functional change intended.

---
 arch/arm64/kvm/Makefile   |   2 +-
 arch/arm64/kvm/id_regs.c  | 460 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++++----------------------------------
 arch/arm64/kvm/sys_regs.h |  19 ++
 4 files changed, 519 insertions(+), 426 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c
arch/arm64/kvm/Makefile +1 -1
index c0c050e53157..a6a315fcd81e 100644
@@ -13,7 +13,7 @@ obj-$(CONFIG_KVM) += hyp/
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
-	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+	 vgic-sys-reg-v3.o fpsimd.o pkvm.o id_regs.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o \
 	 vgic/vgic.o vgic/vgic-init.o \
 	 vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/id_regs.c new file +460 -0

460 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +39 -425

569 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +19 -0
index 6b11f2cc7146..7ce546a8be60 100644
@@ -210,6 +210,19 @@ find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
 	return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
 }
 
+/*
+ * Return true if the register's (Op0, Op1, CRn, CRm, Op2) is
+ * (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+ */
+static inline bool is_id_reg(u32 id)
+{
+	return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
+		sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
+		sys_reg_CRm(id) < 8);
+}
+
+void perform_access(struct kvm_vcpu *vcpu, struct sys_reg_params *params,
+		    const struct sys_reg_desc *r);
 const struct sys_reg_desc *get_reg_by_id(u64 id,
 					 const struct sys_reg_desc table[],
 					 unsigned int num);
@@ -220,6 +233,10 @@ int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
 			 const struct sys_reg_desc table[], unsigned int num);
 int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
 			 const struct sys_reg_desc table[], unsigned int num);
+bool write_to_read_only(struct kvm_vcpu *vcpu,
+			struct sys_reg_params *params, const struct sys_reg_desc *r);
+unsigned int raz_visibility(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *r);
+int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
 
 #define AA32(_x)	.aarch32_map = AA32_##_x
 #define Op0(_x) 	.Op0 = _x
@@ -234,4 +251,6 @@ int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
 	CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)),	\
 	Op2(sys_reg_Op2(reg))
 
+#define KVM_ARM_ID_REG_NUM	56
+
 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
author
#2 lore raw

PATCH v7 2/6 KVM: arm64: Save ID registers' sanitized value per guest

In reply to Jing Zhang

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 47 +++++++++++++++++++++++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 49 +++++++++++++++++++++++++------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  3 +-
 5 files changed, 90 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +47 -0
index bcd774d74f34..2b1fe90a1790 100644
@@ -177,6 +177,20 @@ struct kvm_smccc_features {
 	unsigned long vendor_hyp_bmap;
 };
 
+/*
+ * Emualted CPU ID registers per VM
+ * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+ * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+ *
+ * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+ * Access to id regs are guarded by kvm_arch.config_lock.
+ */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+struct kvm_idregs {
+	u64 regs[KVM_ARM_ID_REG_NUM];
+};
+
 typedef unsigned int pkvm_handle_t;
 
 struct kvm_protected_vm {
@@ -243,6 +257,9 @@ struct kvm_arch {
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
+	/* Emulated CPU ID registers */
+	struct kvm_idregs idregs;
+
 	/*
 	 * For an untrusted host VM, 'pkvm.handle' is used to lookup
 	 * the associated pKVM instance in the hypervisor.
@@ -1008,6 +1025,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_init_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
@@ -1073,4 +1092,32 @@ static inline void kvm_hyp_reserve(void) { }
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
+static inline u64 _idreg_read(struct kvm_arch *arch, u32 id)
+{
+	return arch->idregs.regs[IDREG_IDX(id)];
+}
+
+static inline void _idreg_write(struct kvm_arch *arch, u32 id, u64 val)
+{
+	arch->idregs.regs[IDREG_IDX(id)] = val;
+}
+
+static inline u64 idreg_read(struct kvm_arch *arch, u32 id)
+{
+	u64 val;
+
+	mutex_lock(&arch->config_lock);
+	val = _idreg_read(arch, id);
+	mutex_unlock(&arch->config_lock);
+
+	return val;
+}
+
+static inline void idreg_write(struct kvm_arch *arch, u32 id, u64 val)
+{
+	mutex_lock(&arch->config_lock);
+	_idreg_write(arch, id, val);
+	mutex_unlock(&arch->config_lock);
+}
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/arm.c +1 -0
index 4b2e16e696a8..e34744c36406 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	set_default_spectre(kvm);
 	kvm_arm_init_hypercalls(kvm);
+	kvm_arm_init_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +40 -9

65 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +1 -1
index 8dba10696dac..6370a0db9d26 100644
@@ -364,7 +364,7 @@ static bool trap_loregion(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
 {
-	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
+	u64 val = kvm_arm_read_id_reg(vcpu, SYS_ID_AA64MMFR1_EL1);
 	u32 sr = reg_to_encoding(r);
 
 	if (!(val & (0xfUL << ID_AA64MMFR1_EL1_LO_SHIFT))) {
arch/arm64/kvm/sys_regs.h +1 -2
index 7ce546a8be60..e88fd77309b2 100644
@@ -237,6 +237,7 @@ bool write_to_read_only(struct kvm_vcpu *vcpu,
 			struct sys_reg_params *params, const struct sys_reg_desc *r);
 unsigned int raz_visibility(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *r);
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
+u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id);
 
 #define AA32(_x)	.aarch32_map = AA32_##_x
 #define Op0(_x) 	.Op0 = _x
@@ -251,6 +252,4 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
 	CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)),	\
 	Op2(sys_reg_Op2(reg))
 
-#define KVM_ARM_ID_REG_NUM	56
-
 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#3 lore raw

Re: [PATCH v7 2/6] KVM: arm64: Save ID registers' sanitized value per guest

kernel test robot noticed the following build errors:

Hi Jing,

kernel test robot noticed the following build errors:

[auto build test ERROR on 6a8f57ae2eb07ab39a6f0ccad60c760743051026]

url:    https://github.com/intel-lab-lkp/linux/commits/Jing-Zhang/KVM-arm64-Move-CPU-ID-feature-registers-emulation-into-a-separate-file/20230425-074908
base:   6a8f57ae2eb07ab39a6f0ccad60c760743051026
patch link:    https://lore.kernel.org/r/20230424234704.2571444-3-jingzhangos%40google.com
patch subject: [PATCH v7 2/6] KVM: arm64: Save ID registers' sanitized value per guest
config: arm64-randconfig-r012-20230425 (https://download.01.org/0day-ci/archive/20230425/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project 437b7602e4a998220871de78afcb020b9c14a661)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/cca571c516149d39bbaf1b5e916df617940458a6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jing-Zhang/KVM-arm64-Move-CPU-ID-feature-registers-emulation-into-a-separate-file/20230425-074908 git checkout cca571c516149d39bbaf1b5e916df617940458a6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 prepare

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/arm64/kernel/asm-offsets.c:16:
In file included from include/linux/kvm_host.h:45:

>> arch/arm64/include/asm/kvm_host.h:1109:20: error: no member named 'config_lock' in 'struct kvm_arch'
           mutex_lock(&arch->config_lock);
                       ~~~~  ^
   arch/arm64/include/asm/kvm_host.h:1111:22: error: no member named 'config_lock' in 'struct kvm_arch'
           mutex_unlock(&arch->config_lock);
                         ~~~~  ^
   arch/arm64/include/asm/kvm_host.h:1118:20: error: no member named 'config_lock' in 'struct kvm_arch'
           mutex_lock(&arch->config_lock);
                       ~~~~  ^
   arch/arm64/include/asm/kvm_host.h:1120:22: error: no member named 'config_lock' in 'struct kvm_arch'
           mutex_unlock(&arch->config_lock);
                         ~~~~  ^

4 errors generated.
make[2]: *** [scripts/Makefile.build:114: arch/arm64/kernel/asm-offsets.s] Error 1 make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:1286: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:226: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +1109 arch/arm64/include/asm/kvm_host.h

1104
1105 static inline u64 idreg_read(struct kvm_arch *arch, u32 id)
1106 {
1107 u64 val;
1108

> 1109		mutex_lock(&arch->config_lock);

1110 val = _idreg_read(arch, id);
1111 mutex_unlock(&arch->config_lock);
1112
1113 return val;
1114 }
1115

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#4 lore raw

Re: [PATCH v7 2/6] KVM: arm64: Save ID registers' sanitized value per guest

In reply to Jing Zhang
kernel test robot noticed the following build errors:

Hi Jing,

kernel test robot noticed the following build errors:

[auto build test ERROR on 6a8f57ae2eb07ab39a6f0ccad60c760743051026]

url:    https://github.com/intel-lab-lkp/linux/commits/Jing-Zhang/KVM-arm64-Move-CPU-ID-feature-registers-emulation-into-a-separate-file/20230425-074908
base:   6a8f57ae2eb07ab39a6f0ccad60c760743051026
patch link:    https://lore.kernel.org/r/20230424234704.2571444-3-jingzhangos%40google.com
patch subject: [PATCH v7 2/6] KVM: arm64: Save ID registers' sanitized value per guest
config: arm64-randconfig-r034-20230425 (https://download.01.org/0day-ci/archive/20230425/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/cca571c516149d39bbaf1b5e916df617940458a6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jing-Zhang/KVM-arm64-Move-CPU-ID-feature-registers-emulation-into-a-separate-file/20230425-074908 git checkout cca571c516149d39bbaf1b5e916df617940458a6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 prepare

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/kvm_host.h:45,

                    from arch/arm64/kernel/asm-offsets.c:16:
   arch/arm64/include/asm/kvm_host.h: In function 'idreg_read':
>> arch/arm64/include/asm/kvm_host.h:1109:25: error: 'struct kvm_arch' has no member named 'config_lock'
    1109 |         mutex_lock(&arch->config_lock);
         |                         ^~
   arch/arm64/include/asm/kvm_host.h:1111:27: error: 'struct kvm_arch' has no member named 'config_lock'
    1111 |         mutex_unlock(&arch->config_lock);
         |                           ^~
   arch/arm64/include/asm/kvm_host.h: In function 'idreg_write':
   arch/arm64/include/asm/kvm_host.h:1118:25: error: 'struct kvm_arch' has no member named 'config_lock'
    1118 |         mutex_lock(&arch->config_lock);
         |                         ^~
   arch/arm64/include/asm/kvm_host.h:1120:27: error: 'struct kvm_arch' has no member named 'config_lock'
    1120 |         mutex_unlock(&arch->config_lock);
         |                           ^~

make[2]: *** [scripts/Makefile.build:114: arch/arm64/kernel/asm-offsets.s] Error 1 make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:1286: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:226: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +1109 arch/arm64/include/asm/kvm_host.h

1104
1105 static inline u64 idreg_read(struct kvm_arch *arch, u32 id)
1106 {
1107 u64 val;
1108

> 1109		mutex_lock(&arch->config_lock);

1110 val = _idreg_read(arch, id);
1111 mutex_unlock(&arch->config_lock);
1112
1113 return val;
1114 }
1115

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#5 lore raw

Re: [PATCH v7 2/6] KVM: arm64: Save ID registers' sanitized value per guest

In reply to Jing Zhang

Hi Jing,

Quoted 23 lines · Jing Zhang

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 47 +++++++++++++++++++++++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 49 +++++++++++++++++++++++++------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  3 +-
 5 files changed, 90 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index bcd774d74f34..2b1fe90a1790 100644

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 181

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -177,6 +177,20 @@ struct kvm_smccc_features {
 	unsigned long vendor_hyp_bmap;
 };
 
+/*
+ * Emualted CPU ID registers per VM

typo: emulated

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 192

Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +11 -0
+ * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+ * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+ *
+ * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+ * Access to id regs are guarded by kvm_arch.config_lock.
+ */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+struct kvm_idregs {
+	u64 regs[KVM_ARM_ID_REG_NUM];
+};

What is the purpose of declaring the register array as a separate structure? It has no meaning (nor use) outside of the context of a VM.

I'd prefer the 'regs' array be embedded directly in kvm_arch, and just name it 'idregs'. You can move your macro definitions there as well to immediately precede the field.

Quoted 23 lines
arch/arm64/include/asm/kvm_host.h +5 -0
 typedef unsigned int pkvm_handle_t;
 
 struct kvm_protected_vm {
@@ -243,6 +257,9 @@ struct kvm_arch {
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
+	/* Emulated CPU ID registers */
+	struct kvm_idregs idregs;
+
 	/*
 	 * For an untrusted host VM, 'pkvm.handle' is used to lookup
 	 * the associated pKVM instance in the hypervisor.
@@ -1008,6 +1025,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_init_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);

arch/arm64/include/asm/kvm_host.h static inline void kvm_hyp_reserve(void) { } · line 1095

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1073,4 +1092,32 @@ static inline void kvm_hyp_reserve(void) { }
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
+static inline u64 _idreg_read(struct kvm_arch *arch, u32 id)

<bikeshed>

Personally, I find passing 'kvm_arch' around to be a bit clunky. Almost all functions in KVM take 'struct kvm' as an argument, even if it only accesses the data in 'kvm_arch'.

So, I'd prefer if all these helpers took 'struct kvm *'.

</bikeshed>

Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6 -0
+{
+	return arch->idregs.regs[IDREG_IDX(id)];
+}
+
+static inline void _idreg_write(struct kvm_arch *arch, u32 id, u64 val)
+{

arch/arm64/include/asm/kvm_host.h static inline void kvm_hyp_reserve(void) { } · line 1111

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+	arch->idregs.regs[IDREG_IDX(id)] = val;
+}
+
+static inline u64 idreg_read(struct kvm_arch *arch, u32 id)
+{
+	u64 val;
+
+	mutex_lock(&arch->config_lock);
+	val = _idreg_read(arch, id);
+	mutex_unlock(&arch->config_lock);

What exactly are we protecting against by taking the config_lock here?

While I do believe there is value in serializing writers to the shared data, there isn't a need to serialize reads from the guest.

What about implementing the following:

- Acquire the config_lock for handling writes. Only allow the value to change if !kvm_vm_has_ran_once(). Otherwise, permit identical writes (useful for hotplug, I imagine) or return EBUSY if userspace tried to change something after running the VM.

- Acquire the config_lock for handling reads *from userspace*

- Handle reads from the guest with a plain old load, avoiding the need to acquire any locks.

This has the benefit of avoiding lock contention for guest reads w/o requiring the use of atomic loads/stores (i.e. {READ,WRITE}_ONCE()) to protect said readers.

Quoted 65 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+	return val;
+}
+
+static inline void idreg_write(struct kvm_arch *arch, u32 id, u64 val)
+{
+	mutex_lock(&arch->config_lock);
+	_idreg_write(arch, id, val);
+	mutex_unlock(&arch->config_lock);
+}
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/arm.c +1 -0
index 4b2e16e696a8..e34744c36406 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	set_default_spectre(kvm);
 	kvm_arm_init_hypercalls(kvm);
+	kvm_arm_init_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +10 -9
index 96b4c43a5100..d2fba2fde01c 100644
@@ -52,16 +52,9 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
-/* Read a sanitised cpufeature ID register by sys_reg_desc */
-static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 {
-	u32 id = reg_to_encoding(r);
-	u64 val;
-
-	if (sysreg_visible_as_raz(vcpu, r))
-		return 0;
-
-	val = read_sanitised_ftr_reg(id);
+	u64 val = idreg_read(&vcpu->kvm->arch, id);
 
 	switch (id) {
 	case SYS_ID_AA64PFR0_EL1:
@@ -126,6 +119,14 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r
 	return val;
 }
 
+static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+{
+	if (sysreg_visible_as_raz(vcpu, r))
+		return 0;
+
+	return kvm_arm_read_id_reg(vcpu, reg_to_encoding(r));
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool access_id_reg(struct kvm_vcpu *vcpu,
@@ -458,3 +459,33 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 
 	return 1;
 }

arch/arm64/kvm/id_regs.c int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params) · line 471

Quoted 10 lines
arch/arm64/kvm/id_regs.c +10 -0
+
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_init_id_regs(struct kvm *kvm)
+{
+	int i;
+	u32 id;
+	u64 val;

nit: use reverse christmas/fir tree ordering for locals.

arch/arm64/kvm/id_regs.c int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params) · line 477

Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		id = reg_to_encoding(&id_reg_descs[i]);
+		if (WARN_ON_ONCE(!is_id_reg(id)))
+			/* Shouldn't happen */
+			continue;

I'll make the suggestion once more.

Please do not implement these sort of sanity checks on static data structures at the point userspace has gotten involved. Sanity checking on id_reg_descs[] should happen at the time KVM is initialized. If anything is wrong at that point we should return an error and outright refuse to run KVM.

Signature · 2 lines
Thanks,
Oliver
author
#6 lore raw

Re: [PATCH v7 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Hi Oliver,

Quoted 30 lines · Oliver Upton

Hi Jing,

Quoted 26 lines · Jing Zhang

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 47 +++++++++++++++++++++++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 49 +++++++++++++++++++++++++------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  3 +-
 5 files changed, 90 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index bcd774d74f34..2b1fe90a1790 100644
@@ -177,6 +177,20 @@ struct kvm_smccc_features {
      unsigned long vendor_hyp_bmap;
 };
 
Quoted 4 lines

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 181

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+/*
+ * Emualted CPU ID registers per VM

typo: emulated

Will fix it.

Quoted 16 lines

arch/arm64/include/asm/kvm_host.h struct kvm_smccc_features { · line 192

Quoted 12 lines
arch/arm64/include/asm/kvm_host.h +11 -0
+ * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+ * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+ *
+ * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+ * Access to id regs are guarded by kvm_arch.config_lock.
+ */
+#define KVM_ARM_ID_REG_NUM   56
+#define IDREG_IDX(id)                (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+struct kvm_idregs {
+     u64 regs[KVM_ARM_ID_REG_NUM];
+};

What is the purpose of declaring the register array as a separate structure? It has no meaning (nor use) outside of the context of a VM.

Quoted 4 lines

I'd prefer the 'regs' array be embedded directly in kvm_arch, and just name it 'idregs'. You can move your macro definitions there as well to immediately precede the field.

It was declared as you suggested in the older series. The separate structure was suggested by Marc.

Quoted 30 lines
Quoted 23 lines
arch/arm64/include/asm/kvm_host.h +5 -0
 typedef unsigned int pkvm_handle_t;
 
 struct kvm_protected_vm {
@@ -243,6 +257,9 @@ struct kvm_arch {
      /* Hypercall features firmware registers' descriptor */
      struct kvm_smccc_features smccc_feat;
 
+     /* Emulated CPU ID registers */
+     struct kvm_idregs idregs;
+
      /*
       * For an untrusted host VM, 'pkvm.handle' is used to lookup
       * the associated pKVM instance in the hypervisor.
@@ -1008,6 +1025,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
                              struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_init_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);

arch/arm64/include/asm/kvm_host.h static inline void kvm_hyp_reserve(void) { } · line 1095

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1073,4 +1092,32 @@ static inline void kvm_hyp_reserve(void) { }
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
+static inline u64 _idreg_read(struct kvm_arch *arch, u32 id)

<bikeshed>

Quoted 8 lines

Personally, I find passing 'kvm_arch' around to be a bit clunky. Almost all functions in KVM take 'struct kvm' as an argument, even if it only accesses the data in 'kvm_arch'.

So, I'd prefer if all these helpers took 'struct kvm *'.

</bikeshed>

There is a reason why 'struct kvm' was not used.
Usually arch dependent headers don't include <linux/kvm_host.h> which declares the 'struct kvm'. But here we need to reference the fields in 'struct kvm', we need to include <linux/kvm_host.h>. But <linux/kvm_host.h> is dependent on arch dependent structures defined in <asm/kvm_host.h>.
To use 'struct kvm', one way is to include <linux/kvm_host.h> in the middle of <asm/kvm_host.h> (after those arch dependent structures but before those idregs read/write inlines). Or we can move these idregs read/write functions to id_regs.c as non-static functions, since they are not only used by id_regs.c.

Quoted 10 lines
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+{
+     return arch->idregs.regs[IDREG_IDX(id)];
+}
+
+static inline void _idreg_write(struct kvm_arch *arch, u32 id, u64 val)
+{
+     arch->idregs.regs[IDREG_IDX(id)] = val;
+}
+
+static inline u64 idreg_read(struct kvm_arch *arch, u32 id)
Quoted 8 lines

arch/arm64/include/asm/kvm_host.h static inline void kvm_hyp_reserve(void) { } · line 1111

Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6 -0
+{
+     u64 val;
+
+     mutex_lock(&arch->config_lock);
+     val = _idreg_read(arch, id);
+     mutex_unlock(&arch->config_lock);

What exactly are we protecting against by taking the config_lock here?

The intention is to use the config_lock to protect the whole id_regs[] array, all idregs, not on the granularity of a single idreg. IIUC, there might be some dependencies between feature fields in different idregs, like the PMUVer and PerfMon. If there is no lock for reads from the guest, the guest may get inconsistent values for PMUVer and PerfMon.
But since no changes are allowed when the VM is running, the reads from the guests don't have to use the lock as you suggested below.

Quoted 10 lines

While I do believe there is value in serializing writers to the shared data, there isn't a need to serialize reads from the guest.

What about implementing the following:

- Acquire the config_lock for handling writes. Only allow the value to change if !kvm_vm_has_ran_once(). Otherwise, permit identical writes (useful for hotplug, I imagine) or return EBUSY if userspace tried to change something after running the VM.

Sure, will add this check for writes during VM running.

Quoted 5 lines

- Acquire the config_lock for handling reads *from userspace*

- Handle reads from the guest with a plain old load, avoiding the need to acquire any locks.

Sure, will use this since as long as VM is running, the userspace should not change the values of idregs.

Quoted 68 lines

This has the benefit of avoiding lock contention for guest reads w/o requiring the use of atomic loads/stores (i.e. {READ,WRITE}_ONCE()) to protect said readers.

Quoted 63 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+     return val;
+}
+
+static inline void idreg_write(struct kvm_arch *arch, u32 id, u64 val)
+{
+     mutex_lock(&arch->config_lock);
+     _idreg_write(arch, id, val);
+     mutex_unlock(&arch->config_lock);
+}
+
 #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/arm.c +1 -0
index 4b2e16e696a8..e34744c36406 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
      set_default_spectre(kvm);
      kvm_arm_init_hypercalls(kvm);
+     kvm_arm_init_id_regs(kvm);
 
      /*
       * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +10 -9
index 96b4c43a5100..d2fba2fde01c 100644
@@ -52,16 +52,9 @@ static u8 pmuver_to_perfmon(u8 pmuver)
      }
 }
 
-/* Read a sanitised cpufeature ID register by sys_reg_desc */
-static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 {
-     u32 id = reg_to_encoding(r);
-     u64 val;
-
-     if (sysreg_visible_as_raz(vcpu, r))
-             return 0;
-
-     val = read_sanitised_ftr_reg(id);
+     u64 val = idreg_read(&vcpu->kvm->arch, id);
 
      switch (id) {
      case SYS_ID_AA64PFR0_EL1:
@@ -126,6 +119,14 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r
      return val;
 }
 
+static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+{
+     if (sysreg_visible_as_raz(vcpu, r))
+             return 0;
+
+     return kvm_arm_read_id_reg(vcpu, reg_to_encoding(r));
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool access_id_reg(struct kvm_vcpu *vcpu,
@@ -458,3 +459,33 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 
Quoted 15 lines

arch/arm64/kvm/id_regs.c int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params) · line 471

Quoted 12 lines
arch/arm64/kvm/id_regs.c +10 -0
      return 1;
 }
+
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_init_id_regs(struct kvm *kvm)
+{
+     int i;
+     u32 id;
+     u64 val;

nit: use reverse christmas/fir tree ordering for locals.

Will do.

Quoted 6 lines

arch/arm64/kvm/id_regs.c int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params) · line 477

Quoted 5 lines
arch/arm64/kvm/id_regs.c +5 -0
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+             id = reg_to_encoding(&id_reg_descs[i]);
+             if (WARN_ON_ONCE(!is_id_reg(id)))
+                     /* Shouldn't happen */
+                     continue;
Quoted 7 lines

I'll make the suggestion once more.

Please do not implement these sort of sanity checks on static data structures at the point userspace has gotten involved. Sanity checking on id_reg_descs[] should happen at the time KVM is initialized. If anything is wrong at that point we should return an error and outright refuse to run KVM.

Sure, will move the check to kvm_sys_reg_table_init.

Quoted 4 lines

--
Thanks,
Oliver

Thanks,
Jing

author
#7 lore raw

PATCH v7 3/6 KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

In reply to Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

The setting of CSV bits for protected VMs are removed according to the discussion from Fuad below:
https://lore.kernel.org/all/CA+EHjTwXA9TprX4jeG+-D+c8v9XG+oFdU1o6TSkvVye145_OvA@mail.gmail.com

Besides the removal of CSV bits setting for protected VMs, No other functional change intended.

---
 arch/arm64/include/asm/kvm_host.h |  2 --
 arch/arm64/kvm/arm.c              | 17 ---------------
 arch/arm64/kvm/id_regs.c          | 35 ++++++++++++++++++++++++-------
 3 files changed, 27 insertions(+), 27 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
index 2b1fe90a1790..0c719c34f5b4 100644
@@ -247,8 +247,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	u8 pfr0_csv2;
-	u8 pfr0_csv3;
 	struct {
 		u8 imp:4;
 		u8 unimp:4;
arch/arm64/kvm/arm.c +0 -17
index e34744c36406..0f71b10a2f05 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-	/*
-	 * The default is to expose CSV2 == 1 if the HW isn't affected.
-	 * Although this is a per-CPU feature, we make it global because
-	 * asymmetric systems are just a nuisance.
-	 *
-	 * Userspace can override this as long as it doesn't promise
-	 * the impossible.
-	 */
-	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv2 = 1;
-	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct
@@ -151,7 +135,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	set_default_spectre(kvm);
 	kvm_arm_init_hypercalls(kvm);
 	kvm_arm_init_id_regs(kvm);
 
arch/arm64/kvm/id_regs.c +27 -8
index d2fba2fde01c..18c39af3e319 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 		if (!vcpu_has_sve(vcpu))
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
 		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-				  (u64)vcpu->kvm->arch.pfr0_csv2);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-				  (u64)vcpu->kvm->arch.pfr0_csv3);
 		if (kvm_vgic_global_state.type == VGIC_V3) {
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
 			val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       u64 val)
 {
 	u8 csv2, csv3;
+	u64 sval = val;
 
 	/*
 	 * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -223,8 +218,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 	if (val)
 		return -EINVAL;
 
-	vcpu->kvm->arch.pfr0_csv2 = csv2;
-	vcpu->kvm->arch.pfr0_csv3 = csv3;
+	idreg_write(&vcpu->kvm->arch, reg_to_encoding(rd), sval);
 
 	return 0;
 }
@@ -488,4 +482,29 @@ void kvm_arm_init_id_regs(struct kvm *kvm)
 		val = read_sanitised_ftr_reg(id);
 		idreg_write(&kvm->arch, id, val);
 	}
+
+	/*
+	 * The default is to expose CSV2 == 1 if the HW isn't affected.
+	 * Although this is a per-CPU feature, we make it global because
+	 * asymmetric systems are just a nuisance.
+	 *
+	 * Userspace can override this as long as it doesn't promise
+	 * the impossible.
+	 */
+	mutex_lock(&kvm->arch.config_lock);
+
+	val = _idreg_read(&kvm->arch, SYS_ID_AA64PFR0_EL1);
+
+	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+	}
+	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+	}
+
+	_idreg_write(&kvm->arch, SYS_ID_AA64PFR0_EL1, val);
+
+	mutex_unlock(&kvm->arch.config_lock);
 }
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
author
#8 lore raw

PATCH v7 4/6 KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

In reply to Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 +++---
 arch/arm64/kvm/arm.c              |  6 ---
 arch/arm64/kvm/id_regs.c          | 66 +++++++++++++++++++++++++------
 include/kvm/arm_pmu.h             |  5 ++-
 4 files changed, 63 insertions(+), 25 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index 0c719c34f5b4..3b583b055d07 100644
@@ -235,6 +235,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT				4
 	/* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED		5
+	/*
+	 * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+	 * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+	 * userspace for VCPUs without PMU.
+	 */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU		6
 
 	unsigned long flags;
 
@@ -247,11 +253,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	struct {
-		u8 imp:4;
-		u8 unimp:4;
-	} dfr0_pmuver;
-
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index 0f71b10a2f05..9ecd0c5d0754 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm_arm_init_hypercalls(kvm);
 	kvm_arm_init_id_regs(kvm);
 
-	/*
-	 * Initialise the default PMUver before there is a chance to
-	 * create an actual PMU.
-	 */
-	kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
 	return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +54 -12

102 lines not on this page · raw message

include/kvm/arm_pmu.h +3 -2
index 628775334d5e..e850432f8c09 100644
@@ -92,8 +92,9 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
-#define kvm_pmu_is_3p5(vcpu)						\
-	(vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)									\
+	 (FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),					\
+		 idreg_read(&vcpu->kvm->arch, SYS_ID_AA64DFR0_EL1)) >= ID_AA64DFR0_EL1_PMUVer_V3P5)
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
author
#9 lore raw

PATCH v7 5/6 KVM: arm64: Reuse fields of sys_reg_desc for idreg

In reply to Jing Zhang

Since reset() and val are not used for idreg in sys_reg_desc, they would be used with other purposes for idregs.
The callback reset() would be used to return KVM sanitised id register values. The u64 val would be used as mask for writable fields in idregs. Only bits with 1 in val are writable from userspace.

---
 arch/arm64/kvm/id_regs.c  | 44 +++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c | 59 +++++++++++++++++++++++++++------------
 arch/arm64/kvm/sys_regs.h | 10 ++++---
 3 files changed, 77 insertions(+), 36 deletions(-)
arch/arm64/kvm/id_regs.c +30 -14
index 15f79a654be8..a5fabbb04ceb 100644
@@ -55,6 +55,11 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
+static u64 general_read_kvm_sanitised_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd)
+{
+	return read_sanitised_ftr_reg(reg_to_encoding(rd));
+}
+
 u64 kvm_arm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
 {
 	u64 val = idreg_read(&vcpu->kvm->arch, id);
@@ -333,6 +338,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
+/*
+ * Since reset() callback and field val are not used for idregs, they will be
+ * used for specific purposes for idregs.
+ * The reset() would return KVM sanitised register value. The value would be the
+ * same as the host kernel sanitised value if there is no KVM sanitisation.
+ * The val would be used as a mask indicating writable fields for the idreg.
+ * Only bits with 1 are writable from userspace. This mask might not be
+ * necessary in the future whenever all ID registers are enabled as writable
+ * from userspace.
+ */
+
 /* sys_reg_desc initialiser for known cpufeature ID registers */
 #define ID_SANITISED(name) {			\
 	SYS_DESC(SYS_##name),			\
@@ -340,6 +356,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.get_user = get_id_reg,			\
 	.set_user = set_id_reg,			\
 	.visibility = id_visibility,		\
+	.reset = general_read_kvm_sanitised_reg,\
+	.val = 0,				\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
@@ -349,6 +367,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.get_user = get_id_reg,			\
 	.set_user = set_id_reg,			\
 	.visibility = aa32_id_visibility,	\
+	.reset = general_read_kvm_sanitised_reg,\
+	.val = 0,				\
 }
 
 /*
@@ -361,7 +381,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.access = access_id_reg,			\
 	.get_user = get_id_reg,				\
 	.set_user = set_id_reg,				\
-	.visibility = raz_visibility			\
+	.visibility = raz_visibility,			\
+	.reset = NULL,					\
+	.val = 0,					\
 }
 
 /*
@@ -375,6 +397,8 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	.get_user = get_id_reg,			\
 	.set_user = set_id_reg,			\
 	.visibility = raz_visibility,		\
+	.reset = NULL,				\
+	.val = 0,				\
 }
 
 const struct sys_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
@@ -485,10 +509,7 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 	return 1;
 }
 
-/*
- * Set the guest's ID registers that are defined in id_reg_descs[]
- * with ID_SANITISED() to the host's sanitized value.
- */
+/* Initialize the guest's ID registers with KVM sanitised values. */
 void kvm_arm_init_id_regs(struct kvm *kvm)
 {
 	int i;
@@ -501,16 +522,11 @@ void kvm_arm_init_id_regs(struct kvm *kvm)
 			/* Shouldn't happen */
 			continue;
 
-		/*
-		 * Some hidden ID registers which are not in arm64_ftr_regs[]
-		 * would cause warnings from read_sanitised_ftr_reg().
-		 * Skip those ID registers to avoid the warnings.
-		 */
-		if (id_reg_descs[i].visibility == raz_visibility)
-			/* Hidden or reserved ID register */
-			continue;
+		val = 0;
+		/* Read KVM sanitised register value if available */
+		if (id_reg_descs[i].reset)
+			val = id_reg_descs[i].reset(NULL, &id_reg_descs[i]);
 
-		val = read_sanitised_ftr_reg(id);
 		idreg_write(&kvm->arch, id, val);
 	}
 
arch/arm64/kvm/sys_regs.c +41 -18

187 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +6 -4
index e88fd77309b2..21869319f6e1 100644
@@ -65,12 +65,12 @@ struct sys_reg_desc {
 		       const struct sys_reg_desc *);
 
 	/* Initialization for vcpu. */
-	void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
+	u64 (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
 
 	/* Index into sys_reg[], or 0 if we don't need to save it. */
 	int reg;
 
-	/* Value (usually reset value) */
+	/* Value (usually reset value), or write mask for idregs */
 	u64 val;
 
 	/* Custom get/set_user functions, fallback to generic if NULL */
@@ -123,19 +123,21 @@ static inline bool read_zero(struct kvm_vcpu *vcpu,
 }
 
 /* Reset functions */
-static inline void reset_unknown(struct kvm_vcpu *vcpu,
+static inline u64 reset_unknown(struct kvm_vcpu *vcpu,
 				 const struct sys_reg_desc *r)
 {
 	BUG_ON(!r->reg);
 	BUG_ON(r->reg >= NR_SYS_REGS);
 	__vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
+	return __vcpu_sys_reg(vcpu, r->reg);
 }
 
-static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
+static inline u64 reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	BUG_ON(!r->reg);
 	BUG_ON(r->reg >= NR_SYS_REGS);
 	__vcpu_sys_reg(vcpu, r->reg) = r->val;
+	return __vcpu_sys_reg(vcpu, r->reg);
 }
 
 static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
author
#10 lore raw

PATCH v7 6/6 KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

In reply to Jing Zhang

Refactor writings for ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon based on utilities introduced by ID register descriptor array.

---
 arch/arm64/include/asm/cpufeature.h |   1 +
 arch/arm64/kernel/cpufeature.c      |   2 +-
 arch/arm64/kvm/id_regs.c            | 318 +++++++++++++++++++---------
 3 files changed, 223 insertions(+), 98 deletions(-)
arch/arm64/include/asm/cpufeature.h +1 -0
index 6bf013fb110d..dc769c2eb7a4 100644
@@ -915,6 +915,7 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
 	return 8;
 }
 
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
 
 extern struct arm64_ftr_override id_aa64mmfr1_override;
arch/arm64/kernel/cpufeature.c +1 -1
index 2e3e55139777..677ec4fe9f6b 100644
@@ -791,7 +791,7 @@ static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
 	return reg;
 }
 
-static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 				s64 cur)
 {
 	s64 ret = 0;
arch/arm64/kvm/id_regs.c +221 -97

427 lines not on this page · raw message

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog