From nobody Tue May 7 03:03:40 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1571404052; cv=none; d=zoho.com; s=zohoarc; b=BUyF5zmjl+/U74Jv8oVUYfNLd2gk2yoyMshcB/2NBW9zpM8lrMqqQb3dCWrcgEmhQIAYMmpUwB0271fa/aGc7+UszkVjTuPMyDv/PQhCSYTQu48YbVhi7knlvvsA7+E9sYCc3CB7Wuys6f/QLnj1BaFXcL4Lpz1skBxMnflOHEI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571404052; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=cc1fu+rEog1Sogj+5Hgd0cRCKCbzkHAuM+4eo6lcw7k=; b=n7J21trqY0ZJWb5t9ksh+FJxLiIapCyOPCh5aViZB5LYKcAFzNf7cl8PzdFFif93ZXDZh0VqSgF8AlGoAIoPQDfNtcNgjjt79tKUBya8keF8gFT+24qKkvOZhxzHlxW/1SQldLbKWnG4QDAfnZWw6OQmzvAro9md0iFrHRtlZ0c= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1571404052520316.09198278107203; Fri, 18 Oct 2019 06:07:32 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iLRw5-0000rr-5f; Fri, 18 Oct 2019 13:04:53 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iLRgP-0007nG-OU for xen-devel@lists.xenproject.org; Fri, 18 Oct 2019 12:48:41 +0000 Received: from Galois.linutronix.de (unknown [2a0a:51c0:0:12e:550::1]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 97e500f0-f1a5-11e9-beca-bc764e2007e4; Fri, 18 Oct 2019 12:48:33 +0000 (UTC) Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1iLRg3-00078L-5U; Fri, 18 Oct 2019 14:48:19 +0200 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id AADD51C03AB; Fri, 18 Oct 2019 14:48:18 +0200 (CEST) X-Inumbo-ID: 97e500f0-f1a5-11e9-beca-bc764e2007e4 Date: Fri, 18 Oct 2019 12:48:18 -0000 From: "tip-bot2 for Masami Hiramatsu" To: linux-tip-commits@vger.kernel.org In-Reply-To: <156777564986.25081.4964537658500952557.stgit@devnote2> References: <156777564986.25081.4964537658500952557.stgit@devnote2> MIME-Version: 1.0 Message-ID: <157140289854.29376.4430795054323201093.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1, SHORTCIRCUIT=-0.0001 X-Mailman-Approved-At: Fri, 18 Oct 2019 13:04:51 +0000 Subject: [Xen-devel] [tip: x86/core] x86: xen: insn: Decode Xen and KVM emulate-prefix signature X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Reply-To: linux-kernel@vger.kernel.org Cc: Juergen Gross , Stefano Stabellini , "Peter Zijlstra \(Intel\)" , Andrew Cooper , x86@kernel.org, linux-kernel@vger.kernel.org, Borislav Petkov , Masami Hiramatsu , Josh Poimboeuf , xen-devel@lists.xenproject.org, Boris Ostrovsky , Randy Dunlap , Ingo Molnar Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" The following commit has been merged into the x86/core branch of tip: Commit-ID: 4d65adfcd1196818659d3bd9b42dccab291e1751 Gitweb: https://git.kernel.org/tip/4d65adfcd1196818659d3bd9b42dccab2= 91e1751 Author: Masami Hiramatsu AuthorDate: Fri, 06 Sep 2019 22:14:10 +09:00 Committer: Peter Zijlstra CommitterDate: Thu, 17 Oct 2019 21:31:57 +02:00 x86: xen: insn: Decode Xen and KVM emulate-prefix signature Decode Xen and KVM's emulate-prefix signature by x86 insn decoder. It is called "prefix" but actually not x86 instruction prefix, so this adds insn.emulate_prefix_size field instead of reusing insn.prefixes. If x86 decoder finds a special sequence of instructions of XEN_EMULATE_PREFIX and 'ud2a; .ascii "kvm"', it just counts the length, set insn.emulate_prefix_size and fold it with the next instruction. In other words, the signature and the next instruction is treated as a single instruction. Signed-off-by: Masami Hiramatsu Signed-off-by: Peter Zijlstra (Intel) Acked-by: Josh Poimboeuf Cc: Juergen Gross Cc: x86@kernel.org Cc: Boris Ostrovsky Cc: Ingo Molnar Cc: Stefano Stabellini Cc: Andrew Cooper Cc: Borislav Petkov Cc: xen-devel@lists.xenproject.org Cc: Randy Dunlap Link: https://lkml.kernel.org/r/156777564986.25081.4964537658500952557.stgi= t@devnote2 --- arch/x86/include/asm/insn.h | 6 ++++- arch/x86/lib/insn.c | 34 ++++++++++++++++++++- tools/arch/x86/include/asm/emulate_prefix.h | 14 ++++++++- tools/arch/x86/include/asm/insn.h | 6 ++++- tools/arch/x86/lib/insn.c | 34 ++++++++++++++++++++- tools/objtool/sync-check.sh | 3 +- tools/perf/check-headers.sh | 3 +- 7 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 tools/arch/x86/include/asm/emulate_prefix.h diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index 154f27b..5c1ae3e 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -45,6 +45,7 @@ struct insn { struct insn_field immediate2; /* for 64bit imm or seg16 */ }; =20 + int emulate_prefix_size; insn_attr_t attr; unsigned char opnd_bytes; unsigned char addr_bytes; @@ -128,6 +129,11 @@ static inline int insn_is_evex(struct insn *insn) return (insn->vex_prefix.nbytes =3D=3D 4); } =20 +static inline int insn_has_emulate_prefix(struct insn *insn) +{ + return !!insn->emulate_prefix_size; +} + /* Ensure this instruction is decoded completely */ static inline int insn_complete(struct insn *insn) { diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c index 0b5862b..4042795 100644 --- a/arch/x86/lib/insn.c +++ b/arch/x86/lib/insn.c @@ -13,6 +13,8 @@ #include #include =20 +#include + /* Verify next sizeof(t) bytes can be on the same instruction */ #define validate_next(t, insn, n) \ ((insn)->next_byte + sizeof(t) + n <=3D (insn)->end_kaddr) @@ -58,6 +60,36 @@ void insn_init(struct insn *insn, const void *kaddr, int= buf_len, int x86_64) insn->addr_bytes =3D 4; } =20 +static const insn_byte_t xen_prefix[] =3D { __XEN_EMULATE_PREFIX }; +static const insn_byte_t kvm_prefix[] =3D { __KVM_EMULATE_PREFIX }; + +static int __insn_get_emulate_prefix(struct insn *insn, + const insn_byte_t *prefix, size_t len) +{ + size_t i; + + for (i =3D 0; i < len; i++) { + if (peek_nbyte_next(insn_byte_t, insn, i) !=3D prefix[i]) + goto err_out; + } + + insn->emulate_prefix_size =3D len; + insn->next_byte +=3D len; + + return 1; + +err_out: + return 0; +} + +static void insn_get_emulate_prefix(struct insn *insn) +{ + if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix))) + return; + + __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix)); +} + /** * insn_get_prefixes - scan x86 instruction prefix bytes * @insn: &struct insn containing instruction @@ -76,6 +108,8 @@ void insn_get_prefixes(struct insn *insn) if (prefixes->got) return; =20 + insn_get_emulate_prefix(insn); + nb =3D 0; lb =3D 0; b =3D peek_next(insn_byte_t, insn); diff --git a/tools/arch/x86/include/asm/emulate_prefix.h b/tools/arch/x86/i= nclude/asm/emulate_prefix.h new file mode 100644 index 0000000..70f5b98 --- /dev/null +++ b/tools/arch/x86/include/asm/emulate_prefix.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_EMULATE_PREFIX_H +#define _ASM_X86_EMULATE_PREFIX_H + +/* + * Virt escape sequences to trigger instruction emulation; + * ideally these would decode to 'whole' instruction and not destroy + * the instruction stream; sadly this is not true for the 'kvm' one :/ + */ + +#define __XEN_EMULATE_PREFIX 0x0f,0x0b,0x78,0x65,0x6e /* ud2 ; .ascii "x= en" */ +#define __KVM_EMULATE_PREFIX 0x0f,0x0b,0x6b,0x76,0x6d /* ud2 ; .ascii "kv= m" */ + +#endif diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm= /insn.h index 37a4c39..568854b 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -45,6 +45,7 @@ struct insn { struct insn_field immediate2; /* for 64bit imm or seg16 */ }; =20 + int emulate_prefix_size; insn_attr_t attr; unsigned char opnd_bytes; unsigned char addr_bytes; @@ -128,6 +129,11 @@ static inline int insn_is_evex(struct insn *insn) return (insn->vex_prefix.nbytes =3D=3D 4); } =20 +static inline int insn_has_emulate_prefix(struct insn *insn) +{ + return !!insn->emulate_prefix_size; +} + /* Ensure this instruction is decoded completely */ static inline int insn_complete(struct insn *insn) { diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c index 79e048f..0151dfc 100644 --- a/tools/arch/x86/lib/insn.c +++ b/tools/arch/x86/lib/insn.c @@ -13,6 +13,8 @@ #include "../include/asm/inat.h" #include "../include/asm/insn.h" =20 +#include "../include/asm/emulate_prefix.h" + /* Verify next sizeof(t) bytes can be on the same instruction */ #define validate_next(t, insn, n) \ ((insn)->next_byte + sizeof(t) + n <=3D (insn)->end_kaddr) @@ -58,6 +60,36 @@ void insn_init(struct insn *insn, const void *kaddr, int= buf_len, int x86_64) insn->addr_bytes =3D 4; } =20 +static const insn_byte_t xen_prefix[] =3D { __XEN_EMULATE_PREFIX }; +static const insn_byte_t kvm_prefix[] =3D { __KVM_EMULATE_PREFIX }; + +static int __insn_get_emulate_prefix(struct insn *insn, + const insn_byte_t *prefix, size_t len) +{ + size_t i; + + for (i =3D 0; i < len; i++) { + if (peek_nbyte_next(insn_byte_t, insn, i) !=3D prefix[i]) + goto err_out; + } + + insn->emulate_prefix_size =3D len; + insn->next_byte +=3D len; + + return 1; + +err_out: + return 0; +} + +static void insn_get_emulate_prefix(struct insn *insn) +{ + if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix))) + return; + + __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix)); +} + /** * insn_get_prefixes - scan x86 instruction prefix bytes * @insn: &struct insn containing instruction @@ -76,6 +108,8 @@ void insn_get_prefixes(struct insn *insn) if (prefixes->got) return; =20 + insn_get_emulate_prefix(insn); + nb =3D 0; lb =3D 0; b =3D peek_next(insn_byte_t, insn); diff --git a/tools/objtool/sync-check.sh b/tools/objtool/sync-check.sh index 0a832e2..9bd04bb 100755 --- a/tools/objtool/sync-check.sh +++ b/tools/objtool/sync-check.sh @@ -4,6 +4,7 @@ FILES=3D' arch/x86/include/asm/inat_types.h arch/x86/include/asm/orc_types.h +arch/x86/include/asm/emulate_prefix.h arch/x86/lib/x86-opcode-map.txt arch/x86/tools/gen-insn-attr-x86.awk ' @@ -46,6 +47,6 @@ done check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_ty= pes.h[\">]"' check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[= \">]"' check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*= asm/insn.h[\">]"' -check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*= asm/in\(at\|sn\).h[\">]"' +check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*= asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_pre= fix.h[\">]"' =20 cd - diff --git a/tools/perf/check-headers.sh b/tools/perf/check-headers.sh index cea13cb..499235a 100755 --- a/tools/perf/check-headers.sh +++ b/tools/perf/check-headers.sh @@ -28,6 +28,7 @@ arch/x86/include/asm/disabled-features.h arch/x86/include/asm/required-features.h arch/x86/include/asm/cpufeatures.h arch/x86/include/asm/inat_types.h +arch/x86/include/asm/emulate_prefix.h arch/x86/include/uapi/asm/prctl.h arch/x86/lib/x86-opcode-map.txt arch/x86/tools/gen-insn-attr-x86.awk @@ -116,7 +117,7 @@ check lib/ctype.c '-I "^EXPORT_SYMBOL" -I "^#inc= lude " -B check arch/x86/include/asm/inat.h '-I "^#include [\"<]\(asm/\)*inat_ty= pes.h[\">]"' check arch/x86/include/asm/insn.h '-I "^#include [\"<]\(asm/\)*inat.h[= \">]"' check arch/x86/lib/inat.c '-I "^#include [\"<]\(../include/\)*asm/in= sn.h[\">]"' -check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*asm/in= \(at\|sn\).h[\">]"' +check arch/x86/lib/insn.c '-I "^#include [\"<]\(../include/\)*= asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_pre= fix.h[\">]"' =20 # diff non-symmetric files check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/s= yscalls/syscall_64.tbl _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel