[1] | 1 | /* |
---|
[567] | 2 | * ppm.h - Per-cluster Physical Pages Manager definition. |
---|
[18] | 3 | * |
---|
[1] | 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
[567] | 5 | * Alain Greiner (2016,2017,2018) |
---|
[1] | 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef _PPM_H_ |
---|
| 26 | #define _PPM_H_ |
---|
| 27 | |
---|
[457] | 28 | #include <hal_kernel_types.h> |
---|
[1] | 29 | #include <list.h> |
---|
[567] | 30 | #include <busylock.h> |
---|
[606] | 31 | #include <remote_queuelock.h> |
---|
[1] | 32 | #include <boot_info.h> |
---|
| 33 | #include <page.h> |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /***************************************************************************************** |
---|
[409] | 37 | * This structure defines the Physical Pages Manager in a cluster. |
---|
| 38 | * In each cluster, the physical memory bank starts at local physical address 0 and |
---|
[567] | 39 | * contains an integer number of pages, defined by the <pages_nr> field in the |
---|
[50] | 40 | * boot_info structure. It is split in three parts: |
---|
[606] | 41 | * |
---|
[50] | 42 | * - the "kernel_code" section contains the kernel code, loaded by the boot-loader. |
---|
| 43 | * It starts at PPN = 0 and the size is defined by the <pages_offset> field in the |
---|
| 44 | * boot_info structure. |
---|
| 45 | * - the "pages_tbl" section contains the physical page descriptors array. It starts |
---|
| 46 | * at PPN = pages_offset, and it contains one entry per small physical page in cluster. |
---|
[567] | 47 | * It is created and initialized by the hal_ppm_create() function. |
---|
[50] | 48 | * - The "kernel_heap" section contains all physical pages that are are not in the |
---|
[567] | 49 | * kernel_code and pages_tbl sections, and that have not been reserved by the |
---|
[50] | 50 | * architecture specific bootloader. The reserved pages are defined in the boot_info |
---|
| 51 | * structure. |
---|
[160] | 52 | * |
---|
[50] | 53 | * The main service provided by the PMM is the dynamic allocation of physical pages |
---|
[567] | 54 | * from the "kernel_heap" section. This low-level allocator implements the buddy |
---|
| 55 | * algorithm: an allocated block is an integer number n of small pages, where n |
---|
| 56 | * is a power of 2, and ln(n) is called order. |
---|
| 57 | * This allocator being shared by the local threads, the free_page lists rooted |
---|
| 58 | * in the PPM descriptor are protected by a local busylock, because it is used |
---|
| 59 | * by the idle_thread during kernel_init(). |
---|
| 60 | * |
---|
| 61 | * Another service is to register the dirty pages in a specific dirty_list, that is |
---|
| 62 | * also rooted in the PPM, in order to be able to save all dirty pages on disk. |
---|
[606] | 63 | * This dirty list is protected by a specific remote_queuelock, because it can be |
---|
| 64 | * modified by a remote thread, but it is implemented as a local list, because it |
---|
| 65 | * contains only local pages. |
---|
[1] | 66 | ****************************************************************************************/ |
---|
[50] | 67 | |
---|
[1] | 68 | typedef struct ppm_s |
---|
| 69 | { |
---|
[606] | 70 | busylock_t free_lock; /*! lock protecting free_pages[] lists */ |
---|
| 71 | list_entry_t free_pages_root[CONFIG_PPM_MAX_ORDER]; /*! roots of free lists */ |
---|
| 72 | uint32_t free_pages_nr[CONFIG_PPM_MAX_ORDER]; /*! free pages number */ |
---|
| 73 | page_t * pages_tbl; /*! pointer on page descriptors array */ |
---|
| 74 | uint32_t pages_nr; /*! total number of small physical page */ |
---|
| 75 | remote_queuelock_t dirty_lock; /*! lock protecting dirty pages list */ |
---|
| 76 | list_entry_t dirty_root; /*! root of dirty pages list */ |
---|
| 77 | void * vaddr_base; /*! pointer on local physical memory base */ |
---|
[1] | 78 | } |
---|
| 79 | ppm_t; |
---|
| 80 | |
---|
[567] | 81 | /************** functions to allocate / release physical pages *************************/ |
---|
| 82 | |
---|
[1] | 83 | /***************************************************************************************** |
---|
| 84 | * This is the low-level physical pages allocation function. |
---|
| 85 | * It allocates N contiguous physical pages. N is a power of 2. |
---|
[18] | 86 | * In normal use, you don't need to call it directly, as the recommended way to get |
---|
[1] | 87 | * physical pages is to call the generic allocator defined in kmem.h. |
---|
[7] | 88 | ***************************************************************************************** |
---|
[1] | 89 | * @ order : ln2( number of 4 Kbytes pages) |
---|
| 90 | * @ returns a pointer on the page descriptor if success / NULL otherwise |
---|
[50] | 91 | **************************************************************************************à))**/ |
---|
[1] | 92 | page_t * ppm_alloc_pages( uint32_t order ); |
---|
| 93 | |
---|
| 94 | /***************************************************************************************** |
---|
[53] | 95 | * This is the low-level physical pages release function. It takes the lock protecting |
---|
| 96 | * the free_list before register the released page in the relevant free_list. |
---|
[18] | 97 | * In normal use, you do not need to call it directly, as the recommended way to free |
---|
[1] | 98 | * physical pages is to call the generic allocator defined in kmem.h. |
---|
[7] | 99 | ***************************************************************************************** |
---|
[1] | 100 | * @ page : pointer to the page descriptor to be released |
---|
| 101 | ****************************************************************************************/ |
---|
| 102 | void ppm_free_pages( page_t * page ); |
---|
| 103 | |
---|
| 104 | /***************************************************************************************** |
---|
[53] | 105 | * This function does the same as the ppm_free_page() function, without taking the lock. |
---|
| 106 | * It is used by the hal_ppm_init() function to initialize the pages_tbl[] array, when |
---|
| 107 | * there is no concurrent access issue. |
---|
| 108 | ***************************************************************************************** |
---|
| 109 | * @ page : pointer to the page descriptor to be released |
---|
| 110 | ****************************************************************************************/ |
---|
| 111 | void ppm_free_pages_nolock( page_t * page ); |
---|
| 112 | |
---|
| 113 | /***************************************************************************************** |
---|
[50] | 114 | * This function check if a page descriptor pointer is valid. |
---|
[7] | 115 | ***************************************************************************************** |
---|
[1] | 116 | * @ page : pointer on a page descriptor |
---|
| 117 | * @ returns true if valid / false otherwise. |
---|
| 118 | ****************************************************************************************/ |
---|
| 119 | inline bool_t ppm_page_is_valid( page_t * page ); |
---|
| 120 | |
---|
[315] | 121 | |
---|
[567] | 122 | /************** functions to translate [ page <-> base <-> ppn ] ***********************/ |
---|
[315] | 123 | |
---|
[1] | 124 | /***************************************************************************************** |
---|
[315] | 125 | * Get extended pointer on page base from extended pointer on page descriptor. |
---|
[7] | 126 | ***************************************************************************************** |
---|
[315] | 127 | * @ page_xp : extended pointer to page descriptor |
---|
| 128 | * @ returns extended pointer on page base. |
---|
[1] | 129 | ****************************************************************************************/ |
---|
[315] | 130 | inline xptr_t ppm_page2base( xptr_t page_xp ); |
---|
[1] | 131 | |
---|
| 132 | /***************************************************************************************** |
---|
[315] | 133 | * Get extended pointer on page descriptor from extended pointer on page base. |
---|
[7] | 134 | ***************************************************************************************** |
---|
[315] | 135 | * @ base_xp : extended pointer to page base. |
---|
| 136 | * @ returns extended pointer on page descriptor |
---|
[1] | 137 | ****************************************************************************************/ |
---|
[315] | 138 | inline xptr_t ppm_base2page( xptr_t base_xp ); |
---|
[1] | 139 | |
---|
| 140 | /***************************************************************************************** |
---|
[407] | 141 | * Get extended pointer on page base from global PPN. |
---|
[315] | 142 | ***************************************************************************************** |
---|
| 143 | * @ ppn : global physical page number. |
---|
| 144 | * @ returns extended pointer on page base. |
---|
| 145 | ****************************************************************************************/ |
---|
| 146 | inline xptr_t ppm_ppn2base( ppn_t ppn ); |
---|
| 147 | |
---|
| 148 | /***************************************************************************************** |
---|
| 149 | * Get global PPN from extended pointer on page base. |
---|
| 150 | ***************************************************************************************** |
---|
| 151 | * @ base_xp : extended pointer to page base. |
---|
| 152 | * @ returns global physical page number. |
---|
| 153 | ****************************************************************************************/ |
---|
| 154 | inline ppn_t ppm_base2ppn( xptr_t base_xp ); |
---|
| 155 | |
---|
| 156 | /***************************************************************************************** |
---|
| 157 | * Get global PPN from extended pointer on page descriptor. |
---|
| 158 | ***************************************************************************************** |
---|
| 159 | * @ page_xp : pointer to page descriptor |
---|
| 160 | * @ returns global physical page number. |
---|
| 161 | ****************************************************************************************/ |
---|
| 162 | inline ppn_t ppm_page2ppn( xptr_t page_xp ); |
---|
| 163 | |
---|
| 164 | /***************************************************************************************** |
---|
| 165 | * Get extended pointer on page descriptor from global PPN. |
---|
| 166 | ***************************************************************************************** |
---|
| 167 | * @ ppn : global physical page number |
---|
| 168 | * @ returns extended pointer on page descriptor. |
---|
| 169 | ****************************************************************************************/ |
---|
| 170 | inline xptr_t ppm_ppn2page( ppn_t ppn ); |
---|
| 171 | |
---|
| 172 | |
---|
[606] | 173 | /*********** debug functions **********************************************************/ |
---|
[315] | 174 | |
---|
| 175 | /***************************************************************************************** |
---|
[433] | 176 | * This function prints the PPM allocator status in the calling thread cluster. |
---|
[1] | 177 | ****************************************************************************************/ |
---|
[486] | 178 | void ppm_print( void ); |
---|
[1] | 179 | |
---|
| 180 | /***************************************************************************************** |
---|
| 181 | * This function checks PPM allocator consistency. |
---|
[7] | 182 | ***************************************************************************************** |
---|
[1] | 183 | * @ ppm : pointer on PPM allocator. |
---|
[53] | 184 | * @ return 0 if PPM is OK / return -1 if PPM not consistent. |
---|
[1] | 185 | ****************************************************************************************/ |
---|
[53] | 186 | error_t ppm_assert_order( ppm_t * ppm ); |
---|
[1] | 187 | |
---|
[567] | 188 | |
---|
| 189 | /*********** functions to handle dirty pages *******************************************/ |
---|
| 190 | |
---|
| 191 | /***************************************************************************************** |
---|
[606] | 192 | * This function registers a page identified by the <page_xp> argument as dirty. |
---|
| 193 | * It can be called by a thread running in any cluster. |
---|
[567] | 194 | * - it takes the queuelock protecting the PPM dirty_list. |
---|
| 195 | * - it test the PG_DIRTY flag in the page descriptor. |
---|
| 196 | * . if page already dirty => do nothing |
---|
| 197 | * . it page not dirty => set the PG_DIRTY flag and register page in PPM dirty list. |
---|
| 198 | * - it releases the queuelock protecting the PPM dirty_list. |
---|
| 199 | ***************************************************************************************** |
---|
[606] | 200 | * @ page_xp : extended pointer on page descriptor. |
---|
[567] | 201 | * @ returns true if page was not dirty / returns false if page was dirty |
---|
| 202 | ****************************************************************************************/ |
---|
[606] | 203 | bool_t ppm_page_do_dirty( xptr_t page_xp ); |
---|
[567] | 204 | |
---|
| 205 | /***************************************************************************************** |
---|
[606] | 206 | * This function unregisters a page identified by the <page_xp> argument as dirty. |
---|
| 207 | * It can be called by a thread running in any cluster. |
---|
[567] | 208 | * - it takes the queuelock protecting the PPM dirty_list. |
---|
| 209 | * - it test the PG_DIRTY flag in the page descriptor. |
---|
| 210 | * . if page not dirty => do nothing |
---|
| 211 | * . it page dirty => reset the PG_DIRTY flag and remove page from PPM dirty list. |
---|
| 212 | * - it releases the queuelock protecting the PPM dirty_list. |
---|
| 213 | ***************************************************************************************** |
---|
[606] | 214 | * @ page_xp : extended pointer on page descriptor. |
---|
[567] | 215 | * @ returns true if page was dirty / returns false if page was not dirty |
---|
| 216 | ****************************************************************************************/ |
---|
[606] | 217 | bool_t ppm_page_undo_dirty( xptr_t page_xp ); |
---|
[567] | 218 | |
---|
| 219 | /***************************************************************************************** |
---|
[606] | 220 | * This function synchronizes (i.e. update the IOC device) all dirty pages in a cluster. |
---|
[567] | 221 | * - it takes the queuelock protecting the PPM dirty_list. |
---|
| 222 | * - it scans the PPM dirty list, and for each page: |
---|
| 223 | * . it takes the lock protecting the page. |
---|
| 224 | * . it removes the page from the PPM dirty_list. |
---|
| 225 | * . it reset the PG_DIRTY flag. |
---|
| 226 | * . it releases the lock protecting the page. |
---|
| 227 | * - it releases the queuelock protecting the PPM dirty_list. |
---|
| 228 | $ The PPM dirty_list is empty when the sync operation completes. |
---|
| 229 | ****************************************************************************************/ |
---|
[606] | 230 | void ppm_sync_dirty_pages( void ); |
---|
[567] | 231 | |
---|
[1] | 232 | #endif /* _PPM_H_ */ |
---|