Reputation: 103
I am currently reading 'Linux Kernel Development' by Robert Love and I do not understand what this bit of assembly is doing.
Basically, in each process kernel stack, there is a struct thread_info
which resides at the end of the stack. Now, on the x86 architecture, we can apparently grab this (assuming 8KB stack size) by using the following assembly
movl $-8192, %eax
andl %esp, %eax
So basically ANDing the stack pointer by 0xffffe000. I'm confused as to what is going on here? I don't see why masking the least significant 13 bits of %esp
takes us to the struct. I know I'll feel stupid once it is explained, but it is bugging me.
Thanks.
Upvotes: 10
Views: 989
Reputation: 4871
The stack grows downwards, so the end of the stack is the lowest address in the stack, and the structure's starting address. And stacks are stored at multiples of 8KB. Therefore, erasing the 13 least significant bits gets the lowest address of the stack and therefore the start of the structure. Does this make sense?
Upvotes: 5