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 static int dev_zero = -1;
26 addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
29 size_t page_size = getpagesize();
34 for(off = 0; off < n; off += page_size)
35 if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
43 runtime_SysAlloc(uintptr n)
52 dev_zero = open("/dev/zero", O_RDONLY);
54 runtime_printf("open /dev/zero: errno=%d\n", errno);
61 p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
62 if (p == MAP_FAILED) {
64 runtime_printf("runtime: mmap: access denied\n");
65 runtime_printf("if you're running SELinux, enable execmem for this process.\n");
74 runtime_SysUnused(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
77 runtime_madvise(v, n, MADV_DONTNEED);
82 runtime_SysFree(void *v, uintptr n)
89 runtime_SysReserve(void *v, uintptr n)
94 // On 64-bit, people with ulimit -v set complain if we reserve too
95 // much address space. Instead, assume that the reservation is okay
96 // and check the assumption in SysMap.
97 if(sizeof(void*) == 8)
101 if (dev_zero == -1) {
102 dev_zero = open("/dev/zero", O_RDONLY);
104 runtime_printf("open /dev/zero: errno=%d\n", errno);
111 p = runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
112 if((uintptr)p < 4096 || -(uintptr)p < 4096) {
119 runtime_SysMap(void *v, uintptr n)
127 if (dev_zero == -1) {
128 dev_zero = open("/dev/zero", O_RDONLY);
130 runtime_printf("open /dev/zero: errno=%d\n", errno);
137 // On 64-bit, we don't actually have v reserved, so tread carefully.
138 if(sizeof(void*) == 8) {
139 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
140 if(p != v && addrspace_free(v, n)) {
141 // On some systems, mmap ignores v without
142 // MAP_FIXED, so retry if the address space is free.
143 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
146 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
147 runtime_throw("runtime: address space conflict");
152 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
154 runtime_throw("runtime: cannot map pages in arena address space");