Adding system calls to the Linux-3.10.1 kernel

Source: Internet
Author: User
Tags bz2

Reproduced from the spiritual Leap cloud: the original link

1. Write in front

A system call is a set of "special" interfaces that the operating system provides to user program calls. Through this set of "special" interface, the user program can obtain the services provided by the operating system kernel, such as file system related system calls to provide open files, close files or read and write file services, clock-related system calls to provide access to system time, set the system time service.

Logically, a system call can be seen as an interface between a kernel and a user-space program-like an intermediary that communicates the request of a user process to the kernel, and then sends the processing results back to the user process after the kernel has finished processing the request.

A typical invocation procedure for a system call is shown.

The role of the system call:

The fundamental reason why system services need to be made available to user space through system calls is to "protect" the system. We know that operating system running space is divided into kernel space and user space, they run at different levels, logically isolated from each other, so the user process normally does not allow access to the kernel data, do not allow the use of kernel functions, they can only manipulate user data in user space, call user space functions. However, in many cases, the user process needs to obtain system services (call System program), then must take advantage of the system to provide users with the "special" interface-system call, which is specific to specify the user process into the kernel location, that is, the user access to the kernel path is pre-defined, only from the specified location into the kernel, Without allowing arbitrary jumps into the kernel. With the above-mentioned unified access path limit, we can guarantee the kernel security of the system effectively. We can visually describe the mechanism: as a tourist, you can buy tickets to enter the safari park, but you must be honest in the sightseeing car, according to the prescribed Route sightseeing tour. Of course, don't get off the bus, because that's too dangerous, not to let you lose your life, is to scare the wild animals.

In the previous article "Linux-3.10.1 Kernel Compilation and Installation" (see http://www.lingyuecloud.com/Index/details/id/58.html), we know that using the Linux kernel's open source features, Linux systems can be customized for development, compilation, and installation. For example, by modifying the Linux kernel source code, we have customized a new feature, so how do you use this new feature in your application? To do this, we also need to bridge the "application" with "new features added to the kernel"-a system call that connects both.

Suppose we have obtained the Linux-3.10.1 source package linux-3.10.1.tar.bz2, unzip the source package to get linux-3.10.1 folder, and switch to the folder (how to get the package linux-3.10.1.tar.bz2?). How to unzip? See http://www.lingyuecloud.com/index/details/id/58.html--"Linux-3.10.1 kernel compilation and Installation"). Next, the Spirit Leap Desktop cloud will take the Linux-3.10.1 kernel as an example, detailing the process of adding a new system call to the kernel, and all of the following are done under the linux-3.10.1 directory.

2. Environmental statement

Note: In the following description, all operations involved in the server Ubuntu 12.04 operating system environment are logged in and executed as root.

3. Add a system call to the Linux-3.10.1 kernel

3.1 System call number

The purpose of the Linux system call is to index the value of its values as a subscript in the system call table during the system call, resulting in the address of the function that processes the system call.

Each system call has a unique system call number, and the application can invoke the specified system call through the system call number.

3.2 System Call Table

As with SSDT in Windows systems (System Services Descriptor table), the Linux system call table retains the entry address of the function that handles each system call, which is actually a two-dimensional array of pointers [X][y], X represents the system call number, and Y represents the entry address of the system call function.

3.3 Adding system calls Lingyuecloudsyscall

1) Add custom system call source code

Add the implementation function for the custom system call Lingyuecloudsyscall in the sys.c file under the "Linux-3.10.1/kernel" directory.

Open the Sys.c file and add the following sample code:

#vim KERNEL/SYS.C

Asmlinkage long Sys_lingyuecloudsyscall (int number) {
PRINTK ("Hello lingyuecloud! Call number is%d\n ", number);
return number;

}

2) Modify the system call table

The system call table file is in the Syscall_64.tbl file in the "Linux-3.10.1/arch/x86/syscalls" directory.

Open Syscall_64.tbl and add a new system call pointer, as follows:

#vim ARCH/X86/SYSCALLS/SYSCALL_64.TBL

314 Lingyuecloudsyscall Sys_ Lingyuecloudsyscall;

As shown in the following:

Where 314 is the system call number for Lingyuecloudsyscall, the application can invoke the Lingyuecloudsyscall system call through this call number, or use a different system call number, but be careful not to duplicate the existing system call number. 64 is suitable for 64-bit system kernel environment, relevant description details can be consulted Https://en.wikipedia.org/wiki/X32_ABI.

3) Add a function declaration for system call Lingyuecloudsyscall

Add a function declaration to the syscalls.h file in the "linux-3.10.1/include/linux/" directory.

Open Syscalls.h and add the following in the penultimate line:

#vim Include/linux/syscalls.h

Asmlinkage long sys_lingyuecloudsyscall (int num);

As shown in the following:

4) Recompile

5) kernel

The compilation process is described in http://www.lingyuecloud.com/index/details/id/58.html--"Linux-3.10.1 kernel compilation and Installation".

6) test system call

Write the test program lingyuecloud_test.c:

#include <stdio.h>

#include <linux/unistd.h>

#include <sys/syscall.h>

int main ()

{

Long A;

A = Syscall (314,100); Call No. 314 system call, Sys_lingyuecloudcall ();

printf ("The number is%d\n", a);

return 0;

}

To compile the test program:

#gcc-O lingyuecloud lingyuecloud_test.c

The test results are as follows:

If you can see the above results, your first custom system call has been added successfully.

Adding system calls to the Linux-3.10.1 kernel

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.