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

Cache object: 502df3ec6f47b4dd0e68c7cf13370348


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