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

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

Fix several bugs in the fork() syscall.

File size: 16.9 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
230////////////////////////////////////////////////
231error_t cluster_pid_alloc( xptr_t    process_xp,
232                           pid_t   * pid )
233{
234    error_t     error;
235    lpid_t      lpid;
236    bool_t      found;
237
238    pmgr_t    * pm         = &LOCAL_CLUSTER->pmgr;
239
240    // get the process manager lock
241    spinlock_lock( &pm->pref_lock );
242
243    // search an empty slot
244    found = false;
245    for( lpid = 0 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ )
246    {
247        if( pm->pref_tbl[lpid] == XPTR_NULL )
248        {
249            found = true;
250            break;
251        }
252    }
253
254    if( found )
255    {
256        // register process in pref_tbl[]
257        pm->pref_tbl[lpid] = process_xp;
258        pm->pref_nr++;
259
260        // returns pid
261        *pid = PID( local_cxy , lpid );
262
263        error = 0;
264    }
265    else
266    {
267        error = EAGAIN;
[19]268    }
[1]269
270    // release the processs_manager lock
271    spinlock_unlock( &pm->pref_lock );
272
273    return error;
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
283    // check pid argument
284    if( (lpid >= CONFIG_MAX_PROCESS_PER_CLUSTER) || (owner_cxy != local_cxy) )
285    {
[374]286        panic("illegal PID");
[1]287    }
288
289    pmgr_t  * pm = &LOCAL_CLUSTER->pmgr;
290
291    // get the process manager lock
292    spinlock_lock( &pm->pref_lock );
293
294    // remove process from pref_tbl[]
295    pm->pref_tbl[lpid] = XPTR_NULL;
296    pm->pref_nr--;
297
298    // release the processs_manager lock
299    spinlock_unlock( &pm->pref_lock );
300
301} // end cluster_pid_release()
302
303///////////////////////////////////////////////////////////
304process_t * cluster_get_local_process_from_pid( pid_t pid )
305{
[23]306    xptr_t         process_xp;
307    process_t    * process_ptr;
308    xptr_t         root_xp;
309    xptr_t         iter_xp;
310    bool_t         found;
[19]311
[23]312    found   = false;
313    root_xp = XPTR( local_cxy , &LOCAL_CLUSTER->pmgr.local_root );
314
315    XLIST_FOREACH( root_xp , iter_xp )
[1]316    {
[23]317        process_xp  = XLIST_ELEMENT( iter_xp , process_t , local_list );
318        process_ptr = (process_t *)GET_PTR( process_xp );
319        if( process_ptr->pid == pid )
[1]320        {
[23]321            found = true;
[1]322            break;
323        }
324    }
325
[23]326    if (found ) return process_ptr;
327    else        return NULL;
328
[1]329}  // end cluster_get_local_process_from_pid()
330
331//////////////////////////////////////////////////////
332void cluster_process_local_link( process_t * process )
333{
[407]334    uint32_t irq_state;
[1]335    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
336
337    // get lock protecting the process manager local list
[407]338    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , & irq_state );
[1]339
[23]340    xlist_add_first( XPTR( local_cxy , &pm->local_root ),
341                     XPTR( local_cxy , &process->local_list ) );
[1]342    pm->local_nr++;
343
344    // release lock protecting the process manager local list
[407]345    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
[1]346}
347
348////////////////////////////////////////////////////////
349void cluster_process_local_unlink( process_t * process )
350{
[407]351    uint32_t irq_state;
[1]352    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
353
354    // get lock protecting the process manager local list
[407]355    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , &irq_state );
[1]356
[23]357    xlist_unlink( XPTR( local_cxy , &process->local_list ) );
[1]358    pm->local_nr--;
359
360    // release lock protecting the process manager local list
[407]361    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
[1]362}
363
364///////////////////////////////////////////////////////
365void cluster_process_copies_link( process_t * process )
366{
[407]367    uint32_t irq_state;
[1]368    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
369
370    // get owner cluster identifier CXY and process LPID
371    pid_t    pid        = process->pid;
372    cxy_t    owner_cxy  = CXY_FROM_PID( pid );
373    lpid_t   lpid       = LPID_FROM_PID( pid );
374
375    // get extended pointer on lock protecting copies_list[lpid]
[120]376    xptr_t copies_lock  = XPTR( owner_cxy , &pm->copies_lock[lpid] );
[1]377
378    // get extended pointer on the copies_list[lpid] root
[120]379    xptr_t copies_root  = XPTR( owner_cxy , &pm->copies_root[lpid] );
[1]380
381    // get extended pointer on the local copies_list entry
382    xptr_t copies_entry = XPTR( local_cxy , &process->copies_list );
383
[19]384    // get lock protecting copies_list[lpid]
[407]385    remote_spinlock_lock_busy( copies_lock , &irq_state );
[1]386
387    xlist_add_first( copies_root , copies_entry );
388    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , 1 );
389
[19]390    // release lock protecting copies_list[lpid]
[407]391    remote_spinlock_unlock_busy( copies_lock , irq_state );
[1]392}
393
394/////////////////////////////////////////////////////////
395void cluster_process_copies_unlink( process_t * process )
396{
[407]397    uint32_t irq_state;
[1]398    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
399
400    // get owner cluster identifier CXY and process LPID
401    pid_t    pid        = process->pid;
402    cxy_t    owner_cxy  = CXY_FROM_PID( pid );
403    lpid_t   lpid       = LPID_FROM_PID( pid );
404
405    // get extended pointer on lock protecting copies_list[lpid]
406    xptr_t copies_lock  = hal_remote_lwd( XPTR( owner_cxy , &pm->copies_lock[lpid] ) );
407
408    // get extended pointer on the local copies_list entry
409    xptr_t copies_entry = XPTR( local_cxy , &process->copies_list );
410
[19]411    // get lock protecting copies_list[lpid]
[407]412    remote_spinlock_lock_busy( copies_lock , &irq_state );
[1]413
414    xlist_unlink( copies_entry );
415    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , -1 );
416
[19]417    // release lock protecting copies_list[lpid]
[407]418    remote_spinlock_unlock_busy( copies_lock , irq_state );
[1]419}
420
421////////////////////////////////////////////////////////////////////////////////////////
422// TODO Il me semble que la seule chose que fait ce kernel thread à chaque réveil
[19]423// est de mettre à jour la DQDT, et de se rendormir... A-t-on besoin d'un thread ? [AG]
[1]424//////////////////////////////////////////////////////////////////////////////////////////
425
426#if 0
427void * cluster_manager_thread( void * arg )
428{
429        register struct dqdt_cluster_s * root;
430        register struct cluster_s      * root_home;
431
432        register uint32_t                tm_start;
433        register uint32_t                tm_end;
434        register uint32_t                cpu_id;
435        struct cluster_s               * cluster;
436        struct thread_s                * this;
437        struct event_s                   event;
438        struct alarm_info_s              info;
439        register uint32_t                cntr;
440        register bool_t                  isRootMgr;
441        register uint32_t                period;
442
443        cpu_enable_all_irq( NULL );
444
445        cluster   = arg;
446        this      = CURRENT_THREAD;
447        cpu_id    = cpu_get_id();
448        root      = dqdt_root;
449        root_home = dqdt_root->home;
450        isRootMgr = (cluster == root_home) ? true : false;
451        cntr      = 0;
[19]452        period    = (isRootMgr) ?
453                CONFIG_DQDT_ROOTMGR_PERIOD * MSEC_PER_TICK :
[1]454                CONFIG_DQDT_MGR_PERIOD * MSEC_PER_TICK;
455
456        event_set_senderId(&event, this);
457        event_set_priority(&event, E_CHR);
458        event_set_handler(&event, &manager_alarm_event_handler);
[19]459
[1]460        info.event = &event;
461        thread_preempt_disable(CURRENT_THREAD);
462
463    // infinite loop
464        while(1)
465        {
466                tm_start = cpu_time_stamp();
467                dqdt_update();
468                tm_end   = cpu_time_stamp();
469
470                if(isRootMgr)
471                {
472                        if((cntr % 10) == 0)
473                        {
[19]474                                printk(INFO, "INFO: cpu %d, DQDT update ended [ %u - %u ]\n",
475                                       cpu_id,
476                                       tm_end,
[1]477                                       tm_end - tm_start);
478
479                                dqdt_print_summary(root);
480                        }
481                }
482
483                alarm_wait( &info , period );
484                sched_sleep(this);
485                cntr ++;
486        }
487
488        return NULL;
489} // end cluster_manager_thread()
490
491//////////////////////////////////////////
492EVENT_HANDLER(manager_alarm_event_handler)
493{
494        struct thread_s *manager;
[19]495
[1]496        manager = event_get_senderId(event);
[19]497
[1]498        thread_preempt_disable(CURRENT_THREAD);
499
500        //printk(INFO, "%s: cpu %d [%u]\n", __FUNCTION__, cpu_get_id(), cpu_time_stamp());
501
502        sched_wakeup(manager);
[19]503
[1]504        thread_preempt_enable(CURRENT_THREAD);
505
506        return 0;
507}
508
509///////////////////////////////////////////////
510EVENT_HANDLER(cluster_key_create_event_handler)
511{
512        struct cluster_s *cluster;
513        struct thread_s *sender;
514        ckey_t *ckey;
515        uint32_t key;
516
517        sender  = event_get_senderId(event);
518        ckey    = event_get_argument(event);
519        cluster = current_cluster;
520        key     = cluster->next_key;
521
522        while((key < CLUSTER_TOTAL_KEYS_NR) && (cluster->keys_tbl[key] != NULL))
523                key ++;
524
525        if(key < CLUSTER_TOTAL_KEYS_NR)
526        {
527                ckey->val = key;
[19]528                cluster->keys_tbl[key] = (void *) 0x1; // Reserved
[1]529                cluster->next_key = key;
530                event_set_error(event, 0);
531        }
532        else
533                event_set_error(event, ENOSPC);
534
535        sched_wakeup(sender);
536        return 0;
537}
538
539///////////////////////////////////////////////
540EVENT_HANDLER(cluster_key_delete_event_handler)
541{
542        struct cluster_s *cluster;
543        struct thread_s *sender;
544        ckey_t *ckey;
545        uint32_t key;
546
547        sender  = event_get_senderId(event);
548        ckey    = event_get_argument(event);
549        cluster = current_cluster;
550        key     = ckey->val;
551
552        if(key < cluster->next_key)
553                cluster->next_key = key;
554
555        cluster->keys_tbl[key] = NULL;
556        event_set_error(event, 0);
557
558        sched_wakeup(sender);
559        return 0;
560}
561
562#define _CKEY_CREATE  0x0
563#define _CKEY_DELETE  0x1
564
565error_t cluster_do_key_op(ckey_t *key, uint32_t op)
566{
567        struct event_s event;
568        struct thread_s *this;
569        struct cluster_s *cluster;
570        struct cpu_s *cpu;
571
572        this = CURRENT_THREAD;
573
574        event_set_priority(&event, E_FUNC);
575        event_set_senderId(&event, this);
576        event_set_argument(&event, key);
577
578        if(op == _CKEY_CREATE)
579                event_set_handler(&event, cluster_key_create_event_handler);
580        else
581                event_set_handler(&event, cluster_key_delete_event_handler);
582
583        cluster = current_cluster;
584        cpu     = cluster->bscluster->bscpu;
585        event_send(&event, &cpu->re_listner);
586
587        sched_sleep(this);
588
589        return event_get_error(&event);
590}
591
592error_t cluster_key_create(ckey_t *key)
593{
594        return cluster_do_key_op(key, _CKEY_CREATE);
595}
596
597error_t cluster_key_delete(ckey_t *key)
598{
599        return cluster_do_key_op(key, _CKEY_DELETE);
600}
601
602void* cluster_getspecific(ckey_t *key)
603{
604        struct cluster_s *cluster;
605
606        cluster = current_cluster;
607        return cluster->keys_tbl[key->val];
608}
609
610void  cluster_setspecific(ckey_t *key, void *val)
611{
612        struct cluster_s *cluster;
613
614        cluster = current_cluster;
615        cluster->keys_tbl[key->val] = val;
616}
617#endif
Note: See TracBrowser for help on using the repository browser.