blob: e7650bd71109af8819e9cb2c7479a2162353f917 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/sched.h>
4#include <linux/user.h>
Roland McGrath4c79a2d2008-01-30 13:31:52 +01005#include <linux/regset.h>
Al Viro2cf09662013-01-21 15:25:54 -05006#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8#include <asm/uaccess.h>
9#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/ldt.h>
11#include <asm/processor.h>
12#include <asm/proto.h>
13
Roland McGrath4c79a2d2008-01-30 13:31:52 +010014#include "tls.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
17 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
18 */
19static int get_free_idx(void)
20{
21 struct thread_struct *t = &current->thread;
22 int idx;
23
24 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
Roland McGrathefd1ca52008-01-30 13:30:46 +010025 if (desc_empty(&t->tls_array[idx]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return idx + GDT_ENTRY_TLS_MIN;
27 return -ESRCH;
28}
29
Andy Lutomirski41bdc782014-12-04 16:48:16 -080030static bool tls_desc_okay(const struct user_desc *info)
31{
32 if (LDT_empty(info))
33 return true;
34
35 /*
36 * espfix is required for 16-bit data segments, but espfix
37 * only works for LDT segments.
38 */
39 if (!info->seg_32bit)
40 return false;
41
42 return true;
43}
44
Roland McGrath1bd57182008-01-30 13:31:51 +010045static void set_tls_desc(struct task_struct *p, int idx,
Roland McGrath4c79a2d2008-01-30 13:31:52 +010046 const struct user_desc *info, int n)
Roland McGrath1bd57182008-01-30 13:31:51 +010047{
48 struct thread_struct *t = &p->thread;
49 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
50 int cpu;
51
52 /*
53 * We must not get preempted while modifying the TLS.
54 */
55 cpu = get_cpu();
56
Roland McGrath4c79a2d2008-01-30 13:31:52 +010057 while (n-- > 0) {
58 if (LDT_empty(info))
59 desc->a = desc->b = 0;
60 else
61 fill_ldt(desc, info);
62 ++info;
63 ++desc;
64 }
Roland McGrath1bd57182008-01-30 13:31:51 +010065
66 if (t == &current->thread)
67 load_TLS(t, cpu);
68
69 put_cpu();
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * Set a given TLS descriptor:
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Roland McGrathefd1ca52008-01-30 13:30:46 +010075int do_set_thread_area(struct task_struct *p, int idx,
76 struct user_desc __user *u_info,
77 int can_allocate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct user_desc info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (copy_from_user(&info, u_info, sizeof(info)))
82 return -EFAULT;
83
Andy Lutomirski41bdc782014-12-04 16:48:16 -080084 if (!tls_desc_okay(&info))
85 return -EINVAL;
86
Roland McGrathefd1ca52008-01-30 13:30:46 +010087 if (idx == -1)
88 idx = info.entry_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /*
91 * index -1 means the kernel should try to find and
92 * allocate an empty descriptor:
93 */
Roland McGrathefd1ca52008-01-30 13:30:46 +010094 if (idx == -1 && can_allocate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 idx = get_free_idx();
96 if (idx < 0)
97 return idx;
98 if (put_user(idx, &u_info->entry_number))
99 return -EFAULT;
100 }
101
102 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
103 return -EINVAL;
104
Roland McGrath4c79a2d2008-01-30 13:31:52 +0100105 set_tls_desc(p, idx, &info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
Al Viro2cf09662013-01-21 15:25:54 -0500110SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
Roland McGrath13abd0e52008-01-30 13:30:45 +0100111{
Al Viro2cf09662013-01-21 15:25:54 -0500112 return do_set_thread_area(current, -1, u_info, 1);
Roland McGrath13abd0e52008-01-30 13:30:45 +0100113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115
116/*
117 * Get the current Thread-Local Storage area:
118 */
119
Roland McGrath1bd57182008-01-30 13:31:51 +0100120static void fill_user_desc(struct user_desc *info, int idx,
121 const struct desc_struct *desc)
122
123{
124 memset(info, 0, sizeof(*info));
125 info->entry_number = idx;
126 info->base_addr = get_desc_base(desc);
127 info->limit = get_desc_limit(desc);
128 info->seg_32bit = desc->d;
129 info->contents = desc->type >> 2;
130 info->read_exec_only = !(desc->type & 2);
131 info->limit_in_pages = desc->g;
132 info->seg_not_present = !desc->p;
133 info->useable = desc->avl;
134#ifdef CONFIG_X86_64
135 info->lm = desc->l;
136#endif
137}
Roland McGrath13abd0e52008-01-30 13:30:45 +0100138
Roland McGrathefd1ca52008-01-30 13:30:46 +0100139int do_get_thread_area(struct task_struct *p, int idx,
140 struct user_desc __user *u_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct user_desc info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Roland McGrathefd1ca52008-01-30 13:30:46 +0100144 if (idx == -1 && get_user(idx, &u_info->entry_number))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EFAULT;
Roland McGrath1bd57182008-01-30 13:31:51 +0100146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
148 return -EINVAL;
149
Roland McGrath1bd57182008-01-30 13:31:51 +0100150 fill_user_desc(&info, idx,
151 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (copy_to_user(u_info, &info, sizeof(info)))
154 return -EFAULT;
155 return 0;
156}
157
Al Viro2cf09662013-01-21 15:25:54 -0500158SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Al Viro2cf09662013-01-21 15:25:54 -0500160 return do_get_thread_area(current, -1, u_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Roland McGrath4c79a2d2008-01-30 13:31:52 +0100162
163int regset_tls_active(struct task_struct *target,
164 const struct user_regset *regset)
165{
166 struct thread_struct *t = &target->thread;
167 int n = GDT_ENTRY_TLS_ENTRIES;
168 while (n > 0 && desc_empty(&t->tls_array[n - 1]))
169 --n;
170 return n;
171}
172
173int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
174 unsigned int pos, unsigned int count,
175 void *kbuf, void __user *ubuf)
176{
177 const struct desc_struct *tls;
178
Dan Carpenter8f0750f2012-03-24 10:52:50 +0300179 if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
Roland McGrath4c79a2d2008-01-30 13:31:52 +0100180 (pos % sizeof(struct user_desc)) != 0 ||
181 (count % sizeof(struct user_desc)) != 0)
182 return -EINVAL;
183
184 pos /= sizeof(struct user_desc);
185 count /= sizeof(struct user_desc);
186
187 tls = &target->thread.tls_array[pos];
188
189 if (kbuf) {
190 struct user_desc *info = kbuf;
191 while (count-- > 0)
192 fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
193 tls++);
194 } else {
195 struct user_desc __user *u_info = ubuf;
196 while (count-- > 0) {
197 struct user_desc info;
198 fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
199 if (__copy_to_user(u_info++, &info, sizeof(info)))
200 return -EFAULT;
201 }
202 }
203
204 return 0;
205}
206
207int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
208 unsigned int pos, unsigned int count,
209 const void *kbuf, const void __user *ubuf)
210{
211 struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
212 const struct user_desc *info;
Andy Lutomirski41bdc782014-12-04 16:48:16 -0800213 int i;
Roland McGrath4c79a2d2008-01-30 13:31:52 +0100214
Dan Carpenter8f0750f2012-03-24 10:52:50 +0300215 if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
Roland McGrath4c79a2d2008-01-30 13:31:52 +0100216 (pos % sizeof(struct user_desc)) != 0 ||
217 (count % sizeof(struct user_desc)) != 0)
218 return -EINVAL;
219
220 if (kbuf)
221 info = kbuf;
222 else if (__copy_from_user(infobuf, ubuf, count))
223 return -EFAULT;
224 else
225 info = infobuf;
226
Andy Lutomirski41bdc782014-12-04 16:48:16 -0800227 for (i = 0; i < count / sizeof(struct user_desc); i++)
228 if (!tls_desc_okay(info + i))
229 return -EINVAL;
230
Roland McGrath4c79a2d2008-01-30 13:31:52 +0100231 set_tls_desc(target,
232 GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
233 info, count / sizeof(struct user_desc));
234
235 return 0;
236}