9 #define MAP_ANON MAP_ANONYMOUS
17 static int dev_zero
= -1;
21 addrspace_free(void *v
__attribute__ ((unused
)), uintptr n
__attribute__ ((unused
)))
24 size_t page_size
= getpagesize();
29 for(off
= 0; off
< n
; off
+= page_size
)
30 if(mincore((char *)v
+ off
, page_size
, (void *)&one_byte
) != -1
38 runtime_SysAlloc(uintptr n
)
47 dev_zero
= open("/dev/zero", O_RDONLY
);
49 printf("open /dev/zero: errno=%d\n", errno
);
56 p
= runtime_mmap(nil
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_PRIVATE
, fd
, 0);
57 if (p
== MAP_FAILED
) {
59 printf("runtime: mmap: access denied\n");
60 printf("if you're running SELinux, enable execmem for this process.\n");
69 runtime_SysUnused(void *v
, uintptr n
)
73 // TODO(rsc): call madvise MADV_DONTNEED
77 runtime_SysFree(void *v
, uintptr n
)
84 runtime_SysReserve(void *v
, uintptr n
)
88 // On 64-bit, people with ulimit -v set complain if we reserve too
89 // much address space. Instead, assume that the reservation is okay
90 // and check the assumption in SysMap.
91 if(sizeof(void*) == 8)
96 dev_zero
= open("/dev/zero", O_RDONLY
);
98 printf("open /dev/zero: errno=%d\n", errno
);
105 return runtime_mmap(v
, n
, PROT_NONE
, MAP_ANON
|MAP_PRIVATE
, fd
, 0);
109 runtime_SysMap(void *v
, uintptr n
)
117 if (dev_zero
== -1) {
118 dev_zero
= open("/dev/zero", O_RDONLY
);
120 printf("open /dev/zero: errno=%d\n", errno
);
127 // On 64-bit, we don't actually have v reserved, so tread carefully.
128 if(sizeof(void*) == 8) {
129 p
= runtime_mmap(v
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_PRIVATE
, fd
, 0);
130 if(p
!= v
&& addrspace_free(v
, n
)) {
131 // On some systems, mmap ignores v without
132 // MAP_FIXED, so retry if the address space is free.
133 p
= runtime_mmap(v
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, fd
, 0);
136 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v
, p
);
137 runtime_throw("runtime: address space conflict");
142 p
= runtime_mmap(v
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, fd
, 0);
144 runtime_throw("runtime: cannot map pages in arena address space");