Tutorial: Calling an OKL4 Server from OK Linux
Now we have a functional hello server we will use it from the para-virtualized Linux. We will do so using a new entry in /proc.
Step 1: Library
The first thing to do is to link the kernel to our client library libhello.a. To achieve this we simply add a -lhello flag to the kernel Makefile.
$ cat linux/kernel-2.6.11-v2/arch/l4/Makefile [...] libs-y += -lvtimer -lvserial -ll4e -lll -liguana -ll4 -lgcc -lmutex -lcircular_buffer -lnaming -lhello [...]
Step 2: A New Module
We will now create a new kernel module to allow use interacting with the server using a /proc entry. We will work directly in a hello folder in the kernel root in order to make things fast and easy.
$ mkdir linux/kernel-2.6.11-v2/hello $ touch linux/kernel-2.6.11-v2/hello/Makefile $ touch linux/kernel-2.6.11-v2/hello/my_l4_hello.c $ ls linux/kernel-2.6.11-v2/hello Makefile my_l4_hello.c
We need to modify the global Makefile :
$ cat linux/kernel-2.6.11-v2/Makefile [...] core-y := usr/ hello/ [...]
and create a new one for our source file.
$ cat linux/kernel-2.6.11-v2/hello/Makefile obj-y += my_l4_hello.o
Now here is the code for our module :
$ cat linux/kernel-2.6.11-v2/hello/my_l4_hello.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>
#include <hello/hello.h>
/* Defines the license for this LKM */
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("L4 Hello Wrapper");
static struct proc_dir_entry *proc_entry;
int hello_read( char *page, char **start, off_t off,
int count, int *eof, void *data )
{
say_hello();
return 0;
}
int my_module_init( void )
{
int ret = 0;
proc_entry = create_proc_entry( "hello", 0644, NULL );
if (proc_entry == NULL)
{
ret = -ENOMEM;
printk(KERN_INFO "fortune: Couldn't create proc entry\n");
}
else
{
proc_entry->read_proc = hello_read;
proc_entry->write_proc = hello_write;
proc_entry->owner = THIS_MODULE;
printk(KERN_INFO "<===== Hello Module Started =====>\n");
}
hello_init();
return ret;
}
void my_module_cleanup( void )
{
remove_proc_entry("hello", &proc_root);
printk(KERN_INFO "<===== Hello Module Unloaded =====>\n");
return;
}
module_init( my_module_init );
module_exit( my_module_cleanup );This module creates a new entry in /proc (/proc/hello) and calls say_hello on each reading access.
We can see that using our server is pretty simple, three line are concerned:
#include <hello/hello.h>: to include user library headers
hello_init();: in order to initiate the communication.
say_hello();: to interact with our server.
Step 3: Final test
We compile and test:
$ ./tools/build.py machine=ia32_pc99 project=iguana wombat=True simulate [...] <===== Hello Module Started =====> [...] <===== Hello Server Started =====> [...] Please press Enter to activate this console. BusyBox v1.00 (2008.03.28-14:47+0000) Built-in shell (ash) Enter 'help' for a list of built-in commands. / # cat /proc/hello I say hello / #
Acknowledgment: Tutorial by Remy Gottschalk, via http://lists.okl4.org/pipermail/developer/2008-April/000857.html