View of xos/usr/lib/libc/mman.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include <sys/mman.h>
 
#include "xos.h"
 
#include <limits.h>
#include <errno.h>
 
#define PAGE_OFFSET(addr) ((addr) & (PAGE_SIZE - 1))
 
void __attribute__ ((weak, visibility ("default"))) *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
{
  int xos_prot;
  unsigned long poff;
 
  if (prot & PROT_READ) {
    if (prot & PROT_WRITE)
      xos_prot = _XOS_PROT_RDWR;
    else
      xos_prot = _XOS_PROT_RDONLY;
  }
  else {
    errno = EINVAL;
    return MAP_FAILED;
  }
  if ((flags & MAP_SHARED && flags & MAP_PRIVATE) || (!(flags & MAP_SHARED) && !(flags & MAP_PRIVATE))) {
    errno = EINVAL;
    return MAP_FAILED;
  }
  if (flags & MAP_SHARED) {
    errno = EINVAL;
    return MAP_FAILED;
  }
  if (!(flags & MAP_FIXED) && addr) {
    poff = PAGE_OFFSET((unsigned long)addr);
    addr -= poff;
    len = len + poff;
    if (!(flags & MAP_ANONYMOUS))
      off -= poff;
  }
  if (flags & MAP_ANONYMOUS)
    fd = -1;
  return (void *)__syscall_mmap(addr, len, xos_prot, fd, off);
}
 
int __attribute__ ((weak, visibility ("default"))) munmap(void *addr, size_t len)
{
  return __syscall_munmap(addr, len);
}
 
int __attribute__ ((weak, visibility ("default"))) mprotect(void *addr, size_t len, int prot)
{
  int xos_prot;
 
  if (prot & PROT_READ) {
    if (prot & PROT_WRITE)
      xos_prot = _XOS_PROT_RDWR;
    else
      xos_prot = _XOS_PROT_RDONLY;
  }
  else {
    errno = EINVAL;
    return -1;
  }
  return __syscall_mprotect(addr, len, xos_prot);
}