History log of /seL4-camkes-master/projects/musllibc/src/thread/i386/__set_thread_area.s
Revision Date Author Comments
# 707d7c30 15-May-2015 Rich Felker <dalias@aerifal.cx>

in i386 __set_thread_area, don't assume %gs register is initially zero

commit f630df09b1fd954eda16e2f779da0b5ecc9d80d3 added logic to handle
the case where __set_thread_area is called more than once by reusing
the GDT slot already in the %gs register, and only setting up a new
GDT slot when %gs is zero. this created a hidden assumption that %gs
is zero when a new process image starts, which is true in practice on
Linux, but does not seem to be documented ABI, and fails to hold under
qemu app-level emulation.

while it would in theory be possible to zero %gs in the entry point
code, this code is shared between static and dynamic binaries, and
dynamic binaries must not clobber the value of %gs already setup by
the dynamic linker.

the alternative solution implemented in this commit simply uses global
data to store the GDT index that's selected. __set_thread_area should
only be called in the initial thread anyway (subsequent threads get
their thread pointer setup by __clone), but even if it were called by
another thread, it would simply read and write back the same GDT index
that was already assigned to the initial thread, and thus (in the x86
memory model) there is no data race.


# f630df09 13-Apr-2015 Rich Felker <dalias@aerifal.cx>

allow i386 __set_thread_area to be called more than once

previously a new GDT slot was requested, even if one had already been
obtained by a previous call. instead extract the old slot number from
GS and reuse it if it was already set. the formula (GS-3)/8 for the
slot number automatically yields -1 (request for new slot) if GS is
zero (unset).


# 64e32287 10-Jun-2014 Rich Felker <dalias@aerifal.cx>

add thread-pointer support for pre-2.6 kernels on i386

such kernels cannot support threads, but the thread pointer is also
important for other purposes, most notably stack protector. without a
valid thread pointer, all code compiled with stack protector will
crash. the same applies to any use of thread-local storage by
applications or libraries.

the concept of this patch is to fall back to using the modify_ldt
syscall, which has been around since linux 1.0, to setup the gs
segment register. since the kernel does not have a way to
automatically assign ldt entries, use of slot zero is hard-coded. if
this fallback path is used, __set_thread_area returns a positive value
(rather than the usual zero for success, or negative for error)
indicating to the caller that the thread pointer was successfully set,
but only for the main thread, and that thread creation will not work
properly. the code in __init_tp has been changed accordingly to record
this result for later use by pthread_create.


# 3f72cdac 18-Sep-2011 Rich Felker <dalias@aerifal.cx>

overhaul clone syscall wrapping

several things are changed. first, i have removed the old __uniclone
function signature and replaced it with the "standard" linux
__clone/clone signature. this was necessary to expose clone to
applications anyway, and it makes it easier to port __clone to new
archs, since it's now testable independently of pthread_create.

secondly, i have removed all references to the ugly ldt descriptor
structure (i386 only) from the c code and pthread structure. in places
where it is needed, it is now created on the stack just when it's
needed, in assembly code. thus, the i386 __clone function takes the
desired thread pointer as its argument, rather than an ldt descriptor
pointer, just like on all other sane archs. this should not affect
applications since there is really no way an application can use clone
with threads/tls in a way that doesn't horribly conflict with and
clobber the underlying implementation's use. applications are expected
to use clone only for creating actual processes, possibly with new
namespace features and whatnot.


# c7d19f99 14-Jun-2011 Rich Felker <dalias@aerifal.cx>

restore use of .type in asm, but use modern @function (vs %function)

this seems to be necessary to make the linker accept the functions in
a shared library (perhaps to generate PLT entries?)

strictly speaking libc-internal asm should not need it. i might clean
that up later.


# 1e4f1cf1 13-Jun-2011 Rich Felker <dalias@aerifal.cx>

remove all .size and .type directives for functions from the asm

these are useless and have caused problems for users trying to build
with non-gnu tools like tcc's assembler.


# 7b2dd223 15-Feb-2011 Rich Felker <dalias@aerifal.cx>

finish unifying thread register handling in preparation for porting