start_kernel——boot_cpu_init及PER_CPU
init/main.c
/*
* Activate the first processor.
*/
static void __init boot_cpu_init(void)
{
int cpu = smp_processor_id();
/* Mark the boot cpu "present", "online" etc for SMP and UP case */
set_cpu_online(cpu, true);
set_cpu_active(cpu, true);
set_cpu_present(cpu, true);
set_cpu_possible(cpu, true);
}
active第一个CPU。默认第一个CPU为boot CPU.
include/linux/smp.h
/*
* smp_processor_id(): get the current CPU ID.
*
* if DEBUG_PREEMPT is enabled then we check whether it is
* used in a preemption-safe way. (smp_processor_id() is safe
* if it's used in a preemption-off critical section, or in
* a thread that is bound to the current CPU.)
*
* NOTE: raw_smp_processor_id() is for internal use only
* (smp_processor_id() is the preferred variant), but in rare
* instances it might also be used to turn off false positives
* (i.e. smp_processor_id() use that the debugging code reports but
* which use for some reason is legal). Don't use this to hack around
* the warning message, as your code might not work under PREEMPT.
*/
#ifdef CONFIG_DEBUG_PREEMPT
extern unsigned int debug_smp_processor_id(void);
# define smp_processor_id() debug_smp_processor_id()
#else
# define smp_processor_id() raw_smp_processor_id()
#endif
得到当前CPU的ID
在我的代码中定义了CONFIG_DEBUG_PREEMPT宏,所以调用
lib/smp_processor.c
vnotrace unsigned int debug_smp_processor_id(void)
{
int this_cpu = raw_smp_processor_id();
if (likely(preempt_count()))
goto out;
if (irqs_disabled())
goto out;
/*
* Kernel threads bound to a single CPU can safely use
* smp_processor_id():
*/
if (cpumask_equal(tsk_cpus_allowed(current), cpumask_of(this_cpu)))
goto out;
/*
* It is valid to assume CPU-locality during early bootup:
*/
if (system_state != SYSTEM_RUNNING)
goto out;
/*
* Avoid recursion:
*/
preempt_disable_notrace();
if (!printk_ratelimit())
goto out_enable;
printk(KERN_ERR "BUG: using smp_processor_id() in preemptible [%08x] "
"code: %s/%d\n",
preempt_count() - 1, current->comm, current->pid);
print_symbol("caller is %s\n", (long)__builtin_return_address(0));
dump_stack();
out_enable:
preempt_enable_no_resched_notrace();
out:
return this_cpu;
}
EXPORT_SYMBOL(debug_smp_processor_id);
我们仅仅看raw_smp_processor_id函数:
acrh/x86/include/asm/smp.h
#ifdef CONFIG_X86_32_SMP
/*
* This function is needed by all SMP systems. It must _always_ be valid
* from the initial startup. We map APIC_BASE very early in page_setup(),
* so this is correct in the x86 case.
*/
#define raw_smp_processor_id() (this_cpu_read(cpu_number))
extern int safe_smp_processor_id(void);
#elif defined(CONFIG_X86_64_SMP)
#define raw_smp_processor_id() (this_cpu_read(cpu_number))
32位情况下调用this_cpu_read(cpu_number)
先看一下cpu_number的定义:
DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number);
EXPORT_PER_CPU_SYMBOL(cpu_number);
PER_CPU变量
定义一个type为int。name为cpu_number的PER_CPU变量
include/linux/percpu-defs.h
#define DEFINE_PER_CPU_READ_MOSTLY(type, name) \
DEFINE_PER_CPU_SECTION(type, name, "..readmostly")
include/linux/percpu-defs.h
/*
* s390 and alpha modules require percpu variables to be defined as
* weak to force the compiler to generate GOT based external
* references for them. This is necessary because percpu sections
* will be located outside of the usually addressable area.
*
* This definition puts the following two extra restrictions when
* defining percpu variables.
*
* 1. The symbol must be globally unique, even the static ones.
* 2. Static percpu variables cannot be defined inside a function.
*
* Archs which need weak percpu definitions should define
* ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary.
*
* To ensure that the generic code observes the above two
* restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak
* definition is used for all cases.
*/
#if defined(ARCH_NEEDS_WEAK_PER_CPU) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU)
/*
* __pcpu_scope_* dummy variable is used to enforce scope. It
* receives the static modifier when it's used in front of
* DEFINE_PER_CPU() and will trigger build failure if
* DECLARE_PER_CPU() is used for the same variable.
*
* __pcpu_unique_* dummy variable is used to enforce symbol uniqueness
* such that hidden weak symbol collision, which will cause unrelated
* variables to share the same address, can be detected during build.
*/
#define DECLARE_PER_CPU_SECTION(type, name, sec) \
extern __PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \
extern __PCPU_ATTRS(sec) __typeof__(type) name
#define DEFINE_PER_CPU_SECTION(type, name, sec) \
__PCPU_DUMMY_ATTRS char __pcpu_scope_##name; \
extern __PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
__PCPU_DUMMY_ATTRS char __pcpu_unique_##name; \
extern __PCPU_ATTRS(sec) __typeof__(type) name; \
__PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES __weak \
__typeof__(type) name
#else
/*
* Normal declaration and definition macros.
*/
#define DECLARE_PER_CPU_SECTION(type, name, sec) \
extern __PCPU_ATTRS(sec) __typeof__(type) name
#define DEFINE_PER_CPU_SECTION(type, name, sec) \
__PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES \
__typeof__(type) name
#endif
ARCH_NEEDS_WEAK_PER_CPU和CONFIG_DEBUG_FORCE_WEAK_PER_CPU都没有被定义,所以调用
__PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES \
__typeof__(type) name
即
__PCPU_ATTRS("..readmostly") PER_CPU_DEF_ATTRIBUTES \
__typeof__(int) cpu_number
__PCPU_ATTRS定义在include/linux/percpu-defs.h
/*
* Base implementations of per-CPU variable declarations and definitions, where
* the section in which the variable is to be placed is provided by the
* 'sec' argument. This may be used to affect the parameters governing the
* variable's storage.
*
* NOTE! The sections for the DECLARE and for the DEFINE must match, lest
* linkage errors occur due the compiler generating the wrong code to access
* that section.
*/
#define __PCPU_ATTRS(sec) \
__percpu __attribute__((section(PER_CPU_BASE_SECTION sec))) \
PER_CPU_ATTRIBUTES
__percpu在include/linux/compiler.h被定义为空
PER_CPU_ATTRIBUTES在include/asm-generic/percpu.h被定义为空
PER_CPU_DEF_ATTRIBUTES在include/asm-generic/percpu.h被定义为空
PER_CPU_BASE_SECTION定义在include/asm-generic/percpu.h
#ifndef PER_CPU_BASE_SECTION
#ifdef CONFIG_SMP
#define PER_CPU_BASE_SECTION ".data..percpu"
#else
#define PER_CPU_BASE_SECTION ".data"
#endif
#endif
我的代码中PER_CPU_BASE_SECTION被定义为”.data..percpu”
所以终于展开得到
__attribute__((section(".data..percpu" "..readmostly")))
__typeof__(int) cpu_number
“gcc支持一种叫做类型识别的技术。通过typeof(x)关键字,获得x的数据类型。而如果是在一个要被一些c文件包括的头文件里获得变量的数据类型。就须要用_typeof_而不是typeof关键字了,比方说我们这里。
最后。这里就是声明一个int类型的cpu_number变量,编译的时候把他指向.data..percpu段的開始位置”
(引號内这段摘抄自http://blog.chinaunix.net/uid-27717694-id-4033413.html)
我们回到raw_smp_processor_id()宏。看一下this_cpu_read(cpu_number)
include/linux/percpu.h
# define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, (pcp))
include/linux/percpu.h
#define __pcpu_size_call_return(stem, variable) \
({ typeof(variable) pscr_ret__; \
__verify_pcpu_ptr(&(variable)); \
switch(sizeof(variable)) { \
case 1: pscr_ret__ = stem##1(variable);break; \
case 2: pscr_ret__ = stem##2(variable);break; \
case 4: pscr_ret__ = stem##4(variable);break; \
case 8: pscr_ret__ = stem##8(variable);break; \
default: \
__bad_size_call_parameter();break; \
} \
pscr_ret__; \
})
再往下就涉及汇编代码了。我们不分析详细架构。
回到boot_cpu_init函数接下来运行:
/* Mark the boot cpu "present", "online" etc for SMP and UP case */
set_cpu_online(cpu, true);
set_cpu_active(cpu, true);
set_cpu_present(cpu, true);
set_cpu_possible(cpu, true);
对boot cpu进行一系列的设置:
先看set_cpu_online,这里採用位图对CPU变量进行管理,
linux/kernel/cpu.c
void set_cpu_online(unsigned int cpu, bool online)
{
if (online)
cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
else
cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
}
cpu_online_bits的定义:
linux/kernel/cpu.c
static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
include/linux/types.h
#define DECLARE_BITMAP(name,bits) \
unsigned long name[BITS_TO_LONGS(bits)]
展开后得到
unsigned long cpu_online_bits[BITS_TO_LONGS(CONFIG_NR_CPUS)] __read_mostly;
CONFIG_NR_CPUS is maximum supported processors.
与详细的架构相关。
BITS_TO_LONGS定义在include/linux/bitops.h
#define BITS_PER_BYTE 8
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
include/linux/bitops.h
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
DIV_ROUND_UP表示向上圆整,是一种算法,保证数组的维度不小于nr。而且多出的部分不会太大。
所以这里实际上是定义了一个名为cpu_online_bits,类型为unsigned long的数组。
如果有一个4核CPU。那么CONFIG_NR_CPUS的值为4,而long是32位,sizeof(long)为4,那么DIV_ROUND_UP(4, 8*4)的值为1.即unsigned long bitmap[1],一共32位,足够4个CPU用了。
to_cpumask对cpu_online_bits数组进行转化:
/**
* to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
* @bitmap: the bitmap
*
* There are a few places where cpumask_var_t isn't appropriate and
* static cpumasks must be used (eg. very early boot), yet we don't
* expose the definition of 'struct cpumask'.
*
* This does the conversion, and can be used as a constant initializer.
*/
#define to_cpumask(bitmap) \
((struct cpumask *)(1 ? (bitmap) \
: (void *)sizeof(__check_is_bitmap(bitmap))))
事实上就是将bitmap转化为struct cpumask *类型。
include/linux/cpumask.h
typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
include/linux/threads.h
/* Places which use this should consider cpumask_var_t. */
#define NR_CPUS CONFIG_NR_CPUS
它的成员也是一个unsigned long 类型的数组。
include/linux/cpumask.h
/**
* cpumask_set_cpu - set a cpu in a cpumask
* @cpu: cpu number (< nr_cpu_ids)
* @dstp: the cpumask pointer
*/
static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
{
set_bit(cpumask_check(cpu), cpumask_bits(dstp));
}
接下来就是设置bit位。不同架构相应不同的汇编。我们不准备分析。
以上是对set_cpu_online的分析,其它几个函数也差点儿相同。
这里能够看到,非常多cpu相关的变量是用位图形式进行管理的。通过操作位图。可改变cpu的状态。
start_kernel——boot_cpu_init及PER_CPU的更多相关文章
- 第3阶段——内核启动分析之start_kernel初始化函数(5)
内核启动分析之start_kernel初始化函数(init/main.c) stext函数启动内核后,就开始进入start_kernel初始化各个函数, 下面只是浅尝辄止的描述一下函数的功能,很多函数 ...
- Linux移植之内核启动过程start_kernel函数简析
在Linux移植之内核启动过程引导阶段分析中从arch/arm/kernel/head.S开始分析,最后分析到课start_kernel这个C函数,下面就简单分析下这个函数,因为涉及到Linux的内容 ...
- linux源码分析(五)-start_kernel
前置:这里使用的linux版本是4.8,x86体系. local_irq_disable(); 这个函数是做了关闭中断操作.和后面的local_irq_enable相对应.说明启动的下面函数是不允许被 ...
- arm linux kernel启动之start_kernel
了解完kernel启动以前的汇编之后我们来看看正式的c语言启动代码,也就是我们的start_kernel函数了.start_kernel相当大,里面每一个调用到的函数都足够我们伤脑筋了,我这里只是浅尝 ...
- 实验三:gdb跟踪调试内核从start_kernel到init进程启动
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...
- 全局启动函数start_kernel函数注解
asmlinkage void __init start_kernel(void) { char * command_line; extern struct kernel_param __start_ ...
- linux源码分析(三)-start_kernel
前置:这里使用的linux版本是4.8,x86体系. start_kernel是过了引导阶段,进入到了内核启动阶段的入口.函数在init/main.c中. set_task_stack_end_mag ...
- Linux内核启动过程start_kernel分析
虽然题目是start_kernel分析,但是由于我在ubuntu环境下配置实验环境遇到了一些问题,我觉得有必要把这些问题及其解决办法写下来. 首先我使用的是Ubuntu14.04 amx64,以下的步 ...
- arm linux kernel 从入口到start_kernel 的代码分析
参考资料: <ARM体系结构与编程> <嵌入式Linux应用开发完全手册> Linux_Memory_Address_Mapping http://www.chinaunix. ...
随机推荐
- 在Eclipse中创建Maven多模块项目
在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...
- EntboostChat 0.9(越狱版)公布,iOS免费企业IM
恩布互联公布IOS免费企业IM 0.9越狱预览版本号,支持全部iPhone4/5手机(6未上真机測试),iPad平板,主要功能包含单聊.群聊,企业组织结构,文本.表情.图片.文件.截图.离线消息等: ...
- ADT、C和Java
<编程导论(Java)·5 链表.数组和栈> 数据抽象使得用户程序猿在编写客户程序时,摆脱该数据类型的实现细节而只关心该数据类型的接口.在计算机科学中.有一些重要的数据抽象--数据结构,应 ...
- tensorflow利用预训练模型进行目标检测(三):将检测结果存入mysql数据库
mysql版本:5.7 : 数据库:rdshare:表captain_america3_sd用来记录某帧是否被检测.表captain_america3_d用来记录检测到的数据. python模块,包部 ...
- 最大似然估计的缺陷 —— 方差和均值的 bias
0. 均匀分布期望的最大似然估计 首先我们来看,如何通过最大似然估计的形式估计均匀分布的期望.均匀分布的概率密度函数为:f(x|θ)=1θ,0≤x≤θ.不失一般性地,将 x1,x2,-,xn 排序为顺 ...
- Node.js:连接 MongoDB
ylbtech-Node.js:连接 MongoDB 1.返回顶部 1. Node.js 连接 MongoDB MongoDB是一种文档导向数据库管理系统,由C++撰写而成. 本章节我们将为大家介绍如 ...
- ubuntu下Chrome谷歌浏览器部分网站图片显示不正常的解决方法
title: ubuntu下Chrome谷歌浏览器部分网站图片显示不正常的解决方法 toc: false date: 2018-09-02 14:37:26 categories: methods t ...
- 一个登录页面的spring 逻辑过程
1.首先用户访问login.jsp 2.用户在登录页面输入用户名/密码,提交表单到服务器,Spring根据配置调用LoginController控制器响应登录请求(关键) 3.LoginControl ...
- BZOJ 4184 线段树+高斯消元
思路: 线段树表示的是时间 每回最多log个段 区间覆盖 一直到叶子 的线性基 xor 一下 就是答案 一开始没有思路 看了这篇题解 豁然开朗 http://www.cnblogs.com/joyou ...
- python3 pymysql学习笔记
练手项目需要用到mysql就顺手把mysql也学了,这个模块没什么好说的,比较简单,实际整个过程我都是在学mysql语句,但还是发现了一些问题. fetchall() 获取结果集中的所有行 这个函数难 ...