Tijl Coosemans tijl at
Wed Oct 10 06:51:16 PDT 2007

On Tuesday 09 October 2007 02:48:51 Mihai Donțu wrote:
> I have *one* more question: maybe I don't fully understand the hole
> BASE thing, but since the FreeBSD kernel does not preserve %gs and
> %fs, what is the purpose of amd64_set_XXbase()?

The %fs, %gs registers and fsbase and gsbase MSRs are separate
registers. When you write %gs:offset, you actually get (gsbase+offset),
so the actual value of %gs doesn't matter.

There are two ways to set gsbase. One is by using the privileged
instruction wrmsr to set gsbase directly (full 64bit base address),
which is what amd64_set_gsbase() exposes to userland. The other is by
loading a descriptor selector in %gs in which case gsbase will be set
to the base address (only 32bit base address) of a descriptor entry in
either the GDT or LDT.

To get back to what you are trying to do, because %gs isn't preserved,
I think you should avoid writing to it and instead strictly use
amd64_set_gsbase(). But from what you've written, I'm guessing you're
already doing this, so the next thing to try is to create threads with
PTHREAD_SCOPE_SYSTEM or use libthr instead of libpthread, because if
I'm not mistaken, PTHREAD_SCOPE_PROCESS in libpthread doesn't preserve
gsbase either.

More information about the freebsd-questions mailing list