Skip to contentSkip to conversation

m68k, bpf: Add initial BPF JIT compiler support

PATCH Superseded Kuan-Wei Chiu 4 participants 4 messages May 3 – May 5

Subsystems (2)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

Changes in v2:
- Fix potential overflow in BPF_DW by using 'lea 4(%a0), %a0'.
- Pass the extra_pass flag to bpf_jit_get_func_addr().
- Fix movem.l register masks in prologue and epilogue.
- Remove the non-existent M68010 Kconfig symbol.
- Fix casing in MAINTAINERS.
- Fix operand order in BPF_XOR comments.

 MAINTAINERS                  |    6 +
 arch/m68k/Kbuild             |    2 +-
 arch/m68k/Kconfig            |    1 +
 arch/m68k/net/Makefile       |    2 +
 arch/m68k/net/bpf_jit_comp.c | 1550 ++++++++++++++++++++++++++++++++++
 5 files changed, 1560 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..530551834c16 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..da43d6660c5c 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)
 	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 +1550 -0

1,550 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 1 potential issue(s) to consider: - [High] BPF-to-BPF subprogram calls (BPF_PSEUDO_CALL) are broken due to calling convention mismatches…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] BPF-to-BPF subprogram calls (BPF_PSEUDO_CALL) are broken due to calling convention mismatches (uninitialized arguments and swapped return value). --

commit f2d67ed1dad4f59dffd02142f7b92c353536f557
Author: Kuan-Wei Chiu <[email protected]>
Subject: m68k, bpf: Add initial BPF JIT compiler support

This commit introduces a BPF JIT compiler for the m68k architecture, generating m68020+ machine code. It currently excludes 68000/68010 and coldfire processors because it relies on 32-bit branch displacements.

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

[ ... ]

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

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1208

Quoted 9 lines
arch/m68k/net/bpf_jit_comp.c new file +9 -0
+	emit_16(ctx, 0x4e90);					/* jsr (%a0) */
+
+	emit_16(ctx, 0x4fef);					/* lea 40(%sp), %sp */
+	emit_16(ctx, 40);
+
+	emit_16(ctx, 0xc340);					/* exg %d0, %d1 */
+
+	return 0;
+}

For BPF-to-BPF subprogram calls (BPF_PSEUDO_CALL), does this exg %d0, %d1 swap the halves of the return value incorrectly?

The caller seems to assume all callees return values using the C ABI (high 32 bits in %d0, low in %d1). However, BPF subprograms place their 64-bit return value in BPF_REG_0 (mapped to %d1 and %d0), so swapping them here would corrupt the returned value.

[ ... ]

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

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1231

Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +6 -0
+	emit_16(ctx, 0x202e);					/* move.l 8(%fp), %d0 */
+	emit_16(ctx, 8);
+
+	bpf_put_reg32(bpf2m68k[BPF_REG_1][1], M68K_D0, ctx);
+	emit_16(ctx, 0x7000 | (M68K_D0 << 9));			/* moveq #0, %d0 */
+	bpf_put_reg32(bpf2m68k[BPF_REG_1][0], M68K_D0, ctx);

[ ... ]

In the case of BPF-to-BPF subprogram calls, emit_call() pushes arguments R1-R5 onto the stack before the call, but this prologue appears to unconditionally assume the C ABI.

It reads a single 32-bit context pointer from 8(%fp) into BPF_R1_LO and zeroes BPF_R1_HI, while completely ignoring R2-R5 passed by the caller.

Could this lead to subprograms operating on uninitialized stack garbage for R2-R5, and having their R1 incorrectly initialized?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Kuan-Wei Chiu
This isn't a bug, but the operand order in these comments is reversed. The m68k encoding 0xb180 | (s_lo << 9) | d_lo represents 'eor.l s_lo, d_lo', not 'eor.l d_lo, s_lo'. Compare with the identical…
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0
new file mode 100644
index 000000000000..98cef53c6f39

[ ... ]

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2 -0
+static void emit_alu64_k(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_OP(insn->code)) {

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 470

Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4 -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 */
+		break;

This isn't a bug, but the operand order in these comments is reversed. The m68k encoding 0xb180 | (s_lo << 9) | d_lo represents 'eor.l s_lo, d_lo', not 'eor.l d_lo, s_lo'. Compare with the identical bytecode in emit_alu64_x's BPF_XOR case which correctly comments it as 'eor.l s_lo, d_lo', and emit_alu32_x's BPF_XOR which uses 'eor.l src, dst'.

---
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/25287107687

Tested-by on v2 1/1
#3 lore raw
In reply to Kuan-Wei Chiu

Hi Kuan-Wei,

Quoted 10 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.

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]

Results match on a real 68040 (Apple Macintosh LC475, 1993 vintage):

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]
test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED

# uname -a
Linux buildroot 7.1.0-rc1-multi-00465-g1f6c5a18ddbd #26 Tue May  5
12:25:51 JST 2026 m68k GNU/Linux
# cat /proc/cpuinfo
CPU:            68040
MMU:            68040
FPU:            68040
Clocking:       32.3MHz
BogoMips:       21.55
Calibration:    107776 loops

Also the time the test took seems to be considerably shorter with JIT.

So: