在 Linux系统中,对于多核的ARM芯片而言,在Biotron代码中,每个CPU都会识别自身ID,如果ID是0,则引导Bootloader和 Linux内核执行,如果ID不是0,则Biotron一般在上电时将自身置于WFI或者WFE状态,并等待CPU0给其发CPU核间中断或事件(一般通过SEV指令)以唤醒它。一个典型的多核 Linux启动过程如图20.6所示。

被CPU0唤醒的CPUn可以在运行过程中进行热插拔,譬如运行如下命令即可卸载CPU1,并且将CPUI上的任务全部迁移到其他CPU中:

# echo 0 > /sys/devices/system/cpu/cpu1/online

同理,运行如下命令可以再次启动CPU1:

# echo 1 > /sys/devices/system/cpu/cpu1/online

之后CPU1会主动参与系统中各个CPU之间的运行任务的负载均衡工作;

CPUO唤醒其他CPU的动作在内核中被封装为一个 smp_operations的结构体,对于ARM而言,它定义于 arch/arm/include/asm/smp.h中。该结构体的成员函数如代码清单所示。


struct smp_operations {
#ifdef CONFIG_SMP
/*
* Setup the set of possible CPUs (via set_cpu_possible)
*/
void (*smp_init_cpus)(void);
/*
* Initialize cpu_possible map, and enable coherency
*/
void (*smp_prepare_cpus)(unsigned int max_cpus); /*
* Perform platform specific initialisation of the specified CPU.
*/
void (*smp_secondary_init)(unsigned int cpu);
/*
* Boot a secondary CPU, and assign it the specified idle task.
* This also gives us the initial stack to use for this CPU.
*/
int (*smp_boot_secondary)(unsigned int cpu, struct task_struct *idle);
#ifdef CONFIG_HOTPLUG_CPU
int (*cpu_kill)(unsigned int cpu);
void (*cpu_die)(unsigned int cpu);
int (*cpu_disable)(unsigned int cpu);
#endif
#endif
};

CPUO唤醒其他CPU的动作在内核中被封装为一个 smp_operations 的结构体,对于ARM而言,它定义于 arch/arm/include/asm/smp.h中。该结构体的成员函数如代码清单所示。

DT_MACHINE_START(VEXPRESS DT,"ARM-Versatile Express)
.dt_compat = v2m_dt_match,
.smp = smp_ops(express_smp_ops),
.map_io = v2m_dt_map_io,
MACHINE_END

通过 arch/arm/mach-vexpress/platsmp.c的实现代码可以看出, smp_operations的成员函数smp_init_cpus(),即 vexpress_smp_init_cpus调用的ct_ca9x4_init_cpu_map(会探测SoC内CPU核的个数,并通过 set_cpu_possible设置这些CPU可见。

smp_operations的成员函数 smp_prepare_cpus,即 vexpress_smp_prepare_cpus则会通过v2m_flags_set( virt_to_phys( versatile_secondary_startup)设置其他CPU的启动地址为versatile_secondary_startup,如代码清单所示。

smp_prepare_cpus()设置CPU1...n启动地址:


static void __init vexpress_smp_prepare_cpus(unsigned int max_cpus)
{
/*
* Initialise the present map, which describes the set of CPUs
* actually populated at the present time.
*/
if (ct_desc)
ct_desc->smp_enable(max_cpus);
else
vexpress_dt_smp_prepare_cpus(max_cpus); /*
* Write the address of secondary startup into the
* system-wide flags register. The boot monitor waits
* until it receives a soft interrupt, and then the
* secondary CPU branches to this address.
*/
vexpress_flags_set(virt_to_phys(versatile_secondary_startup));
}

注意这部分具体实现方式是与SOC相关的,由芯片设计及芯片内部的Bootrom决定。对于VEXPRESS来讲,设置方法如下:

void __init v2m_flags_set(u32 data)
{
writel(~0, v2m_sysreg_base + V2M_SYS_FLAGSCLR);
writel(data, v2m_sysreg_base + V2M_SYS_FLAGSCLR);
}

即填充 v2m_sysreg_base+V2M_SYS_FLAGSCLR标记清除寄存器为0xFFFFFFFF,将CPU1...n初始启动执行的指令地址填入v2m_sysreg_base+V2M_SYS_FLAGSSET寄存器。这两个地址由芯片内部的Bootrom程序设定的。填入的CPU1...n的起始地址都通过virt_to_phys转化为物理地址,因为此时CPU1...n的MMU尚未开启;

比较关键的是smp_operations的成员函数smp_boot_secondary(),它是完成CPU最终唤醒的工作,对于本例而言,versatile_boot_secondary()

CPU0通过终端唤醒其他CPU:

/*
* Write 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 __cpuinit write_pen_release(int val)
{
pen_release = val;
smp_wmb();
__cpuc_flush_dcache_area((void *)&pen_release, sizeof(pen_release));
outer_clean_range(__pa(&pen_release), __pa(&pen_release + 1));
}
int __cpuinit versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
{
unsigned long timeout; /*
* Set synchronisation state between this boot processor
* and the secondary one
*/
spin_lock(&boot_lock); /*
* This is really belt and braces; we hold unintended secondary
* CPUs in the holding pen until we're ready for them. However,
* since we haven't sent them a soft interrupt, they shouldn't
* be there.
*/
write_pen_release(cpu_logical_map(cpu)); /*
* Send the secondary CPU a soft interrupt, thereby causing
* the boot monitor to read the system wide flags register,
* and branch to the address found there.
*/
arch_send_wakeup_ipi_mask(cpumask_of(cpu)); timeout = jiffies + (1 * HZ);
while (time_before(jiffies, timeout)) {
smp_rmb();
if (pen_release == -1)
break; udelay(10);
} /*
* now the secondary core is starting up let it run its
* calibrations, then wait for it to finish
*/
spin_unlock(&boot_lock); return pen_release != -1 ? -ENOSYS : 0;
}

调用的 write_pen_release会将 pen_release变量设置为要唤醒的CPU核的CPU号 cpu_logical_map(cpu),而后通过 arch_send_wakeup_ipi mask给要唤醒的CPU发IPI中断,这个时候,被唤醒的CPU会退出WFI状态并从前面 smp_operations中的smp_prepare_cpus成员函数,即 vexpress_smp_prepare_cpus里通过 v2m_flags_set()设置的起始地址 versatile_secondary_startup开始执行,如果顺利的话,该CPU会将原先为正数的pen_release写为-1,以便CPU0从等待pen_release成为-1的循环跳出;

versatile_secondary_startup实现于arch/arm/plat-versatile/headsmp.S中,是一段汇编,如下代码所示:


/*
* Realview/Versatile Express specific entry point for secondary CPUs.
* This provides a "holding pen" into which all secondary cores are held
* until we're ready for them to initialise.
*/
ENTRY(versatile_secondary_startup)
mrc p15, 0, r0, c0, c0, 5
bic r0, #0xff000000
adr r4, 1f
ldmia r4, {r5, r6}
sub r4, r4, r5
add r6, r6, r4
pen: ldr r7, [r6]
cmp r7, r0
bne pen /*
* we've been released from the holding pen: secondary_stack
* should now contain the SVC stack for this core
*/
b secondary_startup .align
1: .long .
.long pen_release
ENDPROC(versatile_secondary_startup)

上述循环代码的循环是等待pen_release变量称为CPU0设置的cpu_logical_map(cpu),一般就直接成立了。第16行调用内核通用的secondary_startup()函数,经过一系列初始化(如MMU等),最终新的被唤醒的CPU将调用smp_operationssmp_secondary_init()的成员函数,对于本例为versatile_secondary_init()

void __cpuinit versatile_secondary_init(unsigned int cpu)
{
/*
* let the primary processor know we're out of the
* pen, then head off into the C entry point
*/
write_pen_release(-1); /*
* Synchronise with the boot thread.
*/
spin_lock(&boot_lock);
spin_unlock(&boot_lock);
}

上述代码会将pen_release写为-1,于是CPU0还在执行代码的versatile_boot_secondary()函数中的如下循环就退出了:

timeout = jiffies + (1 * HZ);
while (time_before(jiffies, timeout)) {
smp_rmb();
if (pen_release == -1)
break; udelay(10);
}

这样CPU0就知道目标CPU已经被正确地唤醒,此后CPU0和新唤醒的其他CPU各自运行。整个系统在运行过程中会进行实时进程和正常进程的动态负载均衡。

下图总结了前文提到的vexpress_smp_prepare_cpus()versatile_boot_secondary()write_pen_release()versatile_secondary_startup()versatile_secondary_init()这些函数的执行顺序;

SMP多核启动的更多相关文章

  1. [转]Linux芯片级移植与底层驱动(基于3.7.4内核)

      1.   SoC Linux底层驱动的组成和现状 为了让Linux在一个全新的ARM SoC上运行,需要提供大量的底层支撑,如定时器节拍.中断控制器.SMP启动.CPU hotplug以及底层的G ...

  2. 使用PSCI机制的SMP启动分析

    其他core的入口 文件:arch/arm64/kernel/head.S secondary_entry: 在从bl31切到EL1上的Linux Kernel后: 第595行,在el2_setup中 ...

  3. ARM Linux 内核 panic 之cache 一致性 ——Cortex-A9多核cache和TLB一致性广播

    ARM Linux 内核 panic 之cache 一致性 ——Cortex-A9多核cache和TLB一致性广播 Cortex-A9的多喝CPU可以接收和执行一致性广播操作,当其使能并处于SMP模式 ...

  4. ChCore Lab4 多核处理 实验笔记

    本文为上海交大 ipads 研究所陈海波老师等人所著的<现代操作系统:原理与实现>的课程实验(LAB)的学习笔记的第四篇:多核处理.所有章节的笔记可在此处查看:chcore | 康宇PL' ...

  5. v87.01 鸿蒙内核源码分析 (内核启动篇) | 从汇编到 main () | 百篇博客分析 OpenHarmony 源码

    本篇关键词:内核重定位.MMU.SVC栈.热启动.内核映射表 内核汇编相关篇为: v74.01 鸿蒙内核源码分析(编码方式) | 机器指令是如何编码的 v75.03 鸿蒙内核源码分析(汇编基础) | ...

  6. python之进程与线程

    什么是操作系统       可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...

  7. 通过多线程处理提高Redis性能

    Redis通常被称为单进程单线程模型. 这不是真的! Redis还运行多个后端线程来执行后端清理工作,例如清理脏数据和关闭文件描述符.在Redis中,主线程负责主要任务,包括但不限于:接收来自客户端的 ...

  8. Linux中断管理 (3)workqueue工作队列

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

  9. 转载:2.1 运行中的Nginx进程间的关系《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19624.html 在正式提供服务的产品环境下,部署Nginx时都是使用一个master进程来管理多个worker进程,一般情况下,w ...

随机推荐

  1. 看看一个老程序员如何手写SpringMVC!

    人见人爱的Spring已然不仅仅只是一个框架了.如今,Spring已然成为了一个生态.但深入了解Spring的却寥寥无几.这里,我带大家一起来看看,我是如何手写Spring的.我将结合对Spring十 ...

  2. VMware12 安装 Ubuntu18.04

    安装Ubuntu18.04虚拟机 Ubuntu获取地址: 官网:https://www.ubuntu.com/download/server 清华镜像站:https://mirrors.tuna.ts ...

  3. resin远程调试配置

    1.进入resin的安装路径下的conf目录,下面有个resin.conf的文件,打开它,将下面这段配置添加进去,然后重启resin(大家应该知道如何重启吧): <jvm-arg>-Xde ...

  4. 13-03 Java 基本类型包装类概述,Integer类,Character

    基本类型包装类概述 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据.常用的操作之一:用于基本数据类型与字符串之间的转换.基本类型和包装类的对应Byte,Short,Inte ...

  5. Maven项目打包成可执行Jar文件

    在使用Maven完成项目以后,如果需要打包成可执行的Jar文件,我们通过eclipse的导出很麻烦,还得指定入口文件的位置,还得说明依赖的jar包,既然都使用Maven了,很重要的一个目的就是让这些繁 ...

  6. 使用flexible适配移动端h5页面

    flexible是淘宝提供的一套REM手机适配的库,用法也非常简单 首先,在页面中引入相关资源 包括flexible.js和flexible_css.js(用于清除默认样式),或者通过cdn方式引入 ...

  7. HTML XML 介绍

    一. HTML(HyperTextMark-upLanguage)即超文本标记语言,是WWW的描述语言. 二. XML即ExtentsibleMarkup Language(可扩展标记语言), XML ...

  8. Redis持久化存储与复制功能简述

    一.分布式系统基础理论 分布式系统的两个基础理论: 1.CAP理论 如图: Consistency(强一致性):数据一致更新,所有数据变动都是同步的.Availability(可用性):好的响应性能. ...

  9. ARM常用汇编指令列表 --- 转自百度文库

  10. 【原】PHP从入门到精通2小时【图文并茂】

    原创内容,转载请注明. 主要内容: 搭建PHP开发环境 第一个helloworld程序 变量 全局变量 循环结构 函数 数组 面向对象编程 继承 接口 多态 日志 文件的读写 时间格式和时区 创建图形 ...