1 /* Defining _XOPEN_SOURCE hides the declaration of madvise() on Solaris <
2 11 and the MADV_DONTNEED definition on IRIX 6.5. */
14 #define MAP_ANON MAP_ANONYMOUS
22 #define MAP_NORESERVE 0
26 static int dev_zero = -1;
30 addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
33 size_t page_size = getpagesize();
38 for(off = 0; off < n; off += page_size)
39 if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
47 mmap_fixed(byte *v, uintptr n, int32 prot, int32 flags, int32 fd, uint32 offset)
51 p = runtime_mmap((void *)v, n, prot, flags, fd, offset);
52 if(p != v && addrspace_free(v, n)) {
53 // On some systems, mmap ignores v without
54 // MAP_FIXED, so retry if the address space is free.
57 p = runtime_mmap((void *)v, n, prot, flags|MAP_FIXED, fd, offset);
63 runtime_SysAlloc(uintptr n)
72 dev_zero = open("/dev/zero", O_RDONLY);
74 runtime_printf("open /dev/zero: errno=%d\n", errno);
81 p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, fd, 0);
82 if (p == MAP_FAILED) {
84 runtime_printf("runtime: mmap: access denied\n");
85 runtime_printf("if you're running SELinux, enable execmem for this process.\n");
89 runtime_printf("runtime: mmap: too much locked memory (check 'ulimit -l').\n");
98 runtime_SysUnused(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
101 runtime_madvise(v, n, MADV_DONTNEED);
106 runtime_SysFree(void *v, uintptr n)
109 runtime_munmap(v, n);
113 runtime_SysReserve(void *v, uintptr n)
119 if (dev_zero == -1) {
120 dev_zero = open("/dev/zero", O_RDONLY);
122 runtime_printf("open /dev/zero: errno=%d\n", errno);
129 // On 64-bit, people with ulimit -v set complain if we reserve too
130 // much address space. Instead, assume that the reservation is okay
131 // if we can reserve at least 64K and check the assumption in SysMap.
132 // Only user-mode Linux (UML) rejects these requests.
133 if(sizeof(void*) == 8 && (uintptr)v >= 0xffffffffU) {
134 p = mmap_fixed(v, 64<<10, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
137 runtime_munmap(p, 64<<10);
141 // Use the MAP_NORESERVE mmap() flag here because typically most of
142 // this reservation will never be used. It does not make sense
143 // reserve a huge amount of unneeded swap space. This is important on
144 // systems which do not overcommit memory by default.
145 p = runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_NORESERVE, fd, 0);
152 runtime_SysMap(void *v, uintptr n)
160 if (dev_zero == -1) {
161 dev_zero = open("/dev/zero", O_RDONLY);
163 runtime_printf("open /dev/zero: errno=%d\n", errno);
170 // On 64-bit, we don't actually have v reserved, so tread carefully.
171 if(sizeof(void*) == 8 && (uintptr)v >= 0xffffffffU) {
172 p = mmap_fixed(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, fd, 0);
173 if(p == MAP_FAILED && errno == ENOMEM)
174 runtime_throw("runtime: out of memory");
176 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
177 runtime_throw("runtime: address space conflict");
182 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
183 if(p == MAP_FAILED && errno == ENOMEM)
184 runtime_throw("runtime: out of memory");
186 runtime_throw("runtime: cannot map pages in arena address space");