riscv-software-src / riscv-tools

RISC-V Tools (ISA Simulator and Tests)
1.13k stars 446 forks source link

Cross compile LKM for Linux kernel on RISC-V #334

Closed 0ena closed 3 years ago

0ena commented 3 years ago

Hi,

I am trying to build a simple Linux Kernel Module for the Ariane RISC-V implementation. I must say that I am using the modified version of the riscv-tools of Ariane-SDK, but I didn't know anywhere else to ask for help, as I am really struggling with the creation of the Makefile for the cross-compile.

The code of my LKM is the following:

/* Kernel Programming */

#include <linux/module.h>   // Needed by all modules
#include <linux/kernel.h>

static int init_module(void)
{
   printk("Hello, world 2\n");
   return 0;
}

static void cleanup_module(void)
{
   printk("Goodbye, world 2\n");
}

My initial Makefile attempt was the following:

WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /home/user1/tools/ariane-sdk/ariane-sdk/buildroot/output/build/linux-ariane-v0.7
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := riscv64-unknown-linux-gnu-gcc
OBJS    := ${patsubst %.c, %.o, ${wildcard *.c}}

all: ${OBJS}

.PHONY: clean

clean:
        rm -rf *.o

After a discussion I had on this thread, I tried to build a Makefile, following the example shown here. Now my Makefile is the following:

DRIVER = hello-2.ko
PWD := $(shell pwd)
LINUXSRC := $(PWD)/../../../../home/user1/tools/ariane-sdk/ariane-sdk/buildroot/output/build/linux-ariane-v0.7

default:
        $(MAKE) -C $(LINUXSRC) ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- M=$(PWD)

clean:
        rm -rvf *.o *.ko

The buildroot/output/build/linux-ariane-v0.7 folder has the kernel tree of the Ariane-sdk that I am trying to cross-compile for.

When I run the above Makefile (the 2nd one), I get the following:

make -C /Projects/Proj/test/../../../../home/user1/tools/ariane-sdk/ariane-sdk/buildroot/output/build/linux-ariane-v0.7 ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- M=/Projects/Proj/test
make[1]: Entering directory `/home/user1/tools/ariane-sdk/ariane-sdk/buildroot/output/build/linux-ariane-v0.7'
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `/home/user1/tools/ariane-sdk/ariane-sdk/buildroot/output/build/linux-ariane-v0.7'

but no .ko file is generated, only 2 blank files Module.symvers and modules.order.

Can someone please advise me on where I am wrong with my Makefile?

Thank you in advance for your help and time!

Kind regards, Nassos

0ena commented 3 years ago

I was able to solve my issue. I posted the solution here. I am closing this.