0

I try to link some assembler into my kernel module. Later I need to read/write some registers. But already the make fails.

Here is the Makefile, I also have done a Makefile only for the asm which worked.

obj-m := test.o
test-objs := main.o module.o
main.o: main.s
        as -o $@ $^ 

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

main.s

.text
.global init

init:
        mov r0, #1
        ldr r1, =msg
        ldr r2, =len
        mov r7, #4
        swi 0

        mov r7, #1
        swi 0

.data
msg:
        .asciz  "hello world\n"
        len = .-msg

module.c

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

extern int init(void);
extern int cleanup(void);

static int __init main_init(void)
{ 
        return init();
}

static void __exit main_cleanup(void)
{
        cleanup();
}

module_init(main_init);
module_exit(main_cleanup);

So when I run the make:

$ make build 
make -C /lib/modules/5.10.17-v7l+/build M=/home/pi/test_asm_kmodule2 modules
make[1]: Entering directory '/usr/src/linux-headers-5.10.17-v7l+'
make[2]: *** No rule to make target '/home/pi/test_asm_kmodule2/main.o', needed by '/home/pi/test_asm_kmodule2/test.o'.  Stop.
make[1]: *** [Makefile:1804: /home/pi/test_asm_kmodule2] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.10.17-v7l+'
make: *** [Makefile:7: build] Error 2

1 Answers1

0

After oakad suggested to look in the kernel, my working solution so far: Makefile

obj-m += test.o

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

test.c

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

static int __init my_module_init(void)
{
        int a = 11;
        int b = 22;
        int reg = 0;

        asm volatile("add %[result], %[value1], %[value2]" : [result]"=r" (reg) : [value1]"r" (a), [value2]"r" (b));

        printk(KERN_INFO "result: %d\n",reg);
        return 0;
}

static void __exit my_module_cleanup(void)
{
        printk(KERN_INFO "end of test\n");
}

module_init(my_module_init);
module_exit(my_module_cleanup);

check

make
insmod ./test.ko
rmmod test
Mar 10 20:04:23 rpi4 kernel: [43868.429553] result: 33
Mar 10 20:04:31 rpi4 kernel: [43876.478748] end of test