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((struct n_desc_struct
*)(t
->tls_array
) + idx
))
23 return idx
+ GDT_ENTRY_TLS_MIN
;
28 * Set a given TLS descriptor:
29 * When you want addresses > 32bit use arch_prctl()
31 int do_set_thread_area(struct thread_struct
*t
, struct user_desc __user
*u_info
)
33 struct user_desc info
;
34 struct n_desc_struct
*desc
;
37 if (copy_from_user(&info
, u_info
, sizeof(info
)))
40 idx
= info
.entry_number
;
43 * index -1 means the kernel should try to find and
44 * allocate an empty descriptor:
50 if (put_user(idx
, &u_info
->entry_number
))
54 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
57 desc
= ((struct n_desc_struct
*)t
->tls_array
) + idx
- GDT_ENTRY_TLS_MIN
;
60 * We must not get preempted while modifying the TLS.
64 if (LDT_empty(&info
)) {
68 desc
->a
= LDT_entry_a(&info
);
69 desc
->b
= LDT_entry_b(&info
);
71 if (t
== ¤t
->thread
)
78 asmlinkage
long sys32_set_thread_area(struct user_desc __user
*u_info
)
80 return do_set_thread_area(¤t
->thread
, u_info
);
85 * Get the current Thread-Local Storage area:
88 #define GET_BASE(desc) ( \
89 (((desc)->a >> 16) & 0x0000ffff) | \
90 (((desc)->b << 16) & 0x00ff0000) | \
91 ( (desc)->b & 0xff000000) )
93 #define GET_LIMIT(desc) ( \
94 ((desc)->a & 0x0ffff) | \
95 ((desc)->b & 0xf0000) )
97 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
98 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
99 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
100 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
101 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
102 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
103 #define GET_LONGMODE(desc) (((desc)->b >> 21) & 1)
105 int do_get_thread_area(struct thread_struct
*t
, struct user_desc __user
*u_info
)
107 struct user_desc info
;
108 struct n_desc_struct
*desc
;
111 if (get_user(idx
, &u_info
->entry_number
))
113 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
116 desc
= ((struct n_desc_struct
*)t
->tls_array
) + idx
- GDT_ENTRY_TLS_MIN
;
118 memset(&info
, 0, sizeof(struct user_desc
));
119 info
.entry_number
= idx
;
120 info
.base_addr
= GET_BASE(desc
);
121 info
.limit
= GET_LIMIT(desc
);
122 info
.seg_32bit
= GET_32BIT(desc
);
123 info
.contents
= GET_CONTENTS(desc
);
124 info
.read_exec_only
= !GET_WRITABLE(desc
);
125 info
.limit_in_pages
= GET_LIMIT_PAGES(desc
);
126 info
.seg_not_present
= !GET_PRESENT(desc
);
127 info
.useable
= GET_USEABLE(desc
);
128 info
.lm
= GET_LONGMODE(desc
);
130 if (copy_to_user(u_info
, &info
, sizeof(info
)))
135 asmlinkage
long sys32_get_thread_area(struct user_desc __user
*u_info
)
137 return do_get_thread_area(¤t
->thread
, u_info
);
141 int ia32_child_tls(struct task_struct
*p
, struct pt_regs
*childregs
)
143 struct n_desc_struct
*desc
;
144 struct user_desc info
;
145 struct user_desc __user
*cp
;
148 cp
= (void __user
*)childregs
->rsi
;
149 if (copy_from_user(&info
, cp
, sizeof(info
)))
151 if (LDT_empty(&info
))
154 idx
= info
.entry_number
;
155 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
158 desc
= (struct n_desc_struct
*)(p
->thread
.tls_array
) + idx
- GDT_ENTRY_TLS_MIN
;
159 desc
->a
= LDT_entry_a(&info
);
160 desc
->b
= LDT_entry_b(&info
);