| 1 | /* | 
|---|
| 2 |  * page.h - physical page descriptor and related operations | 
|---|
| 3 |  * | 
|---|
| 4 |  * Authors Ghassan Almalles (2008,2009,2010,2011,2012) | 
|---|
| 5 |  *         Alain Greiner    (2016) | 
|---|
| 6 |  * | 
|---|
| 7 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 8 |  * | 
|---|
| 9 |  * This file is part of ALMOS-MKH. | 
|---|
| 10 |  * | 
|---|
| 11 |  * ALMOS-MKH 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-MKH 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-MKH; if not, write to the Free Software Foundation, | 
|---|
| 22 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 23 |  */ | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef _PAGE_H_ | 
|---|
| 26 | #define _PAGE_H_ | 
|---|
| 27 |  | 
|---|
| 28 | #include <almos_config.h> | 
|---|
| 29 | #include <hal_types.h> | 
|---|
| 30 | #include <spinlock.h> | 
|---|
| 31 | #include <list.h> | 
|---|
| 32 | #include <slist.h> | 
|---|
| 33 | #include <xlist.h> | 
|---|
| 34 |  | 
|---|
| 35 | /***   Forward declarations   ***/ | 
|---|
| 36 |  | 
|---|
| 37 | struct mapper_s; | 
|---|
| 38 |   | 
|---|
| 39 | /************************************************************************************* | 
|---|
| 40 |  * This  defines the flags that can be attached to a physical page. | 
|---|
| 41 |  ************************************************************************************/ | 
|---|
| 42 |  | 
|---|
| 43 | #define PG_INIT             0x0001     // page descriptor has been initialised | 
|---|
| 44 | #define PG_RESERVED         0x0002     // cannot be allocated by PPM | 
|---|
| 45 | #define PG_FREE             0x0004     // page can be allocated by PPM | 
|---|
| 46 | #define PG_INLOAD           0x0008     // on-going load from disk  | 
|---|
| 47 | #define PG_IO_ERR           0x0010     // mapper signals a read/write access error | 
|---|
| 48 | #define PG_BUFFER           0x0020     // used in blockio.c | 
|---|
| 49 | #define PG_DIRTY            0x0040     // page has been written | 
|---|
| 50 | #define PG_LOCKED       0x0080     // page is locked | 
|---|
| 51 |  | 
|---|
| 52 | #define PG_ALL          0xFFFF     // All flags | 
|---|
| 53 |  | 
|---|
| 54 | /************************************************************************************* | 
|---|
| 55 |  * This structure defines a physical page descriptor. | 
|---|
| 56 |  * Size is 60 bytes for a 32 bits core... | 
|---|
| 57 |  ************************************************************************************/ | 
|---|
| 58 |  | 
|---|
| 59 | typedef struct page_s | 
|---|
| 60 | { | 
|---|
| 61 |     uint16_t          flags;          /*! flags defined above                  (2)  */ | 
|---|
| 62 |     uint16_t          order;          /*! log2( number of 4Kbytes pages)       (2)  */ | 
|---|
| 63 |  | 
|---|
| 64 |     struct mapper_s * mapper;         /*! local pointer on associated mapper   (4)  */ | 
|---|
| 65 |     uint32_t          index;          /*! page index in mapper                 (4)  */ | 
|---|
| 66 |   | 
|---|
| 67 |         union                             /*!                                      (4)  */ | 
|---|
| 68 |         { | 
|---|
| 69 |                 uint32_t      private;        /*! TODO ??? [AG]                             */ | 
|---|
| 70 |                 void        * data;           /*! TODO ??? [AG]                             */ | 
|---|
| 71 |                 slist_entry_t root;           /*! TODO ??? [AG]                             */ | 
|---|
| 72 |         }; | 
|---|
| 73 |  | 
|---|
| 74 |         list_entry_t      list;           /*! for both dirty pages and free pages  (8)  */  | 
|---|
| 75 |  | 
|---|
| 76 |     xlist_entry_t     wait_root;      /*! root of list of waiting threads      (16) */ | 
|---|
| 77 |  | 
|---|
| 78 |         uint32_t          refcount;       /*! reference counter                    (4)  */ | 
|---|
| 79 |         spinlock_t        lock;           /*! only used to set the PG_LOCKED flag  (16) */ | 
|---|
| 80 | } | 
|---|
| 81 | page_t; | 
|---|
| 82 |  | 
|---|
| 83 | /************************************************************************************* | 
|---|
| 84 |  * This function initializes one page descriptor. | 
|---|
| 85 |  * @ page    : pointer to page descriptor | 
|---|
| 86 |  ************************************************************************************/ | 
|---|
| 87 | inline void page_init( page_t * page ); | 
|---|
| 88 |  | 
|---|
| 89 | /************************************************************************************* | 
|---|
| 90 |  * This function set one or several flags in page descriptor flags. | 
|---|
| 91 |  * @ page    : pointer to page descriptor. | 
|---|
| 92 |  * @ value   : all non zero bits in value will be set.  | 
|---|
| 93 |  ************************************************************************************/ | 
|---|
| 94 | inline void page_set_flag( page_t   * page, | 
|---|
| 95 |                            uint16_t   value ); | 
|---|
| 96 |  | 
|---|
| 97 | /************************************************************************************* | 
|---|
| 98 |  * This function reset one or several flags in page descriptor flags. | 
|---|
| 99 |  * @ page    : pointer to page descriptor. | 
|---|
| 100 |  * @ value   : all non zero bits in value will be cleared.  | 
|---|
| 101 |  ************************************************************************************/ | 
|---|
| 102 | inline void page_clear_flag( page_t   * page, | 
|---|
| 103 |                              uint16_t   value ); | 
|---|
| 104 |  | 
|---|
| 105 | /************************************************************************************* | 
|---|
| 106 |  * This function test the value of one or several flags in page descriptor flags. | 
|---|
| 107 |  * @ page    : pointer to page descriptor. | 
|---|
| 108 |  * @ value   : all non zero bits will be tested. | 
|---|
| 109 |  * @ returns true if at least one non zero bit in value is set / false otherwise.  | 
|---|
| 110 |  ************************************************************************************/ | 
|---|
| 111 | inline bool_t page_is_flag( page_t   * page, | 
|---|
| 112 |                             uint16_t   value ); | 
|---|
| 113 |  | 
|---|
| 114 | /************************************************************************************* | 
|---|
| 115 |  * This function synchronizes (i.e. update the disk) all dirty pages in a cluster. | 
|---|
| 116 |  * It scan the PPM dirty list, that should be empty when this operation is completed.  | 
|---|
| 117 |  ************************************************************************************/ | 
|---|
| 118 | void sync_all_pages(); | 
|---|
| 119 |  | 
|---|
| 120 | /************************************************************************************* | 
|---|
| 121 |  * This function set the PG_DIRTY flag in the page descriptor, | 
|---|
| 122 |  * and register the page in the dirty list in PPM. | 
|---|
| 123 |  * @ page     : pointer on page descriptor. | 
|---|
| 124 |  * @ returns true if page was not dirty / returns false if page was dirty | 
|---|
| 125 |  ************************************************************************************/ | 
|---|
| 126 | bool_t page_do_dirty( page_t * page ); | 
|---|
| 127 |  | 
|---|
| 128 | /************************************************************************************* | 
|---|
| 129 |  * This function reset the PG_DIRTY flag in the page descriptor, | 
|---|
| 130 |  * and remove the page from the dirty list in PPM. | 
|---|
| 131 |  * @ page     : pointer on page descriptor. | 
|---|
| 132 |  * @ returns true if page was dirty / returns false if page was not dirty  | 
|---|
| 133 |  ************************************************************************************/ | 
|---|
| 134 | bool_t page_undo_dirty( page_t * page ); | 
|---|
| 135 |  | 
|---|
| 136 | /************************************************************************************* | 
|---|
| 137 |  * This function makes a local copy of the content of a src page  to a dst page. | 
|---|
| 138 |  * @ dst      : pointer on destination page descriptor. | 
|---|
| 139 |  * @ src      : pointer on source page descriptor. | 
|---|
| 140 |  ************************************************************************************/ | 
|---|
| 141 | void page_copy( page_t * dst, | 
|---|
| 142 |                 page_t * src ); | 
|---|
| 143 |  | 
|---|
| 144 | /************************************************************************************* | 
|---|
| 145 |  * This function reset to 0 all bytes in a given page.  | 
|---|
| 146 |  * @ page     : pointer on page descriptor. | 
|---|
| 147 |  ************************************************************************************/ | 
|---|
| 148 | void page_zero( page_t * page ); | 
|---|
| 149 |  | 
|---|
| 150 | /************************************************************************************* | 
|---|
| 151 |  * This blocking function set the PG_LOCKED flag on the page. | 
|---|
| 152 |  * It deschedule if the page has already been locked by another thread, | 
|---|
| 153 |  * and returns only when the flag has been successfully set.  | 
|---|
| 154 |  * @ page     : pointer on page descriptor. | 
|---|
| 155 |  ************************************************************************************/ | 
|---|
| 156 | void page_lock( page_t * page ); | 
|---|
| 157 |  | 
|---|
| 158 | /************************************************************************************* | 
|---|
| 159 |  * This blocking function reset the PG_LOCKED flag on the page, if there is no | 
|---|
| 160 |  * other waiting thread. IF there is waiting thread(s), it activates the first | 
|---|
| 161 |  * waiting thread without modifying the PG_LOCKED flag. | 
|---|
| 162 |  * @ page     : pointer on page descriptor. | 
|---|
| 163 |  ************************************************************************************/ | 
|---|
| 164 | void page_unlock( page_t * page ); | 
|---|
| 165 |  | 
|---|
| 166 | /************************************************************************************* | 
|---|
| 167 |  * This blocking function atomically increment the page refcount. | 
|---|
| 168 |  * @ page     : pointer on page descriptor. | 
|---|
| 169 |  ************************************************************************************/ | 
|---|
| 170 | inline void page_refcount_up( page_t * page ); | 
|---|
| 171 |  | 
|---|
| 172 | /************************************************************************************* | 
|---|
| 173 |  * This blocking function atomically decrement the page refcount. | 
|---|
| 174 |  * @ page     : pointer on page descriptor. | 
|---|
| 175 |  ************************************************************************************/ | 
|---|
| 176 | inline void page_refcount_down( page_t * page ); | 
|---|
| 177 |  | 
|---|
| 178 | /************************************************************************************* | 
|---|
| 179 |  * This function display the values contained in a page descriptor. | 
|---|
| 180 |  ************************************************************************************/ | 
|---|
| 181 | void page_print( page_t * page ); | 
|---|
| 182 |  | 
|---|
| 183 |  | 
|---|
| 184 | #endif  /* _PAGE_H_ */ | 
|---|