source: trunk/kernel/kern/kernel_init.c @ 568

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

Complete restructuration of kernel locks.

File size: 58.7 KB
Line 
1/*
2 * kernel_init.c - kernel parallel initialization
3 *
4 * Authors :  Mohamed Lamine Karaoui (2015)
5 *            Alain Greiner  (2016,2017)
6 *
7 * Copyright (c) Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <errno.h>
27#include <hal_kernel_types.h>
28#include <hal_special.h>
29#include <hal_context.h>
30#include <hal_irqmask.h>
31#include <hal_macros.h>
32#include <hal_ppm.h>
33#include <barrier.h>
34#include <xbarrier.h>
35#include <remote_fifo.h>
36#include <core.h>
37#include <list.h>
38#include <xlist.h>
39#include <xhtab.h>
40#include <thread.h>
41#include <scheduler.h>
42#include <kmem.h>
43#include <cluster.h>
44#include <string.h>
45#include <memcpy.h>
46#include <ppm.h>
47#include <page.h>
48#include <chdev.h>
49#include <boot_info.h>
50#include <dqdt.h>
51#include <dev_mmc.h>
52#include <dev_dma.h>
53#include <dev_iob.h>
54#include <dev_ioc.h>
55#include <dev_txt.h>
56#include <dev_pic.h>
57#include <printk.h>
58#include <vfs.h>
59#include <devfs.h>
60#include <mapper.h>
61
62///////////////////////////////////////////////////////////////////////////////////////////
63// All the following global variables are replicated in all clusters.
64// They are initialised by the kernel_init() function.
65//
66// WARNING : The section names have been defined to control the base addresses of the
67// boot_info structure and the idle thread descriptors, through the kernel.ld script:
68// - the boot_info structure is built by the bootloader, and used by kernel_init.
69//   it must be the first object in the kdata segment.
70// - the array of idle threads descriptors must be placed on the first page boundary after
71//   the boot_info structure in the kdata segment.
72///////////////////////////////////////////////////////////////////////////////////////////
73
74// This variable defines the local boot_info structure
75__attribute__((section(".kinfo")))
76boot_info_t          boot_info;
77
78// This variable defines the "idle" threads descriptors array
79__attribute__((section(".kidle")))
80char                 idle_threads[CONFIG_THREAD_DESC_SIZE *
81                                   CONFIG_MAX_LOCAL_CORES]   CONFIG_PPM_PAGE_ALIGNED;
82
83// This variable defines the local cluster manager
84__attribute__((section(".kdata")))
85cluster_t            cluster_manager                         CONFIG_CACHE_LINE_ALIGNED;
86
87// This variable defines the TXT_TX[0] chdev
88__attribute__((section(".kdata")))
89chdev_t              txt0_tx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
90
91// This variable defines the TXT_RX[0] chdev
92__attribute__((section(".kdata")))
93chdev_t              txt0_rx_chdev                           CONFIG_CACHE_LINE_ALIGNED;
94
95// This variables define the kernel process0 descriptor
96__attribute__((section(".kdata")))
97process_t            process_zero                            CONFIG_CACHE_LINE_ALIGNED;
98
99// This variable defines extended pointers on the distributed chdevs
100__attribute__((section(".kdata")))
101chdev_directory_t    chdev_dir                               CONFIG_CACHE_LINE_ALIGNED;
102
103// This variable contains the input IRQ indexes for the IOPIC controller
104__attribute__((section(".kdata")))
105iopic_input_t        iopic_input                             CONFIG_CACHE_LINE_ALIGNED;
106
107// This variable contains the input IRQ indexes for the LAPIC controller
108__attribute__((section(".kdata")))
109lapic_input_t        lapic_input                             CONFIG_CACHE_LINE_ALIGNED;
110
111// This variable defines the local cluster identifier
112__attribute__((section(".kdata")))
113cxy_t                local_cxy                               CONFIG_CACHE_LINE_ALIGNED;
114
115// This variable is used for CP0 cores synchronisation in kernel_init()
116__attribute__((section(".kdata")))
117xbarrier_t           global_barrier                          CONFIG_CACHE_LINE_ALIGNED;
118
119// This variable is used for local cores synchronisation in kernel_init()
120__attribute__((section(".kdata")))
121barrier_t            local_barrier                           CONFIG_CACHE_LINE_ALIGNED;
122
123// This variable defines the array of supported File System contexts
124__attribute__((section(".kdata")))
125vfs_ctx_t            fs_context[FS_TYPES_NR]                 CONFIG_CACHE_LINE_ALIGNED;
126
127// kernel_init is the entry point defined in hal/tsar_mips32/kernel.ld
128// It is used by the bootloader.
129extern void kernel_init( boot_info_t * info );
130
131// This array is used for debug, and describes the kernel locks usage,
132// It must be kept consistent with the defines in kernel_config.h file.
133char * lock_type_str[] =
134{
135    "unused_0",              //  0
136
137    "CLUSTER_KCM",           //  1
138    "PPM_FREE",              //  2
139    "SCHED_STATE",           //  3
140    "VMM_STACK",             //  4
141    "VMM_MMAP",              //  5
142    "VFS_CTX",               //  6
143    "KCM_STATE",             //  7
144    "KHM_STATE",             //  8
145    "HTAB_STATE",            //  9
146
147    "THREAD_JOIN",           // 10
148    "VFS_MAIN",              // 11
149    "CHDEV_QUEUE",           // 12
150    "CHDEV_TXT0",            // 13
151    "CHDEV_TXTLIST",         // 14
152    "PAGE_STATE",            // 15
153    "MUTEX_STATE",           // 16
154    "CONDVAR_STATE",         // 17
155    "SEM_STATE",             // 18
156    "XHTAB_STATE",           // 19
157
158    "unused_20",             // 20
159
160    "CLUSTER_PREFTBL",       // 21
161    "PPM_DIRTY",             // 22
162
163    "CLUSTER_LOCALS",        // 23
164    "CLUSTER_COPIES",        // 24
165    "PROCESS_CHILDREN",      // 25
166    "PROCESS_USERSYNC",      // 26
167    "PROCESS_FDARRAY",       // 27
168
169    "MAPPER_STATE",          // 28
170    "PROCESS_THTBL",         // 29
171
172    "PROCESS_CWD",           // 30
173    "VFS_INODE",             // 31
174    "VFS_FILE",              // 32
175    "VMM_VSL",               // 33
176};       
177
178// these debug variables are used to analyse the sys_read() and sys_write() syscalls timing
179
180#if DEBUG_SYS_READ
181uint32_t   enter_sys_read;
182uint32_t   exit_sys_read;
183
184uint32_t   enter_devfs_read;
185uint32_t   exit_devfs_read;
186
187uint32_t   enter_txt_read;
188uint32_t   exit_txt_read;
189
190uint32_t   enter_chdev_cmd_read;
191uint32_t   exit_chdev_cmd_read;
192
193uint32_t   enter_chdev_server_read;
194uint32_t   exit_chdev_server_read;
195
196uint32_t   enter_tty_cmd_read;
197uint32_t   exit_tty_cmd_read;
198
199uint32_t   enter_tty_isr_read;
200uint32_t   exit_tty_isr_read;
201#endif
202
203// these debug variables are used to analyse the sys_write() syscall timing
204
205#if DEBUG_SYS_WRITE   
206uint32_t   enter_sys_write;
207uint32_t   exit_sys_write;
208
209uint32_t   enter_devfs_write;
210uint32_t   exit_devfs_write;
211
212uint32_t   enter_txt_write;
213uint32_t   exit_txt_write;
214
215uint32_t   enter_chdev_cmd_write;
216uint32_t   exit_chdev_cmd_write;
217
218uint32_t   enter_chdev_server_write;
219uint32_t   exit_chdev_server_write;
220
221uint32_t   enter_tty_cmd_write;
222uint32_t   exit_tty_cmd_write;
223
224uint32_t   enter_tty_isr_write;
225uint32_t   exit_tty_isr_write;
226#endif
227
228// intrumentation variables : cumulated costs per syscall type in cluster
229uint32_t   syscalls_cumul_cost[SYSCALLS_NR];
230
231// intrumentation variables : number of syscalls per syscal type in cluster
232uint32_t   syscalls_occurences[SYSCALLS_NR];
233
234///////////////////////////////////////////////////////////////////////////////////////////
235// This function displays the ALMOS_MKH banner.
236///////////////////////////////////////////////////////////////////////////////////////////
237static void print_banner( uint32_t nclusters , uint32_t ncores )
238{
239    printk("\n"
240           "                    _        __    __     _____     ______         __    __    _   __   _     _   \n"
241           "          /\\       | |      |  \\  /  |   / ___ \\   / _____|       |  \\  /  |  | | / /  | |   | |  \n"
242           "         /  \\      | |      |   \\/   |  | /   \\ | | /             |   \\/   |  | |/ /   | |   | |  \n"
243           "        / /\\ \\     | |      | |\\  /| |  | |   | | | |_____   ___  | |\\  /| |  |   /    | |___| |  \n"
244           "       / /__\\ \\    | |      | | \\/ | |  | |   | | \\_____  \\ |___| | | \\/ | |  |   \\    |  ___  |  \n"
245           "      / ______ \\   | |      | |    | |  | |   | |       | |       | |    | |  | |\\ \\   | |   | |  \n"
246           "     / /      \\ \\  | |____  | |    | |  | \\___/ |  _____/ |       | |    | |  | | \\ \\  | |   | |  \n"
247           "    /_/        \\_\\ |______| |_|    |_|   \\_____/  |______/        |_|    |_|  |_|  \\_\\ |_|   |_|  \n"
248           "\n\n\t\t Advanced Locality Management Operating System / Multi Kernel Hybrid\n"
249           "\n\n\t\t %s / %d cluster(s) / %d core(s) per cluster\n\n",
250           CONFIG_ALMOS_VERSION , nclusters , ncores );
251}
252
253
254///////////////////////////////////////////////////////////////////////////////////////////
255// This function initializes the TXT_TX[0] and TXT_RX[0] chdev descriptors, implementing
256// the "kernel terminal", shared by all kernel instances for debug messages.
257// These chdev are implemented as global variables (replicated in all clusters),
258// because this terminal is used before the kmem allocator initialisation, but only
259// the chdevs in cluster 0 are registered in the "chdev_dir" directory.
260// As this TXT0 chdev supports only the TXT_SYNC_WRITE command, we don't create
261// a server thread, we don't allocate a WTI, and we don't initialize the waiting queue.
262// Note: The TXT_RX[0] chdev is created, but is not used by ALMOS-MKH (september 2018).
263///////////////////////////////////////////////////////////////////////////////////////////
264// @ info    : pointer on the local boot-info structure.
265///////////////////////////////////////////////////////////////////////////////////////////
266static void __attribute__ ((noinline)) txt0_device_init( boot_info_t * info )
267{
268    boot_device_t * dev_tbl;         // pointer on array of devices in boot_info
269    uint32_t        dev_nr;          // actual number of devices in this cluster
270    xptr_t          base;            // remote pointer on segment base
271    uint32_t        func;            // device functional index
272    uint32_t        impl;            // device implementation index
273    uint32_t        i;               // device index in dev_tbl
274    uint32_t        x;               // X cluster coordinate
275    uint32_t        y;               // Y cluster coordinate
276    uint32_t        channels;        // number of channels
277
278    // get number of peripherals and base of devices array from boot_info
279    dev_nr      = info->ext_dev_nr;
280    dev_tbl     = info->ext_dev;
281
282    // loop on external peripherals to find TXT device
283    for( i = 0 ; i < dev_nr ; i++ )
284    {
285        base        = dev_tbl[i].base;
286        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
287        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
288        channels    = dev_tbl[i].channels;
289
290        if (func == DEV_FUNC_TXT )
291        {
292            // initialize TXT_TX[0] chdev
293            txt0_tx_chdev.func    = func;
294            txt0_tx_chdev.impl    = impl;
295            txt0_tx_chdev.channel = 0;
296            txt0_tx_chdev.base    = base;
297            txt0_tx_chdev.is_rx   = false;
298            remote_busylock_init( XPTR( local_cxy , &txt0_tx_chdev.wait_lock ),
299                                  LOCK_CHDEV_TXT0 );
300           
301            // initialize TXT_RX[0] chdev
302            txt0_rx_chdev.func    = func;
303            txt0_rx_chdev.impl    = impl;
304            txt0_rx_chdev.channel = 0;
305            txt0_rx_chdev.base    = base;
306            txt0_rx_chdev.is_rx   = true;
307            remote_busylock_init( XPTR( local_cxy , &txt0_rx_chdev.wait_lock ),
308                                  LOCK_CHDEV_TXT0 );
309           
310            // make TXT specific initialisations
311            dev_txt_init( &txt0_tx_chdev );                 
312            dev_txt_init( &txt0_rx_chdev );
313
314            // register TXT_TX[0] & TXT_RX[0] in chdev_dir[x][y]
315            // for all valid clusters             
316            for( x = 0 ; x < info->x_size ; x++ )
317            {
318                for( y = 0 ; y < info->y_size ; y++ )
319                {
320                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
321
322                    if( cluster_is_active( cxy ) )
323                    {
324                        hal_remote_s64( XPTR( cxy , &chdev_dir.txt_tx[0] ) ,
325                                        XPTR( local_cxy , &txt0_tx_chdev ) );
326                        hal_remote_s64( XPTR( cxy , &chdev_dir.txt_rx[0] ) ,
327                                        XPTR( local_cxy , &txt0_rx_chdev ) );
328                    }
329                }
330            }
331
332            hal_fence();
333        }
334        } // end loop on devices
335}  // end txt0_device_init()
336
337///////////////////////////////////////////////////////////////////////////////////////////
338// This function allocates memory and initializes the chdev descriptors for the internal
339// peripherals contained in the local cluster, other than the LAPIC, as specified by
340// the boot_info, including the linking with the driver for the specified implementation.
341// The relevant entries in all copies of the devices directory are initialised.
342///////////////////////////////////////////////////////////////////////////////////////////
343// @ info    : pointer on the local boot-info structure.
344///////////////////////////////////////////////////////////////////////////////////////////
345static void __attribute__ ((noinline)) internal_devices_init( boot_info_t * info )
346{
347    boot_device_t * dev_tbl;         // pointer on array of internaldevices in boot_info
348        uint32_t        dev_nr;          // actual number of devices in this cluster
349        xptr_t          base;            // remote pointer on segment base
350    uint32_t        func;            // device functionnal index
351    uint32_t        impl;            // device implementation index
352        uint32_t        i;               // device index in dev_tbl
353        uint32_t        x;               // X cluster coordinate
354        uint32_t        y;               // Y cluster coordinate
355        uint32_t        channels;        // number of channels
356        uint32_t        channel;         // channel index
357        chdev_t       * chdev_ptr;       // local pointer on created chdev
358
359    // get number of internal peripherals and base from boot_info
360        dev_nr  = info->int_dev_nr;
361    dev_tbl = info->int_dev;
362
363    // loop on internal peripherals
364        for( i = 0 ; i < dev_nr ; i++ )
365        {
366        base        = dev_tbl[i].base;
367        channels    = dev_tbl[i].channels;
368        func        = FUNC_FROM_TYPE( dev_tbl[i].type );
369        impl        = IMPL_FROM_TYPE( dev_tbl[i].type );
370 
371        //////////////////////////
372        if( func == DEV_FUNC_MMC ) 
373        {
374
375            // check channels
376            if( channels != 1 )
377            printk("\n[PANIC] in %s : MMC device must be single channel\n", __FUNCTION__ );
378
379            // create chdev in local cluster
380            chdev_ptr = chdev_create( func,
381                                      impl,
382                                      0,          // channel
383                                      false,      // direction
384                                      base );
385
386            // check memory
387            if( chdev_ptr == NULL )
388            printk("\n[PANIC] in %s : cannot create MMC chdev\n", __FUNCTION__ );
389           
390            // make MMC specific initialisation
391            dev_mmc_init( chdev_ptr );
392
393            // set the MMC field in all chdev_dir[x][y] structures
394            for( x = 0 ; x < info->x_size ; x++ )
395            {
396                for( y = 0 ; y < info->y_size ; y++ )
397                {
398                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
399
400                    if( cluster_is_active( cxy ) )
401                    {
402                        hal_remote_s64( XPTR( cxy , &chdev_dir.mmc[local_cxy] ), 
403                                        XPTR( local_cxy , chdev_ptr ) );
404                    }
405                }
406            }
407
408#if( DEBUG_KERNEL_INIT & 0x1 )
409if( hal_time_stamp() > DEBUG_KERNEL_INIT )
410printk("\n[DBG] %s : created MMC in cluster %x / chdev = %x\n",
411__FUNCTION__ , local_cxy , chdev_ptr );
412#endif
413        }
414        ///////////////////////////////
415        else if( func == DEV_FUNC_DMA )
416        {
417            // create one chdev per channel in local cluster
418            for( channel = 0 ; channel < channels ; channel++ )
419            {   
420                // create chdev[channel] in local cluster
421                chdev_ptr = chdev_create( func,
422                                          impl,
423                                          channel,
424                                          false,     // direction
425                                          base );
426
427                // check memory
428                if( chdev_ptr == NULL )
429                printk("\n[PANIC] in %s : cannot create DMA chdev\n", __FUNCTION__ );
430           
431                // make DMA specific initialisation
432                dev_dma_init( chdev_ptr );     
433
434                // initialize only the DMA[channel] field in the local chdev_dir[x][y]
435                // structure because the DMA device is not remotely accessible.
436                chdev_dir.dma[channel] = XPTR( local_cxy , chdev_ptr );
437
438#if( DEBUG_KERNEL_INIT & 0x1 )
439if( hal_time_stamp() > DEBUG_KERNEL_INIT )
440printk("\n[DBG] %s : created DMA[%d] in cluster %x / chdev = %x\n",
441__FUNCTION__ , channel , local_cxy , chdev_ptr );
442#endif
443            }
444        }
445    }
446}  // end internal_devices_init()
447
448///////////////////////////////////////////////////////////////////////////////////////////
449// This function allocates memory and initializes the chdev descriptors for the 
450// external (shared) peripherals other than the IOPIC, as specified by the boot_info.
451// This includes the dynamic linking with the driver for the specified implementation.
452// These chdev descriptors are distributed on all clusters, using a modulo on a global
453// index, identically computed in all clusters.
454// This function is executed in all clusters by the CP0 core, that computes a global index
455// for all external chdevs. Each CP0 core creates only the chdevs that must be placed in
456// the local cluster, because the global index matches the local index.
457// The relevant entries in all copies of the devices directory are initialised.
458///////////////////////////////////////////////////////////////////////////////////////////
459// @ info    : pointer on the local boot-info structure.
460///////////////////////////////////////////////////////////////////////////////////////////
461static void external_devices_init( boot_info_t * info )
462{
463    boot_device_t * dev_tbl;         // pointer on array of external devices in boot_info
464        uint32_t        dev_nr;          // actual number of external devices
465        xptr_t          base;            // remote pointer on segment base
466    uint32_t        func;            // device functionnal index
467    uint32_t        impl;            // device implementation index
468        uint32_t        i;               // device index in dev_tbl
469        uint32_t        x;               // X cluster coordinate
470        uint32_t        y;               // Y cluster coordinate
471        uint32_t        channels;        // number of channels
472        uint32_t        channel;         // channel index
473        uint32_t        directions;      // number of directions (1 or 2)
474        uint32_t        rx;              // direction index (0 or 1)
475    chdev_t       * chdev;           // local pointer on one channel_device descriptor
476    uint32_t        ext_chdev_gid;   // global index of external chdev
477
478    // get number of peripherals and base of devices array from boot_info
479    dev_nr      = info->ext_dev_nr;
480    dev_tbl     = info->ext_dev;
481
482    // initializes global index (PIC is already placed in cluster 0
483    ext_chdev_gid = 1;
484
485    // loop on external peripherals
486    for( i = 0 ; i < dev_nr ; i++ )
487    {
488        base     = dev_tbl[i].base;
489        channels = dev_tbl[i].channels;
490        func     = FUNC_FROM_TYPE( dev_tbl[i].type );
491        impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
492
493        // There is one chdev per direction for NIC and for TXT
494        if((func == DEV_FUNC_NIC) || (func == DEV_FUNC_TXT)) directions = 2;
495        else                                                 directions = 1;
496
497        // do nothing for ROM, that does not require a device descriptor.
498        if( func == DEV_FUNC_ROM ) continue;
499
500        // do nothing for PIC, that is already initialized
501        if( func == DEV_FUNC_PIC ) continue;
502
503        // check PIC device initialized
504        if( chdev_dir.pic == XPTR_NULL )
505        printk("\n[PANIC] in %s : PIC device must be initialized first\n", __FUNCTION__ );
506
507        // check external device functionnal type
508        if( (func != DEV_FUNC_IOB) && (func != DEV_FUNC_IOC) && (func != DEV_FUNC_TXT) &&
509            (func != DEV_FUNC_NIC) && (func != DEV_FUNC_FBF) )
510        printk("\n[PANIC] in %s : undefined peripheral type\n", __FUNCTION__ );
511
512        // loops on channels
513        for( channel = 0 ; channel < channels ; channel++ )
514        {
515            // loop on directions
516            for( rx = 0 ; rx < directions ; rx++ )
517            {
518                // skip TXT0 that has already been initialized
519                if( (func == DEV_FUNC_TXT) && (channel == 0) ) continue;
520
521                // all kernel instances compute the target cluster for all chdevs,
522                // computing the global index ext_chdev_gid[func,channel,direction]
523                cxy_t target_cxy;
524                while( 1 )
525                {
526                    uint32_t offset     = ext_chdev_gid % ( info->x_size * info->y_size );
527                    uint32_t x          = offset / info->y_size;
528                    uint32_t y          = offset % info->y_size;
529
530                    target_cxy = HAL_CXY_FROM_XY( x , y );
531
532                    // exit loop if target cluster is active
533                    if( cluster_is_active( target_cxy ) ) break;
534               
535                    // increment global index otherwise
536                    ext_chdev_gid++;
537                }
538
539                // allocate and initialize a local chdev
540                // when local cluster matches target cluster
541                if( target_cxy == local_cxy )
542                {
543                    chdev = chdev_create( func,
544                                          impl,
545                                          channel,
546                                          rx,          // direction
547                                          base );
548
549                    if( chdev == NULL )
550                    printk("\n[PANIC] in %s : cannot allocate chdev for external device\n",
551                    __FUNCTION__ );
552
553                    // make device type specific initialisation
554                    if     ( func == DEV_FUNC_IOB ) dev_iob_init( chdev );
555                    else if( func == DEV_FUNC_IOC ) dev_ioc_init( chdev );
556                    else if( func == DEV_FUNC_TXT ) dev_txt_init( chdev );
557                    else if( func == DEV_FUNC_NIC ) dev_nic_init( chdev );
558                    else if( func == DEV_FUNC_FBF ) dev_fbf_init( chdev );
559
560                    // all external (shared) devices are remotely accessible
561                    // initialize the replicated chdev_dir[x][y] structures
562                    // defining the extended pointers on chdev descriptors
563                    xptr_t * entry;
564
565                    if(func==DEV_FUNC_IOB             ) entry  = &chdev_dir.iob;
566                    if(func==DEV_FUNC_IOC             ) entry  = &chdev_dir.ioc[channel];
567                    if(func==DEV_FUNC_FBF             ) entry  = &chdev_dir.fbf[channel];
568                    if((func==DEV_FUNC_TXT) && (rx==0)) entry  = &chdev_dir.txt_tx[channel];
569                    if((func==DEV_FUNC_TXT) && (rx==1)) entry  = &chdev_dir.txt_rx[channel];
570                    if((func==DEV_FUNC_NIC) && (rx==0)) entry  = &chdev_dir.nic_tx[channel];
571                    if((func==DEV_FUNC_NIC) && (rx==1)) entry  = &chdev_dir.nic_rx[channel];
572
573                    for( x = 0 ; x < info->x_size ; x++ )
574                    {
575                        for( y = 0 ; y < info->y_size ; y++ )
576                        {
577                            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
578
579                            if( cluster_is_active( cxy ) )
580                            {
581                                hal_remote_s64( XPTR( cxy , entry ),
582                                                XPTR( local_cxy , chdev ) );
583                            }
584                        }
585                    }
586
587#if( DEBUG_KERNEL_INIT & 0x1 )
588if( hal_time_stamp() > DEBUG_KERNEL_INIT )
589printk("\n[DBG] %s : create chdev %s / channel = %d / rx = %d / cluster %x / chdev = %x\n",
590__FUNCTION__ , chdev_func_str( func ), channel , rx , local_cxy , chdev );
591#endif
592                }  // end if match
593
594                // increment chdev global index (matching or not)
595                ext_chdev_gid++;
596
597            } // end loop on directions
598        }  // end loop on channels
599        } // end loop on devices
600}  // end external_devices_init()
601
602///////////////////////////////////////////////////////////////////////////////////////////
603// This function is called by CP0 in cluster 0 to allocate memory and initialize the PIC
604// device, namely the informations attached to the external IOPIC controller, that
605// must be replicated in all clusters (struct iopic_input).
606// This initialisation must be done before other devices initialisation because the IRQ
607// routing infrastructure is required for both internal and external devices init.
608///////////////////////////////////////////////////////////////////////////////////////////
609// @ info    : pointer on the local boot-info structure.
610///////////////////////////////////////////////////////////////////////////////////////////
611static void __attribute__ ((noinline)) iopic_init( boot_info_t * info )
612{
613    boot_device_t * dev_tbl;         // pointer on boot_info external devices array
614        uint32_t        dev_nr;          // actual number of external devices
615        xptr_t          base;            // remote pointer on segment base
616    uint32_t        func;            // device functionnal index
617    uint32_t        impl;            // device implementation index
618        uint32_t        i;               // device index in dev_tbl
619    uint32_t        x;               // cluster X coordinate
620    uint32_t        y;               // cluster Y coordinate
621    bool_t          found;           // IOPIC found
622        chdev_t       * chdev;           // pointer on PIC chdev descriptor
623
624    // get number of external peripherals and base of array from boot_info
625        dev_nr      = info->ext_dev_nr;
626    dev_tbl     = info->ext_dev;
627
628    // avoid GCC warning
629    base        = XPTR_NULL;
630    impl        = 0;
631
632    // loop on external peripherals to get the IOPIC 
633        for( i = 0 , found = false ; i < dev_nr ; i++ )
634        {
635        func = FUNC_FROM_TYPE( dev_tbl[i].type );
636
637        if( func == DEV_FUNC_PIC )
638        {
639            base     = dev_tbl[i].base;
640            impl     = IMPL_FROM_TYPE( dev_tbl[i].type );
641            found    = true;
642            break;
643        }
644    }
645
646    // check PIC existence
647    if( found == false )
648    printk("\n[PANIC] in %s : PIC device not found\n", __FUNCTION__ );
649
650    // allocate and initialize the PIC chdev in cluster 0
651    chdev = chdev_create( DEV_FUNC_PIC,
652                          impl,
653                          0,      // channel
654                          0,      // direction,
655                          base );
656
657    // check memory
658    if( chdev == NULL )
659    printk("\n[PANIC] in %s : no memory for PIC chdev\n", __FUNCTION__ );
660
661    // make PIC device type specific initialisation
662    dev_pic_init( chdev );
663
664    // register, in all clusters, the extended pointer
665    // on PIC chdev in "chdev_dir" array
666    xptr_t * entry = &chdev_dir.pic;   
667               
668    for( x = 0 ; x < info->x_size ; x++ )
669    {
670        for( y = 0 ; y < info->y_size ; y++ )
671        {
672            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
673
674            if( cluster_is_active( cxy ) )
675            {
676                hal_remote_s64( XPTR( cxy , entry ) , 
677                                XPTR( local_cxy , chdev ) );
678            }
679        }
680    }
681
682    // initialize, in all clusters, the "iopic_input" structure
683    // defining how external IRQs are connected to IOPIC
684
685    // register default value for unused inputs
686    for( x = 0 ; x < info->x_size ; x++ )
687    {
688        for( y = 0 ; y < info->y_size ; y++ )
689        {
690            cxy_t cxy = HAL_CXY_FROM_XY( x , y );
691
692            if( cluster_is_active( cxy ) )
693            {
694                hal_remote_memset( XPTR( cxy , &iopic_input ), 
695                                   0xFF , sizeof(iopic_input_t) );
696            }
697        }
698    }
699
700    // register input IRQ index for valid inputs
701    uint32_t   id;         // input IRQ index
702    uint8_t    valid;      // input IRQ is connected
703    uint32_t   type;       // source device type
704    uint8_t    channel;    // source device channel
705    uint8_t    is_rx;      // source device direction
706    uint32_t * ptr;        // local pointer on one field in iopic_input stucture
707
708    for( id = 0 ; id < CONFIG_MAX_EXTERNAL_IRQS ; id++ )
709    {
710        valid   = dev_tbl[i].irq[id].valid;
711        type    = dev_tbl[i].irq[id].dev_type;
712        channel = dev_tbl[i].irq[id].channel;
713        is_rx   = dev_tbl[i].irq[id].is_rx;
714        func    = FUNC_FROM_TYPE( type );
715
716        // get pointer on relevant field in iopic_input
717        if( valid )
718        {
719            if     ( func == DEV_FUNC_IOC )                 ptr = &iopic_input.ioc[channel]; 
720            else if((func == DEV_FUNC_TXT) && (is_rx == 0)) ptr = &iopic_input.txt_tx[channel];
721            else if((func == DEV_FUNC_TXT) && (is_rx != 0)) ptr = &iopic_input.txt_rx[channel];
722            else if((func == DEV_FUNC_NIC) && (is_rx == 0)) ptr = &iopic_input.nic_tx[channel];
723            else if((func == DEV_FUNC_NIC) && (is_rx != 0)) ptr = &iopic_input.nic_rx[channel];
724            else if( func == DEV_FUNC_IOB )                 ptr = &iopic_input.iob;
725            else     printk("\n[PANIC] in %s : illegal source device for IOPIC input" );
726
727            // set one entry in all "iopic_input" structures
728            for( x = 0 ; x < info->x_size ; x++ )
729            {
730                for( y = 0 ; y < info->y_size ; y++ )
731                {
732                    cxy_t cxy = HAL_CXY_FROM_XY( x , y );
733
734                    if( cluster_is_active( cxy ) )
735                    {
736                        hal_remote_s64( XPTR( cxy , ptr ) , id ); 
737                    }
738                }
739            }
740        }
741    } 
742
743#if( DEBUG_KERNEL_INIT & 0x1 )
744if( hal_tim_stamp() > DEBUG_KERNEL_INIT )
745{
746    printk("\n[DBG] %s created PIC chdev in cluster %x at cycle %d\n",
747    __FUNCTION__ , local_cxy , (uint32_t)hal_time_stamp() );
748    dev_pic_inputs_display();
749}
750#endif
751   
752}  // end iopic_init()
753
754///////////////////////////////////////////////////////////////////////////////////////////
755// This function is called by all CP0s in all cluster to complete the PIC device
756// initialisation, namely the informations attached to the LAPIC controller.
757// This initialisation must be done after the IOPIC initialisation, but before other
758// devices initialisation because the IRQ routing infrastructure is required for both
759// internal and external devices initialisation.
760///////////////////////////////////////////////////////////////////////////////////////////
761// @ info    : pointer on the local boot-info structure.
762///////////////////////////////////////////////////////////////////////////////////////////
763static void __attribute__ ((noinline)) lapic_init( boot_info_t * info )
764{
765    boot_device_t * dev_tbl;      // pointer on boot_info internal devices array
766    uint32_t        dev_nr;       // number of internal devices
767    uint32_t        i;            // device index in dev_tbl
768        xptr_t          base;         // remote pointer on segment base
769    uint32_t        func;         // device functionnal type in boot_info
770    bool_t          found;        // LAPIC found
771
772    // get number of internal peripherals and base
773        dev_nr      = info->int_dev_nr;
774    dev_tbl     = info->int_dev;
775
776    // loop on internal peripherals to get the lapic device
777        for( i = 0 , found = false ; i < dev_nr ; i++ )
778        {
779        func = FUNC_FROM_TYPE( dev_tbl[i].type );
780
781        if( func == DEV_FUNC_ICU )
782        {
783            base     = dev_tbl[i].base;
784            found    = true;
785            break;
786        }
787    }
788
789    // if the LAPIC controller is not defined in the boot_info,
790    // we simply don't initialize the PIC extensions in the kernel,
791    // making the assumption that the LAPIC related informations
792    // are hidden in the hardware specific PIC driver.
793    if( found )
794    {
795        // initialise the PIC extensions for
796        // the core descriptor and core manager extensions
797        dev_pic_extend_init( (uint32_t *)GET_PTR( base ) );
798
799        // initialize the "lapic_input" structure
800        // defining how internal IRQs are connected to LAPIC
801        uint32_t        id;
802        uint8_t         valid;
803        uint8_t         channel;
804        uint32_t        func;
805
806        for( id = 0 ; id < CONFIG_MAX_INTERNAL_IRQS ; id++ )
807        {
808            valid    = dev_tbl[i].irq[id].valid;
809            func     = FUNC_FROM_TYPE( dev_tbl[i].irq[id].dev_type );
810            channel  = dev_tbl[i].irq[id].channel;
811
812            if( valid ) // only valid local IRQs are registered
813            {
814                if     ( func == DEV_FUNC_MMC ) lapic_input.mmc = id;
815                else if( func == DEV_FUNC_DMA ) lapic_input.dma[channel] = id;
816                else     printk("\n[PANIC] in %s : illegal source device for LAPIC input" );
817            }
818        }
819    }
820}  // end lapic_init()
821
822///////////////////////////////////////////////////////////////////////////////////////////
823// This static function returns the identifiers of the calling core.
824///////////////////////////////////////////////////////////////////////////////////////////
825// @ info    : pointer on boot_info structure.
826// @ lid     : [out] core local index in cluster.
827// @ cxy     : [out] cluster identifier.
828// @ lid     : [out] core global identifier (hardware).
829// @ return 0 if success / return EINVAL if not found.
830///////////////////////////////////////////////////////////////////////////////////////////
831static error_t __attribute__ ((noinline)) get_core_identifiers( boot_info_t * info,
832                                                                lid_t       * lid,
833                                                                cxy_t       * cxy,
834                                                                gid_t       * gid )
835{
836    uint32_t   i;
837    gid_t      global_id;
838
839    // get global identifier from hardware register
840    global_id = hal_get_gid();
841
842    // makes an associative search in boot_info to get (cxy,lid) from global_id
843    for( i = 0 ; i < info->cores_nr ; i++ )
844    {
845        if( global_id == info->core[i].gid )
846        {
847            *lid = info->core[i].lid;
848            *cxy = info->core[i].cxy;
849            *gid = global_id;
850            return 0;
851        }
852    }
853    return EINVAL;
854}
855
856///////////////////////////////////////////////////////////////////////////////////////////
857// This function is the entry point for the kernel initialisation.
858// It is executed by all cores in all clusters, but only core[0], called CP0,
859// initializes the shared resources such as the cluster manager, or the local peripherals.
860// To comply with the multi-kernels paradigm, it accesses only local cluster memory, using
861// only information contained in the local boot_info_t structure, set by the bootloader.
862// Only CP0 in cluster 0 print the log messages.
863///////////////////////////////////////////////////////////////////////////////////////////
864// @ info    : pointer on the local boot-info structure.
865///////////////////////////////////////////////////////////////////////////////////////////
866void kernel_init( boot_info_t * info )
867{
868    lid_t        core_lid = -1;             // running core local index
869    cxy_t        core_cxy = -1;             // running core cluster identifier
870    gid_t        core_gid;                  // running core hardware identifier
871    cluster_t  * cluster;                   // pointer on local cluster manager
872    core_t     * core;                      // pointer on running core descriptor
873    thread_t   * thread;                    // pointer on idle thread descriptor
874
875    xptr_t       vfs_root_inode_xp;         // extended pointer on VFS root inode
876    xptr_t       devfs_dev_inode_xp;        // extended pointer on DEVFS dev inode   
877    xptr_t       devfs_external_inode_xp;   // extended pointer on DEVFS external inode       
878    xptr_t       devfs_internal_inode_xp;   // extended pointer on DEVFS internal inode       
879
880    error_t      error;
881    reg_t        status;                    // running core status register
882
883    /////////////////////////////////////////////////////////////////////////////////
884    // STEP 0 : Each core get its core identifier from boot_info, and makes
885    //          a partial initialisation of its private idle thread descriptor.
886    //          CP0 initializes the "local_cxy" global variable.
887    //          CP0 in cluster IO initializes the TXT0 chdev to print log messages.
888    /////////////////////////////////////////////////////////////////////////////////
889
890    error = get_core_identifiers( info,
891                                  &core_lid,
892                                  &core_cxy,
893                                  &core_gid );
894
895    // CP0 initializes cluster identifier
896    if( core_lid == 0 ) local_cxy = info->cxy;
897
898    // each core gets a pointer on its private idle thread descriptor
899    thread = (thread_t *)( idle_threads + (core_lid * CONFIG_THREAD_DESC_SIZE) );
900
901    // each core registers this thread pointer in hardware register
902    hal_set_current_thread( thread );
903
904    // each core register core descriptor pointer in idle thread descriptor
905    thread->core = &LOCAL_CLUSTER->core_tbl[core_lid];
906
907    // each core initializes the idle thread locks counters
908    thread->busylocks = 0;
909
910#if DEBUG_BUSYLOCK
911    // each core initialise the idle thread list of busylocks
912    xlist_root_init( XPTR( local_cxy , &thread->busylocks_root ) );
913#endif
914
915    // CP0 initializes cluster info
916    if( core_lid == 0 ) cluster_info_init( info );
917
918    // CP0 in cluster 0 initialises TXT0 chdev descriptor
919    if( (core_lid == 0) && (core_cxy == 0) ) txt0_device_init( info );
920
921    /////////////////////////////////////////////////////////////////////////////////
922    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
923                                        (info->x_size * info->y_size) );
924    barrier_wait( &local_barrier , info->cores_nr );
925    /////////////////////////////////////////////////////////////////////////////////
926
927#if DEBUG_KERNEL_INIT
928// if( (core_lid ==  0) & (local_cxy == 0) )
929printk("\n[DBG] %s : exit barrier 0 : TXT0 initialized / sr %x / cycle %d\n",
930__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
931#endif
932
933    /////////////////////////////////////////////////////////////////////////////
934    // STEP 1 : all cores check core identifier.
935    //          CP0 initializes the local cluster manager.
936    //          This includes the memory allocators.
937    /////////////////////////////////////////////////////////////////////////////
938
939    // all cores check identifiers
940    if( error )
941    printk("\n[PANIC] in %s : illegal core : gid %x / cxy %x / lid %d",
942    __FUNCTION__, core_lid, core_cxy, core_lid );
943
944    // CP0 initializes cluster manager complex structures
945    if( core_lid == 0 )
946    {
947        error = cluster_manager_init( info );
948
949        if( error )
950        printk("\n[PANIC] in %s : cannot initialize cluster manager in cluster %x\n",
951        __FUNCTION__, local_cxy );
952    }
953
954    /////////////////////////////////////////////////////////////////////////////////
955    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
956                                        (info->x_size * info->y_size) );
957    barrier_wait( &local_barrier , info->cores_nr );
958    /////////////////////////////////////////////////////////////////////////////////
959
960#if DEBUG_KERNEL_INIT
961if( (core_lid ==  0) & (local_cxy == 0) ) 
962printk("\n[DBG] %s : exit barrier 1 : clusters initialised / sr %x / cycle %d\n",
963__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
964#endif
965
966    /////////////////////////////////////////////////////////////////////////////////
967    // STEP 2 : CP0 initializes the process_zero descriptor.
968    //          CP0 in cluster 0 initializes the IOPIC device.
969    /////////////////////////////////////////////////////////////////////////////////
970
971    // all cores get pointer on local cluster manager & core descriptor
972    cluster = &cluster_manager;
973    core    = &cluster->core_tbl[core_lid];
974
975    // all CP0s initialize the process_zero descriptor
976    if( core_lid == 0 ) process_zero_create( &process_zero );
977
978    // CP0 in cluster 0 initializes the PIC chdev,
979    if( (core_lid == 0) && (local_cxy == 0) ) iopic_init( info );
980   
981    ////////////////////////////////////////////////////////////////////////////////
982    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
983                                        (info->x_size * info->y_size) );
984    barrier_wait( &local_barrier , info->cores_nr );
985    ////////////////////////////////////////////////////////////////////////////////
986
987#if DEBUG_KERNEL_INIT
988if( (core_lid ==  0) & (local_cxy == 0) ) 
989printk("\n[DBG] %s : exit barrier 2 : PIC initialised / sr %x / cycle %d\n",
990__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
991#endif
992
993    ////////////////////////////////////////////////////////////////////////////////
994    // STEP 3 : CP0 initializes the distibuted LAPIC descriptor.
995    //          CP0 initializes the internal chdev descriptors
996    //          CP0 initialize the local external chdev descriptors
997    ////////////////////////////////////////////////////////////////////////////////
998
999    // all CP0s initialize their local LAPIC extension,
1000    if( core_lid == 0 ) lapic_init( info );
1001
1002    // CP0 scan the internal (private) peripherals,
1003    // and allocates memory for the corresponding chdev descriptors.
1004    if( core_lid == 0 ) internal_devices_init( info );
1005       
1006
1007    // All CP0s contribute to initialise external peripheral chdev descriptors.
1008    // Each CP0[cxy] scan the set of external (shared) peripherals (but the TXT0),
1009    // and allocates memory for the chdev descriptors that must be placed
1010    // on the (cxy) cluster according to the global index value.
1011
1012    if( core_lid == 0 ) external_devices_init( info );
1013
1014    /////////////////////////////////////////////////////////////////////////////////
1015    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1016                                        (info->x_size * info->y_size) );
1017    barrier_wait( &local_barrier , info->cores_nr );
1018    /////////////////////////////////////////////////////////////////////////////////
1019
1020#if DEBUG_KERNEL_INIT
1021if( (core_lid ==  0) & (local_cxy == 0) ) 
1022printk("\n[DBG] %s : exit barrier 3 : all chdev initialised / sr %x / cycle %d\n",
1023__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
1024#endif
1025
1026#if( DEBUG_KERNEL_INIT & 1 )
1027if( (core_lid ==  0) & (local_cxy == 0) ) 
1028chdev_dir_display();
1029#endif
1030   
1031    /////////////////////////////////////////////////////////////////////////////////
1032    // STEP 4 : All cores enable IPI (Inter Procesor Interrupt),
1033    //          Alh cores initialize IDLE thread.
1034    //          Only CP0 in cluster 0 creates the VFS root inode.
1035    //          It access the boot device to initialize the file system context.
1036    /////////////////////////////////////////////////////////////////////////////////
1037
1038    // All cores enable IPI
1039    dev_pic_enable_ipi();
1040    hal_enable_irq( &status );
1041
1042    // all cores initialize the idle thread descriptor
1043    thread_idle_init( thread,
1044                      THREAD_IDLE,
1045                      &thread_idle_func,
1046                      NULL,
1047                      core_lid );
1048
1049    // all cores unblock idle thread, and register it in scheduler
1050    thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL );
1051    core->scheduler.idle = thread;
1052
1053#if( DEBUG_KERNEL_INIT & 1 )
1054sched_display( core_lid );
1055#endif
1056
1057    // CPO in cluster 0 creates the VFS root
1058    if( (core_lid ==  0) && (local_cxy == 0 ) ) 
1059    {
1060        vfs_root_inode_xp = XPTR_NULL;
1061
1062        // File System must be FATFS in this implementation,
1063        // but other File System can be introduced here
1064        if( CONFIG_VFS_ROOT_IS_FATFS )
1065        {
1066            // 1. allocate memory for FATFS context in cluster 0
1067            fatfs_ctx_t * fatfs_ctx = fatfs_ctx_alloc();
1068
1069            if( fatfs_ctx == NULL )
1070            printk("\n[PANIC] in %s : cannot create FATFS context in cluster 0\n",
1071            __FUNCTION__ );
1072
1073            // 2. access boot device to initialize FATFS context
1074            fatfs_ctx_init( fatfs_ctx );
1075 
1076            // 3. get various informations from FATFS context
1077            uint32_t root_dir_cluster = fatfs_ctx->root_dir_cluster;
1078            uint32_t cluster_size     = fatfs_ctx->bytes_per_sector * 
1079                                        fatfs_ctx->sectors_per_cluster;
1080            uint32_t total_clusters   = fatfs_ctx->fat_sectors_count << 7;
1081 
1082            // 4. initialize the FATFS entry in the vfs_context[] array
1083            vfs_ctx_init( FS_TYPE_FATFS,                               // fs type
1084                          0,                                           // attributes: unused
1085                              total_clusters,               
1086                              cluster_size,
1087                              vfs_root_inode_xp,                           // VFS root
1088                          fatfs_ctx );                                 // extend
1089
1090            // 5. create VFS root inode in cluster 0
1091            error = vfs_inode_create( XPTR_NULL,                           // dentry_xp
1092                                      FS_TYPE_FATFS,                       // fs_type
1093                                      INODE_TYPE_DIR,                      // inode_type
1094                                      (void *)(intptr_t)root_dir_cluster,  // extend
1095                                      0,                                   // attr
1096                                      0,                                   // rights
1097                                      0,                                   // uid
1098                                      0,                                   // gid
1099                                      &vfs_root_inode_xp );                // return
1100            if( error )
1101            printk("\n[PANIC] in %s : cannot create VFS root inode in cluster 0\n",
1102            __FUNCTION__ );
1103
1104            // 6. update the FATFS entry in vfs_context[] array
1105            fs_context[FS_TYPE_FATFS].vfs_root_xp = vfs_root_inode_xp;
1106
1107            // 7. check FATFS initialization
1108            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1109
1110            if( ((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster != 8 )
1111            printk("\n[PANIC] in %s : illegal FATFS context in cluster 0\n",
1112            __FUNCTION__ );
1113        }
1114        else
1115        {
1116            printk("\n[PANIC] in %s : unsupported VFS type in cluster 0\n",
1117            __FUNCTION__ );
1118        }
1119
1120        // register VFS root inode in process_zero descriptor of cluster 0
1121        process_zero.vfs_root_xp = vfs_root_inode_xp;
1122        process_zero.vfs_cwd_xp  = vfs_root_inode_xp;
1123    }
1124
1125    /////////////////////////////////////////////////////////////////////////////////
1126    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1127                                        (info->x_size * info->y_size) );
1128    barrier_wait( &local_barrier , info->cores_nr );
1129    /////////////////////////////////////////////////////////////////////////////////
1130
1131#if DEBUG_KERNEL_INIT
1132if( (core_lid ==  0) & (local_cxy == 0) ) 
1133printk("\n[DBG] %s : exit barrier 4 : VFS root initialized in cluster 0 / sr %x / cycle %d\n",
1134__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
1135#endif
1136
1137    /////////////////////////////////////////////////////////////////////////////////
1138    // STEP 5 : Other CP0s allocate memory for the selected FS context,
1139    //          and initialise both the local FS context and the local VFS context
1140    //          from values stored in cluster 0.
1141    //          They get the VFS root inode extended pointer from cluster 0.
1142    /////////////////////////////////////////////////////////////////////////////////
1143
1144    if( (core_lid ==  0) && (local_cxy != 0) ) 
1145    {
1146        // File System must be FATFS in this implementation,
1147        // but other File System can be introduced here
1148        if( CONFIG_VFS_ROOT_IS_FATFS )
1149        {
1150            // 1. allocate memory for local FATFS context
1151            fatfs_ctx_t * local_fatfs_ctx = fatfs_ctx_alloc();
1152
1153            // check memory
1154            if( local_fatfs_ctx == NULL )
1155            printk("\n[PANIC] in %s : cannot create FATFS context in cluster %x\n",
1156            __FUNCTION__ , local_cxy );
1157
1158            // 2. get local pointer on VFS context for FATFS
1159            vfs_ctx_t   * vfs_ctx = &fs_context[FS_TYPE_FATFS];
1160
1161            // 3. get local pointer on FATFS context in cluster 0
1162            fatfs_ctx_t * remote_fatfs_ctx = hal_remote_lpt( XPTR( 0 , &vfs_ctx->extend ) );
1163
1164            // 4. copy FATFS context from cluster 0 to local cluster
1165            hal_remote_memcpy( XPTR( local_cxy , local_fatfs_ctx ), 
1166                               XPTR( 0 ,         remote_fatfs_ctx ), sizeof(fatfs_ctx_t) );
1167
1168            // 5. copy VFS context from cluster 0 to local cluster
1169            hal_remote_memcpy( XPTR( local_cxy , vfs_ctx ), 
1170                               XPTR( 0 ,         vfs_ctx ), sizeof(vfs_ctx_t) );
1171
1172            // 6. update extend field in local copy of VFS context
1173            vfs_ctx->extend = local_fatfs_ctx;
1174
1175            if( ((fatfs_ctx_t *)vfs_ctx->extend)->sectors_per_cluster != 8 )
1176            printk("\n[PANIC] in %s : illegal FATFS context in cluster %x\n",
1177            __FUNCTION__ , local_cxy );
1178        }
1179
1180        // get extended pointer on VFS root inode from cluster 0
1181        vfs_root_inode_xp = hal_remote_l64( XPTR( 0 , &process_zero.vfs_root_xp ) );
1182
1183        // update local process_zero descriptor
1184        process_zero.vfs_root_xp = vfs_root_inode_xp;
1185        process_zero.vfs_cwd_xp  = vfs_root_inode_xp;
1186    }
1187
1188    /////////////////////////////////////////////////////////////////////////////////
1189    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1190                                        (info->x_size * info->y_size) );
1191    barrier_wait( &local_barrier , info->cores_nr );
1192    /////////////////////////////////////////////////////////////////////////////////
1193
1194#if DEBUG_KERNEL_INIT
1195if( (core_lid ==  0) & (local_cxy == 1) ) 
1196printk("\n[DBG] %s : exit barrier 5 : VFS root initialized in cluster 1 / sr %x / cycle %d\n",
1197__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
1198#endif
1199
1200    /////////////////////////////////////////////////////////////////////////////////
1201    // STEP 6 : CP0 in cluster 0 makes the global DEVFS tree initialisation:
1202    //          It initializes the DEVFS context, and creates the DEVFS
1203    //          "dev" and "external" inodes in cluster 0.
1204    /////////////////////////////////////////////////////////////////////////////////
1205
1206    if( (core_lid ==  0) && (local_cxy == 0) ) 
1207    {
1208        // 1. allocate memory for DEVFS context extension in cluster 0
1209        devfs_ctx_t * devfs_ctx = devfs_ctx_alloc();
1210
1211        if( devfs_ctx == NULL )
1212        printk("\n[PANIC] in %s : cannot create DEVFS context in cluster 0\n",
1213        __FUNCTION__ , local_cxy );
1214
1215        // 2. initialize the DEVFS entry in the vfs_context[] array
1216        vfs_ctx_init( FS_TYPE_DEVFS,                                // fs type
1217                      0,                                            // attributes: unused
1218                          0,                                            // total_clusters: unused
1219                          0,                                            // cluster_size: unused
1220                          vfs_root_inode_xp,                            // VFS root
1221                      devfs_ctx );                                  // extend
1222
1223        // 3. create "dev" and "external" inodes (directories)
1224        devfs_global_init( process_zero.vfs_root_xp,
1225                           &devfs_dev_inode_xp,
1226                           &devfs_external_inode_xp );
1227
1228        // 4. initializes DEVFS context extension
1229        devfs_ctx_init( devfs_ctx,
1230                        devfs_dev_inode_xp,
1231                        devfs_external_inode_xp );
1232    }   
1233
1234    /////////////////////////////////////////////////////////////////////////////////
1235    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1236                                        (info->x_size * info->y_size) );
1237    barrier_wait( &local_barrier , info->cores_nr );
1238    /////////////////////////////////////////////////////////////////////////////////
1239
1240#if DEBUG_KERNEL_INIT
1241if( (core_lid ==  0) & (local_cxy == 0) ) 
1242printk("\n[DBG] %s : exit barrier 6 : DEVFS root initialized in cluster 0 / sr %x / cycle %d\n",
1243__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
1244#endif
1245
1246    /////////////////////////////////////////////////////////////////////////////////
1247    // STEP 7 : All CP0s complete in parallel the DEVFS tree initialization.
1248    //          Each CP0 get the "dev" and "external" extended pointers from
1249    //          values stored in cluster 0.
1250    //          Then each CP0 in cluster(i) creates the DEVFS "internal" directory,
1251    //          and creates the pseudo-files for all chdevs in cluster (i).
1252    /////////////////////////////////////////////////////////////////////////////////
1253
1254    if( core_lid == 0 )
1255    {
1256        // get extended pointer on "extend" field of VFS context for DEVFS in cluster 0
1257        xptr_t  extend_xp = XPTR( 0 , &fs_context[FS_TYPE_DEVFS].extend );
1258
1259        // get pointer on DEVFS context in cluster 0
1260        devfs_ctx_t * devfs_ctx = hal_remote_lpt( extend_xp );
1261       
1262        devfs_dev_inode_xp      = hal_remote_l64( XPTR( 0 , &devfs_ctx->dev_inode_xp ) );
1263        devfs_external_inode_xp = hal_remote_l64( XPTR( 0 , &devfs_ctx->external_inode_xp ) );
1264
1265        // populate DEVFS in all clusters
1266        devfs_local_init( devfs_dev_inode_xp,
1267                          devfs_external_inode_xp,
1268                          &devfs_internal_inode_xp );
1269    }
1270
1271    /////////////////////////////////////////////////////////////////////////////////
1272    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ), 
1273                                        (info->x_size * info->y_size) );
1274    barrier_wait( &local_barrier , info->cores_nr );
1275    /////////////////////////////////////////////////////////////////////////////////
1276
1277#if DEBUG_KERNEL_INIT
1278if( (core_lid ==  0) & (local_cxy == 0) ) 
1279printk("\n[DBG] %s : exit barrier 7 : DEV initialized in cluster 0 / sr %x / cycle %d\n",
1280__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
1281#endif
1282
1283    /////////////////////////////////////////////////////////////////////////////////
1284    // STEP 8 : CP0 in cluster 0 creates the first user process (process_init)
1285    /////////////////////////////////////////////////////////////////////////////////
1286
1287    if( (core_lid == 0) && (local_cxy == 0) ) 
1288    {
1289
1290#if( DEBUG_KERNEL_INIT & 1 )
1291vfs_display( vfs_root_inode_xp );
1292#endif
1293
1294       process_init_create();
1295    }
1296
1297    /////////////////////////////////////////////////////////////////////////////////
1298    if( core_lid == 0 ) xbarrier_wait( XPTR( 0 , &global_barrier ),
1299                                        (info->x_size * info->y_size) );
1300    barrier_wait( &local_barrier , info->cores_nr );
1301    /////////////////////////////////////////////////////////////////////////////////
1302
1303#if DEBUG_KERNEL_INIT
1304if( (core_lid ==  0) & (local_cxy == 0) ) 
1305printk("\n[DBG] %s : exit barrier 8 : process init created / sr %x / cycle %d\n", 
1306__FUNCTION__, (uint32_t)hal_get_sr(), (uint32_t)hal_get_cycles() );
1307#endif
1308
1309#if (DEBUG_KERNEL_INIT & 1)
1310if( (core_lid ==  0) & (local_cxy == 0) ) 
1311sched_display( 0 );
1312#endif
1313
1314    /////////////////////////////////////////////////////////////////////////////////
1315    // STEP 9 : CP0 in cluster 0 print banner
1316    /////////////////////////////////////////////////////////////////////////////////
1317   
1318    if( (core_lid == 0) && (local_cxy == 0) ) 
1319    {
1320        print_banner( (info->x_size * info->y_size) , info->cores_nr );
1321
1322#if( DEBUG_KERNEL_INIT & 1 )
1323printk("\n\n***** memory fooprint for main kernel objects\n\n"
1324                   " - thread descriptor  : %d bytes\n"
1325                   " - process descriptor : %d bytes\n"
1326                   " - cluster manager    : %d bytes\n"
1327                   " - chdev descriptor   : %d bytes\n"
1328                   " - core descriptor    : %d bytes\n"
1329                   " - scheduler          : %d bytes\n"
1330                   " - rpc fifo           : %d bytes\n"
1331                   " - page descriptor    : %d bytes\n"
1332                   " - mapper root        : %d bytes\n"
1333                   " - ppm manager        : %d bytes\n"
1334                   " - kcm manager        : %d bytes\n"
1335                   " - khm manager        : %d bytes\n"
1336                   " - vmm manager        : %d bytes\n"
1337                   " - gpt root           : %d bytes\n"
1338                   " - list item          : %d bytes\n"
1339                   " - xlist item         : %d bytes\n"
1340                   " - busylock           : %d bytes\n"
1341                   " - remote busylock    : %d bytes\n"
1342                   " - queuelock          : %d bytes\n"
1343                   " - remote queuelock   : %d bytes\n"
1344                   " - rwlock             : %d bytes\n"
1345                   " - remote rwlock      : %d bytes\n",
1346                   sizeof( thread_t           ),
1347                   sizeof( process_t          ),
1348                   sizeof( cluster_t          ),
1349                   sizeof( chdev_t            ),
1350                   sizeof( core_t             ),
1351                   sizeof( scheduler_t        ),
1352                   sizeof( remote_fifo_t      ),
1353                   sizeof( page_t             ),
1354                   sizeof( mapper_t           ),
1355                   sizeof( ppm_t              ),
1356                   sizeof( kcm_t              ),
1357                   sizeof( khm_t              ),
1358                   sizeof( vmm_t              ),
1359                   sizeof( gpt_t              ),
1360                   sizeof( list_entry_t       ),
1361                   sizeof( xlist_entry_t      ),
1362                   sizeof( busylock_t         ),
1363                   sizeof( remote_busylock_t  ),
1364                   sizeof( queuelock_t        ),
1365                   sizeof( remote_queuelock_t ),
1366                   sizeof( rwlock_t           ),
1367                   sizeof( remote_rwlock_t    ));
1368#endif
1369
1370    }
1371
1372    // each core activates its private TICK IRQ
1373    dev_pic_enable_timer( CONFIG_SCHED_TICK_MS_PERIOD );
1374
1375#if DEBUG_KERNEL_INIT
1376printk("\n[DBG] %s : thread %x on core[%x,%d] jumps to thread_idle_func() / cycle %d\n",
1377__FUNCTION__ , CURRENT_THREAD , local_cxy , core_lid , (uint32_t)hal_get_cycles() );
1378#endif
1379
1380    // each core jump to thread_idle_func
1381    thread_idle_func();
1382}
1383
Note: See TracBrowser for help on using the repository browser.