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 * 3. 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);
91 #ifndef _SYS_SYSPROTO_H_
101 register struct sysarch_args *uap;
104 union descriptor *lp;
106 struct i386_ldt_args largs;
107 struct i386_ioperm_args iargs;
108 struct i386_get_xfpustate xfpu;
111 struct segment_descriptor sd, *sdp;
113 AUDIT_ARG_CMD(uap->op);
115 #ifdef CAPABILITY_MODE
117 * When adding new operations, add a new case statement here to
118 * explicitly indicate whether or not the operation is safe to
119 * perform in capability mode.
121 if (IN_CAPABILITY_MODE(td)) {
125 case I386_GET_IOPERM:
126 case I386_GET_FSBASE:
127 case I386_SET_FSBASE:
128 case I386_GET_GSBASE:
129 case I386_SET_GSBASE:
130 case I386_GET_XFPUSTATE:
133 case I386_SET_IOPERM:
136 if (KTRPOINT(td, KTR_CAPFAIL))
137 ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL);
145 case I386_GET_IOPERM:
146 case I386_SET_IOPERM:
147 if ((error = copyin(uap->parms, &kargs.iargs,
148 sizeof(struct i386_ioperm_args))) != 0)
153 if ((error = copyin(uap->parms, &kargs.largs,
154 sizeof(struct i386_ldt_args))) != 0)
156 if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0)
159 case I386_GET_XFPUSTATE:
160 if ((error = copyin(uap->parms, &kargs.xfpu,
161 sizeof(struct i386_get_xfpustate))) != 0)
170 error = i386_get_ldt(td, &kargs.largs);
173 if (kargs.largs.descs != NULL) {
174 lp = (union descriptor *)malloc(
175 kargs.largs.num * sizeof(union descriptor),
177 error = copyin(kargs.largs.descs, lp,
178 kargs.largs.num * sizeof(union descriptor));
180 error = i386_set_ldt(td, &kargs.largs, lp);
183 error = i386_set_ldt(td, &kargs.largs, NULL);
186 case I386_GET_IOPERM:
187 error = i386_get_ioperm(td, &kargs.iargs);
189 error = copyout(&kargs.iargs, uap->parms,
190 sizeof(struct i386_ioperm_args));
192 case I386_SET_IOPERM:
193 error = i386_set_ioperm(td, &kargs.iargs);
196 error = vm86_sysarch(td, uap->parms);
198 case I386_GET_FSBASE:
199 sdp = &td->td_pcb->pcb_fsd;
200 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
201 error = copyout(&base, uap->parms, sizeof(base));
203 case I386_SET_FSBASE:
204 error = copyin(uap->parms, &base, sizeof(base));
207 * Construct a descriptor and store it in the pcb for
208 * the next context switch. Also store it in the gdt
209 * so that the load of tf_fs into %fs will activate it
210 * at return to userland.
212 sd.sd_lobase = base & 0xffffff;
213 sd.sd_hibase = (base >> 24) & 0xff;
215 /* need to do nosegneg like Linux */
216 sd.sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
218 sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
221 sd.sd_type = SDT_MEMRWA;
228 td->td_pcb->pcb_fsd = sd;
230 HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[0]),
233 PCPU_GET(fsgs_gdt)[0] = sd;
236 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
239 case I386_GET_GSBASE:
240 sdp = &td->td_pcb->pcb_gsd;
241 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
242 error = copyout(&base, uap->parms, sizeof(base));
244 case I386_SET_GSBASE:
245 error = copyin(uap->parms, &base, sizeof(base));
248 * Construct a descriptor and store it in the pcb for
249 * the next context switch. Also store it in the gdt
250 * because we have to do a load_gs() right now.
252 sd.sd_lobase = base & 0xffffff;
253 sd.sd_hibase = (base >> 24) & 0xff;
256 /* need to do nosegneg like Linux */
257 sd.sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
259 sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
262 sd.sd_type = SDT_MEMRWA;
269 td->td_pcb->pcb_gsd = sd;
271 HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[1]),
274 PCPU_GET(fsgs_gdt)[1] = sd;
277 load_gs(GSEL(GUGS_SEL, SEL_UPL));
280 case I386_GET_XFPUSTATE:
281 if (kargs.xfpu.len > cpu_max_ext_state_size -
282 sizeof(union savefpu))
285 error = copyout((char *)(get_pcb_user_save_td(td) + 1),
286 kargs.xfpu.addr, kargs.xfpu.len);
296 i386_extend_pcb(struct thread *td)
301 struct soft_segment_descriptor ssd = {
302 0, /* segment base address (overwritten) */
303 ctob(IOPAGES + 1) - 1, /* length */
304 SDT_SYS386TSS, /* segment type */
305 0, /* priority level */
306 1, /* descriptor present */
308 0, /* default 32 size */
312 ext = (struct pcb_ext *)kmem_malloc(kernel_arena, ctob(IOPAGES+1),
314 /* -16 is so we can convert a trapframe into vm86trapframe inplace */
315 ext->ext_tss.tss_esp0 = td->td_kstack + ctob(KSTACK_PAGES) -
316 sizeof(struct pcb) - 16;
317 ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
319 * The last byte of the i/o map must be followed by an 0xff byte.
320 * We arbitrarily allocate 16 bytes here, to keep the starting
321 * address on a doubleword boundary.
323 offset = PAGE_SIZE - 16;
324 ext->ext_tss.tss_ioopt =
325 (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
326 ext->ext_iomap = (caddr_t)ext + offset;
327 ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
329 addr = (u_long *)ext->ext_vm86.vm86_intmap;
330 for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
333 ssd.ssd_base = (unsigned)&ext->ext_tss;
334 ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
335 ssdtosd(&ssd, &ext->ext_tssd);
337 KASSERT(td == curthread, ("giving TSS to !curthread"));
338 KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
340 /* Switch to the new TSS. */
342 td->td_pcb->pcb_ext = ext;
343 PCPU_SET(private_tss, 1);
344 *PCPU_GET(tss_gdt) = ext->ext_tssd;
345 ltr(GSEL(GPROC0_SEL, SEL_KPL));
352 i386_set_ioperm(td, uap)
354 struct i386_ioperm_args *uap;
359 if ((error = priv_check(td, PRIV_IO)) != 0)
361 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
365 * While this is restricted to root, we should probably figure out
366 * whether any other driver is using this i/o address, as so not to
367 * cause confusion. This probably requires a global 'usage registry'.
370 if (td->td_pcb->pcb_ext == 0)
371 if ((error = i386_extend_pcb(td)) != 0)
373 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
375 if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
378 for (i = uap->start; i < uap->start + uap->length; i++) {
380 iomap[i >> 3] &= ~(1 << (i & 7));
382 iomap[i >> 3] |= (1 << (i & 7));
388 i386_get_ioperm(td, uap)
390 struct i386_ioperm_args *uap;
395 if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
398 if (td->td_pcb->pcb_ext == 0) {
403 iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
406 state = (iomap[i >> 3] >> (i & 7)) & 1;
407 uap->enable = !state;
410 for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
411 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
421 * Update the GDT entry pointing to the LDT to point to the LDT of the
422 * current process. Manage dt_lock holding/unholding autonomously.
425 set_user_ldt(struct mdproc *mdp)
427 struct proc_ldt *pldt;
431 if (!mtx_owned(&dt_lock)) {
432 mtx_lock_spin(&dt_lock);
438 i386_reset_ldt(pldt);
439 PCPU_SET(currentldt, (int)pldt);
442 gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
444 gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
446 lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
447 PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
450 mtx_unlock_spin(&dt_lock);
455 set_user_ldt_rv(struct vmspace *vmsp)
460 if (vmsp != td->td_proc->p_vmspace)
463 set_user_ldt(&td->td_proc->p_md);
470 * dt_lock must be held. Returns with dt_lock held.
473 user_ldt_alloc(struct mdproc *mdp, int len)
475 struct proc_ldt *pldt, *new_ldt;
477 mtx_assert(&dt_lock, MA_OWNED);
478 mtx_unlock_spin(&dt_lock);
479 new_ldt = malloc(sizeof(struct proc_ldt),
480 M_SUBPROC, M_WAITOK);
482 new_ldt->ldt_len = len = NEW_MAX_LD(len);
483 new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
484 round_page(len * sizeof(union descriptor)), M_WAITOK);
485 new_ldt->ldt_refcnt = 1;
486 new_ldt->ldt_active = 0;
488 mtx_lock_spin(&dt_lock);
489 if ((pldt = mdp->md_ldt)) {
490 if (len > pldt->ldt_len)
492 bcopy(pldt->ldt_base, new_ldt->ldt_base,
493 len * sizeof(union descriptor));
495 bcopy(ldt, new_ldt->ldt_base, PAGE_SIZE);
497 mtx_unlock_spin(&dt_lock); /* XXX kill once pmap locking fixed. */
498 pmap_map_readonly(kernel_pmap, (vm_offset_t)new_ldt->ldt_base,
499 new_ldt->ldt_len*sizeof(union descriptor));
500 mtx_lock_spin(&dt_lock); /* XXX kill once pmap locking fixed. */
505 * dt_lock must be held. Returns with dt_lock held.
508 user_ldt_alloc(struct mdproc *mdp, int len)
510 struct proc_ldt *pldt, *new_ldt;
512 mtx_assert(&dt_lock, MA_OWNED);
513 mtx_unlock_spin(&dt_lock);
514 new_ldt = malloc(sizeof(struct proc_ldt),
515 M_SUBPROC, M_WAITOK);
517 new_ldt->ldt_len = len = NEW_MAX_LD(len);
518 new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
519 len * sizeof(union descriptor), M_WAITOK);
520 new_ldt->ldt_refcnt = 1;
521 new_ldt->ldt_active = 0;
523 mtx_lock_spin(&dt_lock);
524 gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
525 gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
526 ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
528 if ((pldt = mdp->md_ldt) != NULL) {
529 if (len > pldt->ldt_len)
531 bcopy(pldt->ldt_base, new_ldt->ldt_base,
532 len * sizeof(union descriptor));
534 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
541 * Must be called with dt_lock held. Returns with dt_lock unheld.
544 user_ldt_free(struct thread *td)
546 struct mdproc *mdp = &td->td_proc->p_md;
547 struct proc_ldt *pldt;
549 mtx_assert(&dt_lock, MA_OWNED);
550 if ((pldt = mdp->md_ldt) == NULL) {
551 mtx_unlock_spin(&dt_lock);
555 if (td == curthread) {
557 i386_reset_ldt(&default_proc_ldt);
558 PCPU_SET(currentldt, (int)&default_proc_ldt);
561 PCPU_SET(currentldt, _default_ldt);
566 user_ldt_deref(pldt);
570 user_ldt_deref(struct proc_ldt *pldt)
573 mtx_assert(&dt_lock, MA_OWNED);
574 if (--pldt->ldt_refcnt == 0) {
575 mtx_unlock_spin(&dt_lock);
576 kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base,
577 pldt->ldt_len * sizeof(union descriptor));
578 free(pldt, M_SUBPROC);
580 mtx_unlock_spin(&dt_lock);
584 * Note for the authors of compat layers (linux, etc): copyout() in
585 * the function below is not a problem since it presents data in
586 * arch-specific format (i.e. i386-specific in this case), not in
587 * the OS-specific one.
590 i386_get_ldt(td, uap)
592 struct i386_ldt_args *uap;
595 struct proc_ldt *pldt;
597 union descriptor *lp;
600 printf("i386_get_ldt: start=%d num=%d descs=%p\n",
601 uap->start, uap->num, (void *)uap->descs);
604 mtx_lock_spin(&dt_lock);
605 if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
606 nldt = pldt->ldt_len;
607 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
608 mtx_unlock_spin(&dt_lock);
609 num = min(uap->num, nldt);
611 mtx_unlock_spin(&dt_lock);
612 nldt = sizeof(ldt)/sizeof(ldt[0]);
613 num = min(uap->num, nldt);
614 lp = &ldt[uap->start];
617 if ((uap->start > (unsigned int)nldt) ||
618 ((unsigned int)num > (unsigned int)nldt) ||
619 ((unsigned int)(uap->start + num) > (unsigned int)nldt))
622 error = copyout(lp, uap->descs, num * sizeof(union descriptor));
624 td->td_retval[0] = num;
630 i386_set_ldt(td, uap, descs)
632 struct i386_ldt_args *uap;
633 union descriptor *descs;
637 struct mdproc *mdp = &td->td_proc->p_md;
638 struct proc_ldt *pldt;
639 union descriptor *dp;
642 printf("i386_set_ldt: start=%d num=%d descs=%p\n",
643 uap->start, uap->num, (void *)uap->descs);
647 /* Free descriptors */
648 if (uap->start == 0 && uap->num == 0) {
650 * Treat this as a special case, so userland needn't
651 * know magic number NLDT.
654 uap->num = MAX_LD - NLDT;
658 mtx_lock_spin(&dt_lock);
659 if ((pldt = mdp->md_ldt) == NULL ||
660 uap->start >= pldt->ldt_len) {
661 mtx_unlock_spin(&dt_lock);
664 largest_ld = uap->start + uap->num;
665 if (largest_ld > pldt->ldt_len)
666 largest_ld = pldt->ldt_len;
667 i = largest_ld - uap->start;
668 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
669 sizeof(union descriptor) * i);
670 mtx_unlock_spin(&dt_lock);
674 if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
675 /* verify range of descriptors to modify */
676 largest_ld = uap->start + uap->num;
677 if (uap->start >= MAX_LD || largest_ld > MAX_LD) {
682 /* Check descriptors for access violations */
683 for (i = 0; i < uap->num; i++) {
686 switch (dp->sd.sd_type) {
687 case SDT_SYSNULL: /* system null */
690 case SDT_SYS286TSS: /* system 286 TSS available */
691 case SDT_SYSLDT: /* system local descriptor table */
692 case SDT_SYS286BSY: /* system 286 TSS busy */
693 case SDT_SYSTASKGT: /* system task gate */
694 case SDT_SYS286IGT: /* system 286 interrupt gate */
695 case SDT_SYS286TGT: /* system 286 trap gate */
696 case SDT_SYSNULL2: /* undefined by Intel */
697 case SDT_SYS386TSS: /* system 386 TSS available */
698 case SDT_SYSNULL3: /* undefined by Intel */
699 case SDT_SYS386BSY: /* system 386 TSS busy */
700 case SDT_SYSNULL4: /* undefined by Intel */
701 case SDT_SYS386IGT: /* system 386 interrupt gate */
702 case SDT_SYS386TGT: /* system 386 trap gate */
703 case SDT_SYS286CGT: /* system 286 call gate */
704 case SDT_SYS386CGT: /* system 386 call gate */
705 /* I can't think of any reason to allow a user proc
706 * to create a segment of these types. They are
712 /* memory segment types */
713 case SDT_MEMEC: /* memory execute only conforming */
714 case SDT_MEMEAC: /* memory execute only accessed conforming */
715 case SDT_MEMERC: /* memory execute read conforming */
716 case SDT_MEMERAC: /* memory execute read accessed conforming */
717 /* Must be "present" if executable and conforming. */
718 if (dp->sd.sd_p == 0)
721 case SDT_MEMRO: /* memory read only */
722 case SDT_MEMROA: /* memory read only accessed */
723 case SDT_MEMRW: /* memory read write */
724 case SDT_MEMRWA: /* memory read write accessed */
725 case SDT_MEMROD: /* memory read only expand dwn limit */
726 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
727 case SDT_MEMRWD: /* memory read write expand dwn limit */
728 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
729 case SDT_MEME: /* memory execute only */
730 case SDT_MEMEA: /* memory execute only accessed */
731 case SDT_MEMER: /* memory execute read */
732 case SDT_MEMERA: /* memory execute read accessed */
739 /* Only user (ring-3) descriptors may be present. */
740 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
744 if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
745 /* Allocate a free slot */
746 mtx_lock_spin(&dt_lock);
747 if ((pldt = mdp->md_ldt) == NULL) {
748 if ((error = i386_ldt_grow(td, NLDT + 1))) {
749 mtx_unlock_spin(&dt_lock);
756 * start scanning a bit up to leave room for NVidia and
757 * Wine, which still user the "Blat" method of allocation.
759 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
760 for (i = NLDT; i < pldt->ldt_len; ++i) {
761 if (dp->sd.sd_type == SDT_SYSNULL)
765 if (i >= pldt->ldt_len) {
766 if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
767 mtx_unlock_spin(&dt_lock);
773 error = i386_set_ldt_data(td, i, 1, descs);
774 mtx_unlock_spin(&dt_lock);
776 largest_ld = uap->start + uap->num;
777 mtx_lock_spin(&dt_lock);
778 if (!(error = i386_ldt_grow(td, largest_ld))) {
779 error = i386_set_ldt_data(td, uap->start, uap->num,
782 mtx_unlock_spin(&dt_lock);
785 td->td_retval[0] = uap->start;
790 i386_set_ldt_data(struct thread *td, int start, int num,
791 union descriptor *descs)
793 struct mdproc *mdp = &td->td_proc->p_md;
794 struct proc_ldt *pldt = mdp->md_ldt;
796 mtx_assert(&dt_lock, MA_OWNED);
799 xen_update_descriptor(
800 &((union descriptor *)(pldt->ldt_base))[start],
810 i386_set_ldt_data(struct thread *td, int start, int num,
811 union descriptor *descs)
813 struct mdproc *mdp = &td->td_proc->p_md;
814 struct proc_ldt *pldt = mdp->md_ldt;
816 mtx_assert(&dt_lock, MA_OWNED);
820 &((union descriptor *)(pldt->ldt_base))[start],
821 num * sizeof(union descriptor));
827 i386_ldt_grow(struct thread *td, int len)
829 struct mdproc *mdp = &td->td_proc->p_md;
830 struct proc_ldt *new_ldt, *pldt;
831 caddr_t old_ldt_base = NULL_LDT_BASE;
834 mtx_assert(&dt_lock, MA_OWNED);
841 /* Allocate a user ldt. */
842 if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
843 new_ldt = user_ldt_alloc(mdp, len);
849 if (new_ldt->ldt_len <= pldt->ldt_len) {
851 * We just lost the race for allocation, so
852 * free the new object and return.
854 mtx_unlock_spin(&dt_lock);
855 kmem_free(kernel_arena,
856 (vm_offset_t)new_ldt->ldt_base,
857 new_ldt->ldt_len * sizeof(union descriptor));
858 free(new_ldt, M_SUBPROC);
859 mtx_lock_spin(&dt_lock);
864 * We have to substitute the current LDT entry for
865 * curproc with the new one since its size grew.
867 old_ldt_base = pldt->ldt_base;
868 old_ldt_len = pldt->ldt_len;
869 pldt->ldt_sd = new_ldt->ldt_sd;
870 pldt->ldt_base = new_ldt->ldt_base;
871 pldt->ldt_len = new_ldt->ldt_len;
873 mdp->md_ldt = pldt = new_ldt;
876 * Signal other cpus to reload ldt. We need to unlock dt_lock
877 * here because other CPU will contest on it since their
878 * curthreads won't hold the lock and will block when trying
881 mtx_unlock_spin(&dt_lock);
882 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
883 NULL, td->td_proc->p_vmspace);
885 set_user_ldt(&td->td_proc->p_md);
886 mtx_unlock_spin(&dt_lock);
888 if (old_ldt_base != NULL_LDT_BASE) {
889 kmem_free(kernel_arena, (vm_offset_t)old_ldt_base,
890 old_ldt_len * sizeof(union descriptor));
891 free(new_ldt, M_SUBPROC);
893 mtx_lock_spin(&dt_lock);