From patchwork Tue Jul 27 05:54:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 12402009 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 082DEC4338F for ; Tue, 27 Jul 2021 07:23:53 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BBE536103E for ; Tue, 27 Jul 2021 07:23:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BBE536103E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=oSlkDnUUS5QSybYy8/TggnRjSSZhZDKxCxUq5ZYnpxo=; b=jSM3dEYSkor6Tt 7JFxWFxe/hA7XoXwMMJYHIt6YP4WixqhlpUaAM/L9nZUNPB+Gb9EUGXZ6fDIsGzs00V1z0KFGHs5C T1R2kOS+2nydOWUI//+M0YRgCEs+tLqFAI1BcNuWI04RZIS04AMGyjxKmJ13fAH+FQzAFyG3CeuE6 3yz2DZnVikt8yn/0IhGXx7pUDOozH7vt7mCzULqTMRhKeW+n+MV2cxJK91wVaFfcSCLRv6dcdPIK6 /tbL9BYMV2V9APICiFKpQPr8BEsfwp7c8qDHh/E0cTilgVFey41etPI9y50pzSP4vzxetdMqvi+Gs URZcukXDUe16krMpzJmw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m8HR4-00DoHu-DC; Tue, 27 Jul 2021 07:23:30 +0000 Received: from esa3.hgst.iphmx.com ([216.71.153.141]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m8G40-00DUnS-0i; Tue, 27 Jul 2021 05:55:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1627365335; x=1658901335; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=E1C6pXKYdSesDKZ4JQ3FsK4UrJ8weY+QcvvHX5zob2E=; b=dO71Hs4jtEwR143hISwd6r1A8+4Du5aiLnA6R2cRz0Us+Vj5+j8qus0a CKUF1uOgoky9s9RNMUmd+axX55Q41kCWoWVLcbdjrdbp6JBblwiVhB44c WVOSGu+HDwRLonaPFj+lMV+VBwUaNaQZL1yTrqJZIsfYM6PzOSh5skUCv MqmNG6h1jBXO/XZ2isVwirbtfKonHDR3rDAfjvCHPKLeeGF9bURBpF2gE Y/ys7MJ5T4fJ0SwvEqYQocuBv4gOhIQlyqflz4SnjylWv1jl2GUUY2lZW kx6qgomiDDGA0Z8Vp/6bhuBc3YkkGhUvTLqyr55E5Eac7IAb9cwqrxI44 g==; X-IronPort-AV: E=Sophos;i="5.84,272,1620662400"; d="scan'208";a="180385871" Received: from mail-mw2nam12lp2049.outbound.protection.outlook.com (HELO NAM12-MW2-obe.outbound.protection.outlook.com) ([104.47.66.49]) by ob1.hgst.iphmx.com with ESMTP; 27 Jul 2021 13:55:34 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MWjynRb5Prq2qpRJJ4U8ZdhJu9iARnIaTgrqyw4ZtkWJhfdwp6ivYJUgjnZ88muUrqM/1RgVkyLa5Eq6LJUvcXSZPcDY70kuiuuZJzK1W8Q8nIv4hcPp/4NZLnI6cb10+uCMVA15epLT56v3kc7AAuXtB2OLtm/xl6hwu3ukSuVKYjkpCc6OvOsPjrgq6/mnHCppXP36BHQHZ4Huy1e798NiY3+eJWJiehGKG33+ORALxVT2FWmV6jN9IzKp/8/Uu9H2m8tX5Vbcji3O/DacbNFYDlOodftzg+Nx5kXX3NsWR25BgAln+bsnehX1eOvNvo9VS+vZO8NTeQxBeRcRGw== 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=AU+UkWXbvVeqRuf2X19G49X/Gr+tosV5obk26wxLUU8=; b=BGNj6r1NLjgarPrD3VAgNiRpzXVUDN1wtcHIoWpW1BcLTXAEV0y0j4bQGE/pS2bzw2z4EqSmjWLjI7wjPKhuDE/5Bzi+OGGXPUdB7Fhg9S6uf8BX6aOOcvJwETIBqFZTY4GOu0hUhzDn9q2Bx4xrrbO7+t7ula+oCbolS3ghf3zqI6r7Jl8OfwrF8pSLEQsWCU4JeGiRIYmBvg8pyTB//3Kc2sJg8vms+fWmyncxZQTjU/Snep8KdmFP7hwZmiqv9ULUr4euOL51aCE4ZYPrLw5yUlUYMVvHfVXPrmoDSsYZIPWAXjekWeMUYm8tv9I4sLRkphgYwphuu5P5UN894A== 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=AU+UkWXbvVeqRuf2X19G49X/Gr+tosV5obk26wxLUU8=; b=kt+z2Odd18G8AbnGfyH6jCyEuQU+6YtsZmkLH1kNVAcZ+P1NilQZ4Kiiwe2bhih1uokJw7PqoHTyhTZKOS3CoWCivRcUFzo3ZhB0970R5Wjz1rMY+I4KXMD966yni7ATa5SJrh9+yeGFIfQF4JnvamfUwY4oZeGnOE31I/P8Ow0= Authentication-Results: dabbelt.com; dkim=none (message not signed) header.d=none;dabbelt.com; dmarc=none action=none header.from=wdc.com; Received: from CO6PR04MB7812.namprd04.prod.outlook.com (2603:10b6:303:138::6) by CO6PR04MB8377.namprd04.prod.outlook.com (2603:10b6:303:140::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.29; Tue, 27 Jul 2021 05:55:34 +0000 Received: from CO6PR04MB7812.namprd04.prod.outlook.com ([fe80::a153:b7f8:c87f:89f8]) by CO6PR04MB7812.namprd04.prod.outlook.com ([fe80::a153:b7f8:c87f:89f8%8]) with mapi id 15.20.4352.031; Tue, 27 Jul 2021 05:55:34 +0000 From: Anup Patel To: Palmer Dabbelt , Palmer Dabbelt , Paul Walmsley , Albert Ou , Paolo Bonzini Cc: Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel Subject: [PATCH v19 04/17] RISC-V: KVM: Implement VCPU interrupts and requests handling Date: Tue, 27 Jul 2021 11:24:37 +0530 Message-Id: <20210727055450.2742868-5-anup.patel@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210727055450.2742868-1-anup.patel@wdc.com> References: <20210727055450.2742868-1-anup.patel@wdc.com> X-ClientProxiedBy: MA1PR0101CA0030.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:22::16) To CO6PR04MB7812.namprd04.prod.outlook.com (2603:10b6:303:138::6) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (122.171.179.229) by MA1PR0101CA0030.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:22::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.26 via Frontend Transport; Tue, 27 Jul 2021 05:55:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a5390c98-2792-483f-0e5a-08d950c32664 X-MS-TrafficTypeDiagnostic: CO6PR04MB8377: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: f+L14vK3CKZbmk9HEHJZpXgA3jUnm+Ri9UUQqa8Hjt5y41hD1mDJI7nZUw92Z60tzRubMnPe3/VXovyEajSDTICFkN4rUFPHmy/ZYrWELwAj+fawStKujSMe2GXiZVxN940l7H1Uu0ynAgGH4Kkqd43A+7UOUhTDnngP4EhOVoOiSiGGXJtW4ewBczPPiokvsCCP92LiIcnQ+7HF6uyh6a+yXx3ZtT651Vtq2hfoOhoa21HIeQxK2gmVm7ZWQqEBGItipUwskXT0z29SqQc7sDv3oMdBXT8QaYV3oqCVqkkGB0DcyYS1XftiZQ+GNeYcf1H4C/9ZWCk+Hv8IGXdIFMz8cHohga9BDmaB3U4azRBf8CkvLgwbsyo+MHI+XrfVc87mOz81DL4qlATcHSi3sZeTqhwxA0oXAsdk7n1j9JL8E2LCZSXjykP1c+aMglHNPMOx9bBg9kXGNu6mTvI32eIJ882G5MLqqYUTjvt+Ws+sO7WCuXqysRIcZM9+cE66pKH1Q7VA3HdA+0P/LRyUxH9xoZwsGzsfKrW+vD1RTFJYl5yaZx96uvre+yAWYggaXTOmFcmgVfPzyaovhw1K8zh65ZQtsInC0GYPoTtRMO9py6S0i2RKHHTRfpu4465Fuz/1yPuYMEftYkxzWzQrK8jh+/1XwDpyWiCz0t/ilArCxjtmhAFiKiW5IIs06XJC X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CO6PR04MB7812.namprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(7696005)(52116002)(26005)(66946007)(54906003)(5660300002)(956004)(2616005)(8936002)(186003)(38100700002)(316002)(36756003)(508600001)(8886007)(7416002)(66476007)(2906002)(1076003)(110136005)(86362001)(44832011)(83380400001)(8676002)(4326008)(66556008)(55016002)(38350700002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mN6FY7JHwNKgvFQIubL/N4TgqujDNO95n3f18yIjyMHDCG4IuDSbhGSogSJrYWIul6Re/9L5oPz3z3aYkUaiss/Bd7Ma/x9OM4FI6vDAro+mBLwW1MLM+Kxs0+mm5WYOycBS/X+Zavt9KZa+QIw/VUa+0Gz1f4kyUS6Vjnx9fPvwUEGHA58WTvi5OD6fyZHSMpoLDRDeAvzw+wsbRvb5zPFT5L+eo0JO3Ccb9dVxv72Vf4yvdyUMbOZ5xlV4G+g2qgAUpTNZdOWAvdNDTdeB4EGu5+njB65DYPqBRVqkH3j9h7dQd4HpzCa6ks+/MWhO1gPyNIa6GHeW0Pq38J61j89dA9+HiZlFB4XPs9JVfVrEUDSf58qekKIVBMj7La9q/6BQwpDCqEztAbfF6TiHdch5YbV0VmuvT01eXhNEIIfMpqqciswZO4EIpixjEhrOsCJNFBO+E5cIHrcSId0nmtKLYm8bBjIbbOfFij3ofFDhXlhH+zH/AYcJ3BXLQ+UytN+bPk1a5NyHnjhtNsBCkgtANybr7ScaA+Wl163OK4xCgnYBgqO5ZoOsx3Ia8U4E7Nhd0axoLDx8zG415M6bhBsKLoIJ+D4pqREZC9aQkI5XXAt9CW5tOi84cd9KIyai7GeXaJHnu1JgX6VkN5xG5gRBWI1J7qplVAHGRcxmxHHXVo1GDD7+/1sVUfD0rq7hEE6Xhm1dC1b8LmL8XG1/gcGOdKTQEq7vKpDsZTfUzNXvl1GZIoSpzV7PtRQZre3p/23B+q+UCAoP/9We09+fC9TjqJwv/ue0DnbUrVzHj17ICo2zxHlqZ5KoOFv7rABEGCwiKPO6ike8i09FYEkZrSQKPPMKYDk2qCGTiMC9f/5NL6ns1Cq0y5+ppJqj0aHC17JV/Qc5D0rrQbmFr4oxvfY/ck4pDVK8tY2T0SU9fvhvdgyMZHudEC6REJ0OYsEwAUy1ZPV3HJl/EuOSKIZcDNqbcX6UKw4oJMo2/Bh9Qe6RMhQfPovaCofNwFyEh9iusvhvNni/5Ayw1J9KMukL9hsqDb7sqB2gt78YtF9QsjrvxYCm/8m/VhsRfLGOhvZzF1nqIKJyFE1N8yIlovokWJ04ZOSFTogl1dlC/NRQtGdS8ezPR4p/pFv1va/0icwWIcWw2AtyS+S5rNPChQADVf9ODAwgOIZeYbgTiW+biClb03P5RWlMa1ttfWauqSsmSh61VtXOlqi9msxvvjI0Xr7wINIS7gfJMMT8+XRO2lUIUklQvFj7aXxIvnKpgPSw7hNijzMsc/Cuf9GHidpU9IjpK28Y9NORPrVUNbDEEBF/zerPtQdkXzzQXEtzyqO4 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: a5390c98-2792-483f-0e5a-08d950c32664 X-MS-Exchange-CrossTenant-AuthSource: CO6PR04MB7812.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2021 05:55:34.3806 (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: yIO2ZrcqzPWWT2OvHTobjsQ6akXd5rNQ6gUDGHnacZrJvZkRGxmNyIZbpX/SCzmn9nA++GeGky2GR1WGVjFbCQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR04MB8377 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210726_225536_143597_6FE324AD X-CRM114-Status: GOOD ( 21.38 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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 | 23 ++++ arch/riscv/include/uapi/asm/kvm.h | 3 + arch/riscv/kvm/vcpu.c | 182 +++++++++++++++++++++++++++--- 3 files changed, 195 insertions(+), 13 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 5fd969bd3bfd..87871b11d8ec 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -127,6 +127,21 @@ struct kvm_vcpu_arch { /* CPU CSR context upon Guest VCPU reset */ struct kvm_vcpu_csr guest_reset_csr; + /* + * 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; @@ -150,4 +165,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, static inline void __kvm_riscv_switch_to(struct kvm_vcpu_arch *vcpu_arch) {} +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_interrupts(struct kvm_vcpu *vcpu, unsigned long mask); +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/asm/kvm.h index 984d041a3e3b..3d3d703713c6 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -18,6 +18,9 @@ #define KVM_COALESCED_MMIO_PAGE_OFFSET 1 +#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/vcpu.c b/arch/riscv/kvm/vcpu.c index eef6b772c6e2..b6993108f377 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,9 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) memcpy(csr, reset_csr, sizeof(*csr)); memcpy(cntx, reset_cntx, sizeof(*cntx)); + + WRITE_ONCE(vcpu->arch.irqs_pending, 0); + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); } int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) @@ -102,8 +106,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return kvm_riscv_vcpu_has_interrupts(vcpu, 1UL << IRQ_VS_TIMER); } void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) @@ -116,20 +119,18 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return (kvm_riscv_vcpu_has_interrupts(vcpu, -1UL) && + !vcpu->arch.power_off && !vcpu->arch.pause); } int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; } bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { - /* TODO: */ - return false; + return (vcpu->arch.guest_context.sstatus & SR_SPP) ? true : false; } vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) @@ -140,7 +141,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 = filp->private_data; + void __user *argp = (void __user *)arg; + + if (ioctl == KVM_INTERRUPT) { + struct kvm_interrupt irq; + + if (copy_from_user(&irq, argp, sizeof(irq))) + return -EFAULT; + + if (irq.irq == KVM_INTERRUPT_SET) + return kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_VS_EXT); + else + return kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_VS_EXT); + } + return -ENOIOCTLCMD; } @@ -189,18 +204,121 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) return -EINVAL; } +void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + unsigned long mask, val; + + if (READ_ONCE(vcpu->arch.irqs_pending_mask)) { + mask = xchg_acquire(&vcpu->arch.irqs_pending_mask, 0); + val = READ_ONCE(vcpu->arch.irqs_pending) & mask; + + csr->hvip &= ~mask; + csr->hvip |= val; + } +} + +void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) +{ + unsigned long hvip; + struct kvm_vcpu_arch *v = &vcpu->arch; + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + /* Read current HVIP and HIE CSRs */ + hvip = csr_read(CSR_HVIP); + csr->hie = csr_read(CSR_HIE); + + /* Sync-up HVIP.VSSIP bit changes does by Guest */ + if ((csr->hvip ^ hvip) & (1UL << IRQ_VS_SOFT)) { + if (hvip & (1UL << IRQ_VS_SOFT)) { + if (!test_and_set_bit(IRQ_VS_SOFT, + &v->irqs_pending_mask)) + set_bit(IRQ_VS_SOFT, &v->irqs_pending); + } else { + if (!test_and_set_bit(IRQ_VS_SOFT, + &v->irqs_pending_mask)) + clear_bit(IRQ_VS_SOFT, &v->irqs_pending); + } + } +} + +int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq) +{ + if (irq != IRQ_VS_SOFT && + irq != IRQ_VS_TIMER && + irq != IRQ_VS_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 != IRQ_VS_SOFT && + irq != IRQ_VS_TIMER && + irq != IRQ_VS_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_interrupts(struct kvm_vcpu *vcpu, unsigned long mask) +{ + return (READ_ONCE(vcpu->arch.irqs_pending) & + vcpu->arch.guest_csr.hie & mask) ? true : false; +} + +void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu) +{ + vcpu->arch.power_off = 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 = 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 = KVM_MP_STATE_STOPPED; + else + mp_state->mp_state = KVM_MP_STATE_RUNNABLE; + return 0; } int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - /* TODO: */ - return 0; + int ret = 0; + + switch (mp_state->mp_state) { + case KVM_MP_STATE_RUNNABLE: + vcpu->arch.power_off = false; + break; + case KVM_MP_STATE_STOPPED: + kvm_riscv_vcpu_power_off(vcpu); + break; + default: + ret = -EINVAL; + } + + return ret; } int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, @@ -224,7 +342,33 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu) { - /* TODO: */ + struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); + + if (kvm_request_pending(vcpu)) { + if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) { + rcuwait_wait_event(wait, + (!vcpu->arch.power_off) && (!vcpu->arch.pause), + TASK_INTERRUPTIBLE); + + 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_hvip(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + csr_write(CSR_HVIP, csr->hvip); } int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) @@ -288,6 +432,15 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) srcu_read_unlock(&vcpu->kvm->srcu, vcpu->arch.srcu_idx); smp_mb__after_srcu_read_unlock(); + /* + * We might have got VCPU interrupts updated asynchronously + * so update it in HW. + */ + kvm_riscv_vcpu_flush_interrupts(vcpu); + + /* Update HVIP CSR for current CPU */ + kvm_riscv_update_hvip(vcpu); + if (ret <= 0 || kvm_request_pending(vcpu)) { vcpu->mode = OUTSIDE_GUEST_MODE; @@ -315,6 +468,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) trap.htval = csr_read(CSR_HTVAL); trap.htinst = csr_read(CSR_HTINST); + /* 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