Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp143260ybp; Wed, 16 Oct 2019 15:24:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqxVoekI1680fT2rDnjxzAWutRaZhmi9IVkHZGFkHU3yK7rf1+Nb2aA9Zv1v/+pqdKLeU43w X-Received: by 2002:a05:6402:29a:: with SMTP id l26mr535768edv.290.1571264647844; Wed, 16 Oct 2019 15:24:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1571264647; cv=pass; d=google.com; s=arc-20160816; b=tPskiOX6qr6ud6Dt3x3DLBN76Gm4fkw4YEgGivMdCA1S1kpJ8bwGfkN2foBz1Loelv yUla7REuMJ+jfkSiXsqZZrK/IOIXAb8i/ZqdNUqT5dPf1wpg68f84+fZp+qLjPbgyveU ZqIw2U2xSbjK3htbm7MvR73+Lxe+D/htQxbj3rFqr8j/BdJPkwQB8xVc5IlKR//e3ikL pYE9zXtyE0WPyqnzHTHdbh5RAF8zCKNLUOop43aWMFHhzPqJHY/kS/orJNJ467/Xjime 8WQr5wCnlXS/abL4FpFsONO4EcQa4UcInYpPhZWEHjsxTl19DHrrUul7GOvA6It8BIJr mHxg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :wdcipoutbound:content-language:accept-language:in-reply-to :references:message-id:date:thread-index:thread-topic:subject:cc:to :from:dkim-signature:ironport-sdr:dkim-signature; bh=pJxdlUVT+YmPN9OHbuHIqgaBPZMGQg6yvcm7fwJqT8Q=; b=VviIZbA56/eQngbjj/J3kTex7RLPSvrmNeW1j6XUP34Fsa9VQ45aabCdsKHeqMIAAj G2UFBYt47p6UHlJ+n2lrSVFQob1sLi7Fd1xRBygvlEZud85ALR3vMF1npzB7e20vgw9J QaddUGSFqO7Q6mho8Izao3NhvKRzsRB+G7ibHCPlOu8xu0Cjvlr4bmHPlKnI+RD91WBa o7Yaj5h9bhXYF0JKsYcnq0jwyRI22OimrjLYSZST1Q/c5NYUZiLob/oreqM+GNlnIxZI LOs+7nYAc09PrMgs1EjYRSUsmhHdV+6o8iHUNM/g33vUmhhpb5YWMNkRHZGntnpM8F5V ffhw== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=VJQDt3Ai; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=0FA+0EQ6; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b50si268925ede.28.2019.10.16.15.23.44; Wed, 16 Oct 2019 15:24:07 -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=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=VJQDt3Ai; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=0FA+0EQ6; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393467AbfJPQJT (ORCPT + 99 others); Wed, 16 Oct 2019 12:09:19 -0400 Received: from esa6.hgst.iphmx.com ([216.71.154.45]:34826 "EHLO esa6.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389749AbfJPQJS (ORCPT ); Wed, 16 Oct 2019 12:09:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1571242158; x=1602778158; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=ZNi8NLGQzchIES8/rX4PsvY27cfyhsX+140kOBWusRA=; b=VJQDt3AixjvvdayjuRzy7qYfs8jE6PVvFMr/jn6562AKZZM7zIP79HTP G+sEFraX9NR35X6QcTBJpOT8qPyqJlfRgl0k4wgQMvfyV8/aXiPfihQqU yA3/uL2O1lj1yttnrYnlK8XgzMNA3AvZxv0qA7ZNQ6D0fk9c7Hia1Pxrf 5WwkdnamSgbHEDND0GqZTJB3i/fDcewvA1A5D6eLpr4jrlvuSN8aEQD/p knbdTfgHXSns/dExfM/qldNSp3TlSfF1ugBZqG5/hC/hPqNaYdogioyrT 4MX6TodId0Ofw07+tReX/CG4IsKkJQE5g25/c3+CA+EEsCGy7MHIIAB0n Q==; IronPort-SDR: mt+VuPnLyOfBF2qfk/zhomOiC3Jq8FOeJp9HJWo/d7IEJ/XuoQXsnxmubs2HFX0kl/fQgm8eA9 9wjR8D7+8Z1OawB+XX6MBkjqdJFyRUHdWvPM/CCNYF1E5swm58R02l7xRjZeL1ziTmoU0yxFBU +fFyS2VFbaB127tF8iXbyNz10gzvkdjrbX4PCW11e0uerANgfIGThTRAW0OZHOPQRQw1VYjFYR C0PKDTwry9gFyhkJy3TzmByE4V/uHI7O6iK6zzYJKxkwwo48itGAeHR3BxhENOkcn1n4svLdnU Bf0= X-IronPort-AV: E=Sophos;i="5.67,304,1566835200"; d="scan'208";a="122255498" Received: from mail-by2nam05lp2052.outbound.protection.outlook.com (HELO NAM05-BY2-obe.outbound.protection.outlook.com) ([104.47.50.52]) by ob1.hgst.iphmx.com with ESMTP; 17 Oct 2019 00:09:17 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZaKMMLlLFmNyksjwmNi4ZOczPAbZwenyuT2bgRiwS5YhxibtaMvRmxEubuHdPgnymNkIikm0immZ2EIqIjdW5qOdh1DBqW8kgoFOULVR0RkW5fSIMOk7XwIF6W8Y4jRBL+4CVcC2Oby4Fe4VYDWDJ9lVrcLv3xrXcbVl9/Dea3pe5KwW35CtoVW4v84NqQa6139dh3K1bnDf4iZMb7s752eNkD4NjEFlsNIB2mmC360v3Z360TOnygm23G5pcQbulcJH7Oi+2H8rU2HXC1LvWrPnYhHWCPve0cWC+D5ijwnOazmNdiiCkeo7fnsdCDMHJrT76Dc03OY3bpG/pCxweA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pJxdlUVT+YmPN9OHbuHIqgaBPZMGQg6yvcm7fwJqT8Q=; b=FUBCCYRdLwligsfGe76dilo6c9APlciiAu2skZFonXF4AfGDhETy8QBKZwhZn1GPVTKV25wCIiDk08eR4gGFxK6tVIuI2L3ercsvGF5RmuDqALi8rS4dPfuBW4CyBDNdojfVedwdf1G0vQtPx8Ej7OleN6YDJBAx735+q12qyimUJAMp90Cp2DtVKotKLi7MDGz2a3lhZbGMYsY+xEUlr4jdwqYqn0XNFcsRz5E4ROFpaY9BbTpMqIq4mboTaV2n9kDiLLnEO7LYVFurDCBP2zxYXyYeXE8BdxxAxf6aWxag1iYvxkiNd01FInIONxK6H22nBYOvVnOqRefK7urSkA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pJxdlUVT+YmPN9OHbuHIqgaBPZMGQg6yvcm7fwJqT8Q=; b=0FA+0EQ67BpjaVVO97jYFtotTCF5jwmj0IGLdNueeg6MGOf3eN0O9feqUkyYeENG8Jlo74GzcMCK9asFcc8c/CqAzPq+FWcdOND/coJbvsCmhCkIL+EoI3M6+zc5FDh5y/yoYOvyyt1zyiTu5Zj9iTO8VwqZyhxnxUy3sDn74Go= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB7038.namprd04.prod.outlook.com (10.186.146.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2347.16; Wed, 16 Oct 2019 16:09:15 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a%7]) with mapi id 15.20.2347.023; Wed, 16 Oct 2019 16:09:15 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K CC: Daniel Lezcano , Thomas Gleixner , Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , Anup Patel , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" , Anup Patel Subject: [PATCH v9 06/22] RISC-V: KVM: Implement KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls Thread-Topic: [PATCH v9 06/22] RISC-V: KVM: Implement KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls Thread-Index: AQHVhDwOHj6Ygbw4FU2YJyfrYFDFAw== Date: Wed, 16 Oct 2019 16:09:15 +0000 Message-ID: <20191016160649.24622-7-anup.patel@wdc.com> References: <20191016160649.24622-1-anup.patel@wdc.com> In-Reply-To: <20191016160649.24622-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MAXPR01CA0098.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:5d::16) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [106.51.27.162] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 211ae4cc-2a05-4033-b8d6-08d7525330e1 x-ms-office365-filtering-ht: Tenant x-ms-traffictypediagnostic: MN2PR04MB7038: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:5236; x-forefront-prvs: 0192E812EC x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(366004)(376002)(396003)(136003)(346002)(39860400002)(199004)(189003)(102836004)(44832011)(486006)(2616005)(476003)(386003)(25786009)(446003)(52116002)(99286004)(186003)(76176011)(55236004)(66066001)(26005)(6506007)(5660300002)(36756003)(11346002)(64756008)(6436002)(66946007)(66446008)(66476007)(66556008)(30864003)(86362001)(6486002)(4326008)(6512007)(1076003)(305945005)(14454004)(256004)(7736002)(7416002)(478600001)(6116002)(71190400001)(3846002)(54906003)(110136005)(2906002)(316002)(71200400001)(8936002)(9456002)(50226002)(81156014)(81166006)(8676002)(14444005);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB7038;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: FdHxAkhgo5nM91DxuwO8FPKLppXM6eCMRmfSSuz2EcTiWmdtrmgBNmGr9y68acdlUFLbUuJxo7Qj2EWkKk67RzZLCj+KGly8KOG3blXv6ziQkmIqLQv/nziucy3ZTkJPqDzOPZB1DmBZtJp35vPpNAYlfw0PNt7VipSw9rd+1DSF18MGWNAVw9oTSsz3DIZIBtSYQYtp4jvWBVMhaxlZY246/gGZf3UeMnuXw+Gw6EjQBE9BjcYBJouQX+SpugP5xxHpy5xRZqh1VR1UFsOXBG+ag0b+cxQOj56w+YPwEumonB2frNc1s23dd6PWpc1uoTgXNe43MARe4T8cXooZki7Kqy2QqtYA8681N0mhD0rYzEcDBrCoEKTt5545qkW7J/9Lmxan3jd9IWsYm1ioO4ISPgHR6N88La5de3HocRA= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 211ae4cc-2a05-4033-b8d6-08d7525330e1 X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Oct 2019 16:09:15.4145 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: DhSvfxYCVOUnOIBO3r5S6E2v7bej/9sLNF6KLibDnZNYfVG4PCqN1S2E6iB6vGxdDTRuM8BTb1r55/cBmH8pvA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB7038 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 53 ++++++- arch/riscv/kvm/vcpu.c | 239 +++++++++++++++++++++++++++++- 2 files changed, 289 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/as= m/kvm.h index 6dbc056d58ba..997b85f6fded 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -41,10 +41,61 @@ struct kvm_guest_debug_arch { struct kvm_sync_regs { }; =20 -/* dummy definition */ +/* for KVM_GET_SREGS and KVM_SET_SREGS */ struct kvm_sregs { }; =20 +/* CONFIG registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_config { + unsigned long isa; + unsigned long tbfreq; +}; + +/* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_core { + struct user_regs_struct regs; + unsigned long mode; +}; + +/* Possible privilege modes for kvm_riscv_core */ +#define KVM_RISCV_MODE_S 1 +#define KVM_RISCV_MODE_U 0 + +/* CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_csr { + 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; +}; + +#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_riscv_core, 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_riscv_csr, name) / sizeof(unsigned long)) + #endif =20 #endif /* __LINUX_KVM_RISCV_H */ diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 9107469279a7..f76b165c9203 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -164,6 +164,219 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu,= struct vm_fault *vmf) return VM_FAULT_SIGBUS; } =20 +static int kvm_riscv_vcpu_get_reg_config(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + unsigned long __user *uaddr =3D + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CONFIG); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) + return -EINVAL; + + switch (reg_num) { + case KVM_REG_RISCV_CONFIG_REG(isa): + reg_val =3D vcpu->arch.isa; + break; + case KVM_REG_RISCV_CONFIG_REG(tbfreq): + reg_val =3D 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 =3D + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CONFIG); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) !=3D 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 =3D reg_val; + vcpu->arch.isa &=3D riscv_isa_extension_base(NULL); + vcpu->arch.isa &=3D 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 =3D &vcpu->arch.guest_context; + unsigned long __user *uaddr =3D + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CORE); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) + return -EINVAL; + if (reg_num >=3D sizeof(struct kvm_riscv_core) / sizeof(unsigned long)) + return -EINVAL; + + if (reg_num =3D=3D KVM_REG_RISCV_CORE_REG(regs.pc)) + reg_val =3D cntx->sepc; + else if (KVM_REG_RISCV_CORE_REG(regs.pc) < reg_num && + reg_num <=3D KVM_REG_RISCV_CORE_REG(regs.t6)) + reg_val =3D ((unsigned long *)cntx)[reg_num]; + else if (reg_num =3D=3D KVM_REG_RISCV_CORE_REG(mode)) + reg_val =3D (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 =3D &vcpu->arch.guest_context; + unsigned long __user *uaddr =3D + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CORE); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) + return -EINVAL; + if (reg_num >=3D sizeof(struct kvm_riscv_core) / sizeof(unsigned long)) + return -EINVAL; + + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + if (reg_num =3D=3D KVM_REG_RISCV_CORE_REG(regs.pc)) + cntx->sepc =3D reg_val; + else if (KVM_REG_RISCV_CORE_REG(regs.pc) < reg_num && + reg_num <=3D KVM_REG_RISCV_CORE_REG(regs.t6)) + ((unsigned long *)cntx)[reg_num] =3D reg_val; + else if (reg_num =3D=3D KVM_REG_RISCV_CORE_REG(mode)) { + if (reg_val =3D=3D KVM_RISCV_MODE_S) + cntx->sstatus |=3D SR_SPP; + else + cntx->sstatus &=3D ~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 =3D &vcpu->arch.guest_csr; + unsigned long __user *uaddr =3D + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CSR); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) + return -EINVAL; + if (reg_num >=3D sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) + return -EINVAL; + + if (reg_num =3D=3D KVM_REG_RISCV_CSR_REG(sip)) + kvm_riscv_vcpu_flush_interrupts(vcpu); + + reg_val =3D ((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 =3D &vcpu->arch.guest_csr; + unsigned long __user *uaddr =3D + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CSR); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) + return -EINVAL; + if (reg_num >=3D sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) + return -EINVAL; + + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + ((unsigned long *)csr)[reg_num] =3D reg_val; + + if (reg_num =3D=3D 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) =3D=3D KVM_REG_RISCV_CONFIG) + return kvm_riscv_vcpu_set_reg_config(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) =3D=3D KVM_REG_RISCV_CORE) + return kvm_riscv_vcpu_set_reg_core(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) =3D=3D 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) =3D=3D KVM_REG_RISCV_CONFIG) + return kvm_riscv_vcpu_get_reg_config(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) =3D=3D KVM_REG_RISCV_CORE) + return kvm_riscv_vcpu_get_reg_core(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) =3D=3D 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) { @@ -188,8 +401,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 =3D filp->private_data; + void __user *argp =3D (void __user *)arg; + long r =3D -EINVAL; + + switch (ioctl) { + case KVM_SET_ONE_REG: + case KVM_GET_ONE_REG: { + struct kvm_one_reg reg; + + r =3D -EFAULT; + if (copy_from_user(®, argp, sizeof(reg))) + break; + + if (ioctl =3D=3D KVM_SET_ONE_REG) + r =3D kvm_riscv_vcpu_set_reg(vcpu, ®); + else + r =3D kvm_riscv_vcpu_get_reg(vcpu, ®); + break; + } + default: + break; + } + + return r; } =20 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, --=20 2.17.1