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/amd64/amd64/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) 2003 Peter Wemm.
    3  * Copyright (c) 1990 The Regents of the University of California.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 4. Neither the name of the University nor the names of its contributors
   15  *    may be used to endorse or promote products derived from this software
   16  *    without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  *
   30  *      from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/8.3/sys/amd64/amd64/sys_machdep.c 220651 2011-04-15 10:33:20Z kib $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/kernel.h>
   39 #include <sys/lock.h>
   40 #include <sys/malloc.h>
   41 #include <sys/mutex.h>
   42 #include <sys/priv.h>
   43 #include <sys/proc.h>
   44 #include <sys/sysproto.h>
   45 #include <sys/uio.h>
   46 
   47 #include <vm/vm.h>
   48 #include <vm/pmap.h>
   49 #include <vm/vm_kern.h>         /* for kernel_map */
   50 #include <vm/vm_extern.h>
   51 
   52 #include <machine/frame.h>
   53 #include <machine/md_var.h>
   54 #include <machine/pcb.h>
   55 #include <machine/specialreg.h>
   56 #include <machine/sysarch.h>
   57 #include <machine/tss.h>
   58 #include <machine/vmparam.h>
   59 
   60 #include <security/audit/audit.h>
   61 
   62 #define MAX_LD          8192
   63 
   64 int max_ldt_segment = 1024;
   65 SYSCTL_INT(_machdep, OID_AUTO, max_ldt_segment, CTLFLAG_RDTUN,
   66     &max_ldt_segment, 0,
   67     "Maximum number of allowed LDT segments in the single address space");
   68 
   69 static void
   70 max_ldt_segment_init(void *arg __unused)
   71 {
   72 
   73         TUNABLE_INT_FETCH("machdep.max_ldt_segment", &max_ldt_segment);
   74         if (max_ldt_segment <= 0)
   75                 max_ldt_segment = 1;
   76         if (max_ldt_segment > MAX_LD)
   77                 max_ldt_segment = MAX_LD;
   78 }
   79 SYSINIT(maxldt, SI_SUB_VM_CONF, SI_ORDER_ANY, max_ldt_segment_init, NULL);
   80 
   81 #ifdef notyet
   82 #ifdef SMP
   83 static void set_user_ldt_rv(struct vmspace *vmsp);
   84 #endif
   85 #endif
   86 static void user_ldt_derefl(struct proc_ldt *pldt);
   87 
   88 #ifndef _SYS_SYSPROTO_H_
   89 struct sysarch_args {
   90         int op;
   91         char *parms;
   92 };
   93 #endif
   94 
   95 int
   96 sysarch_ldt(struct thread *td, struct sysarch_args *uap, int uap_space)
   97 {
   98         struct i386_ldt_args *largs, la;
   99         struct user_segment_descriptor *lp;
  100         int error = 0;
  101 
  102         /*
  103          * XXXKIB check that the BSM generation code knows to encode
  104          * the op argument.
  105          */
  106         AUDIT_ARG_CMD(uap->op);
  107         if (uap_space == UIO_USERSPACE) {
  108                 error = copyin(uap->parms, &la, sizeof(struct i386_ldt_args));
  109                 if (error != 0)
  110                         return (error);
  111                 largs = &la;
  112         } else
  113                 largs = (struct i386_ldt_args *)uap->parms;
  114         if (largs->num > max_ldt_segment || largs->num <= 0)
  115                 return (EINVAL);
  116 
  117         switch (uap->op) {
  118         case I386_GET_LDT:
  119                 error = amd64_get_ldt(td, largs);
  120                 break;
  121         case I386_SET_LDT:
  122                 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  123                 if (largs->descs != NULL) {
  124                         lp = malloc(largs->num * sizeof(struct
  125                             user_segment_descriptor), M_TEMP, M_WAITOK);
  126                         error = copyin(largs->descs, lp, largs->num *
  127                             sizeof(struct user_segment_descriptor));
  128                         if (error == 0)
  129                                 error = amd64_set_ldt(td, largs, lp);
  130                         free(lp, M_TEMP);
  131                 } else {
  132                         error = amd64_set_ldt(td, largs, NULL);
  133                 }
  134                 break;
  135         }
  136         return (error);
  137 }
  138 
  139 void
  140 update_gdt_gsbase(struct thread *td, uint32_t base)
  141 {
  142         struct user_segment_descriptor *sd;
  143 
  144         if (td != curthread)
  145                 return;
  146         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  147         critical_enter();
  148         sd = PCPU_GET(gs32p);
  149         sd->sd_lobase = base & 0xffffff;
  150         sd->sd_hibase = (base >> 24) & 0xff;
  151         critical_exit();
  152 }
  153 
  154 void
  155 update_gdt_fsbase(struct thread *td, uint32_t base)
  156 {
  157         struct user_segment_descriptor *sd;
  158 
  159         if (td != curthread)
  160                 return;
  161         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  162         critical_enter();
  163         sd = PCPU_GET(fs32p);
  164         sd->sd_lobase = base & 0xffffff;
  165         sd->sd_hibase = (base >> 24) & 0xff;
  166         critical_exit();
  167 }
  168 
  169 int
  170 sysarch(td, uap)
  171         struct thread *td;
  172         register struct sysarch_args *uap;
  173 {
  174         int error = 0;
  175         struct pcb *pcb = curthread->td_pcb;
  176         uint32_t i386base;
  177         uint64_t a64base;
  178         struct i386_ioperm_args iargs;
  179 
  180         if (uap->op == I386_GET_LDT || uap->op == I386_SET_LDT)
  181                 return (sysarch_ldt(td, uap, UIO_USERSPACE));
  182         /*
  183          * XXXKIB check that the BSM generation code knows to encode
  184          * the op argument.
  185          */
  186         AUDIT_ARG_CMD(uap->op);
  187         switch (uap->op) {
  188         case I386_GET_IOPERM:
  189         case I386_SET_IOPERM:
  190                 if ((error = copyin(uap->parms, &iargs,
  191                     sizeof(struct i386_ioperm_args))) != 0)
  192                         return (error);
  193                 break;
  194         default:
  195                 break;
  196         }
  197 
  198         switch (uap->op) {
  199         case I386_GET_IOPERM:
  200                 error = amd64_get_ioperm(td, &iargs);
  201                 if (error == 0)
  202                         error = copyout(&iargs, uap->parms,
  203                             sizeof(struct i386_ioperm_args));
  204                 break;
  205         case I386_SET_IOPERM:
  206                 error = amd64_set_ioperm(td, &iargs);
  207                 break;
  208         case I386_GET_FSBASE:
  209                 i386base = pcb->pcb_fsbase;
  210                 error = copyout(&i386base, uap->parms, sizeof(i386base));
  211                 break;
  212         case I386_SET_FSBASE:
  213                 error = copyin(uap->parms, &i386base, sizeof(i386base));
  214                 if (!error) {
  215                         pcb->pcb_fsbase = i386base;
  216                         td->td_frame->tf_fs = _ufssel;
  217                         update_gdt_fsbase(td, i386base);
  218                 }
  219                 break;
  220         case I386_GET_GSBASE:
  221                 i386base = pcb->pcb_gsbase;
  222                 error = copyout(&i386base, uap->parms, sizeof(i386base));
  223                 break;
  224         case I386_SET_GSBASE:
  225                 error = copyin(uap->parms, &i386base, sizeof(i386base));
  226                 if (!error) {
  227                         pcb->pcb_gsbase = i386base;
  228                         td->td_frame->tf_gs = _ugssel;
  229                         update_gdt_gsbase(td, i386base);
  230                 }
  231                 break;
  232         case AMD64_GET_FSBASE:
  233                 error = copyout(&pcb->pcb_fsbase, uap->parms, sizeof(pcb->pcb_fsbase));
  234                 break;
  235                 
  236         case AMD64_SET_FSBASE:
  237                 error = copyin(uap->parms, &a64base, sizeof(a64base));
  238                 if (!error) {
  239                         if (a64base < VM_MAXUSER_ADDRESS) {
  240                                 pcb->pcb_fsbase = a64base;
  241                                 set_pcb_flags(pcb, PCB_FULL_IRET);
  242                                 td->td_frame->tf_fs = _ufssel;
  243                         } else
  244                                 error = EINVAL;
  245                 }
  246                 break;
  247 
  248         case AMD64_GET_GSBASE:
  249                 error = copyout(&pcb->pcb_gsbase, uap->parms, sizeof(pcb->pcb_gsbase));
  250                 break;
  251 
  252         case AMD64_SET_GSBASE:
  253                 error = copyin(uap->parms, &a64base, sizeof(a64base));
  254                 if (!error) {
  255                         if (a64base < VM_MAXUSER_ADDRESS) {
  256                                 pcb->pcb_gsbase = a64base;
  257                                 set_pcb_flags(pcb, PCB_FULL_IRET);
  258                                 td->td_frame->tf_gs = _ugssel;
  259                         } else
  260                                 error = EINVAL;
  261                 }
  262                 break;
  263 
  264         default:
  265                 error = EINVAL;
  266                 break;
  267         }
  268         return (error);
  269 }
  270 
  271 int
  272 amd64_set_ioperm(td, uap)
  273         struct thread *td;
  274         struct i386_ioperm_args *uap;
  275 {
  276         int i, error;
  277         char *iomap;
  278         struct amd64tss *tssp;
  279         struct system_segment_descriptor *tss_sd;
  280         u_long *addr;
  281         struct pcb *pcb;
  282 
  283         if ((error = priv_check(td, PRIV_IO)) != 0)
  284                 return (error);
  285         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  286                 return (error);
  287         if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
  288                 return (EINVAL);
  289 
  290         /*
  291          * XXX
  292          * While this is restricted to root, we should probably figure out
  293          * whether any other driver is using this i/o address, as so not to
  294          * cause confusion.  This probably requires a global 'usage registry'.
  295          */
  296         pcb = td->td_pcb;
  297         if (pcb->pcb_tssp == NULL) {
  298                 tssp = (struct amd64tss *)kmem_alloc(kernel_map,
  299                     ctob(IOPAGES+1));
  300                 if (tssp == NULL)
  301                         return (ENOMEM);
  302                 iomap = (char *)&tssp[1];
  303                 addr = (u_long *)iomap;
  304                 for (i = 0; i < (ctob(IOPAGES) + 1) / sizeof(u_long); i++)
  305                         *addr++ = ~0;
  306                 critical_enter();
  307                 /* Takes care of tss_rsp0. */
  308                 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
  309                     sizeof(struct amd64tss));
  310                 tssp->tss_iobase = sizeof(*tssp);
  311                 pcb->pcb_tssp = tssp;
  312                 tss_sd = PCPU_GET(tss);
  313                 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
  314                 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
  315                 tss_sd->sd_type = SDT_SYSTSS;
  316                 ltr(GSEL(GPROC0_SEL, SEL_KPL));
  317                 PCPU_SET(tssp, tssp);
  318                 critical_exit();
  319         } else
  320                 iomap = (char *)&pcb->pcb_tssp[1];
  321         for (i = uap->start; i < uap->start + uap->length; i++) {
  322                 if (uap->enable)
  323                         iomap[i >> 3] &= ~(1 << (i & 7));
  324                 else
  325                         iomap[i >> 3] |= (1 << (i & 7));
  326         }
  327         return (error);
  328 }
  329 
  330 int
  331 amd64_get_ioperm(td, uap)
  332         struct thread *td;
  333         struct i386_ioperm_args *uap;
  334 {
  335         int i, state;
  336         char *iomap;
  337 
  338         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
  339                 return (EINVAL);
  340         if (td->td_pcb->pcb_tssp == NULL) {
  341                 uap->length = 0;
  342                 goto done;
  343         }
  344 
  345         iomap = (char *)&td->td_pcb->pcb_tssp[1];
  346 
  347         i = uap->start;
  348         state = (iomap[i >> 3] >> (i & 7)) & 1;
  349         uap->enable = !state;
  350         uap->length = 1;
  351 
  352         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
  353                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
  354                         break;
  355                 uap->length++;
  356         }
  357 
  358 done:
  359         return (0);
  360 }
  361 
  362 /*
  363  * Update the GDT entry pointing to the LDT to point to the LDT of the
  364  * current process.
  365  */
  366 void
  367 set_user_ldt(struct mdproc *mdp)
  368 {
  369 
  370         critical_enter();
  371         *PCPU_GET(ldt) = mdp->md_ldt_sd;
  372         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
  373         critical_exit();
  374 }
  375 
  376 #ifdef notyet
  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 #endif
  391 
  392 struct proc_ldt *
  393 user_ldt_alloc(struct proc *p, int force)
  394 {
  395         struct proc_ldt *pldt, *new_ldt;
  396         struct mdproc *mdp;
  397         struct soft_segment_descriptor sldt;
  398 
  399         mtx_assert(&dt_lock, MA_OWNED);
  400         mdp = &p->p_md;
  401         if (!force && mdp->md_ldt != NULL)
  402                 return (mdp->md_ldt);
  403         mtx_unlock(&dt_lock);
  404         new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
  405         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
  406              max_ldt_segment * sizeof(struct user_segment_descriptor));
  407         if (new_ldt->ldt_base == NULL) {
  408                 FREE(new_ldt, M_SUBPROC);
  409                 mtx_lock(&dt_lock);
  410                 return (NULL);
  411         }
  412         new_ldt->ldt_refcnt = 1;
  413         sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
  414         sldt.ssd_limit = max_ldt_segment *
  415             sizeof(struct user_segment_descriptor) - 1;
  416         sldt.ssd_type = SDT_SYSLDT;
  417         sldt.ssd_dpl = SEL_KPL;
  418         sldt.ssd_p = 1;
  419         sldt.ssd_long = 0;
  420         sldt.ssd_def32 = 0;
  421         sldt.ssd_gran = 0;
  422         mtx_lock(&dt_lock);
  423         pldt = mdp->md_ldt;
  424         if (pldt != NULL && !force) {
  425                 kmem_free(kernel_map, (vm_offset_t)new_ldt->ldt_base,
  426                     max_ldt_segment * sizeof(struct user_segment_descriptor));
  427                 free(new_ldt, M_SUBPROC);
  428                 return (pldt);
  429         }
  430 
  431         if (pldt != NULL) {
  432                 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
  433                     sizeof(struct user_segment_descriptor));
  434                 user_ldt_derefl(pldt);
  435         }
  436         ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
  437         atomic_store_rel_ptr((volatile uintptr_t *)&mdp->md_ldt,
  438             (uintptr_t)new_ldt);
  439         if (p == curproc)
  440                 set_user_ldt(mdp);
  441 
  442         return (mdp->md_ldt);
  443 }
  444 
  445 void
  446 user_ldt_free(struct thread *td)
  447 {
  448         struct proc *p = td->td_proc;
  449         struct mdproc *mdp = &p->p_md;
  450         struct proc_ldt *pldt;
  451 
  452         mtx_assert(&dt_lock, MA_OWNED);
  453         if ((pldt = mdp->md_ldt) == NULL) {
  454                 mtx_unlock(&dt_lock);
  455                 return;
  456         }
  457 
  458         mdp->md_ldt = NULL;
  459         bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
  460         if (td == curthread)
  461                 lldt(GSEL(GNULL_SEL, SEL_KPL));
  462         user_ldt_deref(pldt);
  463 }
  464 
  465 static void
  466 user_ldt_derefl(struct proc_ldt *pldt)
  467 {
  468 
  469         if (--pldt->ldt_refcnt == 0) {
  470                 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
  471                     max_ldt_segment * sizeof(struct user_segment_descriptor));
  472                 free(pldt, M_SUBPROC);
  473         }
  474 }
  475 
  476 void
  477 user_ldt_deref(struct proc_ldt *pldt)
  478 {
  479 
  480         mtx_assert(&dt_lock, MA_OWNED);
  481         user_ldt_derefl(pldt);
  482         mtx_unlock(&dt_lock);
  483 }
  484 
  485 /*
  486  * Note for the authors of compat layers (linux, etc): copyout() in
  487  * the function below is not a problem since it presents data in
  488  * arch-specific format (i.e. i386-specific in this case), not in
  489  * the OS-specific one.
  490  */
  491 int
  492 amd64_get_ldt(td, uap)
  493         struct thread *td;
  494         struct i386_ldt_args *uap;
  495 {
  496         int error = 0;
  497         struct proc_ldt *pldt;
  498         int num;
  499         struct user_segment_descriptor *lp;
  500 
  501 #ifdef  DEBUG
  502         printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
  503             uap->start, uap->num, (void *)uap->descs);
  504 #endif
  505 
  506         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
  507                 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
  508                     [uap->start];
  509                 num = min(uap->num, max_ldt_segment);
  510         } else
  511                 return (EINVAL);
  512 
  513         if ((uap->start > (unsigned int)max_ldt_segment) ||
  514             ((unsigned int)num > (unsigned int)max_ldt_segment) ||
  515             ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
  516                 return(EINVAL);
  517 
  518         error = copyout(lp, uap->descs, num *
  519             sizeof(struct user_segment_descriptor));
  520         if (!error)
  521                 td->td_retval[0] = num;
  522 
  523         return(error);
  524 }
  525 
  526 int
  527 amd64_set_ldt(td, uap, descs)
  528         struct thread *td;
  529         struct i386_ldt_args *uap;
  530         struct user_segment_descriptor *descs;
  531 {
  532         int error = 0, i;
  533         int largest_ld;
  534         struct mdproc *mdp = &td->td_proc->p_md;
  535         struct proc_ldt *pldt;
  536         struct user_segment_descriptor *dp;
  537         struct proc *p;
  538 
  539 #ifdef  DEBUG
  540         printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
  541             uap->start, uap->num, (void *)uap->descs);
  542 #endif
  543 
  544         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  545         p = td->td_proc;
  546         if (descs == NULL) {
  547                 /* Free descriptors */
  548                 if (uap->start == 0 && uap->num == 0)
  549                         uap->num = max_ldt_segment;
  550                 if (uap->num <= 0)
  551                         return (EINVAL);
  552                 if ((pldt = mdp->md_ldt) == NULL ||
  553                     uap->start >= max_ldt_segment)
  554                         return (0);
  555                 largest_ld = uap->start + uap->num;
  556                 if (largest_ld > max_ldt_segment)
  557                         largest_ld = max_ldt_segment;
  558                 i = largest_ld - uap->start;
  559                 mtx_lock(&dt_lock);
  560                 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
  561                     [uap->start], sizeof(struct user_segment_descriptor) * i);
  562                 mtx_unlock(&dt_lock);
  563                 return (0);
  564         }
  565 
  566         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
  567                 /* verify range of descriptors to modify */
  568                 largest_ld = uap->start + uap->num;
  569                 if (uap->start >= max_ldt_segment ||
  570                     uap->num < 0 || largest_ld > max_ldt_segment)
  571                         return (EINVAL);
  572         }
  573 
  574         /* Check descriptors for access violations */
  575         for (i = 0; i < uap->num; i++) {
  576                 dp = &descs[i];
  577 
  578                 switch (dp->sd_type) {
  579                 case SDT_SYSNULL:       /* system null */
  580                         dp->sd_p = 0;
  581                         break;
  582                 case SDT_SYS286TSS:
  583                 case SDT_SYSLDT:
  584                 case SDT_SYS286BSY:
  585                 case SDT_SYS286CGT:
  586                 case SDT_SYSTASKGT:
  587                 case SDT_SYS286IGT:
  588                 case SDT_SYS286TGT:
  589                 case SDT_SYSNULL2:
  590                 case SDT_SYSTSS:
  591                 case SDT_SYSNULL3:
  592                 case SDT_SYSBSY:
  593                 case SDT_SYSCGT:
  594                 case SDT_SYSNULL4:
  595                 case SDT_SYSIGT:
  596                 case SDT_SYSTGT:
  597                         /* I can't think of any reason to allow a user proc
  598                          * to create a segment of these types.  They are
  599                          * for OS use only.
  600                          */
  601                         return (EACCES);
  602                         /*NOTREACHED*/
  603 
  604                 /* memory segment types */
  605                 case SDT_MEMEC:   /* memory execute only conforming */
  606                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  607                 case SDT_MEMERC:  /* memory execute read conforming */
  608                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  609                          /* Must be "present" if executable and conforming. */
  610                         if (dp->sd_p == 0)
  611                                 return (EACCES);
  612                         break;
  613                 case SDT_MEMRO:   /* memory read only */
  614                 case SDT_MEMROA:  /* memory read only accessed */
  615                 case SDT_MEMRW:   /* memory read write */
  616                 case SDT_MEMRWA:  /* memory read write accessed */
  617                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  618                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  619                 case SDT_MEMRWD:  /* memory read write expand dwn limit */
  620                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  621                 case SDT_MEME:    /* memory execute only */
  622                 case SDT_MEMEA:   /* memory execute only accessed */
  623                 case SDT_MEMER:   /* memory execute read */
  624                 case SDT_MEMERA:  /* memory execute read accessed */
  625                         break;
  626                 default:
  627                         return(EINVAL);
  628                         /*NOTREACHED*/
  629                 }
  630 
  631                 /* Only user (ring-3) descriptors may be present. */
  632                 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
  633                         return (EACCES);
  634         }
  635 
  636         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
  637                 /* Allocate a free slot */
  638                 mtx_lock(&dt_lock);
  639                 pldt = user_ldt_alloc(p, 0);
  640                 if (pldt == NULL) {
  641                         mtx_unlock(&dt_lock);
  642                         return (ENOMEM);
  643                 }
  644 
  645                 /*
  646                  * start scanning a bit up to leave room for NVidia and
  647                  * Wine, which still user the "Blat" method of allocation.
  648                  */
  649                 i = 16;
  650                 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
  651                 for (; i < max_ldt_segment; ++i, ++dp) {
  652                         if (dp->sd_type == SDT_SYSNULL)
  653                                 break;
  654                 }
  655                 if (i >= max_ldt_segment) {
  656                         mtx_unlock(&dt_lock);
  657                         return (ENOSPC);
  658                 }
  659                 uap->start = i;
  660                 error = amd64_set_ldt_data(td, i, 1, descs);
  661                 mtx_unlock(&dt_lock);
  662         } else {
  663                 largest_ld = uap->start + uap->num;
  664                 if (largest_ld > max_ldt_segment)
  665                         return (EINVAL);
  666                 mtx_lock(&dt_lock);
  667                 if (user_ldt_alloc(p, 0) != NULL) {
  668                         error = amd64_set_ldt_data(td, uap->start, uap->num,
  669                             descs);
  670                 }
  671                 mtx_unlock(&dt_lock);
  672         }
  673         if (error == 0)
  674                 td->td_retval[0] = uap->start;
  675         return (error);
  676 }
  677 
  678 int
  679 amd64_set_ldt_data(struct thread *td, int start, int num,
  680     struct user_segment_descriptor *descs)
  681 {
  682         struct mdproc *mdp = &td->td_proc->p_md;
  683         struct proc_ldt *pldt = mdp->md_ldt;
  684 
  685         mtx_assert(&dt_lock, MA_OWNED);
  686 
  687         /* Fill in range */
  688         bcopy(descs,
  689             &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
  690             num * sizeof(struct user_segment_descriptor));
  691         return (0);
  692 }

Cache object: d709b26d94a15cffae351bdb332efaad


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