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 runtime_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 runtime_printf("runtime: mmap: access denied\n");
61 runtime_printf("if you're running SELinux, enable execmem for this process.\n");
70 runtime_SysUnused(void *v
, uintptr n
)
73 runtime_madvise(v
, n
, MADV_DONTNEED
);
78 runtime_SysFree(void *v
, uintptr n
)
85 runtime_SysReserve(void *v
, uintptr n
)
90 // On 64-bit, people with ulimit -v set complain if we reserve too
91 // much address space. Instead, assume that the reservation is okay
92 // and check the assumption in SysMap.
93 if(sizeof(void*) == 8)
98 dev_zero
= open("/dev/zero", O_RDONLY
);
100 runtime_printf("open /dev/zero: errno=%d\n", errno
);
107 p
= runtime_mmap(v
, n
, PROT_NONE
, MAP_ANON
|MAP_PRIVATE
, fd
, 0);
108 if((uintptr
)p
< 4096 || -(uintptr
)p
< 4096) {
115 runtime_SysMap(void *v
, uintptr n
)
123 if (dev_zero
== -1) {
124 dev_zero
= open("/dev/zero", O_RDONLY
);
126 runtime_printf("open /dev/zero: errno=%d\n", errno
);
133 // On 64-bit, we don't actually have v reserved, so tread carefully.
134 if(sizeof(void*) == 8) {
135 p
= runtime_mmap(v
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_PRIVATE
, fd
, 0);
136 if(p
!= v
&& addrspace_free(v
, n
)) {
137 // On some systems, mmap ignores v without
138 // MAP_FIXED, so retry if the address space is free.
139 p
= runtime_mmap(v
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, fd
, 0);
142 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v
, p
);
143 runtime_throw("runtime: address space conflict");
148 p
= runtime_mmap(v
, n
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, fd
, 0);
150 runtime_throw("runtime: cannot map pages in arena address space");