Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 = ¤t->thread; |
| 19 | int idx; |
| 20 | |
| 21 | for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++) |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 22 | if (desc_empty(&t->tls_array[idx])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | return idx + GDT_ENTRY_TLS_MIN; |
| 24 | return -ESRCH; |
| 25 | } |
| 26 | |
| 27 | /* |
| 28 | * Set a given TLS descriptor: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 30 | int do_set_thread_area(struct task_struct *p, int idx, |
| 31 | struct user_desc __user *u_info, |
| 32 | int can_allocate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 34 | struct thread_struct *t = &p->thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct user_desc info; |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 36 | u32 *desc; |
| 37 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | if (copy_from_user(&info, u_info, sizeof(info))) |
| 40 | return -EFAULT; |
| 41 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 42 | if (idx == -1) |
| 43 | idx = info.entry_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * index -1 means the kernel should try to find and |
| 47 | * allocate an empty descriptor: |
| 48 | */ |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 49 | if (idx == -1 && can_allocate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 60 | desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * We must not get preempted while modifying the TLS. |
| 64 | */ |
| 65 | cpu = get_cpu(); |
| 66 | |
| 67 | if (LDT_empty(&info)) { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 68 | desc[0] = 0; |
| 69 | desc[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } else { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 71 | desc[0] = LDT_entry_a(&info); |
| 72 | desc[1] = LDT_entry_b(&info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | if (t == ¤t->thread) |
| 75 | load_TLS(t, cpu); |
| 76 | |
| 77 | put_cpu(); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 81 | asmlinkage int sys_set_thread_area(struct user_desc __user *u_info) |
Roland McGrath | 13abd0e | 2008-01-30 13:30:45 +0100 | [diff] [blame] | 82 | { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 83 | return do_set_thread_area(current, -1, u_info, 1); |
Roland McGrath | 13abd0e | 2008-01-30 13:30:45 +0100 | [diff] [blame] | 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | |
| 87 | /* |
| 88 | * Get the current Thread-Local Storage area: |
| 89 | */ |
| 90 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 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) |
Roland McGrath | 13abd0e | 2008-01-30 13:30:45 +0100 | [diff] [blame] | 99 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 100 | int do_get_thread_area(struct task_struct *p, int idx, |
| 101 | struct user_desc __user *u_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 103 | struct thread_struct *t = &p->thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct user_desc info; |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 105 | u32 *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 107 | if (idx == -1 && get_user(idx, &u_info->entry_number)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return -EFAULT; |
| 109 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 110 | return -EINVAL; |
| 111 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 112 | desc = (u32 *) &t->tls_array[idx - GDT_ENTRY_TLS_MIN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | memset(&info, 0, sizeof(struct user_desc)); |
| 115 | info.entry_number = idx; |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 116 | info.base_addr = get_desc_base((void *)desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 124 | #ifdef CONFIG_X86_64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | info.lm = GET_LONGMODE(desc); |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 126 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | if (copy_to_user(u_info, &info, sizeof(info))) |
| 129 | return -EFAULT; |
| 130 | return 0; |
| 131 | } |
| 132 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 133 | asmlinkage int sys_get_thread_area(struct user_desc __user *u_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame^] | 135 | return do_get_thread_area(current, -1, u_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |