Linux kernel gets current process path analysis

Source: Internet
Author: User

A simple question to get the current process under Linux. We all know that getting the current process in the kernel can take advantage of the present macro

#define get_current ()    (Current_thread_info ()->task)#define current        get_current ()

It is discovered through get_current that it is the current thread of the process that is obtained using the current threads structure thread_info the process pointer to which it belongs to the task

StaticInlinestructThread_info *current_thread_info (void){    structThread_info *ti; __ASM__ ("move.l%%sp,%0 \n\t"        "and.l%1,%0"        : "=&d"(TI):"di"(~ (thread_size-1))        ); returnti;}

The code is simple, gets the value of the SP register, and then thread_size-1 and gets a pointer to the Thread_info. How does it work? We all know that each process is useful for a kernel stack, and when the process enters the kernel in some way, such as a system call, the stack space needs to be switched to the kernel stack. By default, the size of the kernel stack is 2 pages, which is 8KB. The THREAD_INFO structure is stored at the top of the stack, based on the stack being grown from a high address to a low address, so thread_info is at the beginning of the 8KB address range. For example, the kernel stack space is 0xc9563000~0xc9565000 (just doing the example, which does not mean anything), then the schema diagram looks like this:

, the kernel stack, esp point at any time must be located between the 0xc9563000~0xc9565000, so at this point we can be seen as the kernel stack is 8KB aligned, so recall the virtual page how to get the virtual page frame base site? It is to put the internal partial shift all 0, then and address to want to with. So~ Here is also the case, Thread_size-1 is the stack of offset, take the opposite side of the offset portion, the rest of the 1, want to and after all the results always point to the beginning of the kernel stack interval, the example is 0xc9563000.that ' s All,thank you !!

Thank the Lord!

Reference:

Linux kernel 3.10.1 Source code

Linux kernel gets current process path analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.