未验证 提交 e1f877d2 编写于 作者: Z Zihao Yu 提交者: GitHub

Merge pull request #99 from jiangyy/native-memfd

native,platform: use memfd_create() instead of shm_open()
#define _GNU_SOURCE
#include <sys/mman.h>
#include <sys/auxv.h>
#include <fcntl.h>
#include <elf.h>
#include <stdlib.h>
#include "platform.h"
......@@ -10,7 +10,6 @@
#define PMEM_START (void *)0x3000000 // for nanos-lite with vme disabled
#define PMEM_SIZE (128 * 1024 * 1024) // 128MB
static int pmem_fd = 0;
static char pmem_shm_file[] = "/native-pmem-XXXXXX";
static void *pmem = NULL;
static ucontext_t uc_example = {};
static int sys_pgsz;
......@@ -57,10 +56,8 @@ int main(const char *args);
static void init_platform() __attribute__((constructor));
static void init_platform() {
// create shared memory object and set up mapping to simulate the physical memory
assert(access("/", W_OK) != 0);
assert(mkstemp(pmem_shm_file) < 0);
pmem_fd = shm_open(pmem_shm_file, O_RDWR | O_CREAT | O_EXCL, 0700);
// create memory object and set up mapping to simulate the physical memory
pmem_fd = memfd_create("pmem", 0);
assert(pmem_fd != -1);
assert(0 == ftruncate(pmem_fd, PMEM_SIZE));
......@@ -159,16 +156,13 @@ static void init_platform() {
}
void __am_exit_platform(int code) {
int ret = shm_unlink(pmem_shm_file);
printf("Unlink pmem_shm_file %s\n", (ret == 0 ? "successfully" : "fail"));
// let Linux clean up other resource
extern int __am_mpe_init;
if (__am_mpe_init && _ncpu() > 1) kill(0, SIGKILL);
exit(code);
}
void __am_shm_mmap(void *va, void *pa, int prot) {
void __am_pmem_map(void *va, void *pa, int prot) {
// translate AM prot to mmap prot
int mmap_prot = PROT_NONE;
// we do not support executable bit, so mark
......@@ -180,7 +174,7 @@ void __am_shm_mmap(void *va, void *pa, int prot) {
assert(ret != (void *)-1);
}
void __am_shm_munmap(void *va) {
void __am_pmem_unmap(void *va) {
int ret = munmap(va, __am_pgsize);
assert(ret == 0);
}
......
......@@ -11,8 +11,8 @@ void __am_get_example_uc(_Context *r);
void __am_get_intr_sigmask(sigset_t *s);
int __am_is_sigmask_sti(sigset_t *s);
void __am_init_timer_irq();
void __am_shm_mmap(void *va, void *pa, int prot);
void __am_shm_munmap(void *va);
void __am_pmem_map(void *va, void *pa, int prot);
void __am_pmem_unmap(void *va);
// per-cpu structure
typedef struct {
......
......@@ -46,7 +46,7 @@ void __am_switch(_Context *c) {
// munmap all mappings
list_foreach(pp, thiscpu->vm_head) {
if (pp->is_mapped) {
__am_shm_munmap(pp->va);
__am_pmem_unmap(pp->va);
pp->is_mapped = false;
}
}
......@@ -56,7 +56,7 @@ void __am_switch(_Context *c) {
// mmap all mappings
list_foreach(pp, head) {
assert(IN_RANGE(pp->va, USER_SPACE));
__am_shm_mmap(pp->va, pp->pa, pp->prot);
__am_pmem_map(pp->va, pp->pa, pp->prot);
pp->is_mapped = true;
}
}
......@@ -90,7 +90,7 @@ void _map(_AddressSpace *as, void *va, void *pa, int prot) {
if (vm_head == thiscpu->vm_head) {
// enforce the map immediately
__am_shm_mmap(pp->va, pp->pa, pp->prot);
__am_pmem_map(pp->va, pp->pa, pp->prot);
pp->is_mapped = true;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册