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

Last change on this file since 407 was 407, checked in by alain, 5 years ago

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