process descriptor address in kernel stack
In this book (understanding Linux kernel), the kernel can easily obtain the address of the thread_info structure of the process currently running on a CPU from the value of the esp register. In fact, if the thread_union structure is 8 KB (213 bytes) long, the kernel masks out the 13 least significant bits of esp to obtain the base address of the thread_info structure; on the other hand, if the thread_union struc- ture is 4 KB long, the kernel masks out the 12 least significant bits of esp. This is done by the current_thread_info() function, which produces assembly language instructions like the following: movl $0xffffe000,%ecx or 0xfffff000 for 4KB stacks andl %esp,%ecx movl %ecx,p Why is *"stack pointer(esp) & 0xffffe000"* equal to the process descriptor base address? That means the base address of process descriptor is always *0xXYZ...000*, right? It is weird.
On Thu, 19 Mar 2020 16:53:32 +0800, ", Samuel" said:
movl $0xffffe000,%ecx or 0xfffff000 for 4KB stacks andl %esp,%ecx movl %ecx,p
Why is *"stack pointer(esp) & 0xffffe000"* equal to the process descriptor base address?
That means the base address of process descriptor is always *0xXYZ...000*, right? It is weird.
It's not at all weird if the kernel, when allocating the stack space to begin with, asked for 1 (or 2 contiguous) 4K chunks of memory, at a page-aligned address.... For example, see kernel/fork.c: 238 /* 239 * Allocated stacks are cached and later reused by new threads, 240 * so memcg accounting is performed manually on assigning/releasing 241 * stacks to tasks. Drop __GFP_ACCOUNT. 242 */ 243 stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 244 VMALLOC_START, VMALLOC_END, 245 THREADINFO_GFP & ~__GFP_ACCOUNT, 246 PAGE_KERNEL, 247 0, node, __builtin_return_address(0)); I'll leave figuring out what THREAD_ALIGN is set to, as an exercise for the student. :)
participants (2)
-
, Samuel -
Valdis Klētnieks