man-pages/man2/set_thread_area.2.html
2021-03-31 01:06:50 +01:00

362 lines
8.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SET_THREAD_AREA</TITLE>
</HEAD><BODY>
<H1>SET_THREAD_AREA</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
get_thread_area, set_thread_area - manipulate thread-local storage information
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/linux/unistd.h">linux/unistd.h</A>&gt;</B>
<B>#if defined __i386__ || defined __x86_64__</B>
<B># include &lt;<A HREF="file:///usr/include/asm/ldt.h">asm/ldt.h</A>&gt;</B>
<B>int get_thread_area(struct user_desc *</B><I>u_info</I><B>);</B>
<B>int set_thread_area(struct user_desc *</B><I>u_info</I><B>);</B>
<B>#elif defined __m68k__</B>
<B>int get_thread_area(void);</B>
<B>int set_thread_area(unsigned long </B><I>tp</I><B>);</B>
<B>#elif defined __mips__</B>
<B>int set_thread_area(unsigned long </B><I>addr</I><B>);</B>
<B>#endif</B>
</PRE>
<P>
<I>Note</I>:
There are no glibc wrappers for these system calls; see NOTES.
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
These calls provide architecture-specific support for a thread-local storage
implementation.
At the moment,
<B>set_thread_area</B>()
is available on m68k, MIPS, and x86 (both 32-bit and 64-bit variants);
<B>get_thread_area</B>()
is available on m68k and x86.
<P>
On m68k and MIPS,
<B>set_thread_area</B>()
allows storing an arbitrary pointer (provided in the
<B>tp</B>
argument on m68k and in the
<B>addr</B>
argument on MIPS)
in the kernel data structure associated with the calling thread;
this pointer can later be retrieved using
<B>get_thread_area</B>()
(see also NOTES
for information regarding obtaining the thread pointer on MIPS).
<P>
On x86, Linux dedicates three global descriptor table (GDT) entries for
thread-local storage.
For more information about the GDT, see the
Intel Software Developer's Manual or the AMD Architecture Programming Manual.
<P>
Both of these system calls take an argument that is a pointer
to a structure of the following type:
<P>
struct user_desc {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;entry_number;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;base_addr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;limit;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;seg_32bit:1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;contents:2;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;read_exec_only:1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;limit_in_pages:1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;seg_not_present:1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;useable:1;
#ifdef __x86_64__
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;&nbsp;lm:1;
#endif
};
<P>
<B>get_thread_area</B>()
reads the GDT entry indicated by
<I>u_info-&gt;entry_number</I>
and fills in the rest of the fields in
<I>u_info</I>.
<P>
<B>set_thread_area</B>()
sets a TLS entry in the GDT.
<P>
The TLS array entry set by
<B>set_thread_area</B>()
corresponds to the value of
<I>u_info-&gt;entry_number</I>
passed in by the user.
If this value is in bounds,
<B>set_thread_area</B>()
writes the TLS descriptor pointed to by
<I>u_info</I>
into the thread's TLS array.
<P>
When
<B>set_thread_area</B>()
is passed an
<I>entry_number</I>
of -1, it searches for a free TLS entry.
If
<B>set_thread_area</B>()
finds a free TLS entry, the value of
<I>u_info-&gt;entry_number</I>
is set upon return to show which entry was changed.
<P>
A
<I>user_desc</I>
is considered &quot;empty&quot; if
<I>read_exec_only</I>
and
<I>seg_not_present</I>
are set to 1 and all of the other fields are 0.
If an &quot;empty&quot; descriptor is passed to
<B>set_thread_area</B>(),
the corresponding TLS entry will be cleared.
See BUGS for additional details.
<P>
Since Linux 3.19,
<B>set_thread_area</B>()
cannot be used to write non-present segments, 16-bit segments, or code
segments, although clearing a segment is still acceptable.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On x86, these system calls
return 0 on success, and -1 on failure, with
<I>errno</I>
set appropriately.
<P>
On MIPS and m68k,
<B>set_thread_area</B>()
always returns 0.
On m68k,
<B>get_thread_area</B>()
returns the thread area pointer value
(previously set via
<B>set_thread_area</B>()).
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EFAULT</B>
<DD>
<I>u_info</I> is an invalid pointer.
<DT id="2"><B>EINVAL</B>
<DD>
<I>u_info-&gt;entry_number</I> is out of bounds.
<DT id="3"><B>ENOSYS</B>
<DD>
<B>get_thread_area</B>()
or
<B>set_thread_area</B>()
was invoked as a 64-bit system call.
<DT id="4"><B>ESRCH</B>
<DD>
(<B>set_thread_area</B>())
A free TLS entry could not be located.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>set_thread_area</B>()
first appeared in Linux 2.5.29.
<B>get_thread_area</B>()
first appeared in Linux 2.5.32.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>set_thread_area</B>()
and
<B>get_thread_area</B>()
are Linux-specific and should not be used in programs that are intended
to be portable.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Glibc does not provide wrappers for these system calls,
since they are generally intended for use only by threading libraries.
In the unlikely event that you want to call them directly, use
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
<P>
<B><A HREF="/cgi-bin/man/man2html?2+arch_prctl">arch_prctl</A></B>(2)
can interfere with
<B>set_thread_area</B>()
on x86.
See
<B><A HREF="/cgi-bin/man/man2html?2+arch_prctl">arch_prctl</A></B>(2)
for more details.
This is not normally a problem, as
<B><A HREF="/cgi-bin/man/man2html?2+arch_prctl">arch_prctl</A></B>(2)
is normally used only by 64-bit programs.
<P>
On MIPS, the current value of the thread area pointer can be obtained
using the instruction:
<P>
rdhwr dest, $29
<P>
This instruction traps and is handled by kernel.
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
On 64-bit kernels before Linux 3.19,
one of the padding bits in
<I>user_desc</I>,
if set, would prevent the descriptor from being considered empty (see
<B><A HREF="/cgi-bin/man/man2html?2+modify_ldt">modify_ldt</A></B>(2)).
As a result, the only reliable way to clear a TLS entry is to use
<B><A HREF="/cgi-bin/man/man2html?3+memset">memset</A></B>(3)
to zero the entire
<I>user_desc</I>
structure, including padding bits, and then to set the
<I>read_exec_only</I>
and
<I>seg_not_present</I>
bits.
On Linux 3.19, a
<I>user_desc</I>
consisting entirely of zeros except for
<I>entry_number</I>
will also be interpreted as a request to clear a TLS entry, but this
behaved differently on older kernels.
<P>
Prior to Linux 3.19, the DS and ES segment registers must not reference
TLS entries.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+arch_prctl">arch_prctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+modify_ldt">modify_ldt</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2)
(<B>PTRACE_GET_THREAD_AREA</B> and <B>PTRACE_SET_THREAD_AREA</B>)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="11"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAI">NOTES</A><DD>
<DT id="13"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="14"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>