Re: static inline struct task_struct * get_current(void)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 30 Dec 2002 13:49:52 +0530
"SACHIN PRASAD" <majorkernel@hotmail.com> wrote:

> I    was    just    looking    at    the   code    on    the    link
>http://www.kernelnewbies.org/faq/ and found a piece of code
>
>static inline struct task_struct * get_current(void)
>{
>        struct task_struct *current;
>        __asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));
>        return current;
>}
>
>
>
>i am new to kernel prograaming and want an explantions for :-
> 1.  "%0"  is  a  macro   that  expands  to  the  first  input/output
>specification.
>2.Basically, the task's task_struct  and a task's kernel stack occupy
>an  8KB  block that  is  8KB aligned,  with  the  task_struct at  the
>beginning and  the stack growing from  the end downwards.  So you can
>find  the task_struct by  clearing the  bottom 13  bits of  the stack
>pointer value.

Ok this requires an explanation. I'll talk about Intel x86 arch.  From
kernel 2.2  when a process is  created its task_struct  and its kernel
mode stack are placed together  in two contiguos page frames (so 8KB).
They  are organized  "with the  task_struct at  the beginning  and the
stack growing from the end  downwards" as you correctly said. Now when
we  want to  obtain task  descriptor (address  of task_struct)  we use
%esp. Why?  Because if you're in  kernel mode %esp is  the kernel mode
stack pointer. Now you can easily notice that zeroing the last %esp 13
bytes  gives you  exactly what  you need  since it  will point  to the
beginning of the first page frame (where task_struct is).
Now, with this in mind, it's easy to understand get_current(). 

This is inline asm syntax.

asm ( assembler template
            : output operands (optional)
            : input operands (optional)
            : list of clobbered registers (optional)
            );

When you  see "0" in input  operands in get_current it  means it's the
same of  first argument.  "=r" stands for  register and so  the output
operand is to be stored in a register. Now get_current() should appear
simple.   It loads  ~8191UL  in current  and  then ANDs  it with  %esp
storing the result in current.


Regards,

Angelo Dell'Aera 'buffer' 
<buffer@users.sourceforge.net>

PGP information in e-mail header

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)

iD8DBQE+EJc8pONIzxnBXKIRAtDnAKCTiL3VW+LCs3YcpkEOywWMsfbCugCfQDum
JkpPiWGXTCzvYGxxshx8tJ0=
=W5N2
-----END PGP SIGNATURE-----
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux