Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / set_thread_area.2
blob43f3009f764fa9fae04299ef5494f5ad4459d3f5
1 .\" Copyright (C) 2003 Free Software Foundation, Inc.
2 .\" Copyright (C) 2015 Andrew Lutomirski
3 .\" Author: Kent Yoder
4 .\"
5 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
6 .\" This file is distributed according to the GNU General Public License.
7 .\" %%%LICENSE_END
8 .\"
9 .TH SET_THREAD_AREA 2 2021-03-22 "Linux" "Linux Programmer's Manual"
10 .SH NAME
11 get_thread_area, set_thread_area \- manipulate thread-local storage information
12 .SH SYNOPSIS
13 .nf
14 .BR "#include <sys/syscall.h>" "     /* Definition of " SYS_* " constants */"
15 .B #include <unistd.h>
16 .PP
17 .B #if defined __i386__ || defined __x86_64__
18 .BR "# include <asm/ldt.h>" "        /* Definition of " "struct user_desc" " */"
19 .PP
20 .BI "int syscall(SYS_get_thread_area, struct user_desc *" u_info );
21 .BI "int syscall(SYS_set_thread_area, struct user_desc *" u_info );
22 .PP
23 .B #elif defined __m68k__
24 .PP
25 .B "int syscall(SYS_get_thread_area);"
26 .BI "int syscall(SYS_set_thread_area, unsigned long " tp );
27 .PP
28 .B #elif defined __mips__
29 .PP
30 .BI "int syscall(SYS_set_thread_area, unsigned long " addr );
31 .PP
32 .B #endif
33 .fi
34 .PP
35 .IR Note :
36 glibc provides no wrappers for these system calls,
37 necessitating the use of
38 .BR syscall (2).
39 .SH DESCRIPTION
40 These calls provide architecture-specific support for a thread-local storage
41 implementation.
42 At the moment,
43 .BR set_thread_area ()
44 is available on m68k, MIPS, and x86 (both 32-bit and 64-bit variants);
45 .BR get_thread_area ()
46 is available on m68k and x86.
47 .PP
48 On m68k and MIPS,
49 .BR set_thread_area ()
50 allows storing an arbitrary pointer (provided in the
51 .B tp
52 argument on m68k and in the
53 .B addr
54 argument on MIPS)
55 in the kernel data structure associated with the calling thread;
56 this pointer can later be retrieved using
57 .BR get_thread_area ()
58 (see also NOTES
59 for information regarding obtaining the thread pointer on MIPS).
60 .PP
61 On x86, Linux dedicates three global descriptor table (GDT) entries for
62 thread-local storage.
63 For more information about the GDT, see the
64 Intel Software Developer's Manual or the AMD Architecture Programming Manual.
65 .PP
66 Both of these system calls take an argument that is a pointer
67 to a structure of the following type:
68 .PP
69 .in +4n
70 .EX
71 struct user_desc {
72     unsigned int  entry_number;
73     unsigned int  base_addr;
74     unsigned int  limit;
75     unsigned int  seg_32bit:1;
76     unsigned int  contents:2;
77     unsigned int  read_exec_only:1;
78     unsigned int  limit_in_pages:1;
79     unsigned int  seg_not_present:1;
80     unsigned int  useable:1;
81 #ifdef __x86_64__
82     unsigned int  lm:1;
83 #endif
85 .EE
86 .in
87 .PP
88 .BR get_thread_area ()
89 reads the GDT entry indicated by
90 .I u_info\->entry_number
91 and fills in the rest of the fields in
92 .IR u_info .
93 .PP
94 .BR set_thread_area ()
95 sets a TLS entry in the GDT.
96 .PP
97 The TLS array entry set by
98 .BR set_thread_area ()
99 corresponds to the value of
100 .I u_info\->entry_number
101 passed in by the user.
102 If this value is in bounds,
103 .BR set_thread_area ()
104 writes the TLS descriptor pointed to by
105 .I u_info
106 into the thread's TLS array.
108 When
109 .BR set_thread_area ()
110 is passed an
111 .I entry_number
112 of \-1, it searches for a free TLS entry.
114 .BR set_thread_area ()
115 finds a free TLS entry, the value of
116 .I u_info\->entry_number
117 is set upon return to show which entry was changed.
120 .I user_desc
121 is considered "empty" if
122 .I read_exec_only
124 .I seg_not_present
125 are set to 1 and all of the other fields are 0.
126 If an "empty" descriptor is passed to
127 .BR set_thread_area (),
128 the corresponding TLS entry will be cleared.
129 See BUGS for additional details.
131 Since Linux 3.19,
132 .BR set_thread_area ()
133 cannot be used to write non-present segments, 16-bit segments, or code
134 segments, although clearing a segment is still acceptable.
135 .SH RETURN VALUE
136 On x86, these system calls
137 return 0 on success, and \-1 on failure, with
138 .I errno
139 set to indicate the error.
141 On MIPS and m68k,
142 .BR set_thread_area ()
143 always returns 0.
144 On m68k,
145 .BR get_thread_area ()
146 returns the thread area pointer value
147 (previously set via
148 .BR set_thread_area ()).
149 .SH ERRORS
151 .B EFAULT
152 \fIu_info\fP is an invalid pointer.
154 .B EINVAL
155 \fIu_info\->entry_number\fP is out of bounds.
157 .B ENOSYS
158 .BR get_thread_area ()
160 .BR set_thread_area ()
161 was invoked as a 64-bit system call.
163 .B ESRCH
164 .RB ( set_thread_area ())
165 A free TLS entry could not be located.
166 .SH VERSIONS
167 .BR set_thread_area ()
168 first appeared in Linux 2.5.29.
169 .BR get_thread_area ()
170 first appeared in Linux 2.5.32.
171 .SH CONFORMING TO
172 .BR set_thread_area ()
174 .BR get_thread_area ()
175 are Linux-specific and should not be used in programs that are intended
176 to be portable.
177 .SH NOTES
178 These system calls are generally intended for use only by threading libraries.
180 .BR arch_prctl (2)
181 can interfere with
182 .BR set_thread_area ()
183 on x86.
185 .BR arch_prctl (2)
186 for more details.
187 This is not normally a problem, as
188 .BR arch_prctl (2)
189 is normally used only by 64-bit programs.
191 On MIPS, the current value of the thread area pointer can be obtained
192 using the instruction:
194 .in +4n
196 rdhwr dest, $29
200 This instruction traps and is handled by kernel.
201 .SH BUGS
202 On 64-bit kernels before Linux 3.19,
203 .\" commit e30ab185c490e9a9381385529e0fd32f0a399495
204 one of the padding bits in
205 .IR user_desc ,
206 if set, would prevent the descriptor from being considered empty (see
207 .BR modify_ldt (2)).
208 As a result, the only reliable way to clear a TLS entry is to use
209 .BR memset (3)
210 to zero the entire
211 .I user_desc
212 structure, including padding bits, and then to set the
213 .I read_exec_only
215 .I seg_not_present
216 bits.
217 On Linux 3.19, a
218 .I user_desc
219 consisting entirely of zeros except for
220 .I entry_number
221 will also be interpreted as a request to clear a TLS entry, but this
222 behaved differently on older kernels.
224 Prior to Linux 3.19, the DS and ES segment registers must not reference
225 TLS entries.
226 .SH SEE ALSO
227 .BR arch_prctl (2),
228 .BR modify_ldt (2),
229 .BR ptrace (2)
230 .RB ( PTRACE_GET_THREAD_AREA " and " PTRACE_SET_THREAD_AREA )