From patchwork Fri Oct 20 07:21:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 155855 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp881464vqb; Fri, 20 Oct 2023 00:22:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGTeodyyPH0C0uKKNLH0IcLwCdOOh4aSDemNYYfiluKwPfqWaZex7r/CU5LEw5FVwtnUcG/ X-Received: by 2002:a05:6a21:3b4a:b0:14c:d494:77c3 with SMTP id zy10-20020a056a213b4a00b0014cd49477c3mr962462pzb.33.1697786571755; Fri, 20 Oct 2023 00:22:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697786571; cv=none; d=google.com; s=arc-20160816; b=lJCC7sJcbrYttbvlMsRhuq4pa+7T4JsmPOdZPKmy3WWavy+qy0jHxrRAxFppSkxGE/ F1el2vYVXOtLe/Pn6rx96UVZYu5RBi+PnTFdIT1vjAvdKg5nA+k8ibNOwvFWsjOiU3Pq stns5UsaSdTe8K/rdwR9vJI61ukXMRveh8JOgKzlMZUOAw/4efb09ZSid0weXpv1dYt4 Hy2d5w5LARtZffOtI+4XqhPWLJAGNzQCIV2L9dPk2NfIEBP/1lMc+IUcC/l9o3xDfUvS GOU9ma7x9HBP/X6t+G3x2tM9RpUn8BGfFaUa+G0fB1y7XhXG8zrOAP1YMgDmkMu3VA+B 86BQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hO8QQxSKIIq22xFRYvu7Gu8eaqjv2hPcSOTMD/z9e94=; fh=7GYORjoWw1oF4BCDuq40hd+MOEKRwzNqXwnZ/K2vS9Q=; b=GaD5JFnEh1ii3rTncZDCW8BzxBxwtMjX41XS8UedneVVc/7xpXtWvps2Fczn32iQnl J2sgdqpHNv4ZuvEAXgN8hma0RMZmaBwG3lzC/jxtohIFyLtu2VLYcjGkMCn7+PP2Rnf7 uKE5eUpDBwDpSYbXL9FEhA8HOB6RRMIEDyksFtUiQApiQW4r5fH2aDuPKOh39QZgoHRA 7k9M/JEGnqstx6My6tb432mDqT0EGHDRZLTWTQrWGf6LxgGEZ76t6u+go4VqSFH9/Z6x VaBX0aJDyMP0Uq0F/r9cEJZWAS9GUSVtD4IPfcyocxI5Y2PnA6Fpxs8NGezEanWlqX2x s3Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=NF++K+IH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id w4-20020a170902e88400b001c9c9e6371fsi1245095plg.527.2023.10.20.00.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 00:22:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=NF++K+IH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5A4D680842CE; Fri, 20 Oct 2023 00:22:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376430AbjJTHWX (ORCPT + 25 others); Fri, 20 Oct 2023 03:22:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376415AbjJTHWQ (ORCPT ); Fri, 20 Oct 2023 03:22:16 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E42C10CC for ; Fri, 20 Oct 2023 00:22:08 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6bee11456baso463223b3a.1 for ; Fri, 20 Oct 2023 00:22:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1697786528; x=1698391328; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hO8QQxSKIIq22xFRYvu7Gu8eaqjv2hPcSOTMD/z9e94=; b=NF++K+IHtiFmgzrSHLZgrw41LakLY8FUKgQny7rh/tQlqYxqoIAw3AKCFRiZWavtif BMMFkeFZSDxixW4/Yb0ITH9n5zesJUE1Z1JbWE7+0ZUa9LzP7dR7BxshCbs9//6+Rs4U YMotsWDeueiyVfEDFKm1T8pyaltg5uHQG2SN+fxJIWrQSIPkDH1qxonOfwBpU2WK1LUM YOMXwJQhxG6JK0bJPmX7Etl3NMvR52RLmYtSjeE2LPYNaLyqSuqaGtWIEavZ3j3j5VwN rcUiu4zoUjtplmeGYDlwe2Yuh/Cw1kPLDbCKH1PS0yqmRDHsGryReQM+2Vp2wXmLCNBc cRQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697786528; x=1698391328; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hO8QQxSKIIq22xFRYvu7Gu8eaqjv2hPcSOTMD/z9e94=; b=a83/uHoAqh15UoDhs4s0CoIdaDx9lQ1h3pYZ5exjp43ejlyRwbL0oiiYVyyxSgNr3l UNc6sV3unFQ95jhUScdfEcT+kHNoUOZVq83F1XcX+jyfa6aWpwUwr3E+N76FaY0kkX3v KFi535/EHOMOlOboCCEsPgpy9SKToJRFIZeBAQFjZMIyqwlHYNv46VxlXFcUtkuY2xT0 I37qSUhGE0xu1dXrTcJQTQOhgGZ7TuNKV6+0dT5JBnyWNV5mC7oGditTJo1queaZgOlg 122syQMxzU61eBAB8+oB0F+hkK2DDP70yZuv1nPjZKZuJc5kTbw6s9InUjMEyxKBcQ0s BSyA== X-Gm-Message-State: AOJu0YwQrF8T4y4tftEX/1mEIM0+BnoI5HZuvcCc2318kWxM2RK0ImEN OTq5HP9qoBI0EW1O5o/ZNeAhkg== X-Received: by 2002:aa7:9622:0:b0:68e:42c9:74e0 with SMTP id r2-20020aa79622000000b0068e42c974e0mr952140pfg.3.1697786528093; Fri, 20 Oct 2023 00:22:08 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([171.76.83.81]) by smtp.gmail.com with ESMTPSA id v12-20020a63f20c000000b005b32d6b4f2fsm828204pgh.81.2023.10.20.00.22.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 00:22:07 -0700 (PDT) From: Anup Patel To: Paolo Bonzini , Atish Patra , Palmer Dabbelt , Paul Walmsley , Greg Kroah-Hartman , Jiri Slaby Cc: Conor Dooley , Andrew Jones , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-serial@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Anup Patel Subject: [PATCH v3 4/9] RISC-V: KVM: Forward SBI DBCN extension to user-space Date: Fri, 20 Oct 2023 12:51:35 +0530 Message-Id: <20231020072140.900967-5-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020072140.900967-1-apatel@ventanamicro.com> References: <20231020072140.900967-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 20 Oct 2023 00:22:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780258251916328942 X-GMAIL-MSGID: 1780258251916328942 The frozen SBI v2.0 specification defines the SBI debug console (DBCN) extension which replaces the legacy SBI v0.1 console functions namely sbi_console_getchar() and sbi_console_putchar(). The SBI DBCN extension needs to be emulated in the KVM user-space (i.e. QEMU-KVM or KVMTOOL) so we forward SBI DBCN calls from KVM guest to the KVM user-space which can then redirect the console input/output to wherever it wants (e.g. telnet, file, stdio, etc). The SBI debug console is simply a early console available to KVM guest for early prints and it does not intend to replace the proper console devices such as 8250, VirtIO console, etc. Signed-off-by: Anup Patel Reviewed-by: Andrew Jones --- arch/riscv/include/asm/kvm_vcpu_sbi.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/vcpu_sbi.c | 4 ++++ arch/riscv/kvm/vcpu_sbi_replace.c | 32 +++++++++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index c02bda5559d7..6a453f7f8b56 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -73,6 +73,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_ipi; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor; diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index 917d8cc2489e..60d3b21dead7 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -156,6 +156,7 @@ enum KVM_RISCV_SBI_EXT_ID { KVM_RISCV_SBI_EXT_PMU, KVM_RISCV_SBI_EXT_EXPERIMENTAL, KVM_RISCV_SBI_EXT_VENDOR, + KVM_RISCV_SBI_EXT_DBCN, KVM_RISCV_SBI_EXT_MAX, }; diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index bda8b0b33343..a04ff98085d9 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -66,6 +66,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = { .ext_idx = KVM_RISCV_SBI_EXT_PMU, .ext_ptr = &vcpu_sbi_ext_pmu, }, + { + .ext_idx = KVM_RISCV_SBI_EXT_DBCN, + .ext_ptr = &vcpu_sbi_ext_dbcn, + }, { .ext_idx = KVM_RISCV_SBI_EXT_EXPERIMENTAL, .ext_ptr = &vcpu_sbi_ext_experimental, diff --git a/arch/riscv/kvm/vcpu_sbi_replace.c b/arch/riscv/kvm/vcpu_sbi_replace.c index 7c4d5d38a339..23b57c931b15 100644 --- a/arch/riscv/kvm/vcpu_sbi_replace.c +++ b/arch/riscv/kvm/vcpu_sbi_replace.c @@ -175,3 +175,35 @@ const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst = { .extid_end = SBI_EXT_SRST, .handler = kvm_sbi_ext_srst_handler, }; + +static int kvm_sbi_ext_dbcn_handler(struct kvm_vcpu *vcpu, + struct kvm_run *run, + struct kvm_vcpu_sbi_return *retdata) +{ + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + unsigned long funcid = cp->a6; + + switch (funcid) { + case SBI_EXT_DBCN_CONSOLE_WRITE: + case SBI_EXT_DBCN_CONSOLE_READ: + case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE: + /* + * The SBI debug console functions are unconditionally + * forwarded to the userspace. + */ + kvm_riscv_vcpu_sbi_forward(vcpu, run); + retdata->uexit = true; + break; + default: + retdata->err_val = SBI_ERR_NOT_SUPPORTED; + } + + return 0; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn = { + .extid_start = SBI_EXT_DBCN, + .extid_end = SBI_EXT_DBCN, + .default_unavail = true, + .handler = kvm_sbi_ext_dbcn_handler, +};