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.4/sys/amd64/amd64/sys_machdep.c 237009 2012-06-13 15:25:52Z jhb $");
   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         struct i386_get_xfpustate i386xfpu;
  180         struct amd64_get_xfpustate a64xfpu;
  181 
  182         if (uap->op == I386_GET_LDT || uap->op == I386_SET_LDT)
  183                 return (sysarch_ldt(td, uap, UIO_USERSPACE));
  184         /*
  185          * XXXKIB check that the BSM generation code knows to encode
  186          * the op argument.
  187          */
  188         AUDIT_ARG_CMD(uap->op);
  189         switch (uap->op) {
  190         case I386_GET_IOPERM:
  191         case I386_SET_IOPERM:
  192                 if ((error = copyin(uap->parms, &iargs,
  193                     sizeof(struct i386_ioperm_args))) != 0)
  194                         return (error);
  195                 break;
  196         case I386_GET_XFPUSTATE:
  197                 if ((error = copyin(uap->parms, &i386xfpu,
  198                     sizeof(struct i386_get_xfpustate))) != 0)
  199                         return (error);
  200                 a64xfpu.addr = (void *)(uintptr_t)i386xfpu.addr;
  201                 a64xfpu.len = i386xfpu.len;
  202                 break;
  203         case AMD64_GET_XFPUSTATE:
  204                 if ((error = copyin(uap->parms, &a64xfpu,
  205                     sizeof(struct amd64_get_xfpustate))) != 0)
  206                         return (error);
  207                 break;
  208         default:
  209                 break;
  210         }
  211 
  212         switch (uap->op) {
  213         case I386_GET_IOPERM:
  214                 error = amd64_get_ioperm(td, &iargs);
  215                 if (error == 0)
  216                         error = copyout(&iargs, uap->parms,
  217                             sizeof(struct i386_ioperm_args));
  218                 break;
  219         case I386_SET_IOPERM:
  220                 error = amd64_set_ioperm(td, &iargs);
  221                 break;
  222         case I386_GET_FSBASE:
  223                 i386base = pcb->pcb_fsbase;
  224                 error = copyout(&i386base, uap->parms, sizeof(i386base));
  225                 break;
  226         case I386_SET_FSBASE:
  227                 error = copyin(uap->parms, &i386base, sizeof(i386base));
  228                 if (!error) {
  229                         pcb->pcb_fsbase = i386base;
  230                         td->td_frame->tf_fs = _ufssel;
  231                         update_gdt_fsbase(td, i386base);
  232                 }
  233                 break;
  234         case I386_GET_GSBASE:
  235                 i386base = pcb->pcb_gsbase;
  236                 error = copyout(&i386base, uap->parms, sizeof(i386base));
  237                 break;
  238         case I386_SET_GSBASE:
  239                 error = copyin(uap->parms, &i386base, sizeof(i386base));
  240                 if (!error) {
  241                         pcb->pcb_gsbase = i386base;
  242                         td->td_frame->tf_gs = _ugssel;
  243                         update_gdt_gsbase(td, i386base);
  244                 }
  245                 break;
  246         case AMD64_GET_FSBASE:
  247                 error = copyout(&pcb->pcb_fsbase, uap->parms, sizeof(pcb->pcb_fsbase));
  248                 break;
  249                 
  250         case AMD64_SET_FSBASE:
  251                 error = copyin(uap->parms, &a64base, sizeof(a64base));
  252                 if (!error) {
  253                         if (a64base < VM_MAXUSER_ADDRESS) {
  254                                 pcb->pcb_fsbase = a64base;
  255                                 set_pcb_flags(pcb, PCB_FULL_IRET);
  256                                 td->td_frame->tf_fs = _ufssel;
  257                         } else
  258                                 error = EINVAL;
  259                 }
  260                 break;
  261 
  262         case AMD64_GET_GSBASE:
  263                 error = copyout(&pcb->pcb_gsbase, uap->parms, sizeof(pcb->pcb_gsbase));
  264                 break;
  265 
  266         case AMD64_SET_GSBASE:
  267                 error = copyin(uap->parms, &a64base, sizeof(a64base));
  268                 if (!error) {
  269                         if (a64base < VM_MAXUSER_ADDRESS) {
  270                                 pcb->pcb_gsbase = a64base;
  271                                 set_pcb_flags(pcb, PCB_FULL_IRET);
  272                                 td->td_frame->tf_gs = _ugssel;
  273                         } else
  274                                 error = EINVAL;
  275                 }
  276                 break;
  277 
  278         case I386_GET_XFPUSTATE:
  279         case AMD64_GET_XFPUSTATE:
  280                 if (a64xfpu.len > cpu_max_ext_state_size -
  281                     sizeof(struct savefpu))
  282                         return (EINVAL);
  283                 fpugetregs(td);
  284                 error = copyout((char *)(get_pcb_user_save_td(td) + 1),
  285                     a64xfpu.addr, a64xfpu.len);
  286                 return (error);
  287 
  288         default:
  289                 error = EINVAL;
  290                 break;
  291         }
  292         return (error);
  293 }
  294 
  295 int
  296 amd64_set_ioperm(td, uap)
  297         struct thread *td;
  298         struct i386_ioperm_args *uap;
  299 {
  300         int i, error;
  301         char *iomap;
  302         struct amd64tss *tssp;
  303         struct system_segment_descriptor *tss_sd;
  304         u_long *addr;
  305         struct pcb *pcb;
  306 
  307         if ((error = priv_check(td, PRIV_IO)) != 0)
  308                 return (error);
  309         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  310                 return (error);
  311         if (uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
  312                 return (EINVAL);
  313 
  314         /*
  315          * XXX
  316          * While this is restricted to root, we should probably figure out
  317          * whether any other driver is using this i/o address, as so not to
  318          * cause confusion.  This probably requires a global 'usage registry'.
  319          */
  320         pcb = td->td_pcb;
  321         if (pcb->pcb_tssp == NULL) {
  322                 tssp = (struct amd64tss *)kmem_alloc(kernel_map,
  323                     ctob(IOPAGES+1));
  324                 if (tssp == NULL)
  325                         return (ENOMEM);
  326                 iomap = (char *)&tssp[1];
  327                 addr = (u_long *)iomap;
  328                 for (i = 0; i < (ctob(IOPAGES) + 1) / sizeof(u_long); i++)
  329                         *addr++ = ~0;
  330                 critical_enter();
  331                 /* Takes care of tss_rsp0. */
  332                 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
  333                     sizeof(struct amd64tss));
  334                 tssp->tss_iobase = sizeof(*tssp);
  335                 pcb->pcb_tssp = tssp;
  336                 tss_sd = PCPU_GET(tss);
  337                 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
  338                 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
  339                 tss_sd->sd_type = SDT_SYSTSS;
  340                 ltr(GSEL(GPROC0_SEL, SEL_KPL));
  341                 PCPU_SET(tssp, tssp);
  342                 critical_exit();
  343         } else
  344                 iomap = (char *)&pcb->pcb_tssp[1];
  345         for (i = uap->start; i < uap->start + uap->length; i++) {
  346                 if (uap->enable)
  347                         iomap[i >> 3] &= ~(1 << (i & 7));
  348                 else
  349                         iomap[i >> 3] |= (1 << (i & 7));
  350         }
  351         return (error);
  352 }
  353 
  354 int
  355 amd64_get_ioperm(td, uap)
  356         struct thread *td;
  357         struct i386_ioperm_args *uap;
  358 {
  359         int i, state;
  360         char *iomap;
  361 
  362         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
  363                 return (EINVAL);
  364         if (td->td_pcb->pcb_tssp == NULL) {
  365                 uap->length = 0;
  366                 goto done;
  367         }
  368 
  369         iomap = (char *)&td->td_pcb->pcb_tssp[1];
  370 
  371         i = uap->start;
  372         state = (iomap[i >> 3] >> (i & 7)) & 1;
  373         uap->enable = !state;
  374         uap->length = 1;
  375 
  376         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
  377                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
  378                         break;
  379                 uap->length++;
  380         }
  381 
  382 done:
  383         return (0);
  384 }
  385 
  386 /*
  387  * Update the GDT entry pointing to the LDT to point to the LDT of the
  388  * current process.
  389  */
  390 void
  391 set_user_ldt(struct mdproc *mdp)
  392 {
  393 
  394         critical_enter();
  395         *PCPU_GET(ldt) = mdp->md_ldt_sd;
  396         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
  397         critical_exit();
  398 }
  399 
  400 #ifdef notyet
  401 #ifdef SMP
  402 static void
  403 set_user_ldt_rv(struct vmspace *vmsp)
  404 {
  405         struct thread *td;
  406 
  407         td = curthread;
  408         if (vmsp != td->td_proc->p_vmspace)
  409                 return;
  410 
  411         set_user_ldt(&td->td_proc->p_md);
  412 }
  413 #endif
  414 #endif
  415 
  416 struct proc_ldt *
  417 user_ldt_alloc(struct proc *p, int force)
  418 {
  419         struct proc_ldt *pldt, *new_ldt;
  420         struct mdproc *mdp;
  421         struct soft_segment_descriptor sldt;
  422 
  423         mtx_assert(&dt_lock, MA_OWNED);
  424         mdp = &p->p_md;
  425         if (!force && mdp->md_ldt != NULL)
  426                 return (mdp->md_ldt);
  427         mtx_unlock(&dt_lock);
  428         new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
  429         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
  430              max_ldt_segment * sizeof(struct user_segment_descriptor));
  431         if (new_ldt->ldt_base == NULL) {
  432                 FREE(new_ldt, M_SUBPROC);
  433                 mtx_lock(&dt_lock);
  434                 return (NULL);
  435         }
  436         new_ldt->ldt_refcnt = 1;
  437         sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
  438         sldt.ssd_limit = max_ldt_segment *
  439             sizeof(struct user_segment_descriptor) - 1;
  440         sldt.ssd_type = SDT_SYSLDT;
  441         sldt.ssd_dpl = SEL_KPL;
  442         sldt.ssd_p = 1;
  443         sldt.ssd_long = 0;
  444         sldt.ssd_def32 = 0;
  445         sldt.ssd_gran = 0;
  446         mtx_lock(&dt_lock);
  447         pldt = mdp->md_ldt;
  448         if (pldt != NULL && !force) {
  449                 kmem_free(kernel_map, (vm_offset_t)new_ldt->ldt_base,
  450                     max_ldt_segment * sizeof(struct user_segment_descriptor));
  451                 free(new_ldt, M_SUBPROC);
  452                 return (pldt);
  453         }
  454 
  455         if (pldt != NULL) {
  456                 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
  457                     sizeof(struct user_segment_descriptor));
  458                 user_ldt_derefl(pldt);
  459         }
  460         ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
  461         atomic_store_rel_ptr((volatile uintptr_t *)&mdp->md_ldt,
  462             (uintptr_t)new_ldt);
  463         if (p == curproc)
  464                 set_user_ldt(mdp);
  465 
  466         return (mdp->md_ldt);
  467 }
  468 
  469 void
  470 user_ldt_free(struct thread *td)
  471 {
  472         struct proc *p = td->td_proc;
  473         struct mdproc *mdp = &p->p_md;
  474         struct proc_ldt *pldt;
  475 
  476         mtx_assert(&dt_lock, MA_OWNED);
  477         if ((pldt = mdp->md_ldt) == NULL) {
  478                 mtx_unlock(&dt_lock);
  479                 return;
  480         }
  481 
  482         mdp->md_ldt = NULL;
  483         bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
  484         if (td == curthread)
  485                 lldt(GSEL(GNULL_SEL, SEL_KPL));
  486         user_ldt_deref(pldt);
  487 }
  488 
  489 static void
  490 user_ldt_derefl(struct proc_ldt *pldt)
  491 {
  492 
  493         if (--pldt->ldt_refcnt == 0) {
  494                 kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
  495                     max_ldt_segment * sizeof(struct user_segment_descriptor));
  496                 free(pldt, M_SUBPROC);
  497         }
  498 }
  499 
  500 void
  501 user_ldt_deref(struct proc_ldt *pldt)
  502 {
  503 
  504         mtx_assert(&dt_lock, MA_OWNED);
  505         user_ldt_derefl(pldt);
  506         mtx_unlock(&dt_lock);
  507 }
  508 
  509 /*
  510  * Note for the authors of compat layers (linux, etc): copyout() in
  511  * the function below is not a problem since it presents data in
  512  * arch-specific format (i.e. i386-specific in this case), not in
  513  * the OS-specific one.
  514  */
  515 int
  516 amd64_get_ldt(td, uap)
  517         struct thread *td;
  518         struct i386_ldt_args *uap;
  519 {
  520         int error = 0;
  521         struct proc_ldt *pldt;
  522         int num;
  523         struct user_segment_descriptor *lp;
  524 
  525 #ifdef  DEBUG
  526         printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
  527             uap->start, uap->num, (void *)uap->descs);
  528 #endif
  529 
  530         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
  531                 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
  532                     [uap->start];
  533                 num = min(uap->num, max_ldt_segment);
  534         } else
  535                 return (EINVAL);
  536 
  537         if ((uap->start > (unsigned int)max_ldt_segment) ||
  538             ((unsigned int)num > (unsigned int)max_ldt_segment) ||
  539             ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
  540                 return(EINVAL);
  541 
  542         error = copyout(lp, uap->descs, num *
  543             sizeof(struct user_segment_descriptor));
  544         if (!error)
  545                 td->td_retval[0] = num;
  546 
  547         return(error);
  548 }
  549 
  550 int
  551 amd64_set_ldt(td, uap, descs)
  552         struct thread *td;
  553         struct i386_ldt_args *uap;
  554         struct user_segment_descriptor *descs;
  555 {
  556         int error = 0, i;
  557         int largest_ld;
  558         struct mdproc *mdp = &td->td_proc->p_md;
  559         struct proc_ldt *pldt;
  560         struct user_segment_descriptor *dp;
  561         struct proc *p;
  562 
  563 #ifdef  DEBUG
  564         printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
  565             uap->start, uap->num, (void *)uap->descs);
  566 #endif
  567 
  568         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  569         p = td->td_proc;
  570         if (descs == NULL) {
  571                 /* Free descriptors */
  572                 if (uap->start == 0 && uap->num == 0)
  573                         uap->num = max_ldt_segment;
  574                 if (uap->num <= 0)
  575                         return (EINVAL);
  576                 if ((pldt = mdp->md_ldt) == NULL ||
  577                     uap->start >= max_ldt_segment)
  578                         return (0);
  579                 largest_ld = uap->start + uap->num;
  580                 if (largest_ld > max_ldt_segment)
  581                         largest_ld = max_ldt_segment;
  582                 i = largest_ld - uap->start;
  583                 mtx_lock(&dt_lock);
  584                 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
  585                     [uap->start], sizeof(struct user_segment_descriptor) * i);
  586                 mtx_unlock(&dt_lock);
  587                 return (0);
  588         }
  589 
  590         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
  591                 /* verify range of descriptors to modify */
  592                 largest_ld = uap->start + uap->num;
  593                 if (uap->start >= max_ldt_segment ||
  594                     uap->num < 0 || largest_ld > max_ldt_segment)
  595                         return (EINVAL);
  596         }
  597 
  598         /* Check descriptors for access violations */
  599         for (i = 0; i < uap->num; i++) {
  600                 dp = &descs[i];
  601 
  602                 switch (dp->sd_type) {
  603                 case SDT_SYSNULL:       /* system null */
  604                         dp->sd_p = 0;
  605                         break;
  606                 case SDT_SYS286TSS:
  607                 case SDT_SYSLDT:
  608                 case SDT_SYS286BSY:
  609                 case SDT_SYS286CGT:
  610                 case SDT_SYSTASKGT:
  611                 case SDT_SYS286IGT:
  612                 case SDT_SYS286TGT:
  613                 case SDT_SYSNULL2:
  614                 case SDT_SYSTSS:
  615                 case SDT_SYSNULL3:
  616                 case SDT_SYSBSY:
  617                 case SDT_SYSCGT:
  618                 case SDT_SYSNULL4:
  619                 case SDT_SYSIGT:
  620                 case SDT_SYSTGT:
  621                         /* I can't think of any reason to allow a user proc
  622                          * to create a segment of these types.  They are
  623                          * for OS use only.
  624                          */
  625                         return (EACCES);
  626                         /*NOTREACHED*/
  627 
  628                 /* memory segment types */
  629                 case SDT_MEMEC:   /* memory execute only conforming */
  630                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  631                 case SDT_MEMERC:  /* memory execute read conforming */
  632                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  633                          /* Must be "present" if executable and conforming. */
  634                         if (dp->sd_p == 0)
  635                                 return (EACCES);
  636                         break;
  637                 case SDT_MEMRO:   /* memory read only */
  638                 case SDT_MEMROA:  /* memory read only accessed */
  639                 case SDT_MEMRW:   /* memory read write */
  640                 case SDT_MEMRWA:  /* memory read write accessed */
  641                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  642                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  643                 case SDT_MEMRWD:  /* memory read write expand dwn limit */
  644                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  645                 case SDT_MEME:    /* memory execute only */
  646                 case SDT_MEMEA:   /* memory execute only accessed */
  647                 case SDT_MEMER:   /* memory execute read */
  648                 case SDT_MEMERA:  /* memory execute read accessed */
  649                         break;
  650                 default:
  651                         return(EINVAL);
  652                         /*NOTREACHED*/
  653                 }
  654 
  655                 /* Only user (ring-3) descriptors may be present. */
  656                 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
  657                         return (EACCES);
  658         }
  659 
  660         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
  661                 /* Allocate a free slot */
  662                 mtx_lock(&dt_lock);
  663                 pldt = user_ldt_alloc(p, 0);
  664                 if (pldt == NULL) {
  665                         mtx_unlock(&dt_lock);
  666                         return (ENOMEM);
  667                 }
  668 
  669                 /*
  670                  * start scanning a bit up to leave room for NVidia and
  671                  * Wine, which still user the "Blat" method of allocation.
  672                  */
  673                 i = 16;
  674                 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
  675                 for (; i < max_ldt_segment; ++i, ++dp) {
  676                         if (dp->sd_type == SDT_SYSNULL)
  677                                 break;
  678                 }
  679                 if (i >= max_ldt_segment) {
  680                         mtx_unlock(&dt_lock);
  681                         return (ENOSPC);
  682                 }
  683                 uap->start = i;
  684                 error = amd64_set_ldt_data(td, i, 1, descs);
  685                 mtx_unlock(&dt_lock);
  686         } else {
  687                 largest_ld = uap->start + uap->num;
  688                 if (largest_ld > max_ldt_segment)
  689                         return (EINVAL);
  690                 mtx_lock(&dt_lock);
  691                 if (user_ldt_alloc(p, 0) != NULL) {
  692                         error = amd64_set_ldt_data(td, uap->start, uap->num,
  693                             descs);
  694                 }
  695                 mtx_unlock(&dt_lock);
  696         }
  697         if (error == 0)
  698                 td->td_retval[0] = uap->start;
  699         return (error);
  700 }
  701 
  702 int
  703 amd64_set_ldt_data(struct thread *td, int start, int num,
  704     struct user_segment_descriptor *descs)
  705 {
  706         struct mdproc *mdp = &td->td_proc->p_md;
  707         struct proc_ldt *pldt = mdp->md_ldt;
  708 
  709         mtx_assert(&dt_lock, MA_OWNED);
  710 
  711         /* Fill in range */
  712         bcopy(descs,
  713             &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
  714             num * sizeof(struct user_segment_descriptor));
  715         return (0);
  716 }

Cache object: 7b2508220bd517d4619bc927e4ae86b8


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