六 memory&chosen节点

根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点。memory节点用来描述硬件内存布局的。如果有多块内存,既可以通过多个memory节点表示,也可以通过一个memory节点的reg属性的多个元素支持。举一个例子,假如某个64位的系统有两块内存,分别是

• RAM: 起始地址 0x0, 长度 0x80000000 (2GB)
• RAM: 起始地址 0x100000000, 长度 0x100000000 (4GB)

对于64位的系统,根节点的#address-cells属性和#size-cells属性都设置成2。一个memory节点的形式如下(还记得前几节说过节点地址必须和reg属性第一个地址相同的事情吧):
    memory@0 {
        device_type = "memory";
        reg = <0x000000000 0x00000000 0x00000000 0x80000000
               0x000000001 0x00000000 0x00000001 0x00000000>;
    };

两个memory节点的形式如下:
    memory@0 {
        device_type = "memory";
        reg = <0x000000000 0x00000000 0x00000000 0x80000000>;
    };
    memory@100000000 {
        device_type = "memory";
        reg = <0x000000001 0x00000000 0x00000001 0x00000000>;
    };

chosen节点也位于根节点下,该节点用来给内核传递参数(不代表实际硬件)。对于Linux内核,该节点下最有用的属性是bootargs,该属性的类型是字符串,用来向Linux内核传递cmdline。规范中还定义了stdout-path和stdin-path两个可选的、字符串类型的属性,这两个属性的目的是用来指定标准输入输出设备的,在linux中,这两个属性基本不用。

memory和chosen节点在内核初始化的代码都位于start_kernel()->setup_arch()->setup_machine_fdt()->early_init_dt_scan_nodes()函数中(位于drivers/of/fdt.c),复制代码如下(本节所有代码都来自官方内核4.4-rc7版本):

1078 void __init early_init_dt_scan_nodes(void)
1079 {      
1080     /* Retrieve various information from the /chosen node */
1081     of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1082 
1083     /* Initialize {size,address}-cells info */
1084     of_scan_flat_dt(early_init_dt_scan_root, NULL);
1085 
1086     /* Setup memory, calling early_init_dt_add_memory_arch */
1087     of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1088 }

of_scan_flat_dt函数扫描整个设备树,实际的动作是在回调函数中完成的。第1081行是对chosen节点操作,该行代码的作用是将节点下的bootargs属性的字符串拷贝到boot_command_line指向的内存中。boot_command_line是内核的一个全局变量,在内核的多处都会用到。第1084行是根据根节点的#address-cells属性和#size-cells属性初始化全局变量dt_root_size_cells和dt_root_addr_cells,还记得前边说过如果没有设置属性的话就用默认值,这些都在early_init_dt_scan_root函数中实现。第1087行是对内存进行初始化,复制early_init_dt_scan_memory部分代码如下:

 893 /**
 894  * early_init_dt_scan_memory - Look for an parse memory nodes
 895  */
 896 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
 897                      int depth, void *data)
 898 {
 899     const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
 900     const __be32 *reg, *endp;
 901     int l;
 902 
 903     /* We are scanning "memory" nodes only */
 904     if (type == NULL) {
 905         /*
 906          * The longtrail doesn't have a device_type on the
 907          * /memory node, so look for the node called /memory@0.
 908          */
 909         if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
 910             return 0;
 911     } else if (strcmp(type, "memory") != 0)
 912         return 0;
 913 
 914     reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
 915     if (reg == NULL)
 916         reg = of_get_flat_dt_prop(node, "reg", &l);
 917     if (reg == NULL)
 918         return 0;
 919 
 920     endp = reg + (l / sizeof(__be32));
 921 
 922     pr_debug("memory scan node %s, reg size %d,\n", uname, l);
 923 
 924     while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
 925         u64 base, size;
 926 
 927         base = dt_mem_next_cell(dt_root_addr_cells, &reg);
 928         size = dt_mem_next_cell(dt_root_size_cells, &reg);
 929
 930         if (size == 0)
 931             continue;
 932         pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
 933             (unsigned long long)size);
 934 
 935         early_init_dt_add_memory_arch(base, size);
 936     }
 937 
 938     return 0;
 939 }

第914行可以看出linux内核不仅支持reg属性,也支持linux,usable-memory属性。对于dt_root_addr_cells和dt_root_size_cells的使用也能看出根节点的#address-cells属性和#size-cells属性都是用来描述内存地址和大小的。得到每块内存的起始地址和大小后,在第935行调用early_init_dt_add_memory_arch函数,复制代码如下:
 
 983 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
 984 {
 985     const u64 phys_offset = __pa(PAGE_OFFSET);
 986 
 987     if (!PAGE_ALIGNED(base)) {
 988         if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
 989             pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
 990                 base, base + size);
 991             return;
 992         }
 993         size -= PAGE_SIZE - (base & ~PAGE_MASK);
 994         base = PAGE_ALIGN(base);
 995     }
 996     size &= PAGE_MASK;
 997 
 998     if (base > MAX_MEMBLOCK_ADDR) {
 999         pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1000                 base, base + size);
1001         return;
1002     }
1003 
1004     if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
1005         pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1006                 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1007         size = MAX_MEMBLOCK_ADDR - base + 1;
1008     }
1009 
1010     if (base + size < phys_offset) {
1011         pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1012                base, base + size);
1013         return;
1014     }
1015     if (base < phys_offset) {
1016         pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1015     if (base < phys_offset) {
1016         pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1017                base, phys_offset);
1018         size -= phys_offset - base;
1019         base = phys_offset;
1020     }
1021     memblock_add(base, size);
1022 }

从以上代码可以看出内核对地址和大小做了一系列判断后,最后调用memblock_add将内存块加入内核。

我眼中的Linux设备树(六 memory&chosen节点)的更多相关文章

  1. Linux设备树(六 memory&chosen节点)

    六 memory&chosen节点 根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点.memory节点用来描述硬件内存布局的.如果有多块内存,既可以通过多个memo ...

  2. 我眼中的Linux设备树(五 根节点)

    五 根节点一个最简单的设备树必须包含根节点,cpus节点,memory节点.根节点的名字及全路径都是"/",至少需要包含model和compatible两个属性.model属性我们 ...

  3. 我眼中的Linux设备树(一 概述)

    一 概述设备树(Device tree)是一套用来描述硬件属相的规则.ARM Linux采用设备树机制源于2011年3月份Linux创始人Linus Torvalds发的一封邮件,在这封邮件中他提倡A ...

  4. 我眼中的Linux设备树(二 节点)

    二 节点(node)的表示首先说节点的表示方法,除了根节点只用一个斜杠"/"表示外,其他节点的表示形式如"node-name@unit-address".@前边 ...

  5. 我眼中的Linux设备树(四 中断)

    四 中断中断一般包括中断产生设备和中断处理设备.中断控制器负责处理中断,每一个中断都有对应的中断号及触发条件.中断产生设备可能有多个中断源,有时多个中断源对应中断控制器中的一个中断,这种情况中断产生设 ...

  6. 我眼中的Linux设备树(三 属性)

    三 属性(property)device_type = "memory"就是一个属性,等号前边是属性,后边是值.节点是一个逻辑上相对独立的实体,属性是用来描述节点特性的,根据需要一 ...

  7. linux设备树语法

    设备树语法及绑定 概述 Device Tree是一种用来描述硬件的数据结构,类似板级描述语言,起源于OpenFirmware(OF). 就ARM平台来说,设备树文件存放在arch/arm/boot/d ...

  8. Linux设备树(五 根节点)

    五 根节点 一个最简单的设备树必须包含根节点,cpus节点,memory节点.根节点的名字及全路径都是“/”,至少需要包含model和compatible两个属性.model属性我们在属性那节已经说过 ...

  9. linux 设备树【转】

    转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...

随机推荐

  1. Future学习

    接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果.        Callable接口类似于Runnable,从名字就可以看出来了,但 ...

  2. Java finalize方法使用

    <JAVA编程思想>: Java提供finalize()方法,垃圾回收器准备释放内存的时候,会先调用finalize(). (1).对象不一定会被回收. (2).垃圾回收不是析构函数. ( ...

  3. Mysql数据库连接报错!1130:host XXX is not allowed to connect to this mysql server

    我猜想是可能是连接的用户权限问题.结果这样子操作mysql库,可以解决此问题.在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost” ...

  4. 微信小程序 发现之旅(一)—— 项目搭建与页面跳转

    开发微信小程序需要注册一个小程序账号,具体流程可以参照官方教程: https://mp.weixin.qq.com/debug/wxadoc/dev/index.html 开通账户之后,在 “开发设置 ...

  5. JAVA 第二天 基本数据类型

    在栈中可以直接分配内存的数据是基本数据类型.引用数据类型:数据的引用在栈中,但他的对象在堆中. 基本数据类型,小可转大,大转小会失去精度 第一类:逻辑型boolean 第二类:文本型char 第三类: ...

  6. git提交项目常用命令及git分支的用法

    1.第一步首先从git托管平台clone项目,我这里就使用idea为例: 填写git的url与存放本地目录名及项目名     2.如果你对项目进行了一些修改,就可以执行git命令,进行提交. 有两种方 ...

  7. 解决Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code-front-jet\node_modules\.npminstall\node-sass\3.7.0\node-sass\vendor'

    在使用npm安装node-sass的时候,可能会出现如下的报错: Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code ...

  8. Jmeter(二十)_Mock接口

    首先解释一下什么是mock接口. Mock通常是指,在测试一个对象时,我们构造一些假的对象来模拟与其交互.而这些Mock对象的行为是我们事先设定且符合预期.通过这些Mock对象来测试对象在正常逻辑,异 ...

  9. Sybase identity 字段

    1.identity Oracle, DB2, pgSQL中都有sequence的概念,这个概念比Identity先进很多,在Sybase中没有Sequence对象,与之相对应的是Identity 2 ...

  10. Servlet再度学习

    虽然Servlet已经使用很多了,但是一直都仅局限在其使用操作上. 最近有空想对它进行一个相对全面的了解. 下面是博主整理的一篇博文. 一.Servlet简介 Servlet(Server Apple ...