1
2#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/sched.h>
5#include <linux/user.h>
6#include <linux/regset.h>
7#include <linux/syscalls.h>
8
9#include <linux/uaccess.h>
10#include <asm/desc.h>
11#include <asm/ldt.h>
12#include <asm/processor.h>
13#include <asm/proto.h>
14
15#include "tls.h"
16
17
18
19
20static int get_free_idx(void)
21{
22 struct thread_struct *t = ¤t->thread;
23 int idx;
24
25 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
26 if (desc_empty(&t->tls_array[idx]))
27 return idx + GDT_ENTRY_TLS_MIN;
28 return -ESRCH;
29}
30
31static bool tls_desc_okay(const struct user_desc *info)
32{
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 if (LDT_empty(info) || LDT_zero(info))
55 return true;
56
57
58
59
60
61 if (!info->seg_32bit)
62 return false;
63
64
65 if (info->contents > 1)
66 return false;
67
68
69
70
71
72
73
74
75
76
77 if (info->seg_not_present)
78 return false;
79
80 return true;
81}
82
83static void set_tls_desc(struct task_struct *p, int idx,
84 const struct user_desc *info, int n)
85{
86 struct thread_struct *t = &p->thread;
87 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
88 int cpu;
89
90
91
92
93 cpu = get_cpu();
94
95 while (n-- > 0) {
96 if (LDT_empty(info) || LDT_zero(info)) {
97 memset(desc, 0, sizeof(*desc));
98 } else {
99 fill_ldt(desc, info);
100
101
102
103
104
105 desc->type |= 1;
106 }
107 ++info;
108 ++desc;
109 }
110
111 if (t == ¤t->thread)
112 load_TLS(t, cpu);
113
114 put_cpu();
115}
116
117
118
119
120int do_set_thread_area(struct task_struct *p, int idx,
121 struct user_desc __user *u_info,
122 int can_allocate)
123{
124 struct user_desc info;
125 unsigned short __maybe_unused sel, modified_sel;
126
127 if (copy_from_user(&info, u_info, sizeof(info)))
128 return -EFAULT;
129
130 if (!tls_desc_okay(&info))
131 return -EINVAL;
132
133 if (idx == -1)
134 idx = info.entry_number;
135
136
137
138
139
140 if (idx == -1 && can_allocate) {
141 idx = get_free_idx();
142 if (idx < 0)
143 return idx;
144 if (put_user(idx, &u_info->entry_number))
145 return -EFAULT;
146 }
147
148 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
149 return -EINVAL;
150
151 set_tls_desc(p, idx, &info, 1);
152
153
154
155
156
157
158 modified_sel = (idx << 3) | 3;
159
160 if (p == current) {
161#ifdef CONFIG_X86_64
162 savesegment(ds, sel);
163 if (sel == modified_sel)
164 loadsegment(ds, sel);
165
166 savesegment(es, sel);
167 if (sel == modified_sel)
168 loadsegment(es, sel);
169
170 savesegment(fs, sel);
171 if (sel == modified_sel)
172 loadsegment(fs, sel);
173
174 savesegment(gs, sel);
175 if (sel == modified_sel)
176 load_gs_index(sel);
177#endif
178
179#ifdef CONFIG_X86_32_LAZY_GS
180 savesegment(gs, sel);
181 if (sel == modified_sel)
182 loadsegment(gs, sel);
183#endif
184 } else {
185#ifdef CONFIG_X86_64
186 if (p->thread.fsindex == modified_sel)
187 p->thread.fsbase = info.base_addr;
188
189 if (p->thread.gsindex == modified_sel)
190 p->thread.gsbase = info.base_addr;
191#endif
192 }
193
194 return 0;
195}
196
197SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
198{
199 return do_set_thread_area(current, -1, u_info, 1);
200}
201
202
203
204
205
206
207static void fill_user_desc(struct user_desc *info, int idx,
208 const struct desc_struct *desc)
209
210{
211 memset(info, 0, sizeof(*info));
212 info->entry_number = idx;
213 info->base_addr = get_desc_base(desc);
214 info->limit = get_desc_limit(desc);
215 info->seg_32bit = desc->d;
216 info->contents = desc->type >> 2;
217 info->read_exec_only = !(desc->type & 2);
218 info->limit_in_pages = desc->g;
219 info->seg_not_present = !desc->p;
220 info->useable = desc->avl;
221#ifdef CONFIG_X86_64
222 info->lm = desc->l;
223#endif
224}
225
226int do_get_thread_area(struct task_struct *p, int idx,
227 struct user_desc __user *u_info)
228{
229 struct user_desc info;
230
231 if (idx == -1 && get_user(idx, &u_info->entry_number))
232 return -EFAULT;
233
234 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
235 return -EINVAL;
236
237 fill_user_desc(&info, idx,
238 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
239
240 if (copy_to_user(u_info, &info, sizeof(info)))
241 return -EFAULT;
242 return 0;
243}
244
245SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
246{
247 return do_get_thread_area(current, -1, u_info);
248}
249
250int regset_tls_active(struct task_struct *target,
251 const struct user_regset *regset)
252{
253 struct thread_struct *t = &target->thread;
254 int n = GDT_ENTRY_TLS_ENTRIES;
255 while (n > 0 && desc_empty(&t->tls_array[n - 1]))
256 --n;
257 return n;
258}
259
260int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
261 unsigned int pos, unsigned int count,
262 void *kbuf, void __user *ubuf)
263{
264 const struct desc_struct *tls;
265
266 if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
267 (pos % sizeof(struct user_desc)) != 0 ||
268 (count % sizeof(struct user_desc)) != 0)
269 return -EINVAL;
270
271 pos /= sizeof(struct user_desc);
272 count /= sizeof(struct user_desc);
273
274 tls = &target->thread.tls_array[pos];
275
276 if (kbuf) {
277 struct user_desc *info = kbuf;
278 while (count-- > 0)
279 fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
280 tls++);
281 } else {
282 struct user_desc __user *u_info = ubuf;
283 while (count-- > 0) {
284 struct user_desc info;
285 fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
286 if (__copy_to_user(u_info++, &info, sizeof(info)))
287 return -EFAULT;
288 }
289 }
290
291 return 0;
292}
293
294int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
295 unsigned int pos, unsigned int count,
296 const void *kbuf, const void __user *ubuf)
297{
298 struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
299 const struct user_desc *info;
300 int i;
301
302 if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
303 (pos % sizeof(struct user_desc)) != 0 ||
304 (count % sizeof(struct user_desc)) != 0)
305 return -EINVAL;
306
307 if (kbuf)
308 info = kbuf;
309 else if (__copy_from_user(infobuf, ubuf, count))
310 return -EFAULT;
311 else
312 info = infobuf;
313
314 for (i = 0; i < count / sizeof(struct user_desc); i++)
315 if (!tls_desc_okay(info + i))
316 return -EINVAL;
317
318 set_tls_desc(target,
319 GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
320 info, count / sizeof(struct user_desc));
321
322 return 0;
323}
324