All of lore.kernel.org
 help / color / mirror / Atom feed
* process descriptor address in kernel stack
@ 2020-03-19  8:53 , Samuel
  2020-03-19  9:22 ` Valdis Klētnieks
  0 siblings, 1 reply; 2+ messages in thread
From: , Samuel @ 2020-03-19  8:53 UTC (permalink / raw)
  To: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 882 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 3423 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: process descriptor address in kernel stack
  2020-03-19  8:53 process descriptor address in kernel stack , Samuel
@ 2020-03-19  9:22 ` Valdis Klētnieks
  0 siblings, 0 replies; 2+ messages in thread
From: Valdis Klētnieks @ 2020-03-19  9:22 UTC (permalink / raw)
  To: , Samuel; +Cc: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 1250 bytes --]

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. :)

[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-03-19  9:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-19  8:53 process descriptor address in kernel stack , Samuel
2020-03-19  9:22 ` Valdis Klētnieks

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.