日期:2014-05-16  浏览次数:20692 次

【解答】arm架构的linux内核,软件线程切换的时候,TLB作废吗,cache作废吗?

首先明确:linux下,线程即进程

这种纠结的问题,必须读内核源码才能找到答案,ok,let's go!

armv8 内核中上下文切换代码:
1、linaro-aarch64/kernel/sched/core.c line 1852 context_switch==>该函数调用两个主要函数:switch_mm(),switch_to()如下所示

2、arch/arm64/include/asm/mmu_context.h line 134switch_mm()==>
/* check for possible thread migration */
if (!cpumask_empty(mm_cpumask(next)) &&
!cpumask_test_cpu(cpu, mm_cpumask(next)))
__flush_icache_all();
line 63 check_and_switch_context ()==》
arch/arm64/mm/proc.s cpu_do_switch_mm 没有对tlb进行操作,但是有icache的flush操作
3、include/asm-generic/switch_to.h line 25switch_to ==> arch/arm64/kernel/process.c line 297(__switch_to) ==>
arch/arm64/kernel/entry.s line 538 cpu_switch_to() :没有tlb flush操作

综上所述,没有发现上下文切换时,对tlb进行flush操作,但是在swtich_mm中,有flush icache的操作,其前提条件是cpu的mask不为空且cpu中的cpumask位不为0。

这里又涉及到cpumask,其作用如下:
 * Cpumasks provide a bitmap suitable for representing the
 * set of CPU's in a system, one bit position per CPU number.  In general,
 * only nr_cpu_ids (<= NR_CPUS) bits are valid.

cpumask代表一个cpu中核的位图,一个bit对应一个cpu,当然所有cpumask位数要小于等于总的cpu数才有效。