Linux Kernel System Call report

Source: Internet
Author: User
How does one add a system call to the Linux kernel? Operating System: Ubuntu11.04? Old kernel version: 2.6.38.9? New kernel version: 2.6.39? Compiler version: GCC4.5.2 II. Tutorial purpose? How to compile the kernel? What is the kernel Compilation Principle? How can I add a system call to the kernel? Understand the operating mechanism of system calls? Learn how to call the system in user mode 3. Experiment Principle System Call: user implementation in Linux Kernel

Add a system call to the Linux Kernel

I. Environment Description

? Operating System: Ubuntu 11.04

? Old kernel version: 2.6.38.9

? New kernel version: 2.6.39

? Compiler version: GCC 4.5.2

Ii. Tutorial Purpose

? Measure the test taker's knowledge about how to compile the kernel.

? Deep understanding of kernel compilation principles

? Measure the test taker's knowledge about how to add system calls to the kernel.

? Measure the test taker's understanding about the running mechanism of system calls.

? Learn to call system calls in user mode

Iii. Experiment Principles

System Call: a set of subroutines used by users to implement system programs in the Linux Kernel

The virtual address space of a program is divided into user and kernel States. To enter the kernel, a user process must be added to the kernel through system calling or kernel modules.

Shows the implementation process of the system call (Note: This figure is from the network information ):

1. C programs include header files # include Find the actual bit of the syscall () function/usr/include/unistd through source file tracing. in h: extern long int syscall (long int _ sysno ,...) _ THROW; the function uses the INT 0x80 Soft Interrupt kernel to pass the system call number to save it to eax. Then, the system call number is used to find the function entry address of the corresponding index in the system call table. To execute the service routine. # Define SYSCALL_DEFINE1 (name ,...) SYSCALL_DEFINEx (1, _ # name, _ VA_ARGS _) is actually defined by macro definition. /kernel/sys. in c, SYSCALL_DEFINE1 (my_traverse_process, int, num) {..................................} the '1' after SYSCALL_DEFINE1 indicates that there is a parameter in the system call. The following parameters are interpreted as function names, parameter types, and parameter names. After the function is executed, run syscall_exit and return the value. INT 0x80 can be executed because the system sets 0x80 as the system call interrupt gate at startup. 3. Step indexing? Compile and upgrade the kernel? Add a system call and re-compile the kernel? Compile a user program to call the added System Call 4. experiment step 1. Compile the kernel? Download Linux kernel 2.6.39 from? Decompress the kernel to any directory (you do not need to decompress it to/usr/src/here, Linus also says you do not have to do that, and you have also tested it successfully) (*) Note: the disadvantage is that after the game module is installed, the system will create a soft link to the source code under/lib/modules/2.6.39/to compile the kernel for later use, if a user does not intend to delete the module, the module cannot be compiled .? Go to the kernel directory? First configure make menuconfig to configure the cropping kernel (if there is no need to copy/boot/config-***, this is the current Kernel configuration) cp/boot/config -***. /. config? Start compilation. You can execute make all. Of course, you can also perform make depmake cleanmake bzImahemake modules step by step? Install the kernel driver make modules_install? Install the kernel make install? Generate a boot image file and copy it to mkinitramfs-o initrd. img-2.6.39 2.6.39cp initrd. img-2.6.39? Finally, update grubupdate-grub? Start the new kernel. 2. Modify the kernel source code and add a system call? Open the file first. add the system call number at the end of/arch/x86/include/asm/unistd_32.h, as shown in the following figure (the last line ): # define _ limit 341 # define _ limit 342 # define _ NR_clock_adjtime 343 # define _ NR_syncfs 344 # define _ NR_my_traverse_process 345/* This is added by yan, this is a test for system call */? Open./arch/x86/kernel/syscall_table_32.S (last line). long sys_prlimit64 /* 340 */. long sys_name_to_handle_at.long sys_open_by_handle_at.long sys_clock_adjtime.long sys_syncfs.long sys_my_traverse_process? Open./include/linux/syscall. h and add the declaration of the system call function asmlinkage long sys_my_traverse_process (int num);/* This is added by yan */? Open. /kernel/sys. c file to add its implementation function SYSCALL_DEFINE1 (my_traverse_process, int, num) {struct task_struct * pos; struct list_head * current_head; int count = 0; printk ("Traversal module is working .. \ n "); current_head = & (current-> tasks); list_for_each_entry (pos, current_head, tasks) {count ++; printk (" [process % d]: % s \'s pid is % d \ n ", count, pos-> comm, pos-> pid);} printk (KERN_ALERT" The number of process is: % d \ n ", count); pri Ntk (KERN_ALERT "This is a sample test output from kernel! \ NAnd this is added by yan \ n "); return (long) num;} Note: The function traverses the processes in the system and outputs the process name and process number, and return the input parameter value.

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.