内核 current宏解析
来源:互联网 发布:网络用语薯片什么意思 编辑:程序博客网 时间:2024/06/06 04:32
在内核中,可以通过current宏来获得当前执行进程的task_struct指针。现在来简要分析以下:
最原始的定义如下:
#define current get_current()
#define get_current() (current_thread_info()->task) 可以看出,current调用了 current_thread_info函数,此函数的内核路径为: arch/arm/include/asm/thread_info.h,内核版本为2.6.32.65
static inline struct thread_info *current_thread_info(void)
{
register unsigned long sp asm ("sp");
return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));
}
其中 thread_info结构体如下:
struct thread_info {unsigned longflags;/* low level flags */intpreempt_count;/* 0 => preemptable, <0 => bug */mm_segment_taddr_limit;/* address limit */struct task_struct*task;/* main task structure */struct exec_domain*exec_domain;/* execution domain */__u32cpu;/* cpu */__u32cpu_domain;/* cpu domain */struct cpu_context_savecpu_context;/* cpu context */__u32syscall;/* syscall number */__u8used_cp[16];/* thread used copro */unsigned longtp_value;struct crunch_statecrunchstate;union fp_statefpstate __attribute__((aligned(8)));union vfp_statevfpstate;#ifdef CONFIG_ARM_THUMBEEunsigned longthumbee_state;/* ThumbEE Handler Base register */#endifstruct restart_blockrestart_block;};
当内核线程执行到此处时,其SP堆栈指针指向调用进程所对应的内核线程的栈顶。通过 sp & ~(THREAD_SIZE-1)向上对齐,达到栈底部。如下图所示
将结果强制类型转换为thread_info类型,此类型中有一个成员为task_struct,它就是 当前正在运行进程的 task_struct指针。
备注:
在内核中,进程的task_struct是由slab分配器来分配的,slab分配器的优点是对象复用和缓存着色。
联合体:
#define THREAD_SIZE 8192 //内核线程栈 可以通过内核配置成4K 或者 8K ,此处是8K 。在X86体系结构上,32位的内核栈为8K,64位的为16K。
union thread_union {
struct thread_info thread_info; // sizeof(thread_info) =
unsigned long stack[THREAD_SIZE/sizeof(long)]; //stack 大小为 8K,union联合体的地址是严格按照小端排布的,因此,内核栈的低位地址是thread_info结构体。
};
整个8K的空间,顶部供进程堆栈使用,最下部为thread_info。从用户态切换到内核态时,进程的内核栈还是空的,所以sp寄存器指向栈顶,一旦有数据写入,sp的值就会递减,内核栈按需扩展,理论上最大可扩展到 【8192- sizeof(thread_info) 】大小,考虑到函数的现场保护,往往不会有这么大的栈空间。内核在代表进程执行时和所有的中断服务程序执行时,共享8K的内核栈。
来源:http://www.cnblogs.com/cherishui/p/4255690.html
- 内核 current宏解析
- 内核中的current宏
- 自己实现内核中的current宏
- 分析内核中的current宏,并自己实现
- current宏
- current宏
- 内核IS_ERR宏解析
- current包中countdownlatch解析
- current保重的atomic解析
- linux内核调试技巧--current的应用
- Linux内核调试技巧--current的应用
- linux内核 宏offsetof 解析
- linux内核container_of宏解析
- Linux内核Container_of宏解析
- linux kernel current宏
- current宏的作用
- linux kernel current宏
- current宏的作用
- System.AccessViolationException类型的未经处理的异常在System.Data.dll中发
- Linux中zip压缩和unzip解压缩命令详解
- meta标签详解(整理于网络)
- 文章标题
- OpenCL与CUDA,CPU与GPU
- 内核 current宏解析
- DesignPatterns_ChainOfResponsibility
- EL表达式
- 人见人爱A^B
- 更新ubuntu 10.10的源
- 新的个人计划
- java 实现单例模式
- Linux命令面试问题及答案
- HTML5 Geolocation实用性小调查