i386: vgetcpu()
RFC PATCH 1/1 i386: vgetcpu()
Use the limit field of a GDT entry to store the current CPU number for fast userspace access. This still leaves 12 bits free for other information.
/* vgetcpu.c: get CPU number we are running on.
* Prints -1 if error occurred.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * const argv[])
{
int cpu;
asm (
" lsl %1,%0 \n" \
" jnz 2f \n" \
" and $0xff,%0 \n" \
"2: \n" \
: "=&r" (cpu)
: "r" ((27 << 3) | 3), "0" (-1)
);
printf("cpu: %d\n", cpu);
return 0;
}Signed-off-by Chuck Ebbert <[email protected]>
arch/i386/kernel/cpu/common.c +3 -0
@@ -642,6 +642,9 @@ void __cpuinit cpu_init(void) ((((__u64 )stk16_off) <<32 ) &0xff00000000000000ULL ) | (CPU_16BIT_STACK_SIZE -1 ); +/* Set up GDT entry for per-cpu data */ + *(__u64 *)(&gdt[27 ]) |= cpu; + cpu_gdt_descr->size =GDT_SIZE -1 ; cpu_gdt_descr->address = (unsigned long )gdt;
arch/i386/kernel/head.S +7 -1
@@ -525,7 +525,13 @@ ENTRY(cpu_gdt_table) .quad 0x004092000000ffff /* 0xc8 APM DS data */ .quad 0x0000920000000000 /* 0xd0 - ESPFIX 16-bit SS */ - .quad 0x0000000000000000 /* 0xd8 - unused */ + + /* + * Use a GDT entry to store per-cpu data for user space (DPL 3.) + * 32-bit data segment, byte granularity, base 0, limit set at runtime. + */ + .quad 0x0040f20000000000 /* 0xd8 - for per-cpu user data */ + .quad 0x0000000000000000 /* 0xe0 - unused */ .quad 0x0000000000000000 /* 0xe8 - unused */ .quad 0x0000000000000000 /* 0xf0 - unused */
Signature · 2 lines
Chuck "You can't read a newspaper if you can't read." --George W. Bush
Quoted 3 lines · Chuck Ebbert
Use the limit field of a GDT entry to store the current CPU number for fast userspace access. This still leaves 12 bits free for other information.
Nice trick. Maybe I'll even add that to the x86-64 implementation if it's fast enough. Do you have numbers?
But it needs to be encapsulated in a wrapper I think. Just exposing it to user space is the wrong way to do this.
-Andi