Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2335073ybl; Thu, 29 Aug 2019 06:57:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqymRxo9CgxDT05nC1Nw9FsWKxbLhgqzrODSEp2+Wb6tEBObJa0+V72IhVp+IoDKGQQl+Z6s X-Received: by 2002:a17:902:a40d:: with SMTP id p13mr9887706plq.92.1567087064804; Thu, 29 Aug 2019 06:57:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1567087064; cv=pass; d=google.com; s=arc-20160816; b=zVNCXnxXBzbI21ffs2hyexf5Bsk913so4Tg2cDMRGtEtX9eYKomM8FPdsa75vO+l2n rrgRqlwGaMRP3oJI3HBqnDzUJkpSbTqRaBKBEpmbcNqKz/pAVvkZ07qh/9j7Np0quYMN 0V8MjrhdCbgXT1G+vXOSicS7W9BcRvKuKEtg4HhoX3NJ9bDvIooJeL3uNmPUGx0A3aP9 IKQn9zNX+p0zQiDaZ2SE5XYAjQOgtCcLV6NFNEKCSNjsHGDtZEk5VdNCkwKCuOwz31S3 1PbQL9Hp8E/RqWmuA+6JYo7C6Uzehp9qr4tckFCxRhwg/vjz6gyXpdYmGLFH56Fiy4S2 9JPQ== 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=G4F+pObVzraWgbSZALmMMV+JHw/3vp3JEuzXyWTcRck=; b=oFK+uBAuRFwrTBFRLqFJPcIEPPGcuBSn9tJLP9CzTWGtLbQWtwUfu1Qkp5W/Mgit1m BmV3vxrqf40r60YC3kpE7YyGfrYM0373glcDyqAoUCPBcwgOGjEJG496sKBzqHinvMfL LZNtf8qs7F7qFgGbRan3yItrpWAh2y3G6hv5feAUzBWR85YaoTK2emJImHfJNFsmMUTf xI0YNqYTq0jg0OCCD5RRkLaWqQnvRZu1FeFQgSQm25WF9yr0gsqoYC61THZbG95zygjK K9993pQv0PR5qZzxyfvJSdMP94i4xjiz6kfDkf/RFyZi2tgsRcbPTAkbNgUAnbSAI71/ uqKQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=VHT8JUJI; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=LJ46h6Ln; 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 j1si2017448plk.55.2019.08.29.06.57.28; Thu, 29 Aug 2019 06:57:44 -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=VHT8JUJI; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=LJ46h6Ln; 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 S1728061AbfH2N4F (ORCPT + 99 others); Thu, 29 Aug 2019 09:56:05 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:35113 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728001AbfH2N4F (ORCPT ); Thu, 29 Aug 2019 09:56:05 -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=1567086965; x=1598622965; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=m4kyNVZ1rRk2FthfjB8FQ3wOnCKyxKs5UcOO5M5C6gM=; b=VHT8JUJIuCnpX7TU5mhAYmK5dYTz+GZQHP5kNnWWo/W9xEAYGzOA3iSh rpg0APJejFcncTY4WslcMfkOJJ0R00spQdPbFOT3eD9cqO8G6lsh31kkO 0FwNVeU90IJMSPNQThWHiXCUT5kfrUSzoGZX8U8DZsBaDktpWu5tLwi4D S+e7T+IjTgdIqIrAZv62S8hEDida5mlpC1ifCwDBA9D0Db8Onw807w+dC jfeX7XYWBF8f5uiq/9kJrrMrQ2e5HxbHs29un97oLG4Y1Tg7KXUy5+xAI 5zpCkKXcETdE9fJLsJ+2jLl+j4/Pu8sTV64iKzApS52dCr4WoAO51MRDy A==; IronPort-SDR: T8aTGSH6qJ4Xe/gxUxXfDt6Naf1bV6f6gP+QupGBg7NjwD/jDstg/iQ9NelGwH1PpMM/tfCcc/ YsOpyZO8pZ4YlL3D2p5sDJp22bGoMHZPH5QreE/k89gj1zrlD9toI9VNTz6EWslZ4uZcaU1VMr 39V3/SZFD1G0pFWoXBpg/I0rmfGEQAMf6sWdLRtQUos9Nrk7Q5u6ZE7NJgyRKfDC26V6MsFHdu GfB+Qe6iyIwObtd+L6ebmZpJtgmdNQZXysWPkczd+Xtyg8g9HgOUXmVh6AuYeULN3A6qbimD7T xu8= X-IronPort-AV: E=Sophos;i="5.64,443,1559491200"; d="scan'208";a="116951646" Received: from mail-by2nam05lp2057.outbound.protection.outlook.com (HELO NAM05-BY2-obe.outbound.protection.outlook.com) ([104.47.50.57]) by ob1.hgst.iphmx.com with ESMTP; 29 Aug 2019 21:56:02 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FpW9FdNmT8Y8E0lRSvhDupMgAexiTDwy1PJbBAPSH2f0itRUn+tGFu3YWqtWcrd2iIrSYsVRXE5UzHoIQcSu74r5NqFVeyemrNTj1f/rRHiJd3uRMcRoN3xravqzdZ7UwmH+f0EHxGkYjiFE3EHmIlRjmSA+VJVuzttcrdlcElMFDqHOjJoDTPGrs9aztI/eO68FOYfoU9ywt+8HNnSqMNtpT9F809yXHfyytn3xw93xekA6iS8h85fjs9A5MWHoH0WuN0JVXJoRbxpAAe2Vd9b2NNGHit5yKWUrBCy99QmvmbAVdcIYHLpEllXAGU6930F/K8Go+oYofbKo1YsXPw== 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=G4F+pObVzraWgbSZALmMMV+JHw/3vp3JEuzXyWTcRck=; b=GKZWJaWlJPvkMFABMHAyMMz+E1MPcxMjhHsdxxE6U22XDePCR3dILJ+wCBez8m/rrdVrxFREV62b6+YLZv0Pr5XIbKQ5xzyE+88kHDnAOHYYoadHWXWq0rX6nPHAWd6NVaSPVexchn0eDs2gM+Ogj5xwjFq8CU13W7BUMhIBAb83GtvzehGONwprg3nVtbPO4kR0/3Yk16Fkgiaay+fzKI7EuxlIOvWa3WCcKARShGdRbzXa3BS1Jcd5TR/OiVLryJ8qHCrUT1PpF5S75o/wfBaFnlAttWp/UqdLD/IcwzhSoFF2IHsff+SMlHU9dMOB6E2RI+9wsvIXNfMk9mf9kA== 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=G4F+pObVzraWgbSZALmMMV+JHw/3vp3JEuzXyWTcRck=; b=LJ46h6LnQ1jNpvGkUTvBBf3GubHllZX/9sab6RxVz67Y7C7ZtyEsYlbSb0Wk1cSg69Z8GNaWyISxaQTx9NwZIrH12Sl6hNBuw7/SnzLhqqVrh80aJNihOraKJjB35mPQahZFvlBr8Qck3q56edi8s+mepVIgn4KJ5S/ANhI80sk= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5616.namprd04.prod.outlook.com (20.179.22.29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2199.21; Thu, 29 Aug 2019 13:56:00 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::e1a5:8de2:c3b1:3fb0]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::e1a5:8de2:c3b1:3fb0%7]) with mapi id 15.20.2220.013; Thu, 29 Aug 2019 13:55:59 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K CC: Daniel Lezcano , Thomas Gleixner , 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 v6 07/21] RISC-V: KVM: Implement VCPU interrupts and requests handling Thread-Topic: [PATCH v6 07/21] RISC-V: KVM: Implement VCPU interrupts and requests handling Thread-Index: AQHVXnF8ID7x3Nabu0SaqBvLC55/Xg== Date: Thu, 29 Aug 2019 13:55:59 +0000 Message-ID: <20190829135427.47808-8-anup.patel@wdc.com> References: <20190829135427.47808-1-anup.patel@wdc.com> In-Reply-To: <20190829135427.47808-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MAXPR0101CA0072.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:e::34) 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: [49.207.51.114] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: b4071b3c-89fa-4be0-9922-08d72c889f37 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(5600166)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:MN2PR04MB5616; x-ms-traffictypediagnostic: MN2PR04MB5616: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:3044; x-forefront-prvs: 0144B30E41 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(136003)(366004)(376002)(346002)(396003)(189003)(199004)(7416002)(52116002)(81166006)(316002)(6506007)(386003)(30864003)(25786009)(7736002)(81156014)(305945005)(2906002)(36756003)(71190400001)(14454004)(99286004)(1076003)(256004)(14444005)(4326008)(66946007)(76176011)(66446008)(64756008)(66556008)(66476007)(102836004)(6116002)(50226002)(55236004)(476003)(6512007)(3846002)(71200400001)(5660300002)(26005)(186003)(44832011)(86362001)(66066001)(6436002)(446003)(8936002)(6486002)(110136005)(54906003)(2616005)(486006)(53936002)(11346002)(478600001)(8676002);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB5616;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: aEofCIOJ5EBPISjol18nFXiAOg9EV5YrWnjWgCVJxJZMVJAuoMEy0kInUOYC1W9np9PV5aih/Dk1NqsT/s8pRgy0DjJKakMz9btJxxQsp49yBBwFvEPvtZVfM1Jq+KruRaBP4m+Q9A6+mbXlk/XkD6TfkBKHARDgFwXgppJ9hgtdUGsmhn9eVmw+MT0UnOGyiTskEZUa7fUhEQUO6DXyDUCpaZxmEI3bhkiCNGE6iwOFluWxMLuz/NY9SBU4NZPlmr4EpCKkDsx3EfsJ5kthpCH4hhTDKtCWCOa7ibFBvJzDbxd3fTU63+xwmTCo6gs9QylirMkDv2BBzxvbAIeXgjWkBtXNjIOjn+kjuN5aJ0Uu1b4e2y4P1qKHiSgBO7CzI9TVprr6HZ/GjDDDzAadQ6c8m6UPLREQh4lUOvdHRoE= 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: b4071b3c-89fa-4be0-9922-08d72c889f37 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Aug 2019 13:55:59.7708 (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: lFLJURWpY0hnYYrbxkISEWLfQY7bRRi/kXygZFHUOGXp6xkM1nGEkes+UPVkVGZ/alb/MlydgLZkt3MQrgTKyA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5616 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implements VCPU interrupts and requests which are both asynchronous events. The VCPU interrupts can be set/unset using KVM_INTERRUPT ioctl from user-space. In future, the in-kernel IRQCHIP emulation will use kvm_riscv_vcpu_set_interrupt() and kvm_riscv_vcpu_unset_interrupt() functions to set/unset VCPU interrupts. Important VCPU requests implemented by this patch are: KVM_REQ_SLEEP - set whenever VCPU itself goes to sleep state KVM_REQ_VCPU_RESET - set whenever VCPU reset is requested The WFI trap-n-emulate (added later) will use KVM_REQ_SLEEP request and kvm_riscv_vcpu_has_interrupt() function. The KVM_REQ_VCPU_RESET request will be used by SBI emulation (added later) to power-up a VCPU in power-off state. The user-space can use the GET_MPSTATE/SET_MPSTATE ioctls to get/set power state of a VCPU. Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini Reviewed-by: Alexander Graf --- arch/riscv/include/asm/kvm_host.h | 26 ++++ arch/riscv/include/uapi/asm/kvm.h | 3 + arch/riscv/kvm/main.c | 8 ++ arch/riscv/kvm/vcpu.c | 193 ++++++++++++++++++++++++++++-- 4 files changed, 217 insertions(+), 13 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index dab32c9c3470..d801216da6d0 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -122,6 +122,21 @@ struct kvm_vcpu_arch { /* CPU CSR context upon Guest VCPU reset */ struct kvm_vcpu_csr guest_reset_csr; =20 + /* + * VCPU interrupts + * + * We have a lockless approach for tracking pending VCPU interrupts + * implemented using atomic bitops. The irqs_pending bitmap represent + * pending interrupts whereas irqs_pending_mask represent bits changed + * in irqs_pending. Our approach is modeled around multiple producer + * and single consumer problem where the consumer is the VCPU itself. + */ + unsigned long irqs_pending; + unsigned long irqs_pending_mask; + + /* VCPU power-off state */ + bool power_off; + /* Don't run the VCPU (blocked) */ bool pause; =20 @@ -135,6 +150,9 @@ static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu= *vcpu) {} static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {} =20 +int kvm_riscv_setup_vsip(void); +void kvm_riscv_cleanup_vsip(void); + void kvm_riscv_stage2_flush_cache(struct kvm_vcpu *vcpu); int kvm_riscv_stage2_alloc_pgd(struct kvm *kvm); void kvm_riscv_stage2_free_pgd(struct kvm *kvm); @@ -146,4 +164,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct = kvm_run *run, =20 static inline void __kvm_riscv_switch_to(struct kvm_vcpu_arch *vcpu_arch) = {} =20 +int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq); +int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq= ); +void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu); +bool kvm_riscv_vcpu_has_interrupt(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu); + #endif /* __RISCV_KVM_HOST_H__ */ diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/as= m/kvm.h index d15875818b6e..6dbc056d58ba 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -18,6 +18,9 @@ =20 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1 =20 +#define KVM_INTERRUPT_SET -1U +#define KVM_INTERRUPT_UNSET -2U + /* for KVM_GET_REGS and KVM_SET_REGS */ struct kvm_regs { }; diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index e1ffe6d42f39..d088247843c5 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -48,6 +48,8 @@ int kvm_arch_hardware_enable(void) hideleg |=3D SIE_SEIE; csr_write(CSR_HIDELEG, hideleg); =20 + csr_write(CSR_VSIP, 0); + return 0; } =20 @@ -59,11 +61,17 @@ void kvm_arch_hardware_disable(void) =20 int kvm_arch_init(void *opaque) { + int ret; + if (!riscv_isa_extension_available(NULL, h)) { kvm_info("hypervisor extension not available\n"); return -ENODEV; } =20 + ret =3D kvm_riscv_setup_vsip(); + if (ret) + return ret; + kvm_info("hypervisor extension available\n"); =20 return 0; diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 8272b05d6ce4..3223f723f79e 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] =3D { riscv_isa_extension_mask(s) | \ riscv_isa_extension_mask(u)) =20 +static unsigned long __percpu *vsip_shadow; + static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) { struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; @@ -50,6 +53,9 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) memcpy(csr, reset_csr, sizeof(*csr)); =20 memcpy(cntx, reset_cntx, sizeof(*cntx)); + + WRITE_ONCE(vcpu->arch.irqs_pending, 0); + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); } =20 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) @@ -116,8 +122,8 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) =20 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return READ_ONCE(vcpu->arch.irqs_pending) & + vcpu->arch.guest_csr.vsie & (1UL << IRQ_S_TIMER); } =20 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) @@ -130,20 +136,18 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) =20 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return (kvm_riscv_vcpu_has_interrupt(vcpu) && + !vcpu->arch.power_off && !vcpu->arch.pause); } =20 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return kvm_vcpu_exiting_guest_mode(vcpu) =3D=3D IN_GUEST_MODE; } =20 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { - /* TODO: */ - return false; + return (vcpu->arch.guest_context.sstatus & SR_SPP) ? true : false; } =20 bool kvm_arch_has_vcpu_debugfs(void) @@ -164,7 +168,21 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, = struct vm_fault *vmf) long kvm_arch_vcpu_async_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { - /* TODO; */ + struct kvm_vcpu *vcpu =3D filp->private_data; + void __user *argp =3D (void __user *)arg; + + if (ioctl =3D=3D KVM_INTERRUPT) { + struct kvm_interrupt irq; + + if (copy_from_user(&irq, argp, sizeof(irq))) + return -EFAULT; + + if (irq.irq =3D=3D KVM_INTERRUPT_SET) + return kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_S_EXT); + else + return kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_S_EXT); + } + return -ENOIOCTLCMD; } =20 @@ -213,18 +231,111 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vc= pu, struct kvm_regs *regs) return -EINVAL; } =20 +void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; + unsigned long mask, val; + + if (READ_ONCE(vcpu->arch.irqs_pending_mask)) { + mask =3D xchg_acquire(&vcpu->arch.irqs_pending_mask, 0); + val =3D READ_ONCE(vcpu->arch.irqs_pending) & mask; + + csr->vsip &=3D ~mask; + csr->vsip |=3D val; + } +} + +void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) +{ + vcpu->arch.guest_csr.vsip =3D csr_read(CSR_VSIP); + vcpu->arch.guest_csr.vsie =3D csr_read(CSR_VSIE); + + /* Guest can directly update VSIP software interrupt bits */ + if (vcpu->arch.guest_csr.vsip ^ READ_ONCE(vcpu->arch.irqs_pending)) { + if (vcpu->arch.guest_csr.vsip & (1UL << IRQ_S_SOFT)) + kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_S_SOFT); + else + kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_S_SOFT); + } +} + +int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq) +{ + if (irq !=3D IRQ_S_SOFT && + irq !=3D IRQ_S_TIMER && + irq !=3D IRQ_S_EXT) + return -EINVAL; + + set_bit(irq, &vcpu->arch.irqs_pending); + smp_mb__before_atomic(); + set_bit(irq, &vcpu->arch.irqs_pending_mask); + + kvm_vcpu_kick(vcpu); + + return 0; +} + +int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq= ) +{ + if (irq !=3D IRQ_S_SOFT && + irq !=3D IRQ_S_TIMER && + irq !=3D IRQ_S_EXT) + return -EINVAL; + + clear_bit(irq, &vcpu->arch.irqs_pending); + smp_mb__before_atomic(); + set_bit(irq, &vcpu->arch.irqs_pending_mask); + + return 0; +} + +bool kvm_riscv_vcpu_has_interrupt(struct kvm_vcpu *vcpu) +{ + return (READ_ONCE(vcpu->arch.irqs_pending) & + vcpu->arch.guest_csr.vsie) ? true : false; +} + +void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu) +{ + vcpu->arch.power_off =3D true; + kvm_make_request(KVM_REQ_SLEEP, vcpu); + kvm_vcpu_kick(vcpu); +} + +void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu) +{ + vcpu->arch.power_off =3D false; + kvm_vcpu_wake_up(vcpu); +} + int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - /* TODO: */ + if (vcpu->arch.power_off) + mp_state->mp_state =3D KVM_MP_STATE_STOPPED; + else + mp_state->mp_state =3D KVM_MP_STATE_RUNNABLE; + return 0; } =20 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - /* TODO: */ - return 0; + int ret =3D 0; + + switch (mp_state->mp_state) { + case KVM_MP_STATE_RUNNABLE: + vcpu->arch.power_off =3D false; + break; + case KVM_MP_STATE_STOPPED: + kvm_riscv_vcpu_power_off(vcpu); + break; + default: + ret =3D -EINVAL; + } + + return ret; } =20 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, @@ -248,7 +359,51 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) =20 static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu) { - /* TODO: */ + struct swait_queue_head *wq =3D kvm_arch_vcpu_wq(vcpu); + + if (kvm_request_pending(vcpu)) { + if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) { + swait_event_interruptible_exclusive(*wq, + ((!vcpu->arch.power_off) && + (!vcpu->arch.pause))); + + if (vcpu->arch.power_off || vcpu->arch.pause) { + /* + * Awaken to handle a signal, request to + * sleep again later. + */ + kvm_make_request(KVM_REQ_SLEEP, vcpu); + } + } + + if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) + kvm_riscv_reset_vcpu(vcpu); + } +} + +static void kvm_riscv_update_vsip(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; + unsigned long *vsip =3D raw_cpu_ptr(vsip_shadow); + + if (*vsip !=3D csr->vsip) { + csr_write(CSR_VSIP, csr->vsip); + *vsip =3D csr->vsip; + } +} + +int kvm_riscv_setup_vsip(void) +{ + vsip_shadow =3D alloc_percpu(unsigned long); + if (!vsip_shadow) + return -ENOMEM; + + return 0; +} + +void kvm_riscv_cleanup_vsip(void) +{ + free_percpu(vsip_shadow); } =20 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) @@ -311,6 +466,15 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, str= uct kvm_run *run) srcu_read_unlock(&vcpu->kvm->srcu, vcpu->arch.srcu_idx); smp_mb__after_srcu_read_unlock(); =20 + /* + * We might have got VCPU interrupts updated asynchronously + * so update it in HW. + */ + kvm_riscv_vcpu_flush_interrupts(vcpu); + + /* Update VSIP CSR for current CPU */ + kvm_riscv_update_vsip(vcpu); + if (ret <=3D 0 || kvm_request_pending(vcpu)) { vcpu->mode =3D OUTSIDE_GUEST_MODE; @@ -334,6 +498,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, stru= ct kvm_run *run) scause =3D csr_read(CSR_SCAUSE); stval =3D csr_read(CSR_STVAL); =20 + /* Syncup interrupts state with HW */ + kvm_riscv_vcpu_sync_interrupts(vcpu); + /* * We may have taken a host interrupt in VS/VU-mode (i.e. * while executing the guest). This interrupt is still --=20 2.17.1