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