Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.arcetri.astro.it/irlab/doc/library/linux/khg/HyperNews/get/khg/170.html
Дата изменения: Thu Mar 23 12:37:46 2000
Дата индексирования: Wed Sep 15 00:50:24 2010
Кодировка:

Поисковые слова: m 2
Documention on writing kernel modules The HyperNews Linux KHG Discussion Pages

Note: Documention on writing kernel modules

Forum: The Linux Kernel Hackers' Guide
Keywords: modules insmod
Date: Mon, 01 Dec 1997 20:22:08 GMT
From: Erik Nygren <nygren@mit.edu>

As far as I can tell, this site doesn't yet have information on how to write a loadable kernel module (although there are quite a few queries from people asking about how to do it). After looking around, I found that the insmod/modules.doc and insmod/HOWTO-modularize files in the modules-2.0.0.tar.gz package contained a fairly good description of some of the things you need to do when writing a kernel module. The insmod/drv_hello.c and insmod/Makefile files in that package provide an example character device driver that can be built as a module.

It would be nice if these files (or the relevant contents) could get incorporated into the KHG at some point.

To summarize, it looks like modules should be built with the following compiler options (at least, this is the way the Makefile for drv_hello.o goes):

-O6 -pipe -fomit-frame-pointer -Wall -DMODULE -D__KERNEL__ -DLINUX

Of the above, it seems likely that the key arguments are the -DMODULE -D__KERNEL__ and possibly the -O6 are actually needed. If you want to build your module with versioning support, add the following options:

-DMODVERSIONS -include /usr/include/linux/modversions.h

From the examples and docs, it looks like modules should be in the form:



#include   /* must be first! */
#include   /* optional? */
#include  /* ? */


/* More includes and the body of the driver go here. */

/* Note that any time a function returns a resource to the kernel
 * (for example, after a open),
 * call the MOD_INC_USE_COUNT; macro.  Whenever the
 * kernel releases the resource, call MOD_DEC_USE_COUNT;.
 * This prevents the module from getting removed
 * while other parts of the kernel still have
 * references to its resources.
 */


#ifdef MODULE  /* section containing module-specific code */

int
init_module(void)
{
	/* Module initialization code.
	 * Registers drivers, symbols, handlers, etc. */
	return 0; /* on success */
}

void 
cleanup_module(void)
{
	/* Do cleanup and unregister anything that was
	 * registered in init_module. */
}

#endif


Again, see the documentation scattered through the modules-2.0.0 package (and also presumably through the newer modutils-2.1.x packages) for more detailed information.