Linux-0.11内核源码分析系列:内存管理copy_page_tables()函数分析

/* 
 *Author  : DavidLin 
 *Date    : 2014-11-22pm 
 *Email   : [email protected] or [email protected] 
 *world   : the city of SZ, in China 
 *Ver     : 000.000.001 
 *history :     editor      time            do 
 *          1)LinPeng       2014-11-22      created this file! 
 *          2) 
 */ 
/*
 *  Well, here is one of the most complicated functions in mm. It
 * copies a range of linerar addresses by copying only the pages.
 * Let's hope this is bug-free, 'cause this one I don't want to debug :-)
 *
 * Note! We don't copy just any chunks of memory - addresses have to
 * be divisible by 4Mb (one page-directory entry), as this makes the
 * function easier. It's used only by fork anyway.
 *
 * NOTE 2!! When from==0 we are copying kernel space for the first
 * fork(). Then we DONT want to copy a full page-directory entry, as
 * that would lead to some serious memory waste - we just copy the
 * first 160 pages - 640kB. Even that is more than we need, but it
 * doesn't take any more memory - we don't copy-on-write in the low
 * 1 Mb-range, so the pages can be shared with the kernel. Thus the
 * special case for nr=xxxx.
 */

/* Linus认为下面copy_page_tables()函数是内存管理部分最难的之一
 * copy_page_tables()函数只被fork函数调用
 * 拷贝只是拷贝了一个页表,页表是管理4M地址的,所以按照4M对齐
 * 不拷贝物理页内容,当发生写时拷贝才会拷贝页表所管理的物理页内容
 * 对于进程0和1,只拷贝前160页共640Kb,出于效率考虑
 * 0-1M作为内核驻留地址区域,禁止写覆盖
 * 参数from,to是0-4G线性地址,size是字节为单位
 */
int copy_page_tables(unsigned long from,unsigned long to,long size)
{
	unsigned long * from_page_table;        //用于管理源页表    
	unsigned long * to_page_table;          //用于管理目的页表
	unsigned long this_page;                //用于保存页表
	unsigned long * from_dir, * to_dir;     //用于管理源页目录项,目的页目录项
	unsigned long nr;                       //用于保存页表项个数

	if ((from&0x3fffff) || (to&0x3fffff))    //4M对齐检测,否则die
		panic("copy_page_tables called with wrong alignment");
	from_dir = (unsigned long *) ((from>>20) & 0xffc); /* _pg_dir = 0 */
                                                          //源页目录项
	to_dir = (unsigned long *) ((to>>20) & 0xffc);    //目的页目录项
	size = ((unsigned) (size+0x3fffff)) >> 22;        //页表项个数是字节数除以4M
	for( ; size-->0 ; from_dir++,to_dir++) {                                
		if (1 & *to_dir)    //如果目的页目录项已经被使用,die
			panic("copy_page_tables: already exist");
		if (!(1 & *from_dir))    //如果源页目录项未使用,跳过,不拷贝
			continue;
		from_page_table = (unsigned long *) (0xfffff000 & *from_dir);//取源页表
        if (!(to_page_table = (unsigned long *) get_free_page()))        //取空闲物理页为to_page_table赋值
            return -1;    /* Out of memory, see freeing */            //如果没有空闲物理页,die
        *to_dir = ((unsigned long) to_page_table) | 7;                   //将页表存进相应页目录项,7表示可读写
                                                                               //想一下常用的chmod 777 anyfile
        nr = (from==0)?0xA0:1024;                                        //如果是0地址,只拷贝160页,否则拷贝1024页
                                                                               //一个页目录表管理1024个页目录项
                                                                               //一个页表管理1024个页表项
                                                                               //一个页表项管理有4K物理地址
                                                                               
        for ( ; nr-- > 0 ; from_page_table++,to_page_table++) {
            this_page = *from_page_table;                             //从源页表中取源页表项
            if (!(1 & this_page))                                     //如果源页表项未被使用,跳过
                continue;
            this_page &= ~2;                                          //目的页表项读写位设置为只读                               
            *to_page_table = this_page;                               //将源页表项存进目的页表项
            if (this_page > LOW_MEM) {                                //如果是主内存区
                *from_page_table = this_page;                      //源页表项也要设置为只读
                this_page -= LOW_MEM;                              //取相对主内存的偏移地址
                this_page >>= 12;                                  //取主内存管理数组索引
                mem_map[this_page]++;                              //物理页引用次数加1
            }
        }
    }
    invalidate();                                                           //刷新高速缓存
    return 0;                                                               //返回0表示成功
}    



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。