Re: how does kernel get the "current" task struct?

From: H. Peter Anvin (hpa@transmeta.com)
Date: Wed Feb 02 2000 - 21:07:26 EST


Followup to: <3898C8C2.60C07961@mvista.com>
By author: Jun Sun <jsun@mvista.com>
In newsgroup: linux.dev.kernel
>
>
> I am looking at the source code of kernel v2.2.12, and get
> totally confused by the "current" macro. Kernel uses "current"
> to get a pointer to the task_struct of the current running process.
>
> "current" is defined as a macro in include/asm-i386/current.h :
>
> -------------------
> struct task_struct;
>
> static inline struct task_struct * get_current(void)
> {
> struct task_struct *current;
> __asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));
> return current;
> }
>
> #define current get_current()
> --------------------
>
> Basically, the pointer to the current task_struct is the current stack
> pointer(%esp) substracted by 8K.
>

Wrong. It's %esp with a whole bunch of bits *MASKED*. This works
because the per-task area -- which includes the kernel stack -- is 8K
in size and aligned on an 8K boundary.

> In addition, how can we have different "current" values for
> different CPUs in the SMP version?

Because they have different stack pointers; each one of them pointing
to the respective per-task area.

   -hpa

-- 
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Mon Feb 07 2000 - 21:00:09 EST