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  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  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: stable/10/sys/amd64/amd64/sys_machdep.c 307940 2016-10-25 17:16:08Z 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         char *iomap;
  338         struct amd64tss *tssp;
  339         struct system_segment_descriptor *tss_sd;
  340         u_long *addr;
  341         struct pcb *pcb;
  342         u_int i;
  343         int error;
  344 
  345         if ((error = priv_check(td, PRIV_IO)) != 0)
  346                 return (error);
  347         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  348                 return (error);
  349         if (uap->start > uap->start + uap->length ||
  350             uap->start + uap->length > IOPAGES * PAGE_SIZE * NBBY)
  351                 return (EINVAL);
  352 
  353         /*
  354          * XXX
  355          * While this is restricted to root, we should probably figure out
  356          * whether any other driver is using this i/o address, as so not to
  357          * cause confusion.  This probably requires a global 'usage registry'.
  358          */
  359         pcb = td->td_pcb;
  360         if (pcb->pcb_tssp == NULL) {
  361                 tssp = (struct amd64tss *)kmem_malloc(kernel_arena,
  362                     ctob(IOPAGES+1), M_WAITOK);
  363                 if (tssp == NULL)
  364                         return (ENOMEM);
  365                 iomap = (char *)&tssp[1];
  366                 addr = (u_long *)iomap;
  367                 for (i = 0; i < (ctob(IOPAGES) + 1) / sizeof(u_long); i++)
  368                         *addr++ = ~0;
  369                 critical_enter();
  370                 /* Takes care of tss_rsp0. */
  371                 memcpy(tssp, &common_tss[PCPU_GET(cpuid)],
  372                     sizeof(struct amd64tss));
  373                 tssp->tss_iobase = sizeof(*tssp);
  374                 pcb->pcb_tssp = tssp;
  375                 tss_sd = PCPU_GET(tss);
  376                 tss_sd->sd_lobase = (u_long)tssp & 0xffffff;
  377                 tss_sd->sd_hibase = ((u_long)tssp >> 24) & 0xfffffffffful;
  378                 tss_sd->sd_type = SDT_SYSTSS;
  379                 ltr(GSEL(GPROC0_SEL, SEL_KPL));
  380                 PCPU_SET(tssp, tssp);
  381                 critical_exit();
  382         } else
  383                 iomap = (char *)&pcb->pcb_tssp[1];
  384         for (i = uap->start; i < uap->start + uap->length; i++) {
  385                 if (uap->enable)
  386                         iomap[i >> 3] &= ~(1 << (i & 7));
  387                 else
  388                         iomap[i >> 3] |= (1 << (i & 7));
  389         }
  390         return (error);
  391 }
  392 
  393 int
  394 amd64_get_ioperm(td, uap)
  395         struct thread *td;
  396         struct i386_ioperm_args *uap;
  397 {
  398         int i, state;
  399         char *iomap;
  400 
  401         if (uap->start >= IOPAGES * PAGE_SIZE * NBBY)
  402                 return (EINVAL);
  403         if (td->td_pcb->pcb_tssp == NULL) {
  404                 uap->length = 0;
  405                 goto done;
  406         }
  407 
  408         iomap = (char *)&td->td_pcb->pcb_tssp[1];
  409 
  410         i = uap->start;
  411         state = (iomap[i >> 3] >> (i & 7)) & 1;
  412         uap->enable = !state;
  413         uap->length = 1;
  414 
  415         for (i = uap->start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
  416                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
  417                         break;
  418                 uap->length++;
  419         }
  420 
  421 done:
  422         return (0);
  423 }
  424 
  425 /*
  426  * Update the GDT entry pointing to the LDT to point to the LDT of the
  427  * current process.
  428  */
  429 void
  430 set_user_ldt(struct mdproc *mdp)
  431 {
  432 
  433         critical_enter();
  434         *PCPU_GET(ldt) = mdp->md_ldt_sd;
  435         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
  436         critical_exit();
  437 }
  438 
  439 #ifdef notyet
  440 #ifdef SMP
  441 static void
  442 set_user_ldt_rv(struct vmspace *vmsp)
  443 {
  444         struct thread *td;
  445 
  446         td = curthread;
  447         if (vmsp != td->td_proc->p_vmspace)
  448                 return;
  449 
  450         set_user_ldt(&td->td_proc->p_md);
  451 }
  452 #endif
  453 #endif
  454 
  455 struct proc_ldt *
  456 user_ldt_alloc(struct proc *p, int force)
  457 {
  458         struct proc_ldt *pldt, *new_ldt;
  459         struct mdproc *mdp;
  460         struct soft_segment_descriptor sldt;
  461 
  462         mtx_assert(&dt_lock, MA_OWNED);
  463         mdp = &p->p_md;
  464         if (!force && mdp->md_ldt != NULL)
  465                 return (mdp->md_ldt);
  466         mtx_unlock(&dt_lock);
  467         new_ldt = malloc(sizeof(struct proc_ldt), M_SUBPROC, M_WAITOK);
  468         new_ldt->ldt_base = (caddr_t)kmem_malloc(kernel_arena,
  469              max_ldt_segment * sizeof(struct user_segment_descriptor),
  470              M_WAITOK | M_ZERO);
  471         if (new_ldt->ldt_base == NULL) {
  472                 FREE(new_ldt, M_SUBPROC);
  473                 mtx_lock(&dt_lock);
  474                 return (NULL);
  475         }
  476         new_ldt->ldt_refcnt = 1;
  477         sldt.ssd_base = (uint64_t)new_ldt->ldt_base;
  478         sldt.ssd_limit = max_ldt_segment *
  479             sizeof(struct user_segment_descriptor) - 1;
  480         sldt.ssd_type = SDT_SYSLDT;
  481         sldt.ssd_dpl = SEL_KPL;
  482         sldt.ssd_p = 1;
  483         sldt.ssd_long = 0;
  484         sldt.ssd_def32 = 0;
  485         sldt.ssd_gran = 0;
  486         mtx_lock(&dt_lock);
  487         pldt = mdp->md_ldt;
  488         if (pldt != NULL && !force) {
  489                 kmem_free(kernel_arena, (vm_offset_t)new_ldt->ldt_base,
  490                     max_ldt_segment * sizeof(struct user_segment_descriptor));
  491                 free(new_ldt, M_SUBPROC);
  492                 return (pldt);
  493         }
  494 
  495         if (pldt != NULL) {
  496                 bcopy(pldt->ldt_base, new_ldt->ldt_base, max_ldt_segment *
  497                     sizeof(struct user_segment_descriptor));
  498                 user_ldt_derefl(pldt);
  499         }
  500         ssdtosyssd(&sldt, &p->p_md.md_ldt_sd);
  501         atomic_store_rel_ptr((volatile uintptr_t *)&mdp->md_ldt,
  502             (uintptr_t)new_ldt);
  503         if (p == curproc)
  504                 set_user_ldt(mdp);
  505 
  506         return (mdp->md_ldt);
  507 }
  508 
  509 void
  510 user_ldt_free(struct thread *td)
  511 {
  512         struct proc *p = td->td_proc;
  513         struct mdproc *mdp = &p->p_md;
  514         struct proc_ldt *pldt;
  515 
  516         mtx_assert(&dt_lock, MA_OWNED);
  517         if ((pldt = mdp->md_ldt) == NULL) {
  518                 mtx_unlock(&dt_lock);
  519                 return;
  520         }
  521 
  522         mdp->md_ldt = NULL;
  523         bzero(&mdp->md_ldt_sd, sizeof(mdp->md_ldt_sd));
  524         if (td == curthread)
  525                 lldt(GSEL(GNULL_SEL, SEL_KPL));
  526         user_ldt_deref(pldt);
  527 }
  528 
  529 static void
  530 user_ldt_derefl(struct proc_ldt *pldt)
  531 {
  532 
  533         if (--pldt->ldt_refcnt == 0) {
  534                 kmem_free(kernel_arena, (vm_offset_t)pldt->ldt_base,
  535                     max_ldt_segment * sizeof(struct user_segment_descriptor));
  536                 free(pldt, M_SUBPROC);
  537         }
  538 }
  539 
  540 void
  541 user_ldt_deref(struct proc_ldt *pldt)
  542 {
  543 
  544         mtx_assert(&dt_lock, MA_OWNED);
  545         user_ldt_derefl(pldt);
  546         mtx_unlock(&dt_lock);
  547 }
  548 
  549 /*
  550  * Note for the authors of compat layers (linux, etc): copyout() in
  551  * the function below is not a problem since it presents data in
  552  * arch-specific format (i.e. i386-specific in this case), not in
  553  * the OS-specific one.
  554  */
  555 int
  556 amd64_get_ldt(td, uap)
  557         struct thread *td;
  558         struct i386_ldt_args *uap;
  559 {
  560         int error = 0;
  561         struct proc_ldt *pldt;
  562         int num;
  563         struct user_segment_descriptor *lp;
  564 
  565 #ifdef  DEBUG
  566         printf("amd64_get_ldt: start=%d num=%d descs=%p\n",
  567             uap->start, uap->num, (void *)uap->descs);
  568 #endif
  569 
  570         if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
  571                 lp = &((struct user_segment_descriptor *)(pldt->ldt_base))
  572                     [uap->start];
  573                 num = min(uap->num, max_ldt_segment);
  574         } else
  575                 return (EINVAL);
  576 
  577         if ((uap->start > (unsigned int)max_ldt_segment) ||
  578             ((unsigned int)num > (unsigned int)max_ldt_segment) ||
  579             ((unsigned int)(uap->start + num) > (unsigned int)max_ldt_segment))
  580                 return(EINVAL);
  581 
  582         error = copyout(lp, uap->descs, num *
  583             sizeof(struct user_segment_descriptor));
  584         if (!error)
  585                 td->td_retval[0] = num;
  586 
  587         return(error);
  588 }
  589 
  590 int
  591 amd64_set_ldt(td, uap, descs)
  592         struct thread *td;
  593         struct i386_ldt_args *uap;
  594         struct user_segment_descriptor *descs;
  595 {
  596         int error = 0;
  597         unsigned int largest_ld, i;
  598         struct mdproc *mdp = &td->td_proc->p_md;
  599         struct proc_ldt *pldt;
  600         struct user_segment_descriptor *dp;
  601         struct proc *p;
  602 
  603 #ifdef  DEBUG
  604         printf("amd64_set_ldt: start=%d num=%d descs=%p\n",
  605             uap->start, uap->num, (void *)uap->descs);
  606 #endif
  607 
  608         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
  609         p = td->td_proc;
  610         if (descs == NULL) {
  611                 /* Free descriptors */
  612                 if (uap->start == 0 && uap->num == 0)
  613                         uap->num = max_ldt_segment;
  614                 if (uap->num == 0)
  615                         return (EINVAL);
  616                 if ((pldt = mdp->md_ldt) == NULL ||
  617                     uap->start >= max_ldt_segment)
  618                         return (0);
  619                 largest_ld = uap->start + uap->num;
  620                 if (largest_ld > max_ldt_segment)
  621                         largest_ld = max_ldt_segment;
  622                 if (largest_ld < uap->start)
  623                         return (EINVAL);
  624                 i = largest_ld - uap->start;
  625                 mtx_lock(&dt_lock);
  626                 bzero(&((struct user_segment_descriptor *)(pldt->ldt_base))
  627                     [uap->start], sizeof(struct user_segment_descriptor) * i);
  628                 mtx_unlock(&dt_lock);
  629                 return (0);
  630         }
  631 
  632         if (!(uap->start == LDT_AUTO_ALLOC && uap->num == 1)) {
  633                 /* verify range of descriptors to modify */
  634                 largest_ld = uap->start + uap->num;
  635                 if (uap->start >= max_ldt_segment ||
  636                     largest_ld > max_ldt_segment ||
  637                     largest_ld < uap->start)
  638                         return (EINVAL);
  639         }
  640 
  641         /* Check descriptors for access violations */
  642         for (i = 0; i < uap->num; i++) {
  643                 dp = &descs[i];
  644 
  645                 switch (dp->sd_type) {
  646                 case SDT_SYSNULL:       /* system null */
  647                         dp->sd_p = 0;
  648                         break;
  649                 case SDT_SYS286TSS:
  650                 case SDT_SYSLDT:
  651                 case SDT_SYS286BSY:
  652                 case SDT_SYS286CGT:
  653                 case SDT_SYSTASKGT:
  654                 case SDT_SYS286IGT:
  655                 case SDT_SYS286TGT:
  656                 case SDT_SYSNULL2:
  657                 case SDT_SYSTSS:
  658                 case SDT_SYSNULL3:
  659                 case SDT_SYSBSY:
  660                 case SDT_SYSCGT:
  661                 case SDT_SYSNULL4:
  662                 case SDT_SYSIGT:
  663                 case SDT_SYSTGT:
  664                         /* I can't think of any reason to allow a user proc
  665                          * to create a segment of these types.  They are
  666                          * for OS use only.
  667                          */
  668                         return (EACCES);
  669                         /*NOTREACHED*/
  670 
  671                 /* memory segment types */
  672                 case SDT_MEMEC:   /* memory execute only conforming */
  673                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  674                 case SDT_MEMERC:  /* memory execute read conforming */
  675                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  676                          /* Must be "present" if executable and conforming. */
  677                         if (dp->sd_p == 0)
  678                                 return (EACCES);
  679                         break;
  680                 case SDT_MEMRO:   /* memory read only */
  681                 case SDT_MEMROA:  /* memory read only accessed */
  682                 case SDT_MEMRW:   /* memory read write */
  683                 case SDT_MEMRWA:  /* memory read write accessed */
  684                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  685                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  686                 case SDT_MEMRWD:  /* memory read write expand dwn limit */
  687                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  688                 case SDT_MEME:    /* memory execute only */
  689                 case SDT_MEMEA:   /* memory execute only accessed */
  690                 case SDT_MEMER:   /* memory execute read */
  691                 case SDT_MEMERA:  /* memory execute read accessed */
  692                         break;
  693                 default:
  694                         return(EINVAL);
  695                         /*NOTREACHED*/
  696                 }
  697 
  698                 /* Only user (ring-3) descriptors may be present. */
  699                 if ((dp->sd_p != 0) && (dp->sd_dpl != SEL_UPL))
  700                         return (EACCES);
  701         }
  702 
  703         if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
  704                 /* Allocate a free slot */
  705                 mtx_lock(&dt_lock);
  706                 pldt = user_ldt_alloc(p, 0);
  707                 if (pldt == NULL) {
  708                         mtx_unlock(&dt_lock);
  709                         return (ENOMEM);
  710                 }
  711 
  712                 /*
  713                  * start scanning a bit up to leave room for NVidia and
  714                  * Wine, which still user the "Blat" method of allocation.
  715                  */
  716                 i = 16;
  717                 dp = &((struct user_segment_descriptor *)(pldt->ldt_base))[i];
  718                 for (; i < max_ldt_segment; ++i, ++dp) {
  719                         if (dp->sd_type == SDT_SYSNULL)
  720                                 break;
  721                 }
  722                 if (i >= max_ldt_segment) {
  723                         mtx_unlock(&dt_lock);
  724                         return (ENOSPC);
  725                 }
  726                 uap->start = i;
  727                 error = amd64_set_ldt_data(td, i, 1, descs);
  728                 mtx_unlock(&dt_lock);
  729         } else {
  730                 largest_ld = uap->start + uap->num;
  731                 if (largest_ld > max_ldt_segment)
  732                         return (EINVAL);
  733                 mtx_lock(&dt_lock);
  734                 if (user_ldt_alloc(p, 0) != NULL) {
  735                         error = amd64_set_ldt_data(td, uap->start, uap->num,
  736                             descs);
  737                 }
  738                 mtx_unlock(&dt_lock);
  739         }
  740         if (error == 0)
  741                 td->td_retval[0] = uap->start;
  742         return (error);
  743 }
  744 
  745 int
  746 amd64_set_ldt_data(struct thread *td, int start, int num,
  747     struct user_segment_descriptor *descs)
  748 {
  749         struct mdproc *mdp = &td->td_proc->p_md;
  750         struct proc_ldt *pldt = mdp->md_ldt;
  751 
  752         mtx_assert(&dt_lock, MA_OWNED);
  753 
  754         /* Fill in range */
  755         bcopy(descs,
  756             &((struct user_segment_descriptor *)(pldt->ldt_base))[start],
  757             num * sizeof(struct user_segment_descriptor));
  758         return (0);
  759 }

Cache object: 13e241e5617f919bb4cc99f9c5a48573


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