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;
28 * Set a given TLS descriptor:
30 int do_set_thread_area(struct task_struct *p, int idx,
31 struct user_desc __user *u_info,
34 struct thread_struct *t = &p->thread;
35 struct user_desc info;
39 if (copy_from_user(&info, u_info, sizeof(info)))
43 idx = info.entry_number;
46 * index -1 means the kernel should try to find and
47 * allocate an empty descriptor:
49 if (idx == -1 && can_allocate) {
53 if (put_user(idx, &u_info->entry_number))
57 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
60 desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
63 * We must not get preempted while modifying the TLS.
67 if (LDT_empty(&info)) {
71 desc[0] = LDT_entry_a(&info);
72 desc[1] = LDT_entry_b(&info);
74 if (t == ¤t->thread)
81 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
83 return do_set_thread_area(current, -1, u_info, 1);
88 * Get the current Thread-Local Storage area:
91 #define GET_LIMIT(desc) (((desc)[0] & 0x0ffff) | ((desc)[1] & 0xf0000))
92 #define GET_32BIT(desc) (((desc)[1] >> 22) & 1)
93 #define GET_CONTENTS(desc) (((desc)[1] >> 10) & 3)
94 #define GET_WRITABLE(desc) (((desc)[1] >> 9) & 1)
95 #define GET_LIMIT_PAGES(desc) (((desc)[1] >> 23) & 1)
96 #define GET_PRESENT(desc) (((desc)[1] >> 15) & 1)
97 #define GET_USEABLE(desc) (((desc)[1] >> 20) & 1)
98 #define GET_LONGMODE(desc) (((desc)[1] >> 21) & 1)
100 int do_get_thread_area(struct task_struct *p, int idx,
101 struct user_desc __user *u_info)
103 struct thread_struct *t = &p->thread;
104 struct user_desc info;
107 if (idx == -1 && get_user(idx, &u_info->entry_number))
109 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
112 desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
114 memset(&info, 0, sizeof(struct user_desc));
115 info.entry_number = idx;
116 info.base_addr = get_desc_base((void *)desc);
117 info.limit = GET_LIMIT(desc);
118 info.seg_32bit = GET_32BIT(desc);
119 info.contents = GET_CONTENTS(desc);
120 info.read_exec_only = !GET_WRITABLE(desc);
121 info.limit_in_pages = GET_LIMIT_PAGES(desc);
122 info.seg_not_present = !GET_PRESENT(desc);
123 info.useable = GET_USEABLE(desc);
125 info.lm = GET_LONGMODE(desc);
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);