首页 > 系统服务 > 详细

Linux系统调用程序分析

时间:2015-03-25 21:35:39      阅读:269      评论:0      收藏:0      [点我收藏+]


当用户态进程调用一个系统调用时,CPU切换为内核态,并开始执行一个内核函数;

在Linux中是通过执行int $0x80来执行系统调用,这条汇编指令产生向量为128的编程异常。

内核实现了很多不同的系统调用,进程需要指明系统调用号作为参数进行调用,使用eax寄存器。

寄存器传参数有如下限制:

1> 每个参数的长度不能超过寄存器的长度,即32位;

2> 在系统调用号(eax)之外,参数的个数不能超过6个(ebx,ecx,edx,esi,edi,ebp)。


下面通过对getpid系统调用函数从调用API和汇编方式进行分析:

<span style="font-size:18px;">#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
	pid_t pid;

	pid = getpid();
	printf("The pid of this program is %d\n", pid);
	
	return 0;
}</span>
<span style="font-size:18px;">#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
	pid_t pid;

	//the first arg use %ebx,here not have input arg.
	asm volatile
	(
	<span style="white-space:pre">	</span>"mov $0x14,%%eax\n\t"
		"int $0x80\n\t"
		"movl %%eax,%0\n\t"
		: "+r" (pid)
	);
	printf("The pid of this program is %d\n", pid);
	
	return 0;
}</span>

getpid的系统调用号为20,参考http://codelab.shiyanlou.com/xref/linux-3.18.6/arch/x86/syscalls/syscall_32.tbl,或者linux中的/usr/include/x86_64-linux-gnu/asm/unistd_32.h。



Linux系统调用程序分析

原文:http://blog.csdn.net/kaiandshan/article/details/44627215

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!