Add system calls in linux

Source: Internet
Author: User
Article Title: add system calls in linux. 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.

1. Basic principles of linux system calls

The linux system is compatible with POSIX and is also a set of C language function names. However, the internal implementation method of linux system calls is similar to the INT 21H of DOC. It is a service that enters through the Soft Interrupt of INT 0x80 h, and then serves in different categories according to the system call number.

From the perspective of system analysis, linux calls involve four problems.

(1) data structures and functions related to system calls

The function name starts with "sys _" and is called by the system. For example, the system calls the fork () Response Function sys_fork () (see Kernel/fork. c), the response function of exit () is sys_exit () (see kernel/fork. c ).

The include/asm/unisted. h file specifies a unique number for each system call. If the name is used to represent the name of the system call, the relationship between the system call number and the system call response function is: Use the system call number _ NR_name as the subscript to find the system call table sys_call_table (see

The contents of the corresponding table item in arch/i386/kernel/entry. S), which is the entry address of the sys_name response function called by the system. System Tuning

The sys_call_table table is used to record the position of each sys_name function in the table. There are 190 items in total. With this table, it is easy to call

In the offset of the table, find the corresponding entry address of the System Call response function. There are a total of 256 Items in the system call table, and the remaining items are the system call space that can be added by the user.

(2) the process of converting the system call command of a process to INT 0x80 interrupted

Macro definition _ syscallN () SEE include/asm/unisted. h) for format conversion and parameter transfer of system calls. N is 0 ~ An integer between 5.

_ SyscallN () is responsible for format conversion and parameter transmission for system calls with N parameters. Put the system call number in the EAX register and start INT 0x80

The returned values are sent to the EAX register.

(3) initialization of the system call function module

Initialize the system call, that is, the INT

0x80 initialization. When the system starts, the Assembly subroutine setup_idt (see arch/i386/kernel/head. S) has prepared a 256 idt table, which consists

Start_kernel () (see init/main. c), trap_init () (see

Arch/i386/kernel/traps. c) macro definition of c language called

Set_system_gate (0x80, & system_call) (see include/asm/system. h). Set the Soft Interrupt Service Program 0x80 to system_call (see

Arch/i386/kernel/entry. S), system. call is the total entry point for all system calls.

(4) How does the kernel call services for various systems?

When a process needs to be called, a system call command must be written in the form of a C language function. If the command already exists in a header file

Is expanded by the corresponding _ syscallN (), the user program must contain this file. When a process is executed to a user program's system call command

The function expanded by macro command _ syscallN. The system call parameters are transmitted by common registers, and then INT 0x80 is executed.

The entry address is system_call.

(5) ret_from_sys_call

The assembler section at the ret_from_sys_call entry plays an important role in linux Process Management. All system calls are directed to this entry address before the end of the call and before most services are interrupted are returned. This program is not only a system call service, but also handles interrupt nesting, CPU scheduling, signals, and other transactions.

2. Add a system call by modifying the kernel source code

Through the above analysis of the linux System Call process,

Adding your system call to the kernel is easy. The following describes an actual system call,

And add it to the kernel. The system call to be added is inttestsyscall (). Its function is to display hello world on the control terminal screen,

0 is returned after successful execution.

1. Compile inttestsyscall () system call

Writing a system call means adding a function to the kernel and placing the new function into the file kernel/sys. c. The new function code is as follows:

Asmlingkage sys_testsyscall ()

{Console_print ("hello world \ n ");

Return 0;

}

2. connect to a new system call

After writing a new system call process, the next task is to let the rest of the kernel know the existence of this program, and then re-build the kernel that contains the new system call. To connect a new function to an existing kernel, You need to edit two files:

1). Add inculde/asm/unistd. h to this file

# Define_NR_testsyscall 191

2). are/i386/kernel/entry. s is used to initialize the pointer array. Add a line in this file:

. Long SYMBOL_NAME (_ sys_tsetsycall)

Change the. rept NR_syscalls-190 to NR_SYSCALLS-191, then rereward and run the new kernel.

[1] [2] Next page

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.