source: trunk/libs/libalmosmkh/almosmkh.h @ 605

Last change on this file since 605 was 597, checked in by alain, 6 years ago

Rewrite the mmap() and stat() syscalls to implement the cat command in ksh.

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