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

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

Improve sys_exec.

File size: 17.0 KB
Line 
1/*
2 * cluster.c - Cluster-Manager related operations
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Mohamed Lamine Karaoui (2015)
6 *         Alain Greiner (2016,2017)
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
26#include <kernel_config.h>
27#include <hal_types.h>
28#include <hal_atomic.h>
29#include <hal_special.h>
30#include <hal_ppm.h>
31#include <remote_fifo.h>
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
47/////////////////////////////////////////////////////////////////////////////////////
48// Extern global variables
49/////////////////////////////////////////////////////////////////////////////////////
50
51extern process_t process_zero;     // allocated in kernel_init.c file
52
53
54/////////////////////////////////////////////////
55error_t cluster_init( struct boot_info_s * info )
56{
57    error_t     error;
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
64        cluster->paddr_width     = info->paddr_width;
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
74    // initialize the lock protecting the embedded kcm allocator
75        spinlock_init( &cluster->kcm_lock );
76
77cluster_dmsg("\n[DBG] %s for cluster %x enters\n",
78__FUNCTION__ , local_cxy );
79
80    // initialises DQDT
81    cluster->dqdt_root_level = dqdt_init( info->x_size,
82                                          info->y_size,
83                                          info->y_width );
84    cluster->threads_var = 0;
85    cluster->pages_var   = 0;
86
87    // initialises embedded PPM
88        error = hal_ppm_init( info );
89
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
97cluster_dmsg("\n[DBG] %s : PPM initialized in cluster %x at cycle %d\n",
98__FUNCTION__ , local_cxy , hal_get_cycles() );
99
100    // initialises embedded KHM
101        khm_init( &cluster->khm );
102
103    cluster_dmsg("\n[DBG] %s : KHM initialized in cluster %x at cycle %d\n",
104                 __FUNCTION__ , local_cxy , hal_get_cycles() );
105
106    // initialises embedded KCM
107        kcm_init( &cluster->kcm , KMEM_KCM );
108
109    cluster_dmsg("\n[DBG] %s : KCM initialized in cluster %x at cycle %d\n",
110                 __FUNCTION__ , local_cxy , hal_get_cycles() );
111
112    // initialises all cores descriptors
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        }
119
120cluster_dmsg("\n[DBG] %s : cores initialized in cluster %x at cycle %d\n",
121__FUNCTION__ , local_cxy , hal_get_cycles() );
122
123    // initialises RPC fifo
124        local_fifo_init( &cluster->rpc_fifo );
125    cluster->rpc_threads = 0;
126
127cluster_dmsg("\n[DBG] %s : RPC fifo inialized in cluster %x at cycle %d\n",
128__FUNCTION__ , local_cxy , hal_get_cycles() );
129
130    // initialise pref_tbl[] in process manager
131        spinlock_init( &cluster->pmgr.pref_lock );
132    cluster->pmgr.pref_nr = 0;
133    cluster->pmgr.pref_tbl[0] = XPTR( local_cxy , &process_zero );
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
140        remote_spinlock_init( XPTR( local_cxy , &cluster->pmgr.local_lock ) );
141    xlist_root_init( XPTR( local_cxy , &cluster->pmgr.local_root ) );
142    cluster->pmgr.local_nr = 0;
143
144    // initialise copies_lists in process manager
145    for( lpid = 0 ; lpid < CONFIG_MAX_PROCESS_PER_CLUSTER ; lpid++ )
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] ) );
150    }
151
152cluster_dmsg("\n[DBG] %s Process Manager initialized in cluster %x at cycle %d\n",
153__FUNCTION__ , local_cxy , hal_get_cycles() );
154
155    hal_fence();
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
170    if( x >= cluster->x_size ) return true;
171    if( y >= cluster->y_size ) return true;
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        }
196    }
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 )
206{
207    xptr_t ref_xp;   // extended pointer on reference process descriptor
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
215    // Check valid PID
216    if( lpid >= CONFIG_MAX_PROCESS_PER_CLUSTER )  return XPTR_NULL;
217
218    if( local_cxy == owner_cxy )   // local cluster is owner cluster
219    {
220        ref_xp = cluster->pmgr.pref_tbl[lpid];
221    }
222    else                              // use a remote_lwd to access owner cluster
223    {
224        ref_xp = (xptr_t)hal_remote_lwd( XPTR( owner_cxy , &cluster->pmgr.pref_tbl[lpid] ) );
225    }
226
227    return ref_xp;
228}
229
230///////////////////////////////////////////////
231error_t cluster_pid_alloc( process_t * process,
232                           pid_t     * pid )
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[]
256        pm->pref_tbl[lpid] = XPTR( local_cxy , process );
257        pm->pref_nr++;
258
259        // returns pid
260        *pid = PID( local_cxy , lpid );
261
262        // release the processs_manager lock
263        spinlock_unlock( &pm->pref_lock );
264
265        return 0;
266    }
267    else
268    {
269        // release the processs_manager lock
270        spinlock_unlock( &pm->pref_lock );
271
272        return -1;
273    }
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    pmgr_t  * pm = &LOCAL_CLUSTER->pmgr;
284
285    // check pid argument
286    assert( (lpid < CONFIG_MAX_PROCESS_PER_CLUSTER) && (owner_cxy == local_cxy) ,
287    __FUNCTION__ , "illegal PID" );
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{
304    xptr_t         process_xp;
305    process_t    * process_ptr;
306    xptr_t         root_xp;
307    xptr_t         iter_xp;
308    bool_t         found;
309
310    found   = false;
311    root_xp = XPTR( local_cxy , &LOCAL_CLUSTER->pmgr.local_root );
312
313    XLIST_FOREACH( root_xp , iter_xp )
314    {
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 )
318        {
319            found = true;
320            break;
321        }
322    }
323
324    if (found ) return process_ptr;
325    else        return NULL;
326
327}  // end cluster_get_local_process_from_pid()
328
329//////////////////////////////////////////////////////
330void cluster_process_local_link( process_t * process )
331{
332    uint32_t irq_state;
333    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
334
335    // get lock protecting the process manager local list
336    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , & irq_state );
337
338    xlist_add_first( XPTR( local_cxy , &pm->local_root ),
339                     XPTR( local_cxy , &process->local_list ) );
340    pm->local_nr++;
341
342    // release lock protecting the process manager local list
343    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
344}
345
346////////////////////////////////////////////////////////
347void cluster_process_local_unlink( process_t * process )
348{
349    uint32_t irq_state;
350    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
351
352    // get lock protecting the process manager local list
353    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , &irq_state );
354
355    xlist_unlink( XPTR( local_cxy , &process->local_list ) );
356    pm->local_nr--;
357
358    // release lock protecting the process manager local list
359    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
360}
361
362///////////////////////////////////////////////////////
363void cluster_process_copies_link( process_t * process )
364{
365    uint32_t irq_state;
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]
374    xptr_t copies_lock  = XPTR( owner_cxy , &pm->copies_lock[lpid] );
375
376    // get extended pointer on the copies_list[lpid] root
377    xptr_t copies_root  = XPTR( owner_cxy , &pm->copies_root[lpid] );
378
379    // get extended pointer on the local copies_list entry
380    xptr_t copies_entry = XPTR( local_cxy , &process->copies_list );
381
382    // get lock protecting copies_list[lpid]
383    remote_spinlock_lock_busy( copies_lock , &irq_state );
384
385    xlist_add_first( copies_root , copies_entry );
386    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , 1 );
387
388    // release lock protecting copies_list[lpid]
389    remote_spinlock_unlock_busy( copies_lock , irq_state );
390}
391
392/////////////////////////////////////////////////////////
393void cluster_process_copies_unlink( process_t * process )
394{
395    uint32_t irq_state;
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
409    // get lock protecting copies_list[lpid]
410    remote_spinlock_lock_busy( copies_lock , &irq_state );
411
412    xlist_unlink( copies_entry );
413    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , -1 );
414
415    // release lock protecting copies_list[lpid]
416    remote_spinlock_unlock_busy( copies_lock , irq_state );
417}
418
419////////////////////////////////////////////////////////////////////////////////////////
420// TODO Il me semble que la seule chose que fait ce kernel thread à chaque réveil
421// est de mettre à jour la DQDT, et de se rendormir... A-t-on besoin d'un thread ? [AG]
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;
450        period    = (isRootMgr) ?
451                CONFIG_DQDT_ROOTMGR_PERIOD * MSEC_PER_TICK :
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);
457
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                        {
472                                printk(INFO, "INFO: cpu %d, DQDT update ended [ %u - %u ]\n",
473                                       cpu_id,
474                                       tm_end,
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;
493
494        manager = event_get_senderId(event);
495
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);
501
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;
526                cluster->keys_tbl[key] = (void *) 0x1; // Reserved
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.