Detailed system invocation principles

Source: Internet
Author: User

Reproduced:

First, what is a system call

In the world of Linux, we often encounter the term system invoke, called system calls, is the kernel provides a very powerful series of functions. These system calls are implemented in the kernel, and then the system is called to the user through a certain way, usually through the gate (Gate) into the (trap) implementation. A system call is an interface between a user-space application and a service provided by the kernel. Because the service is provided in the kernel, you cannot perform a direct call; instead, you must use a process to cross the boundaries between user space and the kernel. There are different ways to implement this functionality in a particular schema. Therefore, this article will focus on the most common architecture-- i386.  

Second, the function of system call

System calls play a huge role in Linux systems, and if there is no system call, the application loses its kernel support. Many of the functions we use in programming, such as Fork,Open, and so on, are eventually implemented in system calls, and here we talk about two functions, Fork and exit, both of which are functions in glibc. But if we follow the execution of the function, and look at the implementation of the glibc to the fork and exit functions, we can find that in the implementation code of the GLIBC, the soft interrupt is used to get into the kernel and to implement the function through the system call. The process we have in the implementation of the system call will be described in detail.   

Thus, the system call is the implementation of the user interface in the kernel, if there is no system call, the user can not take advantage of the kernel.  

The reality of system call and the process of invocation

Describe the system calls in detail before you talk about some of the Linux system protection mechanisms.   

The Linux system provides four privilege levels in the protected mode of the CPU, the current kernel only uses the two privilege levels, namely "privilege level 0" and "Privilege level 3", level 0 is what we usually speak of kernel mode, level 3 is what we usually say about user mode. Dividing these two levels is primarily to provide protection for the system. Kernel mode can perform some privileged instructions and enter user mode, while user mode cannot.

In particular, the kernel mode and user mode use their own stack, when the mode switch occurs at the same time to do the stack switch. Each process has its own address space (also known as process space), the address space of the process is divided into two parts: User space and system space, in user mode can only access the process's user space, in kernel mode can access the process's full address space, the address space is a logical address, Through the system section Surface management mechanism, access to the actual memory to do two-level address translation, namely: logical address, linear address, physical address.  

System invocation is equivalent to the function of the kernel, we are the key problem is the conversion from user mode to kernel mode, the switch of stack and the transfer of parameters.   

Iv. two ways of causing system calls

(1)int $0x80, the only way to cause system calls in older Linux kernel versions

(2)sysenter assembly Instructions

This article from csdn Blog, reproduced please indicate the source:http://blog.csdn.net/mlyy225/archive/2010/01/07/5148911.aspx

How to Add a new system call to Linux

A system call is a functional interface between the application and the operating system kernel. The main purpose is to enable users to use the operating system provided by the device management, input/input systems, file system and process control, communications, and storage management, without having to understand the internal structure of the system program and the details of the hardware, thereby reducing user burden and protecting the system and increasing resource utilization.
   Linux operating system as a representative of free software, its excellent performance makes it more widely used, not only by the professional affirmation, and commercial applications are in full swing. InIn Linux, most system calls are contained in theof LinuxLIBC Library, through the standardThe C function call method can invoke these system calls. So, yes.For Linux enthusiasts, how toAdd new system calls to Linux?
   1 Linux system call mechanism
InIn Linux systems, system calls are implemented as an exception type. It will execute the corresponding machine code instructions to generate an abnormal signal. The important effect of creating interrupts or anomalies is that the system automatically switches the user state to the core State to process it. This means that when the system invokes the exception instruction, the system is automatically switched to the kernel state and the execution of the exception handler is scheduled.
   The actual instructions that Linux uses to implement system call exceptions are:
   Int x80
This instruction uses an interrupt/anomaly Vector number128 (i.e.16-Binary80) Transfer control to the kernel. In order to achieve the use of system calls without machine instruction programming, the standardThe C language library provides a short subroutine for each system call, which completes the programming of the machine code. In fact, the machine code snippet is very brief. The only thing it has to do is load the parameters that are sent to the system call into theCPU registers, then executes theint x80 directive. Then the system call is run and the return value of the system call is fed into theIn a register of CPUs, the standard library subroutines program obtains this return value and sends it back to the user program.
To make the execution of a system call a simple task,Linux provides a set of pre-processing macro directives.
They can be used in programs. These macro directives take a certain parameter and then extend to the function called by the specified system.
These macro directives have a name format similar to the following:
   _syscalln (Parameters
whichn is the number of parameters required for the system call, andParameters is replaced by a set of parameters. These parameters enable the macro directive to complete an extension that is appropriate for a particular system call. For example, in order to establish a callSetuid () The function that the system calls, should use:
   _SYSCALL1 (IntSetuiduid_t,UID)
   The Syscalln () macro directive1 parametersint indicates that the type of return value of the resulting function is integer,2 parametersSetuid describes the name of the resulting function. This is followed by each parameter required by the system call. There are two parameters behind this macro directiveUid_t andThe UID is used to specify the type and name of the parameter, respectively.
In addition, the data types of the parameters used as system calls have a limit, and their capacity cannot exceed four bytes. This is because the executionInt? {GetProperty (Content)}x80 instruction makes system calls, all parameter values are presentof 32-bitThe CPU registers. UseAnother limitation brought by the CPU register pass parameter is the number of parameters that can be passed to the system call. This limit can be passed at most5 parameters. SoLinux defines altogether6 different_syscalln () macro command, from_syscall0 (),_SYSCALL1 () until_SYSCALL5 ().
OnceThe _syscalln () macro is extended with the appropriate parameters for a particular system call, and the result is a function with the same name as the system call, which can execute this system call in the user program.
   2 Adding a new system call
If the userTo add a new system call to Linux, you should follow several steps to add success, and the following steps detail how to add a system call.
(1) Add source code
The first task is to write the source program added to the kernel, a function that will be added to a kernel file, the name of the function should be the new system call name before addingSys_ logo. Assume that the newly added system call isMycall (int number), inAdd the source code to the/USR/SRC/LINUX/KERNEL/SYS.C file as follows:
   asmlinkage int Sys_mycall (int number)
   {
   return number;
   }
As a simple example, our newly added system call simply returns an integer value.
(2) Connect a new system call
After adding a new system call, the next task is to make theThe rest of the Linux kernel knows the existence of the program. To add a connection to a new function from an existing kernel program, you need to edit two files.
In our use of theLinux kernel version (RedHat 6.0, the kernel is2.2.5-15), the first file to be modified is:
   /usr/src/linux/include/asm-i386/unistd.h
The file contains a list of system calls that are used to assign a unique number to each system call. The format of each line in the file is as follows:
   #define __NR_NAME NNN
whichName is replaced with the system call names, andThe nnn is the corresponding number of the system call. The new system call name should be added to the end of the list and assigned to the next available system call number in the number sequence. Our system is called as follows:
   #define __nr_mycall 191
The system call number is191, the system call number is191, becauseThe system call number for the Linux-2.2 kernel itself has been used190.
The second file to be modified is:
   /usr/src/linux/arch/i386/kernel/entry. S
The file has a list similar to the following:
   . Long Symbol_name ()
The list is used toSys_call_table[] array is initialized. The array contains pointers to each system call in the kernel. This adds a pointer to the new kernel function in the array. We add a line at the end of the list:
   . Long Symbol_name (Sys_mycall)
(3) Reconstruction of newLinux kernel
To make the new system call take effect, you need to rebuildThe kernel of Linux. This needs to be logged in as Superuser.
   #pwd
   /usr/src/linux
   #
Super users in the current working directory (/usr/src/linux), the kernel can be rebuilt.
   #make Config
   #make DEP
   #make Clearn
   #make Bzimage
Once the compilation is complete, the system generates a compressed kernel image file that can be used for installation:
   /usr/src/linux/arch/i386/boot/bzimage
(4) boot the system with the new kernel
To use the new system call, you need to reboot the system with the newly rebuilt kernel. To do this, you need to modify/etc/lilo.conf file, in our system, the contents of the file are as follows:
   Boot=/dev/hda
   Map=/boot/map
   install=/boot/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
The contents of the file are as follows:
   Boot=/dev/hda
   Map=/boot/map
   install=/boot/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 imageThe bzimage-new becomes the default boot kernel.
In order to use the newlilo.conf configuration file, you should also execute the following command:
   #cp/usr/src/linux/arch/i386/boot/zimage/boot/bzimage-new
Next configurationLilo:
   #/sbin/lilo
Now, when the system is rebooted, theBoot: There are three options behind the prompt:Linux-new,LinuxDOS, the new kernel becomes the default boot kernel.
Thus, the newThe Linux kernel has been established, and the newly added system call has become part of the operating system, and restarting Linux will allow the user to use the system call in the application.
(5) using a new system call
Call Mycall with the newly added system in the application . Also for experimental purposes, we wrote a simple example of xtdy.c.
  #include
  _SYSCALL1 (Int,mycall,int,ret)
  Main ()
  {
  printf ("%d n", Mycall (100));
  }
Compile the program:
  # Cc-o Xtdy xtdy.c
Execution:
  # XTDY
Results:
  # 100
Note that the user should be superuser when compiling and executing the program due to the use of system calls

Http://blog.chinaunix.net/u2/62213/showart_488383.html

Linux adding system calls

I. SOURCE Modification

1 Download a kernel that is close to the kernel version of the system you are using, put it under/USR/SRC, unzip it, make a link ln-s linux-2.6.18.1 linux

2 Modification: Modification of three places

1)Add in/usr/src/linux/kerner/sys.c,

asmlinkage int Sys_mysyscall (int a)

{

return A;

}

2) define the system call number ,/usr/src/linux/include/asm-i386/unistd.h

#define _nr_sysmycall 318//cannot be duplicated with previous

#define _nr_syscalls 319//Modify the number of system calls used in the system

3) Add a custom system call function entry position in the system call vector table,

/usr/src/linux/arch/i386/kernel/syscall_table. S, previous old version is entry.s

. Long Sys_mysyscall

Second, kernel compilation

1. Under/ boot copy configuration file, to /usr/src/linux, rename config,make menuconfig, can not modify, direct exit

2.make Clean Clears the previous compilation traces

3.make, the bzimage is compiled.

4.make modules,make modules-install//Compile, install config configuration module

If you do not perform the second step, for some systems, can not make initrd files. The system will not start .

5. If the direct make install, the system will automatically create the initrd file, and copy the initrd and bzimage files to/ boot, modify grub.conf file, restart the system, choose to enter the new kernel

6. Do not use the Make install command. Copy bzimage to/ boot, rename vmlinuz-2.6.18.1, hand-crafted initrd file,/mkinitrd initrd-2.6.18.1.img 2.6.18.1, INITRD file Fame initrd-2.6.18.1

7. Modify the grub.conf file, copy the original boot settings, rename title and kernel and initrd to new production

Third, write code testing

int main (void)

{

int A=syscall (318,100);//318 is the system call number, and100 is the parameter

printf ("%d\n", a);

return 0;

}

Syscall is a function that the kernel provides as a user program,

If you do not use the Syscall function, you can also use the macro definition, but in the later version of 2.6.20, there is no macro definition, you need to copy from other versions to add.

Detailed system invocation principles

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.