加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 了解从进程内核堆栈获取task_struct指针

发布时间:2020-12-14 00:28:36 所属栏目:Linux 来源:网络整理
导读:现在我正在阅读Robert Love撰写的“ Linux内核开发3D版”一书.在那里他写了关于thread_info结构,其中包含指向task_struct结构的指针,据我所知,它位于进程内核堆栈的底部或顶部(取决于体系结构).直到最近我才熟悉Linux内核API,并且我不知道current()方法的存
现在我正在阅读Robert Love撰写的“ Linux内核开发3D版”一书.在那里他写了关于thread_info结构,其中包含指向task_struct结构的指针,据我所知,它位于进程内核堆栈的底部或顶部(取决于体系结构).直到最近我才熟悉Linux内核API,并且我不知道current()方法的存在.本书的摘录与current()方法的实际工作方式有关:

On x86,current is calculated by masking out the 13 least-significant bits of the stack
pointer to obtain the thread_info structure.This is done by the
current_thread_info() function.The assembly is shown here:
movl $-8192,%eax
andl %esp,%eax
This assumes that the stack size is 8KB.When 4KB stacks are enabled,4096 is used in
lieu of 8192.

我的问题是:

>据我所知,如果我们将十进制值表示为一组位,那么集合中只有一个最低有效位,不是吗?
>什么是神奇的数字13?

对于将阅读本主题的人,我所提出的问题可以得出结论,即作者不能正确理解内存分配和管理的过程.好吧,这可能是正确的,因为在我看来,我可以将为堆栈分配的内存表示为带满位(或字节).所有这些字节都可以通过特定的存储器地址访问,表示为一些十进制值.堆栈的原点是最低的内存地址,堆栈的fin是内存地址的最高值.但是,我们如何才能获得指向位于堆栈末端的thread_info结构的指针,只需通过屏蔽13个位于堆栈指针的ARBITRARY的最低有效位(如果我理解正确的话,我们屏蔽掉了堆栈指针ADDRESS表示为十进制值).

解决方法

内核堆栈顶部包含一个特殊的结构– thread_info:

26 struct thread_info {
 27         struct task_struct      *task;          /* main task structure */
 28         struct exec_domain      *exec_domain;   /* execution domain */
 29         __u32                   flags;          /* low level flags */
 30         __u32                   status;         /* thread synchronous flags */
 31         __u32                   cpu;            /* current CPU */
 32         int                     preempt_count;  /* 0 => preemptable,33                                                    <0 => BUG */
 34         mm_segment_t            addr_limit;
 35         struct restart_block    restart_block;
 36         void __user             *sysenter_return;
 37 #ifdef CONFIG_X86_32
 38         unsigned long           previous_esp;   /* ESP of the previous stack in
 39                                                    case of nested (IRQ) stacks
 40                                                 */
 41         __u8                    supervisor_stack[0];
 42 #endif
 43         unsigned int            sig_on_uaccess_error:1;
 44         unsigned int            uaccess_err:1;  /* uaccess failed */
 45 };

因此,要获取task_struct,您需要从ASM代码获取带有GET_THREAD_INFO的thread_info指针:

183 /* how to get the thread information struct from ASM */
184 #define GET_THREAD_INFO(reg)     
185         movl $-THREAD_SIZE,reg; 
186         andl %esp,reg

…或者使用C代码中的current_thread_info:

174 /* how to get the thread information struct from C */
175 static inline struct thread_info *current_thread_info(void)
176 {
177         return (struct thread_info *)
178                 (current_stack_pointer & ~(THREAD_SIZE - 1));
179 }

注意,对于x86_32和x86_64,定义为(PAGE_SIZE<<< THREAD_SIZE_ORDER)和THREAD_SIZE_ORDER的THREAD_SIZE等于1,因此THREAD_SIZE导致8192(2 ^ 13或1 <<<<<<< 13).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读