免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2115 | 回复: 1
打印 上一主题 下一主题

linux 0.11 中 fork 的实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-22 15:02 |只看该作者 |倒序浏览

                                                在 ./init/main.c 中第一次出现fork调用
static inline _syscall0(int,fork)
void main(void)
{
    if( !fork() ){
        init();
    }
}
其中 _syscall0 是一个宏定义,在 ./include/initd.h 中
#define _syscall0(type,name) \
type name(void) \
{ \
long __res; \
__asm__ volatile ( "int $0x80" \
    :"=a" (__res) \
    :"" (__NR_##name));\
if (__res >= 0) \
    return (type) __res;\
errno = -__res; \
return -1;\
}
在 ./include/unistd.h 中
#define __NR_fork  2
将fork进行宏替换
static int fork(void)
{
    long _res;
    _asm_ volatile ("int $0x80" : "=a"(_res) : "0"(_NR_fork));
    if( _res >= 0 )
        return (int) _res;
    errno = -_res;
    return -1;
}
函数是用NR_fork 来初始化eax (eax=2), 说明fork就是int 0x80的 2号调用,好,进入中断
在 ./kernel/system_call.s 中找到int 0x80中断的处理函数
.align 2
_system_call:
    cmpl $nr_system_calls-1,%eax
    ja bad_sys_call
    push %ds
    push %es
    push %fs
    pushl %edx
    pushl %ecx        # push %ebx,%ecx,%edx as parameters
    pushl %ebx        # to the system call
    movl $0x10,%edx        # set up ds,es to kernel space
    mov %dx,%ds
    mov %dx,%es
    movl $0x17,%edx        # fs points to local data space
    mov %dx,%fs
    call _sys_call_table(,%eax,4)
在./include/linux/sched.h 中有 fn_ptr 的定义
    typedef int (*fn_ptr) ()
在 ./include/linux/sys.h 中
fn_ptr sys_call_table[] = { sys_setup, sys_exit,sys_fork,...}
所以call 的调用地址就是 _sys_call_table + %eax * 4 即:sys_fork ( 其中eax=2, 每个指针的长度是4, 所以用_sys_call_table的首地址加 2*4, 就是 _sys_call_table 数组中的第二个元素的地址,即sys_fork )
call _sys_call_table(,%eax,4) 即:call _sys_fork
在./kernel/system_call.s中
.align 2
_sys_fork:
    call _find_empty_process
    test1 %eax,%eax
    js 1f
    push  %gs
    pushl %esi
    pushl %esi
    pushl %esi
    pushl %esi
    call _copy_process
    add $20,%esp
1:    ret
可见fork又主要是调用了两个函数:
    find_empty_process 和 copy_process ;它们都在kernel/fork.c中实现
说明:gcc编译器在生成汇编代码,其函数名及变量名前都会都会加_,所以在汇编中调用C的函數或变量的时候,需要手动加上一个下划线。
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/107080/showart_2127998.html

论坛徽章:
0
2 [报告]
发表于 2013-10-05 17:52 |只看该作者
写的很好,大赞一个!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP