/* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
/* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
/* Copyright (C) 2014 Stony Brook University
This file is part of Graphene Library OS.
Graphene Library OS is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Graphene Library OS 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see . */
/*
* shim_vma.h
*
* Definitions of types and functions for VMA bookkeeping.
*/
#ifndef _SHIM_VMA_H_
#define _SHIM_VMA_H_
#include
#include
#include
#include
#include
#include
#include
struct shim_handle;
#define VMA_COMMENT_LEN 16
/*
* struct shim_vma_val is the published version of struct shim_vma
* (struct shim_vma is defined in bookkeep/shim_vma.c).
*/
struct shim_vma_val {
void * addr;
uint64_t length;
int prot;
int flags;
uint64_t offset;
struct shim_handle * file;
char comment[VMA_COMMENT_LEN];
};
static inline
void free_vma_val_array (struct shim_vma_val * vmas, size_t count)
{
for (int i = 0 ; i < count ; i++) {
/* need to release the file handle */
if (vmas[i].file)
put_handle(vmas[i].file);
}
free(vmas);
}
/* an additional flag */
#define VMA_UNMAPPED 0x10000000 /* vma is kept for bookkeeping, but the
memory is not actually allocated */
#define VMA_INTERNAL 0x20000000 /* vma is used internally */
#define VMA_TAINTED 0x40000000 /* vma has been protected as writeable,
so it has to be checkpointed during
migration */
#define VMA_CP 0x80000000 /* vma is used for dumping checkpoint
data */
#define VMA_TYPE(flags) ((flags) & (VMA_INTERNAL | VMA_CP))
/*
* We distinguish checkpoint VMAs from user VMAs and other internal VMAs,
* to prevent corrupting internal data when creating processes.
*/
#define CP_VMA_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL|VMA_CP)
#define NEED_MIGRATE_MEMORY(vma) \
(((vma)->flags & VMA_TAINTED || !(vma)->file) && \
!((vma)->flags & VMA_UNMAPPED))
#define NEED_MIGRATE_MEMORY_IF_GIPC(vma) \
(!((vma)->flags & VMA_UNMAPPED) && \
!(!(vma)->prot && !((vma)->flags & VMA_TAINTED)) && \
!((vma)->file && ((vma)->flags & MAP_SHARED)))
static inline PAL_FLG PAL_PROT (int prot, int flags)
{
PAL_FLG pal_prot = 0;
if (prot & PROT_READ)
pal_prot |= PAL_PROT_READ;
if (prot & PROT_WRITE)
pal_prot |= PAL_PROT_WRITE;
if (prot & PROT_EXEC)
pal_prot |= PAL_PROT_EXEC;
if (flags & MAP_PRIVATE)
pal_prot |= PAL_PROT_WRITECOPY;
return pal_prot;
}
int init_vma (void);
/* Bookkeeping mmap() system call */
int bkeep_mmap (void * addr, uint64_t length,
int prot, int flags,
struct shim_handle * file, uint64_t offset,
const char * comment);
/* Bookkeeping munmap() system call */
int bkeep_munmap (void * addr, uint64_t length, int flags);
/* Bookkeeping mprotect() system call */
int bkeep_mprotect (void * addr, uint64_t length, int prot, int flags);
/* Looking up VMA that contains [addr, length) */
int lookup_vma (void * addr, struct shim_vma_val * vma);
/* Looking up VMA that overlaps with [addr, length) */
int lookup_overlap_vma (void * addr, uint64_t length,
struct shim_vma_val * vma);
/*
* Looking for an unmapped space and then adding the corresponding bookkeeping
* (more info in bookkeep/shim_vma.c).
*
* Note: the first argument is "top_addr" because the search is top-down.
*/
void * bkeep_unmapped (void * top_addr, void * bottom_addr, uint64_t length,
int prot, int flags, struct shim_handle * file,
uint64_t offset, const char * comment);
static inline void *
bkeep_unmapped_any (uint64_t length, int prot, int flags,
struct shim_handle * file, uint64_t offset,
const char * comment)
{
return bkeep_unmapped(PAL_CB(user_address.end),
PAL_CB(user_address.start),
length, prot, flags, file, offset, comment);
}
void * bkeep_unmapped_heap (uint64_t length, int prot, int flags,
struct shim_handle * file, uint64_t offset,
const char * comment);
/*
* Dumping all *non-internal* VMAs into a user-allocated buffer ("max_count" is
* the maximal number of entries in the buffer). Return number of filled entries
* if succeeded, or -EOVERFLOW if the buffer is too small.
*/
int dump_all_vmas (struct shim_vma_val * vmas, size_t max_count);
/* Debugging */
void debug_print_vma_list (void);
/* Constants */
extern unsigned long brk_max_size;
extern unsigned long sys_stack_size;
#endif /* _SHIM_VMA_H_ */