Open up the user mode and kernel one of the series: How to user mode application execution system calls

Foreword

Before a period of time to get hold of the kernel, and some more recent development in C ++ in particular aspects of the application. The current daily work encounter some performance analysis, problem selection of different lock API, we found that due to the working principle behind the API user mode programs, particularly its relationship and calls the kernel, the kernel implementation process unclear, leading to pre expectations can not be analyzed. To this end, we intend to combine work problems encountered, such as:

  1. How user mode application execution system calls;
  2. What is the relationship and kernel mode and user mode lock locks the underlying implementation;
  3. Comparison of different user-mode lock (spin locks, read locks, lock condition) and non-locking mechanism of the approach theoretical analysis;
  4.  Libaio asynchronous IO in the kernel of the specific how to achieve;
  5.  System calls, thread scheduling, interrupt respectively on system performance, affect CPU consumption;

We hope that through these basic issues to sort out above, in isolation from one another to get through the mind of knowledge, helping them to establish a relatively close relationship.

ABC system calls

System call functionality

From the perspective of the operating system, the kernel system call provides the lowest level user mode program operating software or hardware interfaces. Linux system, each hardware system architecture has its own system call table, such as the system call table below mips:

 EXPORT(sys_call_table)
    PTR sys_read            /* 5000 */
    PTR sys_write
    PTR sys_open
    PTR sys_close
        ....
        PTR sys_statx
    PTR sys_rseq
    PTR sys_io_pgetevents
    .size   sys_call_table,.-sys_call_table

        ...
        NESTED(handle_sys64, PT_SIZE, sp)
#if !defined(CONFIG_MIPS32_O32) && !defined(CONFIG_MIPS32_N32)
    /*
     * When 32-bit compatibility is configured scall_o32.S
     * already did this.
     */
    .set    noat
    SAVE_SOME
    TRACE_IRQS_ON_RELOAD
    STI
    .set    at
        .....

        syscall_common:
    dsubu   t2, v0, __NR_64_Linux
    sltiu   t0, t2, __NR_64_Linux_syscalls + 1
    beqz    t0, illegal_syscall

    dsll    t0, t2, 3       # offset into table
    dla t2, sys_call_table
    daddu   t0, t2, t0
    ld  t2, (t0)        # syscall routine
    beqz    t2, illegal_syscall

    jalr    t2          # Do The Real Thing (TM)

Work process system calls

From the caller's point of view:

(1) operating specification reference system call parameters and data prepared;
(2) via a standard interface mode system call, the system call interface call;

From the perspective of the caller to see:

After (1) the processor state to state by the system user mode, a general call processing system hardware and kernel, namely the protection of the interrupted process is first CPU environment;

(2) Analysis of the type of system call, the system call into the appropriate processing routine;

(3) system call processing subroutine executed, the recovery process CPU or set new scene is interrupted, and then return Jiong interrupted process or a new process, continue down the implementation.

User mode application fast system call

For the current rapid development, you can quickly call by syscall, such as to achieve the following Libaio two quick calls:

/* Actual syscalls */
int io_setup(int maxevents, io_context_t *ctxp) {
    return syscall(_io_setup, maxevents, ctxp);
}

int io_destroy(io_context_t ctx) {
    return syscall(__io_destroy, ctx);
}

According to the above example, readers can call their own system calls.

Guess you like

Origin blog.51cto.com/xiamachao/2434444