Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3424777pxb; Mon, 16 Nov 2020 14:29:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJziPeMGlSGis42jKfpPmQu8tl1pC92SOqqqqg8qzON6KU+SDPE4HZjCQLKiItPgpGsUFwrv X-Received: by 2002:a05:6402:88d:: with SMTP id e13mr18413055edy.366.1605565779809; Mon, 16 Nov 2020 14:29:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605565779; cv=none; d=google.com; s=arc-20160816; b=oNXkTRHX3XQwMvdr/ff6ulXmUPXqSCpTTGRNkBe9RDwNgZX52oDTYc+Oh8iBar5XFJ O4e/9VAQrVm8sdQ0eOXYVVTCjkceRycz8aRrCrKmrXP1DssFWLkiwyl0CIWI/mPt67ft /fg8nbs0YHdccY8C0nFk0JzbUTI0jXkuIV2SoYO09oJDrCHWdXrhjfNN66yFQBn0vhPJ 25tGj31pEwIT6DVCeX4R78XV6Mm9UU6AR4CoWyT02VVzDVQMh4ViciNwAH+BZhO/fR1E X8CUugKnvBghsz+NMmvjQDKkzYjVZsu9wBFaplYglnZdnnEorwr7irHMSRRCSOppeY05 EUwQ== 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=vhNNUDRNWLolF9X/BNiqAa9sQtbg1yNfQgpeLP8CpL8=; b=JfSiSdxDw3UvAq+G0YUaBghrcIe4fedX15BPNGWK4xLLDUkr34JboSa25KoYf6LcpJ 4asBtyLK8Ouiz4sZaEhKPBols/3VVBm0HzHTDjBuzjJAHbV5XKwkhqj1LZ1N9uNOP+q3 LJLTdSL/xfCmCPapm+PCEzMk1FluOmL6rqGTipSGGeBmWpwcbjdyvQdS6juFNJkvxX25 eY32sAlmx0Kch+nn4yWxPv8z4XHbxf5yvU1LRPCF805rv+Fx6VZMx395LH6MIDUKOOr0 ZaRdKgao2fuH/2Cy7aUMtObwUtqMtY03f81PI5ncyT/csABwajHQ3Hv1okEs6YrTMWmk aHFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AjQCgpzw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 63si13368366edb.454.2020.11.16.14.29.17; Mon, 16 Nov 2020 14:29:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AjQCgpzw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387603AbgKPUnn (ORCPT + 99 others); Mon, 16 Nov 2020 15:43:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733259AbgKPUnl (ORCPT ); Mon, 16 Nov 2020 15:43:41 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E282C0613CF for ; Mon, 16 Nov 2020 12:43:41 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id p22so593334wmg.3 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=reMd14kF09ytALN7E2D2DU2hODp5X4OoAo3CUhFnHBU3P23kYZhiAu63UO8c8MKMA2 hKl4O9sVpmrNsfFkku2lSzK+DXa6X0ZhMvhiKAkmbqY2gzSc5SBZfPItxjQvO5Iv+60e R4scjUWkuD/Pqs17f8+vEH1hkY7BrCd/XyI6wu+cwMW7qf5w6Qt9HGycTXLq/mOcbTwy z8IyOCMZjn4Lrp8/PNCim3+5NtDyVbQXxu+D6IIFhMn8h/zilQUWEPuALLEDfpNH638j WqWF2OeyEpsVArzwmEPpaJEch9YKZycm1CcJmd10Q0UDdjmdzJhgZ48E1Mazd3efo+8+ bkdw== X-Gm-Message-State: AOAM532AguBFAd3X0Ypb9uDsjLVPuRnhYuwQZKj91qCdGSKtF1vTVMOk nY5v1BWQ1cjauRER0N1JRqblFA== 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 Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , Catalin Marinas , Will Deacon , Dennis Zhou , Tejun Heo , Christoph Lameter , Mark Rutland , Lorenzo Pieralisi , Quentin Perret , Andrew Scull , Andrew Walbran , kernel-team@android.com, David Brazdil 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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(); } -- 2.29.2.299.gdc1121823c-goog