Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755047AbdHYIcc (ORCPT ); Fri, 25 Aug 2017 04:32:32 -0400 Received: from mail-db5eur01on0042.outbound.protection.outlook.com ([104.47.2.42]:44352 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754957AbdHYIcW (ORCPT ); Fri, 25 Aug 2017 04:32:22 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Florent.Revest@arm.com; From: Florent Revest To: linux-arm-kernel@lists.infradead.org Cc: matt@codeblueprint.co.uk, ard.biesheuvel@linaro.org, pbonzini@redhat.com, rkrcmar@redhat.com, christoffer.dall@linaro.org, catalin.marinas@arm.com, will.deacon@arm.com, mark.rutland@arm.com, marc.zyngier@arm.com, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, leif.lindholm@arm.com, revestflo@gmail.com, Florent Revest Subject: [RFC 02/11] KVM: arm64: Return an Unknown ID on unhandled HVC Date: Fri, 25 Aug 2017 09:31:32 +0100 Message-Id: <1503649901-5834-3-git-send-email-florent.revest@arm.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1503649901-5834-1-git-send-email-florent.revest@arm.com> References: <1503649901-5834-1-git-send-email-florent.revest@arm.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [217.140.96.140] X-ClientProxiedBy: HE1PR09CA0051.eurprd09.prod.outlook.com (2603:10a6:7:3c::19) To HE1PR08MB0812.eurprd08.prod.outlook.com (2a01:111:e400:59b1::30) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ef105230-a766-4c12-b221-08d4eb93c9a1 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:HE1PR08MB0812; X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;3:YsuGoD1ZZ9Ud1h4BN8oh+BsCBTkmj7WPZH12Xd6gFVZLceEKnxh9LvSuy+Mp9ADClkPn6dwnBjsXPSzANVrWDVWZKWZA8E9jd+JFO7EuhM6avoAyBsADpB2g2fi5I6catEYwgrnqvYETRr+8FOeNTuu1EBjpJtd7GqCLqN8lS0t7jdQgHjw7ALlJ+GdPSO1jt549ifyGaRfaC6oqwhvMktXmXvRC5E4b2E15ULmGnZrfR+XFvGJ2JlIrgzLxD+hA;25:r9dER6nDfpPuP5TfITk9ili89Rc600qobQmbBS1KVwLWLBCdLm3xH1TXoFnJiAXXWIpvB8JprqNEF3YoA0/KztIMt99cL6AxoEQTT59ZTcVcVbQfPrydNGNwtS260DRBXUei5sabIGgla/uWneKkez2a9+1ASl3aQT4T7LIQ+b+KH/Tq/BbfJKg9eHQgZrwsaIPmgCplK71AIZqidypHscqv6lvRXdTaLELdrSqff0wyj+sAqrLY0iYBScyJwHQj/HWFzcLQqKJZzkAo3vnHHvD6qw0U6530irACoo/Rl6uNTXquBgsXv00LLD27yxleBC/tUJmC2DADwgzf96LkNQ==;31:huPiSGLbnX/ZjVK3GkCRu5zcCaijkOvbARZhLgK3zJ8FNNVHwf810wbbda51sbqcPxoLNYceesGTgfp5BKXF8WfHvoIunZdbc6YrJutBT4GT0vL1jE4slBRPFFLNDe2XkOLnV5/dzuLeo9MukGFRwygBplSbRkzbK5xHy92ucnNxEkawL+Bo2uVjwncnzuPmhA8dfo49qRQdcDKc4F+QfBiF1rBO0RJu0ny8S+Fg+pI= X-MS-TrafficTypeDiagnostic: HE1PR08MB0812: X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;20:LNxZusOFqFq2GezL9raCMpkDUkM7V2yVLOyooSjMpONQp9xuiU7sb/XclqBpy2YFHySmgxPW6m/N7y+8KqjEXZO3gXvoTc9QfuMIxtmg5Lsd7qGGM+3gPNRePtDlPpEd4jx6qTzR5RCc9GUhkTLJuYNbVgqdAcp5/Fv4/+NksxhLFzHQsZYNmj+QA7hbjd/Cv09YJo4D0mRi4PrsYW0OSu3O3njimrs4sGfTLwFqkzqLmzJgdoAM3TAz80YO0vbR9FKo+9wkFlyfY5nypivTeaWOd2nI2ZSt4EBJyy7R0uDn9DN+DmSmdiayheQ8VTY6mpzgppbAQBpZZTKC1Jerxzw8KIdAdpeMP97HJx9Xcuqug0jRhVQe4fTfZZh30pUGL6vBoT5/cEwKG+SRWPkuV2pQYQVsyBRULzXhRecrltZ+JSckbGGiKT9ps+cEaVMszvEgJjc40NsVpIf3yOZAgcoL6VJQpRQfsV1HtGLycAeOYnpVFE9L7PvRd1OlvJaC;4:b1HUq316VBsVqcwymqKEOi17AkEPwZVOQaBaMihMo0vH06i3coik3qY7T7Rxdnx+bJjsmuvyEepFfOGDwwUoegPaPnglqF/u+QxdZBE9BB/0rho2voGMrpqlenl9OfPM+vEKS9tyw2sD9euPeUTyT19x0NsNjEc/pihRljNUMs0vThppMXb/iBfdMGJfxe8fECA7fH28k+SUpmI6CyKxIWkyZ+8xoTBq6ZELbWM/VqLPWBNggnKtGRPx6fHUKLDWLFewd4omftMFmWuK0WygSrhV4bNwKhMtnilqGYEo0C0= X-Exchange-Antispam-Report-Test: UriScan:(180628864354917); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(93006095)(93001095)(100000703101)(100105400095)(6055026)(6041248)(20161123560025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123564025)(20161123555025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:HE1PR08MB0812;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:HE1PR08MB0812; X-Forefront-PRVS: 041032FF37 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(7370300001)(6009001)(39860400002)(199003)(40434004)(189002)(478600001)(68736007)(76176999)(25786009)(5660300001)(97736004)(36756003)(6486002)(50226002)(47776003)(50986999)(50466002)(4326008)(48376002)(7350300001)(110136004)(66066001)(7416002)(8676002)(189998001)(305945005)(5890100001)(6916009)(2351001)(105586002)(42186005)(6116002)(2361001)(8746002)(3846002)(575784001)(86362001)(2906002)(72206003)(81156014)(81166006)(2950100002)(106356001)(6666003)(7736002)(33646002)(101416001)(5003940100001)(53936002);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1PR08MB0812;H:e106757-lin.cambridge.arm.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;HE1PR08MB0812;23:IDTjAf834PH+Kg9i5lr7i/QcZnbbJMTkSBJCjFcMq?= =?us-ascii?Q?zT2pHPKjzhDYqw+agfMEzYViXZ6uxQytpKtdSzEFJXI4XcyPDMYxa5nBIIwK?= =?us-ascii?Q?UKRNOpYcEShcw+C0hg59RZOvIiQVVX/sSNUzj7Axr7jimHojdrDd4/cuV6OU?= =?us-ascii?Q?nw+bR4K+smfjvNlNrODOKgkqnkV4T/lBGIznlJWjUcZ37FXsyj8KonJ2jAFM?= =?us-ascii?Q?rxw9IFtHGZ6ghP+a57KNE5VPpDHIzGLcvXgML+YPeKhFMYrHzb61MJxD+7eJ?= =?us-ascii?Q?ZIUjSN/O4nhRAfb5qtCowgVPosr7O5miguxUcr6TTHH2oxcLrUN7b9oaqiHV?= =?us-ascii?Q?Qs/mJK+7zN4oxYmPOwMDCsubRqWhYhyFim7U+0QdgXf8/qGObdKsR+XsBYS0?= =?us-asc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Diagnostics: 1;HE1PR08MB0812;6:Twcnlbxnwp+drq8p0alvCY2RhZLXRg64hyHBwml7wJFF1jPfUUG7yJRNiDt3wOcFed6zUkW0GGHyajieunDmyZX+Oy9WxINssxUWMmTnte2Ct1+pABndEoFNfXFtobRHeQ0PXgoMKr06tjwtfmzMi8nqTqBTRLCVXjmVHXbksLa9SYbpjp8wY7BmLNAHmhWRqZWonU1JP4rnUzjtVY/H6E4HwfZNuImFT4kQvmnKX+XXmB2LLSj0IE/eridHNttbsRm39dJmDZH8zuLVRzggrz6tHAPI2r9ZKvBZvdFZb7N8RUpey6JxSy5JDcrUjJnoQ9koK/Li05urBa6vbfq2Zw==;5:mjU14h2RQD3NNi0tJum0DNYofKwfbUDw8jR6QEc/Y9Pxg0/Mol042KpcscgYRrkk4Rz+KjNf3HEFTwb/n6wI/KU7emeDccliQGXuyPKIFfFy5f6naUKeVQip6DDMWOf3K3yWpup6ppqXtS/+/BFLVg==;24:rQ7bQnFSHSDBCZzF97lKbeEI2c4Pn5Ss4a7jBojLMjzrjf8FoZ9ZffKbgeJbEr2mOoW1mKPZ9P3kP2IqHIOpmlWLH+jS3K/KFIiy6vbPeZw=;7:PjEZgcV8DgWFP5gkftZhtcvRRFieWCtm/8bBrck7pddMbfdHhipA1CDWXrdQrsunZzHWAQtTuomIoXWYr4J6AAjayIUIfSLlJdbQMTuPJDn7aueh3ZswCKKHzXXpDQqWxBx/0qJ59TsxIIXs7SHJHwc9TNgedpnXyZbgvWGe7YzvQpfIl0zr45B2D/tvmyKiulZUhbFfmmDcxNJ3AZMAoLDhWJJPkIgZIck1EjNoN+U= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2017 08:32:13.0681 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR08MB0812 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by nfs id v7P8WdZZ009931 Content-Length: 5601 Lines: 146 So far, when the KVM hypervisor received an hvc from a guest, it only routed the hypercall to the PSCI calls handler. If the function ID of the hypercall wouldn't be supported by the PSCI code, a PSCI_RET_NOT_SUPPORTED error code would be returned in x0. This patch introduces a kvm_psci_is_call() check which is verified before entering the PSCI calls handling code. The HVC is now only routed to the PSCI code if its function ID is in the ranges of PSCI functions defined by SMCCC (0x84000000-0x8400001f and 0xc4000000-0xc400001f). If the function ID is not in those ranges, an Unknown Function Identifier is returned in x0. This implements the behavior defined by SMCCC and paves the way for other hvc handlers. Signed-off-by: Florent Revest --- arch/arm/include/asm/kvm_psci.h | 1 + arch/arm64/include/asm/kvm_psci.h | 1 + arch/arm64/kvm/handle_exit.c | 24 ++++++++++++++++++------ include/uapi/linux/psci.h | 2 ++ virt/kvm/arm/psci.c | 21 +++++++++++++++++++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/arch/arm/include/asm/kvm_psci.h b/arch/arm/include/asm/kvm_psci.h index 6bda945..8dcd642 100644 --- a/arch/arm/include/asm/kvm_psci.h +++ b/arch/arm/include/asm/kvm_psci.h @@ -22,6 +22,7 @@ #define KVM_ARM_PSCI_0_2 2 int kvm_psci_version(struct kvm_vcpu *vcpu); +bool kvm_psci_is_call(struct kvm_vcpu *vcpu); int kvm_psci_call(struct kvm_vcpu *vcpu); #endif /* __ARM_KVM_PSCI_H__ */ diff --git a/arch/arm64/include/asm/kvm_psci.h b/arch/arm64/include/asm/kvm_psci.h index bc39e55..1a28809 100644 --- a/arch/arm64/include/asm/kvm_psci.h +++ b/arch/arm64/include/asm/kvm_psci.h @@ -22,6 +22,7 @@ #define KVM_ARM_PSCI_0_2 2 int kvm_psci_version(struct kvm_vcpu *vcpu); +bool kvm_psci_is_call(struct kvm_vcpu *vcpu); int kvm_psci_call(struct kvm_vcpu *vcpu); #endif /* __ARM64_KVM_PSCI_H__ */ diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 17d8a16..bc7ade5 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -34,19 +35,30 @@ typedef int (*exit_handle_fn)(struct kvm_vcpu *, struct kvm_run *); +/* + * handle_hvc - handle a guest hypercall + * + * @vcpu: the vcpu pointer + * @run: access to the kvm_run structure for results + * + * Route a given hypercall to its right HVC handler thanks to its function ID. + * If no corresponding handler is found, write an Unknown ID in x0 (cf. SMCCC). + * + * This function returns: > 0 (success), 0 (success but exit to user + * space), and < 0 (errors) + */ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) { - int ret; + int ret = 1; trace_kvm_hvc_arm64(*vcpu_pc(vcpu), vcpu_get_reg(vcpu, 0), kvm_vcpu_hvc_get_imm(vcpu)); vcpu->stat.hvc_exit_stat++; - ret = kvm_psci_call(vcpu); - if (ret < 0) { - kvm_inject_undefined(vcpu); - return 1; - } + if (kvm_psci_is_call(vcpu)) + ret = kvm_psci_call(vcpu); + else + vcpu_set_reg(vcpu, 0, SMCCC_STD_RET_UNKNOWN_ID); return ret; } diff --git a/include/uapi/linux/psci.h b/include/uapi/linux/psci.h index 3d7a0fc..79704fe 100644 --- a/include/uapi/linux/psci.h +++ b/include/uapi/linux/psci.h @@ -24,10 +24,12 @@ /* PSCI v0.2 interface */ #define PSCI_0_2_FN_BASE 0x84000000 #define PSCI_0_2_FN(n) (PSCI_0_2_FN_BASE + (n)) +#define PSCI_0_2_FN_END PSCI_0_2_FN(0x1F) #define PSCI_0_2_64BIT 0x40000000 #define PSCI_0_2_FN64_BASE \ (PSCI_0_2_FN_BASE + PSCI_0_2_64BIT) #define PSCI_0_2_FN64(n) (PSCI_0_2_FN64_BASE + (n)) +#define PSCI_0_2_FN64_END PSCI_0_2_FN64(0x1F) #define PSCI_0_2_FN_PSCI_VERSION PSCI_0_2_FN(0) #define PSCI_0_2_FN_CPU_SUSPEND PSCI_0_2_FN(1) diff --git a/virt/kvm/arm/psci.c b/virt/kvm/arm/psci.c index f1e363b..9602894 100644 --- a/virt/kvm/arm/psci.c +++ b/virt/kvm/arm/psci.c @@ -332,3 +332,24 @@ int kvm_psci_call(struct kvm_vcpu *vcpu) return -EINVAL; }; } + +/** + * kvm_psci_is_call - checks if a HVC function ID is in a PSCI range + * @vcpu: Pointer to the VCPU struct + * + * When a hypercall is received from a guest. The SMCCC defines a function ID + * as a value to be put in x0 to identify the destination of the call. The same + * document defines ranges of function IDs to be used by PSCI. This function + * checks whether a given vcpu is requesting a PSCI related handler. + * + * This function returns: + * - true if this HVC should be handled by kvm_psci_call + * - false if it shouldn't + */ +inline bool kvm_psci_is_call(struct kvm_vcpu *vcpu) +{ + unsigned long fn = vcpu_get_reg(vcpu, 0) & ~((u32) 0); + + return ((fn >= PSCI_0_2_FN_BASE && fn <= PSCI_0_2_FN_END) || + (fn >= PSCI_0_2_FN64_BASE && fn <= PSCI_0_2_FN64_END)); +} -- 1.9.1 IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.