10 #define MAP_ANON MAP_ANONYMOUS
18 static int dev_zero = -1;
22 addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
25 size_t page_size = getpagesize();
30 for(off = 0; off < n; off += page_size)
31 if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
39 runtime_SysAlloc(uintptr n)
48 dev_zero = open("/dev/zero", O_RDONLY);
50 printf("open /dev/zero: errno=%d\n", errno);
57 p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
58 if (p == MAP_FAILED) {
60 printf("runtime: mmap: access denied\n");
61 printf("if you're running SELinux, enable execmem for this process.\n");
70 runtime_SysUnused(void *v, uintptr n)
74 // TODO(rsc): call madvise MADV_DONTNEED
78 runtime_SysFree(void *v, uintptr n)
85 runtime_SysReserve(void *v, uintptr n)
89 // On 64-bit, people with ulimit -v set complain if we reserve too
90 // much address space. Instead, assume that the reservation is okay
91 // and check the assumption in SysMap.
92 if(sizeof(void*) == 8)
97 dev_zero = open("/dev/zero", O_RDONLY);
99 printf("open /dev/zero: errno=%d\n", errno);
106 return runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
110 runtime_SysMap(void *v, uintptr n)
118 if (dev_zero == -1) {
119 dev_zero = open("/dev/zero", O_RDONLY);
121 printf("open /dev/zero: errno=%d\n", errno);
128 // On 64-bit, we don't actually have v reserved, so tread carefully.
129 if(sizeof(void*) == 8) {
130 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
131 if(p != v && addrspace_free(v, n)) {
132 // On some systems, mmap ignores v without
133 // MAP_FIXED, so retry if the address space is free.
134 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
137 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
138 runtime_throw("runtime: address space conflict");
143 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
145 runtime_throw("runtime: cannot map pages in arena address space");