GET/SET_ONE_REG and HIOR patches v3
Subsystems (4)
cover GET/SET_ONE_REG and HIOR patches v3
This is a revised version of the ONE_REG interface. The main difference to v1 is that we now encode the register size in the constant, making it very unambiguous what size it is. That way we can just take a pointer from user space to write it to.
Thanks a lot to Scott for reviewing the previous patches and pointing out some design issues before it actually lands upstream.
v1 -> v2:
- do paranthesis
- add patch to rename internal old KVM_REG constants
- fix comments
Alex
Alexander Graf (3):
KVM: PPC: Add generic single register ioctls
KVM: PPC: Add support for explicit HIOR setting
KVM: PPC: Rename MMIO register identifiers
Paul Mackerras (1):
KVM: PPC: Move kvm_vcpu_ioctl_[gs]et_one_reg down to
platform-specific code
Documentation/virtual/kvm/api.txt | 41 ++++++++++++++++++++++++++++++ arch/powerpc/include/asm/kvm.h | 14 ++++++---- arch/powerpc/include/asm/kvm_book3s.h | 2 + arch/powerpc/include/asm/kvm_ppc.h | 3 ++ arch/powerpc/kvm/book3s_hv.c | 36 ++++++++++++++++++++++++++ arch/powerpc/kvm/book3s_paired_singles.c | 9 ++++-- arch/powerpc/kvm/book3s_pr.c | 38 ++++++++++++++++++++++++++- arch/powerpc/kvm/booke.c | 10 +++++++ arch/powerpc/kvm/powerpc.c | 34 ++++++++++++++++++------ include/linux/kvm.h | 36 ++++++++++++++++++++++++++ 10 files changed, 203 insertions(+), 20 deletions(-)
PATCH 1/4 KVM: PPC: Add generic single register ioctls
Right now we transfer a static struct every time we want to get or set registers. Unfortunately, over time we realize that there are more of these than we thought of before and the extensibility and flexibility of transferring a full struct every time is limited.
So this is a new approach to the problem. With these new ioctls, we can get and set a single register that is identified by an ID. This allows for very precise and limited transmittal of data. When we later realize that it's a better idea to shove over multiple registers at once, we can reuse most of the infrastructure and simply implement a GET_MANY_REGS / SET_MANY_REGS interface.
---
v1 -> v2:
- rename KVM_ONE_REG to KVM_REG
- change semantics to include size in constant
and a user pointer to write/read to/from
- update documentation respectively
v2 -> v3:
- make GET_ONE_REG ioctl write-only
--- Documentation/virtual/kvm/api.txt | 40 ++++++++++++++++++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 41 +++++++++++++++++++++++++++++++++++++ include/linux/kvm.h | 35 +++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 0 deletions(-)
Documentation/virtual/kvm/api.txt +40 -0
@@ -1523,6 +1523,46 @@ following algorithm: Some guests configure the LINT1 NMI input to cause a panic, aiding in debugging. +4.65 KVM_SET_ONE_REG + +Capability: KVM_CAP_ONE_REG +Architectures: all +Type: vcpu ioctl +Parameters: struct kvm_one_reg (in) +Returns: 0 on success, negative value on failure + +struct kvm_one_reg { + __u64 id; + __u64 addr; +}; + +Using this ioctl, a single vcpu register can be set to a specific value +defined by user space with the passed in struct kvm_one_reg, where id +refers to the register identifier as described below and addr is a pointer +to a variable with the respective size. There can be architecture agnostic +and architecture specific registers. Each have their own range of operation +and their own constants and width. To keep track of the implemented +registers, find a list below: + + Arch | Register | Width (bits) + | | + +4.66 KVM_GET_ONE_REG + +Capability: KVM_CAP_ONE_REG +Architectures: all +Type: vcpu ioctl +Parameters: struct kvm_one_reg (in and out) +Returns: 0 on success, negative value on failure + +This ioctl allows to receive the value of a single register implemented +in a vcpu. The register to read is indicated by the "id" field of the +kvm_one_reg struct passed in. On success, the register value can be found +at the memory location pointed to by "addr". + +The list of registers accessible using this interface is identical to the +list in 4.64. + 5. The kvm_run structure Application code obtains a pointer to the kvm_run structure by
arch/powerpc/kvm/powerpc.c +41 -0
59 lines not on this page · raw message
include/linux/kvm.h +35 -0
@@ -557,6 +557,7 @@ struct kvm_ppc_pvinfo {#define KVM_CAP_MAX_VCPUS 66/* returns max vcpus per vm */ #define KVM_CAP_PPC_PAPR 68#define KVM_CAP_SW_TLB 69 +#define KVM_CAP_ONE_REG 70#define KVM_CAP_S390_GMAP 71#define KVM_CAP_TSC_DEADLINE_TIMER 72
@@ -653,6 +654,37 @@ struct kvm_dirty_tlb {__u32 num_dirty; }; +/* Available with KVM_CAP_ONE_REG */ + +#define KVM_REG_ARCH_MASK 0xff00000000000000ULL +#define KVM_REG_GENERIC 0x0000000000000000ULL + +/* +* Architecture specific registers are to be defined in arch headers and +* ORed with the arch identifier. +*/ +#define KVM_REG_PPC 0x1000000000000000ULL +#define KVM_REG_X86 0x2000000000000000ULL +#define KVM_REG_IA64 0x3000000000000000ULL +#define KVM_REG_ARM 0x4000000000000000ULL +#define KVM_REG_S390 0x5000000000000000ULL + +#define KVM_REG_SIZE_SHIFT 52 +#define KVM_REG_SIZE_MASK 0x00f0000000000000ULL +#define KVM_REG_SIZE_U8 0x0000000000000000ULL +#define KVM_REG_SIZE_U16 0x0010000000000000ULL +#define KVM_REG_SIZE_U32 0x0020000000000000ULL +#define KVM_REG_SIZE_U64 0x0030000000000000ULL +#define KVM_REG_SIZE_U128 0x0040000000000000ULL +#define KVM_REG_SIZE_U256 0x0050000000000000ULL +#define KVM_REG_SIZE_U512 0x0060000000000000ULL +#define KVM_REG_SIZE_U1024 0x0070000000000000ULL + +struct kvm_one_reg { +__u64 id; +__u64 addr; +}; +/* * ioctls for VM fds */
@@ -781,6 +813,9 @@ struct kvm_dirty_tlb {#define KVM_ALLOCATE_RMA _IOR(KVMIO, 0xa9, struct kvm_allocate_rma)/* Available with KVM_CAP_SW_TLB */ #define KVM_DIRTY_TLB _IOW(KVMIO, 0xaa, struct kvm_dirty_tlb) +/* Available with KVM_CAP_ONE_REG */ +#define KVM_GET_ONE_REG _IOW(KVMIO, 0xab, struct kvm_one_reg) +#define KVM_SET_ONE_REG _IOW(KVMIO, 0xac, struct kvm_one_reg)#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
Signature · 1 line
1.6.0.2
PATCH 2/4 KVM: PPC: Add support for explicit HIOR setting
In reply to Alexander GrafUntil now, we always set HIOR based on the PVR, but this is just wrong. Instead, we should be setting HIOR explicitly, so user space can decide what the initial HIOR value is - just like on real hardware.
We keep the old PVR based way around for backwards compatibility, but once user space uses the SET_ONE_REG based method, we drop the PVR logic.
---
v1 -> v2:
- rename KVM_ONE_REG to KVM_REG
- add size information to HIOR
- change HIOR number to 1, indicating that numbers are consecutive
v2 -> v3:
- paranthesis around macro
--- Documentation/virtual/kvm/api.txt | 1 + arch/powerpc/include/asm/kvm.h | 2 ++ arch/powerpc/include/asm/kvm_book3s.h | 2 ++ arch/powerpc/kvm/book3s_pr.c | 6 ++++-- arch/powerpc/kvm/powerpc.c | 13 +++++++++++++ include/linux/kvm.h | 1 + 6 files changed, 23 insertions(+), 2 deletions(-)
Documentation/virtual/kvm/api.txt +1 -0
@@ -1546,6 +1546,7 @@ registers, find a list below: Arch | Register | Width (bits) | | + PPC | KVM_REG_PPC_HIOR | 64 4.66 KVM_GET_ONE_REG
arch/powerpc/include/asm/kvm.h +2 -0
@@ -327,4 +327,6 @@ struct kvm_book3e_206_tlb_params { __u32 reserved[8]; }; +#define KVM_REG_PPC_HIOR (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x1) +#endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/include/asm/kvm_book3s.h +2 -0
@@ -90,6 +90,8 @@ struct kvmppc_vcpu_book3s {#endif int context_id[SID_CONTEXTS ]; +bool hior_explicit;/* HIOR is set by ioctl, not PVR */ +struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE ];struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG ];struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE ];
arch/powerpc/kvm/book3s_pr.c +4 -2
@@ -157,14 +157,16 @@ void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)#ifdef CONFIG_PPC_BOOK3S_64 if ((pvr >=0x330000 ) && (pvr <0x70330000 )) {kvmppc_mmu_book3s_64_init (vcpu); -to_book3s (vcpu)->hior =0xfff00000 ; +if (!to_book3s (vcpu)->hior_explicit) +to_book3s (vcpu)->hior =0xfff00000 ;to_book3s (vcpu)->msr_mask =0xffffffffffffffffULL ; vcpu->arch.cpu_type =KVM_CPU_3S_64 ; } else#endif {kvmppc_mmu_book3s_32_init (vcpu); -to_book3s (vcpu)->hior =0 ; +if (!to_book3s (vcpu)->hior_explicit) +to_book3s (vcpu)->hior =0 ;to_book3s (vcpu)->msr_mask =0xffffffffULL ; vcpu->arch.cpu_type =KVM_CPU_3S_32 ; }
arch/powerpc/kvm/powerpc.c +13 -0
@@ -209,6 +209,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_PPC_BOOKE_SREGS :#else case KVM_CAP_PPC_SEGSTATE : +case KVM_CAP_PPC_HIOR :case KVM_CAP_PPC_PAPR :#endif case KVM_CAP_PPC_UNSET_IRQ :
@@ -649,6 +650,11 @@ static int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,int r = -EINVAL ;switch (reg->id) { +#ifdef CONFIG_PPC_BOOK3S +case KVM_REG_PPC_HIOR : + r =put_user (to_book3s (vcpu)->hior, (u64 __user *)reg->addr); +break ; +#endif default :break ; }
@@ -662,6 +668,13 @@ static int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,int r = -EINVAL ;switch (reg->id) { +#ifdef CONFIG_PPC_BOOK3S +case KVM_ONE_REG_PPC_HIOR : + r =get_user (to_book3s (vcpu)->hior, (u64 __user *)reg->addr); +if (!r) +to_book3s (vcpu)->hior_explicit = true; +break ; +#endif default :break ; }
include/linux/kvm.h +1 -0
@@ -555,6 +555,7 @@ struct kvm_ppc_pvinfo {#define KVM_CAP_PPC_SMT 64#define KVM_CAP_PPC_RMA 65#define KVM_CAP_MAX_VCPUS 66/* returns max vcpus per vm */ +#define KVM_CAP_PPC_HIOR 67#define KVM_CAP_PPC_PAPR 68#define KVM_CAP_SW_TLB 69#define KVM_CAP_ONE_REG 70
Signature · 1 line
1.6.0.2
PATCH 3/4 KVM: PPC: Move kvm_vcpu_ioctl_[gs]et_one_reg down to platform-specific code
In reply to Alexander GrafFrom: Paul Mackerras <[email protected]>
This moves the get/set_one_reg implementation down from powerpc.c into booke.c, book3s_pr.c and book3s_hv.c. This avoids #ifdefs in C code, but more importantly, it fixes a bug on Book3s HV where we were accessing beyond the end of the kvm_vcpu struct (via the to_book3s() macro) and corrupting memory, causing random crashes and file corruption.
On Book3s HV we only accept setting the HIOR to zero, since the guest runs in supervisor mode and its vectors are never offset from zero.
--- arch/powerpc/include/asm/kvm_ppc.h | 3 ++ arch/powerpc/kvm/book3s_hv.c | 36 ++++++++++++++++++++++++++++++++++ arch/powerpc/kvm/book3s_pr.c | 32 ++++++++++++++++++++++++++++++ arch/powerpc/kvm/booke.c | 10 +++++++++ arch/powerpc/kvm/powerpc.c | 38 ------------------------------------ 5 files changed, 81 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +3 -0
@@ -176,6 +176,9 @@ int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);void kvmppc_get_sregs_ivor (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs);int kvmppc_set_sregs_ivor (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs); +int kvm_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg); +int kvm_vcpu_ioctl_set_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg); +void kvmppc_set_pid (struct kvm_vcpu *vcpu,u32 pid);#ifdef CONFIG_KVM_BOOK3S_64_HV
arch/powerpc/kvm/book3s_hv.c +36 -0
@@ -398,6 +398,42 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,return 0 ; } +int kvm_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) +{ +int r = -EINVAL ; + +switch (reg->id) { +case KVM_REG_PPC_HIOR : + r =put_user (0 , (u64 __user *)reg->addr); +break ; +default : +break ; + } + +return r; +} + +int kvm_vcpu_ioctl_set_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) +{ +int r = -EINVAL ; + +switch (reg->id) { +case KVM_REG_PPC_HIOR : + { +u64 hior; +/* Only allow this to be set to zero */ + r =get_user (hior, (u64 __user *)reg->addr); +if (!r && (hior !=0 )) + r = -EINVAL ; +break ; + } +default : +break ; + } + +return r; +} +int kvmppc_core_check_processor_compat (void ) {if (cpu_has_feature (CPU_FTR_HVMODE ))
arch/powerpc/kvm/book3s_pr.c +32 -0
@@ -874,6 +874,38 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,return 0 ; } +int kvm_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) +{ +int r = -EINVAL ; + +switch (reg->id) { +case KVM_REG_PPC_HIOR : + r =put_user (to_book3s (vcpu)->hior, (u64 __user *)reg->addr); +break ; +default : +break ; + } + +return r; +} + +int kvm_vcpu_ioctl_set_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) +{ +int r = -EINVAL ; + +switch (reg->id) { +case KVM_REG_PPC_HIOR : + r =get_user (to_book3s (vcpu)->hior, (u64 __user *)reg->addr); +if (!r) +to_book3s (vcpu)->hior_explicit = true; +break ; +default : +break ; + } + +return r; +} +int kvmppc_core_check_processor_compat (void ) {return 0 ;
arch/powerpc/kvm/booke.c +10 -0
@@ -887,6 +887,16 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,return kvmppc_core_set_sregs (vcpu, sregs); } +int kvm_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) +{ +return -EINVAL ; +} + +int kvm_vcpu_ioctl_set_one_reg (struct kvm_vcpu *vcpu,struct kvm_one_reg *reg) +{ +return -EINVAL ; +} +int kvm_arch_vcpu_ioctl_get_fpu (struct kvm_vcpu *vcpu,struct kvm_fpu *fpu) {return -ENOTSUPP ;
arch/powerpc/kvm/powerpc.c +0 -38
@@ -644,44 +644,6 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,return r; } -static int kvm_vcpu_ioctl_get_one_reg (struct kvm_vcpu *vcpu, -struct kvm_one_reg *reg) -{ -int r = -EINVAL ; - -switch (reg->id) { -#ifdef CONFIG_PPC_BOOK3S -case KVM_REG_PPC_HIOR : - r =put_user (to_book3s (vcpu)->hior, (u64 __user *)reg->addr); -break ; -#endif -default : -break ; - } - -return r; -} - -static int kvm_vcpu_ioctl_set_one_reg (struct kvm_vcpu *vcpu, -struct kvm_one_reg *reg) -{ -int r = -EINVAL ; - -switch (reg->id) { -#ifdef CONFIG_PPC_BOOK3S -case KVM_ONE_REG_PPC_HIOR : - r =get_user (to_book3s (vcpu)->hior, (u64 __user *)reg->addr); -if (!r) -to_book3s (vcpu)->hior_explicit = true; -break ; -#endif -default : -break ; - } - -return r; -} -int kvm_arch_vcpu_ioctl_get_mpstate (struct kvm_vcpu *vcpu,struct kvm_mp_state *mp_state) {
Signature · 1 line
1.6.0.2
PATCH 4/4 KVM: PPC: Rename MMIO register identifiers
In reply to Alexander GrafWe need the KVM_REG namespace for generic register settings now, so let's rename the existing users to something different, enabling us to reuse the namespace for more visible interfaces.
--- arch/powerpc/include/asm/kvm.h | 12 ++++++------ arch/powerpc/kvm/book3s_paired_singles.c | 9 ++++++--- arch/powerpc/kvm/powerpc.c | 18 +++++++++--------- 3 files changed, 21 insertions(+), 18 deletions(-)
arch/powerpc/include/asm/kvm.h +6 -6
@@ -265,12 +265,12 @@ struct kvm_debug_exit_arch {struct kvm_guest_debug_arch { }; -#define KVM_REG_MASK 0x001f -#define KVM_REG_EXT_MASK 0xffe0 -#define KVM_REG_GPR 0x0000 -#define KVM_REG_FPR 0x0020 -#define KVM_REG_QPR 0x0040 -#define KVM_REG_FQPR 0x0060 +#define KVM_MMIO_REG_MASK 0x001f +#define KVM_MMIO_REG_EXT_MASK 0xffe0 +#define KVM_MMIO_REG_GPR 0x0000 +#define KVM_MMIO_REG_FPR 0x0020 +#define KVM_MMIO_REG_QPR 0x0040 +#define KVM_MMIO_REG_FQPR 0x0060#define KVM_INTERRUPT_SET -1U#define KVM_INTERRUPT_UNSET -2U
arch/powerpc/kvm/book3s_paired_singles.c +6 -3
@@ -196,7 +196,8 @@ static int kvmppc_emulate_fpr_load(struct kvm_run *run, struct kvm_vcpu *vcpu,kvmppc_inject_pf (vcpu, addr, false); goto done_load; }else if (r ==EMULATE_DO_MMIO ) { - emulated =kvmppc_handle_load (run, vcpu,KVM_REG_FPR | rs, len,1 ); + emulated =kvmppc_handle_load (run, vcpu,KVM_MMIO_REG_FPR | rs, + len,1 ); goto done_load; }
@@ -286,11 +287,13 @@ static int kvmppc_emulate_psq_load(struct kvm_run *run, struct kvm_vcpu *vcpu,kvmppc_inject_pf (vcpu, addr, false); goto done_load; }else if ((r ==EMULATE_DO_MMIO ) && w) { - emulated =kvmppc_handle_load (run, vcpu,KVM_REG_FPR | rs,4 ,1 ); + emulated =kvmppc_handle_load (run, vcpu,KVM_MMIO_REG_FPR | rs, +4 ,1 ); vcpu->arch.qpr[rs] = tmp[1 ]; goto done_load; }else if (r ==EMULATE_DO_MMIO ) { - emulated =kvmppc_handle_load (run, vcpu,KVM_REG_FQPR | rs,8 ,1 ); + emulated =kvmppc_handle_load (run, vcpu,KVM_MMIO_REG_FQPR | rs, +8 ,1 ); goto done_load; }
arch/powerpc/kvm/powerpc.c +9 -9
@@ -445,20 +445,20 @@ static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,kvmppc_set_gpr (vcpu, vcpu->arch.io_gpr, gpr); -switch (vcpu->arch.io_gpr &KVM_REG_EXT_MASK ) { -case KVM_REG_GPR : +switch (vcpu->arch.io_gpr &KVM_MMIO_REG_EXT_MASK ) { +case KVM_MMIO_REG_GPR :kvmppc_set_gpr (vcpu, vcpu->arch.io_gpr, gpr);break ; -case KVM_REG_FPR : - vcpu->arch.fpr[vcpu->arch.io_gpr &KVM_REG_MASK ] = gpr; +case KVM_MMIO_REG_FPR : + vcpu->arch.fpr[vcpu->arch.io_gpr &KVM_MMIO_REG_MASK ] = gpr;break ;#ifdef CONFIG_PPC_BOOK3S -case KVM_REG_QPR : - vcpu->arch.qpr[vcpu->arch.io_gpr &KVM_REG_MASK ] = gpr; +case KVM_MMIO_REG_QPR : + vcpu->arch.qpr[vcpu->arch.io_gpr &KVM_MMIO_REG_MASK ] = gpr;break ; -case KVM_REG_FQPR : - vcpu->arch.fpr[vcpu->arch.io_gpr &KVM_REG_MASK ] = gpr; - vcpu->arch.qpr[vcpu->arch.io_gpr &KVM_REG_MASK ] = gpr; +case KVM_MMIO_REG_FQPR : + vcpu->arch.fpr[vcpu->arch.io_gpr &KVM_MMIO_REG_MASK ] = gpr; + vcpu->arch.qpr[vcpu->arch.io_gpr &KVM_MMIO_REG_MASK ] = gpr;break ;#endif default :
Signature · 1 line
1.6.0.2