Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4138230pxv; Mon, 26 Jul 2021 22:57:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHx1T1/5m6541COLHtTkGspvk1VgXKwMFwrUv9GTVeA9vB8Up46J00aNOEWA9Yu/ulABPM X-Received: by 2002:a05:6402:1d37:: with SMTP id dh23mr25080826edb.360.1627365467682; Mon, 26 Jul 2021 22:57:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1627365467; cv=pass; d=google.com; s=arc-20160816; b=DMBEpscj0LpKpCaYbwE/+hFTyHAE8r+CaE8tRPcK7e9zxBS7R8HoN71bl3ga/E4IZr CXVeuWB3sPB1IkWuMu3XrDo1rUE6v9gpa6ZdN9HgR2tQ+eQocp7q0hoc9DWUAM3xj0HE Bxj4hKBYCAAi31jKuYCRNOfN+nPAg33ALMC1UfKGQNMBXjsY1MFFe2lSpQrFVa0Pggoy RG6x9Jkoq4vJtfF8aNmMV/8JiofUU3k9bwdOjk9dw32HSVPtX+QnlbOufPQswQuzM01+ NVoobiOZhjTkWP+CjUphbHx6viALhSXPJ/lg2I0Og8xDSZElv+6r5TPZ3kkAkm9TLUsp j5XA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:wdcipoutbound:mime-version :content-transfer-encoding:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-signature; bh=AU+UkWXbvVeqRuf2X19G49X/Gr+tosV5obk26wxLUU8=; b=C+yWwhYtPgJCNTUyX5ZhpWHhUJgRaJcXcix0Zrp8cO2jhfCmKZRy+v5s8xSGBYoLIx D8NXF/fG3PWKpoN9wq5Sre12IlfuOvfOVtkOj91gqjoKLVEN0eHm/kZeu7BjF+LK+1wG +ahnnGXC07EHyhW++Gy6ZWYc0vlHpxBSHO9JospC9w9r1WBfXJym/fMFoyhUpHkms308 tpDDfLxcpONjl99SV5UtHdYbi/z2CUkAqDWB6xNqSrdngUZjTz+O16NsMudu4xBoqPrS k60L5+3z8fSYiCJFzvPHvFHk0s4ZwRwS/eoVFuEJvu0Y1bnr4+go17TTRvmjZ14anV3d r1sg== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=dO71Hs4j; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=kt+z2Odd; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id lj22si2034639ejb.18.2021.07.26.22.57.24; Mon, 26 Jul 2021 22:57:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=dO71Hs4j; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=kt+z2Odd; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S235554AbhG0Fzs (ORCPT + 99 others); Tue, 27 Jul 2021 01:55:48 -0400 Received: from esa3.hgst.iphmx.com ([216.71.153.141]:33030 "EHLO esa3.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235486AbhG0Fzf (ORCPT ); Tue, 27 Jul 2021 01:55:35 -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=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> Content-Transfer-Encoding: 8bit Content-Type: text/plain 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: =?us-ascii?Q?mN6FY7JHwNKgvFQIubL/N4TgqujDNO95n3f18yIjyMHDCG4IuDSbhGSogSJr?= =?us-ascii?Q?YWIul6Re/9L5oPz3z3aYkUaiss/Bd7Ma/x9OM4FI6vDAro+mBLwW1MLM+Kxs?= =?us-ascii?Q?0+mm5WYOycBS/X+Zavt9KZa+QIw/VUa+0Gz1f4kyUS6Vjnx9fPvwUEGHA58W?= =?us-ascii?Q?Tvi5OD6fyZHSMpoLDRDeAvzw+wsbRvb5zPFT5L+eo0JO3Ccb9dVxv72Vf4yv?= =?us-ascii?Q?dyUMbOZ5xlV4G+g2qgAUpTNZdOWAvdNDTdeB4EGu5+njB65DYPqBRVqkH3j9?= =?us-ascii?Q?h7dQd4HpzCa6ks+/MWhO1gPyNIa6GHeW0Pq38J61j89dA9+HiZlFB4XPs9JV?= =?us-ascii?Q?fVrEUDSf58qekKIVBMj7La9q/6BQwpDCqEztAbfF6TiHdch5YbV0VmuvT01e?= =?us-ascii?Q?XhNEIIfMpqqciswZO4EIpixjEhrOsCJNFBO+E5cIHrcSId0nmtKLYm8bBjIb?= =?us-ascii?Q?bOfFij3ofFDhXlhH+zH/AYcJ3BXLQ+UytN+bPk1a5NyHnjhtNsBCkgtANybr?= =?us-ascii?Q?7ScaA+Wl163OK4xCgnYBgqO5ZoOsx3Ia8U4E7Nhd0axoLDx8zG415M6bhBsK?= =?us-ascii?Q?LoIJ+D4pqREZC9aQkI5XXAt9CW5tOi84cd9KIyai7GeXaJHnu1JgX6VkN5xG?= =?us-ascii?Q?5gRBWI1J7qplVAHGRcxmxHHXVo1GDD7+/1sVUfD0rq7hEE6Xhm1dC1b8LmL8?= =?us-ascii?Q?XG1/gcGOdKTQEq7vKpDsZTfUzNXvl1GZIoSpzV7PtRQZre3p/23B+q+UCAoP?= =?us-ascii?Q?/9We09+fC9TjqJwv/ue0DnbUrVzHj17ICo2zxHlqZ5KoOFv7rABEGCwiKPO6?= =?us-ascii?Q?ike8i09FYEkZrSQKPPMKYDk2qCGTiMC9f/5NL6ns1Cq0y5+ppJqj0aHC17JV?= =?us-ascii?Q?/Qc5D0rrQbmFr4oxvfY/ck4pDVK8tY2T0SU9fvhvdgyMZHudEC6REJ0OYsEw?= =?us-ascii?Q?AUy1ZPV3HJl/EuOSKIZcDNqbcX6UKw4oJMo2/Bh9Qe6RMhQfPovaCofNwFyE?= =?us-ascii?Q?h9iusvhvNni/5Ayw1J9KMukL9hsqDb7sqB2gt78YtF9QsjrvxYCm/8m/VhsR?= =?us-ascii?Q?fLGOhvZzF1nqIKJyFE1N8yIlovokWJ04ZOSFTogl1dlC/NRQtGdS8ezPR4p/?= =?us-ascii?Q?pFv1va/0icwWIcWw2AtyS+S5rNPChQADVf9ODAwgOIZeYbgTiW+biClb03P5?= =?us-ascii?Q?RWlMa1ttfWauqSsmSh61VtXOlqi9msxvvjI0Xr7wINIS7gfJMMT8+XRO2lUI?= =?us-ascii?Q?UklQvFj7aXxIvnKpgPSw7hNijzMsc/Cuf9GHidpU9IjpK28Y9NORPrVUNbDE?= =?us-ascii?Q?EBF/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 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 | 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 -- 2.25.1