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

Cache object: 5d08a6a64c25735b7f52694a75167075


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