source: trunk/kernel/kern/cluster.c @ 424

Last change on this file since 424 was 416, checked in by alain, 7 years ago

Improve sys_exec.

File size: 17.0 KB
RevLine 
[1]1/*
2 * cluster.c - Cluster-Manager related operations
[19]3 *
[1]4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Mohamed Lamine Karaoui (2015)
[23]6 *         Alain Greiner (2016,2017)
[1]7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH..
11 *
12 * ALMOS-MKH. is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH. is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
[14]26#include <kernel_config.h>
[1]27#include <hal_types.h>
28#include <hal_atomic.h>
29#include <hal_special.h>
[50]30#include <hal_ppm.h>
[407]31#include <remote_fifo.h>
[1]32#include <printk.h>
33#include <errno.h>
34#include <spinlock.h>
35#include <core.h>
36#include <scheduler.h>
37#include <list.h>
38#include <cluster.h>
39#include <boot_info.h>
40#include <bits.h>
41#include <ppm.h>
42#include <thread.h>
43#include <kmem.h>
44#include <process.h>
45#include <dqdt.h>
46
[408]47/////////////////////////////////////////////////////////////////////////////////////
[1]48// Extern global variables
[408]49/////////////////////////////////////////////////////////////////////////////////////
[1]50
[23]51extern process_t process_zero;     // allocated in kernel_init.c file
[1]52
53
54/////////////////////////////////////////////////
55error_t cluster_init( struct boot_info_s * info )
56{
[50]57    error_t     error;
[1]58    lpid_t      lpid;     // local process_index
59    lid_t       lid;      // local core index
60
61        cluster_t * cluster = LOCAL_CLUSTER;
62
63    // initialize cluster global parameters
[19]64        cluster->paddr_width     = info->paddr_width;
[1]65        cluster->x_width         = info->x_width;
66        cluster->y_width         = info->y_width;
67        cluster->x_size          = info->x_size;
68        cluster->y_size          = info->y_size;
69        cluster->io_cxy          = info->io_cxy;
70
71    // initialize cluster local parameters
72        cluster->cores_nr        = info->cores_nr;
73
[19]74    // initialize the lock protecting the embedded kcm allocator
[1]75        spinlock_init( &cluster->kcm_lock );
76
[407]77cluster_dmsg("\n[DBG] %s for cluster %x enters\n",
78__FUNCTION__ , local_cxy );
[50]79
[19]80    // initialises DQDT
81    cluster->dqdt_root_level = dqdt_init( info->x_size,
82                                          info->y_size,
[1]83                                          info->y_width );
84    cluster->threads_var = 0;
85    cluster->pages_var   = 0;
86
87    // initialises embedded PPM
[50]88        error = hal_ppm_init( info );
[1]89
[50]90    if( error )
91    {
92        printk("\n[ERROR] in %s : cannot initialize PPM in cluster %x\n",
93               __FUNCTION__ , local_cxy );
94        return ENOMEM;
95    }
96
[407]97cluster_dmsg("\n[DBG] %s : PPM initialized in cluster %x at cycle %d\n",
98__FUNCTION__ , local_cxy , hal_get_cycles() );
[50]99
[1]100    // initialises embedded KHM
101        khm_init( &cluster->khm );
[19]102
[407]103    cluster_dmsg("\n[DBG] %s : KHM initialized in cluster %x at cycle %d\n",
[101]104                 __FUNCTION__ , local_cxy , hal_get_cycles() );
[50]105
[19]106    // initialises embedded KCM
[5]107        kcm_init( &cluster->kcm , KMEM_KCM );
[1]108
[407]109    cluster_dmsg("\n[DBG] %s : KCM initialized in cluster %x at cycle %d\n",
[101]110                 __FUNCTION__ , local_cxy , hal_get_cycles() );
[50]111
[296]112    // initialises all cores descriptors
[1]113        for( lid = 0 ; lid < cluster->cores_nr; lid++ )
114        {
115                core_init( &cluster->core_tbl[lid],    // target core descriptor
116                       lid,                        // local core index
117                       info->core[lid].gid );      // gid from boot_info_t
118        }
[19]119
[407]120cluster_dmsg("\n[DBG] %s : cores initialized in cluster %x at cycle %d\n",
121__FUNCTION__ , local_cxy , hal_get_cycles() );
[50]122
[1]123    // initialises RPC fifo
[407]124        local_fifo_init( &cluster->rpc_fifo );
[279]125    cluster->rpc_threads = 0;
[1]126
[407]127cluster_dmsg("\n[DBG] %s : RPC fifo inialized in cluster %x at cycle %d\n",
128__FUNCTION__ , local_cxy , hal_get_cycles() );
[50]129
[1]130    // initialise pref_tbl[] in process manager
131        spinlock_init( &cluster->pmgr.pref_lock );
132    cluster->pmgr.pref_nr = 0;
[19]133    cluster->pmgr.pref_tbl[0] = XPTR( local_cxy , &process_zero );
[1]134    for( lpid = 1 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ )
135    {
136        cluster->pmgr.pref_tbl[lpid] = XPTR_NULL;
137    }
138
139    // initialise local_list in process manager
[23]140        remote_spinlock_init( XPTR( local_cxy , &cluster->pmgr.local_lock ) );
141    xlist_root_init( XPTR( local_cxy , &cluster->pmgr.local_root ) );
[1]142    cluster->pmgr.local_nr = 0;
143
144    // initialise copies_lists in process manager
[101]145    for( lpid = 0 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ )
[1]146    {
147            remote_spinlock_init( XPTR( local_cxy , &cluster->pmgr.copies_lock[lpid] ) );
148        cluster->pmgr.copies_nr[lpid] = 0;
149        xlist_root_init( XPTR( local_cxy , &cluster->pmgr.copies_root[lpid] ) );
[19]150    }
[1]151
[407]152cluster_dmsg("\n[DBG] %s Process Manager initialized in cluster %x at cycle %d\n",
153__FUNCTION__ , local_cxy , hal_get_cycles() );
[50]154
[124]155    hal_fence();
[1]156
157        return 0;
158} // end cluster_init()
159
160////////////////////////////////////////
161bool_t cluster_is_undefined( cxy_t cxy )
162{
163    cluster_t * cluster = LOCAL_CLUSTER;
164
165    uint32_t y_width = cluster->y_width;
166
167    uint32_t x = cxy >> y_width;
168    uint32_t y = cxy & ((1<<y_width)-1);
169
[19]170    if( x >= cluster->x_size ) return true;
171    if( y >= cluster->y_size ) return true;
[1]172
173    return false;
174}
175
176////////////////////////////////////////////////////////////////////////////////////
177//  Cores related functions
178////////////////////////////////////////////////////////////////////////////////////
179
180/////////////////////////////////
181lid_t cluster_select_local_core()
182{
183    uint32_t min = 100;
184    lid_t    sel = 0;
185    lid_t    lid;
186
187    cluster_t * cluster = LOCAL_CLUSTER;
188
189    for( lid = 0 ; lid < cluster->cores_nr ; lid++ )
190    {
191        if( cluster->core_tbl[lid].usage < min )
192        {
193            min = cluster->core_tbl[lid].usage;
194            sel = lid;
195        }
[19]196    }
[1]197    return sel;
198}
199
200////////////////////////////////////////////////////////////////////////////////////
201//  Process management related functions
202////////////////////////////////////////////////////////////////////////////////////
203
204//////////////////////////////////////////////////////////
205xptr_t cluster_get_reference_process_from_pid( pid_t pid )
[19]206{
[23]207    xptr_t ref_xp;   // extended pointer on reference process descriptor
[1]208
209    cluster_t * cluster = LOCAL_CLUSTER;
210
211    // get owner cluster and lpid
212    cxy_t  owner_cxy = CXY_FROM_PID( pid );
213    lpid_t lpid      = LPID_FROM_PID( pid );
214
[19]215    // Check valid PID
[23]216    if( lpid >= CONFIG_MAX_PROCESS_PER_CLUSTER )  return XPTR_NULL;
[1]217
218    if( local_cxy == owner_cxy )   // local cluster is owner cluster
[19]219    {
[23]220        ref_xp = cluster->pmgr.pref_tbl[lpid];
[1]221    }
222    else                              // use a remote_lwd to access owner cluster
223    {
[23]224        ref_xp = (xptr_t)hal_remote_lwd( XPTR( owner_cxy , &cluster->pmgr.pref_tbl[lpid] ) );
[1]225    }
226
[23]227    return ref_xp;
[1]228}
229
[416]230///////////////////////////////////////////////
231error_t cluster_pid_alloc( process_t * process,
232                           pid_t     * pid )
[1]233{
234    lpid_t      lpid;
235    bool_t      found;
236
237    pmgr_t    * pm         = &LOCAL_CLUSTER->pmgr;
238
239    // get the process manager lock
240    spinlock_lock( &pm->pref_lock );
241
242    // search an empty slot
243    found = false;
244    for( lpid = 0 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ )
245    {
246        if( pm->pref_tbl[lpid] == XPTR_NULL )
247        {
248            found = true;
249            break;
250        }
251    }
252
253    if( found )
254    {
255        // register process in pref_tbl[]
[416]256        pm->pref_tbl[lpid] = XPTR( local_cxy , process );
[1]257        pm->pref_nr++;
258
259        // returns pid
260        *pid = PID( local_cxy , lpid );
261
[416]262        // release the processs_manager lock
263        spinlock_unlock( &pm->pref_lock );
264
265        return 0;
[1]266    }
267    else
268    {
[416]269        // release the processs_manager lock
270        spinlock_unlock( &pm->pref_lock );
271
272        return -1;
[19]273    }
[1]274
275} // end cluster_pid_alloc()
276
277/////////////////////////////////////
278void cluster_pid_release( pid_t pid )
279{
280    cxy_t  owner_cxy  = CXY_FROM_PID( pid );
281    lpid_t lpid       = LPID_FROM_PID( pid );
282
[409]283    pmgr_t  * pm = &LOCAL_CLUSTER->pmgr;
284
[1]285    // check pid argument
[409]286    assert( (lpid < CONFIG_MAX_PROCESS_PER_CLUSTER) && (owner_cxy == local_cxy) ,
287    __FUNCTION__ , "illegal PID" );
[1]288
289    // get the process manager lock
290    spinlock_lock( &pm->pref_lock );
291
292    // remove process from pref_tbl[]
293    pm->pref_tbl[lpid] = XPTR_NULL;
294    pm->pref_nr--;
295
296    // release the processs_manager lock
297    spinlock_unlock( &pm->pref_lock );
298
299} // end cluster_pid_release()
300
301///////////////////////////////////////////////////////////
302process_t * cluster_get_local_process_from_pid( pid_t pid )
303{
[23]304    xptr_t         process_xp;
305    process_t    * process_ptr;
306    xptr_t         root_xp;
307    xptr_t         iter_xp;
308    bool_t         found;
[19]309
[23]310    found   = false;
311    root_xp = XPTR( local_cxy , &LOCAL_CLUSTER->pmgr.local_root );
312
313    XLIST_FOREACH( root_xp , iter_xp )
[1]314    {
[23]315        process_xp  = XLIST_ELEMENT( iter_xp , process_t , local_list );
316        process_ptr = (process_t *)GET_PTR( process_xp );
317        if( process_ptr->pid == pid )
[1]318        {
[23]319            found = true;
[1]320            break;
321        }
322    }
323
[23]324    if (found ) return process_ptr;
325    else        return NULL;
326
[1]327}  // end cluster_get_local_process_from_pid()
328
329//////////////////////////////////////////////////////
330void cluster_process_local_link( process_t * process )
331{
[407]332    uint32_t irq_state;
[1]333    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
334
335    // get lock protecting the process manager local list
[407]336    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , & irq_state );
[1]337
[23]338    xlist_add_first( XPTR( local_cxy , &pm->local_root ),
339                     XPTR( local_cxy , &process->local_list ) );
[1]340    pm->local_nr++;
341
342    // release lock protecting the process manager local list
[407]343    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
[1]344}
345
346////////////////////////////////////////////////////////
347void cluster_process_local_unlink( process_t * process )
348{
[407]349    uint32_t irq_state;
[1]350    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
351
352    // get lock protecting the process manager local list
[407]353    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , &irq_state );
[1]354
[23]355    xlist_unlink( XPTR( local_cxy , &process->local_list ) );
[1]356    pm->local_nr--;
357
358    // release lock protecting the process manager local list
[407]359    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
[1]360}
361
362///////////////////////////////////////////////////////
363void cluster_process_copies_link( process_t * process )
364{
[407]365    uint32_t irq_state;
[1]366    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
367
368    // get owner cluster identifier CXY and process LPID
369    pid_t    pid        = process->pid;
370    cxy_t    owner_cxy  = CXY_FROM_PID( pid );
371    lpid_t   lpid       = LPID_FROM_PID( pid );
372
373    // get extended pointer on lock protecting copies_list[lpid]
[120]374    xptr_t copies_lock  = XPTR( owner_cxy , &pm->copies_lock[lpid] );
[1]375
376    // get extended pointer on the copies_list[lpid] root
[120]377    xptr_t copies_root  = XPTR( owner_cxy , &pm->copies_root[lpid] );
[1]378
379    // get extended pointer on the local copies_list entry
380    xptr_t copies_entry = XPTR( local_cxy , &process->copies_list );
381
[19]382    // get lock protecting copies_list[lpid]
[407]383    remote_spinlock_lock_busy( copies_lock , &irq_state );
[1]384
385    xlist_add_first( copies_root , copies_entry );
386    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , 1 );
387
[19]388    // release lock protecting copies_list[lpid]
[407]389    remote_spinlock_unlock_busy( copies_lock , irq_state );
[1]390}
391
392/////////////////////////////////////////////////////////
393void cluster_process_copies_unlink( process_t * process )
394{
[407]395    uint32_t irq_state;
[1]396    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
397
398    // get owner cluster identifier CXY and process LPID
399    pid_t    pid        = process->pid;
400    cxy_t    owner_cxy  = CXY_FROM_PID( pid );
401    lpid_t   lpid       = LPID_FROM_PID( pid );
402
403    // get extended pointer on lock protecting copies_list[lpid]
404    xptr_t copies_lock  = hal_remote_lwd( XPTR( owner_cxy , &pm->copies_lock[lpid] ) );
405
406    // get extended pointer on the local copies_list entry
407    xptr_t copies_entry = XPTR( local_cxy , &process->copies_list );
408
[19]409    // get lock protecting copies_list[lpid]
[407]410    remote_spinlock_lock_busy( copies_lock , &irq_state );
[1]411
412    xlist_unlink( copies_entry );
413    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , -1 );
414
[19]415    // release lock protecting copies_list[lpid]
[407]416    remote_spinlock_unlock_busy( copies_lock , irq_state );
[1]417}
418
419////////////////////////////////////////////////////////////////////////////////////////
420// TODO Il me semble que la seule chose que fait ce kernel thread à chaque réveil
[19]421// est de mettre à jour la DQDT, et de se rendormir... A-t-on besoin d'un thread ? [AG]
[1]422//////////////////////////////////////////////////////////////////////////////////////////
423
424#if 0
425void * cluster_manager_thread( void * arg )
426{
427        register struct dqdt_cluster_s * root;
428        register struct cluster_s      * root_home;
429
430        register uint32_t                tm_start;
431        register uint32_t                tm_end;
432        register uint32_t                cpu_id;
433        struct cluster_s               * cluster;
434        struct thread_s                * this;
435        struct event_s                   event;
436        struct alarm_info_s              info;
437        register uint32_t                cntr;
438        register bool_t                  isRootMgr;
439        register uint32_t                period;
440
441        cpu_enable_all_irq( NULL );
442
443        cluster   = arg;
444        this      = CURRENT_THREAD;
445        cpu_id    = cpu_get_id();
446        root      = dqdt_root;
447        root_home = dqdt_root->home;
448        isRootMgr = (cluster == root_home) ? true : false;
449        cntr      = 0;
[19]450        period    = (isRootMgr) ?
451                CONFIG_DQDT_ROOTMGR_PERIOD * MSEC_PER_TICK :
[1]452                CONFIG_DQDT_MGR_PERIOD * MSEC_PER_TICK;
453
454        event_set_senderId(&event, this);
455        event_set_priority(&event, E_CHR);
456        event_set_handler(&event, &manager_alarm_event_handler);
[19]457
[1]458        info.event = &event;
459        thread_preempt_disable(CURRENT_THREAD);
460
461    // infinite loop
462        while(1)
463        {
464                tm_start = cpu_time_stamp();
465                dqdt_update();
466                tm_end   = cpu_time_stamp();
467
468                if(isRootMgr)
469                {
470                        if((cntr % 10) == 0)
471                        {
[19]472                                printk(INFO, "INFO: cpu %d, DQDT update ended [ %u - %u ]\n",
473                                       cpu_id,
474                                       tm_end,
[1]475                                       tm_end - tm_start);
476
477                                dqdt_print_summary(root);
478                        }
479                }
480
481                alarm_wait( &info , period );
482                sched_sleep(this);
483                cntr ++;
484        }
485
486        return NULL;
487} // end cluster_manager_thread()
488
489//////////////////////////////////////////
490EVENT_HANDLER(manager_alarm_event_handler)
491{
492        struct thread_s *manager;
[19]493
[1]494        manager = event_get_senderId(event);
[19]495
[1]496        thread_preempt_disable(CURRENT_THREAD);
497
498        //printk(INFO, "%s: cpu %d [%u]\n", __FUNCTION__, cpu_get_id(), cpu_time_stamp());
499
500        sched_wakeup(manager);
[19]501
[1]502        thread_preempt_enable(CURRENT_THREAD);
503
504        return 0;
505}
506
507///////////////////////////////////////////////
508EVENT_HANDLER(cluster_key_create_event_handler)
509{
510        struct cluster_s *cluster;
511        struct thread_s *sender;
512        ckey_t *ckey;
513        uint32_t key;
514
515        sender  = event_get_senderId(event);
516        ckey    = event_get_argument(event);
517        cluster = current_cluster;
518        key     = cluster->next_key;
519
520        while((key < CLUSTER_TOTAL_KEYS_NR) && (cluster->keys_tbl[key] != NULL))
521                key ++;
522
523        if(key < CLUSTER_TOTAL_KEYS_NR)
524        {
525                ckey->val = key;
[19]526                cluster->keys_tbl[key] = (void *) 0x1; // Reserved
[1]527                cluster->next_key = key;
528                event_set_error(event, 0);
529        }
530        else
531                event_set_error(event, ENOSPC);
532
533        sched_wakeup(sender);
534        return 0;
535}
536
537///////////////////////////////////////////////
538EVENT_HANDLER(cluster_key_delete_event_handler)
539{
540        struct cluster_s *cluster;
541        struct thread_s *sender;
542        ckey_t *ckey;
543        uint32_t key;
544
545        sender  = event_get_senderId(event);
546        ckey    = event_get_argument(event);
547        cluster = current_cluster;
548        key     = ckey->val;
549
550        if(key < cluster->next_key)
551                cluster->next_key = key;
552
553        cluster->keys_tbl[key] = NULL;
554        event_set_error(event, 0);
555
556        sched_wakeup(sender);
557        return 0;
558}
559
560#define _CKEY_CREATE  0x0
561#define _CKEY_DELETE  0x1
562
563error_t cluster_do_key_op(ckey_t *key, uint32_t op)
564{
565        struct event_s event;
566        struct thread_s *this;
567        struct cluster_s *cluster;
568        struct cpu_s *cpu;
569
570        this = CURRENT_THREAD;
571
572        event_set_priority(&event, E_FUNC);
573        event_set_senderId(&event, this);
574        event_set_argument(&event, key);
575
576        if(op == _CKEY_CREATE)
577                event_set_handler(&event, cluster_key_create_event_handler);
578        else
579                event_set_handler(&event, cluster_key_delete_event_handler);
580
581        cluster = current_cluster;
582        cpu     = cluster->bscluster->bscpu;
583        event_send(&event, &cpu->re_listner);
584
585        sched_sleep(this);
586
587        return event_get_error(&event);
588}
589
590error_t cluster_key_create(ckey_t *key)
591{
592        return cluster_do_key_op(key, _CKEY_CREATE);
593}
594
595error_t cluster_key_delete(ckey_t *key)
596{
597        return cluster_do_key_op(key, _CKEY_DELETE);
598}
599
600void* cluster_getspecific(ckey_t *key)
601{
602        struct cluster_s *cluster;
603
604        cluster = current_cluster;
605        return cluster->keys_tbl[key->val];
606}
607
608void  cluster_setspecific(ckey_t *key, void *val)
609{
610        struct cluster_s *cluster;
611
612        cluster = current_cluster;
613        cluster->keys_tbl[key->val] = val;
614}
615#endif
Note: See TracBrowser for help on using the repository browser.