The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/sys_machdep.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 396ab346c9e7da2c6e29640d45e719ea


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.