套接字系統(tǒng)調(diào)用如何到達sys_socketcall
accept函數(shù)為例
系統(tǒng)調(diào)用中參數(shù)從用戶態(tài)向內(nèi)核態(tài)的傳遞是通過寄存器完成的,eax表示系統(tǒng)調(diào)用,ebx表示第一個參數(shù),ecx表示第二個參數(shù),edx表示第三個參數(shù)(主要針對socke.S)
第一層:accept.s文件
(glibc函數(shù)庫)sysdepsunissysvlinuxaccept.S
#define socket accept //將socket定義為accept
#define _socket _libc_accept
#define NARGS 3 //系統(tǒng)調(diào)用的個數(shù)
#include < socket.S > //這幾個套接字函數(shù)的通用實現(xiàn)
第二層: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函數(shù)庫: sysdepsunixsysdep.h)
#ifdef __STDC__
#define SYS_ify(syscall_name) SYS_##syscall_name
#else
#define SYS_ify(syscall_name) SYS_/**/syscall_name
#endif
預(yù)處理后為:
movl $SYS_socketcall,%eax
//將系統(tǒng)調(diào)用號放入eax中,但是對于網(wǎng)絡(luò)套接字所有的接口函數(shù)而言,他們的共同入口函數(shù)是sys_socketcall
//在內(nèi)核中,總?cè)肟诤瘮?shù)sys_socketcall的系統(tǒng)調(diào)用號為_NR_socketcall,定義在include/linux/unistd.h
#define __NR_socketcall102
//在glib函數(shù)庫中,有如下的預(yù)定義
#define SYS_socketcall _NR_socketcall
所以:
movl $SYS_socketcall,%eax
//將sys_socketcall函數(shù)對應(yīng)的系統(tǒng)調(diào)用號賦予eax寄存器,從而使套接字系統(tǒng)調(diào)用進入到正常的入口函數(shù)中
這樣套接字系統(tǒng)調(diào)用進入到正確的函數(shù)中了。
那么第一個參數(shù)是識別系統(tǒng)調(diào)用的具體函數(shù)的,這個參數(shù)在socket.S(glibc庫)中:
movl $P(SOCKOP_,socket), %ebx
//預(yù)處理后:
movl $SOCKOP_accept,%ebx//這就完成了第一個參數(shù)的傳遞
關(guān)于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對應(yīng)的值為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內(nèi)核(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對應(yīng)的值相同
#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 */
第二個參數(shù)
在socket.S中:以指針的方式設(shè)置了sys_socketcall的第二個參數(shù)
lea 4(%esp), %ecx /* Address of args is 2nd arg. */
設(shè)置完以上的系統(tǒng)調(diào)用號還有參數(shù)后進入軟中斷:int $0x80
,進入了內(nèi)核態(tài)進行處理
第三層:entry.S文件
_system_call:
pushl %eax # save orig_eax
SAVE_ALL
movl $-ENOSYS,EAX(%esp)
cmpl $(NR_syscalls),%eax
jae ret_from_sys_call
/*對應(yīng)上面,未調(diào)用軟中斷之前,eax寄存器中被初始化為系統(tǒng)調(diào)用號,即_NR_socketcall,
這個調(diào)用號別用于在_sys_call_table數(shù)組中進行函數(shù)指針的尋址,將對應(yīng)的函數(shù)地址對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復(fù)制后,這里進行調(diào)用該函數(shù),到這為止,accept系統(tǒng)調(diào)用將請求傳遞給了sys_socketcall,
然后將socket.S中設(shè)置的參數(shù)傳遞給sock_accept函數(shù),就完成了應(yīng)用層接口函數(shù)accept到BSD socket層函數(shù)的請求傳遞工作
*/
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
-
寄存器
+關(guān)注
關(guān)注
31文章
5359瀏覽量
120827 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4344瀏覽量
62818 -
系統(tǒng)
+關(guān)注
關(guān)注
1文章
1018瀏覽量
21385
發(fā)布評論請先 登錄
相關(guān)推薦
評論