I am writing a small linux loadable kernel module for version 2.6.32 running on x86-64 SMP.
My question is: is there a way to obtain the offset of the address of a per-cpu variable declared in the kernel. By offset I mean the offset of the address from the percpu base, which is found in the gs register. Specifically I am trying to find the offset of the current_task variable, which is a pointer to the task-struct of the current task I believe.
I am looking at arch/x86/include/asm/current.h, which has the get_current() function. This function reads the current_task per-cpu variable using the macro percpu_read_stable. As far as I can understand percpu_read_stable basically expands into an asm routine like this:
asm("movq %%gs:%P1, %0"
: "=r" (ret__)
: "m" (per_cpu__current_task))
This is in arch/x86/include/asm/percpu.h. I want to read the offset after gs from my module. If I try to simple do a printk with the per_cpu_current_task variable, the module is killed.
Thank you for your attention!
Okay, I figured out the offset for this particular symbol. This one is exported by the kernel. Hence there is an entry in /proc/kallsyms
000000000000cbc0 D per_cpu__current_task
So the offset is 0xcbc0 for this particular variable. Of course the offset would vary for other versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With