The purpose of this experiment : to trace the process of system call by a simple menu applet, analyze and summarize the mechanism of system call and the process of three-layer entry.
Experimental principle : system call processing process and interrupt processing mechanism
The system call is implemented through the soft interrupt instruction int 0x80, and this INT 0x80 directive is encapsulated in the function of the C library. (soft interrupts differ from what we often call hard interrupts in that soft interrupts are triggered by instructions, not by hardware peripherals.) INT 0x80 The execution of this instruction will cause the system to jump to a preset kernel space address, which points to the system call handler, the System_call function.
How does the System_call function find a specific system invocation service routine? Find system Call Table sys_call_table! by system call number When the soft interrupt instruction int 0x80 executes, the system call number is placed in the EAX register, the System_call function can read the EAX register fetch, multiply it by 4, generate an offset address, and then use sys_call_table as base address, base address with offset, You can get the address of the specific system call service routine! Then the system invokes the service routine. It is necessary to note that the system invoke service routine takes only the parameters from the stack, so the parameters are stored in the registers before the System_call executes, and the registers are first pressed into the stack when System_call executes. After the system_call exits, the user can obtain (modified) parameters from the register.
Experimental steps:
1. Use GDB to set breakpoints at Sys_time and list the code found
2. Tracking breakpoints with S (step)
3. When entering the System_call, GDB can not continue tracking, the experiment is over, find the source code for analysis
Second, the work process of System_call corresponding assembly code
1. The library function triggers the interrupt and gives the system call number; 2. The operating system finds the corresponding interrupt handler function through the interrupt descriptor:
So we see: ENTRY (System_call)
Further find the corresponding macro definition:/linux-3.18.6/include/linux/linkage.h
# define entry (name) \
. globl name asm_nl \
align asm_nl \
Name
ENTRY (System_call)
Ring0_int_frame # can't unwind into user space anywayAsm_clac PUSHL_CFI%eax # Save Orig_eax Save_all # Saved live Get_thread_info (%EBP) # system call TracinginchOperation/emulation Testl $_tif_work_syscall_entry,ti_flags (%ebp) jnz syscall_trace_entry Cmpl $ (nr_syscalls),%eax Jae Syscall_badsyssyscall_call:call*sys_call_table (,%eax,4# This will actually call the corresponding system call, and the meaning of the call number here also shows the Sys_call_table +4* %EAX is the address of the system call SYSCALL_AFTER_CALL:MOVL%eax,pt_eax (%ESP) # Store thereturnvaluesyscall_exit:lockdep_sys_exit disable_interrupts (clbr_any) # Make sure we don'T miss an interrupt# setting need_resched or Sigpending # between sampling and the Iret trace_i Rqs_off movl Ti_flags (%EBP),%ecx testl $_tif_allwork_mask,%ECX # current->Work jne syscall_exit_work restore_all:trace_irqs_iretrestore_all_notrace: #ifdef config_x86_espfix32 movl PT _eflags (%ESP),%EAX # Mix eflags, SS and CS # WARNING:PT_OLDSS (%ESP) contains the wrong/random valuesifWe # is returning to the kernel. # See commentsinchProcess.c:copy_thread () fordetails. Movb Pt_oldss (%ESP),%Ah Movb pt_cs (%ESP),%al Andl $ (X86_EFLAGS_VM| (Segment_ti_mask <<8) | Segment_rpl_mask),%eax Cmpl $ (Segment_ldt<<8) | USER_RPL),%eax cfi_remember_state JE ldt_ss # returning to User-space with LDT SS#endifRestore_nocheck:restore_regs4# Skip Orig_eax/Error_codeirq_return:interrupt_return
Linux kernel Analysis Week fifth-analyzing the system_call interrupt processing process