套接字系統調用如何到達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
-
寄存器
+關注
關注
31文章
5342瀏覽量
120297 -
函數
+關注
關注
3文章
4329瀏覽量
62588 -
系統
+關注
關注
1文章
1016瀏覽量
21338
發布評論請先 登錄
相關推薦
評論