Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How program and compile "Hello World" code in kernel mode of linux?

Yes, as the title, I don't know how to program and compile "Hello World" code in kernel mode of linux , please help me in the shortest and easy to understand way. Thank you ! (Any related document is welcomed too, I'm just new to this)

like image 206
Little Jack Avatar asked Sep 17 '10 08:09

Little Jack


3 Answers

You can start Here:

/*  
 *  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");
}
like image 58
fseto Avatar answered Oct 03 '22 06:10

fseto


Wow, that's a question!

Just think first that the Linux kernel has no terminal, no direct interaction with the user. A Hello World cannot be invoked as any other user program on the command line. The best fit I can think of is a character device driver implemented as a kernel module that would read "Hello World" on device /dev/helloworld for example.

I can point you to reading the book from Rubini: Linux Device Drivers. It explains and has examples to create simple Hello World kind of kernel modules.

like image 31
Didier Trosset Avatar answered Oct 03 '22 05:10

Didier Trosset


Additional information: The printk function is provided by kernel and it prints to the file such as /var/log/messages. In Ubuntu, this is the /var/log/syslog file. You can see the output of the hello module in this file. Also, thanks fseto for pointing the Linux Kernel Module Programming Guide. It is awesome.

like image 38
Barış Akkurt Avatar answered Oct 03 '22 05:10

Barış Akkurt