| 1 | /* |
|---|
| 2 | * ppm.h - Per-cluster Physical Pages Manager Interface |
|---|
| 3 | * |
|---|
| 4 | * Authors Ghassan Almaless (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-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 | |
|---|
| 28 | #include <hal_types.h> |
|---|
| 29 | #include <list.h> |
|---|
| 30 | #include <spinlock.h> |
|---|
| 31 | #include <boot_info.h> |
|---|
| 32 | #include <page.h> |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /***************************************************************************************** |
|---|
| 36 | * This structure defines the Physical Memory Manager in a cluster. |
|---|
| 37 | * In all clusters, the physical memory bank starts at local physical address 0. |
|---|
| 38 | * The size of this local physical memory is defined by the <pages_nr> field in the |
|---|
| 39 | * boot_info structure. It is split in three parts: |
|---|
| 40 | * - the "kernel_code" section contains the kernel code, loaded by the boot-loader. |
|---|
| 41 | * It starts at PPN = 0 and the size is defined by the <pages_offset> field in the |
|---|
| 42 | * boot_info structure. |
|---|
| 43 | * - the "pages_tbl" section contains the physical page descriptors array. It starts |
|---|
| 44 | * at PPN = pages_offset, and it contains one entry per small physical page in cluster. |
|---|
| 45 | * It is created and initialized by the hal_ppm_create() function. "the |
|---|
| 46 | * - The "kernel_heap" section contains all physical pages that are are not in the |
|---|
| 47 | * in the kernel_code and pages_tbl sections, and that have not been reserved by the |
|---|
| 48 | * architecture specific bootloader. The reserved pages are defined in the boot_info |
|---|
| 49 | * structure. |
|---|
| 50 | * |
|---|
| 51 | * The main service provided by the PMM is the dynamic allocation of physical pages |
|---|
| 52 | * from the "kernel_heap" section. |
|---|
| 53 | * This low-level allocator implements the buddy algorithm: an allocated block is |
|---|
| 54 | * an integer number n of 4 Kbytes pages, and n (called order) is a power of 2. |
|---|
| 55 | ****************************************************************************************/ |
|---|
| 56 | |
|---|
| 57 | typedef struct ppm_s |
|---|
| 58 | { |
|---|
| 59 | spinlock_t free_lock; /*! lock protecting free_pages[] lists */ |
|---|
| 60 | list_entry_t free_pages_root[CONFIG_PPM_MAX_ORDER]; /*! roots of free lists */ |
|---|
| 61 | uint32_t free_pages_nr[CONFIG_PPM_MAX_ORDER]; /*! numbers of free pages */ |
|---|
| 62 | page_t * pages_tbl; /*! pointer on page descriptors array */ |
|---|
| 63 | uint32_t pages_nr; /*! total number of small physical page */ |
|---|
| 64 | spinlock_t dirty_lock; /*! lock protecting the dirty pages list */ |
|---|
| 65 | list_entry_t dirty_root; /*! root of dirty pages list */ |
|---|
| 66 | void * vaddr_base; /*! pointer on local physical memory base */ |
|---|
| 67 | } |
|---|
| 68 | ppm_t; |
|---|
| 69 | |
|---|
| 70 | /***************************************************************************************** |
|---|
| 71 | * This function initializes a PPM (Physical Pages Manager) in a cluster. |
|---|
| 72 | * The physical memory base address in all clusters is zero. |
|---|
| 73 | * The physical memory size is NOT constrained to be smaller than 4 Gbytes. |
|---|
| 74 | ***************************************************************************************** |
|---|
| 75 | * @ ppm : pointer on physical pages manager. |
|---|
| 76 | * @ pages_nr : total physical memory size (number of 4 Kbytes pages). |
|---|
| 77 | * @ pages_offset : number of pages already allocated in this physical memory. |
|---|
| 78 | ****************************************************************************************/ |
|---|
| 79 | void ppm_init( ppm_t * ppm, |
|---|
| 80 | uint32_t pages_nr, |
|---|
| 81 | uint32_t pages_offset ); |
|---|
| 82 | |
|---|
| 83 | /***************************************************************************************** |
|---|
| 84 | * This is the low-level physical pages allocation function. |
|---|
| 85 | * It allocates N contiguous physical pages. N is a power of 2. |
|---|
| 86 | * In normal use, you don't need to call it directly, as the recommended way to get |
|---|
| 87 | * physical pages is to call the generic allocator defined in kmem.h. |
|---|
| 88 | ***************************************************************************************** |
|---|
| 89 | * @ order : ln2( number of 4 Kbytes pages) |
|---|
| 90 | * @ returns a pointer on the page descriptor if success / NULL otherwise |
|---|
| 91 | **************************************************************************************à))**/ |
|---|
| 92 | page_t * ppm_alloc_pages( uint32_t order ); |
|---|
| 93 | |
|---|
| 94 | /***************************************************************************************** |
|---|
| 95 | * This is the low-level physical pages release function. |
|---|
| 96 | * In normal use, you do not need to call it directly, as the recommended way to free |
|---|
| 97 | * physical pages is to call the generic allocator defined in kmem.h. |
|---|
| 98 | ***************************************************************************************** |
|---|
| 99 | * @ page : pointer to the page descriptor to be released |
|---|
| 100 | ****************************************************************************************/ |
|---|
| 101 | void ppm_free_pages( page_t * page ); |
|---|
| 102 | |
|---|
| 103 | /***************************************************************************************** |
|---|
| 104 | * This function check if a page descriptor pointer is valid. |
|---|
| 105 | ***************************************************************************************** |
|---|
| 106 | * @ page : pointer on a page descriptor |
|---|
| 107 | * @ returns true if valid / false otherwise. |
|---|
| 108 | ****************************************************************************************/ |
|---|
| 109 | inline bool_t ppm_page_is_valid( page_t * page ); |
|---|
| 110 | |
|---|
| 111 | /***************************************************************************************** |
|---|
| 112 | * Get the page virtual address from the page descriptor pointer. |
|---|
| 113 | ***************************************************************************************** |
|---|
| 114 | * @ page : pointer to page descriptor |
|---|
| 115 | * @ returns virtual address of page itself. |
|---|
| 116 | ****************************************************************************************/ |
|---|
| 117 | inline void* ppm_page2vaddr( page_t * page ); |
|---|
| 118 | |
|---|
| 119 | /***************************************************************************************** |
|---|
| 120 | * Get the page descriptor pointer from the page virtual address. |
|---|
| 121 | ***************************************************************************************** |
|---|
| 122 | * @ vaddr : page virtual address |
|---|
| 123 | * @ returns pointer on page descriptor |
|---|
| 124 | ****************************************************************************************/ |
|---|
| 125 | inline page_t * ppm_vaddr2page( void * vaddr ); |
|---|
| 126 | |
|---|
| 127 | /***************************************************************************************** |
|---|
| 128 | * Get the PPN from the page descriptor pointer. |
|---|
| 129 | ***************************************************************************************** |
|---|
| 130 | * @ page : pointer to page descriptor |
|---|
| 131 | * @ returns physical page number |
|---|
| 132 | ****************************************************************************************/ |
|---|
| 133 | inline ppn_t ppm_page2ppn( page_t * page ); |
|---|
| 134 | |
|---|
| 135 | /***************************************************************************************** |
|---|
| 136 | * Get the page descriptor pointer from the PPN. |
|---|
| 137 | ***************************************************************************************** |
|---|
| 138 | * @ ppn : physical page number |
|---|
| 139 | * @ returns pointer on page descriptor |
|---|
| 140 | ****************************************************************************************/ |
|---|
| 141 | inline page_t * ppm_ppn2page( ppn_t ppn ); |
|---|
| 142 | |
|---|
| 143 | /***************************************************************************************** |
|---|
| 144 | * Get the page virtual address from the PPN. |
|---|
| 145 | ***************************************************************************************** |
|---|
| 146 | * @ ppn : physical page number |
|---|
| 147 | * @ returns page virtual address. |
|---|
| 148 | ****************************************************************************************/ |
|---|
| 149 | inline void* ppm_ppn2vaddr( ppn_t ppn ); |
|---|
| 150 | |
|---|
| 151 | /***************************************************************************************** |
|---|
| 152 | * Get the PPN from the page virtual address. |
|---|
| 153 | ***************************************************************************************** |
|---|
| 154 | * @ vaddr : page virtual address |
|---|
| 155 | * @ returns physical page number. |
|---|
| 156 | ****************************************************************************************/ |
|---|
| 157 | inline ppn_t ppm_vaddr2ppn( void * base ); |
|---|
| 158 | |
|---|
| 159 | /***************************************************************************************** |
|---|
| 160 | * This function prints the PPM allocator status. |
|---|
| 161 | ***************************************************************************************** |
|---|
| 162 | * @ ppm : pointer on PPM allocator. |
|---|
| 163 | * @ string : define context of display. |
|---|
| 164 | ****************************************************************************************/ |
|---|
| 165 | void ppm_print( ppm_t * ppm, |
|---|
| 166 | char * string ); |
|---|
| 167 | |
|---|
| 168 | /***************************************************************************************** |
|---|
| 169 | * This function checks PPM allocator consistency. |
|---|
| 170 | ***************************************************************************************** |
|---|
| 171 | * @ ppm : pointer on PPM allocator. |
|---|
| 172 | ****************************************************************************************/ |
|---|
| 173 | void ppm_assert_order( ppm_t * ppm ); |
|---|
| 174 | |
|---|
| 175 | #endif /* _PPM_H_ */ |
|---|