From patchwork Mon Nov 16 20:43:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 11910807 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9F7DC61DD8 for ; Mon, 16 Nov 2020 20:47:05 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 903F122245 for ; Mon, 16 Nov 2020 20:47:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="2qncckYJ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="AjQCgpzw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 903F122245 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=qftv7u1XIfBze4bfHgkbS+N6t3cPq1mlH3VS5YVq0m4=; b=2qncckYJcHGxJShu6eDc7MQXK ri/igVmMaVLTOxdull8Ax3NowOXfj7UNC7CaCw5UToAfyAoHr4HphyvKm3nusOYYg9UE143eJBWXU Od/C6sKnSk5fwKmMXr2M/Cvusfp4+ynImbSFz60xvEhaJOY+XmSx2Sq1pyad8JzwsigRemQ2YHSFi LXgxO4KGf7FGW8Pj46X7BFJ6Xz7yb2FSjgUOEIZ7y5r5d+JkJc0Ncfla83dXkEZvclfCJgYbUUcc3 N1e243HP1G/UBiJU0ssqtcbhJ/b70qo5SW1I/oXZJRi36yzVwSRuPKAayIO8ERJhrETtrFs7vBWDo ClxklbWjg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kelN7-0008GI-Lt; Mon, 16 Nov 2020 20:45:09 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kelLh-0007h5-7E for linux-arm-kernel@lists.infradead.org; Mon, 16 Nov 2020 20:43:42 +0000 Received: by mail-wm1-x342.google.com with SMTP id 19so607703wmf.1 for ; Mon, 16 Nov 2020 12:43:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vhNNUDRNWLolF9X/BNiqAa9sQtbg1yNfQgpeLP8CpL8=; b=AjQCgpzwB7upMi2bhD5FEdm5oBzMrdG1EqiZA29adsCIyhJ5kg2kBmwLY9kTrIU5Y9 Cvskk/ELHGvgyg0p3Z81q1cUnW6e5owkv8bTXqRXAdakh3ydKT4uyUIZlbKYq988lYVb 4e+T3m4CKr7dHF0Z0mGi2QceuVtfQ0eezmaW3ww4QSXAcg6JsFBSRes4WJCKwimfbrxT thYGudU6wwJ3Mi/6ymuWAFcM7/xBWSR/sUmtHzlTDWEgG9mH1H+Xr6UFV6cnRBy1DrIY bJWgfAVKFE2Y4i9x+qzdKpj/it9JPSW1/wYkZ3LfTSq4DYrWec5bLCtVLqJ5tNqcjWwq oImQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vhNNUDRNWLolF9X/BNiqAa9sQtbg1yNfQgpeLP8CpL8=; b=uFt8q/pYsMvm4MicA9WFutNpAptA03GOphCnE+/eCpWjOiY7CXiN1RuZyg6cPE0HBI m+HLVBm6V7CHn8hVe1Qph+6u+n3UpDJfptRrUBt5AJH10CMsZQj2MK14hQjLNeaAlsOn QFnvGcE5wN+mb+aBfgb36xOhUT63WyR5OINt4YLbYcfKiVp4c/T3ssXrlnvzngcFVAKd ovKMLvzU+5TDV96Ugq8PL9Eg6knQecQXV2ZQVgjw6M02n/0XKezlr6psDSk7arao+pUh AmiXZEiWC9+htaQLiBSM6DTOCM47A13WnA/Q5rW+SUHERwLVy2yPpsgWV4+uSj0hfE60 7wvQ== X-Gm-Message-State: AOAM530QXlysz9UwWHkaDgzQuLg5pSCpWsFDLU87pxWq2Ro0PFEPY68f phBObLvNucEyVgpj5ivgynY0Kw== X-Google-Smtp-Source: ABdhPJx/ACVuZVlLM8jprNldFukYuBbBqcWgl8oJWqwhavCewWZd3bKxh25HsPT4zSX+nPVpBpe87g== X-Received: by 2002:a1c:e442:: with SMTP id b63mr790926wmh.10.1605559420043; Mon, 16 Nov 2020 12:43:40 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:bc40:bd71:373a:1b33]) by smtp.gmail.com with ESMTPSA id b14sm24646282wrx.35.2020.11.16.12.43.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Nov 2020 12:43:39 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v2 08/24] kvm: arm64: Add SMC handler in nVHE EL2 Date: Mon, 16 Nov 2020 20:43:02 +0000 Message-Id: <20201116204318.63987-9-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201116204318.63987-1-dbrazdil@google.com> References: <20201116204318.63987-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201116_154341_362362_3146D938 X-CRM114-Status: GOOD ( 17.93 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , kernel-team@android.com, Lorenzo Pieralisi , Andrew Walbran , Suzuki K Poulose , Marc Zyngier , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry , Andrew Scull Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add handler of host SMCs in KVM nVHE trap handler. Forward all SMCs to EL3 and propagate the result back to EL1. This is done in preparation for validating host SMCs in KVM nVHE protected mode. The implementation assumes that firmware uses SMCCC v1.2 or older. That means x0-x17 can be used both for arguments and results, other GPRs are preserved. Signed-off-by: David Brazdil --- arch/arm64/kvm/hyp/nvhe/host.S | 38 ++++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 26 ++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index ed27f06a31ba..52dae5cd5a28 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -183,3 +183,41 @@ SYM_CODE_START(__kvm_hyp_host_vector) invalid_host_el1_vect // FIQ 32-bit EL1 invalid_host_el1_vect // Error 32-bit EL1 SYM_CODE_END(__kvm_hyp_host_vector) + +/* + * Forward SMC with arguments in struct kvm_cpu_context, and + * store the result into the same struct. Assumes SMCCC 1.2 or older. + * + * x0: struct kvm_cpu_context* + */ +SYM_CODE_START(__kvm_hyp_host_forward_smc) + /* + * Use x18 to keep a pointer to the host context because x18 + * is callee-saved SMCCC but not in AAPCS64. + */ + mov x18, x0 + + ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)] + ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)] + ldp x4, x5, [x18, #CPU_XREG_OFFSET(4)] + ldp x6, x7, [x18, #CPU_XREG_OFFSET(6)] + ldp x8, x9, [x18, #CPU_XREG_OFFSET(8)] + ldp x10, x11, [x18, #CPU_XREG_OFFSET(10)] + ldp x12, x13, [x18, #CPU_XREG_OFFSET(12)] + ldp x14, x15, [x18, #CPU_XREG_OFFSET(14)] + ldp x16, x17, [x18, #CPU_XREG_OFFSET(16)] + + smc #0 + + stp x0, x1, [x18, #CPU_XREG_OFFSET(0)] + stp x2, x3, [x18, #CPU_XREG_OFFSET(2)] + stp x4, x5, [x18, #CPU_XREG_OFFSET(4)] + stp x6, x7, [x18, #CPU_XREG_OFFSET(6)] + stp x8, x9, [x18, #CPU_XREG_OFFSET(8)] + stp x10, x11, [x18, #CPU_XREG_OFFSET(10)] + stp x12, x13, [x18, #CPU_XREG_OFFSET(12)] + stp x14, x15, [x18, #CPU_XREG_OFFSET(14)] + stp x16, x17, [x18, #CPU_XREG_OFFSET(16)] + + ret +SYM_CODE_END(__kvm_hyp_host_forward_smc) diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index 19332c20fcde..71a17af05953 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -16,6 +16,8 @@ DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); +extern void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt); + static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) { unsigned long func_id = host_ctxt->regs.regs[0]; @@ -106,6 +108,27 @@ static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) host_ctxt->regs.regs[1] = ret; } +static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt) +{ + __kvm_hyp_host_forward_smc(host_ctxt); +} + +static void skip_host_instruction(void) +{ + write_sysreg_el2(read_sysreg_el2(SYS_ELR) + 4, SYS_ELR); +} + +static void handle_host_smc(struct kvm_cpu_context *host_ctxt) +{ + default_host_smc_handler(host_ctxt); + + /* + * Unlike HVC, the return address of an SMC is the instruction's PC. + * Move the return address past the instruction. + */ + skip_host_instruction(); +} + void handle_trap(struct kvm_cpu_context *host_ctxt) { u64 esr = read_sysreg_el2(SYS_ESR); @@ -114,6 +137,9 @@ void handle_trap(struct kvm_cpu_context *host_ctxt) case ESR_ELx_EC_HVC64: handle_host_hcall(host_ctxt); break; + case ESR_ELx_EC_SMC64: + handle_host_smc(host_ctxt); + break; default: hyp_panic(); }