Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp805676ybn; Tue, 24 Sep 2019 09:46:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqxOqZU2WOiMgkqOxwa92bYCqF2pLA7fECx6l0M4IDPUxV6rPvPPfBbk43zsNiBLhdMDqKQB X-Received: by 2002:a17:906:49c7:: with SMTP id w7mr3472658ejv.167.1569343612034; Tue, 24 Sep 2019 09:46:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569343612; cv=none; d=google.com; s=arc-20160816; b=UlKHNjY2X6RPKvSi3xI2CpGZ//II3Rdn00dbCT+x3OeGZkTO59J18Oy5sOTlbMPqu5 vbF88eYvDarTC7+93AQ69qjDmy88wadD9yD4PkreaYCxpyKqfWDd0cQhGx3bKKydPXmj DoAJmnTd2ngChaQp5CL2j4q/JdrdmgPzwCAEHs+RhyZ5NxLZ3UL9kkkE/OfC8m/dWUhL GsaSdaU8xp4NU4Q9gydI9a3as7L2tptgQSg7Jp34bcYCbscO9dJDWJ0ssY81iYs3Y10m mR7AhrhVgWVB1kdlNgqSb8by3J7DdF59pVYX/PR8+4jlqJPmJUxeQyAUB4NstIzNg/Kg Vc+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=9+a8eglVXevMoD1Tojjjkc1G0+vejdfTstL86XSetKs=; b=G46HWs4MWmRHe+fGSe4xhI+bQlEuq457gVENP4M80/iOhpKDJHdrqkk0lVkWYq1a6q 2xE3tEIfAwUrO7hxWxhtPkFy3HSXgczETk55XRhKEpvfefgXBAIN5pMGT5Bqk7raquJ2 1lkP4R+R3qzdRFiQezoDkmkGEI7+0qejRG4dG95qABzx5I61AjmlhTb7IsfKX55JGyC/ H37zuulUPA2C6zRbfuQSAszexWbNnm9Py1Qk7UJBClOUiaEYKkVAsSiAZtzybhiSnAaE snOUGHusvNAa1Ci9GoUYcexKFELX5BX7ZOhB4PpBDbFtBOrQT4553b5Si6zXg34Kp6jx uRjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=F9c7YDGL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c14si1202104ejz.242.2019.09.24.09.46.28; Tue, 24 Sep 2019 09:46:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=F9c7YDGL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405838AbfIWDnB (ORCPT + 99 others); Sun, 22 Sep 2019 23:43:01 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:42083 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404038AbfIWDnA (ORCPT ); Sun, 22 Sep 2019 23:43:00 -0400 Received: by mail-wr1-f66.google.com with SMTP id n14so12174924wrw.9 for ; Sun, 22 Sep 2019 20:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9+a8eglVXevMoD1Tojjjkc1G0+vejdfTstL86XSetKs=; b=F9c7YDGLKppYwwsO10SVV1YeQq5ZEYYdI62OYKCAhOa+toTWJ6kYNv0nqsaorBYGr2 dp4eWslwO3T61yXcRpkfrRRzVr7kb1YQ3+rAJKbIKSNaRm4jKdQ8aH/RVVEthOFKVlXW zgZ/86JyOAfozgV6fiWop0ERJUW3LxnCROck9HggqoeRjPPLkZQSrhXBWyyFuyOcPRUd H0WB6DgDdJT48/l2lh1xOctmoFCHli56KZsWP2qiuL4S57WrkpN+OHD6v9vI9kyKb0B8 PDnIImZni7wNOP3dMbhBdjiJwdhv0ZLdZ1IMub0OEGcUKdZAb7E02qhN66MZfsoE0usG 2A8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9+a8eglVXevMoD1Tojjjkc1G0+vejdfTstL86XSetKs=; b=dbEhS9LfJDqS8VIFNeM1sSncVGsB8okY6YagaLFHHBNRjgdSEIBUTvhLwEwaVZVynV boUkTJ5J1WSVzwVG9yLm0uVWnbaqKGkU8pinQFYfJWkd5++TPH8320qG3sWyKxXuO5+3 iCf/JaBPPDcA2Cau5gf/44YMWq8tcfBX8vLCD8o7SOrCe09GYYie6F5j7s1f/mEcvDJW MNPkOR38CZmb1aWWLaxQh3/qxkm6/Jkwu/V/TkHsj1oSfJeD55qZTG2ynx4MAirzNBRk npyPoDzjkr+qK5vFdVBtF5XZGVyQlut+1YdYz73PHlJoluP9TDc97mfo7VzhdGrcmwfr XZKw== X-Gm-Message-State: APjAAAV1oKX4bQlJa12gm/uqLRqYFrN5jeFJxPO4bCSiwS83Sj9+jTj1 Dhpt4GGI4NMUfSQlpqTjcfTs8cd9CriCmANBmdbd/MPayUg= X-Received: by 2002:adf:f1d1:: with SMTP id z17mr20173583wro.330.1569210176021; Sun, 22 Sep 2019 20:42:56 -0700 (PDT) MIME-Version: 1.0 References: <20190904161245.111924-1-anup.patel@wdc.com> <20190904161245.111924-10-anup.patel@wdc.com> In-Reply-To: <20190904161245.111924-10-anup.patel@wdc.com> From: Anup Patel Date: Mon, 23 Sep 2019 09:12:44 +0530 Message-ID: Subject: Re: [PATCH v7 08/21] RISC-V: KVM: Implement KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls To: Anup Patel , Alexander Graf Cc: Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K , Daniel Lezcano , Thomas Gleixner , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 4, 2019 at 9:44 PM Anup Patel wrote: > > For KVM RISC-V, we use KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls to access > VCPU config and registers from user-space. > > We have three types of VCPU registers: > 1. CONFIG - these are VCPU config and capabilities > 2. CORE - these are VCPU general purpose registers > 3. CSR - these are VCPU control and status registers > > The CONFIG registers available to user-space are ISA and TIMEBASE. Out > of these, TIMEBASE is a read-only register which inform user-space about > VCPU timer base frequency. The ISA register is a read and write register > where user-space can only write the desired VCPU ISA capabilities before > running the VCPU. > > The CORE registers available to user-space are PC, RA, SP, GP, TP, A0-A7, > T0-T6, S0-S11 and MODE. Most of these are RISC-V general registers except > PC and MODE. The PC register represents program counter whereas the MODE > register represent VCPU privilege mode (i.e. S/U-mode). > > The CSRs available to user-space are SSTATUS, SIE, STVEC, SSCRATCH, SEPC, > SCAUSE, STVAL, SIP, and SATP. All of these are read/write registers. > > In future, more VCPU register types will be added (such as FP) for the > KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls. > > Signed-off-by: Anup Patel > Acked-by: Paolo Bonzini > Reviewed-by: Paolo Bonzini > --- > arch/riscv/include/uapi/asm/kvm.h | 46 +++++- > arch/riscv/kvm/vcpu.c | 235 +++++++++++++++++++++++++++++- > 2 files changed, 278 insertions(+), 3 deletions(-) > > diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h > index 6dbc056d58ba..08c4515ad71b 100644 > --- a/arch/riscv/include/uapi/asm/kvm.h > +++ b/arch/riscv/include/uapi/asm/kvm.h > @@ -23,8 +23,15 @@ > > /* for KVM_GET_REGS and KVM_SET_REGS */ > struct kvm_regs { > + /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */ > + struct user_regs_struct regs; > + unsigned long mode; > }; As discussed in LPC 2019 with Alex Graf, I will add separate struct for CORE registers instead of re-using "struct kvm_regs". > > +/* Possible privilege modes for kvm_regs */ > +#define KVM_RISCV_MODE_S 1 > +#define KVM_RISCV_MODE_U 0 > + > /* for KVM_GET_FPU and KVM_SET_FPU */ > struct kvm_fpu { > }; > @@ -41,10 +48,47 @@ struct kvm_guest_debug_arch { > struct kvm_sync_regs { > }; > > -/* dummy definition */ > +/* for KVM_GET_SREGS and KVM_SET_SREGS */ > struct kvm_sregs { > + unsigned long sstatus; > + unsigned long sie; > + unsigned long stvec; > + unsigned long sscratch; > + unsigned long sepc; > + unsigned long scause; > + unsigned long stval; > + unsigned long sip; > + unsigned long satp; > +}; Same as above, I will add separate struct for CSR registers instead of re-using "struct kvm_sregs". > + > +/* for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ > +struct kvm_riscv_config { > + unsigned long isa; > + unsigned long tbfreq; > }; > > +#define KVM_REG_SIZE(id) \ > + (1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT)) > + > +/* If you need to interpret the index values, here is the key: */ > +#define KVM_REG_RISCV_TYPE_MASK 0x00000000FF000000 > +#define KVM_REG_RISCV_TYPE_SHIFT 24 > + > +/* Config registers are mapped as type 1 */ > +#define KVM_REG_RISCV_CONFIG (0x01 << KVM_REG_RISCV_TYPE_SHIFT) > +#define KVM_REG_RISCV_CONFIG_REG(name) \ > + (offsetof(struct kvm_riscv_config, name) / sizeof(unsigned long)) > + > +/* Core registers are mapped as type 2 */ > +#define KVM_REG_RISCV_CORE (0x02 << KVM_REG_RISCV_TYPE_SHIFT) > +#define KVM_REG_RISCV_CORE_REG(name) \ > + (offsetof(struct kvm_regs, name) / sizeof(unsigned long)) > + > +/* Control and status registers are mapped as type 3 */ > +#define KVM_REG_RISCV_CSR (0x03 << KVM_REG_RISCV_TYPE_SHIFT) > +#define KVM_REG_RISCV_CSR_REG(name) \ > + (offsetof(struct kvm_sregs, name) / sizeof(unsigned long)) > + > #endif > > #endif /* __LINUX_KVM_RISCV_H */ > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > index 3223f723f79e..b95dfc959009 100644 > --- a/arch/riscv/kvm/vcpu.c > +++ b/arch/riscv/kvm/vcpu.c > @@ -165,6 +165,215 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) > return VM_FAULT_SIGBUS; > } > > +static int kvm_riscv_vcpu_get_reg_config(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + unsigned long __user *uaddr = > + (unsigned long __user *)(unsigned long)reg->addr; > + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | > + KVM_REG_SIZE_MASK | > + KVM_REG_RISCV_CONFIG); > + unsigned long reg_val; > + > + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) > + return -EINVAL; > + > + switch (reg_num) { > + case KVM_REG_RISCV_CONFIG_REG(isa): > + reg_val = vcpu->arch.isa; > + break; > + case KVM_REG_RISCV_CONFIG_REG(tbfreq): > + reg_val = riscv_timebase; > + break; > + default: > + return -EINVAL; > + }; > + > + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) > + return -EFAULT; > + > + return 0; > +} > + > +static int kvm_riscv_vcpu_set_reg_config(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + unsigned long __user *uaddr = > + (unsigned long __user *)(unsigned long)reg->addr; > + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | > + KVM_REG_SIZE_MASK | > + KVM_REG_RISCV_CONFIG); > + unsigned long reg_val; > + > + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) > + return -EINVAL; > + > + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) > + return -EFAULT; > + > + switch (reg_num) { > + case KVM_REG_RISCV_CONFIG_REG(isa): > + if (!vcpu->arch.ran_atleast_once) { > + vcpu->arch.isa = reg_val; > + vcpu->arch.isa &= riscv_isa_extension_base(NULL); > + vcpu->arch.isa &= KVM_RISCV_ISA_ALLOWED; > + } else { > + return -ENOTSUPP; > + } > + break; > + case KVM_REG_RISCV_CONFIG_REG(tbfreq): > + return -ENOTSUPP; > + default: > + return -EINVAL; > + }; > + > + return 0; > +} > + > +static int kvm_riscv_vcpu_get_reg_core(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; > + unsigned long __user *uaddr = > + (unsigned long __user *)(unsigned long)reg->addr; > + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | > + KVM_REG_SIZE_MASK | > + KVM_REG_RISCV_CORE); > + unsigned long reg_val; > + > + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) > + return -EINVAL; > + > + if (reg_num == KVM_REG_RISCV_CORE_REG(regs.pc)) > + reg_val = cntx->sepc; > + else if (KVM_REG_RISCV_CORE_REG(regs.pc) < reg_num && > + reg_num <= KVM_REG_RISCV_CORE_REG(regs.t6)) > + reg_val = ((unsigned long *)cntx)[reg_num]; > + else if (reg_num == KVM_REG_RISCV_CORE_REG(mode)) > + reg_val = (cntx->sstatus & SR_SPP) ? > + KVM_RISCV_MODE_S : KVM_RISCV_MODE_U; > + else > + return -EINVAL; > + > + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) > + return -EFAULT; > + > + return 0; > +} > + > +static int kvm_riscv_vcpu_set_reg_core(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; > + unsigned long __user *uaddr = > + (unsigned long __user *)(unsigned long)reg->addr; > + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | > + KVM_REG_SIZE_MASK | > + KVM_REG_RISCV_CORE); > + unsigned long reg_val; > + > + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) > + return -EINVAL; > + > + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) > + return -EFAULT; > + > + if (reg_num == KVM_REG_RISCV_CORE_REG(regs.pc)) > + cntx->sepc = reg_val; > + else if (KVM_REG_RISCV_CORE_REG(regs.pc) < reg_num && > + reg_num <= KVM_REG_RISCV_CORE_REG(regs.t6)) > + ((unsigned long *)cntx)[reg_num] = reg_val; > + else if (reg_num == KVM_REG_RISCV_CORE_REG(mode)) { > + if (reg_val == KVM_RISCV_MODE_S) > + cntx->sstatus |= SR_SPP; > + else > + cntx->sstatus &= ~SR_SPP; > + } else > + return -EINVAL; > + > + return 0; > +} > + > +static int kvm_riscv_vcpu_get_reg_csr(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; > + unsigned long __user *uaddr = > + (unsigned long __user *)(unsigned long)reg->addr; > + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | > + KVM_REG_SIZE_MASK | > + KVM_REG_RISCV_CSR); > + unsigned long reg_val; > + > + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) > + return -EINVAL; > + if (reg_num >= sizeof(struct kvm_sregs) / sizeof(unsigned long)) > + return -EINVAL; > + > + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) > + kvm_riscv_vcpu_flush_interrupts(vcpu); > + > + reg_val = ((unsigned long *)csr)[reg_num]; > + > + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) > + return -EFAULT; > + > + return 0; > +} > + > +static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; > + unsigned long __user *uaddr = > + (unsigned long __user *)(unsigned long)reg->addr; > + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | > + KVM_REG_SIZE_MASK | > + KVM_REG_RISCV_CSR); > + unsigned long reg_val; > + > + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) > + return -EINVAL; > + if (reg_num >= sizeof(struct kvm_sregs) / sizeof(unsigned long)) > + return -EINVAL; > + > + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) > + return -EFAULT; > + > + ((unsigned long *)csr)[reg_num] = reg_val; > + > + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) > + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > + > + return 0; > +} > + > +static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG) > + return kvm_riscv_vcpu_set_reg_config(vcpu, reg); > + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE) > + return kvm_riscv_vcpu_set_reg_core(vcpu, reg); > + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR) > + return kvm_riscv_vcpu_set_reg_csr(vcpu, reg); > + > + return -EINVAL; > +} > + > +static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, > + const struct kvm_one_reg *reg) > +{ > + if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG) > + return kvm_riscv_vcpu_get_reg_config(vcpu, reg); > + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE) > + return kvm_riscv_vcpu_get_reg_core(vcpu, reg); > + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR) > + return kvm_riscv_vcpu_get_reg_csr(vcpu, reg); > + > + return -EINVAL; > +} > + > long kvm_arch_vcpu_async_ioctl(struct file *filp, > unsigned int ioctl, unsigned long arg) > { > @@ -189,8 +398,30 @@ long kvm_arch_vcpu_async_ioctl(struct file *filp, > long kvm_arch_vcpu_ioctl(struct file *filp, > unsigned int ioctl, unsigned long arg) > { > - /* TODO: */ > - return -EINVAL; > + struct kvm_vcpu *vcpu = filp->private_data; > + void __user *argp = (void __user *)arg; > + long r = -EINVAL; > + > + switch (ioctl) { > + case KVM_SET_ONE_REG: > + case KVM_GET_ONE_REG: { > + struct kvm_one_reg reg; > + > + r = -EFAULT; > + if (copy_from_user(®, argp, sizeof(reg))) > + break; > + > + if (ioctl == KVM_SET_ONE_REG) > + r = kvm_riscv_vcpu_set_reg(vcpu, ®); > + else > + r = kvm_riscv_vcpu_get_reg(vcpu, ®); > + break; > + } > + default: > + break; > + } > + > + return r; > } > > int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, > -- > 2.17.1 > Regards, Anup