Linux系统调用程序分析
2015-03-25 20:40
459 查看
当用户态进程调用一个系统调用时,CPU切换为内核态,并开始执行一个内核函数;
在Linux中是通过执行int $0x80来执行系统调用,这条汇编指令产生向量为128的编程异常。
内核实现了很多不同的系统调用,进程需要指明系统调用号作为参数进行调用,使用eax寄存器。
寄存器传参数有如下限制:
1> 每个参数的长度不能超过寄存器的长度,即32位;
2> 在系统调用号(eax)之外,参数的个数不能超过6个(ebx,ecx,edx,esi,edi,ebp)。
下面通过对getpid系统调用函数从调用API和汇编方式进行分析:
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。
author: 于凯
参考课程:《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
在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。
author: 于凯
参考课程:《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
相关文章推荐
- Linux下汇编程序及系统调用的深入分析
- linux系统下,c++程序,调用system命令失败,分析过程
- Linux系统调用的实现机制分析
- linux使普通用户获得root权限的vmsplice系统调用漏洞分析
- linux内核分析之系统调用
- Linux网络协议源代码分析 之 系统调用
- linux中mmap系统调用原理分析与实现
- Linux中mmap系统调用原理分析与实现
- Linux系统调用的实现机制分析
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】Linux系统调用的实现机制分析
- linux系统调用实现代码分析
- linux内核情景分析{中断 系统调用 内存}
- Linux-2.6.25 select系统调用源码分析
- 关于linux系统一启动就调用某个程序
- linux系统调用实现代码分析
- linux内核中断、异常、系统调用的分析以及实践
- Linux-2.6.25 select系统调用源码分析
- linux系统调用机制分析
- linux中mmap系统调用原理分析与实现
- linux中mmap系统调用原理分析与实现