2018-08-29 00:53:12 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
void *memory_map(size_t size) {
|
|
|
|
void *p = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
|
|
|
|
if (unlikely(p == MAP_FAILED)) {
|
|
|
|
if (errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM mmap failure");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:04:52 -04:00
|
|
|
int memory_map_fixed(void *ptr, size_t size) {
|
|
|
|
void *p = mmap(ptr, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
|
|
|
|
if (unlikely(p == MAP_FAILED)) {
|
|
|
|
if (errno != ENOMEM) {
|
2018-10-10 19:05:35 -04:00
|
|
|
fatal_error("non-ENOMEM MAP_FIXED mmap failure");
|
2018-08-29 13:04:52 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-29 00:53:12 -04:00
|
|
|
int memory_unmap(void *ptr, size_t size) {
|
|
|
|
int ret = munmap(ptr, size);
|
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM munmap failure");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-19 21:29:40 -04:00
|
|
|
static int memory_protect_prot(void *ptr, size_t size, int prot, UNUSED int pkey) {
|
|
|
|
#ifdef USE_PKEY
|
|
|
|
int ret = pkey_mprotect(ptr, size, prot, pkey);
|
|
|
|
#else
|
2018-08-29 00:53:12 -04:00
|
|
|
int ret = mprotect(ptr, size, prot);
|
2018-10-19 21:29:40 -04:00
|
|
|
#endif
|
2018-08-29 00:53:12 -04:00
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM mprotect failure");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-29 10:43:54 -04:00
|
|
|
|
2018-10-19 21:29:40 -04:00
|
|
|
int memory_protect_ro(void *ptr, size_t size) {
|
|
|
|
return memory_protect_prot(ptr, size, PROT_READ, -1);
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:52:10 -04:00
|
|
|
int memory_protect_rw(void *ptr, size_t size) {
|
2018-10-19 21:29:40 -04:00
|
|
|
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, -1);
|
2018-08-29 10:52:10 -04:00
|
|
|
}
|
|
|
|
|
2018-10-19 21:29:40 -04:00
|
|
|
int memory_protect_rw_metadata(void *ptr, size_t size) {
|
|
|
|
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
|
2018-08-29 10:52:10 -04:00
|
|
|
}
|
|
|
|
|
2018-10-06 10:40:55 -04:00
|
|
|
int memory_remap(void *old, size_t old_size, size_t new_size) {
|
|
|
|
void *ptr = mremap(old, old_size, new_size, 0);
|
|
|
|
if (unlikely(ptr == MAP_FAILED)) {
|
|
|
|
if (errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM mremap failure");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:43:54 -04:00
|
|
|
int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) {
|
|
|
|
void *ptr = mremap(old, old_size, new_size, MREMAP_MAYMOVE|MREMAP_FIXED, new);
|
|
|
|
if (unlikely(ptr == MAP_FAILED)) {
|
|
|
|
if (errno != ENOMEM) {
|
2018-10-10 19:05:35 -04:00
|
|
|
fatal_error("non-ENOMEM MREMAP_FIXED mremap failure");
|
2018-08-29 10:43:54 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|