[PATCH] lib/xz: Add ARM64 BCJ filter

Lasse Collin posted 1 patch 2 years, 1 month ago
lib/xz/Kconfig      |    5 +++++
lib/xz/xz_dec_bcj.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++++-
lib/xz/xz_private.h |    7 +++++--
3 files changed, 59 insertions(+), 3 deletions(-)
[PATCH] lib/xz: Add ARM64 BCJ filter
Posted by Lasse Collin 2 years, 1 month ago
From: Lasse Collin <lasse.collin@tukaani.org>

ARM64 kernel decompression is done by bootloaders but
the filter can still be useful, for example, for Squashfs.

A duplicated check for XZ_DEC_ARM in xz_private.h was omitted too.

Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
---

 lib/xz/Kconfig      |    5 +++++
 lib/xz/xz_dec_bcj.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/xz/xz_private.h |    7 +++++--
 3 files changed, 59 insertions(+), 3 deletions(-)

diff -rup linux-6.6.orig/lib/xz/Kconfig linux-6.6/lib/xz/Kconfig
--- linux-6.6.orig/lib/xz/Kconfig	2023-10-30 04:31:08.000000000 +0200
+++ linux-6.6/lib/xz/Kconfig	2023-11-08 16:27:17.661462876 +0200
@@ -34,6 +34,11 @@ config XZ_DEC_ARMTHUMB
 	default y
 	select XZ_DEC_BCJ
 
+config XZ_DEC_ARM64
+	bool "ARM64 BCJ filter decoder" if EXPERT
+	default y
+	select XZ_DEC_BCJ
+
 config XZ_DEC_SPARC
 	bool "SPARC BCJ filter decoder" if EXPERT
 	default y
diff -rup linux-6.6.orig/lib/xz/xz_dec_bcj.c linux-6.6/lib/xz/xz_dec_bcj.c
--- linux-6.6.orig/lib/xz/xz_dec_bcj.c	2023-10-30 04:31:08.000000000 +0200
+++ linux-6.6/lib/xz/xz_dec_bcj.c	2023-11-08 16:29:37.949475877 +0200
@@ -24,7 +24,8 @@ struct xz_dec_bcj {
 		BCJ_IA64 = 6,       /* Big or little endian */
 		BCJ_ARM = 7,        /* Little endian only */
 		BCJ_ARMTHUMB = 8,   /* Little endian only */
-		BCJ_SPARC = 9       /* Big or little endian */
+		BCJ_SPARC = 9,      /* Big or little endian */
+		BCJ_ARM64 = 10      /* AArch64 */
 	} type;
 
 	/*
@@ -334,6 +335,45 @@ static size_t bcj_sparc(struct xz_dec_bc
 }
 #endif
 
+#ifdef XZ_DEC_ARM64
+static size_t bcj_arm64(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
+{
+	size_t i;
+	uint32_t instr;
+	uint32_t addr;
+
+	for (i = 0; i + 4 <= size; i += 4) {
+		instr = get_unaligned_le32(buf + i);
+
+		if ((instr >> 26) == 0x25) {
+			/* BL instruction */
+			addr = instr - ((s->pos + (uint32_t)i) >> 2);
+			instr = 0x94000000 | (addr & 0x03FFFFFF);
+			put_unaligned_le32(instr, buf + i);
+
+		} else if ((instr & 0x9F000000) == 0x90000000) {
+			/* ADRP instruction */
+			addr = ((instr >> 29) & 3) | ((instr >> 3) & 0x1FFFFC);
+
+			/* Only convert values in the range +/-512 MiB. */
+			if ((addr + 0x020000) & 0x1C0000)
+				continue;
+
+			addr -= (s->pos + (uint32_t)i) >> 12;
+
+			instr &= 0x9000001F;
+			instr |= (addr & 3) << 29;
+			instr |= (addr & 0x03FFFC) << 3;
+			instr |= (0U - (addr & 0x020000)) & 0xE00000;
+
+			put_unaligned_le32(instr, buf + i);
+		}
+	}
+
+	return i;
+}
+#endif
+
 /*
  * Apply the selected BCJ filter. Update *pos and s->pos to match the amount
  * of data that got filtered.
@@ -381,6 +421,11 @@ static void bcj_apply(struct xz_dec_bcj
 		filtered = bcj_sparc(s, buf, size);
 		break;
 #endif
+#ifdef XZ_DEC_ARM64
+	case BCJ_ARM64:
+		filtered = bcj_arm64(s, buf, size);
+		break;
+#endif
 	default:
 		/* Never reached but silence compiler warnings. */
 		filtered = 0;
@@ -554,6 +599,9 @@ XZ_EXTERN enum xz_ret xz_dec_bcj_reset(s
 #ifdef XZ_DEC_SPARC
 	case BCJ_SPARC:
 #endif
+#ifdef XZ_DEC_ARM64
+	case BCJ_ARM64:
+#endif
 		break;
 
 	default:
diff -rup linux-6.6.orig/lib/xz/xz_private.h linux-6.6/lib/xz/xz_private.h
--- linux-6.6.orig/lib/xz/xz_private.h	2023-10-30 04:31:08.000000000 +0200
+++ linux-6.6/lib/xz/xz_private.h	2023-11-08 16:31:17.155717209 +0200
@@ -37,6 +37,9 @@
 #		ifdef CONFIG_XZ_DEC_SPARC
 #			define XZ_DEC_SPARC
 #		endif
+#		ifdef CONFIG_XZ_DEC_ARM64
+#			define XZ_DEC_ARM64
+#		endif
 #		ifdef CONFIG_XZ_DEC_MICROLZMA
 #			define XZ_DEC_MICROLZMA
 #		endif
@@ -98,9 +101,9 @@
  */
 #ifndef XZ_DEC_BCJ
 #	if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
-			|| defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
+			|| defined(XZ_DEC_IA64) \
 			|| defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
-			|| defined(XZ_DEC_SPARC)
+			|| defined(XZ_DEC_SPARC) || defined(XZ_DEC_ARM64)
 #		define XZ_DEC_BCJ
 #	endif
 #endif
Re: [PATCH] lib/xz: Add ARM64 BCJ filter
Posted by Andrew Morton 2 years, 1 month ago
On Wed, 8 Nov 2023 19:44:48 +0200 Lasse Collin <lasse.collin@tukaani.org> wrote:

> ARM64 kernel decompression is done by bootloaders but
> the filter can still be useful, for example, for Squashfs.
> 
> A duplicated check for XZ_DEC_ARM in xz_private.h was omitted too.

There's nothing here that uses the new functionality?

Is a squashfs patch expected?
Re: [PATCH] lib/xz: Add ARM64 BCJ filter
Posted by Lasse Collin 2 years, 1 month ago
On 2023-11-08 Andrew Morton wrote:
> On Wed, 8 Nov 2023 19:44:48 +0200 Lasse Collin
> <lasse.collin@tukaani.org> wrote:
> 
> > ARM64 kernel decompression is done by bootloaders but
> > the filter can still be useful, for example, for Squashfs.
> > 
> > A duplicated check for XZ_DEC_ARM in xz_private.h was omitted too.  
> 
> There's nothing here that uses the new functionality?
> 
> Is a squashfs patch expected?

I need to send a patch to mksquashfs. In the kernel the XZ filters are
handled by the XZ code, so Squashfs in the kernel needs no changes.

Initramfs can use the filters if initramfs contains a significant amount
of executable code. There are practical reasons why it's not commonly
done on other archs though. The main one is that kernel modules
shouldn't be filtered (worse compression). There are plans to improve
the compression tools to make this easier (it would help in other
contexts too) but it will take time.

-- 
Lasse Collin