簡體   English   中英

Linux內核中的current_thread_info()內聯函數?

[英]current_thread_info() inline function in Linux kernel?

我了解到thread_info存儲在堆棧的底部。 在查看內核的源代碼時,我試圖了解如何在linux內核中獲取當前的thread_info?
下面的源代碼是current_stack_pointer的13位屏蔽。

這是我無法得到的。 我不明白th​​read_info的位置會發生變化。 為什么它是當前的堆棧指針而不是堆棧的開始?

請幫我理解這段代碼

/*
 * how to get the current stack pointer in C
 */
register unsigned long current_stack_pointer asm ("sp");

/*
 * how to get the thread information struct from C
 */
static inline struct thread_info *current_thread_info(void) __attribute_const__;

static inline struct thread_info *current_thread_info(void)
{
    return (struct thread_info *)
        (current_stack_pointer & ~(THREAD_SIZE - 1));
}

為什么它是當前的堆棧指針而不是堆棧的開始?

您總是可以從平台特定的堆棧指針寄存器中獲取當前堆棧指針,但是您無法輕松找到堆棧的開頭 - 它就不存在了。

為此,您可以限制線程堆棧在內存中的位置:堆棧始終在內存中按其大小保持對齊,其大小始終為2的冪。 這樣,如果堆棧大小為2^n ,則將較低的n歸零可以啟動堆棧。

在現代Linux堆棧中,通常為8 KiB,並且始終為8 KiB對齊(其地址的低13位始終為0)。 將當前堆棧指針的低13位歸零可以啟動堆棧。

這正是表達式current_stack_pointer & ~(THREAD_SIZE - 1)作用:查找當前堆棧的開始。 這並不意味着struct thread_info在內存中移動 - 它不會。 即使堆棧指針發生變化,將較低位置零也會在線程中給出相同的值。 這就是為什么它也用__attribute_const__標記,它擴展為__attribute__((const))並告訴GCC這個函數總是返回相同的值,這樣就可以省略對這個函數的多次調用。


PS:當堆棧向下增長時,“當前堆棧的開始”是指最低地址。

暫無
暫無

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

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