From: Dylan Hatch <dylanbhatch@google.com>
To: Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nsc@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Weinan Liu <wnliu@google.com>, Jens Remus <jremus@linux.ibm.com>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
linux-crypto@vger.kernel.org, llvm@lists.linux.dev,
Josh Poimboeuf <jpoimboe@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Ard Biesheuvel <ardb@kernel.org>,
Indu Bhagat <ibhagatgnu@gmail.com>,
Prasanna Kumar T S M <ptsm@linux.microsoft.com>,
live-patching@vger.kernel.org,
Dylan Hatch <dylanbhatch@google.com>
Subject: [RFC PATCH 03/12] arm64: eh_frame: track CFA and x29/x30 register state in CFI parser
Date: Tue, 17 Nov 2026 04:34:55 +0000 [thread overview]
Message-ID: <20261117043504.1021063-4-dylanbhatch@google.com> (raw)
In-Reply-To: <20261117043504.1021063-1-dylanbhatch@google.com>
Extend <asm/eh_frame.h> to decode ULEB128/SLEB128 operands, evaluate
initial CIE opcodes, and track CFA (cfa_reg, cfa_off) and x29/x30
frame-record save rules (fp_rule/fp_off, lr_rule/lr_off) across FDE
instructions, including DW_CFA_remember_state and DW_CFA_restore_state.
Signed-off-by: Dylan Hatch <dylanbhatch@google.com>
---
arch/arm64/include/asm/eh_frame.h | 273 +++++++++++++++++++++++++++---
arch/arm64/kernel/pi/patch-scs.c | 1 +
2 files changed, 249 insertions(+), 25 deletions(-)
diff --git a/arch/arm64/include/asm/eh_frame.h b/arch/arm64/include/asm/eh_frame.h
index 6c17fed1860f1..1dcb5b6151bbc 100644
--- a/arch/arm64/include/asm/eh_frame.h
+++ b/arch/arm64/include/asm/eh_frame.h
@@ -2,6 +2,10 @@
/*
* Minimal DWARF .eh_frame CFI parser for arm64.
*
+ * Shared between early boot/module dynamic SCS patching
+ * (arch/arm64/kernel/pi/patch-scs.c) and build-time .lr_unwind generation
+ * (scripts/gen_lr_unwind.c).
+ *
* This minimal DWARF CFI parser is partially based on the code in
* arch/arc/kernel/unwind.c, and on the document below:
* https://refspecs.linuxbase.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
@@ -19,7 +23,11 @@
#define U32_MAX ((u32)~0U)
#endif
+#define AARCH64_DWARF_FP 29
#define AARCH64_DWARF_LR 30
+#define AARCH64_DWARF_SP 31
+
+#define EH_FRAME_STATE_STACK_MAX 8
#define DW_CFA_nop 0x00
#define DW_CFA_advance_loc1 0x02
@@ -27,14 +35,22 @@
#define DW_CFA_advance_loc4 0x04
#define DW_CFA_offset_extended 0x05
#define DW_CFA_restore_extended 0x06
+#define DW_CFA_undefined 0x07
#define DW_CFA_same_value 0x08
+#define DW_CFA_register 0x09
#define DW_CFA_remember_state 0x0a
#define DW_CFA_restore_state 0x0b
#define DW_CFA_def_cfa 0x0c
#define DW_CFA_def_cfa_register 0x0d
#define DW_CFA_def_cfa_offset 0x0e
+#define DW_CFA_def_cfa_expression 0x0f
+#define DW_CFA_expression 0x10
+#define DW_CFA_offset_extended_sf 0x11
+#define DW_CFA_def_cfa_sf 0x12
#define DW_CFA_def_cfa_offset_sf 0x13
+#define DW_CFA_val_expression 0x16
#define DW_CFA_negate_ra_state 0x2d
+#define DW_CFA_GNU_args_size 0x2e
#define DW_EH_PE_sdata4 0x0b
#define DW_EH_PE_sdata8 0x0c
@@ -47,9 +63,27 @@ enum {
EDYNSCS_INVALID_CFA_OPCODE = 4,
};
+enum eh_frame_reg_rule {
+ EH_REG_UNMODIFIED = 0,
+ EH_REG_OFFSET = 1,
+ EH_REG_UNSUPPORTED = 2,
+};
+
+struct eh_frame_reg_state {
+ s64 cfa_off;
+ s64 fp_off;
+ s64 lr_off;
+ u32 cfa_reg;
+ u8 fp_rule;
+ u8 lr_rule;
+};
+
struct eh_frame_cfi_state {
- u64 loc;
- bool ra_negated;
+ u64 loc;
+ struct eh_frame_reg_state reg;
+ struct eh_frame_reg_state stack[EH_FRAME_STATE_STACK_MAX];
+ unsigned int stack_depth;
+ bool ra_negated;
};
struct eh_frame {
@@ -75,6 +109,7 @@ struct eh_frame {
u8 return_address_register;
u8 augmentation_data_size;
u8 fde_pointer_format;
+ u8 cie_opcodes[];
};
struct { /* FDE (sdata4) */
@@ -92,40 +127,100 @@ struct eh_frame {
};
struct eh_frame_fde_iter {
- const u8 *start;
- const u8 *p;
- int size;
- int code_align;
- bool use_sdata8;
- const struct eh_frame *cie;
- const struct eh_frame *fde;
+ const u8 *start;
+ const u8 *p;
+ int size;
+ int code_align;
+ int data_align;
+ bool use_sdata8;
+ struct eh_frame_reg_state cie_reg;
+ const struct eh_frame *cie;
+ const struct eh_frame *fde;
};
#define EH_FRAME_FDE_ITER_INIT(buf, len) \
- { .start = (buf), .p = (buf), .size = (len), .code_align = 1 }
+ { .start = (buf), .p = (buf), .size = (len), \
+ .code_align = 1, .data_align = -4, \
+ .cie_reg = { .cfa_reg = AARCH64_DWARF_SP } }
-/*
- * Skip one uleb128/sleb128 encoded quantity from the opcode stream. All bytes
- * except the last one have bit #7 set.
- */
-static __always_inline int skip_xleb128(const u8 **opcode, int *size)
+static __always_inline int eh_frame_read_uleb128(const u8 **opcode, int *size,
+ u64 *val)
{
+ unsigned int shift = 0;
+ u64 res = 0;
u8 c;
do {
- if (*size <= 0)
+ if (*size <= 0 || shift >= 64)
return EDYNSCS_INVALID_CFA_OPCODE;
c = *(*opcode)++;
(*size)--;
+ if (shift == 63 && c > 1)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ res |= (u64)(c & 0x7f) << shift;
+ shift += 7;
} while (c & BIT(7));
+ *val = res;
+ return 0;
+}
+
+static __always_inline int eh_frame_read_sleb128(const u8 **opcode, int *size,
+ s64 *val)
+{
+ unsigned int shift = 0;
+ u64 res = 0;
+ u8 c;
+
+ do {
+ if (*size <= 0 || shift >= 64)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ c = *(*opcode)++;
+ (*size)--;
+ if (shift == 63 && c != 0x00 && c != 0x7f)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ res |= (u64)(c & 0x7f) << shift;
+ shift += 7;
+ } while (c & BIT(7));
+
+ if (shift < 64 && (c & BIT(6)))
+ res |= ~0ULL << shift;
+
+ *val = (s64)res;
return 0;
}
+static __always_inline void eh_frame_set_reg_rule(struct eh_frame_reg_state *r,
+ u64 reg, u8 rule, s64 off)
+{
+ if (reg == AARCH64_DWARF_FP) {
+ r->fp_rule = rule;
+ r->fp_off = off;
+ } else if (reg == AARCH64_DWARF_LR) {
+ r->lr_rule = rule;
+ r->lr_off = off;
+ }
+}
+
+static __always_inline void eh_frame_restore_reg(struct eh_frame_reg_state *r,
+ const struct eh_frame_reg_state *cie,
+ u64 reg)
+{
+ if (reg == AARCH64_DWARF_FP) {
+ r->fp_rule = cie->fp_rule;
+ r->fp_off = cie->fp_off;
+ } else if (reg == AARCH64_DWARF_LR) {
+ r->lr_rule = cie->lr_rule;
+ r->lr_off = cie->lr_off;
+ }
+}
+
static __always_inline int eh_frame_step_opcode(const u8 **opcode, int *size,
const struct eh_frame_fde_iter *it,
struct eh_frame_cfi_state *st)
{
+ u64 ureg, uoff;
+ s64 soff;
u8 op;
if (*size <= 0)
@@ -137,8 +232,18 @@ static __always_inline int eh_frame_step_opcode(const u8 **opcode, int *size,
switch (op) {
case DW_CFA_nop:
+ break;
+
case DW_CFA_remember_state:
+ if (st->stack_depth >= EH_FRAME_STATE_STACK_MAX)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->stack[st->stack_depth++] = st->reg;
+ break;
+
case DW_CFA_restore_state:
+ if (!st->stack_depth)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->reg = st->stack[--st->stack_depth];
break;
case DW_CFA_advance_loc1:
@@ -168,18 +273,106 @@ static __always_inline int eh_frame_step_opcode(const u8 **opcode, int *size,
*size -= 4;
break;
- case DW_CFA_def_cfa:
case DW_CFA_offset_extended:
- if (skip_xleb128(opcode, size))
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ eh_frame_read_uleb128(opcode, size, &uoff))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ eh_frame_set_reg_rule(&st->reg, ureg, EH_REG_OFFSET,
+ (s64)uoff * it->data_align);
+ break;
+
+ case DW_CFA_offset_extended_sf:
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ eh_frame_read_sleb128(opcode, size, &soff))
return EDYNSCS_INVALID_CFA_OPCODE;
- fallthrough;
+ eh_frame_set_reg_rule(&st->reg, ureg, EH_REG_OFFSET,
+ soff * it->data_align);
+ break;
+
+ case DW_CFA_restore_extended:
+ if (eh_frame_read_uleb128(opcode, size, &ureg))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ eh_frame_restore_reg(&st->reg, &it->cie_reg, ureg);
+ break;
+
+ case DW_CFA_same_value:
+ if (eh_frame_read_uleb128(opcode, size, &ureg))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ eh_frame_set_reg_rule(&st->reg, ureg, EH_REG_UNMODIFIED, 0);
+ break;
+
+ case DW_CFA_undefined:
+ if (eh_frame_read_uleb128(opcode, size, &ureg))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ eh_frame_set_reg_rule(&st->reg, ureg, EH_REG_UNSUPPORTED, 0);
+ break;
+
+ case DW_CFA_register:
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ eh_frame_read_uleb128(opcode, size, &uoff))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ eh_frame_set_reg_rule(&st->reg, ureg, EH_REG_UNSUPPORTED, 0);
+ break;
+
+ case DW_CFA_def_cfa:
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ eh_frame_read_uleb128(opcode, size, &uoff) ||
+ ureg >= U32_MAX)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->reg.cfa_reg = ureg;
+ st->reg.cfa_off = (s64)uoff;
+ break;
+
+ case DW_CFA_def_cfa_sf:
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ eh_frame_read_sleb128(opcode, size, &soff) ||
+ ureg >= U32_MAX)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->reg.cfa_reg = ureg;
+ st->reg.cfa_off = soff * it->data_align;
+ break;
+
+ case DW_CFA_def_cfa_register:
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ ureg >= U32_MAX)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->reg.cfa_reg = ureg;
+ break;
+
case DW_CFA_def_cfa_offset:
+ if (eh_frame_read_uleb128(opcode, size, &uoff))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->reg.cfa_off = (s64)uoff;
+ break;
+
case DW_CFA_def_cfa_offset_sf:
- case DW_CFA_def_cfa_register:
- case DW_CFA_same_value:
- case DW_CFA_restore_extended:
- case 0x80 ... 0xbf: /* DW_CFA_offset */
- if (skip_xleb128(opcode, size))
+ if (eh_frame_read_sleb128(opcode, size, &soff))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ st->reg.cfa_off = soff * it->data_align;
+ break;
+
+ case DW_CFA_def_cfa_expression:
+ if (eh_frame_read_uleb128(opcode, size, &uoff) ||
+ uoff > (u64)*size)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ *opcode += uoff;
+ *size -= uoff;
+ st->reg.cfa_reg = U32_MAX;
+ break;
+
+ case DW_CFA_expression:
+ case DW_CFA_val_expression:
+ if (eh_frame_read_uleb128(opcode, size, &ureg) ||
+ eh_frame_read_uleb128(opcode, size, &uoff) ||
+ uoff > (u64)*size)
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ *opcode += uoff;
+ *size -= uoff;
+ eh_frame_set_reg_rule(&st->reg, ureg, EH_REG_UNSUPPORTED, 0);
+ break;
+
+ case DW_CFA_GNU_args_size:
+ if (eh_frame_read_uleb128(opcode, size, &uoff))
return EDYNSCS_INVALID_CFA_OPCODE;
break;
@@ -191,7 +384,15 @@ static __always_inline int eh_frame_step_opcode(const u8 **opcode, int *size,
st->loc += (op & 0x3f) * it->code_align;
break;
+ case 0x80 ... 0xbf: /* DW_CFA_offset (reg in low 6 bits) */
+ if (eh_frame_read_uleb128(opcode, size, &uoff))
+ return EDYNSCS_INVALID_CFA_OPCODE;
+ eh_frame_set_reg_rule(&st->reg, op & 0x3f, EH_REG_OFFSET,
+ (s64)uoff * it->data_align);
+ break;
+
case 0xc0 ... 0xff: /* DW_CFA_restore (reg in low 6 bits) */
+ eh_frame_restore_reg(&st->reg, &it->cie_reg, op & 0x3f);
break;
default:
@@ -204,7 +405,14 @@ static __always_inline int eh_frame_step_opcode(const u8 **opcode, int *size,
static __always_inline int eh_frame_parse_cie(const struct eh_frame *frame,
struct eh_frame_fde_iter *it)
{
- if (sizeof(frame->size) + frame->size < sizeof(*frame))
+ struct eh_frame_cfi_state cie_st = {
+ .reg = { .cfa_reg = AARCH64_DWARF_SP },
+ };
+ const u8 *opcode;
+ int size, ret;
+
+ if (sizeof(frame->size) + frame->size <
+ offsetof(struct eh_frame, cie_opcodes))
return EDYNSCS_INVALID_CIE_HEADER;
/*
@@ -225,6 +433,9 @@ static __always_inline int eh_frame_parse_cie(const struct eh_frame *frame,
return EDYNSCS_INVALID_CIE_HEADER;
it->code_align = frame->code_alignment_factor;
+ it->data_align = (frame->data_alignment_factor & BIT(6)) ?
+ (int)frame->data_alignment_factor - 128 :
+ frame->data_alignment_factor;
switch (frame->fde_pointer_format) {
case DW_EH_PE_pcrel | DW_EH_PE_sdata4:
@@ -237,6 +448,18 @@ static __always_inline int eh_frame_parse_cie(const struct eh_frame *frame,
return EDYNSCS_INVALID_CIE_SDATA_SIZE;
}
+ it->cie_reg = cie_st.reg;
+ opcode = frame->cie_opcodes;
+ size = (int)(sizeof(frame->size) + frame->size -
+ offsetof(struct eh_frame, cie_opcodes));
+
+ while (size > 0) {
+ ret = eh_frame_step_opcode(&opcode, &size, it, &cie_st);
+ if (ret)
+ return ret;
+ }
+
+ it->cie_reg = cie_st.reg;
it->cie = frame;
return 0;
}
diff --git a/arch/arm64/kernel/pi/patch-scs.c b/arch/arm64/kernel/pi/patch-scs.c
index b240144f7d80e..f4264dda828f7 100644
--- a/arch/arm64/kernel/pi/patch-scs.c
+++ b/arch/arm64/kernel/pi/patch-scs.c
@@ -61,6 +61,7 @@ static int scs_handle_fde_frame(const struct eh_frame_fde_iter *it,
.loc = it->use_sdata8 ?
(u64)&frame->initial_loc64 + frame->initial_loc64 :
(u64)offset_to_ptr(&frame->initial_loc),
+ .reg = it->cie_reg,
};
const u8 *opcode;
int size, ret;
--
2.56.0.679.gc527c65306-goog
next prev parent reply other threads:[~2026-11-17 4:35 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 4:34 [RFC PATCH 00/12] arm64, unwind: add .lr_unwind unwinder for reliable stacktrace Dylan Hatch
2026-11-17 4:34 ` [RFC PATCH 01/12] arm64, unwind: build with .eh_frame and add gen_lr_unwind skeleton Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
2026-11-17 4:34 ` [RFC PATCH 02/12] arm64: factor .eh_frame parser out of patch-scs.c into <asm/eh_frame.h> Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
2026-11-17 4:34 ` Dylan Hatch [this message]
2026-11-17 4:34 ` [RFC PATCH 04/12] arm64, unwind: parse .eh_frame into .lr_unwind entries in gen_lr_unwind Dylan Hatch
2026-11-17 4:34 ` [RFC PATCH 05/12] scripts/gen_lr_unwind: emit .lr_unwind_fde and .lr_unwind_ranges Dylan Hatch
2026-11-17 4:34 ` [RFC PATCH 06/12] scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
2026-11-17 4:34 ` [RFC PATCH 07/12] scripts/sorttable: sort .lr_unwind_fde by start_offset Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 08/12] arm64, unwind: add in-kernel .lr_unwind lookup and validation Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 09/12] arm64/module, unwind: add .lr_unwind support for modules Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 10/12] arm64: entry: add unwind info for call_on_irq_stack() Dylan Hatch
2026-11-17 4:35 ` [RFC PATCH 11/12] arm64, crypto/lib: annotate leaf functions with CFI info Dylan Hatch
2026-11-18 5:07 ` Eric Biggers
2026-11-17 4:35 ` [RFC PATCH 12/12] unwind: arm64: use .lr_unwind to unwind interrupt frames Dylan Hatch
2026-11-18 4:36 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117043504.1021063-4-dylanbhatch@google.com \
--to=dylanbhatch@google.com \
--cc=ardb@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=ibhagatgnu@gmail.com \
--cc=jpoimboe@kernel.org \
--cc=jremus@linux.ibm.com \
--cc=justinstitt@google.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=mark.rutland@arm.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nsc@kernel.org \
--cc=ptsm@linux.microsoft.com \
--cc=rostedt@goodmis.org \
--cc=will@kernel.org \
--cc=wnliu@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.