Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1434546ybh; Fri, 13 Mar 2020 00:59:43 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtQphqOXn/9oJrLmXAMSnX5e+vvHzOiPqhCC5znVSYCF7NML13GZMTop7i7legg4WHz+hdE X-Received: by 2002:aca:be09:: with SMTP id o9mr6012824oif.177.1584086383511; Fri, 13 Mar 2020 00:59:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1584086383; cv=pass; d=google.com; s=arc-20160816; b=PYc1LYnfdfHpjwodjphbsTypIkTz8oo4A0DgbDbYtK3m9gzvr5AoN0lCjWuR/erkS3 FKYzvzW3cpFgQ5bzizLQJJwMLj8NAKNicGqJBelftn+YefRfzuDWj9+lURByxKbhny2K kIk9/UoJ5jsFpObHR8hmHiK/VSIM9yfdJYvpDRtCBc1VF+z4CTYUpCV/yxzaA7DJyVyw FZYdq5aBAL8TbVVJmFh8t/yZWAMTpTEHJZ4Kk365vVAf0kLS2JIxoatdxjcx34zEmPj5 tcR3DkiEt5NyyCO6bZlL/E16o0gUckueidAWDOksNhjF7TE6WQYji1xGCsLlNTCI9S4D ixXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:wdcipoutbound:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :ironport-sdr:dkim-signature; bh=LvB9ox5fKm7xC7WUp2jlrr30s0UF0k8FQkCgdst4oOo=; b=UzvlTZkFMXKpxscAH9m8oKvyM2hARjk2UByA1bga9d+7bTOSm3TnwxYEbZRq7W0W9W /x6drxDjJihTFHxWFaXalnY3RDNw0BOWrZlSB+64ny7el2KvbtbcUd+J7uNw175S5rzM eb7lGxnMmJ0NvUyWEzSvNme0No3HUH0mz/gEJ4ojPEvx3VNArTdVvfKTV05H/Y6LYMOS L5mbP3nczGw7MOjfZ2vaYU5AFCcnQIfi+aWWB1hu4j+eoauYdr9/rz3IYRIx7tq62M4X WDLWWIX5gu4mxIQGKC5ENPk/AjAv5tHSpYl0U0GaBncnbbX3k9teUlIQjDkL5qK/8x5y agwg== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=lIYEjey6; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=wbBs1DsD; 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 f21si4410673otf.128.2020.03.13.00.59.30; Fri, 13 Mar 2020 00:59:43 -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=lIYEjey6; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=wbBs1DsD; 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 S1726664AbgCMH6q (ORCPT + 99 others); Fri, 13 Mar 2020 03:58:46 -0400 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:9414 "EHLO esa5.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726423AbgCMH6q (ORCPT ); Fri, 13 Mar 2020 03:58:46 -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=1584086325; x=1615622325; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=1adPztt3Xc3PkfAiUTQlbSJ359vgo6sYidZL0zXuVwU=; b=lIYEjey6p1OJGsFoaTOImkKz86GbGqw3bX3aqSrLUfKqSkNKSDvXxtNk MfzLOmXx25lgzfRz4/B8xjctwb/WAufPVkK9Hu2OJWHCdNSSoJTB8QvOF dqSmtIzHIwOQe27gxxVupY2qcoMwk1LzeEgZn9+4d5tl8L8OmXNf9AQ67 pQbhXujAoUQcot7utHRragRv2uw1V6xUwvQrH58Pj8dLmB4/wXAgkYD9u cCg3GcP6dNpn+oUvwFyig8/l2sTsZLotTl57BWSGsp71dbW+Qfp4SpL2Q vzOxM08YJjQIcUlEM0oMNexuvMlnrnR7Pq6y1FGeAGWgdKpg6a5yjvRnV w==; IronPort-SDR: m48lKFF/4pSaYyt8HsJd2qsaZ9w+t5iS0UhLpKhOPAdHq588BGnHSayahqlwKrQ6xT68p+HwhP c6LsQNO7PENRh5sJOSs+opANrRYafGsL52LxLT3S1ymqqgM8Q+1Bet0skeT/MNY6STNMtc/ZD6 r++OTfJUbLEFTg5c03z2PBjXSdtNfkb+Vv7mOUUtDfKSvQUNrIeb7RlUISMkAi2TdxLUNW/K9x 8/Ktb77Kjoutuww33zvsSsULXrLirHZp30VXZ4qdm18RPPDKiFAH2tjkI5hDoRJgUcJ0/QSMaC L0c= X-IronPort-AV: E=Sophos;i="5.70,547,1574092800"; d="scan'208";a="132827886" Received: from mail-cys01nam02lp2055.outbound.protection.outlook.com (HELO NAM02-CY1-obe.outbound.protection.outlook.com) ([104.47.37.55]) by ob1.hgst.iphmx.com with ESMTP; 13 Mar 2020 15:58:33 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NYUZIASV/TTfK9oTZBslSVbtVH/doEoeH5vsJICo8y+/U0hC/QkowyiL/rOxr7/x5891W3+bPXUh5+x9Fc2FvrEVVnieXMBFS5G23LoKPm9iKavvVgLdIgTaz1f4pDgW+y0+Gk2w7YASrzc97/Y97iAo+YVpA2e8GklY5CQ0G4G5Ai8+t563sd/DNkWWQosc2Ecrw0pCxASP7k+tYQplHHeGynNC7FfMopejLL4AUeTegwX5pDWnQy86tpEK126hknRzI4CXFCzxFoSB+S1PT6jeAZf8uWuSA4gatNEW9Mod1snuoHVLNKVrgr/UdtKa6pBMgr4AD0UV1d+dVIyUhw== 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=LvB9ox5fKm7xC7WUp2jlrr30s0UF0k8FQkCgdst4oOo=; b=DV/nzocT2Y5X6YemXji4VvhNzoTUKXpF6wES4jTefJ93haJyQrTWCW0lf8MDUSJmkquG52gp7FGvlAU8UzmTrWCbWrKMrD821kIK32Y0thy6GFvcF3n5bldwMJB6sWpygFPSb9Dz5nddNrnTngA2JTLL52ZB3LENeMgJ1QhadrMDbUPtbJMgQER4tWp6X7VJo49AE1enXUnk/491mcMcck47JHds+swevMnFGMCgD5obeXPAMI/CCggyZdV+5Snewaz7JbMrka15ejZhGnZIT5kqo5gAwY9it8HjjDLH6TKYNiQJA9DMW7UhvDVj4McHdcTGl14dtatrecEu6Rc8JA== 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=LvB9ox5fKm7xC7WUp2jlrr30s0UF0k8FQkCgdst4oOo=; b=wbBs1DsDh3TSt0dAJOM/Wc+Bd+uXDRsTaKgCndHAIGgqjzKxFxEShEjQ4ixlUQBs4YUYnFI1fwVQ8tibO+IfXPIyeDdH1Zk1vXjiYrnEZcvjUlFYxuUMtACXMQIeEAcH3Jz0KXtXGJrupBqIEE5Ef3bVwqnS8K1sf8PWtQtl2LE= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; Received: from MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) by MN2PR04MB6944.namprd04.prod.outlook.com (2603:10b6:208:1ed::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.14; Fri, 13 Mar 2020 07:58:32 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::159d:10c9:f6df:64c8]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::159d:10c9:f6df:64c8%6]) with mapi id 15.20.2814.018; Fri, 13 Mar 2020 07:58:32 +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 v11 12/20] RISC-V: KVM: Implement VMID allocator Date: Fri, 13 Mar 2020 13:21:23 +0530 Message-Id: <20200313075131.69837-13-anup.patel@wdc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200313075131.69837-1-anup.patel@wdc.com> References: <20200313075131.69837-1-anup.patel@wdc.com> Content-Type: text/plain X-ClientProxiedBy: MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::19) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (1.39.129.91) by MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.14 via Frontend Transport; Fri, 13 Mar 2020 07:58:24 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [1.39.129.91] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ddc72119-38bb-4c4b-82b2-08d7c7245266 X-MS-TrafficTypeDiagnostic: MN2PR04MB6944: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-Forefront-PRVS: 034119E4F6 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(376002)(136003)(346002)(396003)(366004)(199004)(26005)(4326008)(44832011)(55016002)(1076003)(8936002)(478600001)(86362001)(36756003)(66556008)(16526019)(66946007)(956004)(66476007)(186003)(2616005)(316002)(7416002)(1006002)(110136005)(8886007)(8676002)(2906002)(81166006)(81156014)(5660300002)(54906003)(52116002)(7696005)(36456003)(42976004);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB6944;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SCF6YZD5POFS/1sLxv6p4/h5aFBf4CziRbt5fVxPeHkrgOb5q41xteRYi9dEIbxyiUbmn2Fr+TsnOQe8IB/gh7esR4w/iD8UBPcXa2DOjlQM8kL4ZGn0vHQciDDq6JAfmjiQ8jHzyp0bX7T5ew9n9C221KL55NkwlbCCUPZzmZioeZEZHpDGXzOpge/1nRbFdxLus5Bw5lehlXIL0NShx3MmfXMTuUJDOSbqpyeGHDdlk8rwnPRwfwIIdlYyEsoHUXlRhdCIcz0jUiW9hDtxJO6oQqbrbi0OkpwkSuubMAgf5YBbh8/I+EHCYSuZ6t+ERG0WA/sfMdaVvlRFkWRX1MQ7O4ds4upCB9kYBp56Ei5AJX58uxet7L+7Pl1sXEp79yEStBfiifxlLlukB30PIZdO8FHVsAuvEyHDQ7NIZZk0KZYMjq79zEr0rVO7MeCDg4FmiFv/g7yVPdoftkAK9hyefVraNEWfwcOjHKkfXblMy04lb/9TQy+yvFAgOV6mazgnByy+Y5GxPx1VCh5Fwg== X-MS-Exchange-AntiSpam-MessageData: 4hBMoT5DMC63HQwX1Kr4eWyRlqe34JhhZymMECKoP0YRMWEsvQR9ZHjsO8mRt2BXEVx3i4x6hikkfO5uvf/i2q2XhDu9LMydtIFHEmuszVBLvdBJn1WD2PrbCsZ38YVx0RK4IePwqGW4xyPlRYZ8CA== X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: ddc72119-38bb-4c4b-82b2-08d7c7245266 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Mar 2020 07:58:32.0889 (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: 5tcHGiRedMt2mZmVPy/zsx/9no1pTaWtXmxndDZ3Qov4SgVvTgAHj3P5codEHMF683IhuuCzGH08VXlANlkBJQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6944 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) struct kvm_vm_stat { ulong remote_tlb_flush; @@ -47,7 +48,19 @@ struct kvm_vcpu_stat { struct kvm_arch_memory_slot { }; +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_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) {} +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 hva, 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); +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); 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 := -Ivirt/kvm -Iarch/riscv/kvm kvm-objs := $(common-objs-y) -kvm-objs += main.o vm.o mmu.o vcpu.o vcpu_exit.o vcpu_switch.o +kvm-objs += main.o vm.o vmid.o tlb.o mmu.o +kvm-objs += vcpu.o vcpu_exit.o vcpu_switch.o obj-$(CONFIG_KVM) += kvm.o diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index b28c2a0fb5b1..fe1a8edc6d63 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; } + kvm_riscv_stage2_vmid_detect(); + kvm_info("hypervisor extension available\n"); + kvm_info("host has %ld VMID bits\n", kvm_riscv_stage2_vmid_bits()); + return 0; } 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 99acd836ba06..186458eddcfc 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -633,6 +633,12 @@ static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu) 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(); } } @@ -677,6 +683,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) /* Check conditions before entering the guest */ cond_resched(); + kvm_riscv_stage2_vmid_update(vcpu); + kvm_riscv_check_vcpu_requests(vcpu); preempt_disable(); @@ -713,6 +721,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) kvm_riscv_update_hip(vcpu); if (ret <= 0 || + kvm_riscv_stage2_vmid_ver_changed(&vcpu->kvm->arch.vmid) || kvm_request_pending(vcpu)) { vcpu->mode = 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; + r = kvm_riscv_stage2_vmid_init(kvm); + if (r) { + kvm_riscv_stage2_free_pgd(kvm); + return r; + } + return 0; } 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 = 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 = csr_read(CSR_HGATP); + csr_write(CSR_HGATP, old | HGATP_VMID_MASK); + vmid_bits = csr_read(CSR_HGATP); + vmid_bits = (vmid_bits & HGATP_VMID_MASK) >> HGATP_VMID_SHIFT; + vmid_bits = 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 = 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 = 0; + kvm->arch.vmid.vmid = 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) != + 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 = &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 == 0)) { + WRITE_ONCE(vmid_version, READ_ONCE(vmid_version) + 1); + vmid_next = 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 = vmid_next; + vmid_next++; + vmid_next &= (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); +} -- 2.17.1