2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_capsicum.h"
36 #include "opt_kstack_pages.h"
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
47 #include <sys/sysproto.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
54 #include <machine/cpu.h>
55 #include <machine/pcb.h>
56 #include <machine/pcb_ext.h>
57 #include <machine/proc.h>
58 #include <machine/sysarch.h>
60 #include <security/audit/audit.h>
63 #include <machine/xen/xenfunc.h>
65 void i386_reset_ldt(struct proc_ldt *pldt);
68 i386_reset_ldt(struct proc_ldt *pldt)
70 xen_set_ldt((vm_offset_t)pldt->ldt_base, pldt->ldt_len);
73 #define i386_reset_ldt(x)
76 #include <vm/vm_kern.h> /* for kernel_map */
79 #define LD_PER_PAGE 512
80 #define NEW_MAX_LD(num) ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
81 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
82 #define NULL_LDT_BASE ((caddr_t)NULL)
85 static void set_user_ldt_rv(struct vmspace *vmsp);
87 static int i386_set_ldt_data(struct thread *, int start, int num,
88 union descriptor *descs);
89 static int i386_ldt_grow(struct thread *td, int len);
92 fill_based_sd(struct segment_descriptor *sdp, uint32_t base)
95 sdp->sd_lobase = base & 0xffffff;
96 sdp->sd_hibase = (base >> 24) & 0xff;
98 /* need to do nosegneg like Linux */
99 sdp->sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
101 sdp->sd_lolimit = 0xffff; /* 4GB limit, wraps around */
103 sdp->sd_hilimit = 0xf;
104 sdp->sd_type = SDT_MEMRWA;
105 sdp->sd_dpl = SEL_UPL;
112 #ifndef _SYS_SYSPROTO_H_
113 struct sysarch_args {
122 register struct sysarch_args *uap;
125 union descriptor *lp;
127 struct i386_ldt_args largs;
128 struct i386_ioperm_args iargs;
129 struct i386_get_xfpustate xfpu;
132 struct segment_descriptor sd, *sdp;
134 AUDIT_ARG_CMD(uap->op);
136 #ifdef CAPABILITY_MODE
138 * When adding new operations, add a new case statement here to
139 * explicitly indicate whether or not the operation is safe to
140 * perform in capability mode.
142 if (IN_CAPABILITY_MODE(td)) {
146 case I386_GET_IOPERM:
147 case I386_GET_FSBASE:
148 case I386_SET_FSBASE:
149 case I386_GET_GSBASE:
150 case I386_SET_GSBASE:
151 case I386_GET_XFPUSTATE:
154 case I386_SET_IOPERM:
157 if (KTRPOINT(td, KTR_CAPFAIL))
158 ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL);
166 case I386_GET_IOPERM:
167 case I386_SET_IOPERM:
168 if ((error = copyin(uap->parms, &kargs.iargs,
169 sizeof(struct i386_ioperm_args))) != 0)
174 if ((error = copyin(uap->parms, &kargs.largs,
175 sizeof(struct i386_ldt_args))) != 0)
177 if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0)
180 case I386_GET_XFPUSTATE:
181 if ((error = copyin(uap->parms, &kargs.xfpu,
182 sizeof(struct i386_get_xfpustate))) != 0)
191 error = i386_get_ldt(td, &kargs.largs);
194 if (kargs.largs.descs != NULL) {
195 lp = (union descriptor *)malloc(
196 kargs.largs.num * sizeof(union descriptor),
198 error = copyin(kargs.largs.descs, lp,
199 kargs.largs.num * sizeof(union descriptor));
201 error = i386_set_ldt(td, &kargs.largs, lp);
204 error = i386_set_ldt(td, &kargs.largs, NULL);
207 case I386_GET_IOPERM:
208 error = i386_get_ioperm(td, &kargs.iargs);
210 error = copyout(&kargs.iargs, uap->parms,
211 sizeof(struct i386_ioperm_args));
213 case I386_SET_IOPERM:
214 error = i386_set_ioperm(td, &kargs.iargs);
217 error = vm86_sysarch(td, uap->parms);
219 case I386_GET_FSBASE:
220 sdp = &td->td_pcb->pcb_fsd;
221 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
222 error = copyout(&base, uap->parms, sizeof(base));
224 case I386_SET_FSBASE:
225 error = copyin(uap->parms, &base, sizeof(base));
228 * Construct a descriptor and store it in the pcb for
229 * the next context switch. Also store it in the gdt
230 * so that the load of tf_fs into %fs will activate it
231 * at return to userland.
233 fill_based_sd(&sd, base);
235 td->td_pcb->pcb_fsd = sd;
237 HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[0]),
240 PCPU_GET(fsgs_gdt)[0] = sd;
243 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
246 case I386_GET_GSBASE:
247 sdp = &td->td_pcb->pcb_gsd;
248 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
249 error = copyout(&base, uap->parms, sizeof(base));
251 case I386_SET_GSBASE:
252 error = copyin(uap->parms, &base, sizeof(base));
255 * Construct a descriptor and store it in the pcb for
256 * the next context switch. Also store it in the gdt
257 * because we have to do a load_gs() right now.
259 fill_based_sd(&sd, base);
261 td->td_pcb->pcb_gsd = sd;
263 HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[1]),
266 PCPU_GET(fsgs_gdt)[1] = sd;
269 load_gs(GSEL(GUGS_SEL, SEL_UPL));
272 case I386_GET_XFPUSTATE:
273 if (kargs.xfpu.len > cpu_max_ext_state_size -
274 sizeof(union savefpu))
277 error = copyout((char *)(get_pcb_user_save_td(td) + 1),
278 kargs.xfpu.addr, kargs.xfpu.len);
288 i386_extend_pcb(struct thread *td)
293 struct soft_segment_descriptor ssd = {
294 0, /* segment base address (overwritten) */
295 ctob(IOPAGES + 1) - 1, /* length */
296 SDT_SYS386TSS, /* segment type */
297 0, /* priority level */
298 1, /* descriptor present */
300 0, /* default 32 size */
304 ext = (struct pcb_ext *)kmem_malloc(kernel_arena, ctob(IOPAGES+1),
306 /* -16 is so we can convert a trapframe into vm86trapframe inplace */
307 ext->ext_tss.tss_esp0 = (vm_offset_t)td->td_pcb - 16;
308 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
310 * The last byte of the i/o map must be followed by an 0xff byte.
311 * We arbitrarily allocate 16 bytes here, to keep the starting
312 * address on a doubleword boundary.
314 offset = PAGE_SIZE - 16;
315 ext->ext_tss.tss_ioopt =
316 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
317 ext->ext_iomap = (caddr_t)ext + offset;
318 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
320 addr = (u_long *)ext->ext_vm86.vm86_intmap;
321 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
324 ssd.ssd_base = (unsigned)&ext->ext_tss;
325 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
326 ssdtosd(&ssd, &ext->ext_tssd);
328 KASSERT(td == curthread, ("giving TSS to !curthread"));
329 KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
331 /* Switch to the new TSS. */
333 td->td_pcb->pcb_ext = ext;
334 PCPU_SET(private_tss, 1);
335 *PCPU_GET(tss_gdt) = ext->ext_tssd;
336 ltr(GSEL(GPROC0_SEL, SEL_KPL));
343 i386_set_ioperm(td, uap)
345 struct i386_ioperm_args *uap;
350 if ((error = priv_check(td, PRIV_IO)) != 0)
352 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
356 * While this is restricted to root, we should probably figure out
357 * whether any other driver is using this i/o address, as so not to
358 * cause confusion. This probably requires a global 'usage registry'.
361 if (td->td_pcb->pcb_ext == 0)
362 if ((error = i386_extend_pcb(td)) != 0)
364 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
366 if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
369 for (i = uap->start; i < uap->start + uap->length; i++) {
371 iomap[i >> 3] &= ~(1 << (i & 7));
373 iomap[i >> 3] |= (1 << (i & 7));
379 i386_get_ioperm(td, uap)
381 struct i386_ioperm_args *uap;
386 if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
389 if (td->td_pcb->pcb_ext == 0) {
394 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
397 state = (iomap[i >> 3] >> (i & 7)) & 1;
398 uap->enable = !state;
401 for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
402 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
412 * Update the GDT entry pointing to the LDT to point to the LDT of the
413 * current process. Manage dt_lock holding/unholding autonomously.
416 set_user_ldt(struct mdproc *mdp)
418 struct proc_ldt *pldt;
422 if (!mtx_owned(&dt_lock)) {
423 mtx_lock_spin(&dt_lock);
429 i386_reset_ldt(pldt);
430 PCPU_SET(currentldt, (int)pldt);
433 gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
435 gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
437 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
438 PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
441 mtx_unlock_spin(&dt_lock);
446 set_user_ldt_rv(struct vmspace *vmsp)
451 if (vmsp != td->td_proc->p_vmspace)
454 set_user_ldt(&td->td_proc->p_md);
461 * dt_lock must be held. Returns with dt_lock held.
464 user_ldt_alloc(struct mdproc *mdp, int len)
466 struct proc_ldt *pldt, *new_ldt;
468 mtx_assert(&dt_lock, MA_OWNED);
469 mtx_unlock_spin(&dt_lock);
470 new_ldt = malloc(sizeof(struct proc_ldt),
471 M_SUBPROC, M_WAITOK);
473 new_ldt->ldt_len = len = NEW_MAX_LD(len);
474 new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
475 round_page(len * sizeof(union descriptor)), M_WAITOK);
476 new_ldt->ldt_refcnt = 1;
477 new_ldt->ldt_active = 0;
479 mtx_lock_spin(&dt_lock);
480 if ((pldt = mdp->md_ldt)) {
481 if (len > pldt->ldt_len)
483 bcopy(pldt->ldt_base, new_ldt->ldt_base,
484 len * sizeof(union descriptor));
486 bcopy(ldt, new_ldt->ldt_base, PAGE_SIZE);
488 mtx_unlock_spin(&dt_lock); /* XXX kill once pmap locking fixed. */
489 pmap_map_readonly(kernel_pmap, (vm_offset_t)new_ldt->ldt_base,
490 new_ldt->ldt_len*sizeof(union descriptor));
491 mtx_lock_spin(&dt_lock); /* XXX kill once pmap locking fixed. */
496 * dt_lock must be held. Returns with dt_lock held.
499 user_ldt_alloc(struct mdproc *mdp, int len)
501 struct proc_ldt *pldt, *new_ldt;
503 mtx_assert(&dt_lock, MA_OWNED);
504 mtx_unlock_spin(&dt_lock);
505 new_ldt = malloc(sizeof(struct proc_ldt),
506 M_SUBPROC, M_WAITOK);
508 new_ldt->ldt_len = len = NEW_MAX_LD(len);
509 new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
510 len * sizeof(union descriptor), M_WAITOK);
511 new_ldt->ldt_refcnt = 1;
512 new_ldt->ldt_active = 0;
514 mtx_lock_spin(&dt_lock);
515 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
516 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
517 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
519 if ((pldt = mdp->md_ldt) != NULL) {
520 if (len > pldt->ldt_len)
522 bcopy(pldt->ldt_base, new_ldt->ldt_base,
523 len * sizeof(union descriptor));
525 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
532 * Must be called with dt_lock held. Returns with dt_lock unheld.
535 user_ldt_free(struct thread *td)
537 struct mdproc *mdp = &td->td_proc->p_md;
538 struct proc_ldt *pldt;
540 mtx_assert(&dt_lock, MA_OWNED);
541 if ((pldt = mdp->md_ldt) == NULL) {
542 mtx_unlock_spin(&dt_lock);
546 if (td == curthread) {
548 i386_reset_ldt(&default_proc_ldt);
549 PCPU_SET(currentldt, (int)&default_proc_ldt);
552 PCPU_SET(currentldt, _default_ldt);
557 user_ldt_deref(pldt);
561 user_ldt_deref(struct proc_ldt *pldt)
564 mtx_assert(&dt_lock, MA_OWNED);
565 if (--pldt->ldt_refcnt == 0) {
566 mtx_unlock_spin(&dt_lock);
567 kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base,
568 pldt->ldt_len * sizeof(union descriptor));
569 free(pldt, M_SUBPROC);
571 mtx_unlock_spin(&dt_lock);
575 * Note for the authors of compat layers (linux, etc): copyout() in
576 * the function below is not a problem since it presents data in
577 * arch-specific format (i.e. i386-specific in this case), not in
578 * the OS-specific one.
581 i386_get_ldt(td, uap)
583 struct i386_ldt_args *uap;
586 struct proc_ldt *pldt;
588 union descriptor *lp;
591 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
592 uap->start, uap->num, (void *)uap->descs);
595 mtx_lock_spin(&dt_lock);
596 if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
597 nldt = pldt->ldt_len;
598 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
599 mtx_unlock_spin(&dt_lock);
600 num = min(uap->num, nldt);
602 mtx_unlock_spin(&dt_lock);
603 nldt = sizeof(ldt)/sizeof(ldt[0]);
604 num = min(uap->num, nldt);
605 lp = &ldt[uap->start];
608 if ((uap->start > (unsigned int)nldt) ||
609 ((unsigned int)num > (unsigned int)nldt) ||
610 ((unsigned int)(uap->start + num) > (unsigned int)nldt))
613 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
615 td->td_retval[0] = num;
621 i386_set_ldt(td, uap, descs)
623 struct i386_ldt_args *uap;
624 union descriptor *descs;
628 struct mdproc *mdp = &td->td_proc->p_md;
629 struct proc_ldt *pldt;
630 union descriptor *dp;
633 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
634 uap->start, uap->num, (void *)uap->descs);
638 /* Free descriptors */
639 if (uap->start == 0 && uap->num == 0) {
641 * Treat this as a special case, so userland needn't
642 * know magic number NLDT.
645 uap->num = MAX_LD - NLDT;
649 mtx_lock_spin(&dt_lock);
650 if ((pldt = mdp->md_ldt) == NULL ||
651 uap->start >= pldt->ldt_len) {
652 mtx_unlock_spin(&dt_lock);
655 largest_ld = uap->start + uap->num;
656 if (largest_ld > pldt->ldt_len)
657 largest_ld = pldt->ldt_len;
658 i = largest_ld - uap->start;
659 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
660 sizeof(union descriptor) * i);
661 mtx_unlock_spin(&dt_lock);
665 if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
666 /* verify range of descriptors to modify */
667 largest_ld = uap->start + uap->num;
668 if (uap->start >= MAX_LD || largest_ld > MAX_LD) {
673 /* Check descriptors for access violations */
674 for (i = 0; i < uap->num; i++) {
677 switch (dp->sd.sd_type) {
678 case SDT_SYSNULL: /* system null */
681 case SDT_SYS286TSS: /* system 286 TSS available */
682 case SDT_SYSLDT: /* system local descriptor table */
683 case SDT_SYS286BSY: /* system 286 TSS busy */
684 case SDT_SYSTASKGT: /* system task gate */
685 case SDT_SYS286IGT: /* system 286 interrupt gate */
686 case SDT_SYS286TGT: /* system 286 trap gate */
687 case SDT_SYSNULL2: /* undefined by Intel */
688 case SDT_SYS386TSS: /* system 386 TSS available */
689 case SDT_SYSNULL3: /* undefined by Intel */
690 case SDT_SYS386BSY: /* system 386 TSS busy */
691 case SDT_SYSNULL4: /* undefined by Intel */
692 case SDT_SYS386IGT: /* system 386 interrupt gate */
693 case SDT_SYS386TGT: /* system 386 trap gate */
694 case SDT_SYS286CGT: /* system 286 call gate */
695 case SDT_SYS386CGT: /* system 386 call gate */
696 /* I can't think of any reason to allow a user proc
697 * to create a segment of these types. They are
703 /* memory segment types */
704 case SDT_MEMEC: /* memory execute only conforming */
705 case SDT_MEMEAC: /* memory execute only accessed conforming */
706 case SDT_MEMERC: /* memory execute read conforming */
707 case SDT_MEMERAC: /* memory execute read accessed conforming */
708 /* Must be "present" if executable and conforming. */
709 if (dp->sd.sd_p == 0)
712 case SDT_MEMRO: /* memory read only */
713 case SDT_MEMROA: /* memory read only accessed */
714 case SDT_MEMRW: /* memory read write */
715 case SDT_MEMRWA: /* memory read write accessed */
716 case SDT_MEMROD: /* memory read only expand dwn limit */
717 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
718 case SDT_MEMRWD: /* memory read write expand dwn limit */
719 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
720 case SDT_MEME: /* memory execute only */
721 case SDT_MEMEA: /* memory execute only accessed */
722 case SDT_MEMER: /* memory execute read */
723 case SDT_MEMERA: /* memory execute read accessed */
730 /* Only user (ring-3) descriptors may be present. */
731 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
735 if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
736 /* Allocate a free slot */
737 mtx_lock_spin(&dt_lock);
738 if ((pldt = mdp->md_ldt) == NULL) {
739 if ((error = i386_ldt_grow(td, NLDT + 1))) {
740 mtx_unlock_spin(&dt_lock);
747 * start scanning a bit up to leave room for NVidia and
748 * Wine, which still user the "Blat" method of allocation.
750 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
751 for (i = NLDT; i < pldt->ldt_len; ++i) {
752 if (dp->sd.sd_type == SDT_SYSNULL)
756 if (i >= pldt->ldt_len) {
757 if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
758 mtx_unlock_spin(&dt_lock);
764 error = i386_set_ldt_data(td, i, 1, descs);
765 mtx_unlock_spin(&dt_lock);
767 largest_ld = uap->start + uap->num;
768 mtx_lock_spin(&dt_lock);
769 if (!(error = i386_ldt_grow(td, largest_ld))) {
770 error = i386_set_ldt_data(td, uap->start, uap->num,
773 mtx_unlock_spin(&dt_lock);
776 td->td_retval[0] = uap->start;
781 i386_set_ldt_data(struct thread *td, int start, int num,
782 union descriptor *descs)
784 struct mdproc *mdp = &td->td_proc->p_md;
785 struct proc_ldt *pldt = mdp->md_ldt;
787 mtx_assert(&dt_lock, MA_OWNED);
790 xen_update_descriptor(
791 &((union descriptor *)(pldt->ldt_base))[start],
801 i386_set_ldt_data(struct thread *td, int start, int num,
802 union descriptor *descs)
804 struct mdproc *mdp = &td->td_proc->p_md;
805 struct proc_ldt *pldt = mdp->md_ldt;
807 mtx_assert(&dt_lock, MA_OWNED);
811 &((union descriptor *)(pldt->ldt_base))[start],
812 num * sizeof(union descriptor));
818 i386_ldt_grow(struct thread *td, int len)
820 struct mdproc *mdp = &td->td_proc->p_md;
821 struct proc_ldt *new_ldt, *pldt;
822 caddr_t old_ldt_base = NULL_LDT_BASE;
825 mtx_assert(&dt_lock, MA_OWNED);
832 /* Allocate a user ldt. */
833 if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
834 new_ldt = user_ldt_alloc(mdp, len);
840 if (new_ldt->ldt_len <= pldt->ldt_len) {
842 * We just lost the race for allocation, so
843 * free the new object and return.
845 mtx_unlock_spin(&dt_lock);
846 kmem_free(kernel_arena,
847 (vm_offset_t)new_ldt->ldt_base,
848 new_ldt->ldt_len * sizeof(union descriptor));
849 free(new_ldt, M_SUBPROC);
850 mtx_lock_spin(&dt_lock);
855 * We have to substitute the current LDT entry for
856 * curproc with the new one since its size grew.
858 old_ldt_base = pldt->ldt_base;
859 old_ldt_len = pldt->ldt_len;
860 pldt->ldt_sd = new_ldt->ldt_sd;
861 pldt->ldt_base = new_ldt->ldt_base;
862 pldt->ldt_len = new_ldt->ldt_len;
864 mdp->md_ldt = pldt = new_ldt;
867 * Signal other cpus to reload ldt. We need to unlock dt_lock
868 * here because other CPU will contest on it since their
869 * curthreads won't hold the lock and will block when trying
872 mtx_unlock_spin(&dt_lock);
873 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
874 NULL, td->td_proc->p_vmspace);
876 set_user_ldt(&td->td_proc->p_md);
877 mtx_unlock_spin(&dt_lock);
879 if (old_ldt_base != NULL_LDT_BASE) {
880 kmem_free(kernel_arena, (vm_offset_t)old_ldt_base,
881 old_ldt_len * sizeof(union descriptor));
882 free(new_ldt, M_SUBPROC);
884 mtx_lock_spin(&dt_lock);