source: trunk/kernel/kern/kernel_init.c

Last change on this file was 683, checked in by alain, 3 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

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