kernel – hello world

To compile up this module you will need to have the linux header files, which should be installed but if not you just need to either use apt-get, rpm, pacman etc to install the linux header files.

With kubuntu I use the aptitude command to install applications and updates etc, but to get the linux-headers I used

aptitude install linux-headers

that is normally linked to the latest version of the linux kernel that you are using.

The main part of the program is outputting a potential parameter being passed (passing parameters to a nvidia kernel module here) and also saying hello world, kinder two for the price of one as such tutorial.

As in c language there is the printf, but in the kernel there is a printk which is the same similar thing but more for outputting errors/messages to the user.

So for example

printk(KERN_INFO "hi there\n");

the KERN_INFO is a kernel information marco that sends the details to a information level of output, e.g. /var/log/messages or you could use dmesg

here is the code on how to pull in parameters from a kernel module being loaded, the parameter_int is my parameter to be checked against and the S_IRUSR is the access rights (IR = read permission, IW = write permission, USR = user, GRP = group, OTH = others)

The module_param takes 3 parameters, the first is the variable to place the value into, second is the variable type and the third is the access rights, with the MODULE_PARM_DESC is the description of the parameter to pass, in this case it is just a integer value.

static int parameter_int = 0;
module_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(parameter_int, "An integer value");

Within a normal c/c++ program there is a main function where the program is run from, but in the kernel space since the main program is already running you can define what functions to call when the module is inserted and also removed,

module_init(hello_init);
module_exit(hello_exit);

the module_init parameter is the function to call when the module is loaded and the module_exit parameter is the function to call when module is removed from the kernel space.

Here is the full code, if you save this as helloworld_parameter.c

/* hello world with passing some parameters in the kernel module */
 
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("genux");
 
/* need to setup a setup a static variable to hold the parameter value and
   set it to a default value is none is passed */
static int parameter_int = 0;
 
/* the linux/stat.h has the S_IRUSR definitions etc.. */
/* S_IRUSR = read permission, owner
   S_IWUSR = write permission, owner
   S_IRGRP = read permission, group
   S_IROTH = read permission, others
   */
 
module_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(parameter_int, "An integer value");
 
static int __init hello_init(void)
{
	printk(KERN_INFO "Hello world\n\n");
	printk(KERN_INFO "my parameter int value is : %d\n", parameter_int);
	return 0;
}
 
static void __exit hello_exit(void)
{
	printk(KERN_INFO "Goodbye from hello world parameter\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

since we are compiling a kernel module we need to link to the loaded kernel modules, here is a Makefile to compile up the program, so save this as “Makefile”

obj-m += helloworld_parameter.o
 
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
 
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

the first is the obj-m += which means compile a object module and you could have more than one file to compile up so use the += to add more files to it, the -C means change directory for the build environment for the kernel space, the M is a parameter passed to the build environment to use this current directory for where the source files are, and the modules means to create kernel modules e.g. filename.ko.

once you have run the

make

there should be a file called helloworld_parameter.ko, to find out details about your new module you can use the modinfo

modinfo helloworld_parameter.ko
filename:       helloworld_parameter.ko
author:         genux
license:        GPL
srcversion:     A81F18D40DA3C5FAB1C71FF
depends:
vermagic:       2.6.31-17-generic SMP mod_unload modversions
parm:           parameter_int:An interger value (int)

and the parm: is the important part here, it is what the parameter is called to pass a value to for example to watch the module being inserted if you open up two consoles and on one put

tail -f /var/log/messages

in the second console do

insmod helloworld_parameter.ko
rmmod helloworld_parameter.ko
insmod helloworld_parameter.ko parameter_int=3
rmmod helloworld_parameter.ko

and the output should be in the first console

Hello world
 
my parameter int value is : 0
Goodbye from hello world parameter
 
Hello world
 
my parameter int value is : 3
Goodbye from hello world parameter

hope that helps with kernel modules, I am planning on doing a kernel module for a custom built USB device.

kernel – passing module parameters

With normal programs that allow you pass parameters on there command line, for example

printoutmyname genux

where the program is called printoutmyname and the first parameter is genux (it is normal that the first parameter [0] is actually the program name and the second parameter [1] is the parameter that is passed)

Well in Linux kernel (where here is a example on how to compile the kernel for a ubuntu based setup) you can pass parameters to the modules that are getting loaded. One module would be your graphics card, in my case a nvidia graphics card.

To find out what parameters can be passed to the graphics card you will have to find the kernel module file (filename.ko, where the ko is the kernel object file), so to search for the nvidia.ko in my case I did

locate nvidia.ko

and then changed to that directory and did a module information on it

cd /lib/modules/2.6.31-17-generic/updates/dkms/
ls
nvidia.ko  vboxdrv.ko  vboxnetadp.ko  vboxnetflt.ko

and doing a module information on it you call the modinfo on the kernel object file as

modinfo nvidia.ko
</pre
 
my output was
 
<pre lang="bash">
filename:       nvidia.ko
license:        NVIDIA
alias:          char-major-195-*
alias:          pci:v000010DEd*sv*sd*bc03sc02i00*
alias:          pci:v000010DEd*sv*sd*bc03sc00i00*
depends:
vermagic:       2.6.31-17-generic SMP mod_unload modversions
parm:           NVreg_EnableVia4x:int
parm:           NVreg_EnableALiAGP:int
parm:           NVreg_ReqAGPRate:int
parm:           NVreg_EnableAGPSBA:int
parm:           NVreg_EnableAGPFW:int
parm:           NVreg_Mobile:int
parm:           NVreg_ResmanDebugLevel:int
parm:           NVreg_RmLogonRC:int
parm:           NVreg_ModifyDeviceFiles:int
parm:           NVreg_DeviceFileUID:int
parm:           NVreg_DeviceFileGID:int
parm:           NVreg_DeviceFileMode:int
parm:           NVreg_RemapLimit:int
parm:           NVreg_UpdateMemoryTypes:int
parm:           NVreg_UseVBios:int
parm:           NVreg_RMEdgeIntrCheck:int
parm:           NVreg_UsePageAttributeTable:int
parm:           NVreg_EnableMSI:int
parm:           NVreg_MapRegistersEarly:int
parm:           NVreg_RmNvclkIdleGraphics:int
parm:           NVreg_RegistryDwords:charp
parm:           NVreg_NvAGP:int

where the parm: is a parameter to pass to the module on load (insmod, or loaded via the kernel at boot time which you can force to load via the /etc/modules file and the parameters can be placed in the /etc/modprobe.d directory).

for example to load the nvidia module with a parameter NVreg_NvAGP you would do something like

insmod nvidia.ko NVreg_NvAGP=1

and the passing value is 1 to the NVreg_NvAGP parameter

Compile a k/ubuntu kernel

Because of the issues that I have had with a 4GB of RAM and the nvidia onboard graphics trying to grab memory where the system RAM was placed. The nvidia onboard graphics was told to grab the memory from 0xb0000000 – 0xbfffffff because that was where the ACER BIOS was telling it where to go!!. (Here for more details)

Anyway, because of this, I am having to compile up the linux kernel ubuntu style. I am using 9.10 kubuntu at present.

I have used this ubuntu guide on how to compile up a linux kernel from this ubuntu website, kernel/compile.

The main parts that I have run are, (I am not using sudo because I setup the root password so that I do not have to type in sudo all of the time, I setup the root password by sudo passwd root)

cd /usr/src/
apt-get install fakeroot kernel-wedge build-essential makedumpfile
 
apt-get build-dep linux
 
apt-get build-dep linux-image-$(uname -r)
apt-get source linux-image-$(uname -r)
 
debian/rules updateconfigs
 
cd debian.master/scripts/misc 
chmod a+x *
cd -
debian/rules updateconfigs

I go into the /usr/src, because when you get the apt-get source of a the latest linux-image it places the files within the present directory that you are using and usually the linux kernel source files are placed in /usr/src.

The reason for the chmod a+x * is because some of the debian(.master)/scripts/misc need to be updated to have there execute permissions granted.

Make some changes to the kernel files, in my case the arch/x86/pci/i386.c file. Then to compile up the kernel

fakeroot debian/rules clean
CONCURRENCY_LEVEL=2 AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules binary-generic

4GB – 32bit northbridge BAR1 error

With the Laptop that I have, it appears to have a 32 bit northbridge with a 64 bit cpu’s, I have logged this error with the PCI kernel group because hopefully they will be able to give some advice and also I am giving data back to the Linux group on the whole.

Shall put any advice on the kernel PCI group list back on this page regarding the error, if anyone wants to look over the 2GB/4GB dmesg + /proc/iomem files just say.

“I have a acer aspire 9815 with from what I have read a 32bit Northbridge and when you add 4GB of RAM with a nVidia 256 of “virtual
ram” causes a base address registers (BAR1), but works fine with 2GB because there is a problem with the way that Linux kernel is
allocating the memory associated with the devices and places then out of range.

Here is my lspci for the PCI host bridge

Host bridge: Intel Corporation Mobile 945GM/PM/GMS, 943/940GML and
945GT Express Memory Controller Hub (rev 03)
PCI bridge: Intel Corporation Mobile 945GM/PM/GMS, 943/940GML and
945GT Express PCI Express Root Port (rev 03)

From this website it appears that some one was trying to update the PCI IOMEM part of the kernl

http://tjworld.net/wiki/Linux/PCIDynamicResourceAllocationManagement

but says that it will be in the .30 / .31 kernel but I am not able to still use the 4GB of ram and I am using the 2.6.31-10-generic
#35-Ubuntu SMP Tue Sep 22 17:33:42 UTC 2009 i686 GNU/Linux from a kubuntu 9.10 setup and was hoping to have the fix already applied.

Was wondering since I am using the k/ubuntu kernel does the “real” kernel have the PCI IOMEM Upgrade ? or do I have pass some kernel
parameters on grub to allow for this to work correctly ? or has there been another upgrade to the PCI structure that will come in a later
release ?

Also there was some advice to update the BIOS for some laptops and set the upper memory limit, does this make the PC memory have a virtual
head to it and thus the OS will not see the rest of RAM ? or is it just for the PCI setup aspects that look at the upper memory limit and
the rest of the OS can see the and use the rest of the RAM ?

Any advice would be great.”

UPDATE

The only advice was that how about trying out the 64bit kernel, which is what I am using.