Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6295603ybl; Mon, 23 Dec 2019 03:38:49 -0800 (PST) X-Google-Smtp-Source: APXvYqyviyNGoTGpKbapDhumzGZnF7VvX3tB+BibVtJLfYQ5B4T0M25QI5NlAtHBWrvR9+cHGGvv X-Received: by 2002:a05:6830:20c6:: with SMTP id z6mr30472218otq.272.1577101128982; Mon, 23 Dec 2019 03:38:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1577101128; cv=pass; d=google.com; s=arc-20160816; b=eNKVaJuFk5swc19caGGImfCk3YTjyANzpnHn1MWRqjm3fgrthsYWDL40Dgzr7Rkkg+ 6a2AA6etjmKRN07/wSN6TIVV6Tsj0UNy7h7LkXvQOp1VcqToGK2Rq+QLfOt7Hnb5fzA/ gdsKgOkBLLK9zuQbfNNcbxgnBcxCUldMi3yNjzMimplrbIDHWH9u+asocv1xOy1YkSmf mlk3ctFeXUivp4nLlc32aNmm0U37g7LTDZHLeQnIu06mmwBYtDddExO7BX1pJE//6JW/ mHp6pkeHlzztuvB08sEFMUAO34xVuP4vpWm9cjZXsDU7t+JWoZWoEpvkim9gA9JlSrOm bxtg== 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=tPO8PM5BCzJDnQ9EOciNMlleCS1DQrxPQFo5NSbkaZ4=; b=cphiTbil6KSOj6/1e8pF0Lh4DkJDlPwNt4S34J09CxgJLJFiebqEOjj+zfyW73/H8b kJH/PGc7tOHn644/gFqMWypk4zkUpqXMNMg872oLAPvuRdtxIhwLBt8mSj0FnxqQBG1L wi3wKhmU8F2sNp5qbzj0JATta2LnRsEYI+UEEmRLO/9N2pIORentFroJ1nEgGK/2sdwi z6RcFO0mrleKmoXoMYXSKZl2tXI6sIDzhQz9zQJSQTAUz6joAd9/Oeq254SACDUXzALU bL51MMrBFR6Ev+kdQ6NmzfGg3ZujBaV60hh14WZC8Pz81UjVU4DgUoveuGe5Tf42Wfwn 8o5A== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=iDzfQ6ds; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=aegKboYP; 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 k7si8114290otb.90.2019.12.23.03.38.37; Mon, 23 Dec 2019 03:38:48 -0800 (PST) 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=iDzfQ6ds; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=aegKboYP; 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 S1727462AbfLWLgt (ORCPT + 99 others); Mon, 23 Dec 2019 06:36:49 -0500 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:12767 "EHLO esa5.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726860AbfLWLgt (ORCPT ); Mon, 23 Dec 2019 06:36:49 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1577101009; x=1608637009; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=PTsMROgmwzMsMFghlFvp9MJ38GWZfim4BJuLMYuiK+Y=; b=iDzfQ6dsPvsqu5DLlPhBsdelNLIn/Ttwc4EGYgpKXdeO9phQg6Ap9xru R3a/2pJh2gMg4CxXftnTg/RrILi823YTtxfBK1g32jEQzuzsMwRb3pw61 wv634k51HvbAykR0bqrB1AeIlX4/FMsmF0foGNNHOZNaHpUqsmGYp7NYs pFvvRDK45YzQe44dc8D4S46H+SQ1tDnLNBo9sJpCtZ7scEx7RppbQ/BgM E1SLMB0OYmUF9r1dDAyJvU/i1hYK4+OlcYSHgkNdrgAa17Vo33N5+cwz0 5WPDkWpZQB22GBvPjASL7vllqhFvDjwt83pv2gQbEhmscCbnmIlZN66E+ A==; IronPort-SDR: IejnBxGiZmDr0rPxr4/wQzRav0K5elZXGOhQyHSZBCYNiHPTLTwXLp/eurPgFku+WjDGuaNg4E dLE2H8KtRUn/swcF123eVtTT7jf4YRzyil2dL8najwZiCCxMnCE3KUjgqPqRMSIfh6HeLmM9MM 16++xTZEGAphWnyzmiS2gRVbLnQD17M45Gs0fGMySqemrZGC3+I/pFtseEgLEmXn4k2vAubYIn TxsTnUQU5FW4YDUPL9aYZ4y2twLJwlK3Cxyd2+XRunyAl18hry4jrV6Gbwk1ulvnu9E6YHgNT6 xQk= X-IronPort-AV: E=Sophos;i="5.69,347,1571673600"; d="scan'208";a="126720957" Received: from mail-sn1nam02lp2057.outbound.protection.outlook.com (HELO NAM02-SN1-obe.outbound.protection.outlook.com) ([104.47.36.57]) by ob1.hgst.iphmx.com with ESMTP; 23 Dec 2019 19:36:48 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CQ3Y2vKQlQTNrgDi5Jyv7NwgRN7GdEfwmTEzaDyZn+2Pxeh8xuCYnxM3jfIbTXAaF6ypAw1Gn0XAdOj9deU4bNxEJCWl6JUu4GA06Nr1/ajRAKYFkKXMZVvU6BUOe28Bqa1w03I9Lejm4HbuYoEgcFdCI188UZ5/R/KDV2fFmDHAlKjmln345DVHiWuWnSJxbLHewJmJtZQWoYNCJE246Jc/bwO846BVcGfoIm78qJD9/SrvEb6BlQ5g91Of6eMysDHISe4IL3Doafx5miZYqEvEGnNwM9qBmAmvRX7IvEvyWuBud/rMJ8doMuoVycZ2eL/nutcVMHIGid1kynAByA== 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=tPO8PM5BCzJDnQ9EOciNMlleCS1DQrxPQFo5NSbkaZ4=; b=XaCbMVDfemg7/LhJTtk8+GmP+HNoF2/Mit2h4YMQGTijVBHBBKVf9xxsfYM25PPMWisctctbPxmhzfRx8CiwuN3OK9f3WS4dbE1aM31xGd1oaNAaMjviyrMJhnoudl3k/vtw5sMsX0haI2MQ8m2l3zm3IrN3qGtXpTRQyG1igivMP46jN2tmyD4ovho8qGc4lRwa2lwacLfE0UZXkUjmYU2hgFc1CTkth628dhcHc5aeKuFGVOp1euOW5cHhYZyyBKXASyiqEvOk90wvBFF5dlyk6B2KVxb754YnLauDwXfFP2q6BQTv33p3OTf2K66GxPHFRoEJ4OPir4NoC0+REQ== 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=tPO8PM5BCzJDnQ9EOciNMlleCS1DQrxPQFo5NSbkaZ4=; b=aegKboYPeinEmUbt+DgUyC/NMEePhWVELnNsjSBMlixepc9UzaQdnUUqdVPIziz19t8/m29UIKm86mloVEggtcuHw38lqQnsUvDn1KaPr65FKzybOdQptH6vCgDwMMhSYohWbCU6gbrv7jRzAughg8DhluYtfjJRogSrI2+ORAo= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB6096.namprd04.prod.outlook.com (20.178.248.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2559.14; Mon, 23 Dec 2019 11:36:46 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::a9a0:3ffa:371f:ad89]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::a9a0:3ffa:371f:ad89%7]) with mapi id 15.20.2559.017; Mon, 23 Dec 2019 11:36:46 +0000 Received: from wdc.com (106.51.20.238) by MA1PR01CA0077.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00::17) with Microsoft SMTP Server (version=TLS1_2, cipher=) via Frontend Transport; Mon, 23 Dec 2019 11:36:40 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Albert Ou , Paolo Bonzini , Radim K CC: Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , 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 v10 11/19] RISC-V: KVM: Implement VMID allocator Thread-Topic: [PATCH v10 11/19] RISC-V: KVM: Implement VMID allocator Thread-Index: AQHVuYVBTPzw/FFUK06e43EXoOHWfg== Date: Mon, 23 Dec 2019 11:36:46 +0000 Message-ID: <20191223113443.68969-12-anup.patel@wdc.com> References: <20191223113443.68969-1-anup.patel@wdc.com> In-Reply-To: <20191223113443.68969-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0077.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00::17) 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.20.238] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: aa5093e9-5372-49ed-5c99-08d7879c63ba x-ms-traffictypediagnostic: MN2PR04MB6096: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:8273; x-forefront-prvs: 0260457E99 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(376002)(396003)(136003)(346002)(366004)(39860400002)(189003)(199004)(4326008)(55236004)(8676002)(66946007)(66446008)(64756008)(26005)(71200400001)(5660300002)(8886007)(7696005)(52116002)(36756003)(66556008)(66476007)(1076003)(86362001)(7416002)(81166006)(478600001)(316002)(2616005)(8936002)(54906003)(16526019)(55016002)(81156014)(186003)(1006002)(956004)(110136005)(2906002)(44832011)(32040200004);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB6096;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: BCL:0; x-microsoft-antispam-message-info: uemogeFB2rfg8LpM1jVT85cRw/RnX7JKdiNlgk461ebYatknQShFvwH3LwY5p4Ge+tXPw/xeh5fGdoK6bcF9n4wlsoqXhTUIC4dSWRborCYQpEpwhfq/6gZB7U/i21xPO7CxtkjzgXqtZ8dt643cvD0MtpW1t1yt8Do/bG4Sxyc17mzYr7nXNxJGF111IdXlIT/ZRCPZl6VZOyw1u+hNwRpnqhaJIkNGJFLKt7Ro765uI4O+7gUlu5FFBOHrzTlGeymnM3tdp5fD/23eQipxl9tdpLnPg4P9XFs8NtHaRYKWzdx+KUKNHcLFKj1HaRnZEH+4jG5mie1yZreoLcOscj2MdOZ74BlcwcT8tr2N9e1bYvgBzCDfQ9x36NRfhTz/6Xxik0n0yT7Ucxvf+asz7K94imR2wJQpfkXX7xl9D4FKJopPTdVzYJSgIat1H94c2SJgMv7+RQxaFnVTuDbfsNoGHZpHT1gcP2v4QOU+j+/NxqZP+pO5fJYMGibk3/Ua 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: aa5093e9-5372-49ed-5c99-08d7879c63ba X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Dec 2019 11:36:46.0656 (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: m/DU5xDxptErGxiF3h6fdMRP0UiogCrxq3cdtx2aL04xpAzrwdftf0M3QKsWsNcDzG8GnJcl3s6G5oM2tAfgzg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6096 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We implement a simple VMID allocator for Guests/VMs which: 1. Detects number of VMID bits at boot-time 2. Uses atomic number to track VMID version and increments VMID version whenever we run-out of VMIDs 3. Flushes Guest TLBs on all host CPUs whenever we run-out of VMIDs 4. Force updates HW Stage2 VMID for each Guest VCPU whenever VMID changes using VCPU request KVM_REQ_UPDATE_HGATP Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini Reviewed-by: Alexander Graf --- arch/riscv/include/asm/kvm_host.h | 25 +++++++ arch/riscv/kvm/Makefile | 3 +- arch/riscv/kvm/main.c | 4 + arch/riscv/kvm/tlb.S | 43 +++++++++++ arch/riscv/kvm/vcpu.c | 9 +++ arch/riscv/kvm/vm.c | 6 ++ arch/riscv/kvm/vmid.c | 120 ++++++++++++++++++++++++++++++ 7 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/kvm/tlb.S create mode 100644 arch/riscv/kvm/vmid.c diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index 4e9305f2d43d..35a291493f05 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -27,6 +27,7 @@ #define KVM_REQ_SLEEP \ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(1) +#define KVM_REQ_UPDATE_HGATP KVM_ARCH_REQ(2) =20 struct kvm_vm_stat { ulong remote_tlb_flush; @@ -47,7 +48,19 @@ struct kvm_vcpu_stat { struct kvm_arch_memory_slot { }; =20 +struct kvm_vmid { + /* + * Writes to vmid_version and vmid happen with vmid_lock held + * whereas reads happen without any lock held. + */ + unsigned long vmid_version; + unsigned long vmid; +}; + struct kvm_arch { + /* stage2 vmid */ + struct kvm_vmid vmid; + /* stage2 page table */ pgd_t *pgd; phys_addr_t pgd_phys; @@ -167,6 +180,12 @@ static inline void kvm_arch_vcpu_uninit(struct kvm_vcp= u *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 +void __kvm_riscv_hfence_gvma_vmid_gpa(unsigned long vmid, + unsigned long gpa); +void __kvm_riscv_hfence_gvma_vmid(unsigned long vmid); +void __kvm_riscv_hfence_gvma_gpa(unsigned long gpa); +void __kvm_riscv_hfence_gvma_all(void); + int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long h= va, bool is_write); void kvm_riscv_stage2_flush_cache(struct kvm_vcpu *vcpu); @@ -174,6 +193,12 @@ int kvm_riscv_stage2_alloc_pgd(struct kvm *kvm); void kvm_riscv_stage2_free_pgd(struct kvm *kvm); void kvm_riscv_stage2_update_hgatp(struct kvm_vcpu *vcpu); =20 +void kvm_riscv_stage2_vmid_detect(void); +unsigned long kvm_riscv_stage2_vmid_bits(void); +int kvm_riscv_stage2_vmid_init(struct kvm *kvm); +bool kvm_riscv_stage2_vmid_ver_changed(struct kvm_vmid *vmid); +void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu); + void __kvm_riscv_unpriv_trap(void); =20 unsigned long kvm_riscv_vcpu_unpriv_read(struct kvm_vcpu *vcpu, diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 845579273727..c0f57f26c13d 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -8,6 +8,7 @@ ccflags-y :=3D -Ivirt/kvm -Iarch/riscv/kvm =20 kvm-objs :=3D $(common-objs-y) =20 -kvm-objs +=3D main.o vm.o mmu.o vcpu.o vcpu_exit.o vcpu_switch.o +kvm-objs +=3D main.o vm.o vmid.o tlb.o mmu.o +kvm-objs +=3D vcpu.o vcpu_exit.o vcpu_switch.o =20 obj-$(CONFIG_KVM) +=3D kvm.o diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index f29768c82fbe..08d09b52fbba 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -77,8 +77,12 @@ int kvm_arch_init(void *opaque) return -ENODEV; } =20 + kvm_riscv_stage2_vmid_detect(); + kvm_info("hypervisor extension available\n"); =20 + kvm_info("host has %ld VMID bits\n", kvm_riscv_stage2_vmid_bits()); + return 0; } =20 diff --git a/arch/riscv/kvm/tlb.S b/arch/riscv/kvm/tlb.S new file mode 100644 index 000000000000..453fca8d7940 --- /dev/null +++ b/arch/riscv/kvm/tlb.S @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +#include +#include + + .text + .altmacro + .option norelax + + /* + * Instruction encoding of hfence.gvma is: + * 0110001 rs2(5) rs1(5) 000 00000 1110011 + */ + +ENTRY(__kvm_riscv_hfence_gvma_vmid_gpa) + /* hfence.gvma a1, a0 */ + .word 0x62a60073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_vmid_gpa) + +ENTRY(__kvm_riscv_hfence_gvma_vmid) + /* hfence.gvma zero, a0 */ + .word 0x62a00073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_vmid) + +ENTRY(__kvm_riscv_hfence_gvma_gpa) + /* hfence.gvma a0 */ + .word 0x62050073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_gpa) + +ENTRY(__kvm_riscv_hfence_gvma_all) + /* hfence.gvma */ + .word 0x62000073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_all) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index ec60f3e783e5..807fd05aa9a4 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -651,6 +651,12 @@ static void kvm_riscv_check_vcpu_requests(struct kvm_v= cpu *vcpu) =20 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) kvm_riscv_reset_vcpu(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu)) + kvm_riscv_stage2_update_hgatp(vcpu); + + if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) + __kvm_riscv_hfence_gvma_all(); } } =20 @@ -695,6 +701,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, stru= ct kvm_run *run) /* Check conditions before entering the guest */ cond_resched(); =20 + kvm_riscv_stage2_vmid_update(vcpu); + kvm_riscv_check_vcpu_requests(vcpu); =20 preempt_disable(); @@ -731,6 +739,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, stru= ct kvm_run *run) kvm_riscv_update_hip(vcpu); =20 if (ret <=3D 0 || + kvm_riscv_stage2_vmid_ver_changed(&vcpu->kvm->arch.vmid) || kvm_request_pending(vcpu)) { vcpu->mode =3D OUTSIDE_GUEST_MODE; local_irq_enable(); diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index ac0211820521..c5aab5478c38 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -26,6 +26,12 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type= ) if (r) return r; =20 + r =3D kvm_riscv_stage2_vmid_init(kvm); + if (r) { + kvm_riscv_stage2_free_pgd(kvm); + return r; + } + return 0; } =20 diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c new file mode 100644 index 000000000000..2c6253b293bc --- /dev/null +++ b/arch/riscv/kvm/vmid.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned long vmid_version =3D 1; +static unsigned long vmid_next; +static unsigned long vmid_bits; +static DEFINE_SPINLOCK(vmid_lock); + +void kvm_riscv_stage2_vmid_detect(void) +{ + unsigned long old; + + /* Figure-out number of VMID bits in HW */ + old =3D csr_read(CSR_HGATP); + csr_write(CSR_HGATP, old | HGATP_VMID_MASK); + vmid_bits =3D csr_read(CSR_HGATP); + vmid_bits =3D (vmid_bits & HGATP_VMID_MASK) >> HGATP_VMID_SHIFT; + vmid_bits =3D fls_long(vmid_bits); + csr_write(CSR_HGATP, old); + + /* We polluted local TLB so flush all guest TLB */ + __kvm_riscv_hfence_gvma_all(); + + /* We don't use VMID bits if they are not sufficient */ + if ((1UL << vmid_bits) < num_possible_cpus()) + vmid_bits =3D 0; +} + +unsigned long kvm_riscv_stage2_vmid_bits(void) +{ + return vmid_bits; +} + +int kvm_riscv_stage2_vmid_init(struct kvm *kvm) +{ + /* Mark the initial VMID and VMID version invalid */ + kvm->arch.vmid.vmid_version =3D 0; + kvm->arch.vmid.vmid =3D 0; + + return 0; +} + +bool kvm_riscv_stage2_vmid_ver_changed(struct kvm_vmid *vmid) +{ + if (!vmid_bits) + return false; + + return unlikely(READ_ONCE(vmid->vmid_version) !=3D + READ_ONCE(vmid_version)); +} + +void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu) +{ + int i; + struct kvm_vcpu *v; + struct cpumask hmask; + struct kvm_vmid *vmid =3D &vcpu->kvm->arch.vmid; + + if (!kvm_riscv_stage2_vmid_ver_changed(vmid)) + return; + + spin_lock(&vmid_lock); + + /* + * We need to re-check the vmid_version here to ensure that if + * another vcpu already allocated a valid vmid for this vm. + */ + if (!kvm_riscv_stage2_vmid_ver_changed(vmid)) { + spin_unlock(&vmid_lock); + return; + } + + /* First user of a new VMID version? */ + if (unlikely(vmid_next =3D=3D 0)) { + WRITE_ONCE(vmid_version, READ_ONCE(vmid_version) + 1); + vmid_next =3D 1; + + /* + * We ran out of VMIDs so we increment vmid_version and + * start assigning VMIDs from 1. + * + * This also means existing VMIDs assignement to all Guest + * instances is invalid and we have force VMID re-assignement + * for all Guest instances. The Guest instances that were not + * running will automatically pick-up new VMIDs because will + * call kvm_riscv_stage2_vmid_update() whenever they enter + * in-kernel run loop. For Guest instances that are already + * running, we force VM exits on all host CPUs using IPI and + * flush all Guest TLBs. + */ + riscv_cpuid_to_hartid_mask(cpu_online_mask, &hmask); + sbi_remote_hfence_gvma(cpumask_bits(&hmask), 0, 0); + } + + vmid->vmid =3D vmid_next; + vmid_next++; + vmid_next &=3D (1 << vmid_bits) - 1; + + WRITE_ONCE(vmid->vmid_version, READ_ONCE(vmid_version)); + + spin_unlock(&vmid_lock); + + /* Request stage2 page table update for all VCPUs */ + kvm_for_each_vcpu(i, v, vcpu->kvm) + kvm_make_request(KVM_REQ_UPDATE_HGATP, v); +} --=20 2.17.1