| 1 | /* | 
|---|
| 2 | * almosmkh.h - User level ALMOS-MKH specific library definition. | 
|---|
| 3 | * | 
|---|
| 4 | * Author     Alain Greiner (2016,2017,2018) | 
|---|
| 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 _LIBALMOSMKH_H_ | 
|---|
| 25 | #define _LIBALMOSMKH_H_ | 
|---|
| 26 |  | 
|---|
| 27 | /*************************************************************************************** | 
|---|
| 28 | * This file defines an user level, ALMOS-MKH specific library, containing: | 
|---|
| 29 | * - non standard system calls. | 
|---|
| 30 | * - debug functions. | 
|---|
| 31 | * - remote malloc extensions. | 
|---|
| 32 | **************************************************************************************/ | 
|---|
| 33 |  | 
|---|
| 34 | #include <pthread.h> | 
|---|
| 35 | #include <shared_almos.h> | 
|---|
| 36 |  | 
|---|
| 37 | /****************** Non standard (ALMOS_MKH specific) system calls ********************/ | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | /*************************************************************************************** | 
|---|
| 41 | * This function is used to give the process identified by the <pid> argument the | 
|---|
| 42 | * exclusive ownership of its TXT terminal. | 
|---|
| 43 | *************************************************************************************** | 
|---|
| 44 | * @ pid        : process identifier. | 
|---|
| 45 | * @ returns O if success / returns -1 if process not found. | 
|---|
| 46 | **************************************************************************************/ | 
|---|
| 47 | int fg( unsigned int pid ); | 
|---|
| 48 |  | 
|---|
| 49 | /*************************************************************************************** | 
|---|
| 50 | * This function stores in the buffer identified by the <owner> argument a non zero | 
|---|
| 51 | * value when the process identified by the <pid> argument is currently the exclusive | 
|---|
| 52 | * owner of its TXT terminal. | 
|---|
| 53 | *************************************************************************************** | 
|---|
| 54 | * @ pid        : [in]  process identifier. | 
|---|
| 55 | * @ owner      : [out] pointer on buffer to store the | 
|---|
| 56 | * @ returns O if success / returns -1 if process not found. | 
|---|
| 57 | **************************************************************************************/ | 
|---|
| 58 | int is_fg( unsigned int pid, | 
|---|
| 59 | unsigned int * owner ); | 
|---|
| 60 |  | 
|---|
| 61 | /*************************************************************************************** | 
|---|
| 62 | * This function returns the hardware platform parameters. | 
|---|
| 63 | *************************************************************************************** | 
|---|
| 64 | * @ x_size   : [out] number of clusters in a row. | 
|---|
| 65 | * @ y_size   : [out] number of clusters in a column. | 
|---|
| 66 | * @ ncores   : [out] number of cores per cluster. | 
|---|
| 67 | * @ return always 0. | 
|---|
| 68 | **************************************************************************************/ | 
|---|
| 69 | int get_config( unsigned int * x_size, | 
|---|
| 70 | unsigned int * y_size, | 
|---|
| 71 | unsigned int * ncores ); | 
|---|
| 72 |  | 
|---|
| 73 | /*************************************************************************************** | 
|---|
| 74 | * This function returns the cluster an local index for the calling core. | 
|---|
| 75 | *************************************************************************************** | 
|---|
| 76 | * @ cxy      : [out] cluster identifier. | 
|---|
| 77 | * @ lid      : [out] core local index in cluster. | 
|---|
| 78 | * @ return always 0. | 
|---|
| 79 | **************************************************************************************/ | 
|---|
| 80 | int get_core( unsigned int * cxy, | 
|---|
| 81 | unsigned int * lid ); | 
|---|
| 82 |  | 
|---|
| 83 | /*************************************************************************************** | 
|---|
| 84 | * This function returns the calling core cycles counter, | 
|---|
| 85 | * taking into account a possible overflow on 32 bits architectures. | 
|---|
| 86 | *************************************************************************************** | 
|---|
| 87 | * @ cycle    : [out] current cycle value. | 
|---|
| 88 | * @ return always 0. | 
|---|
| 89 | **************************************************************************************/ | 
|---|
| 90 | int get_cycle( unsigned long long * cycle ); | 
|---|
| 91 |  | 
|---|
| 92 | /*************************************************************************************** | 
|---|
| 93 | * This function implement the operations related to User Thread Local Storage. | 
|---|
| 94 | *************************************************************************************** | 
|---|
| 95 | * @ operation  : UTLS operation type as defined in "shared_sycalls.h" file. | 
|---|
| 96 | * @ value      : argument value for the UTLS_SET operation. | 
|---|
| 97 | * @ return value for the UTLS_GET and UTLS_GET_ERRNO / return -1 if failure. | 
|---|
| 98 | **************************************************************************************/ | 
|---|
| 99 | int utls( unsigned int operation, | 
|---|
| 100 | unsigned int value ); | 
|---|
| 101 |  | 
|---|
| 102 | /*************************************************************************************** | 
|---|
| 103 | * This function returns a positive integer fom the standard "stdin" stream. | 
|---|
| 104 | *************************************************************************************** | 
|---|
| 105 | * returns the integer value if success / returns -1 if failure. | 
|---|
| 106 | **************************************************************************************/ | 
|---|
| 107 | int getint(); | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | /***************** Non standard (ALMOS-MKH specific) debug functions ******************/ | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 | /*************************************************************************************** | 
|---|
| 114 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 115 | * the thread / process / core identifiers, the current cycle, plus a user defined | 
|---|
| 116 | * message as specified by the <string> argument. | 
|---|
| 117 | *************************************************************************************** | 
|---|
| 118 | * @ string    : [in] user defined message. | 
|---|
| 119 | **************************************************************************************/ | 
|---|
| 120 | void display_string( char * string ); | 
|---|
| 121 |  | 
|---|
| 122 | /*************************************************************************************** | 
|---|
| 123 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 124 | * the state of the  VMM for the process <pid>, in cluster <cxy>. | 
|---|
| 125 | * It can be called by any thread running in any cluster. | 
|---|
| 126 | *************************************************************************************** | 
|---|
| 127 | * @ pid      : [in] process identifier. | 
|---|
| 128 | * @ return 0 if success / return -1 if illegal argument. | 
|---|
| 129 | **************************************************************************************/ | 
|---|
| 130 | int display_vmm(unsigned int cxy, unsigned int  pid ); | 
|---|
| 131 |  | 
|---|
| 132 | /*************************************************************************************** | 
|---|
| 133 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 134 | * the state of the core scheduler identified by the <cxy> and <lid> arguments. | 
|---|
| 135 | * It can be called by any thread running in any cluster. | 
|---|
| 136 | *************************************************************************************** | 
|---|
| 137 | * @ cxy      : [in] target cluster identifier. | 
|---|
| 138 | * @ lid      : [in] target core local index. | 
|---|
| 139 | * @ return 0 if success / return -1 if illegal arguments. | 
|---|
| 140 | **************************************************************************************/ | 
|---|
| 141 | int display_sched( unsigned int  cxy, | 
|---|
| 142 | unsigned int  lid ); | 
|---|
| 143 |  | 
|---|
| 144 | /*************************************************************************************** | 
|---|
| 145 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 146 | * the list of process registered in a given cluster identified by the <cxy> argument. | 
|---|
| 147 | * It can be called by any thread running in any cluster. | 
|---|
| 148 | *************************************************************************************** | 
|---|
| 149 | * @ cxy      : [in] target cluster identifier. | 
|---|
| 150 | * @ return 0 if success / return -1 if illegal argument. | 
|---|
| 151 | **************************************************************************************/ | 
|---|
| 152 | int display_cluster_processes( unsigned int  cxy ); | 
|---|
| 153 |  | 
|---|
| 154 | /*************************************************************************************** | 
|---|
| 155 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 156 | * the list of processes attached to a given TXT channel. | 
|---|
| 157 | * It can be called by any thread running in any cluster. | 
|---|
| 158 | *************************************************************************************** | 
|---|
| 159 | * @ txt_id   : [in] TXT terminal indes. | 
|---|
| 160 | * @ return 0 if success / return -1 if illegal argument. | 
|---|
| 161 | **************************************************************************************/ | 
|---|
| 162 | int display_txt_processes( unsigned int txt_id ); | 
|---|
| 163 |  | 
|---|
| 164 | /*************************************************************************************** | 
|---|
| 165 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 166 | * the list of channel devices available in the architecture. | 
|---|
| 167 | * It can be called by any thread running in any cluster. | 
|---|
| 168 | *************************************************************************************** | 
|---|
| 169 | * @ return always 0. | 
|---|
| 170 | **************************************************************************************/ | 
|---|
| 171 | int display_chdev(); | 
|---|
| 172 |  | 
|---|
| 173 | /*************************************************************************************** | 
|---|
| 174 | * This debug function displays on the kernel terminal TXT0 | 
|---|
| 175 | * the list of channel device or pseudo-files registered in the VFS cache. | 
|---|
| 176 | * It can be called by any thread running in any cluster. | 
|---|
| 177 | *************************************************************************************** | 
|---|
| 178 | * @ return always 0. | 
|---|
| 179 | **************************************************************************************/ | 
|---|
| 180 | int display_vfs(); | 
|---|
| 181 |  | 
|---|
| 182 | /*************************************************************************************** | 
|---|
| 183 | * This debug function displays on the kernel terminal TXT0 the current DQDT state. | 
|---|
| 184 | * It can be called by any thread running in any cluster. | 
|---|
| 185 | *************************************************************************************** | 
|---|
| 186 | * @ return always 0. | 
|---|
| 187 | **************************************************************************************/ | 
|---|
| 188 | int display_dqdt(); | 
|---|
| 189 |  | 
|---|
| 190 | /***************************************************************************************** | 
|---|
| 191 | * This debug function is used to activate / desactivate the context switches trace | 
|---|
| 192 | * for a core identified by the <cxy> and <lid> arguments. | 
|---|
| 193 | * It can be called by any thread running in any cluster. | 
|---|
| 194 | ***************************************************************************************** | 
|---|
| 195 | * @ active     : activate trace if non zero / desactivate if zero. | 
|---|
| 196 | * @ cxy        : cluster identifier. | 
|---|
| 197 | * @ lid        : core local index. | 
|---|
| 198 | * @ returns O if success / returns -1 if illegal arguments. | 
|---|
| 199 | ****************************************************************************************/ | 
|---|
| 200 | int trace( unsigned int active, | 
|---|
| 201 | unsigned int cxy, | 
|---|
| 202 | unsigned int lid ); | 
|---|
| 203 |  | 
|---|
| 204 | /**************************************************************************************** | 
|---|
| 205 | * This blocking function implements an user-level interactive debugger that can be | 
|---|
| 206 | * introduced in any user application to display various kernel distributed structures. | 
|---|
| 207 | * The supported commands are: | 
|---|
| 208 | * - p (cxy)     : display all processes descriptors in a given cluster. | 
|---|
| 209 | * - s (cxy,lid) : display all threads attached to a given core in a given cluster. | 
|---|
| 210 | * - v (cxy)     : display the calling process VMM in a given cluster. | 
|---|
| 211 | * - t (tid)     : display all owner process descriptors attached to a TXT terminal. | 
|---|
| 212 | * - x           : force the calling process to exit. | 
|---|
| 213 | * - c           : continue calling process execution. | 
|---|
| 214 | * - h           : list the supported commands | 
|---|
| 215 | ***************************************************************************************/ | 
|---|
| 216 | void idbg(); | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | /****************** Non standard (ALMOS-MKH specific) malloc operations  ***************/ | 
|---|
| 220 |  | 
|---|
| 221 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 222 | // General principles: | 
|---|
| 223 | // - In user space the HEAP zone spread between the ELF zone and the STACK zone, | 
|---|
| 224 | //   as defined in the kernel_config.h file. | 
|---|
| 225 | // - The malloc library uses the mmap() syscall to create - on demand - | 
|---|
| 226 | //   one vseg in a given cluster. The size of this vseg is defined below | 
|---|
| 227 | //   by the MALLOC_LOCAL_STORE_SIZE parameter. | 
|---|
| 228 | // - For a standard malloc(), the target cluster is the cluster containing | 
|---|
| 229 | //   the core running the client thread. | 
|---|
| 230 | // - For a remote_malloc(), the target cluster is explicitely defined | 
|---|
| 231 | //   by the argument. | 
|---|
| 232 | // - In each cluster, the available storage in virtual space is handled by a | 
|---|
| 233 | //   local allocator using the buddy algorithm. | 
|---|
| 234 | // | 
|---|
| 235 | // TODO : In this first implementation one single - fixed size - vseg | 
|---|
| 236 | //        is allocated on demand in each cluster. | 
|---|
| 237 | //        We should introduce the possibility to dynamically allocate | 
|---|
| 238 | //        several vsegs in each cluster, using several mmap when required. | 
|---|
| 239 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 240 | // Free blocks organisation in each cluster : | 
|---|
| 241 | // - All free blocks have a size that is a power of 2, larger or equal | 
|---|
| 242 | //   to MALLOC_MIN_BLOCK_SIZE (typically 64 bytes). | 
|---|
| 243 | // - All free blocks are aligned. | 
|---|
| 244 | // - They are pre-classed in an array of linked lists, where all blocks in a | 
|---|
| 245 | //   given list have the same size. | 
|---|
| 246 | // - The NEXT pointer implementing those linked lists is written | 
|---|
| 247 | //   in the first bytes of the block itself, using the unsigned int type. | 
|---|
| 248 | // - The pointers on the first free block for each size are stored in an | 
|---|
| 249 | //   array of pointers free[32] in the storage(x,y) descriptor. | 
|---|
| 250 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 251 | // Allocation policy: | 
|---|
| 252 | // - The block size required by the user can be any value, but the allocated | 
|---|
| 253 | //   block size can be larger than the requested size: | 
|---|
| 254 | // - The allocator computes actual_size, that is the smallest power of 2 | 
|---|
| 255 | //   value larger or equal to the requested size AND larger or equal to | 
|---|
| 256 | //   MALLOC_MIN_BLOCK_SIZE. | 
|---|
| 257 | // - It pop the linked list of free blocks corresponding to actual_size, | 
|---|
| 258 | //   and returns the block B if the list[actual_size] is not empty. | 
|---|
| 259 | // - If the list[actual_size] is empty, it pop the list[actual_size * 2]. | 
|---|
| 260 | //   If a block B' is found, it breaks this block in 2 B/2 blocks, returns | 
|---|
| 261 | //   the first B/2 block and push the other B/2 block into list[actual_size]. | 
|---|
| 262 | // - If the list[actual_size * 2] is empty, it pop the list[actual_size * 4]. | 
|---|
| 263 | //   If a block B is found, it break this block in 3 blocks B/4, B/4 and B/2, | 
|---|
| 264 | //   returns the first B/4 block, push the other blocks B/4 and B/2 into | 
|---|
| 265 | //   the proper lists. etc... | 
|---|
| 266 | // - If no block satisfying the request is available it returns a failure | 
|---|
| 267 | //   (NULL pointer). | 
|---|
| 268 | // - This allocation policy has the nice following property: | 
|---|
| 269 | //   If the vseg is aligned (the vseg base is a multiple of the | 
|---|
| 270 | //   vseg size), all allocated blocks are aligned on the actual_size. | 
|---|
| 271 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 272 | // Free policy: | 
|---|
| 273 | // - Each allocated block is registered in an alloc[] array of unsigned char. | 
|---|
| 274 | // - This registration is required by the free() operation, because the size | 
|---|
| 275 | //   of the allocated block must be obtained from the base address of the block. | 
|---|
| 276 | // - The number of entries in this array is equal to the max number | 
|---|
| 277 | //   of allocated block : MALLOC_LOCAL_STORE_SIZE / MALLOC_MIN_BLOCK_SIZE. | 
|---|
| 278 | // - For each allocated block, the value registered in the alloc[] array | 
|---|
| 279 | //   is log2( size_of_allocated_block ). | 
|---|
| 280 | // - The index in this array is computed from the allocated block base address: | 
|---|
| 281 | //      index = (block_base - vseg_base) / MALLOC_MIN_BLOCK_SIZE | 
|---|
| 282 | // - The alloc[] array is stored at the end of heap segment. This consume | 
|---|
| 283 | //   (1 / MALLOC_MIN_BLOCK_SIZE) of the total storage capacity. | 
|---|
| 284 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 285 |  | 
|---|
| 286 |  | 
|---|
| 287 | #define MALLOC_INITIALIZED         0xBABEF00D   // magic number when initialised | 
|---|
| 288 | #define MALLOC_MIN_BLOCK_SIZE      0x40         // 64 bytes | 
|---|
| 289 | #define MALLOC_LOCAL_STORE_SIZE    0x800000     // 8 Mbytes | 
|---|
| 290 | #define MALLOC_MAX_CLUSTERS        0x100        // 256 clusters | 
|---|
| 291 |  | 
|---|
| 292 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 293 | //               store(x,y) descriptor (one per cluster) | 
|---|
| 294 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 295 |  | 
|---|
| 296 | typedef struct malloc_store_s | 
|---|
| 297 | { | 
|---|
| 298 | pthread_mutex_t mutex;           // lock protecting exclusive access to local heap | 
|---|
| 299 | unsigned int    initialized;     // initialised when value == MALLOC_INITIALIZED | 
|---|
| 300 | unsigned int    cxy;             // cluster identifier | 
|---|
| 301 | unsigned int    store_base;      // store base address | 
|---|
| 302 | unsigned int    store_size;      // store size (bytes) | 
|---|
| 303 | unsigned int    alloc_base;      // alloc[] array base address | 
|---|
| 304 | unsigned int    alloc_size;      // alloc[] array size (bytes) | 
|---|
| 305 | unsigned int    free[32];        // array of addresses of first free block | 
|---|
| 306 | } | 
|---|
| 307 | malloc_store_t; | 
|---|
| 308 |  | 
|---|
| 309 | /***************************************************************************************** | 
|---|
| 310 | * This function allocates <size> bytes of memory in user space, and returns a pointer | 
|---|
| 311 | * to the allocated buffer. The pysical memory is allocated from store located in | 
|---|
| 312 | * cluster identified by the <cxy> argument. | 
|---|
| 313 | ***************************************************************************************** | 
|---|
| 314 | * @ size    : number of requested bytes. | 
|---|
| 315 | * @ cxy     : target cluster identifier. | 
|---|
| 316 | * @ returns a pointer on the allocated buffer if success / returns NULL if failure | 
|---|
| 317 | ****************************************************************************************/ | 
|---|
| 318 | void * remote_malloc( unsigned int size, | 
|---|
| 319 | unsigned int cxy ); | 
|---|
| 320 |  | 
|---|
| 321 | /***************************************************************************************** | 
|---|
| 322 | * This function releases the memory buffer identified by the <ptr> argument, | 
|---|
| 323 | * to the store identified by the <cxy> argument. | 
|---|
| 324 | * It displays an error message, but does nothing if the ptr is illegal. | 
|---|
| 325 | ***************************************************************************************** | 
|---|
| 326 | * @ ptr   : pointer on the released buffer. | 
|---|
| 327 | * @ cxy   : target cluster identifier. | 
|---|
| 328 | ****************************************************************************************/ | 
|---|
| 329 | void remote_free( void        * ptr, | 
|---|
| 330 | unsigned int  cxy ); | 
|---|
| 331 |  | 
|---|
| 332 | /***************************************************************************************** | 
|---|
| 333 | * This function releases the memory buffer identified by the <ptr> argument, | 
|---|
| 334 | * to the store located in cluster identified by the <cxy> argument, and allocates | 
|---|
| 335 | * a new buffer containing <size> bytes from this store. | 
|---|
| 336 | * The content of the old buffer is copied to the new buffer, up to <size> bytes. | 
|---|
| 337 | * It displays an error message, but does nothing if the ptr is illegal. | 
|---|
| 338 | ***************************************************************************************** | 
|---|
| 339 | * @ ptr     : pointer on the released buffer. | 
|---|
| 340 | * @ size    : new buffer requested size (bytes). | 
|---|
| 341 | * @ cxy     : target cluster identifier. | 
|---|
| 342 | * @ return a pointer on allocated buffer if success / return NULL if failure | 
|---|
| 343 | ****************************************************************************************/ | 
|---|
| 344 | void * remote_realloc( void        * ptr, | 
|---|
| 345 | unsigned int  size, | 
|---|
| 346 | unsigned int  cxy ); | 
|---|
| 347 |  | 
|---|
| 348 | /***************************************************************************************** | 
|---|
| 349 | * This function allocates enough space for <count> objects that are <size> bytes | 
|---|
| 350 | * of memory each from the store located in cluster identied by the <cxy> argument. | 
|---|
| 351 | * The allocated memory is filled with bytes of value zero. | 
|---|
| 352 | ***************************************************************************************** | 
|---|
| 353 | * @ count   : number of requested objects. | 
|---|
| 354 | * @ size    : number of bytes per object. | 
|---|
| 355 | * @ cxy     : target cluster identifier. | 
|---|
| 356 | * @ returns a pointer on allocated buffer if success / returns NULL if failure | 
|---|
| 357 | ****************************************************************************************/ | 
|---|
| 358 | void * remote_calloc( unsigned int count, | 
|---|
| 359 | unsigned int size, | 
|---|
| 360 | unsigned int cxy ); | 
|---|
| 361 |  | 
|---|
| 362 | #endif /* _LIBALMOSMKH_H_ */ | 
|---|
| 363 |  | 
|---|