To access the system call, the C library function is generally used. The function enters the kernel state through the Soft Interrupt number 0x80, calls the corresponding interrupt handler, and runs the interrupt code in the kernel state through passing parameters.
Generally, system calls are supported by the C library. You can use the system call by adding a standard header file and linking it to the C library. However, if it is a system call implemented at the underlying layer, the C library may not support it. Fortunately, Linux itself provides a set of macros for direct access to system calls. It sets the registers and calls the commands. These macros are _ syscalln (), where N ranges from 0 to 6, representing the number of parameters to be passed to the system call, this is because the macro must know how many parameters are pushed into the register in what order. For example, the C library has implemented the OPEN function. Here we implement the open system call function.
The system call definition of the open function is long open (const char * filename, int flags, int mode );
We use _ syscalln () to implement the following:
# Define nr_open 5
_ Syscall3 (long, open, const char *, filename, Int, flags, Int, Mode)
Int main (){
Int FD = open ("FILENAME", flags, mode );
Close (FD );
Return 0;
}
Close is not implemented here, so the C library is called, but the self-implemented OPEN function is called.