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: releng/10.0/sys/i386/i386/sys_machdep.c 255677 2013-09-18 19:26:08Z pjd $");
   34 
   35 #include "opt_capsicum.h"
   36 #include "opt_kstack_pages.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/capability.h>
   40 #include <sys/systm.h>
   41 #include <sys/lock.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mutex.h>
   44 #include <sys/priv.h>
   45 #include <sys/proc.h>
   46 #include <sys/smp.h>
   47 #include <sys/sysproto.h>
   48 
   49 #include <vm/vm.h>
   50 #include <vm/pmap.h>
   51 #include <vm/vm_map.h>
   52 #include <vm/vm_extern.h>
   53 
   54 #include <machine/cpu.h>
   55 #include <machine/pcb.h>
   56 #include <machine/pcb_ext.h>
   57 #include <machine/proc.h>
   58 #include <machine/sysarch.h>
   59 
   60 #include <security/audit/audit.h>
   61 
   62 #ifdef XEN 
   63 #include <machine/xen/xenfunc.h>
   64 
   65 void i386_reset_ldt(struct proc_ldt *pldt); 
   66 
   67 void 
   68 i386_reset_ldt(struct proc_ldt *pldt) 
   69 { 
   70         xen_set_ldt((vm_offset_t)pldt->ldt_base, pldt->ldt_len); 
   71 } 
   72 #else  
   73 #define i386_reset_ldt(x) 
   74 #endif 
   75 
   76 #include <vm/vm_kern.h>         /* for kernel_map */
   77 
   78 #define MAX_LD 8192
   79 #define LD_PER_PAGE 512
   80 #define NEW_MAX_LD(num)  ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
   81 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
   82 #define NULL_LDT_BASE   ((caddr_t)NULL)
   83 
   84 #ifdef SMP
   85 static void set_user_ldt_rv(struct vmspace *vmsp);
   86 #endif
   87 static int i386_set_ldt_data(struct thread *, int start, int num,
   88         union descriptor *descs);
   89 static int i386_ldt_grow(struct thread *td, int len);
   90 
   91 #ifndef _SYS_SYSPROTO_H_
   92 struct sysarch_args {
   93         int op;
   94         char *parms;
   95 };
   96 #endif
   97 
   98 int
   99 sysarch(td, uap)
  100         struct thread *td;
  101         register struct sysarch_args *uap;
  102 {
  103         int error;
  104         union descriptor *lp;
  105         union {
  106                 struct i386_ldt_args largs;
  107                 struct i386_ioperm_args iargs;
  108         } kargs;
  109         uint32_t base;
  110         struct segment_descriptor sd, *sdp;
  111 
  112         AUDIT_ARG_CMD(uap->op);
  113 
  114 #ifdef CAPABILITY_MODE
  115         /*
  116          * When adding new operations, add a new case statement here to
  117          * explicitly indicate whether or not the operation is safe to
  118          * perform in capability mode.
  119          */
  120         if (IN_CAPABILITY_MODE(td)) {
  121                 switch (uap->op) {
  122                 case I386_GET_LDT:
  123                 case I386_SET_LDT:
  124                 case I386_GET_IOPERM:
  125                 case I386_GET_FSBASE:
  126                 case I386_SET_FSBASE:
  127                 case I386_GET_GSBASE:
  128                 case I386_SET_GSBASE:
  129                         break;
  130 
  131                 case I386_SET_IOPERM:
  132                 default:
  133 #ifdef KTRACE
  134                         if (KTRPOINT(td, KTR_CAPFAIL))
  135                                 ktrcapfail(CAPFAIL_SYSCALL, NULL, NULL);
  136 #endif
  137                         return (ECAPMODE);
  138                 }
  139         }
  140 #endif
  141 
  142         switch (uap->op) {
  143         case I386_GET_IOPERM:
  144         case I386_SET_IOPERM:
  145                 if ((error = copyin(uap->parms, &kargs.iargs,
  146                     sizeof(struct i386_ioperm_args))) != 0)
  147                         return (error);
  148                 break;
  149         case I386_GET_LDT:
  150         case I386_SET_LDT:
  151                 if ((error = copyin(uap->parms, &kargs.largs,
  152                     sizeof(struct i386_ldt_args))) != 0)
  153                         return (error);
  154                 if (kargs.largs.num > MAX_LD || kargs.largs.num <= 0)
  155                         return (EINVAL);
  156                 break;
  157         default:
  158                 break;
  159         }
  160 
  161         switch(uap->op) {
  162         case I386_GET_LDT:
  163                 error = i386_get_ldt(td, &kargs.largs);
  164                 break;
  165         case I386_SET_LDT:
  166                 if (kargs.largs.descs != NULL) {
  167                         lp = (union descriptor *)kmem_malloc(kernel_arena,
  168                             kargs.largs.num * sizeof(union descriptor),
  169                             M_WAITOK);
  170                         if (lp == NULL) {
  171                                 error = ENOMEM;
  172                                 break;
  173                         }
  174                         error = copyin(kargs.largs.descs, lp,
  175                             kargs.largs.num * sizeof(union descriptor));
  176                         if (error == 0)
  177                                 error = i386_set_ldt(td, &kargs.largs, lp);
  178                         kmem_free(kernel_arena, (vm_offset_t)lp,
  179                             kargs.largs.num * sizeof(union descriptor));
  180                 } else {
  181                         error = i386_set_ldt(td, &kargs.largs, NULL);
  182                 }
  183                 break;
  184         case I386_GET_IOPERM:
  185                 error = i386_get_ioperm(td, &kargs.iargs);
  186                 if (error == 0)
  187                         error = copyout(&kargs.iargs, uap->parms,
  188                             sizeof(struct i386_ioperm_args));
  189                 break;
  190         case I386_SET_IOPERM:
  191                 error = i386_set_ioperm(td, &kargs.iargs);
  192                 break;
  193         case I386_VM86:
  194                 error = vm86_sysarch(td, uap->parms);
  195                 break;
  196         case I386_GET_FSBASE:
  197                 sdp = &td->td_pcb->pcb_fsd;
  198                 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
  199                 error = copyout(&base, uap->parms, sizeof(base));
  200                 break;
  201         case I386_SET_FSBASE:
  202                 error = copyin(uap->parms, &base, sizeof(base));
  203                 if (!error) {
  204                         /*
  205                          * Construct a descriptor and store it in the pcb for
  206                          * the next context switch.  Also store it in the gdt
  207                          * so that the load of tf_fs into %fs will activate it
  208                          * at return to userland.
  209                          */
  210                         sd.sd_lobase = base & 0xffffff;
  211                         sd.sd_hibase = (base >> 24) & 0xff;
  212 #ifdef XEN
  213                         /* need to do nosegneg like Linux */
  214                         sd.sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
  215 #else                   
  216                         sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
  217 #endif
  218                         sd.sd_hilimit = 0xf;
  219                         sd.sd_type  = SDT_MEMRWA;
  220                         sd.sd_dpl   = SEL_UPL;
  221                         sd.sd_p     = 1;
  222                         sd.sd_xx    = 0;
  223                         sd.sd_def32 = 1;
  224                         sd.sd_gran  = 1;
  225                         critical_enter();
  226                         td->td_pcb->pcb_fsd = sd;
  227 #ifdef XEN
  228                         HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[0]),
  229                             *(uint64_t *)&sd);
  230 #else
  231                         PCPU_GET(fsgs_gdt)[0] = sd;
  232 #endif
  233                         critical_exit();
  234                         td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
  235                 }
  236                 break;
  237         case I386_GET_GSBASE:
  238                 sdp = &td->td_pcb->pcb_gsd;
  239                 base = sdp->sd_hibase << 24 | sdp->sd_lobase;
  240                 error = copyout(&base, uap->parms, sizeof(base));
  241                 break;
  242         case I386_SET_GSBASE:
  243                 error = copyin(uap->parms, &base, sizeof(base));
  244                 if (!error) {
  245                         /*
  246                          * Construct a descriptor and store it in the pcb for
  247                          * the next context switch.  Also store it in the gdt
  248                          * because we have to do a load_gs() right now.
  249                          */
  250                         sd.sd_lobase = base & 0xffffff;
  251                         sd.sd_hibase = (base >> 24) & 0xff;
  252 
  253 #ifdef XEN
  254                         /* need to do nosegneg like Linux */
  255                         sd.sd_lolimit = (HYPERVISOR_VIRT_START >> 12) & 0xffff;
  256 #else   
  257                         sd.sd_lolimit = 0xffff; /* 4GB limit, wraps around */
  258 #endif
  259                         sd.sd_hilimit = 0xf;
  260                         sd.sd_type  = SDT_MEMRWA;
  261                         sd.sd_dpl   = SEL_UPL;
  262                         sd.sd_p     = 1;
  263                         sd.sd_xx    = 0;
  264                         sd.sd_def32 = 1;
  265                         sd.sd_gran  = 1;
  266                         critical_enter();
  267                         td->td_pcb->pcb_gsd = sd;
  268 #ifdef XEN
  269                         HYPERVISOR_update_descriptor(vtomach(&PCPU_GET(fsgs_gdt)[1]),
  270                             *(uint64_t *)&sd);
  271 #else                   
  272                         PCPU_GET(fsgs_gdt)[1] = sd;
  273 #endif
  274                         critical_exit();
  275                         load_gs(GSEL(GUGS_SEL, SEL_UPL));
  276                 }
  277                 break;
  278         default:
  279                 error = EINVAL;
  280                 break;
  281         }
  282         return (error);
  283 }
  284 
  285 int
  286 i386_extend_pcb(struct thread *td)
  287 {
  288         int i, offset;
  289         u_long *addr;
  290         struct pcb_ext *ext;
  291         struct soft_segment_descriptor ssd = {
  292                 0,                      /* segment base address (overwritten) */
  293                 ctob(IOPAGES + 1) - 1,  /* length */
  294                 SDT_SYS386TSS,          /* segment type */
  295                 0,                      /* priority level */
  296                 1,                      /* descriptor present */
  297                 0, 0,
  298                 0,                      /* default 32 size */
  299                 0                       /* granularity */
  300         };
  301 
  302         ext = (struct pcb_ext *)kmem_malloc(kernel_arena, ctob(IOPAGES+1),
  303             M_WAITOK);
  304         if (ext == 0)
  305                 return (ENOMEM);
  306         bzero(ext, sizeof(struct pcb_ext)); 
  307         /* -16 is so we can convert a trapframe into vm86trapframe inplace */
  308         ext->ext_tss.tss_esp0 = td->td_kstack + ctob(KSTACK_PAGES) -
  309             sizeof(struct pcb) - 16;
  310         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
  311         /*
  312          * The last byte of the i/o map must be followed by an 0xff byte.
  313          * We arbitrarily allocate 16 bytes here, to keep the starting
  314          * address on a doubleword boundary.
  315          */
  316         offset = PAGE_SIZE - 16;
  317         ext->ext_tss.tss_ioopt = 
  318             (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
  319         ext->ext_iomap = (caddr_t)ext + offset;
  320         ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
  321 
  322         addr = (u_long *)ext->ext_vm86.vm86_intmap;
  323         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
  324                 *addr++ = ~0;
  325 
  326         ssd.ssd_base = (unsigned)&ext->ext_tss;
  327         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
  328         ssdtosd(&ssd, &ext->ext_tssd);
  329 
  330         KASSERT(td == curthread, ("giving TSS to !curthread"));
  331         KASSERT(td->td_pcb->pcb_ext == 0, ("already have a TSS!"));
  332 
  333         /* Switch to the new TSS. */
  334         critical_enter();
  335         td->td_pcb->pcb_ext = ext;
  336         PCPU_SET(private_tss, 1);
  337         *PCPU_GET(tss_gdt) = ext->ext_tssd;
  338         ltr(GSEL(GPROC0_SEL, SEL_KPL));
  339         critical_exit();
  340 
  341         return 0;
  342 }
  343 
  344 int
  345 i386_set_ioperm(td, uap)
  346         struct thread *td;
  347         struct i386_ioperm_args *uap;
  348 {
  349         int i, error;
  350         char *iomap;
  351 
  352         if ((error = priv_check(td, PRIV_IO)) != 0)
  353                 return (error);
  354         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  355                 return (error);
  356         /*
  357          * XXX 
  358          * While this is restricted to root, we should probably figure out
  359          * whether any other driver is using this i/o address, as so not to
  360          * cause confusion.  This probably requires a global 'usage registry'.
  361          */
  362 
  363         if (td->td_pcb->pcb_ext == 0)
  364                 if ((error = i386_extend_pcb(td)) != 0)
  365                         return (error);
  366         iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
  367 
  368         if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
  369                 return (EINVAL);
  370 
  371         for (i = uap->start; i < uap->start + uap->length; i++) {
  372                 if (uap->enable)
  373                         iomap[i >> 3] &= ~(1 << (i & 7));
  374                 else
  375                         iomap[i >> 3] |= (1 << (i & 7));
  376         }
  377         return (error);
  378 }
  379 
  380 int
  381 i386_get_ioperm(td, uap)
  382         struct thread *td;
  383         struct i386_ioperm_args *uap;
  384 {
  385         int i, state;
  386         char *iomap;
  387 
  388         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
  389                 return (EINVAL);
  390 
  391         if (td->td_pcb->pcb_ext == 0) {
  392                 uap->length = 0;
  393                 goto done;
  394         }
  395 
  396         iomap = (char *)td->td_pcb->pcb_ext->ext_iomap;
  397 
  398         i = uap->start;
  399         state = (iomap[i >> 3] >> (i & 7)) & 1;
  400         uap->enable = !state;
  401         uap->length = 1;
  402 
  403         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
  404                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
  405                         break;
  406                 uap->length++;
  407         }
  408 
  409 done:
  410         return (0);
  411 }
  412 
  413 /*
  414  * Update the GDT entry pointing to the LDT to point to the LDT of the
  415  * current process. Manage dt_lock holding/unholding autonomously.
  416  */   
  417 void
  418 set_user_ldt(struct mdproc *mdp)
  419 {
  420         struct proc_ldt *pldt;
  421         int dtlocked;
  422 
  423         dtlocked = 0;
  424         if (!mtx_owned(&dt_lock)) {
  425                 mtx_lock_spin(&dt_lock);
  426                 dtlocked = 1;
  427         }
  428 
  429         pldt = mdp->md_ldt;
  430 #ifdef XEN
  431         i386_reset_ldt(pldt);
  432         PCPU_SET(currentldt, (int)pldt);
  433 #else   
  434 #ifdef SMP
  435         gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
  436 #else
  437         gdt[GUSERLDT_SEL].sd = pldt->ldt_sd;
  438 #endif
  439         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
  440         PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
  441 #endif /* XEN */ 
  442         if (dtlocked)
  443                 mtx_unlock_spin(&dt_lock);
  444 }
  445 
  446 #ifdef SMP
  447 static void
  448 set_user_ldt_rv(struct vmspace *vmsp)
  449 {
  450         struct thread *td;
  451 
  452         td = curthread;
  453         if (vmsp != td->td_proc->p_vmspace)
  454                 return;
  455 
  456         set_user_ldt(&td->td_proc->p_md);
  457 }
  458 #endif
  459 
  460 #ifdef XEN
  461 
  462 /* 
  463  * dt_lock must be held. Returns with dt_lock held. 
  464  */ 
  465 struct proc_ldt * 
  466 user_ldt_alloc(struct mdproc *mdp, int len) 
  467 { 
  468         struct proc_ldt *pldt, *new_ldt; 
  469  
  470         mtx_assert(&dt_lock, MA_OWNED); 
  471         mtx_unlock_spin(&dt_lock); 
  472         new_ldt = malloc(sizeof(struct proc_ldt), 
  473                 M_SUBPROC, M_WAITOK); 
  474  
  475         new_ldt->ldt_len = len = NEW_MAX_LD(len); 
  476         new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena, 
  477                 round_page(len * sizeof(union descriptor)), M_WAITOK);
  478         if (new_ldt->ldt_base == NULL) { 
  479                 free(new_ldt, M_SUBPROC);
  480                 mtx_lock_spin(&dt_lock);
  481                 return (NULL);
  482         } 
  483         new_ldt->ldt_refcnt = 1; 
  484         new_ldt->ldt_active = 0; 
  485  
  486         mtx_lock_spin(&dt_lock);
  487         if ((pldt = mdp->md_ldt)) { 
  488                 if (len > pldt->ldt_len) 
  489                         len = pldt->ldt_len; 
  490                 bcopy(pldt->ldt_base, new_ldt->ldt_base, 
  491                     len * sizeof(union descriptor)); 
  492         } else { 
  493                 bcopy(ldt, new_ldt->ldt_base, PAGE_SIZE); 
  494         } 
  495         mtx_unlock_spin(&dt_lock);  /* XXX kill once pmap locking fixed. */
  496         pmap_map_readonly(kernel_pmap, (vm_offset_t)new_ldt->ldt_base, 
  497                           new_ldt->ldt_len*sizeof(union descriptor)); 
  498         mtx_lock_spin(&dt_lock);  /* XXX kill once pmap locking fixed. */
  499         return (new_ldt);
  500 } 
  501 #else
  502 /*
  503  * dt_lock must be held. Returns with dt_lock held.
  504  */
  505 struct proc_ldt *
  506 user_ldt_alloc(struct mdproc *mdp, int len)
  507 {
  508         struct proc_ldt *pldt, *new_ldt;
  509 
  510         mtx_assert(&dt_lock, MA_OWNED);
  511         mtx_unlock_spin(&dt_lock);
  512         new_ldt = malloc(sizeof(struct proc_ldt),
  513                 M_SUBPROC, M_WAITOK);
  514 
  515         new_ldt->ldt_len = len = NEW_MAX_LD(len);
  516         new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
  517                 len * sizeof(union descriptor), M_WAITOK);
  518         if (new_ldt->ldt_base == NULL) {
  519                 free(new_ldt, M_SUBPROC);
  520                 mtx_lock_spin(&dt_lock);
  521                 return (NULL);
  522         }
  523         new_ldt->ldt_refcnt = 1;
  524         new_ldt->ldt_active = 0;
  525 
  526         mtx_lock_spin(&dt_lock);
  527         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
  528         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
  529         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
  530 
  531         if ((pldt = mdp->md_ldt) != NULL) {
  532                 if (len > pldt->ldt_len)
  533                         len = pldt->ldt_len;
  534                 bcopy(pldt->ldt_base, new_ldt->ldt_base,
  535                     len * sizeof(union descriptor));
  536         } else
  537                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
  538         
  539         return (new_ldt);
  540 }
  541 #endif /* !XEN */
  542 
  543 /*
  544  * Must be called with dt_lock held.  Returns with dt_lock unheld.
  545  */
  546 void
  547 user_ldt_free(struct thread *td)
  548 {
  549         struct mdproc *mdp = &td->td_proc->p_md;
  550         struct proc_ldt *pldt;
  551 
  552         mtx_assert(&dt_lock, MA_OWNED);
  553         if ((pldt = mdp->md_ldt) == NULL) {
  554                 mtx_unlock_spin(&dt_lock);
  555                 return;
  556         }
  557 
  558         if (td == curthread) {
  559 #ifdef XEN
  560                 i386_reset_ldt(&default_proc_ldt);
  561                 PCPU_SET(currentldt, (int)&default_proc_ldt);
  562 #else
  563                 lldt(_default_ldt);
  564                 PCPU_SET(currentldt, _default_ldt);
  565 #endif
  566         }
  567 
  568         mdp->md_ldt = NULL;
  569         user_ldt_deref(pldt);
  570 }
  571 
  572 void
  573 user_ldt_deref(struct proc_ldt *pldt)
  574 {
  575 
  576         mtx_assert(&dt_lock, MA_OWNED);
  577         if (--pldt->ldt_refcnt == 0) {
  578                 mtx_unlock_spin(&dt_lock);
  579                 kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base,
  580                         pldt->ldt_len * sizeof(union descriptor));
  581                 free(pldt, M_SUBPROC);
  582         } else
  583                 mtx_unlock_spin(&dt_lock);
  584 }
  585 
  586 /*
  587  * Note for the authors of compat layers (linux, etc): copyout() in
  588  * the function below is not a problem since it presents data in
  589  * arch-specific format (i.e. i386-specific in this case), not in
  590  * the OS-specific one.
  591  */
  592 int
  593 i386_get_ldt(td, uap)
  594         struct thread *td;
  595         struct i386_ldt_args *uap;
  596 {
  597         int error = 0;
  598         struct proc_ldt *pldt;
  599         int nldt, num;
  600         union descriptor *lp;
  601 
  602 #ifdef  DEBUG
  603         printf("i386_get_ldt: start=%d num=%d descs=%p\n",
  604             uap->start, uap->num, (void *)uap->descs);
  605 #endif
  606 
  607         mtx_lock_spin(&dt_lock);
  608         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
  609                 nldt = pldt->ldt_len;
  610                 lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
  611                 mtx_unlock_spin(&dt_lock);
  612                 num = min(uap->num, nldt);
  613         } else {
  614                 mtx_unlock_spin(&dt_lock);
  615                 nldt = sizeof(ldt)/sizeof(ldt[0]);
  616                 num = min(uap->num, nldt);
  617                 lp = &ldt[uap->start];
  618         }
  619 
  620         if ((uap->start > (unsigned int)nldt) ||
  621             ((unsigned int)num > (unsigned int)nldt) ||
  622             ((unsigned int)(uap->start + num) > (unsigned int)nldt))
  623                 return(EINVAL);
  624 
  625         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
  626         if (!error)
  627                 td->td_retval[0] = num;
  628 
  629         return(error);
  630 }
  631 
  632 int
  633 i386_set_ldt(td, uap, descs)
  634         struct thread *td;
  635         struct i386_ldt_args *uap;
  636         union descriptor *descs;
  637 {
  638         int error = 0, i;
  639         int largest_ld;
  640         struct mdproc *mdp = &td->td_proc->p_md;
  641         struct proc_ldt *pldt;
  642         union descriptor *dp;
  643 
  644 #ifdef  DEBUG
  645         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
  646             uap->start, uap->num, (void *)uap->descs);
  647 #endif
  648 
  649         if (descs == NULL) {
  650                 /* Free descriptors */
  651                 if (uap->start == 0 && uap->num == 0) {
  652                         /*
  653                          * Treat this as a special case, so userland needn't
  654                          * know magic number NLDT.
  655                          */
  656                         uap->start = NLDT;
  657                         uap->num = MAX_LD - NLDT;
  658                 }
  659                 if (uap->num == 0)
  660                         return (EINVAL);
  661                 mtx_lock_spin(&dt_lock);
  662                 if ((pldt = mdp->md_ldt) == NULL ||
  663                     uap->start >= pldt->ldt_len) {
  664                         mtx_unlock_spin(&dt_lock);
  665                         return (0);
  666                 }
  667                 largest_ld = uap->start + uap->num;
  668                 if (largest_ld > pldt->ldt_len)
  669                         largest_ld = pldt->ldt_len;
  670                 i = largest_ld - uap->start;
  671                 bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
  672                     sizeof(union descriptor) * i);
  673                 mtx_unlock_spin(&dt_lock);
  674                 return (0);
  675         }
  676 
  677         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
  678                 /* verify range of descriptors to modify */
  679                 largest_ld = uap->start + uap->num;
  680                 if (uap->start >= MAX_LD || largest_ld > MAX_LD) {
  681                         return (EINVAL);
  682                 }
  683         }
  684 
  685         /* Check descriptors for access violations */
  686         for (i = 0; i < uap->num; i++) {
  687                 dp = &descs[i];
  688 
  689                 switch (dp->sd.sd_type) {
  690                 case SDT_SYSNULL:       /* system null */ 
  691                         dp->sd.sd_p = 0;
  692                         break;
  693                 case SDT_SYS286TSS: /* system 286 TSS available */
  694                 case SDT_SYSLDT:    /* system local descriptor table */
  695                 case SDT_SYS286BSY: /* system 286 TSS busy */
  696                 case SDT_SYSTASKGT: /* system task gate */
  697                 case SDT_SYS286IGT: /* system 286 interrupt gate */
  698                 case SDT_SYS286TGT: /* system 286 trap gate */
  699                 case SDT_SYSNULL2:  /* undefined by Intel */ 
  700                 case SDT_SYS386TSS: /* system 386 TSS available */
  701                 case SDT_SYSNULL3:  /* undefined by Intel */
  702                 case SDT_SYS386BSY: /* system 386 TSS busy */
  703                 case SDT_SYSNULL4:  /* undefined by Intel */ 
  704                 case SDT_SYS386IGT: /* system 386 interrupt gate */
  705                 case SDT_SYS386TGT: /* system 386 trap gate */
  706                 case SDT_SYS286CGT: /* system 286 call gate */ 
  707                 case SDT_SYS386CGT: /* system 386 call gate */
  708                         /* I can't think of any reason to allow a user proc
  709                          * to create a segment of these types.  They are
  710                          * for OS use only.
  711                          */
  712                         return (EACCES);
  713                         /*NOTREACHED*/
  714 
  715                 /* memory segment types */
  716                 case SDT_MEMEC:   /* memory execute only conforming */
  717                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  718                 case SDT_MEMERC:  /* memory execute read conforming */
  719                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  720                          /* Must be "present" if executable and conforming. */
  721                         if (dp->sd.sd_p == 0)
  722                                 return (EACCES);
  723                         break;
  724                 case SDT_MEMRO:   /* memory read only */
  725                 case SDT_MEMROA:  /* memory read only accessed */
  726                 case SDT_MEMRW:   /* memory read write */
  727                 case SDT_MEMRWA:  /* memory read write accessed */
  728                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  729                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  730                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
  731                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  732                 case SDT_MEME:    /* memory execute only */ 
  733                 case SDT_MEMEA:   /* memory execute only accessed */
  734                 case SDT_MEMER:   /* memory execute read */
  735                 case SDT_MEMERA:  /* memory execute read accessed */
  736                         break;
  737                 default:
  738                         return(EINVAL);
  739                         /*NOTREACHED*/
  740                 }
  741 
  742                 /* Only user (ring-3) descriptors may be present. */
  743                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
  744                         return (EACCES);
  745         }
  746 
  747         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
  748                 /* Allocate a free slot */
  749                 mtx_lock_spin(&dt_lock);
  750                 if ((pldt = mdp->md_ldt) == NULL) {
  751                         if ((error = i386_ldt_grow(td, NLDT + 1))) {
  752                                 mtx_unlock_spin(&dt_lock);
  753                                 return (error);
  754                         }
  755                         pldt = mdp->md_ldt;
  756                 }
  757 again:
  758                 /*
  759                  * start scanning a bit up to leave room for NVidia and
  760                  * Wine, which still user the "Blat" method of allocation.
  761                  */
  762                 dp = &((union descriptor *)(pldt->ldt_base))[NLDT];
  763                 for (i = NLDT; i < pldt->ldt_len; ++i) {
  764                         if (dp->sd.sd_type == SDT_SYSNULL)
  765                                 break;
  766                         dp++;
  767                 }
  768                 if (i >= pldt->ldt_len) {
  769                         if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
  770                                 mtx_unlock_spin(&dt_lock);
  771                                 return (error);
  772                         }
  773                         goto again;
  774                 }
  775                 uap->start = i;
  776                 error = i386_set_ldt_data(td, i, 1, descs);
  777                 mtx_unlock_spin(&dt_lock);
  778         } else {
  779                 largest_ld = uap->start + uap->num;
  780                 mtx_lock_spin(&dt_lock);
  781                 if (!(error = i386_ldt_grow(td, largest_ld))) {
  782                         error = i386_set_ldt_data(td, uap->start, uap->num,
  783                             descs);
  784                 }
  785                 mtx_unlock_spin(&dt_lock);
  786         }
  787         if (error == 0)
  788                 td->td_retval[0] = uap->start;
  789         return (error);
  790 }
  791 #ifdef XEN
  792 static int
  793 i386_set_ldt_data(struct thread *td, int start, int num,
  794         union descriptor *descs)
  795 {
  796         struct mdproc *mdp = &td->td_proc->p_md;
  797         struct proc_ldt *pldt = mdp->md_ldt;
  798 
  799         mtx_assert(&dt_lock, MA_OWNED);
  800 
  801         while (num) {
  802                 xen_update_descriptor(
  803                     &((union descriptor *)(pldt->ldt_base))[start],
  804                     descs);
  805                 num--;
  806                 start++;
  807                 descs++;
  808         }
  809         return (0);
  810 }
  811 #else
  812 static int
  813 i386_set_ldt_data(struct thread *td, int start, int num,
  814         union descriptor *descs)
  815 {
  816         struct mdproc *mdp = &td->td_proc->p_md;
  817         struct proc_ldt *pldt = mdp->md_ldt;
  818 
  819         mtx_assert(&dt_lock, MA_OWNED);
  820 
  821         /* Fill in range */
  822         bcopy(descs,
  823             &((union descriptor *)(pldt->ldt_base))[start],
  824             num * sizeof(union descriptor));
  825         return (0);
  826 }
  827 #endif /* !XEN */
  828 
  829 static int
  830 i386_ldt_grow(struct thread *td, int len) 
  831 {
  832         struct mdproc *mdp = &td->td_proc->p_md;
  833         struct proc_ldt *new_ldt, *pldt;
  834         caddr_t old_ldt_base = NULL_LDT_BASE;
  835         int old_ldt_len = 0;
  836 
  837         mtx_assert(&dt_lock, MA_OWNED);
  838 
  839         if (len > MAX_LD)
  840                 return (ENOMEM);
  841         if (len < NLDT + 1)
  842                 len = NLDT + 1;
  843 
  844         /* Allocate a user ldt. */
  845         if ((pldt = mdp->md_ldt) == NULL || len > pldt->ldt_len) {
  846                 new_ldt = user_ldt_alloc(mdp, len);
  847                 if (new_ldt == NULL)
  848                         return (ENOMEM);
  849                 pldt = mdp->md_ldt;
  850 
  851                 if (pldt != NULL) {
  852                         if (new_ldt->ldt_len <= pldt->ldt_len) {
  853                                 /*
  854                                  * We just lost the race for allocation, so
  855                                  * free the new object and return.
  856                                  */
  857                                 mtx_unlock_spin(&dt_lock);
  858                                 kmem_free(kernel_arena,
  859                                    (vm_offset_t)new_ldt->ldt_base,
  860                                    new_ldt->ldt_len * sizeof(union descriptor));
  861                                 free(new_ldt, M_SUBPROC);
  862                                 mtx_lock_spin(&dt_lock);
  863                                 return (0);
  864                         }
  865 
  866                         /*
  867                          * We have to substitute the current LDT entry for
  868                          * curproc with the new one since its size grew.
  869                          */
  870                         old_ldt_base = pldt->ldt_base;
  871                         old_ldt_len = pldt->ldt_len;
  872                         pldt->ldt_sd = new_ldt->ldt_sd;
  873                         pldt->ldt_base = new_ldt->ldt_base;
  874                         pldt->ldt_len = new_ldt->ldt_len;
  875                 } else
  876                         mdp->md_ldt = pldt = new_ldt;
  877 #ifdef SMP
  878                 /*
  879                  * Signal other cpus to reload ldt.  We need to unlock dt_lock
  880                  * here because other CPU will contest on it since their
  881                  * curthreads won't hold the lock and will block when trying
  882                  * to acquire it.
  883                  */
  884                 mtx_unlock_spin(&dt_lock);
  885                 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
  886                     NULL, td->td_proc->p_vmspace);
  887 #else
  888                 set_user_ldt(&td->td_proc->p_md);
  889                 mtx_unlock_spin(&dt_lock);
  890 #endif
  891                 if (old_ldt_base != NULL_LDT_BASE) {
  892                         kmem_free(kernel_arena, (vm_offset_t)old_ldt_base,
  893                             old_ldt_len * sizeof(union descriptor));
  894                         free(new_ldt, M_SUBPROC);
  895                 }
  896                 mtx_lock_spin(&dt_lock);
  897         }
  898         return (0);
  899 }

Cache object: 747a4882365036157296afbc3a1b4609


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