Linux kernel programming experience

Source: Internet
Author: User
Article title: Linux kernel programming experience. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
At present, under the advocacy of the proprietary operating system in China, the IT industry has set off a wave of Linux programming boom. Linux attracts more and more programmers to go deep into Linux kernel development with its advantages such as open source code and flexible configuration. I recently practiced a real-time transformation task for Linux and accumulated some practical experience in Linux kernel programming, here, I would like to discuss issues related to kernel compilation and system calling with interested fans.
  
Compile the kernel
  
In the practice of Linux programming, the kernel compilation problem is often encountered. Why should we compile the kernel? First, you can customize the kernel module. Linux introduces the concept of "dynamic loading module", allowing users to compile drivers and unnecessary kernel function code into "modules", which are dynamically loaded by the system as needed, the system is automatically uninstalled when it is not needed, which improves the efficiency and flexibility of the system. Second, you can customize system functions. When a device is added, system functions are added, and defects exposed by the system need to be patched, it is inevitable to compile the kernel when the new kernel version is ready for upgrade. Moreover, compiling the kernel is the charm of Linux's unique "system-level DIY!
  
Okay. now let's get started -- compile the kernel!
  
(1) installation source code
First, you must determine whether your Linux system has installed the kernel source code:
# Rpm-q kernel-source
Kernel_source-2.2.5-16
If no installation is confirmed, you need to find the installation disk or download the kernel-source-2.2.5-15.i386.rpm from the Internet and install:
# Rpm-Uhv kernel-source-2.2.5-15.i386.rpm
If the upgrade is in the new version, you need to find the upgrade package (linux-2.2.16.tar.gz) and decompress and install it yourself:
# Cd/usr/src
Go to the source code directory.
# Rm-rf linux
Delete previous links.
# Tar xzvf linux-2.2.16.tar.gz
Decompress the upgrade package.
# Ln-s linux-2.2.16 linux
Rebuild the directory link.
  
(2) configure the kernel
Enter the directory where the kernel source code is located:
# Cd/usr/src/linux
Clear unnecessary files first (usually generated in previous versions:
# Make mrproper
Start to configure the kernel (if you are not familiar with the various options, we recommend that you press the Enter key ):
# Make config
  
(3) compile the kernel
Clear the previously generated target file and other files:
# Make clean
Streamline the dependency between files:
# Make dep
Compile the compressed kernel:
# Make bzImage
Compilation module:
# Make modules-install
  
(4) new kernel installation
Copy the new kernel file to the/boot directory used to store the startup file:
# Cp/usr/src/linux/System. map/boot/System. new
# Cp/usr/src/linux/arch/i386/boot/bzImage/boot/vmlinuz. new
Enter the Startup directory:
# Cd/boot
Create a link for the new kernel:
# Rm System. map
# Ln-s System. new System. map
# Rm vmlinuz
# Ln-s vmlinuz. new vmlinuz
Edit the configuration file/etc/LILO. conf of lilo to enable LILO to start the new kernel:
# Vi/etc/lilo. conf
Add the following parts at the end of the file: (the content of the last two lines must be consistent with the corresponding lines of the old kernel)
Image =/boot/vmlinuz. new
Lable = new
Root =/dev/hda3
Read-only
Rewrite the start sector of LILO to make the change take effect:
# Lilo
  
(5) restart the system
# Reboot
When lilo appears after restart: Enter the new kernel number when prompted (press the TAB key to display all labels ):
Lilo: new
OK !! Boot new ......
.....
Everything runs normally and the new kernel boot is successful!
The above steps were successfully tested on pentium III/64 M/20G and Red Hat Linux 6.0 (2.2.5-15.
  
Add system call
  
In actual programming, especially when we need to add or improve system functions, we often use the system call function. A system call function is usually called by a user process in the user state. The kernel responds to soft interruptions caused by system calls through the system_call function, after correctly accessing the core stack and the system call toggle table, the system is stuck in the operating system kernel for processing.
  
System calling is a common way for a user process to switch from the user state to the core state. Some kernel code of the operating system is directly called by writing system call functions, which is also required by Linux kernel programmers. The following describes how to create a system call function named print_info in Linux to increase the number of system calls in the kernel.
  
The following basic steps are required:
  
1. Compile the system call function
Edit The sys. c file:
# Cd/usr/src/linux/kernel
# Vi sys. c
Add a system call function at the end of the file:
Asmlinkage int sys_print_info (int testflag)
{
Printk ("Its my syscall function! N ");
Return 0;
}
This function has an int entry parameter testflag and returns an integer 0.
  
2. modify the file related to the system call number
Edit the entry table file:
# Cd/usr/src/linux/arch/i386/kernel
# Vi entry. S
Add the function entry address to the sys_call_table table:
The last few lines of source code in arch/i386/kernel/entry. S are changed:
......
. Long SYMBOL_NAME (sys_sendfile)
. Long SYMBOL_NAME (sys_ni_syscall)/* streams1 */
. Long SYMBOL_NAME (sys_ni_syscall)/* streams2 */
. Long SYMBOL_NAME (sys_vfork)/* 190 */
Rept NR_syscalls-190
. Long SYMBOL_NAME (sys_ni_syscall)
. Endr
After modification:
......
. Long SYMBOL_NAME (sys_sendfile)
. Long SYMBOL_NAME (sys_ni_syscall)/* streams1 */
. Long SYMBOL_NAME (sys_ni_syscall)/* streams2 */
. Long SYMBOL_NAME (sys_vfork)/* 190 */
. Long SYMBOL_NAME (sys_print_info)/* added by I */
. Rept NR_syscalls-191
. Endr
Modify the corresponding header file:
# Cd/usr/src/linux/include/asm
# Vi unistd. h
Make necessary declarations in include/asm/unistd. h for user and other system processes to query or call the vectors corresponding to the added sys_call_table table.
# Define _ NR_putpmsg 189
# Define _ NR_vfork 190
# Define _ NR_print_info 191/* added by I */
  
3. Compile the kernel and restart it.
  
4. test
Write a user test program (test. c ):
# Vi test. c
# Include
# Include
Extern int errno;
_ Syscall1 (int, print_info, int, testflag)
Main ()
{
Int I;
I = print_info (0 );
If (I = 0)
Printf ("I = % d, syscall success! N ", I );
}
If you want to use the system call function in your program, you must declare the call _ syscall before the main function. 1 indicates that the system call has only one entry parameter, the first int indicates that the return value of the system call is an integer, print_info indicates the name of the system call function, the second int indicates that the type of the entry parameter is an integer, and testflag indicates the entry parameter name.
Compile the test program:
# Gcc-o test. c
Run the test program:
#./Test
Its my syscall function!
I = 0, syscall success!
OK !!! Added the system call function!
The above steps were successfully tested on pentium III/64 M/20G and Red Hat Linux 6.0 (2.2.5-15.
(Author: Li Yanbin)
  

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.