I am using the esp
value of kernel stack to calculate the process descriptor pointer value.
According to ULK book, I just need to mask 13 least significant bits of esp
to obtain the base address of the thread_info
structure.
My test is:
- write a kernel module because I need to get value of kernel stack
- In the kernel init function, get the value of kernel stack
- use following formula to get the process descriptor pointer of the process running on the CPU:
*((unsigned int*) esp & 0xffffe000)
- use the current macro, print out its value.
I think the value of step3 should be same as the value of step 4.
But my experiment results shows: sometimes they are same, and sometimes they are different. Could any explain why? Or开发者_如何学运维 am I missing anything?
This is because at the base of the kernel stack you will find a struct thread_info
instance (platform dependent) and not a struct task_struct
. The current()
macro provides a pointer to the current task_struct
.
Try the following:
struct thread_info *info = (struct thread_info*)(esp & 0xfffe000);
struct task_struct *my_current = info->task;
Now you can compare my_current
with current()
.
Finally, I solved this problem. Everything is correct expect for the size of kernel stack. My kernel use 4KB stack instead of 8KB stack. So I just need to mask low 12 bits of the ESP. Thanks for all the suggestions and answer!
精彩评论