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

Cache object: f4cb3f10046fd78dd71c5715490b7ba5


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