Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a built in object for Linux Kernel?

Everywhere I search for Linux Kernel Development, I get answers for creating Linux Kernel modules. Example

 /*
* hello−1.c − The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}

Here, there is init_module and cleanup_module functions which i understand contains things to be executed when the kernel is initialized and cleaned up. There are made by adding obj-m += hello-1.c to the makefile.

But I dont want this. I want to add a built in program, not a driver, basically a service to facilitate cloud uploading of some data from the kernel level. I dont event want the module option for the program when compiling the kernel.

I understand for just programs I should use obj-y not obj-m. But there is no manual to write these kind of programs. Why? Am I missing something? Does these programs also have the init_module and cleanup_module functions even though they are not modules?

like image 579
Jamsheed Kamarudeen Avatar asked Jan 13 '15 06:01

Jamsheed Kamarudeen


People also ask

What is Makefile in Linux kernel?

The top Makefile is responsible for building two major products: vmlinux (the resident kernel image) and modules (any module files). It builds these goals by recursively descending into the subdirectories of the kernel source tree. The list of subdirectories which are visited depends upon the kernel configuration.

How do I create a module in built module?

If you would like to include your module into your kernel modules (not building), you need to copy yourmodule_file. ko under /lib/modules/$(uname -r)/ and give the command depmod -a , after this you can load your module with modprobe .


2 Answers

For example consider that your source is under driver/new in the linux kernel source tree. You need to modify Makefile's under drivers and new to build your module statically into linux kernel.

Under drivers/Makefile add the below line at the end.

obj-y   += new/

Under drivers/new/Makefile add the below line at the end.

obj-y   += hello.o

After build the linux kernel. And load to see that your module has printed the printk messages using dmesg command.

Note: When building module statically into linux, change

int init_module(void)

to

int __init init_module(void)

and change

void cleanup_module(void)

to

void __exit cleanup_module(void)
like image 59
Santosh A Avatar answered Sep 22 '22 13:09

Santosh A


Look into kernel doc Makefiles

Refer:

" --- 3.2 Built-in object goals - obj-y

The kbuild Makefile specifies object files for vmlinux
in the $(obj-y) lists.  These lists depend on the kernel
configuration.

Kbuild compiles all the $(obj-y) files.  It then calls
"$(LD) -r" to merge these files into one built-in.o file.
built-in.o is later linked into vmlinux by the parent Makefile.

The order of files in $(obj-y) is significant.  Duplicates in
the lists are allowed: the first instance will be linked into
built-in.o and succeeding instances will be ignored.

Link order is significant, because certain functions
(module_init() / __initcall) will be called during boot in the
order they appear. So keep in mind that changing the link
order may e.g. change the order in which your SCSI
controllers are detected, and thus your disks are renumbered.

Example:
    #drivers/isdn/i4l/Makefile
    # Makefile for the kernel ISDN subsystem and device drivers.
    # Each configuration option enables a list of files.
    obj-$(CONFIG_ISDN_I4L)         += isdn.o
    obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o

"

like image 24
askb Avatar answered Sep 20 '22 13:09

askb