Task data
Posted: Sat Jul 09, 2011 2:10 am
Where could one keep a pointer the the task structure, so that it can be accessed (in ring 0) to identify the current task? Is there some "hidden" register or something that I could use?
Often one has an entry in the GDT that points to variables that pertain to the CPU, including the current task pointer. This entry is then loaded into FS or GS at the start of every exception handler. Another method would be to have a global TLB mapping that points to these variables.Where could one keep a pointer the the task structure, so that it can be accessed (in ring 0) to identify the current task? Is there some "hidden" register or something that I could use?
Code: Select all
class FSVar;
template<class T,class U> class StructVar {
public:
DWORD ofs;
StructVar(U T::*x) {ofs=(unsigned long)&(((T*)0)->*x);}
};
template<class T,class U,class V> class CustomVar;
template<class V,class T,class U>
inline CustomVar<T,U,V> GetCustomVar(U T::*x)
{
return CustomVar<T,U,V>(x);
}
template<class T,class U> class CustomVar<T,U,FSVar> : StructVar<T,U> {
public:
CustomVar<T,U,FSVar>(U T::*x) : StructVar<T,U>(x) {}
operator const U() const {
if(sizeof U==4) return (U)__readfsdword(ofs);
if(sizeof U==2) return (U)__readfsword(ofs);
if(sizeof U==1) return (U)__readfsbyte(ofs);
U res;
memcpy(&res,&CPUVar(Self),sizeof U);
return res;
}
U operator=(U x)
{
if(sizeof U==4) __writefsdword(ofs,(DWORD)x);
else if(sizeof U==2) __writefsword(ofs,(WORD)x);
else if(sizeof U==1) __writefsbyte(ofs,(BYTE)x);
else memcpy(&CPUVar(Self),&x,sizeof U);
return U;
}
U operator->() const { return*this; }
};
#define CPUVar(x) GetCustomVar<FSVar>(&ProcessorVars::x)
I have not think about MP yet (building UP kernel is already so complex for me)Often one has an entry in the GDT that points to variables that pertain to the CPU
TSS.SP0. I have thread structure at the top of kernel stack for this thread. It's easy to use.TylerH wrote:Where could one keep a pointer the the task structure, so that it can be accessed (in ring 0) to identify the current task? Is there some "hidden" register or something that I could use?