Forum Discussion
Kernel development on Nios Linux 2.6 distribution
The Nios II Linux 2.6 distribution is not really biased for kernel development, but since this is all what I have, what way you suggest I should develop my drivers?
I understood importing the source files into a kernel project was good just for debugging the kernel, so that you actually see where you are going. So any changes should be made directly to the kernel directory in the Eclipse plug-in directory. Am I right? Okay, I can do the changes there, but how can I get them into the IDE reasonably? There's no "open file" option in the IDE! If I develop my driver as a module, could I just use the Linux application project as a basis and just fix the makefile so that it will be compiled as a module? Will there be a problem with the kernel include files? Thanks for any ideas in advance! -Tervis21 Replies
- Altera_Forum
Honored Contributor
Hey come on!
Am I the only one wanting to develop kernel drivers with this package. I seriously doubt that... Gimme some input here, please... http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/laugh.gif -Tervis - Altera_Forum
Honored Contributor
This message will describe building a hello_world driver for the kernel. The driver will be listed as a miscellaneous driver inside the kernel configuration tool.
(1) Create the code for the driver (hello_world.c):
(2) Create the Makefile for the driver:#include <linux/init.h># include <linux/module.h># include <linux/kernel.h> static int hello_init(void) { printk (KERN_ALERT "Hello, world!\n"); return 0; } static void hello_exit (void) { printk (KERN_ALERT "Goodbye, cruel world\n"); } module_init (hello_init); module_exit (hello_exit); MODULE_LICENSE("Dual BSD/GPL");
(3) Copy the two files into the source tree using the following few commands:ifneq ($(KERNELRELEASE),) # This is the section of the Makefile that builds the module inside the kernel obj-$(CONFIG_HELLO_WORLD) := hello_world.o else # This section is for building the module outside of the source # $(ECLIPSE_WORKSPACE) is assumed to be defined externally by the environment # please reference the articles on http://www.lwn.net regarding# kernel module development for more information about this Makefile KDIR := $(KERNEL_PLUGIN)/linux-2.6.x KERNEL_PROJECT := eval_kit_0 BUILDDIR := $(ECLIPSE_WORKSPACE)/$(KERNEL_PROJECT)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) O=$(BUILDDIR) SUBDIRS=$(PWD) modules endif
(4) Update the Makefile and Kconfig file in the $(KERNEL_PLUGIN)/linux-2.6.x/drivers/misc directory...cd <wherever the above files live> mkdir $(KERNEL_PLUGIN)/linux-2.6.x/misc/hello_world cp * $(KERNEL_PLUGIN)/linux-2.6.x/drivers/misc/hello_world## Makefile for misc devices that really don't fit anywhere else.# obj- := misc.o # Dummy rule to force built-in.o to be made obj-$(CONFIG_IBM_ASM) += ibmasm/ # add the following line obj-$(CONFIG_HELLO_WORLD) += hello_world/
After saving all the modified files, you can now configure a kernel with the above driver. It is recommended that the driver be compiled into the kernel for testing purposes since that should help with the debugging effort. The kernel option will appear in Device Drivers -> Miscellaneous -> Hello_world example. Once you have configured, built, and uploaded your kernel, the kernel should start and display: "Hello, World!" while it is starting up. Much of the above code was taken from LWN.net's Device driver series of articles. Hope this helps... Ken. BTW, debugging is discussed within the reference manual I believe... I may post some additional instructions up here later on if people have trouble with debugging the kernel.## Misc strange devices# menu "Misc devices" config IBM_ASM tristate "Device driver for IBM RSA service processor" depends on X86 default n ---help--- This option enables device driver support for in-band access to the IBM RSA (Condor) service processor in eServer xSeries systems. The ibmasm device driver allows user space application to access ASM (Advanced Systems Management) functions on the service processor. The driver is meant to be used in conjunction with a user space API. The ibmasm driver also enables the OS to use the UART on the service processor board as a regular serial port. If unsure, say N. # add the following few lines config HELLO_WORLD tristate "Hello_world example" default n endmenu - Altera_Forum
Honored Contributor
Is it possible to develop a kernel driver as an independant module ? This module will be dynamically linked in the kernel with insmod command.
I tried to develop such a module, like I used to do under "standard" Linux (Red Hat or Montavista), but I get several warnings on compiling "linux/module.h" and "linux/kernel.h". Is there other defines to add that __KERNEL__ and MODULE ? Should I use the standard C library for kernel development ? - Altera_Forum
Honored Contributor
<div class='quotetop'>QUOTE </div>
--- Quote Start --- Is it possible to develop a kernel driver as an independant module ? This module will be dynamically linked in the kernel with insmod command.[/b] --- Quote End --- Yep. You'll need to use insmod from busybox to load in the module. I've done it a number of times using a very simple hello_world example driver. <div class='quotetop'>QUOTE </div> --- Quote Start --- Is there other defines to add that __KERNEL__ and MODULE ? Should I use the standard C library for kernel development ?[/b] --- Quote End --- That I'm not sure of, my driver development skills aren't very strong. I would recommend the following two sites for more information. linux device drivers, 2nd edition: online book (http://www.xml.com/ldd/chapter/book/) lwn: porting device drivers to the 2.6 kernel (http://lwn.net/articles/driver-porting/) I found that most of the information from both sources are quite applicable. Hope this helps... - Altera_Forum
Honored Contributor
Previous answer is not enough to build a module independantly from Kernel. My goal is to have my source files on a separate directory (which is handled by a Software Configuration Manager, here ClearCase) with a Makefile to generate the .o module file.
I try to get back the compilation options from the .xxx.o.cmd file in kernel/build/drivers directory. I can now compile files and generate a module without any warning nor error. But this .o file is not recognized by insmod : it gives the following error message : No module found in object insmod: cannot insert "testModule_mod.o": Invalid module format (-1): Exec format error Here is the Makefile (a little bit ... complicated !). With this Makefile, I tried to re-compile one Kernel driver, and I'm very surprise to find an object with a different size (124kb instead of 126kb) : I surely made a mistake, but I can't see where (no errors, no warning... but a little small difference !).all: testModule_mod.o testModule_mod.o: testModule.o nios2-elf-ld -mnios2elf -r -o testModule_mod.o testModule.o testModule.o: testModule.c Makefile nios2-elf-gcc -nostdinc -Wp,-MD,.testModule.o.d -iwithprefix include -D__KERNEL__ -Iinclude -Iinclude2 -I/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/include -I/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/ -I$(LIBMYKERNEL) -I$(LIBKERNEL) -D__KERNEL__ -I/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/include -Iinclude -I/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/include2 -Iinclude2 -I/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/include -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -DNO_MM -pipe -D__linux__ -D__ELF__ -DNO_TEXT_SECTIONS -fno-builtin -O2 -g -G 0 -DUTS_SYSNAME=\"uClinux\" -I/cygdrive/c/altera/kits/nios2/bin/nios2-gnutools/H-i686-pc-cygwin/bin/../lib/gcc-lib/nios2-elf/3.3.3/include -O2 -fomit-frame-pointer -DKBUILD_BASENAME=testModule -DMODULE -c -o testModule.o testModule.c - Altera_Forum
Honored Contributor
That's an extremely complicated makefile... have you tried using the Makefile I provided in this thread? It does allow for building outside of the kernel tree. As a bonus, it also pulls in all the necessary rules for building the module properly from the original kernel source instead of requiring you to specify the rules manually.
Give it a try, and post any errors that you get when you try running "make". It worked like a charm when I did it. In fact, here's the Makefile tailored to your situation:CONFIG_TEST_MOD=m ifneq ($(KERNELRELEASE),) # This is the section of the Makefile that builds the module inside the kernel obj-$(CONFIG_TEST_MOD) := testModule_mod.o else # This section is for building the module outside of the source# $(ECLIPSE_WORKSPACE) is assumed to be defined externally by the environment # please reference the articles on http://www.lwn.net regarding# kernel module development for more information about this Makefile KDIR := $(KERNEL_PLUGIN)/linux-2.6.x $(error You need to specify the kernel project that this module is being built for before this will work!)# delete the above line after you have modified the variable below to its proper setting KERNEL_PROJECT := your_kernel_project BUILDDIR := $(ECLIPSE_WORKSPACE)/$(KERNEL_PROJECT)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) O=$(BUILDDIR) SUBDIRS=$(PWD) modules endif - Altera_Forum
Honored Contributor
I'm sorry, but I still have a lot of errors with your Makefile. I think the basic environment information is missing...
Here are the error messages : (invoked directly from NIOS II-IDE, Build project option) <div class='quotetop'>QUOTE </div> --- Quote Start --- make -k default make -C /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x O=/cygdrive/c/altera/kits/nios2/bin/eclipse/workspace/Kernel_DemoBoard_LAN/build SUBDIRS=/cygdrive/c/altera/kits/nios2/bin/eclipse/workspace/testModule modules make[1]: Entering directory `/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x' /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/Makefile:421: /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/arch/i386/Makefile: No such file or directory gcc: not found make[2]: *** No rule to make target `/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/arch/i386/Makefile'. make[2]: Failed to remake makefile `/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x/arch/i386/Makefile'. gcc: not found Using /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x as source for kernel gcc: not found *** Warning: Overriding SUBDIRS on the command line can cause *** inconsistencies gcc: not found HOSTCC scripts/basic/fixdep gcc: not found make[3]: *** [scripts/basic/fixdep] Error 127 make[3]: Target `__build' not remade because of errors. make[2]: *** [scripts_basic] Error 2 make[2]: Target `modules' not remade because of errors. make[1]: *** [modules] Error 2 make[1]: Leaving directory `/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x' make: *** [default] Error 2[/b] --- Quote End --- It appears that architecture information is missing, as Cross-compiler. So I tried to add 'ARCH=nios2nommu CROSS_COMPILE=nios2-elf-' in $(MAKE) command. Result is better, but there is still problems : <div class='quotetop'>QUOTE </div> --- Quote Start --- make -k default make -C /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x ARCH=nios2nommu CROSS_COMPILE=nios2-elf- O=/cygdrive/c/altera/kits/nios2/bin/eclipse/workspace/Kernel_DemoBoard_LAN/build SUBDIRS=/cygdrive/c/altera/kits/nios2/bin/eclipse/workspace/testModule modules make[1]: Entering directory `/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x' Using /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x as source for kernel *** Warning: Overriding SUBDIRS on the command line can cause *** inconsistencies HOSTCC scripts/basic/fixdep gcc: not found make[3]: *** [scripts/basic/fixdep] Error 127 make[3]: Target `__build' not remade because of errors. make[2]: *** [scripts_basic] Error 2 make[2]: Target `modules' not remade because of errors. make[1]: *** [modules] Error 2 make: *** [default] Error 2 make[1]: Leaving directory `/cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5/linux-2.6.x'[/b] --- Quote End --- Don't you have such errors ? - Altera_Forum
Honored Contributor
Ah... I see. I forgot to mention that I meant for you to use the Makefile from Nios II SDK shell. There isn't an appropriate project type within the Nios II IDE for module/driver development yet.
The Nios II SDK shell will contain all the correct environment variables that you need. It will also contain a correct PATH setting. Give it another shot through the Nios II SDK Shell and let me know how it goes... - Altera_Forum
Honored Contributor
OK, it's perfect ! I'm sorry for so many exchanges for a so easy conclusion !
Hope it may help other guys ! - Altera_Forum
Honored Contributor
Hi,
To continue on this same subject, I just dowloaded the new release 1.3 and I try to compile my driver, as a module. It doesn't compile any more : the error is "cannot find .config file". It seems to look for this file in the "plugins" kernel directory instead of my kernel directory. Is there something to change in the Makefile ?CONFIG_TEST_MOD=m ifneq ($(KERNELRELEASE),) # This is the section of the Makefile that builds the module inside the kernel obj-$(CONFIG_TEST_MOD) := JGXEOE.o else # This section is for building the module outside of the source# $(ECLIPSE_WORKSPACE) is assumed to be defined externally by the environment # please reference the articles on http://www.lwn.net regarding# kernel module development for more information about this Makefile KERNEL_PLUGIN := /cygdrive/c/altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.kernel_0.1.5 ECLIPSE_WORKSPACE := /cygdrive/c/altera/kits/nios2/bin/eclipse/workspace KDIR := $(KERNEL_PLUGIN)/linux-2.6.x KERNEL_PROJECT := Kernel_MUX BUILDDIR := $(ECLIPSE_WORKSPACE)/$(KERNEL_PROJECT)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) O=$(BUILDDIR) SUBDIRS=$(PWD) modules endif