db_memory.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "pal_defs.h"
  19. #include "pal_linux_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_linux.h"
  23. #include "pal_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. #include <asm/mman.h>
  27. #include <asm/fcntl.h>
  28. bool _DkCheckMemoryMappable (const void * addr, size_t size)
  29. {
  30. return (addr < DATA_END && addr + size > TEXT_START);
  31. }
  32. int _DkVirtualMemoryAlloc (void ** paddr, size_t size, int alloc_type,
  33. int prot)
  34. {
  35. void * addr = *paddr, * mem = addr;
  36. int flags = HOST_FLAGS(alloc_type, prot|PAL_PROT_WRITECOPY);
  37. prot = HOST_PROT(prot);
  38. flags |= MAP_ANONYMOUS|(addr ? MAP_FIXED : 0);
  39. mem = (void *) ARCH_MMAP(addr, size, prot, flags, -1, 0);
  40. if (IS_ERR_P(mem))
  41. return unix_to_pal_error(ERRNO_P(mem));
  42. *paddr = mem;
  43. return 0;
  44. }
  45. int _DkVirtualMemoryFree (void * addr, size_t size)
  46. {
  47. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  48. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  49. }
  50. int _DkVirtualMemoryProtect (void * addr, size_t size, int prot)
  51. {
  52. int ret = INLINE_SYSCALL(mprotect, 3, addr, size, HOST_PROT(prot));
  53. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  54. }
  55. static int read_proc_meminfo (const char * key, unsigned long * val)
  56. {
  57. int fd = INLINE_SYSCALL(open, 3, "/proc/meminfo", O_RDONLY, 0);
  58. if (IS_ERR(fd))
  59. return -PAL_ERROR_DENIED;
  60. char buffer[40];
  61. int ret = 0;
  62. size_t n;
  63. size_t r = 0;
  64. size_t len = strlen(key);
  65. ret = -PAL_ERROR_DENIED;
  66. while (1) {
  67. ret = INLINE_SYSCALL(read, 3, fd, buffer + r, 40 - r);
  68. if (IS_ERR(ret)) {
  69. ret = -PAL_ERROR_DENIED;
  70. break;
  71. }
  72. for (n = r ; n < r + ret ; n++)
  73. if (buffer[n] == '\n')
  74. break;
  75. r += ret;
  76. if (n == r + ret || n <= len) {
  77. ret = -PAL_ERROR_INVAL;
  78. break;
  79. }
  80. if (!memcmp(key, buffer, len) && buffer[len] == ':') {
  81. for (size_t i = len + 1; i < n ; i++)
  82. if (buffer[i] != ' ') {
  83. *val = atol(buffer + i);
  84. break;
  85. }
  86. ret = 0;
  87. break;
  88. }
  89. memmove(buffer, buffer + n + 1, r - n - 1);
  90. r -= n + 1;
  91. }
  92. INLINE_SYSCALL(close, 1, fd);
  93. return ret;
  94. }
  95. unsigned long _DkMemoryQuota (void)
  96. {
  97. if (linux_state.memory_quota == (unsigned long) -1)
  98. return 0;
  99. if (linux_state.memory_quota)
  100. return linux_state.memory_quota;
  101. unsigned long quota = 0;
  102. if (read_proc_meminfo("MemTotal", &quota) < 0) {
  103. linux_state.memory_quota = (unsigned long) -1;
  104. return 0;
  105. }
  106. return (linux_state.memory_quota = quota * 1024);
  107. }
  108. unsigned long _DkMemoryAvailableQuota (void)
  109. {
  110. unsigned long quota = 0;
  111. if (read_proc_meminfo("MemFree", &quota) < 0)
  112. return 0;
  113. return quota * 1024;
  114. }