Is per-CPU var same as thread local storage? Can I use TLS with %gs?
Posted: Sat Jul 13, 2024 7:40 pm
I use %gs as prefix for per-cpu vars. Offset to per-cpu area is kernel only, so I need to swapgs when switching between kernel mode and user mode.
In the code, I use macro and inline assembly to access per-cpu vars of current CPU, just use movsq with %gs prefix.
Recently I heard thread local storage (TLS), and it uses %fs as prefix. What's good about TLS is compilers have good support for it. Just mark the variable with __thread and access it, compiler will generate code with %fs prefix automatically.
It seems per-cpu variables are just kernel version of TLS. Sometimes I need to access per-cpu var of another CPU, but most time I just access per-cpu var of current CPU. So is there a way to configure GCC to use %gs as TLS self pointer? Since x86 only has swapgs, no swapfs.
If I can make kernel TLS use %gs, then the thiscpu() macro can be completely removed, code will be alot simpler.
Thanks.
In the code, I use macro and inline assembly to access per-cpu vars of current CPU, just use movsq with %gs prefix.
Recently I heard thread local storage (TLS), and it uses %fs as prefix. What's good about TLS is compilers have good support for it. Just mark the variable with __thread and access it, compiler will generate code with %fs prefix automatically.
It seems per-cpu variables are just kernel version of TLS. Sometimes I need to access per-cpu var of another CPU, but most time I just access per-cpu var of current CPU. So is there a way to configure GCC to use %gs as TLS self pointer? Since x86 only has swapgs, no swapfs.
If I can make kernel TLS use %gs, then the thiscpu() macro can be completely removed, code will be alot simpler.
Thanks.