Skip to content

Instantly share code, notes, and snippets.

@jerrinsg
Created June 19, 2018 20:47
Show Gist options
  • Save jerrinsg/d52939f489fbc7e9af22177e0e31bbc5 to your computer and use it in GitHub Desktop.
Save jerrinsg/d52939f489fbc7e9af22177e0e31bbc5 to your computer and use it in GitHub Desktop.
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <xmmintrin.h>
#include <stdio.h>
/* MMap nvm region. */
static void *
mapFile(const char * pathname, // File name.
int initSize, // Initial file size.
bool *isNew) // Is it a new file? [OUT].
{
*isNew = false;
void *addr;
int err;
int fd;
if (access(pathname, F_OK) != 0) {
if ((fd = open(pathname, O_RDWR | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR)) < 0)
{
printf("create file on NVM error\n");
return NULL;
}
printf("First time file creation\n");
*isNew = true;
if ((err = posix_fallocate(fd, 0, initSize)) != 0) {
printf("failed to allocate space\n");
close(fd);
return NULL;
}
if ((err = fsync(fd)) != 0) {
printf("fsync failed\n");
close(fd);
return NULL;
}
} else {
if ((fd = open(pathname, O_RDWR)) < 0) {
printf("open file on NVM error\n");
return NULL;
}
printf("File already exists on NVM\n");
*isNew = false;
}
if ((addr = mmap(NULL, initSize, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0)) == MAP_FAILED)
{
printf("failed to mmap\n");
close(fd);
return NULL;
}
close(fd);
return addr;
}
int main(int argc, char **argv) {
bool pmemnew;
char *pmemaddr = mapFile("/mnt/ext4-pmem0/testflush", 1024, &pmemnew);
if (pmemaddr == NULL) {
printf("File mapping failed\n");
exit(1);
}
// write to pmem
*(pmemaddr) = 47;
_mm_clflush(pmemaddr);
_mm_sfence();
long int ret_status = syscall(333); // 333 is a newly added syscall that causes kernel panic
printf("syscall ret status = %ld", ret_status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment