簡體   English   中英

無法了解“當前”宏如何用於x86體系結構

[英]Unable to understand how the “current” macro works for x86 architecture

我試圖了解current宏的工作原理,因此開始瀏覽Linux內核源代碼版本4.19。 試圖了解x86體系結構

包括/ ASM-通用/ current.h:8

#define get_current() (current_thread_info()->task)
#define current get_current()

然后,我試圖找到current_thread_info()的定義。

包括/ 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

然后我試圖找到當前的定義

拱/ 86 /包含/ 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。

您能解釋一下如何執行電流嗎? 我讀到它放在內核堆棧頂部或底部的某個地方

為了強制轉換指針struct thread_info thread_infostruct task_struct第一個成員

struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
    /*
     * For reasons of header soup (see current_thread_info()), this
     * must be the first element of task_struct.
     */
    struct thread_info      thread_info;
#endif

強制轉換是合法的-我們返回一個指向struct的第一個成員的指針。 它可能曾經使用過&current->thread_info但是如果在某些情況下struct task_struct的定義不透明 (即它是不完整的類型),則不能使用它!


至於DECLARE_PER_CPU東西的工作方式,這取決於。 您過去所學的內容可能不再適用於此。 使用特殊宏讀取和更新使用DECLARE_PER_CPU聲明的變量。 其他這是因為在x86上,讀取是通過特殊的段寄存器進行的。 然后,其他CPU架構必須使用某些完全不同的方法來訪問每CPU值。

通常,應使用this_cpu_read讀取per-cpu變量,這不允許GCC以任何方式對其進行緩存,但是當前線程信息是一個例外,因為當前線程始終在當前線程中運行,而不this_cpu_read在哪個CPU上運行。 arch/x86/include/asm/percpu.h

/*
 * this_cpu_read() makes gcc load the percpu variable every time it is
 * accessed while this_cpu_read_stable() allows the value to be cached.
 * this_cpu_read_stable() is more efficient and can be used if its value
 * is guaranteed to be valid across cpus.  The current users include
 * get_current() and get_thread_info() both of which are actually
 * per-thread variables implemented as per-cpu variables and thus
 * stable for the duration of the respective task.
 */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM