]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/user_ldt.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
63 * User LDT management.
64 * Each task may have its own LDT.
67 #include <kern/kalloc.h>
68 #include <kern/thread.h>
69 #include <kern/misc_protos.h>
71 #include <vm/vm_kern.h>
74 #include <i386/thread.h>
75 #include <i386/user_ldt.h>
76 #include <i386/mp_desc.h>
77 #include <i386/proc_reg.h>
78 #include <i386/machdep_call.h>
80 #include <i386/machine_routines.h>
82 #include <sys/errno.h>
84 static void user_ldt_set_action(void *);
87 * Add the descriptors to the LDT, starting with
88 * the descriptor for 'first_selector'.
95 uint32_t descs
, /* out */
98 user_ldt_t new_ldt
, old_ldt
;
99 struct real_descriptor
*dp
;
101 unsigned int min_selector
= LDTSZ_MIN
; /* do not allow the system selectors to be changed */
102 task_t task
= current_task();
103 unsigned int ldt_count
;
106 if (start_sel
!= LDT_AUTO_ALLOC
107 && (start_sel
!= 0 || num_sels
!= 0)
108 && (start_sel
< min_selector
|| start_sel
>= LDTSZ
))
110 if (start_sel
!= LDT_AUTO_ALLOC
111 && start_sel
+ num_sels
> LDTSZ
)
116 old_ldt
= task
->i386_ldt
;
118 if (start_sel
== LDT_AUTO_ALLOC
) {
120 unsigned int null_count
;
121 struct real_descriptor null_ldt
;
123 bzero(&null_ldt
, sizeof(null_ldt
));
126 * Look for null selectors among the already-allocated
131 while (i
< old_ldt
->count
)
133 if (!memcmp(&old_ldt
->ldt
[i
++], &null_ldt
, sizeof(null_ldt
))) {
135 if (null_count
== num_sels
)
136 break; /* break out of while loop */
143 * If we broke out of the while loop, i points to the selector
144 * after num_sels null selectors. Otherwise it points to the end
145 * of the old LDTs, and null_count is the number of null selectors
148 * Either way, there are null_count null selectors just prior to
149 * the i-indexed selector, and either null_count >= num_sels,
150 * or we're at the end, so we can extend.
152 start_sel
= old_ldt
->start
+ i
- null_count
;
154 start_sel
= LDTSZ_MIN
;
157 if (start_sel
+ num_sels
> LDTSZ
) {
163 if (start_sel
== 0 && num_sels
== 0) {
170 unsigned int begin_sel
= start_sel
;
171 unsigned int end_sel
= begin_sel
+ num_sels
;
173 if (old_ldt
!= NULL
) {
174 if (old_ldt
->start
< begin_sel
)
175 begin_sel
= old_ldt
->start
;
176 if (old_ldt
->start
+ old_ldt
->count
> end_sel
)
177 end_sel
= old_ldt
->start
+ old_ldt
->count
;
180 ldt_count
= end_sel
- begin_sel
;
182 new_ldt
= (user_ldt_t
)kalloc(sizeof(struct user_ldt
) + (ldt_count
* sizeof(struct real_descriptor
)));
183 if (new_ldt
== NULL
) {
188 new_ldt
->start
= begin_sel
;
189 new_ldt
->count
= ldt_count
;
192 * Have new LDT. If there was a an old ldt, copy descriptors
196 bcopy(&old_ldt
->ldt
[0],
197 &new_ldt
->ldt
[old_ldt
->start
- begin_sel
],
198 old_ldt
->count
* sizeof(struct real_descriptor
));
201 * If the old and new LDTs are non-overlapping, fill the
202 * center in with null selectors.
205 if (old_ldt
->start
+ old_ldt
->count
< start_sel
)
206 bzero(&new_ldt
->ldt
[old_ldt
->count
],
207 (start_sel
- (old_ldt
->start
+ old_ldt
->count
)) * sizeof(struct real_descriptor
));
208 else if (old_ldt
->start
> start_sel
+ num_sels
)
209 bzero(&new_ldt
->ldt
[num_sels
],
210 (old_ldt
->start
- (start_sel
+ num_sels
)) * sizeof(struct real_descriptor
));
214 * Install new descriptors.
217 err
= copyin(descs
, (char *)&new_ldt
->ldt
[start_sel
- begin_sel
],
218 num_sels
* sizeof(struct real_descriptor
));
222 user_ldt_free(new_ldt
);
226 bzero(&new_ldt
->ldt
[start_sel
- begin_sel
], num_sels
* sizeof(struct real_descriptor
));
230 * Validate descriptors.
231 * Only allow descriptors with user priviledges.
233 for (i
= 0, dp
= (struct real_descriptor
*) &new_ldt
->ldt
[start_sel
- begin_sel
];
237 switch (dp
->access
& ~ACC_A
) {
240 /* valid empty descriptor */
242 case ACC_P
| ACC_PL_U
| ACC_DATA
:
243 case ACC_P
| ACC_PL_U
| ACC_DATA_W
:
244 case ACC_P
| ACC_PL_U
| ACC_DATA_E
:
245 case ACC_P
| ACC_PL_U
| ACC_DATA_EW
:
246 case ACC_P
| ACC_PL_U
| ACC_CODE
:
247 case ACC_P
| ACC_PL_U
| ACC_CODE_R
:
248 case ACC_P
| ACC_PL_U
| ACC_CODE_C
:
249 case ACC_P
| ACC_PL_U
| ACC_CODE_CR
:
250 case ACC_P
| ACC_PL_U
| ACC_CALL_GATE_16
:
251 case ACC_P
| ACC_PL_U
| ACC_CALL_GATE
:
255 user_ldt_free(new_ldt
);
261 task
->i386_ldt
= new_ldt
; /* new LDT for task */
264 * Switch to new LDT. We need to do this on all CPUs, since
265 * another thread in this same task may be currently running,
266 * and we need to make sure the new LDT is in place
267 * throughout the task before returning to the user.
269 mp_rendezvous_no_intrs(user_ldt_set_action
, task
);
273 /* free old LDT. We can't do this until after we've
274 * rendezvoused with all CPUs, in case another thread
275 * in this task was in the process of context switching.
278 user_ldt_free(old_ldt
);
289 uint32_t descs
, /* out */
293 task_t task
= current_task();
294 unsigned int ldt_count
;
297 if (start_sel
>= 8192)
299 if (start_sel
+ num_sels
> 8192)
306 user_ldt
= task
->i386_ldt
;
310 * copy out the descriptors
314 ldt_count
= user_ldt
->start
+ user_ldt
->count
;
316 ldt_count
= LDTSZ_MIN
;
319 if (start_sel
< ldt_count
)
321 unsigned int copy_sels
= num_sels
;
323 if (start_sel
+ num_sels
> ldt_count
)
324 copy_sels
= ldt_count
- start_sel
;
326 err
= copyout((char *)(current_ldt() + start_sel
),
327 descs
, copy_sels
* sizeof(struct real_descriptor
));
341 kfree(user_ldt
, sizeof(struct user_ldt
) + (user_ldt
->count
* sizeof(struct real_descriptor
)));
348 if (user_ldt
!= NULL
) {
349 size_t size
= sizeof(struct user_ldt
) + (user_ldt
->count
* sizeof(struct real_descriptor
));
350 user_ldt_t new_ldt
= (user_ldt_t
)kalloc(size
);
352 bcopy(user_ldt
, new_ldt
, size
);
363 task_t arg_task
= (task_t
)arg
;
365 if (arg_task
== current_task()) {
366 user_ldt_set(current_thread());
371 * Set the LDT for the given thread on the current CPU. Should be invoked
372 * with interrupts disabled.
378 task_t task
= thread
->task
;
381 user_ldt
= task
->i386_ldt
;
384 struct real_descriptor
*ldtp
= (struct real_descriptor
*)current_ldt();
386 if (user_ldt
->start
> LDTSZ_MIN
) {
387 bzero(&ldtp
[LDTSZ_MIN
],
388 sizeof(struct real_descriptor
) * (user_ldt
->start
- LDTSZ_MIN
));
391 bcopy(user_ldt
->ldt
, &ldtp
[user_ldt
->start
],
392 sizeof(struct real_descriptor
) * (user_ldt
->count
));
394 gdt_desc_p(USER_LDT
)->limit_low
= (sizeof(struct real_descriptor
) * (user_ldt
->start
+ user_ldt
->count
)) - 1;
396 ml_cpu_set_ldt(USER_LDT
);
398 ml_cpu_set_ldt(KERNEL_LDT
);