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

Cache object: dc8ebabf00722075ba699221c2be294f


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