db_memory.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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_freebsd_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_freebsd.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <sys/mman.h>
  29. #include <sys/sysctl.h>
  30. #include <sys/vmmeter.h>
  31. bool _DkCheckMemoryMappable (const void * addr, int size)
  32. {
  33. return (addr <= DATA_END && addr + size >= TEXT_START);
  34. }
  35. int _DkVirtualMemoryAlloc (void ** paddr, int size, int alloc_type,
  36. int prot)
  37. {
  38. void * addr = *paddr, * mem = addr;
  39. int flags = HOST_FLAGS(alloc_type, prot|PAL_PROT_WRITECOPY);
  40. prot = HOST_PROT(prot);
  41. /* The memory should have MAP_PRIVATE and MAP_ANONYMOUS */
  42. flags |= MAP_ANONYMOUS|(addr ? MAP_FIXED : 0);
  43. mem = (void *) ARCH_MMAP(addr, size, prot, flags, -1, 0);
  44. if (IS_ERR_P(mem))
  45. return unix_to_pal_error(ERRNO_P(mem));
  46. *paddr = mem;
  47. return 0;
  48. }
  49. int _DkVirtualMemoryFree (void * addr, int size)
  50. {
  51. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  52. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  53. }
  54. int _DkVirtualMemoryProtect (void * addr, int size, int prot)
  55. {
  56. int ret = INLINE_SYSCALL(mprotect, 3, addr, size, HOST_PROT(prot));
  57. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  58. }
  59. #define MEM_TOTAL 1
  60. #define MEM_FREE 2
  61. #define VM_TOTAL 1
  62. static int get_meminfo (int key, unsigned long * val)
  63. {
  64. int mib[2], len;
  65. struct vmtotal vm;
  66. int ret = -PAL_ERROR_DENIED;
  67. switch(key){
  68. case MEM_TOTAL:
  69. mib[0] = CTL_HW;
  70. mib[1] = HW_REALMEM;
  71. len = sizeof(val);
  72. ret = INLINE_SYSCALL(__sysctl, 6, mib, 2, val, &len, NULL, 0);
  73. break;
  74. case MEM_FREE:
  75. mib[0] = CTL_VM;
  76. mib[1] = VM_TOTAL;
  77. len = sizeof(vm);
  78. ret = INLINE_SYSCALL(__sysctl, 6, mib, 2, &vm, &len, NULL, 0);
  79. *val = vm.t_free * 4096;
  80. break;
  81. }
  82. return ret;
  83. }
  84. unsigned long _DkMemoryQuota (void)
  85. {
  86. if (bsd_state.memory_quota)
  87. return bsd_state.memory_quota;
  88. unsigned long quota = 0;
  89. if (get_meminfo(MEM_TOTAL, &quota) < 0)
  90. return 0;
  91. return quota * 1024;
  92. }
  93. unsigned long _DkMemoryAvailableQuota (void)
  94. {
  95. unsigned long quota = 0;
  96. if (get_meminfo(MEM_FREE, &quota) < 0)
  97. return 0;
  98. return quota * 1024;
  99. }