KVM: arm64: Factor out core register ID enumeration
[linux-2.6-microblaze.git] / arch / arm64 / kvm / guest.c
index dd436a5..a391a61 100644 (file)
@@ -23,6 +23,8 @@
 #include <linux/err.h>
 #include <linux/kvm_host.h>
 #include <linux/module.h>
+#include <linux/stddef.h>
+#include <linux/string.h>
 #include <linux/vmalloc.h>
 #include <linux/fs.h>
 #include <kvm/arm_psci.h>
@@ -193,9 +195,28 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
        return -EINVAL;
 }
 
+static int kvm_arm_copy_core_reg_indices(u64 __user *uindices)
+{
+       unsigned int i;
+       int n = 0;
+       const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
+
+       for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
+               if (uindices) {
+                       if (put_user(core_reg | i, uindices))
+                               return -EFAULT;
+                       uindices++;
+               }
+
+               n++;
+       }
+
+       return n;
+}
+
 static unsigned long num_core_regs(void)
 {
-       return sizeof(struct kvm_regs) / sizeof(__u32);
+       return kvm_arm_copy_core_reg_indices(NULL);
 }
 
 /**
@@ -258,8 +279,14 @@ static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  */
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
 {
-       return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
-               + kvm_arm_get_fw_num_regs(vcpu) + NUM_TIMER_REGS;
+       unsigned long res = 0;
+
+       res += num_core_regs();
+       res += kvm_arm_num_sys_reg_descs(vcpu);
+       res += kvm_arm_get_fw_num_regs(vcpu);
+       res += NUM_TIMER_REGS;
+
+       return res;
 }
 
 /**
@@ -269,15 +296,12 @@ unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
  */
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 {
-       unsigned int i;
-       const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
        int ret;
 
-       for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
-               if (put_user(core_reg | i, uindices))
-                       return -EFAULT;
-               uindices++;
-       }
+       ret = kvm_arm_copy_core_reg_indices(uindices);
+       if (ret)
+               return ret;
+       uindices += ret;
 
        ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
        if (ret)