On Wed, Jul 20, 2005 at 08:41:56AM +0530, Vijay Ram Chitrapu (RCVIJAYD) wrote: > I am a newbie to programming in kernel. > I am facing a problem with "insmod". > I have several files say. test1.c test2.c > I have compiled all the files with the -o option and created a common .o > file. Sample of the above files is as below.. > > int minit(char *a, int argc,char * argv[]) > { > printk("<1>...argc: %d\r\n",argc); ^^^ Do not use "<1>", use the KERN_ macros in include/linux/kernel.h instead. If the values of KERN_ change, your module will continue to work, but only if you use the KERN_ macros. Make the printk(): printk(KERN_ALERT "blablabla\n"); > if (argc < 2) > { > printk("<1>\n"); > return 0; > } > return 0; > } > > the function minit is prototyped in a header file. The entry point of a kernel module is the function pointed by the module_init() macro. The function prototype of the init function is not like in userland, but: static int init_module(void); . IOW: it takes *no* arguments. > All the code files will contains functions similar to this with different > name and prototypes. None of the files have a init_module function. Well, that's an error. There should be an entry point to a module, otherwise there's no way to initialise a module. > While doing insmod of the final obtained .o file, an error is being > thrown saying, Floating Point Exception and core is dumped. > I have debugged the code using GDB and found that the use of the int argc > in the above function was creating the exception.. That doesn't surprise me at all. The function prototype of a module init function is: static int init_module(void); You created a function like: int minit(char *, int, char * []); Your function parameters take whatever was left on the stack (i.e.: random values) > Can anybody please clear my doubts > 1. can i insert a module which does not have a init_module and > cleanup_module functions? No, of course not. How should the kernel know what to do with a random piece of code? > 2. While doing insmod, is the code executed or somethign simialr > happens..? This doubt could be silly but..the reason behind this doubt is > the exception as i said was for local variables. No, the only thing that gets executed is the function declared in the module_init() macro. > 3. under wat conditions do we get a FPE (floating point exception) ? By creating random junk and trying to load it as a module. > Basically the above code was actually written for the user space usage > and am now trying to change it to the kernel space. Do not even *think* about doing that. The kernel has a very specific task: to share resources (CPU time, access to devices and files) between processes and to protect those processes against each other. It is NOT a place to run userspace programs. Trying to run programs in kernel space only has disadvantages: - It doesn't become any faster. - It's much more difficult. - Any programming error you make will be fatal and compromise the stability of the whole system. > Do we need to take some extra care to make the necessary changes? I think you should read "Linux Device Drivers, 3rd edition". If you don't want to buy it, it's available online for free at http://www.xml.com/ldd/chapter/book/ . Erik -- Erik Mouw J.A.K.Mouw@xxxxxxxxxxxxxx mouw@xxxxxxxxxxxx
Attachment:
signature.asc
Description: Digital signature