1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/sched.h>
4 #include <linux/user.h>
6 #include <asm/uaccess.h>
8 #include <asm/system.h>
10 #include <asm/processor.h>
11 #include <asm/proto.h>
14 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
16 static int get_free_idx(void)
18 struct thread_struct *t = ¤t->thread;
21 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
22 if (desc_empty(&t->tls_array[idx]))
23 return idx + GDT_ENTRY_TLS_MIN;
27 static void set_tls_desc(struct task_struct *p, int idx,
28 const struct user_desc *info)
30 struct thread_struct *t = &p->thread;
31 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
35 * We must not get preempted while modifying the TLS.
40 desc->a = desc->b = 0;
44 if (t == ¤t->thread)
51 * Set a given TLS descriptor:
53 int do_set_thread_area(struct task_struct *p, int idx,
54 struct user_desc __user *u_info,
57 struct user_desc info;
59 if (copy_from_user(&info, u_info, sizeof(info)))
63 idx = info.entry_number;
66 * index -1 means the kernel should try to find and
67 * allocate an empty descriptor:
69 if (idx == -1 && can_allocate) {
73 if (put_user(idx, &u_info->entry_number))
77 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
80 set_tls_desc(p, idx, &info);
85 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
87 return do_set_thread_area(current, -1, u_info, 1);
92 * Get the current Thread-Local Storage area:
95 static void fill_user_desc(struct user_desc *info, int idx,
96 const struct desc_struct *desc)
99 memset(info, 0, sizeof(*info));
100 info->entry_number = idx;
101 info->base_addr = get_desc_base(desc);
102 info->limit = get_desc_limit(desc);
103 info->seg_32bit = desc->d;
104 info->contents = desc->type >> 2;
105 info->read_exec_only = !(desc->type & 2);
106 info->limit_in_pages = desc->g;
107 info->seg_not_present = !desc->p;
108 info->useable = desc->avl;
114 int do_get_thread_area(struct task_struct *p, int idx,
115 struct user_desc __user *u_info)
117 struct user_desc info;
119 if (idx == -1 && get_user(idx, &u_info->entry_number))
122 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
125 fill_user_desc(&info, idx,
126 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
128 if (copy_to_user(u_info, &info, sizeof(info)))
133 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
135 return do_get_thread_area(current, -1, u_info);