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/i386/i386/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) 1990 The Regents of the University of California.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
   34  * $FreeBSD$
   35  *
   36  */
   37 
   38 #include "opt_user_ldt.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/sysproto.h>
   43 #include <sys/malloc.h>
   44 #include <sys/proc.h>
   45 
   46 #include <vm/vm.h>
   47 #include <sys/lock.h>
   48 #include <vm/pmap.h>
   49 #include <vm/vm_map.h>
   50 #include <vm/vm_extern.h>
   51 
   52 #include <sys/user.h>
   53 
   54 #include <machine/cpu.h>
   55 #include <machine/ipl.h>
   56 #include <machine/pcb_ext.h>    /* pcb.h included by sys/user.h */
   57 #include <machine/sysarch.h>
   58 #ifdef SMP
   59 #include <machine/smp.h>
   60 #endif
   61 
   62 #include <vm/vm_kern.h>         /* for kernel_map */
   63 
   64 #define MAX_LD 8192
   65 #define LD_PER_PAGE 512
   66 #define NEW_MAX_LD(num)  ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
   67 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
   68 
   69 
   70 
   71 #ifdef USER_LDT
   72 static int i386_get_ldt __P((struct proc *, char *));
   73 static int i386_set_ldt __P((struct proc *, char *));
   74 #endif
   75 static int i386_get_ioperm      __P((struct proc *, char *));
   76 static int i386_set_ioperm      __P((struct proc *, char *));
   77 int i386_extend_pcb     __P((struct proc *));
   78 
   79 #ifndef _SYS_SYSPROTO_H_
   80 struct sysarch_args {
   81         int op;
   82         char *parms;
   83 };
   84 #endif
   85 
   86 int
   87 sysarch(p, uap)
   88         struct proc *p;
   89         register struct sysarch_args *uap;
   90 {
   91         int error = 0;
   92 
   93         switch(uap->op) {
   94 #ifdef  USER_LDT
   95         case I386_GET_LDT:
   96                 error = i386_get_ldt(p, uap->parms);
   97                 break;
   98 
   99         case I386_SET_LDT:
  100                 error = i386_set_ldt(p, uap->parms);
  101                 break;
  102 #endif
  103         case I386_GET_IOPERM:
  104                 error = i386_get_ioperm(p, uap->parms);
  105                 break;
  106         case I386_SET_IOPERM:
  107                 error = i386_set_ioperm(p, uap->parms);
  108                 break;
  109         case I386_VM86:
  110                 error = vm86_sysarch(p, uap->parms);
  111                 break;
  112         default:
  113                 error = EOPNOTSUPP;
  114                 break;
  115         }
  116         return (error);
  117 }
  118 
  119 int
  120 i386_extend_pcb(struct proc *p)
  121 {
  122         int i, offset;
  123         u_long *addr;
  124         struct pcb_ext *ext;
  125         struct soft_segment_descriptor ssd = {
  126                 0,                      /* segment base address (overwritten) */
  127                 ctob(IOPAGES + 1) - 1,  /* length */
  128                 SDT_SYS386TSS,          /* segment type */
  129                 0,                      /* priority level */
  130                 1,                      /* descriptor present */
  131                 0, 0,
  132                 0,                      /* default 32 size */
  133                 0                       /* granularity */
  134         };
  135 
  136         ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
  137         if (ext == 0)
  138                 return (ENOMEM);
  139         p->p_addr->u_pcb.pcb_ext = ext;
  140         bzero(ext, sizeof(struct pcb_ext)); 
  141         ext->ext_tss.tss_esp0 = (unsigned)p->p_addr + ctob(UPAGES) - 16;
  142         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
  143         /*
  144          * The last byte of the i/o map must be followed by an 0xff byte.
  145          * We arbitrarily allocate 16 bytes here, to keep the starting
  146          * address on a doubleword boundary.
  147          */
  148         offset = PAGE_SIZE - 16;
  149         ext->ext_tss.tss_ioopt = 
  150             (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
  151         ext->ext_iomap = (caddr_t)ext + offset;
  152         ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
  153 
  154         addr = (u_long *)ext->ext_vm86.vm86_intmap;
  155         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
  156                 *addr++ = ~0;
  157 
  158         ssd.ssd_base = (unsigned)&ext->ext_tss;
  159         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
  160         ssdtosd(&ssd, &ext->ext_tssd);
  161         
  162         /* switch to the new TSS after syscall completes */
  163         need_resched();
  164 
  165         return 0;
  166 }
  167 
  168 static int
  169 i386_set_ioperm(p, args)
  170         struct proc *p;
  171         char *args;
  172 {
  173         int i, error;
  174         struct i386_ioperm_args ua;
  175         char *iomap;
  176 
  177         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
  178                 return (error);
  179 
  180         if ((error = suser(p)) != 0)
  181                 return (error);
  182         if (securelevel > 0)
  183                 return (EPERM);
  184         /*
  185          * XXX 
  186          * While this is restricted to root, we should probably figure out
  187          * whether any other driver is using this i/o address, as so not to
  188          * cause confusion.  This probably requires a global 'usage registry'.
  189          */
  190 
  191         if (p->p_addr->u_pcb.pcb_ext == 0)
  192                 if ((error = i386_extend_pcb(p)) != 0)
  193                         return (error);
  194         iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
  195 
  196         if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
  197                 return (EINVAL);
  198 
  199         for (i = ua.start; i < ua.start + ua.length; i++) {
  200                 if (ua.enable) 
  201                         iomap[i >> 3] &= ~(1 << (i & 7));
  202                 else
  203                         iomap[i >> 3] |= (1 << (i & 7));
  204         }
  205         return (error);
  206 }
  207 
  208 static int
  209 i386_get_ioperm(p, args)
  210         struct proc *p;
  211         char *args;
  212 {
  213         int i, state, error;
  214         struct i386_ioperm_args ua;
  215         char *iomap;
  216 
  217         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
  218                 return (error);
  219         if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
  220                 return (EINVAL);
  221 
  222         if (p->p_addr->u_pcb.pcb_ext == 0) {
  223                 ua.length = 0;
  224                 goto done;
  225         }
  226 
  227         iomap = (char *)p->p_addr->u_pcb.pcb_ext->ext_iomap;
  228 
  229         i = ua.start;
  230         state = (iomap[i >> 3] >> (i & 7)) & 1;
  231         ua.enable = !state;
  232         ua.length = 1;
  233 
  234         for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
  235                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
  236                         break;
  237                 ua.length++;
  238         }
  239                         
  240 done:
  241         error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
  242         return (error);
  243 }
  244 
  245 #ifdef USER_LDT
  246 /*
  247  * Update the GDT entry pointing to the LDT to point to the LDT of the
  248  * current process.  Do not staticize.
  249  */   
  250 void
  251 set_user_ldt(struct pcb *pcb)
  252 {
  253         struct pcb_ldt *pcb_ldt;
  254 
  255         if (pcb != curpcb)
  256                 return;
  257 
  258         pcb_ldt = pcb->pcb_ldt;
  259 #ifdef SMP
  260         gdt[cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
  261 #else
  262         gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
  263 #endif
  264         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
  265         currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
  266 }
  267 
  268 struct pcb_ldt *
  269 user_ldt_alloc(struct pcb *pcb, int len)
  270 {
  271         struct pcb_ldt *pcb_ldt, *new_ldt;
  272 
  273         MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
  274                 M_SUBPROC, M_WAITOK);
  275         if (new_ldt == NULL)
  276                 return NULL;
  277 
  278         new_ldt->ldt_len = len = NEW_MAX_LD(len);
  279         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
  280                 len * sizeof(union descriptor));
  281         if (new_ldt->ldt_base == NULL) {
  282                 FREE(new_ldt, M_SUBPROC);
  283                 return NULL;
  284         }
  285         new_ldt->ldt_refcnt = 1;
  286         new_ldt->ldt_active = 0;
  287 
  288         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
  289         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
  290         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
  291 
  292         if ((pcb_ldt = pcb->pcb_ldt)) {
  293                 if (len > pcb_ldt->ldt_len)
  294                         len = pcb_ldt->ldt_len;
  295                 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
  296                         len * sizeof(union descriptor));
  297         } else {
  298                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
  299         }
  300         return new_ldt;
  301 }
  302 
  303 void
  304 user_ldt_free(struct pcb *pcb)
  305 {
  306         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
  307 
  308         if (pcb_ldt == NULL)
  309                 return;
  310 
  311         if (pcb == curpcb) {
  312                 lldt(_default_ldt);
  313                 currentldt = _default_ldt;
  314         }
  315 
  316         if (--pcb_ldt->ldt_refcnt == 0) {
  317                 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
  318                         pcb_ldt->ldt_len * sizeof(union descriptor));
  319                 FREE(pcb_ldt, M_SUBPROC);
  320         }
  321         pcb->pcb_ldt = NULL;
  322 }
  323 
  324 static int
  325 i386_get_ldt(p, args)
  326         struct proc *p;
  327         char *args;
  328 {
  329         int error = 0;
  330         struct pcb *pcb = &p->p_addr->u_pcb;
  331         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
  332         int nldt, num;
  333         union descriptor *lp;
  334         int s;
  335         struct i386_ldt_args ua, *uap = &ua;
  336 
  337         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
  338                 return(error);
  339 
  340 #ifdef  DEBUG
  341         printf("i386_get_ldt: start=%d num=%d descs=%p\n",
  342             uap->start, uap->num, (void *)uap->descs);
  343 #endif
  344 
  345         s = splhigh();
  346 
  347         if (pcb_ldt) {
  348                 nldt = pcb_ldt->ldt_len;
  349                 num = min(uap->num, nldt);
  350                 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
  351         } else {
  352                 nldt = sizeof(ldt)/sizeof(ldt[0]);
  353                 num = min(uap->num, nldt);
  354                 lp = &ldt[uap->start];
  355         }
  356 
  357         if ((uap->start > (unsigned int)nldt) ||
  358             ((unsigned int)num > (unsigned int)nldt) ||
  359             ((unsigned int)(uap->start + num) > (unsigned int)nldt)) {
  360                 splx(s);
  361                 return(EINVAL);
  362         }
  363 
  364         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
  365         if (!error)
  366                 p->p_retval[0] = num;
  367 
  368         splx(s);
  369         return(error);
  370 }
  371 
  372 static int
  373 i386_set_ldt(p, args)
  374         struct proc *p;
  375         char *args;
  376 {
  377         int error = 0, i, n;
  378         int largest_ld;
  379         struct pcb *pcb = &p->p_addr->u_pcb;
  380         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
  381         union descriptor *descs;
  382         int descs_size, s;
  383         struct i386_ldt_args ua, *uap = &ua;
  384 
  385         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
  386                 return(error);
  387 
  388 #ifdef  DEBUG
  389         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
  390             uap->start, uap->num, (void *)uap->descs);
  391 #endif
  392 
  393         /* verify range of descriptors to modify */
  394         if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
  395                 (uap->num > MAX_LD))
  396         {
  397                 return(EINVAL);
  398         }
  399         largest_ld = uap->start + uap->num - 1;
  400         if (largest_ld >= MAX_LD)
  401                 return(EINVAL);
  402 
  403         /* allocate user ldt */
  404         if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
  405                 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
  406                 if (new_ldt == NULL)
  407                         return ENOMEM;
  408                 if (pcb_ldt) {
  409                         pcb_ldt->ldt_sd = new_ldt->ldt_sd;
  410                         kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
  411                                 pcb_ldt->ldt_len * sizeof(union descriptor));
  412                         pcb_ldt->ldt_base = new_ldt->ldt_base;
  413                         pcb_ldt->ldt_len = new_ldt->ldt_len;
  414                         FREE(new_ldt, M_SUBPROC);
  415                 } else
  416                         pcb->pcb_ldt = pcb_ldt = new_ldt;
  417 #ifdef SMP
  418                 /* signal other cpus to reload ldt */
  419                 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt, NULL, pcb);
  420 #else
  421                 set_user_ldt(pcb);
  422 #endif
  423         }
  424 
  425         descs_size = uap->num * sizeof(union descriptor);
  426         descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
  427         if (descs == NULL)
  428                 return (ENOMEM);
  429         error = copyin(&uap->descs[0], descs, descs_size);
  430         if (error) {
  431                 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  432                 return (error);
  433         }
  434         /* Check descriptors for access violations */
  435         for (i = 0, n = uap->start; i < uap->num; i++, n++) {
  436                 union descriptor *dp;
  437                 dp = &descs[i];
  438 
  439                 switch (dp->sd.sd_type) {
  440                 case SDT_SYSNULL:       /* system null */ 
  441                         dp->sd.sd_p = 0;
  442                         break;
  443                 case SDT_SYS286TSS: /* system 286 TSS available */
  444                 case SDT_SYSLDT:    /* system local descriptor table */
  445                 case SDT_SYS286BSY: /* system 286 TSS busy */
  446                 case SDT_SYSTASKGT: /* system task gate */
  447                 case SDT_SYS286IGT: /* system 286 interrupt gate */
  448                 case SDT_SYS286TGT: /* system 286 trap gate */
  449                 case SDT_SYSNULL2:  /* undefined by Intel */ 
  450                 case SDT_SYS386TSS: /* system 386 TSS available */
  451                 case SDT_SYSNULL3:  /* undefined by Intel */
  452                 case SDT_SYS386BSY: /* system 386 TSS busy */
  453                 case SDT_SYSNULL4:  /* undefined by Intel */ 
  454                 case SDT_SYS386IGT: /* system 386 interrupt gate */
  455                 case SDT_SYS386TGT: /* system 386 trap gate */
  456                 case SDT_SYS286CGT: /* system 286 call gate */ 
  457                 case SDT_SYS386CGT: /* system 386 call gate */
  458                         /* I can't think of any reason to allow a user proc
  459                          * to create a segment of these types.  They are
  460                          * for OS use only.
  461                          */
  462                         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  463                         return EACCES;
  464 
  465                 /* memory segment types */
  466                 case SDT_MEMEC:   /* memory execute only conforming */
  467                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
  468                 case SDT_MEMERC:  /* memory execute read conforming */
  469                 case SDT_MEMERAC: /* memory execute read accessed conforming */
  470                         /* Must be "present" if executable and conforming. */
  471                         if (dp->sd.sd_p == 0) {
  472                                 kmem_free(kernel_map, (vm_offset_t)descs,
  473                                     descs_size);
  474                                 return (EACCES);
  475                         }
  476                         break;
  477                 case SDT_MEMRO:   /* memory read only */
  478                 case SDT_MEMROA:  /* memory read only accessed */
  479                 case SDT_MEMRW:   /* memory read write */
  480                 case SDT_MEMRWA:  /* memory read write accessed */
  481                 case SDT_MEMROD:  /* memory read only expand dwn limit */
  482                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
  483                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
  484                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
  485                 case SDT_MEME:    /* memory execute only */ 
  486                 case SDT_MEMEA:   /* memory execute only accessed */
  487                 case SDT_MEMER:   /* memory execute read */
  488                 case SDT_MEMERA:  /* memory execute read accessed */
  489                         break;
  490                 default:
  491                         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  492                         return(EINVAL);
  493                         /*NOTREACHED*/
  494                 }
  495 
  496                 /* Only user (ring-3) descriptors may be present. */
  497                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL)) {
  498                         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  499                         return (EACCES);
  500                 }
  501         }
  502 
  503         s = splhigh();
  504 
  505         /* Fill in range */
  506         bcopy(descs, 
  507                  &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
  508                 uap->num * sizeof(union descriptor));
  509         p->p_retval[0] = uap->start;
  510 
  511         splx(s);
  512         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
  513         return (0);
  514 }
  515 #endif  /* USER_LDT */

Cache object: 6a6b2c7e290bb4825947eb64dad965a2


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