......
 # system call handler stub
ENTRY(system_call)
 RING0_INT_FRAME # can't unwind into user space anyway
 pushl %eax # save orig_eax
 CFI_ADJUST_CFA_OFFSET 4
 SAVE_ALL
 GET_THREAD_INFO(%ebp)
     # system call tracing in operation / emulation
 testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
 jnz syscall_trace_entry
 cmpl $(nr_syscalls), %eax
 jae syscall_badsys
syscall_call:
 call *sys_call_table(,%eax,4) //此处执行相应的系统调用
 movl %eax,PT_EAX(%esp) # store the return value
syscall_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_IRQS_OFF
 movl TI_flags(%ebp), %ecx
 testl $_TIF_ALLWORK_MASK, %ecx # current->work
 jne syscall_exit_work
 ......
$ cat ./arch/x86/include/asm/unistd.h #ifdef __KERNEL__ # ifdef CONFIG_X86_32 # include "unistd_32.h" # else # include "unistd_64.h" # endif #else # ifdef __i386__ # include "unistd_32.h" # else # include "unistd_64.h" # endif #endif
# cat arch/x86/include/asm/unistd_32.h #ifndef _ASM_X86_UNISTD_32_H #define _ASM_X86_UNISTD_32_H /* * This file contains the system call numbers. */ #define __NR_restart_syscall 0 #define __NR_exit 1 #define __NR_fork 2 #define __NR_read 3 #define __NR_write 4 #define __NR_open 5 #define __NR_close 6 #define __NR_waitpid 7 #define __NR_creat 8 #define __NR_link 9 #define __NR_unlink 10 #define __NR_execve 11 #define __NR_chdir 12 #define __NR_time 13 #define __NR_mknod 14 #define __NR_chmod 15 #define __NR_lchown 16 #define __NR_break 17 #define __NR_oldstat 18 #define __NR_lseek 19 #define __NR_getpid 20 #define __NR_mount 21 ......
ENTRY(sys_call_table) .long sys_restart_syscall /* 0 - old "setup()" system call, used for restarting */ .long sys_exit .long ptregs_fork .long sys_read .long sys_write .long sys_open /* 5 */ .long sys_close .long sys_waitpid .long sys_creat .long sys_link .long sys_unlink /* 10 */ .long ptregs_execve ...... .long sys_timerfd_settime /* 325 */ .long sys_timerfd_gettime .long sys_signalfd4 .long sys_eventfd2 .long sys_epoll_create1 .long sys_dup3 /* 330 */ .long sys_pipe2 .long sys_inotify_init1 .long sys_preadv .long sys_pwritev .long sys_rt_tgsigqueueinfo /* 335 */ .long sys_perf_event_open
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
    pid_t   pid;
    asm volatile (
            "mov $0, %%ebx\n\t"
            "mov $20, %%eax\n\t"    //把系统调用号20放入eax寄存器中,20对应于SYS_getpid()系统调用
            "int $0x80\n\t"    //0x80中断
            "mov %%eax, %0\n\t"    //将执行结果存放在pid变量中
            :"=m"(pid)
            );
    printf("int PID: %d\n", pid);
    printf("api PID: %d\n", getpid());
    return 0;
}#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
int main(int argc, const char *argv[])
{
    pid_t     pid, pidt;
    pid = getpid();
    pidt = syscall(SYS_getpid);
    printf("getpid: %d\n", pid);
    printf("SYS_getpid: %d\n", pidt);
    return 0;
}系统调用在内核有一个实现函数,以getpid为例,其在内核实现为:/**
 * sys_getpid - return the thread group id of the current process
 *
 * Note, despite the name, this returns the tgid not the pid.  The tgid and
 * the pid are identical unless CLONE_THREAD was specified on clone() in
 * which case the tgid is the same in all threads of the same group.
 *
 * This is SMP safe as current->tgid does not change.
 */
SYSCALL_DEFINE0(getpid)
{
 return task_tgid_vnr(current);
}
asmlinkage long sys_getpid(void)
{
return current->tpid;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/shallnet/article/details/47113753