Assignment of GDT entries
Quoted 4 lines · Jeremy Fitzhardinge
Quoted 3 lines · Albert Cahalan
We actually have an ABI problem right now because of this. Note that i386 and
x86_64use different GDT slots.
Quoted 8 lines
Quoted 4 lines
As far as I can tell, users need to hard-code the mapping from TLS slot to segment number. They use 0,1,2 to ask the kernel to set things up (via
set_thread_area), but can't just pop that into %fs or %gs.That's not true at all. The program I posted earlier in this thread uses
set_thread_area()to allocate a GDT slot, and it works on both native 32 bit and 32-under-64.
The i386 TLS API has three components:
(1) set_thread_area(entry_number == -1):
allocates and sets up the first available TLS entry and copies the chosen GDT index back to user-space
(2) set_thread_area(6 <= entry_number && entry_number <= 8):
allocates and sets up the indicated GDT entry
(3) get_thread_area(6 <= entry_number && entry_number <= 8):
retrieves the contents of the indicated GDT entry
Only (1) works in x86-64's ia32 emulation, the other two fail with EINVAL because x86-64 only accepts GDT indices 12 to 14 for TLS entries. glibc only uses (1).
If you move the i386 TLS GDT entries to other indices then you break (2) and (3) also on i386.
It's not difficult to design a better i386 TLS API that avoids requiring user-space to know the actual GDT indices (just use logical TLS indices and always copy the GDT index to user-space). but unfortunately that doesn't help us now because the TLS GDT indices must remain fixed as long as the current API is supported.
I _personally_ could certainly handle a post-2.6.18 kernel where the improved API (new syscalls) is in place, the GDT indices have been moved, and consequently components (2) and (3) of the old API are broken. However, this still implies breaking binary compatibility, which is not something to be done lightly.
(What's _really_ sad is that the implementation of the i386 TLS API internally operates on logical TLS indices, it's just the syscall interface that insists on requiring actual GDT indices from user-space.)
/Mikael
Quoted 10 lines · Mikael Pettersson
The i386 TLS API has three components:
(1)
set_thread_area(entry_number == -1):
allocates and sets up the first available TLS entry and copies the chosen GDT index back to user-space
(2)set_thread_area(6 <= entry_number && entry_number <= 8):
allocates and sets up the indicated GDT entry
(3)get_thread_area(6 <= entry_number && entry_number <= 8):
retrieves the contents of the indicated GDT entry
Quoted 7 lines
Only (1) works in x86-64's ia32 emulation, the other two fail with EINVAL because x86-64 only accepts GDT indices 12 to 14 for TLS entries. glibc only uses (1).
If you move the i386 TLS GDT entries to other indices then you break (2) and (3) also on i386.
(2) and (3) are always OK if you pass it the result of (1) - ie to update or readback a previously allocated descriptor. Neither is useful without having done (1) first. The fact that 32-on-32 and 32-on-64 differ here means that nothing can (an apparently nothing does) depend on hardcoded knowledge of the TLS descriptor indicies anyway.
Quoted 5 lines
It's not difficult to design a better i386 TLS API that avoids requiring user-space to know the actual GDT indices (just use logical TLS indices and always copy the GDT index to user-space). but unfortunately that doesn't help us
You still need the real indicies to construct a selector to put into a segment register - ie, actually do something useful. Changing the API to use abstract "TLS indicies" would also require a call to return the "TLS base", which hardly seems like an improvement.
Also, there's no inherent reason why the TLS indicies should be contigious; it happens to be true, but there's nothing useful userspace can do with that knowledge. Allowing them to be discontigious may be helpful, for example, in packing the most used TLS entries (ie #1) into a hot cache line, while putting the lesser-used ones elsewhere. The current API could deal with this without needing to change.
J
Quoted 15 lines · Jeremy Fitzhardinge
Quoted 14 lines · Mikael Pettersson
The i386 TLS API has three components:
(1)
set_thread_area(entry_number == -1):
allocates and sets up the first available TLS entry and copies the chosen GDT index back to user-space
(2)set_thread_area(6 <= entry_number && entry_number <= 8):
allocates and sets up the indicated GDT entry
(3)get_thread_area(6 <= entry_number && entry_number <= 8):
retrieves the contents of the indicated GDT entryOnly (1) works in x86-64's ia32 emulation, the other two fail with EINVAL because x86-64 only accepts GDT indices 12 to 14 for TLS entries. glibc only uses (1).
Quoted 7 lines
Quoted 3 lines
If you move the i386 TLS GDT entries to other indices then you break (2) and (3) also on i386.
(2) and (3) are always OK if you pass it the result of (1) - ie to update or readback a previously allocated descriptor. Neither is useful without having done (1) first.
In the real world a process' state is influenced by code I have little control over, usually glibc and other libraries, and fork(). Using (3) I can inspect parts of my process' state that I did not initialize myself.
Quoted 3 lines
The fact that 32-on-32 and 32-on-64
differ here means that nothing can (an apparently nothing does) depend on hardcoded knowledge of the TLS descriptor indicies anyway.
No, it means that x86-64's ia32 emulation was implemented by someone who either didn't realize the difference, or didn't care (because "only glibc matters").
Quoted 8 lines
Quoted 5 lines
It's not difficult to design a better i386 TLS API that avoids requiring user-space to know the actual GDT indices (just use logical TLS indices and always copy the GDT index to user-space). but unfortunately that doesn't help us
You still need the real indicies to construct a selector to put into a segment register - ie, actually do something useful.
Sure.
Quoted 3 lines
Changing the API
to use abstract "TLS indicies" would also require a call to return the "TLS base", which hardly seems like an improvement.
The TLS base can obviously be zero.
User-space asks to access TLS #n (for allocs #n can be -1). The kernel maps that to GDT index #m.
The kernel stores #m in the user-space buffer. User-space maps #m to a selector.
Quoted 6 lines
Also, there's no inherent reason why the TLS indicies should be contigious; it happens to be true, but there's nothing useful userspace can do with that knowledge. Allowing them to be discontigious may be helpful, for example, in packing the most used TLS entries (ie #1) into a hot cache line, while putting the lesser-used ones elsewhere. The current API could deal with this without needing to change.
I have said nothing that would prevent the use of sparse TLS GDT indices.
Look, I'm not saying the current API is perfect, far from it. But it does have valid usage modes which are broken in x86-64's ia32 emulation, and will break on i386 of you reallocate the TLS GDT indices. This is a fact.
This is why I'm asking that if you change things (thus breaking binary compatibility even more), that a corrected API be placed in new syscalls.
That is, instead of forcing user-space to do
uname
if (version >= 2.6.N)
call {set,get}_thread_area with new-style parameters
else
call {set,get}_thread_area with old-style parameters
it should do
call new_{set,get}_thread_area with new-style parameters if (ENOSYS)
call old_{set,get}_thread_area with old-style parameters
/Mikael
Quoted 11 lines · Mikael Pettersson
Quoted 3 lines
Changing the API
to use abstract "TLS indicies" would also require a call to return the "TLS base", which hardly seems like an improvement.The TLS base can obviously be zero.
User-space asks to access TLS #n (for allocs #n can be -1). The kernel maps that to GDT index #m.
The kernel stores #m in the user-space buffer. User-space maps #m to a selector.
I'm missing why this is a substantial improvement over the current interface (or functionally different at all). What does this proposal let you do that the current one doesn't?
Quoted 4 lines
Look, I'm not saying the current API is perfect, far from it. But it does have valid usage modes which are broken in x86-64's ia32 emulation, and will break on i386 of you reallocate the TLS GDT indices. This is a fact.
Hm, well its a "fact" in that they use different segment descriptors, but you'd be hard pressed to say that was a breakage. set_thread_area was added in 2.5.29 (Jul 2002), and x86-64 added support in 2.5.43 (Oct 2002), so the current behaviour is pretty much as it has always been. If you have a program that expects something different, you either wrote it in Jul-Oct 2002, or you made an unsustainable assumption about how set_thread_area() works.
Quoted 4 lines
Look, I'm not saying the current API is perfect, far from it. But it does have valid usage modes which are broken in x86-64's ia32 emulation, and will break on i386 of you reallocate the TLS GDT indices. This is a fact.
You seem to have a specific use-case in mind; do you have a program which would like to use a new interface? Would you mind spelling it out, and describe why the current interface doesn't work for you?
J