db_memory.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_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. #include <asm/fcntl.h>
  30. bool _DkCheckMemoryMappable (const void * addr, int size)
  31. {
  32. return (addr < DATA_END && addr + size > TEXT_START);
  33. }
  34. int _DkVirtualMemoryAlloc (void ** paddr, uint64_t size, int alloc_type,
  35. int prot)
  36. {
  37. void * addr = *paddr, * mem = addr;
  38. int flags = HOST_FLAGS(alloc_type, prot|PAL_PROT_WRITECOPY);
  39. prot = HOST_PROT(prot);
  40. /* The memory should have MAP_PRIVATE and MAP_ANONYMOUS */
  41. flags |= MAP_ANONYMOUS|(addr ? MAP_FIXED : 0);
  42. mem = (void *) ARCH_MMAP(addr, size, prot, flags, -1, 0);
  43. if (IS_ERR_P(mem))
  44. return unix_to_pal_error(ERRNO_P(mem));
  45. *paddr = mem;
  46. return 0;
  47. }
  48. int _DkVirtualMemoryFree (void * addr, uint64_t size)
  49. {
  50. int ret = INLINE_SYSCALL(munmap, 2, addr, size);
  51. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  52. }
  53. int _DkVirtualMemoryProtect (void * addr, uint64_t size, int prot)
  54. {
  55. int ret = INLINE_SYSCALL(mprotect, 3, addr, size, HOST_PROT(prot));
  56. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  57. }
  58. static int read_proc_meminfo (const char * key, unsigned long * val)
  59. {
  60. int fd = INLINE_SYSCALL(open, 3, "/proc/meminfo", O_RDONLY, 0);
  61. if (IS_ERR(fd))
  62. return -PAL_ERROR_DENIED;
  63. char buffer[40];
  64. int r = 0, n, ret = 0, 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 (int 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. }