[445] | 1 | /* |
---|
| 2 | * almosmkh.h - User level ALMOS-MKH specific library definition. |
---|
| 3 | * |
---|
[657] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
[445] | 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 | |
---|
[444] | 24 | #ifndef _LIBALMOSMKH_H_ |
---|
| 25 | #define _LIBALMOSMKH_H_ |
---|
| 26 | |
---|
[445] | 27 | /*************************************************************************************** |
---|
[457] | 28 | * This file defines an user level, ALMOS-MKH specific library, containing: |
---|
[445] | 29 | * - non standard system calls. |
---|
| 30 | * - debug functions. |
---|
[657] | 31 | * - remote malloc extensions. |
---|
| 32 | * - a parallel_pthread_create function. |
---|
| 33 | * - Frame Buffer access syscalls. |
---|
[445] | 34 | **************************************************************************************/ |
---|
| 35 | |
---|
[444] | 36 | #include <pthread.h> |
---|
[445] | 37 | #include <shared_almos.h> |
---|
[444] | 38 | |
---|
[657] | 39 | |
---|
[445] | 40 | /****************** Non standard (ALMOS_MKH specific) system calls ********************/ |
---|
[444] | 41 | |
---|
| 42 | |
---|
[445] | 43 | /*************************************************************************************** |
---|
[589] | 44 | * This syscall gives the process identified by the <pid> argument the exclusive |
---|
| 45 | * ownership of its TXT terminal. |
---|
[445] | 46 | *************************************************************************************** |
---|
[444] | 47 | * @ pid : process identifier. |
---|
| 48 | * @ returns O if success / returns -1 if process not found. |
---|
[445] | 49 | **************************************************************************************/ |
---|
[444] | 50 | int fg( unsigned int pid ); |
---|
| 51 | |
---|
| 52 | /*************************************************************************************** |
---|
[589] | 53 | * This syscall stores in the buffer identified by the <owner> argument a non zero |
---|
[457] | 54 | * value when the process identified by the <pid> argument is currently the exclusive |
---|
| 55 | * owner of its TXT terminal. |
---|
| 56 | *************************************************************************************** |
---|
| 57 | * @ pid : [in] process identifier. |
---|
| 58 | * @ owner : [out] pointer on buffer to store the |
---|
| 59 | * @ returns O if success / returns -1 if process not found. |
---|
| 60 | **************************************************************************************/ |
---|
| 61 | int is_fg( unsigned int pid, |
---|
| 62 | unsigned int * owner ); |
---|
| 63 | |
---|
| 64 | /*************************************************************************************** |
---|
[661] | 65 | * This syscall returns the hardware platform parameters in the hard_config_t pointed |
---|
| 66 | * by the <config> argument. This structure is defined in the shared_almos.h file. |
---|
| 67 | * The available configuration parameters are : |
---|
| 68 | * - cores : x_size / y_size / ncores |
---|
| 69 | * - peripherals : nb_txt_channels / nb_nic_channels / nb_ioc_channels / nb_fbf_channels |
---|
[444] | 70 | *************************************************************************************** |
---|
[661] | 71 | * @ config : [out] pointer on hard_config_t structure in user space. |
---|
| 72 | * @ return 0 if success / return -1 if illegal argument. |
---|
[444] | 73 | **************************************************************************************/ |
---|
[661] | 74 | int get_config( struct hard_config_s * config ); |
---|
[444] | 75 | |
---|
| 76 | /*************************************************************************************** |
---|
[637] | 77 | * This syscall returns the cluster identifier and the local index |
---|
| 78 | * for the calling core. |
---|
[444] | 79 | *************************************************************************************** |
---|
| 80 | * @ cxy : [out] cluster identifier. |
---|
| 81 | * @ lid : [out] core local index in cluster. |
---|
| 82 | * @ return always 0. |
---|
| 83 | **************************************************************************************/ |
---|
[637] | 84 | int get_core_id( unsigned int * cxy, |
---|
| 85 | unsigned int * lid ); |
---|
[444] | 86 | |
---|
[637] | 87 | /*************************************************************************************** |
---|
| 88 | * This syscall returns the number of cores in a given cluster. |
---|
| 89 | *************************************************************************************** |
---|
| 90 | * @ cxy : [in] target cluster identifier. |
---|
| 91 | * @ ncores : [out] number of cores in target cluster. |
---|
| 92 | * @ return always 0. |
---|
| 93 | **************************************************************************************/ |
---|
| 94 | int get_nb_cores( unsigned int cxy, |
---|
| 95 | unsigned int * ncores ); |
---|
| 96 | |
---|
| 97 | /*************************************************************************************** |
---|
| 98 | * This syscall uses the DQDT to search, in a macro-cluster specified by the |
---|
| 99 | * <cxy_base> and <level> arguments arguments, the core with the lowest load. |
---|
| 100 | * it writes in the <cxy> and <lid> buffers the selected core cluster identifier |
---|
| 101 | * and the local core index. |
---|
| 102 | *************************************************************************************** |
---|
| 103 | * @ cxy_base : [in] any cluster identifier in macro-cluster.in clusters array. |
---|
| 104 | * @ level : [in] macro-cluster level in [1,2,3,4,5]. |
---|
| 105 | * @ cxy : [out] selected core cluster identifier. |
---|
[650] | 106 | * @ lid : [out] selected core local index. |
---|
[637] | 107 | * @ return 0 if success / 1 if no core in macro-cluster / -1 if illegal arguments. |
---|
| 108 | **************************************************************************************/ |
---|
| 109 | int get_best_core( unsigned int cxy_base, |
---|
| 110 | unsigned int level, |
---|
| 111 | unsigned int * cxy, |
---|
| 112 | unsigned int * lid ); |
---|
| 113 | |
---|
[444] | 114 | /*************************************************************************************** |
---|
[637] | 115 | * This function returns the value contained in the calling core cycles counter, |
---|
[444] | 116 | * taking into account a possible overflow on 32 bits architectures. |
---|
| 117 | *************************************************************************************** |
---|
| 118 | * @ cycle : [out] current cycle value. |
---|
| 119 | * @ return always 0. |
---|
| 120 | **************************************************************************************/ |
---|
| 121 | int get_cycle( unsigned long long * cycle ); |
---|
| 122 | |
---|
[589] | 123 | /*************************************************************************************** |
---|
| 124 | * This syscall allows the calling thread to specify the target cluster for |
---|
| 125 | * a subsequent fork(). It must be called for each fork(). |
---|
| 126 | *************************************************************************************** |
---|
| 127 | * @ cxy : [in] target cluster identifier. |
---|
| 128 | * @ return 0 if success / returns -1 if illegal cxy argument. |
---|
| 129 | **************************************************************************************/ |
---|
| 130 | int place_fork( unsigned int cxy ); |
---|
| 131 | |
---|
[444] | 132 | /*************************************************************************************** |
---|
[589] | 133 | * This syscall implements the operations related to User Thread Local Storage. |
---|
[445] | 134 | *************************************************************************************** |
---|
| 135 | * @ operation : UTLS operation type as defined in "shared_sycalls.h" file. |
---|
| 136 | * @ value : argument value for the UTLS_SET operation. |
---|
| 137 | * @ return value for the UTLS_GET and UTLS_GET_ERRNO / return -1 if failure. |
---|
| 138 | **************************************************************************************/ |
---|
| 139 | int utls( unsigned int operation, |
---|
| 140 | unsigned int value ); |
---|
| 141 | |
---|
| 142 | /*************************************************************************************** |
---|
[657] | 143 | * This syscall returns an 32 bits integer from the standard "stdin" stream. |
---|
| 144 | * The value is copied in buffer pointed by the <buf> argument |
---|
[580] | 145 | * Both decimal numbers and hexadecimal numbers (prefixed by 0x) are supported. |
---|
[445] | 146 | *************************************************************************************** |
---|
[657] | 147 | * @ buffer : pointer on buffer. |
---|
| 148 | * @ returns 0 if success / returns -1 if failure. |
---|
[445] | 149 | **************************************************************************************/ |
---|
[657] | 150 | int get_uint32( unsigned int * buffer ); |
---|
[445] | 151 | |
---|
[657] | 152 | /*************************************************************************************** |
---|
| 153 | * This syscall returns a character string from the standard "stdin" stream. |
---|
| 154 | * The string is copied in buffer pointed by the <string> argument. |
---|
[661] | 155 | * If the number of characters exceed the buffer length, all character are canceled |
---|
| 156 | * in both the buffer & the echoed string, and a new string capture start. |
---|
| 157 | * - Only printable characters c such as (0x20 <= c) and (c < 0x7f) are echoed |
---|
| 158 | * and copied in the target buffer. |
---|
| 159 | * - The LF (0x0A) character signaling the end of string is replaced by an extra |
---|
| 160 | * NUL (0x00) character in the target buffer. |
---|
| 161 | * - the DEL (0x7F), BS (0x08) control characters are taken into account, and can be |
---|
| 162 | * used to correct both the buffer content and the echoed string. |
---|
[657] | 163 | *************************************************************************************** |
---|
| 164 | * @ string : pointer on the string buffer. |
---|
[661] | 165 | * @ maxlen : max number of bytes in string buffer, including the terminating NUL. |
---|
| 166 | * @ returns string length (including NUL) if success / returns -1 if failure. |
---|
[657] | 167 | **************************************************************************************/ |
---|
| 168 | int get_string( char * string, |
---|
| 169 | int maxlen ); |
---|
[445] | 170 | |
---|
[657] | 171 | |
---|
[445] | 172 | /***************** Non standard (ALMOS-MKH specific) debug functions ******************/ |
---|
| 173 | |
---|
[641] | 174 | /*************************************************************************************** |
---|
| 175 | * This syscall copies in the user structure defined by the <info> argument the values |
---|
| 176 | * registered in the calling thread "thread_info_t" kernel structure. |
---|
| 177 | ****************************************************************************************** |
---|
| 178 | * @ info : [out] pointer on thread_info_t structure in user space. |
---|
| 179 | * @ return 0 if success / return -1 if illegal argument. |
---|
| 180 | *****************************************************************************************/ |
---|
| 181 | int get_thread_info( thread_info_t * info ); |
---|
[445] | 182 | |
---|
| 183 | /*************************************************************************************** |
---|
[589] | 184 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[444] | 185 | * the thread / process / core identifiers, the current cycle, plus a user defined |
---|
| 186 | * message as specified by the <string> argument. |
---|
| 187 | *************************************************************************************** |
---|
| 188 | * @ string : [in] user defined message. |
---|
| 189 | **************************************************************************************/ |
---|
| 190 | void display_string( char * string ); |
---|
| 191 | |
---|
| 192 | /*************************************************************************************** |
---|
| 193 | * This debug function displays on the kernel terminal TXT0 |
---|
[610] | 194 | * the state of the VMM for the process <pid> in cluster <cxy>. |
---|
[444] | 195 | * It can be called by any thread running in any cluster. |
---|
| 196 | *************************************************************************************** |
---|
[625] | 197 | * @ cxy : [in] target cluster identifier. |
---|
[444] | 198 | * @ pid : [in] process identifier. |
---|
[640] | 199 | * @ mapping : [in] detailed mapping if non zero. |
---|
[444] | 200 | * @ return 0 if success / return -1 if illegal argument. |
---|
| 201 | **************************************************************************************/ |
---|
[640] | 202 | int display_vmm( unsigned int cxy, |
---|
| 203 | unsigned int pid, |
---|
| 204 | unsigned int mapping ); |
---|
[444] | 205 | |
---|
| 206 | /*************************************************************************************** |
---|
[589] | 207 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[444] | 208 | * the state of the core scheduler identified by the <cxy> and <lid> arguments. |
---|
| 209 | * It can be called by any thread running in any cluster. |
---|
| 210 | *************************************************************************************** |
---|
| 211 | * @ cxy : [in] target cluster identifier. |
---|
| 212 | * @ lid : [in] target core local index. |
---|
| 213 | * @ return 0 if success / return -1 if illegal arguments. |
---|
| 214 | **************************************************************************************/ |
---|
| 215 | int display_sched( unsigned int cxy, |
---|
| 216 | unsigned int lid ); |
---|
| 217 | |
---|
| 218 | /*************************************************************************************** |
---|
[589] | 219 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[444] | 220 | * the list of process registered in a given cluster identified by the <cxy> argument. |
---|
[589] | 221 | * Only the owned processes are displayed when the <owned> argument is non zero. |
---|
[444] | 222 | * It can be called by any thread running in any cluster. |
---|
| 223 | *************************************************************************************** |
---|
| 224 | * @ cxy : [in] target cluster identifier. |
---|
[589] | 225 | * @ owned : [in] only owned processes if non zero. |
---|
[444] | 226 | * @ return 0 if success / return -1 if illegal argument. |
---|
| 227 | **************************************************************************************/ |
---|
[589] | 228 | int display_cluster_processes( unsigned int cxy, |
---|
| 229 | unsigned int owned ); |
---|
[444] | 230 | |
---|
| 231 | /*************************************************************************************** |
---|
[589] | 232 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[445] | 233 | * the list of processes attached to a given TXT channel. |
---|
| 234 | * It can be called by any thread running in any cluster. |
---|
| 235 | *************************************************************************************** |
---|
| 236 | * @ txt_id : [in] TXT terminal indes. |
---|
| 237 | * @ return 0 if success / return -1 if illegal argument. |
---|
| 238 | **************************************************************************************/ |
---|
| 239 | int display_txt_processes( unsigned int txt_id ); |
---|
| 240 | |
---|
| 241 | /*************************************************************************************** |
---|
[589] | 242 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[597] | 243 | * the set of busylocks hold by a thread identified by the <pid> and <trdid> arguments. |
---|
| 244 | * It can be called by any thread running in any cluster. |
---|
| 245 | *************************************************************************************** |
---|
| 246 | * @ pid : [in] process identifier. |
---|
| 247 | * @ trdid : [in] thread identifier. |
---|
| 248 | * @ return 0 if success / return -1 if illegal arguments. |
---|
| 249 | **************************************************************************************/ |
---|
| 250 | int display_busylocks( unsigned int pid, |
---|
| 251 | unsigned int trdid ); |
---|
| 252 | |
---|
| 253 | /*************************************************************************************** |
---|
| 254 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[444] | 255 | * the list of channel devices available in the architecture. |
---|
| 256 | * It can be called by any thread running in any cluster. |
---|
| 257 | *************************************************************************************** |
---|
| 258 | * @ return always 0. |
---|
| 259 | **************************************************************************************/ |
---|
[478] | 260 | int display_chdev( void ); |
---|
[444] | 261 | |
---|
| 262 | /*************************************************************************************** |
---|
[589] | 263 | * This debug syscall displays on the kernel terminal TXT0 |
---|
[444] | 264 | * the list of channel device or pseudo-files registered in the VFS cache. |
---|
| 265 | * It can be called by any thread running in any cluster. |
---|
| 266 | *************************************************************************************** |
---|
| 267 | * @ return always 0. |
---|
| 268 | **************************************************************************************/ |
---|
[478] | 269 | int display_vfs( void ); |
---|
[444] | 270 | |
---|
| 271 | /*************************************************************************************** |
---|
[589] | 272 | * This debug syscall displays on the kernel terminal TXT0 the current DQDT state. |
---|
[444] | 273 | * It can be called by any thread running in any cluster. |
---|
| 274 | *************************************************************************************** |
---|
| 275 | * @ return always 0. |
---|
| 276 | **************************************************************************************/ |
---|
[478] | 277 | int display_dqdt( void ); |
---|
[444] | 278 | |
---|
[611] | 279 | /*************************************************************************************** |
---|
| 280 | * This debug syscall displays on the kernel terminal TXT0 the content of a given |
---|
| 281 | * page of a given VFS mapper. |
---|
| 282 | * It can be called by any thread running in any cluster. |
---|
| 283 | *************************************************************************************** |
---|
| 284 | * @ path : pathname identifying the file/directory in VFS. |
---|
| 285 | * @ page_id : page index in file. |
---|
| 286 | * @ nbytes : number of bytes to display. |
---|
| 287 | * @ return 0 if success / return -1 if file or page not found. |
---|
| 288 | **************************************************************************************/ |
---|
| 289 | int display_mapper( char * path, |
---|
| 290 | unsigned int page_id, |
---|
| 291 | unsigned int nbytes); |
---|
| 292 | |
---|
[623] | 293 | /*************************************************************************************** |
---|
| 294 | * This debug syscall displays on the kernel terminal TXT0 |
---|
| 295 | * the state of the barrier used by the process identified by the <pid> argument. |
---|
| 296 | * It can be called by any thread running in any cluster. |
---|
| 297 | *************************************************************************************** |
---|
| 298 | * @ pid : [in] process identifier. |
---|
| 299 | * @ return 0 if success / return -1 if illegal arguments. |
---|
| 300 | **************************************************************************************/ |
---|
| 301 | int display_barrier( unsigned int pid ); |
---|
| 302 | |
---|
[626] | 303 | /*************************************************************************************** |
---|
[657] | 304 | * This debug syscall displays on the kernel terminal TXT0 the content of <nb_slots> |
---|
| 305 | * of the FAT mapper, starting from <min_slot>. |
---|
| 306 | * If <nb_slots> is zero, it displays the state of the FATFS context in cluster |
---|
| 307 | * defined by the <min_slot> argument interpreted as a cluster identifier. |
---|
[626] | 308 | * It can be called by any thread running in any cluster. |
---|
| 309 | *************************************************************************************** |
---|
[657] | 310 | * @ min_slot : first slot in page |
---|
| 311 | * @ nb_slots : number of slots to display. |
---|
[626] | 312 | * @ return 0 if success / return -1 if page not found. |
---|
| 313 | **************************************************************************************/ |
---|
[657] | 314 | int display_fat( unsigned int min_slot, |
---|
| 315 | unsigned int nb_slots ); |
---|
[626] | 316 | |
---|
[661] | 317 | /*************************************************************************************** |
---|
| 318 | * This debug syscall displays on the kernel terminal the current state of |
---|
| 319 | * a socket identified by the <pid> and <fdid> arguments. |
---|
| 320 | * It can be called by any thread running in any cluster. |
---|
| 321 | *************************************************************************************** |
---|
| 322 | * @ pid : target process identifier. |
---|
| 323 | * @ fdid : file descriptor identifying the target socket. |
---|
| 324 | * @ return 0 if success / return -1 if socket not found. |
---|
| 325 | **************************************************************************************/ |
---|
| 326 | int display_socket( unsigned int pid, |
---|
| 327 | unsigned int fdid ); |
---|
| 328 | |
---|
[444] | 329 | /***************************************************************************************** |
---|
[589] | 330 | * This debug syscall is used to activate / desactivate the context switches trace |
---|
[444] | 331 | * for a core identified by the <cxy> and <lid> arguments. |
---|
| 332 | * It can be called by any thread running in any cluster. |
---|
| 333 | ***************************************************************************************** |
---|
| 334 | * @ active : activate trace if non zero / desactivate if zero. |
---|
| 335 | * @ cxy : cluster identifier. |
---|
| 336 | * @ lid : core local index. |
---|
| 337 | * @ returns O if success / returns -1 if illegal arguments. |
---|
| 338 | ****************************************************************************************/ |
---|
| 339 | int trace( unsigned int active, |
---|
[450] | 340 | unsigned int cxy, |
---|
| 341 | unsigned int lid ); |
---|
[444] | 342 | |
---|
[445] | 343 | /**************************************************************************************** |
---|
[589] | 344 | * This syscall implements an user-level interactive debugger that can be |
---|
[445] | 345 | * introduced in any user application to display various kernel distributed structures. |
---|
| 346 | ***************************************************************************************/ |
---|
[478] | 347 | void idbg( void ); |
---|
[444] | 348 | |
---|
| 349 | |
---|
[445] | 350 | /****************** Non standard (ALMOS-MKH specific) malloc operations ***************/ |
---|
[444] | 351 | |
---|
[445] | 352 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[444] | 353 | // General principles: |
---|
| 354 | // - In user space the HEAP zone spread between the ELF zone and the STACK zone, |
---|
| 355 | // as defined in the kernel_config.h file. |
---|
| 356 | // - The malloc library uses the mmap() syscall to create - on demand - |
---|
| 357 | // one vseg in a given cluster. The size of this vseg is defined below |
---|
| 358 | // by the MALLOC_LOCAL_STORE_SIZE parameter. |
---|
| 359 | // - For a standard malloc(), the target cluster is the cluster containing |
---|
| 360 | // the core running the client thread. |
---|
| 361 | // - For a remote_malloc(), the target cluster is explicitely defined |
---|
| 362 | // by the argument. |
---|
| 363 | // - In each cluster, the available storage in virtual space is handled by a |
---|
| 364 | // local allocator using the buddy algorithm. |
---|
| 365 | // |
---|
| 366 | // TODO : In this first implementation one single - fixed size - vseg |
---|
| 367 | // is allocated on demand in each cluster. |
---|
| 368 | // We should introduce the possibility to dynamically allocate |
---|
| 369 | // several vsegs in each cluster, using several mmap when required. |
---|
[445] | 370 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[444] | 371 | // Free blocks organisation in each cluster : |
---|
| 372 | // - All free blocks have a size that is a power of 2, larger or equal |
---|
| 373 | // to MALLOC_MIN_BLOCK_SIZE (typically 64 bytes). |
---|
| 374 | // - All free blocks are aligned. |
---|
| 375 | // - They are pre-classed in an array of linked lists, where all blocks in a |
---|
| 376 | // given list have the same size. |
---|
| 377 | // - The NEXT pointer implementing those linked lists is written |
---|
| 378 | // in the first bytes of the block itself, using the unsigned int type. |
---|
| 379 | // - The pointers on the first free block for each size are stored in an |
---|
| 380 | // array of pointers free[32] in the storage(x,y) descriptor. |
---|
[445] | 381 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[444] | 382 | // Allocation policy: |
---|
| 383 | // - The block size required by the user can be any value, but the allocated |
---|
| 384 | // block size can be larger than the requested size: |
---|
| 385 | // - The allocator computes actual_size, that is the smallest power of 2 |
---|
| 386 | // value larger or equal to the requested size AND larger or equal to |
---|
| 387 | // MALLOC_MIN_BLOCK_SIZE. |
---|
| 388 | // - It pop the linked list of free blocks corresponding to actual_size, |
---|
| 389 | // and returns the block B if the list[actual_size] is not empty. |
---|
| 390 | // - If the list[actual_size] is empty, it pop the list[actual_size * 2]. |
---|
| 391 | // If a block B' is found, it breaks this block in 2 B/2 blocks, returns |
---|
| 392 | // the first B/2 block and push the other B/2 block into list[actual_size]. |
---|
| 393 | // - If the list[actual_size * 2] is empty, it pop the list[actual_size * 4]. |
---|
| 394 | // If a block B is found, it break this block in 3 blocks B/4, B/4 and B/2, |
---|
| 395 | // returns the first B/4 block, push the other blocks B/4 and B/2 into |
---|
| 396 | // the proper lists. etc... |
---|
| 397 | // - If no block satisfying the request is available it returns a failure |
---|
| 398 | // (NULL pointer). |
---|
| 399 | // - This allocation policy has the nice following property: |
---|
| 400 | // If the vseg is aligned (the vseg base is a multiple of the |
---|
| 401 | // vseg size), all allocated blocks are aligned on the actual_size. |
---|
[445] | 402 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[444] | 403 | // Free policy: |
---|
| 404 | // - Each allocated block is registered in an alloc[] array of unsigned char. |
---|
| 405 | // - This registration is required by the free() operation, because the size |
---|
| 406 | // of the allocated block must be obtained from the base address of the block. |
---|
| 407 | // - The number of entries in this array is equal to the max number |
---|
| 408 | // of allocated block : MALLOC_LOCAL_STORE_SIZE / MALLOC_MIN_BLOCK_SIZE. |
---|
| 409 | // - For each allocated block, the value registered in the alloc[] array |
---|
| 410 | // is log2( size_of_allocated_block ). |
---|
| 411 | // - The index in this array is computed from the allocated block base address: |
---|
| 412 | // index = (block_base - vseg_base) / MALLOC_MIN_BLOCK_SIZE |
---|
| 413 | // - The alloc[] array is stored at the end of heap segment. This consume |
---|
| 414 | // (1 / MALLOC_MIN_BLOCK_SIZE) of the total storage capacity. |
---|
[445] | 415 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[444] | 416 | |
---|
| 417 | |
---|
| 418 | #define MALLOC_INITIALIZED 0xBABEF00D // magic number when initialised |
---|
| 419 | #define MALLOC_MIN_BLOCK_SIZE 0x40 // 64 bytes |
---|
[643] | 420 | #define MALLOC_LOCAL_STORE_SIZE 0x2000000 // 32 Mbytes |
---|
[444] | 421 | #define MALLOC_MAX_CLUSTERS 0x100 // 256 clusters |
---|
| 422 | |
---|
[445] | 423 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 424 | // store(x,y) descriptor (one per cluster) |
---|
| 425 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[444] | 426 | |
---|
| 427 | typedef struct malloc_store_s |
---|
| 428 | { |
---|
[445] | 429 | pthread_mutex_t mutex; // lock protecting exclusive access to local heap |
---|
[444] | 430 | unsigned int initialized; // initialised when value == MALLOC_INITIALIZED |
---|
| 431 | unsigned int cxy; // cluster identifier |
---|
| 432 | unsigned int store_base; // store base address |
---|
| 433 | unsigned int store_size; // store size (bytes) |
---|
| 434 | unsigned int alloc_base; // alloc[] array base address |
---|
| 435 | unsigned int alloc_size; // alloc[] array size (bytes) |
---|
| 436 | unsigned int free[32]; // array of addresses of first free block |
---|
| 437 | } |
---|
| 438 | malloc_store_t; |
---|
| 439 | |
---|
| 440 | /***************************************************************************************** |
---|
| 441 | * This function allocates <size> bytes of memory in user space, and returns a pointer |
---|
| 442 | * to the allocated buffer. The pysical memory is allocated from store located in |
---|
| 443 | * cluster identified by the <cxy> argument. |
---|
| 444 | ***************************************************************************************** |
---|
| 445 | * @ size : number of requested bytes. |
---|
| 446 | * @ cxy : target cluster identifier. |
---|
| 447 | * @ returns a pointer on the allocated buffer if success / returns NULL if failure |
---|
| 448 | ****************************************************************************************/ |
---|
| 449 | void * remote_malloc( unsigned int size, |
---|
| 450 | unsigned int cxy ); |
---|
| 451 | |
---|
| 452 | /***************************************************************************************** |
---|
| 453 | * This function releases the memory buffer identified by the <ptr> argument, |
---|
| 454 | * to the store identified by the <cxy> argument. |
---|
[650] | 455 | * It does nothing, but displays an error message, if the ptr is illegal. |
---|
[444] | 456 | ***************************************************************************************** |
---|
| 457 | * @ ptr : pointer on the released buffer. |
---|
| 458 | * @ cxy : target cluster identifier. |
---|
| 459 | ****************************************************************************************/ |
---|
| 460 | void remote_free( void * ptr, |
---|
| 461 | unsigned int cxy ); |
---|
| 462 | |
---|
| 463 | /***************************************************************************************** |
---|
| 464 | * This function releases the memory buffer identified by the <ptr> argument, |
---|
| 465 | * to the store located in cluster identified by the <cxy> argument, and allocates |
---|
| 466 | * a new buffer containing <size> bytes from this store. |
---|
| 467 | * The content of the old buffer is copied to the new buffer, up to <size> bytes. |
---|
| 468 | * It displays an error message, but does nothing if the ptr is illegal. |
---|
| 469 | ***************************************************************************************** |
---|
| 470 | * @ ptr : pointer on the released buffer. |
---|
| 471 | * @ size : new buffer requested size (bytes). |
---|
| 472 | * @ cxy : target cluster identifier. |
---|
| 473 | * @ return a pointer on allocated buffer if success / return NULL if failure |
---|
| 474 | ****************************************************************************************/ |
---|
| 475 | void * remote_realloc( void * ptr, |
---|
| 476 | unsigned int size, |
---|
| 477 | unsigned int cxy ); |
---|
[445] | 478 | |
---|
[444] | 479 | /***************************************************************************************** |
---|
| 480 | * This function allocates enough space for <count> objects that are <size> bytes |
---|
| 481 | * of memory each from the store located in cluster identied by the <cxy> argument. |
---|
| 482 | * The allocated memory is filled with bytes of value zero. |
---|
| 483 | ***************************************************************************************** |
---|
| 484 | * @ count : number of requested objects. |
---|
| 485 | * @ size : number of bytes per object. |
---|
| 486 | * @ cxy : target cluster identifier. |
---|
| 487 | * @ returns a pointer on allocated buffer if success / returns NULL if failure |
---|
| 488 | ****************************************************************************************/ |
---|
| 489 | void * remote_calloc( unsigned int count, |
---|
| 490 | unsigned int size, |
---|
| 491 | unsigned int cxy ); |
---|
| 492 | |
---|
[637] | 493 | /********* Non standard (ALMOS-MKH specific) pthread_parallel_create() syscall *********/ |
---|
| 494 | |
---|
| 495 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[650] | 496 | // This syscall can be used to parallelize the creation, and the termination |
---|
| 497 | // of a parallel multi-threaded application. |
---|
| 498 | // It removes in the main thread the sequencial loop that creates the N working threads |
---|
| 499 | // (N pthread_create() ), and removes also the sequencial loop that waits completion |
---|
| 500 | // of these N working threads (N pthread_join() ). |
---|
| 501 | // It creates one <work> thread (in detached mode) per core in the target architecture. |
---|
| 502 | // Each <work> thread is identified by a continuous [tid] index. |
---|
| 503 | // For a regular architecture, defined by the [x_size , y_size , ncores] parameters, |
---|
| 504 | // the number of working threads can be the simply computed as (x_size * y_size * ncores), |
---|
| 505 | // and the coordinates[x,y,lid] of the core running the thread[tid] cand be directly |
---|
| 506 | // derived from the [tid] value with the following relations: |
---|
| 507 | // . cid = (x * y_size) + y |
---|
| 508 | // . tid = (cid * ncores ) + lid |
---|
| 509 | // . lid = tid % ncores |
---|
| 510 | // . cid = tid / ncores |
---|
| 511 | // . y = cid % y_size |
---|
| 512 | // . x = cid / y_size |
---|
| 513 | // The pthread_parallel_create() function returns only when all <work> threads completed |
---|
[637] | 514 | // (successfully or not). |
---|
| 515 | // |
---|
[650] | 516 | // WARNING : The function executed by the working thread is application specific, |
---|
| 517 | // but the structure defining the arguments passed to this function is imposed. |
---|
| 518 | // The "pthread_parallel_work_args_t" structure is defined below, and contains |
---|
| 519 | // two fields: the tid value, and a pointer on a pthread_barrier_t. |
---|
| 520 | // This barrier must be used by each working thread to signal completion before exit. |
---|
| 521 | // The global variables implementing these stuctures for each working thread |
---|
| 522 | // are allocated and initialised by the pthread_parallel_create() function. |
---|
[637] | 523 | // |
---|
[650] | 524 | // Implementation note: the pthread_parallel_create()a function creates a distributed |
---|
| 525 | // quad-tree (DQT) of <build> threads covering all cores required to execute the parallel |
---|
| 526 | // application. This quad tree is entirely defined by the root_level parameter. |
---|
[637] | 527 | // Depending on the hardware topology, this DQT can be truncated, (i.e. some |
---|
| 528 | // parent nodes can have less than 4 chidren), if (x_size != y_size), or if one size |
---|
[650] | 529 | // is not a power of 2. Each <build> thread is identified by two indexes [cid][level]. |
---|
| 530 | // Each <build> thread makes the following tasks: |
---|
[637] | 531 | // 1) It calls the pthread_create() function to create up to 4 children threads, that |
---|
[650] | 532 | // are <work> threads when (level == 0), or <build> threads, when (level > 0). |
---|
| 533 | // 2) It allocates and initializes the barrier, used to block the parent thread until |
---|
| 534 | // children completion. |
---|
[637] | 535 | // 3) It calls the pthread_barrier_wait( self ) to wait until all children threads |
---|
| 536 | // completed (successfully or not). |
---|
| 537 | // 4) It calls the pthread_barrier_wait( parent ) to unblock the parent thread. |
---|
| 538 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 539 | |
---|
| 540 | /***************************************************************************************** |
---|
[650] | 541 | * structure defining the arguments for the <build> thread function |
---|
| 542 | ****************************************************************************************/ |
---|
| 543 | typedef struct pthread_parallel_build_args_s |
---|
| 544 | { |
---|
| 545 | unsigned char cid; // this <build> thread cluster index |
---|
| 546 | unsigned char level; // this <build> thread level in quad-tree |
---|
| 547 | unsigned char parent_cid; // parent <build> thread cluster index |
---|
| 548 | pthread_barrier_t * parent_barrier; // pointer on parent <build> thread barrier |
---|
| 549 | unsigned char root_level; // quad-tree root level |
---|
| 550 | void * work_func; // pointer on working thread function |
---|
| 551 | unsigned int x_size; // platform global parameter |
---|
| 552 | unsigned int y_size; // platform global parameter |
---|
| 553 | unsigned int ncores; // platform global parameter |
---|
| 554 | unsigned int error; // return value : 0 if success |
---|
| 555 | } |
---|
| 556 | pthread_parallel_build_args_t; |
---|
| 557 | |
---|
| 558 | /***************************************************************************************** |
---|
| 559 | * structure defining the arguments for the <work> thread function |
---|
| 560 | ****************************************************************************************/ |
---|
| 561 | typedef struct pthread_parallel_work_args_s |
---|
| 562 | { |
---|
| 563 | unsigned int tid; // thread identifier |
---|
| 564 | pthread_barrier_t * barrier; // to signal completion |
---|
| 565 | } |
---|
| 566 | pthread_parallel_work_args_t; |
---|
| 567 | |
---|
| 568 | /***************************************************************************************** |
---|
| 569 | * This blocking function creates N working threads identified by the [tid] continuous |
---|
| 570 | * index, that execute the code defined by the <work_func> argument, and returns only |
---|
| 571 | * when all working threads completed. |
---|
| 572 | * The number N of created threads is entirely defined by the <root_level> argument, |
---|
| 573 | * that defines an abstract quad-tree, with a square base : root_level in [0,1,2,3,4], |
---|
[637] | 574 | * side in [1,2,4,8,16], nclusters in [1,4,16,64,256]. This base is called macro_cluster. |
---|
[650] | 575 | * A working thread is created on all cores contained in this abstract macro-cluster. |
---|
[637] | 576 | * The actual number of physical clusters containing cores can be smaller than the number |
---|
[650] | 577 | * of clusters covered by the abstract quad tree. |
---|
| 578 | * All threads execute the same <work_func> function, on different arguments, that are |
---|
| 579 | * specified as an array of structures pthread_parallel_work_args_t, allocated and |
---|
| 580 | * initialised by this function. |
---|
[637] | 581 | ***************************************************************************************** |
---|
| 582 | * @ root_level : [in] DQT root level in [0,1,2,3,4]. |
---|
| 583 | * @ work_func : [in] pointer on start function. |
---|
| 584 | * @ return 0 if success / return -1 if failure. |
---|
| 585 | ****************************************************************************************/ |
---|
[650] | 586 | int pthread_parallel_create( unsigned int root_level, |
---|
| 587 | void * work_func ); |
---|
[637] | 588 | |
---|
[650] | 589 | |
---|
| 590 | |
---|
[643] | 591 | /********* Non standard (ALMOS-MKH specific) Frame Buffer access syscalls *************/ |
---|
| 592 | |
---|
| 593 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 594 | // The following system calls can be used to access the SoCLib Frame Buffer, that |
---|
| 595 | // is a very simple graphic controller, that is seen by the software as a single |
---|
| 596 | // buffer of <height> lines of <width> pixels. |
---|
| 597 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 598 | |
---|
| 599 | /***************************************************************************************** |
---|
[657] | 600 | * This function returns in the <width>, <height> and <type> arguments |
---|
| 601 | * the implementation specific frame buffer features. |
---|
[643] | 602 | ***************************************************************************************** |
---|
| 603 | * @ width : [out] number of pixels per line. |
---|
| 604 | * @ height : [out] number of lines. |
---|
| 605 | * @ type : [out] pixel encoding type. |
---|
[657] | 606 | * @ returns 0 if success / returns -1 if not found. |
---|
[643] | 607 | ****************************************************************************************/ |
---|
[647] | 608 | int fbf_get_config( unsigned int * width, |
---|
| 609 | unsigned int * height, |
---|
| 610 | unsigned int * type ); |
---|
[643] | 611 | |
---|
| 612 | /***************************************************************************************** |
---|
[657] | 613 | * This function creates a new window defined by the <l_zero>, <p_zero>, <nlines>, |
---|
| 614 | * and <npixels> arguments for the calling process, and register the process PID, the |
---|
| 615 | * allocated <wid>, and the window size and coordinates in the FBF device descriptor. |
---|
| 616 | * It returns in the output argument <buffer> the pointer on the buffer associated to |
---|
| 617 | * the window, mapped in user space. |
---|
| 618 | * Warning : Both pixel [p_zero,l_zero] and pixel [p_zero+npixels-1,l_zero+nlines-1] |
---|
| 619 | * must be contained in the frame buffer. |
---|
[643] | 620 | ***************************************************************************************** |
---|
[657] | 621 | * @ l_zero : [in] first line index in FBF coordinates system. |
---|
| 622 | * @ p_zero : [in] first pixel index in FBF coordinates system. |
---|
| 623 | * @ nlines : [in] number of lines in the window. |
---|
| 624 | * @ npixels : [in] number of pixels per line in the window. |
---|
| 625 | * @ buffer : [out] pointer on |
---|
| 626 | * @ returns <wid> if success / returns -1 if illegal size or coordinates. |
---|
[643] | 627 | ****************************************************************************************/ |
---|
[657] | 628 | int fbf_create_window( unsigned int l_zero, |
---|
| 629 | unsigned int p_zero, |
---|
| 630 | unsigned int nlines, |
---|
| 631 | unsigned int npixels, |
---|
| 632 | void ** buffer ); |
---|
[643] | 633 | |
---|
| 634 | /***************************************************************************************** |
---|
[657] | 635 | * This function deletes an existing window, identified by the <wid> argument. |
---|
| 636 | * The calling process must be the window owner. |
---|
[643] | 637 | ***************************************************************************************** |
---|
[657] | 638 | * @ wid : window identifier. |
---|
| 639 | * @ returns 0 if success / returns -1 if not found or process not owner. |
---|
| 640 | ****************************************************************************************/ |
---|
| 641 | int fbf_delete_window( unsigned int wid ); |
---|
| 642 | |
---|
| 643 | /***************************************************************************************** |
---|
| 644 | * This function refreshes in FBF all lines of a window identified by the <wid> argument, |
---|
| 645 | * when the line index is in the interval [line_first,line_last[. |
---|
| 646 | * It scans all registered windows to take into account the windows overlap. |
---|
| 647 | ***************************************************************************************** |
---|
| 648 | * @ wid : window identifier. |
---|
| 649 | * @ line_first : first line index. |
---|
| 650 | * @ line_last : last line index (excluded). |
---|
| 651 | * @ returns 0 if success / returns -1 if illegal argument. |
---|
| 652 | ****************************************************************************************/ |
---|
| 653 | int fbf_refresh_window( unsigned int wid, |
---|
| 654 | unsigned int line_first, |
---|
| 655 | unsigned int line_last ); |
---|
| 656 | |
---|
| 657 | /***************************************************************************************** |
---|
| 658 | * This function changes the size of a window identified by the <wid> argument as defined |
---|
| 659 | * by the <width> and height> arguments. The calling process must be the window owner. |
---|
| 660 | * WARNING : Both pixel [p_zero,l_zero] and pixel [p_zero+width-1,l_zero+height-1] |
---|
| 661 | * must be contained in the frame buffer. |
---|
| 662 | ***************************************************************************************** |
---|
| 663 | * @ wid : window identifier. |
---|
| 664 | * @ width : number of pixels in line (one byte per pixel). |
---|
| 665 | * @ height : number of lines in window. |
---|
| 666 | * @ returns 0 if success / returns -1 if illegal arguments. |
---|
| 667 | ****************************************************************************************/ |
---|
| 668 | int fbf_resize_window( unsigned int wid, |
---|
| 669 | unsigned int width, |
---|
| 670 | unsigned int height ); |
---|
| 671 | |
---|
| 672 | /***************************************************************************************** |
---|
| 673 | * This function changes the <l_zero> & <p_zero> coordinates of a window identified |
---|
| 674 | * by the <wid> argument. The calling process must be the window owner. |
---|
| 675 | * WARNING : Both pixel [p_zero,l_zero] and pixel [p_zero+width-1,l_zero+height-1] |
---|
| 676 | * must be contained in the frame buffer. |
---|
| 677 | ***************************************************************************************** |
---|
| 678 | * @ wid : window identifier. |
---|
| 679 | * @ l_zero : new first line index in FBF. |
---|
| 680 | * @ p_zero : new first pixel index in FBF. |
---|
| 681 | * @ returns 0 if success / returns -1 if illegal arguments. |
---|
| 682 | ****************************************************************************************/ |
---|
| 683 | int fbf_move_window( unsigned int wid, |
---|
| 684 | unsigned int l_zero, |
---|
| 685 | unsigned int p_zero ); |
---|
| 686 | |
---|
| 687 | /***************************************************************************************** |
---|
| 688 | * This syscall is deprecated (january 2020 AG) |
---|
| 689 | * This blocking function moves <length> pixels from the user buffer defined by <buffer> |
---|
| 690 | * argument to the frame buffer, starting at <offset> in FBF. |
---|
| 691 | ***************************************************************************************** |
---|
[643] | 692 | * @ buffer : pointer on buffer in user space. |
---|
| 693 | * @ length : number of pixels (one byte per pixel). |
---|
[657] | 694 | * @ offset : first pixel index in window. |
---|
| 695 | * @ returns 0 if success / returns -1 if illegal arguments. |
---|
[643] | 696 | ****************************************************************************************/ |
---|
[647] | 697 | int fbf_write( void * buffer, |
---|
| 698 | unsigned int length, |
---|
| 699 | unsigned int offset ); |
---|
[643] | 700 | |
---|
[657] | 701 | /***************************************************************************************** |
---|
| 702 | * This syscall is deprecated (january 2020 AG) |
---|
| 703 | * This blocking function moves <length> pixels from the frame buffer, starting at |
---|
| 704 | * <offset> in FBF, to the user buffer defined by <buffer> argument. |
---|
| 705 | ***************************************************************************************** |
---|
| 706 | * @ buffer : pointer on buffer in user space. |
---|
| 707 | * @ length : number of pixels (one byte per pixel). |
---|
| 708 | * @ offset : first pixel index in window. |
---|
| 709 | * @ returns 0 if success / returns -1 if illegal arguments. |
---|
| 710 | ****************************************************************************************/ |
---|
| 711 | int fbf_read( void * buffer, |
---|
| 712 | unsigned int length, |
---|
| 713 | unsigned int offset ); |
---|
| 714 | |
---|
| 715 | |
---|
[444] | 716 | #endif /* _LIBALMOSMKH_H_ */ |
---|
| 717 | |
---|