db_memory.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_memory.c
  17. *
  18. * This files contains APIs that allocate, free or protect virtual memory.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <asm/mman.h>
  29. int _DkVirtualMemoryAlloc (void ** paddr, size_t size, int alloc_type,
  30. int prot)
  31. {
  32. void * addr = *paddr, * mem = addr;
  33. int flags = HOST_FLAGS(alloc_type, prot|PAL_PROT_WRITECOPY);
  34. prot = HOST_PROT(prot);
  35. /* The memory should have MAP_PRIVATE and MAP_ANONYMOUS */
  36. flags |= MAP_ANONYMOUS|(addr ? MAP_FIXED : 0);
  37. mem = (void *) ARCH_MMAP(addr, size, prot, flags, -1, 0);
  38. if (IS_ERR_P(mem))
  39. return unix_to_pal_error(ERRNO_P(mem));
  40. *paddr = mem;
  41. return 0;
  42. }
  43. int _DkVirtualMemoryFree (void * addr, size_t size)
  44. {
  45. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  46. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  47. }
  48. int _DkVirtualMemoryProtect (void * addr, size_t size, int prot)
  49. {
  50. int ret = INLINE_SYSCALL(mprotect, 3, addr, size, HOST_PROT(prot));
  51. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  52. }