Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / modify_ldt.2
blobb67a67690622b27c048bf7217dc44426b7e8570a
1 .\" Copyright (c) 1995 Michael Chastain (mec@duracef.shout.net), 22 July 1995.
2 .\" Copyright (c) 2015 Andrew Lutomirski
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\"
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
14 .\"
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 .\" GNU General Public License for more details.
19 .\"
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
23 .\" %%%LICENSE_END
24 .\"
25 .TH MODIFY_LDT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 modify_ldt \- get or set a per-process LDT entry
28 .SH SYNOPSIS
29 .nf
30 .BR "#include <asm/ldt.h>" "         /* Definition of " "struct user_desc" " */"
31 .BR "#include <sys/syscall.h>" "     /* Definition of " SYS_* " constants */"
32 .B #include <unistd.h>
33 .PP
34 .BI "int syscall(SYS_modify_ldt, int " func ", void *" ptr ,
35 .BI "            unsigned long " bytecount );
36 .fi
37 .PP
38 .IR Note :
39 glibc provides no wrapper for
40 .BR modify_ldt (),
41 necessitating the use of
42 .BR syscall (2).
43 .SH DESCRIPTION
44 .BR modify_ldt ()
45 reads or writes the local descriptor table (LDT) for a process.
46 The LDT
47 is an array of segment descriptors that can be referenced by user code.
48 Linux allows processes to configure a per-process (actually per-mm) LDT.
49 For more information about the LDT, see the Intel Software Developer's
50 Manual or the AMD Architecture Programming Manual.
51 .PP
52 When
53 .I func
54 is 0,
55 .BR modify_ldt ()
56 reads the LDT into the memory pointed to by
57 .IR ptr .
58 The number of bytes read is the smaller of
59 .I bytecount
60 and the actual size of the LDT, although the kernel may act as though
61 the LDT is padded with additional trailing zero bytes.
62 On success,
63 .BR modify_ldt ()
64 will return the number of bytes read.
65 .PP
66 When
67 .I func
68 is 1 or 0x11,
69 .BR modify_ldt ()
70 modifies the LDT entry indicated by
71 .IR ptr\->entry_number .
72 .I ptr
73 points to a
74 .I user_desc
75 structure
76 and
77 .I bytecount
78 must equal the size of this structure.
79 .PP
80 The
81 .I user_desc
82 structure is defined in \fI<asm/ldt.h>\fP as:
83 .PP
84 .in +4n
85 .EX
86 struct user_desc {
87     unsigned int  entry_number;
88     unsigned int  base_addr;
89     unsigned int  limit;
90     unsigned int  seg_32bit:1;
91     unsigned int  contents:2;
92     unsigned int  read_exec_only:1;
93     unsigned int  limit_in_pages:1;
94     unsigned int  seg_not_present:1;
95     unsigned int  useable:1;
97 .EE
98 .in
99 .PP
100 In Linux 2.4 and earlier, this structure was named
101 .IR modify_ldt_ldt_s .
104 .I contents
105 field is the segment type (data, expand-down data, non-conforming code, or
106 conforming code).
107 The other fields match their descriptions in the CPU manual, although
108 .BR modify_ldt ()
109 cannot set the hardware-defined "accessed" bit described in the CPU manual.
112 .I user_desc
113 is considered "empty" if
114 .I read_exec_only
116 .I seg_not_present
117 are set to 1 and all of the other fields are 0.
118 An LDT entry can be cleared by setting it to an "empty"
119 .I user_desc
120 or, if
121 .I func
122 is 1, by setting both
123 .I base
125 .I limit
126 to 0.
128 A conforming code segment (i.e., one with
129 .IR contents==3 )
130 will be rejected if
132 func
133 is 1 or if
134 .I seg_not_present
135 is 0.
137 When
138 .I func
139 is 2,
140 .BR modify_ldt ()
141 will read zeros.
142 This appears to be a leftover from Linux 2.4.
143 .SH RETURN VALUE
144 On success,
145 .BR modify_ldt ()
146 returns either the actual number of bytes read (for reading)
147 or 0 (for writing).
148 On failure,
149 .BR modify_ldt ()
150 returns \-1 and sets
151 .I errno
152 to indicate the error.
153 .SH ERRORS
155 .B EFAULT
156 .I ptr
157 points outside the address space.
159 .B EINVAL
160 .I ptr
161 is 0,
163 .I func
164 is 1 and
165 .I bytecount
166 is not equal to the size of the structure
167 .IR user_desc ,
169 .I func
170 is 1 or 0x11 and the new LDT entry has invalid values.
172 .B ENOSYS
173 .I func
174 is neither 0, 1, 2, nor 0x11.
175 .SH CONFORMING TO
176 This call is Linux-specific and should not be used in programs intended
177 to be portable.
178 .SH NOTES
179 .BR modify_ldt ()
180 should not be used for thread-local storage, as it slows down context
181 switches and only supports a limited number of threads.
182 Threading libraries should use
183 .BR set_thread_area (2)
185 .BR arch_prctl (2)
186 instead, except on extremely old kernels that do not support those system
187 calls.
189 The normal use for
190 .BR modify_ldt ()
191 is to run legacy 16-bit or segmented 32-bit code.
192 Not all kernels allow 16-bit segments to be installed, however.
194 Even on 64-bit kernels,
195 .BR modify_ldt ()
196 cannot be used to create a long mode (i.e., 64-bit) code segment.
197 The undocumented field "lm" in
198 .IR user_desc
199 is not useful, and, despite its name,
200 does not result in a long mode segment.
201 .SH BUGS
202 On 64-bit kernels before Linux 3.19,
203 .\" commit e30ab185c490e9a9381385529e0fd32f0a399495
204 setting the "lm" bit in
205 .IR user_desc
206 prevents the descriptor from being considered empty.
207 Keep in mind that the
208 "lm" bit does not exist in the 32-bit headers, but these buggy kernels
209 will still notice the bit even when set in a 32-bit process.
210 .SH SEE ALSO
211 .BR arch_prctl (2),
212 .BR set_thread_area (2),
213 .BR vm86 (2)