窺探 kernel,just for fun --- sys_fork,sys_vfork,sys_clone,kernel_thread

本系列文章由張同浩編寫,轉載請註明出處:http://blog.csdn.net/muge0913/article/details/7479379

郵箱:[email protected]




用戶空間進程創建接口:fork,vfork,clone函數,這裏只做簡單說明。

fork:使用該系統調用時,子進程複製父進程的全部資源。由於要複製父進程進程描述符給子進程(進程描述的結構很大!!),這一過程開銷是很大的。linux採用了”寫時複製技術”(copy on write,COW),使子進程先共享父進程的物理頁,只有子進程進行寫操作時,再複製對應的物理頁,避免了無用的複製開銷,提高了系統的性能。

實現代碼(x86):arch/x86/kernel/process.c

  1. int sys_fork(struct pt_regs *regs)  
  2. {  
  3.      return do_fork(SIGCHLD, regs->sp, regs,0, NULL, NULL);  
  4. }  

實現代碼(arm):arch/arm/kernel/sys_arm.c

  1. /* Fork a newtask - this creates a new program thread. 
  2.  * This is called indirectly via a smallwrapper 
  3.  */  
  4. asmlinkage int sys_fork(struct pt_regs *regs)  
  5. {  
  6. #ifdefCONFIG_MMU  
  7.      return do_fork(SIGCHLD, regs->ARM_sp,regs, 0, NULL, NULL);  
  8. #else  
  9.      /* can not support in nommu mode */  
  10.      return(-EINVAL);  
  11. #endif  
  12. }  

 

vfork:該系統調用創建的子進程,完全運行在父進程地址空間之上。子進程對地址空間任何數據的修改同樣爲父進程所見。vfork執行後父進程堵塞,知道子進程運行結束。

實現代碼(x86):arch/x86/kernel/process.c

  1. intsys_vfork(struct pt_regs *regs)  
  2. {  
  3.      return do_fork(CLONE_VFORK | CLONE_VM |SIGCHLD, regs->sp, regs, 0,NULL, NULL);  
  4. }  

實現代碼(arm):arch/arm/kernel/sys_arm.c

  1. asmlinkage intsys_vfork(struct pt_regs *regs)  
  2. {  
  3.      return do_fork(CLONE_VFORK | CLONE_VM |SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);  
  4. }  

clone:該調用是linux系統所特有的,其NPTL的實現依賴此函數。與fork,vfork相比clone對進程創建有更好的控制能力,能控制子進程和父進程共享何種資源。

實現代碼(x86):arch/x86/kernel/process.c

  1. long sys_clone(unsignedlong clone_flags, unsigned long newsp,  
  2.       void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)  
  3. {  
  4.      if (!newsp)  
  5.          newsp = regs->sp;  
  6.      return do_fork(clone_flags, newsp, regs, 0,parent_tid, child_tid);  
  7. }  

實現代碼(arm):arch/arm/kernel/sys_arm.c

  1. /* Clone a task- this clones the calling program thread. 
  2.  * This is called indirectly via a smallwrapper 
  3.  */  
  4. asmlinkage intsys_clone(unsigned long clone_flags, unsigned long newsp,  
  5.                int __user *parent_tidptr, int tls_val,int__user *child_tidptr, struct pt_regs *regs)  
  6. {  
  7.      if (!newsp)  
  8.          newsp = regs->ARM_sp;  
  9.      return do_fork(clone_flags, newsp, regs, 0,parent_tidptr, child_tidptr);  
  10. }  

上面進程的創建最終依賴於:do_fork,只是向其傳遞了不同的參數。

  1. longdo_fork(unsigned long clone_flags,  
  2.           unsigned long stack_start,  
  3.           struct pt_regs *regs,  
  4.           unsigned long stack_size,  
  5.           int __user *parent_tidptr,  
  6.           int __user *child_tidptr)  

參數clone_flags非常重要,fork把其設置爲SIGCHLD,vfork把其設置爲CLONE_VFORK|CLONE_VM|SIGCHLD,clone由用戶調用時傳遞。總的來說,do_fork由clone_flags決定。其值可以自由組合決定。include/linux/sched.h中宏定義:

  1. /* 
  2.  *cloning flags: 
  3.  */  
  4. #define CSIGNAL             0x000000ff    /*signal mask to be sent at exit */  
  5. #define CLONE_VM            0x00000100    /* set if VM shared between processes */  
  6. #define CLONE_FS            0x00000200    /* set if fs info shared between processes*/  
  7. #define CLONE_FILES         0x00000400    /* set if open files shared betweenprocesses */  
  8. #define CLONE_SIGHAND       0x00000800    /* set if signal handlers and blockedsignals shared */  
  9. #define CLONE_PTRACE        0x00002000    /* set if we want to let tracing continue onthe child too */  
  10. #define CLONE_VFORK         0x00004000    /* set if the parent wants the child to wakeit up on mm_release */  
  11. #define CLONE_PARENT        0x00008000    /* set if we want to have the same parent asthe cloner */  
  12. #define CLONE_THREAD        0x00010000    /* Same thread group? */  
  13. #define CLONE_NEWNS         0x00020000    /* New namespace group? */  
  14. #define CLONE_SYSVSEM       0x00040000    /* share system V SEM_UNDO semantics */  
  15. #define CLONE_SETTLS        0x00080000    /* create a new TLS for the child */  
  16. #define CLONE_PARENT_SETTID 0x00100000    /*set the TID in the parent */  
  17. #define CLONE_CHILD_CLEARTID     0x00200000    /*clear the TID in the child */  
  18. #define CLONE_DETACHED      0x00400000    /* Unused,ignored */  
  19. #define CLONE_UNTRACED      0x00800000    /* set ifthe tracing process can't force CLONE_PTRACE on this clone */  
  20. #define CLONE_CHILD_SETTID  0x01000000    /*set the TID in the child */  
  21. #define CLONE_STOPPED       0x02000000    /* Start instopped state */  
  22. #define CLONE_NEWUTS        0x04000000    /* Newutsname group? */  
  23. #define CLONE_NEWIPC        0x08000000    /* Newipcs */  
  24. #defineCLONE_NEWUSER       0x10000000    /* New user namespace */  
  25. #define CLONE_NEWPID        0x20000000    /* New pidnamespace */  
  26. #define CLONE_NEWNET        0x40000000    /* Newnetwork namespace */  
  27. #define CLONE_IO            0x80000000    /* Clone io context */  

上面的宏定義都佔用了獨立的bit,所以能或|組合使用。其低八位沒有使用,是爲了能和信號量組合使用。



內核線程創建接口:

內核線程是一種特殊的進程,它只能運行在內核態,不能訪問用戶空間的內容。內核線程除了各自的棧和硬件上下文外,共享所用資源。內核利用內核線程來完成一些後臺工作如kswapd,ksoftirqd。內核線程有kernel_thread創建。

  1. 在linux2.6.xxx/arch/x86/include/asm/processor.h  
  2. /* 
  3.  * create a kernel thread without removing itfrom tasklists 
  4.  */  
  5. extern intkernel_thread(int (*fn)(void *), void *arg, unsigned long flags);  

linux2.6.xxx/arch/arm/include/asm/processor.h

  1. /* 
  2.  * Create a new kernel thread 
  3.  */  
  4. extern intkernel_thread(int (*fn)(void *), void *arg, unsigned long flags);  

參數說明:

fn:新創建的內核線程要執行的函數。

arg:fn的參數。

flags:和do_fork中的clone_flags作用相似。

 

 

kernel_thread函數分析:

在linux2.6.xxx/arch/x86/kernel/process.c

  1. /* 
  2.  * Create a kernel thread 
  3.  */  
  4. intkernel_thread(int (*fn)(void *), void *arg, unsigned long flags)  
  5. {  
  6.      struct pt_regs regs;//保存進程的硬件上下文  
  7.    
  8.      memset(®s, 0, sizeof(regs));  
  9.      regs.si = (unsigned long) fn;  
  10.      regs.di = (unsigned long) arg;  
  11.    
  12. #ifdefCONFIG_X86_32  
  13.      regs.ds = __USER_DS;  
  14.      regs.es = __USER_DS;  
  15.      regs.fs = __KERNEL_PERCPU;  
  16.      regs.gs = __KERNEL_STACK_CANARY;  
  17. #else  
  18.      regs.ss = __KERNEL_DS;  
  19. #endif  
  20.    
  21.      regs.orig_ax = -1;  
  22.      regs.ip = (unsigned long)kernel_thread_helper;  
  23.      regs.cs = __KERNEL_CS | get_kernel_rpl();  
  24.      regs.flags = X86_EFLAGS_IF | 0x2;  
  25.    
  26.      /* Ok, create the new process.. */  
  27.      return do_fork(flags | CLONE_VM |CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);  
  28. }  

分析:

從這段代碼可知,內核線程的創建最終還是調用了do_fork。

 

arm架構的kernel_thread實現:

  1. /* 
  2.  * Create a kernel thread. 
  3.  */  
  4. pid_tkernel_thread(int (*fn)(void *), void *arg, unsigned long flags)  
  5. {  
  6.      struct pt_regs regs;  
  7.    
  8.      memset(®s, 0, sizeof(regs));  
  9.    
  10.      regs.ARM_r4 = (unsigned long)arg;  
  11.      regs.ARM_r5 = (unsigned long)fn;  
  12.      regs.ARM_r6 = (unsignedlong)kernel_thread_exit;  
  13.      regs.ARM_r7 = SVC_MODE | PSR_ENDSTATE |PSR_ISETSTATE;  
  14.      regs.ARM_pc = (unsignedlong)kernel_thread_helper;  
  15.      regs.ARM_cpsr = regs.ARM_r7 | PSR_I_BIT;  
  16.    
  17.      return do_fork(flags|CLONE_VM|CLONE_UNTRACED,0, ®s, 0, NULL, NULL);  
  18. }  
  19.    
  20. /* 
  21.  * Shuffle the argument into the correctregister before calling the 
  22.  * thread function.  r4 is the thread argument, r5 is the pointerto 
  23.  * the thread function, and r6 points to theexit function. 
  24.  */  
  25. extern voidkernel_thread_helper(void);  
  26. asm( ".pushsection .text\n"  
  27. "    .align\n"  
  28. "    .type    kernel_thread_helper,#function\n"  
  29. "kernel_thread_helper:\n"  
  30. #ifdefCONFIG_TRACE_IRQFLAGS  
  31. "    bl   trace_hardirqs_on\n"  
  32. #endif  
  33. "    msr  cpsr_c,r7\n"  
  34. "    mov  r0,r4\n"  
  35. "    mov  lr,r6\n"  
  36. "    mov  pc,r5\n"  
  37. "    .size    kernel_thread_helper,. - kernel_thread_helper\n"  
  38. "    .popsection");  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章