x86: introduce fill_ldt
[deliverable/linux.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 fill_ldt((struct desc_struct *)desc, &info);
72
73 if (t == &current->thread)
74 load_TLS(t, cpu);
75
76 put_cpu();
77 return 0;
78 }
79
80 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
81 {
82 return do_set_thread_area(current, -1, u_info, 1);
83 }
84
85
86 /*
87 * Get the current Thread-Local Storage area:
88 */
89
90 #define GET_LIMIT(desc) (((desc)[0] & 0x0ffff) | ((desc)[1] & 0xf0000))
91 #define GET_32BIT(desc) (((desc)[1] >> 22) & 1)
92 #define GET_CONTENTS(desc) (((desc)[1] >> 10) & 3)
93 #define GET_WRITABLE(desc) (((desc)[1] >> 9) & 1)
94 #define GET_LIMIT_PAGES(desc) (((desc)[1] >> 23) & 1)
95 #define GET_PRESENT(desc) (((desc)[1] >> 15) & 1)
96 #define GET_USEABLE(desc) (((desc)[1] >> 20) & 1)
97 #define GET_LONGMODE(desc) (((desc)[1] >> 21) & 1)
98
99 int do_get_thread_area(struct task_struct *p, int idx,
100 struct user_desc __user *u_info)
101 {
102 struct thread_struct *t = &p->thread;
103 struct user_desc info;
104 u32 *desc;
105
106 if (idx == -1 && get_user(idx, &u_info->entry_number))
107 return -EFAULT;
108 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
109 return -EINVAL;
110
111 desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
112
113 memset(&info, 0, sizeof(struct user_desc));
114 info.entry_number = idx;
115 info.base_addr = get_desc_base((void *)desc);
116 info.limit = GET_LIMIT(desc);
117 info.seg_32bit = GET_32BIT(desc);
118 info.contents = GET_CONTENTS(desc);
119 info.read_exec_only = !GET_WRITABLE(desc);
120 info.limit_in_pages = GET_LIMIT_PAGES(desc);
121 info.seg_not_present = !GET_PRESENT(desc);
122 info.useable = GET_USEABLE(desc);
123 #ifdef CONFIG_X86_64
124 info.lm = GET_LONGMODE(desc);
125 #endif
126
127 if (copy_to_user(u_info, &info, sizeof(info)))
128 return -EFAULT;
129 return 0;
130 }
131
132 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
133 {
134 return do_get_thread_area(current, -1, u_info);
135 }
This page took 0.033282 seconds and 6 git commands to generate.