I tried to figure out the details of MACRO current in Linux kernel.
The final assembly code of current is:
movq %%gs:0xb000,%0
The code above can work! But when I print the %%gs, its value is 0, so the %%gs points to the first item of GDT NULL!!?? How it works?
mov %%gs, %0
Instead, the base of gs is in MSR_GS_BASE, and the current can be replaced like:
/*0xb000 is the offset of per_cpu__current_task*/
cur_task = (unsigned long*)(x86_rdmsr64(MSR_GS_BASE) + 0xb000);
println("cur_task:%p",*cur_task);
My questions is:
%gs points to the first item of GDT NULL!!?? How it works as read from MSR_GS_BASE, is it a CPU feature? I need some references about this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…