Skip to contentSkip to conversation

arm/kvm: use kvm_{get,set}_one_reg

PATCH Applied Cornelia Huck 4 participants 10 messages Oct 10, 2023 – Oct 17, 2023 KVM

lore
Share & tools
lore
#0 lore raw

v2 cover arm/kvm: use kvm_{get,set}_one_reg

I sent this cleanup first... in mid-July (ugh). But better late than never, I guess.

From v1:
- fix buglets (thanks Gavin)
- add patch 3 on top

The kvm_{get,set}_one_reg functions have been around for a very long time, and using them instead of open-coding the ioctl invocations saves lines of code, and gives us a tracepoint as well. They cannot be used by invocations of the ioctl not acting on a CPUState, but that still leaves a lot of conversions in the target/arm code.

target/mips and target/ppc also have some potential for conversions, but as I cannot test either (and they are both in 'Odd fixes' anyway), I left them alone.

Survives some testing on a Mt. Snow.

Cornelia Huck (3):
arm/kvm: convert to kvm_set_one_reg
arm/kvm: convert to kvm_get_one_reg
arm/kvm: convert to read_sys_reg64

 target/arm/kvm.c   |  28 +++-------
 target/arm/kvm64.c | 129 ++++++++++++---------------------------------
 2 files changed, 40 insertions(+), 117 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/3 arm/kvm: convert to kvm_set_one_reg

We can neaten the code by switching to the kvm_set_one_reg function.

---
 target/arm/kvm.c   | 13 +++------
 target/arm/kvm64.c | 66 +++++++++++++---------------------------------
 2 files changed, 21 insertions(+), 58 deletions(-)
target/arm/kvm.c +3 -10
index b66b936a9586..1a8084c4601c 100644
@@ -589,7 +589,6 @@ bool write_list_to_kvmstate(ARMCPU *cpu, int level)
     bool ok = true;
 
     for (i = 0; i < cpu->cpreg_array_len; i++) {
-        struct kvm_one_reg r;
         uint64_t regidx = cpu->cpreg_indexes[i];
         uint32_t v32;
         int ret;
@@ -598,19 +597,17 @@ bool write_list_to_kvmstate(ARMCPU *cpu, int level)
             continue;
         }
 
-        r.id = regidx;
         switch (regidx & KVM_REG_SIZE_MASK) {
         case KVM_REG_SIZE_U32:
             v32 = cpu->cpreg_values[i];
-            r.addr = (uintptr_t)&v32;
+            ret = kvm_set_one_reg(cs, regidx, &v32);
             break;
         case KVM_REG_SIZE_U64:
-            r.addr = (uintptr_t)(cpu->cpreg_values + i);
+            ret = kvm_set_one_reg(cs, regidx, cpu->cpreg_values + i);
             break;
         default:
             g_assert_not_reached();
         }
-        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
         if (ret) {
             /* We might fail for "unknown register" and also for
              * "you tried to set a register which is constant with
@@ -731,17 +728,13 @@ void kvm_arm_get_virtual_time(CPUState *cs)
 void kvm_arm_put_virtual_time(CPUState *cs)
 {
     ARMCPU *cpu = ARM_CPU(cs);
-    struct kvm_one_reg reg = {
-        .id = KVM_REG_ARM_TIMER_CNT,
-        .addr = (uintptr_t)&cpu->kvm_vtime,
-    };
     int ret;
 
     if (!cpu->kvm_vtime_dirty) {
         return;
     }
 
-    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
+    ret = kvm_set_one_reg(cs, KVM_REG_ARM_TIMER_CNT, &cpu->kvm_vtime);
     if (ret) {
         error_report("Failed to set KVM_REG_ARM_TIMER_CNT");
         abort();
target/arm/kvm64.c +18 -48

165 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] arm/kvm: convert to kvm_set_one_reg

Quoted 8 lines · Cornelia Huck

We can neaten the code by switching to the kvm_set_one_reg function.

---
  target/arm/kvm.c   | 13 +++------
  target/arm/kvm64.c | 66 +++++++++++++---------------------------------
  2 files changed, 21 insertions(+), 58 deletions(-)
#3 lore raw

PATCH v2 2/3 arm/kvm: convert to kvm_get_one_reg

In reply to Cornelia Huck

We can neaten the code by switching the callers that work on a CPUstate to the kvm_get_one_reg function.

---
 target/arm/kvm.c   | 15 +++---------
 target/arm/kvm64.c | 57 ++++++++++++----------------------------------
 2 files changed, 18 insertions(+), 54 deletions(-)
target/arm/kvm.c +3 -12
index 1a8084c4601c..7903e2ddde1b 100644
@@ -553,24 +553,19 @@ bool write_kvmstate_to_list(ARMCPU *cpu)
     bool ok = true;
 
     for (i = 0; i < cpu->cpreg_array_len; i++) {
-        struct kvm_one_reg r;
         uint64_t regidx = cpu->cpreg_indexes[i];
         uint32_t v32;
         int ret;
 
-        r.id = regidx;
-
         switch (regidx & KVM_REG_SIZE_MASK) {
         case KVM_REG_SIZE_U32:
-            r.addr = (uintptr_t)&v32;
-            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
+            ret = kvm_get_one_reg(cs, regidx, &v32);
             if (!ret) {
                 cpu->cpreg_values[i] = v32;
             }
             break;
         case KVM_REG_SIZE_U64:
-            r.addr = (uintptr_t)(cpu->cpreg_values + i);
-            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
+            ret = kvm_get_one_reg(cs, regidx, cpu->cpreg_values + i);
             break;
         default:
             g_assert_not_reached();
@@ -706,17 +701,13 @@ int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu)
 void kvm_arm_get_virtual_time(CPUState *cs)
 {
     ARMCPU *cpu = ARM_CPU(cs);
-    struct kvm_one_reg reg = {
-        .id = KVM_REG_ARM_TIMER_CNT,
-        .addr = (uintptr_t)&cpu->kvm_vtime,
-    };
     int ret;
 
     if (cpu->kvm_vtime_dirty) {
         return;
     }
 
-    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
+    ret = kvm_get_one_reg(cs, KVM_REG_ARM_TIMER_CNT, &cpu->kvm_vtime);
     if (ret) {
         error_report("Failed to get KVM_REG_ARM_TIMER_CNT");
         abort();
target/arm/kvm64.c +15 -42

144 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] arm/kvm: convert to kvm_get_one_reg

Quoted 9 lines · Cornelia Huck

We can neaten the code by switching the callers that work on a CPUstate to the kvm_get_one_reg function.

---
  target/arm/kvm.c   | 15 +++---------
  target/arm/kvm64.c | 57 ++++++++++++----------------------------------
  2 files changed, 18 insertions(+), 54 deletions(-)
#5 lore raw

PATCH v2 3/3 arm/kvm: convert to read_sys_reg64

In reply to Cornelia Huck

We can use read_sys_reg64 to get the SVE_VLS register instead of calling GET_ONE_REG directly.

---
 target/arm/kvm64.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
target/arm/kvm64.c +1 -5
index 558c0b88dd69..d40c89a84752 100644
@@ -500,10 +500,6 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
             .target = -1,
             .features[0] = (1 << KVM_ARM_VCPU_SVE),
         };
-        struct kvm_one_reg reg = {
-            .id = KVM_REG_ARM64_SVE_VLS,
-            .addr = (uint64_t)&vls[0],
-        };
         int fdarray[3], ret;
 
         probed = true;
@@ -512,7 +508,7 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
             error_report("failed to create scratch VCPU with SVE enabled");
             abort();
         }
-        ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
+        ret = read_sys_reg64(fdarray[2], &vls[0], KVM_REG_ARM64_SVE_VLS);
         kvm_arm_destroy_scratch_host_vcpu(fdarray);
         if (ret) {
             error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
Signature · 1 line
2.41.0
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] arm/kvm: convert to read_sys_reg64

Quoted 9 lines · Cornelia Huck

We can use read_sys_reg64 to get the SVE_VLS register instead of calling GET_ONE_REG directly.

---
  target/arm/kvm64.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
Quoted 24 lines
target/arm/kvm64.c +1 -5
index 558c0b88dd69..d40c89a84752 100644
@@ -500,10 +500,6 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
              .target = -1,
              .features[0] = (1 << KVM_ARM_VCPU_SVE),
          };
-        struct kvm_one_reg reg = {
-            .id = KVM_REG_ARM64_SVE_VLS,
-            .addr = (uint64_t)&vls[0],
-        };
          int fdarray[3], ret;
 
          probed = true;
@@ -512,7 +508,7 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
              error_report("failed to create scratch VCPU with SVE enabled");
              abort();
          }
-        ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
+        ret = read_sys_reg64(fdarray[2], &vls[0], KVM_REG_ARM64_SVE_VLS);
          kvm_arm_destroy_scratch_host_vcpu(fdarray);
          if (ret) {
              error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
#7 lore raw

Re: [PATCH v2 3/3] arm/kvm: convert to read_sys_reg64

In reply to Cornelia Huck
Quoted 26 lines · Cornelia Huck

We can use read_sys_reg64 to get the SVE_VLS register instead of calling GET_ONE_REG directly.

---
 target/arm/kvm64.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
target/arm/kvm64.c +0 -4
index 558c0b88dd69..d40c89a84752 100644
@@ -500,10 +500,6 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
             .target = -1,
             .features[0] = (1 << KVM_ARM_VCPU_SVE),
         };
-        struct kvm_one_reg reg = {
-            .id = KVM_REG_ARM64_SVE_VLS,
-            .addr = (uint64_t)&vls[0],
-        };
         int fdarray[3], ret;
 
         probed = true;

target/arm/kvm64.c uint32_t kvm_arm_sve_get_vls(CPUState *cs) · line 514

Quoted 8 lines
target/arm/kvm64.c +1 -1
@@ -512,7 +508,7 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
             error_report("failed to create scratch VCPU with SVE enabled");
             abort();
         }
-        ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
+        ret = read_sys_reg64(fdarray[2], &vls[0], KVM_REG_ARM64_SVE_VLS);
         kvm_arm_destroy_scratch_host_vcpu(fdarray);
         if (ret) {
             error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",

read_sys_reg64() asserts that the register you're trying to read is 64 bits, but KVM_REG_ARM64_SVE_VLS is not, it's 512 bits:

#define KVM_REG_ARM64_SVE_VLS           (KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \
                                         KVM_REG_SIZE_U512 | 0xffff)

So this change would trip the assert on a host where SVE is supported and enabled.

thanks
-- PMM

#8 lore raw

Re: [PATCH v2 3/3] arm/kvm: convert to read_sys_reg64

Quoted 39 lines · Peter Maydell
Quoted 26 lines · Cornelia Huck

We can use read_sys_reg64 to get the SVE_VLS register instead of calling GET_ONE_REG directly.

---
 target/arm/kvm64.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
target/arm/kvm64.c +0 -4
index 558c0b88dd69..d40c89a84752 100644
@@ -500,10 +500,6 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
             .target = -1,
             .features[0] = (1 << KVM_ARM_VCPU_SVE),
         };
-        struct kvm_one_reg reg = {
-            .id = KVM_REG_ARM64_SVE_VLS,
-            .addr = (uint64_t)&vls[0],
-        };
         int fdarray[3], ret;
 
         probed = true;

target/arm/kvm64.c uint32_t kvm_arm_sve_get_vls(CPUState *cs) · line 514

Quoted 8 lines
target/arm/kvm64.c +1 -1
@@ -512,7 +508,7 @@ uint32_t kvm_arm_sve_get_vls(CPUState *cs)
             error_report("failed to create scratch VCPU with SVE enabled");
             abort();
         }
-        ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &reg);
+        ret = read_sys_reg64(fdarray[2], &vls[0], KVM_REG_ARM64_SVE_VLS);
         kvm_arm_destroy_scratch_host_vcpu(fdarray);
         if (ret) {
             error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",

read_sys_reg64() asserts that the register you're trying to read is 64 bits, but KVM_REG_ARM64_SVE_VLS is not, it's 512 bits:

Quoted 5 lines
#define KVM_REG_ARM64_SVE_VLS           (KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \
                                         KVM_REG_SIZE_U512 | 0xffff)

So this change would trip the assert on a host where SVE is supported and enabled.

Whoops, it seems that I misread this. (And my test environment didn't have that enabled...)

#9 lore raw
In reply to Cornelia Huck
Quoted 7 lines · Cornelia Huck

I sent this cleanup first... in mid-July (ugh). But better late than never, I guess.

From v1:
- fix buglets (thanks Gavin)
- add patch 3 on top

Quoted 5 lines

The kvm_{get,set}_one_reg functions have been around for a very long time, and using them instead of open-coding the ioctl invocations saves lines of code, and gives us a tracepoint as well. They cannot be used by invocations of the ioctl not acting on a CPUState, but that still leaves a lot of conversions in the target/arm code.

I've applied patches 1 and 2 to target-arm.next.

thanks
-- PMM