Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp5400038ybh; Wed, 7 Aug 2019 05:29:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqxd386xUC6e6IQm1pk8s0RZm6wbWrraeK+r1UnPLS6pR0Zl8OOQR/C3EboTm9skhmmNbTAM X-Received: by 2002:a65:64cf:: with SMTP id t15mr7355195pgv.88.1565180995147; Wed, 07 Aug 2019 05:29:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1565180995; cv=pass; d=google.com; s=arc-20160816; b=AnF8vEebJtjQJ6kB6EuHamDGWNJa+febsfV+QRoWn/nTwrR3jKUGH06HQQ63bfWuzy DvmpXGi2xI43d4o3ODpkxHZKWdGyaSxIitZDXYrASU/gs3dbFi1tzQdACrbuND9BGrdZ I2qCNCIlBtEiSC9NfamHzUxxgg1K0Iaj18BHQxilHX6BF8J6TJS7P80bnQIpZmKc0wQD OvW3/7IrAxvcZrb2UyRZQ1h8u9gY8H7KtwHfponJ2S1kGMpTKkJsEtkzDCIoz7/FJpCN KKrUnwtaXLjF3A6AX3JBhhyzjW4MqYx7JLRhqgYM+xNtFJtP+qKe2PO2rh2Anau/L6Tf nbIg== 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=3bM57xL/ucrcUkCHbRYZSIniavXwECEzkj11N/0NWn4=; b=jYpJSfUeIB1Lgx4WlT2krOke2/VumBZJmoI1vxH1TGs7pezT16DfqYnFsPN6UNYg0h qH8Jhnc5TiGCi9Ly944+3/1vlDwd4XZxgvJEE5+I1xf9IK9hfggiQvKJl+l/l7anuxAe okPIXgQdqQaNNprkFMbV+WoTdXQQ6wzAymnjn1R/rs0rP7zsCbKW/db2m2dEDx6D8Xl6 BvncT90rk5s0dA/n7UaWQBgB5Txn/yf8Bq9AgYOXkLgwawoKLO0mkOhFE6WxIuhLNodm shO6MUoze1WtpRrnMmg17wFGnWAxcxcP1TeGpM6Kj+xk/xeCh+fJg2yJNZzs7HmidMrv cM4g== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=JTi7TZOW; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=IScnUskU; 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 q7si16963152pjv.36.2019.08.07.05.29.39; Wed, 07 Aug 2019 05:29:55 -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=JTi7TZOW; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=IScnUskU; 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 S2387884AbfHGM2g (ORCPT + 99 others); Wed, 7 Aug 2019 08:28:36 -0400 Received: from esa3.hgst.iphmx.com ([216.71.153.141]:37760 "EHLO esa3.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726873AbfHGM2e (ORCPT ); Wed, 7 Aug 2019 08:28:34 -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=1565180914; x=1596716914; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=kFhMXG3pkVtZJrYb6d+3l3iexTxc96m2ja4M6W7ep9A=; b=JTi7TZOW5oQ4UYz4BRulFe7JxIddgXiuXoJGun3NG0C9BDMZ+9rb3jGq PsHzeiEg5nwFiqd0q/oreJkx8ubtXlBdxSzFlDGvglPfLtr0KiBydxVWg E5z4jWVGyxYFyC2blRR4EwwAqKyhV/ZmYUCYzxIdi3XLmIqecknEbbiJz 3ZpGlUyOPvBZl2W+RF7QhkEsD4rXzMXj8FKG4QTfG6kX2IQl9GnUcK9EM t4tFx5n63a/bqSGuib/iULf5MEdDLNdpoRLtVn+BKPG1MYN+KyyuUXpEu BXxoCuh8IP8FBKI5Zz9nPs6v0+2x79AkII639fM3+QfQvRaQ1VuXdOkb4 Q==; IronPort-SDR: mySy6OCA49cuBdaokwjgTi0UhiKSo7feIwRg/p3YaF3aSwH9+hfZSG6o77vdCQ9Bni4FUa2Cil 6dI5C8OKs2CdlpRYPlVMoYxsw8vT40Py+Hr1W3Nu5Zm6VrrCVdh0+PwXwSM7CeuHmeZA46ksXE KcYfUNOK1b1yRb7ISM10bevF7rWhQ93lw8DxLctpazeVTHdqkG1lgYLBT9UUDWk8iOZsN+33I2 Yj/ukA1ShAuxcT61gvJWPyAiIa1Cvo9Q2CxNeJTn72cbYsqzgEiLTuExvC6BymVfHT6f4/duze VOs= X-IronPort-AV: E=Sophos;i="5.64,357,1559491200"; d="scan'208";a="119865531" Received: from mail-co1nam05lp2050.outbound.protection.outlook.com (HELO NAM05-CO1-obe.outbound.protection.outlook.com) ([104.47.48.50]) by ob1.hgst.iphmx.com with ESMTP; 07 Aug 2019 20:28:33 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c6PzMaEGYNJxGaId+oYvGPvB4KpoB25NEyAU2p2cNNeHZsbSNlkX39Ka3b9sVCtCHjbgZv4fyNON1y8qFcVjzxeSidUpmsvEkbU8+GkrmbzDjH7DPZklDUpBNf/ElWlO0FwBg04/lFHrq3ykWA3CWl/YI1QuvOB6pkMwx0GCHgedKnOkhMYUIu5SC6lH/kWwsSbOTsTnMSIWLJDeBcKVb/7V5eIb1XTZKyFQTpJpx+ZQ9JlySgzUeb7jRRnfeteJpSccJmroTUuk/78vuQ5M/dABiP4nEmGORbcqv5ID/aBslJ8V0/uRtFoX7j3tyyj6pB3KZ618uFwm0XRlmGUmYA== 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=3bM57xL/ucrcUkCHbRYZSIniavXwECEzkj11N/0NWn4=; b=WZLQe40HJ7UiXC7dK61UizBCOeX774Xu6u8RVAr/ZaD+gEEUkyq2HCxjEAGTwjqv5evkCPNDcO9YI25eCLdp3DRnXMpkB5PExc/7LEVcer7Zb/Cua9XTtdS6Bcr+dkhWse6Ci81d+Df108QM9Dbp//OTiHTypn9LyB2baw2VY5Ktm9QXWtzADk+6LtJ/49gsloi/Hb2cULxkdhG+aF2gJ5KwDe2hisfZKtvjceucA0M1pq+JEjEQ5kNwOBBcuEIcUcDz0vNpEvhGIwoabheFvHwAB32j1PxEDF3rZ82U+952tyLLj1lHWpHqgWB9XrKRVgd5/6DlbxchjVyPLiNXCw== 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=3bM57xL/ucrcUkCHbRYZSIniavXwECEzkj11N/0NWn4=; b=IScnUskUDp7eKxFcfoVAxEBTq6afStsiEvq58GFGToK6/g/bfcmNHDyAXMY1+5VcnNjnlQ/2E2BYmiMJ/NuWFqvR/+wDJhqZJMb1lfcqX5AeTGCz6Gbcgcw4K/dz7JiKuatvy6iJc7AL9TNT/a9g1iHDzBjLK7VkGnI9rqhbTpg= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB6736.namprd04.prod.outlook.com (10.141.117.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2136.17; Wed, 7 Aug 2019 12:28:31 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::a815:e61a:b4aa:60c8]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::a815:e61a:b4aa:60c8%7]) with mapi id 15.20.2157.015; Wed, 7 Aug 2019 12:28:31 +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 v4 06/20] RISC-V: KVM: Implement VCPU create, init and destroy functions Thread-Topic: [PATCH v4 06/20] RISC-V: KVM: Implement VCPU create, init and destroy functions Thread-Index: AQHVTRufabDzARcYWUqhjaAV7o/wXw== Date: Wed, 7 Aug 2019 12:28:31 +0000 Message-ID: <20190807122726.81544-7-anup.patel@wdc.com> References: <20190807122726.81544-1-anup.patel@wdc.com> In-Reply-To: <20190807122726.81544-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: PN1PR01CA0097.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c00::13) 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.52.255] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 0a09c8c6-f838-472a-658c-08d71b32c233 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:MN2PR04MB6736; x-ms-traffictypediagnostic: MN2PR04MB6736: x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:454; x-forefront-prvs: 01221E3973 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(979002)(4636009)(396003)(366004)(136003)(376002)(346002)(39860400002)(199004)(189003)(386003)(1076003)(3846002)(446003)(86362001)(186003)(26005)(11346002)(76176011)(52116002)(36756003)(54906003)(6506007)(102836004)(55236004)(2616005)(478600001)(44832011)(66066001)(110136005)(7416002)(476003)(486006)(8676002)(6436002)(6486002)(6512007)(4326008)(81166006)(81156014)(316002)(66476007)(66556008)(305945005)(68736007)(2906002)(53936002)(25786009)(256004)(5660300002)(14454004)(50226002)(7736002)(71190400001)(99286004)(66446008)(6116002)(64756008)(8936002)(71200400001)(66946007)(14444005)(969003)(989001)(999001)(1009001)(1019001);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB6736;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-message-info: JSmyUoDoZdVVSyI2MIk4xLYvr8BxdekjuqfMDfFKaR86MWjzgjdDVhw/I71YO6wM/uNpfqcPmerRmAZmRZ4yF+9Ptqf/bfqylRbjj1PLeodiX/bL3FgbJzICTURNJNGyzl5iGS9Lkq2yzUnCbOJvMzPObo0UDdUPa1gxWf31IXaJr/9lnzXbyxy9NYSNoiJXPi2K/lqvGfAbEgigaIMLPRa6q4r4tJYV4ufouu2J3CyF3M1nZLmH/ISZRG6R1leXJKLjGvAzT1GteMeMP0n+Zmps9/p49fXb0WIrMApJKkmj9Rk3T8Ti1AEFZDIUGhviDV0sPrHEGBMJcr0h+ld+6ZdgkkGQzVQ9oxdaiuVAJ3UfU3y6NNKI9y+pOyC9yDEVy4ApFk7J3qrwxKS74cUyWcBPRB3HCYV57UAtqcmdFjY= 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: 0a09c8c6-f838-472a-658c-08d71b32c233 X-MS-Exchange-CrossTenant-originalarrivaltime: 07 Aug 2019 12:28:31.8666 (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: x6ZgdHsHBa2FukX/kd5/Hux1ZlYBcfN2ax4PacF9XQbrmssF5hWjcHA759kxgD+Rr9zLmrJhSFgqPKdKFMULgQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6736 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implements VCPU create, init and destroy functions required by generic KVM module. We don't have much dynamic resources in struct kvm_vcpu_arch so thest functions are quite simple for KVM RISC-V. Signed-off-by: Anup Patel --- arch/riscv/include/asm/kvm_host.h | 68 +++++++++++++++++++++++++++++++ arch/riscv/kvm/vcpu.c | 68 +++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 4 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index 9459709656be..dab32c9c3470 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -53,7 +53,75 @@ struct kvm_arch { phys_addr_t pgd_phys; }; =20 +struct kvm_cpu_context { + unsigned long zero; + unsigned long ra; + unsigned long sp; + unsigned long gp; + unsigned long tp; + unsigned long t0; + unsigned long t1; + unsigned long t2; + unsigned long s0; + unsigned long s1; + unsigned long a0; + unsigned long a1; + unsigned long a2; + unsigned long a3; + unsigned long a4; + unsigned long a5; + unsigned long a6; + unsigned long a7; + unsigned long s2; + unsigned long s3; + unsigned long s4; + unsigned long s5; + unsigned long s6; + unsigned long s7; + unsigned long s8; + unsigned long s9; + unsigned long s10; + unsigned long s11; + unsigned long t3; + unsigned long t4; + unsigned long t5; + unsigned long t6; + unsigned long sepc; + unsigned long sstatus; + unsigned long hstatus; +}; + +struct kvm_vcpu_csr { + unsigned long vsstatus; + unsigned long vsie; + unsigned long vstvec; + unsigned long vsscratch; + unsigned long vsepc; + unsigned long vscause; + unsigned long vstval; + unsigned long vsip; + unsigned long vsatp; +}; + struct kvm_vcpu_arch { + /* VCPU ran atleast once */ + bool ran_atleast_once; + + /* ISA feature bits (similar to MISA) */ + unsigned long isa; + + /* CPU context of Guest VCPU */ + struct kvm_cpu_context guest_context; + + /* CPU CSR context of Guest VCPU */ + struct kvm_vcpu_csr guest_csr; + + /* CPU context upon Guest VCPU reset */ + struct kvm_cpu_context guest_reset_context; + + /* CPU CSR context upon Guest VCPU reset */ + struct kvm_vcpu_csr guest_reset_csr; + /* Don't run the VCPU (blocked) */ bool pause; =20 diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 48536cb0c8e7..8272b05d6ce4 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -31,10 +31,48 @@ struct kvm_stats_debugfs_item debugfs_entries[] =3D { { NULL } }; =20 +#define KVM_RISCV_ISA_ALLOWED (riscv_isa_extension_mask(a) | \ + riscv_isa_extension_mask(c) | \ + riscv_isa_extension_mask(d) | \ + riscv_isa_extension_mask(f) | \ + riscv_isa_extension_mask(i) | \ + riscv_isa_extension_mask(m) | \ + riscv_isa_extension_mask(s) | \ + riscv_isa_extension_mask(u)) + +static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; + struct kvm_vcpu_csr *reset_csr =3D &vcpu->arch.guest_reset_csr; + struct kvm_cpu_context *cntx =3D &vcpu->arch.guest_context; + struct kvm_cpu_context *reset_cntx =3D &vcpu->arch.guest_reset_context; + + memcpy(csr, reset_csr, sizeof(*csr)); + + memcpy(cntx, reset_cntx, sizeof(*cntx)); +} + struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) { - /* TODO: */ - return NULL; + int err; + struct kvm_vcpu *vcpu; + + vcpu =3D kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); + if (!vcpu) { + err =3D -ENOMEM; + goto out; + } + + err =3D kvm_vcpu_init(vcpu, kvm, id); + if (err) + goto free_vcpu; + + return vcpu; + +free_vcpu: + kmem_cache_free(kvm_vcpu_cache, vcpu); +out: + return ERR_PTR(err); } =20 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) @@ -48,13 +86,32 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) =20 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) { - /* TODO: */ + struct kvm_cpu_context *cntx; + + /* Mark this VCPU never ran */ + vcpu->arch.ran_atleast_once =3D false; + + /* Setup ISA features available to VCPU */ + vcpu->arch.isa =3D riscv_isa_extension_base(NULL) & KVM_RISCV_ISA_ALLOWED= ; + + /* Setup reset state of shadow SSTATUS and HSTATUS CSRs */ + cntx =3D &vcpu->arch.guest_reset_context; + cntx->sstatus =3D SR_SPP | SR_SPIE; + cntx->hstatus =3D 0; + cntx->hstatus |=3D HSTATUS_SP2V; + cntx->hstatus |=3D HSTATUS_SP2P; + cntx->hstatus |=3D HSTATUS_SPV; + + /* Reset VCPU */ + kvm_riscv_reset_vcpu(vcpu); + return 0; } =20 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) { - /* TODO: */ + kvm_riscv_stage2_flush_cache(vcpu); + kmem_cache_free(kvm_vcpu_cache, vcpu); } =20 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) @@ -199,6 +256,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, stru= ct kvm_run *run) int ret; unsigned long scause, stval; =20 + /* Mark this VCPU ran atleast once */ + vcpu->arch.ran_atleast_once =3D true; + vcpu->arch.srcu_idx =3D srcu_read_lock(&vcpu->kvm->srcu); =20 /* Process MMIO value returned from user-space */ --=20 2.17.1