akaros/tests/mmap_file_vmm.c
<<
>>
Prefs
   1/* Copyright Google, Inc. 2017
   2 * Author: Zach Zimmerman
   3 * mmap_vmm_test: tests mmap with fd's with access from
   4 * vmthreads */
   5
   6#include <stdio.h>
   7#include <pthread.h>
   8#include <stdlib.h>
   9#include <unistd.h>
  10#include <sys/time.h>
  11
  12#include <sys/mman.h>
  13#include <fcntl.h>
  14#include <parlib/parlib.h>
  15#include <parlib/timing.h>
  16#include <parlib/ros_debug.h>
  17
  18#include <sys/types.h>
  19#include <sys/stat.h>
  20#include <unistd.h>
  21
  22#include <vmm/vmm.h>
  23#include <vmm/vthread.h>
  24
  25static struct virtual_machine vm = {.mtx = UTH_MUTEX_INIT};
  26
  27int safe_to_exit;
  28void *addr;
  29size_t nr_pgs = 1;
  30#define STRIDE 1
  31#define NUM_ITERS 100
  32
  33static void mmap_testz(void)
  34{
  35        assert(addr);
  36        for (char *i = addr; (void*)i < addr + nr_pgs * PGSIZE; i += STRIDE)
  37                *i = 'z';
  38}
  39
  40static void mmap_testy(void)
  41{
  42        assert(addr);
  43        for (char *i = addr; (void*)i < addr + nr_pgs * PGSIZE; i += STRIDE)
  44                *i = 'y';
  45}
  46
  47void *worker_thread(void *arg)
  48{
  49        int i;
  50
  51        for (i = 0; i < NUM_ITERS; ++i)
  52                mmap_testy();
  53
  54        safe_to_exit = true;
  55        __asm__ __volatile__("hlt\n\t");
  56        return 0;
  57}
  58
  59int main(void)
  60{
  61        int fd, pid, ret;
  62        char inputfile[50];
  63
  64        pid = getpid();
  65        sprintf(inputfile, "/tmp/mmap-test-%d", pid);
  66
  67        fd = open(inputfile, O_RDWR | O_CREAT, 0666);
  68        if (fd < 0) {
  69                perror("Unable to open file");
  70                exit(-1);
  71        }
  72
  73        ret = unlink(inputfile);
  74        if (ret == -1) {
  75                perror("UNLINK error");
  76                exit(-1);
  77        }
  78
  79        //Increase the file size with ftruncate
  80        ret = ftruncate(fd, nr_pgs * PGSIZE);
  81        if (ret == -1) {
  82                perror("FTRUNCATE error");
  83                exit(-1);
  84        }
  85
  86        nr_pgs = 1;
  87        void *loc = (void*) 0x1000000;
  88
  89        addr = mmap(loc, nr_pgs * PGSIZE, PROT_WRITE | PROT_READ | PROT_EXEC,
  90                    MAP_SHARED, fd, 0);
  91        if (addr == MAP_FAILED) {
  92                perror("mmap failed");
  93                exit(-1);
  94        }
  95
  96        printf("MMap got addr %p\n", addr);
  97        printf("Spawning worker vmthread thread, etc...\n");
  98        vthread_create(&vm, (void*)&worker_thread, NULL);
  99
 100        while (!safe_to_exit)
 101                cpu_relax();
 102
 103        for (char *i = addr; (void*)i < addr + nr_pgs * PGSIZE; i += STRIDE)
 104                assert(*i == 'y');
 105
 106        printf("mmap_file_vmm: test finished, doing teardown\n");
 107
 108        ret = munmap(addr, nr_pgs * PGSIZE);
 109        if (ret == -1) {
 110                perror("mmap_file_vmm: problem unmapping memory after test\n");
 111                exit(-1);
 112        }
 113
 114        ret = close(fd);
 115        if (ret == -1) {
 116                perror("mmap_file_vmm: problem closing file after test\n");
 117                exit(-1);
 118        }
 119
 120        printf("mmap_file_vmm: PASSED\n");
 121        return 0;
 122}
 123