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

Cache object: 447ff48a0c0267a00db671c12ad3e677


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