分类: LINUX
2009-05-11 18:49:40
关于系统调用
我来解释其中的一个吧
比如
/lib/write.c
他的实现如下
_syscall3(int,write,int,fd,const char *,buf,off_t,count)
而_syscall3是一个宏
#define _syscall3(type,name,atype,a,btype,b,ctype,c) \
type name(atype a,btype b,ctype c) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" ((long)(a)),"c" ((long)(b)),"d" ((long)(c))); \
if (__res>=0) \
return (type) __res; \
errno=-__res; \
return -1; \
}
所以_syscall3(int,write,int,fd,const char *,buf,off_t,count)
被展开后就是
int write(int fd, contst char *buf, off_t count){
.....
int 0x80
.....
}
所以也就是一个系统调用,当这个调用被执行时
会执行system_call.s的如下代码
call _sys_call_table(,%eax,4) //line94
而_sys_call_table是一个函数指针数组
他的内容存放在
/include/linux/sys.h
fn_ptr sys_call_table[] = { sys_setup, sys_exit, sys_fork, sys_read,
sys_write, sys_open, sys_close, sys_waitpid, sys_creat, sys_link,
sys_unlink, sys_execve, sys_chdir, sys_time, sys_mknod, sys_chmod,
sys_chown, sys_break, sys_stat, sys_lseek, sys_getpid, sys_mount,
sys_umount, sys_setuid, sys_getuid, sys_stime, sys_ptrace, sys_alarm,
sys_fstat, sys_pause, sys_utime, sys_stty, sys_gtty, sys_access,
sys_nice, sys_ftime, sys_sync, sys_kill, sys_rename, sys_mkdir,
sys_rmdir, sys_dup, sys_pipe, sys_times, sys_prof, sys_brk, sys_setgid,
sys_getgid, sys_signal, sys_geteuid, sys_getegid, sys_acct, sys_phys,
sys_lock, sys_ioctl, sys_fcntl, sys_mpx, sys_setpgid, sys_ulimit,
sys_uname, sys_umask, sys_chroot, sys_ustat, sys_dup2, sys_getppid,
sys_getpgrp, sys_setsid, sys_sigaction, sys_sgetmask, sys_ssetmask,
sys_setreuid,sys_setregid };
根据eax的内容来指向具体的中断函数,也就是sys_write。
sys_write的具体实现很好找了,就不必我说了,呵呵