/*
 * memory.h
 *
 * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __MONOLITIHUM_MEMORY_H__
#define __MONOLITIHUM_MEMORY_H__

#include "defs.h"

#define MEMORY_BLOCK_ACCESSIBLE    (1 << 0)
#define MEMORY_BLOCK_WRITABLE      (1 << 1)
#define MEMORY_BLOCK_EXECUTABLE    (1 << 2)
#define MEMORY_BLOCK_USERMODE      (1 << 3)
#define MEMORY_BLOCK_EVICTABLE     (1 << 29)
#define MEMORY_BLOCK_COPY_ON_WRITE (1 << 30)
#define MEMORY_BLOCK_FREE          (1 << 31)

#define MEMORY_SECTION_WRITABLE     (1 << 0)
#define MEMORY_SECTION_DIRECT_WRITE (1 << 1)

typedef struct
{
    uintptr_t used_virtual;
    uintptr_t committed;
    uintptr_t evicted;
    uintptr_t shared;
} memory_stats_t;

typedef struct
{
    qword_t address;
    qword_t size;
    dword_t flags;
} memory_block_info_t;

sysret_t syscall_alloc_memory(handle_t process, void **address, size_t size, dword_t block_flags);
sysret_t syscall_free_memory(handle_t process, void *address);
sysret_t syscall_commit_memory(handle_t process, void *address, dword_t size);
sysret_t syscall_uncommit_memory(handle_t process, void *address, dword_t size);
sysret_t syscall_set_memory_flags(handle_t process, void *address, dword_t flags);
sysret_t syscall_query_memory(handle_t process, void *address, memory_block_info_t *info);
sysret_t syscall_read_memory(handle_t process, void *address, void *buffer, dword_t size);
sysret_t syscall_write_memory(handle_t process, void *address, void *buffer, dword_t size);
sysret_t syscall_create_memory_section(const char *name, handle_t file, size_t size, dword_t flags, handle_t *handle);
sysret_t syscall_open_memory_section(const char *name, handle_t *handle);
sysret_t syscall_map_memory_section(handle_t process, handle_t section, void **address, qword_t offset, size_t size, dword_t flags);
sysret_t syscall_flush_memory_section(handle_t process, void *address);

#endif
