在 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. Linux学习笔记之三————Linux命令概述

    一.引言 很多人可能在电视或电影中看到过类似的场景,黑客面对一个黑色的屏幕,上面飘着密密麻麻的字符,梆梆一顿敲,就完成了窃取资料的任务. Linux 刚出世时没有什么图形界面,所有的操作全靠命令完成, ...

  2. SQL Server性能优化(15)选择合适的索引

    一.关于聚集索引列的选择(参考) 1. 聚集索引所在的列,或者列的组合最好是唯一的. 当我们创建的聚集索引的值不唯一时,SQL Server则无法仅仅通过聚集索引列(也就是关键字)唯一确定一行.此时, ...

  3. [java初探外篇]__关于StringBuilder类与String类的区别

    前言 我们前面学习到String类的相关知识,知道了它是一个字符串类,并且了解到其中的一些方法,但是当时并没有太过注意到String类的特点,今天就StringBuilder类的学习来比较一下两者的区 ...

  4. javascript的作用域和优先级

    变量的作用域是在定义时决定的,不是在运行时活动对象是在运行时决定的?如果就创建一个对象,使用完毕就完了,就使用json字面量的方式如果对象被反复创建,反复使用,就使用自定义的构造函数方式优先级内部变量 ...

  5. 动手实现react Modal组件

    Modal组件 长话不多说,接下来让我们来动手实现一个react Modal组件. 我们先来看一下实际效果 Modal的布局 首先,让我们先思考下一个Modal组件的布局是怎么样的. 我们先拿一个基本 ...

  6. 再会Java

    作者曾写过一段时间Java, 时间一长也就忘得差不多了. 现在重新学习一个, 故而只是提要式的记录. Java是静态强类型语言, 运行于Java虚拟机(Java Virtual Machine, JV ...

  7. SQL Where in (1,2,3,4) 换成字段一列的值

    ) ; , ) ) FROM r_resource WHERE id IN ( @resource) 换成 ) : , ) ) FROM r_resource )) SELECT cid,id FRO ...

  8. jquery只能输入数字

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 【Linux】CentOS7 安装rabbitmq

    [1.安装erlang环境]yum install http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64. ...

  10. 如何把string转换char*类型

    需要调用string头文件  ( #include<string> ) 用string里的函数c_str()可以把string转换为char* 例如 char * c_str2=str1. ...