{"id":761,"date":"2010-03-01T10:25:39","date_gmt":"2010-03-01T10:25:39","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=761"},"modified":"2010-03-01T12:09:05","modified_gmt":"2010-03-01T12:09:05","slug":"kernel-hello-world","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/01\/kernel-hello-world\/","title":{"rendered":"kernel &#8211; hello world"},"content":{"rendered":"<p>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.<\/p>\n<p>With <a href=\"http:\/\/www.kubuntu.org\/\">kubuntu<\/a> I use the aptitude command to install applications and updates etc, but to get the linux-headers I used <\/p>\n<pre lang=\"bash\">\r\naptitude install linux-headers\r\n<\/pre>\n<p>that is normally linked to the latest version of the linux kernel that you are using.<\/p>\n<p>The main part of the program is outputting a potential parameter being passed (passing parameters to a nvidia kernel module <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/02\/27\/kernel-passing-module-parameters\/\">here<\/a>) and also saying hello world, kinder two for the price of one as such tutorial. <\/p>\n<p>As in c language there is the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Printf\">printf<\/a>, but in the kernel there is a printk which is the same similar thing but more for outputting errors\/messages to the user.<\/p>\n<p>So for example<\/p>\n<pre lang=\"cpp\">\r\nprintk(KERN_INFO \"hi there\\n\");\r\n<\/pre>\n<p>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<\/p>\n<p>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)<\/p>\n<p>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.<\/p>\n<pre lang=\"cpp\">\r\nstatic int parameter_int = 0;\r\nmodule_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);\r\nMODULE_PARM_DESC(parameter_int, \"An integer value\");\r\n<\/pre>\n<p>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,<\/p>\n<pre lang=\"cpp\">\r\nmodule_init(hello_init);\r\nmodule_exit(hello_exit);\r\n<\/pre>\n<p>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.<\/p>\n<p>Here is the full code, if you save this as helloworld_parameter.c<\/p>\n<pre lang=\"cpp\">\r\n\/* hello world with passing some parameters in the kernel module *\/\r\n\r\n#include <linux\/module.h>\r\n#include <linux\/moduleparam.h>\r\n#include <linux\/kernel.h>\r\n#include <linux\/init.h>\r\n#include <linux\/stat.h>\r\n\r\nMODULE_LICENSE(\"GPL\");\r\nMODULE_AUTHOR(\"genux\");\r\n\r\n\/* need to setup a setup a static variable to hold the parameter value and\r\n   set it to a default value is none is passed *\/\r\nstatic int parameter_int = 0;\r\n\r\n\/* the linux\/stat.h has the S_IRUSR definitions etc.. *\/\r\n\/* S_IRUSR = read permission, owner\r\n   S_IWUSR = write permission, owner\r\n   S_IRGRP = read permission, group\r\n   S_IROTH = read permission, others\r\n   *\/\r\n\r\nmodule_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);\r\nMODULE_PARM_DESC(parameter_int, \"An integer value\");\r\n\r\nstatic int __init hello_init(void)\r\n{\r\n\tprintk(KERN_INFO \"Hello world\\n\\n\");\r\n\tprintk(KERN_INFO \"my parameter int value is : %d\\n\", parameter_int);\r\n\treturn 0;\r\n}\r\n\r\nstatic void __exit hello_exit(void)\r\n{\r\n\tprintk(KERN_INFO \"Goodbye from hello world parameter\\n\");\r\n}\r\n\r\nmodule_init(hello_init);\r\nmodule_exit(hello_exit);\r\n<\/pre>\n<p>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 &#8220;Makefile&#8221; <\/p>\n<pre lang=\"bash\">\r\nobj-m += helloworld_parameter.o\r\n\r\nall:\r\n\tmake -C \/lib\/modules\/$(shell uname -r)\/build M=$(PWD) modules\r\n\r\nclean:\r\n\tmake -C \/lib\/modules\/$(shell uname -r)\/build M=$(PWD) clean\r\n<\/pre>\n<p>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.<\/p>\n<p>once you have run the <\/p>\n<pre lang=\"bash\">\r\nmake\r\n<\/pre>\n<p>there should be a file called helloworld_parameter.ko, to find out details about your new module you can use the modinfo<\/p>\n<pre lang=\"bash\">\r\nmodinfo helloworld_parameter.ko\r\nfilename:       helloworld_parameter.ko\r\nauthor:         genux\r\nlicense:        GPL\r\nsrcversion:     A81F18D40DA3C5FAB1C71FF\r\ndepends:\r\nvermagic:       2.6.31-17-generic SMP mod_unload modversions\r\nparm:           parameter_int:An interger value (int)\r\n<\/pre>\n<p>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<\/p>\n<pre lang=\"bash\">\r\ntail -f \/var\/log\/messages\r\n<\/pre>\n<p>in the second console do<\/p>\n<pre lang=\"bash\">\r\ninsmod helloworld_parameter.ko\r\nrmmod helloworld_parameter.ko\r\ninsmod helloworld_parameter.ko parameter_int=3\r\nrmmod helloworld_parameter.ko\r\n<\/pre>\n<p>and the output should be in the first console<\/p>\n<pre lang=\"bash\">\r\nHello world\r\n\r\nmy parameter int value is : 0\r\nGoodbye from hello world parameter\r\n\r\nHello world\r\n\r\nmy parameter int value is : 3\r\nGoodbye from hello world parameter\r\n<\/pre>\n<p>hope that helps with kernel modules, I am planning on doing a kernel module for a custom built USB device.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/03\/01\/kernel-hello-world\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">kernel &#8211; hello world<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,4],"tags":[98,43],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","category-linux","tag-hello-world","tag-kernel"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":763,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions\/763"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}