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