From patchwork Tue Nov 24 10:19:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344552 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 047D5C56202 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 99E8520708 for ; Tue, 24 Nov 2020 10:20:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="Owt+c9vz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731842AbgKXKUD (ORCPT ); Tue, 24 Nov 2020 05:20:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731834AbgKXKUC (ORCPT ); Tue, 24 Nov 2020 05:20:02 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E14AC0613D6 for ; Tue, 24 Nov 2020 02:20:02 -0800 (PST) Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id A69091EC0532; Tue, 24 Nov 2020 11:20:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QDTh3B9uHPHFFJPMJTi8BsUafcIXFSAAaNGvWt8eLZ8=; b=Owt+c9vzKPRwxSEn3d5YrSZFVtiyJz9QsuWYIqlqjD50Sggdl2S7BZDrhMFEjGwOHrO+Uo mDkGZYlJQJ29a4+gIStDbG87BbOk2Ojiln5VQcY8CI67EL0ulcI1NKXvBTFpHw32t2F//r fhuRK7Xm7jtcN4TSlnhzropCmG1LIdo= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 01/19] x86/insn: Rename insn_decode() to insn_decode_regs() Date: Tue, 24 Nov 2020 11:19:34 +0100 Message-Id: <20201124101952.7909-2-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Rename insn_decode() to insn_decode_regs() to denote that it receives regs as param and free the name for a generic version. No functional changes. Signed-off-by: Borislav Petkov --- arch/x86/include/asm/insn-eval.h | 4 ++-- arch/x86/kernel/sev-es.c | 2 +- arch/x86/kernel/umip.c | 2 +- arch/x86/lib/insn-eval.c | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/insn-eval.h b/arch/x86/include/asm/insn-eval.h index a0f839aa144d..3797497a9270 100644 --- a/arch/x86/include/asm/insn-eval.h +++ b/arch/x86/include/asm/insn-eval.h @@ -23,7 +23,7 @@ unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx); int insn_get_code_seg_params(struct pt_regs *regs); int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE]); -bool insn_decode(struct insn *insn, struct pt_regs *regs, - unsigned char buf[MAX_INSN_SIZE], int buf_size); +bool insn_decode_regs(struct insn *insn, struct pt_regs *regs, + unsigned char buf[MAX_INSN_SIZE], int buf_size); #endif /* _ASM_X86_INSN_EVAL_H */ diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index 0bd1a0fc587e..37736486603e 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -256,7 +256,7 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) return ES_EXCEPTION; } - if (!insn_decode(&ctxt->insn, ctxt->regs, buffer, res)) + if (!insn_decode_regs(&ctxt->insn, ctxt->regs, buffer, res)) return ES_DECODE_FAILED; } else { res = vc_fetch_insn_kernel(ctxt, buffer); diff --git a/arch/x86/kernel/umip.c b/arch/x86/kernel/umip.c index f6225bf22c02..e3584894b074 100644 --- a/arch/x86/kernel/umip.c +++ b/arch/x86/kernel/umip.c @@ -356,7 +356,7 @@ bool fixup_umip_exception(struct pt_regs *regs) if (!nr_copied) return false; - if (!insn_decode(&insn, regs, buf, nr_copied)) + if (!insn_decode_regs(&insn, regs, buf, nr_copied)) return false; umip_inst = identify_insn(&insn); diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c index 58f7fb95c7f4..99fafbaf8555 100644 --- a/arch/x86/lib/insn-eval.c +++ b/arch/x86/lib/insn-eval.c @@ -1454,7 +1454,7 @@ int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE]) } /** - * insn_decode() - Decode an instruction + * insn_decode_regs() - Decode an instruction * @insn: Structure to store decoded instruction * @regs: Structure with register values as seen when entering kernel mode * @buf: Buffer containing the instruction bytes @@ -1467,8 +1467,8 @@ int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE]) * * True if instruction was decoded, False otherwise. */ -bool insn_decode(struct insn *insn, struct pt_regs *regs, - unsigned char buf[MAX_INSN_SIZE], int buf_size) +bool insn_decode_regs(struct insn *insn, struct pt_regs *regs, + unsigned char buf[MAX_INSN_SIZE], int buf_size) { int seg_defs; From patchwork Tue Nov 24 10:19:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344555 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D142C56201 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D8E342076B for ; Tue, 24 Nov 2020 10:20:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="YXvbSkxD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731853AbgKXKUE (ORCPT ); Tue, 24 Nov 2020 05:20:04 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50298 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731551AbgKXKUC (ORCPT ); Tue, 24 Nov 2020 05:20:02 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 6AA971EC0258; Tue, 24 Nov 2020 11:20:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213201; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8Wv35TDRv00HluIFQYfNHbq2ifcdB/WqVOCOaknQ4EU=; b=YXvbSkxDqbgewP5a9z/RJ5XSvuQwdKLY7aNuyrhlJoQdMuODaNy0wLibbqjRpbDdStwo4n JtNXlpWVXc53duKXkTRVVcVx5Vqgoi+QNt74M5+Puh+HCjSfFGXCn/A2NvCPv/fFqS8rcR bXKgKe53v1kh78DjFx1Xfo7nTBeaxYs= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 02/19] x86/insn: Add @buf_len param to insn_init() kernel-doc comment Date: Tue, 24 Nov 2020 11:19:35 +0100 Message-Id: <20201124101952.7909-3-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov It wasn't documented so add it. No functional changes. Signed-off-by: Borislav Petkov --- arch/x86/lib/insn.c | 1 + tools/arch/x86/lib/insn.c | 1 + 2 files changed, 2 insertions(+) diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c index 404279563891..1ba994862b56 100644 --- a/arch/x86/lib/insn.c +++ b/arch/x86/lib/insn.c @@ -37,6 +37,7 @@ * insn_init() - initialize struct insn * @insn: &struct insn to be initialized * @kaddr: address (in kernel memory) of instruction (or copy thereof) + * @buf_len: length of the insn buffer at @kaddr * @x86_64: !0 for 64-bit kernel or 64-bit app */ void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64) diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c index 0151dfc6da61..f3277d6e4ef2 100644 --- a/tools/arch/x86/lib/insn.c +++ b/tools/arch/x86/lib/insn.c @@ -37,6 +37,7 @@ * insn_init() - initialize struct insn * @insn: &struct insn to be initialized * @kaddr: address (in kernel memory) of instruction (or copy thereof) + * @buf_len: length of the insn buffer at @kaddr * @x86_64: !0 for 64-bit kernel or 64-bit app */ void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64) From patchwork Tue Nov 24 10:19:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344571 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D24F2C8300C for ; Tue, 24 Nov 2020 10:21:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E8EB20644 for ; Tue, 24 Nov 2020 10:21:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="D9dQU10D" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732034AbgKXKVI (ORCPT ); Tue, 24 Nov 2020 05:21:08 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50314 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731838AbgKXKUD (ORCPT ); Tue, 24 Nov 2020 05:20:03 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 1CAC21EC0535; Tue, 24 Nov 2020 11:20:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vkw/bsFMUcYNETWvJTuX6opRnJzZ1tyb2zDNp1ZcWKM=; b=D9dQU10DRyGJIOYR7I3bJ4Xkes+k/9UHoof63rlPApPckgLdQCpPn5+eAtdNms3Q/ksPEi 2FURtkTVvhN+Hxg2trtd1rbJY7V3QruZhmjVlFZfinRebtVfrnCOJPcm1WZgdDSHNZj/aZ xmD5poTSzovBQVGGbBH4LJvXctcqGX4= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 03/19] x86/insn: Add an insn_decode() API Date: Tue, 24 Nov 2020 11:19:36 +0100 Message-Id: <20201124101952.7909-4-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Users of the instruction decoder should use this to decode instruction bytes. For that, have insn*() helpers return an int value to denote success/failure. While at it, make insn_get_opcode() more stricter as to whether what has seen so far is a valid insn and if not. Copy linux/kconfig.h for the tools-version of the decoder so that it can use IS_ENABLED(). Signed-off-by: Borislav Petkov --- arch/x86/include/asm/insn.h | 24 ++-- arch/x86/lib/insn.c | 182 +++++++++++++++++++++++------- tools/arch/x86/include/asm/insn.h | 24 ++-- tools/arch/x86/lib/insn.c | 182 +++++++++++++++++++++++------- tools/include/linux/kconfig.h | 73 ++++++++++++ 5 files changed, 391 insertions(+), 94 deletions(-) create mode 100644 tools/include/linux/kconfig.h diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index 5c1ae3eff9d4..e80ddfe86255 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -87,13 +87,23 @@ struct insn { #define X86_VEX_M_MAX 0x1f /* VEX3.M Maximum value */ extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64); -extern void insn_get_prefixes(struct insn *insn); -extern void insn_get_opcode(struct insn *insn); -extern void insn_get_modrm(struct insn *insn); -extern void insn_get_sib(struct insn *insn); -extern void insn_get_displacement(struct insn *insn); -extern void insn_get_immediate(struct insn *insn); -extern void insn_get_length(struct insn *insn); +extern int insn_get_prefixes(struct insn *insn); +extern int insn_get_opcode(struct insn *insn); +extern int insn_get_modrm(struct insn *insn); +extern int insn_get_sib(struct insn *insn); +extern int insn_get_displacement(struct insn *insn); +extern int insn_get_immediate(struct insn *insn); +extern int insn_get_length(struct insn *insn); + +enum insn_mode { + INSN_MODE_32, + INSN_MODE_64, + /* Mode is determined by the current kernel build. */ + INSN_MODE_KERN, + INSN_NUM_MODES, +}; + +extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m); /* Attribute will be determined after getting ModRM (for opcode groups) */ static inline void insn_get_attribute(struct insn *insn) diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c index 1ba994862b56..1f749e3b4a25 100644 --- a/arch/x86/lib/insn.c +++ b/arch/x86/lib/insn.c @@ -13,6 +13,9 @@ #include #include +#include +#include + #include /* Verify next sizeof(t) bytes can be on the same instruction */ @@ -98,8 +101,12 @@ static void insn_get_emulate_prefix(struct insn *insn) * Populates the @insn->prefixes bitmap, and updates @insn->next_byte * to point to the (first) opcode. No effect if @insn->prefixes.got * is already set. + * + * * Returns: + * 0: on success + * !0: on error */ -void insn_get_prefixes(struct insn *insn) +int insn_get_prefixes(struct insn *insn) { struct insn_field *prefixes = &insn->prefixes; insn_attr_t attr; @@ -107,7 +114,7 @@ void insn_get_prefixes(struct insn *insn) int i, nb; if (prefixes->got) - return; + return 0; insn_get_emulate_prefix(insn); @@ -218,8 +225,10 @@ void insn_get_prefixes(struct insn *insn) prefixes->got = 1; + return 0; + err_out: - return; + return 1; } /** @@ -231,14 +240,20 @@ void insn_get_prefixes(struct insn *insn) * If necessary, first collects any preceding (prefix) bytes. * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got * is already 1. + * + * Returns: + * 0: on success + * !0: on error */ -void insn_get_opcode(struct insn *insn) +int insn_get_opcode(struct insn *insn) { struct insn_field *opcode = &insn->opcode; insn_byte_t op; int pfx_id; + if (opcode->got) - return; + return 0; + if (!insn->prefixes.got) insn_get_prefixes(insn); @@ -255,9 +270,13 @@ void insn_get_opcode(struct insn *insn) insn->attr = inat_get_avx_attribute(op, m, p); if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) || (!inat_accept_vex(insn->attr) && - !inat_is_group(insn->attr))) - insn->attr = 0; /* This instruction is bad */ - goto end; /* VEX has only 1 byte for opcode */ + !inat_is_group(insn->attr))) { + /* This instruction is bad */ + insn->attr = 0; + return 1; + } + /* VEX has only 1 byte for opcode */ + goto end; } insn->attr = inat_get_opcode_attribute(op); @@ -268,13 +287,18 @@ void insn_get_opcode(struct insn *insn) pfx_id = insn_last_prefix_id(insn); insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr); } - if (inat_must_vex(insn->attr)) - insn->attr = 0; /* This instruction is bad */ + + if (inat_must_vex(insn->attr)) { + /* This instruction is bad */ + insn->attr = 0; + return 1; + } end: opcode->got = 1; + return 0; err_out: - return; + return 1; } /** @@ -284,15 +308,22 @@ void insn_get_opcode(struct insn *insn) * Populates @insn->modrm and updates @insn->next_byte to point past the * ModRM byte, if any. If necessary, first collects the preceding bytes * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1. + * + * Returns: + * 0: on success + * !0: on error */ -void insn_get_modrm(struct insn *insn) +int insn_get_modrm(struct insn *insn) { struct insn_field *modrm = &insn->modrm; insn_byte_t pfx_id, mod; + if (modrm->got) - return; + return 0; + if (!insn->opcode.got) - insn_get_opcode(insn); + if (insn_get_opcode(insn)) + return 1; if (inat_has_modrm(insn->attr)) { mod = get_next(insn_byte_t, insn); @@ -302,17 +333,22 @@ void insn_get_modrm(struct insn *insn) pfx_id = insn_last_prefix_id(insn); insn->attr = inat_get_group_attribute(mod, pfx_id, insn->attr); - if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) - insn->attr = 0; /* This is bad */ + if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) { + /* Bad insn */ + insn->attr = 0; + return 1; + } } } if (insn->x86_64 && inat_is_force64(insn->attr)) insn->opnd_bytes = 8; + modrm->got = 1; + return 0; err_out: - return; + return 1; } @@ -329,8 +365,11 @@ int insn_rip_relative(struct insn *insn) if (!insn->x86_64) return 0; - if (!modrm->got) - insn_get_modrm(insn); + + if (!modrm->got) { + if (insn_get_modrm(insn)) + return 0; + } /* * For rip-relative instructions, the mod field (top 2 bits) * is zero and the r/m field (bottom 3 bits) is 0x5. @@ -344,15 +383,23 @@ int insn_rip_relative(struct insn *insn) * * If necessary, first collects the instruction up to and including the * ModRM byte. + * + * Returns: + * 0: if decoding succeeded + * !0: otherwise. */ -void insn_get_sib(struct insn *insn) +int insn_get_sib(struct insn *insn) { insn_byte_t modrm; if (insn->sib.got) - return; - if (!insn->modrm.got) - insn_get_modrm(insn); + return 0; + + if (!insn->modrm.got) { + if (insn_get_modrm(insn)) + return 1; + } + if (insn->modrm.nbytes) { modrm = (insn_byte_t)insn->modrm.value; if (insn->addr_bytes != 2 && @@ -363,8 +410,10 @@ void insn_get_sib(struct insn *insn) } insn->sib.got = 1; + return 0; + err_out: - return; + return 1; } @@ -375,15 +424,23 @@ void insn_get_sib(struct insn *insn) * If necessary, first collects the instruction up to and including the * SIB byte. * Displacement value is sign-expanded. + * + * * Returns: + * 0: if decoding succeeded + * !0: otherwise. */ -void insn_get_displacement(struct insn *insn) +int insn_get_displacement(struct insn *insn) { insn_byte_t mod, rm, base; if (insn->displacement.got) - return; - if (!insn->sib.got) - insn_get_sib(insn); + return 0; + + if (!insn->sib.got) { + if (insn_get_sib(insn)) + return 1; + } + if (insn->modrm.nbytes) { /* * Interpreting the modrm byte: @@ -426,9 +483,10 @@ void insn_get_displacement(struct insn *insn) } out: insn->displacement.got = 1; + return 0; err_out: - return; + return 1; } /* Decode moffset16/32/64. Return 0 if failed */ @@ -539,20 +597,27 @@ static int __get_immptr(struct insn *insn) } /** - * insn_get_immediate() - Get the immediates of instruction + * insn_get_immediate() - Get the immediate in an instruction * @insn: &struct insn containing instruction * * If necessary, first collects the instruction up to and including the * displacement bytes. * Basically, most of immediates are sign-expanded. Unsigned-value can be - * get by bit masking with ((1 << (nbytes * 8)) - 1) + * computed by bit masking with ((1 << (nbytes * 8)) - 1) + * + * Returns: + * 0: on success + * !0: on error */ -void insn_get_immediate(struct insn *insn) +int insn_get_immediate(struct insn *insn) { if (insn->immediate.got) - return; - if (!insn->displacement.got) - insn_get_displacement(insn); + return 0; + + if (!insn->displacement.got) { + if (insn_get_displacement(insn)) + return 1; + } if (inat_has_moffset(insn->attr)) { if (!__get_moffset(insn)) @@ -605,9 +670,10 @@ void insn_get_immediate(struct insn *insn) } done: insn->immediate.got = 1; + return 0; err_out: - return; + return 1; } /** @@ -616,13 +682,49 @@ void insn_get_immediate(struct insn *insn) * * If necessary, first collects the instruction up to and including the * immediates bytes. - */ -void insn_get_length(struct insn *insn) + * + * Returns: + * - 0 on success + * - !0 on error +*/ +int insn_get_length(struct insn *insn) { if (insn->length) - return; + return 0; + if (!insn->immediate.got) - insn_get_immediate(insn); + if (insn_get_immediate(insn)) + return 1; + insn->length = (unsigned char)((unsigned long)insn->next_byte - (unsigned long)insn->kaddr); + + return 0; +} + +/** + * insn_decode() - Decode an x86 instruction + * @insn: &struct insn to be initialized + * @kaddr: address (in kernel memory) of instruction (or copy thereof) + * @buf_len: length of the insn buffer at @kaddr + * @m: insn mode, see enum insn_mode + * + * Returns: + * 0: if decoding succeeded + * < 0: otherwise. + */ +int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m) +{ + if (m == INSN_MODE_KERN) + insn_init(insn, kaddr, buf_len, IS_ENABLED(CONFIG_X86_64)); + else + insn_init(insn, kaddr, buf_len, m == INSN_MODE_64); + + if (insn_get_length(insn)) + return -EINVAL; + + if (insn_complete(insn)) + return 0; + + return -EINVAL; } diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h index 568854b14d0a..f223bd7ec27e 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -87,13 +87,23 @@ struct insn { #define X86_VEX_M_MAX 0x1f /* VEX3.M Maximum value */ extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64); -extern void insn_get_prefixes(struct insn *insn); -extern void insn_get_opcode(struct insn *insn); -extern void insn_get_modrm(struct insn *insn); -extern void insn_get_sib(struct insn *insn); -extern void insn_get_displacement(struct insn *insn); -extern void insn_get_immediate(struct insn *insn); -extern void insn_get_length(struct insn *insn); +extern int insn_get_prefixes(struct insn *insn); +extern int insn_get_opcode(struct insn *insn); +extern int insn_get_modrm(struct insn *insn); +extern int insn_get_sib(struct insn *insn); +extern int insn_get_displacement(struct insn *insn); +extern int insn_get_immediate(struct insn *insn); +extern int insn_get_length(struct insn *insn); + +enum insn_mode { + INSN_MODE_32, + INSN_MODE_64, + /* Mode is determined by the current kernel build. */ + INSN_MODE_KERN, + INSN_NUM_MODES, +}; + +extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m); /* Attribute will be determined after getting ModRM (for opcode groups) */ static inline void insn_get_attribute(struct insn *insn) diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c index f3277d6e4ef2..41d2418302d9 100644 --- a/tools/arch/x86/lib/insn.c +++ b/tools/arch/x86/lib/insn.c @@ -13,6 +13,9 @@ #include "../include/asm/inat.h" #include "../include/asm/insn.h" +#include +#include + #include "../include/asm/emulate_prefix.h" /* Verify next sizeof(t) bytes can be on the same instruction */ @@ -98,8 +101,12 @@ static void insn_get_emulate_prefix(struct insn *insn) * Populates the @insn->prefixes bitmap, and updates @insn->next_byte * to point to the (first) opcode. No effect if @insn->prefixes.got * is already set. + * + * * Returns: + * 0: on success + * !0: on error */ -void insn_get_prefixes(struct insn *insn) +int insn_get_prefixes(struct insn *insn) { struct insn_field *prefixes = &insn->prefixes; insn_attr_t attr; @@ -107,7 +114,7 @@ void insn_get_prefixes(struct insn *insn) int i, nb; if (prefixes->got) - return; + return 0; insn_get_emulate_prefix(insn); @@ -218,8 +225,10 @@ void insn_get_prefixes(struct insn *insn) prefixes->got = 1; + return 0; + err_out: - return; + return 1; } /** @@ -231,14 +240,20 @@ void insn_get_prefixes(struct insn *insn) * If necessary, first collects any preceding (prefix) bytes. * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got * is already 1. + * + * Returns: + * 0: on success + * !0: on error */ -void insn_get_opcode(struct insn *insn) +int insn_get_opcode(struct insn *insn) { struct insn_field *opcode = &insn->opcode; insn_byte_t op; int pfx_id; + if (opcode->got) - return; + return 0; + if (!insn->prefixes.got) insn_get_prefixes(insn); @@ -255,9 +270,13 @@ void insn_get_opcode(struct insn *insn) insn->attr = inat_get_avx_attribute(op, m, p); if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) || (!inat_accept_vex(insn->attr) && - !inat_is_group(insn->attr))) - insn->attr = 0; /* This instruction is bad */ - goto end; /* VEX has only 1 byte for opcode */ + !inat_is_group(insn->attr))) { + /* This instruction is bad */ + insn->attr = 0; + return 1; + } + /* VEX has only 1 byte for opcode */ + goto end; } insn->attr = inat_get_opcode_attribute(op); @@ -268,13 +287,18 @@ void insn_get_opcode(struct insn *insn) pfx_id = insn_last_prefix_id(insn); insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr); } - if (inat_must_vex(insn->attr)) - insn->attr = 0; /* This instruction is bad */ + + if (inat_must_vex(insn->attr)) { + /* This instruction is bad */ + insn->attr = 0; + return 1; + } end: opcode->got = 1; + return 0; err_out: - return; + return 1; } /** @@ -284,15 +308,22 @@ void insn_get_opcode(struct insn *insn) * Populates @insn->modrm and updates @insn->next_byte to point past the * ModRM byte, if any. If necessary, first collects the preceding bytes * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1. + * + * Returns: + * 0: on success + * !0: on error */ -void insn_get_modrm(struct insn *insn) +int insn_get_modrm(struct insn *insn) { struct insn_field *modrm = &insn->modrm; insn_byte_t pfx_id, mod; + if (modrm->got) - return; + return 0; + if (!insn->opcode.got) - insn_get_opcode(insn); + if (insn_get_opcode(insn)) + return 1; if (inat_has_modrm(insn->attr)) { mod = get_next(insn_byte_t, insn); @@ -302,17 +333,22 @@ void insn_get_modrm(struct insn *insn) pfx_id = insn_last_prefix_id(insn); insn->attr = inat_get_group_attribute(mod, pfx_id, insn->attr); - if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) - insn->attr = 0; /* This is bad */ + if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) { + /* Bad insn */ + insn->attr = 0; + return 1; + } } } if (insn->x86_64 && inat_is_force64(insn->attr)) insn->opnd_bytes = 8; + modrm->got = 1; + return 0; err_out: - return; + return 1; } @@ -329,8 +365,11 @@ int insn_rip_relative(struct insn *insn) if (!insn->x86_64) return 0; - if (!modrm->got) - insn_get_modrm(insn); + + if (!modrm->got) { + if (insn_get_modrm(insn)) + return 0; + } /* * For rip-relative instructions, the mod field (top 2 bits) * is zero and the r/m field (bottom 3 bits) is 0x5. @@ -344,15 +383,23 @@ int insn_rip_relative(struct insn *insn) * * If necessary, first collects the instruction up to and including the * ModRM byte. + * + * Returns: + * 0: if decoding succeeded + * !0: otherwise. */ -void insn_get_sib(struct insn *insn) +int insn_get_sib(struct insn *insn) { insn_byte_t modrm; if (insn->sib.got) - return; - if (!insn->modrm.got) - insn_get_modrm(insn); + return 0; + + if (!insn->modrm.got) { + if (insn_get_modrm(insn)) + return 1; + } + if (insn->modrm.nbytes) { modrm = (insn_byte_t)insn->modrm.value; if (insn->addr_bytes != 2 && @@ -363,8 +410,10 @@ void insn_get_sib(struct insn *insn) } insn->sib.got = 1; + return 0; + err_out: - return; + return 1; } @@ -375,15 +424,23 @@ void insn_get_sib(struct insn *insn) * If necessary, first collects the instruction up to and including the * SIB byte. * Displacement value is sign-expanded. + * + * * Returns: + * 0: if decoding succeeded + * !0: otherwise. */ -void insn_get_displacement(struct insn *insn) +int insn_get_displacement(struct insn *insn) { insn_byte_t mod, rm, base; if (insn->displacement.got) - return; - if (!insn->sib.got) - insn_get_sib(insn); + return 0; + + if (!insn->sib.got) { + if (insn_get_sib(insn)) + return 1; + } + if (insn->modrm.nbytes) { /* * Interpreting the modrm byte: @@ -426,9 +483,10 @@ void insn_get_displacement(struct insn *insn) } out: insn->displacement.got = 1; + return 0; err_out: - return; + return 1; } /* Decode moffset16/32/64. Return 0 if failed */ @@ -539,20 +597,27 @@ static int __get_immptr(struct insn *insn) } /** - * insn_get_immediate() - Get the immediates of instruction + * insn_get_immediate() - Get the immediate in an instruction * @insn: &struct insn containing instruction * * If necessary, first collects the instruction up to and including the * displacement bytes. * Basically, most of immediates are sign-expanded. Unsigned-value can be - * get by bit masking with ((1 << (nbytes * 8)) - 1) + * computed by bit masking with ((1 << (nbytes * 8)) - 1) + * + * Returns: + * 0: on success + * !0: on error */ -void insn_get_immediate(struct insn *insn) +int insn_get_immediate(struct insn *insn) { if (insn->immediate.got) - return; - if (!insn->displacement.got) - insn_get_displacement(insn); + return 0; + + if (!insn->displacement.got) { + if (insn_get_displacement(insn)) + return 1; + } if (inat_has_moffset(insn->attr)) { if (!__get_moffset(insn)) @@ -605,9 +670,10 @@ void insn_get_immediate(struct insn *insn) } done: insn->immediate.got = 1; + return 0; err_out: - return; + return 1; } /** @@ -616,13 +682,49 @@ void insn_get_immediate(struct insn *insn) * * If necessary, first collects the instruction up to and including the * immediates bytes. - */ -void insn_get_length(struct insn *insn) + * + * Returns: + * - 0 on success + * - !0 on error +*/ +int insn_get_length(struct insn *insn) { if (insn->length) - return; + return 0; + if (!insn->immediate.got) - insn_get_immediate(insn); + if (insn_get_immediate(insn)) + return 1; + insn->length = (unsigned char)((unsigned long)insn->next_byte - (unsigned long)insn->kaddr); + + return 0; +} + +/** + * insn_decode() - Decode an x86 instruction + * @insn: &struct insn to be initialized + * @kaddr: address (in kernel memory) of instruction (or copy thereof) + * @buf_len: length of the insn buffer at @kaddr + * @m: insn mode, see enum insn_mode + * + * Returns: + * 0: if decoding succeeded + * < 0: otherwise. + */ +int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m) +{ + if (m == INSN_MODE_KERN) + insn_init(insn, kaddr, buf_len, IS_ENABLED(CONFIG_X86_64)); + else + insn_init(insn, kaddr, buf_len, m == INSN_MODE_64); + + if (insn_get_length(insn)) + return -EINVAL; + + if (insn_complete(insn)) + return 0; + + return -EINVAL; } diff --git a/tools/include/linux/kconfig.h b/tools/include/linux/kconfig.h new file mode 100644 index 000000000000..1555a0c4f345 --- /dev/null +++ b/tools/include/linux/kconfig.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_LINUX_KCONFIG_H +#define _TOOLS_LINUX_KCONFIG_H + +/* CONFIG_CC_VERSION_TEXT (Do not delete this comment. See help in Kconfig) */ + +#ifdef CONFIG_CPU_BIG_ENDIAN +#define __BIG_ENDIAN 4321 +#else +#define __LITTLE_ENDIAN 1234 +#endif + +#define __ARG_PLACEHOLDER_1 0, +#define __take_second_arg(__ignored, val, ...) val + +/* + * The use of "&&" / "||" is limited in certain expressions. + * The following enable to calculate "and" / "or" with macro expansion only. + */ +#define __and(x, y) ___and(x, y) +#define ___and(x, y) ____and(__ARG_PLACEHOLDER_##x, y) +#define ____and(arg1_or_junk, y) __take_second_arg(arg1_or_junk y, 0) + +#define __or(x, y) ___or(x, y) +#define ___or(x, y) ____or(__ARG_PLACEHOLDER_##x, y) +#define ____or(arg1_or_junk, y) __take_second_arg(arg1_or_junk 1, y) + +/* + * Helper macros to use CONFIG_ options in C/CPP expressions. Note that + * these only work with boolean and tristate options. + */ + +/* + * Getting something that works in C and CPP for an arg that may or may + * not be defined is tricky. Here, if we have "#define CONFIG_BOOGER 1" + * we match on the placeholder define, insert the "0," for arg1 and generate + * the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one). + * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when + * the last step cherry picks the 2nd arg, we get a zero. + */ +#define __is_defined(x) ___is_defined(x) +#define ___is_defined(val) ____is_defined(__ARG_PLACEHOLDER_##val) +#define ____is_defined(arg1_or_junk) __take_second_arg(arg1_or_junk 1, 0) + +/* + * IS_BUILTIN(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', 0 + * otherwise. For boolean options, this is equivalent to + * IS_ENABLED(CONFIG_FOO). + */ +#define IS_BUILTIN(option) __is_defined(option) + +/* + * IS_MODULE(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'm', 0 + * otherwise. + */ +#define IS_MODULE(option) __is_defined(option##_MODULE) + +/* + * IS_REACHABLE(CONFIG_FOO) evaluates to 1 if the currently compiled + * code can call a function defined in code compiled based on CONFIG_FOO. + * This is similar to IS_ENABLED(), but returns false when invoked from + * built-in code when CONFIG_FOO is set to 'm'. + */ +#define IS_REACHABLE(option) __or(IS_BUILTIN(option), \ + __and(IS_MODULE(option), __is_defined(MODULE))) + +/* + * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or 'm', + * 0 otherwise. + */ +#define IS_ENABLED(option) __or(IS_BUILTIN(option), IS_MODULE(option)) + +#endif /* _TOOLS_LINUX_KCONFIG_H */ From patchwork Tue Nov 24 10:19:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344556 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CFF2C6379D for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 18E1120708 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="nXW62cHa" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731862AbgKXKUF (ORCPT ); Tue, 24 Nov 2020 05:20:05 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50324 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731846AbgKXKUE (ORCPT ); Tue, 24 Nov 2020 05:20:04 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 15ECA1EC0537; Tue, 24 Nov 2020 11:20:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4CjMZvNFahVZWG+t3f+MDdber+Z+j4pDcsKb1HZOXlw=; b=nXW62cHayHGTvJ5xjpFsMkZtvlK1Blb2iSW6fo5okIdJLWz5fPR8rVsYa4qO6tPWyBvBbj KLjhcM2h/xjTrfJ1Uz1ePz21OaT1CgKtlwCslssm+bp2b1wsUDnJYzCq3CVrpB0mk0vmcc PFUJFa70M47a5LVp3NUH1PT9vhbLZ50= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 04/19] x86/insn-eval: Handle return values from the decoder Date: Tue, 24 Nov 2020 11:19:37 +0100 Message-Id: <20201124101952.7909-5-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Now that the different instruction inspecting functions return a value, test that and return early from callers if error has been encountered. Signed-off-by: Borislav Petkov --- arch/x86/lib/insn-eval.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c index 99fafbaf8555..5825b4cf4386 100644 --- a/arch/x86/lib/insn-eval.c +++ b/arch/x86/lib/insn-eval.c @@ -1117,7 +1117,8 @@ static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs, if (insn->addr_bytes != 8 && insn->addr_bytes != 4) return -EINVAL; - insn_get_modrm(insn); + if (insn_get_modrm(insn)) + return -EINVAL; if (!insn->modrm.nbytes) return -EINVAL; @@ -1125,7 +1126,8 @@ static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs, if (X86_MODRM_MOD(insn->modrm.value) > 2) return -EINVAL; - insn_get_sib(insn); + if (insn_get_sib(insn)) + return -EINVAL; if (!insn->sib.nbytes) return -EINVAL; @@ -1194,8 +1196,8 @@ static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs) short eff_addr; long tmp; - insn_get_modrm(insn); - insn_get_displacement(insn); + if (insn_get_modrm(insn) || insn_get_displacement(insn)) + goto out; if (insn->addr_bytes != 2) goto out; @@ -1491,7 +1493,9 @@ bool insn_decode_regs(struct insn *insn, struct pt_regs *regs, insn->addr_bytes = INSN_CODE_SEG_ADDR_SZ(seg_defs); insn->opnd_bytes = INSN_CODE_SEG_OPND_SZ(seg_defs); - insn_get_length(insn); + if (insn_get_length(insn)) + return false; + if (buf_size < insn->length) return false; From patchwork Tue Nov 24 10:19:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344569 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B6DBC8300B for ; Tue, 24 Nov 2020 10:21:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 23D2720708 for ; Tue, 24 Nov 2020 10:21:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="EYvydFGc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732022AbgKXKVF (ORCPT ); Tue, 24 Nov 2020 05:21:05 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50342 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731851AbgKXKUE (ORCPT ); Tue, 24 Nov 2020 05:20:04 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id C364C1EC0531; Tue, 24 Nov 2020 11:20:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h0DsoKrzcRTQJKgHC9wDoz9crP9Ii4sMW7h2Rj3nPyw=; b=EYvydFGc08vdL4Mj/TGt9+pptIqyYE8P39oXdE1i7/whoJiEIXmL+eExqVV94Qo8oy+NAV FD7lfaRSqxvZnmgqtLpO9VNc/KNobPGSyz4Pstoh1ftbskwYTseRU/fSIXDYMl98t8Kx06 wFefVQpyjzWygFdLCY+8pgEAmJ4EIzM= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 05/19] x86/boot/compressed/sev-es: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:38 +0100 Message-Id: <20201124101952.7909-6-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Other than simplifying the code there should be no functional changes resulting from this. Signed-off-by: Borislav Petkov --- arch/x86/boot/compressed/sev-es.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/arch/x86/boot/compressed/sev-es.c b/arch/x86/boot/compressed/sev-es.c index 954cb2702e23..6bb28170a770 100644 --- a/arch/x86/boot/compressed/sev-es.c +++ b/arch/x86/boot/compressed/sev-es.c @@ -79,16 +79,15 @@ static inline void sev_es_wr_ghcb_msr(u64 val) static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) { char buffer[MAX_INSN_SIZE]; - enum es_result ret; + int ret; memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE); - insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1); - insn_get_length(&ctxt->insn); + ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64); + if (ret < 0) + return ES_DECODE_FAILED; - ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED; - - return ret; + return ES_OK; } static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, From patchwork Tue Nov 24 10:19:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344554 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF71DC63798 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 57134206E0 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="dXrhcU1x" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731873AbgKXKUI (ORCPT ); Tue, 24 Nov 2020 05:20:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731846AbgKXKUG (ORCPT ); Tue, 24 Nov 2020 05:20:06 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8554C0613D6 for ; Tue, 24 Nov 2020 02:20:05 -0800 (PST) Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 7658A1EC0532; Tue, 24 Nov 2020 11:20:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213204; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pp7zBwHJQPdJo01j+3WxNC8JMxQ/nEa8UEHM9ceGnr4=; b=dXrhcU1xfc1R5fxq2mVZtMegQtn6fQzbLeLqnRXf9o0UdlOpuZNNexVzXjq3puD9wcF4db +0GgLwu4TAd09pOshOvEJQXwcQvpBWz3jUV3zD8vKE5EsxzQnK8PviFra0HZeh8yELl34H kLhqiY5kiPUX++8ZZUyDMJJVD2OW52U= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 06/19] perf/x86/intel/ds: Check insn_get_length() retval Date: Tue, 24 Nov 2020 11:19:39 +0100 Message-Id: <20201124101952.7909-7-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov intel_pmu_pebs_fixup_ip() needs only the insn length so use the appropriate helper instead of a full decode. A full decode differs only in running insn_complete() on the decoded insn but that is not needed here. Signed-off-by: Borislav Petkov --- arch/x86/events/intel/ds.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index fb327d11a04d..56cbcfee0ab1 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1263,14 +1263,14 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) is_64bit = kernel_ip(to) || any_64bit_mode(regs); #endif insn_init(&insn, kaddr, size, is_64bit); - insn_get_length(&insn); + /* * Make sure there was not a problem decoding the * instruction and getting the length. This is * doubly important because we have an infinite * loop if insn.length=0. */ - if (!insn.length) + if (insn_get_length(&insn) || !insn.length) break; to += insn.length; From patchwork Tue Nov 24 10:19:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344557 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F17D1C64E7A for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 96DC720708 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="CAT2DMPj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731898AbgKXKUN (ORCPT ); Tue, 24 Nov 2020 05:20:13 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50314 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731861AbgKXKUG (ORCPT ); Tue, 24 Nov 2020 05:20:06 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 2A28D1EC0258; Tue, 24 Nov 2020 11:20:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HZOMF0xnv/asL+0NdEME9qH39KkBlzno1GuCd8ZruUg=; b=CAT2DMPjOIsYz2sqSqiaLAr/dmHpGZENawj1lawO87n6oMUCUBlHqBRrJvAsdHDAlfwmCQ ZWQxyEoNqDTyzCGrPAYzmMdZvTMvwmRg10OSK4IcbeNBNp0KcAgbGLXdjw0fVdwT/CL6lr adozS0LiPh4uOdcQqmdl4gS3kDGJYQI= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 07/19] perf/x86/intel/ds: Check return values of insn decoder functions Date: Tue, 24 Nov 2020 11:19:40 +0100 Message-Id: <20201124101952.7909-8-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov branch_type() doesn't need to call the full insn_decode() because it doesn't need it in all cases thus leave the calls separate. Signed-off-by: Borislav Petkov --- arch/x86/events/intel/lbr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c index 1aadb253d296..0b7d2cc3f001 100644 --- a/arch/x86/events/intel/lbr.c +++ b/arch/x86/events/intel/lbr.c @@ -1224,8 +1224,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort) is64 = kernel_ip((unsigned long)addr) || any_64bit_mode(current_pt_regs()); #endif insn_init(&insn, addr, bytes_read, is64); - insn_get_opcode(&insn); - if (!insn.opcode.got) + if (insn_get_opcode(&insn)) return X86_BR_ABORT; switch (insn.opcode.bytes[0]) { @@ -1262,8 +1261,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort) ret = X86_BR_INT; break; case 0xe8: /* call near rel */ - insn_get_immediate(&insn); - if (insn.immediate1.value == 0) { + if (insn_get_immediate(&insn) || insn.immediate1.value == 0) { /* zero length call */ ret = X86_BR_ZERO_CALL; break; @@ -1279,7 +1277,9 @@ static int branch_type(unsigned long from, unsigned long to, int abort) ret = X86_BR_JMP; break; case 0xff: /* call near absolute, call far absolute ind */ - insn_get_modrm(&insn); + if (insn_get_modrm(&insn)) + return X86_BR_ABORT; + ext = (insn.modrm.bytes[0] >> 3) & 0x7; switch (ext) { case 2: /* near ind call */ From patchwork Tue Nov 24 10:19:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344567 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADAE6C2D0E4 for ; Tue, 24 Nov 2020 10:21:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 691D020644 for ; Tue, 24 Nov 2020 10:21:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="AKVFXS5A" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731883AbgKXKUJ (ORCPT ); Tue, 24 Nov 2020 05:20:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731863AbgKXKUH (ORCPT ); Tue, 24 Nov 2020 05:20:07 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A895C0617A6 for ; Tue, 24 Nov 2020 02:20:07 -0800 (PST) Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id D9C371EC0535; Tue, 24 Nov 2020 11:20:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213206; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KkdBd4Rmj0mVi6uv14FkRq9DB6Srvsm1iTuf+siFuy4=; b=AKVFXS5AhY1vr2HbKkVGOjMMiETV/g1QDQLuUTwdgrSn7lKgQrT8fp0fyW1trACnllFXBw Q4kfF4R3HwZW6fE19fF7nfC+UMVJFkZ4tmESLRgc5sPmpQTIIvqMJPaQtMdre0OwjMajvN as09BlT3KOZiRpoJuLDq+rdMuHvkySA= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 08/19] x86/alternative: Use insn_decode() Date: Tue, 24 Nov 2020 11:19:41 +0100 Message-Id: <20201124101952.7909-9-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov No functional changes, just simplification. Signed-off-by: Borislav Petkov --- arch/x86/kernel/alternative.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 2400ad62f330..02845ea7354a 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1274,15 +1274,15 @@ static void text_poke_loc_init(struct text_poke_loc *tp, void *addr, const void *opcode, size_t len, const void *emulate) { struct insn insn; + int ret; memcpy((void *)tp->text, opcode, len); if (!emulate) emulate = opcode; - kernel_insn_init(&insn, emulate, MAX_INSN_SIZE); - insn_get_length(&insn); + ret = insn_decode(&insn, emulate, MAX_INSN_SIZE, INSN_MODE_KERN); - BUG_ON(!insn_complete(&insn)); + BUG_ON(ret < 0); BUG_ON(len != insn.length); tp->rel_addr = addr - (void *)_stext; From patchwork Tue Nov 24 10:19:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344568 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 359BDC64E90 for ; Tue, 24 Nov 2020 10:21:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DC89C20644 for ; Tue, 24 Nov 2020 10:21:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="bgqIAT0z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732015AbgKXKVB (ORCPT ); Tue, 24 Nov 2020 05:21:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731247AbgKXKUI (ORCPT ); Tue, 24 Nov 2020 05:20:08 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01ACEC0613D6 for ; Tue, 24 Nov 2020 02:20:08 -0800 (PST) Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 8BA331EC053B; Tue, 24 Nov 2020 11:20:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213206; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S/MORH+WOPB4E6UX2lhZ4So7iN1jtjpzx2fOzz/XfOw=; b=bgqIAT0zZWeNBd7KFr8TA5TqclIcNgllS6izdOM4CBxTf4Xf1zqQX7BI6KzTS5+FT9hIlM foS0MY6NPh15+vMt/JRmP/2HM0jrQGhYq+Y3wktFJYzvL+MdfneCkstXIwv+fo6SEBWnJj inkTyUKtt/Gkok4mu5AikTshmc31Y0g= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 09/19] x86/mce: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:42 +0100 Message-Id: <20201124101952.7909-10-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- arch/x86/kernel/cpu/mce/severity.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/mce/severity.c b/arch/x86/kernel/cpu/mce/severity.c index 83df991314c5..a2136ced9d73 100644 --- a/arch/x86/kernel/cpu/mce/severity.c +++ b/arch/x86/kernel/cpu/mce/severity.c @@ -218,15 +218,15 @@ static struct severity { static bool is_copy_from_user(struct pt_regs *regs) { u8 insn_buf[MAX_INSN_SIZE]; - struct insn insn; unsigned long addr; + struct insn insn; + int ret; if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip, MAX_INSN_SIZE)) return false; - kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE); - insn_get_opcode(&insn); - if (!insn.opcode.got) + ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) return false; switch (insn.opcode.value) { @@ -234,10 +234,6 @@ static bool is_copy_from_user(struct pt_regs *regs) case 0x8A: case 0x8B: /* MOVZ mem,reg */ case 0xB60F: case 0xB70F: - insn_get_modrm(&insn); - insn_get_sib(&insn); - if (!insn.modrm.got || !insn.sib.got) - return false; addr = (unsigned long)insn_get_addr_ref(&insn, regs); break; /* REP MOVS */ From patchwork Tue Nov 24 10:19:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344561 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5E4FC64E90 for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92FED2076B for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="SVIeRqU2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731907AbgKXKUP (ORCPT ); Tue, 24 Nov 2020 05:20:15 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50342 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731864AbgKXKUI (ORCPT ); Tue, 24 Nov 2020 05:20:08 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 4F15D1EC0531; Tue, 24 Nov 2020 11:20:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rGgeBbOw/BlvqQETOCKC6lFNWbRtZshXnXH7HSVsfpU=; b=SVIeRqU2b6YjZWwctoqpXwXJAy4I3yvEnPCTZ7d0bTUfVv7bVeFc+XSgLjX8MHwlaJ0ISn GLiZcrvjdfCst/fLkEuibCN1HP62BLCKrGrk8pnY0pKVPs1pI8Cb8lw6LkgAkM47tF4DGp 3DgSTVVZ9AGasS45/Do1BJiplTm3D3w= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 10/19] x86/kprobes: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:43 +0100 Message-Id: <20201124101952.7909-11-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov Acked-by: Masami Hiramatsu --- arch/x86/kernel/kprobes/core.c | 17 +++++++++++------ arch/x86/kernel/kprobes/opt.c | 9 +++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 547c7abb39f5..43d4a3056d21 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -285,6 +285,8 @@ static int can_probe(unsigned long paddr) /* Decode instructions */ addr = paddr - offset; while (addr < paddr) { + int ret; + /* * Check if the instruction has been modified by another * kprobe, in which case we replace the breakpoint by the @@ -296,8 +298,10 @@ static int can_probe(unsigned long paddr) __addr = recover_probed_instruction(buf, addr); if (!__addr) return 0; - kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE); - insn_get_length(&insn); + + ret = insn_decode(&insn, (void *)__addr, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return 0; /* * Another debugging subsystem might insert this breakpoint. @@ -340,8 +344,8 @@ static int is_IF_modifier(kprobe_opcode_t *insn) int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn) { kprobe_opcode_t buf[MAX_INSN_SIZE]; - unsigned long recovered_insn = - recover_probed_instruction(buf, (unsigned long)src); + unsigned long recovered_insn = recover_probed_instruction(buf, (unsigned long)src); + int ret; if (!recovered_insn || !insn) return 0; @@ -351,8 +355,9 @@ int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn) MAX_INSN_SIZE)) return 0; - kernel_insn_init(insn, dest, MAX_INSN_SIZE); - insn_get_length(insn); + ret = insn_decode(insn, dest, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return 0; /* We can not probe force emulate prefixed instruction */ if (insn_has_emulate_prefix(insn)) diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c index 041f0b50bc27..4d67571249e1 100644 --- a/arch/x86/kernel/kprobes/opt.c +++ b/arch/x86/kernel/kprobes/opt.c @@ -299,6 +299,8 @@ static int can_optimize(unsigned long paddr) addr = paddr - offset; while (addr < paddr - offset + size) { /* Decode until function end */ unsigned long recovered_insn; + int ret; + if (search_exception_tables(addr)) /* * Since some fixup code will jumps into this function, @@ -308,8 +310,11 @@ static int can_optimize(unsigned long paddr) recovered_insn = recover_probed_instruction(buf, addr); if (!recovered_insn) return 0; - kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE); - insn_get_length(&insn); + + ret = insn_decode(&insn, (void *)recovered_insn, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return 0; + /* Another subsystem puts a breakpoint */ if (insn.opcode.bytes[0] == INT3_INSN_OPCODE) return 0; From patchwork Tue Nov 24 10:19:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344570 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8ACDC71155 for ; Tue, 24 Nov 2020 10:21:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A045A20708 for ; Tue, 24 Nov 2020 10:21:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="XXYGU9cS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732008AbgKXKU5 (ORCPT ); Tue, 24 Nov 2020 05:20:57 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50314 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731867AbgKXKUI (ORCPT ); Tue, 24 Nov 2020 05:20:08 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 123061EC0532; Tue, 24 Nov 2020 11:20:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n4Z0j0u7jLVJMb8Scn5mS7EE1A0OZxR0KuqIDJ96LRE=; b=XXYGU9cSo13F/iNmooQJDmsUse9c3J+R59DGb3QNkg1h5qo3p6GkujSLrAkfOeHtyPmXDM WE9a7QSN9k4ohF+DDYdlx7GMzChQuehdCitDZsZS7B7OI9OSriT5j0mOjpSR3g+VgS+wdI KUtizmpqvB14FYQOp0XV7I6WmkLQ5G4= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 11/19] x86/sev-es: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:44 +0100 Message-Id: <20201124101952.7909-12-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- arch/x86/kernel/sev-es.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index 37736486603e..564cc9fc693d 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -244,8 +244,7 @@ static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt, static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) { char buffer[MAX_INSN_SIZE]; - enum es_result ret; - int res; + int res, ret; if (user_mode(ctxt->regs)) { res = insn_fetch_from_user(ctxt->regs, buffer); @@ -267,13 +266,13 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) return ES_EXCEPTION; } - insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE - res, 1); - insn_get_length(&ctxt->insn); + ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE - res, INSN_MODE_64); } - ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED; - - return ret; + if (ret < 0) + return ES_DECODE_FAILED; + else + return ES_OK; } static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, From patchwork Tue Nov 24 10:19:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344558 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADF67C64E7D for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 54D39206E0 for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="S0fU1P8k" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731914AbgKXKUQ (ORCPT ); Tue, 24 Nov 2020 05:20:16 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50462 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731875AbgKXKUJ (ORCPT ); Tue, 24 Nov 2020 05:20:09 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id BB0A21EC0537; Tue, 24 Nov 2020 11:20:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YYUAMjY4culhjxafjgDZZiig0hrfgvF8ZUzoeDtoaTA=; b=S0fU1P8kayMSK5iBhckzDCKV3Zi1FnFQQPPxhRutnSciS7Lx2acrsU452dgKAq2JJnXMoS 3fOXpJ82jDMUhJJv5ukqDh/c+1GI+A2bZU9h6QT0pAXXWwkpewWNivOsDlce/CrNGrBVrq NgnuI5p/GrRNkflnHNjnI6AOdSl4S38= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 12/19] x86/traps: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:45 +0100 Message-Id: <20201124101952.7909-13-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- arch/x86/kernel/traps.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index e1b78829d909..4a06f79aaeeb 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -493,14 +493,15 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs, { u8 insn_buf[MAX_INSN_SIZE]; struct insn insn; + int ret; if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip, MAX_INSN_SIZE)) return GP_NO_HINT; - kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE); - insn_get_modrm(&insn); - insn_get_sib(&insn); + ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN); + if (ret < 0) + return GP_NO_HINT; *addr = (unsigned long)insn_get_addr_ref(&insn, regs); if (*addr == -1UL) From patchwork Tue Nov 24 10:19:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344559 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6031FC64E7C for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 14DC120644 for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="MDRVw1Ij" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731922AbgKXKUQ (ORCPT ); Tue, 24 Nov 2020 05:20:16 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50342 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731863AbgKXKUK (ORCPT ); Tue, 24 Nov 2020 05:20:10 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 7422E1EC0258; Tue, 24 Nov 2020 11:20:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8bXJvGwXJ0S/8F5qrEfTDu1a5Q+Rd2aTQVU42AyDxHM=; b=MDRVw1Ij8aeyd6zvmeCYsGdSTgZR9kBgwcEvoi/gBhZEhkTBCxAovvdb6KIlriRDxdbw8p x3AuCKayh3w75tjJ6qtW0h2mPQYhya/t5Zvbc36vm6a2qc6u0idfgzJK4Qmm6jVIMHY57r vPdU/qG9x1UJwyWZAMTxQjE3buB8Ee8= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 13/19] x86/uprobes: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:46 +0100 Message-Id: <20201124101952.7909-14-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- arch/x86/kernel/uprobes.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c index 3fdaa042823d..e5759310f499 100644 --- a/arch/x86/kernel/uprobes.c +++ b/arch/x86/kernel/uprobes.c @@ -275,12 +275,12 @@ static bool is_prefix_bad(struct insn *insn) static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64) { + enum insn_mode m = x86_64 ? INSN_MODE_64 : INSN_MODE_32; u32 volatile *good_insns; + int ret; - insn_init(insn, auprobe->insn, sizeof(auprobe->insn), x86_64); - /* has the side-effect of processing the entire instruction */ - insn_get_length(insn); - if (!insn_complete(insn)) + ret = insn_decode(insn, auprobe->insn, sizeof(auprobe->insn), m); + if (ret < 0) return -ENOEXEC; if (is_prefix_bad(insn)) From patchwork Tue Nov 24 10:19:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344565 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0631BC64E7A for ; Tue, 24 Nov 2020 10:21:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9907720644 for ; Tue, 24 Nov 2020 10:21:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="YDKQ8M0y" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731974AbgKXKUe (ORCPT ); Tue, 24 Nov 2020 05:20:34 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50314 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731891AbgKXKUL (ORCPT ); Tue, 24 Nov 2020 05:20:11 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 28E461EC0535; Tue, 24 Nov 2020 11:20:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213210; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TE43OBjRFKRV/fdNUgMMdLKwnxouvBpc+mpuZM8X2gA=; b=YDKQ8M0yIe5HzIyoyz5fyQSsDGpvxJqOvhyXRo32LyLj+r0wewcdsPm01t3qM317ov9byD QG+nrr/74r5cRtJYfCorWIXtK7WfcB3D/OvJXGyQVYA0+XqUu6+y4Vt3bur3vycAGaFJbt lzf2p2u7zzFxDNPGMoJkyMFWCmhroyw= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 14/19] x86/tools/insn_decoder_test: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:47 +0100 Message-Id: <20201124101952.7909-15-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- arch/x86/tools/insn_decoder_test.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/x86/tools/insn_decoder_test.c b/arch/x86/tools/insn_decoder_test.c index 34eda63c124b..472540aeabc2 100644 --- a/arch/x86/tools/insn_decoder_test.c +++ b/arch/x86/tools/insn_decoder_test.c @@ -120,7 +120,7 @@ int main(int argc, char **argv) while (fgets(line, BUFSIZE, stdin)) { char copy[BUFSIZE], *s, *tab1, *tab2; - int nb = 0; + int nb = 0, ret; unsigned int b; if (line[0] == '<') { @@ -148,10 +148,12 @@ int main(int argc, char **argv) } else break; } + /* Decode an instruction */ - insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64); - insn_get_length(&insn); - if (insn.length != nb) { + ret = insn_decode(&insn, insn_buff, sizeof(insn_buff), + x86_64 ? INSN_MODE_64 : INSN_MODE_32); + + if (ret < 0 || insn.length != nb) { warnings++; pr_warn("Found an x86 instruction decoder bug, " "please report this.\n", sym); From patchwork Tue Nov 24 10:19:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344566 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF200C63798 for ; Tue, 24 Nov 2020 10:21:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5BA7420708 for ; Tue, 24 Nov 2020 10:21:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="b9MI79V6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731965AbgKXKUd (ORCPT ); Tue, 24 Nov 2020 05:20:33 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50462 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731894AbgKXKUL (ORCPT ); Tue, 24 Nov 2020 05:20:11 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 244431EC0537; Tue, 24 Nov 2020 11:20:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213211; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tNubvoHkuGyr1aDlAWLeVt/MEJM9R53/4lTaxWezvhA=; b=b9MI79V6bTWq8/TKTojZAChUJM9trRPGuIPZQGfg9mTWNRFmGbDD62bv6GbaTlM/5tTGZL ve3cNXaljltp+WQVuO4JHEuJXlqfyyqeUlgbvDBq4ofj/i7uNjRbafe5PI4tMP2EHT4SA6 W/3UIUpjpawHpr+fWatAogKgQbzGEOQ= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 15/19] tools/objtool: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:48 +0100 Message-Id: <20201124101952.7909-16-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- tools/objtool/arch/x86/decode.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c index cde9c36e40ae..67ee8d2a9e5c 100644 --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -90,7 +90,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, struct list_head *ops_list) { struct insn insn; - int x86_64, sign; + int x86_64, sign, ret; unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0, rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0, modrm_reg = 0, sib = 0; @@ -101,10 +101,9 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, if (x86_64 == -1) return -1; - insn_init(&insn, sec->data->d_buf + offset, maxlen, x86_64); - insn_get_length(&insn); - - if (!insn_complete(&insn)) { + ret = insn_decode(&insn, sec->data->d_buf + offset, maxlen, + x86_64 ? INSN_MODE_64 : INSN_MODE_32); + if (ret < 0) { WARN("can't decode instruction at %s:0x%lx", sec->name, offset); return -1; } From patchwork Tue Nov 24 10:19:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344563 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E0FBC2D0E4 for ; Tue, 24 Nov 2020 10:21:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 99A6B20644 for ; Tue, 24 Nov 2020 10:21:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="ByP2+PGD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731941AbgKXKU0 (ORCPT ); Tue, 24 Nov 2020 05:20:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731897AbgKXKUN (ORCPT ); Tue, 24 Nov 2020 05:20:13 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A72FC0613D6 for ; Tue, 24 Nov 2020 02:20:13 -0800 (PST) Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id CD5A61EC0531; Tue, 24 Nov 2020 11:20:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213211; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wo4FjvW11Hc6jRo2dt78TwfxgWMKML5VZdnYfgtsWMQ=; b=ByP2+PGDp3C53hyO66N4yj/vLbhtsK4w8W3uflpIXKkdSiyBeZK3ZlZEdO3O3kJi4b9uyH XJiwvD0wDYmQisuEnExET4zkQArg4Px6+6+Wcas7rrOlGd1OmSLlksEKgCIhJqlSfdo8eQ i1nuQtAao+N39a2VtmULPDSunJERkEs= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 16/19] x86/tools/insn_sanity: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:49 +0100 Message-Id: <20201124101952.7909-17-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov --- arch/x86/tools/insn_sanity.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/tools/insn_sanity.c b/arch/x86/tools/insn_sanity.c index 185ceba9d289..51309df285b4 100644 --- a/arch/x86/tools/insn_sanity.c +++ b/arch/x86/tools/insn_sanity.c @@ -222,8 +222,8 @@ static void parse_args(int argc, char **argv) int main(int argc, char **argv) { + int insns = 0, ret; struct insn insn; - int insns = 0; int errors = 0; unsigned long i; unsigned char insn_buff[MAX_INSN_SIZE * 2]; @@ -241,15 +241,15 @@ int main(int argc, char **argv) continue; /* Decode an instruction */ - insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64); - insn_get_length(&insn); + ret = insn_decode(&insn, insn_buff, sizeof(insn_buff), + x86_64 ? INSN_MODE_64 : INSN_MODE_32); if (insn.next_byte <= insn.kaddr || insn.kaddr + MAX_INSN_SIZE < insn.next_byte) { /* Access out-of-range memory */ dump_stream(stderr, "Error: Found an access violation", i, insn_buff, &insn); errors++; - } else if (verbose && !insn_complete(&insn)) + } else if (verbose && ret < 0) dump_stream(stdout, "Info: Found an undecodable input", i, insn_buff, &insn); else if (verbose >= 2) dump_insn(stdout, &insn); From patchwork Tue Nov 24 10:19:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344564 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F0F6C56202 for ; Tue, 24 Nov 2020 10:21:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D96B620708 for ; Tue, 24 Nov 2020 10:21:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="kn6M55Ay" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731948AbgKXKU1 (ORCPT ); Tue, 24 Nov 2020 05:20:27 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50528 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731896AbgKXKUN (ORCPT ); Tue, 24 Nov 2020 05:20:13 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 865991EC0532; Tue, 24 Nov 2020 11:20:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213212; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fzDdRG5vyIJY8aJerKAJ/kASvJzUoQOtDJrPsHL6Xn0=; b=kn6M55AyhUeKsbCxYgUrjC5DF75Ro2dQMZNXgFb+NCsDr1XZZc8Y0r6NFtySIbQL7PIVOX Indx576Ud4kT4IjkU7a5/baH2kYk5991kgO7U3Cb90abO4n1YFSD54T56BxnpWked9lDi/ 2sOpyoRC5fSpx9NYhDw9Cw2DCrzOhtI= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML , Arnaldo Carvalho de Melo Subject: [RFC PATCH v0 17/19] tools/perf: Convert to insn_decode() Date: Tue, 24 Nov 2020 11:19:50 +0100 Message-Id: <20201124101952.7909-18-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Simplify code, no functional changes. Signed-off-by: Borislav Petkov Cc: Arnaldo Carvalho de Melo --- tools/perf/arch/x86/tests/insn-x86.c | 9 ++++----- tools/perf/arch/x86/util/archinsn.c | 9 +++++---- .../intel-pt-decoder/intel-pt-insn-decoder.c | 17 ++++++++++------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/tools/perf/arch/x86/tests/insn-x86.c b/tools/perf/arch/x86/tests/insn-x86.c index 745f29adb14b..6c7970b3acb6 100644 --- a/tools/perf/arch/x86/tests/insn-x86.c +++ b/tools/perf/arch/x86/tests/insn-x86.c @@ -95,13 +95,12 @@ static int get_branch(const char *branch_str) static int test_data_item(struct test_data *dat, int x86_64) { struct intel_pt_insn intel_pt_insn; + int op, branch, ret; struct insn insn; - int op, branch; - insn_init(&insn, dat->data, MAX_INSN_SIZE, x86_64); - insn_get_length(&insn); - - if (!insn_complete(&insn)) { + ret = insn_decode(&insn, dat->data, MAX_INSN_SIZE, + x86_64 ? INSN_MODE_64 : INSN_MODE_32); + if (ret < 0) { pr_debug("Failed to decode: %s\n", dat->asm_rep); return -1; } diff --git a/tools/perf/arch/x86/util/archinsn.c b/tools/perf/arch/x86/util/archinsn.c index 3e6791531ca5..9fb12e8e67eb 100644 --- a/tools/perf/arch/x86/util/archinsn.c +++ b/tools/perf/arch/x86/util/archinsn.c @@ -11,7 +11,7 @@ void arch_fetch_insn(struct perf_sample *sample, struct machine *machine) { struct insn insn; - int len; + int len, ret; bool is64bit = false; if (!sample->ip) @@ -19,8 +19,9 @@ void arch_fetch_insn(struct perf_sample *sample, len = thread__memcpy(thread, machine, sample->insn, sample->ip, sizeof(sample->insn), &is64bit); if (len <= 0) return; - insn_init(&insn, sample->insn, len, is64bit); - insn_get_length(&insn); - if (insn_complete(&insn) && insn.length <= len) + + ret = insn_decode(&insn, sample->insn, len, + is64bit ? INSN_MODE_64 : INSN_MODE_32); + if (ret >= 0 && insn.length <= len) sample->insn_len = insn.length; } diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c b/tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c index fb8a3558d3d5..56b42545946e 100644 --- a/tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c +++ b/tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c @@ -158,11 +158,13 @@ int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64, struct intel_pt_insn *intel_pt_insn) { struct insn insn; + int ret; - insn_init(&insn, buf, len, x86_64); - insn_get_length(&insn); - if (!insn_complete(&insn) || insn.length > len) + ret = insn_decode(&insn, buf, len, + x86_64 ? INSN_MODE_64 : INSN_MODE_32); + if (ret < 0 || insn.length > len) return -1; + intel_pt_insn_decoder(&insn, intel_pt_insn); if (insn.length < INTEL_PT_INSN_BUF_SZ) memcpy(intel_pt_insn->buf, buf, insn.length); @@ -183,12 +185,13 @@ const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused, u8 *inbuf, int inlen, int *lenp) { struct insn insn; - int n, i; + int n, i, ret; int left; - insn_init(&insn, inbuf, inlen, x->is64bit); - insn_get_length(&insn); - if (!insn_complete(&insn) || insn.length > inlen) + ret = insn_decode(&insn, inbuf, inlen, + x->is64bit ? INSN_MODE_64 : INSN_MODE_32); + + if (ret < 0 || insn.length > inlen) return ""; if (lenp) *lenp = insn.length; From patchwork Tue Nov 24 10:19:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344562 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F2B8C71155 for ; Tue, 24 Nov 2020 10:20:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 197BB20708 for ; Tue, 24 Nov 2020 10:20:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="j02h3Jkv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731932AbgKXKUZ (ORCPT ); Tue, 24 Nov 2020 05:20:25 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50342 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731899AbgKXKUO (ORCPT ); Tue, 24 Nov 2020 05:20:14 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 4BA1B1EC0258; Tue, 24 Nov 2020 11:20:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213213; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fu/0usfCnSEQO2cwyUtuP0+wjMZSJ5sA4q+m0Ky8VmQ=; b=j02h3Jkvklx8klJr7jk1UIDNBpt24DntNJiioYTo9nvC1LwCQfgp1tRfNpjwt6N83FkYdl pjDGutKr4YNYGz7k9N7nVoW8XYBd/TXNakRVtaHpfy1ZUxZFh75lSuqtuJLWta3UkSlGMj lVJRCu+UxOqStWYpoqBXJYWhGDyBOjM= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 18/19] x86/insn: Remove kernel_insn_init() Date: Tue, 24 Nov 2020 11:19:51 +0100 Message-Id: <20201124101952.7909-19-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Now that it is not needed anymore, drop it. Signed-off-by: Borislav Petkov --- arch/x86/include/asm/insn.h | 11 ----------- tools/arch/x86/include/asm/insn.h | 11 ----------- 2 files changed, 22 deletions(-) diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index e80ddfe86255..ccf472ae4378 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -114,17 +114,6 @@ static inline void insn_get_attribute(struct insn *insn) /* Instruction uses RIP-relative addressing */ extern int insn_rip_relative(struct insn *insn); -/* Init insn for kernel text */ -static inline void kernel_insn_init(struct insn *insn, - const void *kaddr, int buf_len) -{ -#ifdef CONFIG_X86_64 - insn_init(insn, kaddr, buf_len, 1); -#else /* CONFIG_X86_32 */ - insn_init(insn, kaddr, buf_len, 0); -#endif -} - static inline int insn_is_avx(struct insn *insn) { if (!insn->prefixes.got) diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h index f223bd7ec27e..6c8d6b167bea 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -114,17 +114,6 @@ static inline void insn_get_attribute(struct insn *insn) /* Instruction uses RIP-relative addressing */ extern int insn_rip_relative(struct insn *insn); -/* Init insn for kernel text */ -static inline void kernel_insn_init(struct insn *insn, - const void *kaddr, int buf_len) -{ -#ifdef CONFIG_X86_64 - insn_init(insn, kaddr, buf_len, 1); -#else /* CONFIG_X86_32 */ - insn_init(insn, kaddr, buf_len, 0); -#endif -} - static inline int insn_is_avx(struct insn *insn) { if (!insn->prefixes.got) From patchwork Tue Nov 24 10:19:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 1344560 Return-Path: Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F1DDC64E7B for ; Tue, 24 Nov 2020 10:20:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C9226206E0 for ; Tue, 24 Nov 2020 10:20:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alien8.de header.i=@alien8.de header.b="O9bMWnTC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731929AbgKXKUR (ORCPT ); Tue, 24 Nov 2020 05:20:17 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50314 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731902AbgKXKUP (ORCPT ); Tue, 24 Nov 2020 05:20:15 -0500 Received: from zn.tnic (p200300ec2f0e360052021be21853ebf1.dip0.t-ipconnect.de [IPv6:2003:ec:2f0e:3600:5202:1be2:1853:ebf1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 016271EC0531; Tue, 24 Nov 2020 11:20:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1606213214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w2OoBuykDpspOvHuVYwSh7bmxHirbR42nv4tt/Mbf5c=; b=O9bMWnTCgEPIcCR12FZbw43OnS8CbuwbDEHk2j4Rkm77REWhJOSW8j2PLENH3aL8OvE2Yx BkCF6fffT3TpB/9b+ZQsKTP4A5rdXcH8aKvU1kQFAxCWf5vDJRdYT5akZkuWxVG8x95z+U OWbXNjXL4oDmQTqoFazmqYx8n04SxAk= From: Borislav Petkov To: Andy Lutomirski , Masami Hiramatsu Cc: X86 ML , LKML Subject: [RFC PATCH v0 19/19] x86/insn: Make insn_complete() static Date: Tue, 24 Nov 2020 11:19:52 +0100 Message-Id: <20201124101952.7909-20-bp@alien8.de> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201124101952.7909-1-bp@alien8.de> References: <20201124101952.7909-1-bp@alien8.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov ... and move it above the only place it is used. Signed-off-by: Borislav Petkov --- arch/x86/include/asm/insn.h | 7 ------- arch/x86/lib/insn.c | 7 +++++++ tools/arch/x86/include/asm/insn.h | 7 ------- tools/arch/x86/lib/insn.c | 7 +++++++ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h index ccf472ae4378..331379889f9f 100644 --- a/arch/x86/include/asm/insn.h +++ b/arch/x86/include/asm/insn.h @@ -133,13 +133,6 @@ 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) -{ - return insn->opcode.got && insn->modrm.got && insn->sib.got && - insn->displacement.got && insn->immediate.got; -} - static inline insn_byte_t insn_vex_m_bits(struct insn *insn) { if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */ diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c index 1f749e3b4a25..4dd05534fffb 100644 --- a/arch/x86/lib/insn.c +++ b/arch/x86/lib/insn.c @@ -702,6 +702,13 @@ int insn_get_length(struct insn *insn) return 0; } +/* Ensure this instruction is decoded completely */ +static inline int insn_complete(struct insn *insn) +{ + return insn->opcode.got && insn->modrm.got && insn->sib.got && + insn->displacement.got && insn->immediate.got; +} + /** * insn_decode() - Decode an x86 instruction * @insn: &struct insn to be initialized diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h index 6c8d6b167bea..5e57a4dcac42 100644 --- a/tools/arch/x86/include/asm/insn.h +++ b/tools/arch/x86/include/asm/insn.h @@ -133,13 +133,6 @@ 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) -{ - return insn->opcode.got && insn->modrm.got && insn->sib.got && - insn->displacement.got && insn->immediate.got; -} - static inline insn_byte_t insn_vex_m_bits(struct insn *insn) { if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */ diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c index 41d2418302d9..bb83822b9e7b 100644 --- a/tools/arch/x86/lib/insn.c +++ b/tools/arch/x86/lib/insn.c @@ -702,6 +702,13 @@ int insn_get_length(struct insn *insn) return 0; } +/* Ensure this instruction is decoded completely */ +static inline int insn_complete(struct insn *insn) +{ + return insn->opcode.got && insn->modrm.got && insn->sib.got && + insn->displacement.got && insn->immediate.got; +} + /** * insn_decode() - Decode an x86 instruction * @insn: &struct insn to be initialized