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 | |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame^] | 27 | static void set_tls_desc(struct task_struct *p, int idx, |
| 28 | const struct user_desc *info) |
| 29 | { |
| 30 | struct thread_struct *t = &p->thread; |
| 31 | struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN]; |
| 32 | int cpu; |
| 33 | |
| 34 | /* |
| 35 | * We must not get preempted while modifying the TLS. |
| 36 | */ |
| 37 | cpu = get_cpu(); |
| 38 | |
| 39 | if (LDT_empty(info)) |
| 40 | desc->a = desc->b = 0; |
| 41 | else |
| 42 | fill_ldt(desc, info); |
| 43 | |
| 44 | if (t == ¤t->thread) |
| 45 | load_TLS(t, cpu); |
| 46 | |
| 47 | put_cpu(); |
| 48 | } |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Set a given TLS descriptor: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | */ |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 53 | int do_set_thread_area(struct task_struct *p, int idx, |
| 54 | struct user_desc __user *u_info, |
| 55 | int can_allocate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | struct user_desc info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | if (copy_from_user(&info, u_info, sizeof(info))) |
| 60 | return -EFAULT; |
| 61 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 62 | if (idx == -1) |
| 63 | idx = info.entry_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * index -1 means the kernel should try to find and |
| 67 | * allocate an empty descriptor: |
| 68 | */ |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 69 | if (idx == -1 && can_allocate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | idx = get_free_idx(); |
| 71 | if (idx < 0) |
| 72 | return idx; |
| 73 | if (put_user(idx, &u_info->entry_number)) |
| 74 | return -EFAULT; |
| 75 | } |
| 76 | |
| 77 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 78 | return -EINVAL; |
| 79 | |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame^] | 80 | set_tls_desc(p, idx, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 85 | asmlinkage int sys_set_thread_area(struct user_desc __user *u_info) |
Roland McGrath | 13abd0e | 2008-01-30 13:30:45 +0100 | [diff] [blame] | 86 | { |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 87 | return do_set_thread_area(current, -1, u_info, 1); |
Roland McGrath | 13abd0e | 2008-01-30 13:30:45 +0100 | [diff] [blame] | 88 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | /* |
| 92 | * Get the current Thread-Local Storage area: |
| 93 | */ |
| 94 | |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame^] | 95 | static void fill_user_desc(struct user_desc *info, int idx, |
| 96 | const struct desc_struct *desc) |
| 97 | |
| 98 | { |
| 99 | memset(info, 0, sizeof(*info)); |
| 100 | info->entry_number = idx; |
| 101 | info->base_addr = get_desc_base(desc); |
| 102 | info->limit = get_desc_limit(desc); |
| 103 | info->seg_32bit = desc->d; |
| 104 | info->contents = desc->type >> 2; |
| 105 | info->read_exec_only = !(desc->type & 2); |
| 106 | info->limit_in_pages = desc->g; |
| 107 | info->seg_not_present = !desc->p; |
| 108 | info->useable = desc->avl; |
| 109 | #ifdef CONFIG_X86_64 |
| 110 | info->lm = desc->l; |
| 111 | #endif |
| 112 | } |
Roland McGrath | 13abd0e | 2008-01-30 13:30:45 +0100 | [diff] [blame] | 113 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 114 | int do_get_thread_area(struct task_struct *p, int idx, |
| 115 | struct user_desc __user *u_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | struct user_desc info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 119 | if (idx == -1 && get_user(idx, &u_info->entry_number)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -EFAULT; |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame^] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 123 | return -EINVAL; |
| 124 | |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame^] | 125 | fill_user_desc(&info, idx, |
| 126 | &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]); |
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 | } |