db_memory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_memory.c
  15. *
  16. * This files contains APIs that allocate, free or protect virtual memory.
  17. */
  18. #include <sys/mman.h>
  19. #include <sys/sysctl.h>
  20. #include <sys/vmmeter.h>
  21. #include "api.h"
  22. #include "pal.h"
  23. #include "pal_debug.h"
  24. #include "pal_defs.h"
  25. #include "pal_error.h"
  26. #include "pal_freebsd.h"
  27. #include "pal_freebsd_defs.h"
  28. #include "pal_internal.h"
  29. bool _DkCheckMemoryMappable(const void* addr, size_t size) {
  30. return (addr <= DATA_END && addr + size >= TEXT_START);
  31. }
  32. int _DkVirtualMemoryAlloc(void** paddr, uint64_t size, int alloc_type, int prot) {
  33. void* addr = *paddr;
  34. void* mem = addr;
  35. int flags = HOST_FLAGS(alloc_type, prot | PAL_PROT_WRITECOPY);
  36. prot = HOST_PROT(prot);
  37. flags |= MAP_ANONYMOUS | (addr ? MAP_FIXED : 0);
  38. mem = (void*)ARCH_MMAP(addr, size, prot, flags, -1, 0);
  39. if (IS_ERR_P(mem))
  40. return unix_to_pal_error(ERRNO_P(mem));
  41. *paddr = mem;
  42. return 0;
  43. }
  44. int _DkVirtualMemoryFree(void* addr, uint64_t size) {
  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, uint64_t size, int prot) {
  49. int ret = INLINE_SYSCALL(mprotect, 3, addr, size, HOST_PROT(prot));
  50. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  51. }
  52. #define MEM_TOTAL 1
  53. #define MEM_FREE 2
  54. #define VM_TOTAL 1
  55. static int get_meminfo(int key, unsigned long* val) {
  56. int mib[2], len;
  57. struct vmtotal vm;
  58. int ret = -PAL_ERROR_DENIED;
  59. switch (key) {
  60. case MEM_TOTAL:
  61. mib[0] = CTL_HW;
  62. mib[1] = HW_REALMEM;
  63. len = sizeof(val);
  64. ret = INLINE_SYSCALL(__sysctl, 6, mib, 2, val, &len, NULL, 0);
  65. break;
  66. case MEM_FREE:
  67. mib[0] = CTL_VM;
  68. mib[1] = VM_TOTAL;
  69. len = sizeof(vm);
  70. ret = INLINE_SYSCALL(__sysctl, 6, mib, 2, &vm, &len, NULL, 0);
  71. *val = vm.t_free * 4096;
  72. break;
  73. }
  74. return ret;
  75. }
  76. unsigned long _DkMemoryQuota(void) {
  77. if (bsd_state.memory_quota)
  78. return bsd_state.memory_quota;
  79. unsigned long quota = 0;
  80. if (get_meminfo(MEM_TOTAL, &quota) < 0)
  81. return 0;
  82. return quota * 1024;
  83. }
  84. unsigned long _DkMemoryAvailableQuota(void) {
  85. unsigned long quota = 0;
  86. if (get_meminfo(MEM_FREE, &quota) < 0)
  87. return 0;
  88. return quota * 1024;
  89. }