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

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

Fix several bugs in the fork() syscall.

File size: 16.9 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( 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;
268    }
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    {
286        panic("illegal PID");
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{
306    xptr_t         process_xp;
307    process_t    * process_ptr;
308    xptr_t         root_xp;
309    xptr_t         iter_xp;
310    bool_t         found;
311
312    found   = false;
313    root_xp = XPTR( local_cxy , &LOCAL_CLUSTER->pmgr.local_root );
314
315    XLIST_FOREACH( root_xp , iter_xp )
316    {
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 )
320        {
321            found = true;
322            break;
323        }
324    }
325
326    if (found ) return process_ptr;
327    else        return NULL;
328
329}  // end cluster_get_local_process_from_pid()
330
331//////////////////////////////////////////////////////
332void cluster_process_local_link( process_t * process )
333{
334    uint32_t irq_state;
335    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
336
337    // get lock protecting the process manager local list
338    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , & irq_state );
339
340    xlist_add_first( XPTR( local_cxy , &pm->local_root ),
341                     XPTR( local_cxy , &process->local_list ) );
342    pm->local_nr++;
343
344    // release lock protecting the process manager local list
345    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
346}
347
348////////////////////////////////////////////////////////
349void cluster_process_local_unlink( process_t * process )
350{
351    uint32_t irq_state;
352    pmgr_t * pm = &LOCAL_CLUSTER->pmgr;
353
354    // get lock protecting the process manager local list
355    remote_spinlock_lock_busy( XPTR( local_cxy , &pm->local_lock ) , &irq_state );
356
357    xlist_unlink( XPTR( local_cxy , &process->local_list ) );
358    pm->local_nr--;
359
360    // release lock protecting the process manager local list
361    remote_spinlock_unlock_busy( XPTR( local_cxy , &pm->local_lock ) , irq_state );
362}
363
364///////////////////////////////////////////////////////
365void cluster_process_copies_link( process_t * process )
366{
367    uint32_t irq_state;
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]
376    xptr_t copies_lock  = XPTR( owner_cxy , &pm->copies_lock[lpid] );
377
378    // get extended pointer on the copies_list[lpid] root
379    xptr_t copies_root  = XPTR( owner_cxy , &pm->copies_root[lpid] );
380
381    // get extended pointer on the local copies_list entry
382    xptr_t copies_entry = XPTR( local_cxy , &process->copies_list );
383
384    // get lock protecting copies_list[lpid]
385    remote_spinlock_lock_busy( copies_lock , &irq_state );
386
387    xlist_add_first( copies_root , copies_entry );
388    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , 1 );
389
390    // release lock protecting copies_list[lpid]
391    remote_spinlock_unlock_busy( copies_lock , irq_state );
392}
393
394/////////////////////////////////////////////////////////
395void cluster_process_copies_unlink( process_t * process )
396{
397    uint32_t irq_state;
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
411    // get lock protecting copies_list[lpid]
412    remote_spinlock_lock_busy( copies_lock , &irq_state );
413
414    xlist_unlink( copies_entry );
415    hal_remote_atomic_add( XPTR( owner_cxy , &pm->copies_nr[lpid] ) , -1 );
416
417    // release lock protecting copies_list[lpid]
418    remote_spinlock_unlock_busy( copies_lock , irq_state );
419}
420
421////////////////////////////////////////////////////////////////////////////////////////
422// TODO Il me semble que la seule chose que fait ce kernel thread à chaque réveil
423// est de mettre à jour la DQDT, et de se rendormir... A-t-on besoin d'un thread ? [AG]
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;
452        period    = (isRootMgr) ?
453                CONFIG_DQDT_ROOTMGR_PERIOD * MSEC_PER_TICK :
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);
459
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                        {
474                                printk(INFO, "INFO: cpu %d, DQDT update ended [ %u - %u ]\n",
475                                       cpu_id,
476                                       tm_end,
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;
495
496        manager = event_get_senderId(event);
497
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);
503
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;
528                cluster->keys_tbl[key] = (void *) 0x1; // Reserved
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.