updated: 2025-10-20 Mon 00:00

Run assembly code in kernel


Run aarch64 assembly code in kernel

We first need to write a simple kernel module and call a function inside assembly code.

Example:

module.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h> 

extern unsigned int asm_init(void);
static int __init hello_init(void)
{
  printk(KERN_INFO "Hello, aarch64 %u", asm_init());
  return 0;
}

static void __exit hello_exit(void)
{
  printk(KERN_INFO "Goodbye, aarch64!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Assembly module");
MODULE_AUTHOR("YOU");

test.s

        .text
        .globl asm_init

asm_init:
        mov x0, #1
        ret

Makefile

obj-m += demo_asm.o
demo_asm-objs := module.o test.o

test.o: test.s
        as -o $@ $^ && echo "" > .test.o.cmd

all: test.o
        $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Insert and remove kernel module and check for dmesg or console output.

# insmod demo_asm.ko
# rmmod demo_asm
# dmesg | tail

useful links

https://stackoverflow.com/questions/13668403/writing-x86-64-linux-kernel-module-in-assembler