[1] | 1 | /* |
---|
| 2 | * hal_gpt.h - Generic Page Table API definition. |
---|
| 3 | * |
---|
[640] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[1] | 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 | |
---|
[457] | 27 | #include <hal_kernel_types.h> |
---|
[1] | 28 | |
---|
| 29 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 30 | // Generic Page Table Definition (implementation in hal_gpt.c) |
---|
| 31 | // |
---|
[17] | 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, |
---|
[408] | 37 | // defined as a 32 bits-vector. |
---|
[1] | 38 | // |
---|
[17] | 39 | // Any arch-specific implementation must implement this API. |
---|
[1] | 40 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 41 | |
---|
| 42 | /**** Forward declarations ****/ |
---|
| 43 | |
---|
| 44 | struct page_s; |
---|
[407] | 45 | struct process_s; |
---|
[1] | 46 | |
---|
| 47 | /**************************************************************************************** |
---|
[383] | 48 | * These macros define the masks for the Generic Page Table Entry attributes. |
---|
[1] | 49 | ***************************************************************************************/ |
---|
| 50 | |
---|
[407] | 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 */ |
---|
[624] | 58 | #define GPT_DIRTY 0x0080 /*! PTE has been written */ |
---|
[407] | 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) */ |
---|
[629] | 63 | #define GPT_LOCKED 0x1000 /*! PTE is currently accessed by a thread */ |
---|
[1] | 64 | |
---|
| 65 | /**************************************************************************************** |
---|
[41] | 66 | * This structure defines the Generic Page Table descriptor. |
---|
[1] | 67 | ***************************************************************************************/ |
---|
| 68 | |
---|
| 69 | typedef struct gpt_s |
---|
| 70 | { |
---|
[640] | 71 | void * ptr; /*! local pointer on GPT root */ |
---|
| 72 | uint32_t pte1_wait_events; /*! total number of pte1 wait events on this gpt */ |
---|
| 73 | uint32_t pte1_wait_iters; /*! total number of iterations in all pte1 wait */ |
---|
| 74 | uint32_t pte2_wait_events; /*! total number of pte2 wait events on this gpt */ |
---|
| 75 | uint32_t pte2_wait_iters; /*! total number of iterations in all pte2 wait */ |
---|
[1] | 76 | } |
---|
| 77 | gpt_t; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | /**************************************************************************************** |
---|
[629] | 81 | * This function allocates physical memory for a local GPT, |
---|
[623] | 82 | * and initializes the GPT descriptor, creating an empty GPT. |
---|
[1] | 83 | **************************************************************************************** |
---|
| 84 | * @ gpt : pointer on generic page table descriptor. |
---|
| 85 | * @ returns 0 if success / returns ENOMEM if error. |
---|
| 86 | ***************************************************************************************/ |
---|
| 87 | error_t hal_gpt_create( gpt_t * gpt ); |
---|
| 88 | |
---|
| 89 | /**************************************************************************************** |
---|
| 90 | * This function releases all memory dynamically allocated for a generic page table. |
---|
| 91 | * For a multi-levels radix tree implementation, it includes all nodes in the tree. |
---|
[640] | 92 | * All GPT entries are supposed to be previously unmapped. |
---|
[1] | 93 | **************************************************************************************** |
---|
| 94 | * @ gpt : pointer on generic page table descriptor. |
---|
| 95 | ***************************************************************************************/ |
---|
| 96 | void hal_gpt_destroy( gpt_t * gpt); |
---|
| 97 | |
---|
| 98 | /**************************************************************************************** |
---|
[629] | 99 | * This blocking function atomically set the GPT_LOCKED attribute in a target PTE |
---|
| 100 | * of a remote GPT identified by the <gpt_xp> and <vpn> arguments, after checking |
---|
| 101 | * (in a busy waiting loop) that this attribute has been reset. |
---|
[632] | 102 | * It returns in the <attr> and <ppn> buffers the value of the PTE before modification. |
---|
| 103 | * It atomically allocates memory to register this attribute in the PTE when |
---|
| 104 | * required by a specific GPT implementation (example : allocate a PT2 in the TSAR GPT). |
---|
[629] | 105 | * WARNING : Only small pages can be locked. |
---|
[1] | 106 | **************************************************************************************** |
---|
[629] | 107 | * @ gpt_xp : [in] extended pointer on the generic page table. |
---|
| 108 | * @ vpn : [in] virtual page number of the target PTE. |
---|
| 109 | * @ attr : [out] local buffer for GPT attributes. |
---|
| 110 | * @ ppn : [out] local buffer for physical page number. |
---|
| 111 | * @ returns 0 if success / return -1 if error (no memory or big page). |
---|
[1] | 112 | ***************************************************************************************/ |
---|
[629] | 113 | error_t hal_gpt_lock_pte( xptr_t gpt_xp, |
---|
| 114 | vpn_t vpn, |
---|
| 115 | uint32_t * attr, |
---|
| 116 | ppn_t * ppn ); |
---|
[1] | 117 | |
---|
| 118 | /**************************************************************************************** |
---|
[629] | 119 | * This function atomically reset the GPT_LOCKED attribute in a target PTE in a |
---|
| 120 | * remote GPT identified by the <gpt_xp> and <vpn> arguments. |
---|
[1] | 121 | **************************************************************************************** |
---|
[629] | 122 | * @ gpt_xp : pointer on the generic page table |
---|
[1] | 123 | * @ vpn : virtual page number of the target PTE. |
---|
| 124 | ***************************************************************************************/ |
---|
[629] | 125 | void hal_gpt_unlock_pte( xptr_t gpt_xp, |
---|
| 126 | vpn_t vpn ); |
---|
[1] | 127 | |
---|
| 128 | /**************************************************************************************** |
---|
[629] | 129 | * This low level function maps a new PTE or modifies an existing PTE in a remote GPT |
---|
| 130 | * identified by the <gpt_xp> and <vpn> arguments, as defined by <ppn> and <attr> args. |
---|
| 131 | * This function can be used for both a small page (PTE2), and a big page (PTE1). |
---|
| 132 | * |
---|
| 133 | * WARNING : For a small page, it checks that the GPT_LOCKED attribute has been |
---|
[632] | 134 | * previously set, to prevent concurrent mapping accesses. |
---|
[1] | 135 | **************************************************************************************** |
---|
[629] | 136 | * @ gpt_xp : [in] extended pointer on the page table |
---|
[1] | 137 | * @ vpn : [in] virtual page number |
---|
[629] | 138 | * @ attr : [in] GPT attributes |
---|
[17] | 139 | * @ ppn : [in] physical page number |
---|
[1] | 140 | ***************************************************************************************/ |
---|
[629] | 141 | void hal_gpt_set_pte( xptr_t gpt_xp, |
---|
| 142 | vpn_t vpn, |
---|
| 143 | uint32_t attr, |
---|
| 144 | ppn_t ppn ); |
---|
[1] | 145 | |
---|
| 146 | /**************************************************************************************** |
---|
[629] | 147 | * This low level function unmaps and unlocks a PTE from a remote GPT identified by the |
---|
| 148 | * <gpt_xp> and <vpn> arguments. It does NOT release the allocated physical memory. |
---|
| 149 | * This function can be used for both a small page (PTE2), and a big page (PTE1). |
---|
[1] | 150 | **************************************************************************************** |
---|
[629] | 151 | * @ gpt_xp : [in] extended pointer on the page table |
---|
| 152 | * @ vpn : [in] virtual page number. |
---|
[1] | 153 | ***************************************************************************************/ |
---|
[629] | 154 | void hal_gpt_reset_pte( xptr_t gpt_xp, |
---|
[1] | 155 | vpn_t vpn ); |
---|
| 156 | |
---|
| 157 | /**************************************************************************************** |
---|
[629] | 158 | * This low level function returns in the <attr> and <ppn> arguments the current values |
---|
| 159 | * of a PTE in a a remote GPT, identified by the <gpt> and <vpn> arguments. |
---|
| 160 | * This function can be used for both a small page (PTE2), and a big page (PTE1). |
---|
[1] | 161 | **************************************************************************************** |
---|
[629] | 162 | * @ gpt_xp : [in] extended pointer on the page table. |
---|
| 163 | * @ vpn : [in] virtual page number. |
---|
| 164 | * @ attr : [out] local buffer for generic attributes. |
---|
| 165 | * @ ppn : [out] local buffer for physical page number. |
---|
[1] | 166 | ***************************************************************************************/ |
---|
[587] | 167 | void hal_gpt_get_pte( xptr_t gpt_xp, |
---|
[1] | 168 | vpn_t vpn, |
---|
| 169 | uint32_t * attr, |
---|
| 170 | ppn_t * ppn ); |
---|
[41] | 171 | |
---|
[23] | 172 | /**************************************************************************************** |
---|
[625] | 173 | * This function is used to implement the "fork" system call: It copies a remote |
---|
| 174 | * source PTE, identified by the <src_gpt_xp> and <src_vpn> arguments, to a local |
---|
| 175 | * destination PTE, identified by the <dst_gpt> and <dst_vpn> arguments. |
---|
[408] | 176 | * It does nothing if the source PTE is not MAPPED and SMALL. |
---|
[407] | 177 | * It optionnally activates the "Copy on Write" mechanism: when the <cow> argument is |
---|
[408] | 178 | * true: the GPT_WRITABLE flag is reset, and the GPT_COW flag is set. |
---|
[625] | 179 | * A new second level PT2 is allocated for the destination GPT if required. |
---|
[408] | 180 | * It returns in the <ppn> and <mapped> arguments the PPN value for the copied PTE, |
---|
| 181 | * and a boolean indicating if the PTE is mapped and small, and was actually copied. |
---|
[23] | 182 | **************************************************************************************** |
---|
[625] | 183 | * @ dst_gpt : [in] local pointer on local destination GPT. |
---|
| 184 | * @ dst_vpn : [in] vpn defining the PTE in the desination GPT. |
---|
| 185 | * @ src_gpt_xp : [in] extended pointer on remote source GPT. |
---|
| 186 | * @ src_vpn : [in] vpn defining the PTE in the source GPT. |
---|
[635] | 187 | * @ cow : [in] set COW flag & reset WRITABLE flag if true. |
---|
[408] | 188 | * @ ppn : [out] PPN value (only if mapped is true). |
---|
| 189 | * @ mapped : [out] true if src_gpt[vpn] actually copied to dst_gpt[vpn]. |
---|
| 190 | * @ return 0 if success / return -1 if no memory for a new PT2. |
---|
[23] | 191 | ***************************************************************************************/ |
---|
[408] | 192 | error_t hal_gpt_pte_copy( gpt_t * dst_gpt, |
---|
[625] | 193 | vpn_t dst_vpn, |
---|
[408] | 194 | xptr_t src_gpt_xp, |
---|
[625] | 195 | vpn_t src_vpn, |
---|
[408] | 196 | bool_t cow, |
---|
| 197 | ppn_t * ppn, |
---|
| 198 | bool_t * mapped ); |
---|
[1] | 199 | |
---|
[407] | 200 | /**************************************************************************************** |
---|
[432] | 201 | * This function atomically set the COW flag and reset the WRITABLE flag for all PTEs |
---|
[408] | 202 | * of a remote GPT identified by the <gpt_xp>, <vpn_base>, and <vpn_size arguments. |
---|
[629] | 203 | * It does NOT require the GPT_LOCKED attribute to be set in the target PTE. |
---|
| 204 | * It does nothing if the PTE is not MAPPED and SMALL. |
---|
[408] | 205 | **************************************************************************************** |
---|
| 206 | * @ gpt_xp : [in] extended pointer on the remote GPT. |
---|
| 207 | * @ vpn_base : [in] first virtual page. |
---|
| 208 | * @ vpn_size : [in] number of pages. |
---|
| 209 | ***************************************************************************************/ |
---|
[432] | 210 | void hal_gpt_set_cow( xptr_t gpt_xp, |
---|
| 211 | vpn_t vpn_base, |
---|
| 212 | vpn_t vpn_size ); |
---|
[407] | 213 | |
---|
[408] | 214 | /**************************************************************************************** |
---|
| 215 | * This function is used to maintain coherence amongst the multiple GPT copies. |
---|
| 216 | * It modifies an existing entry identified by the <vpn> argument in a remote GPT |
---|
| 217 | * identified by the <gpt_xp> argument, using remote accesses. |
---|
[632] | 218 | * - The MAPPED and SMALL attributes must be set, and the LOCKED attibute must be reset |
---|
| 219 | * in the <attr> argument. |
---|
| 220 | * - The MAPPED and SMALL attributes must be set in the target PTE. |
---|
[408] | 221 | **************************************************************************************** |
---|
| 222 | * @ gpt_xp : [in] extended pointer on the page table |
---|
| 223 | * @ vpn : [in] virtual page number |
---|
| 224 | * @ attr : [in] generic attributes |
---|
| 225 | * @ ppn : [in] physical page number |
---|
| 226 | ***************************************************************************************/ |
---|
| 227 | void hal_gpt_update_pte( xptr_t gpt_xp, |
---|
| 228 | vpn_t vpn, |
---|
| 229 | uint32_t attr, |
---|
| 230 | ppn_t ppn ); |
---|
| 231 | |
---|
| 232 | |
---|
[1] | 233 | #endif /* _GPT_H_ */ |
---|
[408] | 234 | |
---|