Assignment of GDT entries
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 3 lines · Zachary Amsden
I believe 9,10,11 are reserved for future users like yourself or expanded TLS segments. I think a bank of 3 TLS segments in the GDT is working fine now (does NPTL even use more than one?).
Nope. And there's a comment that wine uses one more. I think the third is completely unused.
I use the third. The sucky thing is that I need to determine if the kernel is 64-bit to know what I must load into the segment register. Fortunately this code is not yet out in the wild, so you can still fix the ABI situation for me at least.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Otherwise line 1 would be ideal for putting 3 TLS, kernel+user code+data and PDA into, thereby making 99.999% of GDT descriptor uses come from one cache line.
That change is visible to userspace, unfortunately.
Don't think it matters much. 32-bit processes on x86-64 seem perfectly happy with the TLS being in a different place.
Heh. I wish. Well, OK, but only because I detect the kernel!
Quoted 7 lines
I think the ABI is defined in terms of "use the selector for the entry that set_thread_area/clone returns", and so is not a constant. But I agree it would be better not to.
Hm, moving user cs/ds would be pretty visible too... Hm, and it would have a greater chance of breaking stuff if they changed, compared to moving the TLS...
I think that would be a lower chance, not a greater chance. Reasons why an app might care:
a. identify a 64-bit kernel
b. far jumps between 32-bit and 64-bit code
c. reload of ds/es after a string operation on thread-private data
Perhaps i386 should change to match x86_64.
Quoted 8 lines · Albert Cahalan
I think that would be a lower chance, not a greater chance. Reasons why an app might care:
a. identify a 64-bit kernel
b. far jumps between 32-bit and 64-bit code
c. reload of ds/es after a string operation on thread-private dataPerhaps i386 should change to match
x86_64.
I agree that the difference is annoying.
However I just wrote a user space implementation of fork that is capable of copying a process from an i386 only kernel to a x86_64 kernel, and executing there without having to detect the kernel type.
It didn't takes hacks to accomplish that.
The basic syscall is:
int set_thread_area (struct user_desc *u_info);
struct user_desc {
unsigned int entry_number;
unsigned long base_addr;
unsigned int limit;
unsigned int seg_32bit:1;
unsigned int contents:2;
unsigned int read_exec_only:1;
unsigned int limit_in_pages:1;
unsigned int seg_not_present:1;
unsigned int useable:1;
};If entry_number is -1 the kernel finds a free gdt entry and sets up the segment and returns with entry_number set to the segment number.
Eric
Quoted 22 lines · Eric W. Biederman
I agree that the difference is annoying.
However I just wrote a user space implementation of fork that is capable of copying a process from an i386 only kernel to a
x86_64kernel, and executing there without having to detect the kernel type.It didn't takes hacks to accomplish that.
The basic syscall is:
intset_thread_area(structuser_desc*u_info);struct user_desc { unsigned int entry_number; unsigned long base_addr; unsigned int limit; unsigned int seg_32bit:1; unsigned int contents:2; unsigned int read_exec_only:1; unsigned int limit_in_pages:1; unsigned int seg_not_present:1; unsigned int useable:1; };
Quoted 3 lines
If
entry_numberis -1 the kernel finds a free gdt entry and sets up the segment and returns withentry_numberset to the segment number.
Eeeeeew.
So if I grabbed the first two slots before glibc got to mess with them, glibc wouldn't break horribly? If I grabbed one slot and glibc grabbed another, Wine would be OK with the third instead of the second?
So basically it's not allowed to just grab the 3rd slot?
What if I want to find out what is already in use? Am I supposed to iterate over all 8191 possible GDT entries? How do I even tell how many slots are available without using them all up?
Eeeeeeew. Well this was documented exactly nowhere. The man page is even vague about entry_number, meaning I had to dig in the kernel source (AMD manual by my side) to find if that was a GDT slot or TLS slot, as array index or byte offset, with or without the low bits all set up for loading into the segment register, loaded for me or not, etc.
Quoted 6 lines · Albert Cahalan
Eeeeeew.
So if I grabbed the first two slots before glibc got to mess with them, glibc wouldn't break horribly? If I grabbed one slot and glibc grabbed another, Wine would be OK with the third instead of the second?
Glibc should allocate a slot just the same way, just like wine does as well. Glibc just usually gets its slot allocated first.
Quoted 2 lines
So basically it's not allowed to just grab the 3rd slot?
You can, but you should be prepared for it to fail as well.
Quoted 5 lines
What if I want to find out what is already in use? Am I supposed to iterate over all 8191 possible GDT entries? How do I even tell how many slots are available without using them all up?
There are only 32 possible GDT entries in 32-bit i386 Linux, and only three of them are usable for userspace. You can't find out which slots are in use, but you can cause one to be allocated and returned to you. This seems like a perfectly reasonable API to me, why do you think it is so ugly?
Zach
Quoted 5 lines · Zachary Amsden
Quoted 1 line · Albert Cahalan
So basically it's not allowed to just grab the 3rd slot?
You can, but you should be prepared for it to fail as well.
Without knowing details of the kernel's GDT, how?
Quoted 10 lines
Quoted 4 lines
What if I want to find out what is already in use? Am I supposed to iterate over all 8191 possible GDT entries? How do I even tell how many slots are available without using them all up?
There are only 32 possible GDT entries in 32-bit i386 Linux, and only three of them are usable for userspace. You can't find out which slots are in use, but you can cause one to be allocated and returned to you. This seems like a perfectly reasonable API to me, why do you think it is so ugly?
Eh, "returned to you" doesn't work for me. I need to figure out what other code (not written by me) uses.
I may need to "borrow" a slot if all three slots are in use. Without using evil knowledge of the GDT, how am I to do that? I don't know what slots might have been allocated by other libraries.
Albert Cahalan wrote
Quoted 9 lines
Quoted 6 lines
There are only 32 possible GDT entries in 32-bit i386 Linux, and only three of them are usable for userspace. You can't find out which slots are in use, but you can cause one to be allocated and returned to you. This seems like a perfectly reasonable API to me, why do you think it is so ugly?
Eh, "returned to you" doesn't work for me. I need to figure out what other code (not written by me) uses.
I don't understand. Why do you need to figure that out? You need a selector, you ask for one, and you get assigned one. It is that simple. You can't figure out what other code uses, and the kernel has no way to tell you, because that is an application level allocation problem, not a kernel responsibility. The kernel has no visibility into userspace intentions regarding segment usage.
Quoted 4 lines
I may need to "borrow" a slot if all three slots are in use. Without using evil knowledge of the GDT, how am I to do that? I don't know what slots might have been allocated by other libraries.
What kind of libraries are you using? Unless this is really, really, special purpose, they are going to allocate at most one, and that is only if you use TLS libraries.
If all three slots are in use (i.e. your allocation fails), you'll have to allocate an LDT selector, just like wine:
void wine_ldt_init_fs( unsigned short sel, const LDT_ENTRY *entry ) {
if ((sel & ~3) == (global_fs_sel & ~3))
{
#ifdef __linux__
struct modify_ldt_s ldt_info;
int ret;
ldt_info.entry_number = sel >> 3;
fill_modify_ldt_struct( &ldt_info, entry );
if ((ret = set_thread_area( &ldt_info ) < 0)) perror(
"set_thread_area" );
#elif defined(__APPLE__)
int ret = thread_set_user_ldt( wine_ldt_get_base(entry),
wine_ldt_get_limit(entry), 0 );
if (ret == -1) perror( "thread_set_user_ldt" );
else assert( ret == global_fs_sel );#endif /* __APPLE__ */
}
else /* LDT selector */
{
internal_set_entry( sel, entry ); <---- just like this
}
wine_set_fs( sel );
}
Zach
Quoted 2 lines · Albert Cahalan
So if I grabbed the first two slots before glibc got to mess with them, glibc wouldn't break horribly?
glibc would be happy with anything it got; if you grabbed all 3 TLS slots it would probably be upset.
Quoted 2 lines
If I grabbed one slot and glibc grabbed another, Wine would be OK with the third instead of the second?
Presumably.
Quoted 1 line
So basically it's not allowed to just grab the 3rd slot?
Eh? You mean there's no "allocate and return TLS slot #N" operation? No, but all the TLS slots should be interchangeable. Once you've got your entry numbers and worked out your selector values, you can just use them.
Quoted 4 lines
What if I want to find out what is already in use? Am I supposed to iterate over all 8191 possible GDT entries? How do I even tell how many slots are available without using them all up?
The kernel reserves 3 slots in the GDT for usermode use, which are per-thread. If you want more segment descriptors, you can always allocate an LDT.
Quoted 2 lines
Eeeeeeew. Well this was documented exactly nowhere. The man page is even vague about
entry_number,
man set_thread_area has this as paragraph 2:
When set_thread_area() is passed an entry_number of -1, it uses a free TLS entry. If set_thread_area() finds a free TLS entry, the value of u_info->entry_number is set upon return to show which entry was changed.
which seems pretty clear to me. A quick run with strace on any binary shows this in action:
set_thread_area({entry_number:-1 -> 6, base_addr:0xb7fb06c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
J