]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/sys_machdep.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / sys_machdep.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_kstack_pages.h"
36 #include "opt_mac.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/smp.h>
46 #include <sys/sysproto.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
52
53 #include <machine/cpu.h>
54 #include <machine/pcb.h>
55 #include <machine/pcb_ext.h>
56 #include <machine/proc.h>
57 #include <machine/sysarch.h>
58
59 #include <security/audit/audit.h>
60
61 #ifdef XEN 
62 #include <machine/xen/xenfunc.h>
63
64 void i386_reset_ldt(struct proc_ldt *pldt); 
65
66 void 
67 i386_reset_ldt(struct proc_ldt *pldt) 
68
69         xen_set_ldt((vm_offset_t)pldt->ldt_base, pldt->ldt_len); 
70
71 #else  
72 #define i386_reset_ldt(x) 
73 #endif 
74
75 #include <vm/vm_kern.h>         /* for kernel_map */
76
77 #define MAX_LD 8192
78 #define LD_PER_PAGE 512
79 #define NEW_MAX_LD(num)  ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
80 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
81 #define NULL_LDT_BASE   ((caddr_t)NULL)
82
83 #ifdef SMP
84 static void set_user_ldt_rv(struct vmspace *vmsp);
85 #endif
86 static int i386_set_ldt_data(struct thread *, int start, int num,
87         union descriptor *descs);
88 static int i386_ldt_grow(struct thread *td, int len);
89
90 #ifndef _SYS_SYSPROTO_H_
91 struct sysarch_args {
92         int op;
93         char *parms;
94 };
95 #endif
96
97 int
98 sysarch(td, uap)
99         struct thread *td;
100         register struct sysarch_args *uap;
101 {
102         int error;
103         union descriptor *lp;
104         union {
105                 struct i386_ldt_args largs;
106                 struct i386_ioperm_args iargs;
107         } kargs;
108         uint32_t base;
109         struct segment_descriptor sd, *sdp;
110
111         AUDIT_ARG(cmd, uap->op);
112         switch (uap->op) {
113         case I386_GET_IOPERM:
114         case I386_SET_IOPERM:
115                 if ((error = copyin(uap->parms, &kargs.iargs,
116                     sizeof(struct i386_ioperm_args))) != 0)
117                         return (error);
118                 break;
119         case I386_GET_LDT:
120         case I386_SET_LDT:
121                 if ((error = copyin(uap->parms, &kargs.largs,
122                     sizeof(struct i386_ldt_args))) != 0)
123                         return (error);
124                 if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0)
125                         return (EINVAL);
126                 break;
127         default:
128                 break;
129         }
130
131         switch(uap->op) {
132         case I386_GET_LDT:
133                 error = i386_get_ldt(td, &kargs.largs);
134                 break;
135         case I386_SET_LDT:
136                 if (kargs.largs.descs != NULL) {
137                         lp = (union descriptor *)kmem_alloc(kernel_map,
138                             kargs.largs.num * sizeof(union descriptor));
139                         if (lp == NULL) {
140                                 error = ENOMEM;
141                                 break;
142                         }
143                         error = copyin(kargs.largs.descs, lp,
144                             kargs.largs.num * sizeof(union descriptor));
145                         if (error == 0)
146                                 error = i386_set_ldt(td, &kargs.largs, lp);
147                         kmem_free(kernel_map, (vm_offset_t)lp,
148                             kargs.largs.num * sizeof(union descriptor));
149                 } else {
150                         error = i386_set_ldt(td, &kargs.largs, NULL);
151                 }
152                 break;
153         case I386_GET_IOPERM:
154                 error = i386_get_ioperm(td, &kargs.iargs);
155                 if (error == 0)
156                         error = copyout(&kargs.iargs, uap->parms,
157                             sizeof(struct i386_ioperm_args));
158                 break;
159         case I386_SET_IOPERM:
160                 error = i386_set_ioperm(td, &kargs.iargs);
161                 break;
162         case I386_VM86:
163                 error = vm86_sysarch(td, uap->parms);
164                 break;
165         case I386_GET_FSBASE:
166                 sdp = &td->td_pcb->pcb_fsd;
167                 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
168                 error = copyout(&base, uap->parms, sizeof(base));
169                 break;
170         case I386_SET_FSBASE:
171                 error = copyin(uap->parms, &base, sizeof(base));
172                 if (!error) {
173                         /*
174                          * Construct a descriptor and store it in the pcb for
175                          * the next context switch.  Also store it in the gdt
176                          * so that the load of tf_fs into %fs will activate it
177                          * at return to userland.
178                          */
179                         sd.sd_lobase = base & 0xffffff;
180                         sd.sd_hibase = (base >> 24) & 0xff;
181 #ifdef XEN
182                         /* need to do nosegneg like Linux */
183                         sd.sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
184 #else                   
185                         sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
186 #endif
187                         sd.sd_hilimit = 0xf;
188                         sd.sd_type  = SDT_MEMRWA;
189                         sd.sd_dpl   = SEL_UPL;
190                         sd.sd_p     = 1;
191                         sd.sd_xx    = 0;
192                         sd.sd_def32 = 1;
193                         sd.sd_gran  = 1;
194                         critical_enter();
195                         td->td_pcb->pcb_fsd = sd;
196 #ifdef XEN
197                         HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[0]),
198                             *(uint64_t *)&sd);
199 #else
200                         PCPU_GET(fsgs_gdt)[0] = sd;
201 #endif
202                         critical_exit();
203                         td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
204                 }
205                 break;
206         case I386_GET_GSBASE:
207                 sdp = &td->td_pcb->pcb_gsd;
208                 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
209                 error = copyout(&base, uap->parms, sizeof(base));
210                 break;
211         case I386_SET_GSBASE:
212                 error = copyin(uap->parms, &base, sizeof(base));
213                 if (!error) {
214                         /*
215                          * Construct a descriptor and store it in the pcb for
216                          * the next context switch.  Also store it in the gdt
217                          * because we have to do a load_gs() right now.
218                          */
219                         sd.sd_lobase = base & 0xffffff;
220                         sd.sd_hibase = (base >> 24) & 0xff;
221
222 #ifdef XEN
223                         /* need to do nosegneg like Linux */
224                         sd.sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
225 #else   
226                         sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
227 #endif
228                         sd.sd_hilimit = 0xf;
229                         sd.sd_type  = SDT_MEMRWA;
230                         sd.sd_dpl   = SEL_UPL;
231                         sd.sd_p     = 1;
232                         sd.sd_xx    = 0;
233                         sd.sd_def32 = 1;
234                         sd.sd_gran  = 1;
235                         critical_enter();
236                         td->td_pcb->pcb_gsd = sd;
237 #ifdef XEN
238                         HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[1]),
239                             *(uint64_t *)&sd);
240 #else                   
241                         PCPU_GET(fsgs_gdt)[1] = sd;
242 #endif
243                         critical_exit();
244                         load_gs(GSEL(GUGS_SEL, SEL_UPL));
245                 }
246                 break;
247         default:
248                 error = EINVAL;
249                 break;
250         }
251         return (error);
252 }
253
254 int
255 i386_extend_pcb(struct thread *td)
256 {
257         int i, offset;
258         u_long *addr;
259         struct pcb_ext *ext;
260         struct soft_segment_descriptor ssd = {
261                 0,                      /* segment base address (overwritten) */
262                 ctob(IOPAGES + 1) - 1,  /* length */
263                 SDT_SYS386TSS,          /* segment type */
264                 0,                      /* priority level */
265                 1,                      /* descriptor present */
266                 0, 0,
267                 0,                      /* default 32 size */
268                 0                       /* granularity */
269         };
270
271         ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
272         if (ext == 0)
273                 return (ENOMEM);
274         bzero(ext, sizeof(struct pcb_ext)); 
275         /* -16 is so we can convert a trapframe into vm86trapframe inplace */
276         ext->ext_tss.tss_esp0 = td->td_kstack + ctob(KSTACK_PAGES) -
277             sizeof(struct pcb) - 16;
278         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
279         /*
280          * The last byte of the i/o map must be followed by an 0xff byte.
281          * We arbitrarily allocate 16 bytes here, to keep the starting
282          * address on a doubleword boundary.
283          */
284         offset = PAGE_SIZE - 16;
285         ext->ext_tss.tss_ioopt = 
286             (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
287         ext->ext_iomap = (caddr_t)ext + offset;
288         ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
289
290         addr = (u_long *)ext->ext_vm86.vm86_intmap;
291         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
292                 *addr++ = ~0;
293
294         ssd.ssd_base = (unsigned)&ext->ext_tss;
295         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
296         ssdtosd(&ssd, &ext->ext_tssd);
297
298         KASSERT(td == curthread, ("giving TSS to !curthread"));
299         KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
300
301         /* Switch to the new TSS. */
302         critical_enter();
303         td->td_pcb->pcb_ext = ext;
304         PCPU_SET(private_tss, 1);
305         *PCPU_GET(tss_gdt) = ext->ext_tssd;
306         ltr(GSEL(GPROC0_SEL, SEL_KPL));
307         critical_exit();
308
309         return 0;
310 }
311
312 int
313 i386_set_ioperm(td, uap)
314         struct thread *td;
315         struct i386_ioperm_args *uap;
316 {
317         int i, error;
318         char *iomap;
319
320         if ((error = priv_check(td, PRIV_IO)) != 0)
321                 return (error);
322         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
323                 return (error);
324         /*
325          * XXX 
326          * While this is restricted to root, we should probably figure out
327          * whether any other driver is using this i/o address, as so not to
328          * cause confusion.  This probably requires a global 'usage registry'.
329          */
330
331         if (td->td_pcb->pcb_ext == 0)
332                 if ((error = i386_extend_pcb(td)) != 0)
333                         return (error);
334         iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
335
336         if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
337                 return (EINVAL);
338
339         for (i = uap->start; i < uap->start + uap->length; i++) {
340                 if (uap->enable)
341                         iomap[i >> 3] &= ~(1 << (i & 7));
342                 else
343                         iomap[i >> 3] |= (1 << (i & 7));
344         }
345         return (error);
346 }
347
348 int
349 i386_get_ioperm(td, uap)
350         struct thread *td;
351         struct i386_ioperm_args *uap;
352 {
353         int i, state;
354         char *iomap;
355
356         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
357                 return (EINVAL);
358
359         if (td->td_pcb->pcb_ext == 0) {
360                 uap->length = 0;
361                 goto done;
362         }
363
364         iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
365
366         i = uap->start;
367         state = (iomap[i >> 3] >> (i & 7)) & 1;
368         uap->enable = !state;
369         uap->length = 1;
370
371         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
372                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
373                         break;
374                 uap->length++;
375         }
376
377 done:
378         return (0);
379 }
380
381 /*
382  * Update the GDT entry pointing to the LDT to point to the LDT of the
383  * current process. Manage dt_lock holding/unholding autonomously.
384  */   
385 void
386 set_user_ldt(struct mdproc *mdp)
387 {
388         struct proc_ldt *pldt;
389         int dtlocked;
390
391         dtlocked = 0;
392         if (!mtx_owned(&dt_lock)) {
393                 mtx_lock_spin(&dt_lock);
394                 dtlocked = 1;
395         }
396
397         pldt = mdp->md_ldt;
398 #ifdef XEN
399         i386_reset_ldt(pldt);
400         PCPU_SET(currentldt, (int)pldt);
401 #else   
402 #ifdef SMP
403         gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
404 #else
405         gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
406 #endif
407         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
408         PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
409 #endif /* XEN */ 
410         if (dtlocked)
411                 mtx_unlock_spin(&dt_lock);
412 }
413
414 #ifdef SMP
415 static void
416 set_user_ldt_rv(struct vmspace *vmsp)
417 {
418         struct thread *td;
419
420         td = curthread;
421         if (vmsp != td->td_proc->p_vmspace)
422                 return;
423
424         set_user_ldt(&td->td_proc->p_md);
425 }
426 #endif
427
428 #ifdef XEN
429
430 /* 
431  * dt_lock must be held. Returns with dt_lock held. 
432  */ 
433 struct proc_ldt * 
434 user_ldt_alloc(struct mdproc *mdp, int len) 
435
436         struct proc_ldt *pldt, *new_ldt; 
437  
438         mtx_assert(&dt_lock, MA_OWNED); 
439         mtx_unlock_spin(&dt_lock); 
440         MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt), 
441                 M_SUBPROC, M_WAITOK); 
442  
443         new_ldt->ldt_len = len = NEW_MAX_LD(len); 
444         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map, 
445                 round_page(len * sizeof(union descriptor))); 
446         if (new_ldt->ldt_base == NULL) { 
447                 FREE(new_ldt, M_SUBPROC);
448                 mtx_lock_spin(&dt_lock);
449                 return (NULL);
450         } 
451         new_ldt->ldt_refcnt = 1; 
452         new_ldt->ldt_active = 0; 
453  
454         if ((pldt = mdp->md_ldt)) { 
455                 if (len > pldt->ldt_len) 
456                         len = pldt->ldt_len; 
457                 bcopy(pldt->ldt_base, new_ldt->ldt_base, 
458                     len * sizeof(union descriptor)); 
459         } else { 
460                 bcopy(ldt, new_ldt->ldt_base, PAGE_SIZE); 
461         } 
462         pmap_map_readonly(kernel_pmap, (vm_offset_t)new_ldt->ldt_base, 
463                           new_ldt->ldt_len*sizeof(union descriptor)); 
464         return (new_ldt);
465
466 #else
467 /*
468  * dt_lock must be held. Returns with dt_lock held.
469  */
470 struct proc_ldt *
471 user_ldt_alloc(struct mdproc *mdp, int len)
472 {
473         struct proc_ldt *pldt, *new_ldt;
474
475         mtx_assert(&dt_lock, MA_OWNED);
476         mtx_unlock_spin(&dt_lock);
477         MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt),
478                 M_SUBPROC, M_WAITOK);
479
480         new_ldt->ldt_len = len = NEW_MAX_LD(len);
481         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
482                 len * sizeof(union descriptor));
483         if (new_ldt->ldt_base == NULL) {
484                 FREE(new_ldt, M_SUBPROC);
485                 mtx_lock_spin(&dt_lock);
486                 return (NULL);
487         }
488         new_ldt->ldt_refcnt = 1;
489         new_ldt->ldt_active = 0;
490
491         mtx_lock_spin(&dt_lock);
492         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
493         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
494         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
495
496         if ((pldt = mdp->md_ldt) != NULL) {
497                 if (len > pldt->ldt_len)
498                         len = pldt->ldt_len;
499                 bcopy(pldt->ldt_base, new_ldt->ldt_base,
500                     len * sizeof(union descriptor));
501         } else
502                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
503         
504         return (new_ldt);
505 }
506 #endif /* !XEN */
507
508 /*
509  * Must be called with dt_lock held.  Returns with dt_lock unheld.
510  */
511 void
512 user_ldt_free(struct thread *td)
513 {
514         struct mdproc *mdp = &td->td_proc->p_md;
515         struct proc_ldt *pldt;
516
517         mtx_assert(&dt_lock, MA_OWNED);
518         if ((pldt = mdp->md_ldt) == NULL) {
519                 mtx_unlock_spin(&dt_lock);
520                 return;
521         }
522
523         if (td == PCPU_GET(curthread)) {
524                 lldt(_default_ldt);
525                 PCPU_SET(currentldt, _default_ldt);
526         }
527
528         mdp->md_ldt = NULL;
529         user_ldt_deref(pldt);
530 }
531
532 void
533 user_ldt_deref(struct proc_ldt *pldt)
534 {
535
536         mtx_assert(&dt_lock, MA_OWNED);
537         if (--pldt->ldt_refcnt == 0) {
538                 mtx_unlock_spin(&dt_lock);
539                 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
540                         pldt->ldt_len * sizeof(union descriptor));
541                 FREE(pldt, M_SUBPROC);
542         } else
543                 mtx_unlock_spin(&dt_lock);
544 }
545
546 /*
547  * Note for the authors of compat layers (linux, etc): copyout() in
548  * the function below is not a problem since it presents data in
549  * arch-specific format (i.e. i386-specific in this case), not in
550  * the OS-specific one.
551  */
552 int
553 i386_get_ldt(td, uap)
554         struct thread *td;
555         struct i386_ldt_args *uap;
556 {
557         int error = 0;
558         struct proc_ldt *pldt;
559         int nldt, num;
560         union descriptor *lp;
561
562 #ifdef  DEBUG
563         printf("i386_get_ldt: start=%d num=%d descs=%p\n",
564             uap->start, uap->num, (void *)uap->descs);
565 #endif
566
567         mtx_lock_spin(&dt_lock);
568         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
569                 nldt = pldt->ldt_len;
570                 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
571                 mtx_unlock_spin(&dt_lock);
572                 num = min(uap->num, nldt);
573         } else {
574                 mtx_unlock_spin(&dt_lock);
575                 nldt = sizeof(ldt)/sizeof(ldt[0]);
576                 num = min(uap->num, nldt);
577                 lp = &ldt[uap->start];
578         }
579
580         if ((uap->start > (unsigned int)nldt) ||
581             ((unsigned int)num > (unsigned int)nldt) ||
582             ((unsigned int)(uap->start + num) > (unsigned int)nldt))
583                 return(EINVAL);
584
585         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
586         if (!error)
587                 td->td_retval[0] = num;
588
589         return(error);
590 }
591
592 int
593 i386_set_ldt(td, uap, descs)
594         struct thread *td;
595         struct i386_ldt_args *uap;
596         union descriptor *descs;
597 {
598         int error = 0, i;
599         int largest_ld;
600         struct mdproc *mdp = &td->td_proc->p_md;
601         struct proc_ldt *pldt;
602         union descriptor *dp;
603
604 #ifdef  DEBUG
605         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
606             uap->start, uap->num, (void *)uap->descs);
607 #endif
608
609         if (descs == NULL) {
610                 /* Free descriptors */
611                 if (uap->start == 0 && uap->num == 0) {
612                         /*
613                          * Treat this as a special case, so userland needn't
614                          * know magic number NLDT.
615                          */
616                         uap->start = NLDT;
617                         uap->num = MAX_LD - NLDT;
618                 }
619                 if (uap->num <= 0)
620                         return (EINVAL);
621                 mtx_lock_spin(&dt_lock);
622                 if ((pldt = mdp->md_ldt) == NULL ||
623                     uap->start >= pldt->ldt_len) {
624                         mtx_unlock_spin(&dt_lock);
625                         return (0);
626                 }
627                 largest_ld = uap->start + uap->num;
628                 if (largest_ld > pldt->ldt_len)
629                         largest_ld = pldt->ldt_len;
630                 i = largest_ld - uap->start;
631                 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
632                     sizeof(union descriptor) * i);
633                 mtx_unlock_spin(&dt_lock);
634                 return (0);
635         }
636
637         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
638                 /* verify range of descriptors to modify */
639                 largest_ld = uap->start + uap->num;
640                 if (uap->start >= MAX_LD ||
641                     uap->num < 0 || largest_ld > MAX_LD) {
642                         return (EINVAL);
643                 }
644         }
645
646         /* Check descriptors for access violations */
647         for (i = 0; i < uap->num; i++) {
648                 dp = &descs[i];
649
650                 switch (dp->sd.sd_type) {
651                 case SDT_SYSNULL:       /* system null */ 
652                         dp->sd.sd_p = 0;
653                         break;
654                 case SDT_SYS286TSS: /* system 286 TSS available */
655                 case SDT_SYSLDT:    /* system local descriptor table */
656                 case SDT_SYS286BSY: /* system 286 TSS busy */
657                 case SDT_SYSTASKGT: /* system task gate */
658                 case SDT_SYS286IGT: /* system 286 interrupt gate */
659                 case SDT_SYS286TGT: /* system 286 trap gate */
660                 case SDT_SYSNULL2:  /* undefined by Intel */ 
661                 case SDT_SYS386TSS: /* system 386 TSS available */
662                 case SDT_SYSNULL3:  /* undefined by Intel */
663                 case SDT_SYS386BSY: /* system 386 TSS busy */
664                 case SDT_SYSNULL4:  /* undefined by Intel */ 
665                 case SDT_SYS386IGT: /* system 386 interrupt gate */
666                 case SDT_SYS386TGT: /* system 386 trap gate */
667                 case SDT_SYS286CGT: /* system 286 call gate */ 
668                 case SDT_SYS386CGT: /* system 386 call gate */
669                         /* I can't think of any reason to allow a user proc
670                          * to create a segment of these types.  They are
671                          * for OS use only.
672                          */
673                         return (EACCES);
674                         /*NOTREACHED*/
675
676                 /* memory segment types */
677                 case SDT_MEMEC:   /* memory execute only conforming */
678                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
679                 case SDT_MEMERC:  /* memory execute read conforming */
680                 case SDT_MEMERAC: /* memory execute read accessed conforming */
681                          /* Must be "present" if executable and conforming. */
682                         if (dp->sd.sd_p == 0)
683                                 return (EACCES);
684                         break;
685                 case SDT_MEMRO:   /* memory read only */
686                 case SDT_MEMROA:  /* memory read only accessed */
687                 case SDT_MEMRW:   /* memory read write */
688                 case SDT_MEMRWA:  /* memory read write accessed */
689                 case SDT_MEMROD:  /* memory read only expand dwn limit */
690                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
691                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
692                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
693                 case SDT_MEME:    /* memory execute only */ 
694                 case SDT_MEMEA:   /* memory execute only accessed */
695                 case SDT_MEMER:   /* memory execute read */
696                 case SDT_MEMERA:  /* memory execute read accessed */
697                         break;
698                 default:
699                         return(EINVAL);
700                         /*NOTREACHED*/
701                 }
702
703                 /* Only user (ring-3) descriptors may be present. */
704                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
705                         return (EACCES);
706         }
707
708         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
709                 /* Allocate a free slot */
710                 mtx_lock_spin(&dt_lock);
711                 if ((pldt = mdp->md_ldt) == NULL) {
712                         if ((error = i386_ldt_grow(td, NLDT + 1))) {
713                                 mtx_unlock_spin(&dt_lock);
714                                 return (error);
715                         }
716                         pldt = mdp->md_ldt;
717                 }
718 again:
719                 /*
720                  * start scanning a bit up to leave room for NVidia and
721                  * Wine, which still user the "Blat" method of allocation.
722                  */
723                 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
724                 for (i = NLDT; i < pldt->ldt_len; ++i) {
725                         if (dp->sd.sd_type == SDT_SYSNULL)
726                                 break;
727                         dp++;
728                 }
729                 if (i >= pldt->ldt_len) {
730                         if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
731                                 mtx_unlock_spin(&dt_lock);
732                                 return (error);
733                         }
734                         goto again;
735                 }
736                 uap->start = i;
737                 error = i386_set_ldt_data(td, i, 1, descs);
738                 mtx_unlock_spin(&dt_lock);
739         } else {
740                 largest_ld = uap->start + uap->num;
741                 mtx_lock_spin(&dt_lock);
742                 if (!(error = i386_ldt_grow(td, largest_ld))) {
743                         error = i386_set_ldt_data(td, uap->start, uap->num,
744                             descs);
745                 }
746                 mtx_unlock_spin(&dt_lock);
747         }
748         if (error == 0)
749                 td->td_retval[0] = uap->start;
750         return (error);
751 }
752 #ifdef XEN
753 static int
754 i386_set_ldt_data(struct thread *td, int start, int num,
755         union descriptor *descs)
756 {
757         struct mdproc *mdp = &td->td_proc->p_md;
758         struct proc_ldt *pldt = mdp->md_ldt;
759
760         mtx_assert(&dt_lock, MA_OWNED);
761
762         /* Fill in range */
763         bcopy(descs,
764             &((union descriptor *)(pldt->ldt_base))[start],
765             num * sizeof(union descriptor));
766         return (0);
767 }
768 #else
769 static int
770 i386_set_ldt_data(struct thread *td, int start, int num,
771         union descriptor *descs)
772 {
773         struct mdproc *mdp = &td->td_proc->p_md;
774         struct proc_ldt *pldt = mdp->md_ldt;
775
776         mtx_assert(&dt_lock, MA_OWNED);
777
778         /* Fill in range */
779         bcopy(descs,
780             &((union descriptor *)(pldt->ldt_base))[start],
781             num * sizeof(union descriptor));
782         return (0);
783 }
784 #endif /* !XEN */
785
786 static int
787 i386_ldt_grow(struct thread *td, int len) 
788 {
789         struct mdproc *mdp = &td->td_proc->p_md;
790         struct proc_ldt *new_ldt, *pldt;
791         caddr_t old_ldt_base = NULL_LDT_BASE;
792         int old_ldt_len = 0;
793
794         mtx_assert(&dt_lock, MA_OWNED);
795
796         if (len > MAX_LD)
797                 return (ENOMEM);
798         if (len < NLDT + 1)
799                 len = NLDT + 1;
800
801         /* Allocate a user ldt. */
802         if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
803                 new_ldt = user_ldt_alloc(mdp, len);
804                 if (new_ldt == NULL)
805                         return (ENOMEM);
806                 pldt = mdp->md_ldt;
807
808                 if (pldt != NULL) {
809                         if (new_ldt->ldt_len <= pldt->ldt_len) {
810                                 /*
811                                  * We just lost the race for allocation, so
812                                  * free the new object and return.
813                                  */
814                                 mtx_unlock_spin(&dt_lock);
815                                 kmem_free(kernel_map,
816                                    (vm_offset_t)new_ldt->ldt_base,
817                                    new_ldt->ldt_len * sizeof(union descriptor));
818                                 FREE(new_ldt, M_SUBPROC);
819                                 mtx_lock_spin(&dt_lock);
820                                 return (0);
821                         }
822
823                         /*
824                          * We have to substitute the current LDT entry for
825                          * curproc with the new one since its size grew.
826                          */
827                         old_ldt_base = pldt->ldt_base;
828                         old_ldt_len = pldt->ldt_len;
829                         pldt->ldt_sd = new_ldt->ldt_sd;
830                         pldt->ldt_base = new_ldt->ldt_base;
831                         pldt->ldt_len = new_ldt->ldt_len;
832                 } else
833                         mdp->md_ldt = pldt = new_ldt;
834 #ifdef SMP
835                 /*
836                  * Signal other cpus to reload ldt.  We need to unlock dt_lock
837                  * here because other CPU will contest on it since their
838                  * curthreads won't hold the lock and will block when trying
839                  * to acquire it.
840                  */
841                 mtx_unlock_spin(&dt_lock);
842                 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
843                     NULL, td->td_proc->p_vmspace);
844 #else
845                 set_user_ldt(&td->td_proc->p_md);
846                 mtx_unlock_spin(&dt_lock);
847 #endif
848                 if (old_ldt_base != NULL_LDT_BASE) {
849                         kmem_free(kernel_map, (vm_offset_t)old_ldt_base,
850                             old_ldt_len * sizeof(union descriptor));
851                         FREE(new_ldt, M_SUBPROC);
852                 }
853                 mtx_lock_spin(&dt_lock);
854         }
855         return (0);
856 }