blob: f11c92a3faac8a2ab91f4abb26510d392c5fb7ef [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>
5
6#include <asm/uaccess.h>
7#include <asm/desc.h>
8#include <asm/system.h>
9#include <asm/ldt.h>
10#include <asm/processor.h>
11#include <asm/proto.h>
12
13/*
14 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
15 */
16static int get_free_idx(void)
17{
18 struct thread_struct *t = &current->thread;
19 int idx;
20
21 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
Roland McGrathefd1ca52008-01-30 13:30:46 +010022 if (desc_empty(&t->tls_array[idx]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 return idx + GDT_ENTRY_TLS_MIN;
24 return -ESRCH;
25}
26
Roland McGrath1bd57182008-01-30 13:31:51 +010027static void set_tls_desc(struct task_struct *p, int idx,
28 const struct user_desc *info)
29{
30 struct thread_struct *t = &p->thread;
31 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
32 int cpu;
33
34 /*
35 * We must not get preempted while modifying the TLS.
36 */
37 cpu = get_cpu();
38
39 if (LDT_empty(info))
40 desc->a = desc->b = 0;
41 else
42 fill_ldt(desc, info);
43
44 if (t == &current->thread)
45 load_TLS(t, cpu);
46
47 put_cpu();
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Set a given TLS descriptor:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Roland McGrathefd1ca52008-01-30 13:30:46 +010053int do_set_thread_area(struct task_struct *p, int idx,
54 struct user_desc __user *u_info,
55 int can_allocate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct user_desc info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (copy_from_user(&info, u_info, sizeof(info)))
60 return -EFAULT;
61
Roland McGrathefd1ca52008-01-30 13:30:46 +010062 if (idx == -1)
63 idx = info.entry_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /*
66 * index -1 means the kernel should try to find and
67 * allocate an empty descriptor:
68 */
Roland McGrathefd1ca52008-01-30 13:30:46 +010069 if (idx == -1 && can_allocate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 idx = get_free_idx();
71 if (idx < 0)
72 return idx;
73 if (put_user(idx, &u_info->entry_number))
74 return -EFAULT;
75 }
76
77 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
78 return -EINVAL;
79
Roland McGrath1bd57182008-01-30 13:31:51 +010080 set_tls_desc(p, idx, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83}
84
Roland McGrathefd1ca52008-01-30 13:30:46 +010085asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
Roland McGrath13abd0e2008-01-30 13:30:45 +010086{
Roland McGrathefd1ca52008-01-30 13:30:46 +010087 return do_set_thread_area(current, -1, u_info, 1);
Roland McGrath13abd0e2008-01-30 13:30:45 +010088}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90
91/*
92 * Get the current Thread-Local Storage area:
93 */
94
Roland McGrath1bd57182008-01-30 13:31:51 +010095static void fill_user_desc(struct user_desc *info, int idx,
96 const struct desc_struct *desc)
97
98{
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;
109#ifdef CONFIG_X86_64
110 info->lm = desc->l;
111#endif
112}
Roland McGrath13abd0e2008-01-30 13:30:45 +0100113
Roland McGrathefd1ca52008-01-30 13:30:46 +0100114int do_get_thread_area(struct task_struct *p, int idx,
115 struct user_desc __user *u_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct user_desc info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Roland McGrathefd1ca52008-01-30 13:30:46 +0100119 if (idx == -1 && get_user(idx, &u_info->entry_number))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -EFAULT;
Roland McGrath1bd57182008-01-30 13:31:51 +0100121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
123 return -EINVAL;
124
Roland McGrath1bd57182008-01-30 13:31:51 +0100125 fill_user_desc(&info, idx,
126 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (copy_to_user(u_info, &info, sizeof(info)))
129 return -EFAULT;
130 return 0;
131}
132
Roland McGrathefd1ca52008-01-30 13:30:46 +0100133asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Roland McGrathefd1ca52008-01-30 13:30:46 +0100135 return do_get_thread_area(current, -1, u_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}