| 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 masks on 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 | |
|---|
| 46 | /**************************************************************************************** |
|---|
| 47 | * These macros define the masks for the Generic Page Table Entry attributes. |
|---|
| 48 | ***************************************************************************************/ |
|---|
| 49 | |
|---|
| 50 | #define GPT_MAPPED 0x0001 /*! PTE is mapped */ |
|---|
| 51 | #define GPT_SMALL 0x0002 /*! PTE is a small page */ |
|---|
| 52 | #define GPT_READABLE 0x0004 /*! PTE is readable */ |
|---|
| 53 | #define GPT_WRITABLE 0x0008 /*! PTE is writable */ |
|---|
| 54 | #define GPT_EXECUTABLE 0x0010 /*! PTE is executable */ |
|---|
| 55 | #define GPT_CACHABLE 0x0020 /*! PTE can be cached */ |
|---|
| 56 | #define GPT_USER 0x0040 /*! PTE is user accessible */ |
|---|
| 57 | #define GPT_DIRTY 0x0080 /*! PTE has been "recently" written */ |
|---|
| 58 | #define GPT_ACCESSED 0x0100 /*! PTE has been "recently" accessed */ |
|---|
| 59 | #define GPT_GLOBAL 0x0200 /*! PTE is kept in TLB at context switch */ |
|---|
| 60 | #define GPT_COW 0x0400 /*! PTE must be copied on write */ |
|---|
| 61 | #define GPT_SWAP 0x0800 /*! PTE swapped on disk (not implemented yet) */ |
|---|
| 62 | #define GPT_LOCKED 0x1000 /*! PTE is protected against concurrent access */ |
|---|
| 63 | |
|---|
| 64 | /**************************************************************************************** |
|---|
| 65 | * This structure defines the Generic Page Table descriptor. |
|---|
| 66 | ***************************************************************************************/ |
|---|
| 67 | |
|---|
| 68 | typedef struct gpt_s |
|---|
| 69 | { |
|---|
| 70 | void * ptr; /*! local pointer on GPT root */ |
|---|
| 71 | ppn_t ppn; /*! PPN of GPT root */ |
|---|
| 72 | struct page_s * page; /*! local pointer on GPT root page descriptor */ |
|---|
| 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 | * @ gpt : pointer on generic page table descriptor. |
|---|
| 101 | * @ pid : process identifier. |
|---|
| 102 | ***************************************************************************************/ |
|---|
| 103 | void hal_gpt_print( gpt_t * gpt, |
|---|
| 104 | pid_t pid ); |
|---|
| 105 | |
|---|
| 106 | /**************************************************************************************** |
|---|
| 107 | * This blocking function gets a lock on a PTE (Page Table Entry) identified |
|---|
| 108 | * by its VPN, and returns only when the PTE has been successfully locked. |
|---|
| 109 | * If the target PTE is not present, it allocates and maps a physical page. |
|---|
| 110 | * A big page cannot be locked. |
|---|
| 111 | **************************************************************************************** |
|---|
| 112 | * @ gpt : pointer on the generic page table |
|---|
| 113 | * @ vpn : virtual page number of the target PTE. |
|---|
| 114 | * @ returns 0 if success / return ENOMEM or EINVAL if error. |
|---|
| 115 | ***************************************************************************************/ |
|---|
| 116 | error_t hal_gpt_lock_pte( gpt_t * gpt, |
|---|
| 117 | vpn_t vpn ); |
|---|
| 118 | |
|---|
| 119 | /**************************************************************************************** |
|---|
| 120 | * This function releases the lock on a PTE identified by its VPN. |
|---|
| 121 | **************************************************************************************** |
|---|
| 122 | * @ gpt : pointer on the generic page table |
|---|
| 123 | * @ vpn : virtual page number of the target PTE. |
|---|
| 124 | * @ returns 0 if success / returns EINVAL if error. |
|---|
| 125 | ***************************************************************************************/ |
|---|
| 126 | error_t hal_gpt_unlock_pte( gpt_t * gpt, |
|---|
| 127 | vpn_t vpn ); |
|---|
| 128 | |
|---|
| 129 | /**************************************************************************************** |
|---|
| 130 | * This function maps a page table entry identified by its VPN, from values defined |
|---|
| 131 | * by the ppn and attr arguments. It allocates physical memory for the local generic |
|---|
| 132 | * page table itself if required. |
|---|
| 133 | **************************************************************************************** |
|---|
| 134 | * @ gpt : [in] pointer on the page table |
|---|
| 135 | * @ vpn : [in] virtual page number |
|---|
| 136 | * @ ppn : [in] physical page number |
|---|
| 137 | * @ attr : [in] generic attributes |
|---|
| 138 | * @ returns 0 if success / returns ENOMEM if error |
|---|
| 139 | ***************************************************************************************/ |
|---|
| 140 | error_t hal_gpt_set_pte( gpt_t * gpt, |
|---|
| 141 | vpn_t vpn, |
|---|
| 142 | ppn_t ppn, |
|---|
| 143 | uint32_t attr ); |
|---|
| 144 | |
|---|
| 145 | /**************************************************************************************** |
|---|
| 146 | * This function unmaps a page table entry identified by the <vpn> argument in the |
|---|
| 147 | * local page table identified by the <gpt> argument. |
|---|
| 148 | * It does NOT release the physical memory allocated for the unmapped page. |
|---|
| 149 | **************************************************************************************** |
|---|
| 150 | * @ gpt : [in] pointer on the local page table |
|---|
| 151 | * @ vpn : [in] virtual page number |
|---|
| 152 | ***************************************************************************************/ |
|---|
| 153 | void hal_gpt_reset_pte( gpt_t * gpt, |
|---|
| 154 | vpn_t vpn ); |
|---|
| 155 | |
|---|
| 156 | /**************************************************************************************** |
|---|
| 157 | * This function returns in the ppn and attr arguments the value of a page table |
|---|
| 158 | * entry identified by its VPN. It returns attr == 0 if the page is not mapped. |
|---|
| 159 | **************************************************************************************** |
|---|
| 160 | * @ gpt : [in] pointer on the page table |
|---|
| 161 | * @ vpn : [in] virtual page number |
|---|
| 162 | * @ attr : [out] generic attributes |
|---|
| 163 | * @ ppn : [out] physical page number |
|---|
| 164 | ***************************************************************************************/ |
|---|
| 165 | void hal_gpt_get_pte( gpt_t * gpt, |
|---|
| 166 | vpn_t vpn, |
|---|
| 167 | uint32_t * attr, |
|---|
| 168 | ppn_t * ppn ); |
|---|
| 169 | |
|---|
| 170 | /**************************************************************************************** |
|---|
| 171 | * This function copies all valid entries from the source <src_gpt> to the <dst_gpt>. |
|---|
| 172 | * The <src_gpt> and the <dst_gpt> point on the same physical pages. |
|---|
| 173 | * If the <cow> argument is true, the GPT_WRITABLE attribute is reset for all writable |
|---|
| 174 | * entries in both <src_gpt> and <dst_gpt>, and the PG_COW flag is registered in all |
|---|
| 175 | * writable physical page descriptors, to support the Copy-On-Write mechanism. |
|---|
| 176 | **************************************************************************************** |
|---|
| 177 | * @ dst_gpt : [in] pointer on the destination GPT. |
|---|
| 178 | * @ src_gpt : [in] pointer on the source GPT. |
|---|
| 179 | * @ cow : [in] activate the COPY-On-Write mechanism if true. |
|---|
| 180 | ***************************************************************************************/ |
|---|
| 181 | error_t hal_gpt_copy( gpt_t * dst_gpt, |
|---|
| 182 | gpt_t * src_gpt, |
|---|
| 183 | bool_t cow ); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | #endif /* _GPT_H_ */ |
|---|