1set_thread_area(2) System Calls Manual set_thread_area(2)
2
3
4
6 get_thread_area, set_thread_area - manipulate thread-local storage in‐
7 formation
8
10 Standard C library (libc, -lc)
11
13 #include <sys/syscall.h> /* Definition of SYS_* constants */
14 #include <unistd.h>
15
16 #if defined __i386__ || defined __x86_64__
17 # include <asm/ldt.h> /* Definition of struct user_desc */
18
19 int syscall(SYS_get_thread_area, struct user_desc *u_info);
20 int syscall(SYS_set_thread_area, struct user_desc *u_info);
21
22 #elif defined __m68k__
23
24 int syscall(SYS_get_thread_area);
25 int syscall(SYS_set_thread_area, unsigned long tp);
26
27 #elif defined __mips__
28
29 int syscall(SYS_set_thread_area, unsigned long addr);
30
31 #endif
32
33 Note: glibc provides no wrappers for these system calls, necessitating
34 the use of syscall(2).
35
37 These calls provide architecture-specific support for a thread-local
38 storage implementation. At the moment, set_thread_area() is available
39 on m68k, MIPS, and x86 (both 32-bit and 64-bit variants);
40 get_thread_area() is available on m68k and x86.
41
42 On m68k and MIPS, set_thread_area() allows storing an arbitrary pointer
43 (provided in the tp argument on m68k and in the addr argument on MIPS)
44 in the kernel data structure associated with the calling thread; this
45 pointer can later be retrieved using get_thread_area() (see also NOTES
46 for information regarding obtaining the thread pointer on MIPS).
47
48 On x86, Linux dedicates three global descriptor table (GDT) entries for
49 thread-local storage. For more information about the GDT, see the In‐
50 tel Software Developer's Manual or the AMD Architecture Programming
51 Manual.
52
53 Both of these system calls take an argument that is a pointer to a
54 structure of the following type:
55
56 struct user_desc {
57 unsigned int entry_number;
58 unsigned int base_addr;
59 unsigned int limit;
60 unsigned int seg_32bit:1;
61 unsigned int contents:2;
62 unsigned int read_exec_only:1;
63 unsigned int limit_in_pages:1;
64 unsigned int seg_not_present:1;
65 unsigned int useable:1;
66 #ifdef __x86_64__
67 unsigned int lm:1;
68 #endif
69 };
70
71 get_thread_area() reads the GDT entry indicated by u_info->entry_number
72 and fills in the rest of the fields in u_info.
73
74 set_thread_area() sets a TLS entry in the GDT.
75
76 The TLS array entry set by set_thread_area() corresponds to the value
77 of u_info->entry_number passed in by the user. If this value is in
78 bounds, set_thread_area() writes the TLS descriptor pointed to by
79 u_info into the thread's TLS array.
80
81 When set_thread_area() is passed an entry_number of -1, it searches for
82 a free TLS entry. If set_thread_area() finds a free TLS entry, the
83 value of u_info->entry_number is set upon return to show which entry
84 was changed.
85
86 A user_desc is considered "empty" if read_exec_only and seg_not_present
87 are set to 1 and all of the other fields are 0. If an "empty" descrip‐
88 tor is passed to set_thread_area(), the corresponding TLS entry will be
89 cleared. See BUGS for additional details.
90
91 Since Linux 3.19, set_thread_area() cannot be used to write non-present
92 segments, 16-bit segments, or code segments, although clearing a seg‐
93 ment is still acceptable.
94
96 On x86, these system calls return 0 on success, and -1 on failure, with
97 errno set to indicate the error.
98
99 On MIPS and m68k, set_thread_area() always returns 0. On m68k,
100 get_thread_area() returns the thread area pointer value (previously set
101 via set_thread_area()).
102
104 EFAULT u_info is an invalid pointer.
105
106 EINVAL u_info->entry_number is out of bounds.
107
108 ENOSYS get_thread_area() or set_thread_area() was invoked as a 64-bit
109 system call.
110
111 ESRCH (set_thread_area()) A free TLS entry could not be located.
112
114 Linux.
115
117 set_thread_area()
118 Linux 2.5.29.
119
120 get_thread_area()
121 Linux 2.5.32.
122
124 These system calls are generally intended for use only by threading li‐
125 braries.
126
127 arch_prctl(2) can interfere with set_thread_area() on x86. See
128 arch_prctl(2) for more details. This is not normally a problem, as
129 arch_prctl(2) is normally used only by 64-bit programs.
130
131 On MIPS, the current value of the thread area pointer can be obtained
132 using the instruction:
133
134 rdhwr dest, $29
135
136 This instruction traps and is handled by kernel.
137
139 On 64-bit kernels before Linux 3.19, one of the padding bits in
140 user_desc, if set, would prevent the descriptor from being considered
141 empty (see modify_ldt(2)). As a result, the only reliable way to clear
142 a TLS entry is to use memset(3) to zero the entire user_desc structure,
143 including padding bits, and then to set the read_exec_only and
144 seg_not_present bits. On Linux 3.19, a user_desc consisting entirely
145 of zeros except for entry_number will also be interpreted as a request
146 to clear a TLS entry, but this behaved differently on older kernels.
147
148 Prior to Linux 3.19, the DS and ES segment registers must not reference
149 TLS entries.
150
152 arch_prctl(2), modify_ldt(2), ptrace(2) (PTRACE_GET_THREAD_AREA and
153 PTRACE_SET_THREAD_AREA)
154
155
156
157Linux man-pages 6.04 2023-03-30 set_thread_area(2)