开发者

process descriptor pointer doesn't match current macro in Linux Kernel

开发者 https://www.devze.com 2023-04-10 12:43 出处:网络
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 t

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:

  1. write a kernel module because I need to get value of kernel stack
  2. In the kernel init function, get the value of kernel stack
  3. use following formula to get the process descriptor pointer of the process running on the CPU: *((unsigned int*) esp & 0xffffe000)
  4. 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消