sys_xxx
都是什么玩意儿?
sys_write的声明
所有的sys_xxx都在linux/include/linux/syscalls.h,例如sys_write
。
asmlinkage long sys_write(unsigned int fd, const char __user *buf, size_t count);
write的定义
定义不太容易直接找到,这里直接给出位置linux/fs/read_write.c。
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)
{
...
}
展开write定义
声明sys_write,却定义write,它们之间有什么联系呢?下面根据linux/include/linux/syscalls.h、linux/include/linux/stringify.h和linux/include/linux/linkage.h里面的宏对其进行展开。
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, size_t, count)
======>
SYSCALL_DEFINEx(3, _write, unsigned int, fd, const char __user *, buf, size_t, count)
======>
SYSCALL_METADATA(_write, 3, unsigned int, fd, const char __user *, buf, size_t, count) \
__SYSCALL_DEFINEx(3, sname, unsigned int, fd, const char __user *, buf, size_t, count)
defined(CONFIG_FTRACE_SYSCALLS) = 1 ======>
asmlinkage long sys_write(__MAP(3,__SC_DECL,unsigned int, fd, const char __user *, buf, size_t, count)) \
__attribute__((alias(__stringify(SyS_write)))); \
static inline long SYSC_write(__MAP(3,__SC_DECL,unsigned int, fd, const char __user *, buf, size_t, count)); \
asmlinkage long SyS_write(__MAP(3,__SC_LONG,unsigned int, fd, const char __user *, buf, size_t, count)); \
asmlinkage long SyS_write(__MAP(3,__SC_LONG,unsigned int, fd, const char __user *, buf, size_t, count)) \
{ \
long ret = SYSC_write(__MAP(3,__SC_CAST,unsigned int, fd, const char __user *, buf, size_t, count)); \
__MAP(3,__SC_TEST,unsigned int, fd, const char __user *, buf, size_t, count); \
__PROTECT(3, ret,__MAP(3,__SC_ARGS,unsigned int, fd, const char __user *, buf, size_t, count)); \
return ret; \
} \
static inline long SYSC_write(__MAP(3,__SC_DECL,unsigned int, fd, const char __user *, buf, size_t, count))
省略一些不关注内容======>
asmlinkage long sys_write(__MAP3(__SC_DECL,unsigned int, fd, const char __user *, buf, size_t, count)) \
__attribute__((alias("SyS_write"))); \
static inline long SYSC_write(__MAP3(__SC_DECL,unsigned int, fd, const char __user *, buf, size_t, count)); \
asmlinkage long SyS_write(__MAP3(__SC_LONG,unsigned int, fd, const char __user *, buf, size_t, count)); \
asmlinkage long SyS_write(__MAP3(__SC_LONG,unsigned int, fd, const char __user *, buf, size_t, count)) \
{ \
return SYSC_write(__MAP3(__SC_CAST,unsigned int, fd, const char __user *, buf, size_t, count)); \ \
} \
static inline long SYSC_write(__MAP3(__SC_DECL,unsigned int, fd, const char __user *, buf, size_t, count))
省略一些不关注内容======>
asmlinkage long sys_write(unsigned int fd, const char __user * buf, size_t count) \
__attribute__((alias("SyS_write"))); \
static inline long SYSC_write(unsigned int fd, const char __user * buf, size_t count); \
asmlinkage long SyS_write(unsigned int fd, const char __user * buf, size_t count); \
asmlinkage long SyS_write(unsigned int fd, const char __user * buf, size_t count) \
{ \
return SYSC_write(unsigned int fd, const char __user * buf, size_t count); \
} \
static inline long SYSC_write(unsigned int fd, const char __user * buf, size_t count)
加上函数体,最终结果就是
asmlinkage long sys_write(unsigned int fd, const char __user * buf, size_t count) \
__attribute__((alias("SyS_write"))); \
static inline long SYSC_write(unsigned int fd, const char __user * buf, size_t count); \
asmlinkage long SyS_write(unsigned int fd, const char __user * buf, size_t count); \
asmlinkage long SyS_write(unsigned int fd, const char __user * buf, size_t count) \
{ \
return SYSC_write(unsigned int fd, const char __user * buf, size_t count); \
} \
static inline long SYSC_write(unsigned int fd, const char __user * buf, size_t count)
{
......
}
因此实际上调用关系是sys_write(SyS_write)
=>SYSC_write
内核为什么要整这个套路呢?