/*
 * hal_gpt.h - Generic Page Table API definition.
 *
 * Authors  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 _GPT_H_
#define _GPT_H_

#include <hal_types.h>

/////////////////////////////////////////////////////////////////////////////////////////
//           Generic Page Table Definition (implementation in hal_gpt.c)
//
// It is specified as a simple (one dimensional) array indexed by the VPN (vpn_t type),
// even if implementations can use a more sophisticated organisation (two-levels or more).
// - The number of entries (number of pages in a virtual space) is architecture
//   dependent, and is defined as (CONFIG_USER_SPACE_SIZE / CONFIG_PPM_PAGE_SIZE).
// - Each entry contains a Physical Page Number (ppn_t type), and a set of attributes,
//   defined as masks on a 32 bits-vector.
//
// Any arch-specific implementation must implement this API.
/////////////////////////////////////////////////////////////////////////////////////////

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

struct page_s;

/****************************************************************************************
 * These macros define the masks for the Generic Page Table Entry attributes.
 ***************************************************************************************/

#define GPT_MAPPED      0x0001  /*! PTE is mapped                                 */
#define GPT_SMALL       0x0002  /*! PTE is a small page                           */
#define GPT_READABLE    0x0004  /*! PTE is readable                               */
#define GPT_WRITABLE    0x0008  /*! PTE is writable                               */
#define GPT_EXECUTABLE  0x0010  /*! PTE is executable                             */
#define GPT_CACHABLE    0x0020  /*! PTE can be cached                             */
#define GPT_USER        0x0040  /*! PTE is user accessible                        */
#define GPT_DIRTY       0x0080  /*! PTE has been "recently" written               */
#define GPT_ACCESSED    0x0100  /*! PTE has been "recently" accessed              */
#define GPT_GLOBAL      0x0200  /*! PTE is kept in TLB at context switch          */
#define GPT_COW         0x0400  /*! PTE must be copied on write                   */
#define GPT_SWAP        0x0800  /*! PTE swapped on disk (not implemented yet)     */
#define GPT_LOCKED      0x1000  /*! PTE is protected against concurrent access    */

/****************************************************************************************
 * This structure defines the Generic Page Table descriptor.
 ***************************************************************************************/

typedef struct gpt_s
{
	void           * ptr;    /*! local pointer on GPT root                             */
	ppn_t            ppn;    /*! PPN of GPT root                                       */
	struct page_s  * page;   /*! local pointer on GPT root page descriptor             */
}
gpt_t;


/****************************************************************************************
 * This function allocates physical memory for first level page table (PT1),
 * and initializes the page table descriptor.
 ****************************************************************************************
 * @ gpt     : pointer on generic page table descriptor.
 * @ returns 0 if success / returns ENOMEM if error.
 ***************************************************************************************/
error_t hal_gpt_create( gpt_t * gpt );

/****************************************************************************************
 * This function releases all memory dynamically allocated for a generic page table.
 * For a multi-levels radix tree implementation, it includes all nodes in the tree.
 * If the calling thread is running in the reference cluster, it checks that user PTE
 * entries are unmapped, and releases the mapped physical pages.
 * The kernel pages are not released.
 ****************************************************************************************
 * @ gpt     : pointer on generic page table descriptor.
 ***************************************************************************************/
void hal_gpt_destroy( gpt_t * gpt);

/****************************************************************************************
 * This function prints on the kernel terminal the content of a generic page table.
 ****************************************************************************************
 * @ gpt     : pointer on generic page table descriptor.
 ***************************************************************************************/
void hal_gpt_print( gpt_t * gpt);

/****************************************************************************************
 * This blocking function gets a lock on a PTE (Page Table Entry) identified
 * by its VPN, and returns only when the PTE has been successfully locked.
 * If the target PTE is not present, it allocates and maps a physical page.
 * A big page cannot be locked.
 ****************************************************************************************
 * @ gpt     : pointer on the generic page table
 * @ vpn     : virtual page number of the target PTE.
 * @ returns 0 if success / return ENOMEM or EINVAL if error.
 ***************************************************************************************/
error_t hal_gpt_lock_pte( gpt_t * gpt,
                          vpn_t   vpn );

/****************************************************************************************
 * This function releases the lock on a PTE identified by its VPN.
 ****************************************************************************************
 * @ gpt     : pointer on the generic page table
 * @ vpn     : virtual page number of the target PTE.
 * @ returns 0 if success / returns EINVAL if error.
 ***************************************************************************************/
error_t hal_gpt_unlock_pte( gpt_t * gpt,
                            vpn_t   vpn );

/****************************************************************************************
 * This function maps a page table entry identified by its VPN, from values defined
 * by the ppn and attr arguments. It allocates physical memory for the local generic
 * page table itself if required.
 ****************************************************************************************
 * @ gpt       : [in] pointer on the page table
 * @ vpn       : [in] virtual page number
 * @ ppn       : [in] physical page number
 * @ attr      : [in] generic attributes
 * @ returns 0 if success / returns ENOMEM if error
 ***************************************************************************************/
error_t hal_gpt_set_pte( gpt_t    * gpt,
                         vpn_t      vpn,
                         ppn_t      ppn,
                         uint32_t   attr );

/****************************************************************************************
 * This function unmaps a page table entry identified by its VPN.
 * If the calling thread is running in the reference cluster, it sends a broadcast RPC
 * to update all other page table copies.
 ****************************************************************************************
 * @ gpt       : [in] pointer on the page table
 * @ vpn       : [in] virtual page number
 ***************************************************************************************/
void hal_gpt_reset_pte( gpt_t * gpt,
                        vpn_t   vpn );

/****************************************************************************************
 * This function returns in the ppn and attr arguments the value of a page table
 * entry identified by its VPN.  It returns attr == 0 if the page is not mapped.
 ****************************************************************************************
 * @ gpt       : [in]  pointer on the page table
 * @ vpn       : [in]  virtual page number
 * @ attr      : [out] generic attributes
 * @ ppn       : [out] physical page number
 ***************************************************************************************/
void hal_gpt_get_pte( gpt_t    * gpt,
                      vpn_t      vpn,
                      uint32_t * attr,
                      ppn_t    * ppn );

/****************************************************************************************
 * This function copies all valid entries from the source <src_gpt> to the <dst_gpt>.
 * The <src_gpt> and the <dst_gpt> point on the same physical pages.
 * If the <cow> argument is true, the GPT_WRITABLE attribute is reset for all writable
 * entries in both <src_gpt> and <dst_gpt>, and the PG_COW flag is registered in all
 * writable physical page descriptors, to support the Copy-On-Write mechanism.
 ****************************************************************************************
 * @ dst_gpt   : [in]  pointer on the destination GPT.
 * @ src_gpt   : [in]  pointer on the source GPT.
 * @ cow       : [in]  activate the COPY-On-Write mechanism if true.
 ***************************************************************************************/
error_t hal_gpt_copy( gpt_t    * dst_gpt,
                      gpt_t    * src_gpt,
                      bool_t     cow );


#endif	/* _GPT_H_ */
