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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/5.2/sys/i386/i386/sys_machdep.c 119825 2003-09-07 05:23:28Z davidxu $");
   38 
   39 #include "opt_kstack_pages.h"
   40 #include "opt_mac.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/lock.h>
   45 #include <sys/mac.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mutex.h>
   48 #include <sys/proc.h>
   49 #include <sys/smp.h>
   50 #include <sys/sysproto.h>
   51 #include <sys/user.h>
   52 
   53 #include <vm/vm.h>
   54 #include <vm/pmap.h>
   55 #include <vm/vm_map.h>
   56 #include <vm/vm_extern.h>
   57 
   58 #include <machine/cpu.h>
   59 #include <machine/pcb_ext.h>    /* pcb.h included by sys/user.h */
   60 #include <machine/proc.h>
   61 #include <machine/sysarch.h>
   62 
   63 #include <vm/vm_kern.h>         /* for kernel_map */
   64 
   65 #define MAX_LD 8192
   66 #define LD_PER_PAGE 512
   67 #define NEW_MAX_LD(num)  ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
   68 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
   69 
   70 
   71 
   72 static int i386_get_ldt(struct thread *, char *);
   73 static int i386_set_ldt(struct thread *, char *);
   74 static int i386_set_ldt_data(struct thread *, int start, int num,
   75         union descriptor *descs);
   76 static int i386_ldt_grow(struct thread *td, int len);
   77 static int i386_get_ioperm(struct thread *, char *);
   78 static int i386_set_ioperm(struct thread *, char *);
   79 #ifdef SMP
   80 static void set_user_ldt_rv(struct thread *);
   81 #endif
   82 
   83 #ifndef _SYS_SYSPROTO_H_
   84 struct sysarch_args {
   85         int op;
   86         char *parms;
   87 };
   88 #endif
   89 
   90 int
   91 sysarch(td, uap)
   92         struct thread *td;
   93         register struct sysarch_args *uap;
   94 {
   95         int error;
   96 
   97         mtx_lock(&Giant);
   98         switch(uap->op) {
   99         case I386_GET_LDT:
  100                 error = i386_get_ldt(td, uap->parms);
  101                 break;
  102 
  103         case I386_SET_LDT:
  104                 error = i386_set_ldt(td, uap->parms);
  105                 break;
  106         case I386_GET_IOPERM:
  107                 error = i386_get_ioperm(td, uap->parms);
  108                 break;
  109         case I386_SET_IOPERM:
  110                 error = i386_set_ioperm(td, uap->parms);
  111                 break;
  112         case I386_VM86:
  113                 error = vm86_sysarch(td, uap->parms);
  114                 break;
  115         default:
  116                 error = EINVAL;
  117                 break;
  118         }
  119         mtx_unlock(&Giant);
  120         return (error);
  121 }
  122 
  123 int
  124 i386_extend_pcb(struct thread *td)
  125 {
  126         int i, offset;
  127         u_long *addr;
  128         struct pcb_ext *ext;
  129         struct soft_segment_descriptor ssd = {
  130                 0,                      /* segment base address (overwritten) */
  131                 ctob(IOPAGES + 1) - 1,  /* length */
  132                 SDT_SYS386TSS,          /* segment type */
  133                 0,                      /* priority level */
  134                 1,                      /* descriptor present */
  135                 0, 0,
  136                 0,                      /* default 32 size */
  137                 0                       /* granularity */
  138         };
  139 
  140         if (td->td_proc->p_flag & P_SA)
  141                 return (EINVAL);                /* XXXKSE */
  142 /* XXXKSE  All the code below only works in 1:1   needs changing */
  143         ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
  144         if (ext == 0)
  145                 return (ENOMEM);
  146         bzero(ext, sizeof(struct pcb_ext)); 
  147         /* -16 is so we can convert a trapframe into vm86trapframe inplace */
  148         ext->ext_tss.tss_esp0 = td->td_kstack + ctob(KSTACK_PAGES) -
  149             sizeof(struct pcb) - 16;
  150         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
  151         /*
  152          * The last byte of the i/o map must be followed by an 0xff byte.
  153          * We arbitrarily allocate 16 bytes here, to keep the starting
  154          * address on a doubleword boundary.
  155          */
  156         offset = PAGE_SIZE - 16;
  157         ext->ext_tss.tss_ioopt = 
  158             (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
  159         ext->ext_iomap = (caddr_t)ext + offset;
  160         ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
  161 
  162         addr = (u_long *)ext->ext_vm86.vm86_intmap;
  163         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
  164                 *addr++ = ~0;
  165 
  166         ssd.ssd_base = (unsigned)&ext->ext_tss;
  167         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
  168         ssdtosd(&ssd, &ext->ext_tssd);
  169 
  170         KASSERT(td->td_proc == curthread->td_proc, ("giving TSS to !curproc"));
  171         KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
  172         mtx_lock_spin(&sched_lock);
  173         td->td_pcb->pcb_ext = ext;
  174         
  175         /* switch to the new TSS after syscall completes */
  176         td->td_flags |= TDF_NEEDRESCHED;
  177         mtx_unlock_spin(&sched_lock);
  178 
  179         return 0;
  180 }
  181 
  182 static int
  183 i386_set_ioperm(td, args)
  184         struct thread *td;
  185         char *args;
  186 {
  187         int i, error;
  188         struct i386_ioperm_args ua;
  189         char *iomap;
  190 
  191         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
  192                 return (error);
  193 
  194 #ifdef MAC
  195         if ((error = mac_check_sysarch_ioperm(td->td_ucred)) != 0)
  196                 return (error);
  197 #endif
  198         if ((error = suser(td)) != 0)
  199                 return (error);
  200         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  201                 return (error);
  202         /*
  203          * XXX 
  204          * While this is restricted to root, we should probably figure out
  205          * whether any other driver is using this i/o address, as so not to
  206          * cause confusion.  This probably requires a global 'usage registry'.
  207          */
  208 
  209         if (td->td_pcb->pcb_ext == 0)
  210                 if ((error = i386_extend_pcb(td)) != 0)
  211                         return (error);
  212         iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
  213 
  214         if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
  215                 return (EINVAL);
  216 
  217         for (i = ua.start; i < ua.start + ua.length; i++) {
  218                 if (ua.enable) 
  219                         iomap[i >> 3] &= ~(1 << (i & 7));
  220                 else
  221                         iomap[i >> 3] |= (1 << (i & 7));
  222         }
  223         return (error);
  224 }
  225 
  226 static int
  227 i386_get_ioperm(td, args)
  228         struct thread *td;
  229         char *args;
  230 {
  231         int i, state, error;
  232         struct i386_ioperm_args ua;
  233         char *iomap;
  234 
  235         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
  236                 return (error);
  237         if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
  238                 return (EINVAL);
  239 
  240         if (td->td_pcb->pcb_ext == 0) {
  241                 ua.length = 0;
  242                 goto done;
  243         }
  244 
  245         iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
  246 
  247         i = ua.start;
  248         state = (iomap[i >> 3] >> (i & 7)) & 1;
  249         ua.enable = !state;
  250         ua.length = 1;
  251 
  252         for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
  253                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
  254                         break;
  255                 ua.length++;
  256         }
  257                         
  258 done:
  259         error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
  260         return (error);
  261 }
  262 
  263 /*
  264  * Update the GDT entry pointing to the LDT to point to the LDT of the
  265  * current process.
  266  *
  267  * This must be called with sched_lock held.  Unfortunately, we can't use a
  268  * mtx_assert() here because cpu_switch() calls this function after changing
  269  * curproc but before sched_lock's owner is updated in mi_switch().
  270  */   
  271 void
  272 set_user_ldt(struct mdproc *mdp)
  273 {
  274         struct proc_ldt *pldt;
  275 
  276         pldt = mdp->md_ldt;
  277 #ifdef SMP
  278         gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
  279 #else
  280         gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
  281 #endif
  282         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
  283         PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
  284 }
  285 
  286 #ifdef SMP
  287 static void
  288 set_user_ldt_rv(struct thread *td)
  289 {
  290 
  291         if (td->td_proc != curthread->td_proc)
  292                 return;
  293 
  294         set_user_ldt(&td->td_proc->p_md);
  295 }
  296 #endif
  297 
  298 /*
  299  * Must be called with either sched_lock free or held but not recursed.
  300  * If it does not return NULL, it will return with it owned.
  301  */
  302 struct proc_ldt *
  303 user_ldt_alloc(struct mdproc *mdp, int len)
  304 {
  305         struct proc_ldt *pldt, *new_ldt;
  306 
  307         if (mtx_owned(&sched_lock))
  308                 mtx_unlock_spin(&sched_lock);
  309         mtx_assert(&sched_lock, MA_NOTOWNED);
  310         MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt),
  311                 M_SUBPROC, M_WAITOK);
  312 
  313         new_ldt->ldt_len = len = NEW_MAX_LD(len);
  314         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
  315                 len * sizeof(union descriptor));
  316         if (new_ldt->ldt_base == NULL) {
  317                 FREE(new_ldt, M_SUBPROC);
  318                 return NULL;
  319         }
  320         new_ldt->ldt_refcnt = 1;
  321         new_ldt->ldt_active = 0;
  322 
  323         mtx_lock_spin(&sched_lock);
  324         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
  325         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
  326         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
  327 
  328         if ((pldt = mdp->md_ldt)) {
  329                 if (len > pldt->ldt_len)
  330                         len = pldt->ldt_len;
  331                 bcopy(pldt->ldt_base, new_ldt->ldt_base,
  332                     len * sizeof(union descriptor));
  333         } else {
  334                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
  335         }
  336         return new_ldt;
  337 }
  338 
  339 /*
  340  * Must be called either with sched_lock free or held but not recursed.
  341  * If md_ldt is not NULL, it will return with sched_lock released.
  342  */
  343 void
  344 user_ldt_free(struct thread *td)
  345 {
  346         struct mdproc *mdp = &td->td_proc->p_md;
  347         struct proc_ldt *pldt = mdp->md_ldt;
  348 
  349         if (pldt == NULL)
  350                 return;
  351 
  352         if (!mtx_owned(&sched_lock))
  353                 mtx_lock_spin(&sched_lock);
  354         mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
  355         if (td == PCPU_GET(curthread)) {
  356                 lldt(_default_ldt);
  357                 PCPU_SET(currentldt, _default_ldt);
  358         }
  359 
  360         mdp->md_ldt = NULL;
  361         if (--pldt->ldt_refcnt == 0) {
  362                 mtx_unlock_spin(&sched_lock);
  363                 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
  364                         pldt->ldt_len * sizeof(union descriptor));
  365                 FREE(pldt, M_SUBPROC);
  366         } else
  367                 mtx_unlock_spin(&sched_lock);
  368 }
  369 
  370 static int
  371 i386_get_ldt(td, args)
  372         struct thread *td;
  373         char *args;
  374 {
  375         int error = 0;
  376         struct proc_ldt *pldt = td->td_proc->p_md.md_ldt;
  377         int nldt, num;
  378         union descriptor *lp;
  379         struct i386_ldt_args ua, *uap = &ua;
  380 
  381         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
  382                 return(error);
  383 
  384 #ifdef  DEBUG
  385         printf("i386_get_ldt: start=%d num=%d descs=%p\n",
  386             uap->start, uap->num, (void *)uap->descs);
  387 #endif
  388 
  389         /* verify range of LDTs exist */
  390         if ((uap->start < 0) || (uap->num <= 0))
  391                 return(EINVAL);
  392 
  393         if (pldt) {
  394                 nldt = pldt->ldt_len;
  395                 num = min(uap->num, nldt);
  396                 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
  397         } else {
  398                 nldt = sizeof(ldt)/sizeof(ldt[0]);
  399                 num = min(uap->num, nldt);
  400                 lp = &ldt[uap->start];
  401         }
  402         if (uap->start + num > nldt)
  403                 return(EINVAL);
  404 
  405         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
  406         if (!error)
  407                 td->td_retval[0] = num;
  408 
  409         return(error);
  410 }
  411 
  412 static int ldt_warnings;
  413 #define NUM_LDT_WARNINGS 10
  414 
  415 static int
  416 i386_set_ldt(td, args)
  417         struct thread *td;
  418         char *args;
  419 {
  420         int error = 0, i;
  421         int largest_ld;
  422         struct mdproc *mdp = &td->td_proc->p_md;
  423         struct proc_ldt *pldt = 0;
  424         struct i386_ldt_args ua, *uap = &ua;
  425         union descriptor *descs, *dp;
  426         int descs_size;
  427 
  428         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
  429                 return(error);
  430 
  431 #ifdef  DEBUG
  432         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
  433             uap->start, uap->num, (void *)uap->descs);
  434 #endif
  435 
  436         if (uap->descs == NULL) {
  437                 /* Free descriptors */
  438                 if (uap->start == 0 && uap->num == 0) {
  439                         /*
  440                          * Treat this as a special case, so userland needn't
  441                          * know magic number NLDT.
  442                          */
  443                         uap->start = NLDT;
  444                         uap->num = MAX_LD - NLDT;
  445                 }
  446                 if (uap->start <= LUDATA_SEL || uap->num <= 0)
  447                         return (EINVAL);
  448                 mtx_lock_spin(&sched_lock);
  449                 pldt = mdp->md_ldt;
  450                 if (pldt == NULL || uap->start >= pldt->ldt_len) {
  451                         mtx_unlock_spin(&sched_lock);
  452                         return (0);
  453                 }
  454                 largest_ld = uap->start + uap->num;
  455                 if (largest_ld > pldt->ldt_len)
  456                         largest_ld = pldt->ldt_len;
  457                 i = largest_ld - uap->start;
  458                 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
  459                     sizeof(union descriptor) * i);
  460                 mtx_unlock_spin(&sched_lock);
  461                 return (0);
  462         }
  463 
  464         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
  465                 /* complain a for a while if using old methods */
  466                 if (ldt_warnings++ < NUM_LDT_WARNINGS) {
  467                         printf("Warning: pid %d used static ldt allocation.\n",
  468                             td->td_proc->p_pid);
  469                         printf("See the i386_set_ldt man page for more info\n");
  470                 }
  471                 /* verify range of descriptors to modify */
  472                 largest_ld = uap->start + uap->num;
  473                 if (uap->start >= MAX_LD ||
  474                     uap->num < 0 || largest_ld > MAX_LD) {
  475                         return (EINVAL);
  476                 }
  477         }
  478 
  479         descs_size = uap->num * sizeof(union descriptor);
  480         descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
  481         if (descs == NULL)
  482                 return (ENOMEM);
  483         error = copyin(uap->descs, descs, descs_size);
  484         if (error) {
  485                 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  486                 return (error);
  487         }
  488 
  489         /* Check descriptors for access violations */
  490         for (i = 0; i < uap->num; i++) {
  491                 dp = &descs[i];
  492 
  493                 switch (dp->sd.sd_type) {
  494                 case SDT_SYSNULL:       /* system null */ 
  495                         dp->sd.sd_p = 0;
  496                         break;
  497                 case SDT_SYS286TSS: /* system 286 TSS available */
  498                 case SDT_SYSLDT:    /* system local descriptor table */
  499                 case SDT_SYS286BSY: /* system 286 TSS busy */
  500                 case SDT_SYSTASKGT: /* system task gate */
  501                 case SDT_SYS286IGT: /* system 286 interrupt gate */
  502                 case SDT_SYS286TGT: /* system 286 trap gate */
  503                 case SDT_SYSNULL2:  /* undefined by Intel */ 
  504                 case SDT_SYS386TSS: /* system 386 TSS available */
  505                 case SDT_SYSNULL3:  /* undefined by Intel */
  506                 case SDT_SYS386BSY: /* system 386 TSS busy */
  507                 case SDT_SYSNULL4:  /* undefined by Intel */ 
  508                 case SDT_SYS386IGT: /* system 386 interrupt gate */
  509                 case SDT_SYS386TGT: /* system 386 trap gate */
  510                 case SDT_SYS286CGT: /* system 286 call gate */ 
  511                 case SDT_SYS386CGT: /* system 386 call gate */
  512                         /* I can't think of any reason to allow a user proc
  513                          * to create a segment of these types.  They are
  514                          * for OS use only.
  515                          */
  516                         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  517                         return (EACCES);
  518                         /*NOTREACHED*/
  519 
  520                 /* memory segment types */
  521                 case SDT_MEMEC:   /* memory execute only conforming */
  522                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  523                 case SDT_MEMERC:  /* memory execute read conforming */
  524                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  525                          /* Must be "present" if executable and conforming. */
  526                         if (dp->sd.sd_p == 0) {
  527                                 kmem_free(kernel_map, (vm_offset_t)descs,
  528                                     descs_size);
  529                                 return (EACCES);
  530                         }
  531                         break;
  532                 case SDT_MEMRO:   /* memory read only */
  533                 case SDT_MEMROA:  /* memory read only accessed */
  534                 case SDT_MEMRW:   /* memory read write */
  535                 case SDT_MEMRWA:  /* memory read write accessed */
  536                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  537                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  538                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
  539                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  540                 case SDT_MEME:    /* memory execute only */ 
  541                 case SDT_MEMEA:   /* memory execute only accessed */
  542                 case SDT_MEMER:   /* memory execute read */
  543                 case SDT_MEMERA:  /* memory execute read accessed */
  544                         break;
  545                 default:
  546                         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  547                         return(EINVAL);
  548                         /*NOTREACHED*/
  549                 }
  550 
  551                 /* Only user (ring-3) descriptors may be present. */
  552                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL)) {
  553                         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  554                         return (EACCES);
  555                 }
  556         }
  557 
  558         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
  559                 /* Allocate a free slot */
  560                 pldt = mdp->md_ldt;
  561                 if (pldt == NULL) {
  562                         error = i386_ldt_grow(td, NLDT+1);
  563                         if (error) {
  564                                 kmem_free(kernel_map, (vm_offset_t)descs,
  565                                     descs_size);
  566                                 return (error);
  567                         }
  568                         pldt = mdp->md_ldt;
  569                 }
  570 again:
  571                 mtx_lock_spin(&sched_lock);
  572                 /*
  573                  * start scanning a bit up to leave room for NVidia and
  574                  * Wine, which still user the "Blat" method of allocation.
  575                  */
  576                 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
  577                 for (i = NLDT; i < pldt->ldt_len; ++i) {
  578                         if (dp->sd.sd_type == SDT_SYSNULL)
  579                                 break;
  580                         dp++;
  581                 }
  582                 if (i >= pldt->ldt_len) {
  583                         mtx_unlock_spin(&sched_lock);
  584                         error = i386_ldt_grow(td, pldt->ldt_len+1);
  585                         if (error) {
  586                                 kmem_free(kernel_map, (vm_offset_t)descs,
  587                                     descs_size);
  588                                 return (error);
  589                         }
  590                         goto again;
  591                 }
  592                 uap->start = i;
  593                 error = i386_set_ldt_data(td, i, 1, descs);
  594                 mtx_unlock_spin(&sched_lock);
  595         } else {
  596                 largest_ld = uap->start + uap->num;
  597                 error = i386_ldt_grow(td, largest_ld);
  598                 if (error == 0) {
  599                         mtx_lock_spin(&sched_lock);
  600                         error = i386_set_ldt_data(td, uap->start, uap->num,
  601                             descs);
  602                         mtx_unlock_spin(&sched_lock);
  603                 }
  604         }
  605         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  606         if (error == 0)
  607                 td->td_retval[0] = uap->start;
  608         return (error);
  609 }
  610 
  611 static int
  612 i386_set_ldt_data(struct thread *td, int start, int num,
  613         union descriptor *descs)
  614 {
  615         struct mdproc *mdp = &td->td_proc->p_md;
  616         struct proc_ldt *pldt = mdp->md_ldt;
  617 
  618         mtx_assert(&sched_lock, MA_OWNED);
  619 
  620         /* Fill in range */
  621         bcopy(descs,
  622             &((union descriptor *)(pldt->ldt_base))[start],
  623             num * sizeof(union descriptor));
  624         return (0);
  625 }
  626 
  627 static int
  628 i386_ldt_grow(struct thread *td, int len) 
  629 {
  630         struct mdproc *mdp = &td->td_proc->p_md;
  631         struct proc_ldt *pldt;
  632         caddr_t old_ldt_base;
  633         int old_ldt_len;
  634 
  635         if (len > MAX_LD)
  636                 return (ENOMEM);
  637         if (len < NLDT+1)
  638                 len = NLDT+1;
  639         pldt = mdp->md_ldt;
  640         /* allocate user ldt */
  641         if (!pldt || len > pldt->ldt_len) {
  642                 struct proc_ldt *new_ldt = user_ldt_alloc(mdp, len);
  643                 if (new_ldt == NULL)
  644                         return (ENOMEM);
  645                 pldt = mdp->md_ldt;
  646                 /* sched_lock was held by user_ldt_alloc */
  647                 if (pldt) {
  648                         if (new_ldt->ldt_len > pldt->ldt_len) {
  649                                 old_ldt_base = pldt->ldt_base;
  650                                 old_ldt_len = pldt->ldt_len;
  651                                 pldt->ldt_sd = new_ldt->ldt_sd;
  652                                 pldt->ldt_base = new_ldt->ldt_base;
  653                                 pldt->ldt_len = new_ldt->ldt_len;
  654                                 mtx_unlock_spin(&sched_lock);
  655                                 kmem_free(kernel_map, (vm_offset_t)old_ldt_base,
  656                                         old_ldt_len * sizeof(union descriptor));
  657                                 FREE(new_ldt, M_SUBPROC);
  658                                 mtx_lock_spin(&sched_lock);
  659                         } else {
  660                                 /*
  661                                  * If other threads already did the work,
  662                                  * do nothing
  663                                  */
  664                                 mtx_unlock_spin(&sched_lock);
  665                                 kmem_free(kernel_map,
  666                                    (vm_offset_t)new_ldt->ldt_base,
  667                                    new_ldt->ldt_len * sizeof(union descriptor));
  668                                 FREE(new_ldt, M_SUBPROC);
  669                                 return (0);
  670                         }
  671                 } else {
  672                         mdp->md_ldt = pldt = new_ldt;
  673                 }
  674 #ifdef SMP
  675                 mtx_unlock_spin(&sched_lock);
  676                 /* signal other cpus to reload ldt */
  677                 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
  678                     NULL, td);
  679 #else
  680                 set_user_ldt(mdp);
  681                 mtx_unlock_spin(&sched_lock);
  682 #endif
  683         }
  684         return (0);
  685 }

Cache object: 587e25fe1e56406a5247e9161c54579b


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