In May 18, my first Linux kernel module

Source: Internet
Author: User

3 days after being depressed, makefile won't be used at the beginning, and a helloworld cannot be compiled. o. Later, I saw a copy on the Internet. The most important thing is that the include path should be the include directory in the kernel source code, in this way, we can compile a module that is the same as the current kernel version, and then say-O2 is required, so that we can add some embedded functions.
Cc = gcc
Cinclu =/usr/src/linux-2.4/include
Cflages =-dlinux-C-O2-wall-I $ (cinclu)
ALL: mydrv1.o
Mydrv1.o: mydrv1.c
$ (CC) $ (cflages) $ <
Clean:
Rm-f *. o
Note that you must have a tab key before compiling the command.

The purpose of this module is to list all processes and all modules. When the kernel global variable current is used, this variable is absent, actually, I forgot to include <Linux/sched. h>
Current is a pointer of the struct task_list type. I don't know when there is no struct task_list * next_task, * prev_task in it. Many modules must be modified to use these two member variables, but in sched. the next_task (p) macro is found in H:>
The most disturbing thing is that when module_list is used, it is always said no. It has been depressing for almost two days. It won't work at all. Otherwise, do not define the module macro, in that case, module_list is available, but it is not a module, and it cannot be used. module_list is used in many places on the Internet and in the book. There is also a foreigner in the mail list who has the same problem, but it has not been solved. It seems like it is in the Linux Kernel mailling list, fixed: who will look at this problem in that place. Later, I found that module_list is useless, and there is no module_list in 2.6.0 ,,
In sched. H
# If defined (module )&&! Defined (_ genksyms __)
.
Extern struct module _ this_dodule;
.
# Else/* module */
.
# Ifndef _ genksyms __
.
Extern struct module * module_list;
.
# Endif /*! _ Genksyms __*/
.
# Endif/* module */

Obviously, it's not used. It's killing me. It's replaced by _ this_module.
# Ifndef _ genksyms __

# Define this_module null
# Define mod_inc_use_count do {} while (0)
# Define mod_dec_use_count do {} while (0)
# Define mod_in_use 1

Extern struct module * module_list;

# Endif /*! _ Genksyms __*/
Many other macros are not available, and they are all false.

No module_list exists in the ksyms-a column.
In/boot/system. map shows the address of module_list. use struct module * temp = (struct module *) 0xc03064c0 in this way. It seems that module_list is actually suspended.

Paste the module here. All processes are listed during insmod and all modules are listed during rmmod.

/*************************************** ******
* Simple work like the PS-A and lsmod
* But reference as module
* Writen by uty @ uaty
**************************************** *****/
# DEFINE _ KERNEL __
# Define Module
// # UNDEF _ genksyms __
# Ifdef modversions
# Include <Linux/modversions. h>
# Endif
// # DEFINE _ no_versions __

# Include <Linux/config. h>
# Include <Linux/module. h>

# Include <Linux/kernel. h>
# Include <Linux/sched. h>

Int init_module (void)
{
Struct task_struct * temp;
For_each_process (temp) {/* Haha:> see the sched. h this is the better way than belows */
Printk ("% d % s/n", temp-> PID, temp-> comm );
}
/* Printk ("<5> init mydrv/N ");
Printk ("% d % s/n", current-> PID, current-> comm );
Temp = current;
Temp = next_task (temp );
Printk ("% d % s/n", current-> PID, current-> comm );
While (1 ){
Temp = next_task (temp );
If (temp-> pid = 0) break;
Printk ("% d % s/n", temp-> PID, temp-> comm );
}
*/
Return 0;
}
Void cleanup_module (void)
{
Struct module * temp;
Int I = 0;
/* Temp = (struct module *) 0xc030b4c0;
Printk ("% s/n", temp-> name); * // * I get the module_list's address from the system. Map */
Printk ("% s/n" ,__ this_module.name);/* but seems doesn't work */
Temp = & __ this_module;
While (I <30 ){
I ++;
If (temp-> next! = NULL ){
Temp = temp-> next;
} Else {
Break;
}
Printk ("% s/n", temp-> name );
}
Printk ("<5> cleanup mydrv/N ");

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.