]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/user_ldt.c
1c735f0d02e8ee93d5b48e828805e0d34ad3412d
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
55 * User LDT management.
56 * Each task may have its own LDT.
59 #include <kern/kalloc.h>
60 #include <kern/thread.h>
61 #include <kern/misc_protos.h>
63 #include <vm/vm_kern.h>
66 #include <i386/thread.h>
67 #include <i386/user_ldt.h>
68 #include <i386/mp_desc.h>
69 #include <i386/proc_reg.h>
70 #include <i386/machdep_call.h>
72 #include <i386/machine_routines.h>
74 #include <sys/errno.h>
76 static void user_ldt_set_action(void *);
79 * Add the descriptors to the LDT, starting with
80 * the descriptor for 'first_selector'.
87 uint32_t descs
, /* out */
90 user_ldt_t new_ldt
, old_ldt
;
91 struct real_descriptor
*dp
;
93 unsigned int min_selector
= LDTSZ_MIN
; /* do not allow the system selectors to be changed */
94 task_t task
= current_task();
95 unsigned int ldt_count
;
98 if (start_sel
!= LDT_AUTO_ALLOC
99 && (start_sel
!= 0 || num_sels
!= 0)
100 && (start_sel
< min_selector
|| start_sel
>= LDTSZ
))
102 if (start_sel
!= LDT_AUTO_ALLOC
103 && start_sel
+ num_sels
> LDTSZ
)
108 old_ldt
= task
->i386_ldt
;
110 if (start_sel
== LDT_AUTO_ALLOC
) {
112 unsigned int null_count
;
113 struct real_descriptor null_ldt
;
115 bzero(&null_ldt
, sizeof(null_ldt
));
118 * Look for null selectors among the already-allocated
123 while (i
< old_ldt
->count
)
125 if (!memcmp(&old_ldt
->ldt
[i
++], &null_ldt
, sizeof(null_ldt
))) {
127 if (null_count
== num_sels
)
128 break; /* break out of while loop */
135 * If we broke out of the while loop, i points to the selector
136 * after num_sels null selectors. Otherwise it points to the end
137 * of the old LDTs, and null_count is the number of null selectors
140 * Either way, there are null_count null selectors just prior to
141 * the i-indexed selector, and either null_count >= num_sels,
142 * or we're at the end, so we can extend.
144 start_sel
= old_ldt
->start
+ i
- null_count
;
146 start_sel
= LDTSZ_MIN
;
149 if (start_sel
+ num_sels
> LDTSZ
) {
155 if (start_sel
== 0 && num_sels
== 0) {
162 unsigned int begin_sel
= start_sel
;
163 unsigned int end_sel
= begin_sel
+ num_sels
;
165 if (old_ldt
!= NULL
) {
166 if (old_ldt
->start
< begin_sel
)
167 begin_sel
= old_ldt
->start
;
168 if (old_ldt
->start
+ old_ldt
->count
> end_sel
)
169 end_sel
= old_ldt
->start
+ old_ldt
->count
;
172 ldt_count
= end_sel
- begin_sel
;
174 new_ldt
= (user_ldt_t
)kalloc(sizeof(struct user_ldt
) + (ldt_count
* sizeof(struct real_descriptor
)));
175 if (new_ldt
== NULL
) {
180 new_ldt
->start
= begin_sel
;
181 new_ldt
->count
= ldt_count
;
184 * Have new LDT. If there was a an old ldt, copy descriptors
188 bcopy(&old_ldt
->ldt
[0],
189 &new_ldt
->ldt
[old_ldt
->start
- begin_sel
],
190 old_ldt
->count
* sizeof(struct real_descriptor
));
193 * If the old and new LDTs are non-overlapping, fill the
194 * center in with null selectors.
197 if (old_ldt
->start
+ old_ldt
->count
< start_sel
)
198 bzero(&new_ldt
->ldt
[old_ldt
->count
],
199 (start_sel
- (old_ldt
->start
+ old_ldt
->count
)) * sizeof(struct real_descriptor
));
200 else if (old_ldt
->start
> start_sel
+ num_sels
)
201 bzero(&new_ldt
->ldt
[num_sels
],
202 (old_ldt
->start
- (start_sel
+ num_sels
)) * sizeof(struct real_descriptor
));
206 * Install new descriptors.
209 err
= copyin(descs
, (char *)&new_ldt
->ldt
[start_sel
- begin_sel
],
210 num_sels
* sizeof(struct real_descriptor
));
214 user_ldt_free(new_ldt
);
218 bzero(&new_ldt
->ldt
[start_sel
- begin_sel
], num_sels
* sizeof(struct real_descriptor
));
222 * Validate descriptors.
223 * Only allow descriptors with user priviledges.
225 for (i
= 0, dp
= (struct real_descriptor
*) &new_ldt
->ldt
[start_sel
- begin_sel
];
229 switch (dp
->access
& ~ACC_A
) {
232 /* valid empty descriptor */
234 case ACC_P
| ACC_PL_U
| ACC_DATA
:
235 case ACC_P
| ACC_PL_U
| ACC_DATA_W
:
236 case ACC_P
| ACC_PL_U
| ACC_DATA_E
:
237 case ACC_P
| ACC_PL_U
| ACC_DATA_EW
:
238 case ACC_P
| ACC_PL_U
| ACC_CODE
:
239 case ACC_P
| ACC_PL_U
| ACC_CODE_R
:
240 case ACC_P
| ACC_PL_U
| ACC_CODE_C
:
241 case ACC_P
| ACC_PL_U
| ACC_CODE_CR
:
242 case ACC_P
| ACC_PL_U
| ACC_CALL_GATE_16
:
243 case ACC_P
| ACC_PL_U
| ACC_CALL_GATE
:
247 user_ldt_free(new_ldt
);
253 task
->i386_ldt
= new_ldt
; /* new LDT for task */
256 * Switch to new LDT. We need to do this on all CPUs, since
257 * another thread in this same task may be currently running,
258 * and we need to make sure the new LDT is in place
259 * throughout the task before returning to the user.
261 mp_rendezvous_no_intrs(user_ldt_set_action
, task
);
265 /* free old LDT. We can't do this until after we've
266 * rendezvoused with all CPUs, in case another thread
267 * in this task was in the process of context switching.
270 user_ldt_free(old_ldt
);
281 uint32_t descs
, /* out */
285 task_t task
= current_task();
286 unsigned int ldt_count
;
289 if (start_sel
>= 8192)
291 if (start_sel
+ num_sels
> 8192)
298 user_ldt
= task
->i386_ldt
;
302 * copy out the descriptors
306 ldt_count
= user_ldt
->start
+ user_ldt
->count
;
308 ldt_count
= LDTSZ_MIN
;
311 if (start_sel
< ldt_count
)
313 unsigned int copy_sels
= num_sels
;
315 if (start_sel
+ num_sels
> ldt_count
)
316 copy_sels
= ldt_count
- start_sel
;
318 err
= copyout((char *)(current_ldt() + start_sel
),
319 descs
, copy_sels
* sizeof(struct real_descriptor
));
333 kfree(user_ldt
, sizeof(struct user_ldt
) + (user_ldt
->count
* sizeof(struct real_descriptor
)));
340 if (user_ldt
!= NULL
) {
341 size_t size
= sizeof(struct user_ldt
) + (user_ldt
->count
* sizeof(struct real_descriptor
));
342 user_ldt_t new_ldt
= (user_ldt_t
)kalloc(size
);
344 bcopy(user_ldt
, new_ldt
, size
);
355 task_t arg_task
= (task_t
)arg
;
357 if (arg_task
== current_task()) {
358 user_ldt_set(current_thread());
363 * Set the LDT for the given thread on the current CPU. Should be invoked
364 * with interrupts disabled.
370 task_t task
= thread
->task
;
373 user_ldt
= task
->i386_ldt
;
376 struct real_descriptor
*ldtp
= (struct real_descriptor
*)current_ldt();
378 if (user_ldt
->start
> LDTSZ_MIN
) {
379 bzero(&ldtp
[LDTSZ_MIN
],
380 sizeof(struct real_descriptor
) * (user_ldt
->start
- LDTSZ_MIN
));
383 bcopy(user_ldt
->ldt
, &ldtp
[user_ldt
->start
],
384 sizeof(struct real_descriptor
) * (user_ldt
->count
));
386 gdt_desc_p(USER_LDT
)->limit_low
= (sizeof(struct real_descriptor
) * (user_ldt
->start
+ user_ldt
->count
)) - 1;
388 ml_cpu_set_ldt(USER_LDT
);
390 ml_cpu_set_ldt(KERNEL_LDT
);