Back to index

glibc  2.9
mmap.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994,1995,1996,1997,1999,2002,2003,2004
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <sys/types.h>
00021 #include <sys/mman.h>
00022 #include <errno.h>
00023 #include <hurd.h>
00024 #include <hurd/fd.h>
00025 
00026 /* Map addresses starting near ADDR and extending for LEN bytes.  from
00027    OFFSET into the file FD describes according to PROT and FLAGS.  If ADDR
00028    is nonzero, it is the desired mapping address.  If the MAP_FIXED bit is
00029    set in FLAGS, the mapping will be at ADDR exactly (which must be
00030    page-aligned); otherwise the system chooses a convenient nearby address.
00031    The return value is the actual mapping address chosen or (__ptr_t) -1
00032    for errors (in which case `errno' is set).  A successful `mmap' call
00033    deallocates any previous mapping for the affected region.  */
00034 
00035 __ptr_t
00036 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
00037 {
00038   error_t err;
00039   vm_prot_t vmprot;
00040   memory_object_t memobj;
00041   vm_address_t mapaddr;
00042 
00043   mapaddr = (vm_address_t) addr;
00044 
00045   /* ADDR and OFFSET must be page-aligned.  */
00046   if ((mapaddr & (vm_page_size - 1)) || (offset & (vm_page_size - 1)))
00047     return (__ptr_t) (long int) __hurd_fail (EINVAL);
00048 
00049   if ((flags & (MAP_TYPE|MAP_INHERIT)) == MAP_ANON
00050       && prot == (PROT_READ|PROT_WRITE)) /* cf VM_PROT_DEFAULT */
00051     {
00052       /* vm_allocate has (a little) less overhead in the kernel too.  */
00053       err = __vm_allocate (__mach_task_self (), &mapaddr, len,
00054                         !(flags & MAP_FIXED));
00055 
00056       if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
00057        {
00058          /* XXX this is not atomic as it is in unix! */
00059          /* The region is already allocated; deallocate it first.  */
00060          err = __vm_deallocate (__mach_task_self (), mapaddr, len);
00061          if (!err)
00062            err = __vm_allocate (__mach_task_self (), &mapaddr, len, 0);
00063        }
00064 
00065       return err ? (__ptr_t) (long int) __hurd_fail (err) : (__ptr_t) mapaddr;
00066     }
00067 
00068   vmprot = VM_PROT_NONE;
00069   if (prot & PROT_READ)
00070     vmprot |= VM_PROT_READ;
00071   if (prot & PROT_WRITE)
00072     vmprot |= VM_PROT_WRITE;
00073   if (prot & PROT_EXEC)
00074     vmprot |= VM_PROT_EXECUTE;
00075 
00076   switch (flags & MAP_TYPE)
00077     {
00078     default:
00079       return (__ptr_t) (long int) __hurd_fail (EINVAL);
00080 
00081     case MAP_ANON:
00082       memobj = MACH_PORT_NULL;
00083       break;
00084 
00085     case MAP_FILE:
00086     case 0:                 /* Allow, e.g., just MAP_SHARED.  */
00087       {
00088        mach_port_t robj, wobj;
00089        if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
00090          {
00091            if (err == MIG_BAD_ID || err == EOPNOTSUPP || err == ENOSYS)
00092              err = ENODEV;  /* File descriptor doesn't support mmap.  */
00093            return (__ptr_t) (long int) __hurd_dfail (fd, err);
00094          }
00095        switch (prot & (PROT_READ|PROT_WRITE))
00096          {
00097          case PROT_READ:
00098            memobj = robj;
00099            if (wobj != MACH_PORT_NULL)
00100              __mach_port_deallocate (__mach_task_self (), wobj);
00101            break;
00102          case PROT_WRITE:
00103            memobj = wobj;
00104            if (robj != MACH_PORT_NULL)
00105              __mach_port_deallocate (__mach_task_self (), robj);
00106            break;
00107          case PROT_READ|PROT_WRITE:
00108            if (robj == wobj)
00109              {
00110               memobj = wobj;
00111               /* Remove extra reference.  */
00112               __mach_port_deallocate (__mach_task_self (), memobj);
00113              }
00114            else if (wobj == MACH_PORT_NULL && /* Not writable by mapping.  */
00115                    !(flags & MAP_SHARED))
00116              /* The file can only be mapped for reading.  Since we are
00117                making a private mapping, we will never try to write the
00118                object anyway, so we don't care.  */
00119              memobj = robj;
00120            else
00121              {
00122               __mach_port_deallocate (__mach_task_self (), wobj);
00123               return (__ptr_t) (long int) __hurd_fail (EACCES);
00124              }
00125            break;
00126          default:           /* impossible */
00127            return 0;
00128          }
00129        break;
00130        /* XXX handle MAP_NOEXTEND */
00131       }
00132     }
00133 
00134   /* XXX handle MAP_INHERIT */
00135 
00136   err = __vm_map (__mach_task_self (),
00137                 &mapaddr, (vm_size_t) len, (vm_address_t) 0,
00138                 ! (flags & MAP_FIXED),
00139                 memobj, (vm_offset_t) offset,
00140                 ! (flags & MAP_SHARED),
00141                 vmprot, VM_PROT_ALL,
00142                 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
00143 
00144   if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
00145     {
00146       /* XXX this is not atomic as it is in unix! */
00147       /* The region is already allocated; deallocate it first.  */
00148       err = __vm_deallocate (__mach_task_self (), mapaddr, len);
00149       if (! err)
00150        err = __vm_map (__mach_task_self (),
00151                      &mapaddr, (vm_size_t) len, (vm_address_t) 0,
00152                      0, memobj, (vm_offset_t) offset,
00153                      ! (flags & MAP_SHARED),
00154                      vmprot, VM_PROT_ALL,
00155                      (flags & MAP_SHARED) ? VM_INHERIT_SHARE
00156                      : VM_INHERIT_COPY);
00157     }
00158 
00159   if (memobj != MACH_PORT_NULL)
00160     __mach_port_deallocate (__mach_task_self (), memobj);
00161 
00162   if (err)
00163     return (__ptr_t) (long int) __hurd_fail (err);
00164 
00165   return (__ptr_t) mapaddr;
00166 }
00167 
00168 weak_alias (__mmap, mmap)