]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/amd64/amd64/sys_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / amd64 / amd64 / sys_machdep.c
1 /*-
2  * Copyright (c) 2003 Peter Wemm.
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/sysproto.h>
44 #include <sys/uio.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_kern.h>         /* for kernel_map */
49 #include <vm/vm_extern.h>
50
51 #include <machine/frame.h>
52 #include <machine/md_var.h>
53 #include <machine/pcb.h>
54 #include <machine/specialreg.h>
55 #include <machine/sysarch.h>
56 #include <machine/tss.h>
57 #include <machine/vmparam.h>
58
59 #include <security/audit/audit.h>
60
61 int max_ldt_segment = 1024;
62 #define LD_PER_PAGE 512
63 #define NULL_LDT_BASE   ((caddr_t)NULL)
64
65 #ifdef notyet
66 #ifdef SMP
67 static void set_user_ldt_rv(struct vmspace *vmsp);
68 #endif
69 #endif
70 static void user_ldt_derefl(struct proc_ldt *pldt);
71
72 #ifndef _SYS_SYSPROTO_H_
73 struct sysarch_args {
74         int op;
75         char *parms;
76 };
77 #endif
78
79 int
80 sysarch_ldt(struct thread *td, struct sysarch_args *uap, int uap_space)
81 {
82         struct i386_ldt_args *largs, la;
83         struct user_segment_descriptor *lp;
84         int error = 0;
85
86         /*
87          * XXXKIB check that the BSM generation code knows to encode
88          * the op argument.
89          */
90         AUDIT_ARG_CMD(uap->op);
91         if (uap_space == UIO_USERSPACE) {
92                 error = copyin(uap->parms, &la, sizeof(struct i386_ldt_args));
93                 if (error != 0)
94                         return (error);
95                 largs = &la;
96         } else
97                 largs = (struct i386_ldt_args *)uap->parms;
98         if (largs->num > max_ldt_segment || largs->num <= 0)
99                 return (EINVAL);
100
101         switch (uap->op) {
102         case I386_GET_LDT:
103                 error = amd64_get_ldt(td, largs);
104                 break;
105         case I386_SET_LDT:
106                 td->td_pcb->pcb_full_iret = 1;
107                 if (largs->descs != NULL) {
108                         lp = (struct user_segment_descriptor *)
109                             kmem_alloc(kernel_map, largs->num *
110                             sizeof(struct user_segment_descriptor));
111                         if (lp == NULL) {
112                                 error = ENOMEM;
113                                 break;
114                         }
115                         error = copyin(largs->descs, lp, largs->num *
116                             sizeof(struct user_segment_descriptor));
117                         if (error == 0)
118                                 error = amd64_set_ldt(td, largs, lp);
119                         kmem_free(kernel_map, (vm_offset_t)lp, largs->num *
120                             sizeof(struct user_segment_descriptor));
121                 } else {
122                         error = amd64_set_ldt(td, largs, NULL);
123                 }
124                 break;
125         }
126         return (error);
127 }
128
129 void
130 update_gdt_gsbase(struct thread *td, uint32_t base)
131 {
132         struct user_segment_descriptor *sd;
133
134         if (td != curthread)
135                 return;
136         td->td_pcb->pcb_full_iret = 1;
137         critical_enter();
138         sd = PCPU_GET(gs32p);
139         sd->sd_lobase = base & 0xffffff;
140         sd->sd_hibase = (base >> 24) & 0xff;
141         critical_exit();
142 }
143
144 void
145 update_gdt_fsbase(struct thread *td, uint32_t base)
146 {
147         struct user_segment_descriptor *sd;
148
149         if (td != curthread)
150                 return;
151         td->td_pcb->pcb_full_iret = 1;
152         critical_enter();
153         sd = PCPU_GET(fs32p);
154         sd->sd_lobase = base & 0xffffff;
155         sd->sd_hibase = (base >> 24) & 0xff;
156         critical_exit();
157 }
158
159 int
160 sysarch(td, uap)
161         struct thread *td;
162         register struct sysarch_args *uap;
163 {
164         int error = 0;
165         struct pcb *pcb = curthread->td_pcb;
166         uint32_t i386base;
167         uint64_t a64base;
168         struct i386_ioperm_args iargs;
169
170         if (uap->op == I386_GET_LDT || uap->op == I386_SET_LDT)
171                 return (sysarch_ldt(td, uap, UIO_USERSPACE));
172         /*
173          * XXXKIB check that the BSM generation code knows to encode
174          * the op argument.
175          */
176         AUDIT_ARG_CMD(uap->op);
177         switch (uap->op) {
178         case I386_GET_IOPERM:
179         case I386_SET_IOPERM:
180                 if ((error = copyin(uap->parms, &iargs,
181                     sizeof(struct i386_ioperm_args))) != 0)
182                         return (error);
183                 break;
184         default:
185                 break;
186         }
187
188         switch (uap->op) {
189         case I386_GET_IOPERM:
190                 error = amd64_get_ioperm(td, &iargs);
191                 if (error == 0)
192                         error = copyout(&iargs, uap->parms,
193                             sizeof(struct i386_ioperm_args));
194                 break;
195         case I386_SET_IOPERM:
196                 error = amd64_set_ioperm(td, &iargs);
197                 break;
198         case I386_GET_FSBASE:
199                 i386base = pcb->pcb_fsbase;
200                 error = copyout(&i386base, uap->parms, sizeof(i386base));
201                 break;
202         case I386_SET_FSBASE:
203                 error = copyin(uap->parms, &i386base, sizeof(i386base));
204                 if (!error) {
205                         pcb->pcb_fsbase = i386base;
206                         td->td_frame->tf_fs = _ufssel;
207                         pcb->pcb_full_iret = 1;
208                         update_gdt_fsbase(td, i386base);
209                 }
210                 break;
211         case I386_GET_GSBASE:
212                 i386base = pcb->pcb_gsbase;
213                 error = copyout(&i386base, uap->parms, sizeof(i386base));
214                 break;
215         case I386_SET_GSBASE:
216                 error = copyin(uap->parms, &i386base, sizeof(i386base));
217                 if (!error) {
218                         pcb->pcb_gsbase = i386base;
219                         pcb->pcb_full_iret = 1;
220                         td->td_frame->tf_gs = _ugssel;
221                         update_gdt_gsbase(td, i386base);
222                 }
223                 break;
224         case AMD64_GET_FSBASE:
225                 error = copyout(&pcb->pcb_fsbase, uap->parms, sizeof(pcb->pcb_fsbase));
226                 break;
227                 
228         case AMD64_SET_FSBASE:
229                 error = copyin(uap->parms, &a64base, sizeof(a64base));
230                 if (!error) {
231                         if (a64base < VM_MAXUSER_ADDRESS) {
232                                 pcb->pcb_fsbase = a64base;
233                                 pcb->pcb_full_iret = 1;
234                                 td->td_frame->tf_fs = _ufssel;
235                         } else
236                                 error = EINVAL;
237                 }
238                 break;
239
240         case AMD64_GET_GSBASE:
241                 error = copyout(&pcb->pcb_gsbase, uap->parms, sizeof(pcb->pcb_gsbase));
242                 break;
243
244         case AMD64_SET_GSBASE:
245                 error = copyin(uap->parms, &a64base, sizeof(a64base));
246                 if (!error) {
247                         if (a64base < VM_MAXUSER_ADDRESS) {
248                                 pcb->pcb_gsbase = a64base;
249                                 pcb->pcb_full_iret = 1;
250                                 td->td_frame->tf_gs = _ugssel;
251                         } else
252                                 error = EINVAL;
253                 }
254                 break;
255
256         default:
257                 error = EINVAL;
258                 break;
259         }
260         return (error);
261 }
262
263 int
264 amd64_set_ioperm(td, uap)
265         struct thread *td;
266         struct i386_ioperm_args *uap;
267 {
268         int i, error;
269         char *iomap;
270         struct amd64tss *tssp;
271         struct system_segment_descriptor *tss_sd;
272         u_long *addr;
273         struct pcb *pcb;
274
275         if ((error = priv_check(td, PRIV_IO)) != 0)
276                 return (error);
277         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
278                 return (error);
279         if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
280                 return (EINVAL);
281
282         /*
283          * XXX
284          * While this is restricted to root, we should probably figure out
285          * whether any other driver is using this i/o address, as so not to
286          * cause confusion.  This probably requires a global 'usage registry'.
287          */
288         pcb = td->td_pcb;
289         if (pcb->pcb_tssp == NULL) {
290                 tssp = (struct amd64tss *)kmem_alloc(kernel_map,
291                     ctob(IOPAGES+1));
292                 if (tssp == NULL)
293                         return (ENOMEM);
294                 iomap = (char *)&tssp[1];
295                 addr = (u_long *)iomap;
296                 for (i = 0; i < (ctob(IOPAGES) + 1) / sizeof(u_long); i++)
297                         *addr++ = ~0;
298                 critical_enter();
299                 /* Takes care of tss_rsp0. */
300                 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
301                     sizeof(struct amd64tss));
302                 tssp->tss_iobase = sizeof(*tssp);
303                 pcb->pcb_tssp = tssp;
304                 tss_sd = PCPU_GET(tss);
305                 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
306                 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
307                 tss_sd->sd_type = SDT_SYSTSS;
308                 ltr(GSEL(GPROC0_SEL, SEL_KPL));
309                 PCPU_SET(tssp, tssp);
310                 critical_exit();
311         } else
312                 iomap = (char *)&pcb->pcb_tssp[1];
313         for (i = uap->start; i < uap->start + uap->length; i++) {
314                 if (uap->enable)
315                         iomap[i >> 3] &= ~(1 << (i & 7));
316                 else
317                         iomap[i >> 3] |= (1 << (i & 7));
318         }
319         return (error);
320 }
321
322 int
323 amd64_get_ioperm(td, uap)
324         struct thread *td;
325         struct i386_ioperm_args *uap;
326 {
327         int i, state;
328         char *iomap;
329
330         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
331                 return (EINVAL);
332         if (td->td_pcb->pcb_tssp == NULL) {
333                 uap->length = 0;
334                 goto done;
335         }
336
337         iomap = (char *)&td->td_pcb->pcb_tssp[1];
338
339         i = uap->start;
340         state = (iomap[i >> 3] >> (i & 7)) & 1;
341         uap->enable = !state;
342         uap->length = 1;
343
344         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
345                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
346                         break;
347                 uap->length++;
348         }
349
350 done:
351         return (0);
352 }
353
354 /*
355  * Update the GDT entry pointing to the LDT to point to the LDT of the
356  * current process.
357  */
358 void
359 set_user_ldt(struct mdproc *mdp)
360 {
361
362         critical_enter();
363         *PCPU_GET(ldt) = mdp->md_ldt_sd;
364         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
365         critical_exit();
366 }
367
368 #ifdef notyet
369 #ifdef SMP
370 static void
371 set_user_ldt_rv(struct vmspace *vmsp)
372 {
373         struct thread *td;
374
375         td = curthread;
376         if (vmsp != td->td_proc->p_vmspace)
377                 return;
378
379         set_user_ldt(&td->td_proc->p_md);
380 }
381 #endif
382 #endif
383
384 struct proc_ldt *
385 user_ldt_alloc(struct proc *p, int force)
386 {
387         struct proc_ldt *pldt, *new_ldt;
388         struct mdproc *mdp;
389         struct soft_segment_descriptor sldt;
390
391         mtx_assert(&dt_lock, MA_OWNED);
392         mdp = &p->p_md;
393         if (!force && mdp->md_ldt != NULL)
394                 return (mdp->md_ldt);
395         mtx_unlock(&dt_lock);
396         new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
397         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
398              max_ldt_segment * sizeof(struct user_segment_descriptor));
399         if (new_ldt->ldt_base == NULL) {
400                 FREE(new_ldt, M_SUBPROC);
401                 mtx_lock(&dt_lock);
402                 return (NULL);
403         }
404         new_ldt->ldt_refcnt = 1;
405         sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
406         sldt.ssd_limit = max_ldt_segment *
407             sizeof(struct user_segment_descriptor) - 1;
408         sldt.ssd_type = SDT_SYSLDT;
409         sldt.ssd_dpl = SEL_KPL;
410         sldt.ssd_p = 1;
411         sldt.ssd_long = 0;
412         sldt.ssd_def32 = 0;
413         sldt.ssd_gran = 0;
414         mtx_lock(&dt_lock);
415         pldt = mdp->md_ldt;
416         if (pldt != NULL && !force) {
417                 kmem_free(kernel_map, (vm_offset_t)new_ldt->ldt_base,
418                     max_ldt_segment * sizeof(struct user_segment_descriptor));
419                 free(new_ldt, M_SUBPROC);
420                 return (pldt);
421         }
422
423         if (pldt != NULL) {
424                 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
425                     sizeof(struct user_segment_descriptor));
426                 user_ldt_derefl(pldt);
427         }
428         ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
429         atomic_store_rel_ptr((volatile uintptr_t *)&mdp->md_ldt,
430             (uintptr_t)new_ldt);
431         if (p == curproc)
432                 set_user_ldt(mdp);
433
434         return (mdp->md_ldt);
435 }
436
437 void
438 user_ldt_free(struct thread *td)
439 {
440         struct proc *p = td->td_proc;
441         struct mdproc *mdp = &p->p_md;
442         struct proc_ldt *pldt;
443
444         mtx_assert(&dt_lock, MA_OWNED);
445         if ((pldt = mdp->md_ldt) == NULL) {
446                 mtx_unlock(&dt_lock);
447                 return;
448         }
449
450         mdp->md_ldt = NULL;
451         bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
452         if (td == curthread)
453                 lldt(GSEL(GNULL_SEL, SEL_KPL));
454         user_ldt_deref(pldt);
455 }
456
457 static void
458 user_ldt_derefl(struct proc_ldt *pldt)
459 {
460
461         if (--pldt->ldt_refcnt == 0) {
462                 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
463                     max_ldt_segment * sizeof(struct user_segment_descriptor));
464                 free(pldt, M_SUBPROC);
465         }
466 }
467
468 void
469 user_ldt_deref(struct proc_ldt *pldt)
470 {
471
472         mtx_assert(&dt_lock, MA_OWNED);
473         user_ldt_derefl(pldt);
474         mtx_unlock(&dt_lock);
475 }
476
477 /*
478  * Note for the authors of compat layers (linux, etc): copyout() in
479  * the function below is not a problem since it presents data in
480  * arch-specific format (i.e. i386-specific in this case), not in
481  * the OS-specific one.
482  */
483 int
484 amd64_get_ldt(td, uap)
485         struct thread *td;
486         struct i386_ldt_args *uap;
487 {
488         int error = 0;
489         struct proc_ldt *pldt;
490         int num;
491         struct user_segment_descriptor *lp;
492
493 #ifdef  DEBUG
494         printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
495             uap->start, uap->num, (void *)uap->descs);
496 #endif
497
498         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
499                 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
500                     [uap->start];
501                 num = min(uap->num, max_ldt_segment);
502         } else
503                 return (EINVAL);
504
505         if ((uap->start > (unsigned int)max_ldt_segment) ||
506             ((unsigned int)num > (unsigned int)max_ldt_segment) ||
507             ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
508                 return(EINVAL);
509
510         error = copyout(lp, uap->descs, num *
511             sizeof(struct user_segment_descriptor));
512         if (!error)
513                 td->td_retval[0] = num;
514
515         return(error);
516 }
517
518 int
519 amd64_set_ldt(td, uap, descs)
520         struct thread *td;
521         struct i386_ldt_args *uap;
522         struct user_segment_descriptor *descs;
523 {
524         int error = 0, i;
525         int largest_ld;
526         struct mdproc *mdp = &td->td_proc->p_md;
527         struct proc_ldt *pldt;
528         struct user_segment_descriptor *dp;
529         struct proc *p;
530
531 #ifdef  DEBUG
532         printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
533             uap->start, uap->num, (void *)uap->descs);
534 #endif
535
536         td->td_pcb->pcb_full_iret = 1;
537         p = td->td_proc;
538         if (descs == NULL) {
539                 /* Free descriptors */
540                 if (uap->start == 0 && uap->num == 0)
541                         uap->num = max_ldt_segment;
542                 if (uap->num <= 0)
543                         return (EINVAL);
544                 if ((pldt = mdp->md_ldt) == NULL ||
545                     uap->start >= max_ldt_segment)
546                         return (0);
547                 largest_ld = uap->start + uap->num;
548                 if (largest_ld > max_ldt_segment)
549                         largest_ld = max_ldt_segment;
550                 i = largest_ld - uap->start;
551                 mtx_lock(&dt_lock);
552                 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
553                     [uap->start], sizeof(struct user_segment_descriptor) * i);
554                 mtx_unlock(&dt_lock);
555                 return (0);
556         }
557
558         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
559                 /* verify range of descriptors to modify */
560                 largest_ld = uap->start + uap->num;
561                 if (uap->start >= max_ldt_segment ||
562                     uap->num < 0 || largest_ld > max_ldt_segment)
563                         return (EINVAL);
564         }
565
566         /* Check descriptors for access violations */
567         for (i = 0; i < uap->num; i++) {
568                 dp = &descs[i];
569
570                 switch (dp->sd_type) {
571                 case SDT_SYSNULL:       /* system null */
572                         dp->sd_p = 0;
573                         break;
574                 case SDT_SYS286TSS:
575                 case SDT_SYSLDT:
576                 case SDT_SYS286BSY:
577                 case SDT_SYS286CGT:
578                 case SDT_SYSTASKGT:
579                 case SDT_SYS286IGT:
580                 case SDT_SYS286TGT:
581                 case SDT_SYSNULL2:
582                 case SDT_SYSTSS:
583                 case SDT_SYSNULL3:
584                 case SDT_SYSBSY:
585                 case SDT_SYSCGT:
586                 case SDT_SYSNULL4:
587                 case SDT_SYSIGT:
588                 case SDT_SYSTGT:
589                         /* I can't think of any reason to allow a user proc
590                          * to create a segment of these types.  They are
591                          * for OS use only.
592                          */
593                         return (EACCES);
594                         /*NOTREACHED*/
595
596                 /* memory segment types */
597                 case SDT_MEMEC:   /* memory execute only conforming */
598                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
599                 case SDT_MEMERC:  /* memory execute read conforming */
600                 case SDT_MEMERAC: /* memory execute read accessed conforming */
601                          /* Must be "present" if executable and conforming. */
602                         if (dp->sd_p == 0)
603                                 return (EACCES);
604                         break;
605                 case SDT_MEMRO:   /* memory read only */
606                 case SDT_MEMROA:  /* memory read only accessed */
607                 case SDT_MEMRW:   /* memory read write */
608                 case SDT_MEMRWA:  /* memory read write accessed */
609                 case SDT_MEMROD:  /* memory read only expand dwn limit */
610                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
611                 case SDT_MEMRWD:  /* memory read write expand dwn limit */
612                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
613                 case SDT_MEME:    /* memory execute only */
614                 case SDT_MEMEA:   /* memory execute only accessed */
615                 case SDT_MEMER:   /* memory execute read */
616                 case SDT_MEMERA:  /* memory execute read accessed */
617                         break;
618                 default:
619                         return(EINVAL);
620                         /*NOTREACHED*/
621                 }
622
623                 /* Only user (ring-3) descriptors may be present. */
624                 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
625                         return (EACCES);
626         }
627
628         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
629                 /* Allocate a free slot */
630                 mtx_lock(&dt_lock);
631                 pldt = user_ldt_alloc(p, 0);
632                 if (pldt == NULL) {
633                         mtx_unlock(&dt_lock);
634                         return (ENOMEM);
635                 }
636
637                 /*
638                  * start scanning a bit up to leave room for NVidia and
639                  * Wine, which still user the "Blat" method of allocation.
640                  */
641                 i = 16;
642                 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
643                 for (; i < max_ldt_segment; ++i, ++dp) {
644                         if (dp->sd_type == SDT_SYSNULL)
645                                 break;
646                 }
647                 if (i >= max_ldt_segment) {
648                         mtx_unlock(&dt_lock);
649                         return (ENOSPC);
650                 }
651                 uap->start = i;
652                 error = amd64_set_ldt_data(td, i, 1, descs);
653                 mtx_unlock(&dt_lock);
654         } else {
655                 largest_ld = uap->start + uap->num;
656                 if (largest_ld > max_ldt_segment)
657                         return (EINVAL);
658                 mtx_lock(&dt_lock);
659                 if (user_ldt_alloc(p, 0) != NULL) {
660                         error = amd64_set_ldt_data(td, uap->start, uap->num,
661                             descs);
662                 }
663                 mtx_unlock(&dt_lock);
664         }
665         if (error == 0)
666                 td->td_retval[0] = uap->start;
667         return (error);
668 }
669
670 int
671 amd64_set_ldt_data(struct thread *td, int start, int num,
672     struct user_segment_descriptor *descs)
673 {
674         struct mdproc *mdp = &td->td_proc->p_md;
675         struct proc_ldt *pldt = mdp->md_ldt;
676
677         mtx_assert(&dt_lock, MA_OWNED);
678
679         /* Fill in range */
680         bcopy(descs,
681             &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
682             num * sizeof(struct user_segment_descriptor));
683         return (0);
684 }