| 1 | /* | 
|---|
| 2 | * hal_gpt.h - Generic Page Table API definition. | 
|---|
| 3 | * | 
|---|
| 4 | * Authors  Alain Greiner (2016) | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 | * | 
|---|
| 8 | * This file is part of ALMOS-MKH. | 
|---|
| 9 | * | 
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 11 | * under the terms of the GNU General Public License as published by | 
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 | * | 
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 | * General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | * You should have received a copy of the GNU General Public License | 
|---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #ifndef _GPT_H_ | 
|---|
| 25 | #define _GPT_H_ | 
|---|
| 26 |  | 
|---|
| 27 | #include <hal_types.h> | 
|---|
| 28 |  | 
|---|
| 29 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 30 | //           Generic Page Table Definition (implementation in hal_gpt.c) | 
|---|
| 31 | // | 
|---|
| 32 | // It is specified as a simple (one dimensional) array indexed by the VPN (vpn_t type), | 
|---|
| 33 | // even if implementations can use a more sophisticated organisation (two-levels or more). | 
|---|
| 34 | // - The number of entries (number of pages in a virtual space) is architecture | 
|---|
| 35 | //   dependent, and is defined as (CONFIG_USER_SPACE_SIZE / CONFIG_PPM_PAGE_SIZE). | 
|---|
| 36 | // - Each entry contains a Physical Page Number (ppn_t type), and a set of attributes, | 
|---|
| 37 | //   defined as a 32 bits-vector. | 
|---|
| 38 | // | 
|---|
| 39 | // Any arch-specific implementation must implement this API. | 
|---|
| 40 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 41 |  | 
|---|
| 42 | /****  Forward declarations  ****/ | 
|---|
| 43 |  | 
|---|
| 44 | struct page_s; | 
|---|
| 45 | struct process_s; | 
|---|
| 46 |  | 
|---|
| 47 | /**************************************************************************************** | 
|---|
| 48 | * These macros define the masks for the Generic Page Table Entry attributes. | 
|---|
| 49 | ***************************************************************************************/ | 
|---|
| 50 |  | 
|---|
| 51 | #define GPT_MAPPED      0x0001       /*! PTE is mapped                                 */ | 
|---|
| 52 | #define GPT_SMALL       0x0002       /*! PTE is a small page                           */ | 
|---|
| 53 | #define GPT_READABLE    0x0004       /*! PTE is readable                               */ | 
|---|
| 54 | #define GPT_WRITABLE    0x0008       /*! PTE is writable                               */ | 
|---|
| 55 | #define GPT_EXECUTABLE  0x0010       /*! PTE is executable                             */ | 
|---|
| 56 | #define GPT_CACHABLE    0x0020       /*! PTE can be cached                             */ | 
|---|
| 57 | #define GPT_USER        0x0040       /*! PTE is user accessible                        */ | 
|---|
| 58 | #define GPT_DIRTY       0x0080       /*! PTE has been "recently" written               */ | 
|---|
| 59 | #define GPT_ACCESSED    0x0100       /*! PTE has been "recently" accessed              */ | 
|---|
| 60 | #define GPT_GLOBAL      0x0200       /*! PTE is kept in TLB at context switch          */ | 
|---|
| 61 | #define GPT_COW         0x0400       /*! PTE must be copied on write                   */ | 
|---|
| 62 | #define GPT_SWAP        0x0800       /*! PTE swapped on disk (not implemented yet)     */ | 
|---|
| 63 | #define GPT_LOCKED      0x1000       /*! PTE is protected against concurrent access    */ | 
|---|
| 64 |  | 
|---|
| 65 | /**************************************************************************************** | 
|---|
| 66 | * This structure defines the Generic Page Table descriptor. | 
|---|
| 67 | ***************************************************************************************/ | 
|---|
| 68 |  | 
|---|
| 69 | typedef struct gpt_s | 
|---|
| 70 | { | 
|---|
| 71 | void           * ptr;    /*! local pointer on GPT root                             */ | 
|---|
| 72 | ppn_t            ppn;    /*! PPN of GPT root                                       */ | 
|---|
| 73 | } | 
|---|
| 74 | gpt_t; | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /**************************************************************************************** | 
|---|
| 78 | * This function allocates physical memory for first level page table (PT1), | 
|---|
| 79 | * and initializes the page table descriptor. | 
|---|
| 80 | **************************************************************************************** | 
|---|
| 81 | * @ gpt     : pointer on generic page table descriptor. | 
|---|
| 82 | * @ returns 0 if success / returns ENOMEM if error. | 
|---|
| 83 | ***************************************************************************************/ | 
|---|
| 84 | error_t hal_gpt_create( gpt_t * gpt ); | 
|---|
| 85 |  | 
|---|
| 86 | /**************************************************************************************** | 
|---|
| 87 | * This function releases all memory dynamically allocated for a generic page table. | 
|---|
| 88 | * For a multi-levels radix tree implementation, it includes all nodes in the tree. | 
|---|
| 89 | * If the calling thread is running in the reference cluster, it checks that user PTE | 
|---|
| 90 | * entries are unmapped, and releases the mapped physical pages. | 
|---|
| 91 | * The kernel pages are not released. | 
|---|
| 92 | **************************************************************************************** | 
|---|
| 93 | * @ gpt     : pointer on generic page table descriptor. | 
|---|
| 94 | ***************************************************************************************/ | 
|---|
| 95 | void hal_gpt_destroy( gpt_t * gpt); | 
|---|
| 96 |  | 
|---|
| 97 | /**************************************************************************************** | 
|---|
| 98 | * This function prints on the kernel terminal the content of a generic page table. | 
|---|
| 99 | **************************************************************************************** | 
|---|
| 100 | * @ process : pointer on local process descriptor. | 
|---|
| 101 | ***************************************************************************************/ | 
|---|
| 102 | void hal_gpt_display( struct process_s * process ); | 
|---|
| 103 |  | 
|---|
| 104 | /**************************************************************************************** | 
|---|
| 105 | * This blocking function gets a lock on a PTE (Page Table Entry) identified | 
|---|
| 106 | * by its VPN, and returns only when the PTE has been successfully locked. | 
|---|
| 107 | * If the target PTE is not present, it allocates and maps a physical page. | 
|---|
| 108 | * A big page cannot be locked. | 
|---|
| 109 | **************************************************************************************** | 
|---|
| 110 | * @ gpt     : pointer on the generic page table | 
|---|
| 111 | * @ vpn     : virtual page number of the target PTE. | 
|---|
| 112 | * @ returns 0 if success / return ENOMEM or EINVAL if error. | 
|---|
| 113 | ***************************************************************************************/ | 
|---|
| 114 | error_t hal_gpt_lock_pte( gpt_t * gpt, | 
|---|
| 115 | vpn_t   vpn ); | 
|---|
| 116 |  | 
|---|
| 117 | /**************************************************************************************** | 
|---|
| 118 | * This function releases the lock on a PTE identified by its VPN. | 
|---|
| 119 | **************************************************************************************** | 
|---|
| 120 | * @ gpt     : pointer on the generic page table | 
|---|
| 121 | * @ vpn     : virtual page number of the target PTE. | 
|---|
| 122 | * @ returns 0 if success / returns EINVAL if error. | 
|---|
| 123 | ***************************************************************************************/ | 
|---|
| 124 | error_t hal_gpt_unlock_pte( gpt_t * gpt, | 
|---|
| 125 | vpn_t   vpn ); | 
|---|
| 126 |  | 
|---|
| 127 | /**************************************************************************************** | 
|---|
| 128 | * This function map a local GPT entry identified by its VPN, from values defined | 
|---|
| 129 | * by the ppn and attr arguments. It allocates physical memory for the local generic | 
|---|
| 130 | * page table itself if required. | 
|---|
| 131 | **************************************************************************************** | 
|---|
| 132 | * @ gpt       : [in] pointer on the page table | 
|---|
| 133 | * @ vpn       : [in] virtual page number | 
|---|
| 134 | * @ attr      : [in] generic attributes | 
|---|
| 135 | * @ ppn       : [in] physical page number | 
|---|
| 136 | * @ returns 0 if success / returns ENOMEM if error | 
|---|
| 137 | ***************************************************************************************/ | 
|---|
| 138 | error_t hal_gpt_set_pte( gpt_t    * gpt, | 
|---|
| 139 | vpn_t      vpn, | 
|---|
| 140 | uint32_t   attr, | 
|---|
| 141 | ppn_t      ppn ); | 
|---|
| 142 |  | 
|---|
| 143 | /**************************************************************************************** | 
|---|
| 144 | * This function unmaps a page table entry identified by the <vpn> argument in the | 
|---|
| 145 | * local page table identified by the <gpt> argument. | 
|---|
| 146 | * It does NOT release the physical memory allocated for the unmapped page. | 
|---|
| 147 | **************************************************************************************** | 
|---|
| 148 | * @ gpt       : [in] pointer on the local page table | 
|---|
| 149 | * @ vpn       : [in] virtual page number | 
|---|
| 150 | ***************************************************************************************/ | 
|---|
| 151 | void hal_gpt_reset_pte( gpt_t * gpt, | 
|---|
| 152 | vpn_t   vpn ); | 
|---|
| 153 |  | 
|---|
| 154 | /**************************************************************************************** | 
|---|
| 155 | * This function returns in the <attr> and <ppn> arguments the current values | 
|---|
| 156 | * stored in a GPT entry, identified by the <gpt> and <vpn> arguments. | 
|---|
| 157 | **************************************************************************************** | 
|---|
| 158 | * @ gpt_xp    : [in]  pointer on the page table | 
|---|
| 159 | * @ vpn       : [in]  virtual page number | 
|---|
| 160 | * @ attr      : [out] generic attributes | 
|---|
| 161 | * @ ppn       : [out] physical page number | 
|---|
| 162 | ***************************************************************************************/ | 
|---|
| 163 | void hal_gpt_get_pte( gpt_t    * gpt, | 
|---|
| 164 | vpn_t      vpn, | 
|---|
| 165 | uint32_t * attr, | 
|---|
| 166 | ppn_t    * ppn ); | 
|---|
| 167 |  | 
|---|
| 168 | /**************************************************************************************** | 
|---|
| 169 | * This function is used to implement the "fork" system call: It copies one GPT entry | 
|---|
| 170 | * identified by the <vpn> argument, from a remote <src_gpt_xp> to a local <dst_gpt>. | 
|---|
| 171 | * It does nothing if the source PTE is not MAPPED and SMALL. | 
|---|
| 172 | * It optionnally activates the "Copy on Write" mechanism: when the <cow> argument is | 
|---|
| 173 | * true: the GPT_WRITABLE flag is reset, and the GPT_COW flag is set. | 
|---|
| 174 | * A new second level PT2(s) is allocated for destination GPT if required. | 
|---|
| 175 | * It returns in the <ppn> and <mapped> arguments the PPN value for the copied PTE, | 
|---|
| 176 | * and a boolean indicating if the PTE is mapped and small, and was actually copied. | 
|---|
| 177 | **************************************************************************************** | 
|---|
| 178 | * @ dst_gpt      : [in]  local pointer on the local destination GPT. | 
|---|
| 179 | * @ src_gpt_xp   : [in]  extended pointer on the remote source GPT. | 
|---|
| 180 | * @ vpn_base     : [in]  vpn defining the PTE to be copied. | 
|---|
| 181 | * @ cow          : [in]  activate the COPY-On-Write mechanism if true. | 
|---|
| 182 | * @ ppn          : [out] PPN value (only if mapped is true). | 
|---|
| 183 | * @ mapped       : [out] true if src_gpt[vpn] actually copied to dst_gpt[vpn]. | 
|---|
| 184 | * @ return 0 if success / return -1 if no memory for a new PT2. | 
|---|
| 185 | ***************************************************************************************/ | 
|---|
| 186 | error_t hal_gpt_pte_copy( gpt_t    * dst_gpt, | 
|---|
| 187 | xptr_t     src_gpt_xp, | 
|---|
| 188 | vpn_t      vpn, | 
|---|
| 189 | bool_t     cow, | 
|---|
| 190 | ppn_t    * ppn, | 
|---|
| 191 | bool_t   * mapped ); | 
|---|
| 192 |  | 
|---|
| 193 | /**************************************************************************************** | 
|---|
| 194 | * This function returns true if the MAPPED and SMALL flags are both set | 
|---|
| 195 | * for a PTE defined by <gpt> and <vpn> arguments. | 
|---|
| 196 | **************************************************************************************** | 
|---|
| 197 | * @ gpt       : [in]  pointer on the page table | 
|---|
| 198 | * @ vpn       : [in]  virtual page number | 
|---|
| 199 | * @ returns true if MAPPED is set. | 
|---|
| 200 | ***************************************************************************************/ | 
|---|
| 201 | bool_t hal_gpt_pte_is_mapped( gpt_t * gpt, | 
|---|
| 202 | vpn_t   vpn ); | 
|---|
| 203 |  | 
|---|
| 204 | /**************************************************************************************** | 
|---|
| 205 | * This function returns true if the MAPPED, SMALL, and COW flags are all set | 
|---|
| 206 | * for a PTE defined by <gpt> and <vpn> arguments. | 
|---|
| 207 | **************************************************************************************** | 
|---|
| 208 | * @ gpt       : [in]  pointer on the page table | 
|---|
| 209 | * @ vpn       : [in]  virtual page number | 
|---|
| 210 | * @ returns true if COW is set. | 
|---|
| 211 | ***************************************************************************************/ | 
|---|
| 212 | bool_t hal_gpt_pte_is_cow( gpt_t * gpt, | 
|---|
| 213 | vpn_t   vpn ); | 
|---|
| 214 |  | 
|---|
| 215 | /**************************************************************************************** | 
|---|
| 216 | * This function atomically flip the COW flag and WRITABLE flag for all PTEs | 
|---|
| 217 | * of a remote GPT identified by the <gpt_xp>, <vpn_base>, and <vpn_size arguments. | 
|---|
| 218 | * - it set COW and reset WRITABLE when <set_cow> argument is true and PTE is WRITABLE. | 
|---|
| 219 | * - it set WRITABLE and reset COW when <set_cow> is false and PTE is COW. | 
|---|
| 220 | * It does nothing if the remote PTE is not MAPPED and SMALL. | 
|---|
| 221 | * It is called when a fork is executed, or when a COW must be resolved. | 
|---|
| 222 | **************************************************************************************** | 
|---|
| 223 | * @ set_cow   : [in]  set COW & reset WRITABLE if true / do the opposite if false. | 
|---|
| 224 | * @ gpt_xp    : [in]  extended pointer on the remote GPT. | 
|---|
| 225 | * @ vpn_base  : [in]  first virtual page. | 
|---|
| 226 | * @ vpn_size  : [in]  number of pages. | 
|---|
| 227 | ***************************************************************************************/ | 
|---|
| 228 | void hal_gpt_flip_cow( bool_t  set_cow, | 
|---|
| 229 | xptr_t  gpt_xp, | 
|---|
| 230 | vpn_t   vpn_base, | 
|---|
| 231 | vpn_t   vpn_size ); | 
|---|
| 232 |  | 
|---|
| 233 | /**************************************************************************************** | 
|---|
| 234 | * This function is used to maintain coherence amongst the multiple GPT copies. | 
|---|
| 235 | * It modifies an existing entry identified by the <vpn> argument in a remote GPT | 
|---|
| 236 | * identified by the <gpt_xp> argument, using remote accesses. | 
|---|
| 237 | * It cannot fail, because only MAPPED & SMALL entries are modified. | 
|---|
| 238 | **************************************************************************************** | 
|---|
| 239 | * @ gpt_xp    : [in] extended pointer on the page table | 
|---|
| 240 | * @ vpn       : [in] virtual page number | 
|---|
| 241 | * @ attr      : [in] generic attributes | 
|---|
| 242 | * @ ppn       : [in] physical page number | 
|---|
| 243 | ***************************************************************************************/ | 
|---|
| 244 | void hal_gpt_update_pte( xptr_t     gpt_xp, | 
|---|
| 245 | vpn_t      vpn, | 
|---|
| 246 | uint32_t   attr, | 
|---|
| 247 | ppn_t      ppn ); | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 | #endif  /* _GPT_H_ */ | 
|---|
| 251 |  | 
|---|