fork.c 代码分析笔记

《linux 内核全然剖析》 fork.c 代码分析笔记-LMLPHP

verifiy_area

long last_pid=0; //全局变量,用来记录眼下最大的pid数值

void verify_area(void * addr,int size) // addr 是虚拟地址 ,size是须要写入的字节大小
{
unsigned long start; start = (unsigned long) addr; //把地址强制类型转换之后,赋值给start
size += start & 0xfff; //取addr在当前虚拟地址中4M页面的偏移值,加上size能够得到要求写入虚拟地址的末端
start &= 0xfffff000; //取addr所在虚拟地址页面的起始处,即该页在数据段中偏移量
start += get_base(current->ldt[2]); //get_base得到当前进程数据段的线性基地址。加上start偏移量,于是实现了虚拟地址转化到线性地址
while (size>0) {
size -= 4096;
write_verify(start);//对线性地址start进行操作。验证该地址处页面是否可写
start += 4096;
}
}

copy_mem

int copy_mem(int nr,struct task_struct * p) //复制内存页表。把进程p的数据段copy到nr*TASK的线性地址处
{
unsigned long old_data_base,new_data_base,data_limit;
unsigned long old_code_base,new_code_base,code_limit; code_limit=get_limit(0x0f);//0x0f任务代码段选择符 ,get_limit 得到代码段的段限长
data_limit=get_limit(0x17);//0x17任务数据段选择符。get_limit 得到数据段的段限长
old_code_base = get_base(current->ldt[1]); //得到代码段的基地址
old_data_base = get_base(current->ldt[2]); //得到数据段的基地址
if (old_data_base != old_code_base) //linux 0.12 是 I&D的模式
panic("We don't support separate I&D");
if (data_limit < code_limit) //要求数据段不小于代码段
panic("Bad data_limit");
new_data_base = new_code_base = nr * TASK_SIZE; //更新代码段的基地址
p->start_code = new_code_base; //更新当前进程代码段的基地址
set_base(p->ldt[1],new_code_base);//把new_code_base 写入到ldt[1]
set_base(p->ldt[2],new_data_base);
if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
//把old_date_base 线性地址的数据拷贝到new_data_base处
//copy_page_tables 成功返回0,错误返回-1.假设失败。就以页为单位。free掉new_date_base涉及的内存页
free_page_tables(new_data_base,data_limit);
return -ENOMEM;
}
return 0;
}

copy_process

/*
* Ok, this is the main fork-routine. It copies the system process
* information (task[nr]) and sets up the necessary registers. It
* also copies the data segment in it's entirety.
*/
int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
long ebx,long ecx,long edx, long orig_eax,
long fs,long es,long ds,
long eip,long cs,long eflags,long esp,long ss)
{
struct task_struct *p;
int i;
struct file *f; p = (struct task_struct *) get_free_page(); //申请一页空内存,由p指向它
if (!p)
return -EAGAIN;
task[nr] = p; //把新进程指针copy到task数组里面
*p = *current; /* NOTE! this doesn't copy the supervisor stack */
p->state = TASK_UNINTERRUPTIBLE;//copy进程的时候,p进程状态设置为TASK_UNINTERRIPTIBLE
p->pid = last_pid;
p->counter = p->priority; //执行时间数
p->signal = 0; //初始没有接受不论什么信号,信号图为空
p->alarm = 0;
p->leader = 0; //fork出的进程不继承session leader,保证session leader仅仅有一个 /* process leadership doesn't inherit */
p->utime = p->stime = 0; //用户态时间和内核态时间
p->cutime = p->cstime = 0;//子进程用户态时间和内核态时间
p->start_time = jiffies; //进程p開始时间
p->tss.back_link = 0;//改动任务状态TSS数据
p->tss.esp0 = PAGE_SIZE + (long) p; //任务内核态的栈指针
p->tss.ss0 = 0x10;
p->tss.eip = eip; //指令代码指针
p->tss.eflags = eflags;
p->tss.eax = 0;
p->tss.ecx = ecx;
p->tss.edx = edx;
p->tss.ebx = ebx;
p->tss.esp = esp;
p->tss.ebp = ebp;
p->tss.esi = esi;
p->tss.edi = edi;
p->tss.es = es & 0xffff;
p->tss.cs = cs & 0xffff;
p->tss.ss = ss & 0xffff;
p->tss.ds = ds & 0xffff;
p->tss.fs = fs & 0xffff;
p->tss.gs = gs & 0xffff;
p->tss.ldt = _LDT(nr); //_LDT宏计算出nr进程的LDT描写叙述符的选择符
p->tss.trace_bitmap = 0x80000000;
if (last_task_used_math == current)
__asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
if (copy_mem(nr,p)) { //把 进程p的内容copy到nr*TASK的线性地址处
task[nr] = NULL; //失败就把task数组的相应指针置为NULL说明进程创建失败
free_page((long) p); //善后,把p相关的内存页释放
return -EAGAIN;
}
for (i=0; i<NR_OPEN;i++) //假设以上copy_mem成功,则把parent 打开的文件也让child继承
if (f=p->filp[i])
f->f_count++;
if (current->pwd)//引用+1
current->pwd->i_count++;
if (current->root)
current->root->i_count++;
if (current->executable)
current->executable->i_count++;
if (current->library)
current->library->i_count++;
set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
p->p_pptr = current;
p->p_cptr = 0;
p->p_ysptr = 0;
p->p_osptr = current->p_cptr;
if (p->p_osptr)
p->p_osptr->p_ysptr = p;
current->p_cptr = p;
p->state = TASK_RUNNING; /* do this last, just in case */
return last_pid;
}

find_empty_process

int find_empty_process(void) //为新进程获取不反复的pid
{
int i; repeat:
if ((++last_pid)<0) last_pid=1;
for(i=0 ; i<NR_TASKS ; i++)
if (task[i] && ((task[i]->pid == last_pid) ||
(task[i]->pgrp == last_pid))) //假设last_pid存在,那么repeat再測试 ++last_pid
goto repeat;
//在已经把lastpid变成全部进程都不同的pid之后,以下继续
for(i=1 ; i<NR_TASKS ; i++) //找出一个空暇进程。返回它的索引 i
if (!task[i])
return i;
return -EAGAIN; //假设64个进程都存在,那么报错
}

《linux 内核全然剖析》 fork.c 代码分析笔记-LMLPHP

05-08 08:09