Users can use system calls in two ways:
The first way is through the C library functions, including the encapsulation functions and other common functions in the C library called by the system. (such as Write,read,kill,mkdir functions)
The second way is to use the _syscall macro. The kernel before the 2.6.18 version is defined in the Include/asm-i386/unistd.h file with 7 _syscall macros, respectively:
_syscall0 (type,name)
_syscall1 (TYPE,NAME,TYPE1,ARG1)
_syscall2 (type,name,type1,arg1,type2,arg2)
_ Syscall3 (TYPE,NAME,TYPE1,ARG1,TYPE2,ARG2,TYPE3,ARG3)
_syscall4 (TYPE,NAME,TYPE1,ARG1,TYPE2,ARG2,TYPE3,ARG3, TYPE4,ARG4)
_syscall5 (TYPE,NAME,TYPE1,ARG1,TYPE2,ARG2,TYPE3,ARG3,TYPE4,ARG4,TYPE5,ARG5)
_syscall6 (type , NAME,TYPE1,ARG1,TYPE2,ARG2,TYPE3,ARG3,TYPE4,ARG4,TYPE5,ARG5,TYPE6,ARG6)
Here's the second way (in fact, the first way is essentially the second way, except that the C library function encapsulates it)
Let me give you an example.
The following figure, after the Testsyscall call, _syscall0 the return value int and function name to the macro definition. I'll talk about Syscall later.
Here's how to generate INT80 interrupts
The corresponding system call number is found according to the macro definition passed in the assembly, which has been generated by the assembly INT80 soft interrupt, switching to the kernel state. The system call function is then found in the corresponding kernel state by the call number in system_call_table according to the offset, such as open to find the Sys_open
Parameter passing:
The function needs to pass in several arguments, and the corresponding number of Syscalln () is called, where n ranges from 0 to 6. Represents the number of parameters that need to be passed to a system call because the macro must understand exactly how many parameters are pressed into the register in what order
For each macro, there are n parameters.
The first parameter corresponds to the return value type of the system call.
The second parameter is the name of the system call. After that, the type and name of each parameter are arranged in the order of the system call parameters.
(My understanding: No. 0, the 1th parameter is fixed, the value of the following 2-6 is the parameter.) )
As shown in the figure above, type is the return type, and name is the system call name. (That is, when the open function is called, it is combined in the assembly as _nr_open)