From nobody Sun Apr 28 22:03:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1608700784; cv=none; d=zohomail.com; s=zohoarc; b=UZ5pZlgpLi0svFIsI2f8XuDSKM6Qsv2DuRBIGuw7LzbL03zA7vpMmhYFw5UtXqSDCQ3sa1YyKRofRNMh12lgxJ/83H3YFRkPO0RtCY1XI/aTLojU/sDG9qvwR/MYoiAQ2gZbuaMAaM/VWMTb4WnCeXmq9wxBYiHgrXDk/9Te5Iw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608700784; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ZbJws1RViL+dhYV0RosYWexYv0hwpQMwpjgoJDxnT9E=; b=ebY/yKe1TiSVwCbJAXi30W+WHLfqzrPTdLmmzrLbLieNT0ub1AiyWpljagU5xBAkNLLs9ya3WTNsAPMlS1/feHpoq7uP83sBQzFWpHg5cJaV42jGAK8kezZCHbJUlsY4Ru7t9jAvn1p2uGwl/Hml1bsE0D+C6GGX/YIXkN7Dg1U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1608700784226658.0925368633597; Tue, 22 Dec 2020 21:19:44 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.58161.102110 (Exim 4.92) (envelope-from ) id 1krwYT-0002S0-CS; Wed, 23 Dec 2020 05:19:21 +0000 Received: by outflank-mailman (output) from mailman id 58161.102110; Wed, 23 Dec 2020 05:19:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1krwYT-0002Rt-9N; Wed, 23 Dec 2020 05:19:21 +0000 Received: by outflank-mailman (input) for mailman id 58161; Wed, 23 Dec 2020 05:19:19 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1krwYR-0002OV-QG for xen-devel@lists.xenproject.org; Wed, 23 Dec 2020 05:19:19 +0000 Received: from us-smtp-delivery-124.mimecast.com (unknown [63.128.21.124]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id a63ff240-9344-47ab-9544-97ce84c407e7; Wed, 23 Dec 2020 05:19:13 +0000 (UTC) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-126-w1lCaopBMcyPQaggoedKzQ-1; Wed, 23 Dec 2020 00:19:08 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9ACE1107ACE6; Wed, 23 Dec 2020 05:19:07 +0000 (UTC) Received: from treble.redhat.com (ovpn-117-91.rdu2.redhat.com [10.10.117.91]) by smtp.corp.redhat.com (Postfix) with ESMTP id D78BE19D9C; Wed, 23 Dec 2020 05:19:06 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a63ff240-9344-47ab-9544-97ce84c407e7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608700753; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZbJws1RViL+dhYV0RosYWexYv0hwpQMwpjgoJDxnT9E=; b=bO4bNhGKNBaWjXiRLOer2cLy8eIm9nXSHjzd8vnNpfko7cM5goSxBDsGSrXWW2tkGnz/bY DMmvjFFcfPvuv+UKEJbCaONeLx0s+4pSF+Sm3B4JS8BCJehHo3OIg58tuHj+7FzDtdwtYM QG4dVy9kFcEoAGyycHcneYgw5/XwPJs= X-MC-Unique: w1lCaopBMcyPQaggoedKzQ-1 From: Josh Poimboeuf To: Juergen Gross Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 1/3] objtool: Refactor ORC section generation Date: Tue, 22 Dec 2020 23:18:08 -0600 Message-Id: <46e2a28520d2d9ddd2a525ecc53a97af1946022f.1608700338.git.jpoimboe@redhat.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Decouple ORC entries from instructions. This simplifies the control/data flow, and is going to make it easier to support alternative instructions which change the stack layout. Signed-off-by: Josh Poimboeuf --- tools/objtool/Makefile | 4 - tools/objtool/arch.h | 4 - tools/objtool/builtin-orc.c | 6 +- tools/objtool/check.h | 3 - tools/objtool/objtool.h | 3 +- tools/objtool/orc_gen.c | 272 ++++++++++++++++++------------------ tools/objtool/weak.c | 7 +- 7 files changed, 140 insertions(+), 159 deletions(-) diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile index 5cdb19036d7f..a43096f713c7 100644 --- a/tools/objtool/Makefile +++ b/tools/objtool/Makefile @@ -46,10 +46,6 @@ ifeq ($(SRCARCH),x86) SUBCMD_ORC :=3D y endif =20 -ifeq ($(SUBCMD_ORC),y) - CFLAGS +=3D -DINSN_USE_ORC -endif - export SUBCMD_CHECK SUBCMD_ORC export srctree OUTPUT CFLAGS SRCARCH AWK include $(srctree)/tools/build/Makefile.include diff --git a/tools/objtool/arch.h b/tools/objtool/arch.h index 4a84c3081b8e..5e3f3ea8bb89 100644 --- a/tools/objtool/arch.h +++ b/tools/objtool/arch.h @@ -11,10 +11,6 @@ #include "objtool.h" #include "cfi.h" =20 -#ifdef INSN_USE_ORC -#include -#endif - enum insn_type { INSN_JUMP_CONDITIONAL, INSN_JUMP_UNCONDITIONAL, diff --git a/tools/objtool/builtin-orc.c b/tools/objtool/builtin-orc.c index 7b31121fa60b..508bdf6ae8dc 100644 --- a/tools/objtool/builtin-orc.c +++ b/tools/objtool/builtin-orc.c @@ -51,11 +51,7 @@ int cmd_orc(int argc, const char **argv) if (list_empty(&file->insn_list)) return 0; =20 - ret =3D create_orc(file); - if (ret) - return ret; - - ret =3D create_orc_sections(file); + ret =3D orc_create(file); if (ret) return ret; =20 diff --git a/tools/objtool/check.h b/tools/objtool/check.h index 5ec00a4b891b..4c10916ff1cf 100644 --- a/tools/objtool/check.h +++ b/tools/objtool/check.h @@ -43,9 +43,6 @@ struct instruction { struct symbol *func; struct list_head stack_ops; struct cfi_state cfi; -#ifdef INSN_USE_ORC - struct orc_entry orc; -#endif }; =20 static inline bool is_static_jump(struct instruction *insn) diff --git a/tools/objtool/objtool.h b/tools/objtool/objtool.h index 4125d4578b23..5e58d3537e2f 100644 --- a/tools/objtool/objtool.h +++ b/tools/objtool/objtool.h @@ -26,7 +26,6 @@ struct objtool_file *objtool_open_read(const char *_objna= me); =20 int check(struct objtool_file *file); int orc_dump(const char *objname); -int create_orc(struct objtool_file *file); -int create_orc_sections(struct objtool_file *file); +int orc_create(struct objtool_file *file); =20 #endif /* _OBJTOOL_H */ diff --git a/tools/objtool/orc_gen.c b/tools/objtool/orc_gen.c index 235663b96adc..73efba2bfa72 100644 --- a/tools/objtool/orc_gen.c +++ b/tools/objtool/orc_gen.c @@ -12,89 +12,84 @@ #include "check.h" #include "warn.h" =20 -int create_orc(struct objtool_file *file) +static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi) { - struct instruction *insn; + struct instruction *insn =3D container_of(cfi, struct instruction, cfi); + struct cfi_reg *bp =3D &cfi->regs[CFI_BP]; =20 - for_each_insn(file, insn) { - struct orc_entry *orc =3D &insn->orc; - struct cfi_reg *cfa =3D &insn->cfi.cfa; - struct cfi_reg *bp =3D &insn->cfi.regs[CFI_BP]; + memset(orc, 0, sizeof(*orc)); =20 - if (!insn->sec->text) - continue; - - orc->end =3D insn->cfi.end; + orc->end =3D cfi->end; =20 - if (cfa->base =3D=3D CFI_UNDEFINED) { - orc->sp_reg =3D ORC_REG_UNDEFINED; - continue; - } - - switch (cfa->base) { - case CFI_SP: - orc->sp_reg =3D ORC_REG_SP; - break; - case CFI_SP_INDIRECT: - orc->sp_reg =3D ORC_REG_SP_INDIRECT; - break; - case CFI_BP: - orc->sp_reg =3D ORC_REG_BP; - break; - case CFI_BP_INDIRECT: - orc->sp_reg =3D ORC_REG_BP_INDIRECT; - break; - case CFI_R10: - orc->sp_reg =3D ORC_REG_R10; - break; - case CFI_R13: - orc->sp_reg =3D ORC_REG_R13; - break; - case CFI_DI: - orc->sp_reg =3D ORC_REG_DI; - break; - case CFI_DX: - orc->sp_reg =3D ORC_REG_DX; - break; - default: - WARN_FUNC("unknown CFA base reg %d", - insn->sec, insn->offset, cfa->base); - return -1; - } + if (cfi->cfa.base =3D=3D CFI_UNDEFINED) { + orc->sp_reg =3D ORC_REG_UNDEFINED; + return 0; + } =20 - switch(bp->base) { - case CFI_UNDEFINED: - orc->bp_reg =3D ORC_REG_UNDEFINED; - break; - case CFI_CFA: - orc->bp_reg =3D ORC_REG_PREV_SP; - break; - case CFI_BP: - orc->bp_reg =3D ORC_REG_BP; - break; - default: - WARN_FUNC("unknown BP base reg %d", - insn->sec, insn->offset, bp->base); - return -1; - } + switch (cfi->cfa.base) { + case CFI_SP: + orc->sp_reg =3D ORC_REG_SP; + break; + case CFI_SP_INDIRECT: + orc->sp_reg =3D ORC_REG_SP_INDIRECT; + break; + case CFI_BP: + orc->sp_reg =3D ORC_REG_BP; + break; + case CFI_BP_INDIRECT: + orc->sp_reg =3D ORC_REG_BP_INDIRECT; + break; + case CFI_R10: + orc->sp_reg =3D ORC_REG_R10; + break; + case CFI_R13: + orc->sp_reg =3D ORC_REG_R13; + break; + case CFI_DI: + orc->sp_reg =3D ORC_REG_DI; + break; + case CFI_DX: + orc->sp_reg =3D ORC_REG_DX; + break; + default: + WARN_FUNC("unknown CFA base reg %d", + insn->sec, insn->offset, cfi->cfa.base); + return -1; + } =20 - orc->sp_offset =3D cfa->offset; - orc->bp_offset =3D bp->offset; - orc->type =3D insn->cfi.type; + switch (bp->base) { + case CFI_UNDEFINED: + orc->bp_reg =3D ORC_REG_UNDEFINED; + break; + case CFI_CFA: + orc->bp_reg =3D ORC_REG_PREV_SP; + break; + case CFI_BP: + orc->bp_reg =3D ORC_REG_BP; + break; + default: + WARN_FUNC("unknown BP base reg %d", + insn->sec, insn->offset, bp->base); + return -1; } =20 + orc->sp_offset =3D cfi->cfa.offset; + orc->bp_offset =3D bp->offset; + orc->type =3D cfi->type; + return 0; } =20 -static int create_orc_entry(struct elf *elf, struct section *u_sec, struct= section *ip_relocsec, - unsigned int idx, struct section *insn_sec, - unsigned long insn_off, struct orc_entry *o) +static int write_orc_entry(struct elf *elf, struct section *orc_sec, + struct section *ip_rsec, unsigned int idx, + struct section *insn_sec, unsigned long insn_off, + struct orc_entry *o) { struct orc_entry *orc; struct reloc *reloc; =20 /* populate ORC data */ - orc =3D (struct orc_entry *)u_sec->data->d_buf + idx; + orc =3D (struct orc_entry *)orc_sec->data->d_buf + idx; memcpy(orc, o, sizeof(*orc)); =20 /* populate reloc for ip */ @@ -133,102 +128,109 @@ static int create_orc_entry(struct elf *elf, struct= section *u_sec, struct secti =20 reloc->type =3D R_X86_64_PC32; reloc->offset =3D idx * sizeof(int); - reloc->sec =3D ip_relocsec; + reloc->sec =3D ip_rsec; =20 elf_add_reloc(elf, reloc); =20 return 0; } =20 -int create_orc_sections(struct objtool_file *file) +struct orc_list_entry { + struct list_head list; + struct orc_entry orc; + struct section *insn_sec; + unsigned long insn_off; +}; + +static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc, + struct section *sec, unsigned long offset) +{ + struct orc_list_entry *entry =3D malloc(sizeof(*entry)); + + if (!entry) { + WARN("malloc failed"); + return -1; + } + + entry->orc =3D *orc; + entry->insn_sec =3D sec; + entry->insn_off =3D offset; + + list_add_tail(&entry->list, orc_list); + return 0; +} + +int orc_create(struct objtool_file *file) { - struct instruction *insn, *prev_insn; - struct section *sec, *u_sec, *ip_relocsec; - unsigned int idx; + struct section *sec, *ip_rsec, *orc_sec; + unsigned int nr =3D 0, idx =3D 0; + struct orc_list_entry *entry; + struct list_head orc_list; =20 - struct orc_entry empty =3D { - .sp_reg =3D ORC_REG_UNDEFINED, + struct orc_entry null =3D { + .sp_reg =3D ORC_REG_UNDEFINED, .bp_reg =3D ORC_REG_UNDEFINED, .type =3D UNWIND_HINT_TYPE_CALL, }; =20 - sec =3D find_section_by_name(file->elf, ".orc_unwind"); - if (sec) { - WARN("file already has .orc_unwind section, skipping"); - return -1; - } - - /* count the number of needed orcs */ - idx =3D 0; + /* Build a deduplicated list of ORC entries: */ + INIT_LIST_HEAD(&orc_list); for_each_sec(file, sec) { + struct orc_entry orc, prev_orc =3D {0}; + struct instruction *insn; + bool empty =3D true; + if (!sec->text) continue; =20 - prev_insn =3D NULL; sec_for_each_insn(file, sec, insn) { - if (!prev_insn || - memcmp(&insn->orc, &prev_insn->orc, - sizeof(struct orc_entry))) { - idx++; - } - prev_insn =3D insn; + if (init_orc_entry(&orc, &insn->cfi)) + return -1; + if (!memcmp(&prev_orc, &orc, sizeof(orc))) + continue; + if (orc_list_add(&orc_list, &orc, sec, insn->offset)) + return -1; + nr++; + prev_orc =3D orc; + empty =3D false; } =20 - /* section terminator */ - if (prev_insn) - idx++; + /* Add a section terminator */ + if (!empty) { + orc_list_add(&orc_list, &null, sec, sec->len); + nr++; + } } - if (!idx) - return -1; + if (!nr) + return 0; =20 + /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */ + sec =3D find_section_by_name(file->elf, ".orc_unwind"); + if (sec) { + WARN("file already has .orc_unwind section, skipping"); + return -1; + } + orc_sec =3D elf_create_section(file->elf, ".orc_unwind", 0, + sizeof(struct orc_entry), nr); + if (!orc_sec) + return -1; =20 - /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */ - sec =3D elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), i= dx); + sec =3D elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), n= r); if (!sec) return -1; - - ip_relocsec =3D elf_create_reloc_section(file->elf, sec, SHT_RELA); - if (!ip_relocsec) + ip_rsec =3D elf_create_reloc_section(file->elf, sec, SHT_RELA); + if (!ip_rsec) return -1; =20 - /* create .orc_unwind section */ - u_sec =3D elf_create_section(file->elf, ".orc_unwind", 0, - sizeof(struct orc_entry), idx); - - /* populate sections */ - idx =3D 0; - for_each_sec(file, sec) { - if (!sec->text) - continue; - - prev_insn =3D NULL; - sec_for_each_insn(file, sec, insn) { - if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc, - sizeof(struct orc_entry))) { - - if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx, - insn->sec, insn->offset, - &insn->orc)) - return -1; - - idx++; - } - prev_insn =3D insn; - } - - /* section terminator */ - if (prev_insn) { - if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx, - prev_insn->sec, - prev_insn->offset + prev_insn->len, - &empty)) - return -1; - - idx++; - } + /* Write ORC entries to sections: */ + list_for_each_entry(entry, &orc_list, list) { + if (write_orc_entry(file->elf, orc_sec, ip_rsec, idx++, + entry->insn_sec, entry->insn_off, + &entry->orc)) + return -1; } =20 - if (elf_rebuild_reloc_section(file->elf, ip_relocsec)) + if (elf_rebuild_reloc_section(file->elf, ip_rsec)) return -1; =20 return 0; diff --git a/tools/objtool/weak.c b/tools/objtool/weak.c index 7843e9a7a72f..553ec9ce51ba 100644 --- a/tools/objtool/weak.c +++ b/tools/objtool/weak.c @@ -25,12 +25,7 @@ int __weak orc_dump(const char *_objname) UNSUPPORTED("orc"); } =20 -int __weak create_orc(struct objtool_file *file) -{ - UNSUPPORTED("orc"); -} - -int __weak create_orc_sections(struct objtool_file *file) +int __weak orc_create(struct objtool_file *file) { UNSUPPORTED("orc"); } --=20 2.29.2 From nobody Sun Apr 28 22:03:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1608700782; cv=none; d=zohomail.com; s=zohoarc; b=WgcSUHsN5ZOVWBs+S0gXGsb+Oq11fmS48/3pwffSQ4CxJcqpDw6DwOU9nG4M280Wn89LTXusGT2WHovhpj7X4viMcc+82FvrkhPhEDd3dEaBuwu68jaFf3XMvRTFKQyZ5sP8/tfh9nfRqdRPSFXBY+XGY6zHv9MKUDdE2DbX+kc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608700782; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9FLECD3fszJkQ0foq9Jee/a0IqJ4VTmTS/l3JtWjF48=; b=CtwpXihSLAouRHe0jeGz3h2eKYaXy8aifpXOPXaUMtWGQ5gC5ZpPA5oMt28V/5jgv3j00SGDdjHiKy3lGMd5ACKxOlaHY5e+tBtfwdUorhMpeDhtjh7rN/qcM5E/DpbUS6sRfSW8POHulN2MiQ7DAV6KlNtNYC/OlNx9C4O5VJ0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1608700782392890.4017499243589; Tue, 22 Dec 2020 21:19:42 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.58159.102086 (Exim 4.92) (envelope-from ) id 1krwYN-0002Oo-R3; Wed, 23 Dec 2020 05:19:15 +0000 Received: by outflank-mailman (output) from mailman id 58159.102086; Wed, 23 Dec 2020 05:19:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1krwYN-0002Oh-N7; Wed, 23 Dec 2020 05:19:15 +0000 Received: by outflank-mailman (input) for mailman id 58159; Wed, 23 Dec 2020 05:19:15 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1krwYM-0002OU-NM for xen-devel@lists.xenproject.org; Wed, 23 Dec 2020 05:19:14 +0000 Received: from us-smtp-delivery-124.mimecast.com (unknown [63.128.21.124]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 0c03b833-e60d-4677-a353-467c4576d44a; Wed, 23 Dec 2020 05:19:12 +0000 (UTC) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-438-Y_oG4oo9O1GqqGNZNmnjhw-1; Wed, 23 Dec 2020 00:19:09 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 700571015C82; Wed, 23 Dec 2020 05:19:08 +0000 (UTC) Received: from treble.redhat.com (ovpn-117-91.rdu2.redhat.com [10.10.117.91]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2D3D19D9C; Wed, 23 Dec 2020 05:19:07 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0c03b833-e60d-4677-a353-467c4576d44a DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608700752; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9FLECD3fszJkQ0foq9Jee/a0IqJ4VTmTS/l3JtWjF48=; b=WztnSoNJN9MHhk7wuDEL8Mwt3/YVi6h2wEmwUlBcH1wu9dnzdNiMHEik/76yTWtqFawd9T 0v728JNeQwJZh9fWU5h+gZvZ8IH4rGucipdv/8Ye2K4tWgXYFr/UwSLvdjqK7vBsf+2vfT AJAUP96U/54+3OHRJUZOqYv+jq1FVOk= X-MC-Unique: Y_oG4oo9O1GqqGNZNmnjhw-1 From: Josh Poimboeuf To: Juergen Gross Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 2/3] objtool: Add 'alt_group' struct Date: Tue, 22 Dec 2020 23:18:09 -0600 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Create a new struct associated with each group of alternatives instructions. This will help with the removal of fake jumps, and more importantly with adding support for stack layout changes in alternatives. Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 29 +++++++++++++++++++++++------ tools/objtool/check.h | 13 ++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index c6ab44543c92..67f39b57c6f7 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -984,20 +984,28 @@ static int handle_group_alt(struct objtool_file *file, struct instruction *orig_insn, struct instruction **new_insn) { - static unsigned int alt_group_next_index =3D 1; struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump =3D= NULL; - unsigned int alt_group =3D alt_group_next_index++; + struct alt_group *orig_alt_group, *new_alt_group; unsigned long dest_off; =20 + + orig_alt_group =3D malloc(sizeof(*orig_alt_group)); + if (!orig_alt_group) { + WARN("malloc failed"); + return -1; + } last_orig_insn =3D NULL; insn =3D orig_insn; sec_for_each_insn_from(file, insn) { if (insn->offset >=3D special_alt->orig_off + special_alt->orig_len) break; =20 - insn->alt_group =3D alt_group; + insn->alt_group =3D orig_alt_group; last_orig_insn =3D insn; } + orig_alt_group->orig_group =3D NULL; + orig_alt_group->first_insn =3D orig_insn; + orig_alt_group->last_insn =3D last_orig_insn; =20 if (next_insn_same_sec(file, last_orig_insn)) { fake_jump =3D malloc(sizeof(*fake_jump)); @@ -1028,8 +1036,13 @@ static int handle_group_alt(struct objtool_file *fil= e, return 0; } =20 + new_alt_group =3D malloc(sizeof(*new_alt_group)); + if (!new_alt_group) { + WARN("malloc failed"); + return -1; + } + last_new_insn =3D NULL; - alt_group =3D alt_group_next_index++; insn =3D *new_insn; sec_for_each_insn_from(file, insn) { struct reloc *alt_reloc; @@ -1041,7 +1054,7 @@ static int handle_group_alt(struct objtool_file *file, =20 insn->ignore =3D orig_insn->ignore_alts; insn->func =3D orig_insn->func; - insn->alt_group =3D alt_group; + insn->alt_group =3D new_alt_group; =20 /* * Since alternative replacement code is copy/pasted by the @@ -1090,6 +1103,10 @@ static int handle_group_alt(struct objtool_file *fil= e, return -1; } =20 + new_alt_group->orig_group =3D orig_alt_group; + new_alt_group->first_insn =3D *new_insn; + new_alt_group->last_insn =3D last_new_insn; + if (fake_jump) list_add(&fake_jump->list, &last_new_insn->list); =20 @@ -2405,7 +2422,7 @@ static int validate_return(struct symbol *func, struc= t instruction *insn, struct static void fill_alternative_cfi(struct objtool_file *file, struct instruc= tion *insn) { struct instruction *first_insn =3D insn; - int alt_group =3D insn->alt_group; + struct alt_group *alt_group =3D insn->alt_group; =20 sec_for_each_insn_continue(file, insn) { if (insn->alt_group !=3D alt_group) diff --git a/tools/objtool/check.h b/tools/objtool/check.h index 4c10916ff1cf..b74c383c2d83 100644 --- a/tools/objtool/check.h +++ b/tools/objtool/check.h @@ -19,6 +19,17 @@ struct insn_state { s8 instr; }; =20 +struct alt_group { + /* + * Pointer from a replacement group to the original group. NULL if it + * *is* the original group. + */ + struct alt_group *orig_group; + + /* First and last instructions in the group */ + struct instruction *first_insn, *last_insn; +}; + struct instruction { struct list_head list; struct hlist_node hash; @@ -34,7 +45,7 @@ struct instruction { s8 instr; u8 visited; u8 ret_offset; - int alt_group; + struct alt_group *alt_group; struct symbol *call_dest; struct instruction *jump_dest; struct instruction *first_jump_src; --=20 2.29.2 From nobody Sun Apr 28 22:03:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1608700784; cv=none; d=zohomail.com; s=zohoarc; b=I3oflw/PjpSlTi1LYQAxFtQ8PM9tn8Rle6yx0pJXWKRZK1jDQfZS7lyNZntsw+vpQVxLshQh4q0/RtqRuduJYxyWPjQmcV2fh38+d1+DQrjetesG6+b+kzDpfN8tLNhuMt9IRJIWTK8y4dpaylNbopQ7oVjqjv2+19Dst2D3CY4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608700784; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7GfgB281hzscc78Xu7z2MmSIJrE3t6vhDKCus+aT9gA=; b=ZNW5wHVYbL6V7SU7CnFAptnYwUeL/yCG4V9FmjbErTrRn0SzZu1+RazmcWyfp/p2zZKz5jwZN+mfwUbl22vxmpb3zZsj8bsZXu9yNxul3g3LB5AJQycoB/yxiORvHcT40JVjTlFZKXz1Iqn79xoc5J9bwY/8/aAnXpiGpz43vXk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1608700784879952.8607780818028; Tue, 22 Dec 2020 21:19:44 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.58164.102122 (Exim 4.92) (envelope-from ) id 1krwYY-0002Wk-Md; Wed, 23 Dec 2020 05:19:26 +0000 Received: by outflank-mailman (output) from mailman id 58164.102122; Wed, 23 Dec 2020 05:19:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1krwYY-0002Wd-JT; Wed, 23 Dec 2020 05:19:26 +0000 Received: by outflank-mailman (input) for mailman id 58164; Wed, 23 Dec 2020 05:19:24 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1krwYW-0002OV-QN for xen-devel@lists.xenproject.org; Wed, 23 Dec 2020 05:19:24 +0000 Received: from us-smtp-delivery-124.mimecast.com (unknown [63.128.21.124]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id 964faad1-99ad-44e8-92f4-6a7cf143853c; Wed, 23 Dec 2020 05:19:15 +0000 (UTC) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-406-OYXhaivwNG27pgyrOvM83g-1; Wed, 23 Dec 2020 00:19:11 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8C7CA18C8C02; Wed, 23 Dec 2020 05:19:09 +0000 (UTC) Received: from treble.redhat.com (ovpn-117-91.rdu2.redhat.com [10.10.117.91]) by smtp.corp.redhat.com (Postfix) with ESMTP id A400319D9C; Wed, 23 Dec 2020 05:19:08 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 964faad1-99ad-44e8-92f4-6a7cf143853c DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608700755; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7GfgB281hzscc78Xu7z2MmSIJrE3t6vhDKCus+aT9gA=; b=BRDPlzYLGZxjOjK1kSh5NMB7++HUO+SUs3CJCT15W3G2lbI5UYOQ2idVD6VdVt4kcyHQ8J rcTq3+3nwjGVOfmdPAjyYwaP+7l95Cc705J7JJvJDYZFv0UJbAicnMLSkUIwGDriazXY2b PCxuTbbrhmhcdEUZ4LlZYKjyAFBWu7c= X-MC-Unique: OYXhaivwNG27pgyrOvM83g-1 From: Josh Poimboeuf To: Juergen Gross Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes , Shinichiro Kawasaki Subject: [PATCH 3/3] objtool: Support stack layout changes in alternatives Date: Tue, 22 Dec 2020 23:18:10 -0600 Message-Id: <9f78604e49b400eb3b2ca613591f8c357474ed4e.1608700338.git.jpoimboe@redhat.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" The ORC unwinder showed a warning [1] which revealed the stack layout didn't match what was expected. The problem was that paravirt patching had replaced "CALL *pv_ops.irq.save_fl" with "PUSHF;POP". That changed the stack layout between the PUSHF and the POP, so unwinding from an interrupt which occurred between those two instructions would fail. Part of the agreed upon solution was to rework the custom paravirt patching code to use alternatives instead, since objtool already knows how to read alternatives (and converging runtime patching infrastructure is always a good thing anyway). But the main problem still remains, which is that runtime patching can change the stack layout. Making stack layout changes in alternatives was disallowed with commit 7117f16bf460 ("objtool: Fix ORC vs alternatives"), but now that paravirt is going to be doing it, it needs to be supported. One way to do so would be to modify the ORC table when the code gets patched. But ORC is simple -- a good thing! -- and it's best to leave it alone. Instead, support stack layout changes by "flattening" all possible stack states (CFI) from parallel alternative code streams into a single set of linear states. The only necessary limitation is that CFI conflicts are disallowed at all possible instruction boundaries. For example, this scenario is allowed: Alt1 Alt2 Alt3 0x00 CALL *pv_ops.save_fl CALL xen_save_fl PUSHF 0x01 POP %RAX 0x02 NOP ... 0x05 NOP ... 0x07 The unwind information for offset-0x00 is identical for all 3 alternatives. Similarly offset-0x05 and higher also are identical (and the same as 0x00). However offset-0x01 has deviating CFI, but that is only relevant for Alt3, neither of the other alternative instruction streams will ever hit that offset. This scenario is NOT allowed: Alt1 Alt2 0x00 CALL *pv_ops.save_fl PUSHF 0x01 NOP6 ... 0x07 NOP POP %RAX The problem here is that offset-0x7, which is an instruction boundary in both possible instruction patch streams, has two conflicting stack layouts. [ The above examples were stolen from Peter Zijlstra. ] The new flattened CFI array is used both for the detection of conflicts (like the second example above) and the generation of linear ORC entries. BTW, another benefit of these changes is that, thanks to some related cleanups (new fake nops and alt_group struct) objtool can finally be rid of fake jumps, which were a constant source of headaches. [1] https://lkml.kernel.org/r/20201111170536.arx2zbn4ngvjoov7@treble Cc: Shinichiro Kawasaki Signed-off-by: Josh Poimboeuf --- .../Documentation/stack-validation.txt | 16 +- tools/objtool/check.c | 175 ++++++++++-------- tools/objtool/check.h | 6 + tools/objtool/orc_gen.c | 56 +++++- 4 files changed, 157 insertions(+), 96 deletions(-) diff --git a/tools/objtool/Documentation/stack-validation.txt b/tools/objto= ol/Documentation/stack-validation.txt index 0542e46c7552..30f38fdc0d56 100644 --- a/tools/objtool/Documentation/stack-validation.txt +++ b/tools/objtool/Documentation/stack-validation.txt @@ -315,13 +315,15 @@ they mean, and suggestions for how to fix them. function tracing inserts additional calls, which is not obvious from= the sources). =20 -10. file.o: warning: func()+0x5c: alternative modifies stack - - This means that an alternative includes instructions that modify the - stack. The problem is that there is only one ORC unwind table, this me= ans - that the ORC unwind entries must be valid for each of the alternatives. - The easiest way to enforce this is to ensure alternatives do not conta= in - any ORC entries, which in turn implies the above constraint. +10. file.o: warning: func()+0x5c: stack layout conflict in alternatives + + This means that in the use of the alternative() or ALTERNATIVE() + macro, the code paths have conflicting modifications to the stack. + The problem is that there is only one ORC unwind table, which means + that the ORC unwind entries must be consistent for all possible + instruction boundaries regardless of which code has been patched. + This limitation can be overcome by massaging the alternatives with + NOPs to shift the stack changes around so they no longer conflict. =20 11. file.o: warning: unannotated intra-function call =20 diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 67f39b57c6f7..81d56fdef1c3 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -19,8 +19,6 @@ #include #include =20 -#define FAKE_JUMP_OFFSET -1 - struct alternative { struct list_head list; struct instruction *insn; @@ -767,9 +765,6 @@ static int add_jump_destinations(struct objtool_file *f= ile) if (!is_static_jump(insn)) continue; =20 - if (insn->offset =3D=3D FAKE_JUMP_OFFSET) - continue; - reloc =3D find_reloc_by_dest_range(file->elf, insn->sec, insn->offset, insn->len); if (!reloc) { @@ -984,7 +979,7 @@ static int handle_group_alt(struct objtool_file *file, struct instruction *orig_insn, struct instruction **new_insn) { - struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump =3D= NULL; + struct instruction *last_orig_insn, *last_new_insn =3D NULL, *insn, *nop = =3D NULL; struct alt_group *orig_alt_group, *new_alt_group; unsigned long dest_off; =20 @@ -994,6 +989,13 @@ static int handle_group_alt(struct objtool_file *file, WARN("malloc failed"); return -1; } + orig_alt_group->cfi =3D calloc(special_alt->orig_len, + sizeof(struct cfi_state *)); + if (!orig_alt_group->cfi) { + WARN("calloc failed"); + return -1; + } + last_orig_insn =3D NULL; insn =3D orig_insn; sec_for_each_insn_from(file, insn) { @@ -1007,42 +1009,45 @@ static int handle_group_alt(struct objtool_file *fi= le, orig_alt_group->first_insn =3D orig_insn; orig_alt_group->last_insn =3D last_orig_insn; =20 - if (next_insn_same_sec(file, last_orig_insn)) { - fake_jump =3D malloc(sizeof(*fake_jump)); - if (!fake_jump) { - WARN("malloc failed"); - return -1; - } - memset(fake_jump, 0, sizeof(*fake_jump)); - INIT_LIST_HEAD(&fake_jump->alts); - INIT_LIST_HEAD(&fake_jump->stack_ops); - init_cfi_state(&fake_jump->cfi); =20 - fake_jump->sec =3D special_alt->new_sec; - fake_jump->offset =3D FAKE_JUMP_OFFSET; - fake_jump->type =3D INSN_JUMP_UNCONDITIONAL; - fake_jump->jump_dest =3D list_next_entry(last_orig_insn, list); - fake_jump->func =3D orig_insn->func; + new_alt_group =3D malloc(sizeof(*new_alt_group)); + if (!new_alt_group) { + WARN("malloc failed"); + return -1; } =20 - if (!special_alt->new_len) { - if (!fake_jump) { - WARN("%s: empty alternative at end of section", - special_alt->orig_sec->name); + if (special_alt->new_len < special_alt->orig_len) { + /* + * Insert a fake nop at the end to make the replacement + * alt_group the same size as the original. This is needed to + * allow propagate_alt_cfi() to do its magic. When the last + * instruction affects the stack, the instruction after it (the + * nop) will propagate the new state to the shared CFI array. + */ + nop =3D malloc(sizeof(*nop)); + if (!nop) { + WARN("malloc failed"); return -1; } + memset(nop, 0, sizeof(*nop)); + INIT_LIST_HEAD(&nop->alts); + INIT_LIST_HEAD(&nop->stack_ops); + init_cfi_state(&nop->cfi); =20 - *new_insn =3D fake_jump; - return 0; + nop->sec =3D special_alt->new_sec; + nop->offset =3D special_alt->new_off + special_alt->new_len; + nop->len =3D special_alt->orig_len - special_alt->new_len; + nop->type =3D INSN_NOP; + nop->func =3D orig_insn->func; + nop->alt_group =3D new_alt_group; + nop->ignore =3D orig_insn->ignore_alts; } =20 - new_alt_group =3D malloc(sizeof(*new_alt_group)); - if (!new_alt_group) { - WARN("malloc failed"); - return -1; + if (!special_alt->new_len) { + *new_insn =3D nop; + goto end; } =20 - last_new_insn =3D NULL; insn =3D *new_insn; sec_for_each_insn_from(file, insn) { struct reloc *alt_reloc; @@ -1081,14 +1086,8 @@ static int handle_group_alt(struct objtool_file *fil= e, continue; =20 dest_off =3D arch_jump_destination(insn); - if (dest_off =3D=3D special_alt->new_off + special_alt->new_len) { - if (!fake_jump) { - WARN("%s: alternative jump to end of section", - special_alt->orig_sec->name); - return -1; - } - insn->jump_dest =3D fake_jump; - } + if (dest_off =3D=3D special_alt->new_off + special_alt->new_len) + insn->jump_dest =3D next_insn_same_sec(file, last_orig_insn); =20 if (!insn->jump_dest) { WARN_FUNC("can't find alternative jump destination", @@ -1103,13 +1102,13 @@ static int handle_group_alt(struct objtool_file *fi= le, return -1; } =20 + if (nop) + list_add(&nop->list, &last_new_insn->list); +end: new_alt_group->orig_group =3D orig_alt_group; new_alt_group->first_insn =3D *new_insn; - new_alt_group->last_insn =3D last_new_insn; - - if (fake_jump) - list_add(&fake_jump->list, &last_new_insn->list); - + new_alt_group->last_insn =3D nop ? : last_new_insn; + new_alt_group->cfi =3D orig_alt_group->cfi; return 0; } =20 @@ -2202,22 +2201,47 @@ static int update_cfi_state(struct instruction *ins= n, struct cfi_state *cfi, return 0; } =20 -static int handle_insn_ops(struct instruction *insn, struct insn_state *st= ate) +/* + * The stack layouts of alternatives instructions can sometimes diverge wh= en + * they have stack modifications. That's fine as long as the potential st= ack + * layouts don't conflict at any given potential instruction boundary. + * + * Flatten the CFIs of the different alternative code streams (both origin= al + * and replacement) into a single shared CFI array which can be used to de= tect + * conflicts and nicely feed a linear array of ORC entries to the unwinder. + */ +static int propagate_alt_cfi(struct objtool_file *file, struct instruction= *insn) { - struct stack_op *op; + struct cfi_state **alt_cfi; + int group_off; =20 - list_for_each_entry(op, &insn->stack_ops, list) { - struct cfi_state old_cfi =3D state->cfi; - int res; + if (!insn->alt_group) + return 0; =20 - res =3D update_cfi_state(insn, &state->cfi, op); - if (res) - return res; + alt_cfi =3D insn->alt_group->cfi; + group_off =3D insn->offset - insn->alt_group->first_insn->offset; =20 - if (insn->alt_group && memcmp(&state->cfi, &old_cfi, sizeof(struct cfi_s= tate))) { - WARN_FUNC("alternative modifies stack", insn->sec, insn->offset); + if (!alt_cfi[group_off]) { + alt_cfi[group_off] =3D &insn->cfi; + } else { + if (memcmp(alt_cfi[group_off], &insn->cfi, sizeof(struct cfi_state))) { + WARN_FUNC("stack layout conflict in alternatives", + insn->sec, insn->offset); return -1; } + } + + return 0; +} + +static int handle_insn_ops(struct instruction *insn, struct insn_state *st= ate) +{ + struct stack_op *op; + + list_for_each_entry(op, &insn->stack_ops, list) { + + if (update_cfi_state(insn, &state->cfi, op)) + return 1; =20 if (op->dest.type =3D=3D OP_DEST_PUSHF) { if (!state->uaccess_stack) { @@ -2407,28 +2431,20 @@ static int validate_return(struct symbol *func, str= uct instruction *insn, struct return 0; } =20 -/* - * Alternatives should not contain any ORC entries, this in turn means they - * should not contain any CFI ops, which implies all instructions should h= ave - * the same same CFI state. - * - * It is possible to constuct alternatives that have unreachable holes tha= t go - * unreported (because they're NOPs), such holes would result in CFI_UNDEF= INED - * states which then results in ORC entries, which we just said we didn't = want. - * - * Avoid them by copying the CFI entry of the first instruction into the w= hole - * alternative. - */ -static void fill_alternative_cfi(struct objtool_file *file, struct instruc= tion *insn) +static struct instruction *next_insn_to_validate(struct objtool_file *file, + struct instruction *insn) { - struct instruction *first_insn =3D insn; struct alt_group *alt_group =3D insn->alt_group; =20 - sec_for_each_insn_continue(file, insn) { - if (insn->alt_group !=3D alt_group) - break; - insn->cfi =3D first_insn->cfi; - } + /* + * Simulate the fact that alternatives are patched in-place. When the + * end of a replacement alt_group is reached, redirect objtool flow to + * the end of the original alt_group. + */ + if (alt_group && insn =3D=3D alt_group->last_insn && alt_group->orig_grou= p) + return next_insn_same_sec(file, alt_group->orig_group->last_insn); + + return next_insn_same_sec(file, insn); } =20 /* @@ -2449,7 +2465,7 @@ static int validate_branch(struct objtool_file *file,= struct symbol *func, sec =3D insn->sec; =20 while (1) { - next_insn =3D next_insn_same_sec(file, insn); + next_insn =3D next_insn_to_validate(file, insn); =20 if (file->c_file && func && insn->func && func !=3D insn->func->pfunc) { WARN("%s() falls through to next function %s()", @@ -2482,6 +2498,9 @@ static int validate_branch(struct objtool_file *file,= struct symbol *func, =20 insn->visited |=3D visited; =20 + if (propagate_alt_cfi(file, insn)) + return 1; + if (!insn->ignore_alts && !list_empty(&insn->alts)) { bool skip_orig =3D false; =20 @@ -2497,9 +2516,6 @@ static int validate_branch(struct objtool_file *file,= struct symbol *func, } } =20 - if (insn->alt_group) - fill_alternative_cfi(file, insn); - if (skip_orig) return 0; } @@ -2733,9 +2749,6 @@ static bool ignore_unreachable_insn(struct objtool_fi= le *file, struct instructio !strcmp(insn->sec->name, ".altinstr_aux")) return true; =20 - if (insn->type =3D=3D INSN_JUMP_UNCONDITIONAL && insn->offset =3D=3D FAKE= _JUMP_OFFSET) - return true; - if (!insn->func) return false; =20 diff --git a/tools/objtool/check.h b/tools/objtool/check.h index b74c383c2d83..45fe87ad662b 100644 --- a/tools/objtool/check.h +++ b/tools/objtool/check.h @@ -28,6 +28,12 @@ struct alt_group { =20 /* First and last instructions in the group */ struct instruction *first_insn, *last_insn; + + /* + * Byte-offset-addressed len-sized array of pointers to CFI structs. + * This is shared with the other alt_groups in the same alternative. + */ + struct cfi_state **cfi; }; =20 struct instruction { diff --git a/tools/objtool/orc_gen.c b/tools/objtool/orc_gen.c index 73efba2bfa72..2c1e3b909be5 100644 --- a/tools/objtool/orc_gen.c +++ b/tools/objtool/orc_gen.c @@ -160,6 +160,13 @@ static int orc_list_add(struct list_head *orc_list, st= ruct orc_entry *orc, return 0; } =20 +static unsigned long alt_group_len(struct alt_group *alt_group) +{ + return alt_group->last_insn->offset + + alt_group->last_insn->len - + alt_group->first_insn->offset; +} + int orc_create(struct objtool_file *file) { struct section *sec, *ip_rsec, *orc_sec; @@ -184,15 +191,48 @@ int orc_create(struct objtool_file *file) continue; =20 sec_for_each_insn(file, sec, insn) { - if (init_orc_entry(&orc, &insn->cfi)) - return -1; - if (!memcmp(&prev_orc, &orc, sizeof(orc))) + struct alt_group *alt_group =3D insn->alt_group; + int i; + + if (!alt_group) { + if (init_orc_entry(&orc, &insn->cfi)) + return -1; + if (!memcmp(&prev_orc, &orc, sizeof(orc))) + continue; + if (orc_list_add(&orc_list, &orc, sec, + insn->offset)) + return -1; + nr++; + prev_orc =3D orc; + empty =3D false; continue; - if (orc_list_add(&orc_list, &orc, sec, insn->offset)) - return -1; - nr++; - prev_orc =3D orc; - empty =3D false; + } + + /* + * Alternatives can have different stack layout + * possibilities (but they shouldn't conflict). + * Instead of traversing the instructions, use the + * alt_group's flattened byte-offset-addressed CFI + * array. + */ + for (i =3D 0; i < alt_group_len(alt_group); i++) { + struct cfi_state *cfi =3D alt_group->cfi[i]; + if (!cfi) + continue; + if (init_orc_entry(&orc, cfi)) + return -1; + if (!memcmp(&prev_orc, &orc, sizeof(orc))) + continue; + if (orc_list_add(&orc_list, &orc, insn->sec, + insn->offset + i)) + return -1; + nr++; + prev_orc =3D orc; + empty =3D false; + } + + /* Skip to the end of the alt_group */ + insn =3D alt_group->last_insn; } =20 /* Add a section terminator */ --=20 2.29.2