How to add new system calls in Linux

Source: Internet
Author: User

A system call is a functional interface between an application and the operating system kernel. The main purpose is to make the user
You can use the relevant device management, input/input systems, file systems and process control provided by the operating system,
Communication and storage management, without having to understand the internal structure of the system program and related hardware details
To reduce user burden, protect the system, and improve resource utilization.

As a representative of Free Software, the Linux operating system has become more and more widely used due to its excellent performance,
Not only have they been recognized by professionals, but commercial applications are also in full swing. In Linux, most
System calls are included in the Linux libc library, which can be called through standard C function call methods.
Call. So, for Linux enthusiasts, how can we add new system calls in Linux?

1. Linux System Call Mechanism

In Linux, system calling is implemented as an exception type. It will execute the corresponding Machine
Code command to generate abnormal signals. The system automatically switches the user State
Core State to process it. This means that when the system calls an abnormal command, the system is automatically switched.
As the core state, and arrange the execution of the exception handling program.

The actual commands used by Linux to implement system call exceptions are:

Int timeout $0x80

This command transfers control to the kernel using the interrupt/exception vector number 128 (that is, the hexadecimal 80. Is
You do not need to use machine instructions for programming when using system calls. In the Standard C language library
Provides a short subroutine to program the machine code. In fact, the machine code segment is very simple
Short. All it has to do is load the parameters sent to the system call to the CPU register, and then execute
Int timeout $0x80 command. Then run the system call. The return value of the system call is sent to a register of the CPU.
, The standard library subroutine gets this return value and sends it back to the user program.

To make the execution of system calls a simple task, Linux provides a set of pre-processing macro commands.
They can be used in programs. These macro commands take certain parameters and extend them to call the specified system call.
.

These macro commands have a name format similar to the following:

_ Syscalln (parameters)

N is the number of parameters required for system calls, while parameters is replaced by a set of parameters. These parameters
The number of macro commands enables expansion of specific system calls. For example, to establish a call to the setuid () System
The following functions should be used for unified calling:

_ Syscall1 (INT, setuid, uid_t, UID)

The Int value of the 1st parameter in the syscalln () macro command indicates that the type of the returned value of the generated function is integer.
Type. The setuid parameter indicates the name of the generated function. Each parameter required by the system call
Number. The macro command is followed by two parameters uid_t and uid respectively to specify the parameter type and name.

In addition, there is a limit on the data types used as system call parameters, and their capacity cannot exceed four
Bytes. This is because when the int running $0x80 command is executed for system calling, all parameter values have 32-bit
CPU registers. Another restriction imposed by passing parameters using CPU registers can be transferred to the system for calling.
The number of parameters. This limit allows up to five parameters to be passed. Therefore, Linux defines six different
_ Syscalln () macro command, from _ syscall0 (), _ syscall1 () to _ syscall5 ().

Once the _ syscalln () macro command is extended with the corresponding parameters called by a specific system
If it is a function with the same name as the system call, it can execute this system call in the user program.

 

2. Add a new system call

If you add a new system call in Linux, follow these steps to add the call successfully.
The following steps describe how to add a system call.

(1) Add source code

The first task is to write the source program added to the kernel, which will be added to a kernel file.
Function. The name of the function should be added with the Sys _ sign before the new system call name. Assume that the newly added System
The call is mycall (INT number). Add the source code to the/usr/src/Linux/kernel/sys. c file.
Code, as follows:

Asmlinkage int sys_mycall (INT number)

{

Return number;

}

As a simple example, the newly added system call only returns an integer value.

(2) connect to a new system call

After a new system call is added, the next task is to make the rest of the Linux kernel know the memory of the program.
In. To add a connection to a new function from an existing kernel program, you need to edit two files.

In our Linux kernel version (RedHat 6.0, the kernel is 2.2.5-15), the first
The modified file is:

/Usr/src/Linux/include/asm-i386/unistd. h

This file contains the system call list, which is used to assign a unique number to each system call. Text
The format of each line is as follows:

# DEFINE _ nr_name NNN

The name is replaced by the system call name, while the NNN is the number corresponding to the system call. You should set
Add the new system call name to the end of the list and assign it the next available system call in the number sequence.
. Our system calls are as follows:

# DEFINE _ nr_mycall 191

System Call number is 191, the reason the system call number is 191, because the Linux-2.2 kernel itself
The Unified Call number 190 is used.

The second file to be modified is:

/Usr/src/Linux/ARCH/i386/kernel/entry. s

This file contains a list similar to the following:

. Long symbol_name ()

This list is used to initialize the sys_call_table [] array. This array contains each
System Call pointer. In this way, the new kernel function pointer is added to the array. We add at the end of the list
Add a row:

. Long symbol_name (sys_mycall)

    

(3) Rebuilding the Linux Kernel

To make the new system call take effect, you need to re-build the Linux kernel. This requires login as a Super User
.

# Pwd
/Usr/src/Linux
#

Super Users can reconstruct the kernel only in the current working directory (/usr/src/Linux.

# Make config
# Make Dep
# Make clearn
# Make bzimage

After compilation, the system generates a kernel image file that can be installed and compressed:

/Usr/src/Linux/ARCH/i386/boot/bzimage

(4) Start the system with a new kernel

To use a new system call, you must use a new kernel to reboot the system. Therefore, you must repair
Change the/etc/Lilo. conf file. In our system, the file content is as follows:

Boot =/dev/hda
Map =/boot/Map
Install =/boot. B
Prompt
Timeout = 50

Image =/boot/vmlinuz-2.2.5-15
Label = Linux
Root =/dev/hdb1
Read-Only

Other =/dev/hda1
Label = DoS
Table =/dev/had

First, edit the file and add a new boot kernel:

Image =/boot/bzimage-New
Label = Linux-New
Root =/dev/hdb1
Read-Only

After adding the file, the content of the file is as follows:

Boot =/dev/hda
Map =/boot/Map
Install =/boot. B
Prompt
Timeout = 50

Image =/boot/bzimage-New
Label = Linux-New
Root =/dev/hdb1
Read-Only

Image =/boot/vmlinuz-2.2.5-15
Label = Linux
Root =/dev/hdb1
Read-Only

Other =/dev/hda1
Label = DoS
Table =/dev/hda

In this way, the new kernel image bzimage-New becomes the default boot kernel.

To use the new Lilo. conf configuration file, run the following command:

# Cp/usr/src/Linux/ARCH/i386/boot/zimage/boot/bzimage-New

Configure lilo:

#/Sbin/lilo

Now, when you reboot the system, there are three options after the boot: Prompt: Linux-new,
Linux and DOS, the new kernel becomes the default boot kernel.

Now, the new Linux kernel has been created and the new system call has become part of the operating system.
Minute, restart Linux, you can use the system to call in the application.

(5) use a new system call

Use the newly added system to call mycall in the application. For the same purpose, we wrote
A simple example is xtdy. C.

/* Xtdy. C */

# Include

_ Syscall1 (INT, mycall, Int, RET)

Main ()

{

Printf ("% d N", mycall (100 ));

}

Compile the program:

# Cc-O xtdy. c

Run:

# Xtdy

Result:

#100

Note: because the system call is used, the user should be a Super User during program compilation and execution.
Copies.

Reference: http://www.chinalinuxpub.com/read.php? WID = 23

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.