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