2005-04-17 05:20:36 +07:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/user.h>
|
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/desc.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/ldt.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/proto.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_alloc_thread_area: get a yet unused TLS descriptor index.
|
|
|
|
*/
|
|
|
|
static int get_free_idx(void)
|
|
|
|
{
|
|
|
|
struct thread_struct *t = ¤t->thread;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
|
2008-01-30 19:30:46 +07:00
|
|
|
if (desc_empty(&t->tls_array[idx]))
|
2005-04-17 05:20:36 +07:00
|
|
|
return idx + GDT_ENTRY_TLS_MIN;
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a given TLS descriptor:
|
|
|
|
*/
|
2008-01-30 19:30:46 +07:00
|
|
|
int do_set_thread_area(struct task_struct *p, int idx,
|
|
|
|
struct user_desc __user *u_info,
|
|
|
|
int can_allocate)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-30 19:30:46 +07:00
|
|
|
struct thread_struct *t = &p->thread;
|
2005-04-17 05:20:36 +07:00
|
|
|
struct user_desc info;
|
2008-01-30 19:30:46 +07:00
|
|
|
u32 *desc;
|
|
|
|
int cpu;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (copy_from_user(&info, u_info, sizeof(info)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
if (idx == -1)
|
|
|
|
idx = info.entry_number;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* index -1 means the kernel should try to find and
|
|
|
|
* allocate an empty descriptor:
|
|
|
|
*/
|
2008-01-30 19:30:46 +07:00
|
|
|
if (idx == -1 && can_allocate) {
|
2005-04-17 05:20:36 +07:00
|
|
|
idx = get_free_idx();
|
|
|
|
if (idx < 0)
|
|
|
|
return idx;
|
|
|
|
if (put_user(idx, &u_info->entry_number))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We must not get preempted while modifying the TLS.
|
|
|
|
*/
|
|
|
|
cpu = get_cpu();
|
|
|
|
|
|
|
|
if (LDT_empty(&info)) {
|
2008-01-30 19:30:46 +07:00
|
|
|
desc[0] = 0;
|
|
|
|
desc[1] = 0;
|
2008-01-30 19:31:13 +07:00
|
|
|
} else
|
|
|
|
fill_ldt((struct desc_struct *)desc, &info);
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
if (t == ¤t->thread)
|
|
|
|
load_TLS(t, cpu);
|
|
|
|
|
|
|
|
put_cpu();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
|
2008-01-30 19:30:45 +07:00
|
|
|
{
|
2008-01-30 19:30:46 +07:00
|
|
|
return do_set_thread_area(current, -1, u_info, 1);
|
2008-01-30 19:30:45 +07:00
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the current Thread-Local Storage area:
|
|
|
|
*/
|
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
#define GET_LIMIT(desc) (((desc)[0] & 0x0ffff) | ((desc)[1] & 0xf0000))
|
|
|
|
#define GET_32BIT(desc) (((desc)[1] >> 22) & 1)
|
|
|
|
#define GET_CONTENTS(desc) (((desc)[1] >> 10) & 3)
|
|
|
|
#define GET_WRITABLE(desc) (((desc)[1] >> 9) & 1)
|
|
|
|
#define GET_LIMIT_PAGES(desc) (((desc)[1] >> 23) & 1)
|
|
|
|
#define GET_PRESENT(desc) (((desc)[1] >> 15) & 1)
|
|
|
|
#define GET_USEABLE(desc) (((desc)[1] >> 20) & 1)
|
|
|
|
#define GET_LONGMODE(desc) (((desc)[1] >> 21) & 1)
|
|
|
|
|
|
|
|
int do_get_thread_area(struct task_struct *p, int idx,
|
|
|
|
struct user_desc __user *u_info)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-30 19:30:46 +07:00
|
|
|
struct thread_struct *t = &p->thread;
|
2005-04-17 05:20:36 +07:00
|
|
|
struct user_desc info;
|
2008-01-30 19:30:46 +07:00
|
|
|
u32 *desc;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
if (idx == -1 && get_user(idx, &u_info->entry_number))
|
2005-04-17 05:20:36 +07:00
|
|
|
return -EFAULT;
|
|
|
|
if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
memset(&info, 0, sizeof(struct user_desc));
|
|
|
|
info.entry_number = idx;
|
2008-01-30 19:31:14 +07:00
|
|
|
info.base_addr = get_desc_base((struct desc_struct *)desc);
|
2005-04-17 05:20:36 +07:00
|
|
|
info.limit = GET_LIMIT(desc);
|
|
|
|
info.seg_32bit = GET_32BIT(desc);
|
|
|
|
info.contents = GET_CONTENTS(desc);
|
|
|
|
info.read_exec_only = !GET_WRITABLE(desc);
|
|
|
|
info.limit_in_pages = GET_LIMIT_PAGES(desc);
|
|
|
|
info.seg_not_present = !GET_PRESENT(desc);
|
|
|
|
info.useable = GET_USEABLE(desc);
|
2008-01-30 19:30:46 +07:00
|
|
|
#ifdef CONFIG_X86_64
|
2005-04-17 05:20:36 +07:00
|
|
|
info.lm = GET_LONGMODE(desc);
|
2008-01-30 19:30:46 +07:00
|
|
|
#endif
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (copy_to_user(u_info, &info, sizeof(info)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 19:30:46 +07:00
|
|
|
asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-01-30 19:30:46 +07:00
|
|
|
return do_get_thread_area(current, -1, u_info);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|