Skip to contentSkip to conversation

m68k, bpf: Add initial BPF JIT compiler support

PATCH Superseded Kuan-Wei Chiu 6 participants 10 messages Apr 29 – May 3

Subsystems (2)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 m68k, bpf: Add initial BPF JIT compiler support

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors. It currently excludes 68000/68010 and coldfire processors, as it relies on 32 bit branch displacements (b<cc>.l) to handle large bpf programs.

Tested with the test_bpf.ko:
test_bpf: Summary: 1053 PASSED, 0 FAILED, [1041/1041 JIT'ed]
test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]

---
Assuming this should be routed through the bpf tree like other arch bpf jit patches, rather than the m68k tree.

 MAINTAINERS                  |    6 +
 arch/m68k/Kbuild             |    2 +-
 arch/m68k/Kconfig            |    1 +
 arch/m68k/net/Makefile       |    2 +
 arch/m68k/net/bpf_jit_comp.c | 1543 ++++++++++++++++++++++++++++++++++
 5 files changed, 1553 insertions(+), 1 deletion(-)
 create mode 100644 arch/m68k/net/Makefile
 create mode 100644 arch/m68k/net/bpf_jit_comp.c
MAINTAINERS +6 -0
index 2fb1c75afd16..6e8a553769d7 100644
@@ -4701,6 +4701,12 @@ L:	[email protected]
 S:	Maintained
 F:	arch/loongarch/net/
 
+BPF JIT FOR M68K
+M:	Kuan-Wei Chiu <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	arch/m68k/net/
+
 BPF JIT for MIPS (32-BIT AND 64-BIT)
 M:	Johan Almbladh <[email protected]>
 M:	Paul Burton <[email protected]>
arch/m68k/Kbuild +1 -1
index 7762af9f6def..058ad41a728c 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-y				+= kernel/ mm/
+obj-y				+= kernel/ mm/ net/
 obj-$(CONFIG_Q40)		+= q40/
 obj-$(CONFIG_AMIGA)		+= amiga/
 obj-$(CONFIG_ATARI)		+= atari/
arch/m68k/Kconfig +1 -0
index 11835eb59d94..67ac01c9ab42 100644
@@ -8,6 +8,7 @@ config M68K
 	select ARCH_HAS_CPU_FINALIZE_INIT if MMU
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DMA_PREP_COHERENT if M68K_NONCOHERENT_DMA && !COLDFIRE
+	select HAVE_EBPF_JIT if (!COLDFIRE && !M68000 && !M68010)
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE if M68K_NONCOHERENT_DMA
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG if RMW_INSNS
 	select ARCH_MIGHT_HAVE_PC_PARPORT if ISA
arch/m68k/net/Makefile new file +2 -0
new file mode 100644
index 000000000000..ec5b14763316
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
arch/m68k/net/bpf_jit_comp.c new file +1543 -0

1,543 lines not on this page · raw message

Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] The BPF JIT compiler for m68k suffers from a 16-bit integer overflow when emitting memory…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] The BPF JIT compiler for m68k suffers from a 16-bit integer overflow when emitting memory instructions for 64-bit values (BPF_DW), resulting in an out-of-bounds read or write by approximately -64KB.
- [High] BPF-to-BPF function calls will crash the kernel because emit_call hardcodes extra_pass to false.
--

commit 700ecd8180ff5bd538d3f6e6f0d7655894abb49b
Author: Kuan-Wei Chiu <[email protected]>

m68k, bpf: Add initial BPF JIT compiler support

Add a BPF JIT compiler for the m68k architecture. Generating m68k machine code targeting m68020+ processors.

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0

[ ... ]

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2 -0
+static void emit_ldx(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{

[ ... ]

Quoted 1 line
arch/m68k/net/bpf_jit_comp.c new file +1 -0
+	switch (BPF_SIZE(insn->code)) {

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 741

Quoted 7 lines
arch/m68k/net/bpf_jit_comp.c new file +7 -0
+	case BPF_DW:
+		emit_16(ctx, 0x2028 | (d_hi << 9));		/* move.l d16(%a0), d_hi */
+		emit_16(ctx, insn->off);
+		emit_16(ctx, 0x2028 | (d_lo << 9));		/* move.l d16+4(%a0), d_lo */
+		emit_16(ctx, insn->off + 4);
+		break;
+	}

Could adding 4 to insn->off cause a 16-bit signed integer overflow here?

The BPF instruction offset (insn->off) is a signed 16-bit integer that can reach up to 32767 for large map accesses. If insn->off is between 32764 and 32767, insn->off + 4 evaluates to a value exceeding the 16-bit signed maximum (e.g., 32768, which becomes 0x8000).

Because m68k hardware sign-extends 16-bit displacements, would emitting 0x8000 result in a negative 32-bit displacement (-32768), leading to an out-of-bounds memory access roughly 64KB behind the intended safe offset?

This same pattern appears to be present in emit_stx() and emit_st() for the BPF_DW cases as well.

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1177

Quoted 9 lines
arch/m68k/net/bpf_jit_comp.c new file +9 -0
+static int emit_call(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{
+	const s8 arg_regs[] = { BPF_REG_5, BPF_REG_4, BPF_REG_3, BPF_REG_2, BPF_REG_1 };
+	const s8 *tmp1 = bpf2m68k[TMP_REG_1];
+	u64 func_addr;
+	bool fixed;
+	int i, err;
+
+	err = bpf_jit_get_func_addr(ctx->prog, insn, false, &func_addr, &fixed);

Will this cause the JIT to emit jumps to address 0x0 for BPF-to-BPF function calls?

Calling bpf_jit_get_func_addr() with extra_pass hardcoded to false causes it to return a NULL function address for BPF-to-BPF pseudo-calls. Since the real addresses are not resolved, does this result in the JIT unconditionally emitting jsr (0), which would crash the kernel when executed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 36 lines · On Wed, Apr 29, 2026 at 01:13:19PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] The BPF JIT compiler for m68k suffers from a 16-bit integer overflow when emitting memory instructions for 64-bit values (BPF_DW), resulting in an out-of-bounds read or write by approximately -64KB.
- [High] BPF-to-BPF function calls will crash the kernel because emit_call hardcodes extra_pass to false.
--

commit 700ecd8180ff5bd538d3f6e6f0d7655894abb49b
Author: Kuan-Wei Chiu <[email protected]>

m68k, bpf: Add initial BPF JIT compiler support

Add a BPF JIT compiler for the m68k architecture. Generating m68k machine code targeting m68020+ processors.

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0

[ ... ]

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2 -0
+static void emit_ldx(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{

[ ... ]

Quoted 1 line
arch/m68k/net/bpf_jit_comp.c new file +1 -0
+	switch (BPF_SIZE(insn->code)) {

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 741

Quoted 7 lines
arch/m68k/net/bpf_jit_comp.c new file +7 -0
+	case BPF_DW:
+		emit_16(ctx, 0x2028 | (d_hi << 9));		/* move.l d16(%a0), d_hi */
+		emit_16(ctx, insn->off);
+		emit_16(ctx, 0x2028 | (d_lo << 9));		/* move.l d16+4(%a0), d_lo */
+		emit_16(ctx, insn->off + 4);
+		break;
+	}

Could adding 4 to insn->off cause a 16-bit signed integer overflow here?

The BPF instruction offset (insn->off) is a signed 16-bit integer that can reach up to 32767 for large map accesses. If insn->off is between 32764 and 32767, insn->off + 4 evaluates to a value exceeding the 16-bit signed maximum (e.g., 32768, which becomes 0x8000).

Quoted 6 lines

Because m68k hardware sign-extends 16-bit displacements, would emitting 0x8000 result in a negative 32-bit displacement (-32768), leading to an out-of-bounds memory access roughly 64KB behind the intended safe offset?

This same pattern appears to be present in emit_stx() and emit_st() for the BPF_DW cases as well.

Ack.
Will fix this by emitting a lea 4(%a0), %a0 instruction before the second memory access.

Quoted 15 lines

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1177

Quoted 9 lines
arch/m68k/net/bpf_jit_comp.c new file +9 -0
+static int emit_call(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{
+	const s8 arg_regs[] = { BPF_REG_5, BPF_REG_4, BPF_REG_3, BPF_REG_2, BPF_REG_1 };
+	const s8 *tmp1 = bpf2m68k[TMP_REG_1];
+	u64 func_addr;
+	bool fixed;
+	int i, err;
+
+	err = bpf_jit_get_func_addr(ctx->prog, insn, false, &func_addr, &fixed);

Will this cause the JIT to emit jumps to address 0x0 for BPF-to-BPF function calls?

Quoted 4 lines

Calling bpf_jit_get_func_addr() with extra_pass hardcoded to false causes it to return a NULL function address for BPF-to-BPF pseudo-calls. Since the real addresses are not resolved, does this result in the JIT unconditionally emitting jsr (0), which would crash the kernel when executed?

Ack. Will fix this by passing ctx->target != NULL as the argument to bpf_jit_get_func_addr().

Regards,
Kuan-Wei

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Kuan-Wei Chiu
This isn't a bug, but the surrounding BPF JIT entries all use lowercase 'for': 'BPF JIT for ARM', 'BPF JIT for ARM64', 'BPF JIT for LOONGARCH', 'BPF JIT for MIPS', etc. For consistency, this should…

MAINTAINERS L: [email protected] · line 4702

Quoted 9 lines
MAINTAINERS +1 -0
index 13f49378b157..9d6e73c1caaf 100644
@@ -4699,6 +4699,12 @@ L:	[email protected]
 S:	Maintained
 F:	arch/loongarch/net/
 
+BPF JIT FOR M68K
           ^^^

This isn't a bug, but the surrounding BPF JIT entries all use lowercase 'for': 'BPF JIT for ARM', 'BPF JIT for ARM64', 'BPF JIT for LOONGARCH', 'BPF JIT for MIPS', etc. For consistency, this should be 'BPF JIT for M68K'.

Quoted 8 lines
MAINTAINERS +5 -0
+M:	Kuan-Wei Chiu <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	arch/m68k/net/
+
 BPF JIT for MIPS (32-BIT AND 64-BIT)
 M:	Johan Almbladh <[email protected]>
 M:	Paul Burton <[email protected]>

[ ... ]

arch/m68k/Kconfig config M68K · line 11

Quoted 9 lines
arch/m68k/Kconfig +1 -0
index 11835eb59d94..67ac01c9ab42 100644
@@ -8,6 +8,7 @@ config M68K
 	select ARCH_HAS_CPU_FINALIZE_INIT if MMU
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DMA_PREP_COHERENT if M68K_NONCOHERENT_DMA && !COLDFIRE
+	select HAVE_EBPF_JIT if (!COLDFIRE && !M68000 && !M68010)
                                                          ^^^^^^^

Does this reference a valid Kconfig symbol? The symbol M68010 doesn't appear to be defined anywhere in the kernel tree. A search across arch/m68k/ and the wider tree shows M68010 is referenced only in this new line. The m68k CPU Kconfig (arch/m68k/Kconfig.cpu) defines COLDFIRE, M68000, M68020, M68030, M68040, M68060, SUN3, M68KCLASSIC, etc., but no M68010.

Because undefined Kconfig symbols evaluate to 'n', the condition !M68010 is always true. This means the gating for 68010 hardware that the commit message describes is never actually enforced (dead sub-expression).

The commit message states the JIT targets 'm68020+ processors... excludes 68000/68010 and coldfire', so the intent is clearly to exclude 68010 - but that exclusion is not functional as written.

Quoted 3 lines
arch/m68k/Kconfig +0 -0
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE if M68K_NONCOHERENT_DMA
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG if RMW_INSNS
 	select ARCH_MIGHT_HAVE_PC_PARPORT if ISA

[ ... ]

Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0
new file mode 100644
index 000000000000..cfeffd03c434

[ ... ]

Quoted 1 line
arch/m68k/net/bpf_jit_comp.c new file +0 -0
@@ -0,0 +1,1543 @@

[ ... ]

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2 -0
+static void emit_alu64_x(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 412

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3 -0
+	case BPF_XOR:
+		emit_16(ctx, 0xb180 | (s_lo << 9) | d_lo);		/* eor.l d_lo, s_lo */
+		emit_16(ctx, 0xb180 | (s_hi << 9) | d_hi);		/* eor.l d_hi, s_hi */
                                                                       ^^^^^^^^^^^

The comments describe the operand order in reverse. The m68k 'eor.l Dn, <ea>' opcode 0xB180 has Dn (bits 11-9) encode the source register and EA (bits 2-0) encode the destination (EA = EA XOR Dn). The emitted instruction is:

0xb180 | (s_lo << 9) | d_lo /* d_lo := d_lo ^ s_lo */

which is functionally correct. However, the comment reads 'eor.l d_lo, s_lo', which in m68k assembler convention (source first) describes the wrong operation: s_lo := d_lo ^ s_lo.

This is inconsistent with the correctly-worded sibling comment in emit_alu32_x:

emit_16(ctx, 0xb180 | (s_reg << 9) | d_reg); /* eor.l src, dst */

The emitted machine code is correct; only the comments are reversed. This also affects the BPF_XOR case in emit_alu64_k (four lines total).

Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +6 -0
+		break;
+	}
+
+	bpf_put_reg32(dst[1], d_lo, ctx);
+	bpf_put_reg32(dst[0], d_hi, ctx);
+}

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1213

Quoted 11 lines
arch/m68k/net/bpf_jit_comp.c new file +11 -0
+static void build_prologue(struct jit_ctx *ctx)
+{
+	int bpf_stack = 512;
+	int total_stack = SCRATCH_SIZE + bpf_stack;
+
+	emit_16(ctx, 0x7021);					/* moveq #33, %d0 */
+
+	emit_16(ctx, 0x4e56);					/* link %a6, #-total_stack */
+	emit_16(ctx, -total_stack);
+
+	emit_16(ctx, 0x48e7);					/* movem.l d2-d5, -(%sp) */
                                                                  ^^^^^^

Does this comment accurately describe the register mask 0x3f3c? The mask 0x3f3c in predecrement mode (bits 2-5 and 8-13 set) encodes the register set {D2, D3, D4, D5, D6, D7, A2, A3, A4, A5} - that's 10 registers, not just d2-d5.

Predecrement register mask encoding uses bit 0=A7, bit 1=A6, ..., bit 7=A0, bit 8=D7, bit 9=D6, ..., bit 15=D0. A true 'movem.l d2-d5, -(%sp)' would use mask 0x3c00.

The save matches the corresponding epilogue restore (mask 0x3cfc, which in postincrement mode covers the same 10 registers), so the code is functionally correct and the stack is balanced. However, D6, D7, A2-A5 are never modified by the JIT and do not need to be saved, causing 24 bytes of wasted stack space and 6 unnecessary register saves per JIT'd function call.

Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4 -0
+	emit_16(ctx, 0x3f3c);
+
+	emit_16(ctx, 0x2d40);					/* move.l %d0, off(%fp) */
+	emit_16(ctx, (u16)STACK_OFFSET(BPF_TC_LO));

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1237

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3 -0
+static void build_epilogue(struct jit_ctx *ctx)
+{
+	emit_16(ctx, 0x4cdf);					/* movem.l (%sp)+, d2-d5 */
                                                                  ^^^^^^

Similar to the prologue comment issue, the mask 0x3cfc in postincrement mode (bits 2-7 and 10-13 set) encodes restoring {D2, D3, D4, D5, D6, D7, A2, A3, A4, A5}. A true 'movem.l (%sp)+, d2-d5' would use mask 0x003c.

Postincrement mask encoding uses bit 0=D0, bit 1=D1, ..., bit 7=D7, bit 8=A0, ..., bit 15=A7. This matches the prologue's save, so the stack is balanced and values are correctly restored, but the comment is inaccurate.

arch/m68k/net/bpf_jit_comp.c line 1242

Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5 -0
+	emit_16(ctx, 0x3cfc);
+
+	emit_16(ctx, 0x4e5e);					/* unlk %fp */
+	emit_16(ctx, 0x4e75);					/* rts */
+}

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25110417551

#4 lore raw
Quoted 8 lines · On Wed, Apr 29, 2026 at 01:39:10PM +0000, [email protected] wrote:
Quoted 8 lines
MAINTAINERS +0 -0
index 13f49378b157..9d6e73c1caaf 100644
@@ -4699,6 +4699,12 @@ L:	[email protected]
 S:	Maintained
 F:	arch/loongarch/net/
 
Quoted 7 lines

MAINTAINERS L: [email protected] · line 4704

Quoted 1 line
MAINTAINERS +1 -0
+BPF JIT FOR M68K
           ^^^

This isn't a bug, but the surrounding BPF JIT entries all use lowercase 'for': 'BPF JIT for ARM', 'BPF JIT for ARM64', 'BPF JIT for LOONGARCH', 'BPF JIT for MIPS', etc. For consistency, this should be 'BPF JIT for M68K'.

I doubt anyone really cares about the casing here, but I will fix it in v2 anyway.

Quoted 30 lines
Quoted 9 lines
MAINTAINERS +5 -0
+M:	Kuan-Wei Chiu <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	arch/m68k/net/
+
 BPF JIT for MIPS (32-BIT AND 64-BIT)
 M:	Johan Almbladh <[email protected]>
 M:	Paul Burton <[email protected]>

[ ... ]

arch/m68k/Kconfig config M68K · line 11

Quoted 9 lines
arch/m68k/Kconfig +1 -0
index 11835eb59d94..67ac01c9ab42 100644
@@ -8,6 +8,7 @@ config M68K
 	select ARCH_HAS_CPU_FINALIZE_INIT if MMU
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DMA_PREP_COHERENT if M68K_NONCOHERENT_DMA && !COLDFIRE
+	select HAVE_EBPF_JIT if (!COLDFIRE && !M68000 && !M68010)
                                                          ^^^^^^^

Does this reference a valid Kconfig symbol? The symbol M68010 doesn't appear to be defined anywhere in the kernel tree. A search across arch/m68k/ and the wider tree shows M68010 is referenced only in this new line. The m68k CPU Kconfig (arch/m68k/Kconfig.cpu) defines COLDFIRE, M68000, M68020, M68030, M68040, M68060, SUN3, M68KCLASSIC, etc., but no M68010.

Quoted 8 lines

Because undefined Kconfig symbols evaluate to 'n', the condition !M68010 is always true. This means the gating for 68010 hardware that the commit message describes is never actually enforced (dead sub-expression).

The commit message states the JIT targets 'm68020+ processors... excludes 68000/68010 and coldfire', so the intent is clearly to exclude 68010 - but that exclusion is not functional as written.

Ah, I actually had a local fixup for this but forgot to squash it before sending.

Out of curiosity, is there any checker tool that warns about referencing non-existent symbols?

Quoted 40 lines
Quoted 4 lines
arch/m68k/Kconfig +0 -0
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE if M68K_NONCOHERENT_DMA
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG if RMW_INSNS
 	select ARCH_MIGHT_HAVE_PC_PARPORT if ISA

[ ... ]

Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0
new file mode 100644
index 000000000000..cfeffd03c434

[ ... ]

Quoted 1 line
arch/m68k/net/bpf_jit_comp.c new file +0 -0
@@ -0,0 +1,1543 @@

[ ... ]

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2 -0
+static void emit_alu64_x(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 412

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3 -0
+	case BPF_XOR:
+		emit_16(ctx, 0xb180 | (s_lo << 9) | d_lo);		/* eor.l d_lo, s_lo */
+		emit_16(ctx, 0xb180 | (s_hi << 9) | d_hi);		/* eor.l d_hi, s_hi */
                                                                       ^^^^^^^^^^^

The comments describe the operand order in reverse. The m68k 'eor.l Dn, <ea>' opcode 0xB180 has Dn (bits 11-9) encode the source register and EA (bits 2-0) encode the destination (EA = EA XOR Dn). The emitted instruction is:

0xb180 | (s_lo << 9) | d_lo /* d_lo := d_lo ^ s_lo */

which is functionally correct. However, the comment reads 'eor.l d_lo, s_lo', which in m68k assembler convention (source first) describes the wrong operation: s_lo := d_lo ^ s_lo.

Quoted 7 lines

This is inconsistent with the correctly-worded sibling comment in emit_alu32_x:

emit_16(ctx, 0xb180 | (s_reg << 9) | d_reg); /* eor.l src, dst */

The emitted machine code is correct; only the comments are reversed. This also affects the BPF_XOR case in emit_alu64_k (four lines total).

Ack. Will fix the comment in v2.

Quoted 32 lines
Quoted 7 lines
arch/m68k/net/bpf_jit_comp.c new file +6 -0
+		break;
+	}
+
+	bpf_put_reg32(dst[1], d_lo, ctx);
+	bpf_put_reg32(dst[0], d_hi, ctx);
+}

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1213

Quoted 11 lines
arch/m68k/net/bpf_jit_comp.c new file +11 -0
+static void build_prologue(struct jit_ctx *ctx)
+{
+	int bpf_stack = 512;
+	int total_stack = SCRATCH_SIZE + bpf_stack;
+
+	emit_16(ctx, 0x7021);					/* moveq #33, %d0 */
+
+	emit_16(ctx, 0x4e56);					/* link %a6, #-total_stack */
+	emit_16(ctx, -total_stack);
+
+	emit_16(ctx, 0x48e7);					/* movem.l d2-d5, -(%sp) */
                                                                  ^^^^^^

Does this comment accurately describe the register mask 0x3f3c? The mask 0x3f3c in predecrement mode (bits 2-5 and 8-13 set) encodes the register set {D2, D3, D4, D5, D6, D7, A2, A3, A4, A5} - that's 10 registers, not just d2-d5.

Predecrement register mask encoding uses bit 0=A7, bit 1=A6, ..., bit 7=A0, bit 8=D7, bit 9=D6, ..., bit 15=D0. A true 'movem.l d2-d5, -(%sp)' would use mask 0x3c00.

Quoted 6 lines

The save matches the corresponding epilogue restore (mask 0x3cfc, which in postincrement mode covers the same 10 registers), so the code is functionally correct and the stack is balanced. However, D6, D7, A2-A5 are never modified by the JIT and do not need to be saved, causing 24 bytes of wasted stack space and 6 unnecessary register saves per JIT'd function call.

Ack. Will change the mask to 0x3c00.

Regards,
Kuan-Wei

Quoted 35 lines
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +4 -0
+	emit_16(ctx, 0x3f3c);
+
+	emit_16(ctx, 0x2d40);					/* move.l %d0, off(%fp) */
+	emit_16(ctx, (u16)STACK_OFFSET(BPF_TC_LO));

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1237

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3 -0
+static void build_epilogue(struct jit_ctx *ctx)
+{
+	emit_16(ctx, 0x4cdf);					/* movem.l (%sp)+, d2-d5 */
                                                                  ^^^^^^

Similar to the prologue comment issue, the mask 0x3cfc in postincrement mode (bits 2-7 and 10-13 set) encodes restoring {D2, D3, D4, D5, D6, D7, A2, A3, A4, A5}. A true 'movem.l (%sp)+, d2-d5' would use mask 0x003c.

Postincrement mask encoding uses bit 0=D0, bit 1=D1, ..., bit 7=D7, bit 8=A0, ..., bit 15=A7. This matches the prologue's save, so the stack is balanced and values are correctly restored, but the comment is inaccurate.

arch/m68k/net/bpf_jit_comp.c line 1242

Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5 -0
+	emit_16(ctx, 0x3cfc);
+
+	emit_16(ctx, 0x4e5e);					/* unlk %fp */
+	emit_16(ctx, 0x4e75);					/* rts */
+}

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25110417551

#5 lore raw
In reply to Kuan-Wei Chiu

Hi Kuan-Wei,

Quoted 7 lines · Kuan-Wei Chiu

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors. It currently excludes 68000/68010 and coldfire processors, as it relies on 32 bit branch displacements (b<cc>.l) to handle large bpf programs.

Quoted 5 lines

Tested with the test_bpf.ko:
test_bpf: Summary: 1053 PASSED, 0 FAILED, [1041/1041 JIT'ed]
test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]

Cool! Which CPUs did you test on? I guess 040 that is in QEMU? I'll check on real 030, 040 and 060 machines a bit later. Since you are doing code generation for m68k now maybe you could help me when I resend my patches for runtime consts support? [0]

Thanks,

Daniel

0 - https://lore.kernel.org/linux-m68k/[email protected]/

#6 lore raw

Hi,

Quoted 7 lines · Daniel Palmer
Quoted 6 lines · Kuan-Wei Chiu

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors. It currently excludes 68000/68010 and coldfire processors, as it relies on 32 bit branch displacements (b<cc>.l) to handle large bpf programs.

Quoted 6 lines
Quoted 3 lines

Tested with the test_bpf.ko:
test_bpf: Summary: 1053 PASSED, 0 FAILED, [1041/1041 JIT'ed]
test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]

Cool! Which CPUs did you test on? I guess 040 that is in QEMU? I'll check on real 030, 040 and 060 machines a bit later.

Before real 030 HW testing, I would recommend checking first that things work well with Hatari emulator. Unlike Qemu, it emulates also CPU caches, so it can find issues related to that, and it provides much easier debugging and (semi-cycle-accurate) profiling support.

Instructions for doing that are in the Hatari repo (GitHub mirror):
https://github.com/hatari/hatari/blob/main/doc/m68k-linux.txt

New enough Hatari release is in Fedora, Ubuntu 26.04, Debian testing...

(If you have any questions, you can mail me directly.)

- Eero

PS. Another alternative for semi-cycle-accurate 030/cache/MMU emulation would be WinUAE Amiga emulator (which is upstream for the Hatari CPU core emulation). I don't have any instructions for that though.

PPS. Hatari emulator has been used e.g. to profile & optimize ScummVM and its game engines enough that upstream ScummVM releases include Atari version which works to some extent even on original 030 machines (and naturally much better on accelerated machines):

	https://www.atari-forum.com/viewtopic.php?t=42429
#7 lore raw
In reply to Daniel Palmer

Hi Daniel,

Quoted 10 lines · Daniel Palmer

Hi Kuan-Wei,

Quoted 7 lines · Kuan-Wei Chiu

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors. It currently excludes 68000/68010 and coldfire processors, as it relies on 32 bit branch displacements (b<cc>.l) to handle large bpf programs.

Quoted 7 lines
Quoted 5 lines

Tested with the test_bpf.ko:
test_bpf: Summary: 1053 PASSED, 0 FAILED, [1041/1041 JIT'ed]
test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]

Cool! Which CPUs did you test on? I guess 040 that is in QEMU?

Yes.

Quoted 1 line

I'll check on real 030, 040 and 060 machines a bit later.

Thanks!
I really appreciate your help with both u-boot and the kernel.

Quoted 2 lines

Since you are doing code generation for m68k now maybe you could help me when I resend my patches for runtime consts support? [0]

Sure, I'll take a look at the patch.
Feel free to Cc me on any future patches if you'd like me to help with reviews.

Regards,
Kuan-Wei

Quoted 6 lines

Thanks,

Daniel

0 - https://lore.kernel.org/linux-m68k/[email protected]/

#8 lore raw
In reply to Kuan-Wei Chiu

Hi Kuan-Wei,

Quoted 5 lines · Kuan-Wei Chiu

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors. It currently excludes 68000/68010 and coldfire processors, as it relies on 32 bit branch displacements (b<cc>.l) to handle large bpf programs.

First of all, this is extremely cool! Thanks a lot for the work!

Secondly, since you seem to be well-versed in compiler-related stuff on m68k, may I ask you whether you would be interested in helping to fix a stopper bug in the LLVM M68k backend?

I have outlined the problem in [1], maybe you could help getting this fixed as this would fully enable LLVM on M68k which means we could finally use Rust on the architecture and maybe also build the Linux kernel on m68k with clang.

Thanks,
Adrian

Quoted 1 line

[1] https://discourse.llvm.org/t/rfh-m68k-implementing-support-for-32-bit-relocations/90660

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer
`. `'   Physicist
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#9 lore raw
Quoted 10 lines · John Paul Adrian Glaubitz

Hi Kuan-Wei,

Quoted 5 lines · Kuan-Wei Chiu

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors. It currently excludes 68000/68010 and coldfire processors, as it relies on 32 bit branch displacements (b<cc>.l) to handle large bpf programs.

First of all, this is extremely cool! Thanks a lot for the work!

Thanks. :)

Quoted 10 lines

Secondly, since you seem to be well-versed in compiler-related stuff on m68k, may I ask you whether you would be interested in helping to fix a stopper bug in the LLVM M68k backend?

I have outlined the problem in [1], maybe you could help getting this fixed as this would fully enable LLVM on M68k which means we could finally use Rust on the architecture and maybe also build the Linux kernel on m68k with clang.

I'm not very familiar with the llvm codebase, but I'll definitely take a look at it and see how much I can help out. :)

Regards,
Kuan-Wei