arm64: patching: implement text_poke API
authorPuranjay Mohan <puranjay12@gmail.com>
Wed, 28 Feb 2024 14:18:23 +0000 (14:18 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 28 Feb 2024 21:44:47 +0000 (13:44 -0800)
The text_poke API is used to implement functions like memcpy() and
memset() for instruction memory (RO+X). The implementation is similar to
the x86 version.

This will be used by the BPF JIT to write and modify BPF programs. There
could be more users of this in the future.

Signed-off-by: Puranjay Mohan <puranjay12@gmail.com>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Link: https://lore.kernel.org/r/20240228141824.119877-2-puranjay12@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
arch/arm64/include/asm/patching.h
arch/arm64/kernel/patching.c

index 68908b82b168f852557e39453e311a8d6ce92f62..587bdb91ab7a6432ac162ff535bb6daf0462a47f 100644 (file)
@@ -8,6 +8,8 @@ int aarch64_insn_read(void *addr, u32 *insnp);
 int aarch64_insn_write(void *addr, u32 insn);
 
 int aarch64_insn_write_literal_u64(void *addr, u64 val);
+void *aarch64_insn_set(void *dst, u32 insn, size_t len);
+void *aarch64_insn_copy(void *dst, void *src, size_t len);
 
 int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
 int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
index b4835f6d594bc5554f3b99228748bff48b7771b1..25553493036845e342bcc483c320738382a911a8 100644 (file)
@@ -105,6 +105,81 @@ noinstr int aarch64_insn_write_literal_u64(void *addr, u64 val)
        return ret;
 }
 
+typedef void text_poke_f(void *dst, void *src, size_t patched, size_t len);
+
+static void *__text_poke(text_poke_f func, void *addr, void *src, size_t len)
+{
+       unsigned long flags;
+       size_t patched = 0;
+       size_t size;
+       void *waddr;
+       void *ptr;
+
+       raw_spin_lock_irqsave(&patch_lock, flags);
+
+       while (patched < len) {
+               ptr = addr + patched;
+               size = min_t(size_t, PAGE_SIZE - offset_in_page(ptr),
+                            len - patched);
+
+               waddr = patch_map(ptr, FIX_TEXT_POKE0);
+               func(waddr, src, patched, size);
+               patch_unmap(FIX_TEXT_POKE0);
+
+               patched += size;
+       }
+       raw_spin_unlock_irqrestore(&patch_lock, flags);
+
+       flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
+
+       return addr;
+}
+
+static void text_poke_memcpy(void *dst, void *src, size_t patched, size_t len)
+{
+       copy_to_kernel_nofault(dst, src + patched, len);
+}
+
+static void text_poke_memset(void *dst, void *src, size_t patched, size_t len)
+{
+       u32 c = *(u32 *)src;
+
+       memset32(dst, c, len / 4);
+}
+
+/**
+ * aarch64_insn_copy - Copy instructions into (an unused part of) RX memory
+ * @dst: address to modify
+ * @src: source of the copy
+ * @len: length to copy
+ *
+ * Useful for JITs to dump new code blocks into unused regions of RX memory.
+ */
+noinstr void *aarch64_insn_copy(void *dst, void *src, size_t len)
+{
+       /* A64 instructions must be word aligned */
+       if ((uintptr_t)dst & 0x3)
+               return NULL;
+
+       return __text_poke(text_poke_memcpy, dst, src, len);
+}
+
+/**
+ * aarch64_insn_set - memset for RX memory regions.
+ * @dst: address to modify
+ * @insn: value to set
+ * @len: length of memory region.
+ *
+ * Useful for JITs to fill regions of RX memory with illegal instructions.
+ */
+noinstr void *aarch64_insn_set(void *dst, u32 insn, size_t len)
+{
+       if ((uintptr_t)dst & 0x3)
+               return NULL;
+
+       return __text_poke(text_poke_memset, dst, &insn, len);
+}
+
 int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn)
 {
        u32 *tp = addr;