Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it> |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 6 | #include "linux/kernel.h" |
| 7 | #include "linux/sched.h" |
| 8 | #include "linux/slab.h" |
| 9 | #include "linux/types.h" |
| 10 | #include "asm/uaccess.h" |
| 11 | #include "asm/ptrace.h" |
| 12 | #include "asm/segment.h" |
| 13 | #include "asm/smp.h" |
| 14 | #include "asm/desc.h" |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 15 | #include "kern.h" |
| 16 | #include "kern_util.h" |
| 17 | #include "mode_kern.h" |
| 18 | #include "os.h" |
| 19 | #include "mode.h" |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 20 | #include "skas.h" |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 21 | |
Jeff Dike | 65a58ab | 2007-05-06 14:51:20 -0700 | [diff] [blame] | 22 | /* |
| 23 | * If needed we can detect when it's uninitialized. |
| 24 | * |
| 25 | * These are initialized in an initcall and unchanged thereafter. |
| 26 | */ |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 27 | static int host_supports_tls = -1; |
Jeff Dike | 65a58ab | 2007-05-06 14:51:20 -0700 | [diff] [blame] | 28 | int host_gdt_entry_tls_min; |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 29 | |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame^] | 30 | int do_set_thread_area(struct user_desc *info) |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 31 | { |
| 32 | int ret; |
| 33 | u32 cpu; |
| 34 | |
| 35 | cpu = get_cpu(); |
| 36 | ret = os_set_thread_area(info, userspace_pid[cpu]); |
| 37 | put_cpu(); |
| 38 | return ret; |
| 39 | } |
| 40 | |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame^] | 41 | int do_get_thread_area(struct user_desc *info) |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 42 | { |
| 43 | int ret; |
| 44 | u32 cpu; |
| 45 | |
| 46 | cpu = get_cpu(); |
| 47 | ret = os_get_thread_area(info, userspace_pid[cpu]); |
| 48 | put_cpu(); |
| 49 | return ret; |
| 50 | } |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * sys_get_thread_area: get a yet unused TLS descriptor index. |
| 54 | * XXX: Consider leaving one free slot for glibc usage at first place. This must |
| 55 | * be done here (and by changing GDT_ENTRY_TLS_* macros) and nowhere else. |
| 56 | * |
| 57 | * Also, this must be tested when compiling in SKAS mode with dinamic linking |
| 58 | * and running against NPTL. |
| 59 | */ |
| 60 | static int get_free_idx(struct task_struct* task) |
| 61 | { |
| 62 | struct thread_struct *t = &task->thread; |
| 63 | int idx; |
| 64 | |
| 65 | if (!t->arch.tls_array) |
| 66 | return GDT_ENTRY_TLS_MIN; |
| 67 | |
| 68 | for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++) |
| 69 | if (!t->arch.tls_array[idx].present) |
| 70 | return idx + GDT_ENTRY_TLS_MIN; |
| 71 | return -ESRCH; |
| 72 | } |
| 73 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 74 | static inline void clear_user_desc(struct user_desc* info) |
| 75 | { |
| 76 | /* Postcondition: LDT_empty(info) returns true. */ |
| 77 | memset(info, 0, sizeof(*info)); |
| 78 | |
| 79 | /* Check the LDT_empty or the i386 sys_get_thread_area code - we obtain |
| 80 | * indeed an empty user_desc. |
| 81 | */ |
| 82 | info->read_exec_only = 1; |
| 83 | info->seg_not_present = 1; |
| 84 | } |
| 85 | |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 86 | #define O_FORCE 1 |
| 87 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 88 | static int load_TLS(int flags, struct task_struct *to) |
| 89 | { |
| 90 | int ret = 0; |
| 91 | int idx; |
| 92 | |
| 93 | for (idx = GDT_ENTRY_TLS_MIN; idx < GDT_ENTRY_TLS_MAX; idx++) { |
| 94 | struct uml_tls_struct* curr = &to->thread.arch.tls_array[idx - GDT_ENTRY_TLS_MIN]; |
| 95 | |
| 96 | /* Actually, now if it wasn't flushed it gets cleared and |
| 97 | * flushed to the host, which will clear it.*/ |
| 98 | if (!curr->present) { |
| 99 | if (!curr->flushed) { |
| 100 | clear_user_desc(&curr->tls); |
| 101 | curr->tls.entry_number = idx; |
| 102 | } else { |
| 103 | WARN_ON(!LDT_empty(&curr->tls)); |
| 104 | continue; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (!(flags & O_FORCE) && curr->flushed) |
| 109 | continue; |
| 110 | |
| 111 | ret = do_set_thread_area(&curr->tls); |
| 112 | if (ret) |
| 113 | goto out; |
| 114 | |
| 115 | curr->flushed = 1; |
| 116 | } |
| 117 | out: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* Verify if we need to do a flush for the new process, i.e. if there are any |
| 122 | * present desc's, only if they haven't been flushed. |
| 123 | */ |
| 124 | static inline int needs_TLS_update(struct task_struct *task) |
| 125 | { |
| 126 | int i; |
| 127 | int ret = 0; |
| 128 | |
| 129 | for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) { |
| 130 | struct uml_tls_struct* curr = &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN]; |
| 131 | |
| 132 | /* Can't test curr->present, we may need to clear a descriptor |
| 133 | * which had a value. */ |
| 134 | if (curr->flushed) |
| 135 | continue; |
| 136 | ret = 1; |
| 137 | break; |
| 138 | } |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | /* On a newly forked process, the TLS descriptors haven't yet been flushed. So |
| 143 | * we mark them as such and the first switch_to will do the job. |
| 144 | */ |
| 145 | void clear_flushed_tls(struct task_struct *task) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) { |
| 150 | struct uml_tls_struct* curr = &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN]; |
| 151 | |
| 152 | /* Still correct to do this, if it wasn't present on the host it |
| 153 | * will remain as flushed as it was. */ |
| 154 | if (!curr->present) |
| 155 | continue; |
| 156 | |
| 157 | curr->flushed = 0; |
| 158 | } |
| 159 | } |
| 160 | |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 161 | /* In SKAS0 mode, currently, multiple guest threads sharing the same ->mm have a |
| 162 | * common host process. So this is needed in SKAS0 too. |
| 163 | * |
| 164 | * However, if each thread had a different host process (and this was discussed |
| 165 | * for SMP support) this won't be needed. |
| 166 | * |
| 167 | * And this will not need be used when (and if) we'll add support to the host |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 168 | * SKAS patch. */ |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 169 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 170 | int arch_switch_tls_skas(struct task_struct *from, struct task_struct *to) |
| 171 | { |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 172 | if (!host_supports_tls) |
| 173 | return 0; |
| 174 | |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 175 | /* We have no need whatsoever to switch TLS for kernel threads; beyond |
| 176 | * that, that would also result in us calling os_set_thread_area with |
| 177 | * userspace_pid[cpu] == 0, which gives an error. */ |
| 178 | if (likely(to->mm)) |
| 179 | return load_TLS(O_FORCE, to); |
| 180 | |
| 181 | return 0; |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 184 | static int set_tls_entry(struct task_struct* task, struct user_desc *info, |
| 185 | int idx, int flushed) |
| 186 | { |
| 187 | struct thread_struct *t = &task->thread; |
| 188 | |
| 189 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls = *info; |
| 193 | t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present = 1; |
| 194 | t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed = flushed; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | int arch_copy_tls(struct task_struct *new) |
| 200 | { |
| 201 | struct user_desc info; |
| 202 | int idx, ret = -EFAULT; |
| 203 | |
| 204 | if (copy_from_user(&info, |
| 205 | (void __user *) UPT_ESI(&new->thread.regs.regs), |
| 206 | sizeof(info))) |
| 207 | goto out; |
| 208 | |
| 209 | ret = -EINVAL; |
| 210 | if (LDT_empty(&info)) |
| 211 | goto out; |
| 212 | |
| 213 | idx = info.entry_number; |
| 214 | |
| 215 | ret = set_tls_entry(new, &info, idx, 0); |
| 216 | out: |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* XXX: use do_get_thread_area to read the host value? I'm not at all sure! */ |
| 221 | static int get_tls_entry(struct task_struct* task, struct user_desc *info, int idx) |
| 222 | { |
| 223 | struct thread_struct *t = &task->thread; |
| 224 | |
| 225 | if (!t->arch.tls_array) |
| 226 | goto clear; |
| 227 | |
| 228 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present) |
| 232 | goto clear; |
| 233 | |
| 234 | *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls; |
| 235 | |
| 236 | out: |
| 237 | /* Temporary debugging check, to make sure that things have been |
| 238 | * flushed. This could be triggered if load_TLS() failed. |
| 239 | */ |
| 240 | if (unlikely(task == current && !t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed)) { |
| 241 | printk(KERN_ERR "get_tls_entry: task with pid %d got here " |
| 242 | "without flushed TLS.", current->pid); |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | clear: |
| 247 | /* When the TLS entry has not been set, the values read to user in the |
| 248 | * tls_array are 0 (because it's cleared at boot, see |
| 249 | * arch/i386/kernel/head.S:cpu_gdt_table). Emulate that. |
| 250 | */ |
| 251 | clear_user_desc(info); |
| 252 | info->entry_number = idx; |
| 253 | goto out; |
| 254 | } |
| 255 | |
| 256 | asmlinkage int sys_set_thread_area(struct user_desc __user *user_desc) |
| 257 | { |
| 258 | struct user_desc info; |
| 259 | int idx, ret; |
| 260 | |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 261 | if (!host_supports_tls) |
| 262 | return -ENOSYS; |
| 263 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 264 | if (copy_from_user(&info, user_desc, sizeof(info))) |
| 265 | return -EFAULT; |
| 266 | |
| 267 | idx = info.entry_number; |
| 268 | |
| 269 | if (idx == -1) { |
| 270 | idx = get_free_idx(current); |
| 271 | if (idx < 0) |
| 272 | return idx; |
| 273 | info.entry_number = idx; |
| 274 | /* Tell the user which slot we chose for him.*/ |
| 275 | if (put_user(idx, &user_desc->entry_number)) |
| 276 | return -EFAULT; |
| 277 | } |
| 278 | |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame^] | 279 | ret = do_set_thread_area(&info); |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 280 | if (ret) |
| 281 | return ret; |
| 282 | return set_tls_entry(current, &info, idx, 1); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Perform set_thread_area on behalf of the traced child. |
| 287 | * Note: error handling is not done on the deferred load, and this differ from |
| 288 | * i386. However the only possible error are caused by bugs. |
| 289 | */ |
| 290 | int ptrace_set_thread_area(struct task_struct *child, int idx, |
| 291 | struct user_desc __user *user_desc) |
| 292 | { |
| 293 | struct user_desc info; |
| 294 | |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 295 | if (!host_supports_tls) |
| 296 | return -EIO; |
| 297 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 298 | if (copy_from_user(&info, user_desc, sizeof(info))) |
| 299 | return -EFAULT; |
| 300 | |
| 301 | return set_tls_entry(child, &info, idx, 0); |
| 302 | } |
| 303 | |
| 304 | asmlinkage int sys_get_thread_area(struct user_desc __user *user_desc) |
| 305 | { |
| 306 | struct user_desc info; |
| 307 | int idx, ret; |
| 308 | |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 309 | if (!host_supports_tls) |
| 310 | return -ENOSYS; |
| 311 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 312 | if (get_user(idx, &user_desc->entry_number)) |
| 313 | return -EFAULT; |
| 314 | |
| 315 | ret = get_tls_entry(current, &info, idx); |
| 316 | if (ret < 0) |
| 317 | goto out; |
| 318 | |
| 319 | if (copy_to_user(user_desc, &info, sizeof(info))) |
| 320 | ret = -EFAULT; |
| 321 | |
| 322 | out: |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Perform get_thread_area on behalf of the traced child. |
| 328 | */ |
| 329 | int ptrace_get_thread_area(struct task_struct *child, int idx, |
| 330 | struct user_desc __user *user_desc) |
| 331 | { |
| 332 | struct user_desc info; |
| 333 | int ret; |
| 334 | |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 335 | if (!host_supports_tls) |
| 336 | return -EIO; |
| 337 | |
Paolo 'Blaisorblade' Giarrusso | aa6758d | 2006-03-31 02:30:22 -0800 | [diff] [blame] | 338 | ret = get_tls_entry(child, &info, idx); |
| 339 | if (ret < 0) |
| 340 | goto out; |
| 341 | |
| 342 | if (copy_to_user(user_desc, &info, sizeof(info))) |
| 343 | ret = -EFAULT; |
| 344 | out: |
| 345 | return ret; |
| 346 | } |
Paolo 'Blaisorblade' Giarrusso | 54d8d3b | 2006-03-31 02:30:24 -0800 | [diff] [blame] | 347 | |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 348 | |
| 349 | /* XXX: This part is probably common to i386 and x86-64. Don't create a common |
| 350 | * file for now, do that when implementing x86-64 support.*/ |
Jeff Dike | 65a58ab | 2007-05-06 14:51:20 -0700 | [diff] [blame] | 351 | static int __init __setup_host_supports_tls(void) |
| 352 | { |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 353 | check_host_supports_tls(&host_supports_tls, &host_gdt_entry_tls_min); |
| 354 | if (host_supports_tls) { |
| 355 | printk(KERN_INFO "Host TLS support detected\n"); |
| 356 | printk(KERN_INFO "Detected host type: "); |
| 357 | switch (host_gdt_entry_tls_min) { |
| 358 | case GDT_ENTRY_TLS_MIN_I386: |
| 359 | printk("i386\n"); |
| 360 | break; |
| 361 | case GDT_ENTRY_TLS_MIN_X86_64: |
| 362 | printk("x86_64\n"); |
| 363 | break; |
| 364 | } |
| 365 | } else |
| 366 | printk(KERN_ERR " Host TLS support NOT detected! " |
| 367 | "TLS support inside UML will not work\n"); |
Jeff Dike | a5d2f46 | 2006-04-10 22:53:26 -0700 | [diff] [blame] | 368 | return 0; |
Paolo 'Blaisorblade' Giarrusso | 3feb885 | 2006-03-31 02:30:25 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | __initcall(__setup_host_supports_tls); |