mem.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Defining _XOPEN_SOURCE hides the declaration of madvise() on Solaris <
  2. 11 and the MADV_DONTNEED definition on IRIX 6.5. */
  3. #undef _XOPEN_SOURCE
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include "runtime.h"
  7. #include "arch.h"
  8. #include "malloc.h"
  9. #ifndef MAP_ANON
  10. #ifdef MAP_ANONYMOUS
  11. #define MAP_ANON MAP_ANONYMOUS
  12. #else
  13. #define USE_DEV_ZERO
  14. #define MAP_ANON 0
  15. #endif
  16. #endif
  17. #ifndef MAP_NORESERVE
  18. #define MAP_NORESERVE 0
  19. #endif
  20. #ifdef USE_DEV_ZERO
  21. static int dev_zero = -1;
  22. #endif
  23. static int32
  24. addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
  25. {
  26. #ifdef HAVE_MINCORE
  27. size_t page_size = getpagesize();
  28. int32 errval;
  29. uintptr chunk;
  30. uintptr off;
  31. // NOTE: vec must be just 1 byte long here.
  32. // Mincore returns ENOMEM if any of the pages are unmapped,
  33. // but we want to know that all of the pages are unmapped.
  34. // To make these the same, we can only ask about one page
  35. // at a time. See golang.org/issue/7476.
  36. static byte vec[1];
  37. errno = 0;
  38. for(off = 0; off < n; off += chunk) {
  39. chunk = page_size * sizeof vec;
  40. if(chunk > (n - off))
  41. chunk = n - off;
  42. errval = mincore((char*)v + off, chunk, (void*)vec);
  43. // ENOMEM means unmapped, which is what we want.
  44. // Anything else we assume means the pages are mapped.
  45. if(errval == 0 || errno != ENOMEM)
  46. return 0;
  47. }
  48. #endif
  49. return 1;
  50. }
  51. static void *
  52. mmap_fixed(byte *v, uintptr n, int32 prot, int32 flags, int32 fd, uint32 offset)
  53. {
  54. void *p;
  55. p = runtime_mmap((void *)v, n, prot, flags, fd, offset);
  56. if(p != v && addrspace_free(v, n)) {
  57. // On some systems, mmap ignores v without
  58. // MAP_FIXED, so retry if the address space is free.
  59. if(p != MAP_FAILED)
  60. runtime_munmap(p, n);
  61. p = runtime_mmap((void *)v, n, prot, flags|MAP_FIXED, fd, offset);
  62. }
  63. return p;
  64. }
  65. void*
  66. runtime_SysAlloc(uintptr n, uint64 *stat)
  67. {
  68. void *p;
  69. int fd = -1;
  70. #ifdef USE_DEV_ZERO
  71. if (dev_zero == -1) {
  72. dev_zero = open("/dev/zero", O_RDONLY);
  73. if (dev_zero < 0) {
  74. runtime_printf("open /dev/zero: errno=%d\n", errno);
  75. exit(2);
  76. }
  77. }
  78. fd = dev_zero;
  79. #endif
  80. p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, fd, 0);
  81. if (p == MAP_FAILED) {
  82. if(errno == EACCES) {
  83. runtime_printf("runtime: mmap: access denied\n");
  84. runtime_printf("if you're running SELinux, enable execmem for this process.\n");
  85. exit(2);
  86. }
  87. if(errno == EAGAIN) {
  88. runtime_printf("runtime: mmap: too much locked memory (check 'ulimit -l').\n");
  89. runtime_exit(2);
  90. }
  91. return nil;
  92. }
  93. runtime_xadd64(stat, n);
  94. return p;
  95. }
  96. void
  97. runtime_SysUnused(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
  98. {
  99. #ifdef MADV_DONTNEED
  100. runtime_madvise(v, n, MADV_DONTNEED);
  101. #endif
  102. }
  103. void
  104. runtime_SysUsed(void *v, uintptr n)
  105. {
  106. USED(v);
  107. USED(n);
  108. }
  109. void
  110. runtime_SysFree(void *v, uintptr n, uint64 *stat)
  111. {
  112. runtime_xadd64(stat, -(uint64)n);
  113. runtime_munmap(v, n);
  114. }
  115. void
  116. runtime_SysFault(void *v, uintptr n)
  117. {
  118. int fd = -1;
  119. #ifdef USE_DEV_ZERO
  120. if (dev_zero == -1) {
  121. dev_zero = open("/dev/zero", O_RDONLY);
  122. if (dev_zero < 0) {
  123. runtime_printf("open /dev/zero: errno=%d\n", errno);
  124. exit(2);
  125. }
  126. }
  127. fd = dev_zero;
  128. #endif
  129. runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, fd, 0);
  130. }
  131. void*
  132. runtime_SysReserve(void *v, uintptr n, bool *reserved)
  133. {
  134. int fd = -1;
  135. void *p;
  136. #ifdef USE_DEV_ZERO
  137. if (dev_zero == -1) {
  138. dev_zero = open("/dev/zero", O_RDONLY);
  139. if (dev_zero < 0) {
  140. runtime_printf("open /dev/zero: errno=%d\n", errno);
  141. exit(2);
  142. }
  143. }
  144. fd = dev_zero;
  145. #endif
  146. // On 64-bit, people with ulimit -v set complain if we reserve too
  147. // much address space. Instead, assume that the reservation is okay
  148. // if we can reserve at least 64K and check the assumption in SysMap.
  149. // Only user-mode Linux (UML) rejects these requests.
  150. if(sizeof(void*) == 8 && (n >> 16) > 1LLU<<16) {
  151. p = mmap_fixed(v, 64<<10, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
  152. if (p != v) {
  153. runtime_munmap(p, 64<<10);
  154. return nil;
  155. }
  156. runtime_munmap(p, 64<<10);
  157. *reserved = false;
  158. return v;
  159. }
  160. // Use the MAP_NORESERVE mmap() flag here because typically most of
  161. // this reservation will never be used. It does not make sense
  162. // reserve a huge amount of unneeded swap space. This is important on
  163. // systems which do not overcommit memory by default.
  164. p = runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_NORESERVE, fd, 0);
  165. if(p == MAP_FAILED)
  166. return nil;
  167. *reserved = true;
  168. return p;
  169. }
  170. void
  171. runtime_SysMap(void *v, uintptr n, bool reserved, uint64 *stat)
  172. {
  173. void *p;
  174. int fd = -1;
  175. runtime_xadd64(stat, n);
  176. #ifdef USE_DEV_ZERO
  177. if (dev_zero == -1) {
  178. dev_zero = open("/dev/zero", O_RDONLY);
  179. if (dev_zero < 0) {
  180. runtime_printf("open /dev/zero: errno=%d\n", errno);
  181. exit(2);
  182. }
  183. }
  184. fd = dev_zero;
  185. #endif
  186. // On 64-bit, we don't actually have v reserved, so tread carefully.
  187. if(!reserved) {
  188. p = mmap_fixed(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, fd, 0);
  189. if(p == MAP_FAILED && errno == ENOMEM)
  190. runtime_throw("runtime: out of memory");
  191. if(p != v) {
  192. runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
  193. runtime_throw("runtime: address space conflict");
  194. }
  195. return;
  196. }
  197. p = runtime_mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
  198. if(p == MAP_FAILED && errno == ENOMEM)
  199. runtime_throw("runtime: out of memory");
  200. if(p != v)
  201. runtime_throw("runtime: cannot map pages in arena address space");
  202. }