2012-01-17 15:18:39 +01:00
|
|
|
/* Defining _XOPEN_SOURCE hides the declaration of madvise() on Solaris <
|
|
|
|
11 and the MADV_DONTNEED definition on IRIX 6.5. */
|
|
|
|
#undef _XOPEN_SOURCE
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
#include <errno.h>
|
2011-04-05 02:02:15 +02:00
|
|
|
#include <unistd.h>
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
#include "runtime.h"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
|
2011-01-29 08:16:20 +01:00
|
|
|
#ifndef MAP_ANON
|
|
|
|
#ifdef MAP_ANONYMOUS
|
|
|
|
#define MAP_ANON MAP_ANONYMOUS
|
|
|
|
#else
|
|
|
|
#define USE_DEV_ZERO
|
|
|
|
#define MAP_ANON 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_DEV_ZERO
|
|
|
|
static int dev_zero = -1;
|
|
|
|
#endif
|
|
|
|
|
2011-04-05 02:02:15 +02:00
|
|
|
static _Bool
|
|
|
|
addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MINCORE
|
|
|
|
size_t page_size = getpagesize();
|
|
|
|
size_t off;
|
|
|
|
char one_byte;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
for(off = 0; off < n; off += page_size)
|
|
|
|
if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
|
|
|
|
|| errno != ENOMEM)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
void*
|
|
|
|
runtime_SysAlloc(uintptr n)
|
|
|
|
{
|
|
|
|
void *p;
|
2011-01-29 08:16:20 +01:00
|
|
|
int fd = -1;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
mstats.sys += n;
|
2011-01-29 08:16:20 +01:00
|
|
|
|
|
|
|
#ifdef USE_DEV_ZERO
|
|
|
|
if (dev_zero == -1) {
|
|
|
|
dev_zero = open("/dev/zero", O_RDONLY);
|
|
|
|
if (dev_zero < 0) {
|
2011-12-21 23:24:47 +01:00
|
|
|
runtime_printf("open /dev/zero: errno=%d\n", errno);
|
2011-01-29 08:16:20 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fd = dev_zero;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
|
2010-12-03 05:34:57 +01:00
|
|
|
if (p == MAP_FAILED) {
|
|
|
|
if(errno == EACCES) {
|
2011-12-21 23:24:47 +01:00
|
|
|
runtime_printf("runtime: mmap: access denied\n");
|
|
|
|
runtime_printf("if you're running SELinux, enable execmem for this process.\n");
|
2011-03-17 00:05:44 +01:00
|
|
|
exit(2);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
return nil;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-17 15:18:39 +01:00
|
|
|
runtime_SysUnused(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2012-01-12 02:31:45 +01:00
|
|
|
#ifdef MADV_DONTNEED
|
|
|
|
runtime_madvise(v, n, MADV_DONTNEED);
|
|
|
|
#endif
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_SysFree(void *v, uintptr n)
|
|
|
|
{
|
|
|
|
mstats.sys -= n;
|
|
|
|
runtime_munmap(v, n);
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
void*
|
|
|
|
runtime_SysReserve(void *v, uintptr n)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
2011-11-01 05:55:15 +01:00
|
|
|
void *p;
|
2011-03-17 00:05:44 +01:00
|
|
|
|
|
|
|
// On 64-bit, people with ulimit -v set complain if we reserve too
|
|
|
|
// much address space. Instead, assume that the reservation is okay
|
|
|
|
// and check the assumption in SysMap.
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
return v;
|
|
|
|
|
|
|
|
#ifdef USE_DEV_ZERO
|
|
|
|
if (dev_zero == -1) {
|
|
|
|
dev_zero = open("/dev/zero", O_RDONLY);
|
|
|
|
if (dev_zero < 0) {
|
2011-12-21 23:24:47 +01:00
|
|
|
runtime_printf("open /dev/zero: errno=%d\n", errno);
|
2011-03-17 00:05:44 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fd = dev_zero;
|
|
|
|
#endif
|
|
|
|
|
2011-11-01 05:55:15 +01:00
|
|
|
p = runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
|
|
|
|
if((uintptr)p < 4096 || -(uintptr)p < 4096) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return p;
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
void
|
2011-03-17 00:05:44 +01:00
|
|
|
runtime_SysMap(void *v, uintptr n)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2011-03-17 00:05:44 +01:00
|
|
|
void *p;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
mstats.sys += n;
|
|
|
|
|
|
|
|
#ifdef USE_DEV_ZERO
|
|
|
|
if (dev_zero == -1) {
|
|
|
|
dev_zero = open("/dev/zero", O_RDONLY);
|
|
|
|
if (dev_zero < 0) {
|
2011-12-21 23:24:47 +01:00
|
|
|
runtime_printf("open /dev/zero: errno=%d\n", errno);
|
2011-03-17 00:05:44 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fd = dev_zero;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// On 64-bit, we don't actually have v reserved, so tread carefully.
|
|
|
|
if(sizeof(void*) == 8) {
|
|
|
|
p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
|
2011-04-05 02:02:15 +02:00
|
|
|
if(p != v && addrspace_free(v, n)) {
|
|
|
|
// On some systems, mmap ignores v without
|
|
|
|
// MAP_FIXED, so retry if the address space is free.
|
|
|
|
p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
if(p != v) {
|
|
|
|
runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
|
|
|
|
runtime_throw("runtime: address space conflict");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
|
|
|
|
if(p != v)
|
|
|
|
runtime_throw("runtime: cannot map pages in arena address space");
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|