Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | /* * Copyright (c) 2000-2009 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ */ /* * Mach Operating System * Copyright (c) 1991 Carnegie Mellon University * All Rights Reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie Mellon * the rights to redistribute these changes. */ /* */ /* * User LDT management. * Each task may have its own LDT. */ #include <kern/kalloc.h> #include <kern/thread.h> #include <kern/misc_protos.h> #include <vm/vm_kern.h> #include <i386/machdep_call.h> #include <i386/user_ldt.h> #include <i386/mp.h> #include <i386/machine_routines.h> #include <i386/proc_reg.h> #include <i386/mp_desc.h> #include <i386/seg.h> #include <i386/thread.h> #include <sys/errno.h> static void user_ldt_set_action(void *); static int i386_set_ldt_impl(uint32_t *retval, uint64_t start_sel, uint64_t descs, uint64_t num_sels); static int i386_get_ldt_impl(uint32_t *retval, uint64_t start_sel, uint64_t descs, uint64_t num_sels); #define USER_LDT_SIZE(descriptors) \ (sizeof(struct user_ldt) + (descriptors * sizeof(struct real_descriptor))) /* * Add the descriptors to the LDT, starting with * the descriptor for 'first_selector'. */ static int i386_set_ldt_impl( uint32_t *retval, uint64_t start_sel, uint64_t descs, /* out */ uint64_t num_sels) { user_ldt_t new_ldt, old_ldt; struct real_descriptor *dp; unsigned int i; unsigned int min_selector = LDTSZ_MIN; /* do not allow the system selectors to be changed */ task_t task = current_task(); unsigned int ldt_count; kern_return_t err; if (start_sel != LDT_AUTO_ALLOC && (start_sel != 0 || num_sels != 0) && (start_sel < min_selector || start_sel >= LDTSZ || num_sels > LDTSZ)) { return EINVAL; } if (start_sel != LDT_AUTO_ALLOC && start_sel + num_sels > LDTSZ) { return EINVAL; } task_lock(task); old_ldt = task->i386_ldt; if (start_sel == LDT_AUTO_ALLOC) { if (old_ldt) { unsigned int null_count; struct real_descriptor null_ldt; bzero(&null_ldt, sizeof(null_ldt)); /* * Look for null selectors among the already-allocated * entries. */ null_count = 0; i = 0; while (i < old_ldt->count) { if (!memcmp(&old_ldt->ldt[i++], &null_ldt, sizeof(null_ldt))) { null_count++; if (null_count == num_sels) { break; /* break out of while loop */ } } else { null_count = 0; } } /* * If we broke out of the while loop, i points to the selector * after num_sels null selectors. Otherwise it points to the end * of the old LDTs, and null_count is the number of null selectors * at the end. * * Either way, there are null_count null selectors just prior to * the i-indexed selector, and either null_count >= num_sels, * or we're at the end, so we can extend. */ start_sel = old_ldt->start + i - null_count; } else { start_sel = LDTSZ_MIN; } if (start_sel + num_sels > LDTSZ) { task_unlock(task); return ENOMEM; } } if (start_sel == 0 && num_sels == 0) { new_ldt = NULL; } else { /* * Allocate new LDT */ unsigned int begin_sel = (unsigned int)start_sel; unsigned int end_sel = (unsigned int)begin_sel + (unsigned int)num_sels; if (old_ldt != NULL) { if (old_ldt->start < begin_sel) { begin_sel = old_ldt->start; } if (old_ldt->start + old_ldt->count > end_sel) { end_sel = old_ldt->start + old_ldt->count; } } ldt_count = end_sel - begin_sel; /* XXX allocation under task lock */ new_ldt = (user_ldt_t)kalloc_data(USER_LDT_SIZE(ldt_count), Z_WAITOK); if (new_ldt == NULL) { task_unlock(task); return ENOMEM; } new_ldt->start = begin_sel; new_ldt->count = ldt_count; /* * Have new LDT. If there was a an old ldt, copy descriptors * from old to new. */ if (old_ldt) { bcopy(&old_ldt->ldt[0], &new_ldt->ldt[old_ldt->start - begin_sel], old_ldt->count * sizeof(struct real_descriptor)); /* * If the old and new LDTs are non-overlapping, fill the * center in with null selectors. */ if (old_ldt->start + old_ldt->count < start_sel) { bzero(&new_ldt->ldt[old_ldt->count], (start_sel - (old_ldt->start + old_ldt->count)) * sizeof(struct real_descriptor)); } else if (old_ldt->start > start_sel + num_sels) { bzero(&new_ldt->ldt[num_sels], (old_ldt->start - (start_sel + num_sels)) * sizeof(struct real_descriptor)); } } /* * Install new descriptors. */ if (descs != 0) { /* XXX copyin under task lock */ err = copyin(descs, (char *)&new_ldt->ldt[start_sel - begin_sel], num_sels * sizeof(struct real_descriptor)); if (err != 0) { task_unlock(task); user_ldt_free(new_ldt); return err; } } else { bzero(&new_ldt->ldt[start_sel - begin_sel], num_sels * sizeof(struct real_descriptor)); } /* * Validate descriptors. * Only allow descriptors with user privileges. */ for (i = 0, dp = (struct real_descriptor *) &new_ldt->ldt[start_sel - begin_sel]; i < num_sels; i++, dp++) { switch (dp->access & ~ACC_A) { case 0: case ACC_P: /* valid empty descriptor, clear Present preemptively */ dp->access &= (~ACC_P & 0xff); break; case ACC_P | ACC_PL_U | ACC_DATA: case ACC_P | ACC_PL_U | ACC_DATA_W: case ACC_P | ACC_PL_U | ACC_DATA_E: case ACC_P | ACC_PL_U | ACC_DATA_EW: case ACC_P | ACC_PL_U | ACC_CODE: case ACC_P | ACC_PL_U | ACC_CODE_R: case ACC_P | ACC_PL_U | ACC_CODE_C: case ACC_P | ACC_PL_U | ACC_CODE_CR: break; default: task_unlock(task); user_ldt_free(new_ldt); return EACCES; } /* Reject attempts to create segments with 64-bit granules */ /* Note this restriction is still correct, even when * executing as a 64-bit process (we want to maintain a single * 64-bit selector (located in the GDT)). */ if (dp->granularity & SZ_64) { task_unlock(task); user_ldt_free(new_ldt); return EACCES; } } } task->i386_ldt = new_ldt; /* new LDT for task */ /* * Switch to new LDT. We need to do this on all CPUs, since * another thread in this same task may be currently running, * and we need to make sure the new LDT is in place * throughout the task before returning to the user. */ mp_broadcast(user_ldt_set_action, task); task_unlock(task); /* free old LDT. We can't do this until after we've * rendezvoused with all CPUs, in case another thread * in this task was in the process of context switching. */ if (old_ldt) { user_ldt_free(old_ldt); } *retval = (uint32_t)start_sel; return 0; } static int i386_get_ldt_impl( uint32_t *retval, uint64_t start_sel, uint64_t descs, /* out */ uint64_t num_sels) { user_ldt_t user_ldt; task_t task = current_task(); unsigned int ldt_count; kern_return_t err; if (start_sel >= LDTSZ || num_sels > LDTSZ) { return EINVAL; } if (start_sel + num_sels > LDTSZ) { return EINVAL; } if (descs == 0) { return EINVAL; } task_lock(task); user_ldt = task->i386_ldt; err = 0; /* * copy out the descriptors */ if (user_ldt != 0) { ldt_count = user_ldt->start + user_ldt->count; } else { ldt_count = LDTSZ_MIN; } if (start_sel < ldt_count) { unsigned int copy_sels = (unsigned int)num_sels; if (start_sel + num_sels > ldt_count) { copy_sels = ldt_count - (unsigned int)start_sel; } err = copyout((char *)(current_ldt() + start_sel), descs, copy_sels * sizeof(struct real_descriptor)); } task_unlock(task); *retval = ldt_count; return err; } void user_ldt_free( user_ldt_t user_ldt) { kfree_data(user_ldt, USER_LDT_SIZE(user_ldt->count)); } user_ldt_t user_ldt_copy( user_ldt_t user_ldt) { if (user_ldt != NULL) { size_t size = USER_LDT_SIZE(user_ldt->count); user_ldt_t new_ldt = (user_ldt_t)kalloc_data(size, Z_WAITOK); if (new_ldt != NULL) { bcopy(user_ldt, new_ldt, size); } return new_ldt; } return 0; } void user_ldt_set_action( void *arg) { task_t arg_task = (task_t)arg; if (arg_task == current_task()) { user_ldt_set(current_thread()); } } /* * Set the LDT for the given thread on the current CPU. Should be invoked * with interrupts disabled. */ void user_ldt_set( thread_t thread) { task_t task = get_threadtask(thread); user_ldt_t user_ldt; user_ldt = task->i386_ldt; if (user_ldt != 0) { struct real_descriptor *ldtp = current_ldt(); if (user_ldt->start > LDTSZ_MIN) { bzero(&ldtp[LDTSZ_MIN], sizeof(struct real_descriptor) * (user_ldt->start - LDTSZ_MIN)); } bcopy(user_ldt->ldt, &ldtp[user_ldt->start], sizeof(struct real_descriptor) * (user_ldt->count)); gdt_desc_p(USER_LDT)->limit_low = (uint16_t)((sizeof(struct real_descriptor) * (user_ldt->start + user_ldt->count)) - 1); ml_cpu_set_ldt(USER_LDT); } else { ml_cpu_set_ldt(KERNEL_LDT); } } /* For 32-bit processes, called via machdep_syscall() */ int i386_set_ldt( uint32_t *retval, uint32_t start_sel, uint32_t descs, /* out */ uint32_t num_sels) { return i386_set_ldt_impl(retval, (uint64_t)start_sel, (uint64_t)descs, (uint64_t)num_sels); } /* For 64-bit processes, called via machdep_syscall64() */ int i386_set_ldt64( uint32_t *retval, uint64_t start_sel, uint64_t descs, /* out */ uint64_t num_sels) { return i386_set_ldt_impl(retval, start_sel, descs, num_sels); } /* For 32-bit processes, called via machdep_syscall() */ int i386_get_ldt( uint32_t *retval, uint32_t start_sel, uint32_t descs, /* out */ uint32_t num_sels) { return i386_get_ldt_impl(retval, (uint64_t)start_sel, (uint64_t)descs, (uint64_t)num_sels); } /* For 64-bit processes, called via machdep_syscall64() */ int i386_get_ldt64( uint32_t *retval, uint64_t start_sel, uint64_t descs, /* out */ uint64_t num_sels) { return i386_get_ldt_impl(retval, start_sel, descs, num_sels); } |