1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/sched.h>
4 #include <linux/user.h>
5 #include <linux/regset.h>
7 #include <asm/uaccess.h>
9 #include <asm/system.h>
11 #include <asm/processor.h>
12 #include <asm/proto.h>
13 #include <asm/syscalls.h>
18 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
20 static int get_free_idx(void)
22 struct thread_struct *t = ¤t->thread;
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;
31 static bool tls_desc_okay(const struct user_desc *info)
37 * espfix is required for 16-bit data segments, but espfix
38 * only works for LDT segments.
46 static void set_tls_desc(struct task_struct *p, int idx,
47 const struct user_desc *info, int n)
49 struct thread_struct *t = &p->thread;
50 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
54 * We must not get preempted while modifying the TLS.
60 desc->a = desc->b = 0;
67 if (t == ¤t->thread)
74 * Set a given TLS descriptor:
76 int do_set_thread_area(struct task_struct *p, int idx,
77 struct user_desc __user *u_info,
80 struct user_desc info;
82 if (copy_from_user(&info, u_info, sizeof(info)))
85 if (!tls_desc_okay(&info))
89 idx = info.entry_number;
92 * index -1 means the kernel should try to find and
93 * allocate an empty descriptor:
95 if (idx == -1 && can_allocate) {
99 if (put_user(idx, &u_info->entry_number))
103 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
106 set_tls_desc(p, idx, &info, 1);
111 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
113 int ret = do_set_thread_area(current, -1, u_info, 1);
114 asmlinkage_protect(1, ret, u_info);
120 * Get the current Thread-Local Storage area:
123 static void fill_user_desc(struct user_desc *info, int idx,
124 const struct desc_struct *desc)
127 memset(info, 0, sizeof(*info));
128 info->entry_number = idx;
129 info->base_addr = get_desc_base(desc);
130 info->limit = get_desc_limit(desc);
131 info->seg_32bit = desc->d;
132 info->contents = desc->type >> 2;
133 info->read_exec_only = !(desc->type & 2);
134 info->limit_in_pages = desc->g;
135 info->seg_not_present = !desc->p;
136 info->useable = desc->avl;
142 int do_get_thread_area(struct task_struct *p, int idx,
143 struct user_desc __user *u_info)
145 struct user_desc info;
147 if (idx == -1 && get_user(idx, &u_info->entry_number))
150 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
153 fill_user_desc(&info, idx,
154 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
156 if (copy_to_user(u_info, &info, sizeof(info)))
161 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
163 int ret = do_get_thread_area(current, -1, u_info);
164 asmlinkage_protect(1, ret, u_info);
168 int regset_tls_active(struct task_struct *target,
169 const struct user_regset *regset)
171 struct thread_struct *t = &target->thread;
172 int n = GDT_ENTRY_TLS_ENTRIES;
173 while (n > 0 && desc_empty(&t->tls_array[n - 1]))
178 int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
179 unsigned int pos, unsigned int count,
180 void *kbuf, void __user *ubuf)
182 const struct desc_struct *tls;
184 if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
185 (pos % sizeof(struct user_desc)) != 0 ||
186 (count % sizeof(struct user_desc)) != 0)
189 pos /= sizeof(struct user_desc);
190 count /= sizeof(struct user_desc);
192 tls = &target->thread.tls_array[pos];
195 struct user_desc *info = kbuf;
197 fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
200 struct user_desc __user *u_info = ubuf;
201 while (count-- > 0) {
202 struct user_desc info;
203 fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
204 if (__copy_to_user(u_info++, &info, sizeof(info)))
212 int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
213 unsigned int pos, unsigned int count,
214 const void *kbuf, const void __user *ubuf)
216 struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
217 const struct user_desc *info;
220 if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
221 (pos % sizeof(struct user_desc)) != 0 ||
222 (count % sizeof(struct user_desc)) != 0)
227 else if (__copy_from_user(infobuf, ubuf, count))
232 for (i = 0; i < count / sizeof(struct user_desc); i++)
233 if (!tls_desc_okay(info + i))
237 GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
238 info, count / sizeof(struct user_desc));