在 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. Docker总结(脑图图片)

  2. Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权

    Spring Boot 集成 Swagger2 很简单,由于接口采用了OAuth2.0 & JWT 协议做了安全验证,使用过程中也遇到了很多小的问题,多次尝试下述配置可以正常使用. Maven ...

  3. Myeclipse--jBPM4.3插件

    http://www.baidupcs.com/file/c7f3b8fc57b056567b37d081b1bcd21e?xcode=3966699596a0e8ec88581bd8407457f9 ...

  4. Hadoop-调优剖析

    1.概述 其实,在从事过调优相关的工作后,会发现其实调优是一项较为复杂的工作.而对于Hadoop这样复杂且庞大的系统来说,调优更是一项巨大的工作,由于Hadoop包含Common.HDFS.MapRe ...

  5. Spring Cloud Consul使用——配置中心

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  6. vue-01

    1, vue优势 虚拟daom, 易用, 灵活, 高效 2, 介绍 渐进式框架 3, 兼容性 es5的星特性, 不支持ie8 4, 新版本内置 webpack

  7. 自动化发布-GitLab WEB Hooks 配置

    钩子(hooks) hooks是在特定事件发生之前或之后执行特定脚本代码功能(从概念上类比,就与监听事件.触发器之类的东西类似). Git hooks就是那些在Git执行特定事件(如commit.pu ...

  8. 获取DNS服务器的版本信息

    1 如何获取DNS服务器的版本信息 向某个DNS服务器发送下面的请求即可获得版本信息 dig @115.124.17.156 version.bind chaos txt 它返回的信息如下: ; &l ...

  9. 记一次IDEA编译器调优

    前言: 我们知道,IDEA是用Java写的,那么他肯定也存在虚拟机的调优的问题,那么今天我们就对它进行开刀. 下面是默认参数 位置在:C:\Program Files\JetBrains\Intell ...

  10. [转]Windows下配置Node.js和Cordova

    本文转自:https://blog.csdn.net/weixin_37730482/article/details/74388056?locationNum=3&fps=1 本文讲解在win ...