include/linux/sched.h

zhi_oo 2015-06-24 05:52:48
2501 #define task_thread_info(task) ((struct thread_info *)(task)->stack)
2502 #define task_stack_page(task) ((task)->stack)
2503
2504 static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
2505 {
2506 *task_thread_info(p) = *task_thread_info(org);
2507 task_thread_info(p)->task = p;
2508 }

2501行中为什么要强转成 struct thread_info *?看2506行中对这个宏的使用,传进去的是一个struct task_struct *。而且通过代码逻辑看,它要做的也是取task_struct 中的stack啊。
在commit f7e4217b007d1f73e7e3cf10ba4fea4a608c603f 中,“rename thread_info to stack” 是不是提这个patch的时候忽视了?
...全文
403 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
WULALA123456WULALA 2015-09-15
  • 打赏
  • 举报
回复
current_thread_info和task_thread_info(current)是同一个意义。都是返回当前进程对应的thread_info结构体。 只是task_thread_info还可以用来获取其他进程的thread_info而已。 你看2.6.32的代码: task_thread_info 就是(struct thread_info *)(task)->stack如果是current的话就是(struct thread_info *)(current)->stack 而研究进程的fork过程中,dup_task_struct函数中stack字段就是赋值的就是为新进程新分配的thread_info类型的指针。也就是stack字段存储的就是进程thread_info地址。 current_thread_info获取当前进程thread_info是基于linux内核把内核栈和thread_info存储在一段连续内存的原理(4K页面或者8k页面,网上有那个图,就是栈从那块内存的高地址往下生长,thread_info是在那块内存的低地址往上存储)可以参看union thread_union结构体 current_thread_info代码 register unsigned long current_stack_pointer asm("esp") __used static inline struct thread_info *current_thread_info(void) { return (struct thread_info *)(current_stack_pointer & ~(THREAD_SIZE - 1)); } 是通过esp(esp寄存器对应就是内核栈顶的地址)获取后相与去掉后12位或者13位(具体的可以写内核模块打印THREAD_SIZE, 如果是8192那就是10 0000 0000 0000,那就是相与掉后13位)得到的就是thread_info的地址。
zhi_oo 2015-06-25
  • 打赏
  • 举报
回复
引用 1 楼 brookmill 的回复:
注意运算符优先级: (struct thread_info *)(task)->stack 等价于 (struct thread_info *)((task)->stack) 所以,这里是先取task_struct 中的stack,再把结果转换类型
感谢楼上的回答。的确如此。stack描述的原来就是thread_info的地址。 还有一个问题: current_thread_info()与 task_thread_info(current) 的返回值是不是同一个意义? 分别代表什么?
brookmill 2015-06-25
  • 打赏
  • 举报
回复
注意运算符优先级: (struct thread_info *)(task)->stack 等价于 (struct thread_info *)((task)->stack) 所以,这里是先取task_struct 中的stack,再把结果转换类型

4,441

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧