码迷,mamicode.com
首页 > 系统相关 > 详细

Linux0.11内核源码分析系列:内存管理copy_page_tables()函数分析

时间:2014-12-28 01:52:27      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:linux kernel

/* 
 *Author  : DavidLin 
 *Date    : 2014-11-22pm 
 *Email   : linpeng1577@163.com or linpeng1577@gmail.com 
 *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()))    
            return -1;  /* Out of memory, see freeing */  //取空闲物理页为to_page_table赋值                                    
                                                          //如果没有空闲物理页,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表示成功
}


本文出自 “山下问童子” 博客,请务必保留此出处http://linpeng.blog.51cto.com/9779987/1596728

Linux0.11内核源码分析系列:内存管理copy_page_tables()函数分析

标签:linux kernel

原文地址:http://linpeng.blog.51cto.com/9779987/1596728

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!