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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
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$");
   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 int
  504 i386_set_ldt(td, uap, descs)
  505         struct thread *td;
  506         struct i386_ldt_args *uap;
  507         union descriptor *descs;
  508 {
  509         int error = 0, i;
  510         int largest_ld;
  511         struct mdproc *mdp = &td->td_proc->p_md;
  512         struct proc_ldt *pldt;
  513         union descriptor *dp;
  514 
  515 #ifdef  DEBUG
  516         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
  517             uap->start, uap->num, (void *)uap->descs);
  518 #endif
  519 
  520         if (descs == NULL) {
  521                 /* Free descriptors */
  522                 if (uap->start == 0 && uap->num == 0) {
  523                         /*
  524                          * Treat this as a special case, so userland needn't
  525                          * know magic number NLDT.
  526                          */
  527                         uap->start = NLDT;
  528                         uap->num = MAX_LD - NLDT;
  529                 }
  530                 if (uap->num <= 0)
  531                         return (EINVAL);
  532                 mtx_lock_spin(&sched_lock);
  533                 pldt = mdp->md_ldt;
  534                 if (pldt == NULL || uap->start >= pldt->ldt_len) {
  535                         mtx_unlock_spin(&sched_lock);
  536                         return (0);
  537                 }
  538                 largest_ld = uap->start + uap->num;
  539                 if (largest_ld > pldt->ldt_len)
  540                         largest_ld = pldt->ldt_len;
  541                 i = largest_ld - uap->start;
  542                 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
  543                     sizeof(union descriptor) * i);
  544                 mtx_unlock_spin(&sched_lock);
  545                 return (0);
  546         }
  547 
  548         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
  549                 /* verify range of descriptors to modify */
  550                 largest_ld = uap->start + uap->num;
  551                 if (uap->start >= MAX_LD ||
  552                     uap->num < 0 || largest_ld > MAX_LD) {
  553                         return (EINVAL);
  554                 }
  555         }
  556 
  557         /* Check descriptors for access violations */
  558         for (i = 0; i < uap->num; i++) {
  559                 dp = &descs[i];
  560 
  561                 switch (dp->sd.sd_type) {
  562                 case SDT_SYSNULL:       /* system null */ 
  563                         dp->sd.sd_p = 0;
  564                         break;
  565                 case SDT_SYS286TSS: /* system 286 TSS available */
  566                 case SDT_SYSLDT:    /* system local descriptor table */
  567                 case SDT_SYS286BSY: /* system 286 TSS busy */
  568                 case SDT_SYSTASKGT: /* system task gate */
  569                 case SDT_SYS286IGT: /* system 286 interrupt gate */
  570                 case SDT_SYS286TGT: /* system 286 trap gate */
  571                 case SDT_SYSNULL2:  /* undefined by Intel */ 
  572                 case SDT_SYS386TSS: /* system 386 TSS available */
  573                 case SDT_SYSNULL3:  /* undefined by Intel */
  574                 case SDT_SYS386BSY: /* system 386 TSS busy */
  575                 case SDT_SYSNULL4:  /* undefined by Intel */ 
  576                 case SDT_SYS386IGT: /* system 386 interrupt gate */
  577                 case SDT_SYS386TGT: /* system 386 trap gate */
  578                 case SDT_SYS286CGT: /* system 286 call gate */ 
  579                 case SDT_SYS386CGT: /* system 386 call gate */
  580                         /* I can't think of any reason to allow a user proc
  581                          * to create a segment of these types.  They are
  582                          * for OS use only.
  583                          */
  584                         return (EACCES);
  585                         /*NOTREACHED*/
  586 
  587                 /* memory segment types */
  588                 case SDT_MEMEC:   /* memory execute only conforming */
  589                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  590                 case SDT_MEMERC:  /* memory execute read conforming */
  591                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  592                          /* Must be "present" if executable and conforming. */
  593                         if (dp->sd.sd_p == 0)
  594                                 return (EACCES);
  595                         break;
  596                 case SDT_MEMRO:   /* memory read only */
  597                 case SDT_MEMROA:  /* memory read only accessed */
  598                 case SDT_MEMRW:   /* memory read write */
  599                 case SDT_MEMRWA:  /* memory read write accessed */
  600                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  601                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  602                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
  603                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  604                 case SDT_MEME:    /* memory execute only */ 
  605                 case SDT_MEMEA:   /* memory execute only accessed */
  606                 case SDT_MEMER:   /* memory execute read */
  607                 case SDT_MEMERA:  /* memory execute read accessed */
  608                         break;
  609                 default:
  610                         return(EINVAL);
  611                         /*NOTREACHED*/
  612                 }
  613 
  614                 /* Only user (ring-3) descriptors may be present. */
  615                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
  616                         return (EACCES);
  617         }
  618 
  619         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
  620                 /* Allocate a free slot */
  621                 pldt = mdp->md_ldt;
  622                 if (pldt == NULL) {
  623                         error = i386_ldt_grow(td, NLDT + 1);
  624                         if (error)
  625                                 return (error);
  626                         pldt = mdp->md_ldt;
  627                 }
  628 again:
  629                 mtx_lock_spin(&sched_lock);
  630                 /*
  631                  * start scanning a bit up to leave room for NVidia and
  632                  * Wine, which still user the "Blat" method of allocation.
  633                  */
  634                 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
  635                 for (i = NLDT; i < pldt->ldt_len; ++i) {
  636                         if (dp->sd.sd_type == SDT_SYSNULL)
  637                                 break;
  638                         dp++;
  639                 }
  640                 if (i >= pldt->ldt_len) {
  641                         mtx_unlock_spin(&sched_lock);
  642                         error = i386_ldt_grow(td, pldt->ldt_len+1);
  643                         if (error)
  644                                 return (error);
  645                         goto again;
  646                 }
  647                 uap->start = i;
  648                 error = i386_set_ldt_data(td, i, 1, descs);
  649                 mtx_unlock_spin(&sched_lock);
  650         } else {
  651                 largest_ld = uap->start + uap->num;
  652                 error = i386_ldt_grow(td, largest_ld);
  653                 if (error == 0) {
  654                         mtx_lock_spin(&sched_lock);
  655                         error = i386_set_ldt_data(td, uap->start, uap->num,
  656                             descs);
  657                         mtx_unlock_spin(&sched_lock);
  658                 }
  659         }
  660         if (error == 0)
  661                 td->td_retval[0] = uap->start;
  662         return (error);
  663 }
  664 
  665 static int
  666 i386_set_ldt_data(struct thread *td, int start, int num,
  667         union descriptor *descs)
  668 {
  669         struct mdproc *mdp = &td->td_proc->p_md;
  670         struct proc_ldt *pldt = mdp->md_ldt;
  671 
  672         mtx_assert(&sched_lock, MA_OWNED);
  673 
  674         /* Fill in range */
  675         bcopy(descs,
  676             &((union descriptor *)(pldt->ldt_base))[start],
  677             num * sizeof(union descriptor));
  678         return (0);
  679 }
  680 
  681 static int
  682 i386_ldt_grow(struct thread *td, int len) 
  683 {
  684         struct mdproc *mdp = &td->td_proc->p_md;
  685         struct proc_ldt *pldt;
  686         caddr_t old_ldt_base;
  687         int old_ldt_len;
  688 
  689         if (len > MAX_LD)
  690                 return (ENOMEM);
  691         if (len < NLDT + 1)
  692                 len = NLDT + 1;
  693 
  694         /* Allocate a user ldt. */
  695         pldt = mdp->md_ldt;
  696         if (!pldt || len > pldt->ldt_len) {
  697                 struct proc_ldt *new_ldt;
  698 
  699                 new_ldt = user_ldt_alloc(mdp, len);
  700                 if (new_ldt == NULL)
  701                         return (ENOMEM);
  702                 pldt = mdp->md_ldt;
  703 
  704                 /* sched_lock was acquired by user_ldt_alloc. */
  705                 if (pldt) {
  706                         if (new_ldt->ldt_len > pldt->ldt_len) {
  707                                 old_ldt_base = pldt->ldt_base;
  708                                 old_ldt_len = pldt->ldt_len;
  709                                 pldt->ldt_sd = new_ldt->ldt_sd;
  710                                 pldt->ldt_base = new_ldt->ldt_base;
  711                                 pldt->ldt_len = new_ldt->ldt_len;
  712                                 mtx_unlock_spin(&sched_lock);
  713                                 kmem_free(kernel_map, (vm_offset_t)old_ldt_base,
  714                                         old_ldt_len * sizeof(union descriptor));
  715                                 FREE(new_ldt, M_SUBPROC);
  716                                 mtx_lock_spin(&sched_lock);
  717                         } else {
  718                                 /*
  719                                  * If other threads already did the work,
  720                                  * do nothing.
  721                                  */
  722                                 mtx_unlock_spin(&sched_lock);
  723                                 kmem_free(kernel_map,
  724                                    (vm_offset_t)new_ldt->ldt_base,
  725                                    new_ldt->ldt_len * sizeof(union descriptor));
  726                                 FREE(new_ldt, M_SUBPROC);
  727                                 return (0);
  728                         }
  729                 } else {
  730                         mdp->md_ldt = pldt = new_ldt;
  731                 }
  732 #ifdef SMP
  733                 mtx_unlock_spin(&sched_lock);
  734                 /* signal other cpus to reload ldt */
  735                 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
  736                     NULL, td);
  737 #else
  738                 set_user_ldt(mdp);
  739                 mtx_unlock_spin(&sched_lock);
  740 #endif
  741         }
  742         return (0);
  743 }

Cache object: a3e6c8f75bf8d7260305c91d3416ba7f


[ 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.