网络套接字系统调用如何到达sys_socketcall

描述

套接字系统调用如何到达sys_socketcall

accept函数为例

系统调用中参数从用户态向内核态的传递是通过寄存器完成的,eax表示系统调用,ebx表示第一个参数,ecx表示第二个参数,edx表示第三个参数(主要针对socke.S)

第一层:accept.s文件

(glibc函数库)sysdepsunissysvlinuxaccept.S

#define socket accept    //将socket定义为accept
#define _socket _libc_accept
#define NARGS 3           //系统调用的个数
#include < socket.S >     //这几个套接字函数的通用实现

第二层:socket.S文件

#include < sysdep.h >
#include < sys/socketcall.h >

#define P(a, b) P2(a, b)
#define P2(a, b) a##b

.text
/* The socket-oriented system calls are handled unusally in Linux.
  They are all gated through the single `socketcall' system call number.
  `socketcall' takes two arguments: the first is the subcode, specifying
  which socket function is being called; and the second is a pointer to
  the arguments to the specific function.

  The .S files for the other calls just #define socket and #include this. */

.globl P(__,socket)
ENTRY (P(__,socket))

/* Save registers. */
movl %ebx, %edx

movl $SYS_ify(socketcall), %eax/* System call number in %eax. */
//展开$SYS_iff()宏
/* Use ## so `socket' is a separate token that might be #define'd. */
movl $P(SOCKOP_,socket), %ebx/* Subcode is first arg to syscall. */
lea 4(%esp), %ecx/* Address of args is 2nd arg. */

       /* Do the system call trap. */
int $0x80

/* Restore registers. */
movl %edx, %ebx

/* %eax is < 0 if there was an error. */
cmpl $-125, %eax
jae syscall_error

/* Successful; return the syscall's value. */
ret

PSEUDO_END (P(__,socket))
c
weak_alias (P(__,socket), socket)

重点看:

movl $SYS_ify(socketcall), %eax /* System call number in %eax. */

展开SYS_iff()宏 (glibc函数库: sysdepsunixsysdep.h)

#ifdef __STDC__
#define SYS_ify(syscall_name) SYS_##syscall_name
#else
#define SYS_ify(syscall_name) SYS_/**/syscall_name
#endif

预处理后为:

movl $SYS_socketcall,%eax
//将系统调用号放入eax中,但是对于网络套接字所有的接口函数而言,他们的共同入口函数是sys_socketcall
//在内核中,总入口函数sys_socketcall的系统调用号为_NR_socketcall,定义在include/linux/unistd.h
#define __NR_socketcall102
//在glib函数库中,有如下的预定义
#define SYS_socketcall _NR_socketcall

所以:

movl $SYS_socketcall,%eax
//将sys_socketcall函数对应的系统调用号赋予eax寄存器,从而使套接字系统调用进入到正常的入口函数中

这样套接字系统调用进入到正确的函数中了。

那么第一个参数是识别系统调用的具体函数的,这个参数在socket.S(glibc库)中:

movl $P(SOCKOP_,socket), %ebx
//预处理后:
movl $SOCKOP_accept,%ebx//这就完成了第一个参数的传递

关于SOCKOP_accept:

查看(glibc库:sysdepsunixsysvlinuxsocketcall.h)

#ifndef _SYS_SOCKETCALL_H

#define _SYS_SOCKETCALL_H	1

/* Define unique numbers for the operations permitted on socket.  Linux
   uses a single system call for all these functions.  The relevant code
   file is /usr/include/linux/net.h.
   We cannot use a enum here because the values are used in assembler
   code.  */

#define SOCKOP_socket		1
#define SOCKOP_bind		2
#define SOCKOP_connect		3
#define SOCKOP_listen		4
//可以看到SOCKOP_accept对应的值为5
#define SOCKOP_accept		5
#define SOCKOP_getsockname	6
#define SOCKOP_getpeername	7
#define SOCKOP_socketpair	8
#define SOCKOP_send		9
#define SOCKOP_recv		10
#define SOCKOP_sendto		11
#define SOCKOP_recvfrom		12
#define SOCKOP_shutdown		13
#define SOCKOP_setsockopt	14
#define SOCKOP_getsockopt	15
#define SOCKOP_sendmsg		16
#define SOCKOP_recvmsg		17

#endif /* _SYS_SOCKETCALL_H */

在linux内核(include/linux/net.h):

#ifndef _SYS_SOCKETCALL_H

#define _SYS_SOCKETCALL_H	1

/* Define unique numbers for the operations permitted on socket.  Linux
   uses a single system call for all these functions.  The relevant code
   file is /usr/include/linux/net.h.
   We cannot use a enum here because the values are used in assembler
   code.  */

#define SOCKOP_socket		1
#define SOCKOP_bind		2
#define SOCKOP_connect		3
#define SOCKOP_listen		4
//这与glibc库中的SOCKOP_accept对应的值相同
#define SOCKOP_accept		5
#define SOCKOP_getsockname	6
#define SOCKOP_getpeername	7
#define SOCKOP_socketpair	8
#define SOCKOP_send		9
#define SOCKOP_recv		10
#define SOCKOP_sendto		11
#define SOCKOP_recvfrom		12
#define SOCKOP_shutdown		13
#define SOCKOP_setsockopt	14
#define SOCKOP_getsockopt	15
#define SOCKOP_sendmsg		16
#define SOCKOP_recvmsg		17

#endif /* _SYS_SOCKETCALL_H */

第二个参数

在socket.S中:以指针的方式设置了sys_socketcall的第二个参数

lea 4(%esp), %ecx		/* Address of args is 2nd arg.  */

设置完以上的系统调用号还有参数后进入软中断:int $0x80,进入了内核态进行处理

第三层:entry.S文件

_system_call:
	pushl %eax			# save orig_eax
	SAVE_ALL
	movl $-ENOSYS,EAX(%esp)
	cmpl $(NR_syscalls),%eax
	jae ret_from_sys_call
    /*对应上面,未调用软中断之前,eax寄存器中被初始化为系统调用号,即_NR_socketcall,
       这个调用号别用于在_sys_call_table数组中进行函数指针的寻址,将对应的函数地址对eax寄存器赋值      */
	movl _sys_call_table(,%eax,4),%eax
	testl %eax,%eax
	je ret_from_sys_call
	movl _current,%ebx
	andl $~CF_MASK,EFLAGS(%esp)	# clear carry - assume no errors
	movl $0,errno(%ebx)
	movl %db6,%edx
	movl %edx,dbgreg6(%ebx)  # save current hardware debugging status
	testb $0x20,flags(%ebx)		# PF_TRACESYS
	jne 1f
    /*上面给eax复制后,这里进行调用该函数,到这为止,accept系统调用将请求传递给了sys_socketcall,
        然后将socket.S中设置的参数传递给sock_accept函数,就完成了应用层接口函数accept到BSD socket层函数的请求传递工作
        */
	call *%eax
	movl %eax,EAX(%esp)		# save the return value
	movl errno(%ebx),%edx
	negl %edx
	je ret_from_sys_call
	movl %edx,EAX(%esp)
	orl $(CF_MASK),EFLAGS(%esp)	# set carry to indicate error
	jmp ret_from_sys_call
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分