/*
 * vseg.h - virtual segment (vseg) related operations
 *
 * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012)
 *           Mohamed Lamine Karaoui (2015)
 *           Alain Greiner (2016)
 *
 * Copyright (c) UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.0 of the License.
 *
 * ALMOS-MKH 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _VSEG_H_
#define _VSEG_H_

#include <hal_types.h>
#include <spinlock.h>
#include <vfs.h>

/****  Forward declarations  ****/

struct vmm_s;

/**********************************************************************************************
 * This enum defines the vseg types
 *********************************************************************************************/

enum
{
    VSEG_TYPE_CODE   = 0,          /*! executable code        / private / localized          */
    VSEG_TYPE_DATA   = 1,          /*! initialized data       / public  / distributed        */
    VSEG_TYPE_HEAP   = 2,          /*! standard malloc        / public  / distributed        */
    VSEG_TYPE_STACK  = 3,          /*! execution stack        / private / localized          */
    VSEG_TYPE_ANON   = 4,          /*! anonymous mmap         / public  / localized          */
    VSEG_TYPE_FILE   = 5,          /*! file mmap              / public  / localized          */
    VSEG_TYPE_REMOTE = 6,          /*! remote mmap            / public  / localized          */
    VSEG_TYPE_KCODE  = 7,          /*! kernel code            / private / localized          */
    VSEG_TYPE_KDATA  = 8,          /*! kernel data            / private / localized          */
    VSEG_TYPE_KDEV   = 9,          /*! device segment         / public  / localized          */

    VSEG_TYPES_NR    = 10,
};


/**********************************************************************************************
 * These masks define the vseg generic (hardware independent) flags.
 *********************************************************************************************/

#define VSEG_USER     0x0001       /*! user accessible                                       */
#define VSEG_WRITE    0x0002       /*! writeable                                             */
#define VSEG_EXEC     0x0004       /*! executable                                            */
#define VSEG_CACHE    0x0008       /*! cachable                                              */
#define VSEG_PRIVATE  0x0010       /*! should not be accessed from another cluster           */
#define VSEG_DISTRIB  0x0020       /*! physically distributed on all clusters                */

/**********************************************************************************************
 * This structure defines a virtual segment descriptor.
 *********************************************************************************************/

typedef struct vseg_s
{
	list_entry_t      list;         /*! all vsegs in same process / same free list if mmap   */
	struct vmm_s    * vmm;          /*! pointer on associated VM manager                     */
    uint32_t          type;         /*! vseg type                                            */
	intptr_t          min;          /*! segment min virtual address                          */
	intptr_t          max;          /*! segment max virtual address (excluded)               */
	vpn_t             vpn_base;     /*! first page of vseg                                   */
	vpn_t             vpn_size;     /*! number of pages occupied                             */
	uint32_t          flags;        /*! vseg attributes                                      */
	xptr_t            mapper;       /*! extended pointer on associated mapper                */
	fdid_t            fdid;         /*! associated fdid for a VSEG_TYPE_FILE                 */
	uint32_t          offset;       /*! offset in file for a VSEG_TYPE_FILE                  */
    cxy_t             cxy;          /*! target cluster for physical mapping                  */
}
vseg_t;

/**********************************************************************************************
 * This function returns a printable string for the vseg type. 
 **********************************************************************************************
 * @ vseg_type  : type of vseg
 * @ return pointer on string
 *********************************************************************************************/
char * vseg_type_str( uint32_t vseg_type );

/**********************************************************************************************
 * This function allocates physical memory for a new vseg descriptor from the local cluster
 * physical memory allocator.
 **********************************************************************************************
 * @ return pointer on allocated vseg descriptor if success / return NULL if failure.
 *********************************************************************************************/
vseg_t * vseg_alloc();

/**********************************************************************************************
 * This function releases physical memory allocated for a vseg descriptor to the local cluster
 * physical memory allocator.
 **********************************************************************************************
 * @ vseg   : local pointer on released vseg descriptor.
 *********************************************************************************************/
void vseg_free( vseg_t * vseg );

/**********************************************************************************************
 * This function initializes a local vseg descriptor, from the arguments values.
 * It does NOT register the vseg in the local VMM.
 **********************************************************************************************
 * @ vseg      : pointer on the vseg descriptor.
 * @ base      : vseg base address.
 * @ size      : vseg size (bytes).
 * @ vpn_base  : first page index.
 * @ vpn_size  : number of pages.
 * @ type      : vseg type.
 * @ cxy       : target cluster for physical mapping.
 * @ fdid      : file descriptor index if VSEG_TYPE_FILE.
 * @ offset    : offset in file if VSEG_TYPE_FILE.
 *********************************************************************************************/
void vseg_init( vseg_t      * vseg,
                intptr_t      base,
	            intptr_t      size,
                vpn_t         vpn_base,
                vpn_t         vpn_size,
	            uint32_t      type,
                cxy_t         cxy,
                fdid_t        fdid,
	            uint32_t      offset );

/**********************************************************************************************
 * This function initializes a local vseg descriptor from values contained in a reference
 * remote vseg descriptor. It does NOT register the vseg in the local VMM.
 **********************************************************************************************
 * @ vseg      : pointer on the vseg descriptor.
 * @ ref_xp    : extended pointer on the reference vseg descriptor.
 *********************************************************************************************/
void vseg_init_from_ref( vseg_t * vseg,
                         xptr_t   ref_xp );

/**********************************************************************************************
 * This function adds a vseg descriptor in the set of vsegs controlled by a given VMM,
 * and updates the vmm field in the vseg descriptor.
 * The lock protecting the vsegs list in VMM must be taken by the caller.
 **********************************************************************************************
 * @ vmm       : pointer on the VMM
 * @ vseg      : pointer on the vseg descriptor
 * @ returns 0 if success / returns ENOMEM if failure.
 *********************************************************************************************/
error_t vseg_attach( struct vmm_s  * vmm,
                     vseg_t        * vseg );

/**********************************************************************************************
 * This function removes a vseg descriptor from the set of vsegs controlled by a given VMM,
 * and updates the vmm field in the vseg descriptor. No memory is released.
 * The lock protecting the vsegs list in VMM must be taken by the caller.
 **********************************************************************************************
 * @ vmm       : pointer on the VMM
 * @ vseg      : pointer on the vseg descriptor
 *********************************************************************************************/
void vseg_detach( struct vmm_s  * vmm,
                  vseg_t        * vseg );


#endif /* _VSEG_H_ */
