Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1436795ybh; Fri, 13 Mar 2020 01:02:07 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuHJVLU/aUnzjAkOaTn8vbd87G7xr6Pfq5wV3ZW3G/qMnsXY8LecZvPWdwoziRNA5uzUo4F X-Received: by 2002:a9d:1eea:: with SMTP id n97mr6397852otn.139.1584086526946; Fri, 13 Mar 2020 01:02:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1584086526; cv=pass; d=google.com; s=arc-20160816; b=LqgFwJVoiYJt1J/5qDjJLp0J4kpgTT5tTxsFJ+hPaB9K88+S3V8Kf6qoNl32z0Uobn FcJKD/xzc/hptN3IBO1FMAUYO21NVWAmI14YSU7iphMvH03dLAJqqHHBYnih2/uQ3MAj I6Z2xcB6nLtgfW/IIFyVeELcdK3XyLK4Br9YqgS/FnYcJSe04BLsqiLq1vmP+P30N/FW gE3CV4B4BTZmwhCy1VaVPzxfgoGOLFcImLlB+6p/7RUr0gIV7+SfQHIbvZoiYABRqtwu f9q1CeWC3Tg7pFuPBu4uB8TNBL/mQ8MzdBQ3aRg/zETFzOKZ01A28YP3TU13wTJzZB2z kn+w== 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=KqjOXoc4XLFVy5PYXmsw04ri/0CbtUpx4QHsNGW699M=; b=EZJ7QiqRih08Uq92iu9Ke6UNZLcebdWNpJu4M9HCvk9lzYhEaXd4G30orsZR4eCFnD c/Yd3g0lG81vjHMNX63jnj0g0Nz70v6a7/fp3Svz8DgNaG+n6V/lPzLLINP1SgYh10h/ pk48+3voGXw7q5bySOELDvreSp7l2hakOYW1OV4TAf1i7StX/DjPEZAK+PENvRY4QEHI QjMNeCnCmkqDZMadW6bfdTPNLQK4Y9CAKUjss7VW1mP2UNnouFr65PsmLsow8Mb/G5Gd yRVTxQ51cZ8mRTpeB+VCblWOBPcIJWssEyTy/x1COjCst9H+3V1EQdxpHCrL1u+PbKA9 75xA== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=NFo8bsRq; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=wMk2pabR; 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 e40si4419334ote.145.2020.03.13.01.01.54; Fri, 13 Mar 2020 01:02:06 -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=NFo8bsRq; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=wMk2pabR; 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 S1726365AbgCMH7x (ORCPT + 99 others); Fri, 13 Mar 2020 03:59:53 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:10290 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726464AbgCMH7x (ORCPT ); Fri, 13 Mar 2020 03:59:53 -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=1584086392; x=1615622392; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=PenPlCt1Y5ef/KTiY7YVFZT7s/9QxTP7nTYZbEQdtWA=; b=NFo8bsRqu6WwePsM33hkPv0jjg4XhchmmdKvAtDdE2Q51DXrgdtDQWhb JKNURVzk77ohUjjGj6bwCPxDlF2pU7SJyAg1qk7Iqypld7z5FmfNCMtKs M1o1waLUzqN8r4OzQQIoVFxf0eBJkreO1mqxC++J1Av5amLlA94gxW9pQ RUDUnJ562sWq9XUvtEEvq8X49K66PHa5VhB3EX9NHNGkf+uUeE24NCaOG Ls6+5SMEh/6/QJ3R8iI7YUuTtn6L0ENnjET4tZpYoAKdFsQUCi665I86V VhfWoIMacaLV70Y+wHZg9FdTk2cWdmoZs5fepgEDEHRu8y405zNG8G/wm A==; IronPort-SDR: fT7qEpKCf7SiUHmkcjxE+Z/REpAlY9hwhWIqXiLGDO7IJdIdTsXTv4avgQRMyQeSHc0OeMXO2c V4ln1l3ZBsEaFvKdvrKzyN0al2nfbeYduXEaCWjgg0aUF1Uf+1QtYgFwj89pkYHATRfO0ZHuJ2 d2zHJdsbxhuz5T8wnDBwn2/qGzQatm6UwFOjCe7fBEi2Gu1pznzcwXAsxwX+88OkOxQCWSQe2g HQlOjqyeSPytQVoPBo8LYEZttQLBCQxKR9seyTqPeqQUKO8DYKq1rTxyr+fF/HLpR7g/C2/gNB +wM= X-IronPort-AV: E=Sophos;i="5.70,547,1574092800"; d="scan'208";a="132374948" Received: from mail-cys01nam02lp2056.outbound.protection.outlook.com (HELO NAM02-CY1-obe.outbound.protection.outlook.com) ([104.47.37.56]) by ob1.hgst.iphmx.com with ESMTP; 13 Mar 2020 15:59:35 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qmm7E5lzavS+Xvlb1B1kbL5TR0xjk+PYqv9NjBYELPmpDrdY5jAErgEK4hFQEZMhlxElGEkc9KHjZJlgQW+QWtcRzPhfwRF4DRXaAaK9vl9s2zBgMCvISIFSANiuoBxLjaYvgd/uSdT/tstuZJ/LOszu3bLfl8vA9l3i65i9yrhbgKAh5OT5OPvokzC0bkyTYCNF45XkDasTFkpcr5gBI1z3I56FDdqkbVmfKIGbQKzhhuUhgebjIhNO+625v7LqWLQBsoooc1dthU80bmZ+BTMBVLD/VTLCjLweU2kihDvK7yfIpvw00BBi8chC51RoYFori7NybUJdoe5uqzVrtw== 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=KqjOXoc4XLFVy5PYXmsw04ri/0CbtUpx4QHsNGW699M=; b=m5rMFTIhvj5HAh8IXYgcZNNeGawMHpU7okRWuFzfB1KYkR8E9vs5xLofMK1geXwyObcLik6Xpgj4aIEtTaUxc2kN8/vPyScq1Sv9ykwL9Jh0eFCaswL4UsY+/rgPLYyPuFVm/DtUEhBzBqFfgC5dwZs33quSES5U1sZCbpnuf1OLQJzmcbkpgfMwW0jNb/ZXt7Etk5NjU9M6i66A4/fTZ8CqycgenUTuERhtv2Z39JSRzZaHErhZKj5RGObvo0cyoRd0mw3FNK3GQ7b4RWwHOAKqw06sqSFQv3JiTEyIxOSOFOxnzeGRxk/bu7ntA7kczpx/71TP7vwm8IUI4Eehlg== 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=KqjOXoc4XLFVy5PYXmsw04ri/0CbtUpx4QHsNGW699M=; b=wMk2pabR+YCdX/s4hZBWjyyaW5IwwvWevW74QBaWg9Kbvsyl3LuoQ01+ofNNWAT7ZsNBPO7mnjh4iMVy81qPZfxPq0fbE3uDGdTl3vPEMB/+unhLkj2ndeZNzGV2dClrRLPEeUHHWZwY6dUkPjuVlvGm6CzaxTHHD8Oah0Rkk9M= 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:59:34 +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:59:34 +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 18/20] RISC-V: KVM: Add SBI v0.1 support Date: Fri, 13 Mar 2020 13:21:29 +0530 Message-Id: <20200313075131.69837-19-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:59:27 +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: 3ce2b0bd-9f7c-4fcf-dd33-08d7c72477b5 X-MS-TrafficTypeDiagnostic: MN2PR04MB6944: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:7691; 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: uCpMiQBzs4ZVAJ64UJu6LCI1GkWWooiTINWsAUcd/EOdnSthFhu7UiIoX6+ylN80okg14N+5I40CVk3zss4NSbGxEuujcjV0eB9dMs9lt2znSBunjT8NlKmUqG2wDowHlKEv3UrYULvOMRtDh8bFDtxzN7V4EF6xX58bvMlJK344axGV4K056JABs6p+XjVWAmn4PJsVdtHGOdBUG0jbHydQcW2+svh4F5w2O+5yscXRLD6ZmFaoCKGH6OcJQSXprYCKOYW8YzdHDW8uvvRenXkSatHwib4OhDjRBOetVFVWa+0YUPM0dRiRch8yRboMDN7hmyWl0a3CaqBmIhtwRCtD48qq4dB3L7E60rTC/8PeZDZyuqFdpXt+ASI0uv3lIvZiePHQMC2cF0qLHWzeQ7uJgY0DOhx+rW8d2FFe17Gk04hHAXImKEkNWFxhmiq5DUAcanolXIFBPwxMANOynJrvUPjENLBytIqFSib5KqVkqMa7dlL+fHvl6xKG4Mvbj3YhZ+H0VYUwk3qT1o0mAg== X-MS-Exchange-AntiSpam-MessageData: 7kydTtsSHptyGkgJY0quCBfSH/iDsMm7Pp4Phd+rdz/Be2FOTMhwftJntzEKqnmRUsjNBni+AaLv/LMVoKucBjAchx8O3EhyDq2jvb3cIkAA5NWDNSaWRm+Ch3IiGmKN490GlYKI2rFbJj00PDvYoQ== X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3ce2b0bd-9f7c-4fcf-dd33-08d7c72477b5 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Mar 2020 07:59:34.3478 (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: HrfCHY3THI3KOTcSRQu3eLkaIwiYJq86R9RisRQJlxluTGsJjj8xqfA7CBHZXtPnQOOrRb+KqtsOJRUBgzlccQ== 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 From: Atish Patra The KVM host kernel is running in HS-mode needs so we need to handle the SBI calls coming from guest kernel running in VS-mode. This patch adds SBI v0.1 support in KVM RISC-V. Almost all SBI v0.1 calls are implemented in KVM kernel module except GETCHAR and PUTCHART calls which are forwarded to user space because these calls cannot be implemented in kernel space. In future, when we implement SBI v0.2 for Guest, we will forward SBI v0.2 experimental and vendor extension calls to user space. Signed-off-by: Atish Patra Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini --- arch/riscv/include/asm/kvm_host.h | 10 ++ arch/riscv/kvm/Makefile | 2 +- arch/riscv/kvm/vcpu.c | 9 ++ arch/riscv/kvm/vcpu_exit.c | 4 + arch/riscv/kvm/vcpu_sbi.c | 171 ++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 8 ++ 6 files changed, 203 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 56a8ab9772ae..a7ad0a8f5604 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -77,6 +77,10 @@ struct kvm_mmio_decode { int return_handled; }; +struct kvm_sbi_context { + int return_handled; +}; + #define KVM_MMU_PAGE_CACHE_NR_OBJS 32 struct kvm_mmu_page_cache { @@ -179,6 +183,9 @@ struct kvm_vcpu_arch { /* MMIO instruction details */ struct kvm_mmio_decode mmio_decode; + /* SBI context */ + struct kvm_sbi_context sbi_context; + /* Cache pages needed to program page tables with spinlock held */ struct kvm_mmu_page_cache mmu_page_cache; @@ -251,4 +258,7 @@ 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); +int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run); +int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run); + #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 := -Ivirt/kvm -Iarch/riscv/kvm kvm-objs := $(common-objs-y) kvm-objs += main.o vm.o vmid.o tlb.o mmu.o -kvm-objs += vcpu.o vcpu_exit.o vcpu_switch.o vcpu_timer.o +kvm-objs += vcpu.o vcpu_exit.o vcpu_switch.o vcpu_timer.o vcpu_sbi.o obj-$(CONFIG_KVM) += kvm.o diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index f1cda13bccfc..12f4ef4cefb0 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -867,6 +867,15 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) } } + /* Process SBI value returned from user-space */ + if (run->exit_reason == KVM_EXIT_RISCV_SBI) { + ret = kvm_riscv_vcpu_sbi_return(vcpu, vcpu->run); + if (ret) { + srcu_read_unlock(&vcpu->kvm->srcu, vcpu->arch.srcu_idx); + return ret; + } + } + if (run->immediate_exit) { srcu_read_unlock(&vcpu->kvm->srcu, vcpu->arch.srcu_idx); return -EINTR; diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index 8d0ae1a23b70..ac97a72bd29f 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -616,6 +616,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, ret = stage2_page_fault(vcpu, run, scause, stval, htval, htinst); break; + case EXC_SUPERVISOR_SYSCALL: + if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) + ret = kvm_riscv_vcpu_sbi_ecall(vcpu, run); + break; default: break; }; diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c new file mode 100644 index 000000000000..5b26a1793108 --- /dev/null +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0 +/** + * Copyright (c) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include +#include + +#define SBI_VERSION_MAJOR 0 +#define SBI_VERSION_MINOR 1 + +static void kvm_sbi_system_shutdown(struct kvm_vcpu *vcpu, + struct kvm_run *run, u32 type) +{ + int i; + struct kvm_vcpu *tmp; + + kvm_for_each_vcpu(i, tmp, vcpu->kvm) + tmp->arch.power_off = true; + kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP); + + memset(&run->system_event, 0, sizeof(run->system_event)); + run->system_event.type = type; + run->exit_reason = KVM_EXIT_SYSTEM_EVENT; +} + +static void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, + struct kvm_run *run) +{ + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + + vcpu->arch.sbi_context.return_handled = 0; + run->exit_reason = KVM_EXIT_RISCV_SBI; + run->riscv_sbi.extension_id = cp->a7; + run->riscv_sbi.function_id = cp->a6; + run->riscv_sbi.args[0] = cp->a0; + run->riscv_sbi.args[1] = cp->a1; + run->riscv_sbi.args[2] = cp->a2; + run->riscv_sbi.args[3] = cp->a3; + run->riscv_sbi.args[4] = cp->a4; + run->riscv_sbi.args[5] = cp->a5; + run->riscv_sbi.ret[0] = cp->a0; + run->riscv_sbi.ret[1] = cp->a1; +} + +int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + + /* Handle SBI return only once */ + if (vcpu->arch.sbi_context.return_handled) + return 0; + vcpu->arch.sbi_context.return_handled = 1; + + /* Update return values */ + cp->a0 = run->riscv_sbi.ret[0]; + cp->a1 = run->riscv_sbi.ret[1]; + + /* Move to next instruction */ + vcpu->arch.guest_context.sepc += 4; + + return 0; +} + +int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + int i, ret = 1; + u64 next_cycle; + struct kvm_vcpu *rvcpu; + bool next_sepc = true; + struct cpumask cm, hm; + ulong hmask, ut_scause = 0; + struct kvm *kvm = vcpu->kvm; + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + + if (!cp) + return -EINVAL; + + switch (cp->a7) { + case SBI_EXT_0_1_CONSOLE_GETCHAR: + case SBI_EXT_0_1_CONSOLE_PUTCHAR: + /* + * The CONSOLE_GETCHAR/CONSOLE_PUTCHAR SBI calls cannot be + * handled in kernel so we forward these to user-space + */ + kvm_riscv_vcpu_sbi_forward(vcpu, run); + next_sepc = false; + ret = 0; + break; + case SBI_EXT_0_1_SET_TIMER: +#if __riscv_xlen == 32 + next_cycle = ((u64)cp->a1 << 32) | (u64)cp->a0; +#else + next_cycle = (u64)cp->a0; +#endif + kvm_riscv_vcpu_timer_next_event(vcpu, next_cycle); + break; + case SBI_EXT_0_1_CLEAR_IPI: + kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_VS_SOFT); + break; + case SBI_EXT_0_1_SEND_IPI: + if (cp->a0) + hmask = kvm_riscv_vcpu_unpriv_read(vcpu, false, cp->a0, + &ut_scause); + else + hmask = (1UL << atomic_read(&kvm->online_vcpus)) - 1; + if (ut_scause) { + kvm_riscv_vcpu_trap_redirect(vcpu, ut_scause, + cp->a0); + next_sepc = false; + break; + } + for_each_set_bit(i, &hmask, BITS_PER_LONG) { + rvcpu = kvm_get_vcpu_by_id(vcpu->kvm, i); + kvm_riscv_vcpu_set_interrupt(rvcpu, IRQ_VS_SOFT); + } + break; + case SBI_EXT_0_1_SHUTDOWN: + kvm_sbi_system_shutdown(vcpu, run, KVM_SYSTEM_EVENT_SHUTDOWN); + next_sepc = false; + ret = 0; + break; + case SBI_EXT_0_1_REMOTE_FENCE_I: + case SBI_EXT_0_1_REMOTE_SFENCE_VMA: + case SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID: + if (cp->a0) + hmask = kvm_riscv_vcpu_unpriv_read(vcpu, false, cp->a0, + &ut_scause); + else + hmask = (1UL << atomic_read(&kvm->online_vcpus)) - 1; + if (ut_scause) { + kvm_riscv_vcpu_trap_redirect(vcpu, ut_scause, + cp->a0); + next_sepc = false; + break; + } + cpumask_clear(&cm); + for_each_set_bit(i, &hmask, BITS_PER_LONG) { + rvcpu = kvm_get_vcpu_by_id(vcpu->kvm, i); + if (rvcpu->cpu < 0) + continue; + cpumask_set_cpu(rvcpu->cpu, &cm); + } + riscv_cpuid_to_hartid_mask(&cm, &hm); + if (cp->a7 == SBI_EXT_0_1_REMOTE_FENCE_I) + sbi_remote_fence_i(cpumask_bits(&hm)); + else if (cp->a7 == SBI_EXT_0_1_REMOTE_SFENCE_VMA) + sbi_remote_hfence_vvma(cpumask_bits(&hm), + cp->a1, cp->a2); + else + sbi_remote_hfence_vvma_asid(cpumask_bits(&hm), + cp->a1, cp->a2, cp->a3); + break; + default: + /* Return error for unsupported SBI calls */ + cp->a0 = SBI_ERR_NOT_SUPPORTED; + break; + }; + + if (next_sepc) + cp->sepc += 4; + + return ret; +} diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 4b95f9a31a2f..2a8d582d46fe 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -236,6 +236,7 @@ struct kvm_hyperv_exit { #define KVM_EXIT_IOAPIC_EOI 26 #define KVM_EXIT_HYPERV 27 #define KVM_EXIT_ARM_NISV 28 +#define KVM_EXIT_RISCV_SBI 28 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -400,6 +401,13 @@ struct kvm_run { __u64 esr_iss; __u64 fault_ipa; } arm_nisv; + /* KVM_EXIT_RISCV_SBI */ + struct { + unsigned long extension_id; + unsigned long function_id; + unsigned long args[6]; + unsigned long ret[2]; + } riscv_sbi; /* Fix the size of the union. */ char padding[256]; }; -- 2.17.1