- 論壇徽章:
- 0
|
在 ./init/main.c 中第一次出現(xiàn)fork調(diào)用
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;
}
函數(shù)是用NR_fork 來初始化eax (eax=2), 說明fork就是int 0x80的 2號調(diào)用,好,進入中斷
在 ./kernel/system_call.s 中找到int 0x80中斷的處理函數(shù)
.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 的調(diào)用地址就是 _sys_call_table + %eax * 4 即:sys_fork ( 其中eax=2, 每個指針的長度是4, 所以用_sys_call_table的首地址加 2*4, 就是 _sys_call_table 數(shù)組中的第二個元素的地址,即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又主要是調(diào)用了兩個函數(shù):
find_empty_process 和 copy_process ;它們都在kernel/fork.c中實現(xiàn)
說明:gcc編譯器在生成匯編代碼,其函數(shù)名及變量名前都會都會加_,所以在匯編中調(diào)用C的函數(shù)或變量的時候,需要手動加上一個下劃線。
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/107080/showart_2127998.html |
|