Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2781922ybh; Mon, 5 Aug 2019 06:45:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqy56e93A4typNzkOO5+3rY6F/Izs4+vpBn6Uo8dg7cfTCSKlQig45xoTrtN+gFm+I6doMAH X-Received: by 2002:a17:902:467:: with SMTP id 94mr143264471ple.131.1565012753161; Mon, 05 Aug 2019 06:45:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1565012753; cv=pass; d=google.com; s=arc-20160816; b=SRMvFKVmicQt/CqE3RYnSggpvqS8pfTM+vPY2e92N1Z4ovINBwabYMuyi+7J1j5+/9 BthmX2JkFN3+eI8uPd3j7eNsOCf/SeyYvuIZngVD6G/IoO4fhWMx4Er8WXAI/GLh49hJ qQUGTYfW2ZlESl+exPzWjy36qKfUkU2uy2tRd5zxKEME9XW9P9FN2sMC/I9Jd4dAz7tu QVyg98wWiWy93vVoBGnlt+lMKBXdVk7US3lvNNo6TEvEX3SzbnTLvLxExvjBsxd50eB8 x9xoJ5VDTDTZnZilH9dJhZpRdi8K1zOI32x5MRi/qrQzXpji+mbWXKCDZMSkiNwP0pFk yVqg== 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=4tpp2vlspgsd/Ex1HC+X3/F63kVMvhXmF152pyfAM5o=; b=aOHI68aFnjxts3KcQxwuM++sNhsn4XdJmyaofzA43Z2HUX0MGjsbywtMzikpkXrft4 ACorHwfDNNA4KszPJa/cWvfGS2rVAN+RuL2AcsLbE3pjrCU8PE3ATxJl0rC6kGOnxVJ0 +dwJ8BCsKTZPn4DJDKmVeG8SEiNqTubVqZP2MRvf44XdYmQwYL0bxzx56f1ZY7kyhcjO KAKF/H+CjlJ8VmiB1q3A4odawyVWWWb5H/j1zoqdl5aMQMWW+PZFPk9J02KCcsSMxX7o fYTj2skBPueKtR7VBsep815xMXJsTKhy1fL5sdJUcNyKfEJZgFl0e0P4P1jXsQ0m15S8 7/IQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=pB+LEJG4; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=vAGhMGQO; 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 36si43919427pla.80.2019.08.05.06.45.37; Mon, 05 Aug 2019 06:45:53 -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=pB+LEJG4; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=vAGhMGQO; 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 S1731057AbfHENo3 (ORCPT + 99 others); Mon, 5 Aug 2019 09:44:29 -0400 Received: from esa1.hgst.iphmx.com ([68.232.141.245]:50835 "EHLO esa1.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729333AbfHENo1 (ORCPT ); Mon, 5 Aug 2019 09:44:27 -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=1565012666; x=1596548666; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=2hBUtAmAmyMzS5+yG5+ita+b0iFuUoY9BycJyWRQv9M=; b=pB+LEJG4j8iXtiubtTprYmTlKLzba5kV8gFGwLpdRiL5jn2hdsnEOGet rkt5+Y1zZi/VMOBECdNnlZysHe1xqs2yJEA4RwH8t9CW1EB3WXGlDfiTu 0P6st1DmK6BR5bfix+H9cYPqfVId712/D1k+wt6vSK9XTeFRz8b/UKi2P AsERSDL+LAfAWZtHrxDuxTNKCrWXshYVv/kK4cn4GlT/cfZCA7iA0K654 WmOpEvouFHrltLrbDPzpTPTnb0cOX2eCYAjwc5bUMuikf2xMOy8/mp65f dGt48Chk3kYwpTDkgkarBRbjAL6Cu/OPCwtsdQ7yDjo4NbRDYfeAzm5OH Q==; IronPort-SDR: kfcXw24eulZout4LEgGG/A52vPZ7gbZiA/GBQdTrDQrVwG/K4TpU3c2du9v3ldXJlFy8VQDzev Z7vfJVmOAjWG7Mnjy9rn+Xg3MlP6UyU7oXKUdJV9YY55zNUUT7msnFpZ6vPn19rto6aq/EAyAa wiBM67OhnRsIgiYHoKCYlP0Fh14Hs58dWAqrDLTmFyL5rpa6Ly/wxfvaoRfZEojQWVVSY3JJp5 F7ns7jADUaLDn0QWbPKERf+RDoUIiGgq4m4G51AmAw4W+ZRP6edPPDSmSLaEJ/ODCUHZW6CyyQ ztk= X-IronPort-AV: E=Sophos;i="5.64,350,1559491200"; d="scan'208";a="221493537" Received: from mail-sn1nam01lp2052.outbound.protection.outlook.com (HELO NAM01-SN1-obe.outbound.protection.outlook.com) ([104.47.32.52]) by ob1.hgst.iphmx.com with ESMTP; 05 Aug 2019 21:44:25 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aT7tTDm1AO0gBC2ODF2/XyA1HZ6ixRxG9k8QO504x20AH2L7lpIwqOa2Urd5FHYV6BM6q5pHQX7Iy05KOuULemk9ULr8jqaKkMsnrAbYuDpniDZp29SWKH+vw6iwnMQcnA5ovAH4T7ijiptp4+WNPiLus3qJd8SykSfDc+KY1HPJTVxv8pqw7cpps33jlU5zUQR6QOKH8JaKRN94Endod5Vt0qDwu6A3yIxthohlq3lP98HgtyKIAGGn5Xx6he9QHB9+Mungnwzsd5NCv6H9UcBVk0wvaU61iwKNBenwBRFMwHAVz/DN00kmUhUJNS4M6rGlhBuntZnouQ+w4xPWQA== 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=4tpp2vlspgsd/Ex1HC+X3/F63kVMvhXmF152pyfAM5o=; b=C+kHa8m1Zg0Mg3XvNdahyrIlfo+OAuFBidzZ96o7TZfobecurR6FY0G5cuumREkdN3jhChe1SBTXgfmjbG2NTCl6KG6SLEz7p4yFjEUtHyhMy3RijEU++VjIenzXou3/nyJD3UqshhDe7WXATZ7pMYIQ/hjkjHaIi7FKDF/FGOvXZNDNTZ1sF5KMjFQYjWTQmORIiX3QkUMfKXlGEzIPETWsKu5epjmAIIJgqkaqRgfH32VFemi2EG+ggvyXmHdz7bgLNzXUnBkG3ke0eOPDo9D628sUhjtlCUyiqK02X/xk0+pb4AwqOSSdKrjTUX4KYeMTiEIQaE9/Jl+t05uhDw== 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=4tpp2vlspgsd/Ex1HC+X3/F63kVMvhXmF152pyfAM5o=; b=vAGhMGQOvvK7o4KpgY+r/zbsk9bfoTxPyWJIizQw2tSMFgBsvpdl206zqvJR+hRJ+gtfgdZFbOnEHAFsCqcvSPSw90zPxJiX0UZ4mULruzV+JN5L/oAOZhUAzXccei42bIweqCZiSaG/AYHDTjZ86QgV3xOgV9RHErn9Zxf/aAU= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB6159.namprd04.prod.outlook.com (20.178.249.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2136.17; Mon, 5 Aug 2019 13:44:23 +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.2136.018; Mon, 5 Aug 2019 13:44:23 +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 v3 17/19] RISC-V: KVM: Add SBI v0.1 support Thread-Topic: [PATCH v3 17/19] RISC-V: KVM: Add SBI v0.1 support Thread-Index: AQHVS5PkZ8/bQ5pdMUKyelce39PYBQ== Date: Mon, 5 Aug 2019 13:44:23 +0000 Message-ID: <20190805134201.2814-18-anup.patel@wdc.com> References: <20190805134201.2814-1-anup.patel@wdc.com> In-Reply-To: <20190805134201.2814-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BMXPR01CA0087.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:54::27) 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.197] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 2a547a2a-4799-48e7-f756-08d719ab0677 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:MN2PR04MB6159; x-ms-traffictypediagnostic: MN2PR04MB6159: x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:6108; x-forefront-prvs: 01208B1E18 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(136003)(366004)(396003)(376002)(346002)(189003)(199004)(66556008)(66476007)(71190400001)(71200400001)(110136005)(54906003)(66946007)(66446008)(64756008)(86362001)(5660300002)(102836004)(52116002)(55236004)(14454004)(99286004)(386003)(78486014)(316002)(256004)(14444005)(6506007)(76176011)(2616005)(6486002)(8676002)(9456002)(8936002)(36756003)(3846002)(6116002)(6512007)(4326008)(2906002)(476003)(305945005)(44832011)(81156014)(81166006)(26005)(186003)(486006)(11346002)(25786009)(7736002)(1076003)(50226002)(478600001)(68736007)(446003)(53936002)(66066001)(6436002)(7416002);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB6159;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-message-info: 5NjZWz3B5UVhxHAZikmjDmIMUbpCpxbsJFRMIYfguQUkEP4iEgjErxv+nWmQ/NvM7TeDbn+qgWvDytkBVnnw8eRpfLifBynDqgsFjA6wJjhVL97hRC5HHmHjKcAxyiq/IIfBxfO2y1/Be8+fTHq1i2v86l2C2xUKGNGAOqMEV/eMdzV1iY/rr/N+ZbZcg1u2N8opcfe2j4upuR1KzJ2B8fcyTSBGqESuSBGeFxZ5RHJNDeg4WR5W3XeFGC75ggVmfTP2zfVhVNKq27rUa5IeNkLgKxJs4zAF2U8jFKs03dG68ylwNjsNkoN2c5qOulRgu3KThX8sb0ZaPh1HPJgEQyQb9J8lxnG6nJ0bA7FLu6O79m0OWlHAGyy+zO7b7yikS4zuJUatZpgXi8Kdd5vHaa+6cFfGJcDPKzcm2D5h1O8= 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: 2a547a2a-4799-48e7-f756-08d719ab0677 X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Aug 2019 13:44:23.7480 (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: Anup.Patel@wdc.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6159 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Atish Patra The KVM host kernel running in HS-mode needs to handle SBI calls coming from guest kernel running in VS-mode. This patch adds SBI v0.1 support in KVM RISC-V. All the SBI calls are implemented correctly except remote tlb flushes. For remote TLB flushes, we are doing full TLB flush and this will be optimized in future. Signed-off-by: Atish Patra Signed-off-by: Anup Patel --- arch/riscv/include/asm/kvm_host.h | 2 + arch/riscv/kvm/Makefile | 2 +- arch/riscv/kvm/vcpu_exit.c | 3 + arch/riscv/kvm/vcpu_sbi.c | 119 ++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/kvm/vcpu_sbi.c diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index 983aea4f6049..9a673f18d772 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -238,4 +238,6 @@ bool kvm_riscv_vcpu_has_interrupt(struct kvm_vcpu *vcpu= ); void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu); =20 +int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu); + #endif /* __RISCV_KVM_HOST_H__ */ diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 3e0c7558320d..b56dc1650d2c 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -9,6 +9,6 @@ ccflags-y :=3D -Ivirt/kvm -Iarch/riscv/kvm kvm-objs :=3D $(common-objs-y) =20 kvm-objs +=3D main.o vm.o vmid.o tlb.o mmu.o -kvm-objs +=3D vcpu.o vcpu_exit.o vcpu_switch.o vcpu_timer.o +kvm-objs +=3D vcpu.o vcpu_exit.o vcpu_switch.o vcpu_timer.o vcpu_sbi.o =20 obj-$(CONFIG_KVM) +=3D kvm.o diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index fbc04fe335ad..87b83fcf9a14 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -534,6 +534,9 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct k= vm_run *run, (vcpu->arch.guest_context.hstatus & HSTATUS_STL)) ret =3D stage2_page_fault(vcpu, run, scause, stval); break; + case EXC_SUPERVISOR_SYSCALL: + if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) + ret =3D kvm_riscv_vcpu_sbi_ecall(vcpu); default: break; }; diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c new file mode 100644 index 000000000000..5793202eb514 --- /dev/null +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0 +/** + * Copyright (c) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include + +#define SBI_VERSION_MAJOR 0 +#define SBI_VERSION_MINOR 1 + +/* TODO: Handle traps due to unpriv load and redirect it back to VS-mode *= / +static unsigned long kvm_sbi_unpriv_load(const unsigned long *addr, + struct kvm_vcpu *vcpu) +{ + unsigned long flags, val; + unsigned long __hstatus, __sstatus; + + local_irq_save(flags); + __hstatus =3D csr_read(CSR_HSTATUS); + __sstatus =3D csr_read(CSR_SSTATUS); + csr_write(CSR_HSTATUS, vcpu->arch.guest_context.hstatus | HSTATUS_SPRV); + csr_write(CSR_SSTATUS, vcpu->arch.guest_context.sstatus); + val =3D *addr; + csr_write(CSR_HSTATUS, __hstatus); + csr_write(CSR_SSTATUS, __sstatus); + local_irq_restore(flags); + + return val; +} + +static void kvm_sbi_system_shutdown(struct kvm_vcpu *vcpu, u32 type) +{ + int i; + struct kvm_vcpu *tmp; + + kvm_for_each_vcpu(i, tmp, vcpu->kvm) + tmp->arch.power_off =3D true; + kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP); + + memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); + vcpu->run->system_event.type =3D type; + vcpu->run->exit_reason =3D KVM_EXIT_SYSTEM_EVENT; +} + +int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu) +{ + int ret =3D 1; + u64 next_cycle; + int vcpuid; + struct kvm_vcpu *remote_vcpu; + ulong dhart_mask; + struct kvm_cpu_context *cp =3D &vcpu->arch.guest_context; + + if (!cp) + return -EINVAL; + switch (cp->a7) { + case SBI_SET_TIMER: +#if __riscv_xlen =3D=3D 32 + next_cycle =3D ((u64)cp->a1 << 32) | (u64)cp->a0; +#else + next_cycle =3D (u64)cp->a0; +#endif + kvm_riscv_vcpu_timer_next_event(vcpu, next_cycle); + break; + case SBI_CONSOLE_PUTCHAR: + /* Not implemented */ + cp->a0 =3D -ENOTSUPP; + break; + case SBI_CONSOLE_GETCHAR: + /* Not implemented */ + cp->a0 =3D -ENOTSUPP; + break; + case SBI_CLEAR_IPI: + kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_S_SOFT); + break; + case SBI_SEND_IPI: + dhart_mask =3D kvm_sbi_unpriv_load((unsigned long *)cp->a0, vcpu); + for_each_set_bit(vcpuid, &dhart_mask, BITS_PER_LONG) { + remote_vcpu =3D kvm_get_vcpu_by_id(vcpu->kvm, vcpuid); + kvm_riscv_vcpu_set_interrupt(remote_vcpu, IRQ_S_SOFT); + } + break; + case SBI_SHUTDOWN: + kvm_sbi_system_shutdown(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN); + ret =3D 0; + break; + case SBI_REMOTE_FENCE_I: + sbi_remote_fence_i(NULL); + break; + /* + * TODO: There should be a way to call remote hfence.bvma. + * Preferred method is now a SBI call. Until then, just flush + * all tlbs. + */ + case SBI_REMOTE_SFENCE_VMA: + /*TODO: Parse vma range.*/ + sbi_remote_sfence_vma(NULL, 0, 0); + break; + case SBI_REMOTE_SFENCE_VMA_ASID: + /*TODO: Parse vma range for given ASID */ + sbi_remote_sfence_vma(NULL, 0, 0); + break; + default: + cp->a0 =3D ENOTSUPP; + break; + }; + + if (ret >=3D 0) + cp->sepc +=3D 4; + + return ret; +} --=20 2.17.1