x86: TLS cleanup
[pandora-kernel.git] / arch / x86 / kernel / tls.c
1 #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  */
16 static 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++)
22                 if (desc_empty(&t->tls_array[idx]))
23                         return idx + GDT_ENTRY_TLS_MIN;
24         return -ESRCH;
25 }
26
27 /*
28  * Set a given TLS descriptor:
29  */
30 int do_set_thread_area(struct task_struct *p, int idx,
31                        struct user_desc __user *u_info,
32                        int can_allocate)
33 {
34         struct thread_struct *t = &p->thread;
35         struct user_desc info;
36         u32 *desc;
37         int cpu;
38
39         if (copy_from_user(&info, u_info, sizeof(info)))
40                 return -EFAULT;
41
42         if (idx == -1)
43                 idx = info.entry_number;
44
45         /*
46          * index -1 means the kernel should try to find and
47          * allocate an empty descriptor:
48          */
49         if (idx == -1 && can_allocate) {
50                 idx = get_free_idx();
51                 if (idx < 0)
52                         return idx;
53                 if (put_user(idx, &u_info->entry_number))
54                         return -EFAULT;
55         }
56
57         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
58                 return -EINVAL;
59
60         desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
61
62         /*
63          * We must not get preempted while modifying the TLS.
64          */
65         cpu = get_cpu();
66
67         if (LDT_empty(&info)) {
68                 desc[0] = 0;
69                 desc[1] = 0;
70         } else {
71                 desc[0] = LDT_entry_a(&info);
72                 desc[1] = LDT_entry_b(&info);
73         }
74         if (t == &current->thread)
75                 load_TLS(t, cpu);
76
77         put_cpu();
78         return 0;
79 }
80
81 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
82 {
83         return do_set_thread_area(current, -1, u_info, 1);
84 }
85
86
87 /*
88  * Get the current Thread-Local Storage area:
89  */
90
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)
99
100 int do_get_thread_area(struct task_struct *p, int idx,
101                        struct user_desc __user *u_info)
102 {
103         struct thread_struct *t = &p->thread;
104         struct user_desc info;
105         u32 *desc;
106
107         if (idx == -1 && get_user(idx, &u_info->entry_number))
108                 return -EFAULT;
109         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
110                 return -EINVAL;
111
112         desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
113
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);
124 #ifdef CONFIG_X86_64
125         info.lm = GET_LONGMODE(desc);
126 #endif
127
128         if (copy_to_user(u_info, &info, sizeof(info)))
129                 return -EFAULT;
130         return 0;
131 }
132
133 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
134 {
135         return do_get_thread_area(current, -1, u_info);
136 }