Linux 在哪里可以找到系统调用源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10149621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:46:19  来源:igfitidea点击:

Where can I find system call source code?

clinuxassemblylinux-kernelsystem-calls

提问by Dr.Knowitall

In Linux where can I find the source code for all system calls given that I have the source tree? Also if I were to want to look up the source code and assembly for a particular system call is there something that I can type in terminal like my_system_call?

在 Linux 中,如果我有源代码树,我在哪里可以找到所有系统调用的源代码?另外,如果我想查找特定系统调用的源代码和程序集,是否可以在终端中键入一些内容 my_system_call

采纳答案by Daniel Kamil Kozar

You'll need the Linux kernel sources in order to see the actual source of the system calls. Manual pages, if installed on your local system, only contain the documentation of the calls and not their source itself.

您将需要 Linux 内核源代码才能查看系统调用的实际来源。手册页,如果安装在您的本地系统上,只包含调用的文档,而不包含它们的来源本身。

Unfortunately for you, system calls aren't stored in just one particular location in the whole kernel tree. This is because various system calls can refer to different parts of the system (process management, filesystem management, etc.) and therefore it would be infeasible to store them apart from the part of the tree related to that particular part of the system.

不幸的是,系统调用并不仅仅存储在整个内核树中的一个特定位置。这是因为各种系统调用可以引用系统的不同部分(进程管理、文件系统管理等),因此将它们与与系统特定部分相关的树部分分开存储是不可行的。

The best thing you can do is look for the SYSCALL_DEFINE[0-6]macro. It is used (obviously) to define the given block of code as a system call. For example, fs/ioctl.chas the following code :

你能做的最好的事情就是寻找SYSCALL_DEFINE[0-6]宏。它用于(显然)将给定的代码块定义为系统调用。例如,fs/ioctl.c有以下代码:

SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
{
/* do freaky ioctl stuff */
}

Such a definition means that the ioctlsyscall is declared and takes three arguments. The number next to the SYSCALL_DEFINEmeans the number of arguments. For example, in the case of getpid(void), declared in kernel/timer.c, we have the following code :

这样的定义意味着ioctl系统调用被声明并接受三个参数。旁边SYSCALL_DEFINE的数字表示参数的数量。例如,在 , 在 中getpid(void)声明的情况下kernel/timer.c,我们有以下代码:

SYSCALL_DEFINE0(getpid)
{
        return task_tgid_vnr(current);
}

Hope that clears things up a little.

希望能把事情弄清楚一点。

回答by Basile Starynkevitch

From an application's point of view, a system callis an elementary and atomic operation done by the kernel.

从应用程序的角度来看,系统调用内核完成的基本原子操作。

The Assembly Howtoexplains what is happening, in terms of machine instruction.

大会HOWTO解释发生了什么,在机器指令的条款。

Of course, the kernel is doing a lot of things when handling a syscall.

当然,内核在处理系统调用时会做很多事情。

Actually, you almost could believe that the entire kernel code is devoted to handle all system calls (this is not entirely true, but almost; from applications' point of view, the kernel is only visible thru system calls). The other answerby Daniel Kamil Kozar is explaining what kernel function is starting the handling of some system call (but very often, many other parts of the kernel indirectly participate to system calls; for example, the scheduler participates indirectly into implementing forkbecause it manages the child process created by a successful forksyscall).

实际上,您几乎可以相信整个内核代码都致力于处理所有系统调用(这并不完全正确,但几乎是正确的;从应用程序的角度来看,内核仅通过系统调用可见)。Daniel Kamil Kozar的另一个答案是解释什么内核函数开始处理某个系统调用(但很多时候,内核的许多其他部分间接参与系统调用;例如,调度程序间接参与实现, fork因为它管理由成功的fork系统调用创建的子进程)。

回答by KitsuneYMG

I know it's old, but I was searching for the source for _system_call()too and found this tidbit

我知道它很旧,但我也在寻找来源_system_call()并找到了这个花絮

Actual code for system_call entry point can be found in /usr/src/linux/kernel/sys_call.S Actual code for many of the system calls can be found in /usr/src/linux/kernel/sys.c, and the rest are found elsewhere. find is your friend.

system_call 入口点的实际代码可以在 /usr/src/linux/kernel/sys_call.S 中找到许多系统调用的实际代码可以在 /usr/src/linux/kernel/sys.c 中找到,其余的在别处找到。find 是你的朋友。

I assume this is dated, because I don't even have that file. However, grep found ENTRY(system_call)in arch/x86/kernel/entry_64.S and seems to be the thing that calls the individual system calls. I'm not up on my intel-syntax x86 asm right now, so you'll have to look and see if this is what you wanted.

我认为这是过时的,因为我什至没有那个文件。然而,ENTRY(system_call)在 arch/x86/kernel/entry_64.S 中找到的 grep似乎是调用单个系统调用的东西。我现在不知道我的 intel-syntax x86 asm,所以你必须看看这是否是你想要的。