【发布时间】:2019-05-25 05:42:42
【问题描述】:
我试图了解 current 宏的工作原理,因此开始浏览 Linux Kernel 源代码版本 4.19。试图了解 x86 架构
包括/asm-generic/current.h:8
#define get_current() (current_thread_info()->task)
#define current get_current()
然后我试图找到 current_thread_info() 的定义。
include/linux/thread_info.h
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
* For CONFIG_THREAD_INFO_IN_TASK kernels we need <asm/current.h> for the
* definition of current, but for !CONFIG_THREAD_INFO_IN_TASK kernels,
* including <asm/current.h> can cause a circular dependency on some platforms.
*/
#include <asm/current.h>
#define current_thread_info() ((struct thread_info *)current)
#endif
然后我试图找到当前的定义
arch/x86/include/asm/current.h
DECLARE_PER_CPU(struct task_struct *, current_task);
static __always_inline struct task_struct *get_current(void)
{
return this_cpu_read_stable(current_task);
}
#define current get_current()
get_current() 再次返回 struct task_struct,为什么我们要在 current_thread_info() 中将其类型转换为 struct thread_info。
您能否解释一下 current 是如何执行的。我在某处读到它位于内核堆栈的顶部或底部
【问题讨论】:
标签: c linux process linux-kernel