1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_kstack_pages.h"
36 #include "opt_mac.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/smp.h>
46 #include <sys/sysproto.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
52
53 #include <machine/cpu.h>
54 #include <machine/pcb.h>
55 #include <machine/pcb_ext.h>
56 #include <machine/proc.h>
57 #include <machine/sysarch.h>
58
59 #include <security/audit/audit.h>
60
61 #include <vm/vm_kern.h> /* for kernel_map */
62
63 #define MAX_LD 8192
64 #define LD_PER_PAGE 512
65 #define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
66 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
67 #define NULL_LDT_BASE ((caddr_t)NULL)
68
69 #ifdef SMP
70 static void set_user_ldt_rv(struct vmspace *vmsp);
71 #endif
72 static int i386_set_ldt_data(struct thread *, int start, int num,
73 union descriptor *descs);
74 static int i386_ldt_grow(struct thread *td, int len);
75
76 #ifndef _SYS_SYSPROTO_H_
77 struct sysarch_args {
78 int op;
79 char *parms;
80 };
81 #endif
82
83 int
84 sysarch(td, uap)
85 struct thread *td;
86 register struct sysarch_args *uap;
87 {
88 int error;
89 union descriptor *lp;
90 union {
91 struct i386_ldt_args largs;
92 struct i386_ioperm_args iargs;
93 } kargs;
94 uint32_t base;
95 struct segment_descriptor sd, *sdp;
96
97 AUDIT_ARG(cmd, uap->op);
98 switch (uap->op) {
99 case I386_GET_IOPERM:
100 case I386_SET_IOPERM:
101 if ((error = copyin(uap->parms, &kargs.iargs,
102 sizeof(struct i386_ioperm_args))) != 0)
103 return (error);
104 break;
105 case I386_GET_LDT:
106 case I386_SET_LDT:
107 if ((error = copyin(uap->parms, &kargs.largs,
108 sizeof(struct i386_ldt_args))) != 0)
109 return (error);
110 if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0)
111 return (EINVAL);
112 break;
113 default:
114 break;
115 }
116
117 switch(uap->op) {
118 case I386_GET_LDT:
119 error = i386_get_ldt(td, &kargs.largs);
120 break;
121 case I386_SET_LDT:
122 if (kargs.largs.descs != NULL) {
123 lp = (union descriptor *)kmem_alloc(kernel_map,
124 kargs.largs.num * sizeof(union descriptor));
125 if (lp == NULL) {
126 error = ENOMEM;
127 break;
128 }
129 error = copyin(kargs.largs.descs, lp,
130 kargs.largs.num * sizeof(union descriptor));
131 if (error == 0)
132 error = i386_set_ldt(td, &kargs.largs, lp);
133 kmem_free(kernel_map, (vm_offset_t)lp,
134 kargs.largs.num * sizeof(union descriptor));
135 } else {
136 error = i386_set_ldt(td, &kargs.largs, NULL);
137 }
138 break;
139 case I386_GET_IOPERM:
140 error = i386_get_ioperm(td, &kargs.iargs);
141 if (error == 0)
142 error = copyout(&kargs.iargs, uap->parms,
143 sizeof(struct i386_ioperm_args));
144 break;
145 case I386_SET_IOPERM:
146 error = i386_set_ioperm(td, &kargs.iargs);
147 break;
148 case I386_VM86:
149 error = vm86_sysarch(td, uap->parms);
150 break;
151 case I386_GET_FSBASE:
152 sdp = &td->td_pcb->pcb_fsd;
153 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
154 error = copyout(&base, uap->parms, sizeof(base));
155 break;
156 case I386_SET_FSBASE:
157 error = copyin(uap->parms, &base, sizeof(base));
158 if (!error) {
159 /*
160 * Construct a descriptor and store it in the pcb for
161 * the next context switch. Also store it in the gdt
162 * so that the load of tf_fs into %fs will activate it
163 * at return to userland.
164 */
165 sd.sd_lobase = base & 0xffffff;
166 sd.sd_hibase = (base >> 24) & 0xff;
167 sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
168 sd.sd_hilimit = 0xf;
169 sd.sd_type = SDT_MEMRWA;
170 sd.sd_dpl = SEL_UPL;
171 sd.sd_p = 1;
172 sd.sd_xx = 0;
173 sd.sd_def32 = 1;
174 sd.sd_gran = 1;
175 critical_enter();
176 td->td_pcb->pcb_fsd = sd;
177 PCPU_GET(fsgs_gdt)[0] = sd;
178 critical_exit();
179 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
180 }
181 break;
182 case I386_GET_GSBASE:
183 sdp = &td->td_pcb->pcb_gsd;
184 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
185 error = copyout(&base, uap->parms, sizeof(base));
186 break;
187 case I386_SET_GSBASE:
188 error = copyin(uap->parms, &base, sizeof(base));
189 if (!error) {
190 /*
191 * Construct a descriptor and store it in the pcb for
192 * the next context switch. Also store it in the gdt
193 * because we have to do a load_gs() right now.
194 */
195 sd.sd_lobase = base & 0xffffff;
196 sd.sd_hibase = (base >> 24) & 0xff;
197 sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
198 sd.sd_hilimit = 0xf;
199 sd.sd_type = SDT_MEMRWA;
200 sd.sd_dpl = SEL_UPL;
201 sd.sd_p = 1;
202 sd.sd_xx = 0;
203 sd.sd_def32 = 1;
204 sd.sd_gran = 1;
205 critical_enter();
206 td->td_pcb->pcb_gsd = sd;
207 PCPU_GET(fsgs_gdt)[1] = sd;
208 critical_exit();
209 load_gs(GSEL(GUGS_SEL, SEL_UPL));
210 }
211 break;
212 default:
213 error = EINVAL;
214 break;
215 }
216 return (error);
217 }
218
219 int
220 i386_extend_pcb(struct thread *td)
221 {
222 int i, offset;
223 u_long *addr;
224 struct pcb_ext *ext;
225 struct soft_segment_descriptor ssd = {
226 0, /* segment base address (overwritten) */
227 ctob(IOPAGES + 1) - 1, /* length */
228 SDT_SYS386TSS, /* segment type */
229 0, /* priority level */
230 1, /* descriptor present */
231 0, 0,
232 0, /* default 32 size */
233 0 /* granularity */
234 };
235
236 if (td->td_proc->p_flag & P_SA)
237 return (EINVAL); /* XXXKSE */
238 /* XXXKSE All the code below only works in 1:1 needs changing */
239 ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
240 if (ext == 0)
241 return (ENOMEM);
242 bzero(ext, sizeof(struct pcb_ext));
243 /* -16 is so we can convert a trapframe into vm86trapframe inplace */
244 ext->ext_tss.tss_esp0 = td->td_kstack + ctob(KSTACK_PAGES) -
245 sizeof(struct pcb) - 16;
246 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
247 /*
248 * The last byte of the i/o map must be followed by an 0xff byte.
249 * We arbitrarily allocate 16 bytes here, to keep the starting
250 * address on a doubleword boundary.
251 */
252 offset = PAGE_SIZE - 16;
253 ext->ext_tss.tss_ioopt =
254 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
255 ext->ext_iomap = (caddr_t)ext + offset;
256 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
257
258 addr = (u_long *)ext->ext_vm86.vm86_intmap;
259 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
260 *addr++ = ~0;
261
262 ssd.ssd_base = (unsigned)&ext->ext_tss;
263 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
264 ssdtosd(&ssd, &ext->ext_tssd);
265
266 KASSERT(td == curthread, ("giving TSS to !curthread"));
267 KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
268
269 /* Switch to the new TSS. */
270 critical_enter();
271 td->td_pcb->pcb_ext = ext;
272 PCPU_SET(private_tss, 1);
273 *PCPU_GET(tss_gdt) = ext->ext_tssd;
274 ltr(GSEL(GPROC0_SEL, SEL_KPL));
275 critical_exit();
276
277 return 0;
278 }
279
280 int
281 i386_set_ioperm(td, uap)
282 struct thread *td;
283 struct i386_ioperm_args *uap;
284 {
285 int i, error;
286 char *iomap;
287
288 if ((error = priv_check(td, PRIV_IO)) != 0)
289 return (error);
290 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
291 return (error);
292 /*
293 * XXX
294 * While this is restricted to root, we should probably figure out
295 * whether any other driver is using this i/o address, as so not to
296 * cause confusion. This probably requires a global 'usage registry'.
297 */
298
299 if (td->td_pcb->pcb_ext == 0)
300 if ((error = i386_extend_pcb(td)) != 0)
301 return (error);
302 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
303
304 if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
305 return (EINVAL);
306
307 for (i = uap->start; i < uap->start + uap->length; i++) {
308 if (uap->enable)
309 iomap[i >> 3] &= ~(1 << (i & 7));
310 else
311 iomap[i >> 3] |= (1 << (i & 7));
312 }
313 return (error);
314 }
315
316 int
317 i386_get_ioperm(td, uap)
318 struct thread *td;
319 struct i386_ioperm_args *uap;
320 {
321 int i, state;
322 char *iomap;
323
324 if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
325 return (EINVAL);
326
327 if (td->td_pcb->pcb_ext == 0) {
328 uap->length = 0;
329 goto done;
330 }
331
332 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
333
334 i = uap->start;
335 state = (iomap[i >> 3] >> (i & 7)) & 1;
336 uap->enable = !state;
337 uap->length = 1;
338
339 for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
340 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
341 break;
342 uap->length++;
343 }
344
345 done:
346 return (0);
347 }
348
349 /*
350 * Update the GDT entry pointing to the LDT to point to the LDT of the
351 * current process. Manage dt_lock holding/unholding autonomously.
352 */
353 void
354 set_user_ldt(struct mdproc *mdp)
355 {
356 struct proc_ldt *pldt;
357 int dtlocked;
358
359 dtlocked = 0;
360 if (!mtx_owned(&dt_lock)) {
361 mtx_lock_spin(&dt_lock);
362 dtlocked = 1;
363 }
364
365 pldt = mdp->md_ldt;
366 #ifdef SMP
367 gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
368 #else
369 gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
370 #endif
371 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
372 PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
373 if (dtlocked)
374 mtx_unlock_spin(&dt_lock);
375 }
376
377 #ifdef SMP
378 static void
379 set_user_ldt_rv(struct vmspace *vmsp)
380 {
381 struct thread *td;
382
383 td = curthread;
384 if (vmsp != td->td_proc->p_vmspace)
385 return;
386
387 set_user_ldt(&td->td_proc->p_md);
388 }
389 #endif
390
391 /*
392 * dt_lock must be held. Returns with dt_lock held.
393 */
394 struct proc_ldt *
395 user_ldt_alloc(struct mdproc *mdp, int len)
396 {
397 struct proc_ldt *pldt, *new_ldt;
398
399 mtx_assert(&dt_lock, MA_OWNED);
400 mtx_unlock_spin(&dt_lock);
401 MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt),
402 M_SUBPROC, M_WAITOK);
403
404 new_ldt->ldt_len = len = NEW_MAX_LD(len);
405 new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
406 len * sizeof(union descriptor));
407 if (new_ldt->ldt_base == NULL) {
408 FREE(new_ldt, M_SUBPROC);
409 return NULL;
410 }
411 new_ldt->ldt_refcnt = 1;
412 new_ldt->ldt_active = 0;
413
414 mtx_lock_spin(&dt_lock);
415 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
416 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
417 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
418
419 if ((pldt = mdp->md_ldt) != NULL) {
420 if (len > pldt->ldt_len)
421 len = pldt->ldt_len;
422 bcopy(pldt->ldt_base, new_ldt->ldt_base,
423 len * sizeof(union descriptor));
424 } else
425 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
426
427 return (new_ldt);
428 }
429
430 /*
431 * Must be called with dt_lock held. Returns with dt_lock unheld.
432 */
433 void
434 user_ldt_free(struct thread *td)
435 {
436 struct mdproc *mdp = &td->td_proc->p_md;
437 struct proc_ldt *pldt;
438
439 mtx_assert(&dt_lock, MA_OWNED);
440 if ((pldt = mdp->md_ldt) == NULL)
441 return;
442
443 if (td == PCPU_GET(curthread)) {
444 lldt(_default_ldt);
445 PCPU_SET(currentldt, _default_ldt);
446 }
447
448 mdp->md_ldt = NULL;
449 if (--pldt->ldt_refcnt == 0) {
450 mtx_unlock_spin(&dt_lock);
451 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
452 pldt->ldt_len * sizeof(union descriptor));
453 FREE(pldt, M_SUBPROC);
454 } else
455 mtx_unlock_spin(&dt_lock);
456 }
457
458 /*
459 * Note for the authors of compat layers (linux, etc): copyout() in
460 * the function below is not a problem since it presents data in
461 * arch-specific format (i.e. i386-specific in this case), not in
462 * the OS-specific one.
463 */
464 int
465 i386_get_ldt(td, uap)
466 struct thread *td;
467 struct i386_ldt_args *uap;
468 {
469 int error = 0;
470 struct proc_ldt *pldt;
471 int nldt, num;
472 union descriptor *lp;
473
474 #ifdef DEBUG
475 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
476 uap->start, uap->num, (void *)uap->descs);
477 #endif
478
479 mtx_lock_spin(&dt_lock);
480 if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
481 nldt = pldt->ldt_len;
482 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
483 mtx_unlock_spin(&dt_lock);
484 num = min(uap->num, nldt);
485 } else {
486 mtx_unlock_spin(&dt_lock);
487 nldt = sizeof(ldt)/sizeof(ldt[0]);
488 num = min(uap->num, nldt);
489 lp = &ldt[uap->start];
490 }
491
492 if ((uap->start > (unsigned int)nldt) ||
493 ((unsigned int)num > (unsigned int)nldt) ||
494 ((unsigned int)(uap->start + num) > (unsigned int)nldt))
495 return(EINVAL);
496
497 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
498 if (!error)
499 td->td_retval[0] = num;
500
501 return(error);
502 }
503
504 static int ldt_warnings;
505 #define NUM_LDT_WARNINGS 10
506
507 int
508 i386_set_ldt(td, uap, descs)
509 struct thread *td;
510 struct i386_ldt_args *uap;
511 union descriptor *descs;
512 {
513 int error = 0, i;
514 int largest_ld;
515 struct mdproc *mdp = &td->td_proc->p_md;
516 struct proc_ldt *pldt;
517 union descriptor *dp;
518
519 #ifdef DEBUG
520 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
521 uap->start, uap->num, (void *)uap->descs);
522 #endif
523
524 if (descs == NULL) {
525 /* Free descriptors */
526 if (uap->start == 0 && uap->num == 0) {
527 /*
528 * Treat this as a special case, so userland needn't
529 * know magic number NLDT.
530 */
531 uap->start = NLDT;
532 uap->num = MAX_LD - NLDT;
533 }
534 if (uap->num <= 0)
535 return (EINVAL);
536 mtx_lock_spin(&dt_lock);
537 if ((pldt = mdp->md_ldt) == NULL ||
538 uap->start >= pldt->ldt_len) {
539 mtx_unlock_spin(&dt_lock);
540 return (0);
541 }
542 largest_ld = uap->start + uap->num;
543 if (largest_ld > pldt->ldt_len)
544 largest_ld = pldt->ldt_len;
545 i = largest_ld - uap->start;
546 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
547 sizeof(union descriptor) * i);
548 mtx_unlock_spin(&dt_lock);
549 return (0);
550 }
551
552 if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
553 /* complain a for a while if using old methods */
554 if (ldt_warnings++ < NUM_LDT_WARNINGS) {
555 printf("Warning: pid %d used static ldt allocation.\n",
556 td->td_proc->p_pid);
557 printf("See the i386_set_ldt man page for more info\n");
558 }
559 /* verify range of descriptors to modify */
560 largest_ld = uap->start + uap->num;
561 if (uap->start >= MAX_LD ||
562 uap->num < 0 || largest_ld > MAX_LD) {
563 return (EINVAL);
564 }
565 }
566
567 /* Check descriptors for access violations */
568 for (i = 0; i < uap->num; i++) {
569 dp = &descs[i];
570
571 switch (dp->sd.sd_type) {
572 case SDT_SYSNULL: /* system null */
573 dp->sd.sd_p = 0;
574 break;
575 case SDT_SYS286TSS: /* system 286 TSS available */
576 case SDT_SYSLDT: /* system local descriptor table */
577 case SDT_SYS286BSY: /* system 286 TSS busy */
578 case SDT_SYSTASKGT: /* system task gate */
579 case SDT_SYS286IGT: /* system 286 interrupt gate */
580 case SDT_SYS286TGT: /* system 286 trap gate */
581 case SDT_SYSNULL2: /* undefined by Intel */
582 case SDT_SYS386TSS: /* system 386 TSS available */
583 case SDT_SYSNULL3: /* undefined by Intel */
584 case SDT_SYS386BSY: /* system 386 TSS busy */
585 case SDT_SYSNULL4: /* undefined by Intel */
586 case SDT_SYS386IGT: /* system 386 interrupt gate */
587 case SDT_SYS386TGT: /* system 386 trap gate */
588 case SDT_SYS286CGT: /* system 286 call gate */
589 case SDT_SYS386CGT: /* system 386 call gate */
590 /* I can't think of any reason to allow a user proc
591 * to create a segment of these types. They are
592 * for OS use only.
593 */
594 return (EACCES);
595 /*NOTREACHED*/
596
597 /* memory segment types */
598 case SDT_MEMEC: /* memory execute only conforming */
599 case SDT_MEMEAC: /* memory execute only accessed conforming */
600 case SDT_MEMERC: /* memory execute read conforming */
601 case SDT_MEMERAC: /* memory execute read accessed conforming */
602 /* Must be "present" if executable and conforming. */
603 if (dp->sd.sd_p == 0)
604 return (EACCES);
605 break;
606 case SDT_MEMRO: /* memory read only */
607 case SDT_MEMROA: /* memory read only accessed */
608 case SDT_MEMRW: /* memory read write */
609 case SDT_MEMRWA: /* memory read write accessed */
610 case SDT_MEMROD: /* memory read only expand dwn limit */
611 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
612 case SDT_MEMRWD: /* memory read write expand dwn limit */
613 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
614 case SDT_MEME: /* memory execute only */
615 case SDT_MEMEA: /* memory execute only accessed */
616 case SDT_MEMER: /* memory execute read */
617 case SDT_MEMERA: /* memory execute read accessed */
618 break;
619 default:
620 return(EINVAL);
621 /*NOTREACHED*/
622 }
623
624 /* Only user (ring-3) descriptors may be present. */
625 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
626 return (EACCES);
627 }
628
629 if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
630 /* Allocate a free slot */
631 mtx_lock_spin(&dt_lock);
632 if ((pldt = mdp->md_ldt) == NULL) {
633 if ((error = i386_ldt_grow(td, NLDT + 1))) {
634 mtx_unlock_spin(&dt_lock);
635 return (error);
636 }
637 pldt = mdp->md_ldt;
638 }
639 again:
640 /*
641 * start scanning a bit up to leave room for NVidia and
642 * Wine, which still user the "Blat" method of allocation.
643 */
644 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
645 for (i = NLDT; i < pldt->ldt_len; ++i) {
646 if (dp->sd.sd_type == SDT_SYSNULL)
647 break;
648 dp++;
649 }
650 if (i >= pldt->ldt_len) {
651 if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
652 mtx_unlock_spin(&dt_lock);
653 return (error);
654 }
655 goto again;
656 }
657 uap->start = i;
658 error = i386_set_ldt_data(td, i, 1, descs);
659 mtx_unlock_spin(&dt_lock);
660 } else {
661 largest_ld = uap->start + uap->num;
662 mtx_lock_spin(&dt_lock);
663 if (!(error = i386_ldt_grow(td, largest_ld))) {
664 error = i386_set_ldt_data(td, uap->start, uap->num,
665 descs);
666 }
667 mtx_unlock_spin(&dt_lock);
668 }
669 if (error == 0)
670 td->td_retval[0] = uap->start;
671 return (error);
672 }
673
674 static int
675 i386_set_ldt_data(struct thread *td, int start, int num,
676 union descriptor *descs)
677 {
678 struct mdproc *mdp = &td->td_proc->p_md;
679 struct proc_ldt *pldt = mdp->md_ldt;
680
681 mtx_assert(&dt_lock, MA_OWNED);
682
683 /* Fill in range */
684 bcopy(descs,
685 &((union descriptor *)(pldt->ldt_base))[start],
686 num * sizeof(union descriptor));
687 return (0);
688 }
689
690 static int
691 i386_ldt_grow(struct thread *td, int len)
692 {
693 struct mdproc *mdp = &td->td_proc->p_md;
694 struct proc_ldt *new_ldt, *pldt;
695 caddr_t old_ldt_base = NULL_LDT_BASE;
696 int old_ldt_len = 0;
697
698 mtx_assert(&dt_lock, MA_OWNED);
699
700 if (len > MAX_LD)
701 return (ENOMEM);
702 if (len < NLDT + 1)
703 len = NLDT + 1;
704
705 /* Allocate a user ldt. */
706 if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
707 new_ldt = user_ldt_alloc(mdp, len);
708 if (new_ldt == NULL)
709 return (ENOMEM);
710 pldt = mdp->md_ldt;
711
712 if (pldt != NULL) {
713 if (new_ldt->ldt_len <= pldt->ldt_len) {
714 /*
715 * We just lost the race for allocation, so
716 * free the new object and return.
717 */
718 mtx_unlock_spin(&dt_lock);
719 kmem_free(kernel_map,
720 (vm_offset_t)new_ldt->ldt_base,
721 new_ldt->ldt_len * sizeof(union descriptor));
722 FREE(new_ldt, M_SUBPROC);
723 mtx_lock_spin(&dt_lock);
724 return (0);
725 }
726
727 /*
728 * We have to substitute the current LDT entry for
729 * curproc with the new one since its size grew.
730 */
731 old_ldt_base = pldt->ldt_base;
732 old_ldt_len = pldt->ldt_len;
733 pldt->ldt_sd = new_ldt->ldt_sd;
734 pldt->ldt_base = new_ldt->ldt_base;
735 pldt->ldt_len = new_ldt->ldt_len;
736 } else
737 mdp->md_ldt = pldt = new_ldt;
738 #ifdef SMP
739 /*
740 * Signal other cpus to reload ldt. We need to unlock dt_lock
741 * here because other CPU will contest on it since their
742 * curthreads won't hold the lock and will block when trying
743 * to acquire it.
744 */
745 mtx_unlock_spin(&dt_lock);
746 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
747 NULL, td->td_proc->p_vmspace);
748 #else
749 set_user_ldt(&td->td_proc->p_md);
750 mtx_unlock_spin(&dt_lock);
751 #endif
752 if (old_ldt_base != NULL_LDT_BASE) {
753 kmem_free(kernel_map, (vm_offset_t)old_ldt_base,
754 old_ldt_len * sizeof(union descriptor));
755 FREE(new_ldt, M_SUBPROC);
756 }
757 mtx_lock_spin(&dt_lock);
758 }
759 return (0);
760 }
Cache object: 5e6fa66c168273a5d91d215b5602f3c2
|