do_arch_prctl - bug?
Hello,
I would like to know why the ARCH_SET_GS action of sys_arch_prctl, write the MSR MSR_KERNEL_GS_BASE and not the MSR MSR_GS_BASE when the variable "doit" equals 1? Is that a bug?
In other words, why the following code :
...
if (doit) {
load_gs_index(0);
ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr)
}
...
is not the following one :
...
if (doit) {
ret = checking_wrmsrl(MSR_GS_BASE, addr)
load_gs_index(0);
} ...
I copy for clarity the beginning of the function "do_arch_prctl" :
long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
{
int ret = 0;
int doit = task == current;
int cpu;
switch (code) {
case ARCH_SET_GS:
if (addr >= TASK_SIZE_OF(task))
return -EPERM;
cpu = get_cpu();
/* handle small bases via the GDT because that's faster to
switch. */
if (addr <= 0xffffffff) {
set_32bit_tls(task, GS_TLS, addr);
if (doit) {
load_TLS(&task->thread, cpu);
load_gs_index(GS_TLS_SEL);
}
task->thread.gsindex = GS_TLS_SEL;
task->thread.gs = 0;
} else {
task->thread.gsindex = 0;
task->thread.gs = addr;
if (doit) {
load_gs_index(0);
ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
}
}
put_cpu();
break;[...]
Regards,
Eric
Quoted 6 lines · On Tue, 18 Nov 2008 15:33:32 +0100 Eric Lacombe <[email protected]> wrote:
Hello,
I would like to know why the
ARCH_SET_GSaction ofsys_arch_prctl, write the MSRMSR_KERNEL_GS_BASEand not the MSRMSR_GS_BASEwhen the variable "doit" equals 1? Is that a bug?
I don't think it is.
The trick is that we use "swapgs" on entering/leaving the kernel, and that will "swap" gs with the MSR, so when we return to userspace, GS gets loaded from the MSR_KERNEL_GS_BASE ...
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
In fact, if what I thought from that function was ok, we will have in fact the order of the two following lines inversed :
...
load_gs_index(0);
ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
...
So that we would have :
...
ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr); load_gs_index(0);
...
Regards,
Eric
Le mardi 18 novembre 2008 18:20:03 Eric Lacombe, vous avez écrit :
Quoted 42 lines
Le mardi 18 novembre 2008 15:45:56, vous avez écrit :
Quoted 5 lines
On Tue, 18 Nov 2008 15:33:32 +0100
Quoted 2 lines · Eric Lacombe
Hello,
Quoted 8 lines
Quoted 3 lines
I would like to know why the
ARCH_SET_GSaction ofsys_arch_prctl, write the MSRMSR_KERNEL_GS_BASEand not the MSRMSR_GS_BASEwhen the variable "doit" equals 1? Is that a bug?I don't think it is.
The trick is that we use "swapgs" on entering/leaving the kernel, and that will "swap" gs with the MSR, so when we return to userspace, GS gets loaded from theMSR_KERNEL_GS_BASE...Yeah when we enter the kernel swapgs is used, so the
MSR_GS_BASEis switched with theMSR_KERNEL_GS_BASE.In fact, what I certainly misunderstand is why
load_gs_indexuse swapgs inside.
From that function, I trust that only when gs is loaded, its hidden part is loaded with theMSR_GS_BASE.
ENTRY(native_load_gs_index)
CFI_STARTPROC
pushf
CFI_ADJUST_CFA_OFFSET8DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))SWAPGSgs_change:
movl %edi,%gs
2: mfence /* workaround */
SWAPGS
popf
CFI_ADJUST_CFA_OFFSET-8
ret
CFI_ENDPROCENDPROC(native_load_gs_index)Regards,
Eric