Chinaunix首页 | 论坛 | 博客
  • 博客访问: 450655
  • 博文数量: 362
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-26 17:08
文章分类

全部博文(362)

文章存档

2015年(362)

我的朋友

分类: LINUX

2015-12-11 00:29:59

   对于ARM中内核如何在启动的时候设置高低端内存的分界线(也是逻辑地址与虚拟地址分界线(虚拟地址)减去那个固定的偏移),这里我稍微引导下(内核分析使用):
   首先定位设置内核虚拟地址起始位置(也就是内核逻辑地址末端+1的地址)的文件:init.c (arch\arm\mm),在这个文件中的void __init bootmem_init(void)函数如下
  1. void __init bootmem_init(void)
  2. {
  3.     unsigned long min, max_low, max_high;

  4.     max_low = max_high = 0;

  5.     find_limits(&min, &max_low, &max_high);

  6.     arm_bootmem_init(min, max_low);

  7.     /*
  8.      * Sparsemem tries to allocate bootmem in memory_present(),
  9.      * so must be done after the fixed reservations
  10.      */
  11.     arm_memory_present();

  12.     /*
  13.      * sparse_init() needs the bootmem allocator up and running.
  14.      */
  15.     sparse_init();

  16.     /*
  17.      * Now free the memory - free_area_init_node needs
  18.      * the sparse mem_map arrays initialized by sparse_init()
  19.      * for memmap_init_zone(), otherwise all PFNs are invalid.
  20.      */
  21.     arm_bootmem_free(min, max_low, max_high);

  22.     high_memory = __va(((phys_addr_t)max_low << PAGE_SHIFT) - 1) + 1;

  23.     /*
  24.      * This doesn't seem to be used by the Linux memory manager any
  25.      * more, but is used by ll_rw_block. If we can get rid of it, we
  26.      * also get rid of some of the stuff above as well.
  27.      *
  28.      * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  29.      * the system, not the maximum PFN.
  30.      */
  31.     max_low_pfn = max_low - PHYS_PFN_OFFSET;
  32.     max_pfn = max_high - PHYS_PFN_OFFSET;
  33. }
   这个high_memory = __va(((phys_addr_t)max_low << PAGE_SHIFT) - 1) + 1;语句就是关键。从这里可以知道max_low就是高端内存的起始地址(物理地址)。那么这个max_low是如何得到的?其实看上面的代码可以推测出,他其实是在find_limits(&min, &max_low, &max_high);中(在同一个文件中)被设置的:
  1. static void __init find_limits(unsigned long *min, unsigned long *max_low,
  2.     unsigned long *max_high)
  3. {
  4.     struct meminfo *mi = &meminfo;
  5.     int i;

  6.     *min = -1UL;
  7.     *max_low = *max_high = 0;

  8.     for_each_bank (i, mi) {
  9.         struct membank *bank = &mi->bank[i];
  10.         unsigned long start, end;

  11.         start = bank_pfn_start(bank);
  12.         end = bank_pfn_end(bank);

  13.         if (*min > start)
  14.             *min = start;
  15.         if (*max_high < end)
  16.             *max_high = end;
  17.         if (bank->highmem)
  18.             continue;
  19.         if (*max_low < end)
  20.             *max_low = end;
  21.     }
  22. }
    这个函数的意思很明显:通过扫描struct meminfo *mi = &meminfo;(结构体meminfo的数组)中的所有信息,设置三个指针所指的变量:
  1. min :内存物理地址起始
  2. max_low :低端内存区物理地址末端
  3. max_high :高端内存区物理地址末端
    从上面可以看出,max_low和max_high所保存的地址不同就是由于bank->highmem造成的,它是内存bank被设为高端内存的依据:
  1. “如果这个内存bank是高端内存(bank->highmem != 0),跳过max_low = end;语句,max_low和max_high将不同(结果实际上是max_high > max_low);
  2. 否则假设没有一个内存bank是高端内存(所有bank->highmem == 0)max_low和max_high必然一致(高端内存大小为0)”
   当然要实现这个函数的功能,必须保证meminfo所指数组中的所有bank是按照地址数据从小到大排序好的哦~~。但是这个大家不用担心,后面会看到的:)
   
   经过上面的跟踪,焦点集中到了全局变量(同一个文件中):
  1. /*
  2. * This keeps memory configuration data used by a couple memory
  3. * initialization functions, as well as show_mem() for the skipping
  4. * of holes in the memory map. It is populated by arm_add_memory().
  5. */
  6. struct meminfo meminfo;
   这个结构体的定义(setup.h (arch\arm\include\asm)):
  1. /*
  2.  * Memory map description
  3.  */
  4. #define NR_BANKS 8  /*现在ARM最大只支持到8个bank哦~*/

  5. struct membank {
  6.     phys_addr_t start;
  7.     unsigned long size;
  8.     unsigned int highmem;  /*我们关心的变量*/
  9. };

  10. struct meminfo {
  11.     int nr_banks;
  12.     struct membank bank[NR_BANKS];  /*我们关心的数组*/
  13. };

  14. extern struct meminfo meminfo;

  15. #define for_each_bank(iter,mi)                \
  16.     for (iter = 0; iter < (mi)->nr_banks; iter++)
  #define bank_pfn_start(bank) __phys_to_pfn((bank)->start)
  #define bank_pfn_end(bank) __phys_to_pfn((bank)->start + (bank)->size)
  #define bank_pfn_size(bank) ((bank)->size >> PAGE_SHIFT)
  #define bank_phys_start(bank) (bank)->start
  #define bank_phys_end(bank) ((bank)->start + (bank)->size)
  #define bank_phys_size(bank) (bank)->size
  只要找到初始化这个全局变量并完成排序的地方,就可以知道高端内存是如何配置的了!!OK,明确目标,go on~~~

   通过查找代码,我们可以在setup.c (arch\arm\kernel)这个文件中找到相关的代码。在系统启动早期会运行的函数(具体的顺序你可以自行分析下ARM内核的启动流程,以后我也会写下)中有这样一个函数:
  1. void __init setup_arch(char **cmdline_p)
  2. {
  3.     struct machine_desc *mdesc;

  4.     unwind_init();

  5.     setup_processor();
  6.     mdesc = setup_machine_fdt(__atags_pointer);
  7.     if (!mdesc)
  8.         mdesc = setup_machine_tags(machine_arch_type);
  9.     machine_desc = mdesc;
  10.     machine_name = mdesc->name;

  11.     if (mdesc->soft_reboot)
  12.         reboot_setup("s");

  13.     init_mm.start_code = (unsigned long) _text;
  14.     init_mm.end_code = (unsigned long) _etext;
  15.     init_mm.end_data = (unsigned long) _edata;
  16.     init_mm.brk     = (unsigned long) _end;

  17.     /* 填充cmd_line以备后用,维护boot_command_line */
  18.     strlcpy(cmd_line, boot_command_line, COMMAND_LINE_SIZE);    /* 拷贝boot_command_line中的数据到cmd_line */
  19.     *cmdline_p = cmd_line;

  20.     parse_early_param();        /* 分析boot_command_line(内核启动参数字符串)中的数据,
  21.                                  * 其中就分析了mem=size@start参数初始化了struct meminfo meminfo;
  22.                                  * 同时如果有vmalloc=size参数也会初始化 vmalloc_min */  

  23.     sanity_check_meminfo();    /* 在此处设置struct meminfo meminfo中每个bank中的highmem变量,
  24.                                 * 通过vmalloc_min确定每个bank中的内存是否属于高端内存 */
  25.     arm_memblock_init(&meminfo, mdesc);   /* 在此处排序按地址数据从小到大排序 */

  26.     paging_init(mdesc);
  27.     request_standard_resources(mdesc);

  28.     unflatten_device_tree();

  29. #ifdef CONFIG_SMP
  30.     if (is_smp())
  31.         smp_init_cpus();
  32. #endif
  33.     reserve_crashkernel();

  34.     cpu_init();
  35.     tcm_init();

  36. #ifdef CONFIG_MULTI_IRQ_HANDLER
  37.     handle_arch_irq = mdesc->handle_irq;
  38. #endif

  39. #ifdef CONFIG_VT
  40. #if defined(CONFIG_VGA_CONSOLE)
  41.     conswitchp = &vga_con;
  42. #elif defined(CONFIG_DUMMY_CONSOLE)
  43.     conswitchp = &dummy_con;
  44. #endif
  45. #endif
  46.     early_trap_init();

  47.     if (mdesc->init_early)
  48.         mdesc->init_early();
  49. }
在上面的注释中,我已经表明了重点和解析,下面我细化下:
(1)获取参数部分
     通过parse_early_param();函数可以解析内核启动参数中的许多字符串,但是对于我们这次分析内存的话主要是分析以下两个参数:
     mem=size@start参数,她为初始化struct meminfo meminfo;(我们一直关注的内存信息哦~)提供信息。具体的获取信息的函数(同样位于setup.c (arch\arm\kernel)):
  1. int __init arm_add_memory(phys_addr_t start, unsigned long size)
  2. {
  3.     struct membank *bank = &meminfo.bank[meminfo.nr_banks];

  4.     if (meminfo.nr_banks >= NR_BANKS) {
  5.         printk(KERN_CRIT "NR_BANKS too low, "
  6.             "ignoring memory at 0x%08llx\n", (long long)start);
  7.         return -EINVAL;
  8.     }

  9.     /*
  10.      * Ensure that start/size are aligned to a page boundary.
  11.      * Size is appropriately rounded down, start is rounded up.
  12.      */
  13.     size -= start & ~PAGE_MASK;
  14.     bank->start = PAGE_ALIGN(start);
  15.     bank->size = size & PAGE_MASK;

  16.     /*
  17.      * Check whether this memory region has non-zero size or
  18.      * invalid node number.
  19.      */
  20.     if (bank->size == 0)
  21.         return -EINVAL;

  22.     meminfo.nr_banks++;
  23.     return 0;
  24. }

  25. /*
  26.  * Pick out the memory size. We look for mem=size@start,
  27.  * where start and size are "size[KkMm]"
  28.  */
  29. static int __init early_mem(char *p)
  30. {
  31.     static int usermem __initdata = 0;
  32.     unsigned long size;
  33.     phys_addr_t start;
  34.     char *endp;

  35.     /*
  36.      * If the user specifies memory size, we
  37.      * blow away any automatically generated
  38.      * size.
  39.      */
  40.     if (usermem == 0) {
  41.         usermem = 1;
  42.         meminfo.nr_banks = 0;
  43.     }

  44.     start = PHYS_OFFSET;
  45.     size = memparse(p, &endp);
  46.     if (*endp == '@')
  47.         start = memparse(endp + 1, NULL);

  48.     arm_add_memory(start, size);

  49.     return 0;
  50. }
  51. early_param("mem", early_mem);
     vmalloc=size参数,她为初始化vmalloc_min(需要保留的内核虚拟地址空间大小,也就是这个内核虚拟地址空间中除去逻辑地址空间和必要的防止越界的保护空洞后最少要预留的地址空间)提供信息。具体的实现函数(位于mmu.c (arch\arm\mm)):
  1. static void * __initdata vmalloc_min = (void *)(VMALLOC_END - SZ_128M); /* 默认值,使得内核虚拟地址保留出128MB的空间以备后用 */
  /* 这里顺便提一下 VMALLOC_END  ,他和芯片构架相关,
   *不一定是0xffffffff,比如2410是0xF6000000UL,pxa是0xe8000000UL*/

  1. /*
  2.  * vmalloc=size forces the vmalloc area to be exactly 'size'
  3.  * bytes. This can be used to increase (or decrease) the vmalloc
  4.  * area - the default is 128m.
  5.  */
  6. static int __init early_vmalloc(char *arg)
  7. {
  8.     unsigned long vmalloc_reserve = memparse(arg, NULL);

  9.     if (vmalloc_reserve < SZ_16M) {
  10.         vmalloc_reserve = SZ_16M;
  11.         printk(KERN_WARNING
  12.             "vmalloc area too small, limiting to %luMB\n",
  13.             vmalloc_reserve >> 20);
  14.     } /* 数据检查,最小值=16MB */

  15.     if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
  16.         vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
  17.         printk(KERN_WARNING
  18.             "vmalloc area is too big, limiting to %luMB\n",
  19.             vmalloc_reserve >> 20);
  20.     }  /* 数据检查,最大值为从这个内核虚拟地址空间减去32MB,
  21.         * 也就是只有32MB的逻辑地址空间,其他地址全部保留备用 */

  22.     vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);   
  23.       /* vmalloc_min其实就是以后可用于映射分配的内核虚拟地址空间(不包含逻辑地址)最小值 */
  24.     return 0;
  25. }
  26. early_param("vmalloc", early_vmalloc);
(2)在获得了必要的信息(初始化好struct meminfo meminfo和vmalloc_min)后,内核通过sanity_check_meminfo函数自动去通过vmalloc_min信息来初始化每个meminfo.bank[?]中的highmem成员。此过程中如果有必要,将可能会改变meminfo中的bank数组。处理函数位于mmu.c (arch\arm\mm)
  1. static phys_addr_t lowmem_limit __initdata = 0;

  2. void __init sanity_check_meminfo(void)
  3. {
  4.     int i, j, highmem = 0;

  5.     for (i = 0, j = 0; i < meminfo.nr_banks; i++) {
  6.         struct membank *bank = &meminfo.bank[j];
  7.         *bank = meminfo.bank[i];

  8. #ifdef CONFIG_HIGHMEM
  9.         if (__va(bank->start) >= vmalloc_min ||
  10.          __va(bank->start) < (void *)PAGE_OFFSET)
  11.             highmem = 1;

  12.         bank->highmem = highmem;

  13.         /*
  14.          * Split those memory banks which are partially overlapping
  15.          * the vmalloc area greatly simplifying things later.
  16.          */
  17.         if (__va(bank->start) < vmalloc_min &&
  18.          bank->size > vmalloc_min - __va(bank->start)) {
  19.             if (meminfo.nr_banks >= NR_BANKS) {
  20.                 printk(KERN_CRIT "NR_BANKS too low, "
  21.                          "ignoring high memory\n");
  22.             } else {
  23.                 memmove(bank + 1, bank,
  24.                     (meminfo.nr_banks - i) * sizeof(*bank));
  25.                 meminfo.nr_banks++;
  26.                 i++;
  27.                 bank[1].size -= vmalloc_min - __va(bank->start);
  28.                 bank[1].start = __pa(vmalloc_min - 1) + 1;
  29.                 bank[1].highmem = highmem = 1;
  30.                 j++;
  31.             }
  32.             bank->size = vmalloc_min - __va(bank->start);
  33.         }
  34. #else
  35.         bank->highmem = highmem;

  36.         /*
  37.          * Check whether this memory bank would entirely overlap
  38.          * the vmalloc area.
  39.          */
  40.         if (__va(bank->start) >= vmalloc_min ||
  41.          __va(bank->start) < (void *)PAGE_OFFSET) {
  42.             printk(KERN_NOTICE "Ignoring RAM at %.8llx-%.8llx "
  43.              "(vmalloc region overlap).\n",
  44.              (unsigned long long)bank->start,
  45.              (unsigned long long)bank->start + bank->size - 1);
  46.             continue;
  47.         }

  48.         /*
  49.          * Check whether this memory bank would partially overlap
  50.          * the vmalloc area.
  51.          */
  52.         if (__va(bank->start + bank->size) > vmalloc_min ||
  53.          __va(bank->start + bank->size) < __va(bank->start)) {
  54.             unsigned long newsize = vmalloc_min - __va(bank->start);
  55.             printk(KERN_NOTICE "Truncating RAM at %.8llx-%.8llx "
  56.              "to -%.8llx (vmalloc region overlap).\n",
  57.              (unsigned long long)bank->start,
  58.              (unsigned long long)bank->start + bank->size - 1,
  59.              (unsigned long long)bank->start + newsize - 1);
  60.             bank->size = newsize;
  61.         }
  62. #endif
  63.         if (!bank->highmem && bank->start + bank->size > lowmem_limit)
  64.             lowmem_limit = bank->start + bank->size;

  65.         j++;
  66.     }
  67. #ifdef CONFIG_HIGHMEM
  68.     if (highmem) {
  69.         const char *reason = NULL;

  70.         if (cache_is_vipt_aliasing()) {
  71.             /*
  72.              * Interactions between kmap and other mappings
  73.              * make highmem support with aliasing VIPT caches
  74.              * rather difficult.
  75.              */
  76.             reason = "with VIPT aliasing cache";
  77.         }
  78.         if (reason) {
  79.             printk(KERN_CRIT "HIGHMEM is not supported %s, ignoring high memory\n",
  80.                 reason);
  81.             while (j > 0 && meminfo.bank[j - 1].highmem)
  82.                 j--;
  83.         }
  84.     }
  85. #endif
  86.     meminfo.nr_banks = j;
  87.     memblock_set_current_limit(lowmem_limit);
  88. }
(3)最后必须做的就是排序了,完成了这个工作就可以完全被我们上面提到的find_limits函数使用了,而这个工作就放在了接下来的arm_memblock_init(&meminfo, mdesc);中的一开头:
  1. static int __init meminfo_cmp(const void *_a, const void *_b)
  2. {
  3.     const struct membank *a = _a, *b = _b;
  4.     long cmp = bank_pfn_start(a) - bank_pfn_start(b);
  5.     return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
  6. }

  7. void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc)
  8. {
  9.     int i;

  10.     sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]), meminfo_cmp, NULL);   /* 极好用的排序函数 */

  11.     memblock_init();
  12.     for (i = 0; i < mi->nr_banks; i++)
  13.         memblock_add(mi->bank[i].start, mi->bank[i].size);

  14.     /* Register the kernel text, kernel data and initrd with memblock. */
  15. #ifdef CONFIG_XIP_KERNEL
  16.     memblock_reserve(__pa(_sdata), _end - _sdata);
  17. #else
  18.     memblock_reserve(__pa(_stext), _end - _stext);
  19. #endif
  20. #ifdef CONFIG_BLK_DEV_INITRD
  21.     if (phys_initrd_size &&
  22.      !memblock_is_region_memory(phys_initrd_start, phys_initrd_size)) {
  23.         pr_err("INITRD: 0x%08lx+0x%08lx is not a memory region - disabling initrd\n",
  24.          phys_initrd_start, phys_initrd_size);
  25.         phys_initrd_start = phys_initrd_size = 0;
  26.     }
  27.     if (phys_initrd_size &&
  28.      memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) {
  29.         pr_err("INITRD: 0x%08lx+0x%08lx overlaps in-use memory region - disabling initrd\n",
  30.          phys_initrd_start, phys_initrd_size);
  31.         phys_initrd_start = phys_initrd_size = 0;
  32.     }
  33.     if (phys_initrd_size) {
  34.         memblock_reserve(phys_initrd_start, phys_initrd_size);

  35.         /* Now convert initrd to virtual addresses */
  36.         initrd_start = __phys_to_virt(phys_initrd_start);
  37.         initrd_end = initrd_start + phys_initrd_size;
  38.     }
  39. #endif

  40.     arm_mm_memblock_reserve();
  41.     arm_dt_memblock_reserve();

  42.     /* reserve any platform specific memblock areas */
  43.     if (mdesc->reserve)
  44.         mdesc->reserve();

  45.     memblock_analyze();
  46.     memblock_dump_all();
  47. }

通过上面的分析,整个高低端内存是如何确定的基本就清晰了,这里总结一下:
ARM构架中,高-低段内存是内核通过内核启动参数( mem=size@start和vmalloc=size)来自动配置的,如果没有特殊去配置他,那么在普通的ARM系统中是不会有高端内存存在的。除非你系统的RAM很大或vmalloc配置得很大,就很可能出现高端内存。

以上是我对高-低端内存学习时跟踪代码的备忘,如果大家在其中发现什么不对的地方,欢迎拍砖、纠正~~谢谢~

对于X86构架的高端内存信息,请参考:    linux的物理内存与线性地址空间布局--1
阅读(607) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~