]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/sys_machdep.c
Make WRFSBASE and WRGSBASE instructions functional.
[FreeBSD/FreeBSD.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  * 3. 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 "opt_capsicum.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/capsicum.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/sysproto.h>
48 #include <sys/uio.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_kern.h>         /* for kernel_map */
53 #include <vm/vm_extern.h>
54
55 #include <machine/frame.h>
56 #include <machine/md_var.h>
57 #include <machine/pcb.h>
58 #include <machine/specialreg.h>
59 #include <machine/sysarch.h>
60 #include <machine/tss.h>
61 #include <machine/vmparam.h>
62
63 #include <security/audit/audit.h>
64
65 #define MAX_LD          8192
66
67 int max_ldt_segment = 1024;
68 SYSCTL_INT(_machdep, OID_AUTO, max_ldt_segment, CTLFLAG_RDTUN,
69     &max_ldt_segment, 0,
70     "Maximum number of allowed LDT segments in the single address space");
71
72 static void
73 max_ldt_segment_init(void *arg __unused)
74 {
75
76         if (max_ldt_segment <= 0)
77                 max_ldt_segment = 1;
78         if (max_ldt_segment > MAX_LD)
79                 max_ldt_segment = MAX_LD;
80 }
81 SYSINIT(maxldt, SI_SUB_VM_CONF, SI_ORDER_ANY, max_ldt_segment_init, NULL);
82
83 #ifdef notyet
84 #ifdef SMP
85 static void set_user_ldt_rv(struct vmspace *vmsp);
86 #endif
87 #endif
88 static void user_ldt_derefl(struct proc_ldt *pldt);
89
90 #ifndef _SYS_SYSPROTO_H_
91 struct sysarch_args {
92         int op;
93         char *parms;
94 };
95 #endif
96
97 int
98 sysarch_ldt(struct thread *td, struct sysarch_args *uap, int uap_space)
99 {
100         struct i386_ldt_args *largs, la;
101         struct user_segment_descriptor *lp;
102         int error = 0;
103
104         /*
105          * XXXKIB check that the BSM generation code knows to encode
106          * the op argument.
107          */
108         AUDIT_ARG_CMD(uap->op);
109         if (uap_space == UIO_USERSPACE) {
110                 error = copyin(uap->parms, &la, sizeof(struct i386_ldt_args));
111                 if (error != 0)
112                         return (error);
113                 largs = &la;
114         } else
115                 largs = (struct i386_ldt_args *)uap->parms;
116
117         switch (uap->op) {
118         case I386_GET_LDT:
119                 error = amd64_get_ldt(td, largs);
120                 break;
121         case I386_SET_LDT:
122                 if (largs->descs != NULL && largs->num > max_ldt_segment)
123                         return (EINVAL);
124                 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
125                 if (largs->descs != NULL) {
126                         lp = malloc(largs->num * sizeof(struct
127                             user_segment_descriptor), M_TEMP, M_WAITOK);
128                         error = copyin(largs->descs, lp, largs->num *
129                             sizeof(struct user_segment_descriptor));
130                         if (error == 0)
131                                 error = amd64_set_ldt(td, largs, lp);
132                         free(lp, M_TEMP);
133                 } else {
134                         error = amd64_set_ldt(td, largs, NULL);
135                 }
136                 break;
137         }
138         return (error);
139 }
140
141 void
142 update_gdt_gsbase(struct thread *td, uint32_t base)
143 {
144         struct user_segment_descriptor *sd;
145
146         if (td != curthread)
147                 return;
148         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
149         critical_enter();
150         sd = PCPU_GET(gs32p);
151         sd->sd_lobase = base & 0xffffff;
152         sd->sd_hibase = (base >> 24) & 0xff;
153         critical_exit();
154 }
155
156 void
157 update_gdt_fsbase(struct thread *td, uint32_t base)
158 {
159         struct user_segment_descriptor *sd;
160
161         if (td != curthread)
162                 return;
163         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
164         critical_enter();
165         sd = PCPU_GET(fs32p);
166         sd->sd_lobase = base & 0xffffff;
167         sd->sd_hibase = (base >> 24) & 0xff;
168         critical_exit();
169 }
170
171 int
172 sysarch(struct thread *td, struct sysarch_args *uap)
173 {
174         int error = 0;
175         struct pcb *pcb = curthread->td_pcb;
176         uint32_t i386base;
177         uint64_t a64base;
178         struct i386_ioperm_args iargs;
179         struct i386_get_xfpustate i386xfpu;
180         struct amd64_get_xfpustate a64xfpu;
181
182 #ifdef CAPABILITY_MODE
183         /*
184          * When adding new operations, add a new case statement here to
185          * explicitly indicate whether or not the operation is safe to
186          * perform in capability mode.
187          */
188         if (IN_CAPABILITY_MODE(td)) {
189                 switch (uap->op) {
190                 case I386_GET_LDT:
191                 case I386_SET_LDT:
192                 case I386_GET_IOPERM:
193                 case I386_GET_FSBASE:
194                 case I386_SET_FSBASE:
195                 case I386_GET_GSBASE:
196                 case I386_SET_GSBASE:
197                 case I386_GET_XFPUSTATE:
198                 case AMD64_GET_FSBASE:
199                 case AMD64_SET_FSBASE:
200                 case AMD64_GET_GSBASE:
201                 case AMD64_SET_GSBASE:
202                 case AMD64_GET_XFPUSTATE:
203                         break;
204
205                 case I386_SET_IOPERM:
206                 default:
207 #ifdef KTRACE
208                         if (KTRPOINT(td, KTR_CAPFAIL))
209                                 ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL);
210 #endif
211                         return (ECAPMODE);
212                 }
213         }
214 #endif
215
216         if (uap->op == I386_GET_LDT || uap->op == I386_SET_LDT)
217                 return (sysarch_ldt(td, uap, UIO_USERSPACE));
218         /*
219          * XXXKIB check that the BSM generation code knows to encode
220          * the op argument.
221          */
222         AUDIT_ARG_CMD(uap->op);
223         switch (uap->op) {
224         case I386_GET_IOPERM:
225         case I386_SET_IOPERM:
226                 if ((error = copyin(uap->parms, &iargs,
227                     sizeof(struct i386_ioperm_args))) != 0)
228                         return (error);
229                 break;
230         case I386_GET_XFPUSTATE:
231                 if ((error = copyin(uap->parms, &i386xfpu,
232                     sizeof(struct i386_get_xfpustate))) != 0)
233                         return (error);
234                 a64xfpu.addr = (void *)(uintptr_t)i386xfpu.addr;
235                 a64xfpu.len = i386xfpu.len;
236                 break;
237         case AMD64_GET_XFPUSTATE:
238                 if ((error = copyin(uap->parms, &a64xfpu,
239                     sizeof(struct amd64_get_xfpustate))) != 0)
240                         return (error);
241                 break;
242         default:
243                 break;
244         }
245
246         switch (uap->op) {
247         case I386_GET_IOPERM:
248                 error = amd64_get_ioperm(td, &iargs);
249                 if (error == 0)
250                         error = copyout(&iargs, uap->parms,
251                             sizeof(struct i386_ioperm_args));
252                 break;
253         case I386_SET_IOPERM:
254                 error = amd64_set_ioperm(td, &iargs);
255                 break;
256         case I386_GET_FSBASE:
257                 update_pcb_bases(pcb);
258                 i386base = pcb->pcb_fsbase;
259                 error = copyout(&i386base, uap->parms, sizeof(i386base));
260                 break;
261         case I386_SET_FSBASE:
262                 error = copyin(uap->parms, &i386base, sizeof(i386base));
263                 if (!error) {
264                         set_pcb_flags(pcb, PCB_FULL_IRET);
265                         pcb->pcb_fsbase = i386base;
266                         td->td_frame->tf_fs = _ufssel;
267                         update_gdt_fsbase(td, i386base);
268                 }
269                 break;
270         case I386_GET_GSBASE:
271                 update_pcb_bases(pcb);
272                 i386base = pcb->pcb_gsbase;
273                 error = copyout(&i386base, uap->parms, sizeof(i386base));
274                 break;
275         case I386_SET_GSBASE:
276                 error = copyin(uap->parms, &i386base, sizeof(i386base));
277                 if (!error) {
278                         set_pcb_flags(pcb, PCB_FULL_IRET);
279                         pcb->pcb_gsbase = i386base;
280                         td->td_frame->tf_gs = _ugssel;
281                         update_gdt_gsbase(td, i386base);
282                 }
283                 break;
284         case AMD64_GET_FSBASE:
285                 update_pcb_bases(pcb);
286                 error = copyout(&pcb->pcb_fsbase, uap->parms,
287                     sizeof(pcb->pcb_fsbase));
288                 break;
289                 
290         case AMD64_SET_FSBASE:
291                 error = copyin(uap->parms, &a64base, sizeof(a64base));
292                 if (!error) {
293                         if (a64base < VM_MAXUSER_ADDRESS) {
294                                 set_pcb_flags(pcb, PCB_FULL_IRET);
295                                 pcb->pcb_fsbase = a64base;
296                                 td->td_frame->tf_fs = _ufssel;
297                         } else
298                                 error = EINVAL;
299                 }
300                 break;
301
302         case AMD64_GET_GSBASE:
303                 update_pcb_bases(pcb);
304                 error = copyout(&pcb->pcb_gsbase, uap->parms,
305                     sizeof(pcb->pcb_gsbase));
306                 break;
307
308         case AMD64_SET_GSBASE:
309                 error = copyin(uap->parms, &a64base, sizeof(a64base));
310                 if (!error) {
311                         if (a64base < VM_MAXUSER_ADDRESS) {
312                                 set_pcb_flags(pcb, PCB_FULL_IRET);
313                                 pcb->pcb_gsbase = a64base;
314                                 td->td_frame->tf_gs = _ugssel;
315                         } else
316                                 error = EINVAL;
317                 }
318                 break;
319
320         case I386_GET_XFPUSTATE:
321         case AMD64_GET_XFPUSTATE:
322                 if (a64xfpu.len > cpu_max_ext_state_size -
323                     sizeof(struct savefpu))
324                         return (EINVAL);
325                 fpugetregs(td);
326                 error = copyout((char *)(get_pcb_user_save_td(td) + 1),
327                     a64xfpu.addr, a64xfpu.len);
328                 break;
329
330         default:
331                 error = EINVAL;
332                 break;
333         }
334         return (error);
335 }
336
337 int
338 amd64_set_ioperm(td, uap)
339         struct thread *td;
340         struct i386_ioperm_args *uap;
341 {
342         char *iomap;
343         struct amd64tss *tssp;
344         struct system_segment_descriptor *tss_sd;
345         struct pcb *pcb;
346         u_int i;
347         int error;
348
349         if ((error = priv_check(td, PRIV_IO)) != 0)
350                 return (error);
351         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
352                 return (error);
353         if (uap->start > uap->start + uap->length ||
354             uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
355                 return (EINVAL);
356
357         /*
358          * XXX
359          * While this is restricted to root, we should probably figure out
360          * whether any other driver is using this i/o address, as so not to
361          * cause confusion.  This probably requires a global 'usage registry'.
362          */
363         pcb = td->td_pcb;
364         if (pcb->pcb_tssp == NULL) {
365                 tssp = (struct amd64tss *)kmem_malloc(kernel_arena,
366                     ctob(IOPAGES+1), M_WAITOK);
367                 iomap = (char *)&tssp[1];
368                 memset(iomap, 0xff, IOPERM_BITMAP_SIZE);
369                 critical_enter();
370                 /* Takes care of tss_rsp0. */
371                 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
372                     sizeof(struct amd64tss));
373                 tssp->tss_iobase = sizeof(*tssp);
374                 pcb->pcb_tssp = tssp;
375                 tss_sd = PCPU_GET(tss);
376                 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
377                 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
378                 tss_sd->sd_type = SDT_SYSTSS;
379                 ltr(GSEL(GPROC0_SEL, SEL_KPL));
380                 PCPU_SET(tssp, tssp);
381                 critical_exit();
382         } else
383                 iomap = (char *)&pcb->pcb_tssp[1];
384         for (i = uap->start; i < uap->start + uap->length; i++) {
385                 if (uap->enable)
386                         iomap[i >> 3] &= ~(1 << (i & 7));
387                 else
388                         iomap[i >> 3] |= (1 << (i & 7));
389         }
390         return (error);
391 }
392
393 int
394 amd64_get_ioperm(td, uap)
395         struct thread *td;
396         struct i386_ioperm_args *uap;
397 {
398         int i, state;
399         char *iomap;
400
401         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
402                 return (EINVAL);
403         if (td->td_pcb->pcb_tssp == NULL) {
404                 uap->length = 0;
405                 goto done;
406         }
407
408         iomap = (char *)&td->td_pcb->pcb_tssp[1];
409
410         i = uap->start;
411         state = (iomap[i >> 3] >> (i & 7)) & 1;
412         uap->enable = !state;
413         uap->length = 1;
414
415         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
416                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
417                         break;
418                 uap->length++;
419         }
420
421 done:
422         return (0);
423 }
424
425 /*
426  * Update the GDT entry pointing to the LDT to point to the LDT of the
427  * current process.
428  */
429 void
430 set_user_ldt(struct mdproc *mdp)
431 {
432
433         critical_enter();
434         *PCPU_GET(ldt) = mdp->md_ldt_sd;
435         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
436         critical_exit();
437 }
438
439 #ifdef notyet
440 #ifdef SMP
441 static void
442 set_user_ldt_rv(struct vmspace *vmsp)
443 {
444         struct thread *td;
445
446         td = curthread;
447         if (vmsp != td->td_proc->p_vmspace)
448                 return;
449
450         set_user_ldt(&td->td_proc->p_md);
451 }
452 #endif
453 #endif
454
455 struct proc_ldt *
456 user_ldt_alloc(struct proc *p, int force)
457 {
458         struct proc_ldt *pldt, *new_ldt;
459         struct mdproc *mdp;
460         struct soft_segment_descriptor sldt;
461
462         mtx_assert(&dt_lock, MA_OWNED);
463         mdp = &p->p_md;
464         if (!force && mdp->md_ldt != NULL)
465                 return (mdp->md_ldt);
466         mtx_unlock(&dt_lock);
467         new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
468         new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
469              max_ldt_segment * sizeof(struct user_segment_descriptor),
470              M_WAITOK | M_ZERO);
471         new_ldt->ldt_refcnt = 1;
472         sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
473         sldt.ssd_limit = max_ldt_segment *
474             sizeof(struct user_segment_descriptor) - 1;
475         sldt.ssd_type = SDT_SYSLDT;
476         sldt.ssd_dpl = SEL_KPL;
477         sldt.ssd_p = 1;
478         sldt.ssd_long = 0;
479         sldt.ssd_def32 = 0;
480         sldt.ssd_gran = 0;
481         mtx_lock(&dt_lock);
482         pldt = mdp->md_ldt;
483         if (pldt != NULL && !force) {
484                 kmem_free(kernel_arena, (vm_offset_t)new_ldt->ldt_base,
485                     max_ldt_segment * sizeof(struct user_segment_descriptor));
486                 free(new_ldt, M_SUBPROC);
487                 return (pldt);
488         }
489
490         if (pldt != NULL) {
491                 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
492                     sizeof(struct user_segment_descriptor));
493                 user_ldt_derefl(pldt);
494         }
495         ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
496         atomic_store_rel_ptr((volatile uintptr_t *)&mdp->md_ldt,
497             (uintptr_t)new_ldt);
498         if (p == curproc)
499                 set_user_ldt(mdp);
500
501         return (mdp->md_ldt);
502 }
503
504 void
505 user_ldt_free(struct thread *td)
506 {
507         struct proc *p = td->td_proc;
508         struct mdproc *mdp = &p->p_md;
509         struct proc_ldt *pldt;
510
511         mtx_assert(&dt_lock, MA_OWNED);
512         if ((pldt = mdp->md_ldt) == NULL) {
513                 mtx_unlock(&dt_lock);
514                 return;
515         }
516
517         mdp->md_ldt = NULL;
518         bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
519         if (td == curthread)
520                 lldt(GSEL(GNULL_SEL, SEL_KPL));
521         user_ldt_deref(pldt);
522 }
523
524 static void
525 user_ldt_derefl(struct proc_ldt *pldt)
526 {
527
528         if (--pldt->ldt_refcnt == 0) {
529                 kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base,
530                     max_ldt_segment * sizeof(struct user_segment_descriptor));
531                 free(pldt, M_SUBPROC);
532         }
533 }
534
535 void
536 user_ldt_deref(struct proc_ldt *pldt)
537 {
538
539         mtx_assert(&dt_lock, MA_OWNED);
540         user_ldt_derefl(pldt);
541         mtx_unlock(&dt_lock);
542 }
543
544 /*
545  * Note for the authors of compat layers (linux, etc): copyout() in
546  * the function below is not a problem since it presents data in
547  * arch-specific format (i.e. i386-specific in this case), not in
548  * the OS-specific one.
549  */
550 int
551 amd64_get_ldt(td, uap)
552         struct thread *td;
553         struct i386_ldt_args *uap;
554 {
555         int error = 0;
556         struct proc_ldt *pldt;
557         int num;
558         struct user_segment_descriptor *lp;
559
560 #ifdef  DEBUG
561         printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
562             uap->start, uap->num, (void *)uap->descs);
563 #endif
564
565         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
566                 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
567                     [uap->start];
568                 num = min(uap->num, max_ldt_segment);
569         } else
570                 return (EINVAL);
571
572         if ((uap->start > (unsigned int)max_ldt_segment) ||
573             ((unsigned int)num > (unsigned int)max_ldt_segment) ||
574             ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
575                 return(EINVAL);
576
577         error = copyout(lp, uap->descs, num *
578             sizeof(struct user_segment_descriptor));
579         if (!error)
580                 td->td_retval[0] = num;
581
582         return(error);
583 }
584
585 int
586 amd64_set_ldt(td, uap, descs)
587         struct thread *td;
588         struct i386_ldt_args *uap;
589         struct user_segment_descriptor *descs;
590 {
591         int error = 0;
592         unsigned int largest_ld, i;
593         struct mdproc *mdp = &td->td_proc->p_md;
594         struct proc_ldt *pldt;
595         struct user_segment_descriptor *dp;
596         struct proc *p;
597
598 #ifdef  DEBUG
599         printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
600             uap->start, uap->num, (void *)uap->descs);
601 #endif
602
603         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
604         p = td->td_proc;
605         if (descs == NULL) {
606                 /* Free descriptors */
607                 if (uap->start == 0 && uap->num == 0)
608                         uap->num = max_ldt_segment;
609                 if (uap->num == 0)
610                         return (EINVAL);
611                 if ((pldt = mdp->md_ldt) == NULL ||
612                     uap->start >= max_ldt_segment)
613                         return (0);
614                 largest_ld = uap->start + uap->num;
615                 if (largest_ld > max_ldt_segment)
616                         largest_ld = max_ldt_segment;
617                 if (largest_ld < uap->start)
618                         return (EINVAL);
619                 i = largest_ld - uap->start;
620                 mtx_lock(&dt_lock);
621                 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
622                     [uap->start], sizeof(struct user_segment_descriptor) * i);
623                 mtx_unlock(&dt_lock);
624                 return (0);
625         }
626
627         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
628                 /* verify range of descriptors to modify */
629                 largest_ld = uap->start + uap->num;
630                 if (uap->start >= max_ldt_segment ||
631                     largest_ld > max_ldt_segment ||
632                     largest_ld < uap->start)
633                         return (EINVAL);
634         }
635
636         /* Check descriptors for access violations */
637         for (i = 0; i < uap->num; i++) {
638                 dp = &descs[i];
639
640                 switch (dp->sd_type) {
641                 case SDT_SYSNULL:       /* system null */
642                         dp->sd_p = 0;
643                         break;
644                 case SDT_SYS286TSS:
645                 case SDT_SYSLDT:
646                 case SDT_SYS286BSY:
647                 case SDT_SYS286CGT:
648                 case SDT_SYSTASKGT:
649                 case SDT_SYS286IGT:
650                 case SDT_SYS286TGT:
651                 case SDT_SYSNULL2:
652                 case SDT_SYSTSS:
653                 case SDT_SYSNULL3:
654                 case SDT_SYSBSY:
655                 case SDT_SYSCGT:
656                 case SDT_SYSNULL4:
657                 case SDT_SYSIGT:
658                 case SDT_SYSTGT:
659                         /* I can't think of any reason to allow a user proc
660                          * to create a segment of these types.  They are
661                          * for OS use only.
662                          */
663                         return (EACCES);
664                         /*NOTREACHED*/
665
666                 /* memory segment types */
667                 case SDT_MEMEC:   /* memory execute only conforming */
668                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
669                 case SDT_MEMERC:  /* memory execute read conforming */
670                 case SDT_MEMERAC: /* memory execute read accessed conforming */
671                          /* Must be "present" if executable and conforming. */
672                         if (dp->sd_p == 0)
673                                 return (EACCES);
674                         break;
675                 case SDT_MEMRO:   /* memory read only */
676                 case SDT_MEMROA:  /* memory read only accessed */
677                 case SDT_MEMRW:   /* memory read write */
678                 case SDT_MEMRWA:  /* memory read write accessed */
679                 case SDT_MEMROD:  /* memory read only expand dwn limit */
680                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
681                 case SDT_MEMRWD:  /* memory read write expand dwn limit */
682                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
683                 case SDT_MEME:    /* memory execute only */
684                 case SDT_MEMEA:   /* memory execute only accessed */
685                 case SDT_MEMER:   /* memory execute read */
686                 case SDT_MEMERA:  /* memory execute read accessed */
687                         break;
688                 default:
689                         return(EINVAL);
690                         /*NOTREACHED*/
691                 }
692
693                 /* Only user (ring-3) descriptors may be present. */
694                 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
695                         return (EACCES);
696         }
697
698         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
699                 /* Allocate a free slot */
700                 mtx_lock(&dt_lock);
701                 pldt = user_ldt_alloc(p, 0);
702                 if (pldt == NULL) {
703                         mtx_unlock(&dt_lock);
704                         return (ENOMEM);
705                 }
706
707                 /*
708                  * start scanning a bit up to leave room for NVidia and
709                  * Wine, which still user the "Blat" method of allocation.
710                  */
711                 i = 16;
712                 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
713                 for (; i < max_ldt_segment; ++i, ++dp) {
714                         if (dp->sd_type == SDT_SYSNULL)
715                                 break;
716                 }
717                 if (i >= max_ldt_segment) {
718                         mtx_unlock(&dt_lock);
719                         return (ENOSPC);
720                 }
721                 uap->start = i;
722                 error = amd64_set_ldt_data(td, i, 1, descs);
723                 mtx_unlock(&dt_lock);
724         } else {
725                 largest_ld = uap->start + uap->num;
726                 if (largest_ld > max_ldt_segment)
727                         return (EINVAL);
728                 mtx_lock(&dt_lock);
729                 if (user_ldt_alloc(p, 0) != NULL) {
730                         error = amd64_set_ldt_data(td, uap->start, uap->num,
731                             descs);
732                 }
733                 mtx_unlock(&dt_lock);
734         }
735         if (error == 0)
736                 td->td_retval[0] = uap->start;
737         return (error);
738 }
739
740 int
741 amd64_set_ldt_data(struct thread *td, int start, int num,
742     struct user_segment_descriptor *descs)
743 {
744         struct mdproc *mdp = &td->td_proc->p_md;
745         struct proc_ldt *pldt = mdp->md_ldt;
746
747         mtx_assert(&dt_lock, MA_OWNED);
748
749         /* Fill in range */
750         bcopy(descs,
751             &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
752             num * sizeof(struct user_segment_descriptor));
753         return (0);
754 }