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

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

概述现在我正在阅读Robert Love撰写的“ Linux内核开发3D版”一书.在那里他写了关于thread_info结构,其中包含指向task_struct结构的指针,据我所知,它位于进程内核堆栈的底部或顶部(取决于体系结构).直到最近我才熟悉Linux内核API,并且我不知道current()方法的存在.本书的摘录与current()方法的实际工作方式有关: On x86, current is 现在我正在阅读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)     5         movl $-THREAD_SIZE,reg; 6         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).

总结

以上是内存溢出为你收集整理的linux – 了解从进程内核堆栈获取task_struct指针全部内容,希望文章能够帮你解决linux – 了解从进程内核堆栈获取task_struct指针所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/1023470.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存