static void *file_ram_alloc(RAMBlock *block,

                            ram_addr_t memory,

                            const char *path)

{

    char *filename;

    char *sanitized_name;

    char *c;

    void *area;

    int fd;

    unsigned long hpagesize;



    hpagesize = gethugepagesize(path);

    if (!hpagesize) {

        return NULL;

    }



    if (memory < hpagesize) {

        return NULL;

    }



    if (kvm_enabled() && !kvm_has_sync_mmu()) {

        fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");

        return NULL;

    }



    /* Make name safe to use with mkstemp by replacing '/' with '_'. */

    sanitized_name = g_strdup(block->mr->name);

    for (c = sanitized_name; *c != '\0'; c++) {

        if (*c == '/')

            *c = '_';

    }



    filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,

                               sanitized_name);

    g_free(sanitized_name);



    fd = mkstemp(filename);

    if (fd < 0) {

        perror("unable to create backing store for hugepages");

        g_free(filename);

        return NULL;

    }

    unlink(filename);

    g_free(filename);



    memory = (memory+hpagesize-1) & ~(hpagesize-1);



    /*

     * ftruncate is not supported by hugetlbfs in older

     * hosts, so don't bother bailing out on errors.

     * If anything goes wrong with it under other filesystems,

     * mmap will fail.

     */

    if (ftruncate(fd, memory))

        perror("ftruncate");



    area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);

    if (area == MAP_FAILED) {

        perror("file_ram_alloc: can't mmap RAM pages");

        close(fd);

        return (NULL);

    }



    if (mem_prealloc) {

        int ret, i;

        struct sigaction act, oldact;

        sigset_t set, oldset;



        memset(&act, 0, sizeof(act));

        act.sa_handler = &sigbus_handler;

        act.sa_flags = 0;



        ret = sigaction(SIGBUS, &act, &oldact);

        if (ret) {

            perror("file_ram_alloc: failed to install signal handler");

            exit(1);

        }



        /* unblock SIGBUS */

        sigemptyset(&set);

        sigaddset(&set, SIGBUS);

        pthread_sigmask(SIG_UNBLOCK, &set, &oldset);



        if (sigsetjmp(sigjump, 1)) {

            fprintf(stderr, "file_ram_alloc: failed to preallocate pages\n");

            exit(1);

        }



        /* MAP_POPULATE silently ignores failures */

        for (i = 0; i < (memory/hpagesize)-1; i++) {

            memset(area + (hpagesize*i), 0, 1);

        }



        ret = sigaction(SIGBUS, &oldact, NULL);

        if (ret) {

            perror("file_ram_alloc: failed to reinstall signal handler");

            exit(1);

        }



        pthread_sigmask(SIG_SETMASK, &oldset, NULL);

    }



    block->fd = fd;

    return area;

}

static void *file_ram_alloc(RAMBlock *block,

                            ram_addr_t memory,

                            const char *path)

{

    char *filename;

    char *sanitized_name;

    char *c;

    void *area;

    int fd;

    unsigned long hpagesize;



    hpagesize = gethugepagesize(path);

    if (!hpagesize) {

        return NULL;

    }



    if (memory < hpagesize) {

        return NULL;

    }



    if (kvm_enabled() && !kvm_has_sync_mmu()) {

        fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");

        return NULL;

    }



    

    sanitized_name = g_strdup(block->mr->name);

    for (c = sanitized_name; *c != '\0'; c++) {

        if (*c == '/')

            *c = '_';

    }



    filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,

                               sanitized_name);

    g_free(sanitized_name);



    fd = mkstemp(filename);

    if (fd < 0) {

        perror("unable to create backing store for hugepages");

        g_free(filename);

        return NULL;

    }

    unlink(filename);

    g_free(filename);



    memory = (memory+hpagesize-1) & ~(hpagesize-1);



    











    if (ftruncate(fd, memory))

        perror("ftruncate");



    area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);

    if (area == MAP_FAILED) {

        perror("file_ram_alloc: can't mmap RAM pages");

        close(fd);

        return (NULL);

    }



    if (mem_prealloc) {

        int ret, i;

        struct sigaction act, oldact;

        sigset_t set, oldset;



        memset(&act, 0, sizeof(act));

        act.sa_handler = &sigbus_handler;

        act.sa_flags = 0;



        ret = sigaction(SIGBUS, &act, &oldact);

        if (ret) {

            perror("file_ram_alloc: failed to install signal handler");

            exit(1);

        }



        

        sigemptyset(&set);

        sigaddset(&set, SIGBUS);

        pthread_sigmask(SIG_UNBLOCK, &set, &oldset);



        if (sigsetjmp(sigjump, 1)) {

            fprintf(stderr, "file_ram_alloc: failed to preallocate pages\n");

            exit(1);

        }



        

        for (i = 0; i < (memory/hpagesize)-1; i++) {

            memset(area + (hpagesize*i), 0, 1);

        }



        ret = sigaction(SIGBUS, &oldact, NULL);

        if (ret) {

            perror("file_ram_alloc: failed to reinstall signal handler");

            exit(1);

        }



        pthread_sigmask(SIG_SETMASK, &oldset, NULL);

    }



    block->fd = fd;

    return area;

}

        for (i = 0; i < (memory/hpagesize)-1; i++) {
static void *FUNC_0(RAMBlock *VAR_0,

                            ram_addr_t VAR_1,

                            const char *VAR_2)

{

    char *VAR_3;

    char *VAR_4;

    char *VAR_5;

    void *VAR_6;

    int VAR_7;

    unsigned long VAR_8;



    VAR_8 = gethugepagesize(VAR_2);

    if (!VAR_8) {

        return NULL;

    }



    if (VAR_1 < VAR_8) {

        return NULL;

    }



    if (kvm_enabled() && !kvm_has_sync_mmu()) {

        fprintf(stderr, "host lacks kvm mmu notifiers, -mem-VAR_2 unsupported\n");

        return NULL;

    }



    

    VAR_4 = g_strdup(VAR_0->mr->name);

    for (VAR_5 = VAR_4; *VAR_5 != '\0'; VAR_5++) {

        if (*VAR_5 == '/')

            *VAR_5 = '_';

    }



    VAR_3 = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", VAR_2,

                               VAR_4);

    g_free(VAR_4);



    VAR_7 = mkstemp(VAR_3);

    if (VAR_7 < 0) {

        perror("unable to create backing store for hugepages");

        g_free(VAR_3);

        return NULL;

    }

    unlink(VAR_3);

    g_free(VAR_3);



    VAR_1 = (VAR_1+VAR_8-1) & ~(VAR_8-1);



    











    if (ftruncate(VAR_7, VAR_1))

        perror("ftruncate");



    VAR_6 = mmap(0, VAR_1, PROT_READ | PROT_WRITE, MAP_PRIVATE, VAR_7, 0);

    if (VAR_6 == MAP_FAILED) {

        perror("FUNC_0: can't mmap RAM pages");

        close(VAR_7);

        return (NULL);

    }



    if (mem_prealloc) {

        int VAR_9, VAR_10;

        struct sigaction VAR_11, VAR_12;

        sigset_t set, oldset;



        memset(&VAR_11, 0, sizeof(VAR_11));

        VAR_11.sa_handler = &sigbus_handler;

        VAR_11.sa_flags = 0;



        VAR_9 = sigaction(SIGBUS, &VAR_11, &VAR_12);

        if (VAR_9) {

            perror("FUNC_0: failed to install signal handler");

            exit(1);

        }



        

        sigemptyset(&set);

        sigaddset(&set, SIGBUS);

        pthread_sigmask(SIG_UNBLOCK, &set, &oldset);



        if (sigsetjmp(sigjump, 1)) {

            fprintf(stderr, "FUNC_0: failed to preallocate pages\n");

            exit(1);

        }



        

        for (VAR_10 = 0; VAR_10 < (VAR_1/VAR_8)-1; VAR_10++) {

            memset(VAR_6 + (VAR_8*VAR_10), 0, 1);

        }



        VAR_9 = sigaction(SIGBUS, &VAR_12, NULL);

        if (VAR_9) {

            perror("FUNC_0: failed to reinstall signal handler");

            exit(1);

        }



        pthread_sigmask(SIG_SETMASK, &oldset, NULL);

    }



    VAR_0->VAR_7 = VAR_7;

    return VAR_6;

}

static void *FUNC_0(RAMBlock *VAR_0,
ram_addr_t VAR_1,
const char *VAR_2)
{ char *VAR_3; char *VAR_4; char *VAR_5; void *VAR_6; int VAR_7; unsigned long VAR_8; VAR_8 = gethugepagesize(VAR_2); if (!VAR_8) { return NULL; } if (VAR_1 < VAR_8) { return NULL; } if (kvm_enabled() && !kvm_has_sync_mmu()) { fprintf(stderr, "host lacks kvm mmu notifiers, -mem-VAR_2 unsupported\n"); return NULL; } VAR_4 = g_strdup(VAR_0->mr->name); for (VAR_5 = VAR_4; *VAR_5 != '\0'; VAR_5++) { if (*VAR_5 == '/')
*VAR_5 = '_'; } VAR_3 = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", VAR_2,
VAR_4); g_free(VAR_4); VAR_7 = mkstemp(VAR_3); if (VAR_7 < 0) { perror("unable to create backing store for hugepages"); g_free(VAR_3); return NULL; } unlink(VAR_3); g_free(VAR_3); VAR_1 = (VAR_1+VAR_8-1) & ~(VAR_8-1); if (ftruncate(VAR_7, VAR_1))
perror("ftruncate"); VAR_6 = mmap(0, VAR_1, PROT_READ | PROT_WRITE, MAP_PRIVATE, VAR_7, 0); if (VAR_6 == MAP_FAILED) { perror("FUNC_0: can't mmap RAM pages"); close(VAR_7); return (NULL); } if (mem_prealloc) { int VAR_9, VAR_10; struct sigaction VAR_11, VAR_12; sigset_t set, oldset; memset(&VAR_11, 0, sizeof(VAR_11)); VAR_11.sa_handler = &sigbus_handler; VAR_11.sa_flags = 0; VAR_9 = sigaction(SIGBUS, &VAR_11, &VAR_12); if (VAR_9) { perror("FUNC_0: failed to install signal handler"); exit(1); } sigemptyset(&set); sigaddset(&set, SIGBUS); pthread_sigmask(SIG_UNBLOCK, &set, &oldset); if (sigsetjmp(sigjump, 1)) { fprintf(stderr, "FUNC_0: failed to preallocate pages\n"); exit(1); } for (VAR_10 = 0; VAR_10 < (VAR_1/VAR_8)-1; VAR_10++) { memset(VAR_6 + (VAR_8*VAR_10), 0, 1); } VAR_9 = sigaction(SIGBUS, &VAR_12, NULL); if (VAR_9) { perror("FUNC_0: failed to reinstall signal handler"); exit(1); } pthread_sigmask(SIG_SETMASK, &oldset, NULL); } VAR_0->VAR_7 = VAR_7; return VAR_6; }