5
votes

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?

2
These are not programs. Just object files for the kernel. And you should modify some existing kernel files to call them! - Basile Starynkevitch
Just because your driver or whatever is a module, does not mean it has to be a dynamically loadable module. We often compile modules statically, as part of the kernel. Just consider your thing a device driver (for starters), and go read LDD3. Then start poring over the kernel sources, and see how the non-driver parts, like kernel tasks (see LANG=C LC_ALL=C ps axfu | sed -ne '1p; / \\_ \[/p' for the ones running on your own machine) differ. However, I do suspect you eventually find out your starting premise is wrong: all this is better done in userspace. - Nominal Animal

2 Answers

5
votes

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)
1
votes

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

"