2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
61 * User LDT management.
62 * Each task may have its own LDT.
65 #include <kern/kalloc.h>
66 #include <kern/thread.h>
67 #include <kern/misc_protos.h>
69 #include <vm/vm_kern.h>
71 #include <i386/machdep_call.h>
72 #include <i386/user_ldt.h>
74 #include <i386/machine_routines.h>
75 #include <i386/proc_reg.h>
76 #include <i386/mp_desc.h>
78 #include <i386/thread.h>
80 #include <IOKit/IOBSD.h> /* for IOTaskHasEntitlement */
81 #include <sys/csr.h> /* for csr_check */
83 #include <sys/errno.h>
85 static void user_ldt_set_action(void *);
86 static int i386_set_ldt_impl(uint32_t *retval
, uint64_t start_sel
, uint64_t descs
,
88 static int i386_get_ldt_impl(uint32_t *retval
, uint64_t start_sel
, uint64_t descs
,
91 #define LDT_IN_64BITPROC_ENTITLEMENT "com.apple.security.ldt-in-64bit-process"
94 * Add the descriptors to the LDT, starting with
95 * the descriptor for 'first_selector'.
102 uint64_t descs
, /* out */
105 user_ldt_t new_ldt
, old_ldt
;
106 struct real_descriptor
*dp
;
108 unsigned int min_selector
= LDTSZ_MIN
; /* do not allow the system selectors to be changed */
109 task_t task
= current_task();
110 unsigned int ldt_count
;
113 if (start_sel
!= LDT_AUTO_ALLOC
114 && (start_sel
!= 0 || num_sels
!= 0)
115 && (start_sel
< min_selector
|| start_sel
>= LDTSZ
|| num_sels
> LDTSZ
)) {
118 if (start_sel
!= LDT_AUTO_ALLOC
&& start_sel
+ num_sels
> LDTSZ
) {
124 old_ldt
= task
->i386_ldt
;
126 if (start_sel
== LDT_AUTO_ALLOC
) {
128 unsigned int null_count
;
129 struct real_descriptor null_ldt
;
131 bzero(&null_ldt
, sizeof(null_ldt
));
134 * Look for null selectors among the already-allocated
139 while (i
< old_ldt
->count
) {
140 if (!memcmp(&old_ldt
->ldt
[i
++], &null_ldt
, sizeof(null_ldt
))) {
142 if (null_count
== num_sels
) {
143 break; /* break out of while loop */
151 * If we broke out of the while loop, i points to the selector
152 * after num_sels null selectors. Otherwise it points to the end
153 * of the old LDTs, and null_count is the number of null selectors
156 * Either way, there are null_count null selectors just prior to
157 * the i-indexed selector, and either null_count >= num_sels,
158 * or we're at the end, so we can extend.
160 start_sel
= old_ldt
->start
+ i
- null_count
;
162 start_sel
= LDTSZ_MIN
;
165 if (start_sel
+ num_sels
> LDTSZ
) {
171 if (start_sel
== 0 && num_sels
== 0) {
178 unsigned int begin_sel
= (unsigned int)start_sel
;
179 unsigned int end_sel
= (unsigned int)begin_sel
+
180 (unsigned int)num_sels
;
182 if (old_ldt
!= NULL
) {
183 if (old_ldt
->start
< begin_sel
) {
184 begin_sel
= old_ldt
->start
;
186 if (old_ldt
->start
+ old_ldt
->count
> end_sel
) {
187 end_sel
= old_ldt
->start
+ old_ldt
->count
;
191 ldt_count
= end_sel
- begin_sel
;
192 /* XXX allocation under task lock */
193 new_ldt
= (user_ldt_t
)kalloc(sizeof(struct user_ldt
) + (ldt_count
* sizeof(struct real_descriptor
)));
194 if (new_ldt
== NULL
) {
199 new_ldt
->start
= begin_sel
;
200 new_ldt
->count
= ldt_count
;
203 * Have new LDT. If there was a an old ldt, copy descriptors
207 bcopy(&old_ldt
->ldt
[0],
208 &new_ldt
->ldt
[old_ldt
->start
- begin_sel
],
209 old_ldt
->count
* sizeof(struct real_descriptor
));
212 * If the old and new LDTs are non-overlapping, fill the
213 * center in with null selectors.
216 if (old_ldt
->start
+ old_ldt
->count
< start_sel
) {
217 bzero(&new_ldt
->ldt
[old_ldt
->count
],
218 (start_sel
- (old_ldt
->start
+ old_ldt
->count
)) * sizeof(struct real_descriptor
));
219 } else if (old_ldt
->start
> start_sel
+ num_sels
) {
220 bzero(&new_ldt
->ldt
[num_sels
],
221 (old_ldt
->start
- (start_sel
+ num_sels
)) * sizeof(struct real_descriptor
));
226 * Install new descriptors.
229 /* XXX copyin under task lock */
230 err
= copyin(descs
, (char *)&new_ldt
->ldt
[start_sel
- begin_sel
],
231 num_sels
* sizeof(struct real_descriptor
));
234 user_ldt_free(new_ldt
);
238 bzero(&new_ldt
->ldt
[start_sel
- begin_sel
], num_sels
* sizeof(struct real_descriptor
));
241 * Validate descriptors.
242 * Only allow descriptors with user privileges.
244 for (i
= 0, dp
= (struct real_descriptor
*) &new_ldt
->ldt
[start_sel
- begin_sel
];
247 switch (dp
->access
& ~ACC_A
) {
250 /* valid empty descriptor, clear Present preemptively */
251 dp
->access
&= (~ACC_P
& 0xff);
253 case ACC_P
| ACC_PL_U
| ACC_DATA
:
254 case ACC_P
| ACC_PL_U
| ACC_DATA_W
:
255 case ACC_P
| ACC_PL_U
| ACC_DATA_E
:
256 case ACC_P
| ACC_PL_U
| ACC_DATA_EW
:
257 case ACC_P
| ACC_PL_U
| ACC_CODE
:
258 case ACC_P
| ACC_PL_U
| ACC_CODE_R
:
259 case ACC_P
| ACC_PL_U
| ACC_CODE_C
:
260 case ACC_P
| ACC_PL_U
| ACC_CODE_CR
:
264 user_ldt_free(new_ldt
);
267 /* Reject attempts to create segments with 64-bit granules */
268 /* Note this restriction is still correct, even when
269 * executing as a 64-bit process (we want to maintain a single
270 * 64-bit selector (located in the GDT)).
272 if (dp
->granularity
& SZ_64
) {
274 user_ldt_free(new_ldt
);
280 task
->i386_ldt
= new_ldt
; /* new LDT for task */
283 * Switch to new LDT. We need to do this on all CPUs, since
284 * another thread in this same task may be currently running,
285 * and we need to make sure the new LDT is in place
286 * throughout the task before returning to the user.
288 mp_broadcast(user_ldt_set_action
, task
);
292 /* free old LDT. We can't do this until after we've
293 * rendezvoused with all CPUs, in case another thread
294 * in this task was in the process of context switching.
297 user_ldt_free(old_ldt
);
300 *retval
= (uint32_t)start_sel
;
309 uint64_t descs
, /* out */
313 task_t task
= current_task();
314 unsigned int ldt_count
;
317 if (start_sel
>= LDTSZ
|| num_sels
> LDTSZ
) {
320 if (start_sel
+ num_sels
> LDTSZ
) {
329 user_ldt
= task
->i386_ldt
;
333 * copy out the descriptors
337 ldt_count
= user_ldt
->start
+ user_ldt
->count
;
339 ldt_count
= LDTSZ_MIN
;
343 if (start_sel
< ldt_count
) {
344 unsigned int copy_sels
= (unsigned int)num_sels
;
346 if (start_sel
+ num_sels
> ldt_count
) {
347 copy_sels
= ldt_count
- (unsigned int)start_sel
;
350 err
= copyout((char *)(current_ldt() + start_sel
),
351 descs
, copy_sels
* sizeof(struct real_descriptor
));
365 kfree(user_ldt
, sizeof(struct user_ldt
) + (user_ldt
->count
* sizeof(struct real_descriptor
)));
372 if (user_ldt
!= NULL
) {
373 size_t size
= sizeof(struct user_ldt
) + (user_ldt
->count
* sizeof(struct real_descriptor
));
374 user_ldt_t new_ldt
= (user_ldt_t
)kalloc(size
);
375 if (new_ldt
!= NULL
) {
376 bcopy(user_ldt
, new_ldt
, size
);
388 task_t arg_task
= (task_t
)arg
;
390 if (arg_task
== current_task()) {
391 user_ldt_set(current_thread());
396 * Set the LDT for the given thread on the current CPU. Should be invoked
397 * with interrupts disabled.
403 task_t task
= thread
->task
;
406 user_ldt
= task
->i386_ldt
;
409 struct real_descriptor
*ldtp
= current_ldt();
411 if (user_ldt
->start
> LDTSZ_MIN
) {
412 bzero(&ldtp
[LDTSZ_MIN
],
413 sizeof(struct real_descriptor
) * (user_ldt
->start
- LDTSZ_MIN
));
416 bcopy(user_ldt
->ldt
, &ldtp
[user_ldt
->start
],
417 sizeof(struct real_descriptor
) * (user_ldt
->count
));
419 gdt_desc_p(USER_LDT
)->limit_low
= (uint16_t)((sizeof(struct real_descriptor
) * (user_ldt
->start
+ user_ldt
->count
)) - 1);
421 ml_cpu_set_ldt(USER_LDT
);
423 ml_cpu_set_ldt(KERNEL_LDT
);
427 /* For 32-bit processes, called via machdep_syscall() */
432 uint32_t descs
, /* out */
435 return i386_set_ldt_impl(retval
, (uint64_t)start_sel
, (uint64_t)descs
,
439 /* For 64-bit processes, called via machdep_syscall64() */
444 uint64_t descs
, /* out */
447 if (csr_check(CSR_ALLOW_UNTRUSTED_KEXTS
) != 0 &&
448 !IOTaskHasEntitlement(current_task(), LDT_IN_64BITPROC_ENTITLEMENT
)) {
452 return i386_set_ldt_impl(retval
, start_sel
, descs
, num_sels
);
455 /* For 32-bit processes, called via machdep_syscall() */
460 uint32_t descs
, /* out */
463 return i386_get_ldt_impl(retval
, (uint64_t)start_sel
, (uint64_t)descs
,
467 /* For 64-bit processes, called via machdep_syscall64() */
472 uint64_t descs
, /* out */
475 if (csr_check(CSR_ALLOW_UNTRUSTED_KEXTS
) != 0 &&
476 !IOTaskHasEntitlement(current_task(), LDT_IN_64BITPROC_ENTITLEMENT
)) {
480 return i386_get_ldt_impl(retval
, start_sel
, descs
, num_sels
);