Use Linux kernel commands

Source: Internet
Author: User
Article title: kernel commands called by 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.

Level: Intermediate

M. Tim Jones (mtj@mtjones.com), consultant engineer, Emulex

April 17, 2007

Linux®System call-we use them every day. But do you know how system calls are executed between the user space and the kernel? This article will explore the Linux System Call Interface (SCI), learn how to add new system calls (and other methods to implement this function), and introduce some SCI-related tools.

System callIt is an interface between the user space application and the services provided by the kernel. Because services are provided in the kernel, they cannot be directly called. On the contrary, you must use a process to span the boundaries between user space and the kernel. The methods for implementing this function in a specific architecture are different. Therefore, this article will focus on the most common architecture-i386.

In this article, I will explore Linux SCI and demonstrate how to add a system call to kernel 2.6.20 and then use this function from user space. We will also study some functions that are useful for system call development and other options for system call. Finally, we will introduce some auxiliary mechanisms related to system calls, such as tracking the usage of system calls in a process.

SCI

The implementation of system calls in Linux varies according to different architectures, and may vary even in a given physical architecture. For example, early x86 processors used an interrupt mechanism to migrate from user space to kernel space, but the new IA-32 processor provided instructions to optimize the conversion (usingsysenterAndsysexitCommand ). Due to the existence of a large number of methods, the final result is also very complex, so this article will focus on the surface discussion of interface details.

To improve the Linux SCI, you do not need to fully understand the internal principles of SCI. Therefore, I will use a simple system call process (see figure 1 ). Each system call is sent to the kernel through a single entry point. The eax register is used to identify a system call that should be called.CLibrary (each call from the user space application ). When the systemCWhen the database calls indexes and parameters, it calls a software interrupt (0x80 interrupt), which will executesystem_callFunction (through the interrupt handler), which processes all system calls according to the identifier in the eax content. After several simple testssystem_call_tableAnd eax indexes to execute real system calls. After the result is returned from the system call, it is finally executed.syscall_exitAnd callresume_userspaceReturns the user space. Then proceedCLibrary, it will return to the user application.


Figure 1. simplified system call process using the interrupt method

The core of SCI is the system calling of multi-path decomposition tables. As shown in Table 2, The eax index is used to determine which system call to call in the table (sys_call_table). The figure also shows some examples of the table content and the location of the content. (For more information about multi-path decomposition, see "system call multi-path decomposition ")


Figure 2. system call tables and various links
 

Add a Linux system call

Multi-Channel decomposition of system calls

Some system calls are further decomposed by the kernel. For example, BSD (Berkeley Software Distribution) socket call (socket,bind,connectAnd a separate system call Index (__NR_socketcall) Is associated, but the kernel will perform multi-path decomposition and use another parameter to make appropriate calls. See./linux/net/socket. csys_socketcallFunction.

Adding a new system call mainly involves some procedural operations, but you should pay attention to several things. This section describes the construction of several system calls to demonstrate their implementation and use of user space applications.

To add a new system call to the kernel, you need to perform three basic steps:

  1. Add a new function.
  2. Update the header file.
  3. Update the system call table for this new function.

Note:This process ignores user space requirements. I will introduce it later.

The most common case is that you create a new file for your function. However, for simplicity, I add my new function to the existing source file. The first two functions shown in listing 1 are simple examples of system calls. Listing 2 provides a slightly complex function that uses pointer parameters.


List 1. simple kernel functions in the system call example

                asmlinkage long sys_getjiffies( void ){  return (long)get_jiffies_64();}asmlinkage long sys_diffjiffies( long ujiffies ){  return (long)get_jiffies_64() - ujiffies;}

 

[1] [2] [3] [4] 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.