• ARM64 SMP多核启动详解1(spin_table)


    1. 简介

    处理器架构:arm64
    uboot版本:uboot-2023
    内核源码:linux-5.10
    ubuntu版本:20.04.1

    一般嵌入式系统使用的都是对称多处理器(Symmetric Multi-Processor, SMP)系统,包含了多个cpu, 这几个cpu都是相同的处理器,如4核Contex-A53。但是在系统 启动阶段他们的地位并不是相同的,其中core0是主cpu(也叫引导处理器),其他core是从cpu(也叫辅处理器),引导cpu负责执行我们的启动加载程序如uboot,以及初始化内核,系统初始化完成之后主core会启动从处理器。

    一般主处理器启动从处理器有以下三种:

    1. ACPI
    2. spin-table
    3. PSCI

    第一种ACPI是高级配置与电源接口(Advanced Configuration and Power Interface)一般在x86平台用的比较多,而后两种spin-table(自旋表)和PSCI(电源状态协调协议 Power State Coordination)会在arm平台上使用。

    2.CPU启动的一些概念

    cpu启动的含义:cpu可以从内存中取指、译码、执行,当然内存可以是soc片内的sram,也可以是ddr。

    我们要知道,程序为何可以在多个cpu上并发执行:他们有各自独立的一套寄存器,如:程序计数器pc,栈指针寄存器sp,通用寄存器等,可以独自 取指、译码、执行,当然内存和外设资源是共享的,多核环境下当访问临界区 资源一般 自旋锁来防止竞态发生。

    soc启动流程:soc启动的一般会从片内的rom, 也叫bootrom开始执行第一条指令,这个地址是系统默认的启动地址,会在bootrom中由芯片厂家固化一段启动代码来加载启动bootloader到片内的sram,启动完成后的bootloader除了做一些硬件初始化之外做的最重要的事情是初始化ddr,因为sram的空间比较小所以需要初始化拥有大内存 ddr,最后会从网络/usb下载 或从存储设备分区上加载内核到ddr某个地址,为内核传递参数之后,然后bootloader就完成了它的使命,跳转到内核,就进入了操作系统内核的世界。

    linux内核启动流程:bootloader将系统的控制权交给内核之后,他首先会进行处理器架构相关初始化部分,如设置异常向量表,初始化mmu(之后内核就从物理地址空间进入了虚拟地址空间的世界,一切是那么的虚无缥缈,又是那么的恰到好处)等等,然后会清bss段,设置sp之后跳转到C语言部分进行更加复杂通用的初始化,其中会进行内存方面的初始化,调度器初始化,文件系统等内核基础组件 初始化工作,随后会进行关键的从处理器的引导过程,然后是各种实质性的设备驱动的初始化,最后 创建系统的第一个用户进程init后进入用户空间执行用户进程宣誓内核初始化完成,可以进程正常的调度执行

    系统初始化阶段大多数都是主处理器做初始化工作,所有不用考虑处理器并发情况,一旦从处理器被bingup起来,调度器和各自的运行队列准备就绪,多个任务就会均衡到各个处理器,开始了并发的世界,一切是那么的神奇。

    3.spin-table

    从bootloader说起(以uboot为例):首先,上电后主处理器和从处理器都会启动,执行uboot,从uboot的_start的汇编代码开始执行,主处理器在uboot中欢快的执行后启动内核,进入内核执行,而从处理器会执行到spin_table_secondary_jump中(注意:之前执行的代码,设置的寄存器都是各cpu独立的寄存器)

    arch/arm/cpu/armv8/start.S:
    
    #if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_SPL_BUILD)
    		branch_if_master x0, master_cpu  //判断是否为主cpu(core0),是跳转到master_cpu,否则往下走
    		b	spin_table_secondary_jump  //跳转执行
    
    • 1
    • 2
    • 3
    • 4
    • 5

    从核执行:

    arch/arm/cpu/armv8/spin_table_v8.S:
    
    ENTRY(spin_table_secondary_jump)
    .globl spin_table_reserve_begin
    spin_table_reserve_begin:
    0:	wfe
    	ldr	x0, spin_table_cpu_release_addr
    	cbz	x0, 0b
    	br	x0
    .globl spin_table_cpu_release_addr
    	.align	3
    spin_table_cpu_release_addr:
    	.quad	0
    .globl spin_table_reserve_end
    spin_table_reserve_end:
    ENDPROC(spin_table_secondary_jump)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在spin_table_secondary_jump中:首先会执行wfe指令,使得从处理器睡眠等待。如果被唤醒,则从处理器会判断spin_table_cpu_release_addr这个地址是否为0,为0则继续跳转到wfe处继续睡眠,否则跳转到spin_table_cpu_release_addr指定的地址处执行。

    那么这个地址什么时候会被设置呢?答案是:主处理器在uboot中读取设备树的相关节点属性获得,我们来看下如何获得。执行路径为:

    do_bootm_linux
    ->boot_prep_linux
     ->image_setup_linux
      ->image_setup_libfdt
       ->arch_fixup_fdt
        ->spin_table_update_dt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在spin_table_update_dt函数中做了几件非常重要的事情:

    arch/arm/cpu/armv8/spin_table.c:
    
    int spin_table_update_dt(void *fdt)
    {
    	int cpus_offset, offset;
    	const char *prop;
    	int ret;
    	unsigned long rsv_addr = (unsigned long)&spin_table_reserve_begin;
    	unsigned long rsv_size = &spin_table_reserve_end -
    						&spin_table_reserve_begin;
    	
    //获取设备树的cpus节点的偏移
    	cpus_offset = fdt_path_offset(fdt, "/cpus");
    	if (cpus_offset < 0)
    		return -ENODEV;
    		
    //寻找每一个device_type属性为cpu的节点
    	for (offset = fdt_first_subnode(fdt, cpus_offset);
    	     offset >= 0;
    	     offset = fdt_next_subnode(fdt, offset)) {
    		prop = fdt_getprop(fdt, offset, "device_type", NULL);
    		if (!prop || strcmp(prop, "cpu"))
    			continue;
    
    		/*
    		 * In the first loop, we check if every CPU node specifies
    		 * spin-table.  Otherwise, just return successfully to not
    		 * disturb other methods, like psci.
    		 */
    		 *
    		 //获得enable-method属性,比较属性值是否为 "spin-table"(即是使用自旋表启动方式)
    		prop = fdt_getprop(fdt, offset, "enable-method", NULL);
    		if (!prop || strcmp(prop, "spin-table"))
    			return 0;
    	}
    
    	for (offset = fdt_first_subnode(fdt, cpus_offset);
    	     offset >= 0;
    	     offset = fdt_next_subnode(fdt, offset)) {
    		prop = fdt_getprop(fdt, offset, "device_type", NULL);
    		if (!prop || strcmp(prop, "cpu"))
    			continue;
    //重点:设置cpu-release-addr属性值为spin_table_cpu_release_addr的地址!
    		ret = fdt_setprop_u64(fdt, offset, "cpu-release-addr",
    				(unsigned long)&spin_table_cpu_release_addr);
    		if (ret)
    			return -ENOSPC;
    	}
    //设置设备树的保留内存 :添加一个内存区域为rsv_addr  <-> rsv_addr  + rsv_size 的地址范围(这是物理地址)
    	ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size);
    	if (ret)
    		return -ENOSPC;
    
    	printf("   Reserved memory region for spin-table: addr=%lx size=%lx\n",
    	       rsv_addr, rsv_size);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    其实,他做的工作主要有两个:

    1. 将即将供内核使用的设备树的cpu节点的cpu-release-addr属性设置为spin_table_cpu_release_addr的地址(这个地址也就是cpu的释放地址)。
    2. 将spin_table_reserve_begin到spin_table_reserve_end符号描述的地址范围添加到设备树的保留内存中

    **实际上保留的是spin_table_secondary_jump汇编函数的指令代码段和spin_table_cpu_release_addr地址内存,当然保留是为了在内核中不被内存管理使用,这样这段物理内存的数据不会被覆盖丢失。**注意:spin_table_cpu_release_addr地址处被初始化为0.

    先来看一下一个使用自旋表作为启动方式的平台设备树cpu节点:

    arch/arm64/boot/dts/xxx.dtsi:
    
       cpu@0 {
                            device_type = "cpu";
                            compatible = "arm,armv8";
                            reg = <0x0 0x000>;
                            enable-method = "spin-table";
                            cpu-release-addr = <0x1 0x0000fff8>;
                    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    可以发现启动方法为spin-table,释放地址初始化为0x10000fff8。
    那么什么时候释放地址spin_table_cpu_release_addr的内容不是0呢?

    那么我们得回到主处理器流程上来:主处理器设置好了设备树,传递给内核设备树地址之后就要启动内核,启动内核之后,执行初始化工作,执行如下路径:

    setup_arch   //arch/arm64/kernel/setup.c:
    ->smp_init_cpus  //arch/arm64/kernel/smp.c
     ->smp_cpu_setup
      ->cpu_ops[cpu]->cpu_init(cpu)
       ->smp_spin_table_ops->cpu_init  //arch/arm64/kernel/cpu_ops.c
        ->smp_spin_table_cpu_init//arch/arm64/kernel/smp_spin_table.c
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    我们来看下smp_spin_table_cpu_init函数:

    static int smp_spin_table_cpu_init(unsigned int cpu)
    {
    	struct device_node *dn;
    	int ret;
    
    	dn = of_get_cpu_node(cpu, NULL);
    	if (!dn)
    		return -ENODEV;
    
    	/*
    	 * Determine the address from which the CPU is polling.
    	 */
    	ret = of_property_read_u64(dn, "cpu-release-addr",
    				   &cpu_release_addr[cpu]);
    	if (ret)
    		pr_err("CPU %d: missing or invalid cpu-release-addr property\n",
    		       cpu);
    
    	of_node_put(dn);
    
    	return ret;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    可以发现,函数读取设备树的cpu-release-addr属性值到cpu_release_addr[cpu]中,cpu_release_addr变量是个NR_CPUS个元素的数组,每个处理器占用一个元素,其实也就是将之前保存的spin_table_reserve_begin符号的物理地址保存到这个变量中

    现在还没有看到设置释放地址的地方,继续往下看:

    主处理器继续执行如下路径:

    start_kernel
    ->arch_call_rest_init
     ->rest_init
      ->kernel_init,
       ->kernel_init_freeable
        ->smp_prepare_cpus  //arch/arm64/kernel/smp.c
         ->cpu_ops[cpu]->cpu_prepare
          ->smp_spin_table_ops->cpu_prepare//arch/arm64/kernel/cpu_ops.c
           ->smp_spin_table_cpu_prepare//arch/arm64/kernel/smp_spin_table.c
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    我们来看这个函数:

    static int smp_spin_table_cpu_prepare(unsigned int cpu)
    {
    	__le64 __iomem *release_addr;
    
    	if (!cpu_release_addr[cpu])
    		return -ENODEV;
    
    	/*
    	 * The cpu-release-addr may or may not be inside the linear mapping.
    	 * As ioremap_cache will either give us a new mapping or reuse the
    	 * existing linear mapping, we can use it to cover both cases. In
    	 * either case the memory will be MT_NORMAL.
    	 */
    	//将释放地址的物理地址映射为虚拟地址
    	release_addr = ioremap_cache(cpu_release_addr[cpu],
    				     sizeof(*release_addr));
    	if (!release_addr)
    		return -ENOMEM;
    
    	/*
    	 * We write the release address as LE regardless of the native
    	 * endianness of the kernel. Therefore, any boot-loaders that
    	 * read this address need to convert this address to the
    	 * boot-loader's endianness before jumping. This is mandated by
    	 * the boot protocol.
    	 */
    	 //将secondary_holding_pen地址写到释放地址处
    	writeq_relaxed(__pa_symbol(secondary_holding_pen), release_addr);
    	__flush_dcache_area((__force void *)release_addr,
    			    sizeof(*release_addr));
    
    	/*
    	 * Send an event to wake up the secondary CPU.
    	 */
    	//发送事件唤醒从处理器
    	sev();
    
    	iounmap(release_addr);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    上面函数主要做两点:

    1. cpu的释放地址处写入secondary_holding_pen的地址,由于获得的内核符号是虚拟地址所以转化为物理地址写到释放地址处
    2. 唤醒处于wfe状态的从处理器

    我们再次回到从处理器睡眠等待的地方:在汇编函数spin_table_secondary_jump中唤醒后执行,wfe的下几行指令,判断spin_table_cpu_release_addr地址处的内容是否为0,这个时候由于主处理器往这个地址写入了释放地址,所有会跳转到secondary_holding_pen处执行,请注意:这个地址是物理地址,而且从处理器还没有开启mmu,所以从处理器还没有进入虚拟地址的世界。

    获得释放地址后的从处理器,犹如脱缰的野马,唤醒后直接进入了内核的世界去执行指令,多么的残暴,来到了如下的汇编函数:

    arch/arm64/kernel/head.S:
    	/*
    	 * This provides a "holding pen" for platforms to hold all secondary
    	 * cores are held until we're ready for them to initialise.
    	 */
    SYM_FUNC_START(secondary_holding_pen)
    	bl	el2_setup			// Drop to EL1, w0=cpu_boot_mode
    	bl	set_cpu_boot_mode_flag
    	mrs	x0, mpidr_el1
    	mov_q	x1, MPIDR_HWID_BITMASK
    	and	x0, x0, x1
    	adr_l	x3, secondary_holding_pen_release
    pen:	ldr	x4, [x3]
    	cmp	x4, x0
    	b.eq	secondary_startup
    	wfe
    	b	pen
    SYM_FUNC_END(secondary_holding_pen)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    判断是否secondary_holding_pen_release被设置为了从处理器的编号,如果设置的不是我的编号,则我再次进入705行执行wfe睡眠等待,行吧,那就等待啥时候主处理器来将secondary_holding_pen_release设置为我的处理器编号吧。那么何时会设置呢?答案是最终要启动从处理器的时候。

    我们再次回到主处理器的处理流程,上面主处理器执行到了smp_prepare_cpus之后,继续往下执行,代码路径如下:

    start_kernel
    ->arch_call_rest_init
      ->rest_init
        ->kernel_init,
      ->kernel_init_freeable
       ->smp_prepare_cpus //arch/arm64/kernel/smp.c
        ->smp_init  //kernel/smp.c  (这是从处理器启动的函数)
        ->cpu_up
         ->do_cpu_up
          ->_cpu_up
           ->cpuhp_up_callbacks
            ->cpuhp_invoke_callback
             ->cpuhp_hp_states[CPUHP_BRINGUP_CPU]
              ->bringup_cpu
               ->__cpu_up  //arch/arm64/kernel/smp.c
                ->boot_secondary
                 ->cpu_ops[cpu]->cpu_boot(cpu)
                  ->smp_spin_table_ops.cpu_boot  //arch/arm64/kernel/cpu_ops.c
                   ->smp_spin_table_cpu_boot //arch/arm64/kernel/smp_spin_table.c
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    我们来看smp_spin_table_cpu_boot函数:

    /*
     * Write secondary_holding_pen_release in a way that is guaranteed to be
     * visible to all observers, irrespective of whether they're taking part
     * in coherency or not.  This is necessary for the hotplug code to work
     * reliably.
     */
    static void write_pen_release(u64 val)
    {
    	void *start = (void *)&secondary_holding_pen_release;
    	unsigned long size = sizeof(secondary_holding_pen_release);
    
    	secondary_holding_pen_release = val;
    	__flush_dcache_area(start, size);
    }
    
    static int smp_spin_table_cpu_boot(unsigned int cpu)
    {
    	/*
    	 * Update the pen release flag.
    	 */
    	write_pen_release(cpu_logical_map(cpu));
    
    	/*
    	 * Send an event, causing the secondaries to read pen_release.
    	 */
    	sev();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    可以看到这里将从处理器编号写到了secondary_holding_pen_release中,然后唤醒从处理器,从处理器再次欢快的执行,最后执行到secondary_startup,来做从处理器的初始化工作(如设置mmu,异常向量表等),最终从处理器还是处于wfi状态,但是这个时候从处理器已经具备了执行进程的能力,可以用来调度进程,触发中断等,和主处理器有着相同的地位.

    SYM_FUNC_START_LOCAL(secondary_startup)
    	/*
    	 * Common entry point for secondary CPUs.
    	 */
    	bl	__cpu_secondary_check52bitva
    	bl	__cpu_setup			// initialise processor
    	adrp	x1, swapper_pg_dir
    	bl	__enable_mmu
    	ldr	x8, =__secondary_switched
    	br	x8
    SYM_FUNC_END(secondary_startup)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    spin-table方式的多核启动方式,顾名思义在于自旋,主处理器和从处理器上电都会启动,主处理器执行uboot畅通无阻,从处理器在spin_table_secondary_jump处wfe睡眠,主处理器通过修改设备树的cpu节点的cpu-release-addr属性为spin_table_cpu_release_addr,这是从处理器的释放地址所在的地方,主处理器进入内核后,会通过smp_prepare_cpus函数调用spin-table 对应的cpu操作集的cpu_prepare方法从而在smp_spin_table_cpu_prepare函数中设置从处理器的释放地址为secondary_holding_pen这个内核函数,然后通过sev指令唤醒从处理器,从处理器继续从secondary_holding_pen开始执行(从处理器来到了内核的世界),发现secondary_holding_pen_release不是自己的处理编号,然后通过wfe继续睡眠,当主处理器完成了大多数的内核组件的初始化之后,调用smp_init来来开始真正的启动从处理器,最终调用spin-table 对应的cpu操作集的cpu_boot方法从而在smp_spin_table_cpu_boot将需要启动的处理器的编号写入secondary_holding_pen_release中,然后再次sev指令唤醒从处理器,从处理器得以继续执行(设置自己异常向量表,初始化mmu等),最终在idle线程中执行wfi睡眠。其他从处理器也是同样的方式启动起来,同样最后进入各种idle进程执行wfi睡眠,主处理器继续往下进行内核初始化,直到启动init进程,后面多个处理器都被启动起来,都可以调度进程,多进程还会被均衡到多核。

  • 相关阅读:
    部署bpmn项目实现activiti流程图的在线绘制
    【Java】获取手机文件名称补充
    ArrayList和linkedList的区别精简概述
    微服务篇-B 深入理解SOA框架(Dubbo)_I 服务注册和发现(学习总结)
    win10 安装 rabbitMQ详细步骤
    GitOps 介绍
    (个人杂记)第八章 按键输入实验
    git基本命令
    空气阻力对乒乓球运动轨迹的影响
    文献 | 柳叶刀发文:虚拟现实的新用途之治疗场所恐惧症
  • 原文地址:https://blog.csdn.net/weixin_44810385/article/details/133134018